Block Codes: 64b/66b, 128b/130b and 128b/132b

Say what a sync header is, why it gives block lock, and what it costs.

You Will Be Able To

  • Compute the overhead and line rate of 8b/10b and of the block codes.
  • Explain how a receiver finds block boundaries from the sync header.
  • Predict when a block-lock machine locks, and when it loses lock.
  • Explain why a block code needs a scrambler.
Lesson
P10.4
Time
30 min
Runs on
Your browser
Check
5 questions

At 10 Gb/s, 8b/10b would need a 12.5 Gb/s line. A block code sends the same data with a line only 3% faster. The price is a receiver that has to hunt for the block boundaries itself.

CodeExtra bitsOverhead on the dataLine rate for 10 Gb/s of data
8b/10b2 per 825%12.5 Gb/s
64b/66b2 per 643.125%10.3125 Gb/s
128b/130b2 per 1281.5625%10.15625 Gb/s
128b/132b4 per 1283.125%10.3125 Gb/s

The overheads were printed by the bench from each layout's header and payload sizes. The line rate is the data rate times (1 + overhead).

A Block Is a Sync Header and a Payload

A block code sends a large payload unchanged and puts a short sync header in front of it.

One 64b/66b Block, Sender to ReceiverArrows carry bits. The header goes around the scrambler at both ends.
  • Neutral
  • Datapath
  • Control / FSM
  • Data
One 64b/66b block, sender to receiverArrows carry bits. The header goes around the scrambler at both ends. neutral Payload 64 data bits neutral Sync header 01 data, 10 control datapath Scrambler payload only neutral 66-bit block header sent first control Block lock finds the header datapath Descrambler payload back 64 bits2 bits, in the clearthe linepayload bits
  1. neutral Payload64 data bits
    • Scrambler
  2. datapath Scramblerpayload only
    • 64 bits → 66-bit block
  3. neutral Sync header01 data, 10 control
    • 2 bits, in the clear → 66-bit block
  4. neutral 66-bit blockheader sent first
    • the line → Block lock
  5. control Block lockfinds the header
    • payload bits → Descrambler
  6. datapath Descramblerpayload back
  1. Scramble the payload

    The 64 data bits pass through the scrambler. That gives them edges and balance on average.

  2. Add the header

    Two bits go in front: 01 for a data block, 10 for a control block. They are never scrambled.

  3. Find the boundary

    The receiver has only a stream of bits. It hunts for the place where the two-bit header keeps showing 01 or 10.

  4. Descramble

    Once locked, it strips the header and descrambles the payload.

Read the diagram as text
  • Payload: 64 data bits
  • Sync header: 01 data, 10 control
  • Scrambler: payload only
  • 66-bit block: header sent first
  • Block lock: finds the header
  • Descrambler: payload back
  • Payload → Scrambler
  • Scrambler → 66-bit block: 64 bits
  • Sync header → 66-bit block: 2 bits, in the clear
  • 66-bit block → Block lock: the line
  • Block lock → Descrambler: payload bits

The sync-bit values follow USB4 v2.0 Figures 4-22 and 4-23. The bench sends payload bit 0 first. USB4 sends byte B0 first, each byte bit 0 first, so bit 56 leads (§4.3.1.4, Figure 4-24). The scrambler is the bench's, as noted below.

In 64b/66b the payload is 64 bits and the header is 2 bits. USB4 draws a data block with sync bits 01 and an ordered-set block with 10 (§4.3.1.2 and §4.3.1.3, Figures 4-22 and 4-23). Its Gen 3 code, 128b/132b, uses 0101 and 1010.

USB 3.2 Gen 2 also uses 128b/132b, with the headers 0011b for data and 1100b for control (§6.3.2.2). Its four header bits allow a single bit error in the header to be corrected (§6.3.2.2). A receiver must correct it (§6.3.2.4). The bench does not correct header bits: it drops the block.

The values 00 and 11 are never sent. Every header therefore holds at least one transition, the only one the code itself guarantees.

Finding the Boundary: Block Lock

The receiver gets bits with no marks, as in 8b/10b. But a block code has no comma to look for. It has only the header.

The receiver therefore guesses a boundary and checks the two bits there. If they are 00 or 11, the guess is wrong: it slips the boundary by one bit. If they are 01 or 10, it counts a good header.

Block Lock, One Header Check at a TimeThe receiver starts 3 bits late. Each column is one check of the two bits where it believes a header sits. In this run it locks after 4 valid headers in a row.
0123456789header bits seen00100001valid (01 or 10)valid in a row012301234slips so far123
Scroll horizontally ↔
  1. Cycle 0

    The two bits are 00: not a header. The receiver slips its boundary by one bit and tries again a block later.

  2. Cycle 4

    After 2 slips the bits look valid three times in a row. The boundary is still wrong: random payload bits are 01 or 10 half the time.

  3. Cycle 5

    An invalid header ends the run. One more slip.

  4. Cycle 9

    Now every header is valid. After 4 in a row the receiver declares lock, 3 slips after it started.

Bench run with a lock count of 4. The lesson bench uses 64, so that a false run like checks 3 to 5 cannot reach it.

Random payload bits look like a valid header half the time. One valid header proves nothing, so the bench waits for 64 in a row. From each of the 66 possible start bits it locked with no wrong payload, after 0 to 65 slips.

Keeping Lock, and Losing It

Once locked, one bad header must not throw the lock away. One bit error can hit a header. But a slipped stream gives bad headers again and again.

The bench drops lock after 16 invalid headers within 64 blocks. A block with an invalid header is not delivered.

After lockUnlocksBlocks delivered of the next 500Delivered with a wrong payload
One header hit by a bit error05000
The channel loses one bit129917
tx.header_00 = 1: the transmitter sends 00100
rx.lock_on_first = 1, then one lost bit738098
rx.never_unlock = 1, then one lost bit0260259

Bench runs: lock after 64 valid headers, unlock after 16 invalid ones within 64 blocks. Both counts are bench values. A block with an invalid header is never delivered. Counts start after the event, so the block with the bad header is not among the 500.

Look at the lost bit. Between the slip and the unlock, 17 blocks came out with a wrong payload. Their headers looked valid by chance. That is why a link still needs a CRC above the block code.

The receiver that never unlocks is the worst. It delivered 259 wrong blocks and never recovered.

Why the Payload Is Scrambled

The header gives one guaranteed transition per block. The payload gives none of its own. A block of zeros is 64 zeros in a row.

The bench sent 1,500 blocks of zero data. Without the scrambler the longest run of equal bits was 65. With the self-synchronising scrambler of P10.5 it was 39.

A block code is therefore DC balanced and rich in edges only on average, through its scrambler. The header itself is never scrambled. USB 3.2 says the header bits bypass the scrambler and do not advance it (§6.3.2.3, rule 1).

The Gearbox

Logic inside a chip works on words of 32 or 64 bits. A block of 66 or 130 bits does not fit them evenly. A gearbox is a small bit FIFO between the two widths.

Each block adds header bits that the data words did not carry. A backlog therefore builds up on the line side, and the data side must pause now and then.

PIPE gives the rule for 128b/130b (§8.27). The MAC pauses its data for one clock exactly every N blocks. N is 4 on an 8-bit interface, 8 on a 16-bit one and 16 on a 32-bit one.

AMD's transceivers offer this as an option: a synchronous or an asynchronous gearbox for 64B/66B (PG182).

Try It

The receiver started 29 bits into the stream. It slipped 29 times, then saw 8 valid headers in a row and locked.

Every header error here was a wrong guess before lock. After lock every header was valid and every payload was right.

Add tx.header_00 = 1; after tx.use_66();. No position now has 8 valid headers in a row, so the receiver never locks and delivers nothing.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: slips 29, locked at line bit 5243, 100 headers checked 21 blocks delivered after lock, 0 with a wrong payload header errors 29, unlocks 0
Read included file: hs_pkg.sv
`timescale 1ns/1ns
// hs_pkg.sv: shared pure functions, and the bench random generator (read-only).
package hs_pkg;
  // xorshift32 (Marsaglia 2003): a linear generator over GF(2), period 2^32 - 1. A zero state is
  // replaced by 1, because zero maps to itself.
  function automatic [31:0] xs32(input [31:0] s);
    reg [31:0] x;
    x = (s == 32'd0) ? 32'd1 : s;
    x = x ^ (x << 13); x = x ^ (x >> 17); x = x ^ (x << 5);
    return x;
  endfunction
  // A Fibonacci PRBS generator x^n + x^m + 1 (PRBS7: 7,6; PRBS9: 9,5; PRBS15: 15,14; PRBS23: 23,18;
  // PRBS31: 31,28). The output bit is s[n-1]; the next state shifts the feedback into bit 0.
  function automatic [31:0] prbs_next(input [31:0] s, input int n, input int m);
    reg fb;
    fb = s[n-1] ^ s[m-1];
    return ((s << 1) | {31'd0, fb}) & ((n == 32) ? 32'hffffffff : ((32'd1 << n) - 32'd1));
  endfunction
  function automatic int ones(input [63:0] v);
    int n; n = 0;
    for (int i = 0; i < 64; i++) n += v[i];
    return n;
  endfunction
  // GF(16) with x^4 + x + 1, and GF(1024) with x^10 + x^3 + 1: shift-and-add multiply, no tables.
  function automatic [3:0] gf16_mul(input [3:0] a, input [3:0] b);
    reg [3:0] p, x; p = 4'd0; x = a;
    for (int i = 0; i < 4; i++) begin
      if (b[i]) p = p ^ x;
      x = x[3] ? ({x[2:0], 1'b0} ^ 4'b0011) : {x[2:0], 1'b0};
    end
    return p;
  endfunction
  function automatic [3:0] gf16_pow(input int e);   // alpha^e, alpha = x
    reg [3:0] p; int k; p = 4'd1; k = e % 15; if (k < 0) k += 15;
    for (int i = 0; i < k; i++) p = gf16_mul(p, 4'd2);
    return p;
  endfunction
  function automatic int gf16_log(input [3:0] a);   // -1 for 0
    reg [3:0] p; p = 4'd1;
    if (a == 4'd0) return -1;
    for (int i = 0; i < 15; i++) begin if (p == a) return i; p = gf16_mul(p, 4'd2); end
    return -1;
  endfunction
  function automatic [9:0] gf1024_mul(input [9:0] a, input [9:0] b);
    reg [9:0] p, x; p = 10'd0; x = a;
    for (int i = 0; i < 10; i++) begin
      if (b[i]) p = p ^ x;
      x = x[9] ? ({x[8:0], 1'b0} ^ 10'b0000001001) : {x[8:0], 1'b0};
    end
    return p;
  endfunction
  function automatic [9:0] gf1024_pow(input int e);
    reg [9:0] p; int k; p = 10'd1; k = e % 1023; if (k < 0) k += 1023;
    for (int i = 0; i < k; i++) p = gf1024_mul(p, 10'd2);
    return p;
  endfunction
endpackage

// hs_rng: the bench's random numbers. Same seed, same numbers, on every engine.
module hs_rng;
  int unsigned s = 32'h2545f491;
  function automatic void seed(input int unsigned v); s = (v == 0) ? 32'd1 : v; endfunction
  function automatic int unsigned next(); s = hs_pkg::xs32(s); return s; endfunction
  function automatic int below(input int n); s = hs_pkg::xs32(s); return (n <= 0) ? 0 : int'(s % n); endfunction
  function automatic bit bit1(); s = hs_pkg::xs32(s); return s[16]; endfunction
  function automatic bit chance_ppm(input int ppm); s = hs_pkg::xs32(s); return (s % 1000000) < ppm; endfunction
  function automatic [7:0] byte8(); s = hs_pkg::xs32(s); return s[23:16]; endfunction
endmodule
Read included file: hs_code.sv
`timescale 1ns/1ns
// hs_code.sv: line codes, CRCs and scramblers (read-only).

// ---------------------------------------------------------------------------------------------
// hs_8b10b: 8b/10b encoder and decoder with running disparity and the 12 K codes.
// code[9:0] = {a,b,c,d,e,i,f,g,h,j}; bit a (code[9]) goes on the wire first. rd: 0 = RD-, 1 = RD+.
//   encode(d, k, rd_in) -> {rd_out, code[9:0]}            (11 bits)
//   decode(code, rd_in) -> {rd_out, disp_err, code_err, k, d[7:0]}   (12 bits)
//   enc(d, k) / dec(code): the same, with the running disparity kept in rd_tx / rd_rx
// A K request for a byte that is not one of the 12 K codes is encoded as data and counted in bad_k.
// Decoder: code_err = the code is in neither column of the table; disp_err = it is a valid code
// only in the other column. rd_out follows the sub-block rule on the received bits.
// Faults: no_rd (always the RD- column), a7_never (never the alternate D.x.A7),
//         keep_rd_on_unbalanced (the 4b sub-block is chosen with the disparity from before the 6b one),
//         no_balanced_flip (D.07 and D.x.3 keep their RD- form at RD+), dec_no_disp_check.
module hs_8b10b;
  bit no_rd = 0, a7_never = 0, keep_rd_on_unbalanced = 0, no_balanced_flip = 0, dec_no_disp_check = 0;
  bit trace = 0;
  bit rd_tx = 0, rd_rx = 0;
  int bad_k = 0, symbols = 0, code_errs = 0, disp_errs = 0;
  bit last_k = 0, last_code_err = 0, last_disp_err = 0;
  bit valid_m [0:1023]; bit valid_p [0:1023]; bit [8:0] kd [0:1023]; bit built = 0;
  function automatic [5:0] t6(input [4:0] x);   // 5b/6b, RD- column, abcdei
    case (x)
      5'd0: return 6'b100111;  5'd1: return 6'b011101;  5'd2: return 6'b101101;  5'd3: return 6'b110001;
      5'd4: return 6'b110101;  5'd5: return 6'b101001;  5'd6: return 6'b011001;  5'd7: return 6'b111000;
      5'd8: return 6'b111001;  5'd9: return 6'b100101;  5'd10: return 6'b010101; 5'd11: return 6'b110100;
      5'd12: return 6'b001101; 5'd13: return 6'b101100; 5'd14: return 6'b011100; 5'd15: return 6'b010111;
      5'd16: return 6'b011011; 5'd17: return 6'b100011; 5'd18: return 6'b010011; 5'd19: return 6'b110010;
      5'd20: return 6'b001011; 5'd21: return 6'b101010; 5'd22: return 6'b011010; 5'd23: return 6'b111010;
      5'd24: return 6'b110011; 5'd25: return 6'b100110; 5'd26: return 6'b010110; 5'd27: return 6'b110110;
      5'd28: return 6'b001110; 5'd29: return 6'b101110; 5'd30: return 6'b011110; default: return 6'b101011;
    endcase
  endfunction
  function automatic [3:0] t4d(input [2:0] y);  // 3b/4b data, RD- column, fghj (y = 7 is P7)
    case (y)
      3'd0: return 4'b1011; 3'd1: return 4'b1001; 3'd2: return 4'b0101; 3'd3: return 4'b1100;
      3'd4: return 4'b1101; 3'd5: return 4'b1010; 3'd6: return 4'b0110; default: return 4'b1110;
    endcase
  endfunction
  function automatic [3:0] t4k(input [2:0] y);  // 3b/4b for K codes, RD- form before the flip
    case (y)
      3'd0: return 4'b1011; 3'd1: return 4'b0110; 3'd2: return 4'b1010; 3'd3: return 4'b1100;
      3'd4: return 4'b1101; 3'd5: return 4'b0101; 3'd6: return 4'b1001; default: return 4'b0111;
    endcase
  endfunction
  function automatic bit k_ok(input [7:0] d);
    return d[4:0] == 5'd28 || d == 8'hf7 || d == 8'hfb || d == 8'hfd || d == 8'hfe;
  endfunction
  function automatic int n6(input [5:0] v); return v[0] + v[1] + v[2] + v[3] + v[4] + v[5]; endfunction
  function automatic int n4(input [3:0] v); return v[0] + v[1] + v[2] + v[3]; endfunction
  // running disparity at the end of a sub-block (more ones: +; more zeros: -; 000111 and 0011: +; 111000 and 1100: -)
  function automatic bit rd6(input [5:0] v, input bit rd);
    return (n6(v) > 3) ? 1'b1 : (n6(v) < 3) ? 1'b0 : (v == 6'b000111) ? 1'b1 : (v == 6'b111000) ? 1'b0 : rd;
  endfunction
  function automatic bit rd4(input [3:0] v, input bit rd);
    return (n4(v) > 2) ? 1'b1 : (n4(v) < 2) ? 1'b0 : (v == 4'b0011) ? 1'b1 : (v == 4'b1100) ? 1'b0 : rd;
  endfunction
  // faults: [0] no_rd [1] a7_never [2] keep_rd_on_unbalanced [3] no_balanced_flip
  function automatic [10:0] core(input [7:0] d, input bit k, input bit rd_in, input [3:0] f);
    reg [5:0] six; reg [3:0] four; reg rd0, r6, rd1, a7, kk; reg [4:0] x; reg [2:0] y; int o6, o4;
    x = d[4:0]; y = d[7:5]; kk = k && k_ok(d); rd0 = f[0] ? 1'b0 : rd_in;
    six = (kk && x == 5'd28) ? 6'b001111 : t6(x); o6 = n6(six);
    if (rd0 && (o6 != 3 || (six == 6'b111000 && !f[3]))) begin six = ~six; o6 = 6 - o6; end
    r6 = (o6 > 3) ? 1'b1 : (o6 < 3) ? 1'b0 : (six == 6'b000111) ? 1'b1 : (six == 6'b111000) ? 1'b0 : rd0;
    rd1 = f[2] ? rd0 : r6;
    if (kk) begin
      four = t4k(y); if (rd1) four = ~four;
    end else begin
      a7 = (y == 3'd7) && !f[1] && ((!rd1 && (x == 5'd17 || x == 5'd18 || x == 5'd20)) || (rd1 && (x == 5'd11 || x == 5'd13 || x == 5'd14)));
      four = a7 ? 4'b0111 : t4d(y);
      if (rd1 && (n4(four) != 2 || (four == 4'b1100 && !f[3]))) four = ~four;
    end
    o4 = n4(four);
    return {(o4 > 2) ? 1'b1 : (o4 < 2) ? 1'b0 : (four == 4'b0011) ? 1'b1 : (four == 4'b1100) ? 1'b0 : r6, six, four};
  endfunction
  function automatic [10:0] encode(input [7:0] d, input bit k, input bit rd_in);
    if (k && !k_ok(d)) bad_k++;
    return core(d, k, rd_in, {no_balanced_flip, keep_rd_on_unbalanced, a7_never, no_rd});
  endfunction
  function automatic void build();
    reg [10:0] c;
    for (int i = 0; i < 1024; i++) begin valid_m[i] = 0; valid_p[i] = 0; kd[i] = 9'd0; end
    for (int i = 0; i < 268; i++) begin
      reg [7:0] d; reg k;
      if (i < 256) begin d = i[7:0]; k = 0; end
      else begin k = 1; d = (i < 264) ? {3'(i - 256), 5'd28} : (i == 264) ? 8'hf7 : (i == 265) ? 8'hfb : (i == 266) ? 8'hfd : 8'hfe; end
      c = core(d, k, 1'b0, 4'd0); valid_m[c[9:0]] = 1; kd[c[9:0]] = {k, d};
      c = core(d, k, 1'b1, 4'd0); valid_p[c[9:0]] = 1; kd[c[9:0]] = {k, d};
    end
    built = 1;
  endfunction
  function automatic [11:0] decode(input [9:0] code, input bit rd_in);
    reg here, there, ce, de, r;
    if (!built) build();
    here = rd_in ? valid_p[code] : valid_m[code];
    there = rd_in ? valid_m[code] : valid_p[code];
    ce = !(here || there); de = !ce && !here && !dec_no_disp_check;
    r = rd4(code[3:0], rd6(code[9:4], rd_in));
    return {r, de, ce, ce ? 9'd0 : kd[code]};
  endfunction
  function automatic [9:0] enc(input [7:0] d, input bit k);
    reg [10:0] r; r = encode(d, k, rd_tx); rd_tx = r[10];
    if (trace) $display("s%0d TX %s%0d.%0d code=%b rd=%s", symbols, (k && k_ok(d)) ? "K" : "D", d[4:0], d[7:5], r[9:0], r[10] ? "+" : "-");
    symbols++;
    return r[9:0];
  endfunction
  function automatic [8:0] dec(input [9:0] code);
    reg [11:0] r; r = decode(code, rd_rx); rd_rx = r[11];
    last_disp_err = r[10]; last_code_err = r[9]; last_k = r[8];
    if (r[9]) code_errs++; if (r[10]) disp_errs++;
    if (trace && (r[9] || r[10])) $display("RX %b %s", code, r[9] ? "code error" : "disparity error");
    return r[8:0];
  endfunction
  function automatic bit comma7(input [6:0] w); return w == 7'b0011111 || w == 7'b1100000; endfunction
endmodule

// ---------------------------------------------------------------------------------------------
// hs_crc: a CRC of 1 to 32 bits with any polynomial, seed, bit order and final inversion.
// The register shifts left; the polynomial omits the x^width term. The field is sent register bit
// width-1 first (USB 2.0, USB 3.2 and Ethernet all send it this way).
//   use_crc32(), use_crc16_usb2(), use_crc5_usb2(), use_crc16_usb3(): parameter sets
//   reset(), crc_bit(b), crc_byte(v), crc_bits(v, n), field(), field_bit(i), check_value(), crc_residue_ok()
// Faults: msb_first (bytes enter MSB first), no_invert (the field is not complemented), seed_zero.
module hs_crc;
  int width = 32;
  bit [31:0] poly = 32'h04c11db7, seed = 32'hffffffff, residue = 32'hc704dd7b;
  bit lsb_first = 1, invert = 1;
  bit msb_first = 0, no_invert = 0, seed_zero = 0;
  bit [31:0] r = 32'hffffffff;
  function automatic [31:0] mask(); return (width >= 32) ? 32'hffffffff : ((32'd1 << width) - 32'd1); endfunction
  function automatic void reset(); r = seed_zero ? 32'd0 : (seed & mask()); endfunction
  function automatic void set(input int w, input [31:0] p, input [31:0] s, input [31:0] res, input bit lsb, input bit inv);
    width = w; poly = p; seed = s; residue = res; lsb_first = lsb; invert = inv; reset();
  endfunction
  function automatic void use_crc32();      set(32, 32'h04c11db7, 32'hffffffff, 32'hc704dd7b, 1, 1); endfunction
  function automatic void use_crc16_usb2(); set(16, 32'h8005, 32'hffff, 32'h800d, 1, 1); endfunction
  function automatic void use_crc5_usb2();  set(5, 32'h05, 32'h1f, 32'h0c, 1, 1); endfunction
  function automatic void use_crc16_usb3(); set(16, 32'h100b, 32'hffff, 32'hf6aa, 1, 1); endfunction
  function automatic void crc_bit(input bit b);
    reg fb; fb = b ^ r[width - 1];
    r = ((r << 1) ^ (fb ? poly : 32'd0)) & mask();
  endfunction
  function automatic void crc_byte(input [7:0] v);
    for (int i = 0; i < 8; i++) crc_bit(v[(lsb_first ^ msb_first) ? i : 7 - i]);
  endfunction
  function automatic void crc_bits(input [31:0] v, input int n);   // n bits of v, bit 0 first
    for (int i = 0; i < n; i++) crc_bit(v[i]);
  endfunction
  function automatic [31:0] field(); return (invert && !no_invert) ? (~r & mask()) : r; endfunction
  function automatic bit field_bit(input int i); reg [31:0] f; f = field(); return f[width - 1 - i]; endfunction
  // the usual published "check" number: the field with its first-sent bit read as bit 0
  function automatic [31:0] check_value();
    reg [31:0] f, o; f = field(); o = 32'd0;
    for (int i = 0; i < width; i++) o[i] = f[width - 1 - i];
    return lsb_first ? o : f;
  endfunction
  function automatic bit crc_residue_ok(); return r == (residue & mask()); endfunction
endmodule

// ---------------------------------------------------------------------------------------------
// hs_scr: additive (frame-synchronous) and multiplicative (self-synchronising) scramblers.
// poly holds the coefficients: bit k is x^k; the +1 term is always used (taps() sets bit 0); len is the degree.
// Additive: a Galois LFSR. The key bit is s[len-1]; each step shifts left and adds the low terms of
//   the polynomial when the key bit is 1. The data never enters the register.
// Multiplicative: the line bit is the data bit XOR the register bits at the polynomial's terms;
//   the scrambler shifts its own output in, the descrambler shifts the received line bit in.
//   scr_add(len, poly, seed), scr_mul(len, poly), use_usb3_gen1(), use_x58(), use_x23_usb3() (see its note)
//   scr_bit(b), dscr_bit(b), scr_byte(v), dscr_byte(v) (bit 0 first), scr_reset(), scr_hold() (a skip
//   symbol: no step), scr_unscrambled() (a symbol sent in the clear: 8 steps, data untouched)
// Faults: advance_on_skip (scr_hold steps the LFSR 8 times), no_reset (scr_reset does nothing),
//         wrong_tap (the lowest term x^k becomes x^(k+1)), seed_zero (scr_reset loads 0).
module hs_scr;
  bit mult = 0; int len = 16;
  bit [63:0] poly = 64'h10039, seed = 64'hffff;
  bit advance_on_skip = 0, no_reset = 0, wrong_tap = 0, seed_zero = 0;
  bit [63:0] s = 64'hffff;
  int steps = 0;
  function automatic [63:0] lmask(); return (len >= 64) ? 64'hffffffffffffffff : ((64'd1 << len) - 64'd1); endfunction
  function automatic [63:0] taps();
    reg [63:0] p; int k; p = poly | 64'd1; k = 1;
    if (wrong_tap) begin
      while (k < len && !p[k]) k++;
      if (k < len) begin p[k] = 1'b0; p[k + 1] = 1'b1; end
    end
    return p;
  endfunction
  function automatic void load(); s = (seed_zero ? 64'd0 : seed) & lmask(); endfunction
  function automatic void scr_add(input int n, input [63:0] p, input [63:0] sd); mult = 0; len = n; poly = p; seed = sd; s = sd & lmask(); endfunction
  function automatic void scr_mul(input int n, input [63:0] p); mult = 1; len = n; poly = p; seed = 64'd0; s = 64'd0; endfunction
  function automatic void use_usb3_gen1(); scr_add(16, 64'h10039, 64'hffff); endfunction   // x^16 + x^5 + x^4 + x^3 + 1, seed FFFFh
  function automatic void use_x58(); scr_mul(58, (64'd1 << 58) | (64'd1 << 39) | 64'd1); endfunction   // x^58 + x^39 + 1
  // USB 3.2 Gen 2 (§6.3.2.3) polynomial x^23 + x^21 + x^16 + x^8 + x^5 + x^2 + 1 with seed 1DBFBCh. Only the
  // polynomial and seed are the specification's: the register form here is this library's Galois form, and
  // the reset and bypass rules of §6.3.2.3 are not modelled, so do not quote its key stream as USB's.
  function automatic void use_x23_usb3(); scr_add(23, (64'd1 << 23) | (64'd1 << 21) | (64'd1 << 16) | (64'd1 << 8) | (64'd1 << 5) | (64'd1 << 2) | 64'd1, 64'h1dbfbc); endfunction
  function automatic void scr_reset(); if (!no_reset) load(); endfunction
  function automatic bit key(); return s[len - 1]; endfunction
  function automatic void step();
    reg kb; kb = s[len - 1];
    s = ((s << 1) & lmask()) ^ (kb ? (taps() & lmask()) : 64'd0);
    steps++;
  endfunction
  // the XOR of the register bits at the polynomial's terms x^1..x^len (bit k-1 of the register for x^k)
  function automatic bit fbx(input [63:0] st); return ^(st & (taps() >> 1) & lmask()); endfunction
  function automatic bit scr_bit(input bit b);
    reg o;
    if (!mult) begin o = b ^ s[len - 1]; step(); end
    else begin o = b ^ fbx(s); s = ((s << 1) | {63'd0, o}) & lmask(); steps++; end
    return o;
  endfunction
  function automatic bit dscr_bit(input bit b);
    reg o;
    if (!mult) begin o = b ^ s[len - 1]; step(); end
    else begin o = b ^ fbx(s); s = ((s << 1) | {63'd0, b}) & lmask(); steps++; end
    return o;
  endfunction
  function automatic [7:0] scr_byte(input [7:0] v);
    reg [7:0] o; for (int i = 0; i < 8; i++) o[i] = scr_bit(v[i]); return o;
  endfunction
  function automatic [7:0] dscr_byte(input [7:0] v);
    reg [7:0] o; for (int i = 0; i < 8; i++) o[i] = dscr_bit(v[i]); return o;
  endfunction
  // a skip symbol passes unscrambled and the LFSR holds
  function automatic void scr_hold(); if (advance_on_skip && !mult) for (int i = 0; i < 8; i++) step(); endfunction
  // another symbol that is sent unscrambled (a K code other than COM and SKP): the LFSR still steps 8 times
  function automatic void scr_unscrambled(); if (!mult) for (int i = 0; i < 8; i++) step(); endfunction
endmodule
Read included file: hs_blk.sv
`timescale 1ns/1ns
// hs_blk.sv: block codes (read-only). A block is a sync header and a payload. Headers are written as
// they go on the wire, most significant bit first ("01" sends 0, then 1). The payload is sent bit 0
// first and is scrambled with a self-synchronising x^58 + x^39 + 1 scrambler (the header never is).
// Every layout uses that 64b/66b-style scrambler. USB 3.2 Gen 2 (§6.3.2.3) and USB4 (§4.3.1.5) use an
// additive x^23 LFSR kept in step by ordered sets instead; that is not modelled here (hs_scr has the
// polynomial as use_x23_usb3() for a scrambler on its own). Set scramble = 0 / descramble = 0 for none.
//   use_66()        64b/66b: data 01, control 10 (USB4 v2.0 Figure 4-22/4-23)
//   use_132_usb4()  128b/132b: data 0101, ordered set 1010 (USB4 v2.0 Figure 4-22/4-23)
//   use_132_usb3()  128b/132b: data 0011b, control 1100b, bit 0 first (USB 3.2 §6.3.2.2, Figure 6-11),
//                   so on the wire data is 1100 and control is 0011
//   use_130()       128b/130b with the 64b/66b header values: a bench layout (PCIe's own values are unverified)
// hs_blk (transmitter): blk_tx(payload, ctrl) -> the block in the low n bits, bit n-1 sent first.
// hs_blk_lock (receiver): push(b) one line bit at a time; returns 1 when a block with a valid header is
//   complete and locked (then hdr, ctrl and payload hold it). A locked block with an invalid header is not
//   delivered: push returns 0, hdr_ok is 0 and hdr_errs counts it. USB 3.2's single-bit header correction
//   (§6.3.2.2) is not modelled. Lock after lock_good valid headers in a row; an invalid header
//   before lock slips the boundary by one bit; once locked, bad_limit invalid headers within a window of
//   bad_window blocks lose lock. The default counts (64, 64, 16) are those of IEEE 802.3 Figure 49-14 (read 2026-11-14).
// hs_gearbox: a bit FIFO between a block width and a word width: gb_push(v, n), gb_pop(n), gb_level().
// Faults: header_00 (hs_blk sends 00 on every block), lock_on_first and never_unlock (hs_blk_lock).
module hs_blk;
  int hdr_bits = 2, pay_bits = 64; bit [3:0] hdr_data = 4'b0001, hdr_ctrl = 4'b0010;
  bit scramble = 1, header_00 = 0, trace = 0; int blocks = 0;
  hs_scr scr(); bit ready = 0;
  // set up on first use (an initial block here could run after the bench's first call)
  function automatic void prep(); if (!ready) begin scr.use_x58(); scr.s = 64'h3ff_ffff_ffff_ffff; ready = 1; end endfunction
  function automatic void set(input int h, input int p, input [3:0] d, input [3:0] c); hdr_bits = h; pay_bits = p; hdr_data = d; hdr_ctrl = c; endfunction
  function automatic void use_66(); set(2, 64, 4'b0001, 4'b0010); endfunction
  function automatic void use_130(); set(2, 128, 4'b0001, 4'b0010); endfunction
  function automatic void use_132_usb4(); set(4, 128, 4'b0101, 4'b1010); endfunction
  function automatic void use_132_usb3(); set(4, 128, 4'b1100, 4'b0011); endfunction
  function automatic int n(); return hdr_bits + pay_bits; endfunction
  function automatic [131:0] blk_tx(input [127:0] payload, input bit ctrl);
    reg [131:0] v; reg [3:0] h; int m; prep(); m = n(); v = 132'd0;
    h = header_00 ? 4'd0 : ctrl ? hdr_ctrl : hdr_data;
    for (int i = 0; i < hdr_bits; i++) v[m - 1 - i] = h[hdr_bits - 1 - i];
    for (int i = 0; i < pay_bits; i++) v[m - 1 - hdr_bits - i] = scramble ? scr.scr_bit(payload[i]) : payload[i];
    if (trace) $display("b%0d TX %s", blocks, ctrl ? "ctrl" : "data");
    blocks++;
    return v;
  endfunction
endmodule

module hs_blk_lock;
  int hdr_bits = 2, pay_bits = 64; bit [3:0] hdr_data = 4'b0001, hdr_ctrl = 4'b0010;
  int lock_good = 64, bad_window = 64, bad_limit = 16;
  bit descramble = 1, lock_on_first = 0, never_unlock = 0, trace = 0;
  bit locked = 0; int nbits = 0, pos = 0, slips = 0, good_run = 0, hdr_errs = 0, blocks = 0, lock_at = -1, unlocks = 0, win_n = 0, win_bad = 0;
  bit [131:0] sh = 132'd0; bit [3:0] hdr; bit ctrl; bit hdr_ok = 0; bit [127:0] payload;
  hs_scr dscr(); bit ready = 0;
  function automatic void set(input int h, input int p, input [3:0] d, input [3:0] c); hdr_bits = h; pay_bits = p; hdr_data = d; hdr_ctrl = c; endfunction
  function automatic void use_66(); set(2, 64, 4'b0001, 4'b0010); endfunction
  function automatic void use_130(); set(2, 128, 4'b0001, 4'b0010); endfunction
  function automatic void use_132_usb4(); set(4, 128, 4'b0101, 4'b1010); endfunction
  function automatic void use_132_usb3(); set(4, 128, 4'b1100, 4'b0011); endfunction
  function automatic void restart(); locked = 0; nbits = 0; pos = 0; slips = 0; good_run = 0; hdr_errs = 0; blocks = 0; lock_at = -1; unlocks = 0; win_n = 0; win_bad = 0; endfunction
  function automatic bit push(input bit b);
    int m; reg ok; m = hdr_bits + pay_bits;
    if (!ready) begin dscr.use_x58(); ready = 1; end
    sh = {sh[130:0], b}; nbits++; pos++;
    if (pos < m) return 0;
    pos = 0; hdr = 4'd0;
    for (int i = 0; i < hdr_bits; i++) hdr[hdr_bits - 1 - i] = sh[m - 1 - i];
    ok = (hdr == hdr_data) || (hdr == hdr_ctrl); ctrl = (hdr == hdr_ctrl); hdr_ok = ok;
    blocks++;
    if (!ok) hdr_errs++;
    if (!locked) begin
      if (ok) begin
        good_run++;
        if (good_run >= (lock_on_first ? 1 : lock_good)) begin
          locked = 1; win_n = 0; win_bad = 0; if (lock_at < 0) lock_at = nbits;
          if (trace) $display("b%0d LOCK bit=%0d slips=%0d", blocks, nbits, slips);
        end
      end else begin good_run = 0; pos = -1; slips++; end
      return 0;
    end
    win_n++; if (!ok) win_bad++;
    if (win_bad >= bad_limit && !never_unlock) begin
      locked = 0; good_run = 0; unlocks++; pos = -1; slips++;
      if (trace) $display("b%0d UNLOCK bit=%0d", blocks, nbits);
      return 0;
    end
    if (win_n >= bad_window) begin win_n = 0; win_bad = 0; end
    // the descrambler still takes the payload bits of a bad block, so it stays in step
    for (int i = 0; i < pay_bits; i++) payload[i] = descramble ? dscr.dscr_bit(sh[m - 1 - hdr_bits - i]) : sh[m - 1 - hdr_bits - i];
    return ok;
  endfunction
endmodule

module hs_gearbox;
  bit q [0:1023]; int head = 0, count = 0, overflows = 0;
  function automatic void gb_push(input [131:0] v, input int n);   // bit n-1 first
    for (int i = n - 1; i >= 0; i--) begin
      if (count == 1024) overflows++; else begin q[(head + count) % 1024] = v[i]; count++; end
    end
  endfunction
  function automatic int gb_level(); return count; endfunction
  function automatic [131:0] gb_pop(input int n);                    // the oldest bit lands in bit n-1
    reg [131:0] w; w = 132'd0;
    for (int i = n - 1; i >= 0 && count > 0; i--) begin w[i] = q[head]; head = (head + 1) % 1024; count--; end
    return w;
  endfunction
endmodule

Check Yourself

Predict the outputA 64b/66b receiver starts exactly on a block boundary. It locks after 4 valid headers in a row. Every header is valid. The program prints the line bit at which it locks. What does it print?
`timescale 1ns/1ns
module tb;
  hs_blk tx(); hs_blk_lock rx(); hs_rng rng();
  reg [131:0] v; reg [127:0] pay; int delivered, wrong;
  // one block: random payload (one control block in eight), every bit pushed into the receiver
  task automatic block(input int drop_at);
    pay[127:96] = rng.next(); pay[95:64] = rng.next(); pay[63:32] = rng.next(); pay[31:0] = rng.next();
    v = tx.blk_tx(pay, rng.below(8) == 0);
    for (int i = tx.n() - 1; i >= 0; i--) if (i != drop_at) if (rx.push(v[i])) begin
      delivered++;
      if (delivered > 1 && ((rx.payload ^ pay) << (128 - tx.pay_bits)) != 128'd0) wrong++;
    end
  endtask
  initial begin
    tx.use_66(); rx.use_66(); rng.seed(32'd9);
    rx.lock_good = 4;                          // lock after 4 valid headers in a row
    for (int b = 0; b < 10; b++) block(-1);    // the receiver starts on a block boundary
    $display("%0d", rx.lock_at);
    $finish;
  end
endmodule
Read included file: hs_pkg.sv
`timescale 1ns/1ns
// hs_pkg.sv: shared pure functions, and the bench random generator (read-only).
package hs_pkg;
  // xorshift32 (Marsaglia 2003): a linear generator over GF(2), period 2^32 - 1. A zero state is
  // replaced by 1, because zero maps to itself.
  function automatic [31:0] xs32(input [31:0] s);
    reg [31:0] x;
    x = (s == 32'd0) ? 32'd1 : s;
    x = x ^ (x << 13); x = x ^ (x >> 17); x = x ^ (x << 5);
    return x;
  endfunction
  // A Fibonacci PRBS generator x^n + x^m + 1 (PRBS7: 7,6; PRBS9: 9,5; PRBS15: 15,14; PRBS23: 23,18;
  // PRBS31: 31,28). The output bit is s[n-1]; the next state shifts the feedback into bit 0.
  function automatic [31:0] prbs_next(input [31:0] s, input int n, input int m);
    reg fb;
    fb = s[n-1] ^ s[m-1];
    return ((s << 1) | {31'd0, fb}) & ((n == 32) ? 32'hffffffff : ((32'd1 << n) - 32'd1));
  endfunction
  function automatic int ones(input [63:0] v);
    int n; n = 0;
    for (int i = 0; i < 64; i++) n += v[i];
    return n;
  endfunction
  // GF(16) with x^4 + x + 1, and GF(1024) with x^10 + x^3 + 1: shift-and-add multiply, no tables.
  function automatic [3:0] gf16_mul(input [3:0] a, input [3:0] b);
    reg [3:0] p, x; p = 4'd0; x = a;
    for (int i = 0; i < 4; i++) begin
      if (b[i]) p = p ^ x;
      x = x[3] ? ({x[2:0], 1'b0} ^ 4'b0011) : {x[2:0], 1'b0};
    end
    return p;
  endfunction
  function automatic [3:0] gf16_pow(input int e);   // alpha^e, alpha = x
    reg [3:0] p; int k; p = 4'd1; k = e % 15; if (k < 0) k += 15;
    for (int i = 0; i < k; i++) p = gf16_mul(p, 4'd2);
    return p;
  endfunction
  function automatic int gf16_log(input [3:0] a);   // -1 for 0
    reg [3:0] p; p = 4'd1;
    if (a == 4'd0) return -1;
    for (int i = 0; i < 15; i++) begin if (p == a) return i; p = gf16_mul(p, 4'd2); end
    return -1;
  endfunction
  function automatic [9:0] gf1024_mul(input [9:0] a, input [9:0] b);
    reg [9:0] p, x; p = 10'd0; x = a;
    for (int i = 0; i < 10; i++) begin
      if (b[i]) p = p ^ x;
      x = x[9] ? ({x[8:0], 1'b0} ^ 10'b0000001001) : {x[8:0], 1'b0};
    end
    return p;
  endfunction
  function automatic [9:0] gf1024_pow(input int e);
    reg [9:0] p; int k; p = 10'd1; k = e % 1023; if (k < 0) k += 1023;
    for (int i = 0; i < k; i++) p = gf1024_mul(p, 10'd2);
    return p;
  endfunction
endpackage

// hs_rng: the bench's random numbers. Same seed, same numbers, on every engine.
module hs_rng;
  int unsigned s = 32'h2545f491;
  function automatic void seed(input int unsigned v); s = (v == 0) ? 32'd1 : v; endfunction
  function automatic int unsigned next(); s = hs_pkg::xs32(s); return s; endfunction
  function automatic int below(input int n); s = hs_pkg::xs32(s); return (n <= 0) ? 0 : int'(s % n); endfunction
  function automatic bit bit1(); s = hs_pkg::xs32(s); return s[16]; endfunction
  function automatic bit chance_ppm(input int ppm); s = hs_pkg::xs32(s); return (s % 1000000) < ppm; endfunction
  function automatic [7:0] byte8(); s = hs_pkg::xs32(s); return s[23:16]; endfunction
endmodule
Read included file: hs_code.sv
`timescale 1ns/1ns
// hs_code.sv: line codes, CRCs and scramblers (read-only).

// ---------------------------------------------------------------------------------------------
// hs_8b10b: 8b/10b encoder and decoder with running disparity and the 12 K codes.
// code[9:0] = {a,b,c,d,e,i,f,g,h,j}; bit a (code[9]) goes on the wire first. rd: 0 = RD-, 1 = RD+.
//   encode(d, k, rd_in) -> {rd_out, code[9:0]}            (11 bits)
//   decode(code, rd_in) -> {rd_out, disp_err, code_err, k, d[7:0]}   (12 bits)
//   enc(d, k) / dec(code): the same, with the running disparity kept in rd_tx / rd_rx
// A K request for a byte that is not one of the 12 K codes is encoded as data and counted in bad_k.
// Decoder: code_err = the code is in neither column of the table; disp_err = it is a valid code
// only in the other column. rd_out follows the sub-block rule on the received bits.
// Faults: no_rd (always the RD- column), a7_never (never the alternate D.x.A7),
//         keep_rd_on_unbalanced (the 4b sub-block is chosen with the disparity from before the 6b one),
//         no_balanced_flip (D.07 and D.x.3 keep their RD- form at RD+), dec_no_disp_check.
module hs_8b10b;
  bit no_rd = 0, a7_never = 0, keep_rd_on_unbalanced = 0, no_balanced_flip = 0, dec_no_disp_check = 0;
  bit trace = 0;
  bit rd_tx = 0, rd_rx = 0;
  int bad_k = 0, symbols = 0, code_errs = 0, disp_errs = 0;
  bit last_k = 0, last_code_err = 0, last_disp_err = 0;
  bit valid_m [0:1023]; bit valid_p [0:1023]; bit [8:0] kd [0:1023]; bit built = 0;
  function automatic [5:0] t6(input [4:0] x);   // 5b/6b, RD- column, abcdei
    case (x)
      5'd0: return 6'b100111;  5'd1: return 6'b011101;  5'd2: return 6'b101101;  5'd3: return 6'b110001;
      5'd4: return 6'b110101;  5'd5: return 6'b101001;  5'd6: return 6'b011001;  5'd7: return 6'b111000;
      5'd8: return 6'b111001;  5'd9: return 6'b100101;  5'd10: return 6'b010101; 5'd11: return 6'b110100;
      5'd12: return 6'b001101; 5'd13: return 6'b101100; 5'd14: return 6'b011100; 5'd15: return 6'b010111;
      5'd16: return 6'b011011; 5'd17: return 6'b100011; 5'd18: return 6'b010011; 5'd19: return 6'b110010;
      5'd20: return 6'b001011; 5'd21: return 6'b101010; 5'd22: return 6'b011010; 5'd23: return 6'b111010;
      5'd24: return 6'b110011; 5'd25: return 6'b100110; 5'd26: return 6'b010110; 5'd27: return 6'b110110;
      5'd28: return 6'b001110; 5'd29: return 6'b101110; 5'd30: return 6'b011110; default: return 6'b101011;
    endcase
  endfunction
  function automatic [3:0] t4d(input [2:0] y);  // 3b/4b data, RD- column, fghj (y = 7 is P7)
    case (y)
      3'd0: return 4'b1011; 3'd1: return 4'b1001; 3'd2: return 4'b0101; 3'd3: return 4'b1100;
      3'd4: return 4'b1101; 3'd5: return 4'b1010; 3'd6: return 4'b0110; default: return 4'b1110;
    endcase
  endfunction
  function automatic [3:0] t4k(input [2:0] y);  // 3b/4b for K codes, RD- form before the flip
    case (y)
      3'd0: return 4'b1011; 3'd1: return 4'b0110; 3'd2: return 4'b1010; 3'd3: return 4'b1100;
      3'd4: return 4'b1101; 3'd5: return 4'b0101; 3'd6: return 4'b1001; default: return 4'b0111;
    endcase
  endfunction
  function automatic bit k_ok(input [7:0] d);
    return d[4:0] == 5'd28 || d == 8'hf7 || d == 8'hfb || d == 8'hfd || d == 8'hfe;
  endfunction
  function automatic int n6(input [5:0] v); return v[0] + v[1] + v[2] + v[3] + v[4] + v[5]; endfunction
  function automatic int n4(input [3:0] v); return v[0] + v[1] + v[2] + v[3]; endfunction
  // running disparity at the end of a sub-block (more ones: +; more zeros: -; 000111 and 0011: +; 111000 and 1100: -)
  function automatic bit rd6(input [5:0] v, input bit rd);
    return (n6(v) > 3) ? 1'b1 : (n6(v) < 3) ? 1'b0 : (v == 6'b000111) ? 1'b1 : (v == 6'b111000) ? 1'b0 : rd;
  endfunction
  function automatic bit rd4(input [3:0] v, input bit rd);
    return (n4(v) > 2) ? 1'b1 : (n4(v) < 2) ? 1'b0 : (v == 4'b0011) ? 1'b1 : (v == 4'b1100) ? 1'b0 : rd;
  endfunction
  // faults: [0] no_rd [1] a7_never [2] keep_rd_on_unbalanced [3] no_balanced_flip
  function automatic [10:0] core(input [7:0] d, input bit k, input bit rd_in, input [3:0] f);
    reg [5:0] six; reg [3:0] four; reg rd0, r6, rd1, a7, kk; reg [4:0] x; reg [2:0] y; int o6, o4;
    x = d[4:0]; y = d[7:5]; kk = k && k_ok(d); rd0 = f[0] ? 1'b0 : rd_in;
    six = (kk && x == 5'd28) ? 6'b001111 : t6(x); o6 = n6(six);
    if (rd0 && (o6 != 3 || (six == 6'b111000 && !f[3]))) begin six = ~six; o6 = 6 - o6; end
    r6 = (o6 > 3) ? 1'b1 : (o6 < 3) ? 1'b0 : (six == 6'b000111) ? 1'b1 : (six == 6'b111000) ? 1'b0 : rd0;
    rd1 = f[2] ? rd0 : r6;
    if (kk) begin
      four = t4k(y); if (rd1) four = ~four;
    end else begin
      a7 = (y == 3'd7) && !f[1] && ((!rd1 && (x == 5'd17 || x == 5'd18 || x == 5'd20)) || (rd1 && (x == 5'd11 || x == 5'd13 || x == 5'd14)));
      four = a7 ? 4'b0111 : t4d(y);
      if (rd1 && (n4(four) != 2 || (four == 4'b1100 && !f[3]))) four = ~four;
    end
    o4 = n4(four);
    return {(o4 > 2) ? 1'b1 : (o4 < 2) ? 1'b0 : (four == 4'b0011) ? 1'b1 : (four == 4'b1100) ? 1'b0 : r6, six, four};
  endfunction
  function automatic [10:0] encode(input [7:0] d, input bit k, input bit rd_in);
    if (k && !k_ok(d)) bad_k++;
    return core(d, k, rd_in, {no_balanced_flip, keep_rd_on_unbalanced, a7_never, no_rd});
  endfunction
  function automatic void build();
    reg [10:0] c;
    for (int i = 0; i < 1024; i++) begin valid_m[i] = 0; valid_p[i] = 0; kd[i] = 9'd0; end
    for (int i = 0; i < 268; i++) begin
      reg [7:0] d; reg k;
      if (i < 256) begin d = i[7:0]; k = 0; end
      else begin k = 1; d = (i < 264) ? {3'(i - 256), 5'd28} : (i == 264) ? 8'hf7 : (i == 265) ? 8'hfb : (i == 266) ? 8'hfd : 8'hfe; end
      c = core(d, k, 1'b0, 4'd0); valid_m[c[9:0]] = 1; kd[c[9:0]] = {k, d};
      c = core(d, k, 1'b1, 4'd0); valid_p[c[9:0]] = 1; kd[c[9:0]] = {k, d};
    end
    built = 1;
  endfunction
  function automatic [11:0] decode(input [9:0] code, input bit rd_in);
    reg here, there, ce, de, r;
    if (!built) build();
    here = rd_in ? valid_p[code] : valid_m[code];
    there = rd_in ? valid_m[code] : valid_p[code];
    ce = !(here || there); de = !ce && !here && !dec_no_disp_check;
    r = rd4(code[3:0], rd6(code[9:4], rd_in));
    return {r, de, ce, ce ? 9'd0 : kd[code]};
  endfunction
  function automatic [9:0] enc(input [7:0] d, input bit k);
    reg [10:0] r; r = encode(d, k, rd_tx); rd_tx = r[10];
    if (trace) $display("s%0d TX %s%0d.%0d code=%b rd=%s", symbols, (k && k_ok(d)) ? "K" : "D", d[4:0], d[7:5], r[9:0], r[10] ? "+" : "-");
    symbols++;
    return r[9:0];
  endfunction
  function automatic [8:0] dec(input [9:0] code);
    reg [11:0] r; r = decode(code, rd_rx); rd_rx = r[11];
    last_disp_err = r[10]; last_code_err = r[9]; last_k = r[8];
    if (r[9]) code_errs++; if (r[10]) disp_errs++;
    if (trace && (r[9] || r[10])) $display("RX %b %s", code, r[9] ? "code error" : "disparity error");
    return r[8:0];
  endfunction
  function automatic bit comma7(input [6:0] w); return w == 7'b0011111 || w == 7'b1100000; endfunction
endmodule

// ---------------------------------------------------------------------------------------------
// hs_crc: a CRC of 1 to 32 bits with any polynomial, seed, bit order and final inversion.
// The register shifts left; the polynomial omits the x^width term. The field is sent register bit
// width-1 first (USB 2.0, USB 3.2 and Ethernet all send it this way).
//   use_crc32(), use_crc16_usb2(), use_crc5_usb2(), use_crc16_usb3(): parameter sets
//   reset(), crc_bit(b), crc_byte(v), crc_bits(v, n), field(), field_bit(i), check_value(), crc_residue_ok()
// Faults: msb_first (bytes enter MSB first), no_invert (the field is not complemented), seed_zero.
module hs_crc;
  int width = 32;
  bit [31:0] poly = 32'h04c11db7, seed = 32'hffffffff, residue = 32'hc704dd7b;
  bit lsb_first = 1, invert = 1;
  bit msb_first = 0, no_invert = 0, seed_zero = 0;
  bit [31:0] r = 32'hffffffff;
  function automatic [31:0] mask(); return (width >= 32) ? 32'hffffffff : ((32'd1 << width) - 32'd1); endfunction
  function automatic void reset(); r = seed_zero ? 32'd0 : (seed & mask()); endfunction
  function automatic void set(input int w, input [31:0] p, input [31:0] s, input [31:0] res, input bit lsb, input bit inv);
    width = w; poly = p; seed = s; residue = res; lsb_first = lsb; invert = inv; reset();
  endfunction
  function automatic void use_crc32();      set(32, 32'h04c11db7, 32'hffffffff, 32'hc704dd7b, 1, 1); endfunction
  function automatic void use_crc16_usb2(); set(16, 32'h8005, 32'hffff, 32'h800d, 1, 1); endfunction
  function automatic void use_crc5_usb2();  set(5, 32'h05, 32'h1f, 32'h0c, 1, 1); endfunction
  function automatic void use_crc16_usb3(); set(16, 32'h100b, 32'hffff, 32'hf6aa, 1, 1); endfunction
  function automatic void crc_bit(input bit b);
    reg fb; fb = b ^ r[width - 1];
    r = ((r << 1) ^ (fb ? poly : 32'd0)) & mask();
  endfunction
  function automatic void crc_byte(input [7:0] v);
    for (int i = 0; i < 8; i++) crc_bit(v[(lsb_first ^ msb_first) ? i : 7 - i]);
  endfunction
  function automatic void crc_bits(input [31:0] v, input int n);   // n bits of v, bit 0 first
    for (int i = 0; i < n; i++) crc_bit(v[i]);
  endfunction
  function automatic [31:0] field(); return (invert && !no_invert) ? (~r & mask()) : r; endfunction
  function automatic bit field_bit(input int i); reg [31:0] f; f = field(); return f[width - 1 - i]; endfunction
  // the usual published "check" number: the field with its first-sent bit read as bit 0
  function automatic [31:0] check_value();
    reg [31:0] f, o; f = field(); o = 32'd0;
    for (int i = 0; i < width; i++) o[i] = f[width - 1 - i];
    return lsb_first ? o : f;
  endfunction
  function automatic bit crc_residue_ok(); return r == (residue & mask()); endfunction
endmodule

// ---------------------------------------------------------------------------------------------
// hs_scr: additive (frame-synchronous) and multiplicative (self-synchronising) scramblers.
// poly holds the coefficients: bit k is x^k; the +1 term is always used (taps() sets bit 0); len is the degree.
// Additive: a Galois LFSR. The key bit is s[len-1]; each step shifts left and adds the low terms of
//   the polynomial when the key bit is 1. The data never enters the register.
// Multiplicative: the line bit is the data bit XOR the register bits at the polynomial's terms;
//   the scrambler shifts its own output in, the descrambler shifts the received line bit in.
//   scr_add(len, poly, seed), scr_mul(len, poly), use_usb3_gen1(), use_x58(), use_x23_usb3() (see its note)
//   scr_bit(b), dscr_bit(b), scr_byte(v), dscr_byte(v) (bit 0 first), scr_reset(), scr_hold() (a skip
//   symbol: no step), scr_unscrambled() (a symbol sent in the clear: 8 steps, data untouched)
// Faults: advance_on_skip (scr_hold steps the LFSR 8 times), no_reset (scr_reset does nothing),
//         wrong_tap (the lowest term x^k becomes x^(k+1)), seed_zero (scr_reset loads 0).
module hs_scr;
  bit mult = 0; int len = 16;
  bit [63:0] poly = 64'h10039, seed = 64'hffff;
  bit advance_on_skip = 0, no_reset = 0, wrong_tap = 0, seed_zero = 0;
  bit [63:0] s = 64'hffff;
  int steps = 0;
  function automatic [63:0] lmask(); return (len >= 64) ? 64'hffffffffffffffff : ((64'd1 << len) - 64'd1); endfunction
  function automatic [63:0] taps();
    reg [63:0] p; int k; p = poly | 64'd1; k = 1;
    if (wrong_tap) begin
      while (k < len && !p[k]) k++;
      if (k < len) begin p[k] = 1'b0; p[k + 1] = 1'b1; end
    end
    return p;
  endfunction
  function automatic void load(); s = (seed_zero ? 64'd0 : seed) & lmask(); endfunction
  function automatic void scr_add(input int n, input [63:0] p, input [63:0] sd); mult = 0; len = n; poly = p; seed = sd; s = sd & lmask(); endfunction
  function automatic void scr_mul(input int n, input [63:0] p); mult = 1; len = n; poly = p; seed = 64'd0; s = 64'd0; endfunction
  function automatic void use_usb3_gen1(); scr_add(16, 64'h10039, 64'hffff); endfunction   // x^16 + x^5 + x^4 + x^3 + 1, seed FFFFh
  function automatic void use_x58(); scr_mul(58, (64'd1 << 58) | (64'd1 << 39) | 64'd1); endfunction   // x^58 + x^39 + 1
  // USB 3.2 Gen 2 (§6.3.2.3) polynomial x^23 + x^21 + x^16 + x^8 + x^5 + x^2 + 1 with seed 1DBFBCh. Only the
  // polynomial and seed are the specification's: the register form here is this library's Galois form, and
  // the reset and bypass rules of §6.3.2.3 are not modelled, so do not quote its key stream as USB's.
  function automatic void use_x23_usb3(); scr_add(23, (64'd1 << 23) | (64'd1 << 21) | (64'd1 << 16) | (64'd1 << 8) | (64'd1 << 5) | (64'd1 << 2) | 64'd1, 64'h1dbfbc); endfunction
  function automatic void scr_reset(); if (!no_reset) load(); endfunction
  function automatic bit key(); return s[len - 1]; endfunction
  function automatic void step();
    reg kb; kb = s[len - 1];
    s = ((s << 1) & lmask()) ^ (kb ? (taps() & lmask()) : 64'd0);
    steps++;
  endfunction
  // the XOR of the register bits at the polynomial's terms x^1..x^len (bit k-1 of the register for x^k)
  function automatic bit fbx(input [63:0] st); return ^(st & (taps() >> 1) & lmask()); endfunction
  function automatic bit scr_bit(input bit b);
    reg o;
    if (!mult) begin o = b ^ s[len - 1]; step(); end
    else begin o = b ^ fbx(s); s = ((s << 1) | {63'd0, o}) & lmask(); steps++; end
    return o;
  endfunction
  function automatic bit dscr_bit(input bit b);
    reg o;
    if (!mult) begin o = b ^ s[len - 1]; step(); end
    else begin o = b ^ fbx(s); s = ((s << 1) | {63'd0, b}) & lmask(); steps++; end
    return o;
  endfunction
  function automatic [7:0] scr_byte(input [7:0] v);
    reg [7:0] o; for (int i = 0; i < 8; i++) o[i] = scr_bit(v[i]); return o;
  endfunction
  function automatic [7:0] dscr_byte(input [7:0] v);
    reg [7:0] o; for (int i = 0; i < 8; i++) o[i] = dscr_bit(v[i]); return o;
  endfunction
  // a skip symbol passes unscrambled and the LFSR holds
  function automatic void scr_hold(); if (advance_on_skip && !mult) for (int i = 0; i < 8; i++) step(); endfunction
  // another symbol that is sent unscrambled (a K code other than COM and SKP): the LFSR still steps 8 times
  function automatic void scr_unscrambled(); if (!mult) for (int i = 0; i < 8; i++) step(); endfunction
endmodule
Read included file: hs_blk.sv
`timescale 1ns/1ns
// hs_blk.sv: block codes (read-only). A block is a sync header and a payload. Headers are written as
// they go on the wire, most significant bit first ("01" sends 0, then 1). The payload is sent bit 0
// first and is scrambled with a self-synchronising x^58 + x^39 + 1 scrambler (the header never is).
// Every layout uses that 64b/66b-style scrambler. USB 3.2 Gen 2 (§6.3.2.3) and USB4 (§4.3.1.5) use an
// additive x^23 LFSR kept in step by ordered sets instead; that is not modelled here (hs_scr has the
// polynomial as use_x23_usb3() for a scrambler on its own). Set scramble = 0 / descramble = 0 for none.
//   use_66()        64b/66b: data 01, control 10 (USB4 v2.0 Figure 4-22/4-23)
//   use_132_usb4()  128b/132b: data 0101, ordered set 1010 (USB4 v2.0 Figure 4-22/4-23)
//   use_132_usb3()  128b/132b: data 0011b, control 1100b, bit 0 first (USB 3.2 §6.3.2.2, Figure 6-11),
//                   so on the wire data is 1100 and control is 0011
//   use_130()       128b/130b with the 64b/66b header values: a bench layout (PCIe's own values are unverified)
// hs_blk (transmitter): blk_tx(payload, ctrl) -> the block in the low n bits, bit n-1 sent first.
// hs_blk_lock (receiver): push(b) one line bit at a time; returns 1 when a block with a valid header is
//   complete and locked (then hdr, ctrl and payload hold it). A locked block with an invalid header is not
//   delivered: push returns 0, hdr_ok is 0 and hdr_errs counts it. USB 3.2's single-bit header correction
//   (§6.3.2.2) is not modelled. Lock after lock_good valid headers in a row; an invalid header
//   before lock slips the boundary by one bit; once locked, bad_limit invalid headers within a window of
//   bad_window blocks lose lock. The default counts (64, 64, 16) are those of IEEE 802.3 Figure 49-14 (read 2026-11-14).
// hs_gearbox: a bit FIFO between a block width and a word width: gb_push(v, n), gb_pop(n), gb_level().
// Faults: header_00 (hs_blk sends 00 on every block), lock_on_first and never_unlock (hs_blk_lock).
module hs_blk;
  int hdr_bits = 2, pay_bits = 64; bit [3:0] hdr_data = 4'b0001, hdr_ctrl = 4'b0010;
  bit scramble = 1, header_00 = 0, trace = 0; int blocks = 0;
  hs_scr scr(); bit ready = 0;
  // set up on first use (an initial block here could run after the bench's first call)
  function automatic void prep(); if (!ready) begin scr.use_x58(); scr.s = 64'h3ff_ffff_ffff_ffff; ready = 1; end endfunction
  function automatic void set(input int h, input int p, input [3:0] d, input [3:0] c); hdr_bits = h; pay_bits = p; hdr_data = d; hdr_ctrl = c; endfunction
  function automatic void use_66(); set(2, 64, 4'b0001, 4'b0010); endfunction
  function automatic void use_130(); set(2, 128, 4'b0001, 4'b0010); endfunction
  function automatic void use_132_usb4(); set(4, 128, 4'b0101, 4'b1010); endfunction
  function automatic void use_132_usb3(); set(4, 128, 4'b1100, 4'b0011); endfunction
  function automatic int n(); return hdr_bits + pay_bits; endfunction
  function automatic [131:0] blk_tx(input [127:0] payload, input bit ctrl);
    reg [131:0] v; reg [3:0] h; int m; prep(); m = n(); v = 132'd0;
    h = header_00 ? 4'd0 : ctrl ? hdr_ctrl : hdr_data;
    for (int i = 0; i < hdr_bits; i++) v[m - 1 - i] = h[hdr_bits - 1 - i];
    for (int i = 0; i < pay_bits; i++) v[m - 1 - hdr_bits - i] = scramble ? scr.scr_bit(payload[i]) : payload[i];
    if (trace) $display("b%0d TX %s", blocks, ctrl ? "ctrl" : "data");
    blocks++;
    return v;
  endfunction
endmodule

module hs_blk_lock;
  int hdr_bits = 2, pay_bits = 64; bit [3:0] hdr_data = 4'b0001, hdr_ctrl = 4'b0010;
  int lock_good = 64, bad_window = 64, bad_limit = 16;
  bit descramble = 1, lock_on_first = 0, never_unlock = 0, trace = 0;
  bit locked = 0; int nbits = 0, pos = 0, slips = 0, good_run = 0, hdr_errs = 0, blocks = 0, lock_at = -1, unlocks = 0, win_n = 0, win_bad = 0;
  bit [131:0] sh = 132'd0; bit [3:0] hdr; bit ctrl; bit hdr_ok = 0; bit [127:0] payload;
  hs_scr dscr(); bit ready = 0;
  function automatic void set(input int h, input int p, input [3:0] d, input [3:0] c); hdr_bits = h; pay_bits = p; hdr_data = d; hdr_ctrl = c; endfunction
  function automatic void use_66(); set(2, 64, 4'b0001, 4'b0010); endfunction
  function automatic void use_130(); set(2, 128, 4'b0001, 4'b0010); endfunction
  function automatic void use_132_usb4(); set(4, 128, 4'b0101, 4'b1010); endfunction
  function automatic void use_132_usb3(); set(4, 128, 4'b1100, 4'b0011); endfunction
  function automatic void restart(); locked = 0; nbits = 0; pos = 0; slips = 0; good_run = 0; hdr_errs = 0; blocks = 0; lock_at = -1; unlocks = 0; win_n = 0; win_bad = 0; endfunction
  function automatic bit push(input bit b);
    int m; reg ok; m = hdr_bits + pay_bits;
    if (!ready) begin dscr.use_x58(); ready = 1; end
    sh = {sh[130:0], b}; nbits++; pos++;
    if (pos < m) return 0;
    pos = 0; hdr = 4'd0;
    for (int i = 0; i < hdr_bits; i++) hdr[hdr_bits - 1 - i] = sh[m - 1 - i];
    ok = (hdr == hdr_data) || (hdr == hdr_ctrl); ctrl = (hdr == hdr_ctrl); hdr_ok = ok;
    blocks++;
    if (!ok) hdr_errs++;
    if (!locked) begin
      if (ok) begin
        good_run++;
        if (good_run >= (lock_on_first ? 1 : lock_good)) begin
          locked = 1; win_n = 0; win_bad = 0; if (lock_at < 0) lock_at = nbits;
          if (trace) $display("b%0d LOCK bit=%0d slips=%0d", blocks, nbits, slips);
        end
      end else begin good_run = 0; pos = -1; slips++; end
      return 0;
    end
    win_n++; if (!ok) win_bad++;
    if (win_bad >= bad_limit && !never_unlock) begin
      locked = 0; good_run = 0; unlocks++; pos = -1; slips++;
      if (trace) $display("b%0d UNLOCK bit=%0d", blocks, nbits);
      return 0;
    end
    if (win_n >= bad_window) begin win_n = 0; win_bad = 0; end
    // the descrambler still takes the payload bits of a bad block, so it stays in step
    for (int i = 0; i < pay_bits; i++) payload[i] = descramble ? dscr.dscr_bit(sh[m - 1 - hdr_bits - i]) : sh[m - 1 - hdr_bits - i];
    return ok;
  endfunction
endmodule

module hs_gearbox;
  bit q [0:1023]; int head = 0, count = 0, overflows = 0;
  function automatic void gb_push(input [131:0] v, input int n);   // bit n-1 first
    for (int i = n - 1; i >= 0; i--) begin
      if (count == 1024) overflows++; else begin q[(head + count) % 1024] = v[i]; count++; end
    end
  endfunction
  function automatic int gb_level(); return count; endfunction
  function automatic [131:0] gb_pop(input int n);                    // the oldest bit lands in bit n-1
    reg [131:0] w; w = 132'd0;
    for (int i = n - 1; i >= 0 && count > 0; i--) begin w[i] = q[head]; head = (head + 1) % 1024; count--; end
    return w;
  endfunction
endmodule
ConceptA link must carry 10 Gb/s of data with 64b/66b coding. What is its line rate?
ConceptWhy does a block-lock machine wait for many valid headers in a row before it locks?
ConceptA locked 64b/66b receiver sees one invalid header, then valid ones again. What should it do?
ConceptWhat does the USB 3.2 Gen 2 receiver gain from a four-bit block header, compared with a two-bit one?

Common Mistakes

Wrong Call 64b/66b DC balanced in the way 8b/10b is.

Only the scrambler makes the payload balanced on average: unscrambled zero data gave a run of 65 bits.

Fix Rely on the scrambler for balance and edges, and size the clock recovery for rare long runs.

Wrong Scramble the sync header along with the payload.

The receiver finds the boundary from the header before it can descramble anything.

Fix Send the header in the clear, and keep the scrambler from stepping on it.

Wrong Declare block lock on the first valid header.

Payload bits match 01 or 10 half the time, so a wrong boundary often looks valid once.

Fix Count many valid headers in a row before lock, and slip on every invalid one.

Before
if (hdr_ok) locked = 1;
After
if (!hdr_ok) begin good = 0; slip(); end
else if (++good == LOCK_COUNT) locked = 1;

Wrong Treat 128b/130b and 128b/132b as the same code.

They have different header sizes and values, so their overheads and lock rules differ.

Fix Read the header width and the legal values from the standard you implement.

Remember

  • A block code sends a big payload plus a short sync header: 64b/66b and 128b/132b cost 3.125%, 128b/130b 1.5625%.
  • The header values 00 and 11 are never sent, so each header has one guaranteed transition.
  • Block lock slips one bit on every invalid header and locks only after many valid headers in a row.
  • Once locked, a burst of bad headers drops the lock. One bad header does not.
  • The payload gets its balance and edges from a scrambler. The header is never scrambled.

Practice

Report a problem with this page