Files
verilator/examples/make_tracing_sc/sc_main.cpp
T

140 lines
4.0 KiB
C++
Raw Normal View History

2008-06-09 21:25:10 -04:00
// -*- SystemC -*-
2006-08-26 11:35:28 +00:00
// DESCRIPTION: Verilator Example: Top level main for invoking SystemC model
//
// This file ONLY is placed under the Creative Commons Public Domain.
// SPDX-FileCopyrightText: 2017 Wilson Snyder
// SPDX-License-Identifier: CC0-1.0
//======================================================================
2006-08-26 11:35:28 +00:00
2021-01-03 11:54:43 -05:00
// For std::unique_ptr
#include <memory>
// SystemC global header
#include <systemc>
2006-08-26 11:35:28 +00:00
// Include common routines
#include <verilated.h>
#if VM_TRACE
#include <verilated_vcd_sc.h>
#endif
#include <sys/stat.h> // mkdir
2006-08-26 11:35:28 +00:00
// Include model header, generated from Verilating "top.v"
#include "Vtop.h"
2006-08-26 11:35:28 +00:00
using namespace sc_core;
using namespace sc_dt;
2006-08-26 11:35:28 +00:00
int sc_main(int argc, char* argv[]) {
2019-10-06 10:32:49 -04:00
// This is a more complicated example, please also see the simpler examples/make_hello_c.
2021-03-07 08:28:13 -05:00
// Create logs/ directory in case we have traces to put under it
Verilated::mkdir("logs");
// Set debug level, 0 is off, 9 is highest presently used
2021-03-07 08:28:13 -05:00
// May be overridden by commandArgs argument parsing
Verilated::debug(0);
// Randomization reset policy
2021-03-07 08:28:13 -05:00
// May be overridden by commandArgs argument parsing
2006-08-26 11:35:28 +00:00
Verilated::randReset(2);
2021-03-07 08:28:13 -05:00
#if VM_TRACE
// Before any evaluation, need to know to calculate those signals only used for tracing
Verilated::traceEverOn(true);
#endif
2019-03-01 20:14:48 -05:00
// Pass arguments so Verilated code can see them, e.g. $value$plusargs
// This needs to be called before you create any model
Verilated::commandArgs(argc, argv);
2006-08-26 11:35:28 +00:00
// General logfile
std::ios::sync_with_stdio();
2006-08-26 11:35:28 +00:00
// Define clocks
2021-01-03 11:54:43 -05:00
sc_clock clk{"clk", 10, SC_NS, 0.5, 3, SC_NS, true};
sc_clock fastclk{"fastclk", 2, SC_NS, 0.5, 2, SC_NS, true};
// Define interconnect
2006-08-26 11:35:28 +00:00
sc_signal<bool> reset_l;
sc_signal<uint32_t> in_small;
sc_signal<uint64_t> in_quad;
2021-01-03 11:57:29 -05:00
sc_signal<sc_bv<70>> in_wide;
sc_signal<uint32_t> out_small;
sc_signal<uint64_t> out_quad;
2021-01-03 11:57:29 -05:00
sc_signal<sc_bv<70>> out_wide;
2006-08-26 11:35:28 +00:00
// Construct the Verilated model, from inside Vtop.h
2021-01-03 11:54:43 -05:00
// Using unique_ptr is similar to "Vtop* top = new Vtop" then deleting at end
const std::unique_ptr<Vtop> top{new Vtop{"top"}};
2021-03-07 08:28:13 -05:00
// Attach Vtop's signals to this upper model
top->clk(clk);
top->fastclk(fastclk);
top->reset_l(reset_l);
top->in_small(in_small);
top->in_quad(in_quad);
top->in_wide(in_wide);
top->out_small(out_small);
top->out_quad(out_quad);
top->out_wide(out_wide);
2006-08-26 11:35:28 +00:00
// You must do one evaluation before enabling waves, in order to allow
// SystemC to interconnect everything for testing.
sc_start(SC_ZERO_TIME);
2006-08-26 11:35:28 +00:00
#if VM_TRACE
2017-11-05 06:51:29 -05:00
// If verilator was invoked with --trace argument,
// and if at run time passed the +trace argument, turn on tracing
2020-08-15 10:12:55 -04:00
VerilatedVcdSc* tfp = nullptr;
2017-11-05 06:51:29 -05:00
const char* flag = Verilated::commandArgsPlusMatch("trace");
if (flag && 0 == std::strcmp(flag, "+trace")) {
2022-11-27 08:32:34 -05:00
std::cout << "Enabling waves into logs/vlt_dump.vcd...\n";
2018-01-29 19:07:49 -05:00
tfp = new VerilatedVcdSc;
2018-12-05 20:14:22 -05:00
top->trace(tfp, 99); // Trace 99 levels of hierarchy
2018-08-27 18:07:52 -04:00
Verilated::mkdir("logs");
2018-01-29 19:07:49 -05:00
tfp->open("logs/vlt_dump.vcd");
2017-11-05 06:51:29 -05:00
}
2006-08-26 11:35:28 +00:00
#endif
// Simulate until $finish
while (!Verilated::gotFinish()) {
#if VM_TRACE
2017-10-10 07:18:01 -04:00
// Flush the wave files each cycle so we can immediately see the output
// Don't do this in "real" programs, do it in an abort() handler instead
if (tfp) tfp->flush();
2018-01-29 19:07:49 -05:00
#endif
2017-10-10 07:18:01 -04:00
// Apply inputs
if (sc_time_stamp() > sc_time(1, SC_NS) && sc_time_stamp() < sc_time(10, SC_NS)) {
reset_l = !1; // Assert reset
2020-04-07 20:55:47 -04:00
} else {
reset_l = !0; // Deassert reset
2017-10-10 07:18:01 -04:00
}
2018-01-29 19:07:49 -05:00
2017-10-10 07:18:01 -04:00
// Simulate 1ns
2020-04-07 20:55:47 -04:00
sc_start(1, SC_NS);
2006-08-26 11:35:28 +00:00
}
// Final model cleanup
top->final();
2006-08-26 11:35:28 +00:00
2018-01-29 19:07:49 -05:00
// Close trace if opened
#if VM_TRACE
if (tfp) {
tfp->close();
2020-08-15 10:12:55 -04:00
tfp = nullptr;
}
2018-01-29 19:07:49 -05:00
#endif
2021-03-07 08:28:13 -05:00
// Coverage analysis (calling write only after the test is known to pass)
#if VM_COVERAGE
2018-08-27 18:07:52 -04:00
Verilated::mkdir("logs");
2015-08-12 19:18:58 -04:00
VerilatedCov::write("logs/coverage.dat");
#endif
2006-08-26 11:35:28 +00:00
2021-01-03 11:57:29 -05:00
// Return good completion status
return 0;
2006-08-26 11:35:28 +00:00
}