-
Notifications
You must be signed in to change notification settings - Fork 19
/
testb.h
333 lines (306 loc) · 9.4 KB
/
testb.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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
////////////////////////////////////////////////////////////////////////////////
//
// Filename: ../demo-out/testb.h
// {{{
// Project: AutoFPGA, a utility for composing FPGA designs from peripherals
//
// DO NOT EDIT THIS FILE!
// Computer Generated: This file is computer generated by AUTOFPGA. DO NOT EDIT.
// DO NOT EDIT THIS FILE!
//
// CmdLine: ./autofpga -d -o ../demo-out -I ../auto-data bkram.txt buserr.txt clkcounter.txt clock.txt enet.txt flash.txt global.txt gpio.txt gps.txt hdmi.txt icape.txt legalgen.txt mdio.txt pic.txt pwrcount.txt rtcdate.txt rtcgps.txt sdram.txt sdspi.txt spio.txt version.txt wbmouse.txt wboledbw.txt wbpmic.txt wbscopc.txt wbscope.txt wbubus.txt xpander.txt zipmaster.txt
//
// Creator: Dan Gisselquist, Ph.D.
// Gisselquist Technology, LLC
//
////////////////////////////////////////////////////////////////////////////////
// }}}
// Copyright (C) 2017-2021, Gisselquist Technology, LLC
// {{{
// This program is free software (firmware): you can redistribute it and/or
// modify it under the terms of the GNU General Public License as published
// by the Free Software Foundation, either version 3 of the License, or (at
// your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
// for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program. (It's in the $(ROOT)/doc directory. Run make with no
// target there if the PDF file isn't present.) If not, see
// <http://www.gnu.org/licenses/> for a copy.
// }}}
// License: GPL, v3, as defined and found on www.gnu.org,
// {{{
// http://www.gnu.org/licenses/gpl.html
//
////////////////////////////////////////////////////////////////////////////////
//
// }}}
#ifndef TESTB_H
#define TESTB_H
// #define TRACE_FST
#include <stdio.h>
#include <stdint.h>
#ifdef TRACE_FST
#define TRACECLASS VerilatedFstC
#include <verilated_fst_c.h>
#else // TRACE_FST
#define TRACECLASS VerilatedVcdC
#include <verilated_vcd_c.h>
#endif
#include <tbclock.h>
//
// The TESTB class is a useful wrapper for interacting with a Verilator
// based design. Key to its capabilities are the tick() method for
// advancing the simulation timestep, and the opentrace() and
// closetrace() methods for handling VCD tracefile generation. To
// use a non-VCD trace, redefine TRACECLASS before calling this
// function to the trace class you wish to use.
//
template <class VA> class TESTB {
public:
VA *m_core;
bool m_changed;
TRACECLASS* m_trace;
bool m_done, m_paused_trace;
uint64_t m_time_ps;
// TBCLOCK is a clock support class, enabling multiclock simulation
// operation.
TBCLOCK m_hdmi_out_clk;
TBCLOCK m_clk;
TBCLOCK m_net_rx_clk;
TBCLOCK m_hdmi_in_clk;
TBCLOCK m_hdmi_in_hsclk;
TESTB(void) {
// {{{
m_core = new VA;
m_time_ps = 0ul;
m_trace = NULL;
m_done = false;
m_paused_trace = false;
Verilated::traceEverOn(true);
// Set the initial clock periods
m_hdmi_out_clk.init(6734); // 148.50 MHz
m_clk.init(10000); // 100.00 MHz
m_net_rx_clk.init(8000); // 125.00 MHz
m_hdmi_in_clk.init(6734); // 148.50 MHz
m_hdmi_in_hsclk.init(673); // 1485.88 MHz
}
// }}}
virtual ~TESTB(void) {
// {{{
if (m_trace) m_trace->close();
delete m_core;
m_core = NULL;
}
// }}}
//
// opentrace()
// {{{
//
// Useful for beginning a (VCD) trace. To open such a trace, just call
// opentrace() with the name of the VCD file you'd like to trace
// everything into
virtual void opentrace(const char *vcdname, int depth=99) {
if (!m_trace) {
m_trace = new TRACECLASS;
m_core->trace(m_trace, 99);
m_trace->spTrace()->set_time_resolution("ps");
m_trace->spTrace()->set_time_unit("ps");
m_trace->open(vcdname);
m_paused_trace = false;
}
}
// }}}
//
// trace()
// {{{
// A synonym for opentrace() above.
//
void trace(const char *vcdname) {
opentrace(vcdname);
}
// }}}
//
// pausetrace(pause)
// {{{
// Set/clear a flag telling us whether or not to write to the VCD trace
// file. The default is to write to the file, but this can be changed
// by calling pausetrace. pausetrace(false) will resume tracing,
// whereas pausetrace(true) will stop all calls to Verilator's trace()
// function
//
virtual bool pausetrace(bool pausetrace) {
m_paused_trace = pausetrace;
return m_paused_trace;
}
// }}}
//
// pausetrace()
// {{{
// Like pausetrace(bool) above, except that pausetrace() will return
// the current status of the pausetrace flag above. Specifically, it
// will return true if the trace has been paused or false otherwise.
virtual bool pausetrace(void) {
return m_paused_trace;
}
// }}}
//
// closetrace()
// {{{
// Closes the open trace file. No more information will be written
// to it
virtual void closetrace(void) {
if (m_trace) {
m_trace->close();
delete m_trace;
m_trace = NULL;
}
}
// }}}
//
// eval()
// {{{
// This is a synonym for Verilator's eval() function. It evaluates all
// of the logic within the design. AutoFPGA based designs shouldn't
// need to be calling this, they should call tick() instead. However,
// in the off chance that your design inputs depend upon combinatorial
// expressions that would be output based upon other input expressions,
// you might need to call this function.
virtual void eval(void) {
m_core->eval();
}
// }}}
//
// tick()
// {{{
// tick() is the main entry point into this helper core. In general,
// tick() will advance the clock by one clock tick. In a multiple clock
// design, this will advance the clocks up until the nearest clock
// transition.
virtual void tick(void) {
unsigned mintime = m_hdmi_out_clk.time_to_edge();
if (m_clk.time_to_edge() < mintime)
mintime = m_clk.time_to_edge();
if (m_net_rx_clk.time_to_edge() < mintime)
mintime = m_net_rx_clk.time_to_edge();
if (m_hdmi_in_clk.time_to_edge() < mintime)
mintime = m_hdmi_in_clk.time_to_edge();
if (m_hdmi_in_hsclk.time_to_edge() < mintime)
mintime = m_hdmi_in_hsclk.time_to_edge();
assert(mintime > 1);
// Pre-evaluate, to give verilator a chance to settle any
// combinatorial logic thatthat may have changed since the
// last clockevaluation, and then record that in the trace.
eval();
if (m_trace && !m_paused_trace) m_trace->dump(m_time_ps+1);
// Advance each clock
m_core->i_hdmi_out_clk = m_hdmi_out_clk.advance(mintime);
m_core->i_clk = m_clk.advance(mintime);
m_core->i_net_rx_clk = m_net_rx_clk.advance(mintime);
m_core->i_hdmi_in_clk = m_hdmi_in_clk.advance(mintime);
m_core->i_hdmi_in_hsclk = m_hdmi_in_hsclk.advance(mintime);
m_time_ps += mintime;
eval();
// If we are keeping a trace, dump the current state to that
// trace now
if (m_trace && !m_paused_trace) {
m_trace->dump(m_time_ps);
m_trace->flush();
}
if (m_hdmi_out_clk.falling_edge()) {
m_changed = true;
sim_hdmi_out_clk_tick();
}
if (m_clk.falling_edge()) {
m_changed = true;
sim_clk_tick();
}
if (m_net_rx_clk.falling_edge()) {
m_changed = true;
sim_net_rx_clk_tick();
}
if (m_hdmi_in_clk.falling_edge()) {
m_changed = true;
sim_hdmi_in_clk_tick();
}
if (m_hdmi_in_hsclk.falling_edge()) {
m_changed = true;
sim_hdmi_in_hsclk_tick();
}
}
// }}}
virtual void sim_hdmi_out_clk_tick(void) {
// {{{
// AutoFPGA will override this method within main_tb.cpp if any
// @SIM.TICK key is present within a design component also
// containing a @SIM.CLOCK key identifying this clock. That
// component must also set m_changed to true.
m_changed = false;
}
// }}}
virtual void sim_clk_tick(void) {
// {{{
// AutoFPGA will override this method within main_tb.cpp if any
// @SIM.TICK key is present within a design component also
// containing a @SIM.CLOCK key identifying this clock. That
// component must also set m_changed to true.
m_changed = false;
}
// }}}
virtual void sim_net_rx_clk_tick(void) {
// {{{
// AutoFPGA will override this method within main_tb.cpp if any
// @SIM.TICK key is present within a design component also
// containing a @SIM.CLOCK key identifying this clock. That
// component must also set m_changed to true.
m_changed = false;
}
// }}}
virtual void sim_hdmi_in_clk_tick(void) {
// {{{
// AutoFPGA will override this method within main_tb.cpp if any
// @SIM.TICK key is present within a design component also
// containing a @SIM.CLOCK key identifying this clock. That
// component must also set m_changed to true.
m_changed = false;
}
// }}}
virtual void sim_hdmi_in_hsclk_tick(void) {
// {{{
// AutoFPGA will override this method within main_tb.cpp if any
// @SIM.TICK key is present within a design component also
// containing a @SIM.CLOCK key identifying this clock. That
// component must also set m_changed to true.
m_changed = false;
}
// }}}
virtual bool done(void) {
// {{{
if (m_done)
return true;
if (Verilated::gotFinish())
m_done = true;
return m_done;
}
// }}}
//
// reset()
// {{{
// Sets the i_reset input for one clock tick. It's really just a
// function for the capabilies shown below. You'll want to reset any
// external input values before calling this though.
virtual void reset(void) {
m_core->i_reset = 1;
tick();
while(!m_core->i_clk)
tick();
m_core->i_reset = 0;
// printf("RESET\n");
}
// }}}
};
#endif // TESTB