diff --git a/npc/csrc/Flow/main.cpp b/npc/csrc/Flow/main.cpp index 3fd0129..536716d 100644 --- a/npc/csrc/Flow/main.cpp +++ b/npc/csrc/Flow/main.cpp @@ -1,12 +1,11 @@ -#include "VFlow___024root.h" -#include "tracer.h" #include #include +#include #include +#include #include #include #include -#include #include #include #include @@ -15,18 +14,15 @@ #define MAX_SIM_TIME 100 #define VERILATOR_TRACE -std::vector regsHandle; -int regs[32]; - template class Tracer { #ifdef VERILATOR_TRACE std::shared_ptr top; std::unique_ptr m_trace; - uint64_t time = 0; + uint64_t cycle = 0; #endif public: - Tracer(std::shared_ptr top, std::filesystem::path wavefile) { + Tracer(T *top, std::filesystem::path wavefile) { #ifdef VERILATOR_TRACE top = top; Verilated::traceEverOn(true); @@ -46,7 +42,7 @@ class Tracer { */ void update() { #ifdef VERILATOR_TRACE - m_trace->dump(time++); + m_trace->dump(cycle++); #endif } }; @@ -88,6 +84,8 @@ class _RegistersVPI : public _RegistersBase { } }; +typedef _RegistersVPI Registers; + template class Memory { std::array mem; @@ -101,30 +99,57 @@ class Memory { } }; -typedef _RegistersVPI Registers; -static int sim_time = 0; +template +class VlModuleInterfaceCommon : public T { + uint64_t sim_time = 0; + uint64_t posedge_cnt = 0; + std::unique_ptr> tracer; + public: + VlModuleInterfaceCommon(bool do_trace, std::filesystem::path wavefile = "waveform.vcd") { + if(do_trace) tracer = std::make_unique>(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 VlModule; int main(int argc, char **argv, char **env) { - int sim_time = 0; - int posedge_cnt = 0; Verilated::commandArgs(argc, argv); - auto top = std::make_shared(); - auto top_tracer = std::make_unique>(top, "waveform.vcd"); + auto top = std::make_shared(false, "waveform.vcd"); Registers regs("TOP.Flow.reg_0.regFile_"); - top->reset = 0; - top->eval(); - for (sim_time = 10; sim_time < MAX_SIM_TIME; sim_time++) { - top->clock = !top->clock; - if(top->clock == 1) { + top->reset_eval(10); + for (int i = 0; i < MAX_SIM_TIME; i++) { + if(top->is_posedge()) { // Posedge - ++posedge_cnt; regs.update(); regs.print_regs(); } top->eval(); } - exit(EXIT_SUCCESS); + return 0; }