# 軟硬體協同設計HW1
### ● 作業標題:4-bit full adder
### ● 班級姓名座號:國立高雄大學電機系大四 B1095117 黃致衡
### ● 授課老師:林宏益教授
---
## 一、作業內容:
使用fulladder製作出4bits的加法器
## 二、實作步驟:
### 1. 先寫出1bit的全加器 (fulladder.v)
```
module fadd (
a, // I 1-bit : first input
b, // I 1-bit : Second input
cin, // I 1-bit : Carry input
s, // O 1-bit : sum output
cout // O 1-bit : carry output
);
input a, b, cin;
output s, cout;
wire s, co;
assign {cout, s} = a + b + cin;
endmodule // End of Module addbit
```
### 2. 再使用 hierachy 的方式將 1bit 的 module 引用至 top module 中 (fourbits__fulladder.v)
```
`include "fulladder.v"
module add4__explicit (
s, // O 4-bit : Output of the adder
cout, // O 1-bit : Carry output of adder
r1, // I 4-bit : first input
r2, // I 4-bit : second input
ci // O 1-bit : carry input
);
input [3:0] r1;
input [3:0] r2;
input ci;
output [3:0] s;
output cout;
wire [3:0] r1, r2;
wire ci;
wire [3:0] s;
wire cout, c1, c2, c3;
fadd u0 (
.a (r1[0]), // I 1-bit : first input
.b (r2[0]), // I 1-bit : Second input
.cin (ci), // I 1-bit : Carry input
.s (s[0]), // O 1-bit : sum output
.cout (c1) // O 1-bit : carry output
);
fadd u1 (
.a (r1[1]) , // I 1-bit : first input
.b (r2[1]) , // I 1-bit : Second input
.cin (c1) , // I 1-bit : Carry input
.s (s[1]) , // O 1-bit : sum output
.cout (c2) // O 1-bit : carry output
);
fadd u2 (
.a (r1[2]) , // I 1-bit : first input
.b (r2[2]) , // I 1-bit : Second input
.cin (c2) , // I 1-bit : Carry input
.s (s[2]) , // O 1-bit : sum output
.cout (c3) // O 1-bit : carry output
);
fadd u3 (
.a (r1[3]) , // I 1-bit : first input
.b (r2[3]) , // I 1-bit : Second input
.cin (c3) , // I 1-bit : Carry input
.s (s[3]) , // O 1-bit : sum output
.cout (cout) // O 1-bit : carry output
);
endmodule // End Of Module adder
```
### 3. 撰寫 Testbench 並設定 $random 使其產生15組的測試數據 (fourbits__fulladder__tb.v)
```
`timescale 1ns/100ps
`include "fulladder.v"
`include "fourbits__fulladder.v"
module add4__explicit__tb;
reg [3:0] A, B;
reg CIN;
wire [3:0] SUM;
wire COUT;
add4__explicit add4_explicit_tb(SUM, COUT, A, B, CIN);
initial
begin
$dumpfile("hw_and_sw_hw1.vcd");
$dumpvars(0, add4__explicit__tb);
$monitor("A = %b, B = %b, SUM = %b, COUT = %b", A, B, SUM, COUT);
CIN=4'b0000;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 A={$random} % 16; B={$random} % 16;
#10 $finish;
end
endmodule
```
### 4. 使用 lcarus Verilog 經過 compile 產生 .vcd檔,並使用 GTKWave 查看 simulation 的波型結果
### 5. 開啟 Xilinx Vivado 並進行 Synthesis for FPGA
## 三、實作結果:
### ◎ schematic



### ◎ .vcd

## 四、實驗心得:
本次的作業困擾我最多的地方在於對環境的不熟悉,撰寫 Verilog 程式本身不是最難的問題,而是在於在 Compile 過程中對於 tool 的陌生使得我在過程中頻頻出現報錯的警訊等等,尤其以在 VScode 中鍵入 make 時,看不懂腳本的我更是對於指令不知如何下而不知所措。所幸在同學的協助下告訴我 Top module 的設置方式才使的我的程式能順利完成編譯過程。
也謝謝教授親自拍攝了 VScode 的設定過程影片,使我能依樣畫葫蘆把環境建置好,謝謝教授的教導。
## 五、參考文獻:
> EEF946〈軟硬體協同設計〉課堂參考講義