Jump to content
Roman Popov

Elaboration time sized array

Recommended Posts

Hello, I need a data-structure to model RAMs and ROMs that are defined at elaboration time. For example when size and contents of memory are read from file during model construction.  I thought that sc_vector can be a solution, but looks like it has no specialization for members that are not sc_objects.  Any suggestions?

Currently I'm thinking of creating a wrapper over std::vector with following api:

template<typename T>
class sc_elab_vector : public sc_object {
     std::vector<remove_const_t<T>>  m_vec;
     //...
};

sc_elab_vector<int>  ram; 
sc_elab_vector<const int> rom;
  
SC_CTOR(mod) { // ELABORATION 
    ram.resize(42); // OK
    ram[0] = 42; // OK, for example models RAM on FPGA, initialized on reset
  
    for(int i = 0; i < 42; i++)
        rom.push_back(i); // OK
    rom[0] = 42; // OK
}
  
void test_thread() {  // SIMULATION
    ram[0] = 42; // OK
    ram.push_back(1); // Runtime error
    rom[0] = 42; // Runtime error  
}

Concept looks pretty generic for me, probably something like that should be standardized? Currently for SystemC synthesis we only have Compile-time sized arrays. But elaboration-time programming is more flexible than compile-time metaprogramming

Share this post


Link to post
Share on other sites

Hello Roman,

instead of developing your own class, have you considered to use std::unique_ptr<T[]> (C++'11) and std::make_unique<T[]> to allocate your array of a fixed size during elaboration? For examples see, e.g.:

http://en.cppreference.com/w/cpp/memory/unique_ptr

http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique

Regards,

Torsten

Share this post


Link to post
Share on other sites
On 11/25/2017 at 7:23 AM, maehne said:

Hello Roman,

instead of developing your own class, have you considered to use std::unique_ptr<T[]> (C++'11) and std::make_unique<T[]> to allocate your array of a fixed size during elaboration? 

I thought about it. There are two issues:

1) Raw arrays do not store size. This is inconvenient: for debugging purposes I often want to visualize contents of RAM. But without knowing a size it is not possible to create a pretty printer.  (Except for non-trivial constructible types, that actually store size, as specified by GCC/Clang ABI https://itanium-cxx-abi.github.io/cxx-abi/abi.html#array-cookies )

2) When modeling ROMs it will require to know memory contents at compile time:

    std::unique_ptr<const int[]> rom(new const int[3] {1,2,3});

As I understand I can't create initializer list {1,2,3} at runtime. So I can't initialize ROM contents from file.

 

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

×