Source code coming soon. The GitHub repository is private for now and opens to everyone very soon.
BetterSPICE Beta 0.8.0 Join the beta Download soon
Part VI  Parts and modelsReferenceContentsIndex
39

Digital logic

BetterSPICE has two kinds of digital parts. The simple gates below have no supply pins and switch at half their supply parameter. The logic families (74HC, 74LS, CD4000 and others) have real supply pins, real thresholds and output drive (Logic families). Both run in the same analogue simulation as the rest of the circuit, so edges have real rise times and real loading.

39.1Simple gates, flip-flop and counter

Simple gates, flip-flop and counter
TypeKeyPinsVariantsClass
not, and, or, nand, nor, xor, dff, counter4 (U)librarynot: in, out. Gates: in1, in2, out. dff: d, clk, q, qn. counter4: clk, q0 … q3noneapproximate

The supply is the parameter vdd. Outputs are referenced to ground behind rout. An input is high above vdd/2, with no hysteresis and no input current. Each output edge starts half a propagation delay after the input crosses and lasts one tpd. The D flip-flop samples d on the rising clock edge. The counter counts rising clock edges and wraps from 15 to 0. q0 is the least significant bit.

Simple gates, flip-flop and counter
ParameterKeyUnitDefaultRangeValue comes fromWhat it does
SupplyvddV5> 0. Slider 1 to 15 V.Default, inspectorHigh level and threshold basis.
Propagation delaytpds10 ns≥ 0Default, inspector
Output resistanceroutΩ50> 0File or All parameters only

Key: the name in the circuit file, in a parameter step (R1.r) and for AI agents. Range: a value outside it is refused with E_PARAM. Value comes from: where you can set it. The All parameters tab and agents can set every parameter.

noteInputs need a driver. A gate input draws no current, so a net with only gate inputs on it has no DC path and is reported as floating. Drive every input, or tie it to a supply.

39.2Flip-flops and latches (JK, T, D latch, SR latch)

Flip-flops and latches (JK, T, D latch, SR latch)
TypeKeyPinsVariantsClass
jkff, tff, dlatch, srlatch (U)libraryjkff: j, clk, k, set, rst, q, qn. tff: t, clk, set, rst, q, qn. dlatch: d, en, q, qn. srlatch: s, r, q, qnnoneapproximate

The same electrical model as the D flip-flop: the supply vdd, inputs high above vdd/2, outputs behind rout, each output edge half a propagation delay after the input and one tpd long. All four power up with q low.

The JK flip-flop acts on the rising clock edge: J and K both low hold, J high sets, K high resets, both high toggle. The T flip-flop toggles on the rising edge while t is high and holds while it is low. Both have an asynchronous set (S) and reset (R), active high. An open set or reset pin reads low, so it can stay unconnected. Set and reset together drive q and qn both high, as a 74xx part does, and the stored bit returns when they are released.

The D latch is transparent while en is high: q follows d. While en is low it holds. The SR latch is the pair of cross-coupled NOR gates: s sets, r resets, and with both low it holds. s and r both high is the forbidden input: q and qn are both low. The input released last wins. Released in the same instant, the latch settles reset. A real pair goes metastable there and can settle either way.

Flip-flops and latches (JK, T, D latch, SR latch)
ParameterKeyUnitDefaultRangeValue comes fromWhat it does
SupplyvddV5> 0. Slider 1 to 15 V.Default, inspectorHigh level and threshold basis.
Propagation delaytpds10 ns≥ 0Default, inspector
Output resistanceroutΩ50> 0File or All parameters only

Key: the name in the circuit file, in a parameter step (R1.r) and for AI agents. Range: a value outside it is refused with E_PARAM. Value comes from: where you can set it. The All parameters tab and agents can set every parameter.

noteChecked against ngspice. The four parts are compared with the ngspice XSPICE flip-flops and a pair of XSPICE NOR gates on a random input sequence. Like the D flip-flop they are written to an ngspice export as comments.

39.3Logic families

The 74xx and 4000-series parts and the single gates with supply pins take their thresholds, output drive, delay and supply current from a logic family, chosen with the family parameter. Their outputs are driven from their own VCC and GND pins, so a weak supply or a long line shows up as it would on a board, and a supply outside the family's range warns W_SUPPLY.

Logic families
FamilySupplyInput low / highOutput R high / lowEdgeDelayQuiescentInputs above VCC
hc (default)2 to 6 V0.3 / 0.7 VCC50 / 40 Ω6 ns9 ns2 µAclamped
hct4.5 to 5.5 V0.8 / 2.0 V50 / 40 Ω6 ns12 ns2 µAclamped
ahc2 to 5.5 V0.3 / 0.7 VCC25 / 20 Ω3 ns5 ns2 µAclamped
lvc1.65 to 5.5 Vbelow 2.7 V: 0.35 / 0.65 VCC, else 0.8 / 2.0 V15 / 12 Ω2 ns4 ns1 µAtolerant
cd40003 to 18 V0.3 / 0.7 VDD500 Ω at 5 V50 ns100 ns1 µAclamped
ls4.75 to 5.25 V0.8 / 2.0 V100 / 25 Ω8 ns10 ns4 mAtolerant

Drive, edge and delay are taken at 5 V for parts with supply pins. Inputs have 3.5 pF and, unless the family is tolerant, clamp diodes to VCC and GND. Schmitt inputs (the 14 and 1G14, and the buf_st and inv_st gates, or any part with schmitt = 1) have a hysteresis of 0.15 VCC. Output changes come tpd after the input, and a glitch shorter than tpd is swallowed (inertial delay). Below the family's minimum supply the outputs release.

Logic families
ParameterKeyUnitDefaultRangeValue comes fromWhat it does
Logic familyfamilyhc (cd4000 for 4017, 4040, 4060, 4511)hc, hct, ahc, lvc, cd4000, lsDefault, inspector, part file
Propagation delaytpdsfrom the family≥ 0Default, inspector, part file
Supply (gates without supply pins)vccV5> 0Default, inspector, part file
Open drainod0 / 10Default, inspector, part fileOutputs have no high side.
Schmitt inputsschmitt0 / 10Default, inspector, part file

Key: the name in the circuit file, in a parameter step (R1.r) and for AI agents. Range: a value outside it is refused with E_PARAM. Value comes from: where you can set it. The All parameters tab and agents can set every parameter.

39.4Single gates

Single gates
TypePinsVariantsClass
gate (U)buf, inv, buf_st, inv_st: in, out. tri_buf, tri_inv: in, oe (active low), out. 2-input: in1, in2, out. 3, 4 and 8-input: in1 …, out. Variants ending _p add VCC, GND.and2 to and8, nand2 to nand8, or2 to or8, nor2 to nor8, xor2, xnor2, buf, inv, buf_st, inv_st, tri_buf, tri_inv, each also with _papproximate

Placed with Alt+G. Without _p the gate is powered from its vcc parameter and switches relative to ground, with no clamps and no supply current.

39.574xx and 4000-series packages

xx and 4000-series packages
TypePinsVariantsClass
logic (U)The package pins in order, VCC and GND included.see the tableapproximate

Placed with Alt+L. Every package simulates its full function, with its pins in package order.

xx and 4000-series packages
VariantFunctionNotes
00, 08, 32, 86Quad 2-input NAND, AND, OR, XOR
02Quad 2-input NOR
04, 14Hex inverter, hex Schmitt inverter
u04Hex unbuffered inverter (74HCU04)A continuous high-gain stage (gain 20, rout 300 Ω) for crystal oscillators. Set from the file or an AI agent.
125Quad tri-state buffernOE active low.
74Dual D flip-flop with preset and clearClocks on the rising edge.
1383-to-8 decoderEnabled with G1 high, nG2A and nG2B low. Outputs active low.
1518-to-1 multiplexerY and nW.
157Quad 2-to-1 multiplexer
1614-bit synchronous counterAsynchronous clear, synchronous load, ENP and ENT, ripple carry.
1648-bit serial-in parallel-out shift registerData = A AND B.
1658-bit parallel-in serial-out shift register
244Octal buffer, two enables
245Octal bus transceiverDIR high: A to B.
573Octal transparent latch
574Octal D flip-flop
5958-bit shift register with output latchSRCLK shifts, RCLK latches, QH' cascades.
123Dual retriggerable monostableThe pulse width follows the external R and C, about 0.45 RC.
4017Decade counter (Johnson)CO high for counts 0 to 4.
404012-stage ripple counterCounts on the falling edge.
406014-stage counter with oscillatorAn RC or crystal around PHI, PHO and PHOB oscillates.
4511BCD to 7-segment latch and decoderBlanks above 9.
1g00, 1g08, 1g32, 1g86Single NAND, AND, OR, XOR (SOT-23-5)
1g04, 1g14Single inverter, Schmitt inverter
1g125Single tri-state buffer
Try it
Shift out a byte
  1. Open Counters and registers, a 74HC595 example, or wire a ctrl block to SER, SRCLK and RCLK of a 595 with LEDs on QA to QH.
  2. Open the logic analyzer and add QA to QH.

You should seeEach SRCLK edge shifts a bit in, and the LEDs change all at once on the RCLK edge.