0
1
00:00:01,460 --> 00:00:07,760
In this lecture, I am going to explain how to implement a binary to BCD converter algorithm called 
1

2
00:00:07,760 --> 00:00:08,570
Double Dabble. 
2

3
00:00:09,230 --> 00:00:14,390
This algorithm uses two simple logical operators: shift and add-3. 
3

4
00:00:14,900 --> 00:00:17,240
So, its resource utilisation is low.
4

5
00:00:22,130 --> 00:00:29,060
This algorithm uses a small number of logic gates. It is also known as shift-and-add-3 as it uses 
5

6
00:00:29,060 --> 00:00:30,560
two operators: shift and add-3.
6

7
00:00:30,560 --> 00:00:36,070
First, we need a variable to store both binary and BCD numbers. 
7

8
00:00:36,710 --> 00:00:39,640
The variable should have enough space for both numbers. 
8

9
00:00:40,670 --> 00:00:43,940
The binary number is saved on the right-hand side of the variable. 
9

10
00:00:44,540 --> 00:00:50,540
The left-hand side of the variable hosts the packed BCD code, 4 bits for each digit. 
10

11
00:00:53,080 --> 00:01:01,310
This diagram shows such a variable containing an 8-bit binary and a 2-digit BCD. At the start, the BCD 
11

12
00:01:01,330 --> 00:01:03,660
digits are initialized to zero.
12

13
00:01:05,100 --> 00:01:11,610
Let’s assume that the binary number has n-bits; then the double-dabble algorithm performs these two
13

14
00:01:11,610 --> 00:01:13,950
operators (n-1) times.
14

15
00:01:15,840 --> 00:01:23,220
First, left shift one bit the entire variable. Then, add 3 to each BCD digit that is greater than 4 
15

16
00:01:23,470 --> 00:01:25,800
(don’t apply that to the most significant digit). 
16

17
00:01:27,390 --> 00:01:34,110
This means we should repeat these two operators n-1 times. After finishing these operations, 
17

18
00:01:34,350 --> 00:01:36,450
we perform one left-shift operator.
18

19
00:01:38,180 --> 00:01:41,480
Let’s demonstrate this algorithm using an example. 
19

20
00:01:42,840 --> 00:01:49,950
Let’s assume that the binary number is 0b0010 1111, which its equivalent 
20

21
00:01:49,960 --> 00:01:51,180
decimal is 47.
21

22
00:01:53,130 --> 00:01:59,520
The variable should have at least 16 bits. First, we initialise the binary part with 00101111 
22

23
00:01:59,520 --> 00:02:03,400
00101111 and the two BCD digits with 0.
23

24
00:02:06,390 --> 00:02:13,290
Then we perform the left-shift and check the BCD digits if they are greater than 4. After the fifth 
24

25
00:02:13,290 --> 00:02:17,420
left-shift, the first BCD digit is 5, which is greater than 4, 
25

26
00:02:17,670 --> 00:02:19,560
so we should perform add-3. 
26

27
00:02:20,720 --> 00:02:26,690
Then we continue performing the left shift, and in the end, we have the BCD digits in their place. 
27

28
00:02:28,150 --> 00:02:34,630
Now, let’s write the HLS code to implement the DoubleDabble algorithm for a two-digit BCD number. For this 
28

29
00:02:34,630 --> 00:02:40,720
purpose, we consider 8 bits for the input binary number and 8 bits for the two BCD codes, which 
29

30
00:02:40,720 --> 00:02:43,630
means the algorithm variable should have 16 bits.
30

31
00:02:45,860 --> 00:02:52,580
To develop the HLS code, first, we need to define our data types, 16bit, and 8bit unsigned integer 
31

32
00:02:52,580 --> 00:02:54,110
data types can be used.
32

33
00:02:55,830 --> 00:03:01,860
It is a good practice to put the left-shift and add-3 operators in a subfunction and call that 
33

34
00:03:01,860 --> 00:03:03,930
several times in the HLS top-function.
34

35
00:03:05,560 --> 00:03:12,040
I call the function “double_dabble” which receives a 16-bit variable and return the modified 16-bit 
35

36
00:03:12,040 --> 00:03:15,430
variable after applying the left-shift and conditional add-3.
36

37
00:03:17,520 --> 00:03:22,590
Notice that, according to the algorithm, we only need to check the first BCD digit.
37

38
00:03:25,240 --> 00:03:32,380
Now, let’s write the top-function, which is called binary2bcd. It receives an 8-bit binary number 
38

39
00:03:32,380 --> 00:03:39,500
which we know it is between 0 and 99 and returns its BCD code via an 8-bit pointer variable.
39

40
00:03:40,340 --> 00:03:47,050
First, we should define the algorithm variable, which has 16 bits. And initialise that with input binary 
40

41
00:03:47,050 --> 00:03:49,600
number and set the BCDs to zero.
41

42
00:03:50,350 --> 00:03:56,530
Then according to our algorithm, we should invoke the double-dabble sub-function seven times and then 
42

43
00:03:56,530 --> 00:03:59,140
perform the left shift. In the last line, 
43

44
00:03:59,270 --> 00:04:01,960
we should copy the BCD codes into the pointer variable. 
44

45
00:04:02,590 --> 00:04:05,590
Finally, don’t forget to add interfaces. 
45

46
00:04:07,080 --> 00:04:10,290
This code can be synthesized into a combinational logic circuit.
46

47
00:04:12,090 --> 00:04:18,270
How can we show a two-digit decimal number on two seven segments? Answering this question would be 
47

48
00:04:18,270 --> 00:04:19,500
the goal of the next lecture.
48

49
00:04:21,640 --> 00:04:27,610
Double-Dabble is an algorithm to convert a binary number into the 1 BCD using a few logic 
49

50
00:04:27,610 --> 00:04:28,120
gates.
50

51
00:04:29,630 --> 00:04:35,690
Now the quiz of this lecture. Write the HLS code for the binary2bcd function using the double-dabble 
51

52
00:04:35,690 --> 00:04:39,290
algorithm to support a 4-digit BCD number.
