Search code examples
systemc

How to define `sc_fixed` variables that have word/integer lengths that are based off (but not the same size as!) those of a template parameter


Let's say I want to write a generic SystemC module which does some algorithm processing on fixed point data of various representations, so I implement it as as module template where the fixed point input/output type is specified as a template parameter. My algorithm requires more headroom for it's intermediate operations than is available by the input/output type so I'd like to declare some intermediate fixed point variables which have more integer bits but the same number of fractional bits. How do I do this? Is there any way to query the word/integer lengths of the sc_fixed template parameter and use/modify them to define new sc_fixed variables within the module.

I've tried using the wl() and iwl() methods but these only get evaluated at runtime so can't be used as template arguments.

Here is an example of what doesn't work:

template <typename T>
SC_MODULE(proc)
{
    sc_in<bool> clk;    // Clock signal
    sc_in<bool> rst_n;  // Reset signal
    sc_in<T> in_sig;    // Input signal
    sc_out<T> out_sig;  // Output signal

    const T tttt;
    typedef sc_fixed<tttt.wl()+3, tttt.iwl()+3> internal_sig_t;
    internal_sig_t internal_sig_0, internal_sig_1;

    ...
}

Is there a way of doing this which actually works?

Note: As a workaround I could just pass the sc_fixed template parameters instead of the predefined type but this starts getting very messy if I have multiple input/output types with different options for not just for word/integer lengths so would prefer not to go this route.


Solution

  • Answer provided on this forum thread: https://forums.accellera.org/topic/7596-how-to-define-sc_fixed-variables-that-have-wordinteger-lengths-that-are-based-off-but-not-the-same-size-as-those-of-a-template-parameter/