137 lines
3.6 KiB
Verilog
137 lines
3.6 KiB
Verilog
module DoubleDabbleCounter_Top(
|
|
input i_Clk,
|
|
|
|
output o_Segment1_A,
|
|
output o_Segment1_B,
|
|
output o_Segment1_C,
|
|
output o_Segment1_D,
|
|
output o_Segment1_E,
|
|
output o_Segment1_F,
|
|
output o_Segment1_G,
|
|
output o_Segment2_A,
|
|
output o_Segment2_B,
|
|
output o_Segment2_C,
|
|
output o_Segment2_D,
|
|
output o_Segment2_E,
|
|
output o_Segment2_F,
|
|
output o_Segment2_G
|
|
);
|
|
reg [6:0] r_currentNumber = 0;
|
|
reg [$clog2(10000000)-1:0] r_counter;
|
|
|
|
always @(posedge i_Clk) begin
|
|
if (r_counter == 10000000) begin
|
|
r_counter <= 0;
|
|
if (r_currentNumber == 99) begin
|
|
r_currentNumber <= 0;
|
|
end else begin
|
|
r_currentNumber <= r_currentNumber + 1;
|
|
end
|
|
end else begin
|
|
r_counter <= r_counter + 1;
|
|
end
|
|
end
|
|
|
|
wire [8:0] w_binaryCodedNumber;
|
|
|
|
wire [3:0] w_decimal1, w_decimal2;
|
|
wire [6:0] w_segment1, w_segment2;
|
|
|
|
IntegerToBCD integerToBCD (
|
|
.i_BinaryNumber(r_currentNumber),
|
|
.o_BinaryCodedDecimal(w_binaryCodedNumber)
|
|
);
|
|
|
|
assign w_decimal1 = w_binaryCodedNumber[7:4];
|
|
assign w_decimal2 = w_binaryCodedNumber[3:0];
|
|
|
|
BCDTo7Segment segment1 (
|
|
.i_Clk(i_Clk),
|
|
.i_BinaryCodedDecimal(w_decimal1),
|
|
.o_SegmentLights(w_segment1)
|
|
);
|
|
|
|
BCDTo7Segment segment2 (
|
|
.i_Clk(i_Clk),
|
|
.i_BinaryCodedDecimal(w_decimal2),
|
|
.o_SegmentLights(w_segment2)
|
|
);
|
|
|
|
assign o_Segment1_A = !w_segment1[6];
|
|
assign o_Segment1_B = !w_segment1[5];
|
|
assign o_Segment1_C = !w_segment1[4];
|
|
assign o_Segment1_D = !w_segment1[3];
|
|
assign o_Segment1_E = !w_segment1[2];
|
|
assign o_Segment1_F = !w_segment1[1];
|
|
assign o_Segment1_G = !w_segment1[0];
|
|
|
|
assign o_Segment2_A = !w_segment2[6];
|
|
assign o_Segment2_B = !w_segment2[5];
|
|
assign o_Segment2_C = !w_segment2[4];
|
|
assign o_Segment2_D = !w_segment2[3];
|
|
assign o_Segment2_E = !w_segment2[2];
|
|
assign o_Segment2_F = !w_segment2[1];
|
|
assign o_Segment2_G = !w_segment2[0];
|
|
|
|
// register that goes to 99
|
|
// debounce two input buttons
|
|
// button 1 increases
|
|
// button 2 decreases
|
|
|
|
endmodule
|
|
|
|
module BCDTo7Segment(
|
|
input i_Clk,
|
|
input [3:0] i_BinaryCodedDecimal,
|
|
output reg [6:0] o_SegmentLights
|
|
);
|
|
always @(posedge i_Clk) begin
|
|
case (i_BinaryCodedDecimal)
|
|
0: o_SegmentLights <= 7'b1111110;
|
|
1: o_SegmentLights <= 7'b0110000;
|
|
2: o_SegmentLights <= 7'b1101101;
|
|
3: o_SegmentLights <= 7'b1111001;
|
|
4: o_SegmentLights <= 7'b0110011;
|
|
5: o_SegmentLights <= 7'b1011011;
|
|
6: o_SegmentLights <= 7'b1011111;
|
|
7: o_SegmentLights <= 7'b1110000;
|
|
8: o_SegmentLights <= 7'b1111111;
|
|
9: o_SegmentLights <= 7'b1111011;
|
|
default: o_SegmentLights <= 7'b0000000;
|
|
endcase
|
|
end
|
|
endmodule
|
|
|
|
/**
|
|
* From https://github.com/AmeerAbdelhadi/Binary-to-BCD-Converter
|
|
* This halts until the conversion is done!
|
|
*/
|
|
module IntegerToBCD(
|
|
input [MAX_BIT_WIDTH-1:0] i_BinaryNumber,
|
|
output reg [SCRATCH_AREA_WIDTH:0] o_BinaryCodedDecimal
|
|
);
|
|
// this is all the go board will need for the 7 segment display to go to 99
|
|
parameter MAX_BIT_WIDTH = 7;
|
|
|
|
localparam SCRATCH_AREA_WIDTH = MAX_BIT_WIDTH+(MAX_BIT_WIDTH-4)/3;
|
|
|
|
integer i,j;
|
|
|
|
always @(i_BinaryNumber) begin
|
|
for (i = 0; i <= SCRATCH_AREA_WIDTH; i = i + 1) begin
|
|
o_BinaryCodedDecimal[i] = 0;
|
|
end
|
|
|
|
o_BinaryCodedDecimal[MAX_BIT_WIDTH-1:0] = i_BinaryNumber;
|
|
|
|
// this does the shift-and-add in small pieces
|
|
for (i = 0; i <= MAX_BIT_WIDTH - 4; i = i + 1) begin
|
|
for (j = 0; j <= i / 3; j = j + 1) begin
|
|
if (o_BinaryCodedDecimal[MAX_BIT_WIDTH-i+4*j -: 4] > 4) begin
|
|
o_BinaryCodedDecimal[MAX_BIT_WIDTH-i+4*j -: 4] = o_BinaryCodedDecimal[MAX_BIT_WIDTH-i+4*j -: 4] + 4'd3;
|
|
end
|
|
end
|
|
end
|
|
end
|
|
endmodule
|