/** * Per-simulator bridge state for custom chips. * * Each simulator family exposes its peripherals differently: * - AVR (avr8js) — `simulator.usart` / `simulator.spi` / `simulator.i2cBus` * - RP2040 (rp2040js) — `simulator.serialWriteByte` / `simulator.setSPIHandler` / * `simulator.addI2CDevice` (per-bus indexing) * - ESP32 (QEMU shim) — `simulator.sendPinEvent` (no I2C/SPI/UART today) * * The bridges in this module install a single dispatcher per simulator that * fans out to every chip subscribed, regardless of family. */ import { SPIBus } from './SPIBus'; export type SimulatorKind = 'avr' | 'rp2040' | 'esp32' | 'unknown'; export function detectSimulatorKind(simulator: any): SimulatorKind { if (!simulator) return 'unknown'; if (simulator.usart && simulator.spi && simulator.i2cBus) return 'avr'; if (typeof simulator.addI2CDevice === 'function' && typeof simulator.setSPIHandler === 'function') { return 'rp2040'; } if (typeof simulator.sendPinEvent === 'function') return 'esp32'; return 'unknown'; } export interface SimulatorBridges { /** Set of UART RX listeners (one per UART chip). */ uartListeners: Set<(byte: number) => void>; /** Whether the UART dispatcher has already been wired to the simulator. */ uartInstalled: boolean; /** Original onByteTransmit so non-chip listeners still receive bytes. */ uartPreviousOnByteTransmit: ((byte: number) => void) | null; /** Shared SPI bus across all custom chips on this simulator. */ spiBus: SPIBus; /** Whether the SPI dispatcher has already been wired. */ spiInstalled: boolean; /** Original SPI byte handler (preserved for restore). */ spiPreviousOnByte: ((byte: number) => void) | null; } const SIM_BRIDGES = new WeakMap(); export function getSimulatorBridges(simulator: any): SimulatorBridges { let b = SIM_BRIDGES.get(simulator); if (!b) { b = { uartListeners: new Set(), uartInstalled: false, uartPreviousOnByteTransmit: null, spiBus: new SPIBus(), spiInstalled: false, spiPreviousOnByte: null, }; SIM_BRIDGES.set(simulator, b); } return b; } // ── UART ──────────────────────────────────────────────────────────────────── /** * Install the UART TX-out dispatcher idempotently. Whatever family the * simulator belongs to, the dispatcher fans bytes out to every listener in * `uartListeners` (one per UART chip). */ export function ensureUartBridge(simulator: any): void { const b = getSimulatorBridges(simulator); if (b.uartInstalled) return; const kind = detectSimulatorKind(simulator); if (kind === 'avr' && simulator.usart) { b.uartPreviousOnByteTransmit = simulator.usart.onByteTransmit ?? null; const previous = b.uartPreviousOnByteTransmit; simulator.usart.onByteTransmit = (byte: number) => { if (previous) { try { previous(byte); } catch { /* swallow */ } } for (const listener of b.uartListeners) { try { listener(byte); } catch { /* swallow */ } } }; b.uartInstalled = true; return; } if (kind === 'rp2040') { // RP2040 emits each TX byte through `onSerialData(char)` (a string). const previous = simulator.onSerialData; simulator.onSerialData = (charStr: string) => { if (previous) { try { previous(charStr); } catch { /* swallow */ } } const code = typeof charStr === 'string' ? charStr.charCodeAt(0) : Number(charStr); if (!Number.isFinite(code)) return; for (const listener of b.uartListeners) { try { listener(code & 0xff); } catch { /* swallow */ } } }; b.uartInstalled = true; return; } // esp32/unknown: no client-side UART bridge today (QEMU has its own path). } /** * Inject a byte into the simulator's RX path so the sketch's `Serial.read()` * returns it. Routes per simulator family. */ export function avrUartTx(simulator: any, byte: number): void { const kind = detectSimulatorKind(simulator); try { if (kind === 'avr' && simulator.usart && typeof simulator.usart.writeByte === 'function') { simulator.usart.writeByte(byte); } else if (kind === 'rp2040' && typeof simulator.serialWriteByte === 'function') { simulator.serialWriteByte(byte); } } catch { /* swallow — peripheral may not be ready yet */ } } // ── SPI ───────────────────────────────────────────────────────────────────── /** * Install the SPI master TX → SPIBus dispatcher idempotently. The chip's * SPIDevice (created in `vx_spi_attach`) ends up on `b.spiBus` and is picked * up automatically — no per-chip wiring needed beyond `bridges.spiBus`. */ export function ensureSpiBridge(simulator: any): void { const b = getSimulatorBridges(simulator); if (b.spiInstalled) return; const kind = detectSimulatorKind(simulator); if (kind === 'avr' && simulator.spi) { b.spiPreviousOnByte = simulator.spi.onByte ?? null; simulator.spi.onByte = (mosi: number) => { const miso = b.spiBus.transferByte(mosi); simulator.spi.completeTransfer(miso); }; b.spiInstalled = true; return; } if (kind === 'rp2040' && typeof simulator.setSPIHandler === 'function') { // RP2040 has SPI0 and SPI1; we route both through the same bus so // CS-gated chips can live on either. for (const bus of [0, 1] as const) { try { simulator.setSPIHandler(bus, (mosi: number) => b.spiBus.transferByte(mosi)); } catch { /* this bus may not be in use; ignore */ } } b.spiInstalled = true; return; } } // ── I2C adapter ───────────────────────────────────────────────────────────── /** * Pick the right I2C bus object for the chip runtime to call `addDevice`/ * `removeDevice` on. AVR exposes `simulator.i2cBus` directly; RP2040 needs * a tiny adapter to forward to its `addI2CDevice` (per-bus) API. * * Returns `null` if the simulator doesn't expose any I2C bus (ESP32 today). */ export function getI2CBus(simulator: any, bus: 0 | 1 = 0): { addDevice: (device: any) => void; removeDevice: (address: number) => void; } | null { const kind = detectSimulatorKind(simulator); if (kind === 'avr' && simulator.i2cBus) { return simulator.i2cBus; } if (kind === 'rp2040' && typeof simulator.addI2CDevice === 'function') { return { addDevice: (device) => simulator.addI2CDevice(device, bus), removeDevice: (address) => simulator.removeI2CDevice?.(address, bus), }; } return null; }