module BCD_adder(SW,HEX0,HEX1,LEDR);
input [17:0] SW;
output [17:0] LEDR;
output [6:0] HEX0,HEX1;
wire [3:0] swa2,swb2,sum1,sum2;
wire cout1,CY,CY2;
BCD_4bit(SW[3:0],SW[7:4],swa2,swb2);
//BCD Adder********
_4bit_adder A1(a2,b2,SW[17],cout1,sum1);
assign CY=(sum1[3]&sum1[2]) | (sum1[3]&s[1]) | cout1;
_4bit_adder A2(sum1,{1'b0,T,T,1'b0},1'b0,CY2,sum2);
//BCD Adder*********
hex_7seg s1(sum2,HEX0);
hex_7seg s2({1'b0,1'b0,1'b0,CY},HEX1);
endmodule
module BCD_4bit(a,b,a1,b1);
input [3:0] a,b;
output [3:0] a1,b1;
reg [3:0] a1,b1;
always @(a or b)
begin
if(a<9)
a1=a;
else
a1=4'b1001;
if(b<9)
b1=b;
else
b1=4'b1001;
end
endmodule
module _4bit_adder(a_in,b_in,cin,cout,sum);
input [3:0] a_in,b_in;
input cin;
output [3:0] sum;
output cout;
assign {cout,sum}=a_in+b_in+cin;
endmodule
module hex_7seg(hex,segout);
input [3:0] hex;
output [6:0] segout;
reg [6:0] segout;
always@(hex)
begin
case(hex)
4'b0000 : segout = 7'b 100_0000;
4'b0001 : segout = 7'b 111_1001;
4'b0010 : segout = 7'b 010_0100;
4'b0011 : segout = 7'b 011_0000;
4'b0100 : segout = 7'b 001_1001;
4'b0101 : segout = 7'b 001_0010;
4'b0110 : segout = 7'b 000_0010;
4'b0111 : segout = 7'b 111_1000;
4'b1000 : segout = 7'b 000_0000;
4'b1001 : segout = 7'b 001_0000;
default : segout = 7'b 111_1111;
endcase
end
endmodule
沒有留言:
張貼留言