Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(override flash): support override flash from dut #75

Merged
merged 1 commit into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion difftest/difftest-def.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions difftest/difftest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -486,7 +491,7 @@ const std::vector<std::pair<reg_t, abstract_device_t*>> 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
};
}
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to NEMU (https://github.com/OpenXiangShan/NEMU/blob/master/src/cpu/difftest/ref.c#L104-L109) and DiffTest (https://github.com/OpenXiangShan/difftest/blob/master/src/test/csrc/difftest/difftest.cpp#L596), this void *flash_bin is a char * pointer to the flash file instead of a pointer to the memory contents.

I agree that an API with pointer to the flash contents is better. However, this change breaks our code in NEMU and DiffTest. Can you use this Spike version to diff with NEMU?

I would like to add a difftest_load_flash_v2 for the new API.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry it was an oversight on my part, this interface is not used in NEMU, I'm adding implementations for both sides (NEMU and spike)

ref->flash_cpy_from_dut(flash_bin, size);
}

void difftest_set_mhartid(int mhartid) {
Expand Down
12 changes: 12 additions & 0 deletions difftest/difftest.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> 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();
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions riscv/devices.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>& contents() { return data; }

// 提供访问器
std::vector<char>& get_data() { return data; }

private:
std::vector<char> data;
};
Expand Down
5 changes: 5 additions & 0 deletions riscv/sim.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions riscv/sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading