-2

I am new to systemC It might look stupid but I would appreciate help. In the below code

  1. In main function :write() to aa and bb shows value 0 when read using aa.read() and bb.read() which should be 10 and 20
  2. And also I think it should enter the method do_add() in the adder module as it is sensitive to a and b and a,b are binded to aa and bb signals but it doesn't call the method do_add(). How does it work and is there any error in the code?

For compiling the code:

  1. g++ -I. -I$SYSTEMC_HOME/include -L. -L$SYSTEMC_HOME/lib-linux64 -Wl,-rpath=$SYSTEMC_HOME/lib-linux64 -o out adder.cpp -lsystemc -lm
  2. ./out

#include "systemc.h"

#define WIDTH  32

SC_MODULE(adder) {
    sc_in<sc_uint<WIDTH> > a, b;  
    sc_out<sc_uint<WIDTH> > sum;

    void do_add() {
        // cout<<"hello"<<endl;
        // cout<<a.read()<<b.read()<<"\n";
        sum.write(a.read() + b.read());
        // cout<<sum.read()<<endl;
    }

    SC_CTOR(adder) {
        SC_METHOD(do_add);   
        sensitive << a << b; 
    }
};

int sc_main(int argc, char* argv[]) {
    sc_signal<sc_uint<WIDTH> >aa,bb;

    adder add("Adder");
    add.a(aa);
    add.b(bb);

    aa.write(10);
    bb.write(20);

    cout<<aa.read()<<bb.read()<<"\n";
}
Shibli
  • 5,879
  • 13
  • 62
  • 126

1 Answers1

1

There are a number of things here. Firstly, you never actually start the simulation (see function sc_start) and even if you did it would exit immediately since there are no events to process. And lastly, even if you fixed those issues you would still get the same result since SystemC simulates hardware and reading a port in the same delta cycle as it was written will give you the original value on the port not the newly written one. How can hardware change the value of a signal in zero time?

The best advice I can give is to look in the directory where you downloaded SystemC and you will see a directory of PDF documents. On my SystemC it is in a directory called 'docs' (it may be different on yours depending on who installed it and where). If you look in there you will find a 'user guide' and some other PDF documents. These explain how SystemC works and give you examples you can try and modify to your own needs. Also there are some examples in the SystemC sources in the 'examples' folder. You can try playing around with these to get a feel for it and maybe just cut and paste some code and modify it to get what you require.

systemcpro
  • 856
  • 1
  • 7
  • 15