Skip to content

Commit 347a03b

Browse files
committed
Add XSPICE support to libngspice and test
- Enable XSPICE in libngspice build with --enable-xspice - Build cmpp natively for code model generation (wasm can't execute) - Package XSPICE code model libraries (*.cm files) to /usr/lib/ngspice/ - Add XSPICE mixed-signal test with ADC bridge - Use NgSpiceShared.new_instance() to avoid cffi duplicate declarations - Fix verilog install hook to prevent build errors
1 parent 4d74964 commit 347a03b

File tree

3 files changed

+85
-2
lines changed

3 files changed

+85
-2
lines changed

packages/inspice/test_inspice.py

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
@run_in_pyodide(packages=["inspice"])
55
def test_inspice(selenium):
6+
"""Test basic ngspice analog simulation"""
67
from InSpice.Spice.NgSpice.Shared import NgSpiceShared
78

8-
ngspice = NgSpiceShared()
9+
# Use new_instance() singleton factory to avoid cffi duplicate declarations
10+
ngspice = NgSpiceShared.new_instance()
11+
912
circuit = """
1013
.title Voltage Multiplier
1114
@@ -51,3 +54,45 @@ def test_inspice(selenium):
5154
ngspice.run()
5255
plot = ngspice.plot(simulation=None, plot_name=ngspice.last_plot)
5356
assert "V(6)" in plot
57+
58+
59+
@run_in_pyodide(packages=["inspice"])
60+
def test_inspice_xspice(selenium):
61+
"""Test XSPICE mixed-signal simulation with ADC bridge"""
62+
from InSpice.Spice.NgSpice.Shared import NgSpiceShared
63+
64+
# Use new_instance() singleton factory - reuses same instance as test_inspice
65+
# This avoids cffi duplicate declaration errors with global ffi.cdef()
66+
ngspice = NgSpiceShared.new_instance()
67+
68+
# Load XSPICE code model
69+
ngspice.exec_command("codemodel /usr/lib/ngspice/digital.cm")
70+
71+
# Simple XSPICE test: Analog sine -> ADC bridge
72+
# NOTE: lowercase 'a' for XSPICE device instance names
73+
circuit = """
74+
.title XSPICE ADC Bridge Test
75+
76+
vdummy dummy 0 DC=0
77+
78+
* Analog sine wave source
79+
vin in 0 SIN(0 2.5 1e6 0 0)
80+
81+
* ADC bridge: Convert analog to digital
82+
aadc [in] [dout] adc1
83+
.model adc1 adc_bridge(in_low = 1.0 in_high = 2.0)
84+
85+
* Resistor to complete circuit
86+
R1 in 0 1MEG
87+
88+
.tran 0.01us 5us
89+
.end
90+
"""
91+
92+
ngspice.load_circuit(circuit)
93+
ngspice.run()
94+
plot = ngspice.plot(simulation=None, plot_name=ngspice.last_plot)
95+
96+
# Verify analog input exists (proves simulation ran)
97+
# Note: plot keys are lowercase node names without V() prefix
98+
assert "in" in plot

packages/libngspice/meta.yaml

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@ source:
1010
patches:
1111
- patches/0001-keep-alive-API-functions.patch
1212
- patches/0001-no-hicum2.patch
13+
- patches/0003-fix-verilog-install-hook.patch
1314

1415
build:
1516
type: shared_library
1617
script: |
1718
bash ./autogen.sh
1819
20+
# First build cmpp natively for build-time code generation
21+
mkdir build-native && cd build-native
22+
../configure --enable-xspice --disable-debug --without-x --with-readline=no
23+
make -C src/xspice/cmpp -j ${PYODIDE_JOBS:-3}
24+
cd ..
25+
1926
configure_args=(
2027
--prefix=${WASM_LIBRARY_DIR}
21-
--disable-xspice
28+
--enable-xspice
2229
--disable-debug
2330
--disable-dependency-tracking
2431
--enable-cider
@@ -30,10 +37,24 @@ build:
3037
3138
mkdir release-lib && cd release-lib
3239
emconfigure ../configure "${configure_args[@]}" CFLAGS="${SIDE_MODULE_CFLAGS}"
40+
41+
# Build everything except xspice/cmpp first
42+
emmake make -j ${PYODIDE_JOBS:-3} -C src/xspice/cmpp LDFLAGS="${SIDE_MODULE_LDFLAGS}"
43+
44+
# Now replace the wasm cmpp with the native one and touch to prevent rebuild
45+
cp ../build-native/src/xspice/cmpp/cmpp src/xspice/cmpp/cmpp
46+
touch src/xspice/cmpp/cmpp
47+
48+
# Continue building the rest
3349
emmake make -j ${PYODIDE_JOBS:-3} LDFLAGS="${SIDE_MODULE_LDFLAGS}"
3450
emmake make install
3551
cp ${WASM_LIBRARY_DIR}/lib/libngspice.so ${DISTDIR}
3652
53+
# Copy XSPICE code model libraries to ngspice/*.cm
54+
# Since shared_library packages extract to /usr/lib/, the final path will be /usr/lib/ngspice/*.cm
55+
mkdir -p ${DISTDIR}/ngspice
56+
cp ${WASM_LIBRARY_DIR}/lib/ngspice/*.cm ${DISTDIR}/ngspice/
57+
3758
about:
3859
home: http://ngspice.sourceforge.net
3960
license: BSD-3-Clause
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
diff --git a/src/xspice/verilog/Makefile.am b/src/xspice/verilog/Makefile.am
2+
index 1234567..abcdefg 100644
3+
--- a/src/xspice/verilog/Makefile.am
4+
+++ b/src/xspice/verilog/Makefile.am
5+
@@ -45,9 +45,11 @@ endif
6+
# On Windows, the dummy libvvp.* files are removed also.
7+
8+
install-exec-hook:
9+
cd $(DESTDIR)$(pkglibdir); \
10+
rm -f ivlng*a libvvp* ; \
11+
- mv ivlngvpi.* ivlng.vpi
12+
+ if [ -f ivlngvpi.so ] || [ -f ivlngvpi.dll ] || [ -f ivlngvpi.dylib ]; then \
13+
+ mv ivlngvpi.* ivlng.vpi; \
14+
+ fi
15+
16+
uninstall-hook:
17+
rm -f $(DESTDIR)$(pkglibdir)/ivlng.vpi $(DESTDIR)$(pkglibdir)/ivlng.so

0 commit comments

Comments
 (0)