Project 1, Part 3: The Control Unit
The previous article identified all control signals required by the datapath. This article designs the finite state machine (FSM) that drives those control lines. The control unit executes instructions across three sequential states: FETCH, DECODE, and EXECUTE.
Control State Machine Architecture
The FSM transitions sequentially through three distinct operational phases:
- FETCH: Instruction memory outputs the instruction byte indexed by the Program Counter.
- DECODE: Register file addresses are decoded, and source operand data appears on read ports.
- EXECUTE: The ALU computes results, memory writes occur, and the Program Counter updates.
Executing each instruction across three clock cycles makes internal operations visible during waveform simulation.
Two-Block SystemVerilog Implementation
The control unit uses the standard two-process FSM coding architecture from Section 5.6:
typedef enum logic [1:0] {FETCH, DECODE, EXECUTE} cpu_state_t;
cpu_state_t state, next_state;
// Block 1: Synchronous state register update
always_ff @(posedge clk or negedge rst_n) begin
if (!rst_n) state <= FETCH;
else state <= next_state;
end
// Block 2: Combinational next-state and control output decode
always_comb begin
// Default assignments prevent unintended latch inference
next_state = state;
reg_wr_en = 1'b0;
mem_we = 1'b0;
pc_write = 1'b0;
alu_b_sel = 1'b0;
wr_data_sel = 2'b00;
branch_taken = 1'b0;
unique case (state)
FETCH: begin
next_state = DECODE;
end
DECODE: begin
next_state = EXECUTE;
end
EXECUTE: begin
next_state = FETCH;
unique case (opcode)
3'b000, 3'b001, 3'b010, 3'b011: begin // ADD, SUB, AND, OR
alu_b_sel = 1'b0; // Select register Rs
reg_wr_en = 1'b1; // Enable write-back
wr_data_sel = 2'b00; // Route ALU result
pc_write = 1'b1; // Advance Program Counter
end
3'b100: begin // LDI (Load Immediate)
reg_wr_en = 1'b1;
wr_data_sel = 2'b10; // Route immediate data
pc_write = 1'b1;
end
3'b101: begin // LD (Load from Memory)
reg_wr_en = 1'b1;
wr_data_sel = 2'b01; // Route memory data
pc_write = 1'b1;
end
3'b110: begin // ST (Store to Memory)
mem_we = 1'b1; // Assert memory write enable
pc_write = 1'b1;
end
3'b111: begin // BEQ (Branch if Equal to Zero)
branch_taken = (rd_data_a == 8'b0);
pc_write = 1'b1;
end
default: ; // Handled safely by top-level defaults
endcase
end
default: next_state = FETCH; // Safe recovery from illegal states
endcase
end
The top-level default assignments initialize all seven control signals to inactive values.
This practice eliminates unintended latch inference across incomplete branches.
The default clause in the outer case statement guarantees recovery if noise corrupts the state register.
Practice Exercise
Trace the control signals during the EXECUTE state for a BEQ instruction when rd_data_a equals 5.
Confirm that the Program Counter increments correctly.
Solution
For opcode 3'b111 with rd_data_a = 5:
- Condition
(rd_data_a == 8'b0)evaluates to false. - Signal
branch_takenevaluates to logic 0. - Signal
pc_writeasserts to logic 1. Becausebranch_takenis low, the datapath Program Counter multiplexer selectsPC + 1. The Program Counter advances to the next sequential instruction. Ifpc_writefailed to assert on unfulfilled branches, the core would lock up on the same address.
The control unit sequences datapath registers and multiplexers across fetch, decode, and execute states. Safe default assignments prevent latch inference in combinational decode logic. The next article integrates datapath and control units into a complete core and validates execution with a testbench.