This design shows a simple alarm clock implementation in SystemC.
It consists of a user-defined Counter class, which is used
within a templated AlarmClock module.
Whenever the Counter overflows, the AlarmClock signals
this for one cycle on its alarm output.
#include <systemc.h> // ----------------------------------------------------------- // // simple counter class class Counter { public: Counter( unsigned int lowerBound, unsigned int upperBound, unsigned int initialValue ); bool overflow() const; void clearOverflow(); void setLowerBound( unsigned int lowerBound ); void setUpperBound( unsigned int upperBound ); void reset(); unsigned int getValue() const; void count( unsigned int stepSize = 1 ); private: unsigned int m_lowerBound; unsigned int m_upperBound; unsigned int m_counter; bool m_overflow; }; // ----------------------------------------------------------- // // alarm clock module template< unsigned int INTERVAL> SC_MODULE( AlarmClock ) { // Ports sc_in< bool > clock; sc_in< bool > reset; sc_out< bool > alarm; // Process prototypes void watchdog(); SC_CTOR( AlarmClock ) { SC_CTHREAD( watchdog, clock.pos() ); reset_signal_is(reset, true); } }; // ----------------------------------------------------------- // // main process template< unsigned int INTERVAL > void AlarmClock< INTERVAL >::watchdog() { Counter cntr( 0, INTERVAL - 1, 0 ); // local counter variable cntr.reset(); // reset block alarm.write( false ); wait (); while ( true ) // main loop { cntr.count(); // increment counter if ( cntr.overflow() ) // signal alarm { alarm.write( true ); cntr.clearOverflow(); } else { alarm.write( false ); } wait (); } } // ----------------------------------------------------------- // // sc_main implementation (stripped for synthesis) int sc_main(int, char *[]) { // top-level module AlarmClock<10> alarm_clock( "alarm_clock" ); return 0; } // ----------------------------------------------------------- // // counter implementation using namespace std; Counter::Counter( unsigned int lowerBound, unsigned int upperBound, unsigned int initialValue ) { #ifndef SC_SYNTHESIS if ( lowerBound >= upperBound ) { cerr << "lowerBound greater of equal to upperBound" << endl; } if ( ! ( ( lowerBound <= initialValue ) && ( initialValue <= upperBound ) ) ) { cerr << "initialValue out of bounds" << endl; } #endif m_counter = initialValue; m_lowerBound = lowerBound; m_upperBound = upperBound; m_overflow = false; } bool Counter::overflow() const { return( m_overflow ); } void Counter::clearOverflow() { m_overflow = false; } void Counter::setLowerBound( unsigned int lowerBound ) { m_lowerBound = lowerBound; } void Counter::setUpperBound( unsigned int upperBound ) { m_upperBound = upperBound; } void Counter::reset() { m_counter = m_lowerBound; m_overflow = false; } unsigned int Counter::getValue() const { return( m_counter ); } void Counter::count( unsigned int stepSize ) { unsigned int diff = m_upperBound - m_counter; if ( diff < stepSize ) // counter overflow { m_counter = m_lowerBound + ( stepSize - diff ) - 1; m_overflow = true; } else { m_counter += stepSize; } }