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 () 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:
- fetches an instruction from memory;
- decodes it;
- 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 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 bits each, possibly the same physical memory), the register file (), 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: bits; , , give fields , , .
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. ) and PC-relative: , with a 6-bit offset AD () 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 | |
| Increment | 0000001 | INC RD,RA | |
| Add | 0000010 | ADD RD,RA,RB | |
| Subtract | 0000101 | SUB RD,RA,RB | |
| Decrement | 0000110 | DEC RD,RA | |
| AND | 0001000 | AND RD,RA,RB | |
| OR | 0001001 | OR RD,RA,RB | |
| XOR | 0001010 | XOR RD,RA,RB | |
| NOT | 0001011 | NOT RD,RA | |
| Move B | 0001100 | MOVB RD,RB | |
| Shift right | 0001101 | SHR RD,RB | |
| Shift left | 0001110 | SHL RD,RB | |
| Load immediate | 1001100 | LDI RD,OP | |
| Add immediate | 1000010 | ADI RD,RA,OP | |
| Load | 0010000 | LD RD,RA | |
| Store | 0100000 | ST RA,RB | |
| Branch on zero | 1100000 | BRZ RA,AD | if : , else |
| Branch on negative | 1100001 | BRN RA,AD | if : , else |
| Jump | 1110000 | JMP RA |
The mnemonic is the symbolic form of the opcode, translated into binary by the assembler. Every instruction (except taken jumps) also does . 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 : ADD , SUB , AND , LDI (, with the constant), ADI . The three top bits (15, 14, 13) give the type: register operations, LD, ST, operations with a constant, conditional branches, unconditional jump.
Example in memory
| address | content | instruction | effect |
|---|---|---|---|
| 25 | 0000101 001 010 011 |
SUB, DR=1, SA=2, SB=3 | |
| 35 | 0100000 000 100 101 |
ST, SA=4, SB=5 | |
| 45 | 1000010 010 111 011 |
ADI, DR=2, SA=7, OP=3 | |
| 55 | 1100000 101 110 100 |
BRZ, AD=101 100, SA=6 |
if : |
The BRZ offset is in 6-bit two's complement: if the program goes back to . With and the instruction at 35 writes at address .
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 ), 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 ;
- jump: bus A;
- branch: sign-extended offset, if the condition holds.
Instruction decoder. A combinational network producing the datapath control signals. Some bits go straight through: = bits 8-6 (DR), = bits 5-3 (SA), = bits 2-0 (SB), = 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 |
(0 register, 1 constant), (0 result, 1 memory data), (write register file), (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: .
One instruction, one cycle. The PC addresses the instruction memory; the instruction is decoded; registers and 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: , , , ;
- LD RD,RA: address = bus A (), , ;
- ST RA,RB: address = bus A, data = bus B (), , .
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 ns, so 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 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 ( to ); the 4-bit fields , , select a user register (DR/SA/SB) or an internal one, according to the most significant bit.
Multi-cycle control word (28 bits): (4, next state) (2) (4) (4) (4) (4) . (program sequence) drives the PC: hold, increment, conditional branch, jump; loads the IR; 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 , and instruction execution (EX0), which performs the operation, updates the PC and returns to INF. ADD: INF, then EX0 with and .
Longer instructions (3 or more cycles): a register-indirect load takes 3 cycles: INF; EX0: (internal register ); EX1: and . Multi-position shifts (amount given by OP) need a loop EX0 to EX4 using as working register and as a counter decremented until zero (the status signal detects it): each iteration shifts one bit, at the end .
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 (bus A).
- Forgetting 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 ().
Versione ripasso
- PC: address of the next instruction, or parallel load for jumps. Execution = fetch, decode, execute (one or more microinstructions).
- 16-bit formats: register
opcode(7) DR SA SB; immediateopcode DR SA OP(3)(zero fill); branchopcode AD(3) SA AD(3), 6-bit two's-complement PC-relative offset. - Load/store: only LD () and ST () touch data memory. Set: MOVA, INC, ADD, SUB, DEC, AND, OR, XOR, NOT, MOVB, SHR, SHL; LDI, ADI; LD, ST; BRZ, BRN ( if true), JMP (). Otherwise .
- Codes: bits 12-9 of the opcode are the code (ADD , SUB , AND ); bits 15-13 give the type ( register, LD, ST, constant, branches, jump).
- Examples:
0000101 001 010 011is ;1100000 101 110 100is BRZ with AD : if ; ST at 35 with , writes at address . - Single cycle: combinational decoder; straight from the bits, from bits 15, 14, 13, 9; 20-bit control word; two memories. Period = sum of the critical-path delays ns, about 102 MHz, sized on LD.
- Multi-cycle: one memory; IR plus internal registers to ; sequential decoder (state, opcode, status control word); 28-bit control word (NS, PS, IL, DX, AX, BX, MB, FS, MD, RW, MM, MW). : hold, increment, conditional branch, jump.
- States: INF () then EX0: ADD takes two cycles; takes three (INF, EX0 , EX1); multi-bit shifts loop with as counter until .
- 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.