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

@ -1,15 +1,19 @@
#ifndef _NPC_COMPONENTS_H_
#define _NPC_COMPONENTS_H_
#include "types.h"
#include <array>
#include <cmath>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
template <typename T, std::size_t nr> class _RegistersBase {
std::array<T, nr> regs;
@ -29,9 +33,14 @@ public:
template <typename T, std::size_t n> class Memory {
std::size_t addr_to_index(std::size_t addr) {
if (addr < 0x80000000) {
extern bool g_skip_memcheck;
if (g_skip_memcheck) {
return 0;
}
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 (addr >> 2) - 0x20000000;
}
@ -45,7 +54,10 @@ template <typename T, std::size_t n> class Memory {
public:
std::array<T, n> mem;
Memory(std::filesystem::path filepath, bool is_binary = true) {
std::vector<std::array<uint64_t, 2>> trace_ranges;
Memory(std::filesystem::path filepath, bool is_binary,
std::vector<std::array<uint64_t, 2>> &&trace_ranges)
: trace_ranges(std::move(trace_ranges)) {
if (!std::filesystem::exists(filepath))
throw std::runtime_error("Memory file not found");
if (is_binary) {
@ -82,5 +94,22 @@ public:
void *guest_to_host(std::size_t addr) {
return mem.data() + addr_to_index(addr);
}
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" << pc << " ";
if (is_read)
os << "[R] ";
else
os << "[W] " << value << " -> ";
os << "0x" << addr << std::dec << std::endl;
std::cout << os.rdbuf();
break;
}
}
}
};
#endif

View file

@ -3,13 +3,17 @@
#include <CLI/App.hpp>
#include <CLI/CLI.hpp>
#include <CLI/Validators.hpp>
#include <cstddef>
#include <filesystem>
#include <utility>
struct Config {
std::filesystem::path memory_file;
uint64_t max_sim_time = 1000;
uint64_t max_sim_time = 0;
bool memory_file_binary = {true};
bool do_trace{false};
bool do_mtrace{false};
std::vector<std::array<std::size_t, 2>> mtrace_ranges{
{0x80000000, 0x8ffffffff}};
std::filesystem::path wavefile;
std::filesystem::path lib_ref;
void cli_parse(int argc, char **argv);

View file

@ -15,7 +15,6 @@
#include <ostream>
#include <stdexcept>
#include <trm_interface.hpp>
Disassembler d{"riscv32-linux-pc-gnu"};
using paddr_t = uint32_t;
struct DifftestTrmInterface : public TrmInterface {

View file

@ -1,5 +1,6 @@
#ifndef _NPC_TRM_INTERFACE_HEADER_FILE_
#define _NPC_TRM_INTERFACE_HEADER_FILE_
#include <disasm.hpp>
#include <dlfcn.h>
#include <filesystem>
#include <functional>
@ -7,6 +8,8 @@
#include <string>
#include <types.h>
extern Disassembler d;
template <typename R, size_t nr_reg> struct CPUStateBase {
R reg[nr_reg] = {0};
word_t pc = 0x80000000;

View file

@ -1,5 +1,6 @@
#ifndef _NPC_TRACER_H_
#define _NPC_TRACER_H_
#include "components.hpp"
#include <filesystem>
#include <verilated_vcd_c.h>
@ -19,7 +20,8 @@ public:
~Tracer() { m_trace->close(); }
/**
* @brief: Dump signals to waveform file. Must be called once after every top->eval() call.
* Dump signals to waveform file. Must be called once after every top->eval()
* call.
*/
void update() { m_trace->dump(cycle++); }
};
@ -30,9 +32,8 @@ template <typename T> class VlModuleInterfaceCommon : public T {
std::unique_ptr<Tracer<T>> tracer;
public:
VlModuleInterfaceCommon<T>(bool do_trace,
std::filesystem::path wavefile = "waveform.vcd") {
if (do_trace)
VlModuleInterfaceCommon<T>(std::filesystem::path wavefile) {
if (!wavefile.empty())
tracer = std::make_unique<Tracer<T>>(this, wavefile);
}
void eval() {
@ -51,9 +52,12 @@ public:
}
}
void reset_eval(int n) {
extern bool g_skip_memcheck;
g_skip_memcheck = true;
this->reset = 1;
this->eval(n);
this->reset = 0;
g_skip_memcheck = false;
}
bool is_posedge() {
// Will be posedge when eval is called

View file

@ -0,0 +1,36 @@
#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