Protocols

Checksum

A small fixed-width value computed from a block of data and sent alongside it so the receiver can detect corruption.

Detailed Explanation

A checksum is the cheapest form of error detection available in digital protocols. The sender runs a simple function — an XOR, a one's-complement sum, or a full CRC — over the payload, appends the result, and ships the bundle downstream. The receiver re-runs the same function and compares; a mismatch means bits flipped in transit and the packet must be dropped or retransmitted. Because the function is purely combinational over the payload, checksums are trivial to pipeline in RTL.

Real protocols tune the checksum function to the expected error model. Ethernet uses a CRC-32 because it catches all burst errors up to 32 bits and most longer ones. TCP/IP uses a much weaker one's-complement sum because the expensive CRC already sits in the MAC layer below. On-chip fabrics sometimes carry ECC instead so that single-bit errors can be *corrected*, not just detected.

Code Example

systemverilog
// One's-complement 16-bit checksum (IPv4 style)
module checksum_ip (
  input  logic [15:0] word_i,
  input  logic        valid_i, sof_i, eof_i,
  input  logic        clk, rst_n,
  output logic [15:0] csum_o,
  output logic        csum_valid_o
);
  logic [16:0] acc_q;
  always_ff @(posedge clk or negedge rst_n)
    if (!rst_n)        acc_q <= '0;
    else if (sof_i)    acc_q <= {1'b0, word_i};
    else if (valid_i)  acc_q <= acc_q + word_i + acc_q[16];
  assign csum_o       = ~(acc_q[15:0] + acc_q[16]);
  assign csum_valid_o = eof_i;
endmodule