upsilon/firmware/rtl/testbench.hpp

38 lines
619 B
C++

#pragma once
#include <verilated.h>
#include "util.hpp"
/* https://zipcpu.com/blog/2017/06/21/looking-at-verilator.html */
template <class TOP> class TB {
int tick_count;
int bailout;
public:
TOP mod;
TB(int argc, char *argv[], int _bailout = 0) : mod(), bailout(_bailout) {
mod.clk = 0;
tick_count = 0;
}
virtual ~TB() {
mod.final();
}
void run_clock() {
mod.clk = !mod.clk;
mod.eval();
Verilated::timeInc(1);
mod.clk = !mod.clk;
mod.eval();
Verilated::timeInc(1);
tick_count++;
if (bailout > 0 && tick_count >= bailout)
exit(1);
if (Verilated::gotError())
exit(1);
}
};