1 |
6 |
root |
//---------------------------------------------------------------------------
|
2 |
|
|
// Binary to BCD converter, serial implementation, 1 clock per input bit.
|
3 |
|
|
//
|
4 |
|
|
//
|
5 |
|
|
// Description: See description below (which suffices for IP core
|
6 |
|
|
// specification document.)
|
7 |
|
|
//
|
8 |
|
|
// Copyright (C) 2002 John Clayton and OPENCORES.ORG (this Verilog version)
|
9 |
|
|
//
|
10 |
|
|
// This source file may be used and distributed without restriction provided
|
11 |
|
|
// that this copyright statement is not removed from the file and that any
|
12 |
|
|
// derivative work contains the original copyright notice and the associated
|
13 |
|
|
// disclaimer.
|
14 |
|
|
//
|
15 |
|
|
// This source file is free software; you can redistribute it and/or modify
|
16 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
17 |
|
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
18 |
|
|
// (at your option) any later version.
|
19 |
|
|
//
|
20 |
|
|
// This source is distributed in the hope that it will be useful, but WITHOUT
|
21 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
22 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
23 |
|
|
// License for more details.
|
24 |
|
|
//
|
25 |
|
|
// You should have received a copy of the GNU Lesser General Public License
|
26 |
|
|
// along with this source.
|
27 |
|
|
// If not, download it from http://www.opencores.org/lgpl.shtml
|
28 |
|
|
//
|
29 |
|
|
//-----------------------------------------------------------------------------
|
30 |
|
|
//
|
31 |
|
|
// Author: John Clayton
|
32 |
|
|
// Date : Nov. 19, 2003
|
33 |
|
|
// Update: Nov. 19, 2003 Copied this file from "led_display_driver.v" and
|
34 |
|
|
// modified it.
|
35 |
|
|
// Update: Nov. 24, 2003 Fixed bcd_asl function, tested module. It works!
|
36 |
|
|
// Update: Nov. 25, 2003 Changed bit_counter and related logic so that long
|
37 |
|
|
// start pulses produce correct results at the end of
|
38 |
|
|
// the pulse.
|
39 |
|
|
//
|
40 |
|
|
//-----------------------------------------------------------------------------
|
41 |
|
|
// Description:
|
42 |
|
|
//
|
43 |
|
|
// This module takes a binary input, and converts it into BCD output, with each
|
44 |
|
|
// binary coded decimal digit of course occupying 4-bits.
|
45 |
|
|
// The user can specify the number of input bits separately from the number of
|
46 |
|
|
// output digits. Be sure that you have specified enough output digits to
|
47 |
|
|
// represent the largest number you expect on the binary input, or else the
|
48 |
|
|
// most significant digits of the result will be cut off.
|
49 |
|
|
//
|
50 |
|
|
//-----------------------------------------------------------------------------
|
51 |
|
|
|
52 |
|
|
|
53 |
|
|
module binary_to_bcd (
|
54 |
|
|
clk_i,
|
55 |
|
|
ce_i,
|
56 |
|
|
rst_i,
|
57 |
|
|
start_i,
|
58 |
|
|
dat_binary_i,
|
59 |
|
|
dat_bcd_o,
|
60 |
|
|
done_o
|
61 |
|
|
);
|
62 |
|
|
parameter BITS_IN_PP = 16; // # of bits of binary input
|
63 |
|
|
parameter BCD_DIGITS_OUT_PP = 5; // # of digits of BCD output
|
64 |
|
|
parameter BIT_COUNT_WIDTH_PP = 4; // Width of bit counter
|
65 |
|
|
|
66 |
|
|
// I/O declarations
|
67 |
|
|
input clk_i; // clock signal
|
68 |
|
|
input ce_i; // clock enable input
|
69 |
|
|
input rst_i; // synchronous reset
|
70 |
|
|
input start_i; // initiates a conversion
|
71 |
|
|
input [BITS_IN_PP-1:0] dat_binary_i; // input bus
|
72 |
|
|
output [4*BCD_DIGITS_OUT_PP-1:0] dat_bcd_o; // output bus
|
73 |
|
|
output done_o; // indicates conversion is done
|
74 |
|
|
|
75 |
|
|
reg [4*BCD_DIGITS_OUT_PP-1:0] dat_bcd_o;
|
76 |
|
|
|
77 |
|
|
// Internal signal declarations
|
78 |
|
|
|
79 |
|
|
reg [BITS_IN_PP-1:0] bin_reg;
|
80 |
|
|
reg [4*BCD_DIGITS_OUT_PP-1:0] bcd_reg;
|
81 |
|
|
wire [BITS_IN_PP-1:0] bin_next;
|
82 |
|
|
reg [4*BCD_DIGITS_OUT_PP-1:0] bcd_next;
|
83 |
|
|
reg busy_bit;
|
84 |
|
|
reg [BIT_COUNT_WIDTH_PP-1:0] bit_count;
|
85 |
|
|
wire bit_count_done;
|
86 |
|
|
|
87 |
|
|
//--------------------------------------------------------------------------
|
88 |
|
|
// Functions & Tasks
|
89 |
|
|
//--------------------------------------------------------------------------
|
90 |
|
|
|
91 |
|
|
function [4*BCD_DIGITS_OUT_PP-1:0] bcd_asl;
|
92 |
|
|
input [4*BCD_DIGITS_OUT_PP-1:0] din;
|
93 |
|
|
input newbit;
|
94 |
|
|
integer k;
|
95 |
|
|
reg cin;
|
96 |
|
|
reg [3:0] digit;
|
97 |
|
|
reg [3:0] digit_less;
|
98 |
|
|
begin
|
99 |
|
|
cin = newbit;
|
100 |
|
|
for (k=0; k<BCD_DIGITS_OUT_PP; k=k+1)
|
101 |
|
|
begin
|
102 |
|
|
digit[3] = din[4*k+3];
|
103 |
|
|
digit[2] = din[4*k+2];
|
104 |
|
|
digit[1] = din[4*k+1];
|
105 |
|
|
digit[0] = din[4*k];
|
106 |
|
|
digit_less = digit - 5;
|
107 |
|
|
if (digit > 4'b0100)
|
108 |
|
|
begin
|
109 |
|
|
bcd_asl[4*k+3] = digit_less[2];
|
110 |
|
|
bcd_asl[4*k+2] = digit_less[1];
|
111 |
|
|
bcd_asl[4*k+1] = digit_less[0];
|
112 |
|
|
bcd_asl[4*k+0] = cin;
|
113 |
|
|
cin = 1'b1;
|
114 |
|
|
end
|
115 |
|
|
else
|
116 |
|
|
begin
|
117 |
|
|
bcd_asl[4*k+3] = digit[2];
|
118 |
|
|
bcd_asl[4*k+2] = digit[1];
|
119 |
|
|
bcd_asl[4*k+1] = digit[0];
|
120 |
|
|
bcd_asl[4*k+0] = cin;
|
121 |
|
|
cin = 1'b0;
|
122 |
|
|
end
|
123 |
|
|
|
124 |
|
|
end // end of for loop
|
125 |
|
|
end
|
126 |
|
|
endfunction
|
127 |
|
|
|
128 |
|
|
//--------------------------------------------------------------------------
|
129 |
|
|
// Module code
|
130 |
|
|
//--------------------------------------------------------------------------
|
131 |
|
|
|
132 |
|
|
// Perform proper shifting, binary ASL and BCD ASL
|
133 |
|
|
assign bin_next = {bin_reg,1'b0};
|
134 |
|
|
always @(bcd_reg or bin_reg)
|
135 |
|
|
begin
|
136 |
|
|
bcd_next <= bcd_asl(bcd_reg,bin_reg[BITS_IN_PP-1]);
|
137 |
|
|
end
|
138 |
|
|
|
139 |
|
|
// Busy bit, input and output registers
|
140 |
|
|
always @(posedge clk_i)
|
141 |
|
|
begin
|
142 |
|
|
if (rst_i)
|
143 |
|
|
begin
|
144 |
|
|
busy_bit <= 0; // Synchronous reset
|
145 |
|
|
dat_bcd_o <= 0;
|
146 |
|
|
end
|
147 |
|
|
else if (start_i && ~busy_bit)
|
148 |
|
|
begin
|
149 |
|
|
busy_bit <= 1;
|
150 |
|
|
bin_reg <= dat_binary_i;
|
151 |
|
|
bcd_reg <= 0;
|
152 |
|
|
end
|
153 |
|
|
else if (busy_bit && ce_i && bit_count_done && ~start_i)
|
154 |
|
|
begin
|
155 |
|
|
busy_bit <= 0;
|
156 |
|
|
dat_bcd_o <= bcd_next;
|
157 |
|
|
end
|
158 |
|
|
else if (busy_bit && ce_i && ~bit_count_done)
|
159 |
|
|
begin
|
160 |
|
|
bcd_reg <= bcd_next;
|
161 |
|
|
bin_reg <= bin_next;
|
162 |
|
|
end
|
163 |
|
|
end
|
164 |
|
|
assign done_o = ~busy_bit;
|
165 |
|
|
|
166 |
|
|
// Bit counter
|
167 |
|
|
always @(posedge clk_i)
|
168 |
|
|
begin
|
169 |
|
|
if (~busy_bit) bit_count <= 0;
|
170 |
|
|
else if (ce_i && ~bit_count_done) bit_count <= bit_count + 1;
|
171 |
|
|
end
|
172 |
|
|
assign bit_count_done = (bit_count == (BITS_IN_PP-1));
|
173 |
|
|
|
174 |
|
|
endmodule
|
175 |
|
|
|