refactor: move to dpi module for memory access
This commit is contained in:
parent
a478ef7639
commit
e30a91da01
14 changed files with 444 additions and 207 deletions
101
npc/csrc/Flow/components.hpp
Normal file
101
npc/csrc/Flow/components.hpp
Normal file
|
@ -0,0 +1,101 @@
|
|||
|
||||
#ifndef _NPC_COMPONENTS_H_
|
||||
#define _NPC_COMPONENTS_H_
|
||||
#include <array>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <verilated_vpi.h>
|
||||
|
||||
template <typename T, std::size_t nr> class _RegistersBase {
|
||||
std::array<T, nr> regs;
|
||||
virtual T fetch_reg(std::size_t id);
|
||||
|
||||
public:
|
||||
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) {
|
||||
s_vpi_value v;
|
||||
v.format = vpiIntVal;
|
||||
vpi_get_value(reg_handles[id], &v);
|
||||
return v.value.integer;
|
||||
}
|
||||
|
||||
public:
|
||||
_RegistersVPI<T, nr>(const std::string regs_prefix) {
|
||||
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);
|
||||
reg_handles[i] = vh;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
// 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:
|
||||
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
|
27
npc/csrc/Flow/config.cpp
Normal file
27
npc/csrc/Flow/config.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include "config.hpp"
|
||||
|
||||
void Config::cli_parse(int argc, char **argv) {
|
||||
CLI::App app;
|
||||
app.add_option("-m,--memory", memory_file, "Content of memory")
|
||||
->required()
|
||||
->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([this](const std::string &) {
|
||||
if (!this->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");
|
||||
|
||||
try {
|
||||
app.parse(argc, argv);
|
||||
} catch (const CLI::ParseError &e) {
|
||||
exit((app).exit(e));
|
||||
}
|
||||
}
|
||||
|
||||
Config config;
|
19
npc/csrc/Flow/config.hpp
Normal file
19
npc/csrc/Flow/config.hpp
Normal file
|
@ -0,0 +1,19 @@
|
|||
#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 wavefile;
|
||||
std::filesystem::path memory_file;
|
||||
uint64_t max_sim_time = 1000;
|
||||
bool memory_file_binary = {true};
|
||||
bool do_trace{false};
|
||||
void cli_parse(int argc, char **argv);
|
||||
};
|
||||
|
||||
extern Config config;
|
||||
|
||||
#endif
|
|
@ -1,189 +1,39 @@
|
|||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <sys/types.h>
|
||||
#include <vpi_user.h>
|
||||
#include "components.hpp"
|
||||
#include "config.hpp"
|
||||
#include "vl_wrapper.hpp"
|
||||
#include <VFlow.h>
|
||||
#include <cstdlib>
|
||||
#include <memory>
|
||||
#include <verilated.h>
|
||||
#include <verilated_vcd_c.h>
|
||||
#include <verilated_vpi.h>
|
||||
#include <string>
|
||||
#define MAX_SIM_TIME 100
|
||||
#define VERILATOR_TRACE
|
||||
|
||||
template <class T>
|
||||
class Tracer {
|
||||
#ifdef VERILATOR_TRACE
|
||||
std::shared_ptr<T> top;
|
||||
std::unique_ptr<VerilatedVcdC> m_trace;
|
||||
uint64_t cycle = 0;
|
||||
#endif
|
||||
public:
|
||||
Tracer(T *top, std::filesystem::path wavefile) {
|
||||
#ifdef VERILATOR_TRACE
|
||||
top = top;
|
||||
Verilated::traceEverOn(true);
|
||||
m_trace = std::make_unique<VerilatedVcdC>();
|
||||
top->trace(m_trace.get(), 5);
|
||||
m_trace->open(wavefile.c_str());
|
||||
#endif
|
||||
}
|
||||
~Tracer() {
|
||||
#ifdef VERILATOR_TRACE
|
||||
m_trace->close();
|
||||
#endif
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump signals to waveform file. Must be called once after every top->eval() call.
|
||||
*/
|
||||
void update() {
|
||||
#ifdef VERILATOR_TRACE
|
||||
m_trace->dump(cycle++);
|
||||
#endif
|
||||
}
|
||||
};
|
||||
template <typename T, std::size_t nr>
|
||||
class _RegistersBase {
|
||||
std::array<T, nr> regs;
|
||||
virtual T fetch_reg(size_t id);
|
||||
public:
|
||||
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(size_t id) {
|
||||
s_vpi_value v;
|
||||
v.format = vpiIntVal;
|
||||
vpi_get_value(reg_handles[id], &v);
|
||||
return v.value.integer;
|
||||
}
|
||||
public:
|
||||
_RegistersVPI<T, nr>(const std::string regs_prefix) {
|
||||
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);
|
||||
reg_handles[i] = vh;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef _RegistersVPI<uint32_t, 32> Registers;
|
||||
template <typename T, std::size_t n>
|
||||
class Memory {
|
||||
std::array<T, n> mem;
|
||||
size_t addr_to_index(size_t addr) {
|
||||
// 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:
|
||||
const T& operator[](size_t addr) {
|
||||
return this->read(addr);
|
||||
}
|
||||
/**
|
||||
* Always reads and returns 4 bytes from the address raddr & ~0x3u.
|
||||
*/
|
||||
T read(int raddr) {
|
||||
return mem[addr_to_index(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) {
|
||||
mem[addr_to_index((uint32_t)waddr)] = expand_bits(wmask) & wdata;
|
||||
}
|
||||
};
|
||||
using VlModule = VlModuleInterfaceCommon<VFlow>;
|
||||
using Registers = _RegistersVPI<uint32_t, 32>;
|
||||
|
||||
extern "C" {
|
||||
void *pmem_get() {
|
||||
static auto pmem = new Memory<int, 128 * 1024>;
|
||||
return pmem;
|
||||
}
|
||||
int pmem_read(int raddr) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<Memory<int, 128 * 1024>*>(pmem);
|
||||
return mem->read(raddr);
|
||||
}
|
||||
void *pmem_get() {
|
||||
static auto pmem = new Memory<int, 128 * 1024>(config.memory_file,
|
||||
config.memory_file_binary);
|
||||
return pmem;
|
||||
}
|
||||
|
||||
void pmem_write(int waddr, int wdata, char wmask) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<Memory<int, 128 * 1024>*>(pmem);
|
||||
return mem->write((size_t)waddr, wdata, wmask);
|
||||
}
|
||||
}
|
||||
int pmem_read(int raddr) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<Memory<int, 128 * 1024> *>(pmem);
|
||||
return mem->read(raddr);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
typedef VlModuleInterfaceCommon<VFlow> VlModule;
|
||||
void pmem_write(int waddr, int wdata, char wmask) {
|
||||
void *pmem = pmem_get();
|
||||
auto mem = static_cast<Memory<int, 128 * 1024> *>(pmem);
|
||||
return mem->write((std::size_t)waddr, wdata, wmask);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv, char **env) {
|
||||
Verilated::commandArgs(argc, argv);
|
||||
|
||||
auto top = std::make_shared<VlModule>(false, "waveform.vcd");
|
||||
|
||||
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 < MAX_SIM_TIME; i++) {
|
||||
if(top->is_posedge()) {
|
||||
for (int i = 0; i < config.max_sim_time; i++) {
|
||||
if (top->is_posedge()) {
|
||||
// Posedge
|
||||
regs.update();
|
||||
regs.print_regs();
|
||||
|
|
65
npc/csrc/Flow/vl_wrapper.hpp
Normal file
65
npc/csrc/Flow/vl_wrapper.hpp
Normal 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
|
Loading…
Add table
Add a link
Reference in a new issue