SerDes Architecture
Name every block of a serializer/deserializer and say what each one adds or removes.
You Will Be Able To
- Name the blocks of a SerDes transmitter and receiver in order.
- Explain why a receiver needs a comma aligner after the deserializer.
- Choose the block that handles a clock difference, a swapped pair or a lost bit.
- Predict what a receiver delivers when one block is missing or wrong.
- Lesson
- P10.2
- Time
- 30 min
- Runs on
- Your browser
- Check
- 5 questions
A new board brings up its link. Training passes and the decoder reports no errors at all. Yet 446 of 600 received bytes are wrong. The two wires of one pair were swapped in the layout.
| Receiver | Wrong | Code errors | Disparity errors |
|---|---|---|---|
| Clean lane | 0 of 600 codes | 0 | 0 |
| P and N swapped on the board | 446 of 600 bytes | 0 | 0 |
| Swapped, and the receiver inverts every bit | 0 of 600 bytes | 0 | 0 |
Bench run: 600 symbols, random bytes with a K28.5 every 50 symbols. The middle row is wrong, yet nothing in the decoder complains.
One Byte, Six Forms
A SerDes (serializer/deserializer) moves bytes across a link one bit at a time. On the way a byte changes its form several times.
- Neutral
- Datapath
- Clock / reset
- I/O
- Channel / handshake
- Data
- Clock
- Encode
The byte becomes a ten-bit code with enough edges and a balanced line (lesson P10.3).
- Serialize
The serializer sends the ten bits one after another. The PLL multiplies a slow reference clock up to the bit rate.
- Drive
The driver puts the bits on a differential pair. Its equalizer pre-shapes them for the loss ahead (lesson P10.9).
Read the diagram as text
- Byte from the link layer: 8 bits, parallel
- 8b/10b encoder: 10-bit code
- PLL: bit clock from a reference clock
- Serializer: one bit per UI
- Driver and equalizer: a differential voltage
- Channel: board, cable, connector
- Byte from the link layer → 8b/10b encoder
- 8b/10b encoder → Serializer: 10 bits at once
- PLL → Serializer: clock
- Serializer → Driver and equalizer: bit a first
- Driver and equalizer → Channel
PIPE Rev 7.1 Figure 4-4 shows this chain: 8b/10b encoding, parallel to serial, the differential driver.
The transmitter knows where each code starts, because it made the code.
| Where | Form of the data | The letter S in the bench run |
|---|---|---|
| Link layer | A byte | 53h |
| After the encoder | A ten-bit code | 1100100101 |
| On the wire | Ten bits in a row, bit a first, with no gap | one UI each |
| After the deserializer | Ten-bit words, cut from an unknown start | two halves of two words |
| After the aligner | Whole ten-bit codes | 1100100101 |
| After the decoder | A byte and two flags | 53h, no error |
Bench run of the example below. The receiver started 7 bits into an idle pattern, so its first cut was wrong until the comma arrived.
The Receiver Must Find Its Symbols
- I/O
- Clock / reset
- Datapath
- Storage
- Neutral
- Data
- Clock
- Recover the clock
The CDR finds the bit timing from the edges in the data (lesson P10.6). It makes the receive clock.
- Cut into words
The deserializer groups every ten bits. It started at whatever bit came first, so the cut is usually in the wrong place.
- Align
The aligner watches for the comma inside K28.5. It moves the cut so that the comma starts a word.
- Change clocks
The recovered clock and the local clock differ slightly. The elastic buffer adds or drops skip symbols between them.
- Decode
The decoder turns each code back into a byte. It flags a code error or a disparity error.
Read the diagram as text
- Receive front end: equalizer, slicer: bits
- Clock and data recovery: a clock from the data edges
- Deserializer: 10 bits, cut anywhere
- Comma aligner: moves the cut to K28.5
- Elastic buffer: recovered clock to local clock
- 8b/10b decoder: byte and two error flags
- Byte to the link layer: on the local clock
- Receive front end → Deserializer: bits
- Clock and data recovery → Deserializer: clock
- Deserializer → Comma aligner: 10-bit words
- Comma aligner → Elastic buffer: whole codes
- Elastic buffer → 8b/10b decoder
- 8b/10b decoder → Byte to the link layer
PIPE Rev 7.1 Figure 4-6 shows the same chain. Clock and data recovery comes first, then serial to parallel, K28.5 detection, the elastic buffer and 8b/10b decode.
The receiver knows neither. It gets a stream of bits with no gaps and no marks. The deserializer simply cuts every ten bits, starting wherever it started.
The comma aligner fixes the cut. It slides a seven-bit window along the bits. When it sees the comma 0011111 or 1100000, it knows a comma symbol starts there. Links use K28.5 for this: USB 3.2 Table 6-2 names it COM, the symbol for alignment.
| Bits before the first symbol | With the aligner: wrong codes | Without it: codes that fail to decode |
|---|---|---|
| 0 | 0 of 200 | 0 of 200 |
| 1 | 0 of 200 | 40 of 200 |
| 2 | 0 of 200 | 44 of 200 |
| 3 | 0 of 200 | 46 of 200 |
| 4 | 0 of 200 | 31 of 200 |
| 5 | 0 of 200 | 45 of 200 |
| 6 | 0 of 200 | 35 of 200 |
| 7 | 0 of 200 | 42 of 200 |
| 8 | 0 of 200 | 41 of 200 |
| 9 | 0 of 200 | 45 of 200 |
| 10 | 0 of 200 | 0 of 201 |
| 11 | 0 of 200 | 45 of 201 |
| 12 | 0 of 200 | 36 of 201 |
| 13 | 0 of 200 | 40 of 201 |
| 14 | 0 of 200 | 32 of 201 |
| 15 | 0 of 200 | 60 of 201 |
Bench run: 200 symbols per row. The aligner reads every row right. Without it, only 0 and 10 bits of lead-in happen to put the cut in the right place.
Notice the right column. A wrong cut still gives mostly valid codes: only 31 to 60 of about 200 fail to decode. The rest decode to wrong bytes with no error. The decoder cannot tell you that the cut is wrong.
Two Clocks Meet in the Elastic Buffer
The receiver now has two clocks. The CDR's recovered clock follows the far transmitter. The rest of the chip runs on a local clock.
The two are close, but never equal. PIPE puts an elastic buffer between them to absorb the difference in frequency (§8.14). It adds or removes skip symbols, which carry no data, so it never overflows or runs dry.
What Goes Wrong in Each Block
| Receiver | Result | Why |
|---|---|---|
Realigns on every comma pattern (ln.align_any_7 = 1) | 582 of 600 codes wrong, 54 realigns | K28.7 followed by D12.0 forms a comma across the boundary. |
P and N swapped (ln.swap_pn = 1) | 446 of 600 bytes wrong, no flag | Every bit is inverted. The inverse of a code is also a code. |
One bit lost on the line (ln.slip_at = 2003) | 4 code errors, then aligned again | Every later cut is one bit off until the next comma. |
Bench runs of the same lane. The rule "4 code errors in 16 codes drop the alignment" is a bench value.
A swapped pair is easy to miss. Inverting K28.5 at RD- gives 1100000101, which is K28.5 at RD+. So the aligner still finds its comma.
The bench inverted all 512 data codes. 112 still decode to the same byte, and the other 400 decode to another valid symbol. Not one becomes an invalid code. That is why the hook raised no flag.
USB 3.2 tells the receiver how to spot it. During training, its D10.2 symbols arrive as D21.5, and the receiver must then invert the received bits (§6.4.2.1). PIPE gives the PHY a control for this, RxPolarity (§8.17).
One lost bit is different. Every later cut is one bit off, so codes fail. The bench receiver drops its alignment after four code errors in sixteen codes. It aligns again at the next comma.
Where the Blocks Live
| Layer | Blocks | Where the data is |
|---|---|---|
| MAC (link side) | Training state machine, lane deskew, scrambling | Bytes |
| PCS (physical coding sublayer) | 8b/10b or 128b/130b coding, elastic buffer, receiver detection | Codes and bytes |
| PMA (physical media attachment) | Analog buffers, serializer and deserializer | Bits and a 10-bit interface |
PIPE Rev 7.1 Figure 2-1 (original PIPE). In the SerDes form of PIPE (§4.2) the PHY keeps only the serializer and deserializer. The coding and the elastic buffer move into the MAC.
Vendor transceivers make the same blocks options. AMD's transceiver wizard offers comma detection and alignment, clock correction and channel bonding as features to switch on (PG182, feature summary).
Try It
The receiver took in 77 bits: 7 idle bits and 70 code bits. It aligned once, at the comma of K28.5, and delivered all seven codes.
The first received code is the comma itself. The idle bits before it never leave the aligner.
Add ln.no_align = 1; before ln.lane_start(). The receiver now cuts from its first bit. The bytes come out as noise and the second code is not a valid code at all.
TX K28.5 code 0011111010 TX byte 53 S code 1100100101 TX byte 65 e code 1010010011 TX byte 72 r code 0100110011 TX byte 44 D code 0010100101 TX byte 65 e code 1010011100 TX byte 73 s code 1100101100 receiver: 77 bits in, aligned 1 time(s), 7 codes out RX code 0011111010 K bc RX code 1100100101 byte 53 RX code 1010010011 byte 65 RX code 0100110011 byte 72 RX code 0010100101 byte 44 RX code 1010011100 byte 65 RX code 1100101100 byte 73Read 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: hs_lane.sv
`timescale 1ns/1ns
// hs_lane.sv: one lane at bit level, and a behavioural clock and data recovery loop (read-only).
// hs_lane: lane_tx(code) serialises a 10-bit code (bit 9 first) into the channel; the channel adds
// start_bits idle bits (1010..., random phase) before the first symbol (lane_start), flips bits at ber_ppm, drops the bit
// numbered slip_at, and inverts every bit when swap_pn is set (P and N swapped on the board).
// The receiver shifts bits in, finds a comma (0011111 or 1100000 as the first 7 bits of a symbol),
// aligns to it, and queues every 10-bit code after that: rx_avail(), rx_pop(), or the task
// lane_rx(code, aligned). Its own decoder watches the codes: 4 code errors in 16 codes drop the
// alignment (a bench rule, not a specification's), and the next comma aligns again. The decoder takes
// its starting disparity from the first symbol after alignment (the column that symbol is valid in),
// as USB 3.2 does. Counters: commas, aligns, realigns, flips, code_errs, disp_errs.
// The clean aligner aligns once and then ignores commas; align_any_7 is the common design that
// realigns on every comma, which the false comma inside K28.7 + D3/D11/D12/D19/D20/D28/K28 breaks.
// Faults: no_align (never aligns: codes are cut from wherever the receiver started),
// align_any_7 (aligns again on every comma pattern, even one that crosses a symbol boundary),
// polarity_inverted (the receiver inverts every bit it takes in).
module hs_lane;
int start_bits = 0, ber_ppm = 0, slip_at = -1; bit swap_pn = 0;
bit no_align = 0, align_any_7 = 0, polarity_inverted = 0, trace = 0;
hs_rng rng(); hs_8b10b c8();
int tx_bits = 0, rx_bits = 0, flips = 0, commas = 0, aligns = 0, realigns = 0, code_errs = 0, disp_errs = 0, codes = 0;
bit aligned = 0, fresh = 0; int left = 0; bit [9:0] sh = 10'd0;
bit [9:0] q [0:255]; int qh = 0, qc = 0;
bit errs [0:15]; int ei = 0, recent = 0;
function automatic void lane_start();
tx_bits = 0; rx_bits = 0; flips = 0; commas = 0; aligns = 0; realigns = 0; code_errs = 0; disp_errs = 0; codes = 0;
aligned = 0; left = 10; sh = 10'd0; qh = 0; qc = 0; ei = 0; recent = 0; c8.rd_rx = 0;
for (int i = 0; i < 16; i++) errs[i] = 0;
begin reg b0; b0 = rng.bit1(); for (int i = 0; i < start_bits; i++) rx_bit(b0 ^ i[0]); end
endfunction
function automatic void emit();
reg [11:0] r; reg e;
if (qc < 256) begin q[(qh + qc) % 256] = sh; qc++; end
codes++;
if (fresh) begin r = c8.decode(sh, 1'b0); c8.rd_rx = (r[10] && !r[9]) ? 1'b1 : 1'b0; fresh = 0; end
r = c8.decode(sh, c8.rd_rx); c8.rd_rx = r[11]; e = r[9];
if (e) code_errs++; else if (r[10]) disp_errs++;
recent = recent - errs[ei] + e; errs[ei] = e; ei = (ei + 1) % 16;
if (recent >= 4 && aligned && !no_align) begin
aligned = 0; recent = 0; for (int i = 0; i < 16; i++) errs[i] = 0;
if (trace) $display("r%0d LOST alignment", rx_bits);
end
endfunction
function automatic void rx_bit(input bit b);
reg c;
b = b ^ polarity_inverted; sh = {sh[8:0], b}; rx_bits++; left--;
c = (sh[6:0] == 7'b0011111 || sh[6:0] == 7'b1100000);
if (c) commas++;
// a comma that ends here started 7 bits ago: its symbol ends 3 bits from now
if (c && !no_align && (!aligned || (align_any_7 && left != 3))) begin
if (aligned) realigns++; else aligns++;
aligned = 1; left = 3; fresh = 1;
if (trace) $display("r%0d ALIGN", rx_bits);
return;
end
if (left == 0) begin left = 10; if (aligned || no_align) emit(); end
endfunction
function automatic void chan(input bit b);
int i; i = tx_bits; tx_bits++;
if (i == slip_at) return;
b = b ^ swap_pn;
if (ber_ppm > 0 && rng.chance_ppm(ber_ppm)) begin b = !b; flips++; end
rx_bit(b);
endfunction
function automatic void lane_tx(input [9:0] code); for (int i = 9; i >= 0; i--) chan(code[i]); endfunction
function automatic int rx_avail(); return qc; endfunction
function automatic [9:0] rx_pop(); reg [9:0] c; c = q[qh]; if (qc > 0) begin qh = (qh + 1) % 256; qc--; end return c; endfunction
task automatic lane_rx(output [9:0] code, output bit valid); valid = (qc > 0); code = rx_pop(); endtask
endmodule
// hs_cdr: a bang-bang (early/late) CDR on real-valued time, in receiver UI. ppm is the transmitter's
// frequency offset: its bit period is 1 / (1 + ppm * 1e-6) UI, so +ppm is a faster transmitter. Each UI
// the receiver takes a data sample at t and an edge sample at t + 0.5; on a transition it moves t by
// phase_step: later when the edge sample still shows the old bit (the receiver clock is early, in TI
// SPRABC1's words), earlier otherwise. A slip is a step of the sample index other than one bit:
// slips_skip counts skipped bits (a faster transmitter), slips_repeat repeated ones (a slower one).
// phase_step = 1/64 UI on every transition is a bench value: TI SPRABC1 §8.2.5 / Table 9 describes 1/48 UI
// steps after a vote over 12 bits (about ±868 ppm of tracking), so "tracks +5000 ppm" is this loop's
// property, not a real receiver's.
// cdr_run(ppm, n_ui, phase0): runs n_ui UI from sampling time phase0 (0.5 is the centre of bit 0);
// cdr_start(ppm, phase0) then cdr_step() once per UI does the same one UI at a time (err_now, ui_now)
// pattern: 0 PRBS7, 1 runs of runlen equal bits (then runlen alternating bits), 2 a clock (1010...)
// results: lock_ui (first UI within 0.05 UI of the centre; -1 if never), slips, first_slip, maxerr
// (worst distance from the centre after lock, in UI)
// Faults: freeze (the loop never moves), votes_inverted (moves away from the edge), step_too_big (1/4 UI),
// step_too_small (1/128 UI: too slow for 5000 ppm with PRBS7's transition density).
// A negative phase0 is treated as 0.
module hs_cdr;
real phase_step = 1.0 / 64.0; int pattern = 0, runlen = 5;
bit freeze = 0, votes_inverted = 0, step_too_big = 0, step_too_small = 0;
real ttx, t, maxerr; int lock_ui, slips, first_slip, slips_skip, slips_repeat;
bit prbs [0:126]; bit built = 0;
function automatic void build(); reg [31:0] s; s = 32'h7f; for (int i = 0; i < 127; i++) begin prbs[i] = s[6]; s = hs_pkg::prbs_next(s, 7, 6); end built = 1; endfunction
function automatic bit bitat(input int k);
int m;
if (k < 0) return 1'b0;
if (pattern == 2) return k[0];
if (pattern == 1) begin m = k % (2 * runlen); return (m < runlen) ? 1'b1 : m[0]; end
if (!built) build();
return prbs[k % 127];
endfunction
function automatic int bitno(input real tt); return (tt < 0.0) ? -1 : $rtoi(tt / ttx); endfunction
real st_run, err_now; int ui_now;
// cdr_start + cdr_step run the loop one UI at a time (a bench that watches every UI); cdr_run is both.
function automatic void cdr_start(input real ppm, input real phase0);
ttx = 1.0 / (1.0 + ppm * 1.0e-6); t = (phase0 < 0.0) ? 0.0 : phase0; slips = 0; slips_skip = 0; slips_repeat = 0; lock_ui = -1; first_slip = -1; maxerr = 0.0;
st_run = step_too_big ? 0.25 : step_too_small ? 1.0 / 128.0 : phase_step; ui_now = 0; err_now = 0.0;
endfunction
// One UI. Returns how many bits the sample index moved: 1, or 2 for a skip, 0 for a repeat.
// err_now is then the distance of the sampling point from the bit centre, in UI (0 to 0.5).
function automatic int cdr_step();
int k0, k1, ke; reg d0, d1, e, early; real frac;
ui_now++;
k0 = bitno(t); ke = bitno(t + 0.5); t = t + 1.0; k1 = bitno(t);
d0 = bitat(k0); e = bitat(ke); d1 = bitat(k1);
early = (e === d0) ^ votes_inverted;
if (!freeze && d0 !== d1) t = t + (early ? st_run : -st_run);
if (k1 - k0 != 1) begin slips++; if (k1 - k0 > 1) slips_skip++; else slips_repeat++; if (first_slip < 0) first_slip = ui_now; end
frac = t / ttx - $rtoi(t / ttx); err_now = frac - 0.5; if (err_now < 0.0) err_now = -err_now;
if (lock_ui < 0 && err_now < 0.05) lock_ui = ui_now;
if (lock_ui >= 0 && err_now > maxerr) maxerr = err_now;
return k1 - k0;
endfunction
task automatic cdr_run(input real ppm, input int n_ui, input real phase0);
cdr_start(ppm, phase0);
for (int i = 1; i <= n_ui; i++) void'(cdr_step());
endtask
endmodule
Check Yourself
Common Mistakes
Wrong Assume the deserializer knows where a symbol starts.
It cuts every ten bits from whatever bit arrived first, so the cut is right only by luck.
Fix Put a comma aligner after the deserializer, and send a comma before any data.
Wrong Realign on every comma pattern the window sees, with no guard against K28.7.
K28.7 followed by some symbols forms a comma across the boundary: the bench twin realigned 54 times.
Fix Realign only after repeated decode errors, as the bench does. Or realign on a misplaced comma but ignore one that straddles a K28.7, as TI does (§8.2.8.1).
Wrong Expect a swapped P/N pair to show up as decode errors.
The inverse of every valid code is also a valid code, so the decoder sees nothing wrong.
Fix Check a known training symbol, such as D10.2, and invert the received bits when it arrives as D21.5.
Wrong Place the elastic buffer before the CDR.
Before the CDR there is no recovered clock and no symbol, only an analog signal.
Fix Put the elastic buffer after alignment, where whole symbols cross from the recovered clock to the local clock.
Remember
- A byte becomes a code, then line bits, then a voltage. The receiver undoes each step in reverse.
- The deserializer cuts at an unknown place. The comma aligner moves the cut to the comma in K28.5.
- The PLL makes the transmit clock and the CDR the receive clock. The elastic buffer moves data from the receive clock to the local clock.
- A wrong cut or a swapped pair mostly gives valid codes with wrong bytes. The decoder flags alone cannot find them.
- PIPE splits the blocks into PMA, PCS and MAC. The SerDes form of PIPE leaves only the serializer and deserializer in the PHY.