# 數位系統實驗報告Lab11 :::info 學號: B093040044 系級: 資工113 姓名: 蔡明軒 ::: ``` 實驗日期: 2021/11/29 ``` ## 實驗一 ### 內容 - 設計並驗證一個由兩個4-bit magnitude comparators組成的8-bit magnitude comparator ### 過程 1. 利用$x_i=(A_iB_i'+A_i'B_i)'$,得到L(較小)、G(較大)、E(相等)三個Boolean function $L=A_3B_3'+x_3A_2B_2'+x_3x_2A_1B_1'+x_3x_2x_1A_0B_0'$ $G=A_3'B_3+x_3A_2'B_2+x_3x_2A_1'B_1+x_3x_2x_1A_0'B_0$ $E=x_3x_2x_1x_0$ 2. 利用上列式子,寫出一個4-bit magnitude comparator ```verilog= module four_bit_magnitude_comparator(output L,G,E,input[3:0]A,B); wire[3:0] x; assign x[0] = ~(A[0]^B[0]), x[1] = ~(A[1]^B[1]), x[2] = ~(A[2]^B[2]), x[3] = ~(A[3]^B[3]), L = ~A[3]&B[3] | x[3]&~A[2]&B[2] | x[3]&x[2]&~A[1]&B[1] | x[3]&x[2]&x[1]&~A[0]&B[0], G = A[3]&~B[3] | x[3]&A[2]&~B[2] | x[3]&x[2]&A[1]&~B[1] | x[3]&x[2]&x[1]&A[0]&~B[0], E = x[0]&x[1]&x[2]&x[3]; endmodule ``` 3. 將8-bit切成兩半,分別放入4-bit magnitude comparator處理,較大的位數得到L2,G2,E2,較小的則得到L1,G1,E1 4. 利用下列式子,合併兩個結果,寫出8-bit magnitude comparator $L=L_2+E_2L_1$ $G=G_2+E_2G_1$ $E=E_2E_1$ ```verilog= module eight_bit_magnitude_comparator (output L,G,E,input[7:0]A,B); wire L1,L2,G1,G2,E1,E2; four_bit_magnitude_comparator c1(L2,G2,E2,A[7:4],B[7:4]), c2(L1,G1,E1,A[3:0],B[3:0]); assign L = L2 | E2&L1, G = G2 | E2&G1, E = E2&E1; endmodule ``` 5. 依照PTT給的測試資料,撰寫testbench檔案 | A | B | |:--------:| :------: | | 10101010 | 10101010 | | 11010011 | 10100000 | | 01010111 | 01010011 | | 01111111 | 10000000 | | 10010011 | 10010100 | ```verilog= `timescale 1ns / 1ps module tb; reg[7:0] A; reg[7:0] B; wire L,G,E; eight_bit_magnitude_comparator UUT(.L(L),.G(G),.E(E),.A(A),.B(B)); initial begin A=8'b10101010;B=8'b10101010; #10 A=8'b11010011;B=8'b10100000; #10 A=8'b01010111;B=8'b01010011; #10 A=8'b01111111;B=8'b10000000; #10 A=8'b10010011;B=8'b10010100; #10 $finish; end endmodule ``` 6. Run Synthesis and Simulation 7. 確認結果是否符合預期 ### 結果 - 預期結果 | A | B | L | G | E | |:--------:|:--------:| --- | --- | --- | | 10101010 | 10101010 | 0 | 0 | 1 | | 11010011 | 10100000 | 0 | 1 | 0 | | 01010111 | 01010011 | 0 | 1 | 0 | | 01111111 | 10000000 | 1 | 0 | 0 | | 10010011 | 10010100 | 1 | 0 | 0 | - 實際結果與預期結果相同 ![](https://i.imgur.com/QwblUMI.png) ## 實驗二 ### 內容 - 設計並驗證一個由兩個2-to-4 decoders 組成的 3-to-8 decoder ### 過程 1. 根據ppt給的真值表,寫出2-to-4 decoder ![](https://i.imgur.com/G6P66q0.png) ```verilog= module decoder_2x4(output[3:0] D,input A,B,E); assign D[0] = ~A & ~B & E, D[1] = ~A & B & E, D[2] = A & ~B & E, D[3] = A & B & E; endmodule ``` 2. 將y,z當成A,B,x當成E,~E,用兩個2-to-4 decoder組合成3-to-8 decoder ```verilog= module decoder_3x8(output[7:0] D,input x,y,z); decoder_2x4 d1(D[7:4],y,z,x), d2(D[3:0],y,z,~x); endmodule ``` 3. 撰寫三變數的testbench ```verilog= `timescale 1ns / 1ps module tb; reg x; reg y; reg z; wire[7:0] D; decoder_3x8 UUT(.D(D),.x(x),.y(y),.z(z)); initial begin x=1'b0; y=1'b0;z=1'b0; #10 x=1'b0; y=1'b0; z=1'b1; #10 x=1'b0; y=1'b1; z=1'b0; #10 x=1'b0; y=1'b1; z=1'b1; #10 x=1'b1; y=1'b0; z=1'b0; #10 x=1'b1; y=1'b0; z=1'b1; #10 x=1'b1; y=1'b1; z=1'b0; #10 x=1'b1; y=1'b1; z=1'b1; #10 $finish; end endmodule ``` 4. Run Synthesis and Simulation 5. 確認結果是否符合預期 ### 結果 - 實際結果與預期結果相同 ![](https://i.imgur.com/UvkeM8U.png) ## 實驗三 ### 內容 - 利用Behavioral level modeling,設計並驗證一個8-to-1 multiplexer - 利用 8-to-1 MUX ,實作並驗證下列四輸入的Boolean function $F(A,B,C,D)=\Sigma (1,2,5,8,9,10,12,13)$ ### 過程 1. 利用Behavioral level modeing 撰寫一個 8-to-1 multiplexer ```verilog= module mux_8x1(output reg Y,input [7:0] I,input [2:0] S); always @(I, S) case (S) 3'b000: Y = I[0]; 3'b001: Y = I[1]; 3'b010: Y = I[2]; 3'b011: Y = I[3]; 3'b100: Y = I[4]; 3'b101: Y = I[5]; 3'b110: Y = I[6]; 3'b111: Y = I[7]; endcase endmodule ``` 2. 將F(A,B,C,D)的真值表切成兩個兩個一組,找出F和D的關係 ![](https://i.imgur.com/ts2g6w2.png) 3. 將步驟二的結果寫成verilog,A,B,C當成多工器的SELECT port,步驟二的結果寫入陣列I,F為Boolean function的輸出 ```verilog= module F(output F,input A,B,C,D); wire[2:0] S; wire[7:0] I; assign S[0]=C,S[1]=B,S[2]=A,I[0]=D, I[1]=~D,I[2]=D,I[3]=1'b0,I[4]=1'b1, I[5]=~D,I[6]=1'b1,I[7]=1'b0; mux_8x1 m1(F,I,S); endmodule ``` 4. 撰寫四變數的testbench ```verilog= `timescale 1ns / 1ps module tb; reg A; reg B; reg C; reg D; wire F; F UUT(.F(F),.A(A),.B(B),.C(C),.D(D)); 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 ``` 5. Run Synthesis and Simulation 6. 確認結果是否符合預期 ### 結果 - 實際結果與預期結果相同 ![](https://i.imgur.com/FLts0dv.png) ## 實驗心得 >這次實驗與上周正課所教的比較器、解碼器、多工器相關。首先,第一題需要我們用兩個4-bit比較器組合成8-bit比較器,上禮拜的實驗的adder也有用到這個概念,透過先撰寫較小的module,再將這些module合併,可以大大減少我們需要打的code,只要有找到合併的方法,就可以利用這個方式讓撰寫程式過程更加輕鬆。 >實驗二是要撰寫Decoder,由於我是直接參考ppt一開始給的Decoder程式碼,沒注意到它和我們現在要做的是反向的,所以跑出來的結果也是相反的,後來我試著把一些not gate刪掉,才得到正確的結果。 >實驗三是要我們利用多工器來實現出一個布林函數,這次我採用了這次實驗所教的Behavioral level modeling才撰寫多工器,我第一次看到這個寫法的時候覺得挺新奇的,沒想到Verilog也會有提供類似C++的switch case寫法,後來我有去網路查一查其他的語法,發現連for迴圈,if else條件式判斷都有,令我大開眼界。