Skip to main content

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
FieldDescription
ACTIONSingle character: W (write), R (read), T (tone), S (pulse), E (event), C (change)
TYPEPin type: D (digital), A (analog), P (PWM alias)
PINPin number
VAL1Primary value (state, threshold, frequency)
VAL2Secondary value (duration, edge type)
CRCXOR checksum of all bytes before `
\rCarriage 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​

  1. Tokenizer: Converts raw text into tokens (symbols, numbers, strings, parentheses)
  2. Parser: Builds an Abstract Syntax Tree (AST) from tokens
  3. Evaluator: Walks the AST and executes operations
  4. Hardware Abstraction Layer (HAL): Translates high-level commands to serial frames

Execution Modes​

ModeDescriptionUse Case
InteractiveREPL over serial linkDevelopment and debugging
Headless IPCJSON lines on stdin/stdoutTesting and automation
SendInject script via TCP:9000Remote deployment
BuildTranspile to Intel HEXFirmware compilation
FlashBurn script to firmwareStandalone 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.