Professional Documents
Culture Documents
EE2026 L1 NumberSystems VerilogIntro
EE2026 L1 NumberSystems VerilogIntro
Digital Design
NUMBER SYSTEMS & VERILOG INTRO
Chua Dingjuan elechuad@nus.edu.sg
NUMBER SYSTEMS
Positional Number Systems : Decimal, Binary, Hex
Binary Arithmetic, Signed Number Representations
Introduction to Verilog
Positional Number System (Decimal)
Decimal number: Digits (0 to 9) Radix (r=10)
Terminologies
◦ Radix (or base)
◦ Digits and a numeral (0 →
radix-1) Number 1260.25
◦ Radix point
◦ Place value (or weight) is in
the power of the base 103 100 10-1
(positive on the left and
negative on the right side of 102
Radix point
the radix point integer part fractional part
*Weighted sum of each digit (each digit is weighted by its place value)
3
Radix r and its Decimal Equivalent
General form of Number of radix r:
Radix point
Ar = ( an an −1... ao .a−1... a− m )r
where an , an −1,..., a0 ,..a− m 0,...( r − 1) (Integer only)
Decimal equivalent:
Ar = (an an −1... ao .a−1... a−m )r Radix point is here
4
Binary Number
Digits (0 to 1) Radix (r=2)
Number 10110.01
20 2-1
Radix point
24
Decimal Equivalent: 23
N10 = 1 24 + 0 23 + 1 22 + 1 21 + 0 20 + 0 2 −1 + 1 2 −2
1
= 16 + 0 + 4 + 2 + 0 + 0 +
4
= 22.25
(10110.01)2 = (22.25)10
5
MSB and LSB of a Binary Number
MSB
◦Most significant bit
LSB
◦Least significant bit
1011001
(Left-most bit) MSB LSB (Right-most bit)
Range
◦ 0 to 2n – 1, where n is the number of bits ( 2n values )
6
( 1 1 0 1 . 0 1 1 0 )2
Hexadecimal number ( D . 6 )16
Digits (0 to 15) Hex Dec Bin
Radix (r=16) 0 0 0000
1 1 0001
3 3 0011
4 4 0100
16-2
163 2 160 16-1 5 5 0101
16
6 6 0110
Radix point
Decimal Equivalent: 7 7 0111
−1 −2 8 8 1000
N10 = 1 16 + 8 16 + F 16 + 4 16 + 2 16 + 10 16
3 2 1 0
9 9 1001
2 10
= 4096 + 2048 + 240 + 4 + + A 10 1010
16 256 B 11 1011
21 C 12 1100
= 6388 +
128 (18F4.2A)16 (6388.16)10 D 13 1101
6388.16 E 14 1110
F 15 1111
7
( 1 0 1 . 0 1 1 )2
Octal number ( 5 . 3 )8
Radix (r=8) Digits (0 to 7) Oct Dec
0 0
1 1
3
2
4 4
80 8-1 5 5
82 6 6
Decimal Equivalent: Radix point
7 7
N10 = 7 82 + 5 81 + 4 80 + 2 8−1 ? 8
? 9
2
= 448 + 40 + 4 + ? 10
8
= 492.25
8
Radix Conversion
Three types of conversions:
o Radix r (r10) → Decimal
9
Radix r (r 10) → Decimal (r = 10)
Binary → Decimal (10110.01)2 = (??)10
(10110.01)2 → 1 24 + 0 23 + 1 22 + 1 21 + 0 20 + 0 2 −1 + 1 2 −2 = (22.25)10
10
Decimal (r = 10) → Radix r (r 10)
Decimal → Binary (102)10 = (??)2
(102)10 = A2 = ( an an −1... ao .a−1... a− m )r
= an 2 n + an −1 2 n −1 + ... + a1 21 + ao (Assume integer)
= ( an 2 n + an −1 2 n −1 + ... + a1 21 ) + ao
Integer multiple of 2
25/2 12 → a2 N10 = a6 26 + a5 25 + a4 2 4 + a3 23
12/2 6 → a3 + a2 2 2 + a1 21 + a0 20
6/2 3 → a4 = 1 2 6 + 1 25 + 0 2 4 + 0 23
3/2 1 → a5 + 1 2 2 + 1 21 + 0 20
1/2 0 → a6 = 64 + 32 + 0 + 0 + 4 + 2 + 0
= 102
Stop when the quotient = 0
12
How about Fractional Numbers?
Decimal → Binary (0.58)10 = (??)2
Multiply by 2:
13
How about Fractional Numbers? – cont.
Decimal → Binary (0.58)10 = (??)2
15
Binary Arithmetic
ADDITION, SUBTRACT, MULTIPLICATION, DIVISION
Page 16
Addition
Addition table:
0+0=0
0+1=1
1 + 1 = 10
Example:
10111 + 110 = 11101
1 1
Carry
10111
+ 110
---------------
11101
17
Multiplication
Multiplication table:
0x0=0
0x1=0
1x1=1 Example:
10111 x 110 = 10001010
10111 Multiplicand
x 110 Multiplier
---------------
Multiplication: 00000
→ Shift then Add 10111 Partial products
→ Only need “add” operation + 10111
----------------------
10001010 Product
18
Subtraction
Subtraction table:
0-0=0
1-0=1
1-1=0
0–1=1 with a borrow from the next (higher) bit
Example:
11011 - 110 = 10101
11011
- 110
---------------
10101
19
Division (shift and subtract)
→ Shift then subtraction
Division → Only need “subtract” operation
• Set quotient to 0
100101/101 = ?
• Align leftmost digits in dividend and divisor
• Repeat
111 quotient • If that portion of the dividend above
the divisor is greater than or equal to
101 100101 the divisor
101 • Then subtract divisor from that
portion of the dividend and
1000 • Concatenate 1 to the right hand
101 end of the quotient
• Else concatenate 0 to the right
111 hand end of the quotient
101 • Shift the divisor one place right
• Until dividend is less than the divisor
10 remainder
• quotient is correct, dividend is remainder
• STOP
Check in decimal
20
Arithmetic
•Only addition, subtraction and shifting are needed
for 4 binary arithmetic operations
•Subtraction can be performed by adding a negative
number
•Thus, a computer may only use adders and shifters
to perform all binary arithmetic operations
•This requires an appropriate representation of the
negative binary numbers
21
Signed Binary numbers
Three ways to represent the signed binary
numbers
22
Unsigned Binary number
23
Signed Binary – Signed Magnitude (S-M)
Example:
Signed binary number (n bits)
Decimal S-M
3 011
1 0 1 … … 1 0
2 010
1 001
Note:
Magnitude +0 000 Two zeros
-0 100
Sign bit 0 – represents a positive number
-1 101
1 – represents a negative number
-2 110 Negative
MSB is a sign bit numbers
-3 111
24
Signed Magnitude – cont.
More examples: 00111010 = +0111010 = (58)10
25
Arithmetic
using Binary Numbers (S-M) ?
Computer performs binary arithmetic operations using only
◦ Adders
◦ Multipliers
Subtraction is performed by adding a negative number
27
Diminished Radix Complement
A* = rn – A – 1 or A*= (rn – 1) - A
Examples:
Decimal Operation: 1 1 1
Binary number:
A = 0011 → A* = (100002 – 1) – 00112 = 11112 – 00112 = 11002
29
1’s Complement representation of
signed binary number
No change for positive numbers and use 1’s complement for negative numbers
Two Zeroes?
30
2’s Complement of a Binary Number
“2’s Complement” is the radix complement of binary
numbers
2’s complement of a n-bit number can be obtained by
adding “1” to its 1’s complement (reversing all the bits), i.e.,
A* = 2n – A
= (2n – A -1) + 1
Binary number (n=8): 01011100
31
2’s Complement Arithmetic
No change for positive numbers and use 2’s complement for negative numbers
Page 36
The Module
A piece of hardware with inputs & outputs : module
Module box
Name Port Declaration
module box( input a, b
input [1:0] c,
output [3:0] y );
// Here is where the magic
happens!
endmodule y : y[3] y[2] y[1] y[0]
o Verilog is CasE-SeNSitiVe….
o Module Name : No spaces, No starting with numbers (1box), use meaningful names (box)
o Port Direction : input, output, inout (bidirectional)
o Port Bitwidth : input a,b ; input [1:0] c ; output [3:0] y
By default, signals Input c is a 2-bit bus / Output y is a 4-bit vector
are one bit! vector (little endian)
o Don’t forget the _____!
Page 37
Data Types – Net / wire
module box( input a, b box
input [1:0] c,
output [3:0] y ); a 4
wire tmp; b y
2 tmp
assign tmp = a; c
Page 41
Useful Operators
Operator Description Examples: a = 4’b1010, b=4’b0000
Page 42
What is happening here?
o Let’s assume that a, b and c are being box
provided these values as shown → a=1
b=0 4
module box( input a, b y
2
input [1:0] c, c = 2’b10
output [3:0] y );
wire tmp; Net / Variable Number Value in
reg [1:0] one = 3; Name of bits? dec / bin
reg two; a
integer three = 1;
b
Page 43
Summary
We have covered :
•Positional number system (radix 10, 2, 8 and 16)
•Conversion among decimal, binary, octal and hex
•Binary arithmetic
•Signed binary number representations, SM, 1’s C, 2’s C
•Arithmetic using SM, 1’s C, 2’s C
We have covered:
•Introduction to Verilog
•Module, input and output ports (Single Bit and Multi-Bit signals)
•Data Types (wire/net vs reg)
•Numerical values (Hexa/Decimal/Binary/Octal/Integer)
•Addition / Subtraction / Conditional Operators
Page 44