In this lecture, I am going to explain how to implement a binary to BCD converter algorithm called 1
Double Dabble. 2
This algorithm uses two simple logical operators: shift and add-3. 3
So, its resource utilisation is low. 4
This algorithm uses a small number of logic gates. It is also known as shift-and-add-3 as it uses 5
two operators: shift and add-3. 6
First, we need a variable to store both binary and BCD numbers. 7
The variable should have enough space for both numbers. 8
The binary number is saved on the right-hand side of the variable. 9
The left-hand side of the variable hosts the packed BCD code, 4 bits for each digit. 10
This diagram shows such a variable containing an 8-bit binary and a 2-digit BCD. At the start, the BCD 11
digits are initialized to zero. 12
Let’s assume that the binary number has n-bits; then the double-dabble algorithm performs these two 13
operators (n-1) times. 14
First, left shift one bit the entire variable. Then, add 3 to each BCD digit that is greater than 4 15
(don’t apply that to the most significant digit). 16
This means we should repeat these two operators n-1 times. After finishing these operations, 17
we perform one left-shift operator. 18
Let’s demonstrate this algorithm using an example. 19
Let’s assume that the binary number is 0b0010 1111, which its equivalent 20
decimal is 47. 21
The variable should have at least 16 bits. First, we initialise the binary part with 00101111 22
00101111 and the two BCD digits with 0. 23
Then we perform the left-shift and check the BCD digits if they are greater than 4. After the fifth 24
left-shift, the first BCD digit is 5, which is greater than 4, 25
so we should perform add-3. 26
Then we continue performing the left shift, and in the end, we have the BCD digits in their place. 27
Now, let’s write the HLS code to implement the DoubleDabble algorithm for a two-digit BCD number. For this 28
purpose, we consider 8 bits for the input binary number and 8 bits for the two BCD codes, which 29
means the algorithm variable should have 16 bits. 30
To develop the HLS code, first, we need to define our data types, 16bit, and 8bit unsigned integer 31
data types can be used. 32
It is a good practice to put the left-shift and add-3 operators in a subfunction and call that 33
several times in the HLS top-function. 34
I call the function “double_dabble” which receives a 16-bit variable and return the modified 16-bit 35
variable after applying the left-shift and conditional add-3. 36
Notice that, according to the algorithm, we only need to check the first BCD digit. 37
Now, let’s write the top-function, which is called binary2bcd. It receives an 8-bit binary number 38
which we know it is between 0 and 99 and returns its BCD code via an 8-bit pointer variable. 39
First, we should define the algorithm variable, which has 16 bits. And initialise that with input binary 40
number and set the BCDs to zero. 41
Then according to our algorithm, we should invoke the double-dabble sub-function seven times and then 42
perform the left shift. In the last line, 43
we should copy the BCD codes into the pointer variable. 44
Finally, don’t forget to add interfaces. 45
This code can be synthesized into a combinational logic circuit. 46
How can we show a two-digit decimal number on two seven segments? Answering this question would be 47
the goal of the next lecture. 48
Double-Dabble is an algorithm to convert a binary number into the 1 BCD using a few logic 49
gates. 50
Now the quiz of this lecture. Write the HLS code for the binary2bcd function using the double-dabble 51
algorithm to support a 4-digit BCD number.