100BASE-X: 4B/5B
Fast Ethernet sends five bits for every four. The extra bit buys clock edges and marks for the start and the end of a frame. It also shows up errors. See a frame become a stream.
You Will Be Able To
- Read the 4B/5B table: sixteen Data code-groups and the ones with a role of their own.
- Wrap a frame into a stream: /J/K/ at the start, /T/R/ at the end, /I/ between.
- Say how the receiver finds the start of a stream in a row of bits.
- Tell a data error, a false carrier and a premature end apart.
- Lesson
- P11.43
- Time
- 35 min
- Runs on
- Your browser
- Check
- 5 questions
Sixteen nibbles need sixteen codes. Five bits give 32, so half of them are spare.
The sixteen chosen for data never leave the line flat for long: at most 3 zeros in a row.
- Cycle 0
Between frames the line carries /I/: all ones.
- Cycle 1
The frame starts. The first two preamble nibbles are replaced by /J/ and /K/.
- Cycle 3
Every other nibble becomes its Data code-group, the rest of the preamble too.
- Cycle 5
After the last FCS nibble come /T/ and /R/. They take up the start of the gap.
- Cycle 7
Idle again.
Our own drawing after IEEE Std 802.3-2022 Figure 24-5 and 24.2.2.2.
Raw Nibbles Would Starve the Receiver
- Channel / handshake
- Datapath
- I/O
- Data
- Nibble
The MAC hands over four bits at a time on the MII.
- Code-group
The PCS looks the nibble up in a table and gets five bits. It adds the delimiters around a frame.
- Bits
The PMA sends the five bits one by one. A 1 is a change of level, a 0 is none.
- Line
The PMD shapes the signal for its medium: twisted pair or fibre.
Read the diagram as text
- MII: nibbles, 25 million a second
- PCS: 4B/5B: a code-group for each nibble
- PMA: bits in a row, as NRZI
- PMD: the line signal for the cable
- MII → PCS: 4 bits
- PCS → PMA: 5 bits
- PMA → PMD: 125 million bits a second
Our own drawing after IEEE Std 802.3-2022 Figures 24-4 and 24-6.
A receiver gets no clock wire. It must find the bit times from the changes in the signal.
The PMA of 100BASE-X sends NRZI: a 1 is a change of level, a 0 is no change (1.4.417, 24.3).
So a long run of zeros is a flat line, and the receiver loses its place.
A frame may hold any data, also a thousand zeros in a row. The PCS must break them up.
It does so with a block code: every nibble of four bits becomes a code-group of five (24.2.2.1).
Sixteen Data Code-Groups
| Nibble | Code-group | Nibble | Code-group |
|---|---|---|---|
| 0 | 11110 | 8 | 10010 |
| 1 | 01001 | 9 | 10011 |
| 2 | 10100 | A | 10110 |
| 3 | 10101 | B | 10111 |
| 4 | 01010 | C | 11010 |
| 5 | 01011 | D | 11011 |
| 6 | 01110 | E | 11100 |
| 7 | 01111 | F | 11101 |
IEEE Std 802.3-2022 Table 24-1, the sixteen Data code-groups. A code-group is written bit 4 first, and bit 4 is sent first.
Every Data code-group has at least 2 ones. Two in a row hold at most 3 zeros side by side.
So the line changes at least every fourth bit time, whatever the data is.
The price is speed on the line. 100 Mb/s of data are 125 million code-bits a second (24.2.2.1).
The table comes from FDDI, an older 100 Mb/s network. Fast Ethernet reused its line parts.
The Other Sixteen
Sixteen of the 32 code-groups are data. That leaves 16.
| Name | Code-group | Role |
|---|---|---|
| /I/ | 11111 | Idle: sent between streams |
| /J/ | 11000 | Start-of-Stream delimiter, part 1 |
| /K/ | 10001 | Start-of-Stream delimiter, part 2 |
| /T/ | 01101 | End-of-Stream delimiter, part 1 |
| /R/ | 00111 | End-of-Stream delimiter, part 2 |
| /H/ | 00100 | Transmit error: sent on purpose to spoil a frame |
| /P/ | 00000 | Sleep, only with Energy-Efficient Ethernet. Invalid without it |
| /V/ | nine more | Invalid: never sent |
IEEE Std 802.3-2022 Table 24-1. Nine more are invalid: 00001, 00010, 00011, 00101 and 00110. Also 01000, 01100, 10000 and 11001.
Six have a role. They mark the idle line, the start, the end and a deliberate error.
The rest are invalid. A receiver that sees one knows that a bit went wrong.
That is a small error check for free, long before the FCS.
A Stream: /J/K/, Data, /T/R/
The PCS wraps every frame into a stream (24.2.2.2).
The pair /J/K/ is the Start-of-Stream delimiter. It takes the place of the first preamble octet.
The receiver puts that octet back, so the MAC notices nothing.
The pair /T/R/ is the End-of-Stream delimiter. It follows the last nibble of the FCS.
It is sent in the time the MAC counts as the gap between frames.
Between streams the line carries /I/, all ones: a change in every bit time.
The Transmit Process
The Transmit process reads three MII signals and picks one code-group for each nibble time.
| The MII shows | The PCS sends |
|---|---|
| TX_EN 0 | /I/ |
| TX_EN rises | /J/, then /K/. The two nibbles of those times are not used |
| TX_EN 1, TX_ER 0 | the Data code-group of TXD |
| TX_EN 1, TX_ER 1 | /H/ |
| TX_EN falls | /T/, then /R/, then /I/ |
IEEE Std 802.3-2022 24.2.4.2 and Figure 24-8, without the LPI states.
| On the MII | Code-groups on the line |
|---|---|
| a frame of eight nibbles | /J/ /K/ 5 D 1 2 A 3 /T/ /R/ /I/ |
| TX_ER in the sixth nibble | /J/ /K/ 5 D 1 /H/ A 3 /T/ /R/ /I/ |
| TX_ER in the first nibble | /J/ /K/ /H/ D 1 2 A 3 /T/ /R/ /I/ |
Bench run of a private model of Figure 24-8. The frame is the eight nibbles 5 5 5 D 1 2 A 3.
With TX_ER the PCS sends /H/. The far end sees an invalid code-group and raises RX_ER.
A repeater uses this to pass on an error it received (24.2.2.1.7).
Finding the Start in a Row of Bits
A code-group goes out bit 4 first (24.2.2.4). The receiver gets a row of bits with no marks.
It keeps the last ten bits in a window. On an idle line the window is all ones.
Two zeros that are not neighbours mean a carrier: somebody is sending (24.2.4.4.1).
At that moment the window must hold /I/ and /J/. Five bits later it must hold /J/ and /K/.
Then the receiver is aligned: from now on every fifth bit ends a code-group.
No row of Data code-groups holds the ten bits of /J/K/, at any offset. That is why it can be found blind.
The Receive Process
After /J/K/ the receiver raises RX_DV and hands two nibbles 5 to the MII: the octet that /J/K/ replaced.
Then it is in the state RECEIVE. For each code-group it looks at that one and at the next.
| The window holds | State | The MII shows |
|---|---|---|
| /T/ then /R/ | END OF STREAM | RX_DV falls |
| /I/ then /I/ | PREMATURE END | RX_ER, then RX_DV falls |
| a Data code-group first | DATA | its nibble on RXD |
| anything else | DATA ERROR | RX_ER |
IEEE Std 802.3-2022 Figure 24-11, the four ways out of RECEIVE. The window is the current code-group and the next one.
/T/ alone is not an end. Without /R/ behind it, it is a data error.
One /I/ alone is not an end either. Only two in a row are.
False Carrier and Premature End
| Stream, and what the bench counts | The MII |
|---|---|
| A clean stream: nothing | · 5 5 5 D 1 2 · · · · |
| /H/ for a nibble: 1 data error | · 5 5 5 ER 1 2 · · · · |
| /T/ with no /R/: 1 data error | · 5 5 5 ER 1 2 · · · · |
| No /T/R/ at the end: 1 premature end | · 5 5 5 D 1 ER · · · · |
| /J/ with no /K/: 1 false carrier | · FC FC FC FC · · · 5 5 7 |
Bench run of a private model of Figure 24-11 on aligned code-groups. The MII column has one entry for each code-group. A digit is a nibble with RX_DV. ER is RX_ER with RX_DV. FC is the false carrier code: RX_ER and RXD 1110 without RX_DV. A dot is idle.
A false carrier is activity that does not start with /J/K/.
The receiver reports it with a code of its own: RX_ER and RXD 1110 while RX_DV stays low (24.2.4.4.2).
It then waits for ten ones in a row before it looks for a new start.
A premature end is a stream that falls back to idle with no /T/R/.
The receiver raises RX_ER for it, so the MAC drops the frame.
Below the PCS, and After 100 Mb/s
Two PHYs share the PCS and PMA of 100BASE-X. One is 100BASE-TX on twisted pair, the other 100BASE-FX on fibre.
The twisted-pair PMD is taken over from FDDI by reference (25.2).
It scrambles the bits and sends them as MLT-3, a code with three levels (25.5.1). This course runs neither.
The next step in speed dropped 4B/5B. At 1 Gb/s, 1000BASE-X uses 8B/10B (P11.20).
On twisted pair, 1000BASE-T uses five levels on four pairs at once, called 4D-PAM5 (40.1.3).
Try It
eth_5b_line is a model of the bit side of the receiver. send puts one code-group on the line, bit 4 first.
The bench sends two /I/, then /J/K/, three Data code-groups, /T/R/ and idle.
At bit 15 the window holds two zeros of /J/ that are not neighbours: a carrier.
At bit 20 the window holds /J/ and /K/. The receiver is aligned.
From then on a code-group is ready every five bits. It is printed one group late, because the window holds two.
The three Data code-groups print as -. Look them up in the table: they are 5, D and 2.
The code table is not in the readable library. It is the practice problem.
Change /K/ to another code-group. Predict what the bench prints at bit 20.
bit 15: a carrier, and the window holds /I/J/ bit 20: /J/K/ found, the receiver is aligned bit 20: code-group 11000 J bit 25: code-group 10001 K bit 30: code-group 01011 - bit 35: code-group 11011 - bit 40: code-group 10100 - bit 45: code-group 01101 T bit 45: /T/R/, the end of the streamRead 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_4b5b.sv
`timescale 1ns/1ns
// eth_4b5b.sv: the code-bit side of the 100BASE-X PCS (read-only). A bench model of Receive Bits (Figure 24-10)
// and of the states of Figure 24-11 that come before alignment. The code table itself is not here.
// eth_5b_line: code-groups as bits, and the hunt for the start of a stream.
// I, J, K, T, R, H the six code-groups with a role of their own (Table 24-1)
// reset() the window full of ones, no carrier, not aligned
// bit_in(b) one code-bit from the PMA; the first bit of a code-group is its bit 4 (24.2.2.4)
// rx_bits the last ten code-bits; bit 0 is the newest (24.2.3.2)
// state 0 idle, 1 /I/J/ seen, 2 aligned, 3 false carrier
// got 1 after the bit that completes a code-group; rx_bits[9:5] is then the group to use
// name(cg) "I", "J", "K", "T", "R", "H", or "-" for every other code-group
module eth_5b_line;
localparam [4:0] I = 5'b11111, J = 5'b11000, K = 5'b10001, T = 5'b01101, R = 5'b00111, H = 5'b00100;
reg [9:0] rx_bits = 10'h3ff;
int state = 0, phase = 0;
bit got = 0;
function automatic void reset();
rx_bits = 10'h3ff; state = 0; phase = 0; got = 0;
endfunction
function automatic void bit_in(input bit b);
rx_bits = {rx_bits[8:0], b};
got = 0;
case (state)
0: if (!rx_bits[0] && rx_bits[9:2] != 8'hff) // two zeros that are not neighbours: a carrier
state = (rx_bits == {I, J}) ? 1 : 3;
1: if (rx_bits[9:5] == J) begin // five bits later the window holds /J/ and its follower
if (rx_bits[4:0] == K) begin state = 2; phase = 0; got = 1; end
else state = 3;
end
2: begin phase = (phase + 1) % 5; got = (phase == 0); end
default: if (rx_bits == {I, I}) state = 0; // a false carrier ends with ten ones
endcase
endfunction
function automatic void unalign(); // the end of a stream: hunt again
rx_bits = 10'h3ff; state = 0; phase = 0; got = 0;
endfunction
function automatic string name(input [4:0] cg);
case (cg)
I: return "I";
J: return "J";
K: return "K";
T: return "T";
R: return "R";
H: return "H";
default: return "-";
endcase
endfunction
endmodule
Check Yourself
Common Mistakes
Wrong Send a code-group bit 0 first.
The receiver hunts for /J/K/ in the order bit 4 to bit 0.
Fix Bit 4 goes on the line first.
Wrong End the stream as soon as /T/ is decoded.
/T/ counts only with /R/ behind it.
Fix Test the pair. /T/ alone is a data error.
Wrong Pass /J/ and /K/ to the MAC as two more nibbles.
They took the place of the first preamble octet.
Fix Hand the MII two nibbles 5 for them.
Wrong Treat every code-group that is not data as an error.
Six of them have a role: idle, the two delimiters and /H/.
Fix Check the delimiters first, then the data, and call the rest an error.
Remember
- 4B/5B sends every nibble as five bits. Sixteen code-groups are data, six have a role, the rest are invalid.
- The Data code-groups keep the line changing: at most three zeros in a row.
- A stream is /J/K/, Data code-groups and /T/R/. /J/K/ takes the place of the first preamble octet.
- The receiver finds /J/K/ in the raw bits and is aligned from there.
- RX_ER reports an invalid code-group or a stream that stops early. A false carrier has a code of its own.