System Architecture
HIL is a multi-layered system that bridges high-level LISP programming with low-level hardware control. This document describes the complete architecture, from the user's LISP script down to the electrical signals on a microcontroller pin.
System Overview
Layer 1: Firmware (TinyGo)
The firmware runs on an ATmega328P microcontroller (Arduino Uno/Nano). It is compiled with TinyGo and communicates with the host monitor over a serial UART link.
Firmware Capabilities
Digital I/O: The firmware can read and write digital pins. WRITE D 13 1 sets pin 13 HIGH. READ D 2 reads the state of pin 2.
Analog I/O: ADC readings return 10-bit values (0–1023). PWM output accepts 8-bit values (0–255).
Autopilot Mode: The firmware can run a pre-loaded script autonomously without host intervention. This is achieved by burning the script into the node's persistent memory via the hil-cli firmware flash command (Rashwan, 2026).
Event System: Digital events (rising/falling/both edge) and analog threshold events can be armed to trigger callbacks without polling.
Firmware Memory Layout
┌──────────────────────────┐ 0x0000
│ Interrupt Vector Table │
├──────────────────────────┤ 0x0020
│ Program Code (TinyGo) │
│ - Frame Parser │
│ - Pin Controller │
│ - Autopilot Engine │
├──────────────────────────┤ ~0x2000
│ Global Variables │
├──────────────────────────┤
│ Stack │ ↓ grows down
└──────────────────────────┘ 0x08FF
The ATmega328P has 32KB of flash, 2KB of SRAM, and 1KB of EEPROM. The TinyGo runtime uses approximately 16KB of flash, leaving ~16KB for user scripts in autopilot mode (Atmel, 2016).
Layer 2: Serial Wire Protocol
The serial wire protocol is a compact ASCII-based frame format for communication between the host monitor and firmware:
ACTION TYPE PIN VAL1 VAL2|CRC\r
| Field | Description |
|---|---|
| ACTION | Single character: W (write), R (read), T (tone), S (pulse), E (event), C (change) |
| TYPE | Pin type: D (digital), A (analog), P (PWM alias) |
| PIN | Pin number |
| VAL1 | Primary value (state, threshold, frequency) |
| VAL2 | Secondary value (duration, edge type) |
| CRC | XOR checksum of all bytes before ` |
\r | Carriage return terminator |
Example: W D 13 1 0|5D\r sets digital pin 13 HIGH.
The CRC is computed as the XOR of every byte in the command portion (before |). This provides basic error detection for noisy serial links (Stallings, 2020).
Layer 3: Host Monitor
The host monitor is a Go binary (hil-orchestrator) that runs on the host computer. It contains:
Execution Pipeline
- Tokenizer: Converts raw text into tokens (symbols, numbers, strings, parentheses)
- Parser: Builds an Abstract Syntax Tree (AST) from tokens
- Evaluator: Walks the AST and executes operations
- Hardware Abstraction Layer (HAL): Translates high-level commands to serial frames
Execution Modes
| Mode | Description | Use Case |
|---|---|---|
| Interactive | REPL over serial link | Development and debugging |
| Headless IPC | JSON lines on stdin/stdout | Testing and automation |
| Send | Inject script via TCP:9000 | Remote deployment |
| Build | Transpile to Intel HEX | Firmware compilation |
| Flash | Burn script to firmware | Standalone deployment |
TCP Control Server
The monitor runs a background TCP server on port 9000. External tools (web IDE, admin panel, VS Code extension) can connect to inject scripts, query status, and receive output in real time.
Client connects → TCP:9000
→ Sends: "run (SET x 42)\n"
→ Monitor executes the expression
→ Returns: "ok\n"
Layer 4: Package & IoT Services
Package Registry
The package registry (hilp/registry/server.js) is a Node.js HTTP service that provides npm-like package management. It stores package metadata in index.json and package tarballs in data/packages/.
IoT Integration
HIL supports MQTT, WebSocket, and HTTP for IoT communication. The MQTT client can publish sensor data and subscribe to control topics:
; Publish temperature to MQTT
(SET temp (LM35))
(MQTT-PUBLISH "sensors/temp" temp)
; Subscribe to a control topic
(MQTT-SUBSCRIBE "actuators/fan")
Layer 5: User Interface
Web REPL
The landing page at hil-os.org includes an interactive LISP REPL that runs entirely in the browser. It uses the same tokenizer, parser, and evaluator as the host monitor, implemented in JavaScript.
Admin Panel
The admin panel at hil-os.org/admin provides:
- Dashboard with system metrics
- Package management
- Device monitoring
- OTA firmware updates
- Error reporting and resolution
Cross-Cutting Concerns
Error Handling
Security
- Admin token authentication (random on startup if not configured)
- Session cookies with HttpOnly and SameSite flags
- Rate limiting on API endpoints
- Input validation and sanitization on all user-provided data
References
Atmel. (2016). ATmega328P datasheet (Rev. 8271D). Microchip Technology.
Rashwan, S. (2026). HIL: A LISP-based hardware-in-the-loop operating system. GitHub Repository. https://github.com/SRashwan/hilp
Stallings, W. (2020). Computer organization and architecture (11th ed.). Pearson.