Jump to content

Carry class in sc_signal and memory allocation


Recommended Posts

I am using systemC for system-level simulation. I'd like to carry class in sc_signal like below:

    sc_port<sc_signal_out_if<pkt>> out;

Is there any guide for such application? I found I need at least overload operator == and <<.

One question is about memory allocation like code below:

    void main() {
      pkt *p = NULL;

      while (true) {
        p = new pkt(64);

        out->write(*p);
  
        wait(p->len/rate, SC_NS);
      }
    }

The issue is, when *p is written into out sc_port, its memory will be freed as it is allocated locally. I put a debug code there and found p always points to same address. I am familiar with systemverilog but have not used c/c++ for quite a long time. In UVM, I need write a copy of p into the output port for such issue but I have no idea how to do it in systemC. Any suggestion of this?

Thanks,

David

Link to comment
Share on other sites

If you write to the signal a copy of pkt is stored. This is C++ value semantics. Even an implementation like this

    void main() {
      while (true) {
        pkt p(64);
        out->write(p);
        wait(p.len/rate, SC_NS);
      }
    }

works correcty and even better than yours as you created a memory leak. Since at the reassignment of p using new the previously allocated memory gets lost. Better to use a std::unique_ptr.

I highly suggest to revive your C++ knowledge based on a modern C++ standard (C++11 or better C++17).

 

Link to comment
Share on other sites

Thanks for Eyck's prompt response. Surely I'll make myself get familiar with modern c++11 or 17 per your suggestion.

However I found it doesn't work as expected. My read side is something like this:

  public:
    sc_port<sc_signal_in_if<pkt>> in;

    SC_HAS_PROCESS(dummy_sink);
    dummy_sink(sc_module_name name): sc_module(name) {
      SC_THREAD(main);
      sensitive << in;
      dont_initialize();
    }

    void main() {
      pkt p;

      while(true) {
        p = in->read();

        wait();
      }

This never gets triggered, I put debug code in my overloaded operator == and found the lhs and rhs are always equal.

I guessed that because in the write loop, it just writes an object pointer to sc_signal instead of values. As the newly created p is a local object which will be freed after the while(true) loop, the out->write did update the value of same object again and again. When the sensitivity check is done, it check previous p and current p(actually points to same one), it never get triggered.

I tried your code, the result was the same. I put a print after pkt p(64) to show it address, it is the same. It seems I need manually managed memory by allocating in producer and freeing in consumer.

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