-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinary_to_bcd.v
executable file
·101 lines (90 loc) · 2.98 KB
/
binary_to_bcd.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
`timescale 1ns / 1ps
`default_nettype none
//////////////////////////////////////////////////////////////////////////////////
// Company:
// Engineer: M0WUT
//
// Create Date: 09.02.2020 16:56:38
// Design Name:
// Module Name: binary_to_bcd
// Project Name:
// Target Devices:
// Tool Versions:
// Description: Converts a 37 bit binary number to 11 digit BCD
//
// Dependencies:
//
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
//
//////////////////////////////////////////////////////////////////////////////////
module binary_to_bcd #(
parameter INPUT_WIDTH = 37,
parameter DECIMAL_DIGITS = 11
)(
input wire i_clk,
input wire i_resetn,
input wire [INPUT_WIDTH - 1 : 0] i_binary,
output reg [(DECIMAL_DIGITS * 4) - 1 : 0] o_bcd
);
reg [INPUT_WIDTH + 4 * DECIMAL_DIGITS - 1 : 0] r_shifter = 0;
reg[INPUT_WIDTH - 1 : 0] r_oldBinary = 0;
parameter s_IDLE = 0;
parameter s_SHIFTING = 1;
parameter s_CHECKING = 2;
parameter s_DONE = 3;
reg [1:0] r_state = s_IDLE;
reg [INPUT_WIDTH -1 : 0] r_bitsToShift = 0;
reg [$clog2(DECIMAL_DIGITS) : 0] r_digitsToCheck = 0;
always @(posedge i_clk or negedge i_resetn)
begin
if(~i_resetn) begin
r_state <= s_IDLE;
r_oldBinary <= 0;
r_bitsToShift <= 0;
r_digitsToCheck <= 0;
o_bcd = 0;
end else begin
case (r_state)
s_IDLE: begin
if (i_binary != r_oldBinary) begin
// New data to decode
r_shifter <= i_binary;
r_state <= s_SHIFTING;
r_oldBinary <= i_binary;
r_bitsToShift <= INPUT_WIDTH - 1;
end else begin
r_state <= s_IDLE;
end
end // case s_IDLE
s_SHIFTING: begin
r_shifter <= r_shifter << 1;
if(r_bitsToShift == 0) begin
r_state <= s_DONE;
end else begin
r_digitsToCheck <= DECIMAL_DIGITS - 1;
r_bitsToShift <= r_bitsToShift - 1;
r_state <= s_CHECKING;
end
end // case s_SHIFTING
s_CHECKING: begin
if(r_digitsToCheck == 0) begin
r_state <= s_SHIFTING;
end else begin
if(r_shifter[4 * (r_digitsToCheck - 1) + INPUT_WIDTH +: 4] > 4)
r_shifter[4 * (r_digitsToCheck - 1) + INPUT_WIDTH +: 4] <= r_shifter[4 * (r_digitsToCheck - 1) + INPUT_WIDTH +: 4] + 3;
r_digitsToCheck <= r_digitsToCheck - 1;
end
end // case s_CHECKING
s_DONE: begin
o_bcd <= r_shifter[INPUT_WIDTH +: (4 * DECIMAL_DIGITS)];
r_state <= s_IDLE;
end // case s_DONE
default: begin
r_state <= s_IDLE;
end
endcase
end
end
endmodule