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.
Produce a synthesizable, lint-clean combo_datapath module plus a self-checking testbench you can paste into the EcrioniX Verilog simulator and run immediately.
A minimal datapath slice: decode selects operands and operation; ALU computes result and zero flag.
Opcode op[2:0] | Operation | Operand B source |
|---|---|---|
3'b000 | ADD: a + b | Register b |
3'b001 | SUB: a - b | Register b |
3'b010 | AND: a & b | Register b |
3'b011 | OR: a | b | Register b |
3'b100 | XOR: a ^ b | Register b |
3'b101 | ADD imm: a + imm | Immediate imm |
3'b110 | PASS: a (copy) | Ignored |
3'b111 | CLR: output 0 | Ignored |
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.
op[2:0] opcode, a[7:0] and b[7:0] register operands, imm[7:0] immediateresult[7:0] ALU result, zero flag (1 when result is all zeros)always @(*), default assignments, named localparam opcodesWidth 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).
Decode translates op into internal control signals. Default-assign first, then override per opcode.
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
Select operand B from register or immediate, then compute based on alu_sel.
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);
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.
Copy this complete design into the simulator. It integrates decode, mux, and ALU in one clean module.
// 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
Paste this after the DUT in the simulator. It checks every opcode and prints PASS/FAIL.
// 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
Output 1 on a separate lt flag when a < b (unsigned). Keep it combinational.
Extract alu_core, op_decode, and operand_mux — practice hierarchy from Module 3.
Replace hard-coded 8 with parameter WIDTH = 8 and use {WIDTH{1'b0}} in defaults.
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.
case with defaultcase — one driver on src_balways @(*) assigns alu_out on every pathzero) derived via continuous assign, not sequential logicBefore moving on, confirm you can do each of these without looking at notes:
if/case blocksModule 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.
always @(*) block keeps latches out.localparam opcodes make decode readable and lint-friendly.