OpenCores
URL https://opencores.org/ocsvn/binary_to_bcd/binary_to_bcd/trunk

Subversion Repositories binary_to_bcd

[/] [binary_to_bcd/] [web_uploads/] [bcd_to_binary.v] - Blame information for rev 6

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 6 root
//---------------------------------------------------------------------------
2
// BCD to binary 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. 24, 2003
33
// Update: Nov. 24, 2003  Copied this file from "binary_to_bcd.v" and
34
//                        modified it.
35
// Update: Nov. 25, 2003  Tested this 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 BCD input, and converts it into binary output, with each
44
// binary coded decimal input digit (of course) occupying 4-bits.
45
// The user can specify the number of input digits separately from the number
46
// of output bits.  Be sure that you have specified enough output bits to
47
// represent the largest input number you expect to convert, or else the
48
// most significant portion of the BCD input will never be processed.
49
// This module processes the BCD digits starting with the least significant
50
// and working its way through to the more significant digits, until the binary
51
// output register is "fully stuffed" with output bits.
52
// This means that a particular BCD input digit might only be processed half
53
// way!
54
// Also, there is no checking for invalid BCD digit contents...  Behavior is
55
// undefined for "BCD" input values outside the range [0..9], although it would
56
// be very easy to add error checking with an error indicator output bit.
57
//
58
//-----------------------------------------------------------------------------
59
 
60
 
61
module bcd_to_binary (
62
  clk_i,
63
  ce_i,
64
  rst_i,
65
  start_i,
66
  dat_bcd_i,
67
  dat_binary_o,
68
  done_o
69
  );
70
parameter BCD_DIGITS_IN_PP   = 5;  // # of digits of BCD input
71
parameter BITS_OUT_PP        = 16; // # of bits of binary output
72
parameter BIT_COUNT_WIDTH_PP = 4;  // Width of bit counter
73
 
74
// I/O declarations
75
input  clk_i;                      // clock signal
76
input  ce_i;                       // clock enable input
77
input  rst_i;                      // synchronous reset
78
input  start_i;                    // initiates a conversion
79
input  [4*BCD_DIGITS_IN_PP-1:0] dat_bcd_i;  // input bus
80
output [BITS_OUT_PP-1:0] dat_binary_o;      // output bus
81
output done_o;                     // indicates conversion is done
82
 
83
reg [BITS_OUT_PP-1:0] dat_binary_o;
84
 
85
// Internal signal declarations
86
 
87
reg  [BITS_OUT_PP-1:0] bin_reg;
88
reg  [4*BCD_DIGITS_IN_PP-1:0] bcd_reg;
89
wire [BITS_OUT_PP-1:0] bin_next;
90
reg  [4*BCD_DIGITS_IN_PP-1:0] bcd_next;
91
reg  busy_bit;
92
reg  [BIT_COUNT_WIDTH_PP-1:0] bit_count;
93
wire bit_count_done;
94
 
95
//--------------------------------------------------------------------------
96
// Functions & Tasks
97
//--------------------------------------------------------------------------
98
 
99
function [4*BCD_DIGITS_IN_PP-1:0] bcd_asr;
100
  input [4*BCD_DIGITS_IN_PP-1:0] din;
101
  integer k;
102
  reg cin;
103
  reg [3:0] digit;
104
  reg [3:0] digit_more;
105
 
106
  begin
107
    cin = 1'b0;
108
    for (k=BCD_DIGITS_IN_PP-1; k>=0; k=k-1) // From MS digit to LS digit
109
    begin
110
      digit[3] = 1'b0;
111
      digit[2] = din[4*k+3];
112
      digit[1] = din[4*k+2];
113
      digit[0] = din[4*k+1];
114
      digit_more = digit + 5;
115
      if (cin)
116
      begin
117
        bcd_asr[4*k+3] = digit_more[3];
118
        bcd_asr[4*k+2] = digit_more[2];
119
        bcd_asr[4*k+1] = digit_more[1];
120
        bcd_asr[4*k+0] = digit_more[0];
121
      end
122
      else
123
      begin
124
        bcd_asr[4*k+3] = digit[3];
125
        bcd_asr[4*k+2] = digit[2];
126
        bcd_asr[4*k+1] = digit[1];
127
        bcd_asr[4*k+0] = digit[0];
128
      end
129
      cin = din[4*k+0];
130
    end  // end of for loop
131
  end
132
 
133
endfunction
134
 
135
//--------------------------------------------------------------------------
136
// Module code
137
//--------------------------------------------------------------------------
138
 
139
// Perform proper shifting, binary ASL and BCD ASL
140
assign bin_next = {bcd_reg[0],bin_reg[BITS_OUT_PP-1:1]};
141
always @(bcd_reg)
142
begin
143
  bcd_next <= bcd_asr(bcd_reg);
144
//  bcd_next <= bcd_reg >> 1;  Just for testing...
145
end
146
 
147
// Busy bit, input and output registers
148
always @(posedge clk_i)
149
begin
150
  if (rst_i)
151
  begin
152
    busy_bit <= 0;  // Synchronous reset
153
    dat_binary_o <= 0;
154
  end
155
  else if (start_i && ~busy_bit)
156
  begin
157
    busy_bit <= 1;
158
    bcd_reg <= dat_bcd_i;
159
    bin_reg <= 0;
160
  end
161
  else if (busy_bit && ce_i && bit_count_done && ~start_i)
162
  begin
163
    busy_bit <= 0;
164
    dat_binary_o <= bin_next;
165
  end
166
  else if (busy_bit && ce_i & ~bit_count_done)
167
  begin
168
    bin_reg <= bin_next;
169
    bcd_reg <= bcd_next;
170
  end
171
end
172
assign done_o = ~busy_bit;
173
 
174
// Bit counter
175
always @(posedge clk_i)
176
begin
177
  if (~busy_bit) bit_count <= 0;
178
  else if (ce_i && ~bit_count_done) bit_count <= bit_count + 1;
179
end
180
assign bit_count_done = (bit_count == (BITS_OUT_PP-1));
181
 
182
endmodule
183
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.