interpreter.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* Copyright 2019-2020 Canaan Inc.
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. #include <runtime/k210/interpreter.h>
  16. #if !NNCASE_TARGET_K210_SIMULATOR
  17. #include <sysctl.h>
  18. #endif
  19. using namespace nncase;
  20. using namespace nncase::runtime;
  21. using namespace nncase::runtime::k210;
  22. interpreter::interpreter()
  23. #if NNCASE_TARGET_K210_SIMULATOR
  24. : kpu_mem_(std::make_unique<uint8_t[]>(2 * 1024 * 1024))
  25. #endif
  26. {
  27. #if !NNCASE_TARGET_K210_SIMULATOR
  28. kpu->interrupt_clear.reg = 7;
  29. kpu->interrupt_mask.reg = 7;
  30. kpu->fifo_threshold.reg = 10 | (1 << 4);
  31. kpu->eight_bit_mode.reg = 1;
  32. plic_set_priority(IRQN_AI_INTERRUPT, 1);
  33. #endif
  34. }
  35. xtl::span<uint8_t> interpreter::memory_at(const memory_range &range) const noexcept
  36. {
  37. if (range.memory_type == mem_k210_kpu)
  38. {
  39. uintptr_t base =
  40. #if NNCASE_TARGET_K210_SIMULATOR
  41. (uintptr_t)kpu_mem_.get();
  42. #else
  43. (uintptr_t)AI_IO_BASE_ADDR;
  44. #endif
  45. return { reinterpret_cast<uint8_t *>(base + range.start), range.size };
  46. }
  47. return interpreter_base::memory_at(range);
  48. }
  49. #if !NNCASE_TARGET_K210_SIMULATOR
  50. interpreter::clock_t::time_point interpreter::get_now() const noexcept
  51. {
  52. auto micro = std::chrono::microseconds(sysctl_get_time_us());
  53. return clock_t::time_point(std::chrono::duration_cast<clock_t::duration>(micro));
  54. }
  55. #endif