Showing posts with label Monte Carlo. Show all posts
Showing posts with label Monte Carlo. Show all posts

Wednesday, November 11, 2026

In Case it Helps - Randomized Results in TT with SKY130nm and ngspice

Why my sky130 bandgap gave a different answer every run

One test in our xschem fork failed two of our last four full regression runs. On its own it always passed, so for weeks it was filed as "the known flaky one". This week we found the cause, and it is not a test problem at all. Every ordinary run of a tt_mm sky130 bench is a fresh Monte Carlo trial.

What we saw

The symptom

The test simulates the bandgap testbench that ships with the open_pdks sky130 examples. Sometimes ngspice ran for about 13 seconds, exited with code 1, and wrote no results file. The failing check moved from run to run.

It looked like a load problem, because it only failed inside a full test run. It wasn't.

The evidence

We copied the exact deck the test used and ran it with ngspice directly, 24 times. One run failed:

doAnalyses: TRAN:  Timestep too small; time = 1.63211e-07,
  timestep = 5e-20: trouble with node "v4#branch"
tran simulation(s) aborted

The surprise was earlier in the output. ngspice prints its starting solution before the transient begins, and no two runs printed the same one. Most started near 0 V, with noise from 1e-12 down to 1e-53. The failing run started at a non-physical point, with one node at −1.96×108 V.

Same deck, same binary, a different answer every time. Forcing ngspice onto one thread changed nothing, so it wasn't multithreading.

The cause

tt_mm means "typical, with mismatch"

The bench's corner symbol says corner=tt_mm. In the sky130 model library, that section sets MC_MM_SWITCH=1, so every transistor draws a random mismatch offset when the netlist is loaded. Plain tt sets it to 0.

ngspice has no separate Monte Carlo analysis. Monte Carlo in ngspice means "run again with a different seed". These runs set no seed, so ngspice used a different one every time. Every run was a new random sample of the circuit.

Seven of the bundled sky130 example benches use tt_mm, including the two bandgaps, the comparator, lvnand and srlatch.

The proof

Adding one line before the .control block:

.option seed=1

Six concurrent runs then found the same starting solution, and their results files matched byte for byte once ngspice's date line was removed.

Is ngspice broken?

No. ngspice and the sky130 models are doing what they were designed to do. The bundled sky130_mismatch example even calls setseed 12 when it wants a repeatable run.

The bug is the gap between that design and what a Cadence user expects. In Spectre, mismatch only applies inside a Monte Carlo analysis, so an ordinary run gives the same numbers every time. Our ADE-style front end, ASE-L, handed ngspice the deck without pinning a seed. So pressing Run on a tt_mm bench quietly ran one Monte Carlo trial instead of a repeatable simulation. On this bandgap, about one trial in 24 also failed to converge.

What we're changing

Ordinary runs get a fixed seed

ASE-L will pin the random seed for every run that isn't a Monte Carlo run, so a tt_mm bench gives the same answer each time you press Run.

Keep one subtlety in mind. A fixed seed gives you one particular mismatch sample, repeatably. It is not the mismatch-free typical device you get from tt. If you want typical numbers, use tt. If you want to see mismatch, run Monte Carlo.

If you use ngspice with sky130 today

Check your corner

If your corner ends in _mm and you aren't running Monte Carlo, either switch to the plain corner or put .option seed=1 in your deck. Otherwise two runs of the same schematic can disagree, and you'll chase a change you never made.

If you work with Claude Code: prompts that found this

A flaky test hides its cause until someone keeps the evidence. These are the requests that got us from "known flaky" to a root cause:

  • "Don't re-run it again. Find out why the simulator exits with error 1."
  • "Copy the exact deck the test simulates, and run ngspice on it directly, many times, keeping the output of any run that fails."
  • "Compare a failing run's output with a passing one, and tell me where they first differ."
  • "Before fixing anything, give me the background: where did this come from, and is the tool itself broken?"

The third one cracked it. The difference was before the transient even started, and that pointed straight at the models instead of at the test.

Thursday, August 14, 2025

If You Want to Add Value to Open EDA - chatGPT's Advice

Analog · Automation · Open Flow

EDA Automation for Analog — a 90-Day MVP You Can Start Today

Build the missing glue: auto testbenches, corner/MC orchestration, lint, and clean HTML reports — open-source first, portable to Virtuoso/Spectre later.

xschem ngspice/Xyce Magic KLayout netgen Python · Jinja2 · YAML · pandas

Strategy in One Paragraph

Use free/open tools to prove value on Sky130/GF180: generate testbenches from a small YAML “design contract,” run corners & Monte Carlo with reproducible seeds, lint schematic/layout for analog foot-guns, and publish a one-page HTML dashboard leadership can skim. Keep clean adapters so you can swap ngspice→Spectre and xschem→Virtuoso later with minimal rework.

Tool Stack (Free)

Schematic
xschem
Simulation
ngspice or Xyce
Layout
Magic + KLayout
LVS/DRC
netgen (LVS), Magic/KLayout (DRC)
PDKs
SkyWater 130, GF180 (open)
Glue
Python 3.11+, Jinja2, YAML, pandas, HTML

Milestones & Deliverables

Weeks 1–2 v0.1

  • Repo layout: tools/, examples/, reports/, docs/
  • gen_tb.py — Jinja SPICE TB generator
  • run_corners.py — PVT runner (real if ngspice found; mock otherwise)
  • report_html.py — JSON → HTML report
  • schematic_lint.py — basic xschem lint (stub)

Success: An inverter example renders a shareable HTML report.

Weeks 3–4 v0.2

  • YAML-driven corners (VDD, temp, tt/ss/ff when available)
  • Parameter sweeps: cload, W/L, bias current
  • Measurements: tpHL/tpLH, tr/tf, static current
  • Artifacts: summary.json, results.csv, sortable HTML

Success: 6+ corners with one command, neat table output.

Weeks 5–6 MC

  • Monte Carlo with deterministic seeds
  • Stats & yield: mean/σ/min/max, pass/fail vs. spec
  • HTML distributions + yield bar

Success: 200-run MC of comparator delay across 3 corners.

Weeks 7–8 Lint

  • Schematic lint: unlabeled nets, P/G rails, floating nodes, naming consistency
  • Layout lint (KLayout): symmetry pairs, matched areas, orientation checks
  • Checklist emitted into HTML

Success: Actionable lint before LVS/DRC, fewer late surprises.

Weeks 9–10 LVS/DRC

  • Wrap netgen (LVS): device/net mismatches, pin naming
  • Wrap Magic/KLayout (DRC): total errors, top rules, coordinates
  • One-page sign-off snapshot

Success: PVT + MC + LVS/DRC summary on one screen.

Weeks 11–12 v0.4

  • Cell pack: Inverter, Comparator, Two-stage Op-Amp (or simple LDO stage)
  • Each with ready configs, corners, MC, lint, LVS/DRC
  • One-Click: run_all.py examples/opamp/config.yaml

Success: A non-expert can clone & run and get a professional report.

Example: Minimal Workflow

# 1) Generate a testbench from YAML
python tools/gen_tb.py examples/inverter/config.yaml

# 2) Run corners (uses ngspice if present; otherwise mock)
python tools/run_corners.py examples/inverter/config.yaml --out reports/inverter

# 3) Build a shareable HTML report
python tools/report_html.py reports/inverter/summary.json --out reports/inverter/report.html

What to Automate (High Value)

  • Auto Testbench Generation: from a small YAML “design contract” (pins, supplies, stimulus recipe) → SPICE with measures.
  • Corner/MC Orchestration: reproducible seeds, resumable runs, cache, CSV+JSON artifacts, CI-friendly.
  • Lint: schematic (nets, names, floating nodes) and layout (symmetry/matching heuristics) for early catches.
  • HTML Reports: specs vs targets, pass/fail heatmap, MC yield, LVS/DRC counts, links to artifacts.

Porting to Cadence/Synopsys Later

Design with adapters so swapping tools is trivial:

  • Simulator: NgspiceAdapter ⇄ SpectreAdapter (CLI args, netlist dialect, measures/Ocean)
  • Schematic: xschem parse ⇄ Virtuoso via SKILL/Ocean or exported netlists
  • Layout Checks: KLayout scripts stay useful even in Virtuoso flows; later swap to Calibre/PVS wrappers

The value isn’t replacing Virtuoso — it’s automating everything around it.

Validation & First “Customers”

  • Who first: small analog consultancies & university labs (hate manual PVT/MC + reporting).
  • Pilot: offer your stack free to 1–2 teams for feedback and anonymized screenshots.
  • Measure: time saved/iteration, bugs caught by lint pre-LVS/DRC, earlier MC yield visibility.

Risks & Mitigations

  • Open PDK limits: use Sky130/GF180 to prove flow; keep adapters ready for proprietary PDKs.
  • Layout heuristics: start with simple symmetry checks; evolve toward device-graph matching.
  • Vendor lock-in: strict adapter boundaries to avoid rewrites when adding Spectre/Calibre.

Quick Start Checklist

  • Python 3.11+, optional ngspice on PATH
  • YAML config describing pins, supplies, stimulus, corners, MC
  • Run: generate TB → run corners/MC → build HTML
  • Iterate: add lint and LVS/DRC wrappers, then ship one-click reports

Want an expanded YAML schema, a first KLayout symmetry macro, or a SKILL/Ocean mapping sheet to Virtuoso? Say the word and I’ll include them in a follow-up post.

Getting a Useful VcXsrv Crash Dump

Winner of the first Nobel Prize in Physics ❯ tell me how to do the VcXsrv shortcut that I currently have in my Startup folder so that we get...