-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_vsim.cpp
130 lines (98 loc) · 2.43 KB
/
example_vsim.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// C++ "driver" for UPduino example
//
// vim: set et ts=4 sw=4
//
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "verilated.h"
#include "Vexample_top.h"
// 1 to save FST waveform trace file
#define VM_TRACE 1
#include "verilated_fst_c.h" // for VM_TRACE
#define LOGDIR "logs/"
// Current simulation time (64-bit unsigned)
vluint64_t main_time = 0;
volatile bool done;
static FILE *logfile;
static char log_buff[16384];
static void log_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf(log_buff, sizeof(log_buff), fmt, args);
fputs(log_buff, stdout);
fputs(log_buff, logfile);
va_end(args);
}
static void logonly_printf(const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vsnprintf(log_buff, sizeof(log_buff), fmt, args);
fputs(log_buff, logfile);
va_end(args);
}
void ctrl_c(int s)
{
(void)s;
done = true;
}
// Called by $time in Verilog
double sc_time_stamp()
{
return main_time;
}
int main(int argc, char **argv)
{
struct sigaction sigIntHandler;
sigIntHandler.sa_handler = ctrl_c;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
if ((logfile = fopen(LOGDIR "example_vsim.log", "w")) == NULL)
{
printf("can't create " LOGDIR "example_vsim.log\n");
exit(EXIT_FAILURE);
}
log_printf("\nSimulation started\n");
Verilated::commandArgs(argc, argv);
#if VM_TRACE
Verilated::traceEverOn(true);
#endif
Vexample_top *top = new Vexample_top;
#if VM_TRACE
const auto trace_path = LOGDIR "example_vsim.fst";
logonly_printf("Writing FST waveform file to \"%s\"...\n", trace_path);
VerilatedFstC *tfp = new VerilatedFstC;
top->trace(tfp, 99); // trace to heirarchal depth of 99
tfp->open(trace_path);
#endif
while (!done && !Verilated::gotFinish())
{
top->gpio_20 = 1; // clock rising
top->eval();
#if VM_TRACE
tfp->dump(main_time);
#endif
main_time++;
top->gpio_20 = 0; // clock falling
top->eval();
#if VM_TRACE
tfp->dump(main_time);
#endif
main_time++;
if (main_time >= 10000000ull)
{
done = true;
}
}
top->final();
#if VM_TRACE
tfp->close();
#endif
log_printf("Simulation ended after %lu clock ticks\n",
(main_time / 2));
return EXIT_SUCCESS;
}