A Single-Cycle MIPS Processor
A Single-Cycle MIPS Processor
1
Single-cycle implementation
3
Decoding instructions (R-type)
4
Executing instructions (R-type)
1. Read an instruction from the instruction memory
2. The source registers, specified by instruction fields rs and rt, should be
read from the register file
3. The ALU performs the desired operation
4. Its result is stored in the destination register, which is specified by field
rd of the instruction word
RegWrite
op rs rt rd shamt func
31 26 25 21 20 16 15 11 10 6 5 0
5
Decoding I-type instructions
The lw, sw and beq instructions all use the I-type encoding
— rt is the destination for lw, but a source for beq and sw
— address is a 16-bit signed constant (can be ALU source, sign-extended)
op rs rt address
6 bits 5 bits 5 bits 16 bits
RegWrite
MemWrite MemToReg
Read Instruction I [25 - 21]
Read Read
address [31-0]
register 1 data 1
ALU Read Read 1
I [20 - 16]
Read Zero address data M
Instruction
register 2 Read 0 u
memory 0 Result Write
data 2 M x
M Write address
register u Data 0
u x Write
x Registers memory
I [15 - 11] Write 1 ALUOp data
1 data
MemRead
ALUSrc
RegDst
I [15 - 0] Sign
extend
6
MemToReg
Another mux needed for the register file’s “write data”: it must be able
to store either the ALU output of R-type instructions, or the data
memory output for lw
RegWrite
MemWrite MemToReg
Read Instruction I [25 - 21]
Read Read
address [31-0]
register 1 data 1
ALU Read Read 1
I [20 - 16]
Read Zero address data M
Instruction
register 2 Read 0 u
memory 0 Result Write
data 2 M x
M Write address
register u Data 0
u x Write
x Registers memory
I [15 - 11] Write 1 ALUOp data
1 data
MemRead
ALUSrc
RegDst
I [15 - 0] Sign
extend
7
RegDst
A final annoyance is the destination register of lw is in rt instead of rd
op rs rt address
lw $rt, address($rs)
We’ll add one more mux, controlled by RegDst, to select the destination
register from either instruction field rt (0) or field rd (1)
RegWrite
MemWrite MemToReg
Read Instruction I [25 - 21]
Read Read
address [31-0]
register 1 data 1
ALU Read Read 1
I [20 - 16]
Read Zero address data M
Instruction
register 2 Read 0 u
memory 0 Result Write
data 2 M x
M Write address
register u Data 0
u x Write
x Registers memory
I [15 - 11] Write 1 ALUOp data
1 data
MemRead
ALUSrc
RegDst
I [15 - 0] Sign
extend
8
Branches
For branch instructions, the constant is not an address but an instruction
offset from the next program counter to the desired address
The target address L is three instructions past the or, so the encoding of
the branch instruction has 0000 0000 0000 0011 for the address field
Instructions are four bytes long, so the actual memory offset is 12 bytes
9
The steps in executing a beq
1. Fetch the instruction, like beq $at, $0, offset, from memory
2. Read the source registers, $at and $0, from the register file
4. If the XOR result is 0, the source operands were equal and the PC should
be loaded with the target address, PC + 4 + (offset x 4)
5. Otherwise the branch should not be taken, and the PC should just be
incremented to PC + 4 to fetch the next instruction sequentially
10
Branching hardware
0
M
Add u
x
PC 4
Add 1
Shift
left 2
PCSrc
RegWrite
MemWrite MemToReg
Read Instruction I [25 - 21]
Read Read
address [31-0]
register 1 data 1
ALU Read Read 1
I [20 - 16]
Read Zero address data M
Instruction
register 2 Read 0 u
memory 0 Result Write
data 2 M x
M Write address
register u Data 0
u x Write
x Registers memory
I [15 - 11] Write 1 ALUOp data
1 data
MemRead
ALUSrc
RegDst
I [15 - 0] Sign
extend
11
Datapath for a single-cycle MIPS implementation
0
M
Add u
x
PC 4
Add 1
Shift
left 2
PCSrc
RegWrite
MemWrite MemToReg
Read Instruction I [25 - 21]
Read Read
address [31-0]
register 1 data 1
ALU Read Read 1
I [20 - 16]
Read Zero address data M
Instruction
register 2 Read 0 u
memory 0 Result Write
data 2 M x
M Write address
register u Data 0
u x Write
x Registers memory
I [15 - 11] Write 1 ALUOp data
1 data
MemRead
ALUSrc
RegDst
I [15 - 0] Sign
extend
12
Control
The control unit is responsible for setting all the control signals so that
each instruction is executed properly
— control unit’s input is the 32-bit instruction word
— outputs are values for the blue control signals in the datapath
— most signals can be generated from the instruction opcode alone
13