Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 46 additions & 1 deletion packages/inspice/test_inspice.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

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

ngspice = NgSpiceShared()
# Use new_instance() singleton factory to avoid cffi duplicate declarations
ngspice = NgSpiceShared.new_instance()

circuit = """
.title Voltage Multiplier

Expand Down Expand Up @@ -51,3 +54,45 @@ def test_inspice(selenium):
ngspice.run()
plot = ngspice.plot(simulation=None, plot_name=ngspice.last_plot)
assert "V(6)" in plot


@run_in_pyodide(packages=["inspice"])
def test_inspice_xspice(selenium):
"""Test XSPICE mixed-signal simulation with ADC bridge"""
from InSpice.Spice.NgSpice.Shared import NgSpiceShared

# Use new_instance() singleton factory - reuses same instance as test_inspice
# This avoids cffi duplicate declaration errors with global ffi.cdef()
ngspice = NgSpiceShared.new_instance()

# Load XSPICE code model
ngspice.exec_command("codemodel /usr/lib/ngspice/digital.cm")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some environments (e.g. pyodide venv) we don't install shared libraries to /usr/lib as it actually points the native directory, but I guess it should be okay for unit test.


# Simple XSPICE test: Analog sine -> ADC bridge
# NOTE: lowercase 'a' for XSPICE device instance names
circuit = """
.title XSPICE ADC Bridge Test

vdummy dummy 0 DC=0

* Analog sine wave source
vin in 0 SIN(0 2.5 1e6 0 0)

* ADC bridge: Convert analog to digital
aadc [in] [dout] adc1
.model adc1 adc_bridge(in_low = 1.0 in_high = 2.0)

* Resistor to complete circuit
R1 in 0 1MEG

.tran 0.01us 5us
.end
"""

ngspice.load_circuit(circuit)
ngspice.run()
plot = ngspice.plot(simulation=None, plot_name=ngspice.last_plot)

# Verify analog input exists (proves simulation ran)
# Note: plot keys are lowercase node names without V() prefix
assert "in" in plot
22 changes: 21 additions & 1 deletion packages/libngspice/meta.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ source:
patches:
- patches/0001-keep-alive-API-functions.patch
- patches/0002-fix-hicum2-extern-c.patch
- patches/0003-fix-verilog-install-hook.patch

build:
type: shared_library
script: |
bash ./autogen.sh

# First build cmpp natively for build-time code generation
mkdir build-native && cd build-native
../configure --enable-xspice --disable-debug --without-x --with-readline=no
make -C src/xspice/cmpp -j ${PYODIDE_JOBS:-3}
cd ..

configure_args=(
--prefix=${WASM_LIBRARY_DIR}
--disable-xspice
--enable-xspice
--disable-debug
--disable-dependency-tracking
--enable-cider
Expand All @@ -30,10 +37,23 @@ build:

mkdir release-lib && cd release-lib
emconfigure ../configure "${configure_args[@]}" CFLAGS="${SIDE_MODULE_CFLAGS}"

# Build everything except xspice/cmpp first
emmake make -j ${PYODIDE_JOBS:-3} -C src/xspice/cmpp LDFLAGS="${SIDE_MODULE_LDFLAGS}"

# Now replace the wasm cmpp with the native one and touch to prevent rebuild
cp ../build-native/src/xspice/cmpp/cmpp src/xspice/cmpp/cmpp
touch src/xspice/cmpp/cmpp

# Continue building the rest
emmake make -j ${PYODIDE_JOBS:-3} LDFLAGS="${SIDE_MODULE_LDFLAGS}"
emmake make install
cp ${WASM_LIBRARY_DIR}/lib/libngspice.so ${DISTDIR}

# Copy XSPICE code model libraries to ngspice/*.cm
mkdir -p ${DISTDIR}/ngspice
cp ${WASM_LIBRARY_DIR}/lib/ngspice/*.cm ${DISTDIR}/ngspice/

about:
home: http://ngspice.sourceforge.net
license: BSD-3-Clause
Expand Down
17 changes: 17 additions & 0 deletions packages/libngspice/patches/0003-fix-verilog-install-hook.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
diff --git a/src/xspice/verilog/Makefile.am b/src/xspice/verilog/Makefile.am
index 1234567..abcdefg 100644
--- a/src/xspice/verilog/Makefile.am
+++ b/src/xspice/verilog/Makefile.am
@@ -45,9 +45,11 @@ endif
# On Windows, the dummy libvvp.* files are removed also.

install-exec-hook:
cd $(DESTDIR)$(pkglibdir); \
rm -f ivlng*a libvvp* ; \
- mv ivlngvpi.* ivlng.vpi
+ if [ -f ivlngvpi.so ] || [ -f ivlngvpi.dll ] || [ -f ivlngvpi.dylib ]; then \
+ mv ivlngvpi.* ivlng.vpi; \
+ fi

uninstall-hook:
rm -f $(DESTDIR)$(pkglibdir)/ivlng.vpi $(DESTDIR)$(pkglibdir)/ivlng.so