Jump to content
Lhotun

SC_THREADS not starting?

Recommended Posts

Hello everyone!

I am new in this forum.

I am doing some SystemC exercises after a long time without using it, and I have a very basic problem. I wrote the following code:

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

SC_MODULE(stim)
{
  sc_in<bool> Clk;

  void StimGen()
  {
    cout << sc_time_stamp() << "Hello World!\n";
  }
  SC_CTOR(stim)
  {
    SC_METHOD(StimGen);
    sensitive << Clk.pos();
  }
};


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

  stim Stim1("Stimulus");
  Stim1.Clk(TestClk);

  sc_start();  

  return 0;

}

When run the program, it terminates without printing "Hello World!" string on output. 

What am I doing wrong?

If  I substitute SC_THREAD with SC_METHOD, the "Hello World!" message is printed.

Share this post


Link to post
Share on other sites

I quickly ran your program without any problems. I am using clang++ with SystemC 2.3.1 on a MacBook running OS X 10.10.2 (Yosemite).

 

What version of SystemC are you using? What compiler?

 

I should note, your coding style is a long ways from where I usually start, but it works non-the-less.

 

quick-macosx64.x

        SystemC 2.3.1-Accellera --- Sep 20 2014 09:20:36
        Copyright © 1996-2014 by all Contributors,
        ALL RIGHTS RESERVED
0 sHello World!
0 sHello World!
10 nsHello World!
20 nsHello World!
30 nsHello World!
40 nsHello World!
50 nsHello World!
60 nsHello World!
70 nsHello World!
80 nsHello World!
90 nsHello World!
...

Share this post


Link to post
Share on other sites

Hello everyone!

I am new in this forum.

I am doing some SystemC exercises after a long time without using it, and I have a very basic problem. I wrote the following code:

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

SC_MODULE(stim)
{
  sc_in<bool> Clk;

  void StimGen()
  {
    cout << sc_time_stamp() << "Hello World!\n";
  }
  SC_CTOR(stim)
  {
    SC_METHOD(StimGen);
    sensitive << Clk.pos();
  }
};


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

  stim Stim1("Stimulus");
  Stim1.Clk(TestClk);

  sc_start();  

  return 0;

}

When run the program, it terminates without printing "Hello World!" string on output. 

What am I doing wrong?

If  I substitute SC_THREAD with SC_METHOD, the "Hello World!" message is printed.

 

It should run just fine. The following is the partial output from a Fedora 18

machine with the gcc compiler suite and SystemC 2.3.1.

........

........

1073320 nsHello World!

1073330 nsHello World!

1073340 nsHello World!

1073350 nsHello World!

1073360 nsHello World!

1073370 nsHello World!

1073380 nsHello World!

1073390 nsHello World!

1073400 nsHello World!

1073410 nsHello World!

1073420 nsHello World!

1073430 nsHello World!

1073440 nsHello World!

1073450 nsHello World!

1073460 nsHello World!

1073470 nsHello World!

1073480 nsHello World!

1073490 nsHello World!

.......

.......

The slightly modified source file is:

#include <systemc>

SC_MODULE(stim)

{

  sc_core::sc_in<bool> Clk;

  void StimGen()

  {

    std::cout << sc_core::sc_time_stamp() << "Hello World!" <<std::endl;

  }

  SC_CTOR(stim)

  {

    SC_METHOD(StimGen);

    sensitive << Clk.pos();

  }

};

 

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

{

  sc_core::sc_clock TestClk("clk", 10,sc_core::SC_NS);

  stim Stim1("Stimulus");

  Stim1.Clk(TestClk);

  sc_core::sc_start();

  return 0;

}

 

As you might imagine, it runs in an infinite loop.

Hope that helps.

.

Share this post


Link to post
Share on other sites

Thanks to everyone for the asnwer, but I did a little mistake while posting the code. The one I previously posted works to me as well, the not working one is the following:

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

SC_MODULE(stim)
{
sc_in<bool> Clk;

void StimGen()
{
cout << sc_time_stamp() << "Hello World!\n";
}
SC_CTOR(stim)
{
SC_THREAD(StimGen);
sensitive << Clk.pos();
}
};


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

stim Stim1("Stimulus");
Stim1.Clk(TestClk);

sc_start(); 

return 0;

}

Which is identical to the previous one except for the fact that now i use SC_THREAD instead of SC_METHOD.

 

Thanks again and sorry for the mistake.

Dario

Share this post


Link to post
Share on other sites

Looks like I solved the problem. 
There was an installation problem.

Re-installing using option  --enable-pthreads while configuring the installation solved the problem

Thanks to everyone

Share this post


Link to post
Share on other sites

Alan, thanks for the answer, but it did not solved my problem. The threads seems not to run at all. 

Anyway, the code I posted should print "Hello World" once.

 

Hello Sir,

Apart from the fact that a 'SC_THREAD' is executed exactly once while

a 'SC_METHOD' multiple times, what is the result of applying the infinite loop

into your method ? Please note that SystemC is an event-driven simulator.

and in in your method :

void StimGen()

{

cout << sc_time_stamp() << "Hello World!\n";

}

what is there to indicate to the runtime kernel that this method needs to

be invoked with each rising edge of the clock ? So obviously, the method

is never invoked. Please think about it, and it will immediately solve your

problem.

Share this post


Link to post
Share on other sites

This example is very close to the one in the Duolos website.: 

https://www.doulos.com/knowhow/systemc/tutorial/modules_and_processes/

For me it does not run either. I have tried to compile with --enable_pthread but still it does not compile. I am using SystemC-2.3.3 on ubuntu 18.04.  my installation seems good. make check does give all test passing.

Instead of SC_METHOD I am using SC_THREAD which is sensitive to the pos-edge of the clock.  tracing the waveform I can see the clock is OK. However the SC_THREAD is not executed at all while I expect to at lest execute once.

I have wait() statements in the StimGen() function but actually it does not matter as the first instruction is not print something which is not printed at all.

Fundamental question here is: Are there reasons for a SC_THREAD to not be executed at all ?

 

Elvis

 

 

Share this post


Link to post
Share on other sites

===============================================================================

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


int sc_main(int argc, char* argv[]) {
    // signal connectivity
    sc_signal<bool> ASig, BSig;

    // clock signal is generated from the main in this case
    sc_clock clk("clk", 10, SC_NS, 0.5);

    // instance of stimuli block
    stimuli i_stim("Stimulus");
    i_stim.A(ASig);
    i_stim.B(BSig);
    i_stim.clk(clk);

    // Tracing Waveforms
    sc_trace_file *Tf = sc_create_vcd_trace_file("traces");
    Tf->set_time_unit(1, SC_NS);
    sc_trace(Tf, ASig  , "A" );
    sc_trace(Tf, BSig  , "B" );
    sc_trace(Tf, clk , "clk" );

    // start simulation and run forever (until an sc_stop is called from some processes)
    sc_start();

    // close tracefile
    sc_close_vcd_trace_file(Tf);

    // return 0 in case all is OK
    return 0;
}

===============================================================================

#include <systemc.h>
#include <iostream>
using namespace std;

SC_MODULE(stimuli) {
    sc_out<bool> A, B;
    sc_in<bool> clk;

    void StimGen(void) {}

    SC_CTOR(stimuli) {
        cout << "Constructing Stimuli " << name() << endl; // indication of getting into elaboration phase
        SC_THREAD(StimGen);                                // this is like initial process in verilog and is invoked only once
        sensitive << clk.pos();
    }
};

===============================================================================

#include "stimuli.h"


void stimuli::StimGen() {
        cout << "Stimuli check point - O" << endl;
        A.write(false);
        B.write(false);
        wait();                 // wait() will suspend the proces, SC_THREAD in this case, save the sate and when the simulator will
                                    // resume the process it will continue execution after the wait statement.
        A.write(false);
        B.write(true);
        wait();                 // wait 1 clock cycle - this is because the clock edge will resume the thread again
        A.write(true);
        B.write(false);
        wait();                 // wait 1 clock cycle
        A.write(true);
        B.write(true);
        wait();                 // wait 1 clock cycle
        sc_stop();              // stop the simulation
}

 

I am expecting that at least the checkpoint is displayed.

Thanks for looking at this.

Share this post


Link to post
Share on other sites
8 minutes ago, Elvis said:

...

SC_MODULE(stimuli) {
    void StimGen(void) {}

...

 };

...

void stimuli::StimGen() {

...
}

I am expecting that at least the checkpoint is displayed.

Thanks for looking at this.

 

Your code is wrong, you should get a compiler error, because you have two definitions for   stimuli::StimGen.

My guess is that you have not added  stimuli.cpp to your project file. So it is just ignored. As a result you have empty process  void StimGen(void) { /*Do nothing*/}

 

Share this post


Link to post
Share on other sites

Ok. yes you are right... If I port the code back into the .h file it works... It also seems to be something wrong with the Makefile which is not compiling all the c sources. Maybe this is the real reason.

Thanks for your help.

Share this post


Link to post
Share on other sites
2 hours ago, Elvis said:

  why 2 declarations?  I have a declaration inside the *.h file and the implementation in the .cpp file

This is definition:

void StimGen(void) {}

Declaration should look like this:

void StimGen(void);

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×