Jump to content
Sign in to follow this  
ZhangRan

sc_fifo.read() does not work

Recommended Posts

#include "systemc.h"
SC_MODULE(DF_Adder){
	sc_fifo_in<int> input1, input2;
	sc_fifo_out<int> output;
	void process(){
		while (1) {
			output.write(input1.read() + input2.read());
			}
	}
	SC_CTOR(DF_Adder) {SC_THREAD(process);}
};

SC_MODULE(DF_Const){
	sc_fifo_out<int> output;
	void process() {
		 while (1) {
			output.write(constant_);
		}
	}
	SC_HAS_PROCESS(DF_Const);
	DF_Const(sc_module_name N, int C):
		sc_module(N), constant_(C) { SC_THREAD(process); }
	int constant_;
};

SC_MODULE(DF_Fork){
	sc_fifo_in<int> input;
	sc_fifo_out<int> output1,output2;
	void process(){
		while(1){
			int value = input.read();
			output1.write(value);
			output1.write(value);
		}
	}
	SC_CTOR(DF_Fork) {SC_THREAD(process);}
};

SC_MODULE(DF_Printer){
	sc_fifo_in<int> input;
	SC_HAS_PROCESS(DF_Printer);
	DF_Printer(sc_module_name N, unsigned N_ITER):
		sc_module(N), n_iterations_(N_ITER), done_(false) { SC_THREAD(process); }
	void process(){
		cout << n_iterations_<< endl;
		for (unsigned i=0; i<n_iterations_; i++) {
			int value;
 			value = input.read();
			cout<< name() << "read data is(in print) " << value << endl ;
		}
		done_ = true;
		return;
	}

	~DF_Printer() {
		if (!done_) cout << name() << "not done yet" << endl;
	}
	unsigned n_iterations_;
	bool done_;
	int value;
};
#include "systemc.h"
#include "adder.h"

int sc_main(int, char**){
	//instance
	DF_Const constant("constant", 1);
	DF_Adder adder("adder");
	DF_Fork fork("fork");
	DF_Printer printer("printer",10);
	//fifo
	sc_fifo<int> const_out("const_out", 5);
	sc_fifo<int> adder_out("adder_put", 1);
	sc_fifo<int> feedback("feedback", 1);
	sc_fifo<int> to_printer("to_printer", 1);
	//initial
	//connection
	constant.output(const_out);
	adder.input1(feedback);
	adder.input2(const_out);
	adder.output(adder_out);
	fork.input(adder_out);
	fork.output1(feedback);
	fork.output2(to_printer);
	printer.input(to_printer);
	//start
	sc_start(5,SC_NS);
	feedback.write(42);
	sc_start();
	return 0;
}

Hi, 

I am a noob of sysC, and trying some basic case.

This case is a simple add 1 counter, and using the DF_Printer to print the result. However, the printed value never comes up. It seems like that "value = input.read()" can break the for loop.

can anyone give me some idea of this question?

Thank you very much.

 

Ran

 

Share this post


Link to post
Share on other sites

You are not writing to output2 of fork, hence printer input fifo is not filled. For debugging use num_available() to get the number of available items in the fifo.
"value = input.read()"  is not breaking out of the loop, here read() is a blocking call, as fifo is empty it kept blocking the thread.
As there are no other pending events or waits simulation stopped naturally.

 

Ram

Share this post


Link to post
Share on other sites
SC_MODULE(DF_Fork){
	sc_fifo_in<int> input;
	sc_fifo_out<int> output1,output2;
	void process(){
		while(1){
			int value = input.read();
			output1.write(value);
			//output1.write(value);
			output2.write(value);
		}
	}
	SC_CTOR(DF_Fork) {SC_THREAD(process);}
};

You never write on output2. Your printer then remains at 

value = input.read();

since it waits till data is available. Thus the simulation has nothing to do and stops itself.

You can read about sc_fifo methods here: http://www.asic-world.com/systemc/channels4.html

Share this post


Link to post
Share on other sites
9 hours ago, vrsm said:

You are not writing to output2 of fork, hence printer input fifo is not filled. For debugging use num_available() to get the number of available items in the fifo.
"value = input.read()"  is not breaking out of the loop, here read() is a blocking call, as fifo is empty it kept blocking the thread.
As there are no other pending events or waits simulation stopped naturally.

 

Ram

Thanks, it works well now. 

In addition, I insert :"input.num_available()"  before input.read(). I am curious that the "input.num_available()" is 0 ,but it still can output data. 

 

Ran

Share this post


Link to post
Share on other sites
1 hour ago, ZhangRan said:

I am curious that the "input.num_available()" is 0 ,but it still can output data

Its because printer input.num_available() non-blocking call which is executed even before fork.output2 is written. Printer input.read() is working fine as it a blocking call which is waiting for fifo to get filled, once fork.output2 is written and fifo have a item now and now read() is unblocked and things are working fine.

You cannot predict the thread execution order unless otherwise you are explicitly making them to happen in order. I suggest you keep multiple print statements to see how threads are executed in your simulation.

num_available() is not mandatory for any implementation. But the correct way to use num_available in your experiment is,
 

while(fifo.num_available() == 0)   
{
      wait(SC_ZERO_TIME); //by calling wait you are giving chance for other threads in simulation to run
}
cout<<" items in fifo "<<fifo.num_available()<<endl; 

 

Ram

 

Share this post


Link to post
Share on other sites
27 minutes ago, vrsm said:

Its because printer input.num_available() non-blocking call which is executed even before fork.output2 is written. Printer input.read() is working fine as it a blocking call which is waiting for fifo to get filled, once fork.output2 is written and fifo have a item now and now read() is unblocked and things are working fine.

You cannot predict the thread execution order unless otherwise you are explicitly making them to happen in order. I suggest you keep multiple print statements to see how threads are executed in your simulation.

num_available() is not mandatory for any implementation. But the correct way to use num_available in your experiment is,
 


while(fifo.num_available() == 0)   
{
      wait(SC_ZERO_TIME); //by calling wait you are giving chance for other threads in simulation to run
}
cout<<" items in fifo "<<fifo.num_available()<<endl; 

 

Ram

 

many thanks, i have a better understand of block and non-block now.

Share this post


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

Sign in to follow this  

×
×
  • Create New...