INFORMATICS 2C - Computer Systems: Lab 2

SystemC Basics

The objective of this lab is to introduce you to simulating systems using SystemC and viewing the results using a waveform viewer (gtkwave)

The second systems coursework uses SystemC, so this lab will help you with this coursework.

What is SystemC and how is it used in this course?

SystemC is a hardware description language and a hardware simulator. It is based on library of classes and functions written in C++, an object-oriented enhancement of the C programming language. Hardware designs and accompanying test circuits are described using specially-crafted C++ programs that make use of the SystemC library. When these programs are compiled and run, they simulate the behaviour of the hardware.

This course does not teach you SystemC, nor are you advised to learn it by yourselves at this stage. We are merely using it as a simulation engine because it can effectively model concurrent operations, closely mimicking the operation of real hardware. The code that you are required to write will be plain C and will not use any of the SystemC features. The provided code will obviously use SystemC features but you are not required to learn how it works. What you need to understand is how simple computer hardware works, not the details of how to model it using SystemC.

The Lab

For this lab you will simulate three small circuits to get familiar with running simulations and viewing the results in a waveform viewer called gtkwave.

To get set-up for the lab, download the file sc-lab.tgz to some directory in your disk space and execute the command

  tar xzf sc-lab.tgz

This will create a sub-directory sc-lab containing the SystemC code for the three examples and SystemC library code.

  1. 4-bit Ripple-Carry Adder

    The objective here is to see how a combinational circuit works, learn how to compile and run a SystemC ``program'' and learn how to use gtkwave to view the signals.

    Change to the directory sc-lab/rca. This directory contains the files

    • main.cpp
    • fa.cpp
    • fa.h
    • driver.cpp
    • driver.h
    • Makefile

    The .cpp and .h files contain SystemC C++ code which represents a 4-bit carry-ripple adder just like the one shown below. The gates at the bottom of the figure which drive the sum outputs are 3-input XOR gates: the output is 1 when any one or all three inputs are 1, 0 otherwise. If you are curious, take a look at the main.cpp file. You will see there how instances of the full adders are declared and how signals are wired up to the full adder ports.

    4-bit ripple adder schematic

    The file Makefile contains commands for compiling the adder code using the make utility. Type make to compile the SystemC code and produce an executable called rca. Run the executable by typing ./rca. When you run it, a file called waves.vcd is created. This waveforms file contains a description of all the circuit's signals (inputs, outputs and internal wires) and how they change in time. To view the waveforms, type gtkwave waves.vcd. This opens up the gtkwave application. On the left hand side, you will see "SystemC" inside a text box. Click on it and you will get a list of signal names in the box below. You can now select any signals and click on the Append button below to display them in the large black area to the right. To see the full waveform click on the Zoom Fit button (a magnifying glass with the outline of a box inside). This will zoom out as far as possible so you can see the entire simulation time and all the signal changes. Now you can zoom in to look at the details. If you click on the Waves area, you get a vertical red marker. This will show you the value of each signal at that time at the left of the waveforms.

    Explore the various buttons and options of gtkwave. See the Tips on using GTKWave section below for further information on GTKWave.

    Your aim for this lab is not to become an expert in using rather gtkwave, but rather just to find your way around the basic functionality. For example, you should figure out how you can reorder the displayed signals, combine related signals into vectors, set the format of vector values to binary, hexadecimal, decimal, etc., and expand vectors back into their constituent signals.

    Now clear up all the signals from the waves box and let's explore how the adder works. First find the circuit inputs a3-a0, b3-b0 and cin and add their waves. Zoom out fully to see which numbers are being added by the circuit. Now add the waveforms of the outputs s3-s0 and the carry out cout. Finally add the intermediate signals c0-c2.

    The inputs change at times 0ns, 30ns and 60ns. Note that the waveform ends abruptly when the last change happens. Be careful to see the very last output values, which should be (s3-s0 = 0101)

    Verify that the adder works correctly for the provided input values. Notice the case where the adder overflows and gives a sum of 0 for the second set of inputs.

    Now let's focus on the propagation delays. Notice that from the time the inputs change, it takes a while for the outputs to change to the correct values. This time depends on the gate delays and the actual values of the inputs. In some cases the outputs may change a number of times before they stabilise to the correct values. You can see this happening for the second set of inputs. Look at the intermediate carry signals to see how they change and notice the ripple effect when adding 0111 and 0001.

    If you feel a little adventurous, try changing driver.cpp to provide some other inputs to the adder. All you have to do is modify the binary values inside the double quotes. The first bit after the 'b' is the one that gets assigned to a3 (or b3) and the last is the one that gets assigned to a0 (b0). You have to re-compile (make), re-run (./rca) and load the new waves into gtkwave to see the results.

  2. 4-bit Shift Register

    The objective here is to see how a simple sequential circuit works.

    Change to the directory sc-lab/sr. This directory contains the files

    • main.cpp
    • sr.cpp
    • sr.h
    • driver.cpp
    • driver.h
    • Makefile

    These files represent a 4-bit shift register just like the one shown below. The inputs to the circuit are the clock signal (clk) and a 1-bit input, din.

    4-bit shift register schematic

    Compile (make), run (./shr) and start the waveform viewer (gtkwave waves.vcd). Add all signals to the Waves waveform box. You will probably find it more useful to change the data format of r into binary (Right-mouse-click on r[3:0] in the Signals box to access the Data Format sub-menu). You can also double left-mouse-click on r[3:0] to make visible the individual bits of this bit vector as distinct traces.

    Observe how the shift register works. Initially all D flip-flops (r0-r3) contain 0s. The first input (din) is 0 so when the first rising clock edge arrives, nothing changes in r0-r3. Before the following rising clock edge arrives, input din is set to 1. This 1 is propagated into the first flip-flop (r0 = 1 shortly after the following rising clock edge). It then continues its way through the shift register, while new data enters from the left. Notice how many cycles it takes for a new input in din to propagate to the end of the shift register.

  3. FSM

    The objective here is to see how a simple hardware FSM works.

    Change to the directory sc-lab/fsm. This directory contains the files

    • main.cpp
    • fsm.cpp
    • fsm.h
    • driver.cpp
    • driver.h
    • Makefile

    These files model the implementation of the vending machine FSM described at the end of chapter 5 of the course notes. The inputs to the circuit are the clock signal (clk), a signal representing that a 10p coin has been inserted ten and a signal for a 20p coin twenty.

    Compile, run and open the results in gtkwave. Observe the operation of the circuit. In particular notice the timing of the output signal z and the state transitions. As specified in the notes, z becomes 1 whenever the state s3 (encoded as 11 in binary) is reached. State transition follows the FSM diagram and the state encoding follows that of the table.

    Convince yourself that you understand the timing issues of the FSM: when, in relation to the clock edges, are the inputs applied, when do they change the state of the machine, and when the output signal takes on its new value.

Tips on using GTKWave


Home : Teaching : Courses : Inf2c-cs : 13-14 

Informatics Forum, 10 Crichton Street, Edinburgh, EH8 9AB, Scotland, UK
Tel: +44 131 651 5661, Fax: +44 131 651 1426, E-mail: school-office@inf.ed.ac.uk
Please contact our webadmin with any comments or corrections. Logging and Cookies
Unless explicitly stated otherwise, all material is copyright © The University of Edinburgh