Salta al contenuto
Note per Studenti Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle Control Report

Simple Computer - Instruction Set, Single-Cycle and Multi-Cycle Control

In questa pagina 5

After the datapath (Simple Computer - Datapath, ALU and Register FileA simplified but fully working computer has a datapath, a control unit and a memory; the datapath is a register file with two read ports and one write port, a MUX B (register or constant), a function unit made of ALU plus shifter, and a MUX D (result or data from memory), and it reports the status bits V, C, N, Z. The ALU joins an arithmetic circuit (an adder with input logic on B and a carry-in) and a logic circuit through a MUX, and the 15 microoperations are selected by a 4-bit code FS, so the datapath control word is 16 bits wide.Simple Computer - Datapath, ALU and Register File →) the computer is completed with the control unit and the memory.

Program counter and instruction execution

The computer takes as input a sequence of instructions stored in a memory (RAM or ROM). Each instruction specifies the operation, the operands, the destination and sometimes the next instruction. The Program Counter (PC) holds the address of the next instruction, obtained either by a counter (+1+1) or by a parallel load (jumps).

Executing an instruction means activating the right sequence of microoperations, in the datapath and as transfers to and from memory. The control unit:

  1. fetches an instruction from memory;
  2. decodes it;
  3. executes it with one or more microinstructions.

Definition (instruction, instruction set, ISA). An instruction is a set of bits fully specifying an operation concisely; the instruction set is the set of all instructions; the ISA is the full catalogue with their description.

Example. A 7-bit opcode allows up to 27=1282^7=128 instructions, each 16 bits long.

Instruction set architecture

Three components: memory elements, instruction format, instruction specifications.

User-visible memory elements: the instruction memory and the data memory (here 215×162^{15}\times16 bits each, possibly the same physical memory), the register file (8×168\times16), the program counter. Other elements may exist internally, invisible to the programmer.

Instruction format

Definition (16-bit formats). The bits are split into fields. The opcode specifies the operation; operands (registers or memory) are indicated explicitly (dedicated bits) or implicitly (inside the opcode).

format bits 15-9 bits 8-6 bits 5-3 bits 2-0
Register opcode (7) DR (destination) SA (source A) SB (source B)
Immediate opcode (7) DR SA OP (immediate)
Branch/jump opcode (7) AD left SA AD right

Example. Register format: 7+3+3+3=167+3+3+3=16 bits; DR=R3DR=R_3, SA=R1SA=R_1, SB=R2SB=R_2 give fields 011011, 001001, 010010.

In the immediate format OP has only 3 bits; the other 13 are filled with zeros (zero fill, zf). The branch/jump format alters the flow:

  • branch: conditional on a register (e.g. R[SA]=0R[SA]=0) and PC-relative: PC←PC+offsetPC\leftarrow PC+\text{offset}, with a 6-bit offset AD (3+33+3) sign-extended (se) to jump forwards and backwards (two's complement). Limited range around the PC;
  • jump: wider range, the content of a register is the destination.

Instruction set

instruction opcode mnemonic description
Move A 0000000 MOVA RD,RA R[DR]←R[SA]R[DR]\leftarrow R[SA]
Increment 0000001 INC RD,RA R[DR]←R[SA]+1R[DR]\leftarrow R[SA]+1
Add 0000010 ADD RD,RA,RB R[DR]←R[SA]+R[SB]R[DR]\leftarrow R[SA]+R[SB]
Subtract 0000101 SUB RD,RA,RB R[DR]←R[SA]−R[SB]R[DR]\leftarrow R[SA]-R[SB]
Decrement 0000110 DEC RD,RA R[DR]←R[SA]−1R[DR]\leftarrow R[SA]-1
AND 0001000 AND RD,RA,RB R[SA]∧R[SB]R[SA]\wedge R[SB]
OR 0001001 OR RD,RA,RB R[SA]∨R[SB]R[SA]\vee R[SB]
XOR 0001010 XOR RD,RA,RB R[SA]⊕R[SB]R[SA]\oplus R[SB]
NOT 0001011 NOT RD,RA R[SA]‾\overline{R[SA]}
Move B 0001100 MOVB RD,RB R[DR]←R[SB]R[DR]\leftarrow R[SB]
Shift right 0001101 SHR RD,RB R[DR]←sr R[SB]R[DR]\leftarrow sr\,R[SB]
Shift left 0001110 SHL RD,RB R[DR]←sl R[SB]R[DR]\leftarrow sl\,R[SB]
Load immediate 1001100 LDI RD,OP R[DR]←zf OPR[DR]\leftarrow zf\,OP
Add immediate 1000010 ADI RD,RA,OP R[DR]←R[SA]+zf OPR[DR]\leftarrow R[SA]+zf\,OP
Load 0010000 LD RD,RA R[DR]←M[R[SA]]R[DR]\leftarrow M[R[SA]]
Store 0100000 ST RA,RB M[R[SA]]←R[SB]M[R[SA]]\leftarrow R[SB]
Branch on zero 1100000 BRZ RA,AD if R[SA]=0R[SA]=0: PC←PC+se ADPC\leftarrow PC+se\,AD, else PC←PC+1PC\leftarrow PC+1
Branch on negative 1100001 BRN RA,AD if R[SA]<0R[SA]<0: PC←PC+se ADPC\leftarrow PC+se\,AD, else PC+1PC+1
Jump 1110000 JMP RA PC←R[SA]PC\leftarrow R[SA]

The mnemonic is the symbolic form of the opcode, translated into binary by the assembler. Every instruction (except taken jumps) also does PC←PC+1PC\leftarrow PC+1. This is a load/store architecture: LD and ST are the only instructions accessing data memory.

Code structure. The four low bits of the opcode (instruction bits 12-9) equal the function-unit code FSFS: ADD =0000010→FS=0010=0000010\to FS=0010, SUB =0000101→FS=0101=0000101\to FS=0101, AND →1000\to1000, LDI →1100\to1100 (F=BF=B, with BB the constant), ADI →0010\to0010. The three top bits (15, 14, 13) give the type: 000000 register operations, 001001 LD, 010010 ST, 100100 operations with a constant, 110110 conditional branches, 111111 unconditional jump.

Example in memory

address content instruction effect
25 0000101 001 010 011 SUB, DR=1, SA=2, SB=3 R1←R2−R3R_1\leftarrow R_2-R_3
35 0100000 000 100 101 ST, SA=4, SB=5 M[R4]←R5M[R_4]\leftarrow R_5
45 1000010 010 111 011 ADI, DR=2, SA=7, OP=3 R2←R7+3R_2\leftarrow R_7+3
55 1100000 101 110 100 BRZ, AD=101 100=44=44, SA=6 if R6=0R_6=0: PC←PC−20PC\leftarrow PC-20

The BRZ offset 1011002101100_2 is −20-20 in 6-bit two's complement: if R6=0R_6=0 the program goes back to 55−20=3555-20=35. With R4=70R_4=70 and R5=80R_5=80 the instruction at 35 writes 8080 at address 7070.

Single-cycle computer

The single-cycle computer fetches and executes each instruction in one clock cycle. Blocks: PC, instruction memory (never written), instruction decoder, register file, MUX B, function unit, data memory (written with MWMW), MUX D, a PC control block and an extend unit adding the left bits of the offset. The only memory in the control part is the PC, updated every cycle:

  • normally PC←PC+1PC\leftarrow PC+1;
  • jump: PC←PC\leftarrow bus A;
  • branch: PC←PC+PC\leftarrow PC+ sign-extended offset, if the condition holds.

Instruction decoder. A combinational network producing the datapath control signals. Some bits go straight through: DADA = bits 8-6 (DR), AAAA = bits 5-3 (SA), BABA = bits 2-0 (SB), FSFS = bits 12-9. The other signals depend on bits 15, 14, 13 and 9:

instruction type 15 14 13 9 MB MD RW MW PL JB BC
function-unit operation on registers 0 0 0 X 0 0 1 0 0 X X
memory read (LD) 0 0 1 X 0 1 1 0 0 X X
memory write (ST) 0 1 0 X 0 X 0 1 0 X X
register and constant operation 1 0 0 X 1 0 1 0 0 X X
branch on zero (BRZ) 1 1 0 0 X 0 0 0 1 0 0
branch on negative (BRN) 1 1 0 1 X 0 0 0 1 0 1
jump (JMP) 1 1 1 X X 0 0 0 1 1 X

MBMB (0 register, 1 constant), MDMD (0 result, 1 memory data), RWRW (write register file), MWMW (write data memory), PL (PC load: the PC loads instead of incrementing), JB (jump: PC takes bus A, else PC + offset), BC (condition: 0 zero, 1 negative). The single-cycle control word is 20 bits: DA (3) AA (3) BA (3) MB FS (4) MD RW MW PL JB BCDA\,(3)\ AA\,(3)\ BA\,(3)\ MB\ FS\,(4)\ MD\ RW\ MW\ PL\ JB\ BC.

One instruction, one cycle. The PC addresses the instruction memory; the instruction is decoded; registers SASA and SBSB are read; the function unit computes; the result (or the memory data) returns to the register file (written at the clock edge); the PC is updated.

  • ADD: MB=0MB=0, MD=0MD=0, RW=1RW=1, FS=0010FS=0010;
  • LD RD,RA: address = bus A (R[SA]R[SA]), MD=1MD=1, RW=1RW=1;
  • ST RA,RB: address = bus A, data = bus B (R[SB]R[SB]), MW=1MW=1, RW=0RW=0.

Formula (single-cycle clock period). The period must be at least the sum of the delays of all blocks along the critical path, since in one cycle the signal crosses them in sequence.

block PC instr. memory register file (read) MUX B function unit or data memory MUX D register file (write)
delay (ns) 0.2 4 0.6 0.2 4 0.2 0.6

Sum =0.2+4+0.6+0.2+4+0.2+0.6=9.8=0.2+4+0.6+0.2+4+0.2+0.6=9.8 ns, so fmax≈102f_{max}\approx102 MHz.

Even the simplest instruction (ADD) takes the whole period, sized on the slowest (LD). Separate memories for instructions and data are also needed (both are accessed in one cycle), which is costly. Both limits are overcome by the multi-cycle computer, and performance by the pipeline (Pipelining and HazardsA pipeline splits execution into stages separated by registers (IF, DOF, EX, WB) working in parallel on different instructions; latency stays the same while throughput grows by less than the number of stages (flip-flop delays, slowest stage). With k stages and N instructions k+N-1 cycles are needed. Hazards stall the pipeline: data hazards (operand not yet written; fixes are NOPs, stalls and forwarding) and control hazards (branches; fixes are NOPs or branch prediction with bubbles).Pipelining and Hazards →).

Multi-cycle computer

Instructions take a variable number of clock cycles, so they can be more complex. Changes needed:

  • memory: one memory can hold instructions and data, since one instruction often needs several accesses (the instruction itself and a datum), impossible in one cycle;
  • control unit: the instruction must be kept for several cycles, so an instruction register (IR) with a load signal is added; the combinational decoder becomes a sequential machine: a control-state register, control logic and a next state (state, opcode, status bits ⇒\Rightarrow control word);
  • datapath: duplicated registers hold intermediate results computed in one cycle and needed in a later cycle of the same instruction. They are internal and invisible to the programmer (R8R_8 to R15R_{15}); the 4-bit fields DXDX, AXAX, BXBX select a user register (DR/SA/SB) or an internal one, according to the most significant bit.

Multi-cycle control word (28 bits): NSNS (4, next state) PSPS (2) ILIL DXDX (4) AXAX (4) BXBX (4) MBMB FSFS (4) MDMD RWRW MMMM MWMW. PSPS (program sequence) drives the PC: 0000 hold, 0101 increment, 1010 conditional branch, 1111 jump; ILIL loads the IR; MMMM is the MUX selecting the memory address (PC for fetch, bus A for data).

State machine. The simplest instructions need the minimum of two cycles, because they access both the instruction and the data: instruction fetch (INF), with IR←M[PC]IR\leftarrow M[PC], and instruction execution (EX0), which performs the operation, updates the PC and returns to INF. ADD: INF, then EX0 with R[DR]←R[SA]+R[SB]R[DR]\leftarrow R[SA]+R[SB] and PC←PC+1PC\leftarrow PC+1.

Longer instructions (3 or more cycles): a register-indirect load R[DR]←M[M[R[SA]]]R[DR]\leftarrow M[M[R[SA]]] takes 3 cycles: INF; EX0: R8←M[R[SA]]R_8\leftarrow M[R[SA]] (internal register R8R_8); EX1: R[DR]←M[R8]R[DR]\leftarrow M[R_8] and PC←PC+1PC\leftarrow PC+1. Multi-position shifts (amount given by OP) need a loop EX0 to EX4 using R8R_8 as working register and R9R_9 as a counter decremented until zero (the status signal ZZ detects it): each iteration shifts one bit, at the end R[DR]←R8R[DR]\leftarrow R_8.

Advantages: a faster clock (set by the slowest block in each cycle, not by the whole chain) and one memory only; drawback: every instruction takes at least two cycles. To get back to one instruction per cycle with a fast clock, the pipeline is used.

Common mistakes

  • Saying the LD/ST memory address is an instruction field: it is the content of register SASA (bus A).
  • Forgetting PC←PC+1PC\leftarrow PC+1 on instructions that do not jump.
  • Believing that a simple instruction is faster in a single-cycle machine: the period is the same for all.
  • Confusing internal and user registers.
  • Reading the BRZ offset as unsigned: it is two's complement (44→−2044\to-20).

Versione ripasso

  • PC: address of the next instruction, +1+1 or parallel load for jumps. Execution = fetch, decode, execute (one or more microinstructions).
  • 16-bit formats: register opcode(7) DR SA SB; immediate opcode DR SA OP(3) (zero fill); branch opcode AD(3) SA AD(3), 6-bit two's-complement PC-relative offset.
  • Load/store: only LD (R[DR]←M[R[SA]]R[DR]\leftarrow M[R[SA]]) and ST (M[R[SA]]←R[SB]M[R[SA]]\leftarrow R[SB]) touch data memory. Set: MOVA, INC, ADD, SUB, DEC, AND, OR, XOR, NOT, MOVB, SHR, SHL; LDI, ADI; LD, ST; BRZ, BRN (PC←PC+offsetPC\leftarrow PC+\text{offset} if true), JMP (PC←R[SA]PC\leftarrow R[SA]). Otherwise PC←PC+1PC\leftarrow PC+1.
  • Codes: bits 12-9 of the opcode are the FSFS code (ADD 00100010, SUB 01010101, AND 10001000); bits 15-13 give the type (000000 register, 001001 LD, 010010 ST, 100100 constant, 110110 branches, 111111 jump).
  • Examples: 0000101 001 010 011 is R1←R2−R3R_1\leftarrow R_2-R_3; 1100000 101 110 100 is BRZ with AD =44=−20=44=-20: 55→3555\to35 if R6=0R_6=0; ST at 35 with R4=70R_4=70, R5=80R_5=80 writes 8080 at address 7070.
  • Single cycle: combinational decoder; DA,AA,BA,FSDA,AA,BA,FS straight from the bits, MB,MD,RW,MW,PL,JB,BCMB,MD,RW,MW,PL,JB,BC from bits 15, 14, 13, 9; 20-bit control word; two memories. Period = sum of the critical-path delays =0.2+4+0.6+0.2+4+0.2+0.6=9.8=0.2+4+0.6+0.2+4+0.2+0.6=9.8 ns, about 102 MHz, sized on LD.
  • Multi-cycle: one memory; IR plus internal registers R8R_8 to R15R_{15}; sequential decoder (state, opcode, status ⇒\Rightarrow control word); 28-bit control word (NS, PS, IL, DX, AX, BX, MB, FS, MD, RW, MM, MW). PSPS: 0000 hold, 0101 increment, 1010 conditional branch, 1111 jump.
  • States: INF (IR←M[PC]IR\leftarrow M[PC]) then EX0: ADD takes two cycles; R[DR]←M[M[R[SA]]]R[DR]\leftarrow M[M[R[SA]]] takes three (INF, EX0 R8←M[R[SA]]R_8\leftarrow M[R[SA]], EX1); multi-bit shifts loop with R9R_9 as counter until ZZ.
  • Trade-off: faster clock and single memory, but at least two cycles per instruction; the pipeline restores one instruction per cycle (Pipelining and HazardsA pipeline splits execution into stages separated by registers (IF, DOF, EX, WB) working in parallel on different instructions; latency stays the same while throughput grows by less than the number of stages (flip-flop delays, slowest stage). With k stages and N instructions k+N-1 cycles are needed. Hazards stall the pipeline: data hazards (operand not yet written; fixes are NOPs, stalls and forwarding) and control hazards (branches; fixes are NOPs or branch prediction with bubbles).Pipelining and Hazards →).
  • Mistakes: address of LD/ST read as an instruction field (it is bus A); PC not incremented; BRZ offset read unsigned; internal registers confused with user ones; 20-bit and 28-bit control words mixed up.

Teoria collegata