Lesson 26 +35 XP

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 signalsSequence 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

D3 T4: quad SC ← 0

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.

⚠️ Warning

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:

  1. Fetch an instruction from memory.
  2. Decode the instruction.
  3. Read the effective address from memory if the instruction has an indirect address.
  4. 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).

begin{aligned} T0 &: quad AR ← PC \\ T1 &: quad IR ← M[AR], quad PC ← PC + 1 \\ T2 &: quad D0, ldots, D7 ← Decode IR(12-14), quad AR ← IR(0-11), quad I ← IR(15) end{aligned}

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:

StepTransferBus select S2 S1 S0Control inputs enabled
T0AR ← PC010 (PC)LD of AR
T1IR ← M[AR], PC ← PC + 1111 (memory)memory read, LD of IR, INR of PC
T2AR ← 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:

ConditionInstruction typeAction at T3
D7' I T3memory-reference, indirectAR ← M[AR] (read the effective address)
D7' I' T3memory-reference, directnothing (EA already in AR)
D7 I' T3register-referenceexecute the register-reference instruction
D7 I T3input-outputexecute the input-output instruction

Flowchart of the instruction cycleFlowchart 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:

D7 I' T3: quad SC ← 0 qquad D7 I T3: quad SC ← 0
SolvedExample 1 · Tracing the fetch with numbers

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.

  1. T0: AR ← PC = 07F. PC = 07F, IR unchanged.
  2. T1: IR ← M[07F] = 7200, PC ← 080.
  3. T2: opcode bits IR(12-14) = 111, so D7 = 1; AR ← IR(0-11) = 200; I ← IR(15) = 0.
  4. T3: D7 I' T3 is a register-reference instruction. Bit B9 of IR is 1, so CMA: AC ← AC' = F0F0. SC ← 0.
Answerafter T3: AR = 200, PC = 080, IR = 7200, AC = F0F0
SolvedExample 2 · Which T step does each instruction end on?

How many clock cycles (fetch included) does each instruction need?

  1. Every instruction spends T0, T1, T2 on fetch and decode and T3 on the type check (and indirect read).
  2. Register-reference and I/O: executed in T3, so 4 cycles.
  3. 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.
  4. The indirect read happens inside T3, so it does not add a cycle in this design.
Answerregister-ref and I/O 4; STA, BUN 5; AND, ADD, LDA, BSA 6; ISZ 7

3. The Instruction Set of the Basic Computer

The basic computer has 25 instructions of three types, recognised from bits 12-15:

TypeRecognised byFormat
memory-referenceopcode 000 to 110I, opcode, 12-bit address
register-referenceopcode 111, I = 0 (first hex digit 7)0111, then 12 bits each naming an operation
input-outputopcode 111, I = 1 (first hex digit F)1111, then 12 bits each naming an operation

3.1 Memory-reference instructions

SymbolI = 0I = 1Description
AND0xxx8xxxAND memory word to AC
ADD1xxx9xxxadd memory word to AC (carry to E)
LDA2xxxAxxxload memory word to AC
STA3xxxBxxxstore AC in memory
BUN4xxxCxxxbranch unconditionally
BSA5xxxDxxxbranch and save return address
ISZ6xxxExxxincrement 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.

SymbolHexBitDescription
CLA7800B11clear AC
CLE7400B10clear E
CMA7200B9complement AC
CME7100B8complement E
CIR7080B7circulate right AC and E
CIL7040B6circulate left AC and E
INC7020B5increment AC
SPA7010B4skip next instruction if AC positive
SNA7008B3skip next instruction if AC negative
SZA7004B2skip next instruction if AC zero
SZE7002B1skip next instruction if E is 0
HLT7001B0halt the computer

3.3 Input-output instructions (Fxxx)

SymbolHexDescription
INPF800input a character to AC
OUTF400output a character from AC
SKIF200skip on input flag
SKOF100skip on output flag
IONF080interrupt on
IOFF040interrupt off
SolvedExample 3 · Decoding hexadecimal instructions

Identify the instructions 1A24, 9A24, 7004, F400 and 5135.

  1. 1A24 = 0001 1010 0010 0100: I = 0, opcode 001 = ADD, address A24. ADD A24 (direct).
  2. 9A24 = 1001 ...: I = 1, opcode 001 = ADD, address A24. ADD indirect: EA = M[A24].
  3. 7004: opcode 111 with I = 0, register-reference; bit B2 set, so SZA.
  4. F400: opcode 111 with I = 1, I/O; bit 10 set, so OUT.
  5. 5135: I = 0, opcode 101 = BSA, address 135.
AnswerADD A24; ADD I A24; SZA; OUT; BSA 135
SolvedExample 4 · Total clock cycles of a program

Find the clock cycles to run LDA 104, ADD I 104, STA 105, ISZ 106, CLA, OUT, BUN 100.

  1. LDA 6, ADD (indirect) 6, STA 5, ISZ 7.
  2. CLA 4, OUT 4, BUN 5.
  3. Total = 6 + 6 + 5 + 7 + 4 + 4 + 5.
Answer37 clock cycles

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:

CategoryInstructions of the basic computer
Arithmetic, logical and shiftADD, AND, CMA, CME, INC, CIR, CIL, CLA, CLE
Moving data between memory and registersLDA, STA
Program control and status checkingBUN, BSA, ISZ, SPA, SNA, SZA, SZE, HLT
Input and outputINP, 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.

SolvedExample 5 · OR using only the basic instructions

Write a program segment to compute C = A OR B for words A, B in memory.

  1. A + B = (A' B')'.
  2. LDA A, CMA, STA TMP: TMP holds A'.
  3. LDA B, CMA: AC holds B'.
  4. AND TMP: AC holds A' B'.
  5. CMA: AC holds (A' B')' = A + B. STA C.
AnswerLDA A / CMA / STA TMP / LDA B / CMA / AND TMP / CMA / STA C
SolvedExample 6 · Subtraction without a subtract instruction

AC = 0009H must become B − AC, where M[B] = 0014H (20).

  1. CMA: AC = FFF6. INC: AC = FFF7, the 2's complement of 9 (that is, −9).
  2. ADD B: AC = FFF7 + 0014 = 1000B; the carry out goes to E and AC = 000B.
  3. 000B16 = 11 = 20 - 9. ✓
AnswerCMA, INC, ADD B gives AC = 000BH

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:

VERILOG
// 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
endmodule

The simulation reproduces the cycle counts of Example 2, and their sum is the 37 cycles found in Example 4:

OUTPUT
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 cycles
💡 Tip

Key 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.
🎯 END-TERM EXAM PREP

📝 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)