第四章 組合邏輯 4- 組合電路
4-3 設計步驟 組合電路的設計. 由電路的敘述, 決定所需的輸入與輸出的個數並且對每一個輸入與輸出安排一個變數符號 2. 導出真值表並定義輸入與輸出間的關係 3. 對每一個輸出求出以輸入變數為函數之簡化的布林函數 4. 畫出邏輯圖並且證明設計的正確性 BCD 碼到超 3 碼轉換器 2
BCD 到超 3 碼卡諾圖 BCD 到超 3 碼電路圖 3
4-4 二進位加法器 --- 減法器 半加法器 (half adder) x y S C S = x y + xy C = xy 半加法器 電路 4
全加法器 真值表 x y z C S S = x y z + x yz + x y z + xyz C = xy + yz + xz 全加法器 卡諾圖 5
全加法器 電路 二進位加法器 A = B = S = 6
進位傳播 進位產生 Gi 進位傳播 Pi P = A B i i i G = A B i i i Si = Pi Ci C = G + PC i+ i i i 進位遞迴產生器 7
具有進位遞迴的 4 位元加法器 二進位減法器 4 位元加法器 --- 減法器 8
4-5 十進位加法 / 器 BCD 加法器的推導 BCD 加法器電路 9
4-6 二進位乘法器 2 位元乘 2 位元 4 位元乘 3 位元之二進位乘法器
4-7 大小比較器 A = A B = B 3A2 A A 3B2B B x = A B + A B i i i i i ( A = B) = x x x x 3 3 2 3 ( A > B) = A B + x A B + x x A B + x x x A B 3 3 3 3 2 2 2 ( A < B) = A B + x A B + x x A B + x x x A B 2 3 3 2 2 3 3 2 2 4 位元大小比較器
4-8 解碼器 具有致能輸入之 2 對 4 線解碼器 2
利用 3x8 解碼器建立 4x6 解碼器 利用解碼器實現組合邏輯電路 S( x, y, z) = (,2,4,7) C( x, y, z) = (3,5,6,7) 3
4-9 編碼器 z = D + D y = D x = D 2 4 3 + D + D 3 5 + D 5 + D + D 6 6 + D 7 + D + D 7 7 優先權編碼器之真值表 4
優先權編碼器卡諾圖 4 輸入優先權編碼器 x = D 2 y = D 3 V = D + D 3 + D D 2 + D + D 2 + D 3 5
4- 多工器 (Multiplexers) 4 對 線多工器 6
四重 2 對 線多工器 利用多工器實現布林函數 F( x, y, z) = (,2,6,7 ) 7
利用多工器實現一個 4 輸入函數 F( A, B, C, D) = (,3,4,,2,3,4,5 ) 三態閘 8
利用三態閘構成多工器 4- 組合電路的硬體描述語言 閘階層模型關鍵字 and nand or nor xor xnor not buf 9
HDL 範例 4- (2 對 4 線解碼器的閘階層描述 ) //Gate-level description of a 2-to-4-line decoder //Figure 4-9 module decoder_gl (A,B,E,D); input A,B,E; output [:3]D; wire Anot,Bnot,Enot; not n (Anot,A), n2 (Bnot,B), HDL 範例 4- (2 對 4 線解碼器的閘階層描述 ) n3 (Enot,E); nand n4 (D[],Anot,Bnot,Enot), n5 (D[],Anot,B,Enot), n6 (D[2],A,Bnot,Enot), n7 (D[3],A,B,Enot); endmodule 2
HDL 範例 4-2 (4 位元加法器之底部向上層次化描述 ) //Gate-level hierarchical description of 4-bit adder // Description of half adder (see Fig 4-5b) module halfadder (S,C,x,y); input x,y; output S,C; //Instantiate primitive gates xor (S,x,y); and (C,x,y); HDL 範例 4-2 (4 位元加法器之底部向上層次化描述 ) endmodule //Description of full adder (see Fig 4-8) module fulladder (S,C,x,y,z); input x,y,z; output S,C; wire S,D,D2; //Outputs of first XOR and two AND gates //Instantiate the halfadder halfadder HA (S,D,x,y), 2
HDL 範例 4-2 (4 位元加法器之底部向上層次化描述 ) HA2 (S,D2,S,z); or g(c,d2,d); endmodule //Description of 4-bit adder (see Fig 4-9) module _4bit_adder (S,C4,A,B,C); input [3:] A,B; input C; HDL 範例 4-2 (4 位元加法器之底部向上層次化描述 ) output [3:] S; output C4; wire C,C2,C3; //Intermediate carries //Instantiate the fulladder fulladder FA (S[],C,A[],B[],C), FA (S[],C2,A[],B[],C), FA2 (S[2],C3,A[2],B[2],C2), FA3 (S[3],C4,A[3],B[3],C3); endmodule 22
三態閘 bufif (OUT, A, control); notif (Y, B, enable); 具有三態緩衝器之 2 對 線多工器 module muxtri (A, B, select, OUT); input A, B, select; output OUT; tri OUT; bufif (OUT, A, select); bufif (OUT, B, select); endmodule 23
資料流程模型 Verilog HDL 運算子 HDL 範例 4-3 (2 對 4 線解碼器的資料流程資料流程描述 ) //Dataflow description of a 2-to-4-line decoder //See Fig.4-9 module decoder_df (A,B,E,D); input A,B,E; output [:3] D; assign D[] = ~(~A & ~B & ~E), D[] = ~(~A & B & ~E), D[2] = ~(A & ~B & ~E), D[3] = ~(A & B & ~E); endmodule 24
HDL 範例 4-4 (4 位元加法器之資料流程描述 ) //Dataflow description of 4-bit adder module binary_adder (A,B,Cin,SUM,Cout); input [3:] A,B; input Cin; output [3:] SUM; output Cout; assign {Cout,SUM} = A + B + Cin; endmodule HDL 範例 4-5 ( 大小比較器之資料流程描述 ) //Dataflow description of a 4-bit comparator. module magcomp (A,B,ALTB,AGTB,AEQB); input [3:] A,B; output ALTB,AGTB,AEQB; assign ALTB = (A < B), AGTB = (A > B), AEQB = (A = = B); endmodule 25
HDL 範例 4-6 (2 對 線多工器使用條件式運算子描述 ) //Dataflow description of 2-to--line multiplexer module mux2x_df (A,B,select,OUT); input A,B,select; output OUT; assign OUT = select? A : B; endmodule 行為模型 HDL 範例 4-7 (2 對 線多工器使用條件式運算子描述 ) //Behavioral description of 2-to--line multiplexer module mux2x_bh(a,b,select,out); input A,B,select; output OUT; reg OUT; always @ (select or A or B) if (select == ) OUT = A; else OUT = B; endmodule 26
HDL 範例 4-8 (4 對 線多工器之行為描述 ) //Behavioral description of 4-to-- line multiplexer //Describes the function table of Fig. 4-25(b). module mux4x_bh (i,i,i2,i3,select,y); input i,i,i2,i3; input [:] select; output y; reg y; HDL 範例 4-8 (4 對 線多工器之行為描述 ) always @ (i or i or i2 or i3 or select) case (select) 2'b: y = i; 2'b: y = i; 2'b: y = i2; 2'b: y = i3; endcase endmodule 27
測試平台 一個模擬模組是具有下列形式的 HDL 程式 module 測試名稱 宣告特有的 reg 和 wire 識別字 在測試下例示設計模組 利用 initial 及 always 敘述產生模擬 顯示輸出響應 endmodule 系統功能 $display-- 顯示具有 end-of-line return 之變數或字串的一次值 $write 與 $display 相同, 但是沒有到下一行 $monitor 當在模擬期間值改變時及顯示變數 $time-- 顯示模擬時間 $finish-- 結束模擬 28
模擬與設計的交互模組 HDL 範例 4-9 ( 測試範例 4-6 所描述的 2 對 多工器 ) //Stimulus for mux2x_df. module testmux; reg TA,TB,TS; //inputs for mux wire Y; //output from mux mux2x_df mx (TA,TB,TS,Y); // instantiate mux initial 29
HDL 範例 4-9 ( 測試範例 4-6 所描述的 2 對 多工器 ) begin TS = ; TA = ; TB = ; # TA = ; TB = ; # TS = ; # TA = ; TB = ; end HDL 範例 4-9 ( 測試範例 4-6 所描述的 2 對 多工器 ) initial $monitor("select = %b A = %b B = %b OUT = %b time = %d", TS, TA, TB, Y, $time); endmodule 3
HDL 範例 4-9 ( 測試範例 4-6 所描述的 2 對 多工器 ) //Dataflow description of 2-to--line multiplexer //from Example 4-6 module mux2x_df (A,B,select,OUT); input A,B,select; output OUT; assign OUT = select? A : B; endmodule HDL 範例 4-9 ( 測試範例 4-6 所描述的 2 對 多工器 ) Simulatiom log: select = A= B= OUT= time= select = A= B= OUT= time= select = A= B= OUT= time=2 select = A= B= OUT= time=3 3
HDL 範例 4- ( 全加法器之多階電路的閘階層描述 ) //Gate-level description of circuit of Fig. 4-2 module analysis (A,B,C,F,F2); input A,B,C; output F,F2; wire T,T2,T3,F2not,E,E2,E3; or g (T,A,B,C); and g2 (T2,A,B,C); HDL 範例 4- ( 全加法器之多階電路的閘階層描述 ) and g3 (E,A,B); and g4 (E2,A,C); and g5 (E3,B,C); or g6 (F2,E,E2,E3); not g7 (F2not,F2); and g8 (T3,T,F2not); or g9 (F,T2,T3); endmodule 32
HDL 範例 4- ( 全加法器之多階電路的閘階層描述 ) //Stimulus to analyze the circuit module test_circuit; reg [2:]D; wire F,F2; analysis fig42(d[2],d[],d[],f,f2); initial begin HDL 範例 4- ( 全加法器之多階電路的閘階層描述 ) D = 3'b; repeat(7) # D = D + 'b; end initial $monitor ("ABC = %b F = %b F2 =%b ", D, F, F2); endmodule 33
HDL 範例 4- ( 全加法器之多階電路的閘階層描述 ) Simulation log: ABC= F= F2= ABC= F= F2= ABC= F= F2= ABC= F= F2= ABC= F= F2= ABC= F= F2= ABC= F= F2= ABC= F= F2= 34