Computer Architecture Course Overview
Computer Architecture Course Overview
F342)
Motivation and Introduction
Automatic & General Purpose Computing
2
Class Schedule
• Google Classroom (Class materials)
• Moodle or Quanta (Labs)
Reference Books:
(R1) Digital Design: With a Introduction to the Verilog HDL by M. Morris Mano &
Michael D. Ciletti
(R2) Verilog HDL: A Guide to Digital Design and Synthesis by Samir Palnitkar.
(R3) Computer Organisation & Architecture: Designing for performance by William
Stallings.
7
What is the meaning of Computable?
∞
High-level Synthesis
12
Can we execute other algorithms on this processor?
Special-purpose/Dedicated Processor
MinMax Processor
10 -∞
Input: set of n nos stored in A
Output: Min & Max Stop 0 1
Min = ∞ = LoadMax
MinMax Processor
Necessity of General-purpose processor 13
• Is there an Algorithm which will execute or simulate other Algorithms?
• The processor executes any algorithms
• Programmable
• Turing Model
• Is there any limitation of such an Algorithm?
• Halting problem: Can we have an Algorithm which takes other Algorithm as input and decides
that whether given input Algorithm will halt/stop or not, in general?
• Consider [*] such Algo. exists A(P, D). Another Algo. B(X): loop-forever if A (X, X) = “Halt”
else Halt. Next use B(B), it is unable to decide the answer. A(P, I) doesn’t exist.
• Used Self-referential structure for proof as did by Gödel [*]
• What kind of Algorithm do we need for making the processor general purpose
or programmable?
• Fetch-and-Execute Algorithm Fetch-and-Execute Processor
• Stored program (?) [*]
• Generalized Datapaths ALU/FU
• Generalized Functional Unit Datapath All possible
MEM Operations
• Proposed by Jhon von Neumann [*]
Controller
[*] An URL is embedded.
Find the minimum and maximum number from a set 14
.text
main:
la $a0, array
lw $a1, array_size
lw $t2, maxE # max
lw $t3, minE # min
Find the minimum and maximum number from a 15
set of numbers
loop_array:
beq $a1, $zero, print_and_exit
lw $t0, ($a0)
bge $t0, $t3, not_min # if (current_element >= current_min) {don't modify min}
move $t3, $t0
not_min:
ble $t0, $t2, not_max # if (current_element <= current_max) {don't modify max}
move $t2, $t0
not_max:
addi $a1, $a1, -1
addi $a0, $a0, 4
j loop_array
Find the minimum and maximum number from a 16
set of numbers
# print maximum
print_and_exit: li $v0, 4
# print minimum la $a0, array_min
li $v0, 4 #for string syscall
la $a0, array_max
syscall li $v0, 1
move $a0, $t3
li $v0, 1 #for number syscall
move $a0, $t2
syscall # exit
li $v0, 10
syscall
Find the minimum and maximum number from a 17
set of numbers
int main()
{
int arr[10] = {1, 2, -8, 0, 23, 11, -10};
int N = 10, i;
int minE = 9999, maxE = -9999;
// Traverse the given array
for (i = 0; i < N; i++) {
// If current element is smaller than minE then update it
if (arr[i] < minE) {
minE = arr[i];
}
// If current element is greater than maxE then update it
if (arr[i] > maxE) {
maxE = arr[i];
} } printf("The minimum element is %d", minE); printf("\n");
printf("The maximum element is %d", maxE);
return 0;
}
How does the General-purpose micro-processor 18
Algorithm
Program/Language
Runtime system How do we ensure problems are solved by
(OS, VM, MM) electrons?
ISA (Architecture)
Logic
Devices
Yale Patt, “Requirements, Bottlenecks, and Good Fortune- Agents for Microprocessor Evolution,” Proc. of the IEEE, VOL. 89, NO. 11, NOV. 2001
20
History of computation
• Homework
• Go through the material on Gdrive, shared with you.
• [Link]
7YzjX8JfxbabKT8puzW
• Go through the order mentioned in the xlsx file
• Will ask the question in the next class
24
Changes in Computation
• Manual
• Mechanical
• gears, chains, pulleys, and steam power
• Punch cards
• Electro-mechanical
• switches, relays
• Electrical
• plugboards, vaccum tubes
• later came DRUM memory, core memory, transistors and so on ...
[Link]
25
Computation on 2004
• 64-bit Itanium processor developed by
Intel
• 1.7 billion transistors
• 1.7 GHz, issue up to 8 instructions per
cycle
• 26 MByte of cache
• In ~30 years, about 100,000 fold growth
in transistor count and performance
26
What kind of
growth is it?
29
Future is about
Quantum Computing
30
Quantum Computing
• [Link]
• [Link]
• Google wants to build a useful quantum computer by 2029
• [Link]
decade-commercial-useful-qubits-quantum-transistor
• Quantum Computing: Untangling the Hype (Talk at The Royal Institution)
• [Link]
33
Summary
• Motivation for automated Computation
• Dedicated processor Vs. General-purpose processor
• Limitation of Algorithm
• Building block of a program
• Steps to solve a problem by a computer
• Changes in Computation
Quiz
Computer Architecture (CS F342)
Design, Analysis, Optimization and Execution of
Instructions
2
Algorithm
Program/Language
Runtime system How do we ensure problems are solved by
(OS, VM, MM) electrons?
ISA (Architecture)
Logic
Devices
Yale Patt, “Requirements, Bottlenecks, and Good Fortune- Agents for Microprocessor Evolution,” Proc. of the IEEE, VOL. 89, NO. 11, NOV. 2001
6
Book: Ch-4 of Introduction to Computing Systems from bits & gates to c/c++ & beyond by Yale Patt & Sanjay Patel
8
Simple CPU
• What could be the basic instructions?
• Consider we have a memory unit only; but no general purpose registers
• Addition
• 1’s complement
• Subtraction: 2’s complement Addition
• Multiplication: repeated addition
• Division: repeated subtraction
• Comparison can be done using subtraction
• Shift: multiplication or division
• AND & Complement: NAND (universal gate)
• Jump to some address if Acc is zero
• Data movement between the Accumulator and Memory
9
Simple CPU
• Is there any differences among the instructions?
• Yes, all instructions are same except CMP (in terms of representations: op-code and
addrs)
• We have 6 unique instructions; how does one identify them uniquely inside a
computer?
MAR
Instructions Meaning PC IR MBR
LD <addrs> Acc M[addrs]
ST <addrs> M[addrs] Acc ACC ALU
ADD <addrs> Acc Acc + M[addrs] [+,&]
CMP Acc 𝐴𝑐𝑐 Mem
Data
AND <addrs> Acc Acc & M[addrs] (M)
JMPZ <addrs> PC addrs if Acc = 0
10100101_10100011_11111100_00111010
3 2 1 0
16
Simple memory model
• In the case of a 32-bit word length, natural word boundaries occur at addresses 0, 4, 8, . . . ,
• We say that the word locations have aligned addresses if they begin at a byte address that is a
multiple of the number of bytes in a word.
• The number of bytes in a word is a power of 2
• There is no fundamental reason why words cannot begin at an arbitrary byte address.
• In that case, words are said to have unaligned addresses. But, the most common case is to use
aligned addresses, which makes accessing of memory operands more efficient
17
Simple memory model
A
Data for the
B program
C
21
Datapath for simple CPU
IR MAR
PC MDR
ACC
Data-path
ALU
[+,&] Mem
Temp
(M)
Data
22
The Instruction Cycle (NOT the clock cycle)
Each of these steps is under the direction of the control unit. Time required to complete a step is
called machine cycle.
24
The Instruction Cycle (NOT the clock cycle)
• The DECODE phase examines the instruction in order to figure out what the
microarchitecture is being asked to do.
• How does one achieve that?
• Using a decoder unit
25
The Instruction Cycle (NOT the clock cycle)
• The EVALUATE ADDRESS phase computes the address of the memory location
that is needed to process the instruction.
• For an example, consider a scenario that the memory location of an instruction
can be obtained by sign-extending bits [12:0] of the instruction to 16 bits and
adding that value to the current contents of the PC. This calculation is performed
during the EVALUATE ADDRESS phase.
26
The Instruction Cycle (NOT the clock cycle)
• In the STORE RESULT phase, the result is written to its designated destination.
• The final phase of an instruction’s execution.
29
The Instruction Cycle (NOT the clock cycle)
• Once the instruction cycle has been completed, the control unit begins the
instruction cycle for the next instruction, starting from the top with the FETCH
phase.
• Since the PC was updated during the previous instruction cycle, it contains at this
point the address of the instruction stored in the next sequential memory location.
• Thus, the next sequential instruction is fetched next.
• Processing continues in this way until something breaks this sequential flow, or
the program finishes execution.
• It is worth nothing again that although the instruction cycle consists of six phases,
not all instructions require all six phases.
30
The Instruction Cycle (NOT the clock cycle)
32
Summary
• Building block of a program
• Simple CPU
• Simple Memory model
• Instruction cycle
• Control unit
• Datapath
Computer Architecture (CS
F342)
Design, Analysis, Execution and Optimization of
Instructions
Control unit
Paths in Processor Organization
2 • What kind of data-path is it?
• Shared bus
IR MAR
PC MDR
ACC
Data-path
Types of paths:
ALU • Address path
[+,&] Mem • Data path
Temp
(M) • Control path
Data
3 BUS: Pathway between different
components
• A set of electronic signal pathways that allows information and
signals to travel between components inside or outside of
computer
• Two types: Internal and External
• Internal bus connects different components inside the case: CPU,
Memory, all other components in mother board
• The external bus connects different external devices, peripherals,
expansion slots, I/O ports, drive connection to the rest of the
computer
• External bus is slower than the internal bus
4
Big picture of different components
Bus is a
collection
of wires
(can be
32 no. of
wires)
5
Imposing User Control on BUS
• All component shared the common bus
• What if more than one component want to access bus?
• Is it a data-read operation or data-write operation?
• Data-read operation can be allowed
• How about data-write operation?
• Event called data collision
• How does one control data collision event?
6
Imposing User Control on BUS
• How does one control data collision event?
• Insert a tristate buffer in the junction
• The tristate buffer has an input A, output Y, and enable E. When
the enable is TRUE, the tristate buffer acts as a simple buffer,
transferring the input value to the output. When the enable is
FALSE, the output is allowed to float (Z).
• The symbol Z indicates that a node is being driven neither HIGH nor
LOW. The node is said to be floating, high impedance, or high Z.
A typical misconception is that a floating or undriven node is the same as a logic 0. In reality, a
floating node might be 0, might be 1, or might be at some voltage in between, depending on
the history of the system.
Multiplexer
7
• Mux: Another combinational building block
• Routes one of its N data inputs to its one output, based on binary value of select
inputs
• 4 input mux needs 2 select inputs to indicate which input to route
through
• 8 input mux 3 select inputs
• N inputs log2(N) selects
• Like a railyard switch
Multiplexer
8
2⋅ 1
a3 i
d
b3 i0
s0
1
4- 2⋅ 1
a2 i
4 d
A bit
2x1 b2 i0
I0 4 s0
1
4 D C
B I1 2⋅ 1
a1 i
d
s0 b1 i0
s0
1
2⋅ 1
s0 a0 i
d
b0 0i
s0
1
s0
IR_In
IR
IR_Out
PC_In
PC
Data-path(BUS)
PC_Inr
PC_Out
Identify the control signals for all components
memRead MDR_In
10 0
IR_In MDR Why MuX?
IR 1
IR_Out memWrite
iDCD
PC_In Control signals for:
PC_Inr PC
MDR_Out IR are — IR_In & IR_Out
ACC_Zero ACC_Cmp PC are — PC_In, PC_Inr &
PC_Out
Sharedbus
ACC_In PC_Out
ACC ACC are — ACC_Cmp,
Mem ACC_Zero, ACC_In,
ALU_Add ACC_Out (M) ACC_Out
Temp is — Temp_Out
ALU Data is — Data_In
[+,&] Temp MAR is — MAR_In
MAR MDR are — MDR_In,
Temp_Out MDR_Out, memRead &
ALU_And MAR_In memWrite
ALU are — ALU_Add &
Data ALU_Add
18 control signals
Single bit
More than one bit
Data_In
Design of Control Unit (CU)
11
• We have unique op-code for each instruction
• What is the next step?
• Define the micro-operations of each stage of the instruction
cycle
Machine
Next
/ Present Operation Related Control Signals
state
state
Instruction fetch
T5 T0
Design of Control Unit (CU)
18
• Micro-operations of AND <addrs>:
T2
T3
LD ADD CMP AND
ST JMPZ
T4 T4 T4 T4 T4 T4
T5 T5 T5 T5 T5
T6 T6
T7 T7
21
CU’s Finite State Machine (FSM) Representation
Fetch-decode’s
FSM
LD’s FSM ADD’s FSM ST’s FSM JMPZ’s FSM CMP’s FSM AND’s FSM
Design of Control Unit (CU)
22 • We have defined the micro-operations for our 6 instructions
• What is the next step?
• Generate the states from T0 to T7
• Encode the state words (T0 to T7) to (decimal) number 0 to 7
• How does one generate the states from 0 to 7?
• One can use a counter
• How does one decide the counter size?
• Find the maximum state considering all the instructions, here it is
8 (T0 to T7) for instr. ADD and AND, and therefore counter size is 3-
bits
• How does one uniquely identify the states or decode the state?
• Using a decoder and what could be the decoder size?
Design of Control Unit (CU)
23
Homework
PC_In
PC_Inr PC
MDR_Out
ACC_Zero ACC_Cmp
PC_Out
Sharedbus
ACC_In
ACC
Mem IR_Out
ALU_Add ACC_Out (M) IR_In
ALU
[+,&] Temp
MAR
Temp_Out Control
ALU_And Unit
MAR_In
Data
What kind of CU is it?
How are we generating the control signals?
28
• Step-by-step
• Not generating all the control signals at the
instance (single-cycle approach)
• It is called multi-cycle approach/method
• Here datapath is shared among the components
• Optimizing the resource: bus
• Other example can be found for MIPS processor
Hardwired Control
29
Book: Appendix-C of Introduction to computing systems from bits & gates to c/c++ & beyond by Yale Patt & Sanjay Patel
P&H-COD-[MIPS]-Appendix-D: Mapping Control to Hardware, Section: Implementing the next-State Function with a
Sequencer
Microprogrammed Control
31
IR 1 0
MUX
Microprogram
sequencer: all units,
CMAR except control memory.
Control Memory
Other CF: Control Field
Signals
ACC_Zero
M CF BA
M: Mode BA: Branch Address
Microprogrammed Control
34
• Advantages:
• We have a design flexibility to upgradation of control
memory
• More compact design as compared to hardwired control
unit
• Disadvantages:
• Slow in execution as compared to Hardwired control unit
Microprogrammed Control Unit
36
Clock
Instructions
cycle
LD 6
ADD 8
ST 6
JMPZ 6
CMP 5
AND 8
Clock period
42 T0 Start State
PC_Out
MAR_IN
T1 Maximum time taken
by the state(s) can
T2 decide the clock
periods (T)
T3
LD ADD CMP AND
ST JMPZ
T4 T4 T4 T4 T4 T4
T5 T5 T5 T5 T5
T6 T6
T7 T7
Performance Analysis
43
Computer Organization and Design: The Hardware Software Interface MIPS Edition by David A. Patterson and John
L. Hennessy, Chapter-2.
6
Microprocessor without Interlocked Pipelined Stage
• MIPS processor
• Instruction Set Architecture (ISA)
• R-type
• I-type
• J-type
OP: Basic operation of the instruction, traditionally called the opcode.
rs: The first register source operand.
rt: The second register source operand.
rd: The register destination operand. It gets the result of the operation.
shamt: Shift amount.
funct: Function. This field, often called the function code, selects the specific variant
of the operation in the op field.
Computer Organization and Design: The Hardware Software Interface MIPS Edition by David A. Patterson and John
L. Hennessy, Chapter-2.
7
MIPS Addressing mode
Immediate addressing Register addressing
Base addressing
PC-relative addressing
Pseudodirect addressing
J 200
Computer Organization and Design: The Hardware Software Interface MIPS Edition by David A. Patterson and John
L. Hennessy, Chapter-2.
8
MIPS Operands
Computer Organization and Design: The Hardware Software Interface MIPS Edition by David A. Patterson and John
L. Hennessy, Chapter-2.
MIPS assembly language
9
Computer Organization and Design: The Hardware Software Interface MIPS Edition by David A. Patterson and John
L. Hennessy, Chapter-2.
10
A few instructions
Can we write a MIPS program for generating 7-Fibonacci numbers, using these instructions, considering
that first two Fibonacci numbers are stored at memory location ‘a’ & ‘b’?
Computer Organization and Design: The Hardware Software Interface MIPS Edition by David A. Patterson and John
L. Hennessy, Chapter-2.
11
Instruction format for MIPS-based processor
op rs rt rd shamt funct
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0)
+
4 Why 4?
Read address
PC
Instruction
Instruction Memory
IM[PC]; PC = PC + 4
14
Analysis of data-path for R-type instruction
• ADD $S1, $S2, $S3 //$S1 $S2 + $S3
op rs rt rd shamt funct
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0)
25:21 Read Read
register 1 data 1
op rs rt offset
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0) MemWrite
Write
Shift
by 2-
bits
? ALU Offset indicates
register number of
Read 31:0 instructions
Write data 2 ALUControl
data Left shift by 2-bits
ALUDecoder
RegWrite Sign to align the
15:0
Extn. instruction
ALUOp = (01)2 boundary
Analysis of data-path I-type instruction
19 • ADDI $S1, $S2, -12 //$S1 $S2 + (-12)
op rs rt Immediate
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0)
Instruction Read
register 2
+
20:16 Write ALU
register
Read 31:0
Result Write data 2 ALUControl
data
RegWrite Sign
Extn. ALUDecoder
15:0
Immediate ALUOp = (00) 2
Analysis of data-path j-type instruction
20 • J addrs //PC PC[31:28]addrs[27:0]
op address
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0)
P Read
C address
Instruction
Instruction
Memory
? 25:0
31:28 +
4 << 2
27:0
Building Microprocessor
21
+
4 RegDst
MemWrite
ALUSrc
25:21
Read Read MemtoReg
Read Branch
register data
PC address
1 1
Address
20:16
Read Zero Data Read
Instruction 1
register memo data
0 M
0 2 ry
M U
Instruction M Write U ALU X
Memory U register X Write 0
15:11 X Read 1 data
1 Write data
data 2 SW ALUControl
MemRead
15:0 Result RegWrite Sign ALUDecod
Extn er
.
5:0 ALUOp
LW
Combined Fetch cycle, R, M, I, B and J-type data-path
24
0
M
U
X
+ 1
+ <<2
4 RegDst MemWrite
ALUSrc MemtoReg
0 25:21
M Read Read
Read Branch
U register data
PC address
1 1
Address
X 20:16
1 Read Zero Data Read
Instruction 1
register memo data
0 M
0 2 ry
M U
Jump Instruction M Write U ALU X
Memory 15:11 U register X Write 0
X Read 1 data
1 Write data
31:28 data 2 SW ALUControl
MemRead
25:0 15:0 Result RegWrite Sign ALUDecod
Extn er
.
27:0
<<2 5:0 ALUOp
LW
Combined Fetch cycle, R, M, I, B and J-type data-path and clock
25 0
M
U
X
+ 1
+ CLK <<2
CLK 4 MemWrite
RegDst
ALUSrc CLK MemtoReg
0 25:21 Read Read
M Read register 1 data 1 Branch
U PC address Address
X 20:16
1 Read Zero Data Read
Instruction register 2 1
memo data
0 M
0 ry
M U
Jump Instruction M Write U ALU X
Memory 15:11 U register X Write 0
X Read 1 data
1 Write data 2
CLK
Identify the control signals
26
• Jump
• RegDst
• RegWrite
• ALUSrc
• Branch
• ALUOp
• MemRead
• MemWrite
• MemtoReg
Control Unit
27
MemtoReg
MemWrite
𝑂𝑝𝑐𝑜𝑑𝑒31:26 Main Branch
Decoder Jump
ALUSrc
RegDst
RegWrite
ALUOP1:0
𝐹𝑢𝑛𝑐𝑡5:0 ALU
ALUControl2:0
Decoder
Generation of Controls: Main decoder truth table
28
Inputs to the control unit: op-code part [31:26] and ALUOp Meaning
funct part [5:0] of the instruction
00 add
01 subtract
10 Look at funct field
11 n/a
Output of the control unit:
Instr. Jump RegDst RegWrite ALUSrc Branch ALUOp1 ALUOp0 MemRead MemWrite MemtoReg
R-type 0 1 1 0 0 1 0 0 0 0
lw 0 0 1 1 0 0 0 1 0 1
sw 0 x 0 1 0 0 0 0 1 x
addi 0 0 1 1 0 0 0 0 0 0
B-type 0 x 0 0 1 0 1 0 0 x
J-type 1 x 0 x x x x 0 0 x
ALU Operations
29
35 0
M
U
X
+ 1
+ CLK <<2
CLK 4 MemWrite
RegDst
ALUSrc CLK MemtoReg
0 25:21 Read Read
M Read register 1 data 1 Branch
U PC address Address
X 𝒕𝒔𝒆𝒍𝒆𝒄𝒕 +
20:16
1 Read Zero Data Read
Instruction register 2 1
memo data
𝒕𝒑𝒄𝒒_𝒑𝒄 + 𝒕𝒎𝒆𝒎 + 𝒕0𝒎𝒖𝒙 M
0 𝒕𝑹𝑭𝒓𝒆𝒂𝒅 M 𝒕𝑨𝑳𝑼 ry
U
Jump Instruction M Write U ALU + 𝒕𝒎𝒆𝒎+ X
Memory 15:11 U register X Write 0
X Read 1 data
1 Write data 2 𝒕𝒎𝒖𝒙 +
31:28 data SWALUControl
MemRead
25:0 15:0 Result RegWrite Sign ALUDecod
Extn er
27:0 𝒕𝑹𝑭𝒔𝒆𝒕𝒖𝒑 5:0
.
<<2 ALUOp
LW
CLK
Performance analysis of Single-cycle
36 implementation
IR_Out memWrite
iDCD
PC_In
PC_Inr PC
MDR_Out
ACC_Zero ACC_Cmp Instructions Op- Meaning
PC_Out Cod
Sharedbus
ACC_In e
ACC LD <addrs> 000 Acc M[addrs]
Mem ADD 001 Acc Acc +
ALU_Add ACC_Out (M) <addrs> M[addrs]
ST <addrs> 010 M[addrs] Acc
ALU JMPZ 011 PC addrs if Acc = 0
<addrs>
[+,&] Temp
MAR CMP 100 Acc 𝐴𝑐𝑐
Temp_Out AND 101 Acc Acc &
<addrs> M[addrs]
ALU_And MAR_In
Data
Single bit
More than one bit
Data_In
3
How to make Single-Cycle SimpleCPU?
• Adder WR_EN
MemRead
• ALU
ACC
• Signed-extension unit Address
Data
ALU_Add
• Data memory
Memory
ALU
[+,&]
MemWrite
ALU_And
+ +/- Extn
7
Analysis of data-path for Fetch stage
Why 2 ?
+
2
Read address
PC
Instruction
Instruction Memory
8
Analysis of data-path for ADD instruction
• LD <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
MemRead
Data memory
9
Analysis of data-path for ADD instruction
• ST <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
ACC
Instruction[11:0]
Data memory
WriteMem
10
Analysis of data-path for ADD instruction
• ADD <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
ACC
Instruction[11:0] ADD
Data memory
ADD
11
Analysis of data-path for ADD instruction
• JMPZ <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
ACC_Zero
JMPZ
ACC
PC
Instruction Memory
11:0
15:0
+/- Extn
12
Analysis of data-path for CMP instruction
op
4-bits(15-12) 12-bits (11-0)
ACC
CMP: WR_EN
CMP
PC
Qs
Instruction Memory
13
Analysis of data-path for AND instruction
• AND <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
MemRead
AND: WR_EN
ACC
Instruction[11:0] AND
Data memory
AND
Combined data path for all instructions
14
op Addrs
MUX
4-bits(15-12) 12-bits (11-0)
MemRead
ACC
Read
IM data WR_EN
1
MemRed
MemWrite
ALU_Add
ALU_And
ACC_In [1:0]
𝑂𝑝𝑐𝑜𝑑𝑒15:12 Main LD
Decoder
AND
ADD
CMP
JMPZ
Generation of Controls: Main decoder truth table
17
Inputs to the control unit: op-code part [15:12]
Homework
Output of the control unit:
Instr. MemRead MemWrite ALU_Add ALU_And ACC_In1 ACC_In0 LD AND ADD CMP JMPZ
LD
ST
ADD
JMPZ
CMP
AND
18 Homework
• Design it using Verilog HDL and C++
• Design a test program (7 Fibonacci nos) and load it into Instr. Mem
• Add more instructions
• Register type
• How does one convert MIPS Single-cycle microprocessor’s datapath into
datapath like (shared-bus) SimpleCPU?
19
Summary
• SimpleCPU’s Single-Cycle Datapath & CU design
Computer Architecture (CS F342)
Design, Analysis, Execution and Optimization of
Instructions
Reduced Instruction Set Computer (RISC): MIPS
Multi-cycle Datapath and Control unit
2 Problems of Single-cycle Datapath Design
• Single-cycle design works well but inefficient design
• Clock length (worst-case delay) is same for all instructions
• It is not a balanced design
• CPI is 1
• Use more resources:
• Adder
• Memory
• Necessity of balanced datapath design technique by focusing
on common-case design & analysis principle
Balanced datapath design: Multi-cycle
3
approach
• Instruction execution can be broken down to smaller steps
• Is it similar to SimpleCPU (multi-cycle) approach?
6 0 Redundant
M
Adders
U
X
+ 1
+ CLK <<2
CLK 4 MemWrite
RegDst
ALUSrc CLK MemtoReg
0 25:21
M Read Read
Read Branch
U register data
PC address
1 1
Address
X 20:16
1 Read Zero Data Read
Instruction 1
register memo data
0 M
0 2 ry
M U
Jump Instruction M Write U ALU X
Memory 15:11 U register X Write 0
X Read 1 data
1 Write data
31:28 data 2 SWALUControl
MemRead
25:0 15:0 Result RegWrite Sign ALUDecod
Extn er
.
27:0
<<2 5:0 ALUOp
LW
CLK
Balanced datapath design: Multi-cycle
7
approach
PC+4
CLK
Address
PC
Instruction &
EN Data Memory Zero
PCWrite
WriteData ALU
4
ALUControl
ALUDecod
er
ALUOp
Balanced datapath design: Multi-cycle
9
approach
10
IRWrite
PC+4
CLK CLK
Address
EN
PC
Instruction &
EN Data Memory Zero
PCWrite
WriteData ALU
4
MemWrite ALUControl
ALUDecod
er
ALUOp
Instruction Reg.
1: IR = M[PC]; PC = PC + 4
lw-instr. and non-architectural elements: A, Data and ALUOut-register
1: IR = M[PC]; PC = PC + 4
11 2: A = Reg[25:21];
3: ALUOut = A + SignExtn(Imm)
IRWrite A-Reg. 4: MDR = M[ALUOut]
lorD 5: Reg[20:16] = MDR
CLK ALUOut-Reg.
CLK
CLK CLK
0
M Address
IR 25:21
Read Read A CLK
P U EN register data
C X 1 1
Instruction &
EN 1 data Read Zero
ALUOut
Memory register
CLK 2
WriteData 20:16
ALUResult
Write ALU
register
M Read
D Write data
MemWrite R data 2 ALUControl
15:0 RegWrite Sign ALUDecod
Extn er
.
ALUOp
Data-Reg. /MDR
sw-instr and non-architectural elements: B-register
12 1: IR = M[PC]; PC = PC + 4
2: A = Reg[25:21]; B=Reg[20:16]
IRWrite 3: ALUOut = A + SignExtn(Imm)
lorD
4: M[ALUOut] = B
CLK CLK
CLK
CLK
0
IR 25:21
Read Read CLK
M Address A
P U EN register 1 data
C X 1
Instruction &
EN 1 data 20:16
Read Zero
ALUOut
Memory register 2 CLK
Write ALU
WriteData Read ALUResult
register B
data
2
Write
MemWrite data ALUControl
RegWrite Sign ALUDecod
Extn er
.
15:0
B-register ALUOp
R-type instruction
13 1: IR = M[PC]; PC = PC + 4
2: A = Reg[25:21]; B=Reg[20:16]
IRWrite 3: ALUOut = A + B
4: Reg[15:11]=ALUOut
CLK CLK
CLK
CLK
Address
IR 25:21
Read Read A CLK
P EN register 1 data
C 1
Instruction & 20:16
EN data Read Zero
ALUOut
Memory register 2 CLK
15:11
PCWrite Write ALU
WriteData Read ALUResult
register B
data
2
Write
MemWrite data ALUControl
RegWrite ALUDecod
er
15:0
5:0 ALUOp
Fetch stage and B(R)-type instruction BEQ R1, R2, offset; if true, PC =PC+4+offset
else PC = PC+4
14
IRWrite ALUSrcA
ALUSrcB1:0
CLK 0
CLK PCWrite
CLK M PCSrc
CLK U
X
Address
IR 25:21
Read Read A 1
P register 1 data Branch CLK 0
EN
1 M
C Instruction & 20:16 U
EN data Read Zero
X
ALUOut
Memory register 2 CLK 00
1
Write ALU
WriteData Read 4 01 ALUResult
ALUOut
register B
data
2 11
Write
MemWrite data ALUControl
15:0 RegWrite Sign ALUDecod
Extn er
<<2
1: IR[PC]; PC = PC + 4 .
ALUOp
2: ALUOut = PC + 4 + Shift(Sign(offset))
3: PC = ALUOut if Zero == 1 //A-B
I-type instruction: ADDI
op rs rd Immediate
15
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0)
IRWrite RegDst
1: IR = M[PC]; PC = PC + 4
2: A = Reg[25:21];
CLK 3: ALUOut = A + SignExtn(Imm)
CLK 4: Reg[20:16] = ALUOut
CLK
CLK
Address
IR 25:21
Read Read A
P EN register 1 data CLK
C 1
Instruction &
EN data Read Zero
ALUOut
Memory register 2
20:16
Write ALU
WriteData Read ALUResult
ALUOut
register
data
2
Write
MemWrite data ALUControl
RegWrite Sign ALUDecod
Extn er
.
15:0
ALUOp
Fetch stage and Jump instruction PC PC+4[31:28] addrs[27:0]
Jump
16
0
27:0
IRWrite
1 31:28
Jump
CLK PCWrite
CLK PCSrc
Address
IR
P EN 0
C Instruction &
EN data Zero
Memory 2
WriteData ALU
4 ALUResult
ALUControl
ALUDecod
er
1: IR = M[PC]; PC = PC + 4
2: PC = {PC[31:28], LShift(Addr)} ALUOp
25:0 <<2
Combined Datapath of all types of instructions
Jump
17
0
27:0 ALUSrcA
IRWrite RegDst
1 31:28 ALUSrcB1:0
lorD
CLK 0 Jump
CLK PCWrite
CLK M PCSrc
CLK U
0 X
25:21
M Read Read CLK
Address 1 Branch
P U EN register 1 data 0
C X 1
Instruction & 20:16 1
EN 1 data Read Zero
20:16 register 2 CLK 2
Memory 0 00
CLK
Write ALU
WriteData 15:11 Read 4 01
1 register ALUResult
data 10
0 2 11
M Write
MemWrite U data ALUControl
X
1 RegWrite Sign ALUDecod
Extn er ALUOut
<<2
.
15:0
5:0 ALUOp
MemtoReg
<<2
Control signals
18
• IorD
• Jump
• Memwrite
• IRWrite
• RegDst
• MemtoReg
• RegWrite
• ALUSrcA
• ALUSrcR1:0
• PCWrite
• Branch
• PCSrc
• ALUOp
• ALUControl
Multicycle model
19
Decoding stage
ADDI instruction
27
IF ID
(T0) (T1)
J
ADD LW ADDI BNE
SW
EXE EXE EXE
EXE (T8)
(T6) (T2) ADDI (T10)
LW
SW
ADD
MEM MEM MEM MEM
(T7) (T3) (T5) (T9)
LW
WB
(T4)
30
Clock-cycle needed for the instructions
Instructions Clock-
cycle
LW 5
SW 4
R-type 4
BEQ 3
ADDI 4
J 3
What is next?
31
Instr. Jump RegDst RegWrite ALUSrc Branch ALUOp1 ALUOp0 MemRead MemWrite MemtoReg
R-type 0 1 1 0 0 1 0 0 0 0
lw 0 0 1 1 0 0 0 1 0 1
sw 0 x 0 1 0 0 0 0 1 x
addi 0 0 1 1 0 0 0 0 0 0
B-type 0 x 0 0 1 0 1 0 0 x
J-type 1 x 0 x x x x 0 0 x
5 ns
LW’s FSM
Multi-
Cycle Machine Operation Control signals Next
state State
T2 A + sigEx(offset) ALUSrcA=1, ALUSrcB1:0 = 10, ALUOp=00 T3
T3 Data M[A+sigEx(off)] IorD=1 T4
T4 RF[dest] Data RegDst=0, MemtoReg=1, RegWrite=1 T0
36
Performance Analysis
Jump
40 Path-1: 𝒕𝒑𝒄𝒒 + 𝒕𝒎𝒖𝒙 + 𝒕𝑨𝑳𝑼 + 𝒕𝒎𝒖𝒙 + 𝒕𝒎𝒖𝒙 + 𝒕𝒔𝒆𝒕𝒖𝒑
𝒕𝒎𝒖𝒙 0 27:0
IRWrite RegDst ALUSrcA
1 31:28 ALUSrcB1:0
• 𝑇𝑐 = 𝑡𝑝𝑐𝑞_𝑃𝐶 + 𝑡𝑚𝑢𝑥 + max 𝑡𝐴𝐿𝑈 + 𝑡𝑚𝑢𝑥 + 𝑡𝑚𝑢𝑥 , 𝑡𝑚𝑒𝑚 + 𝑡𝑠𝑒𝑡𝑢𝑝 𝑡𝐴𝐿𝑈 200
𝑡𝑚𝑢𝑥 25
• Tc = 30 + 25 + 250 + 20 = 350 ps
𝑡𝑠𝑒𝑡𝑢𝑝 20
• Execution time =
(100 * 109 instrs.) * (4.12 cycle/instrs.) * (350 * 10-12 s/cycle)
= 133.9 seconds
43
Performance Analysis: A Comparison
• For multi-cycle, Tc = 350 ps and CPI = 4.12 Para Delay (ps)
• For single-cycle, Tc = 925 ps and CPI = 1 meter
𝑡𝑝𝑐𝑞_𝑃𝐶 30
• For multi-cycle, execution time = 133.9 seconds 𝑡𝑚𝑒𝑚 250
• For single-cycle, execution time = 92.5 seconds 𝑡𝑅𝐹𝑟𝑒𝑎𝑑 20
IR_Out memWrite
iDCD
PC_In
PC_Inr PC
MDR_Out
ACC_Zero ACC_Cmp Instructions Op- Meaning
PC_Out Cod
Sharedbus
ACC_In e
ACC LD <addrs> 000 Acc M[addrs]
Mem ADD 001 Acc Acc +
ALU_Add ACC_Out (M) <addrs> M[addrs]
ST <addrs> 010 M[addrs] Acc
ALU JMPZ 011 PC addrs if Acc = 0
<addrs>
[+,&] Temp
MAR CMP 100 Acc 𝐴𝑐𝑐
Temp_Out AND 101 Acc Acc &
<addrs> M[addrs]
ALU_And MAR_In
Data
Single bit
More than one bit
Data_In
Single cycle Simple CPU
3
op Addrs
MUX
4-bits(15-12) 12-bits (11-0)
MemRead
ACC
Read
IM data WR_EN
1
15:0 Sign
Extn.
How does one make Multi-Cycle SimpleCPU without
4 shared-bus?
• Find out the units take large amount of time
• Minimize the resource usage: Memory & Adder
• Insert non-architectural element or register to
hold the intermediate result
• Analyse the datapath for fetch stage and
different instruction execution stages
• Combined the datapath using MUXs
• Design the control unit
5
How does one analyse the data-path?
• ALU
ACC
• Signed-extension unit Address
Memory
ALU_Add
• Data memory
ALU
[+,&]
MemWrite
ALU_And
+/- Extn
8
Analysis of data-path for Fetch stage
IRWrite
MemRead CLK
Data/Instruction IR
ALU
EN
Address
PC
ADD
2
Instruction &
Data Memory
Why 2 ?
1: IR = M[PC]; PC = PC + 2
Instruction Reg. (IR)
Analysis of data-path for LD instruction
9 • LD <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
lorD 1: IR = M[PC]; PC = PC + 2
IRWrite 2. MDR = M[extn(IR11:0)]
CLK
3. ACC = MDR
0 Data/Instruction IR
M +/- Extn
PC U Address EN
Here we consider
X that memory address
1 is 16 bits and byte-
Instruction & addressable.
Data Memory
CLK
ACC
LD
0 Data/Instruction IR
M +/- Extn
PC U Address EN
X ACC
1
WriteData
Instruction &
Data Memory
MemWrite
11
Analysis of data-path for ADD instruction
• ADD <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
1: IR = M[PC]; PC = PC + 2
lorD IRWrite
MemRead CLK 2. MDR = M[extn(IR11:0)]
3. ACC = ACC + MDR
0 Data/Instruction IR
M
PC U Address EN
X
1 ACC
CLK ALU
WriteData ADD
ALU_ADD
JMPZ 1: IR = M[PC]; PC = PC + 2
2. PC = extn(IR11:0) if ACC_Zero ==0
MemRead CLK
IRWrite
ACC
WriteData
Instruction &
Data Memory
+/- Extn
13
Analysis of data-path for CMP instruction
op
1: IR = M[PC]; PC = PC + 2
2. ACC = ~(ACC)
MemRead CLK
IRWrite
Address Data/Instruction IR
PC EN ACC CMP: WR_EN
CMP
Qs
WriteData
Instruction &
Data Memory
14
Analysis of data-path for AND instruction
• AND <addr>
op Addrs
4-bits(15-12) 12-bits (11-0)
1: IR = M[PC]; PC = PC + 2
lorD IRWrite
MemRead CLK 2. MDR = M[extn(IR11:0)]
3. ACC = ACC & MDR
0 Data/Instruction IR
M
PC U Address EN
X
1 ACC
CLK ALU
WriteData AND
ALU_AND
AND: WR_EN
+/- Extn
Combined data path for all instructions
15
Acc_Zero IRWrite
0 1 2
ACC_In [1:0]
JMPZ
MemRead
lorD CLK ACC
ALUSrcA LD
WR_EN
1 Mem IR 1 CMP
1 ADD
Address Read
EN AND
PC 0 0 ALU
0 data
Write
CLK 1
data
MDR ALU_And
2 0
MemWrite ALU_Add
JMPZ
IorD
MemRead
MemWrite
𝑂𝑝𝑐𝑜𝑑𝑒15:12 Main IRWrite
Decoder ALUSrcA
ALUSrcB
ALU_Add
ALU_And
ACC_In [1:0]
LD
AND
ADD
CMP
Generation of Controls: Main decoder state table
18
Inputs to the control unit: op-code part [15:12]
Homework
States diagram
19 Homework
• Design it using Verilog HDL and C++
• Design a test program (7 Fibonacci nos) and load it into Instr. Mem
• Add more instructions
• Register type
• How does one convert MIPS Single-cycle microprocessor’s datapath into
datapath like (shared-bus) SimpleCPU?
Single-purpose/Special-purpose/Dedicated Processor
MinMax Processor
20 What kind of datapath is it? Can we make it Multi-cycle datapath?
Input: set of n nos stored in A
Output: Min & Max
Min = ∞
Max = - ∞ 10 -∞
Do scan i-th no.
If Max < A [i] then Stop 0 1
Max = A [i]
= LoadMax
Stop IncPC
MEM CLK
MinMax Processor
21
Summary
• SimpleCPU’s Multi-Cycle Datapath & CU design
Computer Architecture (CS F342)
Single-purpose Microprocessor: MinMax
A problem: Find the minimum and maximum 2
High-level Construct Digital Construct
number from a set of numbers Scalars/variable Register or wire
• Is there an algorithm to solve it automatically? Arrays Memories
• Yes Operators Functional unit
∞
High-level Synthesis
MUX
3
Can we execute other algorithms on this processor?
Special-purpose/Dedicated Processor
MinMax Microprocessor
Stop MinReg
>
Where is the instructions (ISA)? Controller 0 1 LoadMin
MinMax Microprocessor
MinMax Microprocessor’s Single-cycle Datapath
4
Stop MinReg
>
How many clock cycle does it take Controller LoadMin
0 1
to complete the task?
10 clock cycles ∞
MinMax Microprocessor
Can we make MinMax Microprocessor’s Datapath 5
as Multi-cycle Datapath?
10 -∞
Stop 0 1
= LoadMax
CLK CLK
PC/i
< MaxReg
IncPC
MEM CLK
MinReg
>
Controller 0 LoadMin
1
MinMax Microprocessor
Can we make MinMax Microprocessor’s Datapath 6
as Multi-cycle Datapath?
• Here we have 4 FUs:
=, <, > and + 10 -∞
CLK CLK
• 16 bit MinMax PC/i
Microprocessor < MaxReg
IncPC
• Byte addressable MEM CLK
memory MinReg
>
Controller 0 LoadMin
1
MinMax Microprocessor
7
MinMax Microprocessor
LoadMin MinIn
∞ 1
MinReg Limit Stop
0
NOR
LimitUpdate
MinUpdate ALUSrcA
3
PCUpdate
2
1
PC/i M <
MEM 0
D R
R -1 >
2
1
DataUpdate
0
2 ALUOp
ALUSrcB
9
Control Signals
1. MaxUpdate
2. MinUpdate
3. LoadMin
4. LoadMax
5. MaxIn
6. MinIn
7. PCUpdate
8. DataUpdate
9. LimitUpdate
10. ALUSrcA
11. ALUSrcB
12. ALUOp
Control Unit 10
StartSate MaxUpdate
MinUpdate
T0 LoadMin
LoadMax
MaxIn
T1
MinIn
PCUpdate Homework
DataUpdate
T2
LimitUpdate
ALUSrcA
T3 ALUSrcB
ALUOp
Stop
Coming from
Datapath
11
Homework
1. Can we make its datapath as shared bus one?
Computer Architecture
(CS F342)
Design, Analysis, Execution and Optimization of Instructions
Datapath & CU for Pipelined Microprocessor: MIPS
2
Problems of Multi-cycle Processor
• The fundamental problem
• Split the slowest instruction, lw, 5-steps
• Processor’s clock cycle time does not improve 5-times
• The steps take unequal length of time
• Only one stage is busy and remaining stages are idle
• 5-non-architectural registers and a additional multiplexer
Multi-cycle Single-cycle
Instructions
(Clock-cycle) (Clock-cycle) Single Cycle: Non-shared FUs, CPI =1 or IPC=1, clock period (Tsingle)= slowest
LW 5 1 instr. in ISA
SW 4 1 Multi-cycle: Shared FUs, CPI > 1 or IPC <1, clock period: Tmulti < Tsingle
R-type 4 1
BEQ 3 1 Can we have a microprocessor like: IPC=1 & clock period [< Tmulti < Tsingle]?
ADDI 4 1
J 3 1 Cycles Per Instruction (CPI) Program Execution time: #instr. x CPI x Clk (T)
CPI >1 1
Instructions Per Cycle/Seconds (IPC) = 1/CPI
Lesser than More than
CLK (T)
Single-cycle Multi-Cycle
3
Pipeline in a Chemical Plant
4
Pipeline in the Instruction Execution
Memory
Words Results
Instruction Instruction Instruction
Fetch Decode Execution
[Stage-1] [Stage-2] [Stage-3]
Stage-1 1 2 3 4 5
Stage-2 1 2 3 4
Stage-3 1 2 3
Time
Multiplexer Tmux 25
• Major component—instruction memory (IM), register file (RF) read, ALU execution and data
memory (DM)
• Register file: write operation in the first part of a (neg edge) cycle and read in the second part
• Multi-cycle-clock view
A view of pipeline in operation 11
• Single-cycle-clock view
sw $s6, 20($s1) and $s5, $t5, $t6 sub $s4, $s1, $s5 add $s3, $t1, $t2 LW $s2, 40($0)
Pipelined Datapath 12
• Datapath is formed by partitioning the Instruction execution Cycle & Distributing the delay
element judiciously
• Five stages separated by pipeline registers
Single-cycle
• Datapath is formed by partitioning the Instruction execution Cycle & Distributing the delay
element judiciously Pipelined
• Five stages separated by pipeline registers registers
Feedback
datapaths are
not
pipelined;
however
feedforward
datapaths are
pipelined
Combined Single-cycle data-path 14
18 0
M
U
X
+ 1
+ CLK <<2
CLK 4 MemWrite
RegDst
ALUSrc CLK MemtoReg
0 25:21
M Read Read
Read Branch
U register data 1
PC address
1
Address
X 20:16
1 Read Zero Data Read
Instruction 1
register memor data
0 M
0 2 y
M U
Jump Instruction M Write U ALU X
Memory 15:11 U register X Write 0
X Read 1 data
1 Write data 2
31:28 data SWALUControl
MemRead
25:0 15:0 Result RegWrite Sign ALUDecoder
Extn.
27:0
<<2 5:0 ALUOp
LW
CLK
Pipelined Datapath 15
• Datapath is formed by partitioning the Instruction execution Cycle & Distributing the delay
element judiciously
• Five stages separated by pipeline registers
Pipelined
Is this datapath correct? Example in Slide-8: LW will write data into $4 rather than $2
Pipelined Datapath 16
• Modified datapath
Instr Jump RegDs ALUOp ALUOp ALUSrc Branc MemRea MemWrit RegWri MemtoRe
t 1 0 h d e te g
R-format 0 1 1 0 0 0 0 0 1 0
lw 0 0 0 0 1 0 1 0 1 1
sw 0 x 0 0 1 0 1 0 0 x
beq 0 x 0 1 0 1 0 0 0 x
20
• How to manage the control signals generated for i-th instruction and
control signal will be generated for (i+1)-th instructions?
• Erroneous control signals can be generated
21
• How to manage the control signals generated for i-th instruction and
control signal will be generated for (i+1)-th instructions?
• Erroneous control signals can be generated
• Extension of the pipeline registers for storing the control signals’ values
Pipelined Datapath & Control 22
• CU as in Single-cycle
• Control signals must be pipelined (remain synchronized with instruction)
23
CLK
CL CL CL CL CL
Functional Units
Interconnects (FUs)
(Bus)
Less More
Methods/Algorithms:
Single-bus & Single-FU Single-bus & Many-FUs
Less 1) Multi-Cycle
(Multi-Cycle, IPC < 1) (Multi-Cycle, IPC < 1)
2) Single-Cycle
Many-bus & Single-FU Many-bus & Many-FUs 3) Pipelined
More
(Multi-Cycle, IPC < 1) (Single-Cycle or Pipeline, IPC = 1)
• Pipeline: IPC = 1 (borrowed from Single-Cycle) and less clock period (T)
(borrowed from Multi-Cycle), shared the Buses & FUs by more than one
instruction.
• Program Execution time: #instr. x (1/IPC) x Clk (T)
• Step-1:
• Find out the instructions for the Algorithm(s)
• Step-2: [Microarchitecture design]
• Find out the strategy (Sharedbus/Singlecycle/Multicycle/Pipeline[in order]/etc) for datapath
and next
• Design the datapath and its components for each instructons
• Step-3: How about Single-purpose
microprocessor like
• Design the combined datapaths for all instructions MinMax microprocessor?
• Step-4:
• Decide the clock period based on the critical path [timing analysis]
• Add setup time, hold time, clock-to-Q and etc. to the decided clock period
• Step-5:
• Identify the control signals on the combined datapath
• Step-6:
• Design the Control Unit (H/W or S/W) for generating the such control signals based on the
strategy (Sharedbus/Singlecycle/Multicycle/Pipeline[in order]/etc) decided for datapath
• Step-7:
• Test & verification of the designed microprocessor
27
Homework
• Design the Pipelined MIPS ISA using Verilog HDL and C++
• Convert
• SimpleCPU into Pipelined SimpleCPU
• MinMax microprocessor in Pipelined MinMax
• Design the Pipelined SimpleCPU ISA using Verilog HDL and C++
• Design the Pipelined MinMax microprocessor using Verilog HDL and
C++
• How does Intel manages to run CISC-type code onto RISC-based
pipeline?
28
Summary
• Limitation of Multi-cycle approach
• CPI Vs IPC
• Comparison between single-cycle and pipelined approaches
• Views of pipeline in operation
• Comparison of datapaths
• Design tradeoffs of microprocessors
• Datapath and CU for pipelined processor
Computer Architecture
(CS F342)
Design, Analysis, Execution and Optimization of Instructions
Datapath & CU for Pipelined Microprocessor: SimpleCPU &
MinMax
Shared-bus based SimpleCPU 2
memRead MDR_In
14
2
0
IR_In MDR
IR 1
IR_Out memWrite
iDCD
PC_In
PC_Inr PC
MDR_Out
ACC_Zero ACC_Cmp Instructions Op- Meaning
PC_Out Code
ACC_In Acc M[addrs]
Sharedbus
LD <addrs> 000
ACC
ADD <addrs> 001 Acc Acc + M[addrs]
Mem
ST <addrs> 010 M[addrs] Acc
ALU_Add ACC_Out (M)
JMPZ 011 PC addrs if Acc = 0
<addrs>
ALU
CMP 100 Acc 𝐴𝑐𝑐
[+,&] Temp AND <addrs> 101 Acc Acc & M[addrs]
MAR
Temp_Out
ALU_And MAR_In
Data
Single bit
More than one bit
Data_In
Single-Cycle based SimpleCPU 3
14
MUX
MemRead
ACC
Read
IM data WR_EN
1
Address Read Address
0 PC DM
data LD
AND CMP
Write data ADD ALU
Write
data
ALU_And
+ ALU_Add
2 11:0 MemWrite
15:0 Sign
Extn. How can we make it Pipelined one?
Pipelined Datapath & Control 4
• CU as in Single-cycle
• Control signals must be pipelined (remain synchronized with instruction)
5
14
Pipelined SimpleCPU
Pipelined Register
MemRead
ACC
Read
IM data WR_EN
1
Address Read Address
0 PC DM
data LD
AND CMP
Write data ADD ALU
Write
data
ALU_And
+ ALU_Add
2 11:0 MemWrite
15:0
Sign
Extn.
6
Control Unit
• Control signals will be stored in the register and shift with the
instruction
• Extent the datapath’s pipeline registers for control signals as like
pipelined MIPS processor
MinMax Microprocessor’s Single-cycle Datapath 7
LoadMin
9999
1
LoadLimit
lesser 0 MinIn 10
LoadPC 1
0 1 0 Stop
0 < MinReg
MEM Limit
PC
LimitUpdate
> MaxReg
PCUpdate +
-1
+ memWrite greater 0 MaxIn
2
1
0
LoadMax
MEM Limit
PC
LimitUpdate
> MaxReg
PCUpdate +
-1
+ memWrite greater 0 MaxIn
2
1
0
LoadMax
9
Control Unit
• Control signals will be stored in the register and shift with the
instruction
• Extent the datapath’s pipeline registers for control signals as like
pipelined MIPS processor
10
Summary
• Place pipelined register based on the delay in the component
• Applied pipelined methodology in SimpleCPU and MinMax processor
Computer Architecture
(CS F342)
Design and Analysis of Instructions
Minimization of Structural & Data Hazards in
Pipelined MIPS (RISC) Processor
2
Pipelined-based Processor
• Structural Hazard
• Data Hazard
• Control Hazard
4
Structural Hazards
Memory
Time
Structural Hazards 6
• Two instructions trying to write data in register file at the same time
IF ID EXE MEM WB
ADD $S1, $S2, $S3
Time
7
Inst1 Inst2
10
Inst1 Inst2
11
Inst1 Inst2
12
Data Hazards
• Data dependences between instructions
• True or real
• False or name
• Inst1 & Inst2 are so close that their overlapping would
change their access order to register, Reg.
14
Data Hazards
• Types of data hazards
Data Hazards
• WAW hazards occur
• Write operation in more than one stages
• Allow an instructions to proceed even when a
previous instruction is stalled
Data Hazards
• WAR hazards occur
• Write stage precedes a read stage
Data Hazards
An example
22
Data Hazard
Dependency detection 23
op rs rt rd shamt funct
6-bits(31-26) 5-bits(25-21) 5-bits(20-16) 5-bits(15-11) 5-bits(10-6) 6-bits (5-0) ID/EX EX/MEM
Rs
• 1a: EX/[Link] == ID/[Link] Rt
• 1b: EX/[Link] == ID/[Link]
• 2a: MEM/[Link] == ID/[Link]
• 2b: MEM/[Link] == ID/[Link] Rd
• ALU’s inputs:
• Register file or Decode stage
• Memory stage
• Writeback stage
26
Hazard in WB stage
• No hazard in WB stage
• Reg. file supplies the correct result if the instruction in the ID stage
reads the same written by the instruction in the WB stage
31
Complicated situation
• The result of the instruction in the WB stage, the result of the
instruction in the MEM stage, and source operand of the instruction in
the ALU stage.
add $1, $1, $2
The control logic for (MEM hazard) add $1, $1, $3
add $1, $1, $4
if ( MEM/[Link] AND (MEM/[Link] != 0)
…
AND ! (EX/[Link] AND (EX/[Link] != 0) AND (EX/[Link] == ID/[Link])
What if we want to copy data from one memory location to other memory location
Data Hazards 33
Data Hazards
Data Hazards
How does one decide no. of consecutive NOOP instruction to put after an instruction? It depends on delay
(clock cycle) to produce the correct operand for the dependent instruction(s).
44
Homework
• Show the modified datapath and control in the pipeline for resolving
memory-to-memory operation (lw then sw)
• How many ways one stalls the pipeline?
• Write a Verilog code for 5-stage pipelined MIPS with data forwarding
technique
• Write a C++ program which can read MIPS assembly program and find out
• How many instructions are valid?
• How many registers are used?
• RAR, RAW, WAR & WAW hazards for a given window size/number of stage n
45
Homework
• Write a C++ program which will take any assembly program written
in 32 bit MIPS ISA and find out the 4 hazards (RAR, RAW, WAW,
WAR) and control hazard in 5-stages scalar pipelined MIPS processor
without hazard detection unit. To resolve the 4 hazards, it will
rearrange the instructions. In the worst case, to resolve the hazards, it
will insert the NOP instructions. Similarly, to resolve the control
hazards, it will use delayed branch techniques: from before, from
target and from fall through. In the worst case, to resolve control
hazards, it will insert the NOP instructions. Finally, it will generate
hazard free assembly code. Use MARS simulator to verify the output
of the newly created file.
45
Summary
• Types of hazards
• Minimization of structural hazards with
• Increased resources
• Interlocking technique
• Types of data hazards
• Minimization of data hazards with
• Forwarding technique
• Forwarding with interlocking technique
• Performance analysis
• Compiler-based technique
Computer Architecture (CS F342)
Design and Analysis of Instructions
Minimization of Control Hazards in Pipelined
MIPS (RISC) Processor
2
What is this?
Consider $t1 and $t2
contain same value
L:
3
An example of Control Hazards
Consider $t1 and $t2
contain same value
L:
4
Control Hazards
𝐶𝑃𝐼 𝑢𝑛𝑝𝑖𝑝𝑒𝑙𝑖𝑛𝑒𝑑
Speedup =
1+𝑝𝑖𝑝𝑒𝑙𝑖𝑛𝑒 𝑠𝑡𝑎𝑙𝑙 𝑐𝑦𝑐𝑙𝑒 𝑓𝑟𝑜𝑚 𝑏𝑟𝑎𝑛𝑐ℎ𝑠
𝑃𝑖𝑝𝑒𝑙𝑖𝑛𝑒 𝑑𝑒𝑝𝑡ℎ
=
1+𝐵𝑟𝑎𝑛𝑐ℎ 𝑓𝑟𝑒𝑞𝑢𝑒𝑛𝑐𝑦 ×𝐵𝑟𝑎𝑛𝑐ℎ 𝑝𝑒𝑛𝑎𝑙𝑡𝑦
6
Control Hazards
• Branch target address is computed at the end of ID-stage
L:
Control Hazards 7
• Assumption: predict-taken
• Compiler rearranges the code, for both the assumptions, so that the most
frequent path matches the hardware’s choice
• Predict-taken has less advantages than predict-not-taken
Control Hazards & Software-based solutions 22
Taken ?
PC + sizeof(inst.)
0 Next fetch address
1
Found ?
PC
BTB
34
...
…
…
…
…
35
Control Hazards and Dynamic Branch
Prediction
• The BTB is accessed during the IF-stage
• A table with branch addresses, the corresponding target
addresses, and prediction information
• The PC for the next instruction to fetch is compared with the
entries in the BTB. If a matching entry is found in the BTB,
fetching can start immediately at the target address
36
Control Hazards and Dynamic Branch
Prediction
• What is this prediction bit in the BTB?
• How many bits are needed?
37
Control Hazards and Dynamic Branch
Prediction
• For a missprediction in the “strongly” state cases, the prediction direction is not
changed, rather the prediction goes into the respective “weakly” state
T T (01) T
(11) (10) (00)
predict
Predict Predict predict
weakly not
strongly weakly taken strongly not
taken
taken taken
NT NT NT
46
Control Hazards and Dynamic Branch Prediction
• Other methodology
It differs from the saturation up-down counter method by
changing directly from the “weakly” to the “strongly” states, in
case of misprediction
Minimize the switching (or power consumption)
T for Taken
NT NT for Not Taken
T
T
NT
Example: see in the [Link]
47
Control Hazards and Dynamic Branch Prediction
• n-bits predictor
n-bit counter (0 to 2n-1)
Taken when counter value is one-half of the max. value (2n-1)
Otherwise, Not taken
Studies of n-bits predictor have shown that 2-bits predictor do
almost well, thus most systems rely on 2-bits predictor
48
Bimodal Predictor
• Predict-taken
• Predict-untaken
Pattern History Table (PHT)
PC
k-bit
2k predictors/counters
BTB
How does size of the table
affect the performance?
49
Homework
• Write a Verilog and C++ code for pipelined with full hazard detection
unit
• Incorporate the dynamic branch prediction
• Write a Verilog and C++ code for generalized N-bit branch predictor
• Is there any effect on the branch prediction for the starting state?
Homework: 50
Summary
• Control hazard
• Performance analysis
• Flush the pipeline
• Hardware-based solution technique
• Take decision at ID stage
• Software-based solution technique
• Predict-taken
• Predict-untaken
• Delayed branch
• Dynamic branch prediction techniques
• N-bit FSM
Computer Architecture
(CS F324)
Design and Analysis of Instructions
Minimization of Control Hazards in Pipelined
MIPS (RISC) Processor
Branch Correlation 2
• if (condition1) {…}
•…
• if (condition1 && condition2){…}
• if (condition1){a=2;}
•…
• if (a==0){…}
• if (aa==2){aa=0;}
• if (bb==2){bb=0;}
• if (aa!==bb){…}
PC
Hash 2K counters/
Predictors
GR
K-bit
gshare predictor
K-bit Local
Local history
prediction
Prog. counter table (LHT)
table (LPT)
(2k x m)
(2m x n)
• Updating: insert the branch outcome into the MSB of LHT and perform right-shift,
pointed by the index register (PC)
12
• It consider the local history table as well as the global history table.
• Local history table keep track of the outcomes of all the branches
which are mapped to a single entry
• Global history table keep track of the outcome of all the branches that
are executed so far
• This is implemented in “Alpha 21264” processor and after that there
are several other processors are also used the variant of this
tournament branch predictor mechanism.
14
Dynamic branch prediction:
Organization of a Tournament Branch Predictor
Global
prediction
Local history Local (4096 x 2)
Prog. counter table prediction
(1024 x 10) (1024 x 3)
Choice
MUX prediction
(4096 x 2)
12-bit Global
history reg.
Branch prediction
15
Prediction is made if both the local and global predictions are same.
Otherwise, consult with the choice prediction
Choice prediction:
• The global prediction’s decision if 2-bit saturation counter’s value is ≥ 2
• The local prediction’s decision if 2-bit saturation counter’s value is < 2
17
State diagram of Tournament predictor
0/0, 1/0, 1/1 0/0, 0/1, 1/1
0: Pred. is incorrect
0/1 1/0 0/1 1: Pred. is correct
1/0
m/n: Pred. for P1/P2
0/1
Use Use
0/0, 1/1 P1 P2 0/0, 1/1
(W) (W)
1/0
Weak P1 Weak P2
18
Dynamic branch prediction:
Tournament Branch Prediction Rules
Update the local history table:
• Push the decision into the MSB of LHT
Update the Global history register:
• Push the decision into the MSB of GHR
Update the Local and Global prediction table:
• Based on the n-bit saturation counter
Update the Choice table
• Increment the choice towards GPT
19
Pred.
Feedback
Mechanism
Branch outcome Static,
Update pred. Saturating counter
mechanism ML
Update history DL
RL
21
Summary:
• Shortcomings of 2-bit predictor
• Correlated branch prediction
• gshare branch predictor
• Two-level branch predictor
• Shortcoming of Correlated predictor
• Tournament predictor
• How to initialize the branch predictor’s initial state
• Sophisticated branch predictor
• Championship Branch Prediction (CBP)
Computer Architecture
(CS F342)
Design, Analysis, Execution and Optimization of
Instructions
Fundamentals of Pipelined-based Design
Methodology
2
Problems of Multi-cycle Processor
CLK
4 How to improve the processor’s performance?
• For example
Throughput ≈ 3/n
3-stages
6
Pipelined-based Design Methodology
• k-fold increase in throughput
• Increase in performance
• Partitioning the logics
• Adding new buffer
• Inputs are overlapped in execution
Throughput ≈ 3/n
3-stages
7 Limitations of Pipelined-based Methodology
• Pipelined-based design
• Combinational logic (F)
• Latch (L)
• Max. propagation delay in F: TM
• Min. propagation delay in F: Tm
• Proper latching delay: TL
Minimum clock period in Pipeline-based Systems
10
• Case-2:
• Inputs x2 applied at the stage at time T2
• Effect of the outputs can be found at least at T2 + Tm
• Condition of 2-nd set of signals does not overrun the
1-st set: T2 + Tm > T1 + TM + TL
• Clock period (T): T2 – T1 > TM – Tm + TL
• Max. clocking rate cannot exceed 1/T
Minimum clock period in Pipeline-based Systems
12
• TL:
• feedback loop and stabilizing of the signal
• worst-case clock skew
13 Tradeoff between Cost and Performance
• Cost/performance ratio
𝐶 𝐺+𝑘∗𝐿
=
𝑃 1
𝑇
( + 𝑆)
𝑘
𝐺𝑇
= 𝐿𝑇 + 𝐺𝑆 + 𝐿𝑆𝑘 +
𝑘
• Find minimum cost/performance ratio
16 Tradeoff between Cost and Performance
• Find minimum cost/performance ratio
• First derivative w.r.t k
𝐺𝑇
𝑘𝑜𝑝𝑡 = Under pipeline Over pipeline
𝐿𝑆 (𝑘𝑜𝑝𝑡 > 𝑘) (𝑘𝑜𝑝𝑡 < 𝑘)
Optimum
value (𝑘𝑜𝑝𝑡 )
17 Tradeoff between Cost and Performance
• Find minimum cost/performance ratio
• First derivative (w.r.t k)
𝐺𝑇
𝑘𝑜𝑝𝑡 =
𝐿𝑆
• No consideration on dynamic behavior or runtime
18 Pipeline Idealism
Example:
• An additional delay of 25-ns is required for proper clocking
• What is the clock period now?
• Clock period is (150-ns plus 25-ns): 175-ns
23 Pipeline Idealism: Identical computations
Behavior of a program
3
Matrix multiplication
• Data stored in row-major order for (i=0; i<l; i++)
• Data of A, B & C can be used in near future for (j=0; j<m; j++)
• Data neighboring to previously accessed data for (k=0; k<n; k++)
• Instructions are also to be used in near future A[i][j] += B[i][k] x C[k][j];
• Principle of locality
• Examples
• A loop in the code
• Matrix multiplication
4
Why do we need to study memory/storage
hierarchy?
• Principle of locality can be found in most of the programs
• Memory hierarchy
Faster but smaller memory closer to CPU
Slower but larger memory faraway from the CPU
5
Memory hierarchy
Access time and
space increase
Cost increase
6
What makes improvement in the storage
access time?
• Static Random Access Memory (SRAM) Technology
Registers, L1, L2 & L3 cache
• Dynamic Random Access Memory (DRAM) Technology
Main memory
• Magnetic Technology
Hard disk takes longer access time because of mechanical components
7
Do we really need memory hierarchy?
• Memory Wall Problem: Avg. mem. Reqst./sec.
Significant increase in
Performance ratio
processor performance
Mem. Wall
over the years
Not significant increase in DRAM mem. accs./sec.
main-memory
performance over the years
8
Performance ratio
Performance ratio
Mem. Wall
• Performance difference between processor (CPU) and memory by technology and memory is placed far away
(nm scale) from CPU (off-chip)
• There is a gap in CPU’s request (rate) for the memory accesses and the service (rate) for those request by
memory
• Cache memory technique can speed up the performance of the memory accesses time
11
Caches are in MIPS-processor
Main Memory
BUS
I-Cache
D-Cache
Cache Memory Architecture 12
SRAM Cell
DRAM Cell
Cache size is smaller than the main memory & its 13
associated challenges
• Which data are to be stored/kept in Do we know a related problem?
cache? How does one organize the cloths
• What could be the granularity of in a closet or an Almira?
the data-size? Are we able to answer all the
• Who will decide such granularity? unknown in the left part from this
• Where do we place the data? problem?
• Which data are to be evict out, if Which behavior to be used for
cache (line) is full? organizing the closet?
• How to update data? Can we use such method to
organize the cache memory?
• How to organize the cache for the • Directly (?)
programs to run efficiently? • Or modified one (?)
Behavior of a program
15
Characteristics of Cache Memory
The following items are embedded in the cache:
• Organization
The logical arrangement of storage unit/data
• Content-management heuristics
Decide the best possible items for caching and
evict out the candidate to make room for more important
data not yet cached
• Consistency-management heuristics
Ensure that the instructions and data that the program
expects to receive are the ones the program does, indeed,
receive
Consistency with 1) self, 2) main memory 3) other caches
16
Cache Organization: Blocks, Tags and Set
A cache stores chunks of data (called cache blocks or cache lines) that come from the
memory.
A cache is typically much smaller than the memory:
How does CPU know whether any particular datum is present in the cache or not?
Cache tags fulfil this necessity.
• Cache Operations
• What could be the size of the block in cache & in main memory?
Processor
Processor
MNP
LW R1, X
Y X MNP
MNP
LW R1, X Block
Cache
Block Y
LW R1, X
X MNP
Y X MNP
Memory Block
Memory
18
How many ways one map the incoming block
from main memory onto the Cache?
• A block can be placed • Is there any similar problem
available?
Anywhere Container & items
A fixed position Taking notes during class
& notebooks
A set of positions Can we use this strategy?
Example
• Suppose a program has 2000 data access instructions (loads or stores),
and 1250 of these requested data values are found in the cache. The
other 750 data values are supplied to the processor by main memory or
disk memory. What are the miss and hit rates for the cache?
Example
• Suppose a computer system has a memory organization with only two
levels of hierarchy, a cache and main memory. What is the average
memory access time given the access times and miss rates in the given
Table?
Memory level Access time (cycles) Miss rate
Cache 1 10%
Main memory 100 0%
Total Ref.=12
Hit ratio = 9/12=75%
Miss ratio = 3/12=25%
28
How does one map a incoming block onto a
fixed location in the cache?
• Direct Cache Organization
Block size in main memory is equal as in
cache
CPU generates memory address
Block & offset
Set/Group will contribute to identify the
location in the cache
Tag will contribute to identify a block
Tag contains block address
Total Ref.=12
Hit ratio = 0/12=0%
Miss ratio = 12/12=100%
Cache thrashing
33
How does one map a incoming block onto the
fixed set of locations in the cache?
• Set Associative Cache Organization
Block size in main memory is equal as in
cache
CPU generates memory address
Block & offset
Set/Group will contribute to identify the
location in the cache
Tag will contribute to identify a block
Tag contains block address
Combination of Fully associative & Direct
cache organization
Total Ref.=12
Hit ratio = 8/12=67%
Miss ratio = 4/12=33%
Summary
• Necessity of Memory Hierarchy
• Necessity of Cache memory
• Characteristic of Cache memory
• Program’s behavior
• Elements of Cache Organization
• Cache Mapping techniques
• Fully Associative
• Direct
• Set Associative
• Elements of Cache Optimization
Computer Architecture (CS F342)
Memory/Storage Hierarchy
&
Cache Replacement Policies and Read & Write Strategies
2
Block replacement
• Cache’s size is less as compared with main memory
• For each set, generate block no. between a (min) and b (max)
• a + [rand() % (b-a+1)]
• LRU is difficult/expensive
• Record timestamps? How many bits?
• Find minimum timestamp on each replacement
• Sorted list? Re-sort on every access, hit or miss?
• Is there a way-out?
• Shift-register-based implementation
• Every time a block is referenced as hit or miss, it placed on the head on the ordered list, while
other blocks in the set are pushed down the list
9
each bit represents one branch point in a binary decision tree; let 1 represent that the left side has been referenced more
recently than the right side, and 0 represent that the right has been referenced more recently than left side.
Example
see in the [Link]
Tree-PLRU: four-way set associative - three bits 13
Overhead = (n-1)/n
Example
see in the [Link]
15
Optimal Replacement Policy
Evict the block with longest reuse distance
• Need future’s knowledge
Can we build it?
• General case LRU
• No
• Special case X A B C
• Yes
D A B C
• Trace
• Optimal better than LRU
• 4-way set associative
• LRU Optimal
• Ref. X, A, B, C, D, X
X D B C
16
Hybrid Replacement Algorithm
• Example
• 4-way set associative
• LRU
• Cyclic Ref. X, A, B, C, D, X, …
• Set thrashing: When the “program working set” in a set is larger than set associativity
• Random replacement policy is better when thrashing occurs
• In practice
• Depends on workload
• Avg. hit rate of LRU and Random are similar
• Combined of two approaches: LRU & Random
• How to choose between LRU & Random?
• Set sampling
Qureshi et al., “A Case for MLP-Aware Cache Replacement,“ ISCA 2006.
Belady, “A study of replacement algorithms for a virtual-storage computer,” IBM Systems Journal, 1966.
17
Sophisticated Cache Replacement Policies &
Prefetching Techniques & other info:
Main Memory
20
Main Memory
22
Cache Read Architecture: Look through cache
• Processor starts a read cycle
• Hit:
• Cache will respond to the read cycle without starting an access to main
memory
• Miss:
• Cache passes the bus cycle onto the system bus and main memory
responds
• The data will be stored in the cache also
• Complex & expensive
• Access time is fast
• Drawback of look aside architecture has removed
23
• When the processor must wait for writes to complete during write through,
the processor is said to write stall.
Summary
• Replacement Algorithms
• Read architecture
• Write policies
• Write misses