feat(npc): add mtrace

This commit is contained in:
xinyangli 2024-04-12 09:35:41 +08:00
parent 4c07b66093
commit 9f64a88f8d
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
12 changed files with 110 additions and 123 deletions

View file

@ -7,18 +7,16 @@ void Config::cli_parse(int argc, char **argv) {
->check(CLI::ExistingFile);
app.add_flag("!--no-bin", memory_file_binary,
"Memory file is in text format");
app.add_flag("--trace", do_trace, "Enable tracing");
app.add_option("--wav", wavefile, "output .vcd file path")
->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("--wav", wavefile, "output .vcd file path");
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);
app.add_flag("--mtrace", do_mtrace, "Enable memory tracing");
app.add_option(
"--mtrace-range", mtrace_ranges,
"Specify memory tracing range (default: 0x80000000-0x8fffffff)")
->delimiter(',');
try {
app.parse(argc, argv);

View file

@ -1,20 +0,0 @@
#ifndef _NPC_CONFIG_H_
#define _NPC_CONFIG_H_
#include <CLI/App.hpp>
#include <CLI/CLI.hpp>
#include <CLI/Validators.hpp>
#include <filesystem>
struct Config {
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);
};
extern Config config;
#endif

View file

@ -1,29 +1,35 @@
#include "VFlow___024root.h"
#include "config.hpp"
#include "disasm.hpp"
#include "vl_wrapper.hpp"
#include "vpi_user.h"
#include "vpi_wrapper.hpp"
#include <VFlow.h>
#include <config.hpp>
#include <cstdint>
#include <cstdlib>
#include <disasm.hpp>
#include <filesystem>
#include <fstream>
#include <sdb.hpp>
#include <trm_difftest.hpp>
#include <trm_interface.hpp>
#include <types.h>
#include <vl_wrapper.hpp>
#include <vpi_user.h>
#include <vpi_wrapper.hpp>
using VlModule = VlModuleInterfaceCommon<VFlow>;
using Registers = _RegistersVPI<uint32_t, 32>;
// SDB::SDB<NPC::npc_interface> sdb_dut;
using CPUState = CPUStateBase<uint32_t, 32>;
bool g_skip_memcheck = false;
CPUState npc_cpu;
VlModule *top;
Registers *regs;
vpiHandle pc = nullptr;
extern "C" {
void *pmem_get() {
static auto pmem = new Memory<int, 128 * 1024>(config.memory_file,
config.memory_file_binary);
static auto pmem =
new Memory<int, 128 * 1024>(config.memory_file, config.memory_file_binary,
std::move(config.mtrace_ranges));
return pmem;
}
@ -32,20 +38,20 @@ int pmem_read(int raddr) {
auto mem = static_cast<Memory<int, 128 * 1024> *>(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());
return mem->read(raddr);
}
void pmem_write(int waddr, int wdata, char wmask) {
void *pmem = pmem_get();
auto mem = static_cast<Memory<int, 128 * 1024> *>(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);
}
}
VlModule *top;
Registers *regs;
vpiHandle pc = nullptr;
namespace NPC {
void npc_memcpy(paddr_t addr, void *buf, size_t sz, bool direction) {
if (direction == TRM_FROM_MACHINE) {
@ -78,10 +84,15 @@ void npc_exec(uint64_t n) {
}
}
void npc_atexit(void) {
delete top;
delete regs;
}
void npc_init(int port) {
// top = std::make_unique<VlModule>(config.do_trace, config.wavefile);
top = new VlModule{config.do_trace, config.wavefile};
top = new VlModule{config.wavefile};
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
atexit(npc_atexit);
top->reset_eval(10);
}
@ -115,15 +126,19 @@ word_t reg_str2val(const char *name, bool *success) {
int main(int argc, char **argv, char **env) {
config.cli_parse(argc, argv);
/* -- Difftest -- */
std::filesystem::path ref{config.lib_ref};
RefTrmInterface ref_interface{ref};
DifftestTrmInterface diff_interface{NPC::npc_interface, ref_interface,
pmem_get(), 128};
SDB::SDB sdb_diff{diff_interface};
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(), 1024};
SDB::SDB sdb_diff{diff_interface};
int t = 8;
sdb_diff.main_loop();
int t = 8;
sdb_diff.main_loop();
}
return 0;
}

View file

@ -1,65 +0,0 @@
#ifndef _NPC_TRACER_H_
#define _NPC_TRACER_H_
#include <filesystem>
#include <verilated_vcd_c.h>
template <class T> class Tracer {
std::shared_ptr<T> top;
std::unique_ptr<VerilatedVcdC> m_trace;
uint64_t cycle = 0;
public:
Tracer(T *top, std::filesystem::path wavefile) {
top = top;
Verilated::traceEverOn(true);
m_trace = std::make_unique<VerilatedVcdC>();
top->trace(m_trace.get(), 5);
m_trace->open(wavefile.c_str());
}
~Tracer() { m_trace->close(); }
/**
* Dump signals to waveform file. Must be called once after every top->eval()
* call.
*/
void update() { m_trace->dump(cycle++); }
};
template <typename T> class VlModuleInterfaceCommon : public T {
uint64_t sim_time = 0;
uint64_t posedge_cnt = 0;
std::unique_ptr<Tracer<T>> tracer;
public:
VlModuleInterfaceCommon<T>(bool do_trace,
std::filesystem::path wavefile = "waveform.vcd") {
if (do_trace)
tracer = std::make_unique<Tracer<T>>(this, wavefile);
}
void eval() {
if (this->is_posedge()) {
posedge_cnt++;
}
T::clock = !T::clock;
sim_time++;
T::eval();
if (tracer)
tracer->update();
}
void eval(int n) {
for (int i = 0; i < n; i++) {
this->eval();
}
}
void reset_eval(int n) {
this->reset = 1;
this->eval(n);
this->reset = 0;
}
bool is_posedge() {
// Will be posedge when eval is called
return T::clock == 0;
}
};
#endif

View file

@ -1,36 +0,0 @@
#ifndef _NPC_VPI_WRAPPER_H_
#define _NPC_VPI_WRAPPER_H_
#include <components.hpp>
#include <vpi_user.h>
template <typename T, std::size_t nr>
class _RegistersVPI : public _RegistersBase<T, nr> {
std::array<vpiHandle, nr> reg_handles;
vpiHandle pc_handle;
T vpi_get(vpiHandle vh) {
s_vpi_value v;
v.format = vpiIntVal;
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,
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(), 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);
}
};
#endif