Lesson 29 +40 XP

Hardwired Control & Executing Memory-Reference and Register-Reference Instructions

Hardwired Control & Executing Memory-Reference and Register-Reference Instructions

Lesson 26 brought every instruction to timing step T3 (or T4) with its type known. This lesson finishes the job: the register transfer statements that execute each memory-reference and register-reference instruction, and the hardwired control unit that generates them. At the end we put every statement into one Verilog model of the basic computer and run a real program on it.

1. The Control Unit

The control unit issues the control signals (LD, INR, CLR of each register, the bus select, memory read and write) at the right timing step. There are two ways to build it:

Hardwired controlMicroprogrammed control
Control logicgates, flip-flops, decoderscontrol words stored in a control memory
Speedfastslower (a control-memory read per step)
Changing the designrewire the circuitrewrite the microprogram
Suitssimple, fixed instruction sets (RISC)large instruction sets (CISC)

The basic computer uses hardwired control (microprogrammed control is lesson 33).

Hardwired control unitHardwired control unit

Its inputs are:

  • the two decoders: D0 ldots D7 from the opcode and T0 ldots T15 from SC,
  • the mode bit I (from IR(15)) and the bits IR(0-11), which identify register-reference and I/O operations,
  • the state of other flip-flops and registers (E, AC, the I/O flags).

The control logic gates combine these into the control functions. Every row of the RTL tables below becomes an AND gate such as D1 T5, and every register input is the OR of the conditions that need it.

2. Memory-Reference Instructions

All seven start at T4 with the effective address in AR (put there at T2, or by the indirect read at T3).

SymbolStepRegister transfersCycles
ANDD0 T4DR ← M[AR]6
D0 T5AC ← AC wedge DR, \ SC ← 0
ADDD1 T4DR ← M[AR]6
D1 T5AC ← AC + DR, \ E ← Cout, \ SC ← 0
LDAD2 T4DR ← M[AR]6
D2 T5AC ← DR, \ SC ← 0
STAD3 T4M[AR] ← AC, \ SC ← 05
BUND4 T4PC ← AR, \ SC ← 05
BSAD5 T4M[AR] ← PC, \ AR ← AR + 16
D5 T5PC ← AR, \ SC ← 0
ISZD6 T4DR ← M[AR]7
D6 T5DR ← DR + 1
D6 T6M[AR] ← DR, if (DR = 0) then (PC ← PC + 1), SC ← 0

Notes on each:

  • AND, ADD, LDA need the operand in DR first, because the adder and logic circuit take their inputs from AC and DR, not from the bus. That costs the extra step T4.
  • LDA cannot load AC straight from memory: AC has no path from the bus. The word goes memory → DR → (through the adder and logic) → AC.
  • STA puts AC on the bus (select 100) and writes memory: one step.
  • BUN transfers the effective address to PC: the next fetch reads from there.
  • ISZ increments the memory word and skips the next instruction if it becomes zero. It is used to count loop iterations with a negative count (lesson 32). DR is incremented because memory has no incrementer.

2.1 BSA: calling a subroutine

BSA (branch and save return address) stores the return address (the current PC, which already points to the instruction after the BSA) in the first word of the subroutine, and starts executing at the word after it:

M[EA] ← PC, quad PC ← EA + 1

The subroutine returns with an indirect BUN to its first word, which holds the return address.

BSA stores the return address in the subroutine's first wordBSA stores the return address in the subroutine's first word

The single statement M[AR] ← PC, \ PC ← AR + 1 needs two steps in hardware: AR and PC cannot both use the bus in one clock, and AR has an incrementer while there is none on the path into PC. So T4 stores PC and increments AR; T5 copies AR into PC.

SolvedExample 1 · BSA with numbers

BSA 135 is at address 20. Find the contents of PC, AR and M[135] after it executes, and the instruction that returns.

  1. After the fetch, PC = 21 (the return address). AR = 135.
  2. T4: M[135] ← 21; AR ← 136.
  3. T5: PC ← 136. Execution continues with the subroutine at 136.
  4. The subroutine ends with 1 BUN 135 (indirect): EA = M[135] = 21, so PC ← 21.
AnswerPC = 136, AR = 136, M[135] = 21; return by BUN 135 I
SolvedExample 2 · All seven memory-reference instructions on the same data (Mano)

The instruction at address 021 has I = 0 and address part 083. M[083] = B8F2 and AC = A937 (hex). Find PC, AR, DR, AC and IR after each memory-reference opcode executes.

  1. Every fetch leaves PC = 022 and AR = 083 at T4.
  2. AND: DR = B8F2, AC = A937 ∧ B8F2 = A832.
  3. ADD: DR = B8F2, A937 + B8F2 = 1 6229, so AC = 6229, E = 1.
  4. LDA: DR = B8F2, AC = B8F2.
  5. STA: M[083] ← A937; AC unchanged.
  6. BUN: PC = 083.
  7. BSA: M[083] ← 022, AR = 084, PC = 084.
  8. ISZ: DR = B8F3 is written back to M[083]; not zero, so PC stays 022.

Answer:

OpcodePCARDRACIR
AND022083B8F2A8320083
ADD022083B8F262291083
LDA022083B8F2B8F22083
STA022083(unchanged)A9373083
BUN083083(unchanged)A9374083
BSA084084(unchanged)A9375083
ISZ022083B8F3A9376083

3. Register-Reference Instructions

Register-reference instructions are recognised by D7 I' T3. They need no memory operand, so they execute in T3. Let

r = D7 I' T3 qquad Bi = IR(i), \ i = 0, ldots, 11

Each instruction has exactly one bit Bi set, so each control function is r Bi:

SymbolControlRegister transfer
(all)rSC ← 0
CLAr B11AC ← 0
CLEr B10E ← 0
CMAr B9AC ← overline{AC}
CMEr B8E ← overline{E}
CIRr B7AC ← shr AC, \ AC(15) ← E, \ E ← AC(0)
CILr B6AC ← shl AC, \ AC(0) ← E, \ E ← AC(15)
INCr B5AC ← AC + 1
SPAr B4if (AC(15) = 0) then (PC ← PC + 1)
SNAr B3if (AC(15) = 1) then (PC ← PC + 1)
SZAr B2if (AC = 0) then (PC ← PC + 1)
SZEr B1if (E = 0) then (PC ← PC + 1)
HLTr B0S ← 0 (S is the start-stop flip-flop)

The skip instructions increment PC once more, so the next instruction (already pointed to by PC) is skipped. This is how the basic computer makes conditional branches: a skip followed by a BUN.

SolvedExample 3 · Register-reference instructions in sequence

AC = A937 and E = 0. Find AC and E after each of CLA, CLE, CMA, CME, CIR, CIL, INC applied separately to these starting values.

  1. CLA: AC = 0000. CLE: E = 0 (unchanged).
  2. CMA: AC = 56C8. CME: E = 1.
  3. CIR: A937 = 1010 1001 0011 0111. Shift right with E = 0 entering at the left: 0101 0100 1001 1011 = 549B, E = 1 (the old bit 0).
  4. CIL: shift left with E = 0 entering at the right: 0101 0010 0110 1110 = 526E, E = 1 (the old bit 15).
  5. INC: AC = A938.
Answer0000; E=0; 56C8; E=1; 549B with E=1; 526E with E=1; A938
SolvedExample 4 · A skip used as a branch

Write a segment that jumps to NEG if AC is negative and to POS otherwise.

  1. SPA skips the next instruction when AC(15) = 0 (positive or zero).
  2. Sequence: SPA / BUN NEG / BUN POS.
  3. AC negative: no skip, BUN NEG runs. AC positive: BUN NEG is skipped, BUN POS runs.
AnswerSPA, BUN NEG, BUN POS

4. Verilog: The Complete Basic Computer

The module basic_computer below contains every statement of lessons 26 and 29 (and the I/O instructions of lesson 30), each written under the timing condition from the tables. SC increments by default and the final step of each instruction overrides it with SC ← 0. The program, assembled by hand:

AddressCodeInstructionComment
100210DLDA AAC ← 100
101110EADD BAC ← 100 + 45 = 145 = 0091
102010FAND MSKkeep the low byte (00FF)
1033110STA SUM
1046111ISZ CNTCNT = −1 becomes 0: skip
1057001HLTskipped
1065109BSA NEGcall the subroutine NEG
1073112STA RES
1087001HLT
1090000NEG:return address
10A7200CMA
10B7020INCAC ← −AC
10CC109BUN NEG Ireturn
VERILOG
// Mano's basic computer: every register transfer statement of the control unit, as written
// in the RTL tables, executed on the timing signal T0..T6 it belongs to.
module basic_computer (
    input            clk,
    input            rst,           // synchronous reset: SC <- 0, S <- 1
    input      [11:0] start,        // address of the first instruction
    output reg       S              // start/stop flip-flop: 0 after HLT
);
    reg [15:0] M [0:4095];          // 4096 x 16 memory
    reg [11:0] AR, PC;
    reg [15:0] DR, AC, IR, TR;
    reg [7:0]  INPR, OUTR;
    reg        I, E, IEN, FGI, FGO;
    reg [3:0]  SC;                  // sequence counter

    wire [7:0]  D = 8'b1 << IR[14:12];      // operation decoder D0..D7
    wire [15:0] T = 16'b1 << SC;            // timing decoder T0..T15
    wire        r = D[7] & ~I & T[3];       // register-reference instruction
    wire        p = D[7] & I & T[3];        // input-output instruction
    wire [11:0] B = IR[11:0];

    always @(posedge clk) begin
        if (rst) begin
            SC <= 4'd0;
            S <= 1'b1;
            PC <= start;
            E <= 1'b0;
            IEN <= 1'b0;
            FGI <= 1'b0;
            FGO <= 1'b1;
        end else if (S) begin
            SC <= SC + 1'b1;
            // fetch and decode
            if (T[0]) AR <= PC;
            if (T[1]) begin
                IR <= M[AR];
                PC <= PC + 1'b1;
            end
            if (T[2]) begin
                AR <= IR[11:0];
                I <= IR[15];
            end
            if (~D[7] & I & T[3]) AR <= M[AR][11:0];        // indirect address
            // memory-reference instructions
            if ((D[0] | D[1] | D[2] | D[6]) & T[4]) DR <= M[AR];
            if (D[0] & T[5]) begin AC <= AC & DR;      SC <= 0; end      // AND
            if (D[1] & T[5]) begin {E, AC} <= AC + DR; SC <= 0; end      // ADD
            if (D[2] & T[5]) begin AC <= DR;           SC <= 0; end      // LDA
            if (D[3] & T[4]) begin M[AR] <= AC;        SC <= 0; end      // STA
            if (D[4] & T[4]) begin PC <= AR;           SC <= 0; end      // BUN
            if (D[5] & T[4]) begin                                       // BSA
                M[AR] <= {4'b0, PC};
                AR <= AR + 1'b1;
            end
            if (D[5] & T[5]) begin PC <= AR;           SC <= 0; end
            if (D[6] & T[5]) DR <= DR + 1'b1;                            // ISZ
            if (D[6] & T[6]) begin
                M[AR] <= DR;
                if (DR == 16'h0000) PC <= PC + 1'b1;
                SC <= 0;
            end
            // register-reference instructions (one bit of B each)
            if (r) begin
                SC <= 0;
                if (B[11]) AC <= 16'h0000;                               // CLA
                if (B[10]) E <= 1'b0;                                    // CLE
                if (B[9])  AC <= ~AC;                                    // CMA
                if (B[8])  E <= ~E;                                      // CME
                if (B[7])  begin AC <= {E, AC[15:1]}; E <= AC[0];  end   // CIR
                if (B[6])  begin AC <= {AC[14:0], E}; E <= AC[15]; end   // CIL
                if (B[5])  AC <= AC + 1'b1;                              // INC
                if (B[4] & ~AC[15]) PC <= PC + 1'b1;                     // SPA
                if (B[3] &  AC[15]) PC <= PC + 1'b1;                     // SNA
                if (B[2] & (AC == 16'h0000)) PC <= PC + 1'b1;            // SZA
                if (B[1] & ~E) PC <= PC + 1'b1;                          // SZE
                if (B[0])  S <= 1'b0;                                    // HLT
            end
            // input-output instructions
            if (p) begin
                SC <= 0;
                if (B[11]) begin AC[7:0] <= INPR; FGI <= 1'b0; end       // INP
                if (B[10]) begin OUTR <= AC[7:0]; FGO <= 1'b0; end       // OUT
                if (B[9] & FGI) PC <= PC + 1'b1;                         // SKI
                if (B[8] & FGO) PC <= PC + 1'b1;                         // SKO
                if (B[7]) IEN <= 1'b1;                                   // ION
                if (B[6]) IEN <= 1'b0;                                   // IOF
            end
        end
    end
endmodule

module tb;
    reg clk = 0, rst = 1;
    wire S;
    integer cyc = 0, total = 0;
    basic_computer u (clk, rst, 12'h100, S);
    always #5 clk = ~clk;
    task load(input [11:0] addr, input [15:0] word);
        u.M[addr] = word;
    endtask
    // print each instruction when it has finished (SC is back to 0)
    always @(posedge clk)
        if (!rst && u.S) cyc = cyc + 1;
    always @(negedge clk)
        if (u.SC == 0 && cyc > 0) begin
            $display("IR=%h  AC=%h E=%b  PC=%h  %0d cycles", u.IR, u.AC, u.E, u.PC, cyc);
            total = total + cyc;
            cyc = 0;
        end
    initial begin
        load(12'h100, 16'h210D); // LDA A
        load(12'h101, 16'h110E); // ADD B
        load(12'h102, 16'h010F); // AND MSK
        load(12'h103, 16'h3110); // STA SUM
        load(12'h104, 16'h6111); // ISZ CNT
        load(12'h105, 16'h7001); // HLT (skipped)
        load(12'h106, 16'h5109); // BSA NEG
        load(12'h107, 16'h3112); // STA RES
        load(12'h108, 16'h7001); // HLT
        load(12'h109, 16'h0000); // NEG: return address
        load(12'h10A, 16'h7200); // CMA
        load(12'h10B, 16'h7020); // INC
        load(12'h10C, 16'hC109); // BUN NEG I
        load(12'h10D, 16'h0064); // A = 100
        load(12'h10E, 16'h002D); // B = 45
        load(12'h10F, 16'h00FF); // MSK
        load(12'h110, 16'h0000); // SUM
        load(12'h111, 16'hFFFF); // CNT = -1
        load(12'h112, 16'h0000); // RES
        #12 rst = 0;
        wait (S == 0);
        #20 $display("SUM=%h CNT=%h NEG=%h RES=%h  total %0d cycles",
                     u.M[12'h110], u.M[12'h111], u.M[12'h109], u.M[12'h112], total);
        $finish;
    end
endmodule

Each line shows an instruction after it finished. The ISZ jumps PC from 104 to 106 (skip), BSA stores 107 in M[109] and continues at 10A, and the indirect BUN returns to 107. The cycle counts (6, 5, 7 and 4) are those of the tables, and 0091 negated is FF6F:

OUTPUT
IR=210d  AC=0064 E=0  PC=101  6 cycles
IR=110e  AC=0091 E=0  PC=102  6 cycles
IR=010f  AC=0091 E=0  PC=103  6 cycles
IR=3110  AC=0091 E=0  PC=104  5 cycles
IR=6111  AC=0091 E=0  PC=106  7 cycles
IR=5109  AC=0091 E=0  PC=10a  6 cycles
IR=7200  AC=ff6e E=0  PC=10b  4 cycles
IR=7020  AC=ff6f E=0  PC=10c  4 cycles
IR=c109  AC=ff6f E=0  PC=107  5 cycles
IR=3112  AC=ff6f E=0  PC=108  5 cycles
IR=7001  AC=ff6f E=0  PC=109  4 cycles
SUM=0091 CNT=0000 NEG=0107 RES=ff6f  total 58 cycles
💡 Tip

Key points to remember

  • Hardwired control: decoders (Di, Ti) + I + IR bits + flags → control logic gates.
  • AND/ADD/LDA: T4 DR ← M[AR], T5 operate. STA, BUN: one step at T4.
  • BSA: T4 M[AR] ← PC, AR ← AR + 1; T5 PC ← AR. Return with BUN I.
  • ISZ: read, increment DR, write back and skip if zero (T4 to T6).
  • Register-reference: r = D7 I' T3, control function r Bi; skips do PC ← PC + 1.
🎯 END-TERM EXAM PREP

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

  • Q1. Draw the block diagram of the hardwired control unit of the basic computer and explain its inputs. Compare hardwired and microprogrammed control. (10 Marks)
  • Q2. Write the register transfer statements for the execution of all memory-reference instructions. (10 Marks)
  • Write: the table of section 2 with the timing of each step and the reason DR is used.
  • Q3. Explain BSA and the return from a subroutine with a memory diagram. (5 Marks)
  • Q4. M[083] = B8F2, AC = A937 and the instruction at 021 has address 083. Find AC, PC and DR after AND, ADD, LDA, BSA and ISZ. (10 Marks)
  • Q5. Write the control functions and microoperations of the register-reference instructions. (5 Marks)