Jump to content

Reading inputs of a counter module


Recommended Posts



hi all,


I am very new to systemC and i have finished the theoretical concepts in systemc and just started with the simple implementations.


Here i have tried to implemented a constant module, which is source a module providing outputs (1) at each positive clock edge and this module output is connected to the enable input of the next counter module.


The clock input to the constant module are provided manually in the cpp file.


pls see the following codes and the output.


constant module

#ifndef CONSTANT_H_
#define CONSTANT_H_

#include "systemc.h"
#include <stdlib.h>

sc_in<bool> clk;
sc_out<bool> output;

bool A;
void process(){    //
cout<< "@"<<sc_time_stamp() << " ::constant output written" << output << endl;


sensitive << clk.pos();

#endif /* CONSTANT_H_ */





#ifndef COUNTER_H_
#define COUNTER_H_

#include "systemc.h"
#include <iostream>

SC_MODULE (counter) {
  sc_in<bool>   enable;
  sc_out<sc_uint<1> > counter_out;

  sc_uint<1> count;

   void incr_count () {
   if (enable.read() == 1) {
      cout<<"@" << sc_time_stamp() <<" :: Incremented Counter "<<counter_out.read()<<endl;

    SC_CTOR(counter) {
    cout<< " executing counter"<< endl;
        sensitive  << enable ;
#endif /* COUNTER_H_ */









#include "systemc.h"
#include "counter.h"
#include "constant.h"

int sc_main (int argc, char* argv[]) {
    sc_signal<bool> clock;
    sc_signal<bool> ensignal;
    sc_signal<sc_uint<1> > outputsignal;

    constant c1("constant");

    counter c2("counter");

    for (int i=0;i<5;i++) {
         clock = 0;
         sc_start(1, SC_SEC);
         clock = 1;
         sc_start(1, SC_SEC);


    return 0;





executing counter
@0 s ::constant output written0
@0 s :: Incremented Counter 0
@1 s ::constant output written1
@3 s ::constant output written1
@5 s ::constant output written1
@7 s ::constant output written1
@9 s ::constant output written1



As expected, the constant module produces output at each positive clock edge.


But the problem is, the output written on the constant output port could not be read on the enable inport of counter module.


I guess i have made connections also correct and i couldn't able debug why the counter enable inport cannot read the inputs and not increasing the counter.


Any help / pointing out of problem would be a great help to me.






Link to comment
Share on other sites

The sc_signal class only creates events when it changes value. Because you assign the same value all the time, the counter never gets triggered.


Try changing sc_signal<bool> ensignal to sc_buffer<bool> ensignal;

sc_buffer is a derived class of sc_signal that notifies an event when it is written, even if you write the same value over and over again.






To generate your clock, it would be easier (in sc_main) to do


sc_clock clock("clock", 1, SC_SEC);




sc_start(10, SC_SEC);


The sc_clock instance can be directly bound to a port of type sc_in<bool>.

Link to comment
Share on other sites

Dear Mr. Alan Fitch, Thank you for the reply.

Yes as you mentioned using sc_buffer would be the right way to connect the modules rather than a sc_signal when there is a same output.

i tried following the same way, the sc_buffer gets updated every time the output writes. But the counter is not getting incremented as expected.

Its getting reset every alternative positive edge rather than getting incremented with the previous value.


Is there again any problem in signal updation or in the design of a counter? please help me how to proceed with it




executing counter
@0 s ::constant output written 0
@0 s :: Incremented Counter    0
@1 s ::constant output written 1
@1 s :: Incremented Counter    1
@3 s ::constant output written 1
@3 s :: Incremented Counter    0
@5 s ::constant output written 1
@5 s :: Incremented Counter    1
@7 s ::constant output written 1
@7 s :: Incremented Counter    0
@9 s ::constant output written 1
@9 s :: Incremented Counter    1
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.

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...