Systemc/2-bit adder
รุ่นแก้ไขเมื่อ 08:34, 17 มิถุนายน 2559 โดย Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย '<syntaxhighlight lang="cpp"> #include "systemc.h" #include <cstdio> SC_MODULE (adder1bit) { sc_in<sc_uint<1> > a,b,cin; sc_out<sc_...')
#include "systemc.h"
#include <cstdio>
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);
printf("%d, %d\n",res,res >> 1);
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;
}
};