Computer Architecture: Nguyễn Trí Thành
Computer Architecture: Nguyễn Trí Thành
9/12/2010 1
Fundamentals
9/12/2010 2
Boolean
Boolean Algebra
Algebra
9/12/2010 3
Digital Representation
5V 5V 1 1
0V 0V 0 0
9/12/2010 4
Analog Voltage Waveform Digital Voltage Waveform
Digital Processing
Combine “1”s and “0”s in different ways
To generate more “1”s and “0”s
This is finally what a computer really does
Need a well defined mechanism
Ease design & development of circuits
Boolean Algebra
Mathematical framework for processing “1”s & “0”s
Based on simple, scalable primitive operations
Easy to realize using basic hardware components
Scales to reason about complex operations
Leads to information processing
When combined with suitable interpretations
9/12/2010 5
Axioms of Boolean Algebra
Two Boolean constants
“1” or “true”
“0” or “false”
Boolean variables
An unknown Boolean value
Can be “1” or “0” (but not both at the same time)
3 primary Operators
NOT (unary operator)
AND (Binary operator)
9/12/2010 OR (Binary operator) 6
Truth Table
9/12/2010 7
NOT Operator
A A
0 1
1 0
9/12/2010 8
AND Operator
AND operator
Binary operator: Uses 2 operands
Result is a “1” only if both operands are “1”
AND operation is represented as AB or A•B
Where A and B are two Boolean variables
AND is described by the following truth table:
A B A••B
0 0 0
0 1 0
1 0 0
1 1 1
9/12/2010 9
OR Operator
OR operator
Binary operator: Uses 2 operands
Result is a “1” if any one of the operand is a “1”
OR operation is represented as A+B
Where A and B are two Boolean variables
OR is described by the following truth table:
A B A+B
0 0 0
0 1 1
1 0 1
1 1 1
9/12/2010 10
Exercises
9/12/2010 11
Exercises
9/12/2010 12
Boolean Expression
Combination of operands & operators
Examples
A+A
(A+B)•1
(A•0)+(B•0)
A+B+C+D
A•1+B•1
What is the results of the above expressions if A=1, B=0
Operator precedence
Inner most parentheses
NOT
AND
OR
9/12/2010 13
Boolean Equations & Truth
Tables
Illustrate Truth Table for A+B•C
A B C B•C A+B•C
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
9/12/2010 1 1 1 14
Laws of Boolean Algebra
For a Boolean variable A
Identity law
A+0=A
A•1=A
Zero and One laws
A+1=1
A•0=0
Inverse laws
A+A=1
A•A=0
9/12/2010 15
Laws of Boolean Algebra
(Contd.)
Given Boolean variables A, B, & C
Commutative laws
A+B=B+A
A•B=B•A
Associative laws
A + (B + C) = (A + B) + C
A • (B • C) = (A • B) • C
Distributive laws
A • (B + C) = (A • B) + (A • C)
DeMorgan’s laws
(A • B) = A + B
9/12/2010 (A+B) = A • B 16
Verification of Laws (1)
A A+0
0 0
1 1
9/12/2010 17
Verification of Laws (2)
Distributive law
A • (B + C) = (A • B) + (B • C)
A B C A•(B+C) (A•B)+(B•C)
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
9/12/2010 18
More Boolean Operators
Other commonly used Boolean Operators
Convenient when implementing logic operations using
electronic components
NAND
(A•B)
NOR
(A+B)
9/12/2010 19
English to Logic Conversion
Straightforward strategy
Use common sense
Identify independent clauses
Look for “and” & “or” clauses in sentences
Identify primary inputs
Work logic out for each independent clause
Connect them back together
Optimize the final equation
We will not deal with optimizations in this course
Verify using Truth Table
9/12/2010 20
Example 1
Output is 1 only if the two primary inputs are
zero
Let the primary inputs be A and B
Output is 1 in the following cases
A=0 & B=0 => A • B
The above equation can be rewritten as:
(A+B)
9/12/2010 21
Example 2
Output is 1 only if the two primary inputs are
different
Let the inputs be A and B
Output is 1 in the following cases
A=0, B=1 => A•B
A=1, B=0 => A•B
Combining the above two cases gives:
(A•B)+(A•B)
This operation is called Exclusive-OR or XOR
It is frequently used
Represented as A + B
9/12/2010 22
Truth Table for XOR
A B A+B
0 0 0
0 1 1
1 0 1
1 1 0
9/12/2010 23
Digital
Digital Logic
Logic Circuits
Circuits
9/12/2010 24
Electronics for Boolean
Algebra
Interconnected set of Transistors called Circuits
Transistors are Electronic Switches
Turn “On” or “Off”
Depending on input voltages
Used to implement Boolean expressions Transistor
A (A•B)+C
B
9/12/2010 25
Logic Gates
9/12/2010 26
Basic Gates
Corresponding to basic operations in Boolean
Algebra
NOT Gate Output
A A
XOR
A A⊕
⊕B
B
9/12/2010 28
Equations to Circuits
Convert Boolean equations to Logic Circuits
Logic circuits drawn on paper are often also called
Schematics
Straightforward process
Convert each operator to a Logic Gate
Suitably connect inputs and output
Pay attention to crossing lines versus connected lines
A A A
B B A
A A•B
A
B A•B•C•D B A•B•C•D
C
C
D
D C•D
Solution
O = (A•C)+(B•C)
A A•C
O=(A•C)+(B•C)
C
B
B•C
S1 S2 S3 O
0 0 0 A
Draw
0 0 1 B the
0 1 0 C circuit?
0 1 1 D
1 0 0 E
9/12/2010 33
Logic Circuit for Selector
A
C O
9/12/2010
S1 S2 S3 34
Multiplexer (Mux)
•••
Multiplexer
Such that 2k >= N
1 output line •••
K select
lines
9/12/2010 35
De-Multiplexer (DeMux)
•••
NxK
1 Input N Outputs
•••
De-Multiplexer
9/12/2010 36
De-Multiplexer Logic Circuit
1 X 4 De-Multiplexer
A•S1•S2
A•S1•S2
A•S1•S2
A•S1•S2
9/12/2010 S1 S2 37
Timing
Gates take time to work
Outputs don’t stabilize for some time
Stabilization time is usually in nanoseconds
Gate delays compound in circuits
Final output is not ready until all gates are stable
Propagation delay
Time taken for changes at the input to propagate to
output
Typically, the longest path from input to output
This is often called the “critical path” in a circuit
9/12/2010 38
Example
A A•B
2ns
B A•B•C•D
2ns
C
2ns
D C•D
9/12/2010 39
Timing Diagrams
9/12/2010 40
Timing Diagram Example
A A•B
2ns
B A•B•C
2ns
C
A.B
A.B.C
9/12/2010 42
Clock Example
Clocked I/O
Minimum clock period = 4ns
Maximum Frequency = 1/4ns = 250 MHz
A
2ns A•B•C
Clock 2ns
2ns
B
4 ns
9/12/2010 43
Triggering
Clocks transitions are used in different ways
Level triggering
When clock is in a given state
Edge triggering
Raising edge triggered
When the clock is in transition from 0 → 1
Falling edge triggered
When the clock is in transition from 1 → 0
Rising Falling
edge Clock edge
9/12/2010 44
Latches
Q
R
S R Q
0 0 No change
1 0 1
0 1 0
9/12/2010 1 1 Unstable 46
Clocked S-R Latch
S Q
Clock /
Enable
Q
R
9/12/2010 47
D-Latch
D Q
Clock /
Enable
Q
9/12/2010 48
D-Flip Flop
An edge triggered D-Latch is a D-Flip Flop
D Q
Clock
CK CK
D Q D Q
CK CK
9/12/2010 51
Sequential Logic Circuits
9/12/2010 53
Circuit to read a Bit
4 X 1 Multiplexer
9/12/2010 54
Circuit to write a Bit
Given 4 Flip Flops, develop a logic circuit to
select and change data in a given Flip Flop.
Input D Q
1X4 CK
D Q D Q D Q D Q
CK CK CK CK
Q0 Q1 Q2 Q3
9/12/2010 56
Reading & Writing Words
A fixed number of D-Flip Flops
I1 I2 I3
S0
O1 O2 O3
CLK
9/12/2010 RD 57
(1=Read, 0=Write)
Random Access Memory
(RAM)
RAM is the common form of main memory that is
used to store data and programs in modern
computers.
It is typically designed as a collection of flip flops
as shown in the previous slide
However fabrication technology is different to
reduce cost and improve transistor densities
Terminology:
Lines that carry input or output data are referred to
as data lines or data bus
The select lines associated with the Mux and
DeMux are called the address bus
The selection data is called address
In programming terminology it is called a pointer or a
9/12/2010 58
reference.
Number
Number Representation
Representation
9/12/2010 59
Its all 1s and 0s!
9/12/2010 60
Magic of Interpretation
The key is interpretation
Different sequence of 1s and 0s are assumed to
convey different meanings to different devices
Example: What does bits 1011 imply
Letter “A” on the keyboard
A number (11) to the CPU
Color red to the video card
Music note F# to the sound card
Sector number 11 to the hard disk drive
9/12/2010 61
Standards
Interpretations can be problematic
Different interpretations can lead to confusion
Problems with portability & interoperability
Solution: Standards
Standard ways to represent data
Different data types need different standards
Integers have different representation versus floating point
numbers
Numbers have different representation than characters and
strings
Depending on needs
9/12/2010
Efficiency of hardware implementation 62
Integer representation
Example binary
22 21 20
number: 1 0 1
9/12/2010 66
Unsigned Decimal to Unsigned
Binary Conversion
Performed through successive division by 2
Until quotient becomes 1
Writing remainders in reverse order
Example convert 510 to binary
Remainder
2 5
2 2 1
1 0
510 = 1012
9/12/2010 67
Example 2
Convert 12 to binary
2 12
2 6 0
2 3 0
1 1
1210 = 11002
9/12/2010 68
Fixed Size Representation
9/12/2010 69
Example
Represent 1210 as a 8-bit binary number
Solution:
Convert 1210 to binary which is 11002
Now 11002 as 8-bit number = 000011002
Padding 4 leading 0s to make it 8-bits wide
Represent 39 as a 5-bit binary number
Solution:
Convert 3910 to binary which is 1001112
Cannot fit 6-bits into 5-bits position!
Drop left-most digits as necessary
Result = 001112
9/12/2010 70
Binary to Decimal Conversion
Multiply by powers of 2 and add
Powers of 2 increase from left to right!
Example: Convert 1102 to decimal
22 21 20
1 1 0
Decimal = (1*22) + (1*21) + (0*20)
= 4 + 2 + 0 =6
1102 = 610
9/12/2010 71
Binary to Decimal Conversion
(Example 2)
9/12/2010 73
Range of numbers
Example if K = 8
With 8 bits, maximum number = 28-1 = 255
9/12/2010 74
Octal representation
1 2
8310 = 1238
9/12/2010 76
Octal to Decimal Conversion
9/12/2010 77
Octal to Binary Conversion
9/12/2010 79
Hexadecimal Representation
9/12/2010 80
Motivation for Hex
Primarily for display purposes
Originally intended for 7-segment displays
9/12/2010 81
Decimal to Hex Conversion
2 D (1310)
73410 = 2DE16
9/12/2010 82
Hex to Decimal Conversion
9/12/2010 83
Hexal to Binary Conversion
9/12/2010 85
Table of Equivalent Values
Computer Architecture
9/12/2010 87
Review of Addition
9/12/2010 88
Example
1 7 7
9/12/2010 89
Binary Addition
1 1 0 0 0
9/12/2010 91
Logic Circuit to add 2 Bits: Half
Adder
Truth table for sum &
carry bits are as A B Sum Cout
follows. 0 0 0 0
Given the truth table 1 0 1 0
Sum = A + B 0 1 1 0
Cout = A • B 1 1 0 1
B
Half Sum = A + B
Adder Carry = A • B
9/12/2010 92
Logic Circuit to add 3 bits: Full
Adder
Refer to truth table shown earlier
Sum = A + B + C
Carry = (A•B) + (B•C) + (C•A)
A A⊕ B
B Sum
C
FullAB
BC Carry
Adder
CA
9/12/2010 93
Ripple Carry Adder
9/12/2010 96
Need for Signed
Representations
Unsigned representations cannot represent
negative numbers
Such as: -2, -51 etc.
However negative numbers are frequently
used
Need a representation for positive & negative
numbers – that is, signed numbers
9/12/2010 97
Standard Signed
Representations
Signed numbers are represented using 3
different standards
Sign-bit Magnitude (SBM) representation
1’s Complement Representation
2’s Complement Representation
This is the representation that is used by computers
today!
9/12/2010 98
Strategy for Signed
Representation
General strategy is as follows:
All representations assume fixed size
8, 16, 32, or 64 bits operated on as a single unit-word
Given n bits (unsigned range: 0 to 2n-1)
Break the range into two halves
One half represents positive numbers
0 to 2n-1-1 corresponds to positive numbers
Decimal value: 0 to 2n-1-1
Another half represents negative numbers
2n-1 to 2n-1 corresponds to negative numbers
9/12/2010
Decimal range: -2n-1 to -1 99
Sign-Bit Magnitude (SBM)
Representation
9/12/2010 105
1’s Complement
9/12/2010 106
2’s Complement
Add 12
9/12/2010 108
2’s Complement Example
Binary to decimal
Invert all bits
(positive number)
Add 1
Binary to decimal
(Negative number)
9/12/2010 110
Example
Convert 8-bit 2’s complement to decimal
Case 1: 000010102
Sign bit is 0 indicating positive number
Simply convert binary to decimal to get 1010
Case 2: 111110102
Sign bit is 1 indicating negative number
Step 1: Invert all bits to get 000001012
9/12/2010
Note the negative sign on decimal number! 111
Subtraction using 2’s
Complement
Perform 510 – 310 using 4-bit 2’s complement
510 – 310 = 510 + (-310)
510 = 01012 INV
-310 = (00112 →11002 + 12) = 11012
510 + (-310) = 01012 + 11012 = 00102
00102 = 210
9/12/2010 112
Subtraction using 2’s
Complement
Perform 210 – 410 using 4-bit 2’s complement
210 – 410 = 210 + (-410)
210 = 00102 INV
-410 = (01002 →10112 + 12) = 11002
210 + (-410) = 00102 + 11002 = 11102
11102 is 2’s complement result and because
it is negative (sign bit is 1) it needs to be
converted
INV
11102 → 00012 + 12 = 00102 = -210
9/12/2010 113
Subtraction Circuit
A A+B
B Sum
C
FullAB
BC Carry
Adder
CA
9/12/2010 115
Ripple Carry Adder
A2 A1 A0
FA2 B2 FA1 B1 FA0 B0
1
C2 C1 C0
9/12/2010 117
Add/Subtract Circuit
Circuit to Add or Subtract two 3-bit 2’s complement
number
The XOR gate inverts the bits
only when control input is 1
B2 B1
B0
A2 A1 A0
FA2 FA1 FA0
C3 C2 C1 C0 Control Input
0 = Add
9/12/2010 118
1 = Subtract
ALU
ALU &
& CPU
CPU
9/12/2010 119
Introducing ALU
Multiplexer
A Out
010
B
011
Sum
Cin Full Adder 100
S0
This approach can be
S1 extended to include other
S2 operations such as
subtraction.
9/12/2010
Cout 122
n-bit ALU
n-bit ALU
Repeat 1-bit ALU n times
All 1-bit ALUs get the same selection lines
Carry out (Cout)from one stage is wired to carry-in
of next state
Similar to how a ripple carry adder (or subtraction
circuit) is wired
Performs operation on n-bits simultaneously
9/12/2010 123
Example 4-bit ALU
Selection lines simply renamed to
operation code (OPCode) lines
A3 B3 A2 B2 A1 B1 A0 B0
OP0
OP1
OP2
O4 O3 O2 O1 O0 Add/Sub
9/12/2010 124
ALU Notation
An ALU is denoted using the following graphical
notation.
Operand1 Operand2
(A) (B)
Indicates number of 8 8
bits on each line.
The size of each
operand is typically OP Code Result
the same and 010 A•B
ALU
corresponds to the 110 A+B
word size. 9 210 A
4
310 B
… …
Operation Result
Selection
9/12/2010 (OP Code) 125
Thought Experiment
Solution: Registers!
The fundamental storage units.
9/12/2010 126
Registers Revisited
Fixed number of D-Flip Flops to form a Word
Corresponding to size of ALU operands!
Operate as a single unit
Store/Read n-bits at a time
D0 D1 D2 D3
D Q D Q D Q D Q
CK CK CK CK
Q0 Q1 Q2 Q3
9/12/2010 127
Selecting a Register
8 8
Register 4 11
2
Set of registers is
called a Register File Register
9/12/2010
Select 128
Selecting 2 Registers
ALU needs 2 operands to work!
How to select 2 registers from a Register File?
8 8
00 M 00 M
Register 8 U 8
01 01 U
8 X 8 X
File 10 10
8 1 8 2
11 11
2 8 2 8
Register Register
Select 1 Select 2
9/12/2010
Operand1 Operand2
129
ALU with Inputs
Mux2
Register
8
File
Mux1
(4)
8 8
What do we do with
ALU results or output
from
2 the ALU?2
4 8
Results
DeMux
8
Register 2 8
01
8 Result from ALU
Register 3 10
8
Register 4 11
2
9/12/2010
Select Inputs 131
Handling ALU Output
A
Mux2
8
Each device has its own Clock
Register 8
DeMux
slection logic typically 8
8
specified using a truth File
B
Mux1
table! 8
8 8 (4)
Flags
ALU
2
2 2
4 8 Operation Result
010 A•B
110 A+B
210 A
Operation Operand1 Operand2 Destination
9/12/2010 132
Data path
9/12/2010 133
ALU Notation
An ALU is denoted using the following graphical
notation.
Operand1 Operand2
(A) (B)
8 8
Indicates number of
bits on each line
ALU
Operation Result
9
010 A•B
4
110 A+B
210 A
Operation Result
9/12/2010 134
Selecting 2 Registers
ALU needs 2 operands to work!
How to select 2 registers from a Register File?
8 8
00 M 00 M
Register 8 U 8
01 01 U
8 X 8 X
File 10 10
8 1 8 2
11 11
2 8 2 8
Register Register
Select 1 Select 2
9/12/2010
Operand1 Operand2
135
ALU with Inputs
Mux2
Register
8
File
Mux1
(4)
8 8
What do we do with
ALU results or output
from
2 the ALU?2
4 8
Results
DeMux
8
Register 2 8
01
8 Result from ALU
Register 3 10
8
Register 4 11
2
9/12/2010
Select Inputs 137
Handling ALU Output
A
Mux2
Each device has its own
selection logic typically Clock
32 Register
DeMux
specified using a truth table!
(Note that this is not a single
Mux but 8, 4x1 Muxs) File
B
Mux1
8 8 (4)
Flags
ALU
2
2 2
4 8
Operation Result
010 A•B
110 A+B
Operation Operand1 Operand2 Destination
9/12/2010 210 A 138
Data path
The ALU and associated components
constitute the Data Path
Includes Registers, Multiplexers and any other
device associated with ALU operations
All operands are typically the same size
Register sizes match with size of operand
Size of operands are associated with CPU
32-bit processor (ALU uses 32-bit operands)
64-bit processor (ALU uses 64-bit operands)
9/12/2010 139
Handling Constant Values
Mux2
the 8 bits from the instruction depending on the
operation. If the operation is to store a constant
Register
DeMux
8 then the ALU8 simply passes the constant bits as
8
its output. The constant bits are then pushed to
File
Bthe DeMux and are finally stored in the register
Mux1
0
Mux 1
8 indicated by destination
(4)
8
Flags
This is not a
ALU special device
4 but just a 4 Where do4 these
8 notation to show bits actually
the lines are
4
4 being combined
come from?
4
9/12/2010 142
Operation Operand1 Operand2 Destination
Where do instructions come from?
16
(16 x 256)
Bi-directional
Data Bus
9/12/2010 146
Using Memory
00 01 1X 8 Memory
De-Mux (8 x 256)
Data Bus
S1 S0 1
Instruction Byte Least significant address bit changes between
/ Data Select 0 and 1 for each consecutive address.
When the clock is low (S1=0) the De-Mux places the bytes read from Memory into Byte1
and Byte2 depending on S0 (so S0, the least significant bit from the address bus) which
switches between 0 and 1 when clock is low to fetch two bytes from memory.. When the
clock is high (S1=1), the De-Mux ignores s0 and places the data read from memory into 151
9/12/2010
Byte 3.
Design Considerations
Address needs to be generated to fetch
instructions from memory
Typically one of the registers hardwired to the data path
is reused for this purpose
This register is typically called
Program Counter
Instruction Pointer
In the sample data path Reg3 is being used as the
instruction pointer
Using a register eases handling conditional and
unconditional branching in programs
These are used to achieve if and looping constructs in
high level languages
Also used for managing
subprograms/methods/functions in a high level
9/12/2010
languages 152
Adding Memory to Data path
Register
Latch
8 File (3) *
Mux1
Mux2
Reg3 + R W
8 Flags 8
8
32 110 Address EN RD
8 A B 8 (8 X 256)
Delay to generate 2 clock
0 1
Mux
pulses from 1. This causes
8 R
Mux
ALU 2 2 0 1 * 110 to be added to Reg3 two
times for each clock pulse.
6 8
8
2 8 Data
4
* W
Bus
OP Code Operand1 Operand2 Destination Data Latch
*
00
DeMux
01 8
9/12/2010 11 153
Branching
9/12/2010 154
Branching Circuit
8
9/12/2010 156
Conditional Branching Circuit
8
9/12/2010 157