Loading lessons...
Timing & Control, the Instruction Cycle & Instruction Set Completeness
Timing & Control, the Instruction Cycle & Instruction Set Completeness
The registers and the bus of the last lesson only move data when control signals tell them to. This lesson shows where those signals come from: a sequence counter that numbers the clock pulses of each instruction, and the instruction cycle (fetch, decode, read the effective address, execute) that every instruction goes through. We finish with the complete instruction set of the basic computer and why it is complete.
1. Timing Signals: Sequence Counter + Decoder
All flip-flops and registers are driven by one master clock. A pulse changes a register only if its control inputs (LD, INR, CLR) are enabled. To give each step of an instruction its own control signals, the basic computer uses:
- a 4-bit sequence counter (SC) that counts clock pulses, and
- a 4 × 16 decoder on SC that gives the timing signals T0, T1, ldots, T15.
Exactly one Ti is 1 at any time. SC increments on every clock, so T0, T1, T2, ldots follow one another. When an instruction finishes, its last step clears SC, so the next clock starts again at T0.
A second decoder (3 × 8) on the opcode bits IR(12-14) gives the operation signals D0, ldots, D7. Control functions are AND combinations such as D3 T4: "the instruction is STA (D3) and this is step T4".
Sequence counter, decoder and the T signals
In the timing diagram, SC counts T0 ldots T4. During T4 the condition D3 T4 = 1, so the control statement
enables the CLR input of SC. At the next clock edge SC becomes 0 and T0 is active again. Remember that a control statement acts on the clock edge at the end of the timing signal: the transfer written for T4 happens at the edge where T4 ends.
The memory read cycle must fit in one clock period, so that the word read during T1 is on the bus in time to be loaded into IR at the end of T1. Otherwise the control would need wait states.
2. The Instruction Cycle
A program is executed by repeating, for every instruction, the instruction cycle:
- Fetch an instruction from memory.
- Decode the instruction.
- Read the effective address from memory if the instruction has an indirect address.
- Execute the instruction.
After step 4 control returns to step 1 for the next instruction. This continues until a HALT is executed.
2.1 Fetch and decode
At the start PC holds the address of the first instruction and SC = 0 (so T0 is active).
Why is AR ← PC needed at all? Because only AR is connected to the address inputs of memory. PC must first be copied into AR before memory can be read.
How the bus carries out these transfers:
| Step | Transfer | Bus select S2 S1 S0 | Control inputs enabled |
|---|---|---|---|
| T0 | AR ← PC | 010 (PC) | LD of AR |
| T1 | IR ← M[AR], PC ← PC + 1 | 111 (memory) | memory read, LD of IR, INR of PC |
| T2 | AR ← IR(0-11) | 101 (IR) | LD of AR (the opcode decoder sees IR directly) |
2.2 Determine the type of instruction
During T3 the control knows the opcode (D7 or not) and the mode bit I. Four cases arise:
| Condition | Instruction type | Action at T3 |
|---|---|---|
| D7' I T3 | memory-reference, indirect | AR ← M[AR] (read the effective address) |
| D7' I' T3 | memory-reference, direct | nothing (EA already in AR) |
| D7 I' T3 | register-reference | execute the register-reference instruction |
| D7 I T3 | input-output | execute the input-output instruction |
Flowchart of the instruction cycle
So a memory-reference instruction always reaches T4 with the effective address in AR, whether it was direct or indirect. Execution starts at T4. A register-reference or I/O instruction is executed in T3 and clears SC in the same step:
PC = 07FH (hex) and M[07F] = 7200H (CMA). AC = 0F0FH. Give the contents of AR, PC, IR and AC after T0, T1, T2 and T3.
- T0: AR ← PC = 07F. PC = 07F, IR unchanged.
- T1: IR ← M[07F] = 7200, PC ← 080.
- T2: opcode bits IR(12-14) = 111, so D7 = 1; AR ← IR(0-11) = 200; I ← IR(15) = 0.
- T3: D7 I' T3 is a register-reference instruction. Bit B9 of IR is 1, so CMA: AC ← AC' = F0F0. SC ← 0.
How many clock cycles (fetch included) does each instruction need?
- Every instruction spends T0, T1, T2 on fetch and decode and T3 on the type check (and indirect read).
- Register-reference and I/O: executed in T3, so 4 cycles.
- STA, BUN: execution ends at T4, 5 cycles. AND, ADD, LDA (read DR at T4, use it at T5) and BSA end at T5: 6 cycles. ISZ (read, increment, write and test) ends at T6: 7 cycles.
- The indirect read happens inside T3, so it does not add a cycle in this design.
3. The Instruction Set of the Basic Computer
The basic computer has 25 instructions of three types, recognised from bits 12-15:
| Type | Recognised by | Format |
|---|---|---|
| memory-reference | opcode 000 to 110 | I, opcode, 12-bit address |
| register-reference | opcode 111, I = 0 (first hex digit 7) | 0111, then 12 bits each naming an operation |
| input-output | opcode 111, I = 1 (first hex digit F) | 1111, then 12 bits each naming an operation |
3.1 Memory-reference instructions
| Symbol | I = 0 | I = 1 | Description |
|---|---|---|---|
| AND | 0xxx | 8xxx | AND memory word to AC |
| ADD | 1xxx | 9xxx | add memory word to AC (carry to E) |
| LDA | 2xxx | Axxx | load memory word to AC |
| STA | 3xxx | Bxxx | store AC in memory |
| BUN | 4xxx | Cxxx | branch unconditionally |
| BSA | 5xxx | Dxxx | branch and save return address |
| ISZ | 6xxx | Exxx | increment memory word and skip if zero |
(xxx is the 12-bit address in hex.) Setting I = 1 adds 8 to the first hex digit.
3.2 Register-reference instructions (7xxx)
Only one bit of the low 12 is 1; that bit names the operation, so these codes need no address.
| Symbol | Hex | Bit | Description |
|---|---|---|---|
| CLA | 7800 | B11 | clear AC |
| CLE | 7400 | B10 | clear E |
| CMA | 7200 | B9 | complement AC |
| CME | 7100 | B8 | complement E |
| CIR | 7080 | B7 | circulate right AC and E |
| CIL | 7040 | B6 | circulate left AC and E |
| INC | 7020 | B5 | increment AC |
| SPA | 7010 | B4 | skip next instruction if AC positive |
| SNA | 7008 | B3 | skip next instruction if AC negative |
| SZA | 7004 | B2 | skip next instruction if AC zero |
| SZE | 7002 | B1 | skip next instruction if E is 0 |
| HLT | 7001 | B0 | halt the computer |
3.3 Input-output instructions (Fxxx)
| Symbol | Hex | Description |
|---|---|---|
| INP | F800 | input a character to AC |
| OUT | F400 | output a character from AC |
| SKI | F200 | skip on input flag |
| SKO | F100 | skip on output flag |
| ION | F080 | interrupt on |
| IOF | F040 | interrupt off |
Identify the instructions 1A24, 9A24, 7004, F400 and 5135.
- 1A24 = 0001 1010 0010 0100: I = 0, opcode 001 = ADD, address A24. ADD A24 (direct).
- 9A24 = 1001 ...: I = 1, opcode 001 = ADD, address A24. ADD indirect: EA = M[A24].
- 7004: opcode 111 with I = 0, register-reference; bit B2 set, so SZA.
- F400: opcode 111 with I = 1, I/O; bit 10 set, so OUT.
- 5135: I = 0, opcode 101 = BSA, address 135.
Find the clock cycles to run LDA 104, ADD I 104, STA 105, ISZ 106, CLA, OUT, BUN 100.
- LDA 6, ADD (indirect) 6, STA 5, ISZ 7.
- CLA 4, OUT 4, BUN 5.
- Total = 6 + 6 + 5 + 7 + 4 + 4 + 5.
4. Instruction Set Completeness
A computer's instruction set is complete if it lets the user evaluate any function that is computable. It needs enough instructions in each of four categories:
| Category | Instructions of the basic computer |
|---|---|
| Arithmetic, logical and shift | ADD, AND, CMA, CME, INC, CIR, CIL, CLA, CLE |
| Moving data between memory and registers | LDA, STA |
| Program control and status checking | BUN, BSA, ISZ, SPA, SNA, SZA, SZE, HLT |
| Input and output | INP, OUT, SKI, SKO, ION, IOF |
The set is minimal: there is no subtract, multiply, OR, XOR or shift-by-n instruction. These are built from the existing ones by programs (or later, by subroutines):
- Subtract A - B: complement B (CMA), increment it (INC) to get its 2's complement, then ADD A.
- OR: by De Morgan, A + B = (A' B')', using CMA and AND.
- Multiply: by a loop of shifts (CIL, CIR) and adds, controlled by ISZ.
- Arithmetic shift: circulate through E after setting E appropriately.
The trade-off is speed: a short instruction set gives cheap hardware but long programs.
Write a program segment to compute C = A OR B for words A, B in memory.
- A + B = (A' B')'.
- LDA A, CMA, STA TMP: TMP holds A'.
- LDA B, CMA: AC holds B'.
- AND TMP: AC holds A' B'.
- CMA: AC holds (A' B')' = A + B. STA C.
AC = 0009H must become B − AC, where M[B] = 0014H (20).
- CMA: AC = FFF6. INC: AC = FFF7, the 2's complement of 9 (that is, −9).
- ADD B: AC = FFF7 + 0014 = 1000B; the carry out goes to E and AC = 000B.
- 000B16 = 11 = 20 - 9. ✓
5. Verilog: Timing Unit with SC Clearing
The model below builds the sequence counter, the T decoder and the D decoder, and clears SC with the exact conditions of the basic computer (register/I/O at T3, STA and BUN at T4, AND, ADD, LDA and BSA at T5, ISZ at T6). The testbench loads IR at T1 from the program of Example 4:
// Sequence counter + 4 x 16 timing decoder + 3 x 8 opcode decoder
module control_timing (
input clk,
input rst,
input [15:0] ir,
output reg [3:0] sc,
output [15:0] T,
output [7:0] D,
output clr
);
assign T = 16'b1 << sc;
assign D = 8'b1 << ir[14:12];
// when each instruction is finished, SC is cleared
assign clr = (D[7] & T[3]) // register-reference, I/O
| ((D[3] | D[4]) & T[4]) // STA, BUN
| ((D[0] | D[1] | D[2] | D[5]) & T[5]) // AND, ADD, LDA, BSA
| (D[6] & T[6]); // ISZ
always @(posedge clk)
if (rst || clr) sc <= 4'd0;
else sc <= sc + 4'd1;
endmodule
module tb;
reg clk = 0, rst = 1;
reg [15:0] ir = 16'h7800;
reg [15:0] prog [0:6];
integer k = 0;
wire [3:0] sc;
wire [15:0] T;
wire [7:0] D;
wire clr;
control_timing u (clk, rst, ir, sc, T, D, clr);
always #5 clk = ~clk;
// T1: IR <- M[AR]
always @(posedge clk)
if (!rst && T[1]) begin
ir <= prog[k];
k <= k + 1;
end
always @(posedge clk)
if (!rst && clr)
$display("IR=%h D%0d I=%b finished at T%0d -> %0d clock cycles",
ir, ir[14:12], ir[15], sc, sc + 1);
initial begin
prog[0] = 16'h2104; // LDA 104
prog[1] = 16'h9104; // ADD 104, indirect
prog[2] = 16'h3105; // STA 105
prog[3] = 16'h6106; // ISZ 106
prog[4] = 16'h7800; // CLA
prog[5] = 16'hF400; // OUT
prog[6] = 16'h4100; // BUN 100
#12 rst = 0;
wait (k == 7);
@(posedge clr);
@(posedge clk);
#1 $finish;
end
endmoduleThe simulation reproduces the cycle counts of Example 2, and their sum is the 37 cycles found in Example 4:
IR=2104 D2 I=0 finished at T5 -> 6 clock cycles
IR=9104 D1 I=1 finished at T5 -> 6 clock cycles
IR=3105 D3 I=0 finished at T4 -> 5 clock cycles
IR=6106 D6 I=0 finished at T6 -> 7 clock cycles
IR=7800 D7 I=0 finished at T3 -> 4 clock cycles
IR=f400 D7 I=1 finished at T3 -> 4 clock cycles
IR=4100 D4 I=0 finished at T4 -> 5 clock cyclesKey points to remember
- SC (4-bit counter) + 4 × 16 decoder give T0 ldots T15; IR(12-14) + 3 × 8 decoder give D0 ldots D7.
- Fetch: T0 AR ← PC; T1 IR ← M[AR], PC ← PC + 1; T2 decode, AR ← IR(0-11), I ← IR(15).
- T3: D7' I indirect read; D7 I' register-reference; D7 I input-output.
- 25 instructions: 7 memory-reference, 12 register-reference, 6 I/O.
- Complete: arithmetic/logic/shift, data transfer, program control, I/O.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. Explain the timing and control of the basic computer with a timing diagram of T0 to T4 and the control function D3 T4: SC ← 0. (5 Marks)
- Q2. What is an instruction cycle? Draw its flowchart and write the register transfers for fetch and decode. (10 Marks)
- Write: the four phases, T0, T1, T2 RTL, the bus select codes, and the four cases at T3.
- Q3. List the instructions of the basic computer with their hex codes, grouped by type. (5 Marks)
- Q4. What is meant by instruction set completeness? Is the basic computer's set complete? How are subtraction and OR performed? (5 Marks)
- Q5. Identify the instructions 9A24, 7004 and F400. (2.5 Marks)