(原創) 如何寫一個加法器? (C/C++) (SystemC) (IC Design)
Abstract
寫一個硬體的加法器,幾乎就跟軟體的HelloWorld一樣,是最基本的程式,此範例Demo如何用SystemC寫一個加法器。
Introduction
Adder.h
1#include "SystemC.h"
2
3SC_MODULE(Adder) { // Declare Adder Module
4 sc_in<int> a,b; // Declare in port
5 sc_out<int> sum; // Declare out port
6
7 void doAdd();
8
9 SC_CTOR(Adder) {
10 SC_METHOD(doAdd); // Register doAdd process
11 sensitive << a << b; // specify sensitivity
12 }
13};
2
3SC_MODULE(Adder) { // Declare Adder Module
4 sc_in<int> a,b; // Declare in port
5 sc_out<int> sum; // Declare out port
6
7 void doAdd();
8
9 SC_CTOR(Adder) {
10 SC_METHOD(doAdd); // Register doAdd process
11 sensitive << a << b; // specify sensitivity
12 }
13};
Adder.cpp
1#include "systemc.h"
2#include "Adder.h"
3
4void Adder::doAdd() {
5 sum = a + b;
6}
2#include "Adder.h"
3
4void Adder::doAdd() {
5 sum = a + b;
6}
AdderTest.cpp (TestBench)
1#include <iostream>
2#include "systemc.h"
3#include "Adder.h"
4
5using namespace std;
6
7int sc_main(int argc, char* argv[]) {
8 // Create instance of Adder
9 Adder adder("adder");
10 // Declare singal in testbench
11 sc_signal<int> a,b,sum;
12
13 // Initialize adder port by constructor
14 adder.a(a);
15 adder.b(b);
16 adder.sum(sum);
17
18 a = 1;
19 b = 2;
20
21 // Start simulation
22 sc_start(1);
23 cout << sum << endl;
24
25 return 0;
26}
2#include "systemc.h"
3#include "Adder.h"
4
5using namespace std;
6
7int sc_main(int argc, char* argv[]) {
8 // Create instance of Adder
9 Adder adder("adder");
10 // Declare singal in testbench
11 sc_signal<int> a,b,sum;
12
13 // Initialize adder port by constructor
14 adder.a(a);
15 adder.b(b);
16 adder.sum(sum);
17
18 a = 1;
19 b = 2;
20
21 // Start simulation
22 sc_start(1);
23 cout << sum << endl;
24
25 return 0;
26}
執行結果
1
2 SystemC 2.1.v1 --- Oct 18 2006 02:34:01
3 Copyright (c) 1996-2005 by all Contributors
4 ALL RIGHTS RESERVED
53
6Press any key to continue
2 SystemC 2.1.v1 --- Oct 18 2006 02:34:01
3 Copyright (c) 1996-2005 by all Contributors
4 ALL RIGHTS RESERVED
53
6Press any key to continue