How to convert a binary number to its equivalent BCD? In this lecture, 1
I will explain a straightforward algorithm to design a digital circuit performing this conversion. 2
So designing a combinational circuit to encode a binary number into the BCD is our goal here.. 3
A simple C function can do this encoding process by extracting each digit in a while-loop, as shown in this 4
code. 5
Line 4 in this code finds a least significant digit in the binary number using the modulo 10 6
operation, and line 5 modifies the binary number and prepare that for the next iteration. 7
Let’s take an example and follow the code. 8
If we assume, the binary number is 0b11101011 = 235 9
in decimal then the first iteration of this code extracts the first digit into bcd[0] and replaces 10
the binary number with 23. 11
The second and third iterations find the 3 and 2 digits, respectively. 12
Let’s modify the binary2bcd C function to be synthesised by Vivado-HLS for the Basys3 board. 13
First of all, the while-loop instruction implicitly implies a kind of sequentially ordered execution 14
which means its implementation requires temporary memories which is against the combinational circuit 15
definition. 16
Although, in the next section, I will explain the combinational-loops and the techniques that we use to 17
implement a software loop with combinational circuits. 18
But, until that time, we should get rid of the loop statement. 19
As we are considering the Basys3 board and it has only four 7segments, it is practical to 20
consider only 4-digit BCD numbers in our binary to BCD algorithm. 21
In this case, the input number would be between 0 and 9999. 22
So, we need at most 16 bits to save the final BCD code and 14 bits for the equivalent binary code. 23
Before modifying the C function, let’s define a few arbitrary precision unsigned integer data types. 24
We need a 14-bit data type to get the input binary number, 16-bit data type to send out the BCD code 25
for four 7segments and 4-bit data type to represent a single BCD digit inside the code. 26
The body of the while-loop in the previous C function has two main mathematical calculations, 27
let’s put them in a sub-function and call the sub-function four times corresponding to the four-digit BCD 28
output. 29
Notice that the Vivado-HLS implements the modulus operator with Xilinx LogiCORE divider IP which 30
generally is not a combinational circuit. 31
Therefore, we replace that with the equivalent expression, which is commented out in our original C 32
function. 33
So, the get_digit function, as shown here, will be synthesised into a combinational circuit by 34
Vivado-HLS toolset. 35
Now, let’s write the HLS top- function. The top-function gets a 14-bit binary number via the in_binary variable 36
and returns 4-digit packed BCD through a 16-bit pointer variable named packed_bcd. 37
Inside the function body, let’s define a 14-bit variable to save the input binary value throughout 38
the algorithm. 39
Also, we need to define four 4-bit variables to keep the 4 BCD digits. 40
Now, we call the get_digit sub-function four times to extract the four BCD digits. 41
Then, the four extracted BCD should be concatenated into the output variable. 42
Finally, we should define the function argument interfaces and the function block interface. 43
The HLS C code explained here uses some arithmetic operations such as division and addition. 44
However, a more efficient algorithm that only uses shift and add operators can be used to convert a 45
binary number into the BCD code. 46
This algorithm is called Dobble-Dabble, 47
that is explained in the next lecture. 48
These are our takeaway messages. HLS synthesises the integer and arbitrary-precision data type 49
arithmetic operators into a combinational circuit except for the modulus operator. 50
HLS implements the modulus operator with the Xilinx LogiCORE divider IP which is not generally 51
a combinational circuit. 52
Now the quiz for this lecture. Synthesis the C function “binary2bcd_div” explained earlier in 53
this lecture using HLS and report the resource utilisation.