======== START LECTURE #15 ========

#### Branch on equal (beq)

Compare two registers and branch if equal.

• To check for equal we subtract and test for zero (our ALU does this).
• If \$r=\$s, the target of the branch beq \$r,\$s,disp is the sum of
1. The program counter PC after it has been incremented, that is the address of the next sequential instruction
2. The 16-bit immediate constant ``disp'' (treated as a signed number) left shifted 2 bits.
• The value of PC after the increment is available. We computed it in the basic instruction fetch datapath.
• Since the immediate constant is signed it must be sign extended.

Homework: What would happen if the RegWrite line had a stuck-at-0 fault (was always deasserted)? What would happen if the RegWrite line had a stuck-at-1 fault (was always asserted)?

• The top ``alu symbol'' labeled ``add'' is just an adder so does not need any control
• The shift left 2 is not a shifter. It simply moves wires and includes two zero wires. We need a 32-bit version. Below is a 5 bit version.

## 5.3: A Simple Implementation Scheme

We will just put the pieces together and then figure out the control lines that are needed and how to set them. We are not now worried about speed.

We are assuming that the instruction memory and data memory are separate. So we are not permitting self modifying code. We are not showing how either memory is connected to the outside world (i.e. we are ignoring I/O).

We have to use the same register file with all the pieces since when a load changes a register a subsequent R-type instruction must see the change, when an R-type instruction makes a change the lw/sw must see it (for loading or calculating the effective address, etc.

We could use separate ALUs for each type but it is easy not to so we will use the same ALU for all. We do have a separate adder for incrementing the PC.

#### Combining R-type and lw/sw

The problem is that some inputs can come from different sources.

1. For R-type, both ALU operands are registers. For I-type (lw/sw) the second operand is the (sign extended) immediate field.
2. For R-type, the write data comes from the ALU. For lw it comes from the memory.
3. For R-type, the write register comes from field rd, which is bits 15-11. For sw, the write register comes from field rt, which is bits 20-16.

We will deal with the first two now by using a mux for each. We will deal with the third shortly by (surprise) using a mux.

#### Including instruction fetch

This is quite easy

#### Finally, beq

We need to have an ``if stmt'' for PC (i.e., a mux)

Homework: 5.5 (just the datapath, not the control), 5.8 (just the datapath, not the control), 5.9.