Jump to content

SC_THREADS not starting?


Lhotun

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.

Link to comment
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!
...

Link to comment
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.

.

Link to comment
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

Link to comment
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.

Link to comment
Share on other sites

  • 3 years later...

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

 

 

Link to comment
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.

Link to comment
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*/}

 

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