-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBinary_To_BCD.v
executable file
·169 lines (133 loc) · 4.93 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: Digilent Inc.
// Engineer: Josh Sackos
//
// Create Date: 07/11/2012
// Module Name: Binary_To_BCD
// Project Name: PmodJSTK_Demo
// Target Devices: Nexys3
// Tool versions: ISE 14.1
// Description: This module receives a 10 bit binary value and converts it to
// a packed binary coded decimal (BCD) using the shift add 3
// algorithm.
//
// The output consists of 4 BCD digits as follows:
//
// BCDOUT[15:12] - Thousands place
// BCDOUT[11:8] - Hundreds place
// BCDOUT[7:4] - Tens place
// BCDOUT[3:0] - Ones place
//
// Revision History:
// Revision 0.01 - File Created (Josh Sackos)
//////////////////////////////////////////////////////////////////////////////////
// ==============================================================================
// Define Module
// ==============================================================================
module Binary_To_BCD(
CLK,
RST,
START,
BIN,
BCDOUT
);
// ===========================================================================
// Port Declarations
// ===========================================================================
input CLK; // 100Mhz CLK
input RST; // Reset
input START; // Signal to initialize conversion
input [9:0] BIN; // Binary value to be converted
output [15:0] BCDOUT; // 4 digit binary coded decimal output
// ===========================================================================
// Parameters, Regsiters, and Wires
// ===========================================================================
reg [15:0] BCDOUT = 16'h0000; // Output BCD values, contains 4 digits
reg [4:0] shiftCount = 5'b00000; // Stores number of shifts executed
reg [27:0] tmpSR; // Temporary shift regsiter
reg [2:0] STATE = Idle; // Present state
// FSM States
parameter [2:0] Idle = 3'b000,
Init = 3'b001,
Shift = 3'b011,
Check = 3'b010,
Done = 3'b110;
// ===========================================================================
// Implementation
// ===========================================================================
//------------------------------
// Binary to BCD FSM
//------------------------------
always @(posedge CLK) begin
if(RST == 1'b1) begin
// Reset/clear values
BCDOUT <= 16'h0000;
tmpSR <= 28'h0000000;
STATE <= Idle;
end
else begin
case (STATE)
// Idle State
Idle : begin
BCDOUT <= BCDOUT; // Output does not change
tmpSR <= 28'h0000000; // Temp shift reg empty
if(START == 1'b1) begin
STATE <= Init;
end
else begin
STATE <= Idle;
end
end
// Init State
Init : begin
BCDOUT <= BCDOUT; // Output does not change
tmpSR <= {18'b000000000000000000, BIN}; // Copy input to lower 10 bits
STATE <= Shift;
end
// Shift State
Shift : begin
BCDOUT <= BCDOUT; // Output does not change
tmpSR <= {tmpSR[26:0], 1'b0}; // Shift left 1 bit
shiftCount <= shiftCount + 1'b1; // Count the shift
STATE <= Check; // Check digits
end
// Check State
Check : begin
BCDOUT <= BCDOUT; // Output does not change
// Not done converting
if(shiftCount != 5'd12) begin
// Add 3 to thousands place
if(tmpSR[27:24] >= 3'd5) begin
tmpSR[27:24] <= tmpSR[27:24] + 2'd3;
end
// Add 3 to hundreds place
if(tmpSR[23:20] >= 3'd5) begin
tmpSR[23:20] <= tmpSR[23:20] + 2'd3;
end
// Add 3 to tens place
if(tmpSR[19:16] >= 3'd5) begin
tmpSR[19:16] <= tmpSR[19:16] + 2'd3;
end
// Add 3 to ones place
if(tmpSR[15:12] >= 3'd5) begin
tmpSR[15:12] <= tmpSR[15:12] + 2'd3;
end
STATE <= Shift; // Shift again
end
// Done converting
else begin
STATE <= Done;
end
end
// Done State
Done : begin
BCDOUT <= tmpSR[27:12]; // Assign output the new BCD values
tmpSR <= 28'h0000000; // Clear temp shift register
shiftCount <= 5'b00000; // Clear shift count
STATE <= Idle;
end
endcase
end
end
endmodule