velxio/docs/wiki/circuit-emulation.md

99 lines
4.7 KiB
Markdown
Raw Normal View History

feat: electrical simulation via ngspice-WASM (eecircuit-engine) Adds full SPICE-accurate electrical simulation to Velxio, behind a lazy- loaded ⚡ toolbar toggle. Arduino / ESP32 / RP2040 sketches now co-simulate with real analog behaviour: correct voltages on wires, real I–V curves on LEDs, working potentiometers, NTC thermistors read by analogRead(), PWM driving RC filters, transistors, op-amps, diodes, MOSFETs, etc. Engine: eecircuit-engine (ngspice compiled to WebAssembly). Main bundle stays at 2.4 MB; the 20 MB SPICE chunk only loads when the user activates electrical mode. Disabled at build time via VITE_ELECTRICAL_SIM=false. Frontend additions: - simulation/spice/: SpiceEngine wrapper + lazy entry, NetlistBuilder with UnionFind over wires, componentToSpice mapping (24 metadataIds incl. real part numbers: 2N2222, 2N3055, BC547, IRF540, 2N7000, 1N4148, 1N4007, 1N4733, LEDs, NTC, op-amp ideal), CircuitScheduler with debounced coalescing, AVRSpiceBridge for quasi-static co-simulation. - store/useElectricalStore: Zustand slice, feature-flag aware. - components/analog-ui/: ⚡ toolbar toggle + SVG voltage overlay. - components/components-instruments/: Voltmeter, Ammeter probes. - 62 tests (spice-*, netlist-builder, component-to-spice, instruments). Sandbox (test/test_circuit/): 47-test validation sandbox that proved the approach (hand-rolled MNA baseline + ngspice pipeline) before porting to the app. Kept as reference. Docs: docs/wiki/circuit-emulation-*.md (13 engineering pages covering architecture, solvers, components, AVR bridge, gotchas, performance, integration plan, API reference, appendix) + electrical-simulation- user-guide.md (end-user facing). Reference plan: test/test_circuit/plan/phase_8_velxio_implementation.md Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-15 19:11:54 +07:00
# Circuit Emulation — Wiki Entry Point
Full electrical simulation experiment for Velxio. Validates that Arduino sketches can be co-simulated with real analog circuit behaviour inside the Velxio emulator.
## TL;DR
- **Where**: all code and tests live in [`test/test_circuit/`](../../test/test_circuit/).
- **What works**: 47 / 47 tests, 14 files, ~5 seconds total.
- **Two pipelines**: hand-rolled MNA solver (fast, 0 extra deps) + ngspice-WASM via `eecircuit-engine` (complete SPICE).
- **Showcase**: an Arduino sketch compiled to `.hex` runs on `avr8js` and reads a real voltage that ngspice computes from an NTC thermistor divider, recovering temperature within 0.05 °C across 050 °C.
- **Status**: sandbox is complete; ready to port into `frontend/src/simulation/` behind a feature flag.
## Detailed docs
The full documentation is split into focused pages:
1. [**Overview**](circuit-emulation-overview.md) — goals, outcomes, index.
2. [**Architecture**](circuit-emulation-architecture.md) — dual-pipeline design, data flow, module layout.
3. [**MNA Solver (hand-rolled)**](circuit-emulation-mna-solver.md) — the ~500-line JS SPICE kernel and why we built it first.
4. [**ngspice-WASM (`eecircuit-engine`)**](circuit-emulation-ngspice.md) — installation, API, netlist syntax, gotchas.
5. [**Component Catalog**](circuit-emulation-components.md) — every R/L/C/D/BJT/MOSFET/op-amp/NTC we validated, with parameters.
6. [**AVR Bridge**](circuit-emulation-avr-bridge.md) — how `avr8js` is wrapped (mirroring Velxio) and how `AVRSpiceBridge` does mixed-signal co-simulation.
7. [**Test Catalog**](circuit-emulation-tests.md) — all 47 tests enumerated, with expected results.
8. [**Gotchas**](circuit-emulation-gotchas.md) — every bug we hit and how we fixed it.
9. [**Performance**](circuit-emulation-performance.md) — benchmarks, memory footprint, lazy-load plan.
10. [**Velxio Integration Plan**](circuit-emulation-velxio-integration.md) — concrete files/changes to ship this in the main app. **Superseded by** [`test/test_circuit/plan/phase_8_velxio_implementation.md`](../../test/test_circuit/plan/phase_8_velxio_implementation.md) — more detailed, with new analog components and measurement instruments.
11. [**API Reference**](circuit-emulation-api.md) — every exported function and class.
12. [**Appendix**](circuit-emulation-appendix.md) — reference netlists, AVR opcode tables, model parameters, glossary.
## Quickstart
```bash
cd test/test_circuit
npm install
npm test # all 47 tests
```
Specific suites:
```bash
npx vitest run test/spice_passive.test.js # ngspice DC
npx vitest run test/spice_transient.test.js # ngspice transient
npx vitest run test/spice_ac.test.js # ngspice AC / Bode
npx vitest run test/spice_active.test.js # diode, BJT, MOSFET, op-amp
npx vitest run test/spice_digital.test.js # behavioral gates
npx vitest run test/spice_555_astable.test.js # relaxation oscillator
npx vitest run test/spice_avr_mixed.test.js # AVR ↔ ngspice mixed-signal
```
## Hello-world example
```javascript
import { runNetlist } from '../src/spice/SpiceEngine.js';
const { dcValue } = await runNetlist(`Voltage divider
V1 vcc 0 DC 9
R1 vcc out 1k
R2 out 0 2k
.op
.end`);
console.log(dcValue('v(out)')); // 6
```
## Mixed-signal showcase
```javascript
import { AVRHarness } from '../src/avr/AVRHarness.js';
import { adcReadProgram } from '../src/avr/programs.js';
import { runNetlist } from '../src/spice/SpiceEngine.js';
// 1. Solve an NTC + pullup divider in ngspice
const { dcValue } = await runNetlist(`NTC divider @25C
Vcc vcc 0 DC 5
Rpull vcc a0 10k
Rntc a0 0 10k
.op
.end`);
const va0 = dcValue('v(a0)'); // 2.500 V
// 2. Hand it to an Arduino sketch running in avr8js
const avr = new AVRHarness();
avr.loadProgram(adcReadProgram());
avr.setAnalogVoltage(0, va0);
avr.runCycles(500_000);
// 3. Inspect the ADC result the sketch saw
const ADCH = avr.cpu.data[0x79];
const ADCL = avr.cpu.data[0x78];
const raw = (ADCH << 2) | (ADCL >> 6);
console.log(raw); // 511 → matches 2.5/5 * 1023
```
## What this proves
Velxio can realistically integrate a **full electrical simulator** (ngspice or a hand-rolled fallback) alongside its existing MCU emulators, giving students and hobbyists circuits that behave the way they would on a real breadboard — correct voltages, correct currents, correct LED brightness, correct ADC readings, real op-amp saturation, real RC filter time constants, real Bode plots. All in ~5 seconds of test time, all within a browser-compatible JavaScript/WASM stack.
The groundwork and the port plan are ready. See [Velxio Integration](circuit-emulation-velxio-integration.md) for the next step.