Salta al contenuto
Note per Studenti Fetch-Execute Cycle and Datapath Report

Fetch-Execute Cycle and Datapath

In questa pagina 7
In questa pagina 4

Registers involved

Register Content
PC (Program Counter) address of the next instruction
IR (Instruction Register) instruction being executed
MAR (Memory Address Register) address sent to memory on the address bus
MBR (Memory Buffer Register, or MDR) datum read from or to be written to memory
AC (accumulator) operand and result in accumulator machines

The two phases

Definition (fetch-execute cycle).

  1. Fetch: MAR←PC\text{MAR}\leftarrow\text{PC}; memory reads; MBR←M[MAR]\text{MBR}\leftarrow M[\text{MAR}]; IR←MBR\text{IR}\leftarrow\text{MBR}; PC←PC+\text{PC}\leftarrow\text{PC}+{}instruction length (ARM and MIPS: +4+4 bytes).
  2. Execute: the control unit decodes the IR and performs the action.

Example. With PC=300\text{PC}=300, fetch gives IR=1940\text{IR}=1940 and PC=301\text{PC}=301; execute loads the content of cell 940940 into AC.

Actions are of four kinds:

Kind Example
processor-memory load a datum from memory into a register
processor-I/O read from a peripheral
data processing add, AND, compare in the ALU
control jump: writes a new value into the PC

One instruction may combine several kinds.

Instruction-cycle state diagram

A complete cycle, in general, goes through:

  1. instruction address calculation;
  2. instruction fetch;
  3. operation decoding;
  4. operand address calculation (if the operand is in memory or I/O);
  5. operand fetch (possibly repeated for several operands);
  6. operation execution;
  7. result address calculation and write;
  8. interrupt check: if a request is pending, the state is saved and control jumps to the service routine; otherwise return to step 1 (see Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →).

With indirect addressing (Instruction Formats and Addressing ModesAddressing modes (immediate, direct, indirect, register, register indirect, displacement, PC-relative, scaled indexed, stack) with effective address and a numeric example, fixed versus variable instruction formats, the MIPS R, I and J formats and the 32-bit ARM data-processing format with the encodable-constant rule.Instruction Formats and Addressing Modes →) an extra fetch of the effective address is added. Interrupts are checked between instructions, never during one.

Worked example on an accumulator machine

16-bit instructions (4-bit opcode, 12-bit address); codes 1 = load AC from memory, 5 = add memory to AC, 2 = store AC to memory. Program adding cells 940 and 941 and putting the result in 941 (addresses in hexadecimal):

Address Content Meaning
300 1940 AC ← M[940]
301 5941 AC ← AC + M[941]
302 2941 M[941] ← AC
940 0003 data
941 0002 data

Evolution: PC = 300 → IR = 1940, AC = 3, PC = 301 → IR = 5941, AC = 3 + 2 = 5, PC = 302 → IR = 2941, M[941] = 5, PC = 303. Three instruction cycles, six memory accesses (three instruction fetches and three data accesses).

Micro-operations

Definition (micro-operations). Each phase of the cycle is a sequence of micro-operations, elementary register transfers occurring in one clock cycle. Fetch:

  1. t1t_1: MAR←PC\text{MAR}\leftarrow\text{PC}
  2. t2t_2: MBR←M[MAR]\text{MBR}\leftarrow M[\text{MAR}], PC←PC+4\text{PC}\leftarrow\text{PC}+4
  3. t3t_3: IR←MBR\text{IR}\leftarrow\text{MBR}

Micro-operations that do not conflict (do not use the same resource) can share a cycle.

In t2t_2 the memory read and the PC increment use different resources, so fetch takes 3 cycles.

Datapath of a simple RISC core

Microarchitecture is how an ISA is implemented in hardware. It splits into the datapath (elements that store and transform data) and the control unit (Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →). The model is a simplified RISC with arithmetic, load/store and conditional branch (MIPS-like; valid for a subset of ARM).

Element Type Function
PC register instruction address
instruction memory combinational on read gives the instruction at address PC
PC + 4 adder combinational next sequential address
register file 2 reads, 1 write operands and results
sign extension combinational extends the immediate from 16 (or 12) to 32 bits
ALU (Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →) combinational computation, addresses, comparisons
data memory read/write load and store
multiplexers combinational choose between sources

Definition (instruction paths in the datapath).

  • Arithmetic (add rd, rs, rt): PC → instruction memory → read rs, rt → ALU → write rd; PC ← PC + 4.
  • Load (lw rt, off(rs)): read rs → ALU computes rs + sext(off) → data memory read → write rt. The longest instruction: it uses all elements.
  • Store (sw rt, off(rs)): as load up to the address; the datum read from rt goes to data memory for writing; no register is written.
  • Conditional branch (beq rs, rt, off): the ALU computes rs - rt; if output Zero is 1, PC becomes PC + 4 + (off · 4), otherwise PC + 4. A second adder computes the target.

Example: beq with off=3 and Zero =1=1: PC←PC+4+3⋅4=PC+16\text{PC}\leftarrow\text{PC}+4+3\cdot4=\text{PC}+16.

Multiplexers needed: second ALU input, register rt or extended immediate (ALUSrc); data to write back, ALU result or memory data (MemtoReg); destination register, rd or rt (RegDst); new PC, PC + 4 or branch target (PCSrc = Branch AND Zero).

Single cycle

Formula (single-cycle clock period). Every instruction takes one clock cycle; the period must fit the slowest instruction. With delays memory 200 ps, register file 100 ps, ALU 200 ps:

Instruction Instr. mem. Reg. read ALU Data mem. Reg. write Total
arithmetic 200 100 200 — 100 600 ps
load 200 100 200 200 100 800 ps
store 200 100 200 200 — 700 ps
branch 200 100 200 — — 500 ps

Period = 800 ps (1.25 GHz) for all instructions. Separate instruction and data memories and several adders are needed, since each unit can be used once per cycle.

Multicycle

Definition (multicycle execution). Execution is split into one-cycle steps (fetch, decode and register read, execute, memory access, write back); each instruction uses only the steps it needs. The clock is set by the slowest step (here 200 ps):

  • arithmetic 4 cycles = 800 ps, load 5 cycles = 1000 ps, store 4, branch 3;
  • one memory and one ALU can serve several steps (with intermediate registers between steps);
  • the control unit becomes a finite-state machine.

The really effective improvement is pipelining: the same five steps, with different instructions in different steps at the same time.

Common mistakes

  • Incrementing the PC only at the end of execution: it is incremented already in fetch, so a relative jump offset is added to the already-advanced PC (the branch target is relative to PC + 4).
  • Believing simple instructions are shorter in the single-cycle datapath: the clock is unique, so they all last as long as the slowest.
  • Forgetting that the interrupt check is between instructions.

Versione ripasso

Registers: PC (next instruction), IR, MAR (address to memory), MBR/MDR (datum), AC.

Phases

  1. Fetch: MAR←PC\text{MAR}\leftarrow\text{PC}; MBR←M[MAR]\text{MBR}\leftarrow M[\text{MAR}]; IR←MBR\text{IR}\leftarrow\text{MBR}; PC←PC+4\text{PC}\leftarrow\text{PC}+4 (ARM, MIPS). Micro-operations: t1t_1 MAR←PC; t2t_2 MBR←M[MAR] and PC←PC+4; t3t_3 IR←MBR.
  2. Execute: decode IR; four kinds of action: processor-memory, processor-I/O, data processing, control (jump writes the PC).

Full cycle: address and fetch instruction, decode, operand address and fetch, execute, write result, then interrupt check (if pending: save state, jump to the routine, Interrupts and ExceptionsClasses of interrupts; interrupt-driven I/O; hardware and software handling sequence with context saving on the stack; identifying the source (multiple lines, polling, daisy chain, vector); priorities and nested interrupts; exceptions and modes in ARM.Interrupts and Exceptions →). Indirect addressing (Instruction Formats and Addressing ModesAddressing modes (immediate, direct, indirect, register, register indirect, displacement, PC-relative, scaled indexed, stack) with effective address and a numeric example, fixed versus variable instruction formats, the MIPS R, I and J formats and the 32-bit ARM data-processing format with the encodable-constant rule.Instruction Formats and Addressing Modes →) adds a fetch.

Accumulator example

1 load, 5 add, 2 store; 300 1940, 301 5941, 302 2941; M[940]=3, M[941]=2: AC=3, then AC=5, then M[941]=5. Three cycles, six memory accesses.

Datapath

Elements: PC, instruction memory, PC+4 adder, register file (2 reads, 1 write), sign extension, ALU (Arithmetic Logic UnitRole of the ALU, its inputs and the N Z C V flags, the 1-bit ALU built from AND, OR, a full adder and a multiplexer, the N-bit ALU with subtraction, NOR, set-less-than, zero and overflow detection, comparators, bus sharing with tri-state outputs, and the other computation units.Arithmetic Logic Unit →), data memory, multiplexers (ALUSrc, MemtoReg, RegDst, PCSrc = Branch AND Zero). Control: Hardwired and Microprogrammed ControlWhat the control unit does, control signals of a single-cycle datapath with the table for each instruction class, hardwired control (combinational or finite-state machine), microprogrammed control (control memory, horizontal and vertical microinstructions, microsequencer), and a comparison.Hardwired and Microprogrammed Control →.

  • Arithmetic: read rs, rt → ALU → write rd.
  • Load: ALU computes rs + sext(off) → data memory → write rt; uses all elements.
  • Store: same address, rt to memory, no register write.
  • Branch: ALU rs - rt; if Zero, PC ← PC + 4 + 4·off.

Timing

  • Single cycle: period = slowest instruction. Memory 200, registers 100, ALU 200 ps: arithmetic 600, load 800, store 700, branch 500; period 800 ps (1.25 GHz) for all.
  • Multicycle: 200 ps steps; arithmetic 4 cycles = 800 ps, load 5 = 1000 ps, store 4, branch 3; control = finite-state machine. Better still: pipelining.

Common mistakes: PC incremented in fetch (branch target relative to PC + 4); simple instructions are not faster in single cycle; interrupts checked between instructions.

Teoria collegata