// SystemC counter
// compiler: Borland 5.5 + SystemC_Win1.0 Beta
//
#include <stdio.h>
#include <systemc.h>
//
// C++ counter
//
unsigned int counter(unsigned int cnt)
{
return (cnt+1);
}
//
// SystemC counter module
//
class counter_module : public sc_module
{
SC_HAS_PROCESS(counter_module);
public:
sc_in_clk clk, res;
sc_out<unsigned int> cnt;
private:
void increment_counter()
{
if(res.read() == 0)
cnt.write(0);
else
cnt.write(counter(cnt.read()));
}
public:
counter_module(sc_module_name nm) : sc_module(nm),
clk("clk"),
res("res"),
cnt("cnt")
{
SC_METHOD(increment_counter);
sensitive_pos << clk;
sensitive_neg << res;
}
const char* hdl_name() const { return "counter_module"; }
};
//
// Stimulus generator
//
SC_MODULE(stimulus)
{
sc_in<bool> clk;
sc_out<bool> res;
sc_in<unsigned int> cnt;
unsigned cycle;
SC_CTOR(stimulus)
{
SC_METHOD(entry);
sensitive_pos(clk);
cycle = 0;
}
void entry()
{
cycle++;
if(cycle == 0 || cycle == 22) res.write(false);
else res.write(true);
cout << "Counter = " << cnt.read() << " at cycle " << cycle << endl;
if(cycle == 100)
{
cout << "Simulation of " << cycle << " items finished at time " << sc_simulation_time() << endl;
sc_stop();
};
}
};
//
// Top-level module
//
int sc_main (int argc, char *argv[])
{
sc_clock clk("clk",1,SC_NS);
sc_signal<bool> res;
sc_signal<unsigned int> cnt;
stimulus stimulus_inst("stimulus_block");
stimulus_inst.clk(clk.signal());
stimulus_inst.res(res);
stimulus_inst.cnt(cnt);
counter_module counter_module_inst ("counter_module_dut");
counter_module_inst.clk(clk.signal());
counter_module_inst.res(res);
counter_module_inst.cnt(cnt);
sc_start(clk, -1);
return 0;
}