Review a packet counter from its interface through corner cases, then connect the code to expected hardware.
Reviewing RTL starts with a concrete question: what does this module accept, and what result does it promise? Here a stream feeds a packet sum.
Read the port contract
A transfer occurs at a rising edge when is true. Default parameters make each item a byte. marks the final accepted item:
i_valid
&&
o_ready
i_last
module packet_sum #( parameter int unsigned DATA_WIDTH = 8, parameter int unsigned SUM_WIDTH = 16) ( input logic i_clk, input logic i_rst, input logic i_valid, output logic o_ready, input logic [DATA_WIDTH-1:0] i_data, input logic i_last, output logic [SUM_WIDTH-1:0] o_sum, output logic o_sum_valid); logic [SUM_WIDTH-1:0] accumulator_q; logic [SUM_WIDTH-1:0] extended_data; always_comb begin extended_data = '0; extended_data[DATA_WIDTH-1:0] = i_data; o_ready = 1'b1; end always_ff @(posedge i_clk) begin if (i_rst) begin accumulator_q <= '0; o_sum <= '0; o_sum_valid <= 1'b0; end else begin o_sum_valid <= 1'b0; if (i_valid && o_ready) begin if (i_last) begin o_sum <= accumulator_q + extended_data; o_sum_valid <= 1'b1; accumulator_q <= '0; end else begin accumulator_q <= accumulator_q + extended_data; end end end endendmodule
o_ready stays high. Outside reset, the module accepts one item per cycle without backpressure. The output has no ready either, so its consumer must accept every announced result.
SUM_WIDTH must be at least DATA_WIDTH. The module does not bound packet length: overflow wraps modulo 2**SUM_WIDTH. The system must accept that behavior, limit packet length, or require overflow detection.
Follow a short sequence
For values 3, 5, and 2, with last on 2:
The first transfer stores 3 in the accumulator.
The second raises it to 8.
The last stores 8 + 2 in o_sum and clears the accumulator.
o_sum_valid marks the result during the cycle following that edge.
The final sum uses the previous accumulator and current input, as nonblocking assignments require. Single-item packets can yield results on consecutive cycles; o_sum_valid may then remain high without a low interval.
Check contract boundaries
Check a single-item packet, cycles with i_valid low, reset during a packet, consecutive packets, and sum overflow. Also reject SUM_WIDTH < DATA_WIDTH configurations.
If the consumer can stall, add result storage and backpressure rules. A valid bit alone does not retain a result.
Predict the hardware
The RTL needs an accumulator, adder, output register, and validity bit. The main path crosses the addition toward the accumulator or output. A higher target frequency may require different widths, pipelining, or stream behavior.