BASE-R FEC (Clause 74)

A backplane link makes errors in short bursts. See how BASE-R FEC finds room for 32 parity bits without sending one bit more, and what those bits can repair.

You Will Be Able To

  • Explain where the 32 parity bits of an FEC block come from.
  • Transcode a 66-bit block to 65 bits and back.
  • State what the (2112,2080) code corrects, and follow the search that finds a burst.
  • Trace the FEC lock: four good blocks to lock, eight bad ones to lose it.
Lesson
P11.29
Time
40 min
Runs on
Your browser
Check
5 questions

One FEC block, hit four ways. The decoder repairs a burst of 11 wrong bits, but not 12.

What hit the FEC blockDecoderWords wrong after decoding
Clean blockParity matches0 of 32
One wrong bitBurst found and removed0 of 32
11-bit burstBurst found and removed0 of 32
12-bit burstNot corrected1 of 32

Bench run of a private model of the encoder and decoder: one FEC block of 2,112 bits.

Where the 32 Bits Come From

Forward error correction needs extra bits. But a BASE-R lane has no spare bit rate.

So the FEC sublayer takes bits away first (74.7.3). Every 66-bit block starts with two sync bits.

They are always opposite: 01 or 10. The second bit alone tells data from control.

The FEC sublayer sends one bit in place of the two. Thirty-two blocks then free 32 bit places.

The Transmit Side of BASE-R FECThirty-two 66-bit blocks go in. One FEC block of the same length comes out.
  • Datapath
  • I/O
  • Data
The transmit side of BASE-R FECThirty-two 66-bit blocks go in. One FEC block of the same length comes out. datapath PCS 66-bit blocks,already scrambled datapath Transcode 66 bits to 65: onesync bit less datapath Parity 32 bits for every 32words datapath PN-2112 XOR with a fixedpattern io PMA the same bit rate aswithout FEC 32 × 66 bits32 × 65 bits2,112 bits2,112 bits
  1. datapath PCS66-bit blocks, already scrambled
    • 32 × 66 bits → Transcode
  2. datapath Transcode66 bits to 65: one sync bit less
    • 32 × 65 bits → Parity
  3. datapath Parity32 bits for every 32 words
    • 2,112 bits → PN-2112
  4. datapath PN-2112XOR with a fixed pattern
    • 2,112 bits → PMA
  5. io PMAthe same bit rate as without FEC
  1. Make room

    Each block gives up one sync bit. Thirty-two blocks free 32 bit places.

  2. Fill it

    The 32 free places take the parity of the 2,080 bits before them.

  3. Mark the frame

    The whole block is XORed with PN-2112. The receiver uses that to find where a block starts.

Read the diagram as text
  • PCS: 66-bit blocks, already scrambled
  • Transcode: 66 bits to 65: one sync bit less
  • Parity: 32 bits for every 32 words
  • PN-2112: XOR with a fixed pattern
  • PMA: the same bit rate as without FEC
  • PCS → Transcode: 32 × 66 bits
  • Transcode → Parity: 32 × 65 bits
  • Parity → PN-2112: 2,112 bits
  • PN-2112 → PMA: 2,112 bits

After IEEE Std 802.3-2022 Figures 74-6 and 74-7 (redrawn). The reverse gearbox in front of the transcoder is left out.

Thirty-two blocks of 66 bits are 2,112 bits. One FEC block is 2,112 bits too.

So the lane rate does not change. The sublayer sits between the PCS and the PMA.

It is optional for most PHYs (74.8.1). A 25GBASE-CR, -CR-S, -KR or -KR-S PHY must implement it (74.1).

Clause 74 serves 10GBASE-R, 25GBASE-R, 40GBASE-R and 100GBASE-R PHYs. A multi-lane PHY runs one copy on every PCS lane (74.1).

Transcoding: 66 Bits to 65

The transcoder drops the first sync bit and keeps the second (74.7.3).

It then XORs that bit with data bit 8 of the same block. The result is the transcode bit T (74.7.4.2).

The standard gives the reason for the XOR: DC balance. A long run of data blocks would give a long run of equal T bits.

Data bit 8 comes from the PCS scrambler, so it looks random. The XOR makes T look random too.

BlockSync bits, in sending orderData bit 8Transcode bitSync bits rebuilt
Data block, data bit 8 clear010101
Data block, data bit 8 set011001
Idle control block100010
Control block, data bit 8 set101110

Bench run of a private transcoder model. Every block came back unchanged.

The receiver XORs T with data bit 8 again. That gives the second sync bit, and the first is its inverse.

Marking an Uncorrected Block

One more case: an FEC block that could not be corrected. The decoder may then mark blocks with sync 11 (74.7.4.5.1).

The PCS sees an invalid sync header and treats the block as an error. This marking is an option, set by management (74.8.3).

A 10G or 25G PHY marks 5 of the 32 blocks: the 1st, 9th, 17th, 25th and 32nd.

A 40G or 100G PHY marks all 32. The standard gives the reason: every frame with an error must be reported to the MAC.

FEC blockMarkingPHYBlocks with sync 11Sync headers as sent
12 wrong payload bits: not correctedoffanynone32 of 32
12 wrong payload bits: not correctedon10G or 25G1, 9, 17, 25, 3227 of 32
12 wrong payload bits: not correctedon40G or 100Gall 320 of 32
9 wrong payload bits: correctedon10G or 25Gnone32 of 32

Bench run of a private receiver in lock.

The FEC Block and Its Parity

PartBitsHolds
Word 065transcode bit T0, then 64 payload bits
Word 165T1, then 64 payload bits
...
Word 3165T31, then 64 payload bits
Parity32the remainder of the 2,080 bits before it

After IEEE Std 802.3-2022 Table 74-1, FEC block format (redrawn). Sent from the top row down, each row from its first bit: 32 × 65 + 32 = 2,112 bits.

The code is called (2112,2080): 2,112 bits sent, 2,080 of them information (74.7.1).

The 32 parity bits are a remainder, like a CRC. Take the 2,080 bits as a polynomial and divide by g(x).

ItemValue
Generator g(x)x^32 + x^23 + x^21 + x^11 + x^2 + 1
Parity p(x)x^32 · m(x) mod g(x), where m(x) is the 2,080 information bits
Code rate0.9848 (2,080 / 2,112)
Built froma cyclic code of 42,987 bits, shortened to 2,112

IEEE Std 802.3-2022 74.7.1 and Equations 74-1 to 74-3. The code rate is bench arithmetic.

The code is systematic: the 2,080 bits go out unchanged, and the parity follows them.

The first bit sent is the highest power of x, for the words and for the parity.

Annex 74A has a worked example in that order. The model gives its parity, D96E7685, bit for bit.

A receiver divides all 2,112 bits by g(x). A remainder of zero means the parity matches.

Bursts of up to Eleven Bits

Why this code? A backplane receiver has an equalizer, and one wrong decision there can spoil the next ones.

So errors come in short bursts. The standard names this error propagation as the reason for the code (74.7.1).

The code is guaranteed to correct one burst of up to 11 bits in a block. A decoder must be able to do that (74.7.4.5.1).

What hit the blockDecoderWords wrong after decoding
Clean blockParity matches0 of 32
One wrong bitBurst of 1 bits found at bit 1000, removed0 of 32
11-bit burstBurst of 11 bits found at bit 700, removed0 of 32
11-bit burst into the parityBurst of 11 bits found at bit 2075, removed0 of 32
12-bit burstNot corrected1 of 32
Two wrong bits, 40 apartNot corrected2 of 32

Bench run of a private encoder and decoder.

A burst may fall anywhere, even across the parity. Two wrong bits 40 apart are a burst of 41 bits: too long.

How to decode is left to the design. This model uses error trapping: it slides the remainder back, bit by bit.

When the remainder fits in 11 bits, those bits are the burst, and the slide count gives its place.

StepWhat the decoder doesWhy it works
1Divides all 2,112 bits by g(x)A codeword leaves zero. So the remainder comes from the wrong bits alone
2Looks at the remainderA burst in the last 11 bits of the block is its own remainder
3Divides the remainder by xA burst further up shows multiplied by a power of x. If the lowest bit is set, g(x) is added first; then every term moves down by one
4Counts the stepsAfter j steps the lowest bit of the remainder stands for bit 2,111 - j of the block
5Stops when the lowest bit is set and nothing is set above bit 10These bits are the burst. No two bursts of up to 11 bits share a remainder
6Gives up after 2,112 stepsNo burst of up to 11 bits fits: the block is not corrected

Error trapping in six steps.

Wrong bitsRemainderSteps backPattern found
1,0008388041C1,1111
700 to 71009FF80501,40111111111111
100 and 1108181044C2,00110000000001
2,101, 2,104, 2,105, 2,111000004C1010011000001
0, 3 and 109A800DD62,10110010000001

Bench run of a second private decoder, same rules. Patterns run from first wrong bit to last.

The 12-bit burst was detected: the parity failed and no short burst fitted. Its data went on with the errors in it.

That holds for every burst of 12 or 13 bits. A much longer burst can look like a short burst somewhere else.

This decoder then changes good bits: it mis-corrects. So a longer burst is not always detected.

What hit the blockWhat the search findsDecoderWord bits left wrong
Bits 100 and 111: a burst of 12 bitsNo pattern of up to 11 bits, in 2,112 stepsNot corrected2
Bits 300, 309 and 311: 12 bitsRemainder 04028000, no fitNot corrected3
Bits 321, 330 and 332: the same 12 bits, 21 places onRemainder 04028000, no fitNot corrected3
Wrong parity bits, chosen to misleadA pattern that would start 1 bit in front of bit 0Not corrected0
Bits 1,468, 1,477, 1,479, 1,489 and 1,498One wrong bit: bit 1,500Flips that bit: a mis-correction6

The same model at the edges.

RowWhy
1: 12 bitsThis decoder stops at 11 bits, the least the standard asks for
2 and 3: one pattern, two placesOne remainder for both. Added together, they are g(x) laid on the block: a codeword. No decoder can tell them apart
4: in front of bit 0The block is a shortened codeword: it has no bit in front of bit 0. So no burst of the block has this remainder. The standard does not spell this out
5: five wrong bitsThey left the remainder of one wrong bit. Any decoder that corrects every short burst flips the sixth

Two 32-bit counters record the result: corrected blocks and uncorrected blocks (74.8.4). P11.32 reads them.

PN-2112: A Pattern over Every Block

Before it is sent, the whole block is XORed with a fixed pattern of 2,112 bits: PN-2112 (74.7.4.4.1).

The pattern comes from the scrambler polynomial of P11.24: 1 + x^39 + x^58. Its register starts as 1, 0, 1, 0, ...

The generator starts again for every block. Its first 40 bits are ones, as the table of Annex 74A shows too.

The standard gives two reasons: DC balance, and finding where a block starts.

The second reason needs a picture. For some streams, such as all-zero words, every shifted window is a codeword too.

Stream of two FEC blocksPN-2112Start positions 0 to 31 with good parity
All-zero wordson1 of 32
All-zero wordsoff32 of 32
Random wordson1 of 32
Random wordsoff1 of 32

Bench run of a private model, with its twin no_pn. Only start position 0 is right. Random words give one good start either way.

Without the pattern, every start looks right on such a stream. The receiver could lock one bit off and never notice.

The pattern makes sure that no shifted stream is a codeword again (74.7.4.4.1).

Finding the Block: FEC Lock

The FEC receiver gets no sync headers from the line. So it cannot use the block lock of P11.24.

It uses the parity itself (74.7.4.7). It picks a start, undoes PN-2112, and checks the parity of 2,112 bits.

FEC Block LockOne pass per candidate FEC block of 2,112 bits. n is 4 and m is 8.
  • Control / FSM
  • Control
FEC block lockOne pass per candidate FEC block of 2,112 bits. n is 4 and m is 8. control FEC_LOCK_INIT reset or no signal: lock= false, counts = 0 control TEST_FEC_BLOCK undo PN-2112, checkthe parity control VALID_PARITY good count + 1; badcount = 0 control FEC_BLOCK_LOCK fec_block_lock = true control INVALID_PARITY bad count + 1; goodcount = 0 control SLIP fec_block_lock = false;move one bit parity goodparity bad4 good in a rowfewernot in lock, or 8 badin a rowin lock, fewer than 8counts = 0counts = 0signal back
  1. control FEC_LOCK_INITreset or no signal: lock = false, counts = 0
    • signal back → TEST_FEC_BLOCK
  2. control TEST_FEC_BLOCKundo PN-2112, check the parity
    • parity good → VALID_PARITY
    • parity bad → INVALID_PARITY
  3. control INVALID_PARITYbad count + 1; good count = 0
    • not in lock, or 8 bad in a row → SLIP
    • in lock, fewer than 8 → TEST_FEC_BLOCK
  4. control SLIPfec_block_lock = false; move one bit
    • counts = 0 → TEST_FEC_BLOCK
  5. control VALID_PARITYgood count + 1; bad count = 0
    • 4 good in a row → FEC_BLOCK_LOCK
    • fewer → TEST_FEC_BLOCK
  6. control FEC_BLOCK_LOCKfec_block_lock = true
    • counts = 0 → TEST_FEC_BLOCK
  1. Restart

    A reset or a lost signal clears the lock and both counts.

  2. Hunting

    Out of lock, one bad parity is enough. The candidate start moves by one bit and the test repeats.

  3. Four in a row

    Four blocks in a row with good parity set the lock.

  4. Holding

    In lock, bad blocks are counted in a row. A good block sets the count back to zero.

  5. Losing it

    Eight bad blocks in a row clear the lock. The hunt starts again.

Read the diagram as text
  • FEC_LOCK_INIT: reset or no signal: lock = false, counts = 0
  • TEST_FEC_BLOCK: undo PN-2112, check the parity
  • VALID_PARITY: good count + 1; bad count = 0
  • FEC_BLOCK_LOCK: fec_block_lock = true
  • INVALID_PARITY: bad count + 1; good count = 0
  • SLIP: fec_block_lock = false; move one bit
  • TEST_FEC_BLOCK → VALID_PARITY: parity good
  • TEST_FEC_BLOCK → INVALID_PARITY: parity bad
  • VALID_PARITY → FEC_BLOCK_LOCK: 4 good in a row
  • VALID_PARITY → TEST_FEC_BLOCK: fewer
  • INVALID_PARITY → SLIP: not in lock, or 8 bad in a row
  • INVALID_PARITY → TEST_FEC_BLOCK: in lock, fewer than 8
  • FEC_BLOCK_LOCK → TEST_FEC_BLOCK: counts = 0
  • SLIP → TEST_FEC_BLOCK: counts = 0
  • FEC_LOCK_INIT → TEST_FEC_BLOCK: signal back

After IEEE Std 802.3-2022 Figure 74-11 (redrawn). RESET_CNT is folded into its neighbours.

Parity of the blocks testedLock at the endSlips
3 goodno0
4 goodyes0
3 good, 1 bad, 4 goodyes1
4 good, then 7 badyes0
4 good, then 8 badno1
4 good, 4 bad, 1 good, 7 badyes0

Bench run of the example: eth_fec74_lock on scripted parity results.

Lock needs four good blocks in a row: 8,448 bits. Out of lock, one bad block moves the start by one bit.

There are 2,112 possible starts. So the hunt may need up to 2,111 slips.

Candidate blockStarts at bitParityReceiverLock
10badSkips one bitno
22,113badSkips one bitno
34,226badSkips one bitno
46,339goodCounts a good blockno
58,451goodCounts a good blockno
610,563goodCounts a good blockno
712,675goodCounts a good blockyes

Bench run of a private receiver: three stray bits first.

In lock, seven bad blocks in a row are forgiven. The eighth clears the lock.

A block the decoder repairs still has bad parity. So it counts as a bad block for the lock (74.10.2.3, 74.10.1).

Block in lockDecoderBad parities in a rowLock after it
A clean blockParity matches0yes
Repaired block 1Burst removed1yes
Repaired block 7Burst removed7yes
Repaired block 8Burst removed8no

The same run: eight blocks with two wrong bits each.

Decoding and correction run only in lock. The FEC sublayer reports its lock to the PCS as sync_status (74.7.4.5).

One block lasts 204.8 ns at 10.3125 Gb/s. A decoder must wait for a whole block before it can correct it.

So the standard limits the added delay: 6,144 bit times for a 10GBASE-R FEC, both directions together (74.6).

Try It

The bench runs the lock of Figure 74-11 on scripted parity results. A g is a block with good parity, a b a bad one.

Each line starts from reset and prints the lock and the number of slips at the end.

Three good blocks are not enough. The fourth sets the lock.

Out of lock, one bad block costs a slip. In lock, seven bad blocks in a row cost nothing.

The eighth bad block in a row clears the lock. One good block in between starts the count again.

The readable library holds the lock only. The transcoder, the parity with PN-2112 and the decoder are practice problems.

Add a run of four good, eight bad and four good blocks. Predict the line before you run it.

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

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

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

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

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

// eth_fec74_lock: FEC block lock (Figure 74-11) with n = 4 and m = 8. One call of test() per candidate block.
//   lock    fec_block_lock         slips   how often the candidate start was moved by one bit
module eth_fec74_lock;
  bit lock; int good_cnt, bad_cnt, slips;
  function automatic void reset(); lock = 0; good_cnt = 0; bad_cnt = 0; slips = 0; endfunction
  function automatic void test(input bit parity_good);
    if (parity_good) begin
      good_cnt++; bad_cnt = 0;
      if (good_cnt == 4) begin lock = 1; good_cnt = 0; bad_cnt = 0; end
    end else begin
      bad_cnt++; good_cnt = 0;
      if (bad_cnt == 8 || !lock) begin lock = 0; slips++; good_cnt = 0; bad_cnt = 0; end
    end
  endfunction
endmodule

Check Yourself

Predict the outputThe bench feeds parity results to the lock: g is a good block, b a bad one. What does it print?
`timescale 1ns/1ns
module tb;
  eth_fec74_lock k();
  task automatic run(input string script);
    k.reset();
    for (int i = 0; i < script.len(); i++) k.test(script[i] == "g");
    $display("%s: lock %0d, slips %0d", script, k.lock, k.slips);
  endtask
  initial begin
    run("gggbggggbbb");
    $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_fec.sv
`timescale 1ns/1ns
// eth_fec.sv: the block lock of BASE-R FEC (read-only).

// eth_fec74_lock: FEC block lock (Figure 74-11) with n = 4 and m = 8. One call of test() per candidate block.
//   lock    fec_block_lock         slips   how often the candidate start was moved by one bit
module eth_fec74_lock;
  bit lock; int good_cnt, bad_cnt, slips;
  function automatic void reset(); lock = 0; good_cnt = 0; bad_cnt = 0; slips = 0; endfunction
  function automatic void test(input bit parity_good);
    if (parity_good) begin
      good_cnt++; bad_cnt = 0;
      if (good_cnt == 4) begin lock = 1; good_cnt = 0; bad_cnt = 0; end
    end else begin
      bad_cnt++; good_cnt = 0;
      if (bad_cnt == 8 || !lock) begin lock = 0; slips++; good_cnt = 0; bad_cnt = 0; end
    end
  endfunction
endmodule
ConceptWhat is the transcode bit of a 66-bit block?
ConceptHow long is one BASE-R FEC block?
ConceptWhat must a (2112,2080) decoder be able to correct in one block?
ConceptHow does an FEC receiver find where a block starts?

Common Mistakes

Wrong Send the second sync bit as the transcode bit.

A long run of data blocks then gives a long run of equal bits. The DC balance suffers.

Fix XOR the second sync bit with data bit 8, and undo the same XOR at the receiver.

Wrong Count wrong bits to decide whether a burst can be corrected.

The code corrects by length. Two wrong bits 40 apart are a burst of 41 bits.

Fix Measure from the first wrong bit to the last: up to 11 bits can be corrected.

Wrong Let PN-2112 run on from one block into the next.

The lock test undoes the pattern from its start state at every candidate start. That reason is ours.

Fix Set the generator to its start state before every block.

Wrong Declare FEC lock on the first block with good parity.

A wrong start can pass once by chance.

Fix Ask for four good blocks in a row, as Figure 74-11 does.

Remember

  • BASE-R FEC adds no bit rate. Each block gives up a sync bit, and 32 blocks make room for 32 parity bits.
  • The transcode bit is the second sync bit XOR data bit 8. The receiver undoes the XOR and rebuilds both sync bits.
  • An FEC block is 32 words of 65 bits and 32 parity bits: 2,112 bits.
  • The code corrects one burst of up to 11 bits in a block. A 12- or 13-bit burst is always detected; a much longer one can be mis-corrected.
  • PN-2112 is XORed over every block. The receiver locks on four good parities in a row and lets go after eight bad ones.

Practice

Report a problem with this page