Jump to content

solution for problem ...


mohitnegi

Recommended Posts

I have modified the fifo example and wish to have char as ouput ...the program is as follows...

ca_fifo.h

#include <systemc.h>

SC_MODULE (fifo)
{
  sc_in<bool> read_write_n;
  sc_in<signed char> data_in;
  sc_in<bool> clock;
  sc_in<bool> reset;
  
  sc_out<signed char> output;

  void fifo_read(char &c);
  void fifo_write(char c);
  void fifo_reset();
  void prc_fifo();

  sc_event write_event,read_event;
  enum e{MAX=10};
  char data[MAX];
  int num_element,first;

 SC_CTOR (fifo)
  {
     SC_THREAD(prc_fifo);
     sensitive<<clock.pos();
     sensitive <<read_write_n<<data_in;
     SC_THREAD(fifo_reset);
     sensitive<<reset;
   }   
};

 void fifo::fifo_write(char c)
{
  if(num_element==MAX)
   wait(read_event);

   wait(10,SC_NS);
   data[(first+num_element)% MAX]=c;
   ++num_element;
   write_event.notify();
}

void fifo::fifo_read(char &c)
{
  if(num_element==0)
  wait(write_event);
  
  wait(10,SC_NS); 
  c=data[first];
  -- num_element;
  first=(first+1)%MAX;
  read_event.notify();
}

ca_fifo.cpp

#include "ca_fifo.h"

void fifo::prc_fifo()
{ 
  if(read_write_n==1)
  {
  const char *str="HELLO !!!  WELCOME \n";

  while(*str){
      fifo_write(*str++);
      }
  }
  else
  {
     char c;
     while(true){
      fifo_read(c);
      cout<<c<<endl; 
      }
  }
}
void fifo::fifo_reset()
{
  num_element =first =0;
}


the driver code driver_ca_fifo.cpp

SC_MODULE(driver_ca_fifo)
{
  sc_out<bool> d_read_write_n,d_reset;//d_data_in;
  sc_out<signed char> d_data_in;//,d_data;

  void prc_driver_ca_fifo();  

  SC_CTOR (driver_ca_fifo)
   {
     SC_THREAD(prc_driver_ca_fifo);
     //sensitive<<pos.clock;
   }
};

void driver_ca_fifo::prc_driver_ca_fifo()
{
 while(1)
 {
 d_read_write_n = 1;
 d_reset        = 0;

 wait (50 ,SC_NS);
 d_read_write_n = 0;
 //d_reset        = 1'b0;

 wait (50 ,SC_NS);
 d_read_write_n = 1 ;


  }
}

the monitor monitor_ca_fifo.cpp

SC_MODULE (monitor_ca_fifo)
{
 sc_in<bool> m_read_write_n,m_reset;
 sc_in<signed char> m_data_in,m_out;

 void prc_monitor_ca_fifo();

 SC_CTOR (monitor_ca_fifo)
 {
    SC_METHOD(prc_monitor_ca_fifo);
    sensitive<<m_read_write_n<<m_reset<<m_data_in<<m_out;
  }
};

 void monitor_ca_fifo::prc_monitor_ca_fifo()
 {
  cout<<"at time :"<<sc_time_stamp()<<"::"<<"(read_write_n,reset) \t"<<m_read_write_n<<m_reset<<"read data :\t"<<m_out<<endl;
 }

the main file fifo_main is

the running execution output is

 

 

(gdb) run
Starting program: /home/mohit/Desktop/Untitled Folder/output 
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/i386-linux-gnu/libthread_db.so.1".

             SystemC 2.3.0-ASI --- Sep 18 2013 06:20:13
        Copyright (c) 1996-2012 by all Contributors,
        ALL RIGHTS RESERVED

at time :0 s::(read_write_n,reset) 	00read data :	
at time :0 s::(read_write_n,reset) 	10read data :	





at time :50 ns::(read_write_n,reset) 	00read data :	





at time :100 ns::(read_write_n,reset) 	10read data :	





at time :150 ns::(read_write_n,reset) 	00read data :	




[Inferior 1 (process 3689) exited normally]

 

My ques is the code conceptually correct and how to display the char ....

Link to comment
Share on other sites

Hello Sir,

The simplest test to verify if the fifo is doing what it is supposed

to be doing -- input and output MUST maintain fifo order. Please

create a very simple test harness where a sequence of characters

is written to the fifo, without any read operations. Then perform

a sequence of read operations without any write operations. The

input and output must maintain fifo order -- if not something is 

wrong, and must be corrected. Please always focus on the core

concepts. The input and output must look something like:

Writing : a

Writing : b

Writing : c

Writing : d

....

....

Writing : z

 

Reading : a

Reading : b

Reading : c

Reading : d

......

.....

Reading : z

 

Once the basic test is passed, try adding more complicated cases.

Please do not get bogged down with complicated operations right

at the start. Hope that helps.

Link to comment
Share on other sites

thanks KS,

the main file is as follows ::

#include "ca_fifo.cpp"
#include "driver_ca_fifo.cpp"
#include "monitor_ca_fifo.cpp"

int sc_main(int,char*[])
{
 sc_signal<bool> t_read_write_n;
 sc_signal<signed char > t_data,t_out;
 sc_clock clk("clk",10,SC_NS);
 sc_signal<bool> t_reset;
 
 fifo f1 ("fifo");
 f1.read_write_n(t_read_write_n);
 f1.clock(clk);
 f1.reset(t_reset);
 f1.data_in(t_data);
 f1.output(t_out);

 driver_ca_fifo d1 ("input");
 d1.d_read_write_n(t_read_write_n);
 //d1.d_clock(clk);
 d1.d_reset(t_reset);
 d1.d_data_in(t_data);

 monitor_ca_fifo m1 ("output");
 m1.m_read_write_n(t_read_write_n);
 //m1.m_clock(clk);
 m1.m_reset(t_reset);
 m1.m_data_in(t_data);
 m1.m_out(t_out);

 sc_trace_file *fp;
 fp=sc_create_vcd_trace_file("wave");
 //((vcd_trace_file*)fp)->sc_set_vcd_time_unit(-9);
 sc_trace(fp,clk,"clk");
 sc_trace(fp,t_read_write_n,"read_write_n");
 sc_trace(fp,t_reset,"reset");
 sc_trace(fp,t_data,"data");
 sc_trace(fp,t_out,"out");
 
 sc_start(200,SC_NS);
 sc_close_vcd_trace_file(fp);
}
Link to comment
Share on other sites

Hi all ,

 

I also wish to know how to use sc_clock ...It should be instantiated in main file or somewhere else ...???

As i have instantiated in main file and it is not working ...

 

 

Thanks

Mohit

Hello Sir,

How do you verify that your clock is not working ? Have you tried the other sc_clock

constructors ?

Link to comment
Share on other sites

Hi all ,

 

I also wish to know how to use sc_clock ...It should be instantiated in main file or somewhere else ...???

As i have instantiated in main file and it is not working ...

 

 

Thanks

Mohit

To understand the better utility of sc_clock read

 

http://www.lysium.de/docs/systemc-2.1/docs/html/classsc__core_1_1sc__clock.html

 

or have some examples at

 

http://www.ht-lab.com/howto/vh2sc_tut/vh2sc_tut4.html

 

I have checked your clock issue: its working very fine.

 

P.S: sc_clock can be used anywhere you want, but for betterment we keep it in testbench or so to say in main file.

 

-- correct me if am wrong.

Link to comment
Share on other sites

Hi Mohit,

 

There were some logic implementation problem in your code.

 

Below is the quick and dirty reforms in the same, feel free to ask about changes.

 

fifo.h

#include <systemc.h>

SC_MODULE (fifo)
{
  sc_in<bool> read_write_n;
  sc_in<signed char> data_in;
  sc_in<bool> clock;
  sc_in<bool> reset;
  
  sc_out<signed char> output;

  void fifo_read(char &c);
  void fifo_write(char c);
  void fifo_reset();
  void prc_fifo();
  void clk_check();

  sc_event write_event,read_event;
  enum e{MAX=10};
  char data[MAX];
  int num_element,first;

 SC_CTOR (fifo)
  {
     first = 0;
     num_element = 0;

     SC_THREAD(prc_fifo);
     sensitive<< clock.pos();
     sensitive <<read_write_n;
   }   
};

 void fifo::fifo_write(char c){
  	if(num_element!=MAX){
   	data[(first+num_element)% MAX]=c;
   	++num_element;
	}
}
void fifo::fifo_read(char &c){
	if(num_element!=0){
  	c=data[first];
  	output.write(data[first]);
  	-- num_element;
  	first=(first+1)%MAX;
	}
}

fifo.cpp

#include "fifo.h"
int i = 0;
char c;
const char *str="HELLO !!!  WELCOME \n";
void fifo::prc_fifo() {
	while(true){
	wait();
  	if(read_write_n==1)
  		{
      		fifo_write(*str++);
  		}
  	if(read_write_n==0)
  		{
      		fifo_read(c);
  		}
	}
  }	
void fifo::fifo_reset()
{
  num_element =first =0;
}

;)

Link to comment
Share on other sites

Hi Mohit,

 

There were some logic implementation problem in your code.

 

Below is the quick and dirty reforms in the same, feel free to ask about changes.

 

fifo.h

#include <systemc.h>

SC_MODULE (fifo)
{
  sc_in<bool> read_write_n;
  sc_in<signed char> data_in;
  sc_in<bool> clock;
  sc_in<bool> reset;
  
  sc_out<signed char> output;

  void fifo_read(char &c);
  void fifo_write(char c);
  void fifo_reset();
  void prc_fifo();
  void clk_check();

  sc_event write_event,read_event;
  enum e{MAX=10};
  char data[MAX];
  int num_element,first;

 SC_CTOR (fifo)
  {
     first = 0;
     num_element = 0;

     SC_THREAD(prc_fifo);
     sensitive<< clock.pos();
     sensitive <<read_write_n;
   }   
};

 void fifo::fifo_write(char c){
  	if(num_element!=MAX){
   	data[(first+num_element)% MAX]=c;
   	++num_element;
	}
}
void fifo::fifo_read(char &c){
	if(num_element!=0){
  	c=data[first];
  	output.write(data[first]);
  	-- num_element;
  	first=(first+1)%MAX;
	}
}

fifo.cpp

#include "fifo.h"
int i = 0;
char c;
const char *str="HELLO !!!  WELCOME \n";
void fifo::prc_fifo() {
	while(true){
	wait();
  	if(read_write_n==1)
  		{
      		fifo_write(*str++);
  		}
  	if(read_write_n==0)
  		{
      		fifo_read(c);
  		}
	}
  }	
void fifo::fifo_reset()
{
  num_element =first =0;
}

;)

 

Hy KS,

 

Thnks for the solution ...i tried it ...

in file ca_fifo.h

output.write(data[first]);

only one char is printed out ..so i did a few changes in ca_fifo.cpp

	{
                while(true)
                {
      		fifo_read(c);
                output.write(c);
                }

but now the problem is in decleration ....

sc_out<signed char<31> > d_data_in;

which is giving error in compiling ....

g++ -Wl,-rpath=/home/mohit/Downloads/systemc-2.3.0/lib-linux -I. -I.. -I/home/mohit/Downloads/systemc-2.3.0/include -c fifo_main.cpp
In file included from ca_fifo.cpp:1:0,
                 from fifo_main.cpp:1:
ca_fifo.h:6:16: error: ‘char’ is not a template
ca_fifo.h:10:17: error: ‘char’ is not a template
In file included from fifo_main.cpp:2:0:
driver_ca_fifo.cpp:8:17: error: ‘char’ is not a template
In file included from fifo_main.cpp:3:0:
monitor_ca_fifo.cpp:4:15: error: ‘char’ is not a template
fifo_main.cpp: In function ‘int sc_main(int, char**)’:
fifo_main.cpp:8:19: error: ‘char’ is not a template
make: *** [fifo_main.o] Error 1

now my question is how to declare a string of specific size ...

Link to comment
Share on other sites

Hello Sir,

The simplest test to verify if the fifo is doing what it is supposed

to be doing -- input and output MUST maintain fifo order. Please

create a very simple test harness where a sequence of characters

is written to the fifo, without any read operations. Then perform

a sequence of read operations without any write operations. The

input and output must maintain fifo order -- if not something is 

wrong, and must be corrected. Please always focus on the core

concepts. The input and output must look something like:

Writing : a

Writing : b

Writing : c

Writing : d

....

....

Writing : z

 

Reading : a

Reading : b

Reading : c

Reading : d

......

.....

Reading : z

 

Once the basic test is passed, try adding more complicated cases.

Please do not get bogged down with complicated operations right

at the start. Hope that helps.

 

Hi ,

well i have done the above ...char are being printed ....but now i want the whole process to be in

string instead of char ...

 

how to proceed and what would be its declaration ...

 

With

Mohit N

Link to comment
Share on other sites

Hi ,

well i have done the above ...char are being printed ....but now i want the whole process to be in

string instead of char ...

 

how to proceed and what would be its declaration ...

 

With

Mohit N

Hello,

If you have read my responses to three recent queries on

this newsgroup, you like the others, have run into a fundamental

drawback/problem of C++ templates. The reason why your

fifo works with single characters only is because during the

crucial SystemC elaboration step, the size (number of bits)

for a single character is known to the compiler. Also note

that the SystemC built-in sc_fifo module can tackle built-in

C++ primitive data types(char, double, float int ....) ONLY.

As a character string can be of any size, it is an user

defined data type, and so the user has to provide the

mechanism to handle it.  

So, to transfer character strings via a custom fifo, a good

option would be to:

1. Declare a custom interface - subclass of sc_core::sc_interface

2. Create a class that implements the interface - must ensure

FIFO order in the input/output. This would be the character

string FIFO.

3. Create custom ports to read/write character strings from/to

custom channel

 

Yes, it is rather messy, but it is straightforward. Hope this helps.

Link to comment
Share on other sites

Hello,

If you have read my responses to three recent queries on

this newsgroup, you like the others, have run into a fundamental

drawback/problem of C++ templates. The reason why your

fifo works with single characters only is because during the

crucial SystemC elaboration step, the size (number of bits)

for a single character is known to the compiler. Also note

that the SystemC built-in sc_fifo module can tackle built-in

C++ primitive data types(char, double, float int ....) ONLY.

As a character string can be of any size, it is an user

defined data type, and so the user has to provide the

mechanism to handle it.  

So, to transfer character strings via a custom fifo, a good

option would be to:

1. Declare a custom interface - subclass of sc_core::sc_interface

2. Create a class that implements the interface - must ensure

FIFO order in the input/output. This would be the character

string FIFO.

3. Create custom ports to read/write character strings from/to

custom channel

 

Yes, it is rather messy, but it is straightforward. Hope this helps.

 hi dakupoto,

 

thanks for the response ...

 

But i m not using a built in sc_fifo instead i have made my own fifo named ca_fifo ....

 

could you identify the problem in my fifo....

Link to comment
Share on other sites

 hi dakupoto,

 

thanks for the response ...

 

But i m not using a built in sc_fifo instead i have made my own fifo named ca_fifo ....

 

 

could you identify the problem in my fifo....

 

Hello,

Your code amd modules are doing exactly, what they are supposed to be

doing. You have for example:

SC_MODULE (fifo)

{

  sc_in<bool> read_write_n;

  sc_in<signed char> data_in;

.......

 

In this case, both your input port and the channel through which

the input data arrives, can only handle ONE character at each

triggering event. What you really want to do is have an input port,

an output port and a channel that can handle a character string

at each event. Note the difference - a character string versus a

single character. I am afraid you would have to fully re-write

your code to achieve this. As I have pointed out earlier, built-in

SystemC channels/ports can only handle a single primitive data

atype, at a time, by default. Any special cases need to be handled

with custom code.

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