Build a 3-stage pipelined datapath with valid-ready handshakes: stage 1 latches operands, stage 2 runs a wide multiply partial, stage 3 finishes add/shift and presents the result. Includes global stall and flush for hazard practice.
Produce synthesizable pipe_alu_mul plus self-checking testbench you can paste into the EcrioniX Verilog simulator. This is the Module 8 checkpoint — throughput vs latency made concrete.
Valid-ready at input and output. Internal stages use enable/stall/flush from lessons 8.3–8.4.
| Stage | Function | Register |
|---|---|---|
| S1 | Latch a, b, op on s_valid&s_ready | s1_reg |
| S2 | Compute partial = (a[15:0] * b[15:0]) | s2_reg |
| S3 | Finish: ADD/MUL/AND per op; drive m_valid | s3_reg (output skid) |
s_valid, s_ready, a[15:0], b[15:0], op[1:0] (00=ADD, 01=MUL, 10=AND)m_valid, m_ready, result[31:0]stall freezes all stage regs; flush clears S1/S2 bubblesSteady-state throughput (m_ready=1):
// S1: accept operands when downstream not full
wire s1_full = s2_valid && s3_valid && !m_ready; // simple back-pressure
assign s_ready = !s1_full && !flush;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
s1_valid <= 1'b0;
s1_a <= 16'h0;
s1_b <= 16'h0;
s1_op <= 2'b00;
end else if (flush) begin
s1_valid <= 1'b0;
end else if (!stall) begin
if (s_valid && s_ready) begin
s1_valid <= 1'b1;
s1_a <= a;
s1_b <= b;
s1_op <= op;
end else if (s2_ready)
s1_valid <= 1'b0; // advanced to S2
end
end
s_ready must reflect downstream capacity — not combo-looped through s_valid. Compute readiness from stage occupancy and m_ready, not from the current beat's data.
// S2: partial multiply — breaks long combo path
wire [31:0] partial_mul = s1_a * s1_b;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin
s2_valid <= 1'b0;
s2_partial <= 32'h0;
s2_op <= 2'b00;
s2_a <= 16'h0;
s2_b <= 16'h0;
end else if (flush) begin
s2_valid <= 1'b0;
end else if (!stall && s1_valid && s2_ready) begin
s2_valid <= 1'b1;
s2_partial <= partial_mul;
s2_op <= s1_op;
s2_a <= s1_a;
s2_b <= s1_b;
end else if (s3_ready)
s2_valid <= 1'b0;
end
// S3: final ALU op + output valid-ready
function automatic [31:0] alu_finish (
input [1:0] op,
input [31:0] partial,
input [15:0] a, b
);
case (op)
2'b00: alu_finish = {16'h0, a} + {16'h0, b};
2'b01: alu_finish = partial;
2'b10: alu_finish = {16'h0, a} & {16'h0, b};
default: alu_finish = 32'h0;
endcase
endfunction
wire [31:0] s3_result_w = alu_finish(s2_op, s2_partial, s2_a, s2_b);
assign s2_ready = !s3_valid || m_ready;
assign m_valid = s3_valid;
assign result = s3_result;
always @(posedge clk or negedge rst_n) begin
if (!rst_n) s3_valid <= 1'b0;
else if (!stall) begin
if (s2_valid && s2_ready) begin
s3_valid <= 1'b1;
s3_result <= s3_result_w;
end else if (m_ready)
s3_valid <= 1'b0;
end
end
// pipe_alu_mul — Module 8.5 lab DUT
// 3-stage pipelined ALU/multiplier with valid-ready IO
module pipe_alu_mul (
input wire clk,
input wire rst_n,
input wire stall,
input wire flush,
input wire s_valid,
output wire s_ready,
input wire [15:0] a,
input wire [15:0] b,
input wire [1:0] op,
output wire m_valid,
input wire m_ready,
output wire [31:0] result
);
reg s1_valid, s2_valid, s3_valid;
reg [15:0] s1_a, s1_b, s2_a, s2_b;
reg [1:0] s1_op, s2_op;
reg [31:0] s2_partial, s3_result;
wire s2_ready;
wire [31:0] partial_mul = s1_a * s1_b;
assign s_ready = !stall && !flush &&
(!s3_valid || m_ready);
assign s2_ready = !s3_valid || m_ready;
assign m_valid = s3_valid;
assign result = s3_result;
function [31:0] finish_op;
input [1:0] op_in;
input [31:0] p;
input [15:0] ai, bi;
begin
case (op_in)
2'b00: finish_op = {16'h0, ai} + {16'h0, bi};
2'b01: finish_op = p;
2'b10: finish_op = {16'h0, ai} & {16'h0, bi};
default: finish_op = 32'h0;
endcase
end
endfunction
// S1
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin s1_valid <= 0; s1_a <= 0; s1_b <= 0; s1_op <= 0; end
else if (flush) s1_valid <= 0;
else if (!stall && s_valid && s_ready) begin
s1_valid <= 1; s1_a <= a; s1_b <= b; s1_op <= op;
end else if (s2_ready) s1_valid <= 0;
end
// S2
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin s2_valid <= 0; s2_partial <= 0; s2_op <= 0; s2_a <= 0; s2_b <= 0; end
else if (flush) s2_valid <= 0;
else if (!stall && s1_valid && s2_ready) begin
s2_valid <= 1; s2_partial <= partial_mul; s2_op <= s1_op;
s2_a <= s1_a; s2_b <= s1_b;
end else if (s3_valid && m_ready) s2_valid <= 0;
end
// S3
always @(posedge clk or negedge rst_n) begin
if (!rst_n) begin s3_valid <= 0; s3_result <= 0; end
else if (!stall) begin
if (s2_valid && s2_ready) begin
s3_valid <= 1;
s3_result <= finish_op(s2_op, s2_partial, s2_a, s2_b);
end else if (m_ready) s3_valid <= 0;
end
end
endmodule
module pipe_alu_mul_tb;
reg clk, rst_n, stall, flush;
reg s_valid, m_ready;
reg [15:0] a, b;
reg [1:0] op;
wire s_ready, m_valid;
wire [31:0] result;
pipe_alu_mul dut (
.clk(clk), .rst_n(rst_n), .stall(stall), .flush(flush),
.s_valid(s_valid), .s_ready(s_ready), .a(a), .b(b), .op(op),
.m_valid(m_valid), .m_ready(m_ready), .result(result)
);
initial clk = 0;
always #5 clk = ~clk;
integer errors, ops_sent, ops_rcvd;
reg [31:0] expect;
task drive_op(input [15:0] ta, tb; input [1:0] top);
begin
@(posedge clk); s_valid = 1; a = ta; b = tb; op = top;
wait(s_ready); @(posedge clk); s_valid = 0;
ops_sent = ops_sent + 1;
end
endtask
task check_result(input [31:0] exp);
begin
wait(m_valid); @(posedge clk);
if (result !== exp) begin
$display("FAIL: got %0d expected %0d", result, exp);
errors = errors + 1;
end else begin
$display("PASS: result %0d", result);
ops_rcvd = ops_rcvd + 1;
end
end
endtask
initial begin
errors = 0; ops_sent = 0; ops_rcvd = 0;
rst_n = 0; stall = 0; flush = 0; s_valid = 0; m_ready = 1;
#20 rst_n = 1;
// Test 1: ADD 10 + 20 = 30 (3-cycle latency)
drive_op(16'd10, 16'd20, 2'b00);
#5; // still in pipe
check_result(32'd30);
// Test 2: MUL 100 * 200 = 20000
drive_op(16'd100, 16'd200, 2'b01);
check_result(32'd20000);
// Test 3: back-pressure — hold m_ready low
m_ready = 0;
drive_op(16'd5, 16'd7, 2'b00);
repeat(4) @(posedge clk);
if (!m_valid) begin $display("FAIL: m_valid should be high"); errors++; end
m_ready = 1;
@(posedge clk);
check_result(32'd12);
// Test 4: burst throughput — 4 ops back-to-back
m_ready = 1;
drive_op(16'd1, 16'd2, 2'b00); // ADD → 3
drive_op(16'd3, 16'd4, 2'b00); // ADD → 7
drive_op(16'd2, 16'd3, 2'b01); // MUL → 6
drive_op(16'hFF, 16'h0F, 2'b10); // AND → 0x0F
check_result(32'd3);
check_result(32'd7);
check_result(32'd6);
check_result(32'h0F);
if (errors == 0) $display("=== ALL TESTS PASSED ===");
else $display("=== %0d TESTS FAILED ===", errors);
$finish;
end
endmodule
Assert stall=1 mid-burst and verify pipeline freezes without corrupting in-flight ops.
Drive flush with ops in S1/S2 — confirm no spurious m_valid.
Log ops_rcvd/cycles after warm-up; verify ratio → 1.0 in steady state.
Module 9 is next: Lint, CDC & QoR — clock-domain crossing rules, RDC/lint signoff, and closing timing on real blocks. You'll apply pipeline discipline to multi-clock designs.