Binary to BCD conversion.
This application note is intended to aid those designers
that require a technique to convert a binary number of variable length
(that is, 4, 8, 16 or 24 bits) into an equivalent representation as BCD
digits. Please note that the theory utilized for the implementation of
this VHDL algorithm was obtained from one of the application notes found
in the Xilinx home page in the application notes section. The application
note number is XAPP092 and is title "Serial Code Conversion Between
BCD and Binary".
You can see it here: http://www.xilinx.com/xapp/xapp029.pdf
The procedure is a lot easier to understand if it is first
tried with small number such as a 4-bit or an 8-bit binary number. Another
word of advice; the designer must make sure that the binary number that
will undergo the conversion be a positive or an unsigned number; that is,
its most significant bit must be a '0' (zero). I have yet to verify that
the algorithm works for negative numbers, but it surely works for positive
numbers.
Step#1 Parallel to Serial Conversion
As described in the Xilinx application note, the input data must be in serial format and it must be fed into the Binary-to-BCD converter starting by the most significant bit. Therefore, if the original binary data is available in parallel form, the first step in the process consists in serializing the data. I found out that the easiest and cheapest (in terms of the number of Logic Cells used) way of doing a parallel-to-serial conversion is by using a Finite State Machine. The number of states of the FSM is proportional to the number of bits in the binary number. Each bit, starting by the most significant bit, will be shifted out on one of the edges of the clock signal. The choice of rising or falling edge is left to the designers discretion (I chose to use the rising edge).
The output port of this parallel-to-serial converter must be interfaced directly to the input port of the binary-to-BCD converter. The customized parallel-to-serial entity must also provide a flag to indicate to the binary-to-BCD entity when the last bit of the data has been sent.
Remember that in VHDL all processes happen in parallel, even though they appear to be coded as sequentially. This is an useful feature to keep in mind because both conversions (parallel-to-serial and binary-to-BCD) must take place at the same time.
Step#2 Binary to BCD Conversion
In the Xilinx application note one can read that the binary-to-BCD conversion takes place in a modified shift register that doubles its contents as each data bit is receive, thereby, leaving a "empty" location where the next bit of the data will be placed. I chose to implement this so-called modified shift register with another Finite State Machine. This FSM should have a fixed number of states independent of the number of bits in the binary data. My design has only three states and these include the IDLE and the FINISHED states.
As you all know, a BCD digit is made up of four bits and it can only represent the numbers '0' to '9' ("0000" to "1001" in binary). Therefore, depending on the number of BCD digits that are required, the serial data appended to a STD_LOGIC_VECTOR whose length is directly proportional to the number of BCD digits needed. For example, a 16-bit binary number will be represented in 5 BCD digits; consequently, the length of the STD_LOGIC_VECTOR is 20 bits. In general the length of the vector is equal to the number of BCD digits required multiplied by 4 bit per digit.
I already mentioned that the objective of the FSM is to shift the contents of the "modified register" as each incoming data bit is received. However, in parallel, the designer must have another process for each BCD digit in which he/she checks if the contents of each digit is equal to or greater that 5 before the contents of the "modified register" are doubled (shifted right by one position). This will ensure that each digit remains a true BCD number. These processes do the following manipulation on each set of four bits:
If the number is equal to '5', it will be converted to a '0' and a 1 will be shifted to the next higher digit. A '6' is converted into a '2', a '7' into a '4', an '8' into a '6' and a '9' into an '8'. For each case, a '1' must be shifted into the next higher digit.
COPYRIGHT NOTE
I am enclosing a copy of my VHDL code so that anyone
that wishes to use is free to do so, and to modify it for their particular
application. The only conditions imposed is that credit be given to the
designer of this Binary to BCD converter implementation, whenever this
code is used. ( Don't take this comment too seriously though ).
Author : Marco
Castellon
Course: EE552
April 09, 1998
SAMPLE CODE