Loading lessons...
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 control | Microprogrammed control | |
|---|---|---|
| Control logic | gates, flip-flops, decoders | control words stored in a control memory |
| Speed | fast | slower (a control-memory read per step) |
| Changing the design | rewire the circuit | rewrite the microprogram |
| Suits | simple, fixed instruction sets (RISC) | large instruction sets (CISC) |
The basic computer uses hardwired control (microprogrammed control is lesson 33).
Hardwired 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).
| Symbol | Step | Register transfers | Cycles |
|---|---|---|---|
| AND | D0 T4 | DR ← M[AR] | 6 |
| D0 T5 | AC ← AC wedge DR, \ SC ← 0 | ||
| ADD | D1 T4 | DR ← M[AR] | 6 |
| D1 T5 | AC ← AC + DR, \ E ← Cout, \ SC ← 0 | ||
| LDA | D2 T4 | DR ← M[AR] | 6 |
| D2 T5 | AC ← DR, \ SC ← 0 | ||
| STA | D3 T4 | M[AR] ← AC, \ SC ← 0 | 5 |
| BUN | D4 T4 | PC ← AR, \ SC ← 0 | 5 |
| BSA | D5 T4 | M[AR] ← PC, \ AR ← AR + 1 | 6 |
| D5 T5 | PC ← AR, \ SC ← 0 | ||
| ISZ | D6 T4 | DR ← M[AR] | 7 |
| D6 T5 | DR ← DR + 1 | ||
| D6 T6 | M[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:
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 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.
BSA 135 is at address 20. Find the contents of PC, AR and M[135] after it executes, and the instruction that returns.
- After the fetch, PC = 21 (the return address). AR = 135.
- T4: M[135] ← 21; AR ← 136.
- T5: PC ← 136. Execution continues with the subroutine at 136.
- The subroutine ends with 1 BUN 135 (indirect): EA = M[135] = 21, so PC ← 21.
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.
- Every fetch leaves PC = 022 and AR = 083 at T4.
- AND: DR = B8F2, AC = A937 ∧ B8F2 = A832.
- ADD: DR = B8F2, A937 + B8F2 = 1 6229, so AC = 6229, E = 1.
- LDA: DR = B8F2, AC = B8F2.
- STA: M[083] ← A937; AC unchanged.
- BUN: PC = 083.
- BSA: M[083] ← 022, AR = 084, PC = 084.
- ISZ: DR = B8F3 is written back to M[083]; not zero, so PC stays 022.
Answer:
| Opcode | PC | AR | DR | AC | IR |
|---|---|---|---|---|---|
| AND | 022 | 083 | B8F2 | A832 | 0083 |
| ADD | 022 | 083 | B8F2 | 6229 | 1083 |
| LDA | 022 | 083 | B8F2 | B8F2 | 2083 |
| STA | 022 | 083 | (unchanged) | A937 | 3083 |
| BUN | 083 | 083 | (unchanged) | A937 | 4083 |
| BSA | 084 | 084 | (unchanged) | A937 | 5083 |
| ISZ | 022 | 083 | B8F3 | A937 | 6083 |
3. Register-Reference Instructions
Register-reference instructions are recognised by D7 I' T3. They need no memory operand, so they execute in T3. Let
Each instruction has exactly one bit Bi set, so each control function is r Bi:
| Symbol | Control | Register transfer |
|---|---|---|
| (all) | r | SC ← 0 |
| CLA | r B11 | AC ← 0 |
| CLE | r B10 | E ← 0 |
| CMA | r B9 | AC ← overline{AC} |
| CME | r B8 | E ← overline{E} |
| CIR | r B7 | AC ← shr AC, \ AC(15) ← E, \ E ← AC(0) |
| CIL | r B6 | AC ← shl AC, \ AC(0) ← E, \ E ← AC(15) |
| INC | r B5 | AC ← AC + 1 |
| SPA | r B4 | if (AC(15) = 0) then (PC ← PC + 1) |
| SNA | r B3 | if (AC(15) = 1) then (PC ← PC + 1) |
| SZA | r B2 | if (AC = 0) then (PC ← PC + 1) |
| SZE | r B1 | if (E = 0) then (PC ← PC + 1) |
| HLT | r B0 | S ← 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.
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.
- CLA: AC = 0000. CLE: E = 0 (unchanged).
- CMA: AC = 56C8. CME: E = 1.
- 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).
- CIL: shift left with E = 0 entering at the right: 0101 0010 0110 1110 = 526E, E = 1 (the old bit 15).
- INC: AC = A938.
Write a segment that jumps to NEG if AC is negative and to POS otherwise.
- SPA skips the next instruction when AC(15) = 0 (positive or zero).
- Sequence: SPA / BUN NEG / BUN POS.
- AC negative: no skip, BUN NEG runs. AC positive: BUN NEG is skipped, BUN POS runs.
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:
| Address | Code | Instruction | Comment |
|---|---|---|---|
| 100 | 210D | LDA A | AC ← 100 |
| 101 | 110E | ADD B | AC ← 100 + 45 = 145 = 0091 |
| 102 | 010F | AND MSK | keep the low byte (00FF) |
| 103 | 3110 | STA SUM | |
| 104 | 6111 | ISZ CNT | CNT = −1 becomes 0: skip |
| 105 | 7001 | HLT | skipped |
| 106 | 5109 | BSA NEG | call the subroutine NEG |
| 107 | 3112 | STA RES | |
| 108 | 7001 | HLT | |
| 109 | 0000 | NEG: | return address |
| 10A | 7200 | CMA | |
| 10B | 7020 | INC | AC ← −AC |
| 10C | C109 | BUN NEG I | return |
// 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
endmoduleEach 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:
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 cyclesKey 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.
📝 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)