verilator/examples/make_tracing_c/sim_main.cpp

127 lines
4.7 KiB
C++
Raw Normal View History

// DESCRIPTION: Verilator: Verilog example module
//
// This file ONLY is placed under the Creative Commons Public Domain, for
// any use, without warranty, 2017 by Wilson Snyder.
// SPDX-License-Identifier: CC0-1.0
//======================================================================
2021-01-03 16:54:43 +00:00
// For std::unique_ptr
#include <memory>
// Include common routines
#include <verilated.h>
// Include model header, generated from Verilating "top.v"
#include "Vtop.h"
// Legacy function required only so linking works on Cygwin and MSVC++
double sc_time_stamp() { return 0; }
int main(int argc, char** argv) {
2019-10-06 14:32:49 +00:00
// This is a more complicated example, please also see the simpler examples/make_hello_c.
// Prevent unused variable warnings
if (false && argc && argv) {}
2021-03-07 13:28:13 +00:00
// Create logs/ directory in case we have traces to put under it
Verilated::mkdir("logs");
// Construct a VerilatedContext to hold simulation time, etc.
// Multiple modules (made later below with Vtop) may share the same
// context to share time, or modules may have different contexts if
// they should be independent from each other.
// Using unique_ptr is similar to
// "VerilatedContext* contextp = new VerilatedContext" then deleting at end.
const std::unique_ptr<VerilatedContext> contextp{new VerilatedContext};
// Do not instead make Vtop as a file-scope static variable, as the
// "C++ static initialization order fiasco" may cause a crash
// Set debug level, 0 is off, 9 is highest presently used
2021-03-07 13:28:13 +00:00
// May be overridden by commandArgs argument parsing
contextp->debug(0);
// Randomization reset policy
2021-03-07 13:28:13 +00:00
// May be overridden by commandArgs argument parsing
contextp->randReset(2);
2020-03-02 02:39:23 +00:00
// Verilator must compute traced signals
contextp->traceEverOn(true);
2020-03-02 02:39:23 +00:00
2019-03-02 01:14:48 +00:00
// Pass arguments so Verilated code can see them, e.g. $value$plusargs
// This needs to be called before you create any model
contextp->commandArgs(argc, argv);
2019-03-02 01:14:48 +00:00
2021-03-07 13:28:13 +00:00
// Construct the Verilated model, from Vtop.h generated from Verilating "top.v".
// Using unique_ptr is similar to "Vtop* top = new Vtop" then deleting at end.
// "TOP" will be the hierarchical name of the module.
const std::unique_ptr<Vtop> top{new Vtop{contextp.get(), "TOP"}};
2021-03-07 13:28:13 +00:00
// Set Vtop's input signals
top->reset_l = !0;
top->clk = 0;
top->in_small = 1;
top->in_quad = 0x1234;
top->in_wide[0] = 0x11111111;
top->in_wide[1] = 0x22222222;
top->in_wide[2] = 0x3;
// Simulate until $finish
while (!contextp->gotFinish()) {
2021-03-07 16:33:55 +00:00
// Historical note, before Verilator 4.200 Verilated::gotFinish()
// was used above in place of contextp->gotFinish().
// Most of the contextp-> calls can use Verilated:: calls instead;
2022-03-31 00:17:59 +00:00
// the Verilated:: versions just assume there's a single context
// being used (per thread). It's faster and clearer to use the
// newer contextp-> versions.
contextp->timeInc(1); // 1 timeprecision period passes...
2021-03-07 16:33:55 +00:00
// Historical note, before Verilator 4.200 a sc_time_stamp()
// function was required instead of using timeInc. Once timeInc()
// is called (with non-zero), the Verilated libraries assume the
// new API, and sc_time_stamp() will no longer work.
2020-03-06 00:09:58 +00:00
// Toggle a fast (time/2 period) clock
top->clk = !top->clk;
// Toggle control signals on an edge that doesn't correspond
// to where the controls are sampled; in this example we do
// this only on a negedge of clk, because we know
// reset is not sampled there.
if (!top->clk) {
if (contextp->time() > 1 && contextp->time() < 10) {
2020-03-06 00:09:58 +00:00
top->reset_l = !1; // Assert reset
} else {
top->reset_l = !0; // Deassert reset
}
// Assign some other inputs
top->in_quad += 0x12;
}
// Evaluate model
2020-03-02 02:39:23 +00:00
// (If you have multiple models being simulated in the same
// timestep then instead of eval(), call eval_step() on each, then
2021-03-07 13:28:13 +00:00
// eval_end_step() on each. See the manual.)
top->eval();
// Read outputs
VL_PRINTF("[%" PRId64 "] clk=%x rstl=%x iquad=%" PRIx64 " -> oquad=%" PRIx64
" owide=%x_%08x_%08x\n",
contextp->time(), top->clk, top->reset_l, top->in_quad, top->out_quad,
top->out_wide[2], top->out_wide[1], top->out_wide[0]);
}
// Final model cleanup
top->final();
2021-03-07 13:28:13 +00:00
// Coverage analysis (calling write only after the test is known to pass)
#if VM_COVERAGE
2018-08-27 22:07:52 +00:00
Verilated::mkdir("logs");
contextp->coveragep()->write("logs/coverage.dat");
#endif
2021-01-03 16:57:29 +00:00
// Return good completion status
2021-02-13 22:06:53 +00:00
// Don't use exit() or destructor won't get called
return 0;
}