Tuesday, 10 November 2015

Basic Gates using SystemC - OR Gate and Testbench


//Include SystemC library definitions
#include "systemc.h"

// OR Gate Module
SC_MODULE(or_gate){
 sc_in a, b; // Input of type bool
 sc_out c; // Output of type bool

 // Process(Funtionality of OR)
 void or_gate_p(){
  c.write(a.read() | b.read());
 }

 // Constructor for OR Gate module
 SC_CTOR(or_gate){
  SC_METHOD(or_gate_p);
  // Process sensitivity
  sensitive << a << b;
 }
};

// OR Gate testbench
int sc_main(int argc, char* argv[]){

 // testbench signals
 sc_signal a, b, c;

 // Trace file pointer to store waveforms in VCD
 sc_trace_file *tf;

 // Module instantiation and name based connection
 or_gate or1("or_gate_or1");
 or1.a(a);
 or1.b(b);
 or1.c(c);

 // Trace file name and timescale unit
 tf = sc_create_vcd_trace_file("or_gate");
 tf->set_time_unit(1, SC_NS);

 // Trace file signals
 sc_trace(tf, a, "a");
 sc_trace(tf, b, "b");
 sc_trace(tf, c, "c");


 a = 0;
 b = 0;
 sc_start(1.0, SC_NS);

 a = 0;
 b = 1;
 sc_start(1.0, SC_NS);

 a = 1;
 b = 0;
 sc_start(1.0, SC_NS);

 a = 1;
 b = 1;
 sc_start(1.0, SC_NS);

 sc_stop();

 // Close trace file
 sc_close_vcd_trace_file(tf);

 // Display end simulation time
 cout << "Finished at time " << sc_time_stamp() << endl;

 return 0;
}