800G: Two Flows, 32 Lanes

An 800G PCS is two 400G pipelines side by side. See how one stream of blocks is dealt to two flows, and what lets the far end weave it back.

You Will Be Able To

  • Deal a stream of 66-bit blocks to flow 0 and flow 1.
  • Place the marker groups of both flows at the same point of the stream.
  • Tell the 32 markers apart, and from the 400G markers.
  • Sort 32 lanes into two flows and collect their blocks in order.
Lesson
P11.34
Time
40 min
Runs on
Your browser
Check
5 questions

One stream, two flows. Every other block goes the other way.

66-bit block01234567
Goes toflow 0flow 1flow 0flow 1flow 0flow 1flow 0flow 1

Bench run of a private model. Flow 0 packs blocks 0, 2, 4, 6 into one 257-bit block. Flow 1 packs 1, 3, 5, 7.

Two 400G Pipelines Side by Side

At 800G the standard did not build a wider PCS. It put two 400G pipelines side by side (172.2.1).

The Transmit Side of the 800G PCSOne stream of 66-bit blocks is dealt to two flows. Each flow is a 400G pipeline with 16 lanes.
  • Datapath
  • Channel / handshake
  • Data
The transmit side of the 800G PCSOne stream of 66-bit blocks is dealt to two flows. Each flow is a 400G pipeline with 16 lanes. datapath Encode 64B/66B, one stream datapath Block distribution blocks in turn datapath Flow 0 a 400G pipeline: Aand B channel PCS lanes 0 to15 symbols of A and B datapath Flow 1 a 400G pipeline: Aand B channel PCS lanes 16 to31 symbols of A and B 66 bitseven blocksodd blocks2 x 544 symbols2 x 544 symbols
  1. datapath Encode64B/66B, one stream
    • 66 bits → Block distribution
  2. datapath Block distributionblocks in turn
    • even blocks → Flow 0
    • odd blocks → Flow 1
  3. datapath Flow 0a 400G pipeline: A and B
    • 2 x 544 symbols → PCS lanes 0 to 15
  4. datapath Flow 1a 400G pipeline: A and B
    • 2 x 544 symbols → PCS lanes 16 to 31
  5. channel PCS lanes 0 to 15symbols of A and B
  6. channel PCS lanes 16 to 31symbols of A and B
  1. One encoder

    The 800GMII feeds one 64B/66B encoder, as at 400G.

  2. Two flows

    The blocks are dealt in turn: the first to flow 0, the second to flow 1.

  3. Each flow is a 400G PCS

    It transcodes, scrambles, adds markers, splits into A and B, and encodes.

  4. Thirty-two lanes

    Flow 0 fills PCS lanes 0 to 15. Flow 1 fills lanes 16 to 31.

Read the diagram as text
  • Encode: 64B/66B, one stream
  • Block distribution: blocks in turn
  • Flow 0: a 400G pipeline: A and B
  • PCS lanes 0 to 15: symbols of A and B
  • Flow 1: a 400G pipeline: A and B
  • PCS lanes 16 to 31: symbols of A and B
  • Encode → Block distribution: 66 bits
  • Block distribution → Flow 0: even blocks
  • Block distribution → Flow 1: odd blocks
  • Flow 0 → PCS lanes 0 to 15: 2 x 544 symbols
  • Flow 1 → PCS lanes 16 to 31: 2 x 544 symbols

After IEEE Std 802.3df-2024 Figure 172-2 (redrawn, transmit half).

Each pipeline is called a flow. Each flow has its own transcoder, scrambler, markers and two RS(544,514) encoders.

400GBASE-R800GBASE-R
PCS clause119172 (in 802.3df)
Flowsonetwo
Codewords at a time24: A and B in each flow
PCS lanes1632
Bit rate on the lanes425 Gb/s850 Gb/s
One PCS lane26.5625 Gb/s26.5625 Gb/s
Skew the receiver must absorb180 ns152 ns

IEEE Std 802.3-2022 119.2.4, 119.2.5.1; IEEE Std 802.3df-2024 172.2.1, 172.2.5.1. The 850 Gb/s is bench arithmetic.

The lanes carry 850 Gb/s in all. A PCS lane runs at the 400G lane rate.

Under the PCS, a 32:8 PMA puts four PCS lanes on each of 8 physical lanes (173.2). Each runs PAM4 at 53.125 GBd.

Eight lanes carry every 800G PHY of the amendment: backplane, copper cable and fibre (169.1.3).

An optional extender can sit above the PHY. Its sublayer, the 800GXS, works like this PCS (169.2.2).

Blocks Take Turns

The block distribution is a round robin of whole 66-bit blocks (172.2.4.3).

The first block goes to flow 0, the second to flow 1, the third to flow 0.

Each flow then transcodes four of its blocks into one 257-bit block, as P11.30 showed.

Flow257-bit block66-bit blocks insideMarker group before it
Flow 000, 2, 4, 6yes
Flow 101, 3, 5, 7yes
Flow 018, 10, 12, 14no
Flow 119, 11, 13, 15no
Flow 0216, 18, 20, 22no
Flow 1217, 19, 21, 23no
Flow 0324, 26, 28, 30yes
Flow 1325, 27, 29, 31yes

Bench run of a private model on numbered blocks. For the table a group comes every 3 blocks. The standard's number is 163,832.

So a 257-bit block of flow 0 holds four even blocks. Its partner in flow 1 holds the four odd ones between them.

A frame is spread over both flows. Neither flow alone carries a readable stream.

Marker Groups at the Same Point

Each flow inserts a marker group of its own. It is the 400G group: eight 257-bit blocks (172.2.4.6).

A flow sends one group in every 163,840 blocks. That is 8,192 codewords of the flow.

The groups take 48.8 parts per million of the line. The standard rounds it to 49 (172.2.5.10).

The new rule is where the groups go. Both flows must insert at the same point of the original stream.

Say the group of flow 0 goes before the 257-bit block that holds 66-bit block k.

Then the group of flow 1 goes before the block that holds k + 1.

Look at the table above: the groups sit before blocks 0 and 1, then before 24 and 25.

The far end needs this. It has two streams of blocks and must pair them up again.

Our far end is a model: it waits for a group in each flow, then pairs the blocks.

A real receiver first lines all 32 lanes up on their markers (172.2.5.1). It pairs the blocks wrongly in the same way.

Near endFirst eight blocks out of the far endBlocks out of order
Both groups at the same point40, 41, 42, 43, 44, 45, 46, 470 of 136
Group of flow 1 one 257-bit block late40, 49, 42, 51, 44, 53, 46, 55127 of 128

Bench run of a private model. The far end starts in mid-stream and waits for a marker group in each flow. "Out of order": not one more than the block before.

With one group a block late, flow 1 runs eight blocks ahead. Every pair is wrong, and no frame survives.

Thirty-Two Lanes, Two Marker Tables

Flow 0 deals its two codewords to PCS lanes 0 to 15. Flow 1 uses lanes 16 to 31 (172.2.4.10).

Inside a flow nothing is new: lane j of flow 1 is filled like lane j of a 400G PCS.

The markers are new. Each flow has its own table of 16 (Tables 172-2 and 172-3).

MarkerUM0UM1UM2UM3UM4UM5
400GBASE-R, lane 00171f3fe8e0c
800GBASE-R, lane 0 (flow 0)fe71f3018e0c
800GBASE-R, lane 16 (flow 1)018e0cfe71f3

Bench run of the readable model. Values in hexadecimal, from Table 119-2 and Tables 172-2 and 172-3.

The common part is the 400G one. So the same lock logic finds a marker on any of the 32 lanes.

Lanes j and j + 16 also share their pad octets. Only the unique part tells them apart.

By the bench's count, flow 0 is the 400G marker with UM0 and UM3 inverted, on 16 of 16 lanes.

Flow 1 is the 400G marker with the other four octets inverted. That is our reading of the tables, not a rule.

A 400G receiver names a lane when 9 of the 12 nibbles of the unique part match (119.2.6.2.2).

By the bench's count, an 800G marker matches a 400G one in at most 8 nibbles.

So a 400G port cannot take an 800G lane for one of its own.

Check against Annex 172AFlow 0Flow 1
Four idle blocks, transcodedequalequal
Scrambled from the annex's start state32 of 32 blocks equal32 of 32 blocks equal
Marker group built from the marker tableequalequal
Codewords A and Bboth equalboth equal
First two parity symbols of A869, 920120, 745
Lanes that start with their own marker16 of 16: lanes 0 to 1516 of 16: lanes 16 to 31

Bench run of private models on the example of Annex 172A of IEEE Std 802.3df-2024.

Two Scramblers

Each flow has its own scrambler. It runs over whole 257-bit blocks, as at 400G (172.2.4.5).

A scrambler's output depends on its input and on its 58 bits of state.

Both flows often get the same input: idle blocks, or a fault signal sent over and over.

The bench gave two scramblers one state and the same input. 32 of 32 blocks came out equal.

With another state in one of them, 0 of 32 were equal.

Equal flows put equal bits on lanes j and j + 16. A bit mux below can then send odd patterns.

So the standard says: if the states are set at reset, they should differ.

The Receive Side: Sort, Decode, Merge

The receiver locks to the markers on all 32 lanes and removes the skew (172.2.5.1).

StepWhat it uses
Marker lock, 32 timesthe common part of the marker
Deskew and reorderthe unique part gives the PCS lane, 0 to 31
Split into two sets of 16lanes 0 to 15 are flow 0, lanes 16 to 31 are flow 1
Decode each flowde-interleave, two decoders, remove markers, descramble, transcode
Block collectionone 66-bit block from each flow in turn

IEEE Std 802.3df-2024 172.2.5.1 to 172.2.5.8.

A PCS lane may arrive on any wire. So the split goes by the lane number, never by the wire number.

The block collection undoes the distribution (172.2.5.8).

Its anchor is the marker group. The first block after the group of flow 0 comes first.

The first block after the group of flow 1 follows it. Then the flows alternate.

Some status is shared. A high symbol error ratio in either flow counts for the whole PCS (172.2.6.2.2).

Three bad A or three bad B codewords in a row in one flow restart the lock on all lanes (172.2.6.3).

Try It

eth_am172 holds the markers of all 32 PCS lanes. eth_am119 is the 400G table of P11.33.

Line 1 prints three unique parts. UM3 to UM5 are the inverses of UM0 to UM2 in each of them.

Lane 16 has the two halves of lane 0 in the other order.

lane_of compares the common and the unique part. It finds every 800G lane and refuses all 400G markers.

Two scramblers with one state and one input give the same blocks. That is why the states should differ.

The readable library stops here. Dealing the blocks and collecting them are the practice problem.

Change one bit of a unique part and call lane_of. Predict the result.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: unique part, UM0 to UM5: 400G lane 0: 01 71 f3 fe 8e 0c; 800G lane 0: fe 71 f3 01 8e 0c; 800G lane 16: 01 8e 0c fe 71 f3 common part and pad shared by lanes j and j + 16, common part shared with 400G: 16 of 16 flow 0 is the 400G marker with UM0 and UM3 inverted: 16 of 16; flow 1 with the other four inverted: 16 of 16 lane j + 16 has the two halves of lane j's unique part in the other order: 16 of 16 lane_of finds its own lane: 32 of 32; 400G markers it takes for an 800G lane: 0 of 16 nibbles of the unique part that match, of 12: an 800G and a 400G marker at most 8; two 800G markers at most 4 two scramblers from one state, the same 32 blocks in: 32 of 32 blocks equal after one of them is set to another state: 0 of 32
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_pcs119.sv
`timescale 1ns/1ns
// eth_pcs119.sv: readable parts of the 200GBASE-R and 400GBASE-R PCS (Clause 119) (read-only).

// eth_am119: the alignment markers (119.2.4.4, Figure 119-4, Tables 119-1 and 119-2) and what goes with them.
// Bit 0 of a marker is the first bit sent.
//   g400            0: 200GBASE-R, 8 PCS lanes. 1: 400GBASE-R, 16 PCS lanes
//   lanes()         8 or 16
//   am(x)           the 120-bit marker of PCS lane x: CM0 CM1 CM2 UP0 CM3 CM4 CM5 UP1 UM0 UM1 UM2 UP2 UM3 UM4 UM5,
//                   one octet each, each octet sent lowest bit first
//   common(m)       the 48 bits that are the same on every lane (CM0 to CM5)
//   unique_part(m)  the 48 bits that name the lane (UM0 to UM5)
//   pad_seed(s), pad_bit()   the PRBS9 generator of the pad, x^9 + x^5 + 1. The standard only asks that it
//                   free-runs and never starts at zero.
//   status(deg, far)         tx_am_sf<2:0> without an extender sublayer: bit 2 is FEC_degraded_SER or
//                   rx_local_degraded
module eth_am119;
  bit g400;
  reg [8:0] prbs = 9'h001;
  function automatic int lanes(); return g400 ? 16 : 8; endfunction
  function automatic [119:0] am(input int x);
    if (g400)
      case (x)
      0: return 120'hc8efe26f37101d9d9b565b6264a9a;
      1: return 120'h8121a5987ede5a67d9b56504264a9a;
      2: return 120'ha90cc10156f33efed9b56546264a9a;
      3: return 120'h2f7f797bd0808684d9b5655a264a9a;
      4: return 120'hdaed5e6f2512a19d9b565e1264a9a;
      5: return 120'h2eb0edb1d14f124ed9b565f2264a9a;
      6: return 120'h5e63bd11a19c42eed9b5653d264a9a;
      7: return 120'ha48929cd5b76d632d9b56522264a9a;
      8: return 120'h8a8c1e607573e19fd9b56560264a9a;
      9: return 120'hc33b8e5d3cc471a2d9b5656b264a9a;
      10: return 120'h27146afbd8eb9504d9b565fa264a9a;
      11: return 120'hc799dd8e38662271d9b5656c264a9a;
      12: return 120'h6a095da495f6a25bd9b56518264a9a;
      13: return 120'h3c68ce33c39731ccd9b56514264a9a;
      14: return 120'h5904354ea6fbcab1d9b565d0264a9a;
      15: return 120'h864559a979baa656d9b565b4264a9a;
      default: return 120'd0;
      endcase
    else
      case (x)
      0: return 120'h733f4c298cc0b3d6d9b56505264a9a;
      1: return 120'h8121a5987ede5a67d9b56504264a9a;
      2: return 120'ha90cc10156f33efed9b56546264a9a;
      3: return 120'h2f7f797bd0808684d9b5655a264a9a;
      4: return 120'hdaed5e6f2512a19d9b565e1264a9a;
      5: return 120'h2eb0edb1d14f124ed9b565f2264a9a;
      6: return 120'h5e63bd11a19c42eed9b5653d264a9a;
      7: return 120'ha48929cd5b76d632d9b56522264a9a;
      default: return 120'd0;
      endcase
  endfunction
  function automatic [47:0] common(input [119:0] m); return {m[55:32], m[23:0]}; endfunction
  function automatic [47:0] unique_part(input [119:0] m); return {m[119:96], m[87:64]}; endfunction
  function automatic void pad_seed(input [8:0] s); prbs = (s == 9'd0) ? 9'h001 : s; endfunction
  function automatic bit pad_bit();
    bit o;
    o = prbs[8];
    prbs = {prbs[7:0], prbs[8] ^ prbs[4]};
    return o;
  endfunction
  function automatic [2:0] status(input bit deg, input bit far); return {deg | far, 2'b00}; endfunction
endmodule
Read included file: eth_pcs172.sv
`timescale 1ns/1ns
// eth_pcs172.sv: readable parts of the 800GBASE-R PCS (IEEE Std 802.3df-2024 Clause 172) (read-only).

// eth_am172: the alignment markers of the 32 PCS lanes (172.2.4.6, Tables 172-2 and 172-3; the format is that of
// Figure 119-4). Bit 0 of a marker is the first bit sent. PCS lanes 0 to 15 are flow 0, lanes 16 to 31 are flow 1.
//   am(lane)        the 120-bit marker of PCS lane 0 to 31: CM0 CM1 CM2 UP0 CM3 CM4 CM5 UP1 UM0 UM1 UM2 UP2 UM3 UM4
//                   UM5, one octet each, each octet sent lowest bit first
//   common(m)       the 48 bits that are the same on every lane (CM0 to CM5)
//   unique_part(m)  the 48 bits that name the lane (UM0 to UM5): UM0 in bits 7:0, UM5 in bits 47:40
//   pad_part(m)     the three pad octets (UP0 to UP2)
//   lane_of(m)      the PCS lane whose common and unique parts m carries, or -1. The pad is not compared.
module eth_am172;
  function automatic [119:0] am(input int lane);
    case (lane)
      0: return 120'hc8e0126f371fed9d9b565b6264a9a;
      1: return 120'h81215a987edea567d9b56504264a9a;
      2: return 120'ha90c3e0156f3c1fed9b56546264a9a;
      3: return 120'h2f7f867bd0807984d9b5655a264a9a;
      4: return 120'hdae2ae6f251d519d9b565e1264a9a;
      5: return 120'h2eb012b1d14fed4ed9b565f2264a9a;
      6: return 120'h5e634211a19cbdeed9b5653d264a9a;
      7: return 120'ha489d6cd5b762932d9b56522264a9a;
      8: return 120'h8a8ce16075731e9fd9b56560264a9a;
      9: return 120'hc33b715d3cc48ea2d9b5656b264a9a;
      10: return 120'h271495fbd8eb6a04d9b565fa264a9a;
      11: return 120'hc799228e3866dd71d9b5656c264a9a;
      12: return 120'h6a09a2a495f65d5bd9b56518264a9a;
      13: return 120'h3c683133c397ceccd9b56514264a9a;
      14: return 120'h5904ca4ea6fb35b1d9b565d0264a9a;
      15: return 120'h8645a6a979ba5956d9b565b4264a9a;
      16: return 120'hf371fe260c8e01d9d9b565b6264a9a;
      17: return 120'h7edea59881215a67d9b56504264a9a;
      18: return 120'h56f3c101a90c3efed9b56546264a9a;
      19: return 120'hd080797b2f7f8684d9b5655a264a9a;
      20: return 120'hf251d5e60dae2a19d9b565e1264a9a;
      21: return 120'hd14fedb12eb0124ed9b565f2264a9a;
      22: return 120'ha19cbd115e6342eed9b5653d264a9a;
      23: return 120'h5b7629cda489d632d9b56522264a9a;
      24: return 120'h75731e608a8ce19fd9b56560264a9a;
      25: return 120'h3cc48e5dc33b71a2d9b5656b264a9a;
      26: return 120'hd8eb6afb27149504d9b565fa264a9a;
      27: return 120'h3866dd8ec7992271d9b5656c264a9a;
      28: return 120'h95f65da46a09a25bd9b56518264a9a;
      29: return 120'hc397ce333c6831ccd9b56514264a9a;
      30: return 120'ha6fb354e5904cab1d9b565d0264a9a;
      31: return 120'h79ba59a98645a656d9b565b4264a9a;
      default: return 120'd0;
    endcase
  endfunction
  function automatic [47:0] common(input [119:0] m); return {m[55:32], m[23:0]}; endfunction
  function automatic [47:0] unique_part(input [119:0] m); return {m[119:96], m[87:64]}; endfunction
  function automatic [23:0] pad_part(input [119:0] m); return {m[95:88], m[63:56], m[31:24]}; endfunction
  function automatic int lane_of(input [119:0] m);
    int found;
    found = -1;
    for (int l = 0; l < 32; l++)
      if (found < 0 && common(m) == common(am(l)) && unique_part(m) == unique_part(am(l))) found = l;
    return found;
  endfunction
endmodule

// eth_scr257: the scrambler of one flow (172.2.4.5, 119.2.4.3): the x^58 + x^39 + 1 scrambler of 49.2.6 over all
// 257 bits of a transcoded block, bit 0 first.
//   s             the 58 bits of state: s[0] is the newest scrambled bit (S0 of Figure 49-8)
//   load(v)       sets the state
//   scramble(x)   one 257-bit block
module eth_scr257;
  reg [57:0] s = 58'd0;
  function automatic void load(input [57:0] v); s = v; endfunction
  function automatic [256:0] scramble(input [256:0] x);
    reg [256:0] o; reg b;
    for (int i = 0; i < 257; i++) begin
      b = x[i] ^ s[38] ^ s[57];
      s = {s[56:0], b};
      o[i] = b;
    end
    return o;
  endfunction
endmodule

Check Yourself

Predict the outputThe bench spoils a pad octet of marker 21 and asks for its lane. Then it asks about a 400G marker. Last it adds the lanes of markers 5 and 21. What does it print?
`timescale 1ns/1ns
module tb;
  eth_am172 am(); eth_am119 old(); reg [119:0] m;
  initial begin
    old.g400 = 1;
    m = am.am(21); m[31:24] = 8'h00;                 // UP0 spoiled
    $display("%0d %0d %0d", am.lane_of(m), am.lane_of(old.am(5)), am.lane_of(am.am(5)) + am.lane_of(am.am(21)));
    $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_pcs119.sv
`timescale 1ns/1ns
// eth_pcs119.sv: readable parts of the 200GBASE-R and 400GBASE-R PCS (Clause 119) (read-only).

// eth_am119: the alignment markers (119.2.4.4, Figure 119-4, Tables 119-1 and 119-2) and what goes with them.
// Bit 0 of a marker is the first bit sent.
//   g400            0: 200GBASE-R, 8 PCS lanes. 1: 400GBASE-R, 16 PCS lanes
//   lanes()         8 or 16
//   am(x)           the 120-bit marker of PCS lane x: CM0 CM1 CM2 UP0 CM3 CM4 CM5 UP1 UM0 UM1 UM2 UP2 UM3 UM4 UM5,
//                   one octet each, each octet sent lowest bit first
//   common(m)       the 48 bits that are the same on every lane (CM0 to CM5)
//   unique_part(m)  the 48 bits that name the lane (UM0 to UM5)
//   pad_seed(s), pad_bit()   the PRBS9 generator of the pad, x^9 + x^5 + 1. The standard only asks that it
//                   free-runs and never starts at zero.
//   status(deg, far)         tx_am_sf<2:0> without an extender sublayer: bit 2 is FEC_degraded_SER or
//                   rx_local_degraded
module eth_am119;
  bit g400;
  reg [8:0] prbs = 9'h001;
  function automatic int lanes(); return g400 ? 16 : 8; endfunction
  function automatic [119:0] am(input int x);
    if (g400)
      case (x)
      0: return 120'hc8efe26f37101d9d9b565b6264a9a;
      1: return 120'h8121a5987ede5a67d9b56504264a9a;
      2: return 120'ha90cc10156f33efed9b56546264a9a;
      3: return 120'h2f7f797bd0808684d9b5655a264a9a;
      4: return 120'hdaed5e6f2512a19d9b565e1264a9a;
      5: return 120'h2eb0edb1d14f124ed9b565f2264a9a;
      6: return 120'h5e63bd11a19c42eed9b5653d264a9a;
      7: return 120'ha48929cd5b76d632d9b56522264a9a;
      8: return 120'h8a8c1e607573e19fd9b56560264a9a;
      9: return 120'hc33b8e5d3cc471a2d9b5656b264a9a;
      10: return 120'h27146afbd8eb9504d9b565fa264a9a;
      11: return 120'hc799dd8e38662271d9b5656c264a9a;
      12: return 120'h6a095da495f6a25bd9b56518264a9a;
      13: return 120'h3c68ce33c39731ccd9b56514264a9a;
      14: return 120'h5904354ea6fbcab1d9b565d0264a9a;
      15: return 120'h864559a979baa656d9b565b4264a9a;
      default: return 120'd0;
      endcase
    else
      case (x)
      0: return 120'h733f4c298cc0b3d6d9b56505264a9a;
      1: return 120'h8121a5987ede5a67d9b56504264a9a;
      2: return 120'ha90cc10156f33efed9b56546264a9a;
      3: return 120'h2f7f797bd0808684d9b5655a264a9a;
      4: return 120'hdaed5e6f2512a19d9b565e1264a9a;
      5: return 120'h2eb0edb1d14f124ed9b565f2264a9a;
      6: return 120'h5e63bd11a19c42eed9b5653d264a9a;
      7: return 120'ha48929cd5b76d632d9b56522264a9a;
      default: return 120'd0;
      endcase
  endfunction
  function automatic [47:0] common(input [119:0] m); return {m[55:32], m[23:0]}; endfunction
  function automatic [47:0] unique_part(input [119:0] m); return {m[119:96], m[87:64]}; endfunction
  function automatic void pad_seed(input [8:0] s); prbs = (s == 9'd0) ? 9'h001 : s; endfunction
  function automatic bit pad_bit();
    bit o;
    o = prbs[8];
    prbs = {prbs[7:0], prbs[8] ^ prbs[4]};
    return o;
  endfunction
  function automatic [2:0] status(input bit deg, input bit far); return {deg | far, 2'b00}; endfunction
endmodule
Read included file: eth_pcs172.sv
`timescale 1ns/1ns
// eth_pcs172.sv: readable parts of the 800GBASE-R PCS (IEEE Std 802.3df-2024 Clause 172) (read-only).

// eth_am172: the alignment markers of the 32 PCS lanes (172.2.4.6, Tables 172-2 and 172-3; the format is that of
// Figure 119-4). Bit 0 of a marker is the first bit sent. PCS lanes 0 to 15 are flow 0, lanes 16 to 31 are flow 1.
//   am(lane)        the 120-bit marker of PCS lane 0 to 31: CM0 CM1 CM2 UP0 CM3 CM4 CM5 UP1 UM0 UM1 UM2 UP2 UM3 UM4
//                   UM5, one octet each, each octet sent lowest bit first
//   common(m)       the 48 bits that are the same on every lane (CM0 to CM5)
//   unique_part(m)  the 48 bits that name the lane (UM0 to UM5): UM0 in bits 7:0, UM5 in bits 47:40
//   pad_part(m)     the three pad octets (UP0 to UP2)
//   lane_of(m)      the PCS lane whose common and unique parts m carries, or -1. The pad is not compared.
module eth_am172;
  function automatic [119:0] am(input int lane);
    case (lane)
      0: return 120'hc8e0126f371fed9d9b565b6264a9a;
      1: return 120'h81215a987edea567d9b56504264a9a;
      2: return 120'ha90c3e0156f3c1fed9b56546264a9a;
      3: return 120'h2f7f867bd0807984d9b5655a264a9a;
      4: return 120'hdae2ae6f251d519d9b565e1264a9a;
      5: return 120'h2eb012b1d14fed4ed9b565f2264a9a;
      6: return 120'h5e634211a19cbdeed9b5653d264a9a;
      7: return 120'ha489d6cd5b762932d9b56522264a9a;
      8: return 120'h8a8ce16075731e9fd9b56560264a9a;
      9: return 120'hc33b715d3cc48ea2d9b5656b264a9a;
      10: return 120'h271495fbd8eb6a04d9b565fa264a9a;
      11: return 120'hc799228e3866dd71d9b5656c264a9a;
      12: return 120'h6a09a2a495f65d5bd9b56518264a9a;
      13: return 120'h3c683133c397ceccd9b56514264a9a;
      14: return 120'h5904ca4ea6fb35b1d9b565d0264a9a;
      15: return 120'h8645a6a979ba5956d9b565b4264a9a;
      16: return 120'hf371fe260c8e01d9d9b565b6264a9a;
      17: return 120'h7edea59881215a67d9b56504264a9a;
      18: return 120'h56f3c101a90c3efed9b56546264a9a;
      19: return 120'hd080797b2f7f8684d9b5655a264a9a;
      20: return 120'hf251d5e60dae2a19d9b565e1264a9a;
      21: return 120'hd14fedb12eb0124ed9b565f2264a9a;
      22: return 120'ha19cbd115e6342eed9b5653d264a9a;
      23: return 120'h5b7629cda489d632d9b56522264a9a;
      24: return 120'h75731e608a8ce19fd9b56560264a9a;
      25: return 120'h3cc48e5dc33b71a2d9b5656b264a9a;
      26: return 120'hd8eb6afb27149504d9b565fa264a9a;
      27: return 120'h3866dd8ec7992271d9b5656c264a9a;
      28: return 120'h95f65da46a09a25bd9b56518264a9a;
      29: return 120'hc397ce333c6831ccd9b56514264a9a;
      30: return 120'ha6fb354e5904cab1d9b565d0264a9a;
      31: return 120'h79ba59a98645a656d9b565b4264a9a;
      default: return 120'd0;
    endcase
  endfunction
  function automatic [47:0] common(input [119:0] m); return {m[55:32], m[23:0]}; endfunction
  function automatic [47:0] unique_part(input [119:0] m); return {m[119:96], m[87:64]}; endfunction
  function automatic [23:0] pad_part(input [119:0] m); return {m[95:88], m[63:56], m[31:24]}; endfunction
  function automatic int lane_of(input [119:0] m);
    int found;
    found = -1;
    for (int l = 0; l < 32; l++)
      if (found < 0 && common(m) == common(am(l)) && unique_part(m) == unique_part(am(l))) found = l;
    return found;
  endfunction
endmodule

// eth_scr257: the scrambler of one flow (172.2.4.5, 119.2.4.3): the x^58 + x^39 + 1 scrambler of 49.2.6 over all
// 257 bits of a transcoded block, bit 0 first.
//   s             the 58 bits of state: s[0] is the newest scrambled bit (S0 of Figure 49-8)
//   load(v)       sets the state
//   scramble(x)   one 257-bit block
module eth_scr257;
  reg [57:0] s = 58'd0;
  function automatic void load(input [57:0] v); s = v; endfunction
  function automatic [256:0] scramble(input [256:0] x);
    reg [256:0] o; reg b;
    for (int i = 0; i < 257; i++) begin
      b = x[i] ^ s[38] ^ s[57];
      s = {s[56:0], b};
      o[i] = b;
    end
    return o;
  endfunction
endmodule
ConceptHow many PCS lanes does an 800GBASE-R PCS have?
ConceptWhere does block 6 of the stream go, counting from 0?
ConceptWhat tells PCS lane 3 from PCS lane 19?
ConceptWhy must both flows insert their marker groups at the same point of the stream?

Common Mistakes

Wrong Deal the blocks to the flows four at a time, one 257-bit block each.

The distribution works on single 66-bit blocks, before the transcoder.

Fix Even blocks to flow 0, odd blocks to flow 1.

Wrong Let each flow insert its marker group when it likes.

The far end pairs the blocks of the two flows by their place after the groups.

Fix Insert both groups at the same point of the original stream: before blocks k and k + 1.

Wrong Use the 400G marker table for both flows.

Lanes j and j + 16 would look the same, and a 400G port could lock to them.

Fix Use Table 172-2 for lanes 0 to 15 and Table 172-3 for lanes 16 to 31.

Wrong Send wires 0 to 15 to the decoder of flow 0.

A PCS lane can arrive on any wire.

Fix Read the lane number from the unique part. Lanes 0 to 15 are flow 0.

Remember

  • An 800G PCS is two 400G pipelines, called flows. Each makes two RS(544,514) codewords and fills 16 PCS lanes.
  • Block distribution deals whole 66-bit blocks in turn: even ones to flow 0, odd ones to flow 1.
  • Both flows insert their marker group at the same point of the stream: before blocks k and k + 1.
  • Lanes 0 to 15 and 16 to 31 have their own marker tables. They share the common part and differ in the unique part.
  • The receiver sorts 32 lanes by lane number, decodes each flow, and collects blocks in turn, anchored on the groups.

Practice

Report a problem with this page