2008年11月13日 星期四

全加器範例程式

這是一個SystemC的全加器模型程式
共有main.cpp,module.cpp module.h三支程式
裏面有3個物件(1)full adder[全加器](2)pattern generator[訊號產生器](3)monitor[監控程式]
main.cpp
#include <systemc.h>
#include <iostream>
#include "module.h"

int sc_main(int argc, char** argv){
        sc_signal<int> a, b, carry, co, sum;

        //module ptr
        full_adder adder("adder");
        adder.carry(carry);
        adder.a(a);
        adder.b(b);
        adder.sum(sum);
        adder.co(co);

        pattern_gen pg("pattern_gen");
        pg.d_a(a);
        pg.d_b(b);
        pg.d_carry(carry);

        monitor mon("monitor waveform");
        mon.m_a(a);
        mon.m_b(b);
        mon.m_carry(carry);
        mon.m_sum(sum);
        mon.m_cout(co);


        sc_start(50, SC_NS);

        return 0;
}

module.h
#include <systemc.h>

SC_MODULE(full_adder){
        sc_in<int> carry;
        sc_in<int> a;
        sc_in<int> b;

        sc_out<int> sum;
        sc_out<int> co;


        void proc_full_adder();

        SC_CTOR(full_adder){
                SC_METHOD(proc_full_adder);
                sensitive << carry << a << b;
        }
};

SC_MODULE(pattern_gen){
        sc_out<int> d_a;
        sc_out<int> d_b;
        sc_out<int> d_carry;

        void proc_pattern_gen();

        SC_CTOR(pattern_gen){
                SC_THREAD(proc_pattern_gen);
        }
};

SC_MODULE(monitor){
        sc_in<int> m_a, m_b, m_carry, m_sum, m_cout;

        void proc_monitor();

        SC_CTOR(monitor){
                SC_THREAD(proc_monitor);
                //sensitive << m_a << m_b << m_carry << m_sum << m_cout;
                sensitive << m_a << m_b << m_carry;
        }
};

module.cpp
#include <systemc.h>
#include <iostream>
#include "module.h"
//using namespace std;

void full_adder::proc_full_adder(){
        sum = a ^ b ^ carry;
        co = (a & b) | (b & carry) | (carry & a);
}

void pattern_gen::proc_pattern_gen(){
        sc_uint<3> pattern;
        pattern = 0;

        while (1){
                d_a = pattern[0];
                d_b = pattern[1];
                d_carry = pattern[2];

                wait(5, SC_NS);
                pattern++;
        }
}

void monitor::proc_monitor(){
        while (1){
                cout << "At time " << sc_time_stamp() << "::";
                cout << "(a, b, carry): ";
                cout << m_a << m_b << m_cout;
                cout << " (sum, carry_out): " << m_sum << m_cout << endl;
                wait();
        }
}

Makefile
LIB_DIR=-L/usr/systemc/lib-linux
CPPFLAGS=-I/usr/systemc/include

LIB=-lsystemc

APP=main
OBJS = main.o module.o

$(APP):$(OBJS)
        g++ -o $@ $^ $(LIB_DIR) $(LIB)
.c.o:
        g++ $(CPPFLAGS) -c $@ $<

clean:
        rm -rf $(APP) *.o


執行結果:
At time 0 s::(a, b, carry): 000 (sum, carry_out): 00
At time 5 ns::(a, b, carry): 100 (sum, carry_out): 00
At time 10 ns::(a, b, carry): 010 (sum, carry_out): 10
At time 15 ns::(a, b, carry): 110 (sum, carry_out): 10
At time 20 ns::(a, b, carry): 001 (sum, carry_out): 01
At time 25 ns::(a, b, carry): 100 (sum, carry_out): 10
At time 30 ns::(a, b, carry): 011 (sum, carry_out): 01
At time 35 ns::(a, b, carry): 111 (sum, carry_out): 01
At time 40 ns::(a, b, carry): 001 (sum, carry_out): 11
At time 45 ns::(a, b, carry): 100 (sum, carry_out): 00
資料來源:
http://twins.ee.nctu.edu.tw/courses/soc_sys_overview_04fall/lab.html

3 則留言:

jason lin 提到...
作者已經移除這則留言。
Kao Quey-Liang 提到...

感謝您的範例。

但是module.cpp的第九行少了兩個or,
是否忘了加?

史丹利 提到...

謝謝你!
不知道是不是以前在貼Code時, 貼到不見了!
剛剛補上去了^^