Jump to content

How to initialize sc_fifo


Ganesan R

Recommended Posts

In running the following program I am getting error:

Debug assertion failed, vector subscript out of range.

I gather that the problem lies in initializing sc_fifo. As I am newbie to system C, any help in initializing sc_fifo will be deeply appreciated.

sc_fifo.h

#ifndef SC_FIFO_E_H_
#define SC_FIFO_E_H_
#include <iostream>
#include "systemc.h"
class sc_fifo_e
{
    sc_fifo_e()
    {
        sc_fifo<int>a(10);        
    }
public:
    sc_fifo<int>a;
    
};

#endif

sc_fifo_array.h

 SC_FIFO_ARRAY_H_
#define SC_FIFO_ARRAY_H_
#include <iostream>
#include "systemc.h"
#include <sc_fifo_e.h>

 

SC_MODULE(sc_fifo_array)
{
public:
    SC_HAS_PROCESS(sc_fifo_array);
    void drain_packets(void);
    void p_source(void);
    sc_fifo_array(sc_module_name name_):sc_module(name_)
    {
        
        sc_vector<sc_vector<sc_fifo_e> >q[4][4];
        SC_METHOD(drain_packets);
        SC_THREAD(p_source);
    }

public:
    sc_vector<sc_vector < sc_fifo_e> >  q;
    int i,j,k;
};

void sc_fifo_array::drain_packets(void)
{
    int val;
    if (q[0][0].a.nb_read(val))
        cout<<"packets received\n";
    else
        cout<<"Fifo empty\n";
    next_trigger(4,SC_NS);


}

void sc_fifo_array::p_source(void)
{
   int val=1000;
   for (; ;)
   {
       wait(3, SC_NS);
       val++;
       q[0][0].a.write(val);
       std::cout<<sc_time_stamp()<<"p_thread wrote:"<<val<<std::endl;
   }
}

#endif

Main program:

#include <iostream>
#include <systemc>
#include <sc_fifo_e.h>
#include<sc_fifo_array.h>

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

    sc_fifo_array e1("e1");
    sc_start(10,SC_NS);
cout<<"Hare Ram\n";


return EXIT_SUCCESS;
}

 

 

Thanks.

 

Link to comment
Share on other sites

@David Black hinted you to the right solution: Look up in a good book on C++ how to properly initialise member variables in a constructor! For resources, check out this site.

As an additional hint: instead of initialising your local member variables sc_fifo_e::a and sc_fifo_array::q, you are declaring local variables in their respective constructors with the same name as your member variables. These shadow the member variables and are destroyed once execution leaves the constructor:

class sc_fifo_e
{
    sc_fifo_e()
    : a(10) // member variables need to be initialised in order of declaration
            // in the constructor's initializer list
    {
        // sc_fifo<int>a(10);        // declares a local variable
    }
public:
    sc_fifo<int>a;
    
};
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...