PLLPy: Time-Domain PLL Simulator
A Python library that simulates a phase-locked loop one 10 ps step at a time, locks a 20 MHz reference to 1.2 GHz, and feeds its jitter into a SerDes link model.
Problem
A serial link is only as good as its clock. The clock decides where each bit is sampled, so its timing noise (jitter) sets the bit error rate. That clock comes from a phase-locked loop (PLL), and most PLL tools are either closed-form calculators or full circuit simulators. I wanted something in between: a small Python library where every block is readable code and every node can be plotted.
PLLPy is a capstone team project from the capybara-capstone GitHub organisation, and I did most of the work on it. It simulates a charge-pump PLL one 10 ps time step at a time. In the demo run it locks a 20 MHz reference to 1.2 GHz.
- Time step
- 10 ps
- Reference
- 20 MHz
- Output (locked)
- 1.2 GHz
- Divider N
- 60
What a PLL does
A PLL makes a fast, clean clock from a slow, trusted one. Think of two runners on a track. Frequency is their speed. Phase is the gap between them. The loop watches the gap and nudges the faster runner until the gap stops changing.
Four ideas cover the whole circuit:
- Oscillator. A voltage-controlled oscillator (VCO) is a metronome with a voltage knob. More volts, faster ticks.
- Referee. A phase detector shouts UP when the reference edge comes first and DOWN when the feedback edge does.
- Bucket. A charge pump and loop filter turn those shouts into a control voltage. UP pours charge in. DOWN drains it. The level in the bucket is the knob setting.
- Gears. A divider turns once per N turns of the oscillator. At lock the divided clock matches the reference, so the oscillator runs N times faster.
With the default settings, the lock point follows directly from the numbers: 60 × 20 MHz = 1.2 GHz. The VCO free-runs at 1 GHz and gains 1 GHz per volt. It needs 0.2 V of control to get there.
Architecture
I split the loop into five blocks, one per file. The reference clock is the same Vco class running in clock mode.
A PLL is a feedback loop
The divider feeds the output back until both clocks agree.
The package is 2,733 lines of Python, tests included. Pll in pll.py builds the blocks and runs the sample loop. I put every parameter in Settings. Sweeper re-runs the loop for a list of values. Calculator measures jitter.
The sample loop
I kept the loop free of any solver. Each step passes one number from block to block, and the divider output feeds the detector on the next step.
clk_out = clk._process_and_monitor(1)lpd_out_a, lpd_out_b = lpd._process_and_monitor(clk_out, div_out)lf_out = lf._process(lpd_out_a, lpd_out_b)vco_out = vco._process_and_monitor(lf_out)div_out = div._process_and_monitor(vco_out)At 10 ps per step a 1.2 GHz period is about 83 samples. A 24 µs run is 2.4 million steps in a pure Python loop. That took 10 to 15 s, measured with unpinned numpy/scipy.
The oscillator
Phase is the integral of frequency, and I wrote the VCO to do exactly that: it accumulates phase each step, then takes the sign of a cosine.
self.last += (input_a * self.k_vco_time + self.angular_time)sin_out = cos(self.last + self.white_noise + self.low_freq_noise)out = self.vss + (self.vdd - self.vss) * (sin_out < 0)The settings are in Hz and Hz per volt. I made the class multiply them by 2π·Ts itself, so the user never handles radians.
The key equations
These are textbook relations. The repo does not state them, so the loop numbers below are my derivation, not repo output.
f_vco = f0 + Kvco * Vctrlf_div = f_vco / Nlock: f_div = f_ref -> f_vco = N * f_ref = 60 * 20 MHz = 1.2 GHz Vctrl = (1.2 GHz - 1.0 GHz) / (1 GHz/V) = 0.2 Vwn = sqrt(Icp * Kvco_rad / (2*pi * N * (C + C2)))zeta = R * C * wn / 2With the demo values (25 µA, 1 GHz/V, N = 60, C = 16 pF, C2 = 1.6 pF, R = 8.4 kΩ) I get a natural frequency near 0.78 MHz and a damping ratio near 0.33. That is underdamped, so the step response should ring. It does.
Configuration
Every parameter lives in a Settings object or a JSON file with the same nested keys. The string "None" becomes Python None. This is the file that locks the loop:
{ "name": "UpdatedSimulation", "global_plot_mode": "local", "time_step": 1e-11, "sim_time": 24e-6, "clk": { "k_vco": 20e6, "fo": 0, "white_phase_noise_spectral_density": 0, "low_frequency_phase_noise": 0 }, "vco": { "k_vco": 1e9, "fo": 1000e6 }, "divider": { "n": 60 }, "lf": { "pull_up": 25e-6, "pull_down": 25e-6, "C": 16e-12, "C2": 1.6e-12, "R": 8400, "max_sat": "None", "min_sat": "None" }}The loop filter has three modes, picked by whether R and C2 are set. With neither it is a pure integrator. With R and C it is a first-order RC. With all three it is a second-order lead-lag network, discretised with the bilinear constant k = 2/Ts.
To run the demo I use:
sudo apt install cmakepython -m venv example_venv && source ./example_venv/bin/activatepip install pllpythonpllpython_tutorial # downloads the tutorial folderpython pllpython_tutorial/run_sim.pyResults
Lock transient
The numbers in this section were measured by running the demo settings on unpinned, latest numpy and scipy, not the versions pinned in setup.py. Treat them as a reproduction, not a release benchmark.
The loop locks in about 6 microseconds
Overshoots to 0.354 V, rings, then settles at the 0.2 V setpoint.
| Quantity | Value |
|---|---|
| Locked VCO frequency | 1200.0 MHz |
| Measured reference | 20.00 MHz |
| Control voltage at lock | 0.2000 V |
| Peak overshoot | 0.354 V at 0.68 µs |
| Within 5 % of 0.2 V | after 4.4 µs |
| Within 2 % of 0.2 V | after 6.1 µs |
| Ripple, 18 to 24 µs | about 5.7 nV peak to peak |
The overshoot matches the underdamped estimate above. The settling thresholds are a choice, not a spec. The ripple figure says the loop is quiet once locked, but a noiseless reference is doing much of that work.
Jitter
Calculator.calculate_jitter times every output transition, normalises by the mean half-period and reports the RMS. Over 12 to 24 µs the demo gives 0.0113, about 1.1 % of a half-period.
From jitter to bit error ratio
Two scripts push the PLL into a link model built on serdespy. run_sim_cdr_example.py samples a PRBS13 NRZ stream with the PLL output edges. run_sim_transmit_clock_example.py drives a four-level PAM transmitter through an FIR, a channel, a CTLE, an FFE and a DFE, then counts errors.
pll_jitter = calc.calculate_jitter(np.array(pll.output), start_time=12e-6, stop_time=24e-6)[0]TX.gaussian_jitter(stdev_div_UI=pll_jitter)The PLL jitter is a fraction of a 1.2 GHz half-period. serdespy reads it as a fraction of a unit interval at 26.56 GHz Nyquist. Those are different periods. It is a demonstration of the plumbing, not a calibrated link jitter. The scripts print bits sent, errors and BER. No result is stored in the repo, so I quote none.
Known limitations
I list these because they matter to anyone who clones the repo.
- Stock defaults do not lock with unpinned numpy/scipy.
run_sim.pybuilds plainSettings(...), with the JSON line commented out. The default reference clock carries white phase noise of 3e-10. There the VCO drifted to about 3.2 GHz and the printed jitter was 0.035. Loadingdemo_settings.json, which zeroes that noise, locks. - The first-order RC filter does not pull in. With
Rset andC2unset, the updateV = alpha*V + (1-alpha)*Ihas no R factor. At 25 µA the control voltage stayed at 0 V and the VCO stayed at 1000 MHz. - The VCO unit tests fail with unpinned numpy/scipy. Both score about 50 % similarity against a 97 % threshold. The stored target toggles every 1667 samples, the code every 369. The test settings look like rad/s (
6.2832e9), while the code now expects Hz. This is a unit mismatch between stored data and code, which I infer rather than confirm. The phase-detector, loop-filter and divider tests passed, some only through anoron a cross-correlation score. - CI does not run the tests. The workflow checks out the code and sets up Python 3.10. It never installs the package or calls pytest.
- Smaller quirks. Sweeping the divider
nreports “does not exist”. The “std_dev” return of the jitter function is a mean absolute deviation. The phase-noise plot uses a natural log labelled as dB.
Next steps
PLLPy locks and rings the way the theory predicts, and its blocks are small enough to read in one sitting. The honest state is a working simulator with rough edges. Next I would:
- Regenerate the VCO reference data in Hz and make the unit tests green.
- Add install, pytest and a lock-check step to CI.
- Fix the R-only filter branch and make the lock-ready settings the default.
- Run a Sweeper study of jitter against
RandKvco, and add an eye diagram with and without PLL jitter. - Pass jitter to serdespy in UI units from the real link rate.