diff --git a/flake.lock b/flake.lock index 960d1f6..247c2b9 100644 --- a/flake.lock +++ b/flake.lock @@ -128,11 +128,11 @@ ] }, "locked": { - "lastModified": 1714032877, - "narHash": "sha256-6KKe4xdHPQbGtM5icLHKxgcYyFKF47a98csTQG7vkvU=", + "lastModified": 1710242657, + "narHash": "sha256-tgaILHKZH6vC8P8N8NBMNQ/XIpTY72zMojlGLPLPLZk=", "ref": "refs/heads/master", - "rev": "f6acae14927a6df78ce2304007369095e140da9c", - "revCount": 149, + "rev": "6a499c8371c6a14d11a8c2fcc9f233e7b8e688a0", + "revCount": 148, "type": "git", "url": "https://git.xinyang.life/xin/nur.git" }, diff --git a/flake.nix b/flake.nix index 69dd4e3..0ea12c7 100644 --- a/flake.nix +++ b/flake.nix @@ -22,7 +22,6 @@ overlays = [ (self: super: { nvboard = nur-xin.legacyPackages.${system}.nvboard; - mini-gdbstub = nur-xin.legacyPackages.${system}.mini-gdbstub; }) ]; }; @@ -66,36 +65,23 @@ nativeBuildInputs = [ pkgs.cmake - pkgs.gcc # Generate expr tests ]; cmakeFlags = [ - (pkgs.lib.cmakeFeature "ARCH" "riscv-nemu") + (pkgs.lib.cmakeFeature "ISA" "riscv") + (pkgs.lib.cmakeFeature "PLATFORM" "nemu") ]; buildInputs = [ # SDL2 self.packages.${system}.abstract-machine ]; - - cmakeBuildType = "RelWithDebInfo"; - dontStrip = true; }; devShells.nemu = pkgs.mkShell { packages = with pkgs; [ clang-tools gdb - SDL2 - gnumake - pkg-config - flex - bison - dtc - - readline - libllvm - mini-gdbstub ]; inputsFrom = [ self.packages.${system}.nemu diff --git a/npc/CMakeLists.txt b/npc/CMakeLists.txt index 035d603..bb6cd95 100644 --- a/npc/CMakeLists.txt +++ b/npc/CMakeLists.txt @@ -1,7 +1,7 @@ cmake_minimum_required(VERSION 3.26) project(flow) -set(CMAKE_CXX_STANDARD 17) +set (CMAKE_CXX_STANDARD 17) cmake_policy(SET CMP0144 NEW) include(CMakeDependentOption) @@ -12,24 +12,17 @@ list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake) # -- Build options option(BUILD_USE_BLOOP "Whether to use bloop to speed up elaborate" OFF) option(BUILD_SIM_TARGET "Whether to build verilator simulation binary" ON) -cmake_dependent_option( - BUILD_SIM_NVBOARD_TARGET "Whether to build nvboard target" OFF - "BUILD_SIM_TARGET" OFF) +cmake_dependent_option(BUILD_SIM_NVBOARD_TARGET "Whether to build nvboard target" OFF "BUILD_SIM_TARGET" OFF) option(ENABLE_YSYX_GIT_TRACKER "Ysyx tracker support" ON) -set(TOPMODULE - "Flow" - CACHE STRING "Topmodule name in chisel") -set(DIFFTEST_LIB - "" - CACHE FILEPATH "Dynamic library file used as difftest reference") -set(DIFFTEST_RESOURCE_DIR - "" - CACHE PATH "Dynamic library file used as difftest reference") +set(TOPMODULE "Flow" CACHE STRING "Topmodule name in chisel") +set(DIFFTEST_LIB "" CACHE STRING "Dynamic library file used as difftest reference") # -- Ysyx tracker, configure if(ENABLE_YSYX_GIT_TRACKER) - execute_process(COMMAND ${CMAKE_SOURCE_DIR}/../git_commit.sh "configure(npc)" - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/..) + execute_process( + COMMAND ${CMAKE_SOURCE_DIR}/../git_commit.sh "configure(npc)" + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/.. + ) endif() # -- Check dependencies @@ -58,15 +51,13 @@ set(CHISEL_OUTPUT_VERILATOR_CONF ${CHISEL_OUTPUT_DIR}/conf.vlt) set(CHISEL_OUTPUT_TOPMODULE ${CHISEL_OUTPUT_DIR}/${TOPMODULE}.sv) set(CHISEL_EMIT_ARGS "--target-dir ${CHISEL_OUTPUT_DIR}") -# -- Find difftest binaries -file(GLOB_RECURSE DIFFTEST_BINARY_FILES "${DIFFTEST_RESOURCE_DIR}/*.bin") - # -- Build NVBoard executable if(BUILD_SIM_NVBOARD_TARGET) add_subdirectory(csrc_nvboard) endif() # -- Build Verilator executable and add to test +include_directories(include) add_subdirectory(utils) add_subdirectory(csrc) @@ -75,7 +66,7 @@ add_subdirectory(csrc) if(ENABLE_YSYX_GIT_TRACKER) add_custom_target( ysyx_git_tracer ALL - COMMAND ${CMAKE_SOURCE_DIR}/../git_commit.sh - "build_${CMAKE_PROJECT_NAME}_V${TOPMODULE}" - WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/..) + COMMAND ${CMAKE_SOURCE_DIR}/../git_commit.sh "build_${CMAKE_PROJECT_NAME}_V${TOPMODULE}" + WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/.. + ) endif() diff --git a/npc/core/src/main/resources/RamDpi.v b/npc/core/src/main/resources/RamDpi.v index cbab381..0f4308b 100644 --- a/npc/core/src/main/resources/RamDpi.v +++ b/npc/core/src/main/resources/RamDpi.v @@ -14,7 +14,7 @@ module RamDpi ( input reg [31:0] pc, output reg [31:0] inst ); - always @(posedge clock) begin + always @(*) begin if (valid) begin // 有读写请求时 readData = pmem_read(readAddr); if (writeEnable) begin // 有写请求时 @@ -22,8 +22,8 @@ module RamDpi ( end end else begin - readData = 32'h80000000; + readData = 0; end + inst = pmem_read(pc); end - assign inst = pmem_read(pc); -endmodule +endmodule \ No newline at end of file diff --git a/npc/core/src/main/scala/top/FlowMain.scala b/npc/core/src/main/scala/top/FlowMain.scala index 3377689..b6aa888 100644 --- a/npc/core/src/main/scala/top/FlowMain.scala +++ b/npc/core/src/main/scala/top/FlowMain.scala @@ -242,12 +242,12 @@ class Control(width: Int) extends RawModule { (xori , (r(true.B) :: r(rAluOut) :: r(false.B) :: r(pStaticNpc) :: - r(aOpXor) :: r(aSrcARs1) :: r(aSrcBImmI) :: l(Bool()) :: + r(aOpOr) :: r(aSrcARs1) :: r(aSrcBImmI) :: l(Bool()) :: r(false.B) :: l(UInt(4.W)):: r(false.B) :: HNil)), (ori , (r(true.B) :: r(rAluOut) :: r(false.B) :: r(pStaticNpc) :: - r(aOpOr) :: r(aSrcARs1) :: r(aSrcBImmI) :: l(Bool()) :: + r(aOpXor) :: r(aSrcARs1) :: r(aSrcBImmI) :: l(Bool()) :: r(false.B) :: l(UInt(4.W)):: r(false.B) :: HNil)), (andi , (r(true.B) :: r(rAluOut) :: diff --git a/npc/csrc/CMakeLists.txt b/npc/csrc/CMakeLists.txt index 4adbc86..3f645a7 100644 --- a/npc/csrc/CMakeLists.txt +++ b/npc/csrc/CMakeLists.txt @@ -1,2 +1 @@ -add_subdirectory(devices) -add_subdirectory(${TOPMODULE}) +add_subdirectory(${TOPMODULE}) \ No newline at end of file diff --git a/npc/csrc/Flow/CMakeLists.txt b/npc/csrc/Flow/CMakeLists.txt index 5ea7fce..16aaef1 100644 --- a/npc/csrc/Flow/CMakeLists.txt +++ b/npc/csrc/Flow/CMakeLists.txt @@ -1,29 +1,19 @@ include(ChiselBuild) add_executable(V${TOPMODULE} config.cpp main.cpp) -target_link_libraries(V${TOPMODULE} PRIVATE sdb devices) -target_include_directories(V${TOPMODULE} PRIVATE ${CMAKE_SOURCE_DIR}/include) +target_link_libraries(V${TOPMODULE} PRIVATE sdb) -verilate( - V${TOPMODULE} - TRACE - THREADS - TOP_MODULE - ${TOPMODULE} - PREFIX - V${TOPMODULE} - SOURCES - ${CHISEL_OUTPUT_TOPMODULE} - ${CHISEL_OUTPUT_VERILATOR_CONF} - INCLUDE_DIRS - ${CHISEL_OUTPUT_DIR} +verilate(V${TOPMODULE} TRACE THREADS + TOP_MODULE ${TOPMODULE} + PREFIX V${TOPMODULE} + SOURCES ${CHISEL_OUTPUT_TOPMODULE} ${CHISEL_OUTPUT_VERILATOR_CONF} + INCLUDE_DIRS ${CHISEL_OUTPUT_DIR} VERILATOR_ARGS - "--vpi" # Enable VPI - "-Wno-UNOPTFLAT") + "--vpi" # Enable VPI + "-Wno-UNOPTFLAT" +) -foreach(DIFFTEST_BINARY_FILE IN LISTS DIFFTEST_BINARY_FILES) - get_filename_component(FILENAME ${DIFFTEST_BINARY_FILE} NAME_WE) - add_test(NAME V${TOPMODULE}.${FILENAME} - COMMAND V${TOPMODULE} -m ${DIFFTEST_BINARY_FILE} --diff-lib - ${DIFFTEST_LIB}) - unset(FILENAME) -endforeach() +add_test( + NAME V${TOPMODULE} + COMMAND V${TOPMODULE} + --no-bin -m ${PROJECT_SOURCE_DIR}/resource/addi.txt + --diff-lib ${DIFFTEST_LIB}) diff --git a/npc/csrc/Flow/config.cpp b/npc/csrc/Flow/config.cpp index 71e042f..fd49141 100644 --- a/npc/csrc/Flow/config.cpp +++ b/npc/csrc/Flow/config.cpp @@ -8,7 +8,7 @@ void Config::cli_parse(int argc, char **argv) { app.add_flag("!--no-bin", memory_file_binary, "Memory file is in text format"); app.add_option("--wav", wavefile, "output .vcd file path"); - app.add_flag("-i", interactive, "Launch sdb for interactive session"); + app.add_option("-t", max_sim_time, "Max simulation timestep"); app.add_option("--diff-lib", lib_ref, "Dynamic library file of difftest reference") ->check(CLI::ExistingFile); diff --git a/npc/csrc/Flow/main.cpp b/npc/csrc/Flow/main.cpp index 324156f..664db56 100644 --- a/npc/csrc/Flow/main.cpp +++ b/npc/csrc/Flow/main.cpp @@ -1,15 +1,11 @@ #include "VFlow___024root.h" -#include "components.hpp" #include -#include #include #include #include -#include #include #include #include -#include #include #include #include @@ -29,38 +25,27 @@ VlModule *top; Registers *regs; vpiHandle pc = nullptr; -const size_t PMEM_START = 0x80000000; -const size_t PMEM_END = 0x87ffffff; - extern "C" { -using MMap = MemoryMap, Devices::DeviceMap>; void *pmem_get() { - static Devices::DeviceMap devices{ - new Devices::Serial(0x10000000, 0x1000), - new Devices::RTC(0x10001000, 0x1000), - }; - static auto pmem = new MemoryMap, Devices::DeviceMap>( - std::make_unique>( - config.memory_file, config.memory_file_binary, PMEM_START, PMEM_END), - std::make_unique(devices), config.mtrace_ranges); + static auto pmem = + new Memory(config.memory_file, config.memory_file_binary, + std::move(config.mtrace_ranges)); return pmem; } int pmem_read(int raddr) { void *pmem = pmem_get(); - auto mem = static_cast(pmem); + auto mem = static_cast *>(pmem); // TODO: Do memory difftest at memory read and write to diagnose at a finer // granularity if (config.do_mtrace) mem->trace(raddr, true, regs->get_pc()); - if (g_skip_memcheck) - return mem->read(PMEM_START); return mem->read(raddr); } void pmem_write(int waddr, int wdata, char wmask) { void *pmem = pmem_get(); - auto mem = static_cast(pmem); + auto mem = static_cast *>(pmem); if (config.do_mtrace) mem->trace((std::size_t)waddr, false, regs->get_pc(), wdata); return mem->write((std::size_t)waddr, wdata, wmask); @@ -70,7 +55,10 @@ void pmem_write(int waddr, int wdata, char wmask) { namespace NPC { void npc_memcpy(paddr_t addr, void *buf, size_t sz, bool direction) { if (direction == TRM_FROM_MACHINE) { - static_cast(pmem_get())->copy_to(addr, (uint8_t *)buf, sz); + memcpy( + buf, + static_cast *>(pmem_get())->guest_to_host(addr), + sz); } }; @@ -122,10 +110,7 @@ public: this->memcpy(addr, &buf, sizeof(word_t), TRM_FROM_MACHINE); return buf; } - void print(std::ostream &os) const override { - this->regcpy(cpu_state, TRM_FROM_MACHINE); - os << *(CPUState *)cpu_state << std::endl; - } + void print(std::ostream &os) const override {} }; DutTrmInterface npc_interface = @@ -141,50 +126,18 @@ word_t reg_str2val(const char *name, bool *success) { int main(int argc, char **argv, char **env) { config.cli_parse(argc, argv); - if (config.lib_ref.empty()) { - if (config.interactive) { - SDB::SDB sdb_npc{NPC::npc_interface}; - sdb_npc.main_loop(); - return 0; - } else { - NPC::npc_interface.init(0); - while (true) { - word_t inst = NPC::npc_interface.at(regs->get_pc()); - if (inst == 1048691) { - return 0; - } - NPC::npc_interface.exec(1); - } - } - } - - /* -- Difftest -- */ - std::filesystem::path ref{config.lib_ref}; - RefTrmInterface ref_interface{ref}; - DifftestTrmInterface diff_interface{ - NPC::npc_interface, ref_interface, - static_cast(pmem_get())->get_pmem(), 128 * 1024}; - if (config.interactive) { + if (config.max_sim_time > 1) { + NPC::npc_interface.exec(config.max_sim_time / 2); + } else { + /* -- Difftest -- */ + std::filesystem::path ref{config.lib_ref}; + RefTrmInterface ref_interface{ref}; + DifftestTrmInterface diff_interface{NPC::npc_interface, ref_interface, + pmem_get(), 128 * 1024}; SDB::SDB sdb_diff{diff_interface}; - sdb_diff.main_loop(); - return 0; - } - try { - diff_interface.init(0); - diff_interface.exec(-1); - } catch (TrmRuntimeException &e) { - switch (e.error_code()) { - case TrmRuntimeException::EBREAK: - return 0; - case TrmRuntimeException::DIFFTEST_FAILED: - std::cout << "Difftest Failed" << std::endl; - diff_interface.print(std::cout); - return 1; - default: - std::cerr << "Unknown error happened" << std::endl; - return 1; - } + int t = 8; + sdb_diff.main_loop(); } return 0; diff --git a/npc/csrc/devices/CMakeLists.txt b/npc/csrc/devices/CMakeLists.txt deleted file mode 100644 index fa33794..0000000 --- a/npc/csrc/devices/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -add_library(devices serial.cpp rtc.cpp) - -target_include_directories(devices PUBLIC include) diff --git a/npc/csrc/devices/include/devices.hpp b/npc/csrc/devices/include/devices.hpp deleted file mode 100644 index 4954ac0..0000000 --- a/npc/csrc/devices/include/devices.hpp +++ /dev/null @@ -1,85 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -namespace Devices { -class Device { -public: - uint8_t *p_buf; - uint64_t addr; - size_t len; - Device(uint64_t addr, size_t len, uint8_t buf[]) - : addr(addr), len(len), p_buf(buf) {} - virtual ~Device(){}; - virtual void io_handler(uint32_t offset, size_t len, bool is_write) = 0; - void transfer(uint8_t *src, size_t len, bool is_write) { - if (is_write) { - memmove(p_buf, src, len); - } else { - memmove(src, p_buf, len); - } - }; -}; - -class Serial : public Device { - uint8_t buf[1]; - -public: - Serial(uint64_t addr, size_t len); - ~Serial() override{}; - void io_handler(uint32_t offset, size_t len, bool is_write) override; - // void transfer(uint8_t *src, size_t len, bool is_write) override; -}; - -class RTC : public Device { - uint8_t buf[8]; - uint64_t boot_time; - uint64_t get_time_internal(); - uint64_t get_time(); - -public: - RTC(uint64_t addr, size_t len); - ~RTC() override{}; - void io_handler(uint32_t offset, size_t len, bool is_write) override; - // void transfer(uint8_t *src, size_t len, bool is_write) override; -}; - -class DeviceMap { - std::map addr_to_device; - -public: - DeviceMap(std::initializer_list devices) { - for (auto device : devices) { - addr_to_device.insert(std::make_pair(device->addr, device)); - } - } - bool handle(uint64_t addr, uint8_t *data, size_t len, bool is_write) { - auto it = addr_to_device.upper_bound(addr); - if (it == addr_to_device.begin() || - (--it)->second->addr + it->second->len <= addr) { - std::cerr << "Use of a unintialized device at memory addr: 0x" << std::hex - << addr << std::dec << std::endl; - return false; - } - auto &device = it->second; - uint32_t offset = addr - device->addr; - if (is_write) { - device->transfer(data, len, is_write); - device->io_handler(offset, len, is_write); - } else { - device->io_handler(offset, len, is_write); - device->transfer(data, len, is_write); - } - return true; - } -}; -} // namespace Devices diff --git a/npc/csrc/devices/rtc.cpp b/npc/csrc/devices/rtc.cpp deleted file mode 100644 index 1a26ab1..0000000 --- a/npc/csrc/devices/rtc.cpp +++ /dev/null @@ -1,49 +0,0 @@ -#include -#include - -namespace Devices { -uint64_t RTC::get_time_internal() { -#if defined(CONFIG_TARGET_AM) - uint64_t us = io_read(AM_TIMER_UPTIME).us; -#elif defined(CONFIG_TIMER_GETTIMEOFDAY) - struct timeval now; - gettimeofday(&now, NULL); - uint64_t us = now.tv_sec * 1000000 + now.tv_usec; -#else - struct timespec now; - clock_gettime(CLOCK_MONOTONIC_COARSE, &now); - uint64_t us = now.tv_sec * 1000000 + now.tv_nsec / 1000; -#endif - return us; -} - -uint64_t RTC::get_time() { - if (boot_time == 0) - boot_time = get_time_internal(); - uint64_t now = get_time_internal(); - return now - boot_time; -} - -RTC::RTC(uint64_t addr, size_t len) : Device(addr, len, buf) { - *(uint64_t *)buf = 0; -}; - -void RTC::io_handler(uint32_t offset, size_t len, bool is_write) { - assert(offset == 0 || offset == 4); - if (!is_write && offset == 4) { - uint64_t us = get_time(); - buf[0] = (uint32_t)us; - buf[1] = us >> 32; - } -} - -// void RTC::transfer(uint8_t *src, size_t len, bool is_write) { -// if (is_write) { -// for (size_t i = 0; i < len; i++) -// buf[i] = src[i]; -// } else { -// for (size_t i = 0; i < len; i++) -// src[i] = buf[i]; -// } -// } -} // namespace Devices diff --git a/npc/csrc/devices/serial.cpp b/npc/csrc/devices/serial.cpp deleted file mode 100644 index 8a09551..0000000 --- a/npc/csrc/devices/serial.cpp +++ /dev/null @@ -1,22 +0,0 @@ -#include -#include - -namespace Devices { -Serial::Serial(uint64_t addr, size_t len) : Device(addr, len, buf) { - buf[0] = 0; -}; -void Serial::io_handler(uint32_t offset, size_t len, bool is_write) { - if (is_write) { - std::cout << (char)buf[0]; - } -} -// void Serial::transfer(uint8_t *src, size_t len, bool is_write) { -// if (is_write) { -// for (size_t i = 0; i < len; i++) -// buf[i] = src[i]; -// } else { -// for (size_t i = 0; i < len; i++) -// src[i] = buf[i]; -// } -// } -} // namespace Devices diff --git a/npc/csrc_nvboard/Flow/CMakeLists.txt b/npc/csrc_nvboard/Flow/CMakeLists.txt index 6e3e1b3..82143bc 100644 --- a/npc/csrc_nvboard/Flow/CMakeLists.txt +++ b/npc/csrc_nvboard/Flow/CMakeLists.txt @@ -2,30 +2,22 @@ include(ChiselBuild) add_custom_command( OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/${TOPMODULE}/auto_bind.cpp - COMMAND auto_pin_bind ${CMAKE_SOURCE_DIR}/constr/${TOPMODULE}.nxdc - ${CMAKE_CURRENT_BINARY_DIR}/${TOPMODULE}/auto_bind.cpp - DEPENDS ${CMAKE_SOURCE_DIR}/constr/${TOPMODULE}.nxdc) + COMMAND auto_pin_bind ${CMAKE_SOURCE_DIR}/constr/${TOPMODULE}.nxdc ${CMAKE_CURRENT_BINARY_DIR}/${TOPMODULE}/auto_bind.cpp + DEPENDS ${CMAKE_SOURCE_DIR}/constr/${TOPMODULE}.nxdc +) add_executable(V${TOPMODULE}_nvboard - ${CMAKE_CURRENT_BINARY_DIR}/${TOPMODULE}/auto_bind.cpp main.cpp) + ${CMAKE_CURRENT_BINARY_DIR}/${TOPMODULE}/auto_bind.cpp + main.cpp +) -verilate( - V${TOPMODULE}_nvboard - TRACE - THREADS - TOP_MODULE - ${TOPMODULE} - PREFIX - V${TOPMODULE} - SOURCES - ${CHISEL_OUTPUT_TOPMODULE} - INCLUDE_DIRS - ${CHISEL_OUTPUT_DIR}) +verilate(V${TOPMODULE}_nvboard TRACE THREADS + TOP_MODULE ${TOPMODULE} + PREFIX V${TOPMODULE} + SOURCES ${CHISEL_OUTPUT_TOPMODULE} + INCLUDE_DIRS ${CHISEL_OUTPUT_DIR}) -target_include_directories(V${TOPMODULE}_nvboard PRIVATE ${NVBOARD_INCLUDE_DIR} - ${SDL2_INCLUDE_DIRS}) -target_link_libraries( - V${TOPMODULE}_nvboard PRIVATE ${NVBOARD_LIBRARY} SDL2::SDL2 - SDL2_image::SDL2_image) +target_include_directories(V${TOPMODULE}_nvboard PRIVATE ${NVBOARD_INCLUDE_DIR} ${SDL2_INCLUDE_DIRS}) +target_link_libraries(V${TOPMODULE}_nvboard PRIVATE ${NVBOARD_LIBRARY} SDL2::SDL2 SDL2_image::SDL2_image) -install(TARGETS V${TOPMODULE}_nvboard) +install(TARGETS V${TOPMODULE}_nvboard) \ No newline at end of file diff --git a/npc/include/components.hpp b/npc/include/components.hpp index ca86156..91847a7 100644 --- a/npc/include/components.hpp +++ b/npc/include/components.hpp @@ -11,7 +11,6 @@ #include #include #include -#include #include #include #include @@ -33,21 +32,21 @@ public: } }; -// class MemoryFile { -// std::filesystem::path filepath; -// public: - -// }; - -template class Memory { - paddr_t pmem_start, pmem_end; +template class Memory { + uint32_t expand_bits(uint8_t bits) { + uint32_t x = bits; + x = (x | (x << 7) | (x << 14) | (x << 21)) & 0x01010101; + x = x * 0xFF; + // printf("expand: %hhx->%x\n", bits, x); + return x; + } public: - std::array mem; - // TODO: Read memory file before init and use memcpy to initialize memory. - Memory(std::filesystem::path filepath, bool is_binary, paddr_t pmem_start, - paddr_t pmem_end) - : pmem_start(pmem_start), pmem_end(pmem_end) { + std::array mem; + std::vector> trace_ranges; + Memory(std::filesystem::path filepath, bool is_binary, + std::vector> &&trace_ranges) + : trace_ranges(std::move(trace_ranges)) { if (!std::filesystem::exists(filepath)) throw std::runtime_error("Memory file not found"); if (is_binary) { @@ -63,79 +62,54 @@ public: } } } - const word_t &operator[](std::size_t addr) { return this->read(addr); } - void transfer(paddr_t addr, uint8_t data[], size_t len, bool is_write) { - if (is_write) { - // memcpy(guest_to_host(addr), data, len); - size_t offset = (addr - pmem_start); - std::copy(data, data + len, &mem[offset]); - } else { - // memcpy(data, guest_to_host(addr), len); - size_t offset = (addr - pmem_start); - std::copy(&mem[offset], &mem[offset + len], data); - } + const T &operator[](std::size_t addr) { return this->read(addr); } + /** + * Always reads and returns 4 bytes from the address raddr & ~0x3u. + */ + T read(paddr_t raddr) { + // printf("raddr: 0x%x\n", raddr); + return *(word_t *)guest_to_host(raddr); } - bool in_pmem(paddr_t addr) const { - return addr >= pmem_start && addr <= pmem_end; - } -}; - -template class MemoryMap { - std::unique_ptr ram; - std::unique_ptr devices; - const std::vector> &trace_ranges; - -public: - MemoryMap(std::unique_ptr &&ram, std::unique_ptr &&devices, - const std::vector> &trace_ranges) - : ram(std::move(ram)), devices(std::move(devices)), - trace_ranges(trace_ranges) {} - void write(paddr_t waddr, word_t wdata, char wmask) { + /** + * Always writes to the 4 bytes at the address `waddr` & ~0x3u. + * Each bit in `wmask` represents a mask for one byte in wdata. + * For example, wmask = 0x3 means only the lowest 2 bytes are written, + * and the other bytes in memory remain unchanged. + */ + void write(paddr_t waddr, T wdata, char wmask) { // printf("waddr: 0x%x\n", waddr); - size_t len = (wmask & 1) + ((wmask & 2) >> 1) + ((wmask & 4) >> 2) + - ((wmask & 8) >> 3); - if (ram->in_pmem(waddr)) { - ram->transfer(waddr, (uint8_t *)&wdata, len, true); - } else if (devices->handle(waddr, (uint8_t *)&wdata, len, true)) { + uint8_t *p_data = (uint8_t *)&wdata; + while (wmask & 0x1) { + memcpy(guest_to_host(waddr), p_data, 1); + waddr++; + p_data++; + wmask >>= 1; } } - word_t read(paddr_t raddr) const { - word_t res = 0; - // printf("raddr: 0x%x, in_pmem: %d\n", raddr, ram->in_pmem(raddr)); - if (ram->in_pmem(raddr)) { - ram->transfer(raddr, (uint8_t *)&res, 4, false); - } else if (devices->handle(raddr, (uint8_t *)&res, 4, false)) { + void *guest_to_host(std::size_t addr) { + extern bool g_skip_memcheck; + if (g_skip_memcheck) { + return mem.data(); } - return res; - } - void copy_to(paddr_t addr, uint8_t *buf, size_t len) const { - if (ram->in_pmem(addr)) { - ram->transfer(addr, buf, len, false); - } else { - std::cerr << "Not in pmem" << std::endl; + if (addr < 0x80000000 || addr > 0x87ffffff) { + std::cerr << std::hex << "ACCESS " << addr << std::dec << std::endl; + throw std::runtime_error("Invalid memory access"); } + // Linear mapping + return (uint8_t *)(mem.data() + (addr >> 2) - 0x20000000) + (addr & 0x3); } - void copy_from(paddr_t addr, const uint8_t *buf, size_t len) { - if (ram->in_pmem(addr)) { - ram->transfer(addr, buf, len, true); - } else { - std::cerr << "Not in pmem" << std::endl; - } - } - void *get_pmem() { return ram->mem.data(); } void trace(paddr_t addr, bool is_read, word_t pc = 0, word_t value = 0) { for (auto &r : trace_ranges) { if (r[0] <= addr && r[1] >= addr) { std::stringstream os; + os << std::hex; if (pc != 0) - os << "0x" << std::hex << pc << " "; + os << "0x" << pc << " "; if (is_read) - os << "[R] " - << "0x" << addr << ": 0x" << this->read(addr); + os << "[R] "; else - os << "[W] " << value << " -> " - << "0x" << addr; - os << std::dec << std::endl; + os << "[W] " << value << " -> "; + os << "0x" << addr << std::dec << std::endl; std::cout << os.rdbuf(); break; } diff --git a/npc/include/config.hpp b/npc/include/config.hpp index c57ca2e..de536b6 100644 --- a/npc/include/config.hpp +++ b/npc/include/config.hpp @@ -9,7 +9,7 @@ struct Config { std::filesystem::path memory_file; - bool interactive{false}; + uint64_t max_sim_time = 0; bool memory_file_binary = {true}; bool do_mtrace{false}; std::vector> mtrace_ranges{ @@ -21,4 +21,4 @@ struct Config { extern Config config; -#endif +#endif \ No newline at end of file diff --git a/npc/include/trm_interface.hpp b/npc/include/trm_interface.hpp index 5410820..5acb8d6 100644 --- a/npc/include/trm_interface.hpp +++ b/npc/include/trm_interface.hpp @@ -37,7 +37,6 @@ template struct CPUStateBase { /* This does not update the register!!! */ R at(std::string name) { - // FIXME: Using this to get pc seems broken return name == "pc" ? pc : reg[regs_by_name.at(name)]; } @@ -155,4 +154,4 @@ struct RefTrmInterface : TrmInterface { void print(std::ostream &os) const override { os << *(CPUState *)cpu_state; } }; -#endif +#endif \ No newline at end of file diff --git a/npc/utils/CMakeLists.txt b/npc/utils/CMakeLists.txt index 545b778..d61f3d8 100644 --- a/npc/utils/CMakeLists.txt +++ b/npc/utils/CMakeLists.txt @@ -1,3 +1,3 @@ -if(ENABLE_SDB) +if (ENABLE_SDB) add_subdirectory(sdb) endif() diff --git a/npc/utils/sdb/CMakeLists.txt b/npc/utils/sdb/CMakeLists.txt index 70b3f3d..2a41fbf 100644 --- a/npc/utils/sdb/CMakeLists.txt +++ b/npc/utils/sdb/CMakeLists.txt @@ -5,30 +5,14 @@ find_package(LLVM CONFIG REQUIRED) set(PARSER_DIR "${CMAKE_CURRENT_BINARY_DIR}") set(LEXER_OUT "${PARSER_DIR}/lexer.c") set(PARSER_OUT "${PARSER_DIR}/parser.c") -flex_target(LEXER addrexp.l "${LEXER_OUT}" - DEFINES_FILE "${PARSER_DIR}/addrexp_lex.h") -bison_target(PARSER addrexp.y "${PARSER_OUT}" - DEFINES_FILE "${PARSER_DIR}/addrexp.h") +flex_target(LEXER addrexp.l "${LEXER_OUT}" DEFINES_FILE "${PARSER_DIR}/addrexp_lex.h") +bison_target(PARSER addrexp.y "${PARSER_OUT}" DEFINES_FILE "${PARSER_DIR}/addrexp.h") add_flex_bison_dependency(LEXER PARSER) -add_library(sdb OBJECT sdb.cpp console.cpp disasm.cpp "${LEXER_OUT}" - "${PARSER_OUT}") -llvm_map_components_to_libnames( - LLVM_LIBS - core - target - asmparser - riscvasmparser - riscvdesc - riscvdisassembler - riscvinfo - riscvtargetmca) +add_library(sdb OBJECT sdb.cpp console.cpp disasm.cpp "${LEXER_OUT}" "${PARSER_OUT}") +llvm_map_components_to_libnames(LLVM_LIBS core target asmparser riscvasmparser riscvdesc riscvdisassembler riscvinfo riscvtargetmca) target_link_libraries(sdb PUBLIC ${LLVM_LIBS}) target_link_libraries(sdb PRIVATE ${Readline_LIBRARY}) - -target_include_directories( - sdb - PRIVATE ${PARSER_DIR} ${Readline_INCLUDE_DIR} - PUBLIC ${CMAKE_SOURCE_DIR}/include) - +target_include_directories(sdb PRIVATE ${PARSER_DIR}) +target_include_directories(sdb PRIVATE ${Readline_INCLUDE_DIR}) target_include_directories(sdb PUBLIC include) diff --git a/npc/utils/sdb/include/sdb.hpp b/npc/utils/sdb/include/sdb.hpp index 569773b..964412a 100644 --- a/npc/utils/sdb/include/sdb.hpp +++ b/npc/utils/sdb/include/sdb.hpp @@ -96,4 +96,4 @@ public: }; } // namespace SDB -#endif +#endif \ No newline at end of file diff --git a/npc/utils/sdb/sdb.cpp b/npc/utils/sdb/sdb.cpp index 36aa787..55d6d41 100644 --- a/npc/utils/sdb/sdb.cpp +++ b/npc/utils/sdb/sdb.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include extern "C" {