// internals
Emulator Architecture
Eidolon uses real CPU emulation rather than a simplified model. This document describes how each layer of the simulation works.
High-Level Data Flow
User Code (Monaco Editor)
│
▼
Zustand Store (useEditorStore)
│
▼
eidolon-server ──► arduino-cli ──► .hex / .uf2 file
│
▼
AVRSimulator / RP2040Simulator
│ loadHex()
▼
CPU execution loop (~60 FPS via requestAnimationFrame)
│
▼
Port listeners (PORTB / PORTC / PORTD)
│
▼
PinManager ──► Component state ──► React re-rendersAVR8 Emulation (Arduino Uno / Nano / Mega)
The AVR backend uses avr8js, which implements a complete ATmega328p / ATmega2560 processor.
Execution Loop
Each animation frame executes approximately 267,000 CPU cycles (16 MHz / 60 FPS):
1avrInstruction(cpu); // decode and execute one AVR instruction
2cpu.tick(); // advance peripheral timers and countersSupported Peripherals
| Peripheral | Details |
|---|---|
| GPIO | PORTB (pins 8-13), PORTC (A0-A5), PORTD (pins 0-7) |
| Timer0 / Timer1 / Timer2 | millis(), delay(), PWM via analogWrite() |
| USART | Full transmit and receive, powers the Serial Monitor |
| ADC | 10-bit, 5 V reference on pins A0-A5 |
| SPI | Hardware SPI (enables ILI9341, SD card, etc.) |
| I2C (TWI) | Hardware I2C with virtual device bus |
Pin Mapping
| Arduino Pin | AVR Port | Bit |
|---|---|---|
| 0–7 | PORTD | 0–7 |
| 8–13 | PORTB | 0–5 |
| A0–A5 | PORTC | 0–5 |
RP2040 Emulation (Raspberry Pi Pico)
The RP2040 backend uses rp2040js.
- Real RP2040 emulation at 133 MHz
- UART0 serial output displayed in the Serial Monitor
- 12-bit ADC on GPIO 26-29 (A0-A3) with 3.3 V reference
HEX File Format
Arduino compilation produces Intel HEX format. The parser in hexParser.ts:
- Reads lines starting with
: - Extracts the address, record type, and data bytes
- Returns a
Uint8Arrayof program bytes AVRSimulatorconverts this to aUint16Array(16-bit words, little-endian)
Key Source Files
| File | Purpose |
|---|---|
apps/web/src/simulation/AVRSimulator.ts | AVR8 CPU emulator wrapper |
apps/web/src/simulation/PinManager.ts | Maps Arduino pins to UI components |
apps/web/src/utils/hexParser.ts | Intel HEX parser |
apps/web/src/components/simulator/SimulatorCanvas.tsx | Canvas rendering |
packages/firmware/src/compiler.rs | arduino-cli wrapper |
apps/server/src/firmware/mod.rs | Compilation API endpoint |