From 169f88d5a20ed6b8fc88e482bfde97f2eea55367 Mon Sep 17 00:00:00 2001 From: jiaxiaoyu Date: Wed, 25 Dec 2024 14:40:24 +0800 Subject: [PATCH] feat(override flash): support override flash from dut Signed-off-by: jiaxiaoyu --- difftest/difftest-def.h | 2 +- difftest/difftest.cc | 11 ++++++++--- difftest/difftest.h | 12 ++++++++++++ riscv/devices.h | 4 ++++ riscv/sim.cc | 5 +++++ riscv/sim.h | 2 ++ 6 files changed, 32 insertions(+), 4 deletions(-) diff --git a/difftest/difftest-def.h b/difftest/difftest-def.h index 8e2dda30fe..60a1957f02 100644 --- a/difftest/difftest-def.h +++ b/difftest/difftest-def.h @@ -91,7 +91,7 @@ #define CONFIG_MEMORY_SIZE (16 * 1024 * 1024 * 1024UL) #define CONFIG_FLASH_BASE 0x10000000UL -#define CONFIG_FLASH_SIZE 0x100000UL +#define CONFIG_FLASH_SIZE 0x10000000UL #define CONFIG_PMP_NUM 16 #define CONFIG_PMP_MAX_NUM 16 #define CONFIG_PMP_GRAN 12 diff --git a/difftest/difftest.cc b/difftest/difftest.cc index 454cd3d300..22036c1c01 100644 --- a/difftest/difftest.cc +++ b/difftest/difftest.cc @@ -352,6 +352,11 @@ void DifftestRef::memcpy_from_dut(reg_t dest, void* src, size_t n) { } } +void DifftestRef::flash_cpy_from_dut(const uint8_t* src, size_t n) { + sim->set_mmio(CONFIG_FLASH_BASE, n, src); + p->get_mmu()->flush_icache(); +} + void DifftestRef::debug_memcpy_from_dut(reg_t dest, void* src, size_t n) { #ifdef CONFIG_DIFF_DEBUG_MODE // addr is absolute physical addr @@ -486,7 +491,7 @@ const std::vector> DifftestRef::create_devi std::make_pair(reg_t(DM_BASE_ADDR), new dummy_debug_t), #endif #if defined(CONFIG_FLASH_BASE) && defined(CONFIG_FLASH_SIZE) - std::make_pair(reg_t(CONFIG_FLASH_BASE), new rom_device_t(rom_data)), + std::make_pair(reg_t(CONFIG_FLASH_BASE), new custom_rom_device_t(rom_data)), #endif }; } @@ -638,8 +643,8 @@ void debug_mem_sync(reg_t addr, void* buf, size_t n) { ref->debug_memcpy_from_dut(addr, buf, n); } -void difftest_load_flash(void *flash_bin, size_t size) { - +void difftest_load_flash(const uint8_t *flash_bin, size_t size) { + ref->flash_cpy_from_dut(flash_bin, size); } void difftest_set_mhartid(int mhartid) { diff --git a/difftest/difftest.h b/difftest/difftest.h index 4da7f26a34..02f4eb6c7c 100644 --- a/difftest/difftest.h +++ b/difftest/difftest.h @@ -130,6 +130,17 @@ class DifftestNonRegInterruptPending { bool lcofi_req = false; }; +class custom_rom_device_t : public rom_device_t { + public: + custom_rom_device_t(std::vector data) : rom_device_t(data) {} + // support full override flash only + bool store(reg_t addr, size_t len, const uint8_t* bytes) override { + auto &rom_data = get_data(); + rom_data.assign(bytes, bytes + len); + return true; + } +}; + class DifftestRef { public: DifftestRef(); @@ -141,6 +152,7 @@ class DifftestRef { void get_regs(diff_context_t *ctx); void set_regs(diff_context_t *ctx, bool on_demand); void memcpy_from_dut(reg_t dest, void* src, size_t n); + void flash_cpy_from_dut(const uint8_t* src, size_t n); void debug_memcpy_from_dut(reg_t dest, void* src, size_t n); int store_commit(uint64_t *addr, uint64_t *data, uint8_t *mask); void raise_intr(uint64_t no); diff --git a/riscv/devices.h b/riscv/devices.h index 6ef32e9f4f..88ed5c69c5 100644 --- a/riscv/devices.h +++ b/riscv/devices.h @@ -32,6 +32,10 @@ class rom_device_t : public abstract_device_t { bool load(reg_t addr, size_t len, uint8_t* bytes) override; bool store(reg_t addr, size_t len, const uint8_t* bytes) override; const std::vector& contents() { return data; } + + // 提供访问器 + std::vector& get_data() { return data; } + private: std::vector data; }; diff --git a/riscv/sim.cc b/riscv/sim.cc index a42e746238..42f5a3f6bc 100644 --- a/riscv/sim.cc +++ b/riscv/sim.cc @@ -357,6 +357,11 @@ bool sim_t::mmio_store(reg_t paddr, size_t len, const uint8_t* bytes) return bus.store(paddr, len, bytes); } +bool sim_t::set_mmio(reg_t paddr, size_t len, const uint8_t* bytes) +{ + return mmio_store(paddr, len, bytes); +} + void sim_t::set_rom() { const int reset_vec_size = 8; diff --git a/riscv/sim.h b/riscv/sim.h index d0e68f6548..2c4eca4ce0 100644 --- a/riscv/sim.h +++ b/riscv/sim.h @@ -110,6 +110,8 @@ class sim_t : public htif_t, public simif_t public: #endif virtual char* addr_to_mem(reg_t paddr) override; + virtual bool set_mmio(reg_t paddr, size_t len, const uint8_t* bytes); + #if defined(DIFFTEST) private: #endif