PIC32MZ Instruction Pipeline

Last modified by Microchip on 2023/11/09 09:01

Introduction

The control unit in all Central Processing Units (CPUs) follows the same basic instruction processing sequence:

  1. Fetch the instruction
  2. Decode the instruction
  3. Execute the instruction

On traditional CPUs, these phases are typically executed sequentially as shown:

Modern, high-performance CPUs (like MIPS®) use a technique called Pipelining, whereby these phases of instruction processing are executed in independent overlapping stages, as shown in the accompanying image:

Sequential Instruction Execution

N-stage pipelines, therefore, have n-instructions at different stages of execution moving through the pipeline, similar to an automotive assembly line.

​Pipelines do not improve the instruction execution time (we still need to fetch+decode+execute), rather, they improve the instruction throughput. In our example, we increased the instruction throughput by 3x via the 3-stage pipeline.

Back to top

PIC32MZ Pipeline

The MIPS microAptive® MPU Core implements a five-stage pipeline as shown in the accompanying image:

PIC32MZ Pipelined Instruction Execution

  • I-STAGE: INSTRUCTION FETCH
    • Fetch the instruction from cache/instruction SRAM. Increment the PC by four (4).
  • E-STAGE: EXECUTION
    • Fetch operands from the register file. Begin Arithmetical Logical Unit (ALU) operation. Calculate branch target address. Begin MDU operation.
  • M-STAGE: MEMORY FETCH
    • ALU Op Completes. Data cache/SRAM access is performed for load/store operations. MDU operations proceed.
  • A-STAGE: ALIGN
    • Align loaded data with its word boundary. Load data or MDU result to E-STAGE for bypassing.
  • W-STAGE: WRITEBACK
    • For register-to-register or load instructions, the result is written back to the register file.

​In a multi-stage pipelined implementation, certain operations can lead to interlocks and hazards. The effect of these is to reduce the instruction throughput. The microAptive MPU core has been designed to minimize the effects of these hazards.

Back to top

Data Hazards

The term data hazard refers to the situation where results from prior ALU operations are required before they have been written back to the register file.

In the following example, the result of the first operation (in register t3) is required as input for execution of the second operation:

1 add     t3, t2, t1    /* t3 = t2 + t1 */
2 sub     t4, t3, t7    /* t4 = t3 - t7 (HAZARD: t3 not written yet!) */

The microAptiv® Microprocessor cores implement a bypass mechanism that allows the result of an operation to be sent directly to the instruction that needs it without having to write the result to the register, and then read it back.

The accompanying diagram depicts the PIC32MZ Datapath, showing the A » E and M » E stage bypass connections:

PIC32MZ Datapath

The following pipeline diagram depicts how the result (t3) can be made available after the M-STAGE to the following instruction without stalling the pipeline:

Back to top

Control Hazards

Control hazards arise in pipelined CPU architectures whereby instructions that follow branch instructions are fetched by the control hardware, and (traditionally) are flushed if the branch is taken, reducing instruction throughput.

microAptiv MCU cores implement a delay slot in the pipeline and include branch target address calculation hardware in the E-stage of the pipeline.

For branch operations, these result in:

  • Having an instruction slot available after all branch instructions to do useful work.
  • Guaranteeing that the pipeline fetches either the branch taken instruction or fall-through instruction in the cycle immediately following the delay slot.

Since the branch cannot take effect until the second stage of the pipeline, we say that it is a delayed branch (i.e. the branch/jump will take place after the instruction following the branch is in the pipeline).

The following pipeline diagram demonstrates how either the branch taken instruction (label L2) or the fall-through instruction (label L1) is executed immediately after the delay slot instruction (sw t0, 0(sp)):

Active A to E Stage Bypass on Data Hazard

​Summary: The PIC32MZ pipeline begins the fetch of either the branch path or the fall-through path in the cycle following the delay slot. The MIPS programmer (or compiler) must organize their code to perform some useful work during this delay slot, or simply insert a nop instruction.

Back to top