Appendix C
ngspice and Xschem Cheat Sheet
The commands this course uses, in one place. Everything on this page was run in IIC-OSIC-TOOLS 2026.10, with ngspice-47 and Xschem 3.4.8; the Xschem keys are those of its built-in key list. The ngspice manual has the full syntax of every command.
Contents
C.1 Running a simulation
| Task | Command |
|---|---|
| Run a netlist and exit | ngspice -b tb.cir |
| Run a netlist and keep the prompt | ngspice tb.cir, then plot v(out), print v(out), quit |
| Open a schematic | xschem tb.sch & |
| Netlist and simulate in Xschem | the Netlist and Simulate buttons, or the keys n and s |
| Netlist and simulate a lab testbench from the shell | ../../sim.sh tb.sch in labs/labN/<pdk>/ (the labs) |
| Run a lab testbench over corners and temperatures | ../../corners.py tb.sch --corners tt,ss,ff --temps=-40,27,125 |
C.2 The netlist
| Line | Meaning |
|---|---|
R1 a b 10k, C1 a b 1p, L1 a b 1n |
resistor, capacitor, inductor |
V1 a b dc 0.6 ac 1 |
voltage source with a DC value and a 1 V amplitude for ac |
V1 a b sin(0.6 10m 1meg) |
sine: offset, amplitude, frequency |
V1 a b pulse(0 1.2 10n 1n 1n 50n 100n) |
pulse: low, high, delay, rise, fall, width, period |
V1 a b pwl(0 0 1u 0 1.001u 1.2) |
piecewise linear: time-value pairs |
I1 a b 10u |
current source, flowing from a through the source to b |
E1 p n cp cn 1000, G1 p n cp cn 1m |
voltage- and current-controlled sources (gain, transconductance) |
X1 n1 n2 ... subname par=val |
subcircuit instance; every PDK device is one (Appendix B) |
.lib file section |
read one section (a corner) of a model library |
.include file |
read a whole file |
.param rd=10k then RD vdd out {rd} |
named parameter, used in braces |
.options seed=1307 |
simulator option: here the random seed of a Monte Carlo run |
.temp 125 |
temperature of the run |
.save v(out) @n.xm1.nsg13_lv_nmos[gm] |
vectors to keep; operating-point values must be saved to be swept |
.control … .endc |
the script ngspice runs after reading the circuit |
.end |
end of the netlist |
Node 0 (Xschem writes GND) is ground. Values take the suffixes f p n u m k meg g t; note meg, because m is milli. In SKY130, transistor W and L are in micrometres (W=5 L=0.5); in SG13G2 they are in metres (W=5u L=0.5u).
C.3 Analyses
| Analysis | Example | Result vectors |
|---|---|---|
| Operating point | op |
node voltages, source currents i(vdd), @dev[param] |
| DC sweep | dc Vin 0 1.2 10m |
everything as a function of the swept source |
| Small-signal AC | ac dec 20 1k 10G |
complex v(out); the source’s ac 1 makes v(out) the gain |
| Transient | tran 0.1n 3u |
waveforms; the values are the output time step and the stop time |
| Noise | noise v(out) Vin dec 20 1k 10G |
plot noise1: onoise_spectrum, inoise_spectrum (V/√Hz); plot noise2: onoise_total, inoise_total (V rms) |
| Poles and zeros | pz out 0 in 0 vol pz, then print all |
pole(1), pole(2), …, zero(1), … in rad/s, as real,imag |
Each analysis creates a new plot (op1, dc1, ac1, tran1, noise1, noise2, …). The newest plot is the current one; setplot alone lists them, setplot ac1 changes the current plot, and noise1.onoise_spectrum reads a vector from another plot without switching.
C.4 Commands in the control block
| Command | Example |
|---|---|
| Vector arithmetic | let a3 = a0 - 3, let g = deriv(v(out)), let t = -v(a)/v(b) |
| Magnitude and phase | mag(x), db(x) (= vdb(out) for a node), ph(x) and cph(x) in radians (cph is continuous), real(x), imag(x) |
| Phase in degrees | let phd = 180/pi*cph(v(out)) |
| Print values | print v(out) @n.xm1.nsg13_lv_nmos[gm]; one point of a vector: print x[40] |
| Print with text | echo "vout = $&vq" ($& inserts the value of vector vq) |
| Change a source or element | alter Vin dc = 0.5, alter RD 5k, alter Vg dc = $&vq |
Change a .param |
alterparam rd=20k followed by reset |
| Loop | foreach w 2u 5u 10u … alterparam wn=$w … reset … op … end |
| Temperature | option temp=125 before the analysis, or .temp in the netlist |
| Save results | write tb.raw (current plot); set appendwrite adds later plots to the same file; set filetype=ascii writes text |
| Save columns of text | wrdata out.txt v(out) v(q) |
| Spectrum of a transient | linearize v(out) then fft v(out); the result is a new spectrum plot (sp1, sp2, …) of complex values against frequency |
| Plot (interactive) | plot v(out) v(in), plot vdb(out) xlog |
C.5 Measurements
meas works on dc, ac, tran and sp (FFT) plots. It accepts vectors, not expressions: compute an expression with let first, then measure the vector. Results are new vectors and can feed later measurements.
| Measurement | Example |
|---|---|
| Value at a point | meas ac a0 find vdb(out) at=1k |
| Where a signal crosses a level | meas ac f3db when vdb(out)=a3; meas dc vm when v(out)=0.6 |
| Value of one signal where another crosses | meas ac ph find phd when tdb=0 |
| The n-th crossing | meas tran t50 when v(q)=0.6 rise=2 (also fall=, cross=) |
| Peak to peak, average, rms | meas tran vpp pp v(out) from=1u to=3u, then avg, rms in the same form |
| Maximum, minimum (and where) | meas tran vmax max v(out) from=1u to=3u, meas dc gmax min g |
| Time between two events | meas tran tr trig v(q) val=0.12 rise=1 targ v(q) val=1.08 rise=1 |
| Unity-gain frequency | let m = mag(v(out)) then meas ac fu when m=1 |
| A harmonic of an FFT | let mg = mag(v(out)) then meas sp h1 max mg from=0.9meg to=1.1meg |
The noise plots cannot be measured; index them instead (print noise1.inoise_spectrum[40] with the frequency in noise1.frequency[40]), or read the totals in noise2.
C.6 Recipes from the chapters
Loop gain with two injections (Tian’s method, Eq. 11.7, Chapter 11). Make two copies of the closed loop. In copy v, break the loop with a voltage source Vt_av f_av o_av dc 0 ac 1; in copy i, insert Vt_ai f_ai o_ai dc 0 and inject a current It_ai 0 o_ai dc 0 ac 1. Then
ac dec 30 10 3e9
let tv = -v(o_av)/v(f_av)
let ii = i(vt_ai)
let ti = (1 + ii)/(-ii)
let t = (tv*ti - 1)/(tv + ti + 2)
let tdb = db(t)
let tph = 180/pi*cph(t)
meas ac fu when tdb=0
meas ac ph find tph when tdb=0
let pm = 180 + ph
Monte Carlo mismatch (Chapter 13). Load the mismatch section (mos_tt_mismatch with mm_ok=1 on each SG13G2 instance; tt_mm in SKY130), set .options seed=N, and put many copies of the circuit in one netlist: each copy draws its own random parameters, and a single op returns all of them. Lab 7’s offset testbench has 200 copies of the 5T OTA. Change the seed to get a new sample.
Sweeping a device size. Make the size a .param, then loop over it with foreach, alterparam and reset. reset reloads the circuit with the new parameter; an alter of a source does not need it.
Biasing an open-loop amplifier. Find the input that puts the output at mid-supply with a dc sweep and meas dc vq when v(out)=0.6, then alter Vg dc = $&vq and run op and ac at that bias. For high-gain stages the course uses a DC servo instead: an ideal integrator around the amplifier that holds the output at its target in DC and opens at AC (Chapter 7).
C.7 Xschem keys
Mouse: the wheel zooms, the middle button pans, a right-button drag zooms to an area, a double-click edits an object’s attributes, and Shift + click adds to the selection.
| Key | Action | Key | Action |
|---|---|---|---|
Insert or Shift+I |
insert a symbol from a library | q |
edit the properties of the selection |
w |
draw a wire | Shift+W |
draw a wire that snaps to the nearest pin |
m |
move the selection | c |
copy the selection |
Shift+R |
rotate | Shift+F / Shift+V |
flip horizontally / vertically |
Delete |
delete the selection | u / Shift+U |
undo / redo |
f |
zoom to fit everything | z |
zoom to a box |
Shift+Z / Ctrl+Z |
zoom in / out | Escape |
abort the command, unselect |
e |
descend into the selected symbol’s schematic | Ctrl+E or BackSpace |
go back to the parent |
i |
descend into the symbol itself | a |
make a symbol from the schematic’s pins |
Alt+L |
add a net label (lab_pin) |
Shift+H |
attach net labels to the selected instance |
k |
highlight the selected net | Shift+K |
remove all highlights |
n |
write the hierarchical netlist | s |
run the simulation (asks first) |
Shift+A |
show the netlist window | Ctrl+S |
save |
Ctrl+F |
find by name | t |
place text |
Shift+T |
toggle “ignore” on the selected instances | Shift+O |
light or dark colours |
? |
help | / |
picture of all key bindings |
Attributes worth knowing. In a symbol, spice_sym_def replaces the subcircuit with an included netlist (the course’s post-layout symbol uses tcleval(.include [xschem get current_dirname]/file.spice)). Xschem removes braces from {param} values, so write expressions in single quotes, 'Cc' (the labs). The lab testbenches load their models in a code block named MODELS; in SKY130 it reads the binned set, unlike Xschem’s corner symbol, which loads the continuous one (Section 3.5).