feat(sdb): support sdb
This commit is contained in:
parent
8500df8a6e
commit
e828e140cd
22 changed files with 985 additions and 44 deletions
|
@ -1,5 +1,6 @@
|
|||
include(ChiselBuild)
|
||||
add_executable(V${TOPMODULE} config.cpp main.cpp)
|
||||
target_link_libraries(V${TOPMODULE} PRIVATE disasm sdb)
|
||||
|
||||
verilate(V${TOPMODULE} TRACE COVERAGE THREADS
|
||||
TOP_MODULE ${TOPMODULE}
|
||||
|
@ -8,6 +9,7 @@ verilate(V${TOPMODULE} TRACE COVERAGE THREADS
|
|||
INCLUDE_DIRS ${CHISEL_OUTPUT_DIR}
|
||||
VERILATOR_ARGS
|
||||
"--vpi" # Enable VPI
|
||||
"-Wno-UNOPTFLAT"
|
||||
)
|
||||
|
||||
add_test(
|
||||
|
|
|
@ -1,110 +0,0 @@
|
|||
|
||||
#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);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, std::size_t n> class Memory {
|
||||
std::size_t addr_to_index(std::size_t addr) {
|
||||
if (addr < 0x80000000) {
|
||||
return 0;
|
||||
}
|
||||
// Linear mapping
|
||||
return (addr >> 2) - 0x20000000;
|
||||
}
|
||||
uint32_t expand_bits(uint8_t bits) {
|
||||
uint32_t x = bits;
|
||||
x = (x | (x << 7) | (x << 14) | (x << 21)) & 0x01010101;
|
||||
x = x * 0xFF;
|
||||
// printf("expand: %hhx->%x\n", bits, x);
|
||||
return x;
|
||||
}
|
||||
|
||||
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]));
|
||||
} else {
|
||||
std::string line;
|
||||
std::ifstream file(filepath);
|
||||
int i = 0;
|
||||
while (std::getline(file, line)) {
|
||||
mem[i++] = std::stoul(line, 0, 16);
|
||||
}
|
||||
}
|
||||
}
|
||||
const T &operator[](std::size_t addr) { return this->read(addr); }
|
||||
/**
|
||||
* Always reads and returns 4 bytes from the address raddr & ~0x3u.
|
||||
*/
|
||||
T read(int raddr) {
|
||||
// printf("raddr: 0x%x\n", raddr);
|
||||
return mem[addr_to_index((uint32_t)raddr)];
|
||||
}
|
||||
/**
|
||||
* Always writes to the 4 bytes at the address `waddr` & ~0x3u.
|
||||
* Each bit in `wmask` represents a mask for one byte in wdata.
|
||||
* For example, wmask = 0x3 means only the lowest 2 bytes are written,
|
||||
* and the other bytes in memory remain unchanged.
|
||||
*/
|
||||
void write(int waddr, T wdata, char wmask) {
|
||||
// printf("waddr: 0x%x\n", waddr);
|
||||
mem[addr_to_index((uint32_t)waddr)] = expand_bits(wmask) & wdata;
|
||||
}
|
||||
};
|
||||
#endif
|
|
@ -1,10 +1,15 @@
|
|||
#include "components.hpp"
|
||||
#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 <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <difftest.hpp>
|
||||
#include <sdb.hpp>
|
||||
#include <types.h>
|
||||
|
||||
using VlModule = VlModuleInterfaceCommon<VFlow>;
|
||||
using Registers = _RegistersVPI<uint32_t, 32>;
|
||||
|
@ -31,9 +36,10 @@ void pmem_write(int waddr, int wdata, char wmask) {
|
|||
}
|
||||
}
|
||||
|
||||
Disassembler d{"riscv32-pc-linux-gnu"};
|
||||
|
||||
VlModule *top;
|
||||
Registers *regs;
|
||||
using CPUState = CPUStateBase<uint32_t, 32>;
|
||||
vpiHandle pc = nullptr;
|
||||
void difftest_memcpy(paddr_t, void *, size_t, bool){};
|
||||
|
||||
|
@ -58,6 +64,8 @@ void difftest_exec(uint64_t n) {
|
|||
}
|
||||
}
|
||||
}
|
||||
// std::cout << d.disassemble(top->rootp->Flow__DOT__pc__DOT__pc_reg, (uint8_t *)&top->rootp->Flow__DOT___ram_inst, 4) << std::endl;
|
||||
|
||||
void difftest_init(int port) {
|
||||
// top = std::make_unique<VlModule>(config.do_trace, config.wavefile);
|
||||
top = new VlModule{config.do_trace, config.wavefile};
|
||||
|
@ -65,20 +73,34 @@ void difftest_init(int port) {
|
|||
top->reset_eval(10);
|
||||
}
|
||||
|
||||
DifftestInterface dut_interface = DifftestInterface{
|
||||
&difftest_memcpy, &difftest_regcpy, &difftest_exec, &difftest_init};
|
||||
|
||||
SDB::SDB<dut_interface> sdb_dut;
|
||||
extern "C" {
|
||||
word_t reg_str2val(const char *name, bool *success) {
|
||||
return sdb_dut.reg_str2val(name, success);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **env) {
|
||||
config.cli_parse(argc, argv);
|
||||
|
||||
/* -- 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;
|
||||
sdb_dut.main_loop();
|
||||
while (t--) {
|
||||
diff.step(1);
|
||||
if (!diff.step(1)) {
|
||||
uint32_t pc = regs->get_pc();
|
||||
uint32_t inst = pmem_read(pc);
|
||||
std::cout << diff << d.disassemble(pc, (uint8_t *)&inst, 4) << std::endl;
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
|
36
npc/csrc/Flow/vpi_wrapper.hpp
Normal file
36
npc/csrc/Flow/vpi_wrapper.hpp
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue