Implementing a pipeline CPU using the Xilinx design package for FPGAs

9.Control Hazards and Delayed Branch

Save Time On Research and Writing
Hire a Pro to Write You a 100% Plagiarism-Free Paper.
Get My Paper

The control hazard occurs when a pipelined CPU executes a branch or jump instruction. The jump target address a jump instruction (jr, j, or jal) can be determined in the ID stage and it will be written into PC at the end of the ID stage. But because the pipelined CPU fetches instruction during every clock cycle, the next instruction is being fetched during the ID stage of the jump instruction. The control hazard caused by a conditional branch instruction (beq or bne) becomes more serious than that of a jump instruction because the condition must be evaluated in addition to the calculation of the branch of the target address. Figure 1 shows an example when we calculate the branch target address in the EXE or the ID stage respectively. There are mainly two methods to deal with the instruction(s) next to branch or jump instruction. One method is to cancel it (them). The other is to let it (them) be executed. The second method is called a delayed branch. The position in between the location of a jump or branch instruction and the jump or branch target address are called delay slots. MIPS (microprocessor without interlocked pipeline stages) ISA (instruction set architecture) adopts a one delay slot mechanism: the instruction located in delay slot is always executed no matter wither the branch is taken or not as shown in figure 2. In figure 2 (a) shows the case where the branch is not taken. Figure 2 (b) shows the case where the branch is taken; t is the branch target address. In both cases, the instruction located in a+4 (delay slot) is always executed no matter whether the branch is taken or not. In order to implement the delayed branch with one delay slot, we must let the conditional branch instructions finish the executions in the ID stage. There should be no problem for calculating the branch target address within the ID stage. For checking the condition, we can perform an exclusive OR (XOR) on the two source operands:  rsrtequ = ~| (da^db);    // (da == db)  where the rsrtequ signal indicates where da or db are equal or not. Both da and db should be the state of the art data. Referring to figures 3 and 4, we use the outputs of the multiplexers for internal forwarding as da and db. This is the reason why we put the forwarding to the ID stage instead of to the EXE stage. Because the delayed branch, the return address of the MIPS jal instruction is PC+8. Figure 5 illustrates the execution of the jal instruction. The instruction located in delay slot (PC + 4) was already executed before transferring control to a function (or a subroutine). The return address should be PC+8, which is written into $31 register in the WB stage by the jal instruction. The return form subroutine can be done by the instruction of jr $31. The jr rs instruction reads the content of register rs and writes it into the PC.    Figure 1 Determining whether a branch is taken or not taken (b) Branch is determined in ID stagebeq ID EXE? ? ? ? ? ? ? ? ? ID EXEbeq ID? ? ? ? ? ID EXETarget address: ID EXE MEMIFTarget address:IFIF(a)BranchisdeterminedinEXEstage.Penn    StateUniversity School    of    Electrical    Engineering    and    Computer    Science Page    3of    8Figure 2 Delayed branch  Figure 3 Implementation with delayed branch with one delay slot  (b) Branch is takena:a+4:a+8:a+12:a:a+4:t:t+4:beq IDID EXEbeq IDID EXEIF ID EXE MEMIF ID EXE MEM WBIF ID EXE MEMIF ID EXE MEM WBIFIF(a)Branchisnottaken4clkIF IDopfuncControlunit fwdbfwdapcsrc rsrtequimmaddreqursrtEXEdadb0132013201324a doInstmempc rna qarnbqbdwnweRegfile<<<

Still stressed from student homework?
Get quality assistance from academic writers!

Order your essay today and save 25% with the discount code LAVENDER