Clock and Data Recovery, and the Eye

Explain how a receiver finds the sampling point from the data edges, and read an eye diagram.

You Will Be Able To

  • Explain how early and late votes move the sampling clock to the bit centre.
  • Predict when and how a receiver slips for a given frequency offset with the loop stopped.
  • Explain why long runs of equal bits make clock recovery harder.
  • Read the height and width of an eye diagram.
Lesson
P10.6
Time
35 min
Runs on
Your browser
Check
5 questions

A fast serial link sends no clock. The receiver has its own clock, close to the transmitter's rate but never equal to it. Without correction, the two drift apart.

Transmitter clockLoopBits skipped or repeated in 100,000 UIFirst slip
300 ppm fasterTracking0none
300 ppm fasterStopped30UI 1,667
300 ppm slowerStopped30UI 1,667

Bench runs of the library's bang-bang loop on PRBS7 data. The tracking loop steps 1/64 UI on every transition, a bench value.

Where to Sample: The Eye

The receiver must decide each bit at one instant. The best instant is the middle of the bit, where the signal is furthest from the threshold.

An eye diagram shows where that is. It overlays every bit of a long stream on one unit interval (UI), the time of one bit. TI describes it this way in SLYY057B, page 6.

Eye diagramNRZ Eye, Short Channel10 Gb/s PRBS7 with a 1000 mV swing, through a short RC-ladder channel.
-600 mV-400 mV-200 mV0200 mV400 mV600 mVv(rx)-0.500.51.01.5Time in UI (1 UI = 100 ps)992 mV1.00 UI-600 mV-400 mV-200 mV0200 mV400 mV600 mVv(rx)-0.500.51.01.5Time in UI (1 UI = 100 ps)992 mV1.00 UI
Read the eye as a table
EyeHeightWidth
Eye -500 mV to 500 mV, 1 V apart991.8 mV1.00 UI

Best sampling phase: 1.00 UI (100 ps) after each symbol is launched (the dashed line). Measured over 122 symbols after a warm-up of 10 UI. The height is the gap between the received levels at that phase; the width counts the phases, in steps of 0.02 UI, where the eye stays open.

Simulated with ngspice 42; folded and measured at build time. The channel is a bench model, not a real board.

The eye height is the vertical opening at the best instant: here 992 mV of the 1000 mV swing. The eye width is the span of instants at which every bit is still decided right: 1.00 UI.

A lossy channel closes the eye. On the long bench channel the height falls to 466 mV and the width to 0.82 UI. Lesson P10.9 opens it again.

Jitter is the spread of the zero crossings in time (TI SLYY057B, page 7). It narrows the eye from the sides. It is a timing error, not a voltage error.

Early or Late: Two Samples per Bit

A clock and data recovery circuit (CDR) finds the middle of the bit from the data itself. The common kind takes two samples per bit.

Two Samples per BitThe receiver takes a data sample in the middle of each bit. It takes an edge sample half a UI later, between two bits.
  • Neutral
  • Datapath
  • Clock / reset
  • Data
  • Control
Two samples per bitThe receiver takes a data sample in the middle of each bit. It takes an edge sample half a UI later, between two bits. neutral Data sample bit n neutral Edge sample between n and n + 1 neutral Data sample bit n + 1 datapath Vote early or late clock reset Sampling clock phase moves matches which bit?one step
  1. neutral Data samplebit n
    • Edge sample
  2. neutral Data samplebit n + 1
  3. neutral Edge samplebetween n and n + 1
    • Data sample
    • matches which bit? → Vote
  4. datapath Voteearly or late
    • one step → Sampling clock
  5. clock reset Sampling clockphase moves
  1. Two data samples

    Two data samples in a row differ: there was a transition between them. Equal samples carry no timing news.

  2. Look at the edge sample

    The edge sample sits where the transition should be. It shows which side of the transition it landed on.

  3. Early

    The edge sample still shows bit n. The transition had not happened yet, so the clock is early. Move it later.

  4. Late

    The edge sample already shows bit n + 1, so the clock is late. Move it earlier.

  5. Step

    The bench moves the clock 1/64 UI per vote. A real loop counts votes first and steps less often.

Read the diagram as text
  • Data sample: bit n
  • Edge sample: between n and n + 1
  • Data sample: bit n + 1
  • Vote: early or late
  • Sampling clock: phase moves
  • Data sample → Edge sample
  • Edge sample → Data sample
  • Edge sample → Vote: matches which bit?
  • Vote → Sampling clock: one step

TI SPRABC1 §8.2.5: the clock is early when the edge sample matches the first data sample. It is late when it matches the second.

TransientThe Sampling Point Walks to the Bit CentreThe receiver starts 0.45 UI late. Each transition moves it 1/64 UI toward the centre.
-10%010%20%30%40%50%distance from the bit centre020406080100Unit intervals since the startlock: within 5% of a UIbit centrelock at UI 62-10%010%20%30%40%50%distance from the bit centre050100Unit intervals since the startlock: within 5% of a UIbit centrelock at UI 62
Read the plot as a table
Unit intervals since the startdistance from the bit centre
045%
2038.75%
4024.69%
607.5%
801.25%
1001.25%

Bench run at 0 ppm on PRBS7. The flat stretches are runs of equal bits: no transition, no vote, no step.

The bench loop started 0.45 UI late. It reached 5% of a UI from the centre at UI 62 and stayed there. That moment is lock.

TI's receiver does not step on every vote. It takes a majority vote over each window of twelve bits. When the count of majorities reaches a threshold, it steps 1/48 UI (SPRABC1 §8.2.5.1).

Its fastest first-order setting tracks ±434 ppm (Table 9). TI recommends its second-order loop, which also tracks a frequency offset, for links with two crystals (§8.2.5).

Frequency Offset and Slips

Two clocks never run at exactly the same rate. The offset is given in parts per million (ppm). A transmitter 300 ppm fast sends 300 extra bits in every million.

USB 3.2 lets each reference clock be off by ±300 ppm. Spread spectrum clocking adds more: the rate swings down by up to 5000 ppm at 30 to 33 kHz (§6.4.3, Table 6-17).

Stop the loop and the sampling point drifts by the offset each UI. After 0.5 / |ppm| UI it has drifted half a bit and lands in the next or the previous bit. That is a slip.

TransmitterUI runSlipsKindFirst slip0.5 / |ppm|
300 ppm faster100,00030skipped bitsUI 1,667UI 1,667
300 ppm slower100,00030repeated bitsUI 1,667UI 1,667
1000 ppm faster10,00010skipped bitsUI 500UI 500
5000 ppm faster2,00010skipped bitsUI 100UI 100
5000 ppm slower2,00010repeated bitsUI 100UI 100

Bench runs with cdr.freeze = 1 and the first sample at the bit centre. The last column is the rule, worked by hand.

A faster transmitter makes the receiver skip a bit. A slower one makes it read a bit twice. Either way, every later bit is shifted by one.

With the loop running, each transition pulls the sampling point back. The loop holds lock as long as it can step faster than the clocks drift.

TransmitterLock (UI)SlipsWorst distance from the centre after lock
Same frequency6200.044 UI
300 ppm faster6300.048 UI
300 ppm slower6100.041 UI
5000 ppm faster501 (UI 13)0.062 UI
5000 ppm slower3900.062 UI

Bench runs on PRBS7, 100,000 UI each (20,000 at the same frequency). The first sample is 0.45 UI from the centre.

At 5000 ppm faster the bench loop skipped one bit at UI 13. It was still pulling in from 0.45 UI off, and the drift won for a moment. After lock it held within 0.062 UI.

A Second-Order Loop Tracks the Frequency

The bench loop above steps on every transition, which is fast but noisy. TI's loop is slower: one 1/48 UI step per majority vote over 12 bits, then three windows of rest (§8.2.5.1). That caps it at 1/48 UI per 48 UI, ±434 ppm (Table 9).

A first-order loop corrects only the phase. To follow a steady offset it must keep stepping, and the phase sits wherever the steps just keep up.

A second-order loop adds an integrator: a frequency term that grows while the votes lean one way. Once it matches the offset, the phase needs no more steps. TI recommends it for links with two crystals (§8.2.5, §8.2.5.2).

Transmitter offsetFirst order: slips, worst errorSecond order: slips, worst errorOffset the integrator learned
+200 ppm0, 0.032 UI0, 0.022 UI221 ppm
+400 ppm0, 0.042 UI0, 0.023 UI396 ppm
-600 ppm6, 0.500 UI0, 0.023 UI-625 ppm
+2000 ppm40, 0.500 UI0, 0.415 UI1983 ppm

Bench runs: TI-style voting, 1/48 UI steps, three windows of rest; the integrator moves 2^-17 UI per UI per window. 6,000 UI to settle, then 20,000 UI measured.

At 600 ppm slower, beyond the first-order limit, the first-order loop slipped 6 times. The second-order loop learned -625 ppm and held within 0.023 UI.

At 2000 ppm it still held, but its error swung up to 0.415 UI. The integrator gain sets how fast it learns and how much it hunts; that choice is the loop design.

Transitions Feed the Loop

The loop only learns from transitions. During a run of equal bits it gets no votes, and the sampling point drifts by the offset every UI.

Data at 5000 ppm slowerSlips in 100,000 UIWorst distance from the centre
PRBS700.062 UI
Runs of 5 equal bits00.048 UI
Runs of 66 equal bits00.348 UI
Runs of 200 equal bits250 repeated bits0.500 UI

Bench runs. Each run of equal bits is followed by the same number of alternating bits. At 200 ppm slower, runs of 66 stay within 0.029 UI.

At 5000 ppm a run of 66 bits drifts 66 × 0.005 = 0.33 UI. The bench saw 0.348 UI, about two thirds of the way to a slip. A run of 200 drifts a whole bit.

This is why the line codes of P10.3 and P10.4 matter. 8b/10b never sends more than five equal bits. A block code relies on its scrambler, which only makes long runs rare.

TI says the same of its loop (SPRABC1 §8.2.5.1). Its tracking rate falls when fewer than one bit in twelve has a transition.

When the Loop Is Wrong

LoopSlips in 30,000 UIWorst distance from the centre after lock
Clean, 300 ppm faster00.048 UI
cdr.freeze = 1: never moves9drifts across the whole bit
cdr.votes_inverted = 1: moves away from the edge192never locks
cdr.step_too_big = 1: 1/4 UI steps00.252 UI
Clean, 5000 ppm slower00.062 UI
cdr.step_too_small = 1: 1/128 UI steps, 5000 ppm slower58slips

Bench runs on PRBS7, the first sample 0.45 UI from the centre. Each fault is a switch on the library's loop.

A step that is too big locks fast but never settles: the sampling point jumps 0.252 UI around the centre. A step that is too small cannot keep up with 5000 ppm and slipped 58 times.

A loop with its votes inverted runs away from the centre. It walks to the edge of the eye and slips again and again.

P10.7 measures how much jitter a loop follows, how much the eye must absorb, and how jitter becomes a bit error ratio.

Try It

The first run tracks a transmitter 300 ppm fast. It locks after about 60 UI and never slips.

The second run stops the loop. The first slip comes at 0.5 / 0.0003 = 1,667 UI, then one every 3,333 UI.

Change 300.0 to -300.0 in both runs. The frozen loop now repeats bits instead of skipping them, and slips_skip reads 0.

RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: tracking: locked at UI 63, 0 slips, worst error after lock 0.048 UI frozen: 6 slips (6 skipped), the first at UI 1667
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: 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

Predict the outputThe loop is stopped and the first sample is at the bit centre. The transmitter is 1000 ppm faster. The run lasts 3,000 UI. The program prints the UI of the first slip and the number of slips. What does it print?
`timescale 1ns/1ns
module tb;
  hs_cdr cdr();
  initial begin
    cdr.freeze = 1;                            // the loop never moves
    cdr.cdr_run(1000.0, 3000, 0.5);            // +1000 ppm, 3000 UI, first sample at the bit centre
    $display("%0d %0d", cdr.first_slip, cdr.slips);
    $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: 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
ConceptTwo data samples in a row are 0 then 1. The edge sample between them reads 0. What should the loop do?
ConceptWhy does a code with long runs of equal bits need a tighter ppm budget?
ConceptThe loop is stopped and the transmitter is faster than the receiver. What happens to the data?
ConceptWhat does the width of an eye diagram measure?

Common Mistakes

Wrong Say that the CDR locks to the reference clock.

The reference sets only the nominal rate; the phase of the bits, and any offset, must come from the data edges.

Fix The CDR locks to the incoming data. The reference clock only gives it a starting rate.

Wrong Budget only the ±300 ppm of the crystals.

USB 3.2 adds spread spectrum clocking down to -5000 ppm, so the receiver must track much more than 600 ppm.

Fix Size the loop for the total offset, spread spectrum included, at the lowest transition density the code allows.

Wrong Count a slip as one bit error.

A slip removes or repeats a bit, so every later bit is shifted and every symbol boundary moves.

Fix Treat a slip as a loss of alignment. The receiver must find the comma or the block boundary again.

Wrong Ignore the run length when choosing a code for a link with a large offset.

With no transitions the loop gets no votes; runs of 200 equal bits slipped 250 times at 5000 ppm on the bench.

Fix Bound the run length with the line code, or make long runs rare with a scrambler and check the margin.

Remember

  • A CDR takes a data sample and an edge sample per bit. The edge sample says whether the clock is early or late.
  • Only transitions give votes. Long runs leave the loop blind while the clocks drift.
  • Frequency offset is in ppm. A stopped loop slips after 0.5 / |ppm| UI: skips for a faster transmitter, repeats for a slower one.
  • A slip shifts every later bit, so the receiver must realign.
  • The eye overlays every bit on one UI. Its height is the voltage margin, its width the timing margin.

Practice

Report a problem with this page