82
The MIPS Processor Implementation: Datapath & Control “Computer Organization & Design ” 第四章

The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

  • Upload
    others

  • View
    45

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

The MIPS Processor

Implementation:

Datapath & Control

“Computer Organization & Design ”

第四章

Page 2: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 2

MIPS Processor

• MIPS:无互锁流水级的微处理器(Microprocessor w/o Interlocked Piped Stages) – interlock单元:检测RAW相关,推迟后续指令执行(互锁状态)

• R4000以后开始使用interlock

– 尽量利用软件办法避免流水线中的数据依赖问题

– 设计哲学:MIPS is simple, elegant. • Simplicity favors regularity!

• 1980,Patterson提出RISC指令集

• 1983,Hennessy完成第一个RISC处理器MIPS。

Page 3: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 3

MIPS指令字格式

• 100余条指令(P&H 33条),32个通用寄存器

• 指令格式:定长32位 – R-type:arithmetic instruction

– I-type:data transfer(ld/st), arithmetic(addi)

– J-type:branch instruction(conditional & unconditional)

op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits)

op(6 bits) rs(5 bits) rt(5 bits) immediate(16 bits)

R-type

I-type

op(6 bits) rs(5 bits) rt(5 bits) addr(16 bits)

J-type op(6 bits) addr(26 bits)

op(6 bits) rs(5 bits) rt(5 bits) addr (16 bits)

ALU

move

jmp

Page 4: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

4/41

MIPS寻址方式:与指令类型绑定!

• 立即寻址: I-type

– rs+imm => rt or addr

• 寄存器寻址:R-type

• 基址寻址:I-type

– rs = BaseReg

– 直接寻址?

• PC相对寻址:J-type

• 伪直接寻址:J-type

– pseudodirect addressing

– 26位形式地址左移2位,与PC的高4位拼接

Byte Halfword Word

Registers

Memory

Memory

Word

Memory

Word

Register

Register

1. Immediate addressing

2. Register addressing

3. Base addressing

4. PC-relative addressing

5. Pseudodirect addressing

op rs rt

op rs rt

op rs rt

op

op

rs rt

Address

Address

Address

rd . . . funct

Immediate

PC

PC

+

+

Page 5: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 5

A subset of core MIPS instruction set

• Arithmetic-logical instruction(R-type,op=000 000) – add $t1, $t2, $t3; $t2+$t3->$t1

– slt $s1, $s2, $s3; if($s2<$s3)then $s1=1, else $s1=0

– 汇编指令中源操作数和目的操作数的排列顺序与机器指令字不同!

op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits) R-type

Page 6: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

A conceptual view – computational instructions

• Both source

operands and

the destination

location are in

the register

file. [rs] [rd]

[rt]

add $t1, $t2, $t3; $t2+$t3->$t1

Page 7: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

ALU Interface and Impl

Page 8: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Register:clk、Clear、Preset、write

D 锁存器:clk电平控制 一位 D 触发器:clk边沿控制

带清零和预置的D 触发器 一位寄存器,数据使能=write

Page 9: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

RegisterFile结构与表示:n位

clk、Clear、Preset、write?

Page 10: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

• 例:2R/1W , 4R/2W – Register file designs

are limited by routing and not by transistor density

– Read access time of a register file grows approximately linearly with the number of ports

bit cell of a register file

Joseph A. Fisher,“Embedded Computing - A Vliw Approach To

Architecture Compilers And Tools”,2005

Page 11: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

11

A subset of core MIPS instruction set(con’t)

• Memory-reference instruction(I-type) – lw $s1,100($s2) ;loads words, based $s2(rs) ,op=100011

– sw $s1,100($s2);stores words, based $s2(rs) ,op=101011

– 位扩展

• Branch instruction(I/J-type) – beq $1, $3, 7; if($1=$3)then taken, else untaken,op=000100

• 属“比较并分支”方式

• PC相对寻址:以npc为基址,target为16位(32位有符号扩展后,左移两位(补“00”,字对齐))。

– jump

• Pseudodirect:target为26位,左移2位成28位,拼上PC高4位。

op(6 bits) rs(5 bits) rt(5 bits) addr(16 bits)

J-type op(6 bits) addr(26 bits)

I-type

Page 12: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

位扩展正确性见“CSAPP”

• 位扩展:从较小的数据类型转换成较大的类型

– 无符号扩展(zero extension):高位补0,逻辑运算

– 符号扩展(sign extension):高位补1,补码,算术运算

• 需求:I-type指令,字长32位,立即数16位

– addi $s3,$s3,4;$s3 = $s3 + 4

– lw $t1, offset($t2); M($t2+offset) -> $t1

– beq $1, $3, 7; if($1=$3)then taken, else not taken

op(6 bits) rs(5 bits) rt(5 bits) addr/immediate (16 bits) I-type

Page 13: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

A conceptual view – immediate instructions

• One of the

source

operands is

the immediate

value in the IR. [rs]

[rt]

addi $s3,$s3,4;$s3 = $s3 + 4

Page 14: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

访存(mem access)

• 读写控制:地址、数据、命令

• 时钟同步假设:异步读,同步写

Page 15: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Datapath、Controller、Timing

Page 16: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 16

取指:数据通路,控制信号?

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n

4

A d d 指令字长32位

Page 17: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Decode,read opr

Page 18: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 18

R-type指令的执行

I n s t r u c t i o n R e g i s t e r s

W r i t e r e g i s t e r

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

W r i t e d a t a

A L U r e s u l t

A L U

Z e r o

R e g W r i t e

A L U o p e r a t i o n 3

寄存器号

• 寄存器堆操作 – 读:给出寄存器编号,则寄存器的值自动送到输出端口

– 写:需要寄存器编号和控制信号RegWrite,时钟边沿触发

– 在一个周期内,REG可以同时完成读写操作,但读出的是上一个周期写入的值

op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits) R-type

Page 19: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 19

MIPS指令数据通路总图

R e g i s t e r s

R e g i s t e r #

D a t a

R e g i s t e r #

D a t a m e m o r y

A d d r e s s

D a t a

R e g i s t e r #

P C I n s t r u c t i o n A L U

I n s t r u c t i o n m e m o r y

A d d r e s s

load

store

arithmetic

addr(offset) op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits)

op(6 bits) rs(5 bits) rt(5 bits) addr/immediate(16 bits)

op(6 bits) addr(26 bits)

Page 20: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Controller

• reset?

• 控制信号产生时刻?

Page 21: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

同步电路、clk cycle、控制信号

Page 22: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

22

指令周期的定时模式

• 单周期实现:1指令周期 = 1 时钟周期

– All stages of an instruction are completed within one long clock cycle.

– 所需控制信号同时生成

• 多周期实现:指令周期 = n 时钟周期

– Only one stage of instruction per clock cycle

– 按时钟周期(= 机器周期)生成当前周期所需控制信号

Page 23: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

单周期实现

数据通路

ALU控制

控制器

Page 24: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 24

定长单周期

• 指令周期取值、译码、执行、访存、写回

– All stages of an instruction are completed within

one long clock cycle.

– 采用时钟边沿触发方式

• 所有指令在时钟的一个边开始执行,在下一个边结束

• 控制信号在一个clk内有效

Page 25: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 25

MIPS指令数据通路Overview • 不同指令执行的多数工作都是相同的

• 与指令类型无关的操作 – 取指:将PC送往MEM,nPC = PC+1

– 取数:根据指令字中的地址域读寄存器 • 非ld/st:读两个寄存器

• ld读一个寄存器;st读两个寄存器

• 执行操作:同类指令非常类似 – 不同类型指令都要使用ALU

• 访存指令使用ALU计算地址(add)

• 算逻指令使用ALU完成计算

• 分支指令使用ALU进行条件比较(add)

– 其后,各个指令的工作就不同了 • 访存指令对存储器进行读写

• 算逻指令将ALU结果写回寄存器

• 分支指令将基于比较结果修改下一条指令的地址

Page 26: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 26

取指:数据通路,控制信号?

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n

4

A d d 指令字长32位

Page 27: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Decode,read opr

Page 28: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 28

R-type指令的执行

I n s t r u c t i o n R e g i s t e r s

W r i t e r e g i s t e r

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

W r i t e d a t a

A L U r e s u l t

A L U

Z e r o

R e g W r i t e

A L U o p e r a t i o n 3

寄存器号

• 寄存器堆操作 – 读:给出寄存器编号,则寄存器的值自动送到输出端口

– 写:需要寄存器编号和控制信号RegWrite,时钟边沿触发

– 在一个周期内,REG可以同时完成读写操作,但读出的是上一个周期写入的值

op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits) R-type

Page 29: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

单周期RegFile读写操作约定

• 访问不同寄存器:在一个周期中,RF支持两个读和一个写

• 访问同一个寄存器: –在一个周期内完成读写操作,但读出的是上一个周期写入的值

–不能同时进行读和写:写控制RegWrite与clk同步,两种写方式:

• 后写(late write):前半周期读,后半周期写。——单周期版?

• 先写(early write):与后写相反。——流水线版?

29

Write data

Page 30: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 30

访存指令的执行

• lw $t1, offset($t2); M($t2+offset) -> $t1

• sw $t1, offset($t2); $t1 -> M($t2+offset)

• 需要对指令字中的16位偏移进行32位带符号扩展

I n s t r u c t i o n

1 6 3 2

R e g i s t e r s W r i t e r e g i s t e r

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

D a t a

m e m o r y W r i t e d a t a

R e a d d a t a

W r i t e d a t a

S i g n

e x t e n d

A L U r e s u l t

Z e r o

A L U

A d d r e s s

M e m R e a d

M e m W r i t e

R e g W r i t e

A L U o p e r a t i o n 3

lw sw

rs:基址

rt:ld的目的/sw的源

Page 31: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 31

访存指令和算逻指令的数据通路综合

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n

1 6 3 2

R e g i s t e r s

W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n e x t e n d

A L U r e s u l t

Z e r o

D a t a m e m o r y

A d d r e s s

W r i t e d a t a

R e a d d a t a

M u x

4

A d d

M u x

A L U

R e g W r i t e

A L U o p e r a t i o n 3

M e m R e a d

M e m W r i t e

A L U S r c M e m t o R e g

R-type I-type

算逻or访存?

store

R-type or I-type?

Page 32: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

32

条件转移beq

• beq $t1, $t2, offset – 以分支指令的下一条指令地址nPC为基址

– addr有符号扩展后,左移2位(字对齐),形成“32位字地址”(PC相对寻址)

1 6 3 2 S i g n

e x t e n d

Z e r o A L U

S u m

S h i f t

l e f t 2

T o b r a n c h

c o n t r o l l o g i c

B r a n c h t a r g e t

P C + 4 f r o m i n s t r u c t i o n d a t a p a t h

I n s t r u c t i o n

A d d

R e g i s t e r s W r i t e r e g i s t e r

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

W r i t e d a t a

R e g W r i t e

A L U o p e r a t i o n 3

Page 33: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

33

R-/I-/J-type操作数据通路总图

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n

1 6 3 2

A d d A L U r e s u l t

M u x

R e g i s t e r s

W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S h i f t

l e f t 2

4

M u x

A L U o p e r a t i o n 3

R e g W r i t e

M e m R e a d

M e m W r i t e

P C S r c

A L U S r c

M e m t o R e g

A L U r e s u l t

Z e r o A L U

D a t a m e m o r y

A d d r e s s

W r i t e d a t a

R e a d d a t a M

u x

S i g n

e x t e n d

A d d

顺序?

分支?

PCSrc:nPC来源控制,顺序与分支二选一

“beq指令(Branch)”& “ALU的Zero状态有效”

Page 34: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 34

ALU操作控制信号

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n

1 6 3 2

A d d A L U r e s u l t

M u x

R e g i s t e r s

W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S h i f t

l e f t 2

4

M u x

A L U o p e r a t i o n 3

R e g W r i t e

M e m R e a d

M e m W r i t e

P C S r c

A L U S r c

M e m t o R e g

A L U r e s u l t

Z e r o A L U

D a t a m e m o r y

A d d r e s s

W r i t e d a t a

R e a d d a t a M

u x

S i g n

e x t e n d

A d d

Page 35: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

ALU操作控制

Page 36: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

36

ALU控制信号

Instruction

opcode

ALUop Instruction

operation

Funct

field

desired

ALU action

ALU ctrl

input

LW 00 Load word xxxxxx add 0010

sw 00 store word xxxxxx add 0010

beq 01 Branch eq xxxxxx subtract 0110

R-type 10 Add 100000 Add 0010

R-type 10 Substract 100010 Substract 0110

R-type 10 And 100100 And 0000

R-type 10 Or 100101 Or 0001

R-type 10 Set on less

than 101010 Set on less

than 0111

2位ALUop和6位func组合,产生ALU_ctrl_input(3 or 4位)——即ALU operation

op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits)

op(6 bits) rs(5 bits) rt(5 bits) addr/immediate(16 bits)

op(6 bits) addr(26 bits)

Page 37: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

37

ALU控制选择

• 两位ALUop和func组合产生ALU控制选择

M e m t o R e g

M e m R e a d

M e m W r i t e

A L U O p

A L U S r c

R e g D s t

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 2 5 – 2 1 ]

A d d

I n s t r u c t i o n [ 5 – 0 ]

R e g W r i t e

4

1 6 3 2 I n s t r u c t i o n [ 1 5 – 0 ]

0

R e g i s t e r s

W r i t e r e g i s t e r

W r i t e d a t a

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n

e x t e n d

A L U r e s u l t

Z e r o

D a t a m e m o r y

A d d r e s s R e a d d a t a

M u x

1

0

M u x

1

0

M u x

1

0

M u x

1

I n s t r u c t i o n [ 1 5 – 1 1 ]

A L U c o n t r o l

S h i f t

l e f t 2

P C S r c

A L U

A d d A L U

r e s u l t

Page 38: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 38

控制器:指令执行所需控制信号

• 哪些功能部件需要控制信号?

• 指令译码:指令格式分析 – 操作码:在31-26

– R-type:需要参考5-0

– 目的地址:需要对目的寄存器进行选择控制 • 对R-type指令,在rd;对load,在rt

Page 39: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 39

目的寄存器选择

M e m t o R e g

M e m R e a d

M e m W r i t e

A L U O p

A L U S r c

R e g D s t

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 2 5 – 2 1 ]

A d d

I n s t r u c t i o n [ 5 – 0 ]

R e g W r i t e

4

1 6 3 2 I n s t r u c t i o n [ 1 5 – 0 ]

0

R e g i s t e r s

W r i t e r e g i s t e r

W r i t e d a t a

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n

e x t e n d

A L U r e s u l t

Z e r o

D a t a m e m o r y

A d d r e s s R e a d d a t a

M u x

1

0

M u x

1

0

M u x

1

0

M u x

1

I n s t r u c t i o n [ 1 5 – 1 1 ]

A L U c o n t r o l

S h i f t

l e f t 2

P C S r c

A L U

A d d A L U

r e s u l t

Page 40: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

• op域(6位)译码产生的控制信号:8个

– RegDst:选择rt或rd作为写操作的目的寄存器

• R-type指令与 load指令二选一

– RegWrite:寄存器写操作控制

– ALUSrc:ALU的第二个操作数来源

• R-type指令与 l-type指令(含branch指令)二选一

– ALUOp:R/I-type指令(2位)

– MemRead:存储器读控制,ld指令

– MemWrite:存储器写控制,st指令

– MemtoReg:目的寄存器数据来源

• R-type指令与load指令二选一

– Branch:beq指令,

• PCSrc:nPC来源控制,PCSrc = Branch & Zero

• If taken,then PC = nPC+(sign-extend(IR[15-0] <<2)

• 每个周期都要写PC(=nPC or beq目的),因此忽略“PC写控制”

40

控制器:控制信号生成

Page 41: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 41

数据通路+主控制部件

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

I n s t r u c t i o n [ 2 0 1 6 ]

I n s t r u c t i o n [ 2 5 2 1 ]

A d d

I n s t r u c t i o n [ 5 0 ]

M e m t o R e g

A L U O p

M e m W r i t e

R e g W r i t e

M e m R e a d

B r a n c h

R e g D s t

A L U S r c

I n s t r u c t i o n [ 3 1 2 6 ]

4

1 6 3 2 I n s t r u c t i o n [ 1 5 0 ]

0

0 M u x

0

1

C o n t r o l

A d d A L U

r e s u l t

M u x

0

1

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n e x t e n d

M u x

1

A L U r e s u l t

Z e r o

P C S r c

D a t a m e m o r y

W r i t e d a t a

R e a d d a t a

M u x

1

I n s t r u c t i o n [ 1 5 1 1 ]

A L U c o n t r o l

S h i f t l e f t 2

A L U

A d d r e s s

Page 42: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

42

控制部件真值表

Signal

name

R-type lw

(31)

sw

(35)

beq

inputs op5(26) 0 1 1 0

op4 0 0 0 0

op3 0 0 1 0

op2 0 0 0 1

op1 0 1 1 0

op0(31) 0 1 1 0

outputs RegDst 1 0 x x

ALUSrc 0 1 1 0

MemtoReg 0 1 x x

RegWrite 1 1 0 0

MemRead 0 1 0 0

MemWrite 0 0 1 0

Branch 0 0 0 1

ALUop1 1 0 0 0

ALUop0 0 0 0 1 布尔表达式?

Page 43: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 43

R-type指令的执行过程

• add $t1, $t2, $t3; $t2+$t3->$t1

• 在一个周期内完成如下动作

– 第一步:取指和PC+1

– 第二步:读两个源操作数寄存器$t2和$t3

– 第三步:ALU操作

– 第四步:结果写回目的寄存器$t1

Page 44: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 44

R-type指令的执行路径

P C

I n s t r u c t i o n

m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

I n s t r u c t i o n [ 2 0 1 6 ]

I n s t r u c t i o n [ 2 5 2 1 ]

A d d

I n s t r u c t i o n [ 5 0 ]

M e m t o R e g

A L U O p

M e m W r i t e

R e g W r i t e

M e m R e a d

B r a n c h

R e g D s t

A L U S r c

I n s t r u c t i o n [ 3 1 2 6 ]

4

1 6 3 2 I n s t r u c t i o n [ 1 5 0 ]

0

0 M u x

0

1

A L U c o n t r o l

C o n t r o l

S h i f t l e f t 2

A d d A L U

r e s u l t

M u x

0

1

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n e x t e n d

M u x

1

A L U r e s u l t

Z e r o

D a t a m e m o r y

W r i t e d a t a

R e a d d a t a

M u x

1

I n s t r u c t i o n [ 1 5 1 1 ]

A L U

A d d r e s s

Page 45: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 45

lw指令的执行过程

• lw $t1, offset($t2); M($t2+offset) -> $t1

• 第一步:取指和PC+1

• 第二步:读寄存器$t2

• 第三步:ALU操作完成$t2与符号扩展后的16位offset加

• 第四步:ALU的结果作为访存地址,送往数据MEM

• 第五步:内存中的数据送往$t1

Page 46: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 46

lw指令的执行路径

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

I n s t r u c t i o n [ 1 5 – 1 1 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 2 5 – 2 1 ]

A d d

I n s t r u c t i o n [ 5 – 0 ]

M e m t o R e g

A L U O p

M e m W r i t e

R e g W r i t e

M e m R e a d

B r a n c h

R e g D s t

A L U S r c

I n s t r u c t i o n [ 3 1 – 2 6 ]

4

1 6 3 2 I n s t r u c t i o n [ 1 5 – 0 ]

0

0 M u x

0

1

A L U c o n t r o l

C o n t r o l

S h i f t

l e f t 2

A d d A L U

r e s u l t

M u x

0

1

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n e x t e n d

M u x

1

A L U r e s u l t

Z e r o

D a t a m e m o r y

W r i t e d a t a

R e a d d a t a

M u x

1

A L U A d d r e s s

Page 47: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 47

beq指令的执行过程

• beq $t1, $t2, offset

• 第一步:取指和PC+1

• 第二步:读寄存器$t1,$t2

• 第三步:ALU将$t1和$t2相减;PC+4与被左移两位并进行符号扩展后的16位offset相加,作为分支目标地址

• 第四步:ALU的Zero确定应送往PC的值

Page 48: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 48

beq的执行路径

P C

I n s t r u c t i o n m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

I n s t r u c t i o n [ 1 5 – 1 1 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 2 5 – 2 1 ]

A d d

I n s t r u c t i o n [ 5 – 0 ]

M e m t o R e g

A L U O p

M e m W r i t e

R e g W r i t e

M e m R e a d

B r a n c h

R e g D s t

A L U S r c

I n s t r u c t i o n [ 3 1 – 2 6 ]

4

1 6 3 2 I n s t r u c t i o n [ 1 5 – 0 ]

S h i f t l e f t 2

0 M u x

0

1

A L U c o n t r o l

C o n t r o l

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

S i g n e x t e n d

1

A L U r e s u l t

Z e r o

D a t a m e m o r y

W r i t e d a t a

R e a d d a t a M

u x

R e a d d a t a 2

A d d A L U

r e s u l t

M u x

0

1

M u x

1

0

A L U

A d d r e s s

Page 49: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 49

jump指令的实现

• 无条件转移,关键在于目标地址的拼装

– PC+4的最高4位

– 指令字中的26位地址

– 最低两位补00

• “拼装”:只需合并地址总线

• 译码:增加一个jump指令识别控制

J-type 2(31-26) addr(25-0)

Page 50: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 50

jump指令的实现

S h i f t l e f t 2

P C

I n s t r u c t i o n

m e m o r y

R e a d a d d r e s s

I n s t r u c t i o n [ 3 1 – 0 ]

D a t a m e m o r y

R e a d d a t a

W r i t e d a t a

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

I n s t r u c t i o n [ 1 5 – 1 1 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 2 5 – 2 1 ]

A d d

A L U r e s u l t

Z e r o

I n s t r u c t i o n [ 5 – 0 ]

M e m t o R e g

A L U O p

M e m W r i t e

R e g W r i t e

M e m R e a d

B r a n c h

J u m p R e g D s t

A L U S r c

I n s t r u c t i o n [ 3 1 – 2 6 ]

4

M u x

I n s t r u c t i o n [ 2 5 – 0 ] J u m p a d d r e s s [ 3 1 – 0 ]

P C + 4 [ 3 1 – 2 8 ]

S i g n e x t e n d

1 6 3 2 I n s t r u c t i o n [ 1 5 – 0 ]

1

M u x

1

0

M u x

0

1

M u x

0

1

A L U c o n t r o l

C o n t r o l

A d d A L U

r e s u l t

M u x

0

1 0

A L U

S h i f t l e f t 2

2 6 2 8

A d d r e s s

Page 51: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

51

jump

指令:控制部件真值表?

Signal

name

R-type lw

(31)

sw

(35)

beq

inputs op5(26) 0 1 1 0

op4 0 0 0 0

op3 0 0 1 0

op2 0 0 0 1

op1 0 1 1 0

op0(31) 0 1 1 0

outputs RegDst 1 0 x x

ALUSrc 0 1 1 0

MemtoReg 0 1 x x

RegWrite 1 1 0 0

MemRead 0 1 0 0

MemWrite 0 0 1 0

Branch 0 0 0 1

ALUop1 1 0 0 0

ALUop0 0 0 0 1

Page 52: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Single-Cycle Processor: Clocking

Page 53: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d
Page 54: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

54

View from 30,000 Feet

• What is the role of the Add units?

• Explain the inputs to the data memory unit

• Explain the inputs to the ALU

• Explain the inputs to the register unit

Note: we haven’t bothered

showing multiplexors

Source: H&P textbook

•Clock cycle的宽度?

•能否采用合体MEM?

•能否不用加法器?

•寻址方式?

•CPI =?

Page 55: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

single-cycle processor interfaced to

external memory

Digital Design and Computer

Architecture, David M. Harris, Sarah

L. Harris-MIPS 2ed-Morgan

Kaufmann (2012)

Page 56: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

访存:Cache@MIPS R3000

Page 57: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 57

指令周期定长or不定长?

• 设程序中load有24%,store有12%,R-type有44%,beq有18%,jump有2%。试比较时钟定长单周期实现和不定长单周期实现的性能。

– 程序执行时间=指令数×CPI×时钟宽度

– 定长单周期的时钟为8ns

– 不定长单周期的时钟可以是2ns~8ns。其平均指令执行时间=8×24%+7×12%+6×44%+5×18%+2×2%=6.3ns

– 因此,变长实现较定长实现快8/6.3=1.27倍

Page 58: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

指令周期:single-cycle vs. multicycle

• Multicycle Implementation:

less waste = higher performance

add $t0,$t1,$t2 beq $t0,$t1,L

Single-cycle Implementation:定长指令周期

Multicycle Implementation:不定长指令周期

add $t0,$t1,$t2 beq $t0,$t1,L

waste waste

clock

clock

Page 59: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 59

多周期实现(见COD3,COD4后无)

• 根据指令执行所使用的功能部件,将执行过程划分成多个阶段,每个阶段一个时钟周期

– 在一个周期内的各个部件并行工作

• 复用:功能部件可以在不同的阶段(周期)

– 有利于降低硬件实现复杂度和成本

• 竞争:结构冲突

– 中间结果暂存

• 时钟周期确定:工作量均衡

– 假设一个周期内可以完成

• 一次MEM访问,or

• 一次寄存器访问(2 reads or one write),or

• 一次ALU操作

Page 60: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 60

指令执行的阶段划分

• 共5个阶段 – 取指:取指,PC+1

– 译码:译码阶段、计算beq目标地址

– 执行:R-type指令执行、访存地址计算,分支完成阶段

– 访存:lw读,store和R-type指令完成阶段

– 写回:lw完成阶段

• 注意 – 指令周期不定长:分别为3、4、5个时钟周期

– 时钟周期定长: • 时钟周期标识:控制器据此发出所需控制信号——clk gating

Page 61: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Overview:复用,暂存

Page 62: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 62

多周期数据通路

S h i f t l e f t 2

P C

M e m o r y

M e m D a t a

W r i t e d a t a

M u x

0

1

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

M u x

0

1

M u x

0

1

4

I n s t r u c t i o n [ 1 5 – 0 ]

S i g n e x t e n d

3 2 1 6

I n s t r u c t i o n [ 2 5 – 2 1 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 1 5 – 0 ]

I n s t r u c t i o n r e g i s t e r

1 M u x

0

3

2

M u x

A L U r e s u l t

A L U

Z e r o

M e m o r y d a t a

r e g i s t e r

I n s t r u c t i o n [ 1 5 – 1 1 ]

A

B

A L U O u t

0

1

A d d r e s s

取指 数据访问 PC+4

lw/sw beq sw

Page 63: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 63

多周期控制信号

S h i f t l e f t 2

M e m t o R e g

I o r D M e m R e a d M e m W r i t e

P C

M e m o r y

M e m D a t a

W r i t e d a t a

M u x

0

1

R e g i s t e r s W r i t e r e g i s t e r

W r i t e d a t a

R e a d d a t a 1

R e a d d a t a 2

R e a d r e g i s t e r 1

R e a d r e g i s t e r 2

I n s t r u c t i o n [ 1 5 – 1 1 ]

M u x

0

1

M u x

0

1

4

A L U O p A L U S r c B

R e g D s t R e g W r i t e

I n s t r u c t i o n [ 1 5 – 0 ]

I n s t r u c t i o n [ 5 – 0 ]

S i g n e x t e n d

3 2 1 6

I n s t r u c t i o n [ 2 5 – 2 1 ]

I n s t r u c t i o n [ 2 0 – 1 6 ]

I n s t r u c t i o n [ 1 5 – 0 ]

I n s t r u c t i o n r e g i s t e r

1 M u x

0

3

2

A L U c o n t r o l

M u x

0

1 A L U

r e s u l t

A L U

A L U S r c A

Z e r o A

B

A L U O u t

I R W r i t e

A d d r e s s

M e m o r y d a t a

r e g i s t e r

op(6 bits) rs(5 bits) rt(5 bits) rd(5 bits) shamt(5 bits) funct(6 bits)

op(6 bits) rs(5 bits) rt(5 bits) addr/immediate(16 bits)

op(6 bits) addr(26 bits)

Page 64: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

64

第一阶段:取指 • 取指

– 根据PC从MEM中取指,IR=MEM[PC] – 计算NPC,PC=PC+4 – 控制信号:IorD, MemRead, IRWrite;ALUSrcA, ALUSrcB,

ALUOp, PCWrite

Page 65: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 65

第二阶段:译码

• 指令译码和读寄存器 – 将rs和rt送往A和B:A=Reg[IR[25-21]], B=Reg[IR[20-16]]

• 控制信号:无?

Page 66: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

66

第三阶段:R-type执行、访存地址计算

• 依赖于指令类型

– R-type指令

• ALUOut= A op B

– 访存指令:计算访存地址

• ALUOut=A+(sign-extend(IR[15-0] )

• 需要的控制信号?

Page 67: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 67

第四阶段,第五阶段

• 第四阶段:R-type和sw完成、lw读阶段

– R-type完成:结果写回 • Reg[IR[15-11]] = ALUOut

– sw完成:写入MEM • MEM[ALUOut] = B

– lw读: • MDR = MEM[ALUOut]

• 第五阶段:lw写阶段

– lw写回:Reg[IR[15-11]] = MDR

• 所需的控制信号?

Page 68: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

beq指令数据通路

• 如果beq三个周期完成

– 取指,译码,执行(结构冲突)

Page 69: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

beq执行过程

• 译码周期: ALUSrcA, ALUSrcB, ALUOp

– 计算beq目标:ALUOut = nPC+(sign-extend(IR[15-0] <<2)

• 此时尚不知是何指令,所以读寄存器和计算分支地址可能无效,但

亦无害,可以节省后面的操作

• 执行周期:beq完成,if (A == B) PC=ALUOut;路径?

Page 70: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

jump

• 执行:

– PC = PC[31-28] || (IR[25-0] << 2) ;路径?

Page 71: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 71

nPC产生时刻与控制信号

• 3种情况:顺序,beq,jump – IF:PC=ALU=PC+4 ;PCWrite,PCSource=00

– EXE: • beq:PC=ALUOut=目的;PCWriteCond,PCSource=01

• jmp:伪直接寻址;PCWrite,PCSource=10

Page 72: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 72

多周期总图

Shift left 2

PC

M u x

0

1

RegistersWrite register

Write data

Read data 1

Read data 2

Read register 1

Read register 2

Instruction [15– 11]

M u x

0

1

M u x

0

1

4

Instruction [15– 0]

Sign extend

3216

Instruction [25– 21]

Instruction [20– 16]

Instruction [15– 0]

Instruction register

ALU control

ALU result

ALU

Zero

Memory data

register

A

B

IorD

MemRead

MemWrite

MemtoReg

PCWriteCond

PCWrite

IRWrite

ALUOp

ALUSrcB

ALUSrcA

RegDst

PCSource

RegWrite

Control

Outputs

Op [5– 0]

Instruction [31-26]

Instruction [5– 0]

M u x

0

2

Jump address [31-0]Instruction [25– 0] 26 28

Shift

left 2

PC [31-28]

1

1 M u x

0

3

2

M u x

0

1ALUOut

Memory

MemData

Write data

Address

npc

beq

Page 73: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Multicycle 指令时序

• 各指令完成时间

Page 74: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Control unit internal structure

• 某些信号名与COD不一致

Digital Design and Computer Architecture,2012

Page 75: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 75

FSM控制部件实现 • Moore型(Edward Moore),Mealy型(George Mealy)

– Moore型速度快(输出与输入无关,可以在周期开始处就发出控制信号)。一步延迟(one-step-delay)。输出与时钟完全同步。

– Mealy型电路较小。

– 两种状态机可以相互转换。

• EDA工具可以根据FSM自动综合生成控制器

Next-state

functionCurrent state

Clock

Output

function

Next

state

Outputs

Inputs

存储部件

组合逻辑

Page 76: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

76

多周期MooreFSM

• 每个时钟周期(机器周期)一个状态

P C W r i t e

P C S o u r c e = 1 0

A L U S r c A = 1

A L U S r c B = 0 0 A L U O p = 0 1

P C W r i t e C o n d P C S o u r c e = 0 1

A L U S r c A = 1 A L U S r c B = 0 0 A L U O p = 1 0

R e g D s t = 1 R e g W r i t e

M e m t o R e g = 0

M e m W r i t e I o r D = 1

M e m R e a d

I o r D = 1

A L U S r c A = 1

A L U S r c B = 1 0 A L U O p = 0 0

R e g D s t = 0 R e g W r i t e

M e m t o R e g = 1

A L U S r c A = 0

A L U S r c B = 1 1 A L U O p = 0 0

M e m R e a d

A L U S r c A = 0 I o r D = 0 I R W r i t e

A L U S r c B = 0 1

A L U O p = 0 0 P C W r i t e

P C S o u r c e = 0 0

I n s t r u c t i o n f e t c h I n s t r u c t i o n d e c o d e /

r e g i s t e r f e t c h

J u m p c o m p l e t i o n

B r a n c h c o m p l e t i o n E x e c u t i o n

M e m o r y a d d r e s s

c o m p u t a t i o n

M e m o r y a c c e s s

M e m o r y a c c e s s R - t y p e c o m p l e t i o n

W r i t e - b a c k s t e p

( O p =

' J ' )

( O p =

' L W

' )

4

0 1

9 8 6 2

7 5 3

S t a r t

Page 77: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

Complete multicycle MIPS processor

David M. Harris,Digital Design and Computer Architecture,2012

Page 78: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

多周期CPI

• 设SPECINT2000中,load为25%(取byte占1%,取word为24%),store为10%(存byte为1%,存word为9%),branch为11%(6%beq,5%bne),jump为2%(1%jal+1%jr),52%ALU,则 – CPI=总时钟周期数/总指令数

– 指令周期定长时:CPI=5.0

– 指令周期不定长时:CPI=4.12 • CPI = CPU clock cycles / Instruction count

= 0.25×5+0.10×4+0.52×4+0.11×3+0.02×3 = 4.12

– 单周期MIPS:CPI=1.0

– CPI越小越好?

– 指令阶段越细,周期越多(越小)越好?

– IPC=?

Page 79: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

思考 • 本书有哪些逻辑设计惯例(约定)?

– 功能部件,时钟方法(clocking methodology)

– A、B、MDR、ALUOut等寄存器无写控制,PC、RF、IR有?

– 为何需要MDR(如果MEM=>RF,则ld可少一周期)?

• 为何采用多周期?应该几个周期?

• 每个周期需要哪些控制信号?

• 每个周期有哪些部件空闲(做无用功)?

• PCWrite与PCWriteCond何时有效?

• 执行分支指令时,PC执行了几次写操作?

• 关于beq

– 为何在ID计算目的地址?

– 在EXE计算地址,MEM比较完成,ok?状态机?CPI?

– 先比较,后计算地址,ok?

• jmp在ID完成?

• 为何单周期中,PC无写控制,多周期有?

• 哪些寄存器程序员不可见?

Shift left 2

PC

M u x

0

1

RegistersWrite register

Write data

Read data 1

Read data 2

Read register 1

Read register 2

Instruction [15– 11]

M u x

0

1

M u x

0

1

4

Instruction [15– 0]

Sign extend

3216

Instruction [25– 21]

Instruction [20– 16]

Instruction [15– 0]

Instruction register

ALU control

ALU result

ALU

Zero

Memory data

register

A

B

IorD

MemRead

MemWrite

MemtoReg

PCWriteCond

PCWrite

IRWrite

ALUOp

ALUSrcB

ALUSrcA

RegDst

PCSource

RegWrite

Control

Outputs

Op [5– 0]

Instruction [31-26]

Instruction [5– 0]

M u x

0

2

Jump address [31-0]Instruction [25– 0] 26 28

Shift

left 2

PC [31-28]

1

1 M u x

0

3

2

M u x

0

1ALUOut

Memory

MemData

Write data

Address

Page 80: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d
Page 81: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 81

小结 • 单周期与多周期

– 单周期:在一个周期内完成指令的所有操作 • 周期宽度如何确定?

• 能否在一个clk内完成?

– 多周期:一个周期完成指令的一步 • 与单周期的区别:功能部件复用,中间结果保存

– 可以实现不定长指令周期,提高性能

– 能否采用单周期的数据通路?

• 按当前周期(标识?)产生响应的控制信号 – 何时刷新PC:指令周期中PC保持不变如何实现?

• 作业: 1. 4.1,4.9

2. 分析MIPS三种类型指令的多周期设计方案中每个周期所用到的功能部件。

Page 82: The MIPS Processor Implementation: Datapath & Controlstaff.ustc.edu.cn/~llxx/cod/courseware/03_MIPS_Processor... · 2020-03-09 · PC + 4 from instruction datapath Instruction A d

[email protected] 82/94