?
目錄
1.組合電路設(shè)計方法
1.1真值表方式(本質(zhì)是最小項表達式)
1.2邏輯表達式方式
1.3結(jié)構(gòu)描述方式
1.4抽象描述方式(從電路功能出發(fā))
2組合電路設(shè)計項目
2.1數(shù)字加法器
半加器(1位加法器)
全加器
串行進位加法器(行波進位加法器)
超前進位加法器
2.2數(shù)據(jù)比較器
2.3數(shù)據(jù)選擇器(MUX)
2.4數(shù)字編碼器
2.4.1 BCD編碼器
2.4.2 8線—3線編碼器
2.4.3 8線—3線優(yōu)先編碼器
2.4.4 余3編碼
2.5數(shù)字譯碼器
2.6數(shù)字校驗器
組合電路特點:電路中任意時刻的穩(wěn)態(tài)輸出僅僅取決于該時刻的輸入,而與電路原來的狀態(tài)無關(guān)。(該電路無記憶功能,只有輸入到輸出的通路,無從輸出到輸入的回路)
1.組合電路設(shè)計方法
? ? ? ?設(shè)計一個3個裁判的表決電路,當(dāng)兩個或兩個以上裁判同意時,判決器輸出1,否則輸出0.下面用四種方式分別設(shè)計。
1.1真值表方式(本質(zhì)是最小項表達式)
//A B C OUT 0 0 0 0 0 0 1 0 0 1 0 0 0 1 1 1 1 0 0 0 1 0 1 1 1 1 0 1 1 1 1 1 module voter(A,B,C,out); input A,B,C; output out; reg out; always@(*) begin case({A,B,C}) 3'b000:out <= 0; 3'b001:out <= 0; 3'b010:out <= 0; 3'b011:out <= 1; 3'b100:out <= 0; 3'b101:out <= 1; 3'b110:out <= 1; 3'b111:out <= 1; endcase end endmodule
1.2邏輯表達式方式
將真值表用卡諾圖來表示,然后化解電路得到邏輯函數(shù)表達式? out = AB+BC+AC
nodule voter(A,B,C,out); input A,B,C; output out; assign out = (A&B)|(B&C)|(A&C) endmodule
1.3結(jié)構(gòu)描述方式
從邏輯表達式out=AB+BC+AB可獲得其基本的電路結(jié)構(gòu)圖,根據(jù)結(jié)構(gòu)圖門級建模即可。
module voter(A,B,C,out); input A,B,C; output out; wire w1,w2,w3; and U1(w1,A,B); and U2(w2,B,C); and U3(w3,A,C); or U4(out,w1,w2,w3); endmodule
1.4抽象描述方式(從電路功能出發(fā))
module voter(A,B,C,out); input A,B,C; output out; wire [1:0] sum; assign sum = A+B+C; always@(sum) begin if(sum>1) out = 1; else out = 0; end endmodule
2組合電路設(shè)計項目
2.1數(shù)字加法器
半加器(1位加法器):如果不考慮有來自地位的進位將兩個1位二進制數(shù)相加,稱為半加,實現(xiàn)半加運算的電路稱為半加器。
全加器:再將兩個多位二進制相加時,除了最低位以外,每一位都應(yīng)考慮來自低位的進位,即:將兩個對應(yīng)位的加數(shù)和來自低位的進位3個數(shù)相加,所用的電路稱為全加電路。
串行進位加法器(行波進位加法器):使用全加器,依次將低位加法器的進位輸出端co接到高位全加器的進位輸入端,因每一位的相加結(jié)果都必須等到低一位的進位來到之后才能建立,因此這種結(jié)構(gòu)的電路稱為~
超前進位加法器:引入生成信號和傳播信號,高位的運算不需要等待低位運算完成。
這一級進位輸入信號Ci+1與上一級的兩個加數(shù)Ai Bi、上一級的進位輸入信號的關(guān)系:
第一行包含了所有可能出現(xiàn)進位的情況,第二行為化解之后的結(jié)果。
超前進位加法器是一種高速加法器,可提高運算速度,縮短延時? ?。
//2輸入8bit加法器
module eight_bits_fulladder(sum,cout,a,b,c,cin);
input [7:0] a,b,cin;
output [7:0] sum;
output cout;
assign {cout,sum} = a+b+cin;
endmodule
//4bit超前進位加法器
module fastAdder_4(sum,c_out,a,b,c_in);
input[3:0] a,b,c_in;
output[3:0] sum,c_out;
wire[3:0] g,p;
wire[4:0] c;
assign p=a^b; //傳播信號
assign g=a&b; //生成信號
assign c[0]=c_in;
assign c[1]=g[0]|(p[0]&c[0]); //推導(dǎo)公式
assign c[2]=g[1]|(p[1]&(g[0]|(p[0]&c[0])));
assign c[3]=g[2]|(p[2]&(g[1]|(p[1]&(g[0]|(p[0]&c[0])))));
assign c[4]=g[3]|(p[3]&(g[2]|(p[2]&(g[1]|(p[1]&(g[0]|(p[0]&c[0])))))));
assign sum = p^c[3:0];
assign c_out=c[4];
endmodule
2.2數(shù)據(jù)比較器
數(shù)據(jù)比較器分為兩類:一是等值比較器,只能檢測兩個數(shù)是否一致:二是量值比較強,能比較兩個數(shù)的大小。
//8bit數(shù)據(jù)比較強
module comp_8bit(a,b,agb,aeb,a1b);
input [7:0] a,b;
output agb,aeb,a1b;
reg agb,aeb,a1b; //agb代表a>b,aeb代表a=b;
always@(a or b)
begin
if(a>b)
{agb,aeb,a1b} = 3'b100;
else if(a<b)
{agb,aeb,a1b} = 3'b001;
else
{agb,aeb,a1b} = 3'b010;
end
endmodule
2.3數(shù)據(jù)選擇器(MUX)
常用條件運算符、if_else、case語句等方式實現(xiàn)其功能。
//以4選1選擇器為例,用2選1選擇器構(gòu)成的4選1選擇器
//有4個數(shù)據(jù)輸入端,2個選擇輸入端,一個數(shù)據(jù)輸出端。真值表如圖:
sel[1] sel[0] d_out
0 0 d_in[0]
0 1 d-in[1]
1 0 d_in[2]
1 1 d_in[3]
//條件運算符實現(xiàn)
module mux4to1(d_in,sel,d_out);
input [3:0] d_in;
input [1:0] sel;
output d_out;
wire [1:0] w1; //sel[0]選擇之后的結(jié)果
assign w1 = sel[0]?{d_in[3],d_in[1]}:{d_in[2],d_in[0]};
assign d_out = sel[1]? w1[1]:w1[0];
endmodule
//if_else語句實現(xiàn)
module mux4to1(d_in,sel,d_out);
input [3:0] d_in;
input [1:0] sel;
output d_out;
reg d_out;
always@(*)
begin
if(sel[1]==1)
begin
if(sel[0]==1)
d_out=d_in[3];
else
d_out=d_in[2];
end
else
begin
if(sel[0]==1)
d_out=d_in[1];
else
d_out=d_in[0];
end
end
endmodule
//case語句實現(xiàn)
module mux4to1(d_in,sel,d_out);
input [3:0] d_in;
input [1:0] sel;
output d_out;
reg d_out;
always@(*)
begin
case(sel)
2'b00:d_out <= d_in[0];
2'b01:d_out <= d_in[1];
2'b10:d_out <= d_in[2];
2'b11:d_out <= d_in[3];
endcase
end
endmodule
2.4數(shù)字編碼器(用n位二進制碼來表示m個特定信息,2的n次方>=m)
用二進制代碼表示有關(guān)的信號稱為編碼,所用電路稱為數(shù)據(jù)編碼器。
2.4.1 BCD編碼器:用一組二進制數(shù)來表示一個給定的十進制數(shù)(也稱10線—4線編碼器)
//8421BCD編碼器代碼如下:
module BCD8421(d_in,d_out);
input [8:0]d_in;
output [3:0]d_out;
reg [3:0]d_out;
always@(d_in)
case(d_in)
9'b000000000:d_out=4'b0000;
9'b000000001:d_out=4'b0001;
9'b000000010:d_out=4'b0010;
9'b000000100:d_out=4'b0011;
9'b000001000:d_out=4'b0100;
9'b000010000:d_out=4'b0101;
9'b000100000:d_out=4'b0110;
9'b001000000:d_out=4'b0111;
9'b010000000:d_out=4'b1000;
9'b100000000:d_out=4'b1001;
default d_out= 4'b0000;
endcase
endmodule
2.4.2 8線—3線編碼器
其特點為任何時刻只允許輸入一個有效信號,若出現(xiàn)兩個或兩個以上有效信號,電路就會中斷。
module code8_3(din,dout);
input [7:0]din;
output [2:0]dout;
reg [3:0]dout;
always@(din)
case(din)
8'b00000001:dout=3'b000;
8'b00000010:dout=3'b001;
8'b00000100:dout=3'b010;
8'b00001000:dout=3'b011;
8'b00010000:dout=3'b100;
8'b00100000:dout=3'b101;
8'b01000000:dout=3'b110;
8'b10000000:dout=3'b111; //任何時刻只有一個輸入端為高電平(有效信號)
default:dout=3'b000;
endcase
endmodule
2.4.3 8線—3線優(yōu)先編碼器
普通編碼器的缺點是:在任何時刻只能有一個輸入有效,若不是的話輸出會出錯,有一定局限性。
為了克服這種缺點,采用優(yōu)先編碼器,其是當(dāng)多個輸入端同時有信號時,電路只對其中優(yōu)先級別最高的輸入信號進行編碼。
module code8_3_p(din,dout,en,ys,yex);
input [7:0]din;
input en;
output ys,yex;
output [2:0]dout;
reg [2:0]dout;
reg ys,yex;
always@(din or en)
if(en) {dout,ys,yex}={3'b111,1'b1,1'b1};
else begin
casex(din)
8'b0???????:{dout,ys,yex}={3'b000,1'b1,1'b0};
8'b10??????:{dout,ys,yex}={3'b001,1'b1,1'b0};
8'b110?????:{dout,ys,yex}={3'b010,1'b1,1'b0};
8'b1110????:{dout,ys,yex}={3'b011,1'b1,1'b0};
8'b11110???:{dout,ys,yex}={3'b100,1'b1,1'b0};
8'b111110??:{dout,ys,yex}={3'b101,1'b1,1'b0};
8'b1111110?:{dout,ys,yex}={3'b110,1'b1,1'b0};
8'b11111110:{dout,ys,yex}={3'b111,1'b1,1'b0};
8'b11111111:{dout,ys,yex}={3'b111,1'b0,1'b1};
endcase
end
endmodule
2.4.4 余3編碼
十進制 8421BCD碼 余3碼
0 0000 0011
1 0001 0100
2 0010 0101
3 0011 0110
4 0100 0111
5 0101 1000
6 0110 1001
7 0111 1010
8 1000 1011
9 1001 1100
module code_yu3(d_in,d_out);
input [3:0]d_in;
output [3:0]d_out;
assign d_out=d_in+4'b0011; //842`BCD碼加4'b0011就是當(dāng)前所表達十進制數(shù)所對應(yīng)的余3碼
endmodule
2.5數(shù)字譯碼器(功能是將n個輸入碼進行翻譯,轉(zhuǎn)換為2的n次方個輸出信號)
譯碼器就是對編碼器的反操作,這里就不多贅述了。
舉例3線—8線譯碼器如下:文章來源:http://www.zghlxwxcb.cn/news/detail-442740.html
module decode3_8(en,din,dout);
input [2:0]din;
input en;
output [7:0]dout;
reg [7:0]dout;
always@(en or din)
if(!en) dout=8'b0;
else case(din)
3'b000:dout=8'b00000001;
3'b001:dout=8'b00000010;
3'b010:dout=8'b00000100;
3'b011:dout=8'b00001000;
3'b100:dout=8'b00010000;
3'b101:dout=8'b00100000;
3'b110:dout=8'b01000000;
3'b111:dout=8'b10000000;
endcase
endmodule
2.6數(shù)字校驗器
? ? ? ? 數(shù)字信息在傳輸和存儲過程中,由于噪聲和外界干擾因素會出錯,可用奇偶校驗器檢查數(shù)據(jù)是否出錯,但無法確定錯誤發(fā)生在哪。功能為檢測數(shù)據(jù)中包含1的個數(shù)是奇數(shù)還是偶數(shù)。文章來源地址http://www.zghlxwxcb.cn/news/detail-442740.html
module checker(din,odd,even);
parameter w=8;
input [w-1:0]din;
output odd,even;
assign odd=^din; //偶校驗,歸約運算符的運用
assign even=!odd; //奇校驗,也可寫為 assign even = ~(^din);
endmodule
到了這里,關(guān)于從零學(xué)verilog系列(4)組合邏輯電路設(shè)計方法的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!