concord-consortium / build-ngspice-js

Proof of concept build script that successfully compiles the ngspice circuit simulator to Javascript using Emscripten.
MIT License
14 stars 1 forks source link

build-ngspice-js

Quick & dirty demonstration of building ngspice to Javascript/asm.js with Emscripten. This will eventually enable educational and other webapps to use SPICE circuit simulation lag-free and without requiring developers to provision a server that runs a SPICE binary.

In order for the build product to be a manageable size (~2.5MB), most device types were removed from the ngspice source code before building. The current demo includes voltage and current sources, resistors, capacitors, inductors, diodes, bipolar junction transistors, and ideal voltage-controlled switches. Devices can be added back, but note they must be statically linked, so the device set should be chosen wisely based on project needs.

Note this is intended to become a backend component to be used by a separate UI; it does not provide a user-facing interface (though a command-line based demo would be trivially easy at this point).

The build runs on my computer (running OS X 10.9.5); that is all I can promise.

Setup

  1. Install Emscripten -- either the official Emscripten SDK, or from Homebrew

  2. Clone the emscripten branch of ngspice from the Concord Consortium version on Github: https://github.com/concord-consortium/ngspice:

    git clone --branch emscripten https://github.com/concord-consortium/ngspice.git
  3. Clone this repository into <ngspice repo>/release:

    cd ngspice
    git clone https://github.com/concord-consortium/build-ngspice-js.git release
  4. cd release and run ./build.sh:

    cd release  
    # this must be run from the `<ngspice repo>/release`:
    ./build.sh

This should build ngspice.html, ngspice.js, and ngspice.html.mem. You can open ngspice.html. The page will appear to be blank, but you can open it in your browser's developer console to see the output from SPICE.

Rebuilding:

Key learnings:

TODO: