3

Is it possible to define a SC class as a derivative of another SC class?

For example, a simple D-Flipflop (latch) implementation will have d and clk inputs and a q output. I want to define an Enabled-Latch on top of that class adding the en input and changing the sampling method accordingly. Is this possible?

ysap
  • 7,723
  • 7
  • 59
  • 122

1 Answers1

6

Yes you can, the same way as with regular C++ classes.

I'm not using macros SC_MODULE and SC_CTOR to illustrate that SC_MODULEs are just classes derived from sc_module

Macros SC_HAS_PROCESS and SC_METHOD are left for readablity purposes.

struct dff : public sc_module
{
    SC_HAS_PROCESS(dff);

    sc_in_clk    clk{ "clk" };
    sc_in<bool>  d{ "d" };
    sc_out<bool> q{ "q" };

    dff(const sc_module_name& name) : sc_module(name) {
        SC_METHOD(update_method);
        sensitive << clk.pos();
    }

    virtual void update_method() {
        q = d;
    }
};

struct dff_en : public dff
{
    sc_in<bool>  en{ "en" }; // enable signal
    sc_in<bool>  arst_n{ "arst_n" }; // asynchronous reset, active 0

    dff_en(const sc_module_name& name) : dff(name) {
        // adds to sentivity list of last process declared in base class
        sensitive << arst_n;
    }

    void update_method() override {
        if (!arst_n.read())
            q = 0;
        else if (en.read())
            q = d;
    }
};
random
  • 3,868
  • 3
  • 22
  • 39
  • Thanks. is this considered a SystemC object? How do you add the `en` input to a sensitivity list (assuming you want it so)? – ysap May 11 '16 at 11:53
  • 1
    Look at the source for the stupid SC_ macros and it all becomes a bit more obvious. I would never advise anyone to use the macros. – Ifor May 11 '16 at 14:09
  • I've modified code sample to show how to add to sensitivity list. By @lfor advice I've removed SC_MODULE macro. – random May 11 '16 at 16:39