David Peng Posted July 27, 2023 Report Share Posted July 27, 2023 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 Quote Link to comment Share on other sites More sharing options...
Eyck Posted July 27, 2023 Report Share Posted July 27, 2023 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). Quote Link to comment Share on other sites More sharing options...
David Peng Posted July 27, 2023 Author Report Share Posted July 27, 2023 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. Quote Link to comment Share on other sites More sharing options...
David Peng Posted July 27, 2023 Author Report Share Posted July 27, 2023 Previous debug is wrong. It is due to other trivial reason that the sensitivity list not triggered. CLOSED. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.