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