// sum8_pipe.sv // Same function as sum8_chain, rebuilt as a balanced adder tree with a // register after each level. Each register-to-register path now holds one // adder. Latency: 4 clock cycles (input register + three adder levels). `timescale 1ns/1ps module sum8_pipe #( parameter int W = 16 ) ( input logic clk, input logic rst, input logic in_valid, input logic [7:0][W-1:0] x, output logic out_valid, output logic [W+2:0] y ); logic [7:0][W-1:0] x_r; // stage 0: inputs logic [3:0][W:0] s1; // stage 1: four pair sums logic [1:0][W+1:0] s2; // stage 2: two sums of four logic [3:0] v; // valid pipeline, one bit per stage always_ff @(posedge clk) begin x_r <= x; for (int i = 0; i < 4; i++) begin s1[i] <= (W+1)'(x_r[2*i]) + (W+1)'(x_r[2*i+1]); end for (int i = 0; i < 2; i++) begin s2[i] <= (W+2)'(s1[2*i]) + (W+2)'(s1[2*i+1]); end y <= (W+3)'(s2[0]) + (W+3)'(s2[1]); end always_ff @(posedge clk) begin if (rst) begin v <= '0; end else begin v <= {v[2:0], in_valid}; end end assign out_valid = v[3]; endmodule