ysyx-workbench/npc/csrc/Flow/main.cpp

130 lines
3.3 KiB
C++
Raw Normal View History

2024-04-09 09:03:21 +00:00
#include "VFlow___024root.h"
#include "config.hpp"
2024-04-09 09:03:21 +00:00
#include "disasm.hpp"
#include "vl_wrapper.hpp"
2024-04-04 16:16:58 +00:00
#include "vpi_user.h"
2024-04-09 09:03:21 +00:00
#include "vpi_wrapper.hpp"
2024-03-29 02:35:49 +00:00
#include <VFlow.h>
2024-04-04 16:16:58 +00:00
#include <cstdint>
2024-04-09 09:03:21 +00:00
#include <cstdlib>
#include <filesystem>
#include <fstream>
2024-04-09 09:03:21 +00:00
#include <sdb.hpp>
#include <trm_difftest.hpp>
#include <trm_interface.hpp>
2024-04-09 09:03:21 +00:00
#include <types.h>
2024-03-13 06:53:31 +00:00
using VlModule = VlModuleInterfaceCommon<VFlow>;
using Registers = _RegistersVPI<uint32_t, 32>;
2024-03-29 02:35:49 +00:00
// SDB::SDB<NPC::npc_interface> sdb_dut;
using CPUState = CPUStateBase<uint32_t, 32>;
CPUState npc_cpu;
2024-04-02 08:15:16 +00:00
extern "C" {
void *pmem_get() {
static auto pmem = new Memory<int, 128 * 1024>(config.memory_file,
config.memory_file_binary);
return pmem;
}
2024-04-02 08:15:16 +00:00
int pmem_read(int raddr) {
void *pmem = pmem_get();
auto mem = static_cast<Memory<int, 128 * 1024> *>(pmem);
2024-04-04 16:16:58 +00:00
// TODO: Do memory difftest at memory read and write to diagnose at a finer
// granularity
return mem->read(raddr);
}
2024-04-02 06:30:14 +00:00
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);
}
}
2024-03-29 02:35:49 +00:00
2024-04-04 16:16:58 +00:00
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) {
memcpy(
buf,
static_cast<Memory<int, 128 * 1024> *>(pmem_get())->guest_to_host(addr),
sz);
}
};
void npc_regcpy(void *p, bool direction) {
2024-04-04 16:16:58 +00:00
if (direction == TRM_FROM_MACHINE) {
2024-04-04 16:16:58 +00:00
((CPUState *)p)->pc = regs->get_pc();
for (int i = 0; i < 32; i++) {
((CPUState *)p)->reg[i] = (*regs)[i];
}
}
}
void npc_exec(uint64_t n) {
2024-04-04 16:16:58 +00:00
while (n--) {
for (int i = 0; i < 2; i++) {
if (top->is_posedge()) {
// Posedge
regs->update();
}
top->eval();
}
}
}
2024-04-09 09:03:21 +00:00
void npc_init(int port) {
2024-04-04 16:16:58 +00:00
// top = std::make_unique<VlModule>(config.do_trace, config.wavefile);
top = new VlModule{config.do_trace, config.wavefile};
regs = new Registers("TOP.Flow.reg_0.regFile_", "TOP.Flow.pc.out");
top->reset_eval(10);
}
class DutTrmInterface : public TrmInterface {
public:
DutTrmInterface(memcpy_t f_memcpy, regcpy_t f_regcpy, exec_t f_exec,
init_t f_init, void *cpu_state)
: TrmInterface{f_memcpy, f_regcpy, f_exec, f_init, cpu_state} {}
word_t at(std::string name) const override {
return ((CPUState *)cpu_state)->at(name);
}
word_t at(paddr_t addr) const override {
word_t buf;
this->memcpy(addr, &buf, sizeof(word_t), TRM_FROM_MACHINE);
return buf;
}
void print(std::ostream &os) const override {}
};
DutTrmInterface npc_interface =
DutTrmInterface{&npc_memcpy, &npc_regcpy, &npc_exec, &npc_init, &npc_cpu};
}; // namespace NPC
2024-04-09 09:03:21 +00:00
extern "C" {
word_t reg_str2val(const char *name, bool *success) {
return npc_cpu.reg_str2val(name, success);
2024-04-09 09:03:21 +00:00
}
}
2024-03-29 02:35:49 +00:00
int main(int argc, char **argv, char **env) {
config.cli_parse(argc, argv);
2024-03-29 02:35:49 +00:00
2024-04-04 16:16:58 +00:00
/* -- 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};
2024-04-04 16:16:58 +00:00
int t = 8;
sdb_diff.main_loop();
2024-04-04 16:16:58 +00:00
2024-04-02 06:30:14 +00:00
return 0;
2024-03-13 06:53:31 +00:00
}