Jump to content
Alaba

Initializing an array of rc components

Recommended Posts

Hello 

I 'm trying  to create an nxn array (nested vectors) of an rc component,  with each having different r,c values 

ex: rc[0][0] (r=10,c=5), rc[1][1] (r=3,c=4).....

I've  created the rc sub module that take the values of R and C as  arguments,

As follows:

SC_MODULE (rc){

    sca_eln::sca_terminal n;    
    sca_eln::sca_terminal p;
    sca_eln::sca_r r1;
    sca_eln::sca_c c1;
    sca_eln::sca_node  n1;  
   
    rc( sc_core::sc_module_name nm , double r0_,double c0_) :p("p"),n("n"), r1("r1"),c1("c1"), r0(r0_),c0(c0_) 
     
    {
        r1.p(p);
        r1.n(n1);
        r1.value=r0;    
        c1.p(n1);
        c1.n(n);
        c1.value=c0;
         
    }

   private:
    double r0; 
    double c0; 
};

 

 I have found the following  post that shows how to initialize 1D vector so I customized my module accordingly ,which a follows    

#include <systemc>
#include <systemc-ams>
using namespace  sc_core;

SC_MODULE (rc){

    sca_eln::sca_terminal n;    
    sca_eln::sca_terminal p;
    sca_eln::sca_r r1;
    sca_eln::sca_c c1;
    sca_eln::sca_node  n1;  
   
    rc( sc_core::sc_module_name nm , double r0_,double c0_) :p("p"),n("n"), r1("r1"),c1("c1"), r0(r0_),c0(c0_) 
     
    {
        r1.p(p);
        r1.n(n1);
        r1.value=r0;    
        c1.p(n1);
        c1.n(n);
        c1.value=c0;
         
    }

   private:
    double r0; 
    double c0; 
};


struct create_mod
{
     double m_arg1;
     double m_arg2;
     create_mod(double arg1, double arg2) : m_arg1(arg1), m_arg2(arg2) {}
     rc* operator()(const char* name, size_t)
     {
          return new rc(name, m_arg1, m_arg2);
     }
};



SC_MODULE (rc_tb){
    rc rc0;
    rc_tb( sc_core::sc_module_name nm ):rc0("rc0",1.0,1.0)  {
        unsigned int num_of_rcs = 4;
        unsigned int r_value = 5, c_value = 4;
        sc_vector<rc> vec_rc("mods");
        vec_rc.init(num_of_rcs, create_mod( r_value,  c_value));
        //... connect nodes
    }
 
};

 

Now the code works But it is only for 1D array 

This method also forces me to set same argument values for all the initialized submodules  (  vec_rc.init(num_of_rcs, create_mod( r_value,  c_value));  ).

So my 2 questions are

1- How to make it a 2D vectors ?

2-  How to initialize each component with specific RC values?  

 I also have tried the code  suggested in to make a 2D nested vectors but I failed 

 

I don't mind using either methods  as long as it does the job. It will be nice if I use lambda function also

Thanks in advance 

RF

 

Share this post


Link to post
Share on other sites

Any two-dimensional array a2d[rows][cols] can be mapped to a one-dimensional array a1d[rows*cols]. Accessing an individual element using row and col requires just mapping the coordinate to the equivalent one-dimensional index i = row * cols + col.

If you want to use a nested sc_vector< sc_vector<rc> >, you should adapt the code @Philipp A Hartmann suggested in the second thread, which you mentioned. @T. Zimmermann provided a working example in the same same thread. However, it is not making use of lambda functions to further compact the code. As I wrote in the same thread, you need two element creators, which can be coded on site as lambda functions, one to create an sc_vector for each row and the other to create each rc instance. That creator may pass any additional value besides the module name to the module's constructor. These arguments you have to either fetch from some value array or calculate on the call site. For this, you will have to keep track of the row and col index. The creator of the row vectors gets passed the current index of the row as second argument. The inner creator for the module gets passed the current index within the row, i.e., the col as argument. The current row can be passed to that inner creator by simply capturing it by value (or reference) when defining the lambda function for that inner element creator. An array with component parameters is also best passed through the creators by capturing it by refer

I suggest that you make yourself more familiar with Modern C++ and also to have a look at @ralph.goergen's and @Philipp A Hartmann's paper on the topic presented at DVCon Europe 2015.

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.


×
×
  • Create New...