CIRCT  20.0.0git
driver.cpp
Go to the documentation of this file.
1 //===- driver.cpp - ESI Verilator software driver -------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // A fairly standard, boilerplate Verilator C++ simulation driver. Assumes the
10 // top level exposes just two signals: 'clk' and 'rst'.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef TOP_MODULE
15 #define TOP_MODULE ESI_Cosim_Top
16 #endif // TOP_MODULE
17 
18 // Macro black magic to get the header file name and class name from the
19 // TOP_MODULE macro. Need to disable formatting for this section, as
20 // clang-format messes it up by inserting spaces.
21 
22 // clang-format off
23 #define STRINGIFY_MACRO(x) STR(x)
24 #define STR(x) #x
25 #define EXPAND(x)x
26 #define CONCAT3(n1, n2, n3) STRINGIFY_MACRO(EXPAND(n1)EXPAND(n2)EXPAND(n3))
27 #define TOKENPASTE(x, y) x ## y
28 #define CLASSNAME(x, y) TOKENPASTE(x, y)
29 
30 #include CONCAT3(V,TOP_MODULE,.h)
31 // clang-format on
32 
33 #include "verilated_vcd_c.h"
34 
35 #include "signal.h"
36 #include <iostream>
37 #include <thread>
38 
39 vluint64_t timeStamp;
40 
41 // Stop the simulation gracefully on ctrl-c.
42 volatile bool stopSimulation = false;
43 void handle_sigint(int) { stopSimulation = true; }
44 
45 // Called by $time in Verilog.
46 double sc_time_stamp() { return timeStamp; }
47 
48 int main(int argc, char **argv) {
49  // Register graceful exit handler.
50  signal(SIGINT, handle_sigint);
51 
52  Verilated::commandArgs(argc, argv);
53 
54  // Construct the simulated module's C++ model.
55  auto &dut = *new CLASSNAME(V, TOP_MODULE)();
56  char *waveformFile = getenv("SAVE_WAVE");
57 
58  char *periodStr = getenv("DEBUG_PERIOD");
59  unsigned debugPeriod = 0;
60  if (periodStr) {
61  debugPeriod = std::stoi(periodStr);
62  std::cout << "[driver] Setting debug period to " << debugPeriod
63  << std::endl;
64  }
65 
66  VerilatedVcdC *tfp = nullptr;
67  if (waveformFile) {
68 #ifdef TRACE
69  tfp = new VerilatedVcdC();
70  Verilated::traceEverOn(true);
71  dut.trace(tfp, 99); // Trace 99 levels of hierarchy
72  tfp->open(waveformFile);
73  std::cout << "[driver] Writing trace to " << waveformFile << std::endl;
74 #else
75  std::cout
76  << "[driver] Warning: waveform file specified, but not a debug build"
77  << std::endl;
78 #endif
79  }
80 
81  std::cout << "[driver] Starting simulation" << std::endl;
82 
83  // TODO: Add max speed (cycles per second) option for small, interactive
84  // simulations to reduce waveform for debugging. Should this be a command line
85  // option or configurable over the cosim interface?
86 
87  // Reset.
88  dut.rst = 1;
89  dut.clk = 0;
90 
91  // TODO: Support ESI reset handshake in the future.
92  // Run for a few cycles with reset held.
93  for (timeStamp = 0; timeStamp < 8 && !Verilated::gotFinish(); timeStamp++) {
94  dut.eval();
95  dut.clk = !dut.clk;
96  if (tfp)
97  tfp->dump(timeStamp);
98  }
99 
100  // Take simulation out of reset.
101  dut.rst = 0;
102 
103  // Run for the specified number of cycles out of reset.
104  for (; !Verilated::gotFinish() && !stopSimulation; timeStamp++) {
105  dut.eval();
106  dut.clk = !dut.clk;
107  if (tfp)
108  tfp->dump(timeStamp);
109  if (debugPeriod)
110  std::this_thread::sleep_for(std::chrono::milliseconds(debugPeriod));
111  }
112 
113  // Tell the simulator that we're going to exit. This flushes the output(s) and
114  // frees whatever memory may have been allocated.
115  dut.final();
116  if (tfp)
117  tfp->close();
118 
119  std::cout << "[driver] Ending simulation at tick #" << timeStamp << std::endl;
120  return 0;
121 }
int main(int argc, char **argv)
Definition: driver.cpp:48
vluint64_t timeStamp
Definition: driver.cpp:39
#define CLASSNAME(x, y)
Definition: driver.cpp:28
void handle_sigint(int)
Definition: driver.cpp:43
volatile bool stopSimulation
Definition: driver.cpp:42
double sc_time_stamp()
Definition: driver.cpp:46
#define TOP_MODULE
Definition: driver.cpp:15