Lane Lock, Deskew and Reorder

The lanes left together and in order. They arrive on other wires and at other times. See how the receiver finds each lane, lines them up and rebuilds one stream.

You Will Be Able To

  • Trace the alignment marker lock of one lane: two markers to lock, four bad ones to lose it.
  • Work out the skew between lanes from their markers, and the delay each lane needs.
  • Explain how the marker gives the lane number, and why wire order must not be trusted.
  • State the skew a PCS receiver must absorb, in time and in bits.
Lesson
P11.27
Time
40 min
Runs on
Your browser
Check
5 questions

P11.26 sent four lanes with a marker on each. Here is what a receiver may see.

Four Lanes, as They ArriveThe transmitter sent all four markers at the same moment, on wires 0 to 3. This is the far end.
0123wire0123carries PCS lane1302first marker at block time0237
Scroll horizontally ↔
  1. Cycle 0

    Wire 0 does not carry lane 0. Its marker says PCS lane 1.

  2. Cycle 2

    PCS lane 0 arrives on wire 2, three blocks after the earliest lane.

  3. Cycle 3

    The last marker comes 7 blocks after the first. That difference is the skew.

Bench run of the example: eth_mld_lanes with wire_of and skew set. Skew in whole blocks is a bench choice.

What Went Wrong on the Way

Each lane takes its own path: its own wire, laser or fibre colour. The paths differ a little in length.

Skew is the time between the earliest and the latest lane, measured at their markers (80.5).

A PMA may also move a PCS lane to another wire. P11.28 shows how. So wire numbers mean nothing.

Lanes arriveBlocks in the right place
Lanes on their own wires, on time80 of 80
Lanes on other wires0 of 80
Other wires, and late0 of 80

Bench run of the example: a receiver with no buffers that takes wire k as PCS lane k. Counted over block times 1 to 20: is the block on wire k the one the transmitter put on lane k?

The Receive Side, Lane by LaneThe first two steps run on each wire alone. The last three need all the lanes.
  • Control / FSM
  • Storage
  • Datapath
  • Data
  • Control
The receive side, lane by laneThe first two steps run on each wire alone. The last three need all the lanes. control Block lock per wire: find the66-bit blocks control Marker lock per wire: findmarkers, learn thelane number storage Deskew delay the early lanes datapath Reorder lane number picks theplace datapath Remove markers then one stream tothe descrambler blocksall lanes lockedmarkers level
  1. control Block lockper wire: find the 66-bit blocks
    • blocks → Marker lock
  2. control Marker lockper wire: find markers, learn the lane number
    • all lanes locked → Deskew
  3. storage Deskewdelay the early lanes
    • markers level → Reorder
  4. datapath Reorderlane number picks the place
    • Remove markers
  5. datapath Remove markersthen one stream to the descrambler
  1. Each wire alone

    Every wire hunts for its block boundary, then for its markers. No wire needs the others yet.

  2. All together

    With a marker lock on every wire, buffers line the markers up. The lane numbers put the lanes in order.

  3. One stream again

    The markers are dropped. What is left is the stream the transmitter scrambled.

Read the diagram as text
  • Block lock: per wire: find the 66-bit blocks
  • Marker lock: per wire: find markers, learn the lane number
  • Deskew: delay the early lanes
  • Reorder: lane number picks the place
  • Remove markers: then one stream to the descrambler
  • Block lock → Marker lock: blocks
  • Marker lock → Deskew: all lanes locked
  • Deskew → Reorder: markers level
  • Reorder → Remove markers

After IEEE Std 802.3-2022 82.2.12 to 82.2.15 (redrawn).

Block Lock on Every Lane

Each wire delivers bits. So each wire first needs block lock, as in P11.24 (82.2.12).

The hunt is the same: test sync headers, slip one bit on a bad one. Two counts are larger.

Rule10GBASE-R (Figure 49-14)40G and 100G, per lane (Figure 82-12)
To get lock64 valid headers in a row64 valid headers in a row
Window while in lock64 headers1,024 headers
Invalid headers that lose lock16 in one window65 in one window

IEEE Std 802.3-2022 Figures 49-14 and 82-12. Each lane of a multi-lane PCS runs its own copy.

After the lock, the lane seesBlock lockSlips
64 invalid headers in one windowkept0
65 invalid headers in one windowlost at header 1291
60 at the end of a window, 64 at the start of the nextkept0

Bench runs of a private lock model: one lane, lock set at header 64. P11.27.2 grades this lock.

The window is longer, so a short burst on one lane is less likely to cost block lock. That reason is ours: the standard gives none.

A lane that does lose block lock loses its marker lock too. Alignment is then lost for the whole link.

Block lock also removes the part of the skew that is smaller than a block. Each lane now delivers whole blocks.

The benches of this lesson start here. Their skew is a whole number of blocks, which is a bench choice.

Marker Lock: Which Lane Is This?

With block lock, a lane starts to look for alignment markers (82.2.13). Each lane does this alone.

Alignment Marker Lock, for One LaneOne pass per block. The counter runs for 16,383 blocks, from one marker position to the next.
  • Control / FSM
  • Control
Alignment marker lock, for one laneOne pass per block. The counter runs for 16,383 blocks, from one marker position to the next. control FIND_1ST is this block amarker? control COUNT_1 count 16,383 blocks control COMP_2ND the same markeragain? control 2_GOOD am_lock = true; storethe lane number control COUNT_2 count 16,383 blocks control COMP_AM good: count of bad =0; bad: count + 1 control SLIP am_lock = false; tryanother block a valid markernot a markercounter donesame lane numberdifferentcounter donefewer than 4 bad in arow4 bad in a rowslip done
  1. control FIND_1STis this block a marker?
    • a valid marker → COUNT_1
    • not a marker → SLIP
  2. control SLIPam_lock = false; try another block
    • slip done → FIND_1ST
  3. control COUNT_1count 16,383 blocks
    • counter done → COMP_2ND
  4. control COMP_2NDthe same marker again?
    • same lane number → 2_GOOD
    • different → SLIP
  5. control 2_GOODam_lock = true; store the lane number
    • COUNT_2
  6. control COUNT_2count 16,383 blocks
    • counter done → COMP_AM
  7. control COMP_AMgood: count of bad = 0; bad: count + 1
    • fewer than 4 bad in a row → COUNT_2
    • 4 bad in a row → SLIP
  1. Hunting

    Every block is compared with the marker table. Most blocks are not markers, so the hunt moves on.

  2. Two in a row

    One marker is not trusted. The same lane number must come back exactly 16,384 blocks later.

  3. A false start

    If the second block is not that marker, the first was a fluke. The hunt starts again.

  4. Holding

    In lock, only the marker position is looked at. Four bad markers in a row clear the lock.

Read the diagram as text
  • FIND_1ST: is this block a marker?
  • COUNT_1: count 16,383 blocks
  • COMP_2ND: the same marker again?
  • 2_GOOD: am_lock = true; store the lane number
  • COUNT_2: count 16,383 blocks
  • COMP_AM: good: count of bad = 0; bad: count + 1
  • SLIP: am_lock = false; try another block
  • FIND_1ST → COUNT_1: a valid marker
  • FIND_1ST → SLIP: not a marker
  • COUNT_1 → COMP_2ND: counter done
  • COMP_2ND → 2_GOOD: same lane number
  • COMP_2ND → SLIP: different
  • 2_GOOD → COUNT_2
  • COUNT_2 → COMP_AM: counter done
  • COMP_AM → COUNT_2: fewer than 4 bad in a row
  • COMP_AM → SLIP: 4 bad in a row
  • SLIP → FIND_1ST: slip done

After IEEE Std 802.3-2022 Figure 82-13 (redrawn). AM_LOCK_INIT, AM_RESET_CNT, GOOD_AM and INVALID_AM are folded into their neighbours. The EEE state is left out. Which block SLIP tries next is the design's choice (82.2.19.2.3).

A block is a valid marker if its M octets match a row of the marker table. The two BIP fields are not compared.

The first valid marker gives a lane number. The lane then counts 16,383 blocks and looks again.

The same number there sets am_lock. The lane number is stored as the lane mapping of that wire.

What the lanes carriedAligned at block timeMarker lock lost
Clean lanes169never
A stray marker pattern on one lane, before lock263never
The same, with lock_on_first4651 time
3 damaged markers in a row, in lock169never
4 damaged markers in a row, in lock1691 time, about 202 rows lost

Bench runs of a private receiver model. Bench gap: 100 blocks between markers. The first markers arrive at block times 61 to 68.

A stray pattern cost one marker period: the second look found no marker, and the hunt began again.

The twin lock_on_first trusted it. Two wires then claimed the same lane, and the link came up much later.

In lock, bad markers are counted in a row. Three are forgiven. The fourth clears am_lock, and alignment is lost.

Deskew: Wait for the Slowest Lane

All markers left at the same moment. So the markers show how late each lane is.

In the example the markers came at block times 0, 2, 3, 7. The skew was 7 blocks, or 462 bits.

The receiver cannot speed a lane up. It delays the early ones, each in a buffer of its own.

The latest lane gets no delay. A lane that came 7 blocks earlier waits 7 blocks.

After that, the markers of all lanes leave the buffers in the same block time.

Deskew runs once all lanes have marker lock (Figure 82-14). Then align_status is true and data may flow.

While the buffers are being set, blocks may be thrown away. Nothing useful is lost: the link was not up yet.

Reorder, Then Drop the Markers

The buffers now deliver four blocks per block time. The lane mapping says which is which (82.2.14).

The block from the wire that holds PCS lane 0 goes first, then lane 1, and so on. That undoes the dealing.

ReceiverLanes arriveResult
Cleanon their own wires, on timeall 334 rows right
Cleanon other wiresall 334 rows right
Cleanon other wires, and lateall 327 rows right
Cleanone lane 28 blocks lateall 306 rows right
With assumes_physical_orderon their own wiresall 334 rows right
With assumes_physical_orderon other wires334 of 334 rows wrong
With ignores_skewon other wires, and late327 of 327 rows wrong

Bench runs of a private receiver model and its two twins. A row is the four blocks the transmitter dealt in one round.

The twin assumes_physical_order works only while the lanes stay on their own wires. No standard promises that.

Last, the markers are deleted (82.2.15). The stream is then what the scrambler made, and the descrambler can run.

The marker position is deleted, whatever is in it. A damaged marker must not slip into the data.

The receiver also checks BIP3 there, as P11.26 showed. And it may add idle characters to fill the rate gap.

The Skew Budget, and What the Receiver Reports

How deep must the buffers be? The standard adds up the skew along the link at numbered skew points.

WhereMaximum skewOn a 40GBASE-R laneOn a 100GBASE-R lane
SP1: into the last transmit PMA29 ns≈ 299 bits≈ 150 bits
SP3: out of the transmitter54 ns≈ 557 bits≈ 278 bits
SP4: into the receiver134 ns≈ 1,382 bits≈ 691 bits
SP6: out of the receive PMA160 ns≈ 1,650 bits≈ 825 bits
At the PCS receiver180 ns≈ 1,856 bits≈ 928 bits

IEEE Std 802.3-2022 Table 80-8 (four of its skew points) and Table 82-7. The bit counts of the last two rows are bench arithmetic. For SP6 the table prints 1,649 and 824: it rounds down.

A 40GBASE-R receiver must absorb 180 ns. That is about 28.1 blocks on a lane.

Skew may also drift while the link is up. That skew variation is at most 4 ns: about 41 bits at 40GBASE-R.

With RS-FEC in the PHY, the 100GBASE-R PCS receiver sees only 49 ns, about 253 bits. There the FEC sublayer lines the lanes up first and absorbs the 180 ns itself (Table 80-8).

The receiver reports align_status: every lane in marker lock, each with its own lane number, and all deskewed.

A BER monitor runs as in P11.24 once the lanes are aligned. It watches the sync headers of the whole stream (82.2.19.3).

Its limit is 97 invalid headers in one period.

The period is 1.25 ms for 40GBASE-R and 500 µs for 100GBASE-R. Both hold about 781,250 sync headers.

PCS_status is true with align_status set and hi_ber clear.

With hi_ber set, or without block lock, the receive state diagram sends Local Fault to the RS instead of data (Figure 82-17).

Clause 82 has one test pattern: scrambled idle (82.2.11). The transmitter scrambles idle blocks and adds markers as usual.

Its checker uses the same lock, marker and deskew steps (82.2.18). It counts blocks that do not descramble to idle.

Try It

The bench fills four lanes with eth_mld_lanes, as in P11.26. Then it sets wire_of and skew: the way to the far end.

The first three lines read the wires with no buffers and no reorder. Only the untouched link survives that.

The task look scans each wire for its first marker. It prints the lane number the marker gives, and when it came.

The last line is the skew: the latest marker time minus the earliest.

The library has no deskew and no reorder. Building them is the practice problem.

Set every tx.skew to the same value. Predict the skew line before you run it.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: lanes on their own wires, on time: 80 of 80 blocks in the right place lanes on other wires: 0 of 80 blocks in the right place other wires, and late: 0 of 80 blocks in the right place wire 0: first marker at block time 0, PCS lane 1 wire 1: first marker at block time 2, PCS lane 3 wire 2: first marker at block time 3, PCS lane 0 wire 3: first marker at block time 7, PCS lane 2 skew: 7 blocks, 462 bits
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_pcs.sv
`timescale 1ns/1ns
// eth_pcs.sv: the 1000BASE-X PCS, transmit side and synchronization (read-only). It sits on hs_8b10b (hs_code.sv).

// eth_1000x: the transmit ordered-set layer (36.2.4). The stream is kept as {k, d} and as ten coded bits.
//   clear()            empty stream, running disparity negative
//   idle(k)            k idle ordered sets. Each tests the running disparity first (Figure 36-6):
//                      positive -> /I1/ (K28.5 D5.6), negative -> /I2/ (K28.5 D16.2)
//   cfg(word, k)       k configuration ordered sets, /C1/ and /C2/ in turn: K28.5, D21.5 or D2.2, word[7:0], word[15:8]
//   start_packet()     /S/ in place of the first preamble octet, then six 55h and D5h
//   data(d)            one data code-group
//   end_packet()       /T/ /R/, and one more /R/ when the first /R/ went out on an even position
//   err()              /V/
//   put(k, d)          any code-group
//   n                  code-groups so far; code-group i is even-numbered when i is even
//   sym[i] = {k, d};  code[i] = abcdei fghj, bit a (code[i][9]) is sent first;  rdb[i] = running disparity before i
//   is_k(i, d), comma_plus(), comma_minus()
// Fault: idle_wrong_disparity (every idle is /I2/, so a positive running disparity is never corrected).
module eth_1000x;
  bit idle_wrong_disparity = 0;
  hs_8b10b c8();
  bit [8:0] sym [0:8191]; bit [9:0] code [0:8191]; bit rdb [0:8191];
  int n = 0; bit c2 = 0;
  function automatic void clear(); n = 0; c2 = 0; c8.rd_tx = 0; endfunction
  function automatic void put(input bit k, input [7:0] d);
    sym[n] = {k, d}; rdb[n] = c8.rd_tx; code[n] = c8.enc(d, k); n++;
  endfunction
  function automatic void idle(input int k);
    bit positive;
    for (int i = 0; i < k; i++) begin
      positive = c8.rd_tx && !idle_wrong_disparity;
      put(1'b1, 8'hbc);                                // K28.5
      put(1'b0, positive ? 8'hc5 : 8'h50);             // D5.6 or D16.2
    end
  endfunction
  function automatic void cfg(input [15:0] word, input int k);
    for (int i = 0; i < k; i++) begin
      put(1'b1, 8'hbc);
      put(1'b0, c2 ? 8'h42 : 8'hb5);                   // D2.2 or D21.5
      put(1'b0, word[7:0]); put(1'b0, word[15:8]);
      c2 = !c2;
    end
  endfunction
  function automatic void data(input [7:0] d); put(1'b0, d); endfunction
  function automatic void start_packet();
    put(1'b1, 8'hfb);                                  // /S/ = K27.7
    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);                                  // /T/ = K29.7
    put(1'b1, 8'hf7);                                  // /R/ = K23.7
    if (n % 2 != 0) put(1'b1, 8'hf7);                  // that /R/ was even-numbered: one more
  endfunction
  function automatic void err(); put(1'b1, 8'hfe); endfunction     // /V/ = K30.7
  function automatic bit is_k(input int i, input [7:0] d); return sym[i] == {1'b1, d}; endfunction
  function automatic int comma_plus();
    int c; c = 0;
    for (int i = 0; i < n; i++) if (code[i][9:3] == 7'b0011111) c++;
    return c;
  endfunction
  function automatic int comma_minus();
    int c; c = 0;
    for (int i = 0; i < n; i++) if (code[i][9:3] == 7'b1100000) c++;
    return c;
  endfunction
endmodule

// eth_1000x_sync: the synchronization process of Figure 36-9, one call of rx() per ten-bit code-group.
//   reset()            LOSS_OF_SYNC
//   rx(code)           takes one code-group; returns sync_status (1 = OK)
//   state              0 LOSS_OF_SYNC, 1 COMMA_DETECT_1, 2 ACQUIRE_SYNC_1, 3 COMMA_DETECT_2, 4 ACQUIRE_SYNC_2,
//                      5 COMMA_DETECT_3, 6 SYNC_ACQUIRED_1, 7 SYNC_ACQUIRED_2, 8 SYNC_ACQUIRED_2A,
//                      9 SYNC_ACQUIRED_3, 10 SYNC_ACQUIRED_3A, 11 SYNC_ACQUIRED_4, 12 SYNC_ACQUIRED_4A
//   even               the code-group just taken was even-numbered
//   good_cgs, bad_seen (every bad code-group since reset), k, d (the last decode)
// A code-group is bad when it is invalid, or when it holds a comma and is odd-numbered (36.2.5.1.3).
// Bench choices: in LOSS_OF_SYNC the decoder takes its running disparity from the comma it finds; an invalid
// code-group leaves the decoder's running disparity as the received bits set it.
module eth_1000x_sync;
  hs_8b10b c8();
  int state = 0, good_cgs = 0, bad_seen = 0;
  bit even = 0, sync = 0, rd = 0, k = 0; bit [7:0] d = 0;
  function automatic void reset(); state = 0; good_cgs = 0; bad_seen = 0; even = 0; sync = 0; rd = 0; endfunction
  function automatic bit rx(input [9:0] code);
    reg [11:0] r; bit invalid, comma, is_d, bad;
    comma = code[9:3] == 7'b0011111 || code[9:3] == 7'b1100000;
    if (state == 0 && comma) rd = (code[9:3] == 7'b1100000);
    r = c8.decode(code, rd);                           // {rd_out, disp_err, code_err, k, d}
    rd = r[11]; k = r[8]; d = r[7:0];
    invalid = r[10] || r[9];
    is_d = !invalid && !r[8];
    bad = invalid || (comma && even);                  // even: the one before was even, so this one is odd
    if (bad && state != 0) bad_seen++;
    case (state)
      0: begin if (comma) begin state = 1; even = 1; end else even = !even; end
      1, 3, 5: begin even = 0; state = is_d ? state + 1 : 0; end
      2, 4: begin
        if (bad) state = 0;
        else if (comma) begin state = state + 1; even = 1; end
        else even = !even;
      end
      6: begin even = !even; if (bad) begin state = 7; good_cgs = 0; end end
      7, 9, 11: begin
        even = !even;
        if (bad) begin state = (state == 11) ? 0 : state + 2; good_cgs = 0; end
        else begin state = state + 1; good_cgs = 1; end
      end
      default: begin                                   // 8, 10, 12: counting good code-groups
        even = !even;
        if (bad) begin state = (state == 12) ? 0 : state + 1; good_cgs = 0; end
        else if (good_cgs == 3) begin state = (state == 8) ? 6 : state - 3; good_cgs = 0; end
        else good_cgs++;
      end
    endcase
    sync = state >= 6;
    return sync;
  endfunction
endmodule

// ---- 10GBASE-R and 25GBASE-R: 64B/66B blocks (Clause 49; Clause 107 uses the same code) ----
// Eight XGMII characters are one vector raw[71:0] (49.2.13.2.2): raw[k] is the control flag of character k,
// raw[8 + 8*k +: 8] its octet. A block is blk[65:0]; bit 0 is sent first. blk[1:0] is the sync header,
// blk[9:2] the block type field of a control block, the rest the payload (Figure 49-7).

// eth_66: the control blocks that hold no Start, no Terminate and no frame data.
//   c_code(x)       Table 49-1: the 7-bit C code of XGMII control code x; 7Fh if x is not in the table
//   enc_ctrl(raw)   {ok, blk}: block type 1Eh (eight control characters), 2Dh (four, then an ordered set),
//                   4Bh (an ordered set, then four) or 55h (two ordered sets); ok = 0 if raw is none of these.
//                   It only lays out the fields. Whether the vector may be sent is the transmit process's
//                   decision (eth_66_tx).
//   on_wire(blk)    the 66 bits as text in the order they are sent: sync, block type, payload
//   ham(a, b)       how many bits differ between two block type values
// Fault: wrong_block_type (types 2Dh and 4Bh are swapped).
module eth_66;
  bit wrong_block_type = 0;
  function automatic [6:0] c_code(input [7:0] x);
    case (x)
      8'h07: return 7'h00;                             // idle
      8'h06: return 7'h06;                             // low power idle
      8'hfe: return 7'h1e;                             // error
      8'h1c: return 7'h2d;                             // the six reserved characters
      8'h3c: return 7'h33;
      8'h7c: return 7'h4b;
      8'hbc: return 7'h55;
      8'hdc: return 7'h66;
      8'hf7: return 7'h78;
      default: return 7'h7f;
    endcase
  endfunction
  function automatic [7:0] ch(input [71:0] raw, input int k); return raw[8 + 8*k +: 8]; endfunction
  // characters k to k+3 are a sequence ordered set: 9Ch with its flag, then three data octets
  function automatic bit is_q(input [71:0] raw, input int k);
    return raw[k] && ch(raw, k) == 8'h9c && !raw[k + 1] && !raw[k + 2] && !raw[k + 3];
  endfunction
  // characters k to k+3 are control characters with a C code
  function automatic bit is_c4(input [71:0] raw, input int k);
    for (int j = k; j < k + 4; j++) if (!raw[j] || c_code(ch(raw, j)) == 7'h7f) return 0;
    return 1;
  endfunction
  function automatic [66:0] enc_ctrl(input [71:0] raw);
    reg [65:0] b; bit q0, q4, c0, c4;
    b = 66'd0;
    b[1:0] = 2'b01;                                    // sync header "10": bit 0, sent first, is 1
    q0 = is_q(raw, 0); q4 = is_q(raw, 4); c0 = is_c4(raw, 0); c4 = is_c4(raw, 4);
    if (c0 && c4) b[9:2] = 8'h1e;
    else if (c0 && q4) b[9:2] = wrong_block_type ? 8'h4b : 8'h2d;
    else if (q0 && c4) b[9:2] = wrong_block_type ? 8'h2d : 8'h4b;
    else if (q0 && q4) b[9:2] = 8'h55;
    else return 67'd0;
    if (c0) for (int k = 0; k < 4; k++) b[10 + 7*k +: 7] = c_code(ch(raw, k));
    else begin b[33:10] = raw[39:16]; b[37:34] = 4'h0; end     // D1 to D3, then the O code of the sequence set
    if (c4) for (int k = 4; k < 8; k++) b[10 + 7*k +: 7] = c_code(ch(raw, k));
    else begin b[41:38] = 4'h0; b[65:42] = raw[71:48]; end     // the O code, then D5 to D7
    return {1'b1, b};
  endfunction
  function automatic string on_wire(input [65:0] blk);
    string s;
    s = "";
    for (int i = 0; i < 66; i++) begin
      if (i == 2 || i == 10) s = {s, " "};
      s = {s, blk[i] ? "1" : "0"};
    end
    return s;
  endfunction
  function automatic int ham(input [7:0] a, input [7:0] b);
    int n; reg [7:0] x;
    x = a ^ b; n = 0;
    for (int i = 0; i < 8; i++) if (x[i]) n++;
    return n;
  endfunction
endmodule

// eth_66_tx: the transmit state diagram (Figure 49-16) without the EEE state. One call per vector.
//   step(t)   t is the T_TYPE of the vector: 0 C (control), 1 S (start), 2 T (terminate), 3 D (data), 4 E (none of these).
//             Returns 1 if the vector is encoded and sent, 0 if the error block goes out in its place.
//   state     0 TX_INIT, 1 TX_C, 2 TX_D, 3 TX_T, 4 TX_E
module eth_66_tx;
  int state = 0;
  function automatic void reset(); state = 0; endfunction
  function automatic bit step(input int t);
    case (state)
      0, 1, 3: state = (t == 0) ? 1 : (t == 1) ? 2 : 4;                    // TX_INIT, TX_C, TX_T: only C or S may follow
      2: state = (t == 3) ? 2 : (t == 2) ? 3 : 4;                          // TX_D: only D or T may follow
      default: state = (t == 2) ? 3 : (t == 3) ? 2 : (t == 0) ? 1 : 4;     // TX_E: T, D or C leave it
    endcase
    return state != 4;
  endfunction
endmodule

// eth_66_comp: clock compensation on XGMII columns (49.2.4.7, 49.2.4.10). Call once per column, in order.
// A column is {c[3:0], d[31:0]}: lane k is flag c[k] and octet d[8k+7:8k].
//   can_delete(c, d)   1 if this column may be dropped: four idles that hold none of the first four characters
//                      after a Terminate, or the second of two consecutive sequence ordered sets
//   can_insert(c, d)   1 if an idle column may be added after this one: it is idle or an ordered set
module eth_66_comp;
  bit guard = 0, prev_seq = 0;
  function automatic void reset(); guard = 0; prev_seq = 0; endfunction
  function automatic bit is_seq(input [3:0] c, input [31:0] d); return c == 4'b0001 && d[7:0] == 8'h9c; endfunction
  function automatic bit is_idle(input [3:0] c, input [31:0] d); return c == 4'b1111 && d == 32'h07070707; endfunction
  function automatic bit can_delete(input [3:0] c, input [31:0] d);
    bit ok, has_t;
    has_t = 0;
    for (int k = 0; k < 4; k++) if (c[k] && d[8*k +: 8] == 8'hfd) has_t = 1;
    ok = (is_idle(c, d) && !guard) || (is_seq(c, d) && prev_seq);
    guard = has_t;                                     // the next column holds some of the four characters after /T/
    prev_seq = is_seq(c, d) && !ok;                    // only one of the two
    return ok;
  endfunction
  function automatic bit can_insert(input [3:0] c, input [31:0] d); return is_idle(c, d) || is_seq(c, d); endfunction
endmodule

// eth_66_scr: the scrambler and the descrambler of 10GBASE-R (49.2.6, 49.2.10; Figures 49-8 and 49-10).
// The register is s[57:0]: stage S0 is s[0] and holds the newest line bit. Use one instance to scramble and
// another to descramble.
//   scr_bit(b)       one payload bit in, one line bit out: b XOR S38 XOR S57. The line bit shifts into S0.
//   dscr_bit(b)      one line bit in, one payload bit out: the same XOR. The received bit shifts into S0.
//   scramble(blk)    a whole block: bits 1:0 (the sync header) pass, bits 2 to 65 are scrambled in that order
//   descramble(blk)  the reverse
//   load(v)          sets the register (the standard asks for no start value)
// Fault: scrambles_header (the two sync header bits go through the scrambler too).
module eth_66_scr;
  bit [57:0] s = 58'd0;
  bit scrambles_header = 0;
  function automatic void load(input [57:0] v); s = v; endfunction
  function automatic bit scr_bit(input bit b);
    bit o; o = b ^ s[38] ^ s[57]; s = {s[56:0], o}; return o;
  endfunction
  function automatic bit dscr_bit(input bit b);
    bit o; o = b ^ s[38] ^ s[57]; s = {s[56:0], b}; return o;
  endfunction
  function automatic [65:0] scramble(input [65:0] blk);
    reg [65:0] o; o = blk;
    for (int i = scrambles_header ? 0 : 2; i < 66; i++) o[i] = scr_bit(blk[i]);
    return o;
  endfunction
  function automatic [65:0] descramble(input [65:0] blk);
    reg [65:0] o; o = blk;
    for (int i = 2; i < 66; i++) o[i] = dscr_bit(blk[i]);
    return o;
  endfunction
endmodule

// eth_prbs31: the PRBS31 pattern generator (49.2.8, Figure 49-9). The register is s[30:0], stage S0 is s[0].
//   gen_bit()   the next pattern bit: S27 XOR S30 shifts into S0, and the output is that bit inverted
// The register must not be all zeros: it would stay there. The checker is not in this file.
module eth_prbs31;
  bit [30:0] s = 31'h7fffffff;
  function automatic bit gen_bit();
    bit f; f = s[27] ^ s[30]; s = {s[29:0], f}; return !f;
  endfunction
endmodule

// eth_66_test: the square wave of 49.2.8 and the two data patterns of the pseudo-random test pattern.
//   sq_bit()        square wave: sq_n ones, then sq_n zeros (sq_n may be 4 to 11)
//   pattern()       the 64-bit data pattern: zeros, or the payload of a block with two local fault sets (lf_pattern)
// The pseudo-random pattern generator and its checker (49.2.8, 49.2.12) are not in this file.
module eth_66_test;
  eth_66 code();
  bit lf_pattern = 0;
  int sq_n = 8, sq_k = 0;
  function automatic void restart(); sq_k = 0; endfunction
  function automatic bit sq_bit();
    bit b; b = (sq_k < sq_n); sq_k = (sq_k + 1) % (2 * sq_n); return b;
  endfunction
  function automatic [63:0] pattern();
    reg [66:0] r;
    if (!lf_pattern) return 64'd0;
    r = code.enc_ctrl({64'h0100009c_0100009c, 8'h11}); return r[65:2];
  endfunction
endmodule

// eth_66_loop: PCS loopback (49.2.14.4), switched by MDIO bit 3.0.14.
//   rx_block(from_tx, from_pma)   what the receive path decodes: its own transmit block when loopback is 1
//   pma_word(w)                   what goes to the PMA: the word 00FFh when loopback is 1, else w
module eth_66_loop;
  bit loopback = 0;
  function automatic [65:0] rx_block(input [65:0] from_tx, input [65:0] from_pma); return loopback ? from_tx : from_pma; endfunction
  function automatic [15:0] pma_word(input [15:0] w); return loopback ? 16'h00ff : w; endfunction
endmodule
Read included file: eth_lanes.sv
`timescale 1ns/1ns
// eth_lanes.sv: the multi-lane PCS of 40GBASE-R and 100GBASE-R, and the PMA bit mux (read-only).

// eth_am: alignment markers (82.2.7, Figure 82-9, Tables 82-2 and 82-3). Bit 0 of a block is sent first.
// Bits 1:0 are the control sync header. Eight octets follow, each sent LSB first:
// M0 in bits 9:2, M1 17:10, M2 25:18, BIP3 33:26, M4 41:34, M5 49:42, M6 57:50, BIP7 65:58.
//   lanes               4: the 40GBASE-R markers. 20: the 100GBASE-R markers.
//   m(lane)             {M2, M1, M0} of that PCS lane, from the table
//   marker(lane, bip3)  the whole marker: M4 to M6 are the inverses of M0 to M2, BIP7 is the inverse of bip3
//   lane_of(blk)        the PCS lane number if blk is a marker, else -1. The two BIP fields are not compared.
module eth_am;
  int lanes = 4;
  function automatic [23:0] m(input int lane);
    reg [23:0] v;
    v = 24'd0;
    if (lanes == 4)
      case (lane)
        0: v = 24'h477690;  1: v = 24'he6c4f0;  2: v = 24'h9b65c5;  3: v = 24'h3d79a2;
        default: v = 24'd0;
      endcase
    else
      case (lane)
        0: v = 24'h2168c1;   1: v = 24'h8e719d;   2: v = 24'he84b59;   3: v = 24'h7b954d;
        4: v = 24'h0907f5;   5: v = 24'hc214dd;   6: v = 24'h264a9a;   7: v = 24'h66457b;
        8: v = 24'h7624a0;   9: v = 24'hfbc968;  10: v = 24'h996cfd;  11: v = 24'h5591b9;
        12: v = 24'hb2b95c; 13: v = 24'hbdf81a;  14: v = 24'hcac783;  15: v = 24'hcd3635;
        16: v = 24'h4c31c4; 17: v = 24'hb7d6ad;  18: v = 24'h2a665f;  19: v = 24'he5f0c0;
        default: v = 24'd0;
      endcase
    return v;
  endfunction
  function automatic [65:0] marker(input int lane, input [7:0] bip3);
    reg [23:0] v; reg [7:0] nb; reg [23:0] nv;
    v = m(lane); nv = ~v; nb = ~bip3;
    return {nb, nv, bip3, v, 2'b01};
  endfunction
  function automatic int lane_of(input [65:0] blk);
    int found; reg [23:0] v, nv;
    found = -1; v = blk[25:2]; nv = ~blk[57:34];
    if (blk[1:0] == 2'b01 && v == nv)                     // M4 to M6 must be the inverses of M0 to M2
      for (int l = 0; l < lanes; l++) begin
        v = m(l); nv = ~v;
        if (found < 0 && blk[25:2] == v && blk[57:34] == nv) found = l;
      end
    return found;
  endfunction
endmodule

// eth_mld_lanes: what the PCS lanes of a transmitter carry, lane by lane (82.2.7), and the way to the far end.
// It deals no stream: between the markers every lane holds filler blocks that name their lane and their place.
//   lanes        PCS lanes: 4 or 20. Set it, then call clear().
//   am_gap       blocks between two markers on a lane. The standard's number is 16383; benches use less.
//   bip3[l]      the BIP3 value the markers of lane l carry. This model does not work it out.
//   clear()      empties the lanes
//   fill(k)      every lane now holds k blocks
//   n[l]         blocks on lane l
//   is_am(i)     1 if place i of a lane holds its marker. Every lane starts with one (a bench choice).
//   at(l, i)     block i of lane l
// The way to the far end (82.2.14, 80.5): lanes arrive on other wires, and late.
//   wire_of[l]   the wire PCS lane l arrives on        skew[l]   its delay in whole blocks (a bench choice:
//                the bit part of the skew is what each lane's block lock removes)
//   on_wire(w, t) the block on wire w at block time t: block t - skew of its lane; a filler block outside the lane
// Fault: am_scrambled (each marker goes through a scrambler, from a fixed state).
module eth_mld_lanes;
  eth_am am(); eth_66_scr scr();
  int lanes = 4, am_gap = 16383;
  bit am_scrambled = 0;
  reg [7:0] bip3 [0:19];
  int wire_of [0:19], skew [0:19];
  int n [0:19];
  function automatic void clear();
    am.lanes = lanes;
    for (int l = 0; l < 20; l++) begin n[l] = 0; bip3[l] = 8'h00; wire_of[l] = l; skew[l] = 0; end
  endfunction
  function automatic void fill(input int k);
    for (int l = 0; l < lanes; l++) n[l] = k;
  endfunction
  function automatic bit is_am(input int i); return (i % (am_gap + 1)) == 0; endfunction
  function automatic [65:0] at(input int l, input int i);
    reg [65:0] b;
    if (!is_am(i)) return {l, i, 2'b10};                 // a data block: its payload names the lane and the place
    b = am.marker(l, bip3[l]);
    if (am_scrambled) begin scr.load(58'h155_5555_5555_5555); b = scr.scramble(b); end
    return b;
  endfunction
  function automatic [65:0] on_wire(input int w, input int t);
    reg [65:0] o; int i;
    o = {64'd0, 2'b01};
    for (int l = 0; l < lanes; l++) if (wire_of[l] == w) begin
      i = t - skew[l];
      if (i >= 0 && i < n[l]) o = at(l, i);
    end
    return o;
  endfunction
endmodule

// eth_pma: the PMA bit mux (83.5.2, 120.5.2), in whole PCS-lane bit times.
//   z        PCS lanes: 4 (40GBASE-R), 20 (100GBASE-R), 8 (200GBASE-R) or 16 (400GBASE-R)
//   m, n     input lanes and output lanes. Both divide z.
// A lane that carries z/n PCS lanes sends one bit of each in turn: bits x and x + z/n belong to the same PCS lane.
//   slot     the place of one PCS lane: slot = lane * (PCS lanes per lane) + its position in the lane's turn
//   to[s]    the output slot that input slot s is copied to. Any one-to-one table is legal; it must then stay.
//   straight()     to[s] = s
//   one_to_one()   1 if no output slot is used twice
//   ui(v)          one PCS-lane bit time: v[s] is the bit of input slot s; the result holds the output slots
//   lane(s, lanes), pos(s, lanes)   the lane and the position of slot s on a side with that many lanes
// In whole bit times a bit mux is only a reordering. Its timing (lanes at 2 or 5 times the PCS-lane rate) is not here.
module eth_pma;
  int z = 20, m = 20, n = 10;
  int to [0:19];
  function automatic void straight(); for (int s = 0; s < 20; s++) to[s] = s; endfunction
  function automatic bit one_to_one();
    reg [19:0] used; bit ok;
    used = 20'd0; ok = 1;
    for (int s = 0; s < z; s++) begin if (to[s] < 0 || to[s] >= z || used[to[s]]) ok = 0; else used[to[s]] = 1'b1; end
    return ok;
  endfunction
  function automatic [19:0] ui(input [19:0] v);
    reg [19:0] o;
    o = 20'd0;
    for (int s = 0; s < z; s++) o[to[s]] = v[s];
    return o;
  endfunction
  function automatic int lane(input int s, input int lanes); return s / (z / lanes); endfunction
  function automatic int pos(input int s, input int lanes); return s % (z / lanes); endfunction
endmodule

Check Yourself

Predict the outputFour lanes, 20 blocks between markers. The bench moves the lanes and delays two of them. What does it print for wire 1?
`timescale 1ns/1ns
module tb;
  eth_mld_lanes tx(); int seen;
  initial begin
    tx.lanes = 4; tx.am_gap = 20;
    tx.clear();
    tx.fill(42);
    tx.wire_of[0] = 1; tx.wire_of[1] = 3; tx.wire_of[2] = 0; tx.wire_of[3] = 2;
    tx.skew[0] = 4; tx.skew[2] = 9;
    seen = 0;
    for (int t = 0; t < 40; t++)
      if (tx.am.lane_of(tx.on_wire(1, t)) >= 0) begin
        seen++;
        if (seen == 2) $display("wire 1: PCS lane %0d, second marker at block time %0d", tx.am.lane_of(tx.on_wire(1, t)), t);
      end
    $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_pcs.sv
`timescale 1ns/1ns
// eth_pcs.sv: the 1000BASE-X PCS, transmit side and synchronization (read-only). It sits on hs_8b10b (hs_code.sv).

// eth_1000x: the transmit ordered-set layer (36.2.4). The stream is kept as {k, d} and as ten coded bits.
//   clear()            empty stream, running disparity negative
//   idle(k)            k idle ordered sets. Each tests the running disparity first (Figure 36-6):
//                      positive -> /I1/ (K28.5 D5.6), negative -> /I2/ (K28.5 D16.2)
//   cfg(word, k)       k configuration ordered sets, /C1/ and /C2/ in turn: K28.5, D21.5 or D2.2, word[7:0], word[15:8]
//   start_packet()     /S/ in place of the first preamble octet, then six 55h and D5h
//   data(d)            one data code-group
//   end_packet()       /T/ /R/, and one more /R/ when the first /R/ went out on an even position
//   err()              /V/
//   put(k, d)          any code-group
//   n                  code-groups so far; code-group i is even-numbered when i is even
//   sym[i] = {k, d};  code[i] = abcdei fghj, bit a (code[i][9]) is sent first;  rdb[i] = running disparity before i
//   is_k(i, d), comma_plus(), comma_minus()
// Fault: idle_wrong_disparity (every idle is /I2/, so a positive running disparity is never corrected).
module eth_1000x;
  bit idle_wrong_disparity = 0;
  hs_8b10b c8();
  bit [8:0] sym [0:8191]; bit [9:0] code [0:8191]; bit rdb [0:8191];
  int n = 0; bit c2 = 0;
  function automatic void clear(); n = 0; c2 = 0; c8.rd_tx = 0; endfunction
  function automatic void put(input bit k, input [7:0] d);
    sym[n] = {k, d}; rdb[n] = c8.rd_tx; code[n] = c8.enc(d, k); n++;
  endfunction
  function automatic void idle(input int k);
    bit positive;
    for (int i = 0; i < k; i++) begin
      positive = c8.rd_tx && !idle_wrong_disparity;
      put(1'b1, 8'hbc);                                // K28.5
      put(1'b0, positive ? 8'hc5 : 8'h50);             // D5.6 or D16.2
    end
  endfunction
  function automatic void cfg(input [15:0] word, input int k);
    for (int i = 0; i < k; i++) begin
      put(1'b1, 8'hbc);
      put(1'b0, c2 ? 8'h42 : 8'hb5);                   // D2.2 or D21.5
      put(1'b0, word[7:0]); put(1'b0, word[15:8]);
      c2 = !c2;
    end
  endfunction
  function automatic void data(input [7:0] d); put(1'b0, d); endfunction
  function automatic void start_packet();
    put(1'b1, 8'hfb);                                  // /S/ = K27.7
    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);                                  // /T/ = K29.7
    put(1'b1, 8'hf7);                                  // /R/ = K23.7
    if (n % 2 != 0) put(1'b1, 8'hf7);                  // that /R/ was even-numbered: one more
  endfunction
  function automatic void err(); put(1'b1, 8'hfe); endfunction     // /V/ = K30.7
  function automatic bit is_k(input int i, input [7:0] d); return sym[i] == {1'b1, d}; endfunction
  function automatic int comma_plus();
    int c; c = 0;
    for (int i = 0; i < n; i++) if (code[i][9:3] == 7'b0011111) c++;
    return c;
  endfunction
  function automatic int comma_minus();
    int c; c = 0;
    for (int i = 0; i < n; i++) if (code[i][9:3] == 7'b1100000) c++;
    return c;
  endfunction
endmodule

// eth_1000x_sync: the synchronization process of Figure 36-9, one call of rx() per ten-bit code-group.
//   reset()            LOSS_OF_SYNC
//   rx(code)           takes one code-group; returns sync_status (1 = OK)
//   state              0 LOSS_OF_SYNC, 1 COMMA_DETECT_1, 2 ACQUIRE_SYNC_1, 3 COMMA_DETECT_2, 4 ACQUIRE_SYNC_2,
//                      5 COMMA_DETECT_3, 6 SYNC_ACQUIRED_1, 7 SYNC_ACQUIRED_2, 8 SYNC_ACQUIRED_2A,
//                      9 SYNC_ACQUIRED_3, 10 SYNC_ACQUIRED_3A, 11 SYNC_ACQUIRED_4, 12 SYNC_ACQUIRED_4A
//   even               the code-group just taken was even-numbered
//   good_cgs, bad_seen (every bad code-group since reset), k, d (the last decode)
// A code-group is bad when it is invalid, or when it holds a comma and is odd-numbered (36.2.5.1.3).
// Bench choices: in LOSS_OF_SYNC the decoder takes its running disparity from the comma it finds; an invalid
// code-group leaves the decoder's running disparity as the received bits set it.
module eth_1000x_sync;
  hs_8b10b c8();
  int state = 0, good_cgs = 0, bad_seen = 0;
  bit even = 0, sync = 0, rd = 0, k = 0; bit [7:0] d = 0;
  function automatic void reset(); state = 0; good_cgs = 0; bad_seen = 0; even = 0; sync = 0; rd = 0; endfunction
  function automatic bit rx(input [9:0] code);
    reg [11:0] r; bit invalid, comma, is_d, bad;
    comma = code[9:3] == 7'b0011111 || code[9:3] == 7'b1100000;
    if (state == 0 && comma) rd = (code[9:3] == 7'b1100000);
    r = c8.decode(code, rd);                           // {rd_out, disp_err, code_err, k, d}
    rd = r[11]; k = r[8]; d = r[7:0];
    invalid = r[10] || r[9];
    is_d = !invalid && !r[8];
    bad = invalid || (comma && even);                  // even: the one before was even, so this one is odd
    if (bad && state != 0) bad_seen++;
    case (state)
      0: begin if (comma) begin state = 1; even = 1; end else even = !even; end
      1, 3, 5: begin even = 0; state = is_d ? state + 1 : 0; end
      2, 4: begin
        if (bad) state = 0;
        else if (comma) begin state = state + 1; even = 1; end
        else even = !even;
      end
      6: begin even = !even; if (bad) begin state = 7; good_cgs = 0; end end
      7, 9, 11: begin
        even = !even;
        if (bad) begin state = (state == 11) ? 0 : state + 2; good_cgs = 0; end
        else begin state = state + 1; good_cgs = 1; end
      end
      default: begin                                   // 8, 10, 12: counting good code-groups
        even = !even;
        if (bad) begin state = (state == 12) ? 0 : state + 1; good_cgs = 0; end
        else if (good_cgs == 3) begin state = (state == 8) ? 6 : state - 3; good_cgs = 0; end
        else good_cgs++;
      end
    endcase
    sync = state >= 6;
    return sync;
  endfunction
endmodule

// ---- 10GBASE-R and 25GBASE-R: 64B/66B blocks (Clause 49; Clause 107 uses the same code) ----
// Eight XGMII characters are one vector raw[71:0] (49.2.13.2.2): raw[k] is the control flag of character k,
// raw[8 + 8*k +: 8] its octet. A block is blk[65:0]; bit 0 is sent first. blk[1:0] is the sync header,
// blk[9:2] the block type field of a control block, the rest the payload (Figure 49-7).

// eth_66: the control blocks that hold no Start, no Terminate and no frame data.
//   c_code(x)       Table 49-1: the 7-bit C code of XGMII control code x; 7Fh if x is not in the table
//   enc_ctrl(raw)   {ok, blk}: block type 1Eh (eight control characters), 2Dh (four, then an ordered set),
//                   4Bh (an ordered set, then four) or 55h (two ordered sets); ok = 0 if raw is none of these.
//                   It only lays out the fields. Whether the vector may be sent is the transmit process's
//                   decision (eth_66_tx).
//   on_wire(blk)    the 66 bits as text in the order they are sent: sync, block type, payload
//   ham(a, b)       how many bits differ between two block type values
// Fault: wrong_block_type (types 2Dh and 4Bh are swapped).
module eth_66;
  bit wrong_block_type = 0;
  function automatic [6:0] c_code(input [7:0] x);
    case (x)
      8'h07: return 7'h00;                             // idle
      8'h06: return 7'h06;                             // low power idle
      8'hfe: return 7'h1e;                             // error
      8'h1c: return 7'h2d;                             // the six reserved characters
      8'h3c: return 7'h33;
      8'h7c: return 7'h4b;
      8'hbc: return 7'h55;
      8'hdc: return 7'h66;
      8'hf7: return 7'h78;
      default: return 7'h7f;
    endcase
  endfunction
  function automatic [7:0] ch(input [71:0] raw, input int k); return raw[8 + 8*k +: 8]; endfunction
  // characters k to k+3 are a sequence ordered set: 9Ch with its flag, then three data octets
  function automatic bit is_q(input [71:0] raw, input int k);
    return raw[k] && ch(raw, k) == 8'h9c && !raw[k + 1] && !raw[k + 2] && !raw[k + 3];
  endfunction
  // characters k to k+3 are control characters with a C code
  function automatic bit is_c4(input [71:0] raw, input int k);
    for (int j = k; j < k + 4; j++) if (!raw[j] || c_code(ch(raw, j)) == 7'h7f) return 0;
    return 1;
  endfunction
  function automatic [66:0] enc_ctrl(input [71:0] raw);
    reg [65:0] b; bit q0, q4, c0, c4;
    b = 66'd0;
    b[1:0] = 2'b01;                                    // sync header "10": bit 0, sent first, is 1
    q0 = is_q(raw, 0); q4 = is_q(raw, 4); c0 = is_c4(raw, 0); c4 = is_c4(raw, 4);
    if (c0 && c4) b[9:2] = 8'h1e;
    else if (c0 && q4) b[9:2] = wrong_block_type ? 8'h4b : 8'h2d;
    else if (q0 && c4) b[9:2] = wrong_block_type ? 8'h2d : 8'h4b;
    else if (q0 && q4) b[9:2] = 8'h55;
    else return 67'd0;
    if (c0) for (int k = 0; k < 4; k++) b[10 + 7*k +: 7] = c_code(ch(raw, k));
    else begin b[33:10] = raw[39:16]; b[37:34] = 4'h0; end     // D1 to D3, then the O code of the sequence set
    if (c4) for (int k = 4; k < 8; k++) b[10 + 7*k +: 7] = c_code(ch(raw, k));
    else begin b[41:38] = 4'h0; b[65:42] = raw[71:48]; end     // the O code, then D5 to D7
    return {1'b1, b};
  endfunction
  function automatic string on_wire(input [65:0] blk);
    string s;
    s = "";
    for (int i = 0; i < 66; i++) begin
      if (i == 2 || i == 10) s = {s, " "};
      s = {s, blk[i] ? "1" : "0"};
    end
    return s;
  endfunction
  function automatic int ham(input [7:0] a, input [7:0] b);
    int n; reg [7:0] x;
    x = a ^ b; n = 0;
    for (int i = 0; i < 8; i++) if (x[i]) n++;
    return n;
  endfunction
endmodule

// eth_66_tx: the transmit state diagram (Figure 49-16) without the EEE state. One call per vector.
//   step(t)   t is the T_TYPE of the vector: 0 C (control), 1 S (start), 2 T (terminate), 3 D (data), 4 E (none of these).
//             Returns 1 if the vector is encoded and sent, 0 if the error block goes out in its place.
//   state     0 TX_INIT, 1 TX_C, 2 TX_D, 3 TX_T, 4 TX_E
module eth_66_tx;
  int state = 0;
  function automatic void reset(); state = 0; endfunction
  function automatic bit step(input int t);
    case (state)
      0, 1, 3: state = (t == 0) ? 1 : (t == 1) ? 2 : 4;                    // TX_INIT, TX_C, TX_T: only C or S may follow
      2: state = (t == 3) ? 2 : (t == 2) ? 3 : 4;                          // TX_D: only D or T may follow
      default: state = (t == 2) ? 3 : (t == 3) ? 2 : (t == 0) ? 1 : 4;     // TX_E: T, D or C leave it
    endcase
    return state != 4;
  endfunction
endmodule

// eth_66_comp: clock compensation on XGMII columns (49.2.4.7, 49.2.4.10). Call once per column, in order.
// A column is {c[3:0], d[31:0]}: lane k is flag c[k] and octet d[8k+7:8k].
//   can_delete(c, d)   1 if this column may be dropped: four idles that hold none of the first four characters
//                      after a Terminate, or the second of two consecutive sequence ordered sets
//   can_insert(c, d)   1 if an idle column may be added after this one: it is idle or an ordered set
module eth_66_comp;
  bit guard = 0, prev_seq = 0;
  function automatic void reset(); guard = 0; prev_seq = 0; endfunction
  function automatic bit is_seq(input [3:0] c, input [31:0] d); return c == 4'b0001 && d[7:0] == 8'h9c; endfunction
  function automatic bit is_idle(input [3:0] c, input [31:0] d); return c == 4'b1111 && d == 32'h07070707; endfunction
  function automatic bit can_delete(input [3:0] c, input [31:0] d);
    bit ok, has_t;
    has_t = 0;
    for (int k = 0; k < 4; k++) if (c[k] && d[8*k +: 8] == 8'hfd) has_t = 1;
    ok = (is_idle(c, d) && !guard) || (is_seq(c, d) && prev_seq);
    guard = has_t;                                     // the next column holds some of the four characters after /T/
    prev_seq = is_seq(c, d) && !ok;                    // only one of the two
    return ok;
  endfunction
  function automatic bit can_insert(input [3:0] c, input [31:0] d); return is_idle(c, d) || is_seq(c, d); endfunction
endmodule

// eth_66_scr: the scrambler and the descrambler of 10GBASE-R (49.2.6, 49.2.10; Figures 49-8 and 49-10).
// The register is s[57:0]: stage S0 is s[0] and holds the newest line bit. Use one instance to scramble and
// another to descramble.
//   scr_bit(b)       one payload bit in, one line bit out: b XOR S38 XOR S57. The line bit shifts into S0.
//   dscr_bit(b)      one line bit in, one payload bit out: the same XOR. The received bit shifts into S0.
//   scramble(blk)    a whole block: bits 1:0 (the sync header) pass, bits 2 to 65 are scrambled in that order
//   descramble(blk)  the reverse
//   load(v)          sets the register (the standard asks for no start value)
// Fault: scrambles_header (the two sync header bits go through the scrambler too).
module eth_66_scr;
  bit [57:0] s = 58'd0;
  bit scrambles_header = 0;
  function automatic void load(input [57:0] v); s = v; endfunction
  function automatic bit scr_bit(input bit b);
    bit o; o = b ^ s[38] ^ s[57]; s = {s[56:0], o}; return o;
  endfunction
  function automatic bit dscr_bit(input bit b);
    bit o; o = b ^ s[38] ^ s[57]; s = {s[56:0], b}; return o;
  endfunction
  function automatic [65:0] scramble(input [65:0] blk);
    reg [65:0] o; o = blk;
    for (int i = scrambles_header ? 0 : 2; i < 66; i++) o[i] = scr_bit(blk[i]);
    return o;
  endfunction
  function automatic [65:0] descramble(input [65:0] blk);
    reg [65:0] o; o = blk;
    for (int i = 2; i < 66; i++) o[i] = dscr_bit(blk[i]);
    return o;
  endfunction
endmodule

// eth_prbs31: the PRBS31 pattern generator (49.2.8, Figure 49-9). The register is s[30:0], stage S0 is s[0].
//   gen_bit()   the next pattern bit: S27 XOR S30 shifts into S0, and the output is that bit inverted
// The register must not be all zeros: it would stay there. The checker is not in this file.
module eth_prbs31;
  bit [30:0] s = 31'h7fffffff;
  function automatic bit gen_bit();
    bit f; f = s[27] ^ s[30]; s = {s[29:0], f}; return !f;
  endfunction
endmodule

// eth_66_test: the square wave of 49.2.8 and the two data patterns of the pseudo-random test pattern.
//   sq_bit()        square wave: sq_n ones, then sq_n zeros (sq_n may be 4 to 11)
//   pattern()       the 64-bit data pattern: zeros, or the payload of a block with two local fault sets (lf_pattern)
// The pseudo-random pattern generator and its checker (49.2.8, 49.2.12) are not in this file.
module eth_66_test;
  eth_66 code();
  bit lf_pattern = 0;
  int sq_n = 8, sq_k = 0;
  function automatic void restart(); sq_k = 0; endfunction
  function automatic bit sq_bit();
    bit b; b = (sq_k < sq_n); sq_k = (sq_k + 1) % (2 * sq_n); return b;
  endfunction
  function automatic [63:0] pattern();
    reg [66:0] r;
    if (!lf_pattern) return 64'd0;
    r = code.enc_ctrl({64'h0100009c_0100009c, 8'h11}); return r[65:2];
  endfunction
endmodule

// eth_66_loop: PCS loopback (49.2.14.4), switched by MDIO bit 3.0.14.
//   rx_block(from_tx, from_pma)   what the receive path decodes: its own transmit block when loopback is 1
//   pma_word(w)                   what goes to the PMA: the word 00FFh when loopback is 1, else w
module eth_66_loop;
  bit loopback = 0;
  function automatic [65:0] rx_block(input [65:0] from_tx, input [65:0] from_pma); return loopback ? from_tx : from_pma; endfunction
  function automatic [15:0] pma_word(input [15:0] w); return loopback ? 16'h00ff : w; endfunction
endmodule
Read included file: eth_lanes.sv
`timescale 1ns/1ns
// eth_lanes.sv: the multi-lane PCS of 40GBASE-R and 100GBASE-R, and the PMA bit mux (read-only).

// eth_am: alignment markers (82.2.7, Figure 82-9, Tables 82-2 and 82-3). Bit 0 of a block is sent first.
// Bits 1:0 are the control sync header. Eight octets follow, each sent LSB first:
// M0 in bits 9:2, M1 17:10, M2 25:18, BIP3 33:26, M4 41:34, M5 49:42, M6 57:50, BIP7 65:58.
//   lanes               4: the 40GBASE-R markers. 20: the 100GBASE-R markers.
//   m(lane)             {M2, M1, M0} of that PCS lane, from the table
//   marker(lane, bip3)  the whole marker: M4 to M6 are the inverses of M0 to M2, BIP7 is the inverse of bip3
//   lane_of(blk)        the PCS lane number if blk is a marker, else -1. The two BIP fields are not compared.
module eth_am;
  int lanes = 4;
  function automatic [23:0] m(input int lane);
    reg [23:0] v;
    v = 24'd0;
    if (lanes == 4)
      case (lane)
        0: v = 24'h477690;  1: v = 24'he6c4f0;  2: v = 24'h9b65c5;  3: v = 24'h3d79a2;
        default: v = 24'd0;
      endcase
    else
      case (lane)
        0: v = 24'h2168c1;   1: v = 24'h8e719d;   2: v = 24'he84b59;   3: v = 24'h7b954d;
        4: v = 24'h0907f5;   5: v = 24'hc214dd;   6: v = 24'h264a9a;   7: v = 24'h66457b;
        8: v = 24'h7624a0;   9: v = 24'hfbc968;  10: v = 24'h996cfd;  11: v = 24'h5591b9;
        12: v = 24'hb2b95c; 13: v = 24'hbdf81a;  14: v = 24'hcac783;  15: v = 24'hcd3635;
        16: v = 24'h4c31c4; 17: v = 24'hb7d6ad;  18: v = 24'h2a665f;  19: v = 24'he5f0c0;
        default: v = 24'd0;
      endcase
    return v;
  endfunction
  function automatic [65:0] marker(input int lane, input [7:0] bip3);
    reg [23:0] v; reg [7:0] nb; reg [23:0] nv;
    v = m(lane); nv = ~v; nb = ~bip3;
    return {nb, nv, bip3, v, 2'b01};
  endfunction
  function automatic int lane_of(input [65:0] blk);
    int found; reg [23:0] v, nv;
    found = -1; v = blk[25:2]; nv = ~blk[57:34];
    if (blk[1:0] == 2'b01 && v == nv)                     // M4 to M6 must be the inverses of M0 to M2
      for (int l = 0; l < lanes; l++) begin
        v = m(l); nv = ~v;
        if (found < 0 && blk[25:2] == v && blk[57:34] == nv) found = l;
      end
    return found;
  endfunction
endmodule

// eth_mld_lanes: what the PCS lanes of a transmitter carry, lane by lane (82.2.7), and the way to the far end.
// It deals no stream: between the markers every lane holds filler blocks that name their lane and their place.
//   lanes        PCS lanes: 4 or 20. Set it, then call clear().
//   am_gap       blocks between two markers on a lane. The standard's number is 16383; benches use less.
//   bip3[l]      the BIP3 value the markers of lane l carry. This model does not work it out.
//   clear()      empties the lanes
//   fill(k)      every lane now holds k blocks
//   n[l]         blocks on lane l
//   is_am(i)     1 if place i of a lane holds its marker. Every lane starts with one (a bench choice).
//   at(l, i)     block i of lane l
// The way to the far end (82.2.14, 80.5): lanes arrive on other wires, and late.
//   wire_of[l]   the wire PCS lane l arrives on        skew[l]   its delay in whole blocks (a bench choice:
//                the bit part of the skew is what each lane's block lock removes)
//   on_wire(w, t) the block on wire w at block time t: block t - skew of its lane; a filler block outside the lane
// Fault: am_scrambled (each marker goes through a scrambler, from a fixed state).
module eth_mld_lanes;
  eth_am am(); eth_66_scr scr();
  int lanes = 4, am_gap = 16383;
  bit am_scrambled = 0;
  reg [7:0] bip3 [0:19];
  int wire_of [0:19], skew [0:19];
  int n [0:19];
  function automatic void clear();
    am.lanes = lanes;
    for (int l = 0; l < 20; l++) begin n[l] = 0; bip3[l] = 8'h00; wire_of[l] = l; skew[l] = 0; end
  endfunction
  function automatic void fill(input int k);
    for (int l = 0; l < lanes; l++) n[l] = k;
  endfunction
  function automatic bit is_am(input int i); return (i % (am_gap + 1)) == 0; endfunction
  function automatic [65:0] at(input int l, input int i);
    reg [65:0] b;
    if (!is_am(i)) return {l, i, 2'b10};                 // a data block: its payload names the lane and the place
    b = am.marker(l, bip3[l]);
    if (am_scrambled) begin scr.load(58'h155_5555_5555_5555); b = scr.scramble(b); end
    return b;
  endfunction
  function automatic [65:0] on_wire(input int w, input int t);
    reg [65:0] o; int i;
    o = {64'd0, 2'b01};
    for (int l = 0; l < lanes; l++) if (wire_of[l] == w) begin
      i = t - skew[l];
      if (i >= 0 && i < n[l]) o = at(l, i);
    end
    return o;
  endfunction
endmodule

// eth_pma: the PMA bit mux (83.5.2, 120.5.2), in whole PCS-lane bit times.
//   z        PCS lanes: 4 (40GBASE-R), 20 (100GBASE-R), 8 (200GBASE-R) or 16 (400GBASE-R)
//   m, n     input lanes and output lanes. Both divide z.
// A lane that carries z/n PCS lanes sends one bit of each in turn: bits x and x + z/n belong to the same PCS lane.
//   slot     the place of one PCS lane: slot = lane * (PCS lanes per lane) + its position in the lane's turn
//   to[s]    the output slot that input slot s is copied to. Any one-to-one table is legal; it must then stay.
//   straight()     to[s] = s
//   one_to_one()   1 if no output slot is used twice
//   ui(v)          one PCS-lane bit time: v[s] is the bit of input slot s; the result holds the output slots
//   lane(s, lanes), pos(s, lanes)   the lane and the position of slot s on a side with that many lanes
// In whole bit times a bit mux is only a reordering. Its timing (lanes at 2 or 5 times the PCS-lane rate) is not here.
module eth_pma;
  int z = 20, m = 20, n = 10;
  int to [0:19];
  function automatic void straight(); for (int s = 0; s < 20; s++) to[s] = s; endfunction
  function automatic bit one_to_one();
    reg [19:0] used; bit ok;
    used = 20'd0; ok = 1;
    for (int s = 0; s < z; s++) begin if (to[s] < 0 || to[s] >= z || used[to[s]]) ok = 0; else used[to[s]] = 1'b1; end
    return ok;
  endfunction
  function automatic [19:0] ui(input [19:0] v);
    reg [19:0] o;
    o = 20'd0;
    for (int s = 0; s < z; s++) o[to[s]] = v[s];
    return o;
  endfunction
  function automatic int lane(input int s, input int lanes); return s / (z / lanes); endfunction
  function automatic int pos(input int s, input int lanes); return s % (z / lanes); endfunction
endmodule
ConceptHow does a lane get alignment marker lock?
ConceptWhat tells the receiver which PCS lane a wire carries?
ConceptWhich lanes does the deskew buffer delay?
ConceptHow much skew must a 40GBASE-R PCS receiver absorb?

Common Mistakes

Wrong Take wire k as PCS lane k.

A PMA may move lanes between wires. The data then comes out in the wrong order.

Fix Read the lane number from the marker, and place each lane by that number.

Wrong Trust the first marker pattern a lane shows.

Data can look like a marker by chance. A false lock gives a false lane number.

Fix Ask for the same marker again, exactly 16,384 blocks later.

Wrong Delay the late lanes.

A late lane is already behind. More delay makes the skew larger.

Fix Delay each early lane by its lead over the latest lane.

Wrong Delete a block only if it looks like a marker.

A damaged marker would stay in the stream, and data that looks like a marker would vanish.

Fix In marker lock, delete the block in the marker position. Leave every other block alone.

Remember

  • Lanes arrive on other wires and at other times. Each wire first gets its own block lock.
  • Marker lock needs the same marker twice, 16,384 blocks apart. Four bad markers in a row clear it.
  • The marker gives the lane number. The receiver places lanes by that number, never by wire.
  • Deskew delays the early lanes until all markers line up. The latest lane is not delayed.
  • In lock the marker position is deleted, whatever it holds. The PCS receiver must absorb 180 ns of skew.

Practice

Report a problem with this page