RTL Design / Module 4: Combinational Design from Scratch
Lesson 4.5 · Lab

Lab: clean combinational datapath slice

Time to wire everything together. In this lab you build a single-cycle combinational datapath: operand mux → ALU function unit → flag generation, all driven by a clean opcode decode. No clocks, no latches — just the building blocks from Lessons 4.1–4.4 assembled the way real CPUs start.

Lab goal

Produce a synthesizable, lint-clean combo_datapath module plus a self-checking testbench you can paste into the EcrioniX Verilog simulator and run immediately.

1

Block diagram — what you're building

Combinational datapath block diagram

A minimal datapath slice: decode selects operands and operation; ALU computes result and zero flag.

Opcode op[2:0]OperationOperand B source
3'b000ADD: a + bRegister b
3'b001SUB: a - bRegister b
3'b010AND: a & bRegister b
3'b011OR: a | bRegister b
3'b100XOR: a ^ bRegister b
3'b101ADD imm: a + immImmediate imm
3'b110PASS: a (copy)Ignored
3'b111CLR: output 0Ignored
Analogy

This slice is one row of a spreadsheet: inputs arrive, a formula is selected, a result appears — all in the same "row" with no memory between evaluations. That's combinational design.

2

Interface spec — read before coding

  • Inputs: op[2:0] opcode, a[7:0] and b[7:0] register operands, imm[7:0] immediate
  • Outputs: result[7:0] ALU result, zero flag (1 when result is all zeros)
  • Style: always @(*), default assignments, named localparam opcodes
  • No latches: every output assigned on every path — apply Lesson 4.4 rules

Width discipline: All operands are 8-bit. Addition/subtraction wrap on overflow — we do not implement carry-out in this lab (that's a sequential/flags extension in later modules).

3

Step 1 — Opcode decode block

Decode translates op into internal control signals. Default-assign first, then override per opcode.

decode_block.v (snippet)
localparam OP_ADD  = 3'b000;
localparam OP_SUB  = 3'b001;
localparam OP_AND  = 3'b010;
localparam OP_OR   = 3'b011;
localparam OP_XOR  = 3'b100;
localparam OP_ADDI = 3'b101;
localparam OP_PASS = 3'b110;
localparam OP_CLR  = 3'b111;

localparam ALU_ADD  = 3'd0;
localparam ALU_SUB  = 3'd1;
localparam ALU_AND  = 3'd2;
localparam ALU_OR   = 3'd3;
localparam ALU_XOR  = 3'd4;
localparam ALU_PASS = 3'd5;
localparam ALU_ZERO = 3'd6;

reg [2:0] alu_sel;
reg       use_imm;

always @(*) begin
  alu_sel  = ALU_ADD;
  use_imm  = 1'b0;
  case (op)
    OP_ADD:  begin alu_sel = ALU_ADD;  use_imm = 1'b0; end
    OP_SUB:  begin alu_sel = ALU_SUB;  use_imm = 1'b0; end
    OP_AND:  begin alu_sel = ALU_AND;  use_imm = 1'b0; end
    OP_OR:   begin alu_sel = ALU_OR;   use_imm = 1'b0; end
    OP_XOR:  begin alu_sel = ALU_XOR;  use_imm = 1'b0; end
    OP_ADDI: begin alu_sel = ALU_ADD;  use_imm = 1'b1; end
    OP_PASS: begin alu_sel = ALU_PASS; use_imm = 1'b0; end
    OP_CLR:  begin alu_sel = ALU_ZERO; use_imm = 1'b0; end
    default: begin alu_sel = ALU_ZERO; use_imm = 1'b0; end
  endcase
end
4

Step 2 — Operand mux + ALU core

Select operand B from register or immediate, then compute based on alu_sel.

operand_mux_alu.v (snippet)
wire [7:0] src_b = use_imm ? imm : b;
reg  [7:0] alu_out;

always @(*) begin
  alu_out = 8'h00;
  case (alu_sel)
    ALU_ADD:  alu_out = a + src_b;
    ALU_SUB:  alu_out = a - src_b;
    ALU_AND:  alu_out = a & src_b;
    ALU_OR:   alu_out = a | src_b;
    ALU_XOR:  alu_out = a ^ src_b;
    ALU_PASS: alu_out = a;
    ALU_ZERO: alu_out = 8'h00;
    default:  alu_out = 8'h00;
  endcase
end

assign result = alu_out;
assign zero   = (alu_out == 8'h00);
Trap

Don't drive result from both an assign and an always block. Pick one style per signal. This lab uses assign result = alu_out after an internal reg alu_out.

5

Full DUT — paste-ready module

Copy this complete design into the simulator. It integrates decode, mux, and ALU in one clean module.

combo_datapath.v
// combo_datapath — Module 4.5 lab DUT
// 8-bit combinational datapath: decode + operand mux + ALU
module combo_datapath (
  input  wire [2:0] op,
  input  wire [7:0] a,
  input  wire [7:0] b,
  input  wire [7:0] imm,
  output wire [7:0] result,
  output wire       zero
);

  localparam OP_ADD  = 3'b000;
  localparam OP_SUB  = 3'b001;
  localparam OP_AND  = 3'b010;
  localparam OP_OR   = 3'b011;
  localparam OP_XOR  = 3'b100;
  localparam OP_ADDI = 3'b101;
  localparam OP_PASS = 3'b110;
  localparam OP_CLR  = 3'b111;

  localparam ALU_ADD  = 3'd0;
  localparam ALU_SUB  = 3'd1;
  localparam ALU_AND  = 3'd2;
  localparam ALU_OR   = 3'd3;
  localparam ALU_XOR  = 3'd4;
  localparam ALU_PASS = 3'd5;
  localparam ALU_ZERO = 3'd6;

  reg [2:0] alu_sel;
  reg       use_imm;

  always @(*) begin
    alu_sel = ALU_ADD;
    use_imm = 1'b0;
    case (op)
      OP_ADD:  begin alu_sel = ALU_ADD;  use_imm = 1'b0; end
      OP_SUB:  begin alu_sel = ALU_SUB;  use_imm = 1'b0; end
      OP_AND:  begin alu_sel = ALU_AND;  use_imm = 1'b0; end
      OP_OR:   begin alu_sel = ALU_OR;   use_imm = 1'b0; end
      OP_XOR:  begin alu_sel = ALU_XOR;  use_imm = 1'b0; end
      OP_ADDI: begin alu_sel = ALU_ADD;  use_imm = 1'b1; end
      OP_PASS: begin alu_sel = ALU_PASS; use_imm = 1'b0; end
      OP_CLR:  begin alu_sel = ALU_ZERO; use_imm = 1'b0; end
      default: begin alu_sel = ALU_ZERO; use_imm = 1'b0; end
    endcase
  end

  wire [7:0] src_b = use_imm ? imm : b;
  reg  [7:0] alu_out;

  always @(*) begin
    alu_out = 8'h00;
    case (alu_sel)
      ALU_ADD:  alu_out = a + src_b;
      ALU_SUB:  alu_out = a - src_b;
      ALU_AND:  alu_out = a & src_b;
      ALU_OR:   alu_out = a | src_b;
      ALU_XOR:  alu_out = a ^ src_b;
      ALU_PASS: alu_out = a;
      ALU_ZERO: alu_out = 8'h00;
      default:  alu_out = 8'h00;
    endcase
  end

  assign result = alu_out;
  assign zero   = (alu_out == 8'h00);

endmodule
6

Self-checking testbench — run all opcodes

Paste this after the DUT in the simulator. It checks every opcode and prints PASS/FAIL.

combo_datapath_tb.v
// Self-checking testbench for combo_datapath
module combo_datapath_tb;
  reg  [2:0] op;
  reg  [7:0] a, b, imm;
  wire [7:0] result;
  wire       zero;

  combo_datapath dut (
    .op(op), .a(a), .b(b), .imm(imm),
    .result(result), .zero(zero)
  );

  integer errors;
  reg [7:0] expect;
  reg       expect_zero;

  task check;
    input [255:0] name;
    begin
      #1;
      if (result !== expect || zero !== expect_zero) begin
        $display("FAIL %s: op=%b got result=%h zero=%b expected %h %b",
                 name, op, result, zero, expect, expect_zero);
        errors = errors + 1;
      end else
        $display("PASS %s", name);
    end
  endtask

  initial begin
    errors = 0;
    a = 8'h0A; b = 8'h05; imm = 8'h03;

    op = 3'b000; expect = 8'h0F; expect_zero = 0; check("ADD");
    op = 3'b001; expect = 8'h05; expect_zero = 0; check("SUB");
    op = 3'b010; expect = 8'h00; expect_zero = 1; check("AND");
    op = 3'b011; expect = 8'h0F; expect_zero = 0; check("OR");
    op = 3'b100; expect = 8'h0F; expect_zero = 0; check("XOR");
    op = 3'b101; expect = 8'h0D; expect_zero = 0; check("ADDI");
    op = 3'b110; expect = 8'h0A; expect_zero = 0; check("PASS");
    op = 3'b111; expect = 8'h00; expect_zero = 1; check("CLR");

    if (errors == 0)
      $display("=== ALL TESTS PASSED ===");
    else
      $display("=== %0d TESTS FAILED ===", errors);
    $finish;
  end
endmodule
▶ Run lab in Verilog simulator
7

Extension challenges

➕

Add CMP opcode

Output 1 on a separate lt flag when a < b (unsigned). Keep it combinational.

🔀

Split into submodules

Extract alu_core, op_decode, and operand_mux — practice hierarchy from Module 3.

📐

Parameterize width

Replace hard-coded 8 with parameter WIDTH = 8 and use {WIDTH{1'b0}} in defaults.

Why this matters

Every CPU, DSP, and accelerator datapath starts as combinational slices like this, then gets registered at boundaries. Module 5 adds the registers, counters, and reset strategy that turn this slice into a clocked pipeline stage.

8

Combinational datapath coding checklist

  • Opcode table documented in header comment or localparams
  • Decode block uses default-assign + full case with default
  • Operand mux is a single clean ternary or case — one driver on src_b
  • ALU always @(*) assigns alu_out on every path
  • Flags (zero) derived via continuous assign, not sequential logic
  • Self-checking TB covers every opcode at least once
  • Zero inferred-latch warnings if you run lint locally
9

Module 4 checkpoint

Before moving on, confirm you can do each of these without looking at notes:

  • Build a parameterized mux and 2-to-N decoder from scratch
  • Implement ripple-carry or simple adder/comparator blocks
  • Design a small ALU with named opcodes and full case coverage
  • Spot and fix latch inference in incomplete if/case blocks
  • Integrate decode + mux + ALU into one lint-clean datapath slice
  • Write a self-checking testbench that exercises every opcode

Module 5 is next: Sequential Design from Scratch — flip-flops, counters, shift registers, and how to register this combinational slice at clock edges with clean reset. Your datapath becomes a real pipeline stage.

🎯 What to remember

  • A datapath slice = decode + operand select + function unit + flags, all combinational.
  • Default-assign discipline in every always @(*) block keeps latches out.
  • Named localparam opcodes make decode readable and lint-friendly.
  • Self-checking TBs are your safety net — test every opcode, not just the happy path.
  • Module 4 complete — Module 5 adds registers, clocks, and reset to this foundation.
Advertisement