Lesson 30 +40 XP

Input-Output, Interrupt & Design of the Basic Computer (Control Gates and AC Logic)

Input-Output, Interrupt & Design of the Basic Computer (Control

Gates and AC Logic)

This lesson completes the basic computer. First the input-output registers, flags and instructions, then the program interrupt that lets I/O run without wasting processor time. With every register transfer statement now known, we finish with the actual design: the logic gates that drive the register control inputs, the bus select encoder and the accumulator.

1. Input-Output Configuration

The basic computer talks to one terminal (a keyboard for input and a printer for output) through a serial interface.

Input-output configurationInput-output configuration

  • INPR (8-bit input register) receives an alphanumeric character code from the keyboard.
  • OUTR (8-bit output register) holds a character for the printer.
  • FGI (input flag): set to 1 when a new character is in INPR; cleared when the computer takes it.
  • FGO (output flag): 1 when OUTR is free to accept a new character; cleared when the computer loads OUTR, set again when the printer has printed it.

Input: a key press puts the code in INPR and sets FGI = 1. While FGI = 1 the keyboard cannot change INPR. The computer checks FGI; if it is 1, it moves INPR into AC(0-7) and clears FGI.

Output: the computer checks FGO; if it is 1, it moves AC(0-7) into OUTR and clears FGO. The printer prints the character and sets FGO = 1 again.

1.1 Input-output instructions

I/O instructions are recognised by D7 I T3 (opcode 111, I = 1). Let p = D7 I T3 and Bi = IR(i):

SymbolHexControlRegister transferMeaning
(all)pSC ← 0clear SC
INPF800p B11AC(0-7) ← INPR, \ FGI ← 0input a character
OUTF400p B10OUTR ← AC(0-7), \ FGO ← 0output a character
SKIF200p B9if (FGI = 1) then (PC ← PC + 1)skip on input flag
SKOF100p B8if (FGO = 1) then (PC ← PC + 1)skip on output flag
IONF080p B7IEN ← 1interrupt enable on
IOFF040p B6IEN ← 0interrupt enable off

2. Program Interrupt

With programmed I/O, the program checks the flag in a loop (SKI, BUN back) until it is set. Because I/O devices are far slower than the processor, almost all of that time is wasted.

SolvedExample 1 · Cost of waiting on a flag

The clock is 1 MHz and the wait loop is SKI followed by BUN (back to SKI). The keyboard delivers 10 characters per second. How many loop passes are wasted per character?

  1. SKI is register/I-O type: 4 cycles. BUN: 5 cycles. One pass = 9 cycles = 9 µs.
  2. Time between characters = 1/10 s = 100 ms = 100 000 µs.
  3. Passes per character = 100 000 / 9 ≈ 11 111.
Answerabout 11 111 passes (about 100 000 clock cycles) per character are wasted

The alternative is the interrupt: the computer runs another program and the I/O device interrupts it when a flag is set.

  • IEN (interrupt enable flip-flop) is set by ION and cleared by IOF; the program decides whether it can be interrupted.
  • R (interrupt flip-flop) marks that an interrupt cycle must run instead of the next instruction cycle.

At any time outside T0, T1, T2, if interrupts are enabled and a flag is up, R is set:

T0' T1' T2' (IEN)(FGI + FGO): quad R ← 1

(Setting R only after T2 makes sure an instruction in progress is not disturbed during its fetch.) After the current instruction finishes and SC returns to 0, the control checks R: R = 0 starts a normal instruction cycle; R = 1 starts the interrupt cycle.

2.1 The interrupt cycle

The interrupt cycle is a hardware BSA to address 0: it saves the return address in location 0 and branches to location 1.

begin{aligned} RT0 &: quad AR ← 0, quad TR ← PC \\ RT1 &: quad M[AR] ← TR, quad PC ← 0 \\ RT2 &: quad PC ← PC + 1, quad IEN ← 0, quad R ← 0, quad SC ← 0 end{aligned}

So the fetch phase is now written with R': R'T0: AR ← PC, R'T1: IR ← M[AR], PC ← PC + 1, R'T2: ldots

Interrupt cycle: flowchart and memoryInterrupt cycle: flowchart and memory

Location 1 normally holds a BUN to the I/O service program. That program saves the processor registers, checks the flags, services the device, restores the registers, turns interrupts back on with ION, and returns with BUN 0 I (indirect through location 0, which holds the return address). IEN is cleared during the interrupt cycle so the service program itself is not interrupted.

SolvedExample 2 · Tracing an interrupt

Location 1 holds 0 BUN 1120. An interrupt is requested while the instruction at 255 is executing (IEN = 1, FGI = 1). Find PC, M[0], IEN and R at each stage.

  1. During the instruction at 255, at some T3 or later, R ← 1. When it finishes, PC = 256.
  2. RT0: AR = 0, TR = 256. RT1: M[0] = 256, PC = 0. RT2: PC = 1, IEN = 0, R = 0.
  3. The instruction at 1 (BUN 1120) runs: PC = 1120, and the I/O program executes.
  4. The program ends with ION, then BUN 0 I: EA = M[0] = 256, PC = 256. The interrupted program continues.
AnswerM[0] = 256; PC goes 256 → 1 → 1120 → 256; IEN = 0 during service

3. Design of the Basic Computer

The complete hardware is:

  1. a memory unit of 4096 words × 16 bits,
  2. nine registers: AR, PC, DR, AC, IR, TR, OUTR, INPR, SC,
  3. seven flip-flops: I, S, E, R, IEN, FGI, FGO,
  4. two decoders (3 × 8 for the opcode, 4 × 16 for timing),
  5. a 16-bit common bus,
  6. control logic gates,
  7. an adder and logic circuit connected to the input of AC.

The control logic gates are found by a simple rule: collect every statement that changes a given register or flip-flop, and OR together the control functions that need each of its inputs (LD, INR, CLR).

3.1 Control of AR

From all the RTL statements, AR changes in these places:

Control functionStatement
R' T0AR ← PC
R' T2AR ← IR(0-11)
D7' I T3AR ← M[AR]
R T0AR ← 0
D5 T4AR ← AR + 1

The first three load AR from the bus, so

LD(AR) = R' T0 + R' T2 + D7' I T3 qquad CLR(AR) = R T0 qquad INR(AR) = D5 T4

Control gates for ARControl gates for AR

SolvedExample 3 · Control of the IEN flip-flop

Derive the logic for IEN, built from a JK flip-flop.

  1. Statements that change IEN: p B7: IEN ← 1; p B6: IEN ← 0; R T2: IEN ← 0.
  2. J sets: J = p B7.
  3. K clears: K = p B6 + R T2.
AnswerJ = pB7, K = pB6 + RT2 (with p = D7 I T3)

3.2 Memory read and write

Read = R' T1 + D7' I T3 + (D0 + D1 + D2 + D6) T4
Write = D3 T4 + D5 T4 + D6 T6 + R T1

3.3 Control of the common bus

Seven sources can drive the bus. Let x1, ldots, x7 be 1 when AR, PC, DR, AC, IR, TR or memory must be on the bus. An encoder turns them into the select code S2 S1 S0:

x1x2x3x4x5x6x7S2 S1 S0Register on the bus
0000000000none
1000000001AR
0100000010PC
0010000011DR
0001000100AC
0000100101IR
0000010110TR
0000001111memory
S0 = x1 + x3 + x5 + x7 qquad S1 = x2 + x3 + x6 + x7 qquad S2 = x4 + x5 + x6 + x7

Each xi is found by searching the RTL for statements that read that register onto the bus:

Bus sourceGate logicFrom the statements
x1 (AR)D4 T4 + D5 T5PC ← AR in BUN and BSA
x2 (PC)T0 + D5 T4AR ← PC, TR ← PC, M[AR] ← PC
x3 (DR)D6 T6M[AR] ← DR in ISZ
x4 (AC)D3 T4 + p B10STA and OUT
x5 (IR)R' T2AR ← IR(0-11)
x6 (TR)R T1M[AR] ← TR
x7 (memory)same as Readevery memory read

3.4 Design of the accumulator logic

AC is changed by AND, ADD, LDA (from DR), INP (from INPR), and the register-reference instructions CLA, CMA, CIR, CIL, INC. All the values that need a load pass through the adder and logic circuit, whose 16 stages take AC and DR (and INPR for bits 0-7) as inputs.

Control functionStatementAC input used
D0 T5AC ← AC wedge DRLD (AND gates)
D1 T5AC ← AC + DRLD (adder)
D2 T5AC ← DRLD (transfer DR)
p B11AC(0-7) ← INPRLD (transfer INPR)
r B9AC ← overline{AC}LD (complement)
r B7AC ← shr AC, \ AC(15) ← ELD (shift right)
r B6AC ← shl AC, \ AC(0) ← ELD (shift left)
r B11AC ← 0CLR
r B5AC ← AC + 1INR
LD(AC) = D0 T5 + D1 T5 + D2 T5 + p B11 + r B9 + r B7 + r B6 qquad INR(AC) = r B5 qquad CLR(AC) = r B11

Each of the seven LD terms also selects which operation the adder and logic circuit performs: one stage i of that circuit is an OR of seven AND gates (AC(i) ∧ DR(i) with AND, the full-adder sum with ADD, DR(i) with LDA, INPR(i) with INP, AC(i)' with COM, AC(i+1) with SHR, AC(i−1) with SHL).

SolvedExample 4 · Control signals active in one step

Which control inputs and bus code are active at D5 T4 (BSA) and at D1 T5 (ADD)?

  1. D5 T4: M[AR] ← PC, AR ← AR + 1. PC on the bus (x2: S2 S1 S0 = 010), memory Write, INR(AR).
  2. D1 T5: AC ← AC + DR, E ← Cout. No bus transfer (the adder reads DR directly): S = 000, LD(AC) with the adder selected, E loaded from the carry.
AnswerBSA T4: S = 010, Write, INR(AR); ADD T5: S = 000, LD(AC)
SolvedExample 5 · Gate logic for PC

Derive LD, INR and CLR for PC.

  1. LD (from the bus): D4 T4: PC ← AR and D5 T5: PC ← AR. So LD(PC) = D4 T4 + D5 T5.
  2. CLR: R T1: PC ← 0. So CLR(PC) = R T1.
  3. INR: the fetch, the interrupt cycle, ISZ and every skip: INR(PC) = R' T1 + R T2 + D6 T6 (DR = 0) + r B4 AC(15)' + r B3 AC(15) + r B2 (AC = 0) + r B1 E' + p B9 FGI + p B8 FGO.
AnswerLD(PC) = D4T4 + D5T5; CLR(PC) = RT1; INR(PC) = the OR of the nine terms above

4. Verilog: The Control Gates

The module below is the gate logic of this section, written as Boolean equations (AR, AC, memory and the bus encoder). The testbench applies the IR value and timing step of several RTL statements and prints which control inputs are 1:

VERILOG
// Control gates of the basic computer: AR and AC control inputs, memory
// read/write and the bus select encoder, written straight from the RTL
module control_gates (
    input [7:0] D,
    input [15:0] T,
    input I,
    input R,
    input [11:0] B,
    output ldAR, inrAR, clrAR,
    output ldAC, inrAC, clrAC,
    output rd, wr,
    output [2:0] sel
);
    wire r = D[7] & ~I & T[3];
    wire p = D[7] & I & T[3];
    assign ldAR = ~R & T[0] | ~R & T[2] | ~D[7] & I & T[3];
    assign clrAR = R & T[0];
    assign inrAR = D[5] & T[4];
    assign ldAC = D[0] & T[5] | D[1] & T[5] | D[2] & T[5] | p & B[11]
                | r & B[9] | r & B[7] | r & B[6];
    assign inrAC = r & B[5];
    assign clrAC = r & B[11];
    assign rd = ~R & T[1] | ~D[7] & I & T[3] | (D[0] | D[1] | D[2] | D[6]) & T[4];
    assign wr = D[3] & T[4] | D[5] & T[4] | D[6] & T[6] | R & T[1];
    // which register drives the bus (x1 = AR ... x7 = memory)
    wire x1 = D[4] & T[4] | D[5] & T[5];
    wire x2 = T[0] | D[5] & T[4];
    wire x3 = D[6] & T[6];
    wire x4 = D[3] & T[4] | p & B[10];
    wire x5 = ~R & T[2];
    wire x6 = R & T[1];
    wire x7 = rd;
    // 8 x 3 encoder
    assign sel[0] = x1 | x3 | x5 | x7;
    assign sel[1] = x2 | x3 | x6 | x7;
    assign sel[2] = x4 | x5 | x6 | x7;
endmodule

module tb;
    reg [15:0] ir;
    reg [3:0] sc;
    reg R;
    wire [7:0] D = 8'b1 << ir[14:12];
    wire [15:0] T = 16'b1 << sc;
    wire ldAR, inrAR, clrAR, ldAC, inrAC, clrAC, rd, wr;
    wire [2:0] sel;
    control_gates u (D, T, ir[15], R, ir[11:0], ldAR, inrAR, clrAR,
                     ldAC, inrAC, clrAC, rd, wr, sel);
    task step(input [15:0] w, input [3:0] t, input rr, input [8*24:1] name);
        begin
            ir = w;
            sc = t;
            R = rr;
            #1 $display("bus=%b rd=%b wr=%b  AR(LD,INR,CLR)=%b%b%b  AC(LD,INR,CLR)=%b%b%b  %0s",
                        sel, rd, wr, ldAR, inrAR, clrAR, ldAC, inrAC, clrAC, name);
        end
    endtask
    initial begin
        step(16'h0000, 0, 0, "R'T0  AR <- PC");
        step(16'h0000, 1, 0, "R'T1  IR <- M[AR]");
        step(16'h9104, 2, 0, "R'T2  AR <- IR(0-11)");
        step(16'h9104, 3, 0, "ADD I, T3  AR <- M[AR]");
        step(16'h9104, 4, 0, "ADD T4  DR <- M[AR]");
        step(16'h9104, 5, 0, "ADD T5  AC <- AC + DR");
        step(16'h3104, 4, 0, "STA T4  M[AR] <- AC");
        step(16'h5104, 4, 0, "BSA T4  M[AR] <- PC");
        step(16'h5104, 5, 0, "BSA T5  PC <- AR");
        step(16'h6104, 6, 0, "ISZ T6  M[AR] <- DR");
        step(16'h7800, 3, 0, "CLA");
        step(16'h7020, 3, 0, "INC");
        step(16'hF800, 3, 0, "INP");
        step(16'h0000, 0, 1, "RT0  AR <- 0, TR <- PC");
        step(16'h0000, 1, 1, "RT1  M[AR] <- TR");
    end
endmodule

Each line matches the RTL: for example STA puts AC on the bus (100) and writes, BSA T4 puts PC on the bus (010), writes and increments AR, ADD T5 uses no bus and loads AC, and the interrupt cycle clears AR and then writes TR (110) into M[0]:

OUTPUT
bus=010 rd=0 wr=0  AR(LD,INR,CLR)=100  AC(LD,INR,CLR)=000  R'T0  AR <- PC
bus=111 rd=1 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=000  R'T1  IR <- M[AR]
bus=101 rd=0 wr=0  AR(LD,INR,CLR)=100  AC(LD,INR,CLR)=000  R'T2  AR <- IR(0-11)
bus=111 rd=1 wr=0  AR(LD,INR,CLR)=100  AC(LD,INR,CLR)=000  ADD I, T3  AR <- M[AR]
bus=111 rd=1 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=000  ADD T4  DR <- M[AR]
bus=000 rd=0 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=100  ADD T5  AC <- AC + DR
bus=100 rd=0 wr=1  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=000  STA T4  M[AR] <- AC
bus=010 rd=0 wr=1  AR(LD,INR,CLR)=010  AC(LD,INR,CLR)=000  BSA T4  M[AR] <- PC
bus=001 rd=0 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=000  BSA T5  PC <- AR
bus=011 rd=0 wr=1  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=000  ISZ T6  M[AR] <- DR
bus=000 rd=0 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=001  CLA
bus=000 rd=0 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=010  INC
bus=000 rd=0 wr=0  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=100  INP
bus=010 rd=0 wr=0  AR(LD,INR,CLR)=001  AC(LD,INR,CLR)=000  RT0  AR <- 0, TR <- PC
bus=110 rd=0 wr=1  AR(LD,INR,CLR)=000  AC(LD,INR,CLR)=000  RT1  M[AR] <- TR
💡 Tip

Key points to remember

  • INPR, OUTR (8 bits), flags FGI (input ready), FGO (output free). I/O instructions: p = D7 I T3.
  • Interrupt: T0' T1' T2' (IEN)(FGI + FGO): R ← 1; cycle RT0, RT1, RT2 saves PC in M[0], jumps to 1, clears IEN. Return: BUN 0 I.
  • Design rule: collect all statements that change a register; OR their control functions per input.
  • LD(AR) = R'T0 + R'T2 + D7' I T3, CLR(AR) = RT0, INR(AR) = D5 T4.
  • Bus encoder: S0 = x1 + x3 + x5 + x7, S1 = x2 + x3 + x6 + x7, S2 = x4 + x5 + x6 + x7.
🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Draw the input-output configuration of the basic computer and explain the role of FGI and FGO. List the I/O instructions with their RTL. (10 Marks)
  • Q2. What is a program interrupt? Draw the flowchart of the interrupt cycle and give its register transfers. Show memory before and after an interrupt. (10 Marks)
  • Write: the R ← 1 condition, RT0 to RT2, M[0] holds the return address, BUN 0 I to return.
  • Q3. Derive the control gate logic for AR and draw the gates. (5 Marks)
  • Q4. Design the bus select encoder of the basic computer and derive x1 to x7. (10 Marks)
  • Q5. Explain the design of the accumulator logic. Write LD, INR and CLR of AC. (5 Marks)