XLGMII to 800GMII: The 64-Bit Interfaces

Put the same frame on eight lanes instead of four, and keep every Start on lane 0 without losing bandwidth.

You Will Be Able To

  • Name the MAC-PHY interface for each rate from 25 to 800 Gb/s, with its width and clock.
  • Lay a frame on eight lanes and find where the SFD and Terminate land.
  • Apply the deficit idle count with its bound of 7.
  • Compare the two Start alignment methods by their average gap.
Lesson
P11.18
Time
25 min
Runs on
Your browser
Check
5 questions

From 40 Gb/s up, every Ethernet interface between MAC and PHY has the same shape. It has 64 data bits and 8 control flags; only the clock changes.

RateInterfaceData bitsClockEdges
10 Gb/sXGMII (46)32156.25 MHzboth edges
25 Gb/s25GMII (106)32390.625 MHzboth edges
40 Gb/sXLGMII (81)64625 MHzrising edge
50 Gb/s50GMII (132)64781.25 MHzrising edge
100 Gb/sCGMII (81)641,562.5 MHzrising edge
200 Gb/s200GMII (117)643,125 MHzrising edge
400 Gb/s400GMII (117)646,250 MHzrising edge
800 Gb/s800GMII (802.3df 170)6412,500 MHzrising edge

Clocks are 1/64 of the rate (bench arithmetic). From 25 Gb/s up none of these interfaces has an electrical definition (106.1, 81.1).

One Shape from 40 to 800 Gb/s

The 25GMII is an XGMII with a faster clock (106.1). The 50GMII, 200GMII, 400GMII and 800GMII all copy the CGMII of Clause 81 (132.1, 117.1, 170.1). Only the clock changes.

One MAC, Many RatesThe MAC and RS stay the same shape; only the width and clock of the interface change.
  • Datapath
  • Channel / handshake
  • Data
One MAC, many ratesThe MAC and RS stay the same shape; only the width and clock of the interface change. datapath MAC octets channel 4 lanes: XGMII,25GMII both edges channel 8 lanes: XLGMIIto 800GMII rising edge datapath PCS 64b/66b blocks
  1. datapath MACoctets
    • 4 lanes: XGMII, 25GMII
    • 8 lanes: XLGMII to 800GMII
  2. channel 4 lanes: XGMII, 25GMIIboth edges
    • PCS
  3. channel 8 lanes: XLGMII to 800GMIIrising edge
    • PCS
  4. datapath PCS64b/66b blocks
  1. Four lanes

    Up to 25 Gb/s the interface is four lanes, sampled on both edges.

  2. Eight lanes

    From 40 Gb/s it is eight lanes, sampled on the rising edge only.

  3. Into the PCS

    An 8-lane column is exactly one 64b/66b block, so Start must sit on lane 0.

Read the diagram as text
  • MAC: octets
  • 4 lanes: XGMII, 25GMII: both edges
  • 8 lanes: XLGMII to 800GMII: rising edge
  • PCS: 64b/66b blocks
  • MAC → 4 lanes: XGMII, 25GMII
  • MAC → 8 lanes: XLGMII to 800GMII
  • 4 lanes: XGMII, 25GMII → PCS
  • 8 lanes: XLGMII to 800GMII → PCS

IEEE Std 802.3-2022 81.1, 106.1, 117.1, 132.1; IEEE Std 802.3df-2024 170.1.

The 64-bit interfaces sample on the rising edge only (81.3.1.1). The 32-bit ones use both edges.

From 25 Gb/s up none of these interfaces has an electrical definition (106.1, 81.1). Only the XGMII has one (46.5). The clocks in the table are logical rates. 802.3df drops EEE at 800 Gb/s (170.3).

The 25GMII: An XGMII at 390.625 MHz

The 25GMII has the same logical function as the XGMII (106.1). It has four lanes, 32 data bits and 4 control flags, with data on both clock edges. Only the clock is faster: 390.625 MHz.

Every XGMII rule carries over unchanged. Start sits on lane 0, Terminate on any lane, and the deficit idle count runs from 0 to 3 (46.3.1.4).

The bench sent one 64-octet frame and its gap through the same RS: 84 characters in 21 columns. They last 26.880 ns on a 25GMII and 67.200 ns on an XGMII.

Like the wider interfaces, the 25GMII has no electrical definition (106.1). P11.17.1 tests the RS with a 25GMII stream: the rules are the XGMII's.

Same Frame, Eight Lanes

ColumnLane 0Lane 1Lane 2Lane 3
1S555555
2555555d5
1924TII
20IIII

Bench run of the example: a 65-octet frame on four lanes (its start, then its end).

ColumnL0L1L2L3L4L5L6L7
0IIIIIIII
1S555555555555d5
92a2b2c2d2e29989d
1024TIIIIII

The same frame on eight lanes. Start on lane 0, the six preamble octets on lanes 1 to 6, the SFD on lane 7.

On eight lanes the whole preamble fits in one column: Start, six 55h octets and the SFD (81.2.2). The frame begins on lane 0 of the next column.

The Terminate of this 65-octet frame landed on lane 1: character 8 + 65 = 73, and 73 mod 8 = 1.

Keeping Start on Lane 0

After a frame and a 12-character gap, the next Start can land on any of eight lanes. The RS must move it to lane 0, just as on the XGMII.

The rule is the same, with a larger bound: the deficit idle count may run from 0 to 7 (81.3.1.4). A gap may then be up to seven characters short of 12.

StartWhat the 64-bit RS did
0lane 0, count 0
1would land on lane 4; delete 4 idles, count 4
2would land on lane 5; insert 3 idles, count 1
3would land on lane 6; delete 6 idles, count 7
4would land on lane 7; insert 1 idles, count 6
5lane 0, count 6
6would land on lane 1; delete 1 idles, count 7
7would land on lane 2; insert 6 idles, count 1

Bench run with a private RS (P11.18.1 grades a streaming form of it). 64, 65, 66, 67, 68, 69, 70, 64 octets, each followed by a 12-character gap.

When the count could absorb the deletion, the RS deleted idles. Otherwise it inserted enough to reach the next lane 0, and the count fell.

Method (both allowed by 81.3.1.4)Average gap over 1,000 framesShortest gap
Deficit idle count, 0 to 711.993 characters5
Always insert15.481 characters12

Bench run: 1,000 frames of 64 to 1518 octets, each with a 12-character gap from the MAC.

The deficit idle count kept the average at 11.993 characters. Always inserting cost 15.481: about 29% more gap.

What the Receiver Must Accept

Lane alignment and clock compensation can shrink the gap on its way. At the receiving XLGMII or CGMII it may be as short as one octet (81.2.1). On the XGMII the floor is five (46.2.1).

A receiver for these rates must therefore cope with packets almost back to back.

Try It

The bench lays one 65-octet frame on four lanes, then on eight (rs.lanes = 8). eth_rs aligns Start by adding idle only.

On eight lanes the frame starts in column 1 after four idles were padded to a whole column.

Change packet(47, 11) to packet(48, 11) in the 8-lane part, and predict the new Terminate lane before you run it.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 4 lanes, column 1: S 55 55 55 4 lanes, column 2: 55 55 55 d5 4 lanes, column 19: 24 T I I 4 lanes, column 20: I I I I 8 lanes, column 0: I I I I I I I I 8 lanes, column 1: S 55 55 55 55 55 55 d5 8 lanes, column 9: 2a 2b 2c 2d 2e 29 98 9d 8 lanes, column 10: 24 T I I I I I I 8 lanes: Terminate on lane 1
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: eth_mac.sv
`timescale 1ns/1ns
// eth_frame: one MAC frame as octets, its packet as wire bits, and a bit-serial receiver.
//   pay[i]                         client data, set by the bench before build()
//   build(da, sa, lt, len)         destination, source, Length/Type, len data octets, then the FCS -> oct[0..n-1].
//                                  It adds no pad: give it 46 data octets or more. (Padding is P11.2.1's task; the
//                                  pages show the padding MAC's frames as measured tables.)
//   wire_bits(), wire_bit(i)       the packet in transmit order: preamble, SFD, frame
//   rx_start(), rx_bit(b), rx_end()  hunt for the SFD, collect octets, then parse()
//   parse()                        da, sa, lt, is_length, is_type, data_len, fcs_rx, fcs_calc, fcs_ok
// Faults: fcs_msb_first, fcs_no_invert, fcs_covers_preamble, sfd_wrong, msb_first_octets.
module eth_frame;
  localparam int MIN_FRAME = 64, MAX_BASIC = 1518, MAX_QTAG = 1522, MAX_ENVELOPE = 2000;
  bit fcs_msb_first = 0, fcs_no_invert = 0, fcs_covers_preamble = 0, sfd_wrong = 0, msb_first_octets = 0;
  hs_crc crc();
  bit [7:0] pay [0:2047];
  bit [7:0] oct [0:2047];
  int n = 0;
  bit [47:0] da = 0, sa = 0; bit [15:0] lt = 0;
  bit is_length = 0, is_type = 0, fcs_ok = 0;
  int data_len = 0;
  bit [31:0] fcs_rx = 0, fcs_calc = 0;
  // receiver state
  bit in_frame = 0; bit [7:0] hist = 0, cur = 0; int nb = 0, excess = 0, sfd_at = -1, bits_seen = 0;

  // The FCS of oct[0..len-1] as four octets: the first octet sent is in [7:0].
  function automatic [31:0] fcs_of(input int len);
    crc.use_crc32(); crc.msb_first = fcs_msb_first; crc.no_invert = fcs_no_invert; crc.reset();
    if (fcs_covers_preamble) begin
      for (int i = 0; i < 7; i++) crc.crc_byte(8'h55);
      crc.crc_byte(8'hd5);
    end
    for (int i = 0; i < len; i++) crc.crc_byte(oct[i]);
    return crc.check_value();
  endfunction

  function automatic void build(input [47:0] d, input [47:0] s, input [15:0] t, input int len);
    int k; reg [31:0] f;
    k = 0;
    for (int i = 0; i < 6; i++) begin oct[k] = d[47 - 8*i -: 8]; k++; end
    for (int i = 0; i < 6; i++) begin oct[k] = s[47 - 8*i -: 8]; k++; end
    oct[k] = t[15:8]; k++;
    oct[k] = t[7:0]; k++;
    for (int i = 0; i < len; i++) begin oct[k] = pay[i]; k++; end
    f = fcs_of(k);
    for (int i = 0; i < 4; i++) begin oct[k] = f[8*i +: 8]; k++; end
    n = k; excess = 0;
    parse();
  endfunction

  function automatic void parse();
    da = 0; sa = 0; lt = 0; is_length = 0; is_type = 0; fcs_ok = 0; data_len = 0; fcs_rx = 0; fcs_calc = 0;
    if (n >= 14) begin
      da = {oct[0], oct[1], oct[2], oct[3], oct[4], oct[5]};
      sa = {oct[6], oct[7], oct[8], oct[9], oct[10], oct[11]};
      lt = {oct[12], oct[13]};
      is_length = (lt <= 16'd1500);
      is_type = (lt >= 16'd1536);
    end
    if (n >= 18) begin
      fcs_rx = {oct[n-1], oct[n-2], oct[n-3], oct[n-4]};
      fcs_calc = fcs_of(n - 4);
      fcs_ok = (fcs_rx == fcs_calc);
      data_len = is_length ? int'(lt) : n - 18;
    end
  endfunction

  function automatic int wire_bits(); return 8 * (8 + n); endfunction
  function automatic bit wire_bit(input int i);
    int o, b; reg [7:0] v;
    o = i / 8; b = i % 8;
    if (o < 7) v = 8'h55;
    else if (o == 7) v = sfd_wrong ? 8'h55 : 8'hd5;
    else v = oct[o - 8];
    return msb_first_octets ? v[7 - b] : v[b];
  endfunction

  function automatic void rx_start();
    in_frame = 0; hist = 0; cur = 0; nb = 0; n = 0; excess = 0; sfd_at = -1; bits_seen = 0;
  endfunction
  function automatic void rx_bit(input bit b);
    bits_seen++;
    if (!in_frame) begin
      hist = {hist[6:0], b};                      // the oldest bit is hist[7]
      if (hist == 8'b10101011) begin in_frame = 1; sfd_at = bits_seen; end
    end else begin
      if (msb_first_octets) cur[7 - nb] = b; else cur[nb] = b;
      nb++;
      if (nb == 8) begin oct[n] = cur; n++; nb = 0; end
    end
  endfunction
  function automatic void rx_end(); excess = nb; parse(); endfunction
endmodule
// eth_axis: a MAC client bus in the style of AXI4-Stream (IHI 0051B 2.2 handshake, 2.5 TKEEP, 2.6 TLAST, 2.9 TUSER),
// 64 bits wide. Byte 0 of a beat is tdata[7:0] and is first in the frame (AXI4-Stream 2.4.1). Bench conventions,
// not AXI4-Stream's or 802.3's: tkeep is all ones except on the last beat, where it covers bytes 0 to k-1; one tuser
// bit (transfer-based, 2.9.1), read on the last beat only, marks a bad frame (receive) or asks the MAC to abort it
// (transmit).
//   load(n)                  beats for octets oct[0..n-1]: bd[], bk[], bl[], nbeats; bv[] = valid per cycle, ncyc
//   tx_cycles()              the transmit MAC: one beat per cycle once a frame has started. Returns 0 sent,
//                            1 underrun (tvalid fell inside the frame), 2 aborted by tuser. sent_octets, fail_cycle.
// Fault: tvalid_gap (load() leaves cycle 3 of the frame empty: the client was late with a beat).
module eth_axis;
  bit tvalid_gap = 0;
  bit [7:0] oct [0:2047];
  bit [63:0] bd [0:300]; bit [7:0] bk [0:300]; bit bl [0:300]; bit bu [0:300];
  int nbeats = 0;
  bit bv [0:301]; int bix [0:301]; int ncyc = 0;
  int sent_octets = 0, fail_cycle = -1;
  function automatic void load(input int n);
    int b, k;
    nbeats = (n + 7) / 8;
    for (b = 0; b < nbeats; b++) begin
      bd[b] = 64'd0; bk[b] = 8'd0; bl[b] = (b == nbeats - 1); bu[b] = 0;
      for (k = 0; k < 8; k++) if (8*b + k < n) begin bd[b][8*k +: 8] = oct[8*b + k]; bk[b][k] = 1'b1; end
    end
    ncyc = 0;
    for (b = 0; b < nbeats; b++) begin
      if (tvalid_gap && b == 3) begin bv[ncyc] = 0; bix[ncyc] = -1; ncyc++; end
      bv[ncyc] = 1; bix[ncyc] = b; ncyc++;
    end
  endfunction
  function automatic int tx_cycles();
    bit started; started = 0; sent_octets = 0; fail_cycle = -1;
    for (int c = 0; c < ncyc; c++) begin
      if (!bv[c]) begin
        if (started) begin fail_cycle = c; return 1; end
      end else begin
        started = 1;
        if (bl[bix[c]] && bu[bix[c]]) begin fail_cycle = c; return 2; end
        for (int k = 0; k < 8; k++) if (bk[bix[c]][k]) sent_octets++;
      end
    end
    return 0;
  endfunction
endmodule

// eth_pause: the PAUSE function of MAC Control (Annex 31B) at the station that receives PAUSE frames, in octet
// times (one octet time is 8 bit times; a pause quantum is 512 bit times = 64 octet times, 31B.2).
//   reset(), rx_pause(t, q)  a valid PAUSE frame with pause_time q ended at time t: the timer restarts (31B.3.3)
//   may_start(t)             1 when a new data frame may start at time t. A frame already started is never cut
//                            (31B.3.7). react is the bench's reaction time: frames may still start until t + react.
//   paused_until             the time the timer runs out
//   pause_octets(q, o[])     not here: build a PAUSE frame with eth_frame (DA 01-80-C2-00-00-01, 8808h, 0001h, q)
// Faults: pause_ignored, pause_adds_not_overrides (a new frame adds to the timer, and zero does not cancel),
//         slow_reaction (react = 70 quanta, over the 60 allowed at 10 Gb/s), timer_in_bits (q counts bit times).
module eth_pause;
  localparam int QUANTUM = 64;                     // octet times
  bit pause_ignored = 0, pause_adds_not_overrides = 0, slow_reaction = 0, timer_in_bits = 0;
  int react = 0, t_rx = -1, paused_until = 0;
  function automatic void reset(); t_rx = -1; paused_until = 0; endfunction
  function automatic void rx_pause(input int t, input int q);
    int len;
    if (pause_ignored) return;
    len = timer_in_bits ? (q + 7) / 8 : q * QUANTUM;
    if (pause_adds_not_overrides && paused_until > t) paused_until = paused_until + len;
    else paused_until = t + len;
    t_rx = t;
  endfunction
  function automatic bit may_start(input int t);
    int r;
    r = slow_reaction ? 70 * QUANTUM : react;
    if (t_rx < 0) return 1;
    if (t < t_rx + r) return 1;
    return t >= paused_until;
  endfunction
endmodule

// eth_pfc: priority-based flow control (Annex 31D). The frame is 802.3's; holding a priority's queue is the MAC
// Control client's job (IEEE 802.1Q, not held), so the eight timers here are a bench client, in octet times.
//   build(f_oct[], pev, times[])   not here: the bench writes opcode 01-01, the vector and eight times into eth_frame
//   reset(), rx(t, pev, tm[0..7])  for each e[n] = 1 (bit n of the vector's low octet), priority n stops until
//                                  t + 64 * time[n] (hold_end[n]); e[n] = 0 leaves priority n alone; time[n] = 0 releases it
//   blocked(n, t)                  1 while priority n is held
// Fault: pfc_wrong_priority (reads e[n] from bit 7 - n).
module eth_pfc;
  bit pfc_wrong_priority = 0;
  int hold_end [0:7];
  function automatic void reset(); for (int i = 0; i < 8; i++) hold_end[i] = 0; endfunction
  function automatic void rx(input int t, input [15:0] pev, input [127:0] tm);   // tm[16*n +: 16] = time[n]
    bit e;
    for (int n = 0; n < 8; n++) begin
      e = pfc_wrong_priority ? pev[7 - n] : pev[n];
      if (e) hold_end[n] = t + 64 * int'(tm[16*n +: 16]);
    end
  endfunction
  function automatic bit blocked(input int n, input int t); return t < hold_end[n]; endfunction
endmodule

// eth_csma_tx: one half-duplex transmitter (4.2.3.2), one call of tick() per octet time.
//   reset(), load(frame_octets)   a frame of frame_octets (DA to FCS) waits to be sent; it is padded to min_frame
//   tick(carrier)                 carrier: another station's signal is on the wire here now. Returns 1 when the
//                                 station puts an octet (packet or jam) on the wire.
//   plan[i]                       the wait, in slots, after collision i + 1. The bench sets it (default 0). This
//                                 readable station draws nothing and has no attempt limit.
//   state  0 idle, 1 defer, 2 send, 3 jam, 4 backoff, 5 sent (as far as the sender knows)
//   pkt, sent, quiet, left, attempts, late, last_start, last_r
// min_frame is 64 octets; a bench may lower it to show why it is 64. Fault: no_backoff (every wait is 0 slots).
// Deference is a bench simplification: the 12-octet quiet count restarts on any carrier, own or received, and the
// station never starts while carrier is present. 4.2.8 restarts only the first part of the gap, and then transmits
// whatever the carrier.
module eth_csma_tx;
  localparam int SLOT = 64, JAM = 4, GAP = 12, PRE = 8;   // octet times
  bit no_backoff = 0;
  int min_frame = 64;
  int plan [0:63];
  int state = 0, pkt = 0, sent = 0, quiet = GAP, left = 0, attempts = 0, late = 0, last_start = -1, last_r = 0, now = 0;
  function automatic void reset();
    state = 0; pkt = 0; sent = 0; quiet = GAP; left = 0; attempts = 0; late = 0; last_start = -1; last_r = 0; now = 0;
  endfunction
  function automatic void load(input int frame_octets);
    pkt = PRE + frame_octets;
    if (frame_octets < min_frame) pkt = PRE + min_frame;
    state = 1; attempts = 0; sent = 0; late = 0;
  endfunction
  function automatic bit tick(input bit carrier);
    bit on;
    on = 0;
    if (state == 1 && !carrier && quiet >= GAP) begin state = 2; sent = 0; last_start = now; end
    if (state == 2 && carrier) begin
      attempts++; if (sent > SLOT) late++;
      state = 3; left = JAM;
    end
    case (state)
      2: begin sent++; on = 1; if (sent == pkt) state = 5; end
      3: begin
           on = 1; left--;
           if (left == 0) begin
             last_r = no_backoff ? 0 : plan[(attempts - 1) % 64];
             left = SLOT * last_r;
             state = (left == 0) ? 1 : 4;
           end
         end
      4: begin left--; if (left == 0) state = 1; end
      default: ;
    endcase
    quiet = (carrier || on) ? 0 : ((quiet < GAP) ? quiet + 1 : GAP);
    now++;
    return on;
  endfunction
endmodule

// eth_csma: two eth_csma_tx stations, A and B, at the two ends of a wire with a one-way delay of d octet times.
//   run(fa, ready_a, fb, ready_b, tmax)   each station loads its frame (fa, fb octets) at its ready time
//   on_a[t], on_b[t]          what each station put on the wire at time t
//   ev_t[], ev_who[] (0 A, 1 B), ev_kind[], ev_v[], n_ev   the event log:
//     kind 0 starts attempt v; 1 sees a collision after v octets; 2 jam over, backs off v slots;
//     3 last octet sent (v = octets); 4 gives up after v attempts
//   delivered(i)              1 when station i's last attempt reached the far end with no overlap there
//   a.plan[], b.plan[], a.no_backoff, b.no_backoff, a.min_frame, b.min_frame: the stations' settings
module eth_csma;
  eth_csma_tx a(); eth_csma_tx b();
  int d = 10, n_ev = 0, tend = 0;
  bit on_a [0:16383]; bit on_b [0:16383];
  int ev_t [0:255]; int ev_who [0:255]; int ev_kind [0:255]; int ev_v [0:255];
  function automatic void log_ev(input int t, input int who, input int kind, input int v);
    if (n_ev < 256) begin ev_t[n_ev] = t; ev_who[n_ev] = who; ev_kind[n_ev] = kind; ev_v[n_ev] = v; n_ev++; end
  endfunction
  function automatic void note(input int t, input int who, input int s0, input int s1, input int sent0, input int att, input int r, input int sent1);
    if (s0 != 2 && s1 == 2) log_ev(t, who, 0, att + 1);
    if (s0 == 2 && (s1 == 3 || s1 == 4)) log_ev(t, who, 1, sent0);
    if (s0 == 3 && (s1 == 4 || s1 == 1)) log_ev(t, who, 2, r);
    if (s0 != 5 && s1 == 5) log_ev(t, who, 3, sent1);
    if (s0 != 6 && s1 == 6) log_ev(t, who, 4, att);
  endfunction
  function automatic void run(input int fa, input int ready_a, input int fb, input int ready_b, input int tmax);
    bit ca, cb; int sa, sb, xa, xb;
    a.reset(); b.reset(); n_ev = 0; tend = tmax;
    for (int t = 0; t < tmax; t++) begin
      if (t == ready_a) a.load(fa);
      if (t == ready_b) b.load(fb);
      ca = (t >= d) ? on_b[t - d] : 1'b0;
      cb = (t >= d) ? on_a[t - d] : 1'b0;
      sa = a.state; sb = b.state; xa = a.sent; xb = b.sent;
      on_a[t] = a.tick(ca);
      on_b[t] = b.tick(cb);
      note(t, 0, sa, a.state, xa, a.attempts, a.last_r, a.sent);
      note(t, 1, sb, b.state, xb, b.attempts, b.last_r, b.sent);
    end
  endfunction
  function automatic bit delivered(input int i);
    int s, p, u;
    if (i == 0 && a.state != 5) return 0;
    if (i == 1 && b.state != 5) return 0;
    s = (i == 0) ? a.last_start : b.last_start;
    p = (i == 0) ? a.pkt : b.pkt;
    for (int t = s; t < s + p; t++) begin
      u = t + d;
      if (u < tend && ((i == 0) ? on_b[u] : on_a[u])) return 0;
    end
    return 1;
  endfunction
endmodule

// eth_gap: how long the gap before the next Start is on a 4-lane XGMII, where Start must sit on lane 0
// (46.3.1.4). A gap counts every character from the Terminate to the last idle before Start. A packet is
// 8 + n characters (Start, six preamble octets, SFD, an n-octet frame) and starts on lane 0.
//   reset()                     DIC = 0 (the count is reset only at initialisation)
//   method                      0 deficit idle count, 1 always insert (both allowed by 46.3.1.4)
//   next_gap(n, mac_gap)        the gap after an n-octet frame when the MAC asks for mac_gap (12 or more)
//   deleted, inserted, dic      what the last call did, and the count after it
// Faults: ipg_min_only (always deletes, ignoring the bound), dic_max4 (bounds the count at 4, not 3),
//         start_any_lane (no alignment: the gap is always mac_gap).
module eth_gap;
  bit ipg_min_only = 0, dic_max4 = 0, start_any_lane = 0;
  int method = 0, dic = 0, deleted = 0, inserted = 0;
  function automatic void reset(); dic = 0; deleted = 0; inserted = 0; endfunction
  function automatic int next_gap(input int n, input int mac_gap);
    int m, top;
    deleted = 0; inserted = 0;
    m = (n + mac_gap) % 4;                       // lanes past lane 0 the next Start would land on
    if (start_any_lane || m == 0) return mac_gap;
    top = dic_max4 ? 4 : 3;
    if (method == 0 && (ipg_min_only || dic + m <= top)) begin deleted = m; dic = dic + m; end
    else begin inserted = 4 - m; if (method == 0) dic = dic - (4 - m); end
    return mac_gap - deleted + inserted;
  endfunction
endmodule
Read included file: eth_xgmii.sv
`timescale 1ns/1ns
// eth_rs: the packet as XGMII characters (9 bits: control flag and octet), four lanes per column, and the
// 66-bit block each pair of columns becomes.
//   clear(), put(c, d), idle(k)
//   lanes            4 for the XGMII and 25GMII (default), 8 for the 64-bit interfaces (XLGMII to 800GMII, P11.18)
//   start_packet()   adds idle until lane 0, then Start (FBh), six preamble octets and the SFD
//   end_packet()     Terminate (FDh)
//   columns(), lane_of(i)   columns of lanes characters; the lane of character i
//   block_info(g)    {valid, sync[1:0], type[7:0]} for characters 8g to 8g+7 (pad the tail with idle first)
// Fault: start_any_lane (Start is not aligned to lane 0).
module eth_rs;
  bit start_any_lane = 0;
  int lanes = 4;
  bit [8:0] ch [0:4095];
  int nch = 0;
  function automatic void clear(); nch = 0; endfunction
  function automatic void put(input bit c, input [7:0] d); ch[nch] = {c, d}; nch++; endfunction
  function automatic void idle(input int k); for (int i = 0; i < k; i++) put(1'b1, 8'h07); endfunction
  function automatic void start_packet();
    if (!start_any_lane) while (nch % lanes != 0) put(1'b1, 8'h07);
    put(1'b1, 8'hfb);
    for (int i = 0; i < 6; i++) put(1'b0, 8'h55);
    put(1'b0, 8'hd5);
  endfunction
  function automatic void end_packet(); put(1'b1, 8'hfd); endfunction
  function automatic void fill_to(input int multiple); while (nch % multiple != 0) put(1'b1, 8'h07); endfunction
  function automatic int columns(); return (nch + lanes - 1) / lanes; endfunction
  function automatic int lane_of(input int i); return i % lanes; endfunction
  function automatic bit is_c(input int i); return ch[i][8]; endfunction
  function automatic bit is_code(input int i, input [7:0] v); return ch[i] == {1'b1, v}; endfunction
  function automatic bit is_plain(input int i);            // a control character with a 7-bit C code
    return is_code(i, 8'h07) || is_code(i, 8'h06) || is_code(i, 8'hfe);
  endfunction
  function automatic bit all_data(input int a, input int b);
    for (int i = a; i <= b; i++) if (is_c(i)) return 0;
    return 1;
  endfunction
  function automatic bit all_plain(input int a, input int b);
    for (int i = a; i <= b; i++) if (!is_plain(i)) return 0;
    return 1;
  endfunction
  function automatic [10:0] block_info(input int g);
    int b; reg [7:0] t; bit ok;
    b = 8 * g; ok = 0; t = 8'h00;
    if (all_data(b, b + 7)) return {1'b1, 2'b01, 8'h00};
    if (all_plain(b, b + 7)) begin ok = 1; t = 8'h1e; end
    else if (is_code(b, 8'hfb) && all_data(b + 1, b + 7)) begin ok = 1; t = 8'h78; end
    else if (all_plain(b, b + 3) && is_code(b + 4, 8'hfb) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h33; end
    else if (is_code(b, 8'h9c) && all_data(b + 1, b + 3) && is_code(b + 4, 8'hfb) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h66; end
    else if (is_code(b, 8'h9c) && all_data(b + 1, b + 3) && is_code(b + 4, 8'h9c) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h55; end
    else if (is_code(b, 8'h9c) && all_data(b + 1, b + 3) && all_plain(b + 4, b + 7)) begin ok = 1; t = 8'h4b; end
    else if (all_plain(b, b + 3) && is_code(b + 4, 8'h9c) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h2d; end
    else begin
      for (int k = 0; k < 8; k++)
        if (!ok && is_code(b + k, 8'hfd) && (k == 0 || all_data(b, b + k - 1)) && (k == 7 || all_plain(b + k + 1, b + 7))) begin
          ok = 1;
          case (k)
            0: t = 8'h87; 1: t = 8'h99; 2: t = 8'haa; 3: t = 8'hb4;
            4: t = 8'hcc; 5: t = 8'hd2; 6: t = 8'he1; default: t = 8'hff;
          endcase
        end
    end
    return {ok, 2'b10, t};
  endfunction
endmodule

// eth_xgmii_mon: reads XGMII columns as a receiving RS would (46.2, 46.3.3) and reports what it sees.
// A column is {c[3:0], d[31:0]}: lane k is control flag c[k] and octet d[8k+7:8k]; lane 0 comes first.
//   reset(), col(c, d)      one column; col() returns 1 if the column holds anything the monitor flags
//   frames                  Starts seen on lane 0; octets: data octets after the SFD up to the Terminate
//   start_lane, term_lane   where the last Start and Terminate were (-1: not in this column)
//   bad_start               Starts on lanes 1 to 3 (46.3.3.3: the RS must not pass them to the MAC)
//   errors                  Error characters (FEh) seen inside a frame; err_frames: frames that carried one
//   seq_cols                columns holding a Sequence ordered set (9Ch in lane 0, P11.19)
//   last_len                data octets of the last frame, preamble and SFD excluded
module eth_xgmii_mon;
  int frames = 0, octets = 0, bad_start = 0, errors = 0, err_frames = 0, seq_cols = 0, last_len = 0;
  int start_lane = -1, term_lane = -1, in_pkt = 0, pre_left = 0, len = 0;
  bit had_err = 0;
  function automatic void reset();
    frames = 0; octets = 0; bad_start = 0; errors = 0; err_frames = 0; seq_cols = 0; last_len = 0;
    in_pkt = 0; pre_left = 0; len = 0; had_err = 0; start_lane = -1; term_lane = -1;
  endfunction
  function automatic bit col(input [3:0] c, input [31:0] d);
    bit flag; reg [7:0] o;
    flag = 0; start_lane = -1; term_lane = -1;
    if (c[0] && d[7:0] == 8'h9c) seq_cols++;
    for (int k = 0; k < 4; k++) begin
      o = d[8*k +: 8];
      if (c[k] && o == 8'hfb) begin
        start_lane = k;
        if (k != 0) begin bad_start++; flag = 1; end
        else begin in_pkt = 1; pre_left = 7; len = 0; had_err = 0; frames++; end
      end
      else if (in_pkt && c[k] && o == 8'hfd) begin
        term_lane = k; in_pkt = 0; last_len = len;
        if (had_err) err_frames++;
      end
      else if (in_pkt && c[k] && o == 8'hfe) begin errors++; had_err = 1; flag = 1; len++; end
      else if (in_pkt && !c[k]) begin
        if (pre_left > 0) pre_left--;
        else begin len++; octets++; end
      end
    end
    return flag;
  endfunction
endmodule

Check Yourself

Predict the outputA 75-octet frame starts on lane 0 of an 8-lane interface. On which lane is its Terminate?
`timescale 1ns/1ns
module tb;
  eth_frame f(); eth_rs rs(); eth_xgmii_mon mon();
  reg [3:0] c; reg [31:0] d; bit fl;
  // one frame of len data octets onto the lanes, then a Terminate and gap idles
  task automatic packet(input int len, input int gap);
    for (int i = 0; i < len; i++) f.pay[i] = i;
    f.build(48'h0a0000000002, 48'h0a0000000001, 16'h0800, len);
    rs.start_packet();
    for (int i = 0; i < f.n; i++) rs.put(1'b0, f.oct[i]);
    rs.end_packet(); rs.idle(gap);
  endtask
  function automatic string name(input int i);
    if (!rs.is_c(i)) return $sformatf("%h", rs.ch[i][7:0]);
    case (rs.ch[i][7:0]) 8'h07: return "I"; 8'hfb: return "S"; 8'hfd: return "T"; 8'hfe: return "E"; 8'h9c: return "Q"; default: return "?"; endcase
  endfunction
  // column k as {c, d}, lane 0 in the low bits
  task automatic column(input int k);
    for (int l = 0; l < 4; l++) begin c[l] = rs.ch[4*k + l][8]; d[8*l +: 8] = rs.ch[4*k + l][7:0]; end
  endtask
  task automatic show(input int k);
    column(k);
    $display("column %0d: %0s %0s %0s %0s  control flags, lane 0 first: %b", k, name(4*k), name(4*k + 1), name(4*k + 2), name(4*k + 3), {c[0], c[1], c[2], c[3]});
  endtask
  task automatic run_mon();
    mon.reset();
    for (int k = 0; k < rs.columns(); k++) begin column(k); fl = mon.col(c, d); end
  endtask
  task automatic grid(input int lanes, input int from, input int upto);
    for (int k = from; k < upto; k++) begin
      $write("%0d lanes, column %0d:", lanes, k);
      for (int l = 0; l < lanes; l++) $write(" %0s", name(lanes * k + l));
      $write("\n");
    end
  endtask
  initial begin
    rs.lanes = 8; rs.clear(); packet(57, 11);
    for (int i = 0; i < rs.nch; i++) if (rs.is_code(i, 8'hfd)) $display("%0d", rs.lane_of(i));
    $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: eth_mac.sv
`timescale 1ns/1ns
// eth_frame: one MAC frame as octets, its packet as wire bits, and a bit-serial receiver.
//   pay[i]                         client data, set by the bench before build()
//   build(da, sa, lt, len)         destination, source, Length/Type, len data octets, then the FCS -> oct[0..n-1].
//                                  It adds no pad: give it 46 data octets or more. (Padding is P11.2.1's task; the
//                                  pages show the padding MAC's frames as measured tables.)
//   wire_bits(), wire_bit(i)       the packet in transmit order: preamble, SFD, frame
//   rx_start(), rx_bit(b), rx_end()  hunt for the SFD, collect octets, then parse()
//   parse()                        da, sa, lt, is_length, is_type, data_len, fcs_rx, fcs_calc, fcs_ok
// Faults: fcs_msb_first, fcs_no_invert, fcs_covers_preamble, sfd_wrong, msb_first_octets.
module eth_frame;
  localparam int MIN_FRAME = 64, MAX_BASIC = 1518, MAX_QTAG = 1522, MAX_ENVELOPE = 2000;
  bit fcs_msb_first = 0, fcs_no_invert = 0, fcs_covers_preamble = 0, sfd_wrong = 0, msb_first_octets = 0;
  hs_crc crc();
  bit [7:0] pay [0:2047];
  bit [7:0] oct [0:2047];
  int n = 0;
  bit [47:0] da = 0, sa = 0; bit [15:0] lt = 0;
  bit is_length = 0, is_type = 0, fcs_ok = 0;
  int data_len = 0;
  bit [31:0] fcs_rx = 0, fcs_calc = 0;
  // receiver state
  bit in_frame = 0; bit [7:0] hist = 0, cur = 0; int nb = 0, excess = 0, sfd_at = -1, bits_seen = 0;

  // The FCS of oct[0..len-1] as four octets: the first octet sent is in [7:0].
  function automatic [31:0] fcs_of(input int len);
    crc.use_crc32(); crc.msb_first = fcs_msb_first; crc.no_invert = fcs_no_invert; crc.reset();
    if (fcs_covers_preamble) begin
      for (int i = 0; i < 7; i++) crc.crc_byte(8'h55);
      crc.crc_byte(8'hd5);
    end
    for (int i = 0; i < len; i++) crc.crc_byte(oct[i]);
    return crc.check_value();
  endfunction

  function automatic void build(input [47:0] d, input [47:0] s, input [15:0] t, input int len);
    int k; reg [31:0] f;
    k = 0;
    for (int i = 0; i < 6; i++) begin oct[k] = d[47 - 8*i -: 8]; k++; end
    for (int i = 0; i < 6; i++) begin oct[k] = s[47 - 8*i -: 8]; k++; end
    oct[k] = t[15:8]; k++;
    oct[k] = t[7:0]; k++;
    for (int i = 0; i < len; i++) begin oct[k] = pay[i]; k++; end
    f = fcs_of(k);
    for (int i = 0; i < 4; i++) begin oct[k] = f[8*i +: 8]; k++; end
    n = k; excess = 0;
    parse();
  endfunction

  function automatic void parse();
    da = 0; sa = 0; lt = 0; is_length = 0; is_type = 0; fcs_ok = 0; data_len = 0; fcs_rx = 0; fcs_calc = 0;
    if (n >= 14) begin
      da = {oct[0], oct[1], oct[2], oct[3], oct[4], oct[5]};
      sa = {oct[6], oct[7], oct[8], oct[9], oct[10], oct[11]};
      lt = {oct[12], oct[13]};
      is_length = (lt <= 16'd1500);
      is_type = (lt >= 16'd1536);
    end
    if (n >= 18) begin
      fcs_rx = {oct[n-1], oct[n-2], oct[n-3], oct[n-4]};
      fcs_calc = fcs_of(n - 4);
      fcs_ok = (fcs_rx == fcs_calc);
      data_len = is_length ? int'(lt) : n - 18;
    end
  endfunction

  function automatic int wire_bits(); return 8 * (8 + n); endfunction
  function automatic bit wire_bit(input int i);
    int o, b; reg [7:0] v;
    o = i / 8; b = i % 8;
    if (o < 7) v = 8'h55;
    else if (o == 7) v = sfd_wrong ? 8'h55 : 8'hd5;
    else v = oct[o - 8];
    return msb_first_octets ? v[7 - b] : v[b];
  endfunction

  function automatic void rx_start();
    in_frame = 0; hist = 0; cur = 0; nb = 0; n = 0; excess = 0; sfd_at = -1; bits_seen = 0;
  endfunction
  function automatic void rx_bit(input bit b);
    bits_seen++;
    if (!in_frame) begin
      hist = {hist[6:0], b};                      // the oldest bit is hist[7]
      if (hist == 8'b10101011) begin in_frame = 1; sfd_at = bits_seen; end
    end else begin
      if (msb_first_octets) cur[7 - nb] = b; else cur[nb] = b;
      nb++;
      if (nb == 8) begin oct[n] = cur; n++; nb = 0; end
    end
  endfunction
  function automatic void rx_end(); excess = nb; parse(); endfunction
endmodule
// eth_axis: a MAC client bus in the style of AXI4-Stream (IHI 0051B 2.2 handshake, 2.5 TKEEP, 2.6 TLAST, 2.9 TUSER),
// 64 bits wide. Byte 0 of a beat is tdata[7:0] and is first in the frame (AXI4-Stream 2.4.1). Bench conventions,
// not AXI4-Stream's or 802.3's: tkeep is all ones except on the last beat, where it covers bytes 0 to k-1; one tuser
// bit (transfer-based, 2.9.1), read on the last beat only, marks a bad frame (receive) or asks the MAC to abort it
// (transmit).
//   load(n)                  beats for octets oct[0..n-1]: bd[], bk[], bl[], nbeats; bv[] = valid per cycle, ncyc
//   tx_cycles()              the transmit MAC: one beat per cycle once a frame has started. Returns 0 sent,
//                            1 underrun (tvalid fell inside the frame), 2 aborted by tuser. sent_octets, fail_cycle.
// Fault: tvalid_gap (load() leaves cycle 3 of the frame empty: the client was late with a beat).
module eth_axis;
  bit tvalid_gap = 0;
  bit [7:0] oct [0:2047];
  bit [63:0] bd [0:300]; bit [7:0] bk [0:300]; bit bl [0:300]; bit bu [0:300];
  int nbeats = 0;
  bit bv [0:301]; int bix [0:301]; int ncyc = 0;
  int sent_octets = 0, fail_cycle = -1;
  function automatic void load(input int n);
    int b, k;
    nbeats = (n + 7) / 8;
    for (b = 0; b < nbeats; b++) begin
      bd[b] = 64'd0; bk[b] = 8'd0; bl[b] = (b == nbeats - 1); bu[b] = 0;
      for (k = 0; k < 8; k++) if (8*b + k < n) begin bd[b][8*k +: 8] = oct[8*b + k]; bk[b][k] = 1'b1; end
    end
    ncyc = 0;
    for (b = 0; b < nbeats; b++) begin
      if (tvalid_gap && b == 3) begin bv[ncyc] = 0; bix[ncyc] = -1; ncyc++; end
      bv[ncyc] = 1; bix[ncyc] = b; ncyc++;
    end
  endfunction
  function automatic int tx_cycles();
    bit started; started = 0; sent_octets = 0; fail_cycle = -1;
    for (int c = 0; c < ncyc; c++) begin
      if (!bv[c]) begin
        if (started) begin fail_cycle = c; return 1; end
      end else begin
        started = 1;
        if (bl[bix[c]] && bu[bix[c]]) begin fail_cycle = c; return 2; end
        for (int k = 0; k < 8; k++) if (bk[bix[c]][k]) sent_octets++;
      end
    end
    return 0;
  endfunction
endmodule

// eth_pause: the PAUSE function of MAC Control (Annex 31B) at the station that receives PAUSE frames, in octet
// times (one octet time is 8 bit times; a pause quantum is 512 bit times = 64 octet times, 31B.2).
//   reset(), rx_pause(t, q)  a valid PAUSE frame with pause_time q ended at time t: the timer restarts (31B.3.3)
//   may_start(t)             1 when a new data frame may start at time t. A frame already started is never cut
//                            (31B.3.7). react is the bench's reaction time: frames may still start until t + react.
//   paused_until             the time the timer runs out
//   pause_octets(q, o[])     not here: build a PAUSE frame with eth_frame (DA 01-80-C2-00-00-01, 8808h, 0001h, q)
// Faults: pause_ignored, pause_adds_not_overrides (a new frame adds to the timer, and zero does not cancel),
//         slow_reaction (react = 70 quanta, over the 60 allowed at 10 Gb/s), timer_in_bits (q counts bit times).
module eth_pause;
  localparam int QUANTUM = 64;                     // octet times
  bit pause_ignored = 0, pause_adds_not_overrides = 0, slow_reaction = 0, timer_in_bits = 0;
  int react = 0, t_rx = -1, paused_until = 0;
  function automatic void reset(); t_rx = -1; paused_until = 0; endfunction
  function automatic void rx_pause(input int t, input int q);
    int len;
    if (pause_ignored) return;
    len = timer_in_bits ? (q + 7) / 8 : q * QUANTUM;
    if (pause_adds_not_overrides && paused_until > t) paused_until = paused_until + len;
    else paused_until = t + len;
    t_rx = t;
  endfunction
  function automatic bit may_start(input int t);
    int r;
    r = slow_reaction ? 70 * QUANTUM : react;
    if (t_rx < 0) return 1;
    if (t < t_rx + r) return 1;
    return t >= paused_until;
  endfunction
endmodule

// eth_pfc: priority-based flow control (Annex 31D). The frame is 802.3's; holding a priority's queue is the MAC
// Control client's job (IEEE 802.1Q, not held), so the eight timers here are a bench client, in octet times.
//   build(f_oct[], pev, times[])   not here: the bench writes opcode 01-01, the vector and eight times into eth_frame
//   reset(), rx(t, pev, tm[0..7])  for each e[n] = 1 (bit n of the vector's low octet), priority n stops until
//                                  t + 64 * time[n] (hold_end[n]); e[n] = 0 leaves priority n alone; time[n] = 0 releases it
//   blocked(n, t)                  1 while priority n is held
// Fault: pfc_wrong_priority (reads e[n] from bit 7 - n).
module eth_pfc;
  bit pfc_wrong_priority = 0;
  int hold_end [0:7];
  function automatic void reset(); for (int i = 0; i < 8; i++) hold_end[i] = 0; endfunction
  function automatic void rx(input int t, input [15:0] pev, input [127:0] tm);   // tm[16*n +: 16] = time[n]
    bit e;
    for (int n = 0; n < 8; n++) begin
      e = pfc_wrong_priority ? pev[7 - n] : pev[n];
      if (e) hold_end[n] = t + 64 * int'(tm[16*n +: 16]);
    end
  endfunction
  function automatic bit blocked(input int n, input int t); return t < hold_end[n]; endfunction
endmodule

// eth_csma_tx: one half-duplex transmitter (4.2.3.2), one call of tick() per octet time.
//   reset(), load(frame_octets)   a frame of frame_octets (DA to FCS) waits to be sent; it is padded to min_frame
//   tick(carrier)                 carrier: another station's signal is on the wire here now. Returns 1 when the
//                                 station puts an octet (packet or jam) on the wire.
//   plan[i]                       the wait, in slots, after collision i + 1. The bench sets it (default 0). This
//                                 readable station draws nothing and has no attempt limit.
//   state  0 idle, 1 defer, 2 send, 3 jam, 4 backoff, 5 sent (as far as the sender knows)
//   pkt, sent, quiet, left, attempts, late, last_start, last_r
// min_frame is 64 octets; a bench may lower it to show why it is 64. Fault: no_backoff (every wait is 0 slots).
// Deference is a bench simplification: the 12-octet quiet count restarts on any carrier, own or received, and the
// station never starts while carrier is present. 4.2.8 restarts only the first part of the gap, and then transmits
// whatever the carrier.
module eth_csma_tx;
  localparam int SLOT = 64, JAM = 4, GAP = 12, PRE = 8;   // octet times
  bit no_backoff = 0;
  int min_frame = 64;
  int plan [0:63];
  int state = 0, pkt = 0, sent = 0, quiet = GAP, left = 0, attempts = 0, late = 0, last_start = -1, last_r = 0, now = 0;
  function automatic void reset();
    state = 0; pkt = 0; sent = 0; quiet = GAP; left = 0; attempts = 0; late = 0; last_start = -1; last_r = 0; now = 0;
  endfunction
  function automatic void load(input int frame_octets);
    pkt = PRE + frame_octets;
    if (frame_octets < min_frame) pkt = PRE + min_frame;
    state = 1; attempts = 0; sent = 0; late = 0;
  endfunction
  function automatic bit tick(input bit carrier);
    bit on;
    on = 0;
    if (state == 1 && !carrier && quiet >= GAP) begin state = 2; sent = 0; last_start = now; end
    if (state == 2 && carrier) begin
      attempts++; if (sent > SLOT) late++;
      state = 3; left = JAM;
    end
    case (state)
      2: begin sent++; on = 1; if (sent == pkt) state = 5; end
      3: begin
           on = 1; left--;
           if (left == 0) begin
             last_r = no_backoff ? 0 : plan[(attempts - 1) % 64];
             left = SLOT * last_r;
             state = (left == 0) ? 1 : 4;
           end
         end
      4: begin left--; if (left == 0) state = 1; end
      default: ;
    endcase
    quiet = (carrier || on) ? 0 : ((quiet < GAP) ? quiet + 1 : GAP);
    now++;
    return on;
  endfunction
endmodule

// eth_csma: two eth_csma_tx stations, A and B, at the two ends of a wire with a one-way delay of d octet times.
//   run(fa, ready_a, fb, ready_b, tmax)   each station loads its frame (fa, fb octets) at its ready time
//   on_a[t], on_b[t]          what each station put on the wire at time t
//   ev_t[], ev_who[] (0 A, 1 B), ev_kind[], ev_v[], n_ev   the event log:
//     kind 0 starts attempt v; 1 sees a collision after v octets; 2 jam over, backs off v slots;
//     3 last octet sent (v = octets); 4 gives up after v attempts
//   delivered(i)              1 when station i's last attempt reached the far end with no overlap there
//   a.plan[], b.plan[], a.no_backoff, b.no_backoff, a.min_frame, b.min_frame: the stations' settings
module eth_csma;
  eth_csma_tx a(); eth_csma_tx b();
  int d = 10, n_ev = 0, tend = 0;
  bit on_a [0:16383]; bit on_b [0:16383];
  int ev_t [0:255]; int ev_who [0:255]; int ev_kind [0:255]; int ev_v [0:255];
  function automatic void log_ev(input int t, input int who, input int kind, input int v);
    if (n_ev < 256) begin ev_t[n_ev] = t; ev_who[n_ev] = who; ev_kind[n_ev] = kind; ev_v[n_ev] = v; n_ev++; end
  endfunction
  function automatic void note(input int t, input int who, input int s0, input int s1, input int sent0, input int att, input int r, input int sent1);
    if (s0 != 2 && s1 == 2) log_ev(t, who, 0, att + 1);
    if (s0 == 2 && (s1 == 3 || s1 == 4)) log_ev(t, who, 1, sent0);
    if (s0 == 3 && (s1 == 4 || s1 == 1)) log_ev(t, who, 2, r);
    if (s0 != 5 && s1 == 5) log_ev(t, who, 3, sent1);
    if (s0 != 6 && s1 == 6) log_ev(t, who, 4, att);
  endfunction
  function automatic void run(input int fa, input int ready_a, input int fb, input int ready_b, input int tmax);
    bit ca, cb; int sa, sb, xa, xb;
    a.reset(); b.reset(); n_ev = 0; tend = tmax;
    for (int t = 0; t < tmax; t++) begin
      if (t == ready_a) a.load(fa);
      if (t == ready_b) b.load(fb);
      ca = (t >= d) ? on_b[t - d] : 1'b0;
      cb = (t >= d) ? on_a[t - d] : 1'b0;
      sa = a.state; sb = b.state; xa = a.sent; xb = b.sent;
      on_a[t] = a.tick(ca);
      on_b[t] = b.tick(cb);
      note(t, 0, sa, a.state, xa, a.attempts, a.last_r, a.sent);
      note(t, 1, sb, b.state, xb, b.attempts, b.last_r, b.sent);
    end
  endfunction
  function automatic bit delivered(input int i);
    int s, p, u;
    if (i == 0 && a.state != 5) return 0;
    if (i == 1 && b.state != 5) return 0;
    s = (i == 0) ? a.last_start : b.last_start;
    p = (i == 0) ? a.pkt : b.pkt;
    for (int t = s; t < s + p; t++) begin
      u = t + d;
      if (u < tend && ((i == 0) ? on_b[u] : on_a[u])) return 0;
    end
    return 1;
  endfunction
endmodule

// eth_gap: how long the gap before the next Start is on a 4-lane XGMII, where Start must sit on lane 0
// (46.3.1.4). A gap counts every character from the Terminate to the last idle before Start. A packet is
// 8 + n characters (Start, six preamble octets, SFD, an n-octet frame) and starts on lane 0.
//   reset()                     DIC = 0 (the count is reset only at initialisation)
//   method                      0 deficit idle count, 1 always insert (both allowed by 46.3.1.4)
//   next_gap(n, mac_gap)        the gap after an n-octet frame when the MAC asks for mac_gap (12 or more)
//   deleted, inserted, dic      what the last call did, and the count after it
// Faults: ipg_min_only (always deletes, ignoring the bound), dic_max4 (bounds the count at 4, not 3),
//         start_any_lane (no alignment: the gap is always mac_gap).
module eth_gap;
  bit ipg_min_only = 0, dic_max4 = 0, start_any_lane = 0;
  int method = 0, dic = 0, deleted = 0, inserted = 0;
  function automatic void reset(); dic = 0; deleted = 0; inserted = 0; endfunction
  function automatic int next_gap(input int n, input int mac_gap);
    int m, top;
    deleted = 0; inserted = 0;
    m = (n + mac_gap) % 4;                       // lanes past lane 0 the next Start would land on
    if (start_any_lane || m == 0) return mac_gap;
    top = dic_max4 ? 4 : 3;
    if (method == 0 && (ipg_min_only || dic + m <= top)) begin deleted = m; dic = dic + m; end
    else begin inserted = 4 - m; if (method == 0) dic = dic - (4 - m); end
    return mac_gap - deleted + inserted;
  endfunction
endmodule
Read included file: eth_xgmii.sv
`timescale 1ns/1ns
// eth_rs: the packet as XGMII characters (9 bits: control flag and octet), four lanes per column, and the
// 66-bit block each pair of columns becomes.
//   clear(), put(c, d), idle(k)
//   lanes            4 for the XGMII and 25GMII (default), 8 for the 64-bit interfaces (XLGMII to 800GMII, P11.18)
//   start_packet()   adds idle until lane 0, then Start (FBh), six preamble octets and the SFD
//   end_packet()     Terminate (FDh)
//   columns(), lane_of(i)   columns of lanes characters; the lane of character i
//   block_info(g)    {valid, sync[1:0], type[7:0]} for characters 8g to 8g+7 (pad the tail with idle first)
// Fault: start_any_lane (Start is not aligned to lane 0).
module eth_rs;
  bit start_any_lane = 0;
  int lanes = 4;
  bit [8:0] ch [0:4095];
  int nch = 0;
  function automatic void clear(); nch = 0; endfunction
  function automatic void put(input bit c, input [7:0] d); ch[nch] = {c, d}; nch++; endfunction
  function automatic void idle(input int k); for (int i = 0; i < k; i++) put(1'b1, 8'h07); endfunction
  function automatic void start_packet();
    if (!start_any_lane) while (nch % lanes != 0) put(1'b1, 8'h07);
    put(1'b1, 8'hfb);
    for (int i = 0; i < 6; i++) put(1'b0, 8'h55);
    put(1'b0, 8'hd5);
  endfunction
  function automatic void end_packet(); put(1'b1, 8'hfd); endfunction
  function automatic void fill_to(input int multiple); while (nch % multiple != 0) put(1'b1, 8'h07); endfunction
  function automatic int columns(); return (nch + lanes - 1) / lanes; endfunction
  function automatic int lane_of(input int i); return i % lanes; endfunction
  function automatic bit is_c(input int i); return ch[i][8]; endfunction
  function automatic bit is_code(input int i, input [7:0] v); return ch[i] == {1'b1, v}; endfunction
  function automatic bit is_plain(input int i);            // a control character with a 7-bit C code
    return is_code(i, 8'h07) || is_code(i, 8'h06) || is_code(i, 8'hfe);
  endfunction
  function automatic bit all_data(input int a, input int b);
    for (int i = a; i <= b; i++) if (is_c(i)) return 0;
    return 1;
  endfunction
  function automatic bit all_plain(input int a, input int b);
    for (int i = a; i <= b; i++) if (!is_plain(i)) return 0;
    return 1;
  endfunction
  function automatic [10:0] block_info(input int g);
    int b; reg [7:0] t; bit ok;
    b = 8 * g; ok = 0; t = 8'h00;
    if (all_data(b, b + 7)) return {1'b1, 2'b01, 8'h00};
    if (all_plain(b, b + 7)) begin ok = 1; t = 8'h1e; end
    else if (is_code(b, 8'hfb) && all_data(b + 1, b + 7)) begin ok = 1; t = 8'h78; end
    else if (all_plain(b, b + 3) && is_code(b + 4, 8'hfb) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h33; end
    else if (is_code(b, 8'h9c) && all_data(b + 1, b + 3) && is_code(b + 4, 8'hfb) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h66; end
    else if (is_code(b, 8'h9c) && all_data(b + 1, b + 3) && is_code(b + 4, 8'h9c) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h55; end
    else if (is_code(b, 8'h9c) && all_data(b + 1, b + 3) && all_plain(b + 4, b + 7)) begin ok = 1; t = 8'h4b; end
    else if (all_plain(b, b + 3) && is_code(b + 4, 8'h9c) && all_data(b + 5, b + 7)) begin ok = 1; t = 8'h2d; end
    else begin
      for (int k = 0; k < 8; k++)
        if (!ok && is_code(b + k, 8'hfd) && (k == 0 || all_data(b, b + k - 1)) && (k == 7 || all_plain(b + k + 1, b + 7))) begin
          ok = 1;
          case (k)
            0: t = 8'h87; 1: t = 8'h99; 2: t = 8'haa; 3: t = 8'hb4;
            4: t = 8'hcc; 5: t = 8'hd2; 6: t = 8'he1; default: t = 8'hff;
          endcase
        end
    end
    return {ok, 2'b10, t};
  endfunction
endmodule

// eth_xgmii_mon: reads XGMII columns as a receiving RS would (46.2, 46.3.3) and reports what it sees.
// A column is {c[3:0], d[31:0]}: lane k is control flag c[k] and octet d[8k+7:8k]; lane 0 comes first.
//   reset(), col(c, d)      one column; col() returns 1 if the column holds anything the monitor flags
//   frames                  Starts seen on lane 0; octets: data octets after the SFD up to the Terminate
//   start_lane, term_lane   where the last Start and Terminate were (-1: not in this column)
//   bad_start               Starts on lanes 1 to 3 (46.3.3.3: the RS must not pass them to the MAC)
//   errors                  Error characters (FEh) seen inside a frame; err_frames: frames that carried one
//   seq_cols                columns holding a Sequence ordered set (9Ch in lane 0, P11.19)
//   last_len                data octets of the last frame, preamble and SFD excluded
module eth_xgmii_mon;
  int frames = 0, octets = 0, bad_start = 0, errors = 0, err_frames = 0, seq_cols = 0, last_len = 0;
  int start_lane = -1, term_lane = -1, in_pkt = 0, pre_left = 0, len = 0;
  bit had_err = 0;
  function automatic void reset();
    frames = 0; octets = 0; bad_start = 0; errors = 0; err_frames = 0; seq_cols = 0; last_len = 0;
    in_pkt = 0; pre_left = 0; len = 0; had_err = 0; start_lane = -1; term_lane = -1;
  endfunction
  function automatic bit col(input [3:0] c, input [31:0] d);
    bit flag; reg [7:0] o;
    flag = 0; start_lane = -1; term_lane = -1;
    if (c[0] && d[7:0] == 8'h9c) seq_cols++;
    for (int k = 0; k < 4; k++) begin
      o = d[8*k +: 8];
      if (c[k] && o == 8'hfb) begin
        start_lane = k;
        if (k != 0) begin bad_start++; flag = 1; end
        else begin in_pkt = 1; pre_left = 7; len = 0; had_err = 0; frames++; end
      end
      else if (in_pkt && c[k] && o == 8'hfd) begin
        term_lane = k; in_pkt = 0; last_len = len;
        if (had_err) err_frames++;
      end
      else if (in_pkt && c[k] && o == 8'hfe) begin errors++; had_err = 1; flag = 1; len++; end
      else if (in_pkt && !c[k]) begin
        if (pre_left > 0) pre_left--;
        else begin len++; octets++; end
      end
    end
    return flag;
  endfunction
endmodule
ConceptWhat clock does a 100 Gb/s CGMII use?
ConceptWhich interface keeps the 32-bit XGMII shape?
ConceptWhy does the deficit idle count beat always inserting idle?
ConceptHow short may the gap be at a receiving CGMII?

Common Mistakes

Wrong Bound the deficit idle count at 3 on a 64-bit interface.

That is the XGMII bound; 81.3.1.4 allows 0 to 7.

Fix Use 0 to 7 for XLGMII and the wider interfaces.

Wrong Expect the SFD on lane 3, as on the XGMII.

On eight lanes the preamble fills the first column.

Fix Look for the SFD on lane 7 of the Start column.

Wrong Sample a 64-bit interface on both clock edges.

XLGMII, CGMII and the wider ones use the rising edge only.

Fix One column per rising edge; the clock is 1/64 of the rate.

Wrong Design a receiver that assumes at least five idle octets between packets.

At these rates the gap may shrink to one octet.

Fix Accept a Start one octet after a Terminate.

Remember

  • 25GMII = XGMII shape; XLGMII, 50GMII, CGMII, 200, 400 and 800GMII = 64 bits, rising edge, clock = rate / 64.
  • Eight lanes: Start on lane 0, preamble on 1 to 6, SFD on 7.
  • The deficit idle count is bounded 0 to 7 here. With a 12-character MAC gap, a gap may be 5 to 19 characters long.
  • A receiving RS must accept a gap of one octet.

Practice

Report a problem with this page