Example SystemC Execution

This is an example similar to the Example C++ Execution, but using SystemC. We’ll also explicitly run make.

First you need Verilator installed, see Installation. In brief, if you installed Verilator using the package manager of your operating system, or did a make install to place Verilator into your default path, you do not need anything special in your environment, and should not have VERILATOR_ROOT set. However, if you installed Verilator from sources and want to run Verilator out of where you compiled Verilator, you need to point to the kit:

# See above; don't do this if using an OS-distributed Verilator
export VERILATOR_ROOT=/path/to/where/verilator/was/installed
export PATH=$VERILATOR_ROOT/bin:$PATH

Now, let’s create an example Verilog, and SystemC wrapper file:

mkdir test_our_sc
cd test_our_sc

cat >our.v <<'EOF'
  module our (clk);
     input clk;  // Clock is required to get initial activation
     always @(posedge clk)
        begin $display("Hello World"); $finish; end
  endmodule
EOF

cat >sc_main.cpp <<'EOF'
  #include "Vour.h"
  int sc_main(int argc, char** argv) {
      Verilated::commandArgs(argc, argv);
      sc_clock clk{"clk", 10, SC_NS, 0.5, 3, SC_NS, true};
      Vour* top = new Vour{"top"};
      top->clk(clk);
      while (!Verilated::gotFinish()) { sc_start(1, SC_NS); }
      delete top;
      return 0;
  }
EOF

Now we run Verilator on our little example:

verilator --sc --exe -Wall sc_main.cpp our.v

This example does not use –build, therefore we need to explicitly compile it:

make -j -C obj_dir -f Vour.mk Vour

And now we run it:

obj_dir/Vour

And we get, after the SystemC banner, the same output as the C++ example:

SystemC 2.3.3-Accellera

Hello World
- our.v:4: Verilog $finish

Really, you’re better off using a Makefile to run the steps for you so when your source changes it will automatically run all of the appropriate steps. For examples that do this see the examples directory in the distribution.