-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestbench.h
73 lines (67 loc) · 1.47 KB
/
testbench.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "verilated.h"
#include "verilated_vcd_c.h"
#include <functional>
template <class Module> class Testbench {
public:
unsigned long tickcount;
Module* core;
VerilatedVcdC* trace;
bool print;
std::function<void(void)> tickhandler;
Testbench() : tickcount(0), trace(NULL) {
core = new Module;
Verilated::traceEverOn(true);
print = 1;
}
virtual ~Testbench(void){
if(trace) trace->close();
delete core;
core = NULL;
}
virtual void reset(void){
#ifndef NO_RESET
core->rst = 1;
this->tick();
core->rst = 0;
#endif
}
virtual void tick(void){
tickcount++;
core->clk = 0;
core->eval();
if(trace) trace->dump(10*tickcount-2);
core->clk = 1;
if(tickhandler)tickhandler();
core->eval();
if(trace) trace->dump(10*tickcount);
core->clk = 0;
if(tickhandler)tickhandler();
core->eval();
if(trace){
trace->dump(10*tickcount+5);
trace->flush();
}
}
void register_tick_handler(std::function<void(void)> handler){
tickhandler = handler;
}
virtual void cycles(int cycles){
for(int i=0;i<cycles;i++){
this->tick();
}
}
virtual void opentrace(const char* vcdname){
if(!trace){
trace = new VerilatedVcdC();
core->trace(trace, 99);
trace->open(vcdname);
}
}
virtual void closetrace(void){
if(trace){
trace->close();
trace = NULL;
}
}
virtual bool done(void) { return (Verilated::gotFinish());}
};