Documentation
// 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-renders

AVR8 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 counters

Supported Peripherals

PeripheralDetails
GPIOPORTB (pins 8-13), PORTC (A0-A5), PORTD (pins 0-7)
Timer0 / Timer1 / Timer2millis(), delay(), PWM via analogWrite()
USARTFull transmit and receive, powers the Serial Monitor
ADC10-bit, 5 V reference on pins A0-A5
SPIHardware SPI (enables ILI9341, SD card, etc.)
I2C (TWI)Hardware I2C with virtual device bus

Pin Mapping

Arduino PinAVR PortBit
0–7PORTD0–7
8–13PORTB0–5
A0–A5PORTC0–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:

  1. Reads lines starting with :
  2. Extracts the address, record type, and data bytes
  3. Returns a Uint8Array of program bytes
  4. AVRSimulator converts this to a Uint16Array (16-bit words, little-endian)

Key Source Files

FilePurpose
apps/web/src/simulation/AVRSimulator.tsAVR8 CPU emulator wrapper
apps/web/src/simulation/PinManager.tsMaps Arduino pins to UI components
apps/web/src/utils/hexParser.tsIntel HEX parser
apps/web/src/components/simulator/SimulatorCanvas.tsxCanvas rendering
packages/firmware/src/compiler.rsarduino-cli wrapper
apps/server/src/firmware/mod.rsCompilation API endpoint