Auto-Negotiation for Backplane and Cable

A backplane port can be anything from 1 Gb/s to 200 Gb/s. Before it sends a frame, it trades one 48-bit page with its partner. See how the page travels and what the two ends decide.

You Will Be Able To

  • Read the bits of a DME page from its transitions.
  • Name the fields of the 48-bit base page.
  • Explain what the two nonce checks catch.
  • Pick the technology and the FEC that two ports enable.
Lesson
P11.37
Time
40 min
Runs on
Your browser
Check
5 questions

The bits are not in the levels. They are in the places where the level changes.

The Start of One DME PageEighteen transition positions, 3.2 ns apart. The line level is shown after each position.
01234567891011121314151617position123456789101112131415161718line levelmeaningdelimiterclockD0 = 1clockD1 = 0clockD2 = 0clockD3 = 0clockD4 = 0
Scroll horizontally ↔
  1. Cycle 0

    The level changes at position 1 and then holds for four positions.

  2. Cycle 4

    It changes at position 5 and holds for four more. No other part of a page holds this long.

  3. Cycle 8

    Position 9 is a clock position. The level always changes there.

  4. Cycle 9

    It changes again at position 10: D0 is 1.

  5. Cycle 11

    No change at position 12: D1 is 0.

Bench run of the readable example: the page C00090960401h, from a line at level 0.

A Second Auto-Negotiation

Clause 73 serves backplane PHYs and some copper cable PHYs, from 1000BASE-KX to 200GBASE-CR4.

Its job is the same as in P11.36: advertise, acknowledge, pick the best common mode.

The signal is new. There are no link pulses here.

A page goes out as a square wave with differential Manchester encoding (DME). It has no DC content (73.1).

On a port with several lanes, pages are sent on lane 0 only (73.5.1).

It also proves that a real partner is there. A port must not lock on to crosstalk (73.1).

DME: The Transitions Carry the Bits

A DME page is a row of transition positions, 3.2 ns apart (73.5.2).

A page starts with a delimiter of eight positions. The next section shows it.

After the delimiter, the level changes at every odd position. That is the clock.

An even position after the delimiter is data. A change there is a 1. No change is a 0.

LeastTypicalMost
Transition position to the next3.2 ns − 0.01%3.2 ns3.2 ns + 0.01%
Clock transition to clock transition6.2 ns6.4 ns6.6 ns
Clock transition to the data transition of a 13.0 ns3.2 ns3.4 ns
Transitions in a page51100
One page338.8 ns339.2 ns339.6 ns
One interval of the delimiter12.6 ns12.8 ns13.0 ns

IEEE Std 802.3-2022 Table 73-2. A page is 106 positions: 8 for the delimiter and 2 for each of 49 bits.

So a 1 is two short intervals, and a 0 is one long one.

The level itself means nothing. Turn the whole line upside down and the page reads the same.

The Delimiter and the 49th Bit

Pages follow each other with no gap. So a receiver must find where one starts.

The first eight positions are the Manchester violation delimiter (73.5.3.1).

The level changes at positions 1 and 5 only. That makes two intervals of 12.8 ns.

No other part of a page holds a level for four positions.

After the delimiter come 49 pairs of clock and data. 48 carry the page.

The 49th is a pseudo-random bit. It adds one transition or none, so it decides whether the next page is upside down.

Without it, one page sent again and again would be a periodic wave with strong spectral lines (73.5.2).

In the example the page has 61 transitions with the extra bit at 0, and 62 with it at 1.

The line carriesA receiver reads
the page of the examplethe page C00090960401h
the same levels, all invertedthe page C00090960401h
the next page, pseudo-random bit 1the page C00090960401h
no transition at clock position 21no page
one transition changed at data position 22the page C00090960441h, a wrong page
one more transition in the delimiter, at position 3no page

Bench run of a private model. The line is a model in whole positions: no timing and no jitter.

A missing clock transition spoils the page. A wrong data transition gives a wrong page.

So here too, a page counts only when it has come three times in a row.

The 48-Bit Base Page

The first page each port sends is the base page (73.6).

BitsFieldMeaningIn the example
D0 to D4S0 to S4Selector: 00001 is IEEE 802.31
D5 to D9E0 to E4Echoed nonce0
D10, D11C0, C1Pause: PAUSE and ASM_DIRC0 = 1, C1 = 0
D12C2Reserved0
D13, D14, D15RF, Ack, NPRemote fault, Acknowledge, Next Page0, 0, 0
D16 to D20T0 to T4Transmitted nonce22
D21 to D43A0 to A22Technology ability10GBASE-KR, 100GBASE-KR4, 25G
D44, D45F2, F3FEC requests of a 25G port0, 0
D46, D47F0, F1FEC ability and request at 10 Gb/s per lane1, 1

IEEE Std 802.3-2022 Figure 73-6 and 73.6. D0 is sent first. The last column is the page of the example.

RF, Ack and NP work as in Clause 28. The selector is the same too.

Note the FEC bits: they sit in the order F2, F3, F0, F1.

The Nonce: Am I Talking to Myself?

The standard gives no reason for the nonce. The usual one: a port may hear its own page.

That can happen through crosstalk on a dense backplane, or through a loopback.

The nonce is a random 5-bit number in the field T (73.6.3).

A port picks a new one each time it enters Ability Detect.

The partner sends that number back in its echo field E, together with Ack (73.6.2).

Two Nonce Checks on the WayThe main path of the arbitration, with the two exits that the nonce adds.
  • Control / FSM
  • Control
Two nonce checks on the wayThe main path of the arbitration, with the two exits that the nonce adds. control Ability Detect new nonce T, echo 0,Ack 0 control AcknowledgeDetect echo the partner's T,Ack 1 control CompleteAcknowledge at least six morepages control AN Good Check resolve and enableone PHY control Transmit Disable silence, then startagain match, other noncematch, my own nonceAck, echo rightAck, echo wrongacks senttimer
  1. control Ability Detectnew nonce T, echo 0, Ack 0
    • match, other nonce → Acknowledge Detect
    • match, my own nonce → Transmit Disable
  2. control Transmit Disablesilence, then start again
    • timer → Ability Detect
  3. control Acknowledge Detectecho the partner's T, Ack 1
    • Ack, echo right → Complete Acknowledge
    • Ack, echo wrong → Transmit Disable
  4. control Complete Acknowledgeat least six more pages
    • acks sent → AN Good Check
  5. control AN Good Checkresolve and enable one PHY
  1. Advertise

    The port picks a fresh nonce and sends its page with echo 0 and Ack 0.

  2. First check

    Three equal pages are in. If they carry this port's own nonce, it restarts.

  3. Echo

    If the nonce is another one, the port sends it back in the echo field, with Ack.

  4. Second check

    Three pages with Ack are in. Their echo must be this port's nonce.

  5. Resolve

    The port sends more pages with Ack. Then it picks the technology and the FEC.

Read the diagram as text
  • Ability Detect: new nonce T, echo 0, Ack 0
  • Acknowledge Detect: echo the partner's T, Ack 1
  • Complete Acknowledge: at least six more pages
  • AN Good Check: resolve and enable one PHY
  • Transmit Disable: silence, then start again
  • Ability Detect → Acknowledge Detect: match, other nonce
  • Ability Detect → Transmit Disable: match, my own nonce
  • Acknowledge Detect → Complete Acknowledge: Ack, echo right
  • Acknowledge Detect → Transmit Disable: Ack, echo wrong
  • Complete Acknowledge → AN Good Check: acks sent
  • Transmit Disable → Ability Detect: timer

Our own drawing of the main path of IEEE Std 802.3-2022 Figure 73-11. Parallel detection and Next Pages are left out.

First check: the page that matched carries my own nonce. Then it is my own page, or bad luck. Restart.

Second check: the page with Ack echoes a number that is not my nonce. Then the partner heard someone else. Restart.

My nonce is 22. The partner's page saysWhat I do
nonce 9, no Ackwait for Acknowledge
nonce 22, no Ackrestart: that is my own nonce
nonce 9, Ack, echo 22go on
nonce 9, Ack, echo 21restart: the echo is wrong
nonce 22, Ack, echo 22restart: that is my own nonce

Bench run of a private model. One code for the two checks is a bench choice.

Two real partners pick the same nonce once in 32 tries. They restart with new ones.

In the bench, a port with nonce 22 that hears nonce 9 sends echo 9 and Ack 1.

Technologies and Priority

The ability field has 23 bits. Sixteen are in use (73.6.4).

BitTechnologyBitTechnology
A01000BASE-KXA8100GBASE-CR4
A110GBASE-KX4A925GBASE-KR-S or 25GBASE-CR-S
A210GBASE-KRA1025GBASE-KR or 25GBASE-CR
A340GBASE-KR4A112.5GBASE-KX
A440GBASE-CR4A125GBASE-KR
A5100GBASE-CR10A1350GBASE-KR or 50GBASE-CR
A6100GBASE-KP4A14100GBASE-KR2 or 100GBASE-CR2
A7100GBASE-KR4A15200GBASE-KR4 or 200GBASE-CR4

IEEE Std 802.3-2022 Table 73-4. A16 to A22 are reserved: sent as 0 and ignored.

The bit numbers follow history: each amendment took the next free bits. That is not the order of preference.

PriorityTechnologyRate and lanes
1200GBASE-KR4 or 200GBASE-CR4200 Gb/s, 4 lanes
2100GBASE-KR2 or 100GBASE-CR2100 Gb/s, 2 lanes
3100GBASE-CR4100 Gb/s, 4 lanes
4100GBASE-KR4100 Gb/s, 4 lanes
5100GBASE-KP4100 Gb/s, 4 lanes
6100GBASE-CR10100 Gb/s, 10 lanes
750GBASE-KR or 50GBASE-CR50 Gb/s, 1 lane
840GBASE-CR440 Gb/s, 4 lanes
940GBASE-KR440 Gb/s, 4 lanes
1025GBASE-KR or 25GBASE-CR25 Gb/s, 1 lane
1125GBASE-KR-S or 25GBASE-CR-S25 Gb/s, 1 lane, short reach
1210GBASE-KR10 Gb/s, 1 lane
1310GBASE-KX410 Gb/s, 4 lanes
145GBASE-KR5 Gb/s, 1 lane
152.5GBASE-KX2.5 Gb/s, 1 lane
161000BASE-KX1 Gb/s, 1 lane

IEEE Std 802.3-2022 Table 73-5, the best first. A private model reproduces it row by row: 16 of 16.

Both ends take the highest row that both pages offer (73.7.6).

A reserved bit is ignored, even when both pages set it.

A 25G port that supports the full mode should set A9 and A10. Then it can also meet a short-reach port.

FEC Resolution

The four F bits settle the error correction of P11.29 to P11.31. Which bits count depends on the winner.

The winner isBits that countRule
10GBASE-KR, 40GBASE-KR4, 40GBASE-CR4 or 100GBASE-CR10F0, F1BASE-R FEC if both ends set F0 and at least one sets F1
25GBASE-KR or 25GBASE-CRF2, F3RS-FEC if either end sets F2. If not, BASE-R FEC if either end sets F3
25GBASE-KR-S or 25GBASE-CR-SF2, F3BASE-R FEC if either end sets F2 or F3
100GBASE-KR4, KP4, CR4, KR2 or CR2, 50GBASE-KR or CR, 200GBASE-KR4 or CR4noneNothing to settle: RS-FEC is a fixed part of these PHYs
1000BASE-KX, 2.5GBASE-KX, 5GBASE-KR or 10GBASE-KX4noneThe F bits decide nothing

IEEE Std 802.3-2022 73.6.5.1 and 73.6.5.2. The fourth row is from Tables 80-3 and 131-2 and Clause 119. F0: able. F1: FEC requested. F2: RS-FEC requested. F3: BASE-R FEC requested.

The fast PHYs have nothing to settle. RS-FEC is a fixed part of them, so the F bits decide nothing there.

At 10 Gb/s per lane, FEC needs ability at both ends. One request is enough.

At 25G nobody advertises ability. A request from either end switches FEC on.

A short-reach 25G PHY has no RS-FEC. So a request for RS-FEC gives BASE-R FEC there.

Port A offersA setsPort B offersB setsBoth enableFECPause at A
10GBASE-KR, 40GBASE-KR4, 40GBASE-CR4, 100GBASE-KR4, 25GF0, F110GBASE-KR, 40GBASE-KR4, 25G short reach, 25GF040GBASE-KR4BASE-R FECsend and obey
25G short reach, 25GF225G short reach, 25GF325GRS-FECoff
25G short reach, 25GF225G short reachnone25G short reachBASE-R FECobey only
25G short reach, 25Gnone25G short reach, 25GF0, F125Gnoneoff
1000BASE-KX, 10GBASE-KRF01000BASE-KX, 10GBASE-KRF010GBASE-KRnonesend only
10GBASE-KRF0, F110GBASE-KRF110GBASE-KRnoneoff
50G, 100G on two lanes, 200Gnone50G, 100G on two lanesF2100G on two lanesRS-FEC, fixed by the PHYsend and obey
1000BASE-KXnone10GBASE-KRnonenothing

Bench run of a private model on pairs of base pages. "25G" stands for 25GBASE-KR or 25GBASE-CR. In the seventh row the F bits decide nothing: that PHY always runs RS-FEC.

Look at the first row. Both pages offer 25G, yet 40GBASE-KR4 wins: it is one row higher.

Pause, Next Pages and Old Partners

C0 and C1 are PAUSE and ASM_DIR. They resolve by Table 28B-3, as in P11.21 (73.6.6).

That table asks for full duplex. Every PHY of Table 73-4 is full duplex only, so it always applies.

Next Pages are 48 bits long here, and every Clause 73 port must support them (73.6.9).

A port with nothing more to say answers with null pages.

Parallel detection covers partners with no auto-negotiation: 1000BASE-KX, 2.5GBASE-KX and 10GBASE-KX4 (73.7.4.1).

After the choice, the chosen PHY gets time to bring its link up: link_fail_inhibit_timer (Table 73-7).

That is 40 to 50 ms for 1000BASE-KX, 2.5GBASE-KX, 5GBASE-KR and 10GBASE-KX4.

It is about 500 ms for 10GBASE-KR and for the 25G, 40G and four-lane or ten-lane 100G PHYs.

The PHYs with 50 Gb/s on a lane get about 3.1 s.

For most of these PHYs the next step in that time is link training: P11.38.

Try It

eth_an73_page packs the fields into 48 bits. Here: three technologies, PAUSE, nonce 22, and F0 with F1.

eth_dme_tx sends the page. It is a bench model: one line level for each transition position.

The delimiter is four positions at one level and four at the other.

D0 is 1: the level changes at positions 9 and 10. D1 is 0: it changes at 11 and holds at 12.

The second page has the extra bit at 1. That is one more transition.

The first page ends on level 1. So the second one starts upside down: its delimiter reads 00001111.

Decoding a page is not in the readable library. It is part of the practice problem.

Set the nonce to 5'b00000. Predict the number of transitions of the first page.

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

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

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

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

// ---------------------------------------------------------------------------------------------
// hs_scr: additive (frame-synchronous) and multiplicative (self-synchronising) scramblers.
// poly holds the coefficients: bit k is x^k; the +1 term is always used (taps() sets bit 0); len is the degree.
// Additive: a Galois LFSR. The key bit is s[len-1]; each step shifts left and adds the low terms of
//   the polynomial when the key bit is 1. The data never enters the register.
// Multiplicative: the line bit is the data bit XOR the register bits at the polynomial's terms;
//   the scrambler shifts its own output in, the descrambler shifts the received line bit in.
//   scr_add(len, poly, seed), scr_mul(len, poly), use_usb3_gen1(), use_x58(), use_x23_usb3() (see its note)
//   scr_bit(b), dscr_bit(b), scr_byte(v), dscr_byte(v) (bit 0 first), scr_reset(), scr_hold() (a skip
//   symbol: no step), scr_unscrambled() (a symbol sent in the clear: 8 steps, data untouched)
// Faults: advance_on_skip (scr_hold steps the LFSR 8 times), no_reset (scr_reset does nothing),
//         wrong_tap (the lowest term x^k becomes x^(k+1)), seed_zero (scr_reset loads 0).
module hs_scr;
  bit mult = 0; int len = 16;
  bit [63:0] poly = 64'h10039, seed = 64'hffff;
  bit advance_on_skip = 0, no_reset = 0, wrong_tap = 0, seed_zero = 0;
  bit [63:0] s = 64'hffff;
  int steps = 0;
  function automatic [63:0] lmask(); return (len >= 64) ? 64'hffffffffffffffff : ((64'd1 << len) - 64'd1); endfunction
  function automatic [63:0] taps();
    reg [63:0] p; int k; p = poly | 64'd1; k = 1;
    if (wrong_tap) begin
      while (k < len && !p[k]) k++;
      if (k < len) begin p[k] = 1'b0; p[k + 1] = 1'b1; end
    end
    return p;
  endfunction
  function automatic void load(); s = (seed_zero ? 64'd0 : seed) & lmask(); endfunction
  function automatic void scr_add(input int n, input [63:0] p, input [63:0] sd); mult = 0; len = n; poly = p; seed = sd; s = sd & lmask(); endfunction
  function automatic void scr_mul(input int n, input [63:0] p); mult = 1; len = n; poly = p; seed = 64'd0; s = 64'd0; endfunction
  function automatic void use_usb3_gen1(); scr_add(16, 64'h10039, 64'hffff); endfunction   // x^16 + x^5 + x^4 + x^3 + 1, seed FFFFh
  function automatic void use_x58(); scr_mul(58, (64'd1 << 58) | (64'd1 << 39) | 64'd1); endfunction   // x^58 + x^39 + 1
  // USB 3.2 Gen 2 (§6.3.2.3) polynomial x^23 + x^21 + x^16 + x^8 + x^5 + x^2 + 1 with seed 1DBFBCh. Only the
  // polynomial and seed are the specification's: the register form here is this library's Galois form, and
  // the reset and bypass rules of §6.3.2.3 are not modelled, so do not quote its key stream as USB's.
  function automatic void use_x23_usb3(); scr_add(23, (64'd1 << 23) | (64'd1 << 21) | (64'd1 << 16) | (64'd1 << 8) | (64'd1 << 5) | (64'd1 << 2) | 64'd1, 64'h1dbfbc); endfunction
  function automatic void scr_reset(); if (!no_reset) load(); endfunction
  function automatic bit key(); return s[len - 1]; endfunction
  function automatic void step();
    reg kb; kb = s[len - 1];
    s = ((s << 1) & lmask()) ^ (kb ? (taps() & lmask()) : 64'd0);
    steps++;
  endfunction
  // the XOR of the register bits at the polynomial's terms x^1..x^len (bit k-1 of the register for x^k)
  function automatic bit fbx(input [63:0] st); return ^(st & (taps() >> 1) & lmask()); endfunction
  function automatic bit scr_bit(input bit b);
    reg o;
    if (!mult) begin o = b ^ s[len - 1]; step(); end
    else begin o = b ^ fbx(s); s = ((s << 1) | {63'd0, o}) & lmask(); steps++; end
    return o;
  endfunction
  function automatic bit dscr_bit(input bit b);
    reg o;
    if (!mult) begin o = b ^ s[len - 1]; step(); end
    else begin o = b ^ fbx(s); s = ((s << 1) | {63'd0, b}) & lmask(); steps++; end
    return o;
  endfunction
  function automatic [7:0] scr_byte(input [7:0] v);
    reg [7:0] o; for (int i = 0; i < 8; i++) o[i] = scr_bit(v[i]); return o;
  endfunction
  function automatic [7:0] dscr_byte(input [7:0] v);
    reg [7:0] o; for (int i = 0; i < 8; i++) o[i] = dscr_bit(v[i]); return o;
  endfunction
  // a skip symbol passes unscrambled and the LFSR holds
  function automatic void scr_hold(); if (advance_on_skip && !mult) for (int i = 0; i < 8; i++) step(); endfunction
  // another symbol that is sent unscrambled (a K code other than COM and SKP): the LFSR still steps 8 times
  function automatic void scr_unscrambled(); if (!mult) for (int i = 0; i < 8; i++) step(); endfunction
endmodule
Read included file: eth_dme.sv
`timescale 1ns/1ns
// eth_dme.sv: the pages of Clause 73 auto-negotiation, sender side (read-only). A bench model: the line is a level,
// 0 or 1, written down once for each transition position. Positions are 3.2 ns apart (Table 73-2).

// eth_dme_tx: one DME page as 106 line levels (73.5.2, 73.5.3).
//   level           the line level now. A page starts from the level the page before it ended on.
//   page(d, r)      sends the 48-bit page d and the pseudo-random bit r. Bit k of the result is the level after
//                   transition position k + 1. D0 is sent first.
//   transitions     the number of transitions in the last page
//   prbit()         a bench generator for the pseudo-random bit: x^7 + x^3 + 1, one step for each page
module eth_dme_tx;
  bit level = 0;
  int transitions = 0;
  reg [6:0] lfsr = 7'h7f;
  function automatic bit prbit();
    bit b;
    b = lfsr[6] ^ lfsr[2];
    lfsr = {lfsr[5:0], b};
    return b;
  endfunction
  function automatic [105:0] page(input [47:0] d, input bit r);
    reg [105:0] out; bit flip;
    transitions = 0;
    for (int pos = 1; pos <= 106; pos++) begin
      if (pos <= 8) flip = (pos == 1 || pos == 5);     // the delimiter: two long intervals
      else if (pos % 2 == 1) flip = 1;                 // a clock position: always a transition
      else if (pos == 106) flip = r;                   // the pseudo-random bit
      else flip = d[(pos - 10) / 2];                   // a data position: a transition is a 1
      if (flip) begin level = !level; transitions++; end
      out[pos - 1] = level;
    end
    return out;
  endfunction
endmodule

// eth_an73_page: the base page as fields (Figure 73-6).
//   page(echo, pause, rf, ack, np, nonce, abil, fec)
//     echo   E4 to E0, the echoed nonce        pause  {C1, C0} = {ASM_DIR, PAUSE}
//     nonce  T4 to T0, the transmitted nonce   abil   A22 to A0 (Table 73-4)
//     fec    {F3, F2, F1, F0}. On the wire the order is F2, F3, F0, F1 in D44 to D47.
//   tech(k)  the name of ability bit Ak
module eth_an73_page;
  function automatic [47:0] page(input [4:0] echo, input [1:0] pause, input bit rf, input bit ack, input bit np,
                                 input [4:0] nonce, input [22:0] abil, input [3:0] fec);
    return {fec[1], fec[0], fec[3], fec[2], abil, nonce, np, ack, rf, 1'b0, pause, echo, 5'b00001};
  endfunction
  function automatic string tech(input int k);
    case (k)
      0: return "1000BASE-KX";
      1: return "10GBASE-KX4";
      2: return "10GBASE-KR";
      3: return "40GBASE-KR4";
      4: return "40GBASE-CR4";
      5: return "100GBASE-CR10";
      6: return "100GBASE-KP4";
      7: return "100GBASE-KR4";
      8: return "100GBASE-CR4";
      9: return "25GBASE-KR-S or 25GBASE-CR-S";
      10: return "25GBASE-KR or 25GBASE-CR";
      11: return "2.5GBASE-KX";
      12: return "5GBASE-KR";
      13: return "50GBASE-KR or 50GBASE-CR";
      14: return "100GBASE-KR2 or 100GBASE-CR2";
      15: return "200GBASE-KR4 or 200GBASE-CR4";
      default: return "reserved";
    endcase
  endfunction
endmodule

Check Yourself

Predict the outputThe bench sends a page with only D0 set, and the pseudo-random bit is 1. How many transitions does it print?
`timescale 1ns/1ns
module tb;
  eth_dme_tx tx(); reg [105:0] lv;
  initial begin
    lv = tx.page(48'h000000000001, 1'b1);
    $display("%0d", tx.transitions);
    $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_dme.sv
`timescale 1ns/1ns
// eth_dme.sv: the pages of Clause 73 auto-negotiation, sender side (read-only). A bench model: the line is a level,
// 0 or 1, written down once for each transition position. Positions are 3.2 ns apart (Table 73-2).

// eth_dme_tx: one DME page as 106 line levels (73.5.2, 73.5.3).
//   level           the line level now. A page starts from the level the page before it ended on.
//   page(d, r)      sends the 48-bit page d and the pseudo-random bit r. Bit k of the result is the level after
//                   transition position k + 1. D0 is sent first.
//   transitions     the number of transitions in the last page
//   prbit()         a bench generator for the pseudo-random bit: x^7 + x^3 + 1, one step for each page
module eth_dme_tx;
  bit level = 0;
  int transitions = 0;
  reg [6:0] lfsr = 7'h7f;
  function automatic bit prbit();
    bit b;
    b = lfsr[6] ^ lfsr[2];
    lfsr = {lfsr[5:0], b};
    return b;
  endfunction
  function automatic [105:0] page(input [47:0] d, input bit r);
    reg [105:0] out; bit flip;
    transitions = 0;
    for (int pos = 1; pos <= 106; pos++) begin
      if (pos <= 8) flip = (pos == 1 || pos == 5);     // the delimiter: two long intervals
      else if (pos % 2 == 1) flip = 1;                 // a clock position: always a transition
      else if (pos == 106) flip = r;                   // the pseudo-random bit
      else flip = d[(pos - 10) / 2];                   // a data position: a transition is a 1
      if (flip) begin level = !level; transitions++; end
      out[pos - 1] = level;
    end
    return out;
  endfunction
endmodule

// eth_an73_page: the base page as fields (Figure 73-6).
//   page(echo, pause, rf, ack, np, nonce, abil, fec)
//     echo   E4 to E0, the echoed nonce        pause  {C1, C0} = {ASM_DIR, PAUSE}
//     nonce  T4 to T0, the transmitted nonce   abil   A22 to A0 (Table 73-4)
//     fec    {F3, F2, F1, F0}. On the wire the order is F2, F3, F0, F1 in D44 to D47.
//   tech(k)  the name of ability bit Ak
module eth_an73_page;
  function automatic [47:0] page(input [4:0] echo, input [1:0] pause, input bit rf, input bit ack, input bit np,
                                 input [4:0] nonce, input [22:0] abil, input [3:0] fec);
    return {fec[1], fec[0], fec[3], fec[2], abil, nonce, np, ack, rf, 1'b0, pause, echo, 5'b00001};
  endfunction
  function automatic string tech(input int k);
    case (k)
      0: return "1000BASE-KX";
      1: return "10GBASE-KX4";
      2: return "10GBASE-KR";
      3: return "40GBASE-KR4";
      4: return "40GBASE-CR4";
      5: return "100GBASE-CR10";
      6: return "100GBASE-KP4";
      7: return "100GBASE-KR4";
      8: return "100GBASE-CR4";
      9: return "25GBASE-KR-S or 25GBASE-CR-S";
      10: return "25GBASE-KR or 25GBASE-CR";
      11: return "2.5GBASE-KX";
      12: return "5GBASE-KR";
      13: return "50GBASE-KR or 50GBASE-CR";
      14: return "100GBASE-KR2 or 100GBASE-CR2";
      15: return "200GBASE-KR4 or 200GBASE-CR4";
      default: return "reserved";
    endcase
  endfunction
endmodule
ConceptWhat marks the start of a DME page?
ConceptBoth pages offer 10GBASE-KR, 5GBASE-KR and 2.5GBASE-KX. Which ability bit names the winner?
ConceptThe winner is 10GBASE-KR. Port A sets F0 and F1. Port B sets neither. Is FEC enabled?
ConceptA port in Ability Detect receives three equal pages whose transmitted nonce equals its own. What happens?

Common Mistakes

Wrong Read the data from the line level.

The pseudo-random bit turns whole pages upside down.

Fix Read the transitions: a change at a data position is a 1.

Wrong Take the highest ability bit that both pages set.

A10 is 25G and A3 is 40GBASE-KR4. The bit numbers follow history.

Fix Walk down Table 73-5 and take the first row both pages offer.

Wrong Read F0 to F3 from D44 to D47 in that order.

The page carries F2, F3, F0, F1.

Fix D44 is F2, D45 is F3, D46 is F0 and D47 is F1.

Wrong Echo your own nonce.

The partner checks that its nonce comes back.

Fix Put the partner's transmitted nonce in the echo field, and keep your own in T.

Remember

  • A DME page is 106 positions of 3.2 ns: a delimiter, 48 page bits and one pseudo-random bit.
  • After the delimiter, a transition at an odd position is the clock. A transition at an even position is a 1.
  • The base page carries selector, echoed nonce, pause, RF, Ack, NP, transmitted nonce, 23 ability bits and 4 FEC bits.
  • A port restarts when it hears its own nonce, or when its nonce does not come back in the echo.
  • The winner is the highest row of Table 73-5 that both pages offer. It also decides which F bits count.

Practice

Report a problem with this page