0

Is there a way i can enable an automatic warning for my SystemC simulation whenever a fixed point variable overflows?

I already discovered the overflow_flag() function, but that one have to be check manually for every time i write to a signal in my code. Also, as I interpret the documentation, this flag does not discern between overflowing and precision loss?

Matombo
  • 77
  • 1
  • 9

1 Answers1

1

Is there a way i can enable an automatic warning for my SystemC simulation whenever a fixed point variable overflows?

Not in a centralized, standard way.

If you want to monitor a fixed set of variables, you may be able to use the sc_fxnum_observer extension available in some SystemC implementations.

To use it, you have to #define SC_ENABLE_OBSERVERS before including SystemC (ideally from your compiler command-line). The allows you to "attach" an observer to your sc_fixed<...> (and related) classes, which is notified upon the following events:

class sc_fxnum_observer
{
public:

    virtual void construct( const sc_fxnum& );
    virtual void  destruct( const sc_fxnum& );
    virtual void      read( const sc_fxnum& );
    virtual void     write( const sc_fxnum& );
};

You can then for example check the overflow_flag in a custom observer's write function:

struct overflow_observer : sc_dt::sc_fxnum_observer
{
  virtual void write( const sc_fxnum& v )
  {
    if( v.overflow_flag() )
      // ...
  }
};

During construction of a variable, you can pass a pointer to the observer then:

  overflow_observer my_overflow_observer;
  sc_dt::sc_fixed<...> f( &my_overflow_observer );

For a signal, the easiest solution is to derive a specialized signal and check for the overflow flag inside an overridden update function.

template <int W, int I,
      sc_q_mode Q = SC_DEFAULT_Q_MODE_,
      sc_o_mode O = SC_DEFAULT_O_MODE_, int N = SC_DEFAULT_N_BITS_>
class fixed_signal : public sc_core::sc_signal< sc_dt::sc_fixed<W,I,Q,O,N> >
{
  typedef sc_core::sc_signal< sc_dt::sc_fixed<W,I,Q,O,N> > base_type;
public:
  // constructor, etc.
  // ...
  using base_type::operator=;
protected:
  void update()
  {
    base_type::update();
    if( read().overflow_flag() )
      // ...
  }
};

Also, as I interpret the documentation, this flag does not discern between overflowing and precision loss?

Yes.

pah
  • 313
  • 1
  • 10