Jump to content

Const array inside SC_CTOR


scsc

Recommended Posts

It seemed different behavior when putting a constant array inside the SC_CTOR vs. in process function. For example,

SC_CTOR(ex) {

    ...

  double angles[28];

  SC_METHOD(entry);

   double angles[28] = {...}

}

vs.

void ex::entry()

{

     double angles[...] = {...};

}

When putting constant array inside SC_CTOR, the readout values aren't correct. They are like huge numbers to power of 63. The second way had correct readout values. 

Link to comment
Share on other sites

You are declaring angles local in the body of the constructor. Once this finishes they are destroyed as they are stack allocated. You are running into undefined behavior. This is basic C++, lifetime of variables.

You need to declare angles as member of the class/SC_MODULE and only assign in teh constructor

Link to comment
Share on other sites

48 minutes ago, Eyck said:

You are declaring angles local in the body of the constructor. Once this finishes they are destroyed as they are stack allocated. You are running into undefined behavior. This is basic C++, lifetime of variables.

You need to declare angles as member of the class/SC_MODULE and only assign in teh constructor

But fir_data.h in SystemC's example folder used similar coefficient declaration:

  SC_CTOR(fir_data)
    { 
      SC_METHOD(entry);
      dont_initialize();
      sensitive << reset;
      sensitive << state_out;
      sensitive << sample;
#include "fir_const.h"
    }

Link to comment
Share on other sites

But if you look into fir_const.h you see:

coefs[0] = -6;
coefs[1] = -4;
coefs[2] = 13;
coefs[3] = 16;
coefs[4] = -18;
coefs[5] = -41;
coefs[6] = 23;
coefs[7] = 154;
coefs[8] = 222;
coefs[9] = 154;
coefs[10] = 23;
coefs[11] = -41;
coefs[12] = -18;
coefs[13] = 16;
coefs[14] = 13; 
coefs[15] = -4;

which is just assignement, not declaration. coefs is declared as member variable of module fir:

SC_MODULE(fir) {
  
  sc_in<bool>  reset;
  sc_in<bool>  input_valid;        
  sc_in<int>   sample;              
  sc_out<bool> output_data_ready;
  sc_out<int>  result;
  sc_in_clk    CLK;

  sc_int<9> coefs[16];

  SC_CTOR(fir)
     {      
      SC_CTHREAD(entry, CLK.pos());
          reset_signal_is(reset,true);
      #include "fir_const.h"
    }
  
  void entry();
};

In your code you declare and initialize a local variable:

void ex::entry(){
    double angles[...] = {...};
    ....
} 

which shadows your class member angels. As soon as the funtion ex::entry() finishes the local variable angels get destroyed. What you can do is just assigning it e.g.:

void ex::entry(){
    angles = {...};
    ....
} 

which is similar to the fir example you quated.

As I mentioned earlier, you should make yourself familiar with variable life times and visibility.

 

Link to comment
Share on other sites

Yeah, I did end up putting angles inside entry() and it worked. But I didn't know why the SC_CTOR way didn't work. Now you have pointed out the difference between the FIR example and my error. I complete got it now. Thanks for the advance on variable life times and visibility.

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