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

87 lines
1.7 KiB
C++
Raw Normal View History

2024-03-29 02:35:49 +00:00
#include <vpi_user.h>
#include <VFlow.h>
2024-03-13 06:53:31 +00:00
#include <cstdlib>
2024-03-29 02:35:49 +00:00
#include <vector>
#include <memory>
2024-03-13 06:53:31 +00:00
#include <verilated.h>
#include <verilated_vcd_c.h>
2024-03-29 02:35:49 +00:00
#include <verilated_vpi.h>
#include <string>
2024-03-13 06:53:31 +00:00
#define MAX_SIM_TIME 100
#define VERILATOR_TRACE
2024-03-29 02:35:49 +00:00
std::vector<vpiHandle> regsHandle;
int regs[32];
2024-03-13 06:53:31 +00:00
2024-03-29 02:35:49 +00:00
static void init_vpi_regs() {
std::string regfile = "TOP.Flow.reg_0.regFile_";
for(int i = 0; i < 32; i++) {
std::string regname = regfile + std::to_string(i);
vpiHandle vh = vpi_handle_by_name((PLI_BYTE8 *)regname.c_str(), NULL);
regsHandle.push_back(vh);
}
}
2024-03-13 06:53:31 +00:00
2024-03-29 02:35:49 +00:00
static void init_vpi() {
init_vpi_regs();
}
static int vpi_get_int(vpiHandle vh) {
s_vpi_value v;
v.format = vpiIntVal;
vpi_get_value(vh, &v);
return v.value.integer;
}
static void update_regs() {
for(int i = 0; i < 32; i++) {
regs[i] = vpi_get_int(regsHandle[i]);
}
}
static void print_regs() {
for(int i = 0; i < 32; i++) {
printf("%d: %d\t", i, regs[i]);
if(i % 8 == 7) putchar('\n');
}
putchar('\n');
}
static int sim_time = 0;
int main(int argc, char **argv, char **env) {
int sim_time = 0;
int posedge_cnt = 0;
Verilated::commandArgs(argc, argv);
std::unique_ptr<VFlow> top{new VFlow};
Verilated::traceEverOn(true);
VerilatedVcdC *m_trace = new VerilatedVcdC;
2024-03-13 06:53:31 +00:00
#ifdef VERILATOR_TRACE
2024-03-29 02:35:49 +00:00
top->trace(m_trace, 5);
m_trace->open("waveform.vcd");
2024-03-13 06:53:31 +00:00
#endif
2024-03-29 02:35:49 +00:00
init_vpi();
top->reset = 0;
for (sim_time = 10; sim_time < MAX_SIM_TIME; sim_time++) {
top->eval();
top->clock = !top->clock;
if(top->clock == 1) {
// Posedge
++posedge_cnt;
update_regs();
print_regs();
2024-03-13 06:53:31 +00:00
}
2024-03-29 02:35:49 +00:00
2024-03-13 06:53:31 +00:00
#ifdef VERILATOR_TRACE
2024-03-29 02:35:49 +00:00
m_trace->dump(sim_time);
2024-03-13 06:53:31 +00:00
#endif
2024-03-29 02:35:49 +00:00
}
2024-03-13 06:53:31 +00:00
#ifdef VERILATOR_TRACE
2024-03-29 02:35:49 +00:00
m_trace->close();
2024-03-13 06:53:31 +00:00
#endif
2024-03-29 02:35:49 +00:00
exit(EXIT_SUCCESS);
2024-03-13 06:53:31 +00:00
}