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

86 lines
2.2 KiB
C++
Raw Normal View History

#include "components.hpp"
#include "config.hpp"
#include "vl_wrapper.hpp"
2024-04-04 16:16:58 +00:00
#include "vpi_user.h"
2024-03-29 02:35:49 +00:00
#include <VFlow.h>
2024-04-04 16:16:58 +00:00
#include <cstdint>
#include <difftest.hpp>
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-04 16:16:58 +00:00
VlModule *top;
Registers *regs;
using CPUState = CPUStateBase<uint32_t, 32>;
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();
}
}
}
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-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 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;
while (t--) {
diff.step(1);
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
}