# 數位系統 第六次實驗報告 ###### tags: `digital system` 姓名:高聖傑 系級:資工113 學號:B093040016 實驗日期:11/15 ## 實驗一 ### 內容 - 目標 & 要求 - 實做兩種 BCD to Excess-3 轉換器 - 第一種(直接算) $z = D'$ $y = CD+C'D'$ $x = B'C+B'D+BC'D'$ $w = A+BC+BD$ 並畫出其 logic diagram - 第二種(重複使用運算過程半成品) $z = D'$ $y = CD+C'D' = CD + (C+D)'$ $x = B'C+B'D+BC'D' = B'(C+D)+B(C+D)'$ $w = A+BC+BD = A+B(C+D)$ 並畫出其 logic diagram - 使用 Dataflow level modeling 模擬並驗證兩種 BCD to Excess-3 轉換器等效 ### 過程 #### module code 第一種 ![](https://i.imgur.com/t1a7oRo.png =600x) 第二種 ![](https://i.imgur.com/eYpDlTy.png =450x) ```verilog= module bcd_to_exc3_1 (output z, y, x, w, input a, b, c, d); //wire nb, ncod, a1, a2, a3, a4, o1, o2; /* not(z, d); not(nb, b); and(a1, c, d); or (o1, c, d);//c+d not(ncod, o1); or(y, ncod, a1); and(a2, ncod, b); and(a3, o1, nb); or(x, a2, a3); and(a4, o1, b); or(w, a, a4); */ assign z = ~d; assign y = c&d | ~c&~d; assign x = ~b&c | ~b&d | b&~c&~d; assign w = a | b&c | b&d; endmodule module bcd_to_exc3_2 (output z, y, x, w, input a, b, c, d); //wire nb, ncod, a1, a2, a3, a4, o1, o2; wire cord, ncord; /* not(z, d); not(nb, b); and(a1, c, d); or (o1, c, d);//c+d not(ncod, o1); or(y, ncod, a1); and(a2, ncod, b); and(a3, o1, nb); or(x, a2, a3); and(a4, o1, b); or(w, a, a4); */ assign z = ~d; assign cord = c|d; assign ncord = ~(c|d); assign y = c&d | ncord; assign x = ~b&cord | b&ncord; assign w = a | b&cord; endmodule ``` > 很好笑,我一開始打成 Structural level ,後來看到同學用到 assign 還有 ~, |, & ,才驚覺自己打錯了XDD #### testbench code ```verilog= `timescale 1ns / 1ps module tbGen(); reg A, B, C, D; wire z1, y1, x1, w1, z2, y2, x2, w2; bcd_to_exc3_1 _tb1(.a(A), .b(B), .c(C), .d(D), .z(z1), .y(y1), .x(x1), .w(w1)); bcd_to_exc3_2 _tb2(.a(A), .b(B), .c(C), .d(D), .z(z2), .y(y2), .x(x2), .w(w2)); initial begin A = 1'b0; B = 1'b0; C = 1'b0; D = 1'b0; #10 A = 1'b0; B = 1'b0; C = 1'b0; D = 1'b1; #10 A = 1'b0; B = 1'b0; C = 1'b1; D = 1'b0; #10 A = 1'b0; B = 1'b0; C = 1'b1; D = 1'b1; #10 A = 1'b0; B = 1'b1; C = 1'b0; D = 1'b0; #10 A = 1'b0; B = 1'b1; C = 1'b0; D = 1'b1; #10 A = 1'b0; B = 1'b1; C = 1'b1; D = 1'b0; #10 A = 1'b0; B = 1'b1; C = 1'b1; D = 1'b1; #10 A = 1'b1; B = 1'b0; C = 1'b0; D = 1'b0; #10 A = 1'b1; B = 1'b0; C = 1'b0; D = 1'b1; #10 A = 1'b1; B = 1'b0; C = 1'b1; D = 1'b0; #10 A = 1'b1; B = 1'b0; C = 1'b1; D = 1'b1; #10 A = 1'b1; B = 1'b1; C = 1'b0; D = 1'b0; #10 A = 1'b1; B = 1'b1; C = 1'b0; D = 1'b1; #10 A = 1'b1; B = 1'b1; C = 1'b1; D = 1'b0; #10 A = 1'b1; B = 1'b1; C = 1'b1; D = 1'b1; #10 $finish; end endmodule ``` ### 模擬結果 ![](https://i.imgur.com/FNsnKI7.png) 兩個 function 所產生的波形圖一樣,因此可證明兩種 BCD to Excess-3 轉換器等效 ## 實驗二 ### 內容 - 目標 & 要求 - 使用 K map,簡化 $S_A = z\oplus(x\oplus y), C_A = xy + xz + yz$,並畫出其 logic diagram - 使用 K map,簡化 $S_B = z\oplus(x\oplus y), C_B = z(x\oplus y)+xy$,並畫出其 logic diagram - 使用 Structural level modeling 模擬並驗證 $S_A, S_B$ 等效、 $C_A, C_B$ 等效 ### 過程 full adder A ![](https://i.imgur.com/atQtz7Q.png =400x) full adder B ![](https://i.imgur.com/le47TC6.png =400x) #### module code ```verilog= module full_adder_A (output S, C, input x, y, z); wire a1, a2, a3, x1; and (a1, x, y); and (a2, z, y); and (a3, x, z); xor(x1, x, y); xor(S, x1, z); or(C, a1, a2, a3); endmodule module full_adder_B (output S, C, input x, y, z); wire a1, a2, x1; xor(x1, x, y); xor(S, x1, z); and(a1, x, y); and(a2, x1, z); or(C, a1, a2); endmodule ``` #### testbench code ```verilog= `timescale 1ns / 1ps module tbGen(); reg A, B, C; wire C_A, S_A, C_B, S_B; full_adder_A _tbA(.x(A), .y(B), .z(C), .S(S_A), .C(C_A)); full_adder_B _tbB(.x(A), .y(B), .z(C), .S(S_B), .C(C_B)); initial begin A = 1'b0; B = 1'b0; C = 1'b0; #10 A = 1'b0; B = 1'b0; C = 1'b1; #10 A = 1'b0; B = 1'b1; C = 1'b0; #10 A = 1'b0; B = 1'b1; C = 1'b1; #10 A = 1'b1; B = 1'b0; C = 1'b0; #10 A = 1'b1; B = 1'b0; C = 1'b1; #10 A = 1'b1; B = 1'b1; C = 1'b0; #10 A = 1'b1; B = 1'b1; C = 1'b1; #10 $finish; end endmodule ``` ### 模擬結果 ![](https://i.imgur.com/8pyeEuI.png) 兩個 function 所產生的波形圖一樣,因此可證明 $S_A, S_B$ 等效、 $C_A, C_B$ 等效 ## 實驗三 ### 內容 - 目標 & 要求 - 使用 Structural level modeling 實做一個由 8 個全加器組成的 8-bit ripple-carry adder ### 過程 #### module code 以下的 module half_adder, full_adder, ripple_carry_8_bit_adder 皆使用 Structural level modeling 其中,2 個半加器組成 1 個全加器,8 個全加器組成一個 8-bit 大的 ripple-carry 加法器 ```verilog= module half_adder (output S, C, input x, y); xor (S, x, y); and (C, x, y); endmodule module full_adder (output S, C, input x, y, z); wire S1, C1, C2; half_adder HA1 (S1, C1, x, y); half_adder HA2 (S, C2, S1, z); or (C, C2, C1); endmodule module ripple_carry_8_bit_adder ( output [7: 0] Sum, output ovfl, input [7:0] A, B, input c0); //carry wire c1, c2, c3, c4, c5, c6, c7; full_adder FA0 (Sum[0], c1, A[0], B[0], c0), FA1 (Sum[1], c2, A[1], B[1], c1), FA2 (Sum[2], c3, A[2], B[2], c2), FA3 (Sum[3], c4, A[3], B[3], c3), FA4 (Sum[4], c5, A[4], B[4], c4), FA5 (Sum[5], c6, A[5], B[5], c5), FA6 (Sum[6], c7, A[6], B[6], c6), FA7 (Sum[7], ovfl, A[7], B[7], c7); endmodule ``` #### testbench code ```verilog= `timescale 1ns / 1ps module tbGen(); reg [7:0] A; reg [7:0] B; reg C0; wire ovfl; wire [7:0] Sum; ripple_carry_8_bit_adder _tb(.A(A), .B(B), .c0(C0), .Sum(Sum), .ovfl(ovfl)); initial begin C0 = 1'b0; A = 8'd8; B = 8'd7; #10 C0 = 1'b0; A = 8'd17; B = 8'd23; #10 C0 = 1'b1; A = 8'd0; B = 8'd9; #10 C0 = 1'b1; A = 8'd35; B = 8'd44; #10 $finish; end endmodule ``` ### 模擬結果 ![](https://i.imgur.com/qnTNgGB.png =600x) 兩數字的加法運算結果正確,加入carry(C0)的運算結果也正確 ## 實驗心得 > 這次的實驗主要是讓我們練習用 verilog 程式碼實做 Combinational Logic 的 module。 > 在做實驗一時,我們觀察到一個現象,作為運算過程半成品的 **(C+D)** ,在此電路中重複出現多次。我們可以只計算一次 (C+D) ,並重複使用其結果來節省運算時間與電晶體數量。 > 做一半時搞了個小烏龍,我一開始打成 Structural level ,後來看到同學用到 assign 還有 ~, |, & ,才驚覺自己打錯了 XDD > > 實驗二同樣是重複使用運算過程半成品的 (x⊕y) 來實做全加器,其實有重複使用的版本就是 2 個半加器連在一起再加一個 OR。 > > 實驗三要實做我期待已久的加法器,其結構是把 2 個半加器組成 1 個全加器,8 個全加器組成一個 8-bit 大的 ripple-carry 加法器。 > 把常使用到的邏輯先存成 sub-module,再拿這些 sub-module 組成一個更大的 module ,如此一層包一層的巢狀結構,讓我想到之前學 C++ 的 Struct / Class。 ![](https://i.imgur.com/pEVAkR3.png)