Thursday, October 22, 2026

Measurements on Simulation Output - in Xschem

Keeping the schematic tool out of the optimizer loop: where do waveform measurements actually happen?

I'm setting up a circuit optimization in our xschem fork. The topology is fixed; only device sizes and resistor values change from one iteration to the next. So the optimizer can edit the SPICE netlist directly, run ngspice, and never touch the schematic editor. That leaves one question: how does the optimizer get the measurements back? The phase margin, rise time and crossing times we define in ASE-L (our ADE-L-style simulation environment) have to come from somewhere.

My first idea was a menu item, Tools > Write measures.py, that turns the ASE-L setup into a standalone Python script. Before building it, I asked how measurements are computed today. The answer changed the design.

Two kinds of measurement, computed in two different places

1. Measurements ngspice understands. Rows defined under Outputs > Measurements are written into the netlist as ngspice meas lines. ngspice computes them during the run and writes plain name = value lines to a .meas file next to the raw file. The schematic tool only reads that file back. These rows cover:

  • delay between two edges (TRIG … TARG);
  • the value of a signal at a time, or when another signal crosses a level;
  • the time of the n-th rising, falling or either-way crossing of a value;
  • average, RMS, min, max, peak-to-peak and integral over a window;
  • unwrapped phase, Fourier/THD, and expressions over other measurements.

2. Measurements only the Calculator knows. ngspice has no built-in idea of a Cadence-style riseTime, slewRate, settlingTime, overshoot, dutyCycle, frequency, bandwidth, gainBwProd, gainMargin or phaseMargin. The same goes for cross as the Calculator defines it (every crossing, or a chosen one, with its own edge rules), and for any ASE-L Outputs expression built from these. You could approximate some of them with chains of meas lines, but they would not agree with the Calculator to the last digit. That matters when the optimizer is chasing a spec you also check by eye in ASE-L.

For this second kind, nothing outside the editor is involved. After a run, the xschem process reads the raw file into memory and evaluates every Outputs row itself.

The architecture, in one picture

                       ┌──────────────────── xschem process ────────────────────┐
 ASE-L Outputs pane ──►│ result_probe ──► calc::eval_texts  (plans each row)    │
 Calculator window  ──►│                     │                                  │
                       │                     ▼                                  │
                       │   calc::riseTime / cross / phaseMargin …   (Tcl)       │
                       │        │ build a column        ▲ samples                │
                       │        ▼                       │                        │
                       │   ┌──── `xschem raw …` commands (scheduler.c) ───────┐ │
                       │   │ raw read    raw add    raw values    raw index   │ │
                       │   └────┬───────────┬──────────────▲──────────────────┘ │
                       │        ▼           ▼              │              (C)   │
                       │   raw_read    expression engine (plot_raw_custom_data) │
                       │   (save.c) ──► the raw file as arrays of doubles in RAM│
                       └───────▲────────────────────────────────────────────────┘
                               │ reads
   netlist ──► ngspice ──► <cell>_ase.raw
                    └────► <cell>_ase.meas   (ngspice's own `meas` results)

There are three layers in two languages:

  • A raw-file reader in C (src/save.c). It loads ngspice's output into memory, splitting AC data into magnitude, phase, real and imaginary columns.
  • An expression engine in C, also in save.c. It does point-by-point maths that builds new columns, such as v(out)/v(in), db20() and integ().
  • The measurement verbs in Tcl (src/calculator.tcl). They ask the engine for a column, pull its samples into Tcl, and do the crossing, interpolation and margin arithmetic there. The only way they reach the data is through the xschem raw … commands.

Why not a standalone Python script?

A Python measures.py that recomputes everything would be a second copy of the raw reader, the expression engine and about 4,600 lines of measurement code. This code has edge cases that already took several careful fixes in our own tree:

  • a threshold that lands exactly on a rail inventing a crossing;
  • a rise-time edge pairing up with the wrong transition;
  • stateful functions such as integ() leaking across sweep boundaries.

A second copy drifts, and then the optimizer and the GUI disagree about whether a design meets spec.

Why not call just the measurement part?

That was my next question, and today the honest answer is that you can't. The Tcl measurement code only reaches the data through C commands compiled into the editor, and those keep their data in the editor's global state. Carving it out is possible:

  1. Build the raw reader, the expression engine and the raw command into a small Tcl extension.
  2. Move the measurement procs into a file that loads without any GUI.
  3. Load both from Python through the window-less Tcl interpreter that ships with Python (tkinter.Tcl()).

The editor would compile the same files, so the numbers would still match.

But it is a refactor of the file every simulation result passes through, and one measurement makes it unnecessary for now. Headless xschem starts and exits in about 0.06 seconds, using about 21 MB (three runs, no display). Any simulation worth optimizing takes far longer. So "launch the editor headless" really is "run the measurement part" plus about 60 ms. It reads the raw file once; it loads no GUI, schematic or display.

The plan

A menu item, Tools > Export for optimizer, writes three things:

  1. The netlist ASE-L would run, with the design variables marked for substitution. It keeps the write lines that produce the raw file and the meas block.
  2. A manifest saying where the raw and .meas files land, and where each measurement comes from.
  3. A small measures.py that the optimizer calls after each ngspice run. It returns one dictionary of name → value:
    • ngspice-native rows are parsed from the .meas file, with no xschem involved;
    • Calculator-only rows are evaluated by xschem --nogui --pipe --script measures.tcl <raw>, which runs the same code the Outputs pane uses.

The export also lists which rows need the Calculator, so anything ngspice can compute can be moved over.

If 60 ms per iteration ever matters, or the optimizer has to run where xschem isn't installed, the measurement layer can be carved out later behind the same measures.py interface, and the optimizer won't notice.

If you're doing this with an AI coding assistant

The useful move was asking where the numbers come from before asking for the script. Prompts that got me there:

  • "Before you build it: how are these measurements computed today? Is the waveform loaded into memory by the tool, or does an external script produce the number?"
  • "Which measurements can the simulator compute itself, and which exist only in our Calculator?"
  • "Where is that code, which files? And why can't I call just that part?"
  • "Measure the cost of the simple option before proposing a refactor." The 60 ms figure settled the design in one line.
  • "Draw the architecture as an ASCII block diagram."

No comments:

Post a Comment

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...