feat(sdb): support sdb
Some checks failed
Build abstract machine with nix / build-abstract-machine (push) Successful in 2m41s
Run CTests within npc / npc-test (push) Has been cancelled

This commit is contained in:
xinyangli 2024-04-09 17:03:21 +08:00
parent 8500df8a6e
commit e828e140cd
Signed by: xin
SSH key fingerprint: SHA256:qZ/tzd8lYRtUFSrfBDBMcUqV4GHKxqeqRA3huItgvbk
22 changed files with 985 additions and 44 deletions

View file

@ -0,0 +1,91 @@
#ifndef _NPC_COMPONENTS_H_
#define _NPC_COMPONENTS_H_
#include <array>
#include <cstdlib>
#include <exception>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <stdexcept>
#include <string>
const std::map<std::string, int> riscv32_regs_by_name{
{"$0", 0}, {"ra", 1}, {"sp", 2}, {"gp", 3}, {"tp", 4}, {"t0", 5},
{"t1", 6}, {"t2", 7}, {"s0", 8}, {"s1", 9}, {"a0", 10}, {"a1", 11},
{"a2", 12}, {"a3", 13}, {"a4", 14}, {"a5", 15}, {"a6", 16}, {"a7", 17},
{"s2", 18}, {"s3", 19}, {"s4", 20}, {"s5", 21}, {"s6", 22}, {"s7", 23},
{"s8", 24}, {"s9", 25}, {"s10", 26}, {"s11", 27}, {"t3", 28}, {"t4", 29},
{"t5", 30}, {"t6", 31}};
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 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) {
if (!std::filesystem::exists(filepath))
throw std::runtime_error("Memory file not found");
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

20
npc/include/config.hpp Normal file
View file

@ -0,0 +1,20 @@
#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,12 +1,14 @@
#ifndef _DIFFTEST_DIFFTEST_H_
#define _DIFFTEST_DIFFTEST_H_
#include <cassert>
#include <components.hpp>
#include <cstdint>
#include <cstdlib>
#include <dlfcn.h>
#include <filesystem>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
using paddr_t = uint32_t;
@ -69,14 +71,11 @@ public:
dut.regcpy(dut_state.get(), DIFFTEST_FROM_REF);
}
void step(uint64_t n) {
bool step(uint64_t n) {
ref.exec(n);
dut.exec(n);
fetch_state();
if (*ref_state != *dut_state) {
std::cout << *this;
exit(EXIT_FAILURE);
}
return *ref_state == *dut_state;
}
friend std::ostream &operator<<(std::ostream &os, const Difftest<S> &d) {
@ -90,6 +89,8 @@ public:
template <typename R, size_t nr_reg> struct CPUStateBase {
R reg[nr_reg] = {0};
paddr_t pc = 0x80000000;
static const std::map<std::string, int> inline regs_by_name =
riscv32_regs_by_name;
CPUStateBase() {
for (int i = 0; i < nr_reg; i++)
reg[i] = 0;
@ -106,6 +107,9 @@ template <typename R, size_t nr_reg> struct CPUStateBase {
bool operator!=(const CPUStateBase &other) const {
return !(*this == other); // Reuse the == operator for != implementation
}
/* This does not update the register!!! */
R at(std::string name) { return reg[regs_by_name.at(name)]; }
};
template <typename R, size_t nr_reg>

23
npc/include/types.h Normal file
View file

@ -0,0 +1,23 @@
#ifndef _NPC_TYPES_H__
#define _NPC_TYPES_H__
#include <inttypes.h>
typedef uint32_t word_t;
typedef int32_t sword_t;
static const word_t WORD_T_MAX = UINT32_MAX;
static const sword_t SWORD_T_MAX = INT32_MAX;
static const sword_t SWORD_T_MIN = INT32_MIN;
#define WORD_BYTES 4
#define FMT_WORD "0x%08x"
typedef uint32_t vaddr_t;
typedef uint32_t paddr_t;
#define FMT_ADDR "0x%08x"
typedef uint16_t ioaddr_t;
#ifdef __cplusplus
#include <difftest.hpp>
using CPUState = CPUStateBase<word_t, 32>;
#endif
#endif

View file

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