Jump to content

Adding sc_attribute of type user-defined Class to an sc_object


Recommended Posts


I have defined one class "Class A" and when creating instances of that class e.g.  A* instA1 = new A ();

I want to attach the instA1 object to an sc_in port of my SystemC module as an attribute of that port.

- Can I use for that purpose the add_attribute function?

- How safe is that?

- Are there any other recommended mechanisms (as alternatives)?


Link to comment
Share on other sites

Basically yes but just add_attribute is not enough. From the top of my head: you need to declare in your sc_module:

  sc_core::sc_attribute<A*> attr{"attr", nullptr};
  sc_core::sc_in<bool> pin{"pin"};

in the constructor of your sc_module you need to add the attribute to the sc_object/sc_port:



  attr.value = new A();

you can assign a value. This way the attribute can be found e.g. via the SystemC object tree (sc_core::sc_get_top_level_objects() ).

Alternatives depend of your goal. One option would be to use CCI, esp. cci_param. But again, it depends what you want to achieve...

Link to comment
Share on other sites


I tried out your method as follows:

sc_object* test_object_find = sc_core::sc_find_object(port_name);
sc_core::sc_object& obj = dynamic_cast <sc_core::sc_object&>(*test_object_find);

PortElement<sc_logic>* PE = new PortElement<sc_logic>(count,hierarchical_name,port_name,obj);

sc_core::sc_attribute<PortElement<sc_logic>*> attr{"PortElement", nullptr};

 attr.value = PE;

Until here, I am capable to build and run without issue.

Then when I follow up the above lines of code with this one:


I can build, but when I run the simulation it crashes suddenly.

Which failure did I make when adding the attribute to the port object?

Thanks for your help.




On 8/4/2020 at 8:11 PM, Eyck said:



Link to comment
Share on other sites

Do you have an error message or stacktrace (gdb)? 'simulation [...] crashes suddenly' is very generic and can have manifold root causes...

Where do you execute the code? You can only create sc_object based elements during elaboration. But if PortElement is a plain C++ class I have no idea why it fails...

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.

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