Jump to content

Simple program problem


AdrianAlter

Recommended Posts

Hey everyone! I'm a total beginner with SystemC as i'm just starting to learn about it; i'm doing a simple program that takes two inputs and either sums or multiplies them depending on the signal i'm asserting.
This is the code:
 

#include <systemc.h>

SC_MODULE( operation ){
  sc_in<bool> clock;
  sc_in<bool> enable;
  sc_in<bool> reset;
  sc_in<bool> mult;
  sc_in<sc_uint<8>> in1;
  sc_in<sc_uint<8>> in2;
  sc_out<sc_uint<8>> out;
  sc_uint<8> val;

  void func(){
    cout << "in1: " << in1.read() << endl;
    if(reset.read() == 1){
      out = 0;
      out.write(val);
      cout << "@" << sc_time_stamp() << " ||RESET asserted. VAL: " << out.read() << endl;
    }else if(enable.read() == 1){
        val = val + in1.read() + in2.read();
        out.write(val);
        cout << "@" << sc_time_stamp() << " || I summed the numbers. VAL: " << out.read() << endl;
    }else if(mult.read() == 1){
      val = val * in2.read();
      out.write(val);
      cout << "@" << sc_time_stamp() << " || I multiplied the numbers. VAL: " << out.read() << endl;
    }else{
      cout << "@" << sc_time_stamp() << " || doing nothing atm" << endl;

    }

  }

    SC_CTOR( operation ){
      cout << "@" << sc_time_stamp() << " || executing new " << endl;
      SC_METHOD(func);
      sensitive << reset;
      sensitive << clock.pos();
    }
};
#include <systemc.h>
#include <and.cpp>

int sc_main(int argc, char* argv[]){

  sc_signal<bool> clock;
  sc_signal<bool> reset;
  sc_signal<bool> enable;
  sc_signal<bool> mult;
  sc_signal<sc_uint<8>> result;
  sc_signal<sc_uint<8>> num1;
  sc_signal<sc_uint<8>> num2;
  int i = 0;
  clock = 1;
  operation operation("TEST");
  operation.clock(clock);
  operation.reset(reset);
  operation.enable(enable);
  operation.mult(mult);
  operation.out(result);
  operation.in1(num1);
  operation.in2(num2);
  enable = 1;
  reset = 0;
  mult = 0;
  num1 = 1;
  num2 = 2;
  clock = 0;
  sc_start(10, SC_MS);
  clock = 1;
  sc_start(10, SC_MS);
  

  return 0;
}

And the output:
 


        SystemC 2.3.3-Accellera --- Jun  5 2019 18:17:04
        Copyright (c) 1996-2018 by all Contributors,
        ALL RIGHTS RESERVED
@0 s || executing new 
in1: 1
@0 s || I summed the numbers. VAL: 0
in1: 1
@10 ms || I summed the numbers. VAL: 3

The problem is that at first cycle, VAL is 0, and should be 3, and at second cycle it should be 6.
What am I doing wrong?

Thanks,

Adrian

Link to comment
Share on other sites

Your code is operating correctly; however, you are displaying the results at the wrong time. This is because the write() method of sc_signal is not the same as a simple store operation.

In normal programming, you expect that if you do:

variable = 5;

Then immediately after storing the value into your variable, the variable will contain the value.

You might then assume that the write() of sc_signal is the same. It is not. It takes a small amount of time for the value to be stored. The amount of time is tiny and is refered to as a "delta delay" time. You can cause your program to wait a delta cycle before displaying the result, but it is slightly more complicated because you are using an SC_METHOD style process instead of the SC_THREAD style process.

If you are using C++11 you can replace your cout line with this to see correct answer:

        sc_spawn( [&]{
          wait( SC_ZERO_TIME ); ///< wait one delta-cycle delay
          cout << "@" << sc_time_stamp() <<"(" << sc_delta_count() << ")"
               << " || in1:" << in1.read() << " + in2:" << in2.read() << "= val:" << out.read() << endl;
        });
 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...