P6-76 4bit BCD Adder 適用於DE2-70
BCD加法器
BCD加法運算原則:
BCD加法運算原則:
- 將二個BCD碼先以4位元二進位數加法運算。
- 運算後四位元總和小於或等於9(1001B),且無進位產生,則此”和”為有效BCD值。
- 運算後四位元總和大於9(1001B)或有進位產生,則必須將”和”再加上6(011B),才為有效BCD值。
- 將上述步驟產生三進位加至下一位數。
依上述原則可得一真值表
| |
輸出Y=0表示有效BCD碼,不加6。 輸出Y=1表示需要加6調整。Y=B3B2+B3B1+C4 =B3(B2+B1)+C4 |
若要以二進加法器做BCD碼的加法,其和大於9的數必須加6來修正,至於電路方面就必須有個大於9的檢查電路,能檢知當加法器的和大於9時自動加6,如例6.4-2中的情形。另一種情形如例6.4-3,BCD碼相加的和(0001)並未大於9,但是產生進位後實際的二進值為10001(17),也是大於9,所以圖6.4-1中須要加6的修正的包括主要四位元二進加法器的和S3 S2 S1 S0大於9以及進位CO等於1之時,至於大於9的電路設計可以參考前章中的例5.3-3。
//4-bit BCD adder
//Filename : BCDadder4.v
//------------------------------------
module _4bit_BCD_Adder(SW, LEDR, LEDG , CLOCK_27 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 );
input [17:0] SW; // toggle switches
input [7:0] KEY; // Push bottom
input CLOCK_27; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [8:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3; //7-segment display
//set original program input , output
//(S, Cout, Cin, A, B);
//output [3:0] S; //Sumation output
//output Cout; //Carry out
//input Cin; //Carry in
//input [3:0] A, B;//Input data
wire Cin; //Carry in
wire [3:0] A, B;//Input data
wire [3:0] S; //Sumation output
wire Cout; //Carry out
reg [3:0] gt9=4'b101;
wire [7:0] segout0; //HEX 0
wire [7:0] segout1; //HEX 1
assign A=SW[3:0] ;
assign B=SW[7:4] ;
assign Cin=SW[17];
reg [3:0] A_tmp,B_tmp;
wire [3:0] S_tmp;
wire C4;
reg [3:0] B_mod;
reg F;
always @ ( A or B )
begin
if (A<10)
A_tmp = A; //若是>9 則為9
else
A_tmp =4'b1001;
if (B<10)
B_tmp = B; //若是>9 則為9
else
B_tmp =4'b1001;
end
//4-bit binary adder
adder4 BINADD(
.S(S_tmp),
.Cout(C4),
.Cin(Cin),
.A(A_tmp),
.B(B_tmp)
);
//Modify binary code with '0110'
adder4 MODADD(
.S(S),
.Cout(),
.Cin(1'b0),
.A(S_tmp),
.B(B_mod)
);
always @ (Cin or A or B or C4 or S_tmp)
begin
//F=C4+S3(S2+S1)
F = (C4 | (S_tmp[3] & (S_tmp[2] | S_tmp[1])));
B_mod = {1'b0, F, F, 1'b0}; //Modified code
end
assign Cout = F;
_7seg UUT0(.hex((S)),
.seg(segout0));
_7seg UUT1(.hex({3'b0,Cout}),
.seg(segout1));
assign HEX0=segout0[6:0];
assign HEX1=segout1[6:0];
endmodule
//----------------------
//4-bit unsigned adder
//Filename : adder4.v
//----------------------
module adder4(S, Cout, A, B, Cin);
output [3:0] S; //4-bit sum
output Cout; //Carry out
input [3:0] A, B; //Inputs
input Cin; //Carry in
//Assign the sum of (A+B+Cin) to Cout and Sum
assign {Cout, S} = A + B + Cin;
endmodule
//-----------------------------------------
//Common-cathod seven segment display
//using case.....endcase statement
//Filename : sevenseg_case.v
//-----------------------------------------
module _7seg(hex , seg);
input [3:0] hex;
output [7:0] seg;
reg [7:0] seg;
// segment encoding
// 0
// ---
// 5 | | 1
// --- <- 6
// 4 | | 2
// ---
// 3
always @(hex)
begin
case (hex)
// Dot point is always disable
4'b0001 : seg = 8'b11111001; //1 = F9H
4'b0010 : seg = 8'b10100100; //2 = A4H
4'b0011 : seg = 8'b10110000; //3 = B0H
4'b0100 : seg = 8'b10011001; //4 = 99H
4'b0101 : seg = 8'b10010010; //5 = 92H
4'b0110 : seg = 8'b10000010; //6 = 82H
4'b0111 : seg = 8'b11111000; //7 = F8H
4'b1000 : seg = 8'b10000000; //8 = 80H
4'b1001 : seg = 8'b10010000; //9 = 90H
4'b1010 : seg = 8'b10001000; //A = 88H
4'b1011 : seg = 8'b10000011; //b = 83H
4'b1100 : seg = 8'b11000110; //C = C6H
4'b1101 : seg = 8'b10100001; //d = A1H
4'b1110 : seg = 8'b10000110; //E = 86H
4'b1111 : seg = 8'b10001110; //F = 8EH
default : seg = 8'b11000000; //0 = C0H
endcase
end
endmodule
沒有留言:
張貼留言