Jump to content
Ganesan R

How to initialize sc_fifo

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.

 

Share this post


Link to post
Share on other sites

Your problem is C++ rather than SystemC. "Initialization" should be titled "construction". You either need to use the initializer list to invoke the constructor, or if you are using C++11, you can use uniform initializer syntax.

 

Share this post


Link to post
Share on other sites

Thank you very much Sir.

Yes, I am trying to get on with c++.

Won't this line initialize: sc_vector<sc_vector<sc_fifo_e> >q[4][4] as that has been called inside sc_fifo_array constructor?

Pl. help.

 

 

Share this post


Link to post
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;
    
};

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

×