feat: difftest framework

This commit is contained in:
xinyangli 2024-04-05 00:16:58 +08:00
parent 97cf418c86
commit 849f2bb5f3
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
15 changed files with 318 additions and 84 deletions

View file

@ -1,53 +1,61 @@
#ifndef _NPC_COMPONENTS_H_
#define _NPC_COMPONENTS_H_
#include "vpi_user.h"
#include <array>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <verilated_vpi.h>
template <typename T, std::size_t nr> class _RegistersBase {
std::array<T, nr> regs;
T pc;
virtual T fetch_pc();
virtual T fetch_reg(std::size_t id);
public:
T operator[](size_t id) { return fetch_reg(id); }
T get_pc() { return fetch_pc(); }
void update() {
for (int i = 0; i < regs.size(); i++) {
regs[i] = fetch_reg(i);
}
}
void print_regs() {
for (int i = 0; i < regs.size(); i++) {
printf("%d: %d\t", i, regs[i]);
if (i % 8 == 7)
putchar('\n');
}
putchar('\n');
}
};
template <typename T, std::size_t nr>
class _RegistersVPI : public _RegistersBase<T, nr> {
std::array<vpiHandle, nr> reg_handles;
T fetch_reg(std::size_t id) {
vpiHandle pc_handle;
T vpi_get(vpiHandle vh) {
s_vpi_value v;
v.format = vpiIntVal;
vpi_get_value(reg_handles[id], &v);
vpi_get_value(vh, &v);
return v.value.integer;
}
T fetch_pc(void) { return vpi_get(pc_handle); }
T fetch_reg(std::size_t id) { return vpi_get(reg_handles[id]); }
public:
_RegistersVPI<T, nr>(const std::string regs_prefix) {
_RegistersVPI<T, nr>(const std::string regs_prefix,
const std::string pcname) {
for (int i = 0; i < nr; i++) {
std::string regname = regs_prefix + std::to_string(i);
vpiHandle vh = vpi_handle_by_name((PLI_BYTE8 *)regname.c_str(), NULL);
vpiHandle vh = vpi_handle_by_name((PLI_BYTE8 *)regname.c_str(), nullptr);
if (vh == nullptr) {
std::cerr << "vpiHandle " << regname.c_str() << " not found"
<< std::endl;
exit(EXIT_FAILURE);
}
reg_handles[i] = vh;
}
pc_handle = vpi_handle_by_name((PLI_BYTE8 *)pcname.c_str(), nullptr);
}
};
template <typename T, std::size_t n> class Memory {
std::array<T, n> mem;
std::size_t addr_to_index(std::size_t addr) {
if (addr < 0x80000000) {
return 0;
@ -64,12 +72,13 @@ template <typename T, std::size_t n> class Memory {
}
public:
std::array<T, n> mem;
Memory(std::filesystem::path filepath, bool is_binary = true) {
assert(std::filesystem::exists(filepath));
if (is_binary) {
std::ifstream file(filepath, std::ios::binary);
char *pmem = reinterpret_cast<char *>(mem.data());
file.read(pmem, mem.size() / sizeof(mem[0]));
file.read(pmem, mem.size() * sizeof(mem[0]));
} else {
std::string line;
std::ifstream file(filepath);
@ -84,7 +93,7 @@ public:
* Always reads and returns 4 bytes from the address raddr & ~0x3u.
*/
T read(int raddr) {
printf("raddr: 0x%x\n", raddr);
// printf("raddr: 0x%x\n", raddr);
return mem[addr_to_index((uint32_t)raddr)];
}
/**
@ -94,7 +103,7 @@ public:
* and the other bytes in memory remain unchanged.
*/
void write(int waddr, T wdata, char wmask) {
printf("waddr: 0x%x\n", waddr);
// printf("waddr: 0x%x\n", waddr);
mem[addr_to_index((uint32_t)waddr)] = expand_bits(wmask) & wdata;
}
};

View file

@ -9,13 +9,16 @@ void Config::cli_parse(int argc, char **argv) {
"Memory file is in text format");
app.add_flag("--trace", do_trace, "Enable tracing");
app.add_option("--wav", wavefile, "output .vcd file path")
->check([this](const std::string &) {
if (!this->do_trace)
->check([=](const std::string &) {
if (!do_trace)
throw CLI::ValidationError(
"dependency", "You must turn on trace before specify wave file");
return std::string();
});
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);
try {
app.parse(argc, argv);

View file

@ -6,11 +6,12 @@
#include <filesystem>
struct Config {
std::filesystem::path wavefile;
std::filesystem::path memory_file;
uint64_t max_sim_time = 1000;
bool memory_file_binary = {true};
bool do_trace{false};
std::filesystem::path wavefile;
std::filesystem::path lib_ref;
void cli_parse(int argc, char **argv);
};

View file

@ -1,7 +1,10 @@
#include "components.hpp"
#include "config.hpp"
#include "vl_wrapper.hpp"
#include "vpi_user.h"
#include <VFlow.h>
#include <cstdint>
#include <difftest.hpp>
using VlModule = VlModuleInterfaceCommon<VFlow>;
using Registers = _RegistersVPI<uint32_t, 32>;
@ -16,6 +19,8 @@ void *pmem_get() {
int pmem_read(int raddr) {
void *pmem = pmem_get();
auto mem = static_cast<Memory<int, 128 * 1024> *>(pmem);
// TODO: Do memory difftest at memory read and write to diagnose at a finer
// granularity
return mem->read(raddr);
}
@ -26,19 +31,55 @@ void pmem_write(int waddr, int wdata, char wmask) {
}
}
VlModule *top;
Registers *regs;
using CPUState = CPUStateBase<uint32_t, 32>;
vpiHandle pc = nullptr;
void difftest_memcpy(paddr_t, void *, size_t, bool){};
void difftest_regcpy(void *p, bool direction) {
if (direction == DIFFTEST_FROM_REF) {
((CPUState *)p)->pc = regs->get_pc();
for (int i = 0; i < 32; i++) {
((CPUState *)p)->reg[i] = (*regs)[i];
}
}
}
void difftest_exec(uint64_t n) {
while (n--) {
for (int i = 0; i < 2; i++) {
if (top->is_posedge()) {
// Posedge
regs->update();
}
top->eval();
}
}
}
void difftest_init(int port) {
// top = std::make_unique<VlModule>(config.do_trace, config.wavefile);
top = new VlModule{config.do_trace, config.wavefile};
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
top->reset_eval(10);
}
int main(int argc, char **argv, char **env) {
config.cli_parse(argc, argv);
auto top = std::make_shared<VlModule>(config.do_trace, config.wavefile);
Registers regs("TOP.Flow.reg_0.regFile_");
top->reset_eval(10);
for (int i = 0; i < config.max_sim_time; i++) {
if (top->is_posedge()) {
// Posedge
regs.update();
regs.print_regs();
}
top->eval();
/* -- Difftest -- */
std::filesystem::path ref{config.lib_ref};
DifftestInterface dut_interface = DifftestInterface{
&difftest_memcpy, &difftest_regcpy, &difftest_exec, &difftest_init};
DifftestInterface ref_interface = DifftestInterface{ref};
Difftest<CPUStateBase<uint32_t, 32>> diff{dut_interface, ref_interface,
pmem_get(), 128};
int t = 8;
while (t--) {
diff.step(1);
}
return 0;
}