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

108 lines
2.8 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>
2024-04-04 16:16:58 +00:00
#include <difftest.hpp>
2024-04-09 09:03:21 +00:00
#include <sdb.hpp>
#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
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-09 09:03:21 +00:00
Disassembler d{"riscv32-pc-linux-gnu"};
2024-04-04 16:16:58 +00:00
VlModule *top;
Registers *regs;
vpiHandle pc = nullptr;
void difftest_memcpy(paddr_t, void *, size_t, bool){};
void difftest_regcpy(void *p, bool direction) {
if (direction == DIFFTEST_FROM_REF) {
((CPUState *)p)->pc = regs->get_pc();
for (int i = 0; i < 32; i++) {
((CPUState *)p)->reg[i] = (*regs)[i];
}
}
}
void difftest_exec(uint64_t n) {
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
// std::cout << d.disassemble(top->rootp->Flow__DOT__pc__DOT__pc_reg, (uint8_t *)&top->rootp->Flow__DOT___ram_inst, 4) << std::endl;
2024-04-04 16:16:58 +00:00
void difftest_init(int port) {
// 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);
}
2024-04-09 09:03:21 +00:00
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);
}
}
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};
DifftestInterface ref_interface = DifftestInterface{ref};
Difftest<CPUStateBase<uint32_t, 32>> diff{dut_interface, ref_interface,
pmem_get(), 128};
int t = 8;
2024-04-09 09:03:21 +00:00
sdb_dut.main_loop();
2024-04-04 16:16:58 +00:00
while (t--) {
2024-04-09 09:03:21 +00:00
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;
}
2024-03-29 02:35:49 +00:00
}
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
}