Number Systems Readings: 3-3.3.3, 3.3.5 Problem: Implement simple pocket calculator Need: Display, adders & subtractors, inputs Display: Seven segment displays Inputs: Switches Missing: Way to implement numbers in binary Approach: From decimal to binary numbers (and back) 8
Decimal (Base ) Numbers Positional system - each digit position has a value 2534 = 2*, + 5* + 3* + 4* Alternate view: Digit position I from the right = Digit * I (rightmost is position ) 2534 = 2* 3 + 5* 2 + 3* + 4* 9
Base R Numbers Each digit in range..(r-),,2,3,4,5,6,7,8,9,a,b,c,d,e,f... A = B = C = 2 D = 3 E = 4 F = 5 Digit position I = Digit * R I D 3 D 2 D D (base R) = D 3 *R 3 +D 2 *R 2 +D *R +D *R 2
Number System (Conversion to Decimal) Binary: () 2 = Hexadecimal: (E32) 6 = 2
Conversion from Base R to Decimal Binary: () 2 Hexadecimal: (A6) 6 22
Conversion of Decimal to Binary (Method ) For positive, unsigned numbers Successively subtract the greatest power of two less than the number from the value. Put a in the corresponding digit position 2 = 2 4 =6 2 8 =256 2 2 =496 (4K) 2 =2 2 5 =32 2 9 =52 2 3 =892 (8K) 2 2 =4 2 6 =64 2 =24 (K) 2 3 =8 2 7 =28 2 =248 (2K) 23
Decimal to Binary Method Convert (2578) to binary Convert (289) to binary 24
Conversion of Decimal to Binary (Method 2) For positive, unsigned numbers Repeatedly divide number by 2. Remainder becomes the binary digits (right to left) Explanation: 25
Decimal to Binary Method 2 Convert (289) to binary 26
Decimal to Binary Method 2 Convert (85) to binary 27
Converting Binary to Hexadecimal hex digit = 4 binary digits Convert () 2 to hex Convert (A3FF2A) 6 to binary 28
Converting Decimal to Hex Convert to binary, then to Hex Convert (98) to Hexadecimal 29
Arithmetic Operations Decimal: 5 7 8 9 2 + 7 8 9 5 6 Binary: + Decimal: 5 7 8 9 2-3 2 9 4 6 Binary: - 3
Arithmetic Operations (cont.) Decimal: 2 * 2 4 Binary: * 3
Half Adder Ai Bi Carry Sum Ai Bi Bi Ai Ai Bi Bi Ai Carry = Ai Bi Sum = Ai Bi + Ai Bi = Ai + Bi A i Sum B i Half-adder Schematic Carry 32
Full Adder A B CI CO S 33
Full Adder Implementation 34
Multi-Bit Addition A 3 B 3 A 2 B 2 A B A B A 3 A 2 A A + B 3 B 2 B B A B A B A B A B CO + CI CO + CI CO + CI CO + CI S S S S S 3 S 2 S S 35
Multi-Bit Addition in Verilog, Parameters module uadd #(parameter WIDTH=8) (out, a, b); output logic [WIDTH:] out; input logic [WIDTH-:] a, b; always_comb begin out = a + b; end endmodule module add4 #(parameter W=22) (out, a, b, c, d); output logic [W+:] out; input logic [W-:] a, b, c, d; endmodule 36
Negative Numbers Need an efficient way to represent negative numbers in binary Both positive & negative numbers will be strings of bits Use fixed-width formats (4-bit, 6-bit, etc.) Must provide efficient mathematical operations Addition & subtraction with potentially mixed signs Negation (multiply by -) 37
Sign/Magnitude Representation -4-3 -5-2 -6 - -7 - + +7 + +6 +2 +3 +4 +5 + = + 4 = - 4 - High order bit is sign: = positive (or zero), = negative Three low order bits is the magnitude: () thru 7 () n- Number range for n bits = +/-2 - Representations for : 38
Sign/Magnitude Addition (+2) + (+4) (-2) + (-4) (+2) + ( -4) ( -2) + (+4) Bottom line: Basic mathematics are too complex in Sign/Magnitude 39
Idea: Pick negatives so that addition works Let - = - (+): ( ) - (+) Does addition work? (+2) + ( -) Result: Two s Complement Numbers 4
Two s Complement Only one representation for One more negative number than positive number Fixed width format for both pos. & neg. numbers -4-5 -3-6 -2-7 - -8 + +7 + +6 +2 +3 +4 +5 + = + 3 = - 3-4
Negating in Two s Complement Flip bits & Add Negate () 2 (+2) Negate () 2 (-2) -4-5 -3-6 -2-7 - -8 + +7 + +6 +2 +3 +4 +5 42
Addition in Two s Complement (+2) + (+4) (-2) + (-4) (+2) + ( -4) ( -2) + (+4) 43
Subtraction in Two s Complement A - B = A + (-B) = A + B + - - - 44
Overflows in Two s Complement Add two positive numbers but get a negative number or two negative numbers but get a positive number -3-4 -5-6 -7 - -2-8 + +7 + +6 +2 +5 +3 +4-3 -4-5 -6-7 -2 - -8 + +7 + +6 +2 +5 +3 +4 5 + 3 = -8-7 - 2 = +7 45
Overflow Detection in Two s Complement 5-7 3-2 -8 7 Overflow Overflow 5-3 2-5 7-8 No overflow No overflow 46
Adder/Subtractor A 3 B 3 A 2 B 2 A B A B A B A B A B A B CO + CI CO + CI CO + CI CO + CI S S S S S 3 S 2 S S Overflow A - B = A + (-B) = A + B + 47
Converting Decimal to Two s Complement Convert absolute value to unsigned binary, then fixed width, then negate if necessary Convert (-9) to 6-bit Two s Complement Convert (9) to 6-bit Two s Complement 48
Converting Two s Complement to Decimal If Positive, convert as normal; If Negative, negate then convert. Convert () 2 to Decimal Convert () 2 to Decimal 49
Sign Extension To convert from N-bit to M-bit Two s Complement (N<M), simply duplicate sign bit: Convert () 2 to 8-bit Two s Complement Convert () 2 to 8-bit Two s Complement 5
Solving Complex Problems Many problems too complex to build as one system Replace with communicating sub-circuits Timer ST TS TL Traffic Light Controller Design process: Understand the problem Break problem into subsystems, identifying connections Design individual subsystems. 5
Complex Problem Example Design a digital clock, which can Display the seconds, minutes and hours Have three inputs Increment hour Increment minute Reset seconds 52
Complex Problem Example (cont.) 53
Complex Problem Example (cont.) 54
Complex Problem Example (cont.) 55