81 lines
1.7 KiB
Coq
81 lines
1.7 KiB
Coq
|
module bcd(IN,OUT);
|
||
|
input [3:0] IN;
|
||
|
output [6:0] OUT;
|
||
|
|
||
|
assign OUT=IN[3]? (IN[0]? 7'b0010_000:7'b0000_000):
|
||
|
(IN[2]? (IN[1]? (IN[0]? 7'b1111_000:7'b0000_010):
|
||
|
(IN[0]? 7'b0010_010:7'b0011_001)):
|
||
|
(IN[1]? (IN[0]? 7'b0110_000:7'b0100_100):
|
||
|
(IN[0]? 7'b1111_001:7'b1000_000)));
|
||
|
|
||
|
endmodule
|
||
|
|
||
|
module mux_2to1 (IN1, IN2, S, OUT);
|
||
|
input[3:0]IN1, IN2;
|
||
|
input S;
|
||
|
output [3:0]OUT;
|
||
|
|
||
|
assign OUT=S? IN2:IN1;
|
||
|
|
||
|
endmodule
|
||
|
|
||
|
module BCD_ex(SW, D0, D1);
|
||
|
input [3:0] SW;
|
||
|
output [6:0] D0, D1;
|
||
|
|
||
|
wire z;
|
||
|
wire [4:0] A, V;
|
||
|
wire [4:0] W;
|
||
|
|
||
|
assign V=SW[3:0];
|
||
|
assign z = V[4:0] > 9? 1:0;
|
||
|
assign D1 =z? 7'b1111_001:7'b1000_000;
|
||
|
|
||
|
assign A=V[3:0]-4'b1010;
|
||
|
mux_2to1 M0 (V, A, z, W);
|
||
|
bcd B0 (W, D0);
|
||
|
endmodule
|
||
|
|
||
|
module adder(SUM, COUT, A, B, CIN);
|
||
|
input A, B, CIN;
|
||
|
output SUM, COUT;
|
||
|
|
||
|
wire x;
|
||
|
|
||
|
xor x0(x, A, B);
|
||
|
xor x1(SUM, x, CIN);
|
||
|
|
||
|
assign COUT = x? CIN: B;
|
||
|
|
||
|
endmodule
|
||
|
|
||
|
module RippleCarryAdder(SUM, COUT, IN1, IN2, CIN);
|
||
|
input [4:0] IN1, IN2;
|
||
|
input CIN;
|
||
|
output COUT;
|
||
|
output [3:0] SUM;
|
||
|
|
||
|
wire C0, C1, C2, C3;
|
||
|
|
||
|
adder A0(SUM[0], C0, IN1[0], IN2[0], CIN);
|
||
|
adder A1(SUM[1], C1, IN1[1], IN2[1], C0);
|
||
|
adder A2(SUM[2], C2, IN1[2], IN2[2], C1);
|
||
|
adder A3(SUM[3], C3, IN1[3], IN2[3], C2);
|
||
|
|
||
|
assign COUT = C3;
|
||
|
|
||
|
endmodule
|
||
|
|
||
|
// if the inputs excede decimal value 9, result is unknown
|
||
|
module part4(SW, HEX0, HEX1);
|
||
|
input [8:0] SW;
|
||
|
output [6:0] HEX0, HEX1;
|
||
|
|
||
|
wire COUT;
|
||
|
wire [3:0] U;
|
||
|
|
||
|
// Add the decimal number's 0-9
|
||
|
RippleCarryAdder R0(U, COUT, SW[3:0], SW[7:4], SW[8]);
|
||
|
// send the output along with carry to 7-segment display
|
||
|
BCD_ex({COUT, U}, HEX0, HEX1);
|
||
|
endmodule
|