You are on page 1of 82

Digital Logic Design

1. M. Morris Mano, “Digital Logic Design”, Prentice Hall, 2018.


2. Anand Kumar, “Fundamentals of Digital Circuits”, Prentice Hall, 2014.
3. R. P. Jain, “Modern Digital Electronics”, Tata McGraw Hill, 2009.
Introduction

v(t) Analog Signal


VMax l8 Discrete Signal
l7
l6 Digital Signal
l5
l4
l3
l2
l1
l0
t
t0 t 1 t2 t 3 t 4 t5
0
Introduction (contd.)

• What is a digital systems


 Takes digital input
 Digital output
 Process digital information
• Digital
 Discrete and discontinuous
 Example – decimal digits 0, 1, 2, … 9
 Combination of digits become number – still digital
• We perceive real world as analog – continuous in values
as well as in time
• Can digital system be of any use?
Audio/Video
Image/Video
Presentation
Capture
Audio/Video Playback
Perception/
Playback

Image/Video Information
Representation

Transmission Transmission

Compression
Audio Processing
Capture

Audio Information Media


A/V
Representation Server
Playback
Storage
Example

• Calculator, computer, laptop, smart phones, mobiles,


internet, routers
• LED TV, smart TV, set-top boxes, digital cameras
• Automated washing machine, air conditioners, various
parts of our cars
Why this course?

• Digital systems is the core of digital revolution


 Very large scale integration
• 4th industrial revolution – ML, AI, IoT
 Fast, efficient, small digital circuits
• Designing an efficient system in a given cost, area,
performance and power is the key
Design of digital system

• Representing Information in digital form


• Boolean algebra, Logic gates, optimizations
• Combinational Logic
• Sequential Logic
• System Design
• Technology aspects, Synthesis, FPGA

• Hardware Description Language (Verilog)


• Simulation and synthesis
• FPGA Implementation
Binary representation of digital signal

• Two states of matter – Black/White, clockwise/anti-


clockwise, charged/discharged, +/- charge
• A binary digit represent two states – 0 and 1
• In electronic system: 1 is high voltage, 0 is low voltage or
vice versa
Binary representation

• 1 bit: represents 2 states


• 2 bits: represents 4 states (00, 01, 10, 11)
• 3 bits: represents 8 states
• N bits: represents 2N states

• 10 bits: represents 210 states = 1024 ≈ 1000


• 20 bits: represents 220 states = 210 x 210 ≈ 106
• 30 bits: represents 230 states ≈ 109
– (Approx. population of India)
Creating digital signal from analog signal
Analog vs Digital System

Analog System Digital System


• Physical/natural signals • Analog Input
• Sensors (image, • ADC
microphone, • Digital System
piezoelectric) • DAC
• Analog System • Analog Output
• Transducers and
Actuators
Shift from Analog to Digital
Digital vs Analog

• Advantages: Digital systems


 Accuracy
 Programmability
 Maintainability
 Design automation
• Disadvantages:
 Area (cost),
 Power
 Performance
 Bandwidth
 High frequency operations
Factors pushing the growth story

• Moore’s Law
• Technology
• Compute requirements
• Design Automation
Number System
Traditional number system

• Historically we have used different base numbers (10,


12, 15, 16, 20)
• We used symbols representing various numbers, like in
Roman, I, V, X, L, C
• Positional and non-positional number system
 Value of a symbol depend on its positions
• Converged to positional decimal system for routine task
 Scalable
 Good for calculations
Common Number Systems

System Base Symbols Used by Used in


humans? computers?
Decimal 10 0, 1, … 9 Yes No
Binary 2 0, 1 No Yes
Octal 8 0, 1, … 7 No No
Hexadecimal 16 0, 1, … 9, No No
A, B, C, D, E, F
Conversion among bases

• The possibilities

Decimal Octal

Binary Hexadecimal
Decimal number system

• Value of symbol depends on its position and radix.


 N = (anan-1…a0)R = ∑aiRi
Weight

• Example: 48310 = 4 x 102 + 8 x 101 + 3 x 100


Base

• Definition can be extended to fractions


 N = (anan-1…a0a-1a-2…a-m)R = ∑aiRi
• Example: 1.74 = 1 x 100 + 7 x 10-1 + 4 x 10-2
Binary to Decimal

• Technique
 Multiply each bit by 2n, where n is the ‘weight’ of the bit
 The weight is the position of the bit, starting from 0 on the right
 Add the results

• Example: 1101012 = ?10


 1011012 = 1 x 25 + 0 x 24 + 1 x 23 + 1 x 22 + 0 x 21 + 1 x 20
= 4510
 101.1012 = 1 x 22 + 0 x 21 + 1 x 20 + 1 x 2-1 + 0 x 2-2 + 1 x 2-3
= 4 + 1 + 0.5 + 0.125 = 5.625
Octal to Decimal

• Technique
 Multiply each bit by 8n, where n is the ‘weight’ of the digit
 The weight is the position of the digit, starting from 0 on the right
 Add the results

• Example: 7248 = ?10


 7248 = 7 x 82 + 2 x 81 + 4 x 80
= 46810
Hexadecimal to Decimal

• Technique
 Multiply each bit by 16n, where n is the ‘weight’ of the digit
 The weight is the position of the digit, starting from 0 on the right
 Add the results

• Example: ABC16 = ?10


 ABC16 = A x 162 + B x 161 + C x 160
= 10 x 162 + 11 x 161 + 12 x 160
= 274810
Decimal to Binary

• Technique
 N divide by two, reminder is a0, quotient become N
 Repeat above step to get subsequent digits
• Example: 5310 = ?2
2 53
2 26 1 a0 = 1 5310 = 1101012
2 13 0 a1 = 0
2 6 1 a2 = 1
2 3 0 a3 = 0
2 1 1 a4 = 1
0 1 a5 = 1
Octal to Binary

• Technique
 Convert each octal digit to a 3-bit equivalent binary
representation

• Example: 7058 = ?2
7 0 5

111 000 101

7058 = 1110001012
Hexadecimal to Binary

• Technique
 Convert each hexadecimal digit to a 4-bit equivalent binary
representation

• Example: 10AF16 = ?2
1 0 A F

0001 0000 1010 1111

10AF16 = 00010000101011112
Decimal to Octal

• Technique
 N divide by 8, reminder is a0, quotient become N
 Repeat above step to get subsequent digits
• Example: 123410 = ?8
8 1234
8 154 2 a0 = 2 123410 = 23228
8 19 2 a1 = 2
8 2 3 a2 = 3
0 2 a3 = 2
Decimal to Hexadecimal

• Technique
 N divide by 16, reminder is a0, quotient become N
 Repeat above step to get subsequent digits
• Example: 123410 = ?16
16 1234
16 77 2 a0 = 2 123410 = 4D216
16 4 13 a1 = D
0 4 a2 = 4
Binary to Octal

• Technique
 Group bits in threes, starting on right
 Convert to octal digits

• Example: 10110101112 = ?8
1 011 010 111

1 3 2 7

10110101112 = 13278
Binary to Hexadecimal

• Technique
 Group bits in fours, starting on right
 Convert to hexadecimal digits

• Example: 10101110112 = ?16


10 1011 1011

2 B B

10101110112 = 2BB16
Octal to Hexadecimal

• Technique
 Use binary as an intermediary

• Example: 10768 = ?16


1 0 7 6

001 000 111 110

2 3 E

10768 = 23E16
Hexadecimal to Octal

• Technique
 Use binary as an intermediary

• Example: 1F0C16 = ?8
1 F 0 C

0001 1111 0000 1100

1 7 4 1 4

1F0C16 = 174148
Assignment

Decimal Binary Octal Hexadecimal


33
1110101
703
1AF
Addition in Binary

• Similar to decimal additions


 1 + 1 = (10)2
 1 + 1 + 1= (11)2
 1 + 0 = (1)2

1 0 1 1 1
1 1 0 1 1 1
+ 1 1 0 1 0 1
1 1 0 1 1 0 0
Subtraction in Binary

• Similar to decimal subtraction


 (1 – 1) = (0)2
 (1 – 0) = (1)2
 (0 – 1) = (1)2, borrow 1

1 1 1
1 1 0 0 1 1
- 0 1 0 1 0 1
0 0 1 1 1 1 0
Decimal to binary conversion - fractions

• Fraction part (F) to base R


 Multiply F with R, non-fractional part is a-1, fractional become F
 Repeated multiplication get subsequent digits
• Example: 0.7 to binary

0.7 x2 1.4 a-1 = 1


(0.7)10 = (0.101100)2
0.4 x2 0.8 a-2 = 0
0.8 x2 1.6 a-3 = 1
0.6 x2 1.2 a-4 = 1
0.2 x2 0.4 a-5 = 0
0.4 x2 0.8 a-6 = 0
Assignment

Decimal Binary Octal Hexadecimal


29.8
101.1101
3.07
C.82
How to represent negative numbers?

• Negative Numbers
 Sign and Magnitude Representation
 1’s Complement Representation
 2’s Complement Representation
Goal of negative number systems

• Signed system: Simple. Just flip the sign bit


 0 = positive
 1 = negative
• 1’s complement:
 Easy to derive (Just flip every bit)
• 2’s complement:
 Addition of 1’s complement and 1 produces the 2’s complement
Representation of negative number

• 1’s complement
 Formula: 2n – 1 – x
• i.e. n =4, 24 – 1 – x = 15 – x
• In binary: (1111) – (b3b2b1b0)
• Just flip all the bits
• 2’s complement
 Formula: 2n – x
• i.e. n =4, 24 – x = 16 – x
• In binary: (10000) – (0b3b2b1b0)
• Just flip all the bits and add 1
4-bit Example
b3 b2 b1 b0 Signed 1’ 2’
0 0 0 0 0 0 0 0
1 0 0 0 1 1 1 1
2 0 0 1 0 2 2 2
3 0 0 1 1 3 3 3
4 0 1 0 0 4 4 4
5 0 1 0 1 5 5 5
6 0 1 1 0 6 6 6
7 0 1 1 1 7 7 7
8 1 0 0 0 -0 -7 -8
9 1 0 0 1 -1 -6 -7
10 1 0 1 0 -2 -5 -6
11 1 0 1 1 -3 -4 -5
12 1 1 0 0 -4 -3 -4
13 1 1 0 1 -5 -2 -3
14 1 1 1 0 -6 -1 -2
15 1 1 1 1 -7 -0 -1
Range for n-bits

• 3 bit • 5 bit
 Signed: -3, 3  Signed: -15, 15
 1’s: -3, 3  1’s: -15, 15
 2’s: -4, 3  2’s: -16, 15
• 6 bits • 8 bits
 Signed: -31, 31  Signed: -127, 127
 1’s: -31, 31  1’s: -127, 127
 2’s: -32, 31  2’s: -128, 127

• Formula for calculating the range


 Signed & 1’s: -(2n-1 – 1), (2n-1 – 1)
 2’s: -2n-1, (2n-1 – 1)
2’s complement arithmetic

• Input: Two integers X & Y


 Represent the operands in 2’s complement
 Sum up the two operands and ignore bit n
 The result is the solution in 2’s complement

Arithmetic 2’s Complement


X+Y X+Y
X–Y X + (2n – Y) = 2n + (X – Y)
–X+Y (2n – X) + Y = 2n + (– X + Y)
–X–Y (2n – X) + (2n – Y) = 2n + 2n – (X – Y)
Arithmetic Operations

• Example: 4 – 3 = 1
410 = 01002
310 = 00112, – 310 = 11012

0 1 0 0
+ 1 1 0 1
1 0 0 0 1 1 (after discarding extra bit)

Discard the extra 1 at the left which is 2n form 2’s


complement of – 3. Note that bit bn-1 is 0. Thus, the result
is positive
Arithmetic Operations

• Example: – 4 + 3 = – 1
410 = 01002 , – 410 = 11002
310 = 00112 ,

1 1 0 0
+ 0 0 1 1
1 1 1 1 Using 2’s Complement: 0000 + 1 = 1
So answer = -1

If left most bit is 1, it means that the result is negative


1’s complement arithmetic

• Input: Two integers X & Y


 Represent the operands in 1’s complement
 Sum up the two operands
 Subtract 2n – 1 if there is carry out at left
 The result is the solution in 1’s complement

Arithmetic 1’s Complement


X+Y X+Y
X–Y X + (2n – Y – 1) = 2n – 1 + (X – Y)
–X+Y (2n – 1 – X) + Y = 2n – 1 + (– X + Y)
–X–Y (2n – 1 – X) + (2n – 1 – Y) = 2n – 1 + (2n – 1 – X – Y)
Arithmetic Operations

• Example: 4 – 3 = 1
410 = 01002
310 = 00112, – 310 = 11002 (1’s complement)
0 1 0 0 (4 in decimal)
+ 1 1 0 0 (12 in decimal or 15 – 3)
1 0 0 0 0 (16 in decimal or 15 + 1)
0 0 0 1 (after subtracting 2n – 1)

Discard the extra 1 at the left which is 2n and add one at


the first bit
Arithmetic Operations

• Example: – 4 + 3 = – 1
410 = 01002 , – 410 = 10112 (1’s complement)
310 = 00112 ,

1 0 1 1 (11 in decimal or 15 – 4)
+ 0 0 1 1 (3 in decimal)
1 1 1 0 (14 in decimal or 15 – 1)
The value is in 1’s complement

If left most bit is 1, it means that the result is negative


Representing real numbers

• Digital numbers are discrete


 Can we use them to represent real numbers?
• We learnt to represent fractional part in binary
• One issue: Decimal point also to be represented in
binary
How to represent decimal point

• Fixed point
 Agreement between writer and reader
 Does not change during the program
 Total number of digits remain same
• Example
 Assuming 4 decimal digits can be represented and decimal point
after 2 digits
• 2300, 123, 1, 100 would represent 23.00, 1.23, 0.01, 1.00
 Effectively multiplying a number by 100
Fixed point in binary
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0

• No decimal point
 Maximum range (0 to 65536) or (-32768 to 32767)
• Decimal point after 4 bits
 Range -2048 to 2047 and 4 bits for fraction
• Decimal point after 8 bits
 Range -256 to 255 and 8 bits for fraction

Limited range and precision – with given number of bits


Real numbers
Number of atoms in 1 gram of Hydrogen
602000000000000000000000

Data generated in a day


Weight = 5.972 x 1024 kg in 2018: 2500 peta byte

Electron mass =
Number of bytes in one peta byte
9.1093837015 x 10-31 kg
1125899906842624 ≈ 1015 bytes

Size of transistor in latest chips 7 nm


Best way to represent real numbers

• Scientific notations 0.5 x 106 (not followed)


 Normalized 5.0 x 105
• Large as well as small numbers can be represented
• Exponential in binary
1000001 = 1.000001 x 26
• Representing – decimal point, exponent in binary?
Exponential binary numbers

• Significand (could be +/-) -1.000000 x 2-26


• Fraction or mantissa 1.101001 x 28
• Exponent (could be +/-) -1.100010 x 210
• Typical binary representation 1.000001 x 2-18
 1 bit for sign of significand
 Few bits for exponent
 Rest bits for mantissa

How to distribute bits in exponent and mantissa?


IEEE 754 Floating point

• 32 bit number (single precision)


 1 bit for sign (s)
 8 bits for exponent (e): bias of 127, 0 and 255 and reserved
 23 bits for mantissa (m)

3 3 2 2 2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 1 1 1 9 8 7 6 5 4 3 2 1 0
1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0
s Exponent Mantissa

• Number = (-1)s x (1 + 0.m) x 2e


Floating point example 1

• Decimal number: 24.25


= 16 + 8 + 0.25
• Binary number: 11000.01
• Normal scientific form: 1.100001 x 24
• Sign: 0
• Mantissa: 100 0010 0000 0000 0000 0000
• Exponent: (127 + 4)10 = (131)10 = (100 0001 1)2
• Floating point number
0100 0001 1100 0010 0000 0000 0000 0000
0x41C20000
Floating point example 2

• Decimal number: 0.625


= 0.5 + 0.125
• Binary number: 0.101
• Normal scientific form: 1.01 x 2-1
• Sign: 0
• Mantissa: 010 0000 0000 0000 0000 0000
• Exponent: (127 - 1)10 = (126)10 = (011 1111 0)2
• Floating point number
0011 1111 0010 0000 0000 0000 0000 0000
0x3F200000
Floating point example 3

• Decimal number: -2.625


= 2 + 0.5 + 0.125
• Binary number: 10.101
• Normal scientific form: -1.0101 x 21
• Sign: 1
• Mantissa: 010 1000 0000 0000 0000 0000
• Exponent: (127 + 1)10 = (128)10 = (100 0000 0)2
• Floating point number
1100 0000 0010 1000 0000 0000 0000 0000
0xC0280000
Floating point example 4

• Decimal number: 1
• Binary number: 1
• Normal scientific form: 1.0 x 20
• Sign: 0
• Mantissa: 000 0000 0000 0000 0000 0000
• Exponent: (127 + 0)10 = (127)10 = (011 1111 1)2
• Floating point number
0011 1111 1000 0000 0000 0000 0000 0000
0x3F800000
Example (continued)

• Floating point to decimal


• Floating point number: 0xC2630000
1100 0010 0110 0011 0000 0000 0000 0000
• Sign: 1
• Exponent: 100 0010 0 = 128 + 4 = 132
 Actual exponent = 132 – 127 = 5
• Mantissa: 110 0011 0000 0000 0000 0000
• Binary number = 1.110 0011 x 25
= 111000.11
= 32 + 16 + 8 + 0.5 + 0.25
= 56.75 = - 56.75
Range

• Largest number: exponent 254, mantissa all 1’s


 F = 1.111…1 x 2127 ≈ 2128 ≈ 3.4x1038
• Smallest number: exponent 1, mantissa 0
 F = 1.0 x 2-126 ≈ 1.1 x 10-38
• Zero – Exponent 0, Mantissa 0
 Positive 0: 0x0000 0000
 Negative 0: 0x8000 0000

https://www.h-Schmidt.net/FloatConverter/IEEE754.html
Number line

Largest negative Smallest negative Smallest positive Largest positive


-3.4 x 1038 -1.1 x 10-38 1.1 x 10-38 3.4 x 1038

• Discontinuity at and near 0 and two values of 0


 If number is smaller than smallest positive: positive 0
 If number is smaller than smallest negative: negative 0
 If number is more than largest positive: positive Infinity
Reserve exponent values

• E = 255
– If mantissa is 0: it represents infinity
– If mantissa is ≠: NaN, not a number
• Divide by 0, multiplication of infinity with zero
• E=0
– If mantissa is 0: number is 0
– If mantissa is ≠: denormal number
De-normal numbers

• Numbers smaller than smallest normal number and


larger than 0
• Exponent is zero
• N = (-1)s x P x 2-126, (P = 0 + M, 0 < M < 1)
• Smallest denormal number: 2-149
De-normal number example

• Smallest normal number


– 1.0 x 2-126
• Number smaller than smallest normal number
0.00011 x 2-126
• Sign: 0
• Mantissa: 000 1100 0000 0000 0000 0000
• Exponent: (0)10 = (000 0000 0)2
• Floating point number:
0000 0000 0000 1100 0000 0000 0000 0000
0x000C0000
IEEE 754 Floating point

• 64 bit number (double precision)


– 1 bit for sign (s)
– 11 bits for exponent (e): bias of 1023, 0 and 1024 are reserved
– 52 bits for mantissa (m)
Approx 15 digit of decimal precision

• Half precision number – 16 bits


– 1 bit for sign (s)
– 5 bits for exponent (e): bias of 15
– 10 bits for mantissa (m)
Extended precision numbers

• More precise then double precision


– Non standardized
• Intel’s 80 bit format
– 1 bit for sign (s)
– 15 bits for exponent (e): bias of 15
– 64 bits for mantissa (m)

128 bit or 256 bit floating point formats


Example of half precision

• Decimal number: -2.625


= 2 + 0.5 + 0.125
• Binary number: -10.101
• Normal scientific form: -1.0101 x 21
• Sign: 1
• Mantissa: 01 0100 0000
• Exponent: (15 + 1)10 = (16)10 = (100 00)2
• Floating point number
1100 0001 0100 0000
0xC140
Issues with Floating point numbers

• Quantization issues
– Different rounding can lead to different results
– Different formats have different precision
– Accumulation of errors
• Hardware/computational cost
Addition/subtraction of Floating point

1. Align the exponent 1.000 x 2-1 + (-1.110 x 2-2)


smaller number to
larger exponent Step 1: -1.110 x 2-2 = -0.111 x 2-1)
2. Add/subtract the Step 2: 1.0 – 0.111 = 0.001
significands
Step 3: 0.001 x 2-1 = 1.0 x 2-4
3. Normalize, overflow /
Step 4: Nothing to be done in this
underflow checks
case
4. Round
5. Normalized, otherwise
Sum: 1.000 x 2-4
move to step 3
6. Done
Multiplication in Floating point

1. Add exponents and 1.000 x 2-1 x (-1.110 x 2-2)


subtract bias
2. Multiply significands Step 1: -1 + (-2) = -3
3. Normalize, overflow / 126 + 125 – 127 = 124
underflow checks Step 2: 1.0 x 1.110 = 1.1100 00
4. Round Step 3: Normalize: nothing to be
5. Normalized, otherwise done
move to step 3 Step 4: Nothing to be done in
6. Set sign bit this case
7. Done
Product: -1.11 x 2-3
Number encoding problem

• Finding one-to-one mapping between binary combination


and corresponding decimal value
– 4 bit binary number have 16 possible arrangements
– Which arrangement should be mapped to which decimal number
– Is any other option other than positional number system?
• Positional number system is not the only method!
BCD number system

• Problem: we want to represent decimal numbers in


binary
• Solution:
– Convert decimal number to binary, use positional binary number
• Inconvenient conversion
• For display again binary to decimal conversion is required
– Represent each digit to corresponding binary: BCD number
system
8421 BCD numbers

• Say we want to represent decimal number 5682 to


binary?
– 5: 0101
– 6: 0110 0101 0110 1000 0010
– 8: 1000 5 6 8 2
– 2: 0010
• Each decimal digit is represented by 4 bits/one nibble
– Four bits have weight of 8, 4, 2 and 1
– 8421 BCD number (default BCD representation)
BCD number advantages

• No conversion from decimal representation required


– One-to-one mapping with each digit
– More intuitive representation
• Initial computers (example IBM System/360) used BCD
numbers
BCD Arithmetic

• BCD addition
• Algorithm
– Normal binary addition
– If output of a nibble is more than 9 or carry is generated by the
nibble: add 0110 to the nibble
– Carry pass from one nibble to another in both steps (1 and 2)

56 + 75
0 1 0 1 0 1 1 0
+ 0 1 1 1 0 1 0 1
1 1 0 0 1 0 1 1
+ 0 1 1 0 0 1 1 0
1 0 0 1 1 0 0 0 1
1 3 1
Other BCD representation

• Excess 3 code

0 0011
• Self complementing
1 0100
2 0101 • After addition of two numbers – need
3 0110 to add/subtract 3 to fix the final
4 0111 results
5 1000
6 1001
7 1010
8 1011
9 1100
Error resilient codes

• 2 out of 5 code (2 ones are used to represent symbol)

0 00011
1 00101
2 00110
3 01001
4 01010
5 01100
6 10001
7 10010
8 10100
9 11000
Low power code: Gray code

• Transition between adjacent number is minimum

0 0000
1 0001
2 0011
3 0010
4 0110
5 1110
6 1010
7 1011
8 1001
9 1000
Representing characters

• ASCII
– Using 8 bit number for each commonly used character
• UTF-8
– Compatible with ASCII
– Used 1 to 6 bytes
• UTF-16
Summary

• Binary can be used to represent


– Signed, unsigned numbers, text, floats
– Commands, instructions
• Binary can be used to represent movies, presentations,
documents, etc.

You might also like