Introduction To Systemc: - Systemc Language Reference Manual
Introduction To Systemc: - Systemc Language Reference Manual
SystemC
Reference:
• SystemC: From the Ground Up 2nd edition by David C.
Black
• SystemC Language Reference Manual
Standard Methodology for ICs
• System-level designers write a C or C++ model
• Written in a stylized, hardware-like form
• Sometimes refined to be more hardware-like
• C/C++ model simulated to verify functionality
• Model given to Verilog/VHDL coders
• Verilog or VHDL specification written
• Models simulated together to test equivalence
• Verilog/VHDL model synthesized
Idea of SystemC
• C and C++ are being used as ad-hoc modeling
languages
• Why not formalize their use?
• Why not interpret them as hardware specification
languages just as Verilog and VHDL were?
• SystemC developed at Synopsys to do just this
What Is SystemC?
• A subset of C++ that models/specifies
synchronous digital hardware
• Linux platform
• GCC compiler
• Clocks are special signals that run periodically and can trigger
clocked processes
• Simulation involves
• Creating objects of this class
SC_MODULE(<module name>)
{..........
............//these steps are described next
SC_CTOR(<module name>)
{...........
............//these steps are described next
}
};
SC_MODULE(<module name>)
{
sc_in<datatype> port1; Note that the constructor itself
calls the process which means that
sc_out<datatype> port2; • all the processes are executed
when ever an object of the
module is instantiated or
• the value of any of the signal or
SC_CTOR(<module name>) port in the sensitivity list
{ changes.
SC_METHOD(<process name>);
sensitive << <sensitivity list>;
};
Modules
SC_MODULE(mymod) {
/* port definitions */
/* signal definitions */
/* clock definitions */
/* storage and state variables */
/* process definitions */
SC_CTOR(mymod) {
/* Instances of processes and modules */
}
};
Ports
• Define the interface to each module
• Channels through which data is communicated
• Port consists of a direction
• input sc_in
• output sc_out
• bidirectional sc_inout
• and any C++ or SystemC type
Ports
SC_MODULE(mymod) {
sc_in<bool> load, read;
sc_inout<int> data;
sc_out<bool> full;
/* … */
SC_CTOR(mymod) {
/* Instances of modules that connect to the signals */
}
};
Processes
• Only thing in SystemC that actually does
anything
always block
initial block
void inverter() {
bool internal; Read a value from the port
internal = in;
out = ~internal; Write a value to an output
port
}
THREAD Processes
• Triggered in response to changes on inputs
};
THREAD Processes
• Reawakened whenever an input changes
• State saved between invocations
• Infinite loops should contain a wait()
Relinquish control until
void toggler() { the next change of a
bool last = false; signal on the
for (;;) { sensitivity list for this
last = in; out = last; wait(); process
last = ~in; out = last; wait();
}
}
CTHREAD Processes
• Triggered in response to a single clock edge
• How to declare
• IN : sc_in<port_type>
• OUT : sc_out<port_type>
• Bi-Directional : sc_inout<port_type>
Ports and Signals
• How to read and write a port ?
• Examples:
source /cad/cshrc
sc_main +gui
Clocks
• Special object
• How to create ?
sc_clock clock_name (
“clock_label”, period, duty_ratio, offset, initial_value );
• Clock connection
f1.clk( clk_signal ); //where f1 is a module
•
Clock example:
Data Types
• SystemC supports:
• C/C++ native types
• SystemC types
• SystemC types
• Types for systems modelling
• 2 values (‘0’,’1’)
• 4 values (‘0’,’1’,’Z’,’X’)
• Arbitrary size integer (Signed/Unsigned)
• Fixed point types
Native C++ Data Types
SystemC types
Type Description
• Declaration
• sc_logic my_logic;
• “int” in C++
• The size depends on the machine
• Faster in the simulation
• Logic Vector
• sc_lv<n>
• Vector to the sc_logic type
• SystemC data types may be converted to a standard C++ string using the
data type’s to_string method
Operators for SystemC Data Types
Example
• sc_bv<5> positions = "01101";
• sc_bv<6> mask = "100111";
• sc_bv<5> active = positions & mask;// 00101
• sc_bv<1> all = active.and_reduce(); //
SC_LOGIC_0
• positions.range(3,2) = "00";// 00001
• positions[2] = active[0] ^ flag;
Exercises
1. Write a program to read data from a file using the unified string
representation and store in an array of sc_uint<W>. Output the
values as SC_DEC and SC_HEX_SM.
3. Write a module from scratch using what you know. The output
should count down from 3 to 1 and display the corresponding
words “Ready”, “Set”, “Go” with each count. Compile and run.
A Notion of Time
• Wall-clock time, processor time, and simulated time
• The simulation’s wall-clock time is the time from the
start of execution to completion, including time
waiting on other system activities and applications
• The simulation’s processor time is the actual time
spent executing the simulation, which will always be
less than the simulation’s wall-clock time
• The simulated time is the time being modeled by
the simulation
• sc_time - represented by a minimum of a 64-bit
unsigned integer
• used by the simulation kernel to track simulated
time and to specify delays and timeouts.
A Notion of Time
• SC_FS, SC_PS, SC_NS, SC_US, SC_MS,SC_SEC
• All objects of sc_time use a single (global) time
resolution that has a default of 1 picosecond
• sc_time_stamp ()
• cout << " The time is now “ << sc_time_stamp()
• The time is now 0 ns
• sc_start() - used to start simulation
• sc_start(60.0,SC_SEC); // Limit sim to one minute
• wait(sc_time) – delays in SC_THREAD processes
//fsm.h detect 101 overlapping sequence case 1:
#include "systemc.h" if (in.read()){
SC_MODULE(fsm){ next_state = 1;out=0;}
sc_in<bool> rst,in,clk; else{
sc_out<bool> out; next_state = 2;out=0;}
sc_uint<2> state,next_state; break;
void update_state(){ case 2:
if (rst.read() == true){ if (in.read()){
state = 0; next_state = 1;out=1;}
} else { else{
state = next_state; next_state = 0;out=0;}
}} break;
void ns_logic(){ default:
// Determine next state and output {
switch(state) { next_state = 0;out=0;}
case 0: break;
if (in.read()) { }
next_state = 1;out=0;} }
else {
next_state = 0; out=0;} break;
SC_CTOR(fsm){
SC_METHOD(update_state); rst.write(false);
sensitive << clk.pos()<<rst; in.write(true);
SC_METHOD(ns_logic); wait();
sensitive <<clk.pos()<<in<<rst; in.write(false);
wait();
}}; in.write(true);
wait();
#include "systemc.h" in.write(false);
SC_MODULE(driver) wait();
{ }
sc_out<bool>rst,in;
sc_in<bool>clk; SC_CTOR(driver)
void inputs() {
{ SC_THREAD(inputs);
rst.write(false); sensitive << clk.pos();
in.write(true); }
wait(); };
rst.write(true);
in.write(true);
wait();
#include "systemc.h"
SC_MODULE(monitor)
{
sc_in<bool> rst, in,out;
sc_in<bool> clk;
void mon()
{
while (true){
cout << sc_time_stamp();
cout << " rst "<<rst.read();
cout << " clk "<<clk.read();
cout << " in "<<in.read();
cout << " out "<<out.read() <<endl;
wait();
}}
SC_CTOR(monitor)
{
SC_THREAD(mon);
sensitive << clk.pos();
}};
#include "systemc.h" mon.rst(rst);
#include "driver.h" mon.in(in);
#include "monitor.h" mon.out(out);
#include "fsm.h" mon.clk(clk);
drive.rst(rst); return 0;
drive.in(in); }
drive.clk(clk);
Basic Channels
• Primitive Channels
• sc_mutex, sc_semaphore, and sc_fifo<T>
• sc_mutex - Mutex is short for mutually exclusive text
• A program object that lets multiple program threads share a common
resource, such as file access, without colliding
• A mutex will be in one of two exclusive states: unlocked or locked.
• Only one process can lock a given mutex at one time.
• A mutex can only be unlocked by the particular process that
locked the mutex, but may be locked subsequently by a different
process.
• sc_mutex class comes with pre-defined methods as below.
• int lock() : Lock the mutex if it is free, else wait till mutex gets free.
• int unlock() : Unlock the mutex
• int trylock() : Check if mutex is free, if free then lock it else return -1.
Example of sc_mutex used in bus class Used with an SC_METHOD process, access
might look like this
sc_module bus{
sc_mutex bus_access; void grab_bus_method() {
… if (bus_access.trylock() == 0) {
void write(int addr, int data) { // access bus
bus_access.lock(); …
// perform write bus_access.unlock();
bus_access.unlock(); }
} }
…
};
• sc_semaphore
• For some resources, you may want to model
more than one copy or owner.
• Ex: parking spaces in a parking lot
• A multiport memory model
class multiport_RAM {
sc_semaphore read_ports(3);
sc_semaphore write_ports(2);
…
void read(int addr, int& data) {
read_ports.wait();
// perform read
read_ports.post();
}
void write(int addr, const int& data) {
write_ports.wait();
// perform write
write_ports.post();
}
…
};//endclass
• sc_fifo
• Predefined primitive channel intended to model the behavior of a fifo, that is,
a first-in first-out buffer. Each fifo has a number of slots for storing values.
• The number of slots is fixed when the object is constructed. Default slots
size is 16.
• sc_fifo has following predefined methods.
• write() : This method write the values passed as an argument into the fifo. If
fifo if full then write() function waits till fifo slot is available
• nb_write() : This method is same as write(), only difference is, when fifo is
full nb_write() does not wait till fifo slot is avaible. Rather it returns false.
• read() : This method returns the least recent written data in fifo. If fifo is
empty, then read() function waits till data is available in fifo.
• nb_read() :This method is same as read(), only difference is, when fifo is
empty, nb_read() does not wait till fifo has some data. Rather it returns false.
• num_available() : This method returns the numbers of data values available
in fifo in current delta time.
• num_free() : This method returns the number of free slots available in fifo in
current delta time.
Example void fifowrite(void) {
int val = 10;
#include "systemc.h"
SC_MODULE(example_fifo) { for (;;) {
// Declare the FIFO wait(2, SC_NS);
void fiforead(void) { cout << sc_time_stamp() << ": wrote " << val << " No.
for (;;) { }
wait(3, SC_NS); }
packet_fifo.read(val);
cout << sc_time_stamp() << ": Read " << val <<endl;
//cout << sc_time_stamp()<<"No.of Data in FIFO " SC_CTOR(example_fifo) :
<<packet_fifo.num_available()<<endl; packet_fifo(25)
{
}
SC_THREAD(fifowrite);
} SC_THREAD(fiforead);
}
};
#include "systemc.h"
#include "fifo.h"
}
SystemC Highlights (1)
• Support Hardware-Software Co-Design