2017-09-24 01:18:07 +00:00
|
|
|
// DESCRIPTION: Verilator: Verilog example module
|
|
|
|
//
|
2020-03-21 15:24:24 +00:00
|
|
|
// 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
|
2017-09-24 01:18:07 +00:00
|
|
|
//======================================================================
|
|
|
|
|
2021-01-03 16:54:43 +00:00
|
|
|
// For std::unique_ptr
|
|
|
|
#include <memory>
|
|
|
|
|
2017-09-24 01:18:07 +00:00
|
|
|
// Include common routines
|
|
|
|
#include <verilated.h>
|
|
|
|
|
|
|
|
// Include model header, generated from Verilating "top.v"
|
|
|
|
#include "Vtop.h"
|
|
|
|
|
2021-03-27 12:45:13 +00:00
|
|
|
// Legacy function required only so linking works on Cygwin and MSVC++
|
|
|
|
double sc_time_stamp() { return 0; }
|
|
|
|
|
2022-11-23 23:50:31 +00:00
|
|
|
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.
|
2017-09-24 01:18:07 +00:00
|
|
|
|
|
|
|
// Prevent unused variable warnings
|
2022-11-23 23:50:31 +00:00
|
|
|
if (false && argc && argv) {}
|
2017-09-24 01:18:07 +00:00
|
|
|
|
2021-03-07 13:28:13 +00:00
|
|
|
// Create logs/ directory in case we have traces to put under it
|
|
|
|
Verilated::mkdir("logs");
|
|
|
|
|
2021-03-07 16:01:54 +00:00
|
|
|
// 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};
|
2022-05-15 14:50:44 +00:00
|
|
|
// Do not instead make Vtop as a file-scope static variable, as the
|
|
|
|
// "C++ static initialization order fiasco" may cause a crash
|
2021-03-07 16:01:54 +00:00
|
|
|
|
2017-09-24 01:18:07 +00:00
|
|
|
// 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
|
2021-03-07 16:01:54 +00:00
|
|
|
contextp->debug(0);
|
2017-09-24 01:18:07 +00:00
|
|
|
|
|
|
|
// Randomization reset policy
|
2021-03-07 13:28:13 +00:00
|
|
|
// May be overridden by commandArgs argument parsing
|
2021-03-07 16:01:54 +00:00
|
|
|
contextp->randReset(2);
|
2017-09-24 01:18:07 +00:00
|
|
|
|
2020-03-02 02:39:23 +00:00
|
|
|
// Verilator must compute traced signals
|
2021-03-07 16:01:54 +00:00
|
|
|
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
|
2021-03-07 16:01:54 +00:00
|
|
|
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.
|
2021-03-07 16:01:54 +00:00
|
|
|
const std::unique_ptr<Vtop> top{new Vtop{contextp.get(), "TOP"}};
|
2017-09-24 01:18:07 +00:00
|
|
|
|
2021-03-07 13:28:13 +00:00
|
|
|
// Set Vtop's input signals
|
2017-09-24 01:18:07 +00:00
|
|
|
top->reset_l = !0;
|
|
|
|
top->clk = 0;
|
|
|
|
top->in_small = 1;
|
|
|
|
top->in_quad = 0x1234;
|
2017-11-25 20:14:31 +00:00
|
|
|
top->in_wide[0] = 0x11111111;
|
2017-09-24 01:18:07 +00:00
|
|
|
top->in_wide[1] = 0x22222222;
|
2017-11-25 20:14:31 +00:00
|
|
|
top->in_wide[2] = 0x3;
|
2017-09-24 01:18:07 +00:00
|
|
|
|
|
|
|
// Simulate until $finish
|
2021-03-07 16:01:54 +00:00
|
|
|
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().
|
2021-03-07 16:01:54 +00:00
|
|
|
// 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
|
2021-03-07 16:01:54 +00:00
|
|
|
// 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.
|
2017-10-10 11:18:01 +00:00
|
|
|
|
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) {
|
2021-03-07 16:01:54 +00:00
|
|
|
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;
|
2017-10-10 11:18:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.)
|
2017-10-10 11:18:01 +00:00
|
|
|
top->eval();
|
|
|
|
|
|
|
|
// Read outputs
|
2022-01-01 21:04:20 +00:00
|
|
|
VL_PRINTF("[%" PRId64 "] clk=%x rstl=%x iquad=%" PRIx64 " -> oquad=%" PRIx64
|
|
|
|
" owide=%x_%08x_%08x\n",
|
2021-03-07 16:01:54 +00:00
|
|
|
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]);
|
2017-09-24 01:18:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2017-09-24 01:18:07 +00:00
|
|
|
#if VM_COVERAGE
|
2018-08-27 22:07:52 +00:00
|
|
|
Verilated::mkdir("logs");
|
2021-03-07 16:01:54 +00:00
|
|
|
contextp->coveragep()->write("logs/coverage.dat");
|
2017-09-24 01:18:07 +00:00
|
|
|
#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;
|
2017-09-24 01:18:07 +00:00
|
|
|
}
|