2

I've read that SC_CTHREAD works only with bool, like:

SC_MODULE(my_module){
 sc_in<bool> clk;
 // ...
 void foo();
 // ...
 SC_CTOR(my_module){
  SC_CTHREAD(foo, clk.pos());
 }
}

But what if I have sc_in_clk clk in my module, like it is in this example: http://www.asic-world.com/systemc/process3.html? After such simulation the result of the function is not calculated, so I'm using SC_METHOD(foo); sensitive << clk.pos();.

My question is: how can I use sc_in_clk type and SC_CTHREAD both at the same time? Do I need to cast clk to bool somehow?

ans
  • 378
  • 1
  • 5
  • 18

1 Answers1

2

Yes you can use it both at the same time because sc_in_clk is merely a typedef of sc_in<bool>. That means it doesn't matter whether you use sc_in_clk or sc_in<bool> with SC_CTHREAD.

From the documentation:

typedef sc_in<bool> sc_in_clk;

The typedef sc_in_clk is provided for convenience when adding clock inputs to a module and for backward compatibility with earlier versions of SystemC. An application may use sc_in_clk or sc_in< bool > interchangeably.

I tried to reproduce your problem in my own environment (SystemC 2.3.2). Based on the snippet you posted, I created this small SystemC program:

#include <systemc.h>
SC_MODULE(my_module)
{
    sc_in_clk clk;

    void foo();

    SC_CTOR(my_module) 
    {
        SC_CTHREAD(foo, clk.pos());
    }
};

void my_module::foo()
{
    while(1)
    {
        cout << sc_time_stamp() << endl;
        wait();
    }
}

my_module *DUT;

int sc_main(int argc, char** argv){
    sc_clock clk("clk", 10, SC_NS);

    DUT = new my_module("my_module");
    DUT->clk(clk);

    sc_start(50, SC_NS);
    return 0;
}

This code works as expected and the output is:

0 s
10 ns
20 ns
30 ns
40 ns

You can try to match the structure of your code to the structure of the program above to find potential other bugs in your code.

What is the structure of your void foo()? Does it contain any form of the wait function, other than void wait(); or void wait(int);? Because a clocked thread process may only call these two forms of wait.

Silicon1602
  • 1,151
  • 1
  • 7
  • 18
  • 1
    Thank you for the reply. I guess the problem was in the structure of my `void foo()`. It looked just like some lines of code; with your advice to match the structure I put all this lines in a `while(1){}` so now everything works just fine. – ans Jun 21 '18 at 13:12
  • Good! There is one important difference between method processes and thread processes to keep in mind. The former are triggered by the kernel, execute from the beginning to the end, and then return control to the kernel. The latter are called once and only once by the kernel. That's why you need the endless loop with the wait function . The thread process executes until it reaches the first `wait()`. Then, the kernel can continue execution (instead of restarting it), starting with the statement immediately following the most recent call to function wait. – Silicon1602 Jun 21 '18 at 13:32