Ca HW5
Ca HW5
1 Modules
1.1 Adder
The adder takes two 32-bit numbers, add1 i and add2 i, and outputs a 32-bit number, result o,
which is the sum of add1 i and add2 i.
lw 002 10002
sw 002 10002
Page 1
Homework 5 B11902027 Computer Architecture
1.3 ALU
The ALU uses alucon i, generated by the ALU Control module, to determine the current oper-
ation. Through a series of if-else statements, it processes each operation and outputs the result via
result o. Note that the beq instruction is not handled in this module, as it is managed in the ID
stage.
1.5 Control
In this section, we have two inputs: opcode i and NoOp o. There are seven outputs: regwrite o,
alusrc o, aluop o, memtoreg o, memread o, memwrite o, and Branch o.
• For regwrite o, it is set to 02 for the beq and lw instructions, and 10 for all other instructions.
• For alusrc o, it is set to 12 for instructions that include an immediate value. For the remaining
instructions, it is set to 02 .
• For aluop o, it is set to 112 for I-type instructions, 102 for R-type instructions, and 012 or 002
for load and store instructions. Note that for branch instructions, it is ”don’t care” since these
are completed in the ID stage.
• For memtoreg o, 12 is set only for the lw instruction. For all other instructions, it is set to 02 .
• For memread o, 12 is set only for the lw instruction. For all other instructions, it is set to 02 .
• For memwrite o, 12 is set only for the sw instruction. For all other instructions, it is set to 02 .
• For Branch o, 12 is set only for the beq instruction. For all other instructions, it is set to 02 .
Note that when the input NoOp o is 12 , regwrite o, memtoreg o, memread o, memwrite o, and
Branch o are all set to 02 to perform a No-Operation.
1.6 CPU
In this part, we construct the datapath based on the SPEC of lab2. Unlike the datapath in
lab2, the branch predictor is placed in the EX stage. The prediction result is passed to the ID stage
and is also forwarded as the previous prediction to the IDEX pipeline register for detecting incorrect
predictions in the EX stage. A flush signal is provided to the IFID and IDEX pipeline registers to
manage flushes caused by incorrect predictions. Additionally, two new multiplexers are introduced in
the ID stage to handle branching issues.
Furthermore, the program counters for both branch-taken and branch-not-taken situations are
passed to the IDEX pipeline register, allowing for the correction of errors caused by incorrect branch
Page 2
Homework 5 B11902027 Computer Architecture
predictions. If an incorrect prediction is detected in the EX stage, the program counters for branch-
taken and branch-not-taken are sent back to the newly added multiplexer in the IF stage. By utilizing
the aforementioned flush signal, previous prediction, ALU result, and branching signal, we can con-
struct the selector signal for the two newly added multiplexers.
1.7 Forwarding
In this module, we check whether data forwarding is needed from the MEM stage or the WB stage
to the EX stage. Specifically, we determine if the registers being operated on in the EX stage match
those in the MEM or WB stages for both rs1 and rs2. If they match and the register in the MEM or WB
stage is being written to, it indicates the need for data forwarding.
Note that if data forwarding is required from both stages, priority is given to forwarding from the
MEM stage over the WB stage. This is because the value produced by the MEM stage will overwrite the
one in the WB stage. If data forwarding is not needed, the output will be set to 002 . If data forwarding
is required from the MEM stage, the output will be set to 102 , and if it is needed from the WB stage, the
output will be set to 012 . Referring to the pseudo code provided in the SPEC will help implement
this part effectively.
• Data Hazard: A data hazard occurs when a register is used immediately after a load instruc-
tion. To detect this type of hazard, we check if the register referenced by rs1 and rs2 in the ID
stage is the same as the register used in the load instruction in the EX stage. If this condition is
met, we will take the following actions:
If we don’t detect a data hazard, we will set NoOp o, Stall o, and PCWrite o to 02 , 02 , and 12 ,
respectively.
• Control Hazard: A control hazard occurs when the condition of the branch instruction, beq
in this assignment, is met. In my implementation, the control hazard is not detected using the
hazard detection unit, but it is mentioned here for simplicity. The following are the steps for
checking and tackling the control hazard:
– Checking if a control hazard occurs: Using the Equal module and the output Branch o
from the control unit to check if a control hazard occurs. If the values stored in the registers
rs1 and rs2 are the same, and the value of Branch o is 12 , then a control hazard occurs.
– Tackling the control hazard: We use the output value of the IMMED GEN module to
change the value of the PC. Additionally, we set the input wire flush i to 12 to flush the
already fetched instruction stored in the IF/ID pipeline register.
Page 3
Homework 5 B11902027 Computer Architecture
1.10 MUX32
For MUX32, we check the input alusrc i to determine the value to output through output o. If
alusrc i is 02 , then output o will take the value of input0 i; otherwise, if alusrc i is 12 , output o
will take the value of input1 i.
1.11 MUX32 2
The MUX32 2 is a multiplexer with three input data lines. By checking the value of the input wire
alusrc i, we select the corresponding input data to output through output o. When the value of
alusrc i is 002 , 012 , and 102 , we select the first, second, and third input data to output, respectively.
2 Difficulties
2.1 Naming wires and registers
There are numerous wires and registers in this assignment, which often caused confusion. In
the end, I decided to add a suffix to each wire and register to indicate which pipeline register they
originated from. The downside of this method is that the names of the variables become very long.
3 Environment
• Operating System: MacOS Sonoma version 14.6.1
Page 4