Systemc/2-bit adder

จาก Theory Wiki
ไปยังการนำทาง ไปยังการค้นหา

adder.cpp

#include "systemc.h"

SC_MODULE (adder1bit) {
  sc_in<sc_uint<1> > a,b,cin;
  sc_out<sc_uint<1> > z,cout;

  void add() {
    int aa = a.read();
    int bb = b.read();
    int cc = cin.read();

    int res = aa+bb+cc;
    z.write(res % 2);
    cout.write(res >> 1);
  }

  SC_CTOR(adder1bit) {
    SC_METHOD(add);
    sensitive << a;
    sensitive << b;
    sensitive << cin;
  } 
};

SC_MODULE (v2in) {
  sc_in<sc_uint<2> > i;

  sc_out<sc_uint<1> > o0;
  sc_out<sc_uint<1> > o1;

  SC_CTOR (v2in)
  {
    SC_METHOD (process);
    sensitive << i;
  }

  void process()
  {
    sc_uint<2> t = i.read();
    o0 = (int)(t[0]);
    o1 = (int)(t[1]);
  }
};

SC_MODULE (v2out) {
  sc_in<sc_uint<1> > i0;
  sc_in<sc_uint<1> > i1;
  
  sc_out<sc_uint<2> > o;

  SC_CTOR (v2out)
  {
    SC_METHOD (process);
    sensitive << i0 << i1;
  }

  void process()
  {
    sc_uint<2> t;
    t[0] = i0.read();
    t[1] = i1.read();
    o.write(t);
  }
};

SC_MODULE (adder2bit) {
  sc_in<sc_uint<2> > a,b;
  sc_in<sc_uint<1> > cin;
  
  sc_out<sc_uint<2> > z;
  sc_out<sc_uint<1> > cout;

  adder1bit adder1,adder2;
  v2in va;
  v2in vb;
  v2out vz;

  sc_signal<sc_uint<1> > a0,a1,b0,b1,z0,z1, cinter;
  
  SC_CTOR(adder2bit)
    : adder1("ADDER1"), adder2("ADDER2"), va(""), vb(""), vz("")
  {
    va.i(a);
    va.o0(a0);
    va.o1(a1);

    vb.i(b);
    vb.o0(b0);
    vb.o1(b1);

    vz.i0(z0);
    vz.i1(z1);
    vz.o(z);
    
    adder1.a(a0);
    adder1.b(b0);
    adder1.cin(cin);
    adder1.cout(cinter);
    adder1.z(z0);

    adder2.a(a1);
    adder2.b(b1);
    adder2.cin(cinter);
    adder2.cout(cout);
    adder2.z(z1);
       
    sensitive << a;
    sensitive << b;
    sensitive << cin;
  } 
};

main

#include "systemc.h"
#include "adder.cpp"
#include <iostream>

int sc_main (int argc, char* argv[]) {
  sc_signal<sc_uint<1> > cc;
  sc_signal<sc_uint<2> > x;
  sc_signal<sc_uint<2> > y;
  sc_signal<sc_uint<2> > z;
  sc_signal<sc_uint<1> > ccc;
    
  adder2bit adder("ADDER");

  adder.a(x);
  adder.b(y);
  adder.z(z);
  adder.cin(cc);
  adder.cout(ccc);

  cc = 0;
  sc_start(1, SC_NS);

  sc_trace_file *wf = sc_create_vcd_trace_file("adder1");
  sc_trace(wf, x, "x");
  sc_trace(wf, y, "y");
  sc_trace(wf, cc, "cc");
  sc_trace(wf, z, "z");
  sc_trace(wf, ccc, "ccc");

  for(int bx = 0; bx < 4; bx++) {
    for(int by = 0; by < 4; by++) {
      x = bx;
      y = by;
      sc_start(1, SC_NS);
    }
  }
  
  sc_close_vcd_trace_file(wf);
  return 0;// Terminate simulation
}