Module 10: Capstone Projects · Project 1 — 8-bit RISC Processor Core

Project 1, Part 4: Assembly and Verification

The previous articles defined the instruction set, constructed the datapath, and implemented the control unit. This article integrates those subsystems into a unified top-level CPU module. We verify program execution against an independent behavioral golden reference model.

Top-Level Processor Integration

The top-level module instantiates instruction memory, data memory, register files, ALUs, and control units:

module cpu_top (
    input logic clk, rst_n
);
    logic [7:0] pc, instr, alu_result, rd_data_a, rd_data_b, wr_data, mem_rd_data;
    logic [1:0] rd_addr_a, rd_addr_b, wr_addr;
    logic [2:0] opcode, imm_addr;
    logic       reg_wr_en, mem_we, pc_write, alu_b_sel, branch_taken, zero_flag;
    logic [1:0] wr_data_sel;

    // Single-port instruction memory instance
    ram_sp #(.DATA_WIDTH(8), .DEPTH(256)) imem (
        .clk(clk), .we(1'b0), .addr(pc), .data_in(8'b0), .data_out(instr)
    );

    // Instruction decode logic
    assign opcode    = instr[7:5];
    assign wr_addr   = instr[4:3];
    assign rd_addr_a = instr[4:3];
    assign rd_addr_b = instr[2:1];
    assign imm_addr  = instr[2:0];

    reg_file rf (
        .clk(clk), .rd_addr_a(rd_addr_a), .rd_addr_b(rd_addr_b),
        .wr_addr(wr_addr), .wr_data(wr_data), .wr_en(reg_wr_en),
        .rd_data_a(rd_data_a), .rd_data_b(rd_data_b)
    );

    logic [7:0] alu_b;
    assign alu_b = alu_b_sel ? {5'b0, imm_addr} : rd_data_b;

    alu #(.WIDTH(8)) u_alu (
        .a(rd_data_a), .b(alu_b), .op({1'b0, opcode[1:0]}),
        .result(alu_result), .zero(zero_flag)
    );

    // Single-port data memory instance
    ram_sp #(.DATA_WIDTH(8), .DEPTH(256)) dmem (
        .clk(clk), .we(mem_we), .addr({5'b0, imm_addr}),
        .data_in(rd_data_a), .data_out(mem_rd_data)
    );

    always_comb begin
        unique case (wr_data_sel)
            2'b00:   wr_data = alu_result;
            2'b01:   wr_data = mem_rd_data;
            2'b10:   wr_data = {5'b0, imm_addr};
            default: wr_data = 8'b0;
        endcase
    end

    always_ff @(posedge clk or negedge rst_n) begin
        if (!rst_n) pc <= 8'b0;
        else if (pc_write) pc <= branch_taken ? {5'b0, imm_addr} : (pc + 1'b1);
    end

    cpu_control ctrl (
        .clk(clk), .rst_n(rst_n), .opcode(opcode), .rd_data_a(rd_data_a),
        .reg_wr_en(reg_wr_en), .mem_we(mem_we), .pc_write(pc_write),
        .alu_b_sel(alu_b_sel), .wr_data_sel(wr_data_sel), .branch_taken(branch_taken)
    );
endmodule

The design maps directly to the four-stage instruction execution specification. Because synchronous RAM read operations require one cycle, the multi-cycle FSM provides appropriate timing margins.

Behavioral Golden Reference Model

To verify processor execution reliably, construct an independent software reference model:

// Behavioral reference model executed independently in simulation memory
class cpu_reference_model;
    bit [7:0] regs[4];
    bit [7:0] mem[256];
    bit [7:0] pc;

    function void step(bit [7:0] instr);
        bit [2:0] opcode = instr[7:5];
        bit [1:0] rd     = instr[4:3];
        bit [1:0] rs     = instr[2:1];
        bit [2:0] imm    = instr[2:0];
        bit taken        = 0;

        case (opcode)
            3'b000: regs[rd] = regs[rd] + regs[rs];
            3'b001: regs[rd] = regs[rd] - regs[rs];
            3'b010: regs[rd] = regs[rd] & regs[rs];
            3'b011: regs[rd] = regs[rd] | regs[rs];
            3'b100: regs[rd] = {5'b0, imm};
            3'b101: regs[rd] = mem[imm];
            3'b110: mem[imm] = regs[rd];
            3'b111: if (regs[rd] == 0) begin pc = imm; taken = 1; end
        endcase
        if (!taken) pc = pc + 1'b1;
    endfunction
endclass

The reference model maintains independent register arrays and memory state. It computes expected values algorithmically without sharing RTL logic.

Self-Checking Verification Testbench

The testbench loads binary instructions into memory and compares final memory states:

module tb_cpu_top;
    logic clk = 0, rst_n;
    cpu_top dut (.clk(clk), .rst_n(rst_n));
    cpu_reference_model ref_model;

    always #5 clk = ~clk;

    bit [7:0] program [0:3] = '{
        8'b100_00_101,   // LDI R0, 5
        8'b100_01_011,   // LDI R1, 3
        8'b000_00_01_0,  // ADD R0, R1
        8'b110_00_000    // ST [0], R0
    };

    initial begin
        ref_model = new();
        rst_n = 0;

        // Load test program into hardware RAM and software reference model
        foreach (program[i]) begin
            dut.imem.mem[i] = program[i];
            ref_model.step(program[i]);
        end

        @(negedge clk);
        rst_n = 1;

        // Execute 4 instructions (3 cycles per instruction = 12 cycles)
        repeat (12) @(negedge clk);

        // Verify hardware memory against reference model calculation
        if (dut.dmem.mem[0] === ref_model.mem[0]) begin
            $display("PASS: Memory location [0] matches reference value %0d", dut.dmem.mem[0]);
        end else begin
            $display("FAIL: Got %0d, expected %0d", dut.dmem.mem[0], ref_model.mem[0]);
        end

        $finish;
    end
endmodule

The testbench verifies execution automatically. Automated comparison eliminates subjective inspection of waveform traces.

Practice Exercise

Extend the test program with an untaken branch: Add LDI R2, 1 followed by BEQ R2, 0. Verify that the Program Counter increments to address 6 rather than branching to 0.

Solution

Load the instructions and simulate two additional instructions (6 clock cycles):

dut.imem.mem[4] = 8'b100_10_001;   // LDI R2, 1
dut.imem.mem[5] = 8'b111_10_000;   // BEQ R2, 0

repeat (6) @(negedge clk);

if (dut.pc == 8'd6) begin
    $display("PASS: Untaken branch advanced sequentially to PC=6");
end else begin
    $display("FAIL: PC=%0d, expected 6", dut.pc);
end

Because register R2 contains non-zero data, branch_taken evaluates to low. Signal pc_write asserts, advancing the Program Counter to sequential address 6.

Integrating the ISA, datapath, and control unit completes the 8-bit RISC core implementation. Evaluating hardware outputs against an independent reference model proves functional correctness. This completes Project 1. Project 2 implements an asynchronous serial UART controller.

← Project 1, Part 3: The Control Unit Project 2, Part 1: The UART Protocol →