Skip to main content

SystemC

info

The source code for this example can be found in examples/systemc

Description

This example shows how to do simulation using SystemC with SoCMake.

In this case, we will have a main SystemC file sc_main.cpp that will instantiate a module and a function and use both in a test.

Example

Directory structure

Lets take a look at the directory structure of the example first.

.
├── CMakeLists.txt
└── sc_main.cpp

sc_main.cpp

This is a standard SystemC file. It contains a C++ function to print a message and also a SystemC module, that will also print a message. The main function will then try the 2 approaches by calling the function and instantiating the SystemC module.

sc_main.cpp
// Learn with Examples, 2020, MIT license
#include <systemc> // include the systemC header file
using namespace sc_core; // use namespace

void hello1() { // a normal c++ function
std::cout << "Hello world using approach 1" << std::endl;
}

struct HelloWorld : sc_module { // define a systemC module
SC_CTOR(HelloWorld) {// constructor function, to be explained later
SC_METHOD(hello2); // register a member function to the kernel
}
void hello2(void) { // a function for systemC simulation kernel, void inside () can be omitted
std::cout << "Hello world using approach 2" << std::endl;
}
};

int sc_main(int, char*[]) { // entry point
hello1(); // approach #1: manually invoke a normal function
HelloWorld helloworld("helloworld"); // approach #2, instantiate a systemC module
sc_start(); // let systemC simulation kernel to invoke helloworld.hello2();
return 0;
}

CMakeLists.txt

And finally we need a top CMakeLists.txt that will assemble the full project and create simulation targets.

CMakeLists.txt
cmake_minimum_required(VERSION 3.27)
project(systemc_example CXX)

include("../../SoCMakeConfig.cmake")

systemc_build(VERSION 3.0.0 EXACT_VERSION)

add_executable(systemc_example
sc_main.cpp
)

target_link_libraries(systemc_example PUBLIC
SystemC::systemc)

add_custom_target(run_systemc_example
COMMAND systemc_example)

As usual, we add the executable to our project and we link the SystemC library using the target_link_libraries function.

It can be useful to create a custom target, to directly build and execute the target instead of manually executing it.

Running the simulation

Simulation can be run the same way as always:

mkdir build
cd build
cmake ../ # Configure project
make run_systemc_example -j$(nproc) # Build and run testbench
info

It's possible to use the cmake argument -DFETCHCONTENT_BASE_DIR=... if you want to build systemc in a specific location.