OXYGENHUB
# d44319b1e712 retro-remote-debug-controller / SPEC.md
9.2 KiB · Markdown · last changed Contract 0.2.0: input injection (POST /key, POST /reset) 3 months ago renderedsource blame history raw
1
# Retro Remote Debug Controller — HTTP Control Contract
2
3
**Contract version: 0.2.0** (semver; clients assert on the MAJOR). 0.2 adds
4
input injection (`POST /key`, `POST /reset`); it is purely additive, so 0.1
5
clients keep working and servers advertise their level via `/status.contract`.
6
7
A minimal, portable HTTP API that a retro-platform emulator exposes so an
8
external harness can screenshot the screen, read memory and registers, step the
9
machine deterministically, and inject input. The same contract is implemented
10
by the forked FAB Agon Emulator, the Neo6502 emulator, and the Commander X16
11
emulator, so **one** pytest harness drives all three ports.
12
13
## Principles
14
15
- **Opt-in.** The server is OFF by default and starts only when the emulator is
16
  launched with a control-port flag. Zero cost to normal use.
17
- **Localhost only.** The server MUST bind `127.0.0.1` (never `0.0.0.0`).
18
  `/mem` and input injection are a memory-read / remote-control surface; they do
19
  not leave the machine.
20
- **The contract is the HTTP surface, not the flag.** Each emulator names its
21
  launch flag idiomatically (x16emu: `-controlport N`; FAB: `--control-port N`).
22
  Only the endpoints below are normative.
23
- **Determinism is a first-class requirement.** These are frame-exact ports.
24
  Every state read (`/mem`, `/regs`, `/screenshot`) MUST observe a consistent
25
  machine state — captured at an instruction/frame boundary on the emulator
26
  thread, never a torn mid-instruction read from the HTTP thread. See
27
  *Consistency* below.
28
29
## Conventions
30
31
- JSON responses are `application/json; charset=utf-8`.
32
- Binary responses use the stated content-type; no chunked encoding required.
33
- Errors: HTTP 4xx/5xx with body `{"error":"<message>"}`.
34
- Numbers in JSON are decimal unless a field is documented as a hex string.
35
- Methods are exactly as listed (`GET`/`POST`). Unknown path → 404. Unknown
36
  method on a known path → 405.
37
38
## Endpoints
39
40
### `GET /status`
41
Liveness + contract negotiation. Always available.
42
```json
43
{
44
  "contract": "0.1.0",
45
  "emulator": "x16emu",
46
  "platform": "x16",
47
  "frame": 12345,
48
  "paused": true,
49
  "running": true
50
}
51
```
52
- `contract` — this document's version. Clients MUST reject a mismatched MAJOR.
53
- `platform` — one of `x16`, `neo6502`, `agon` (extensible). Tells the client
54
  the memory/register model (see *Per-platform* appendix).
55
- `frame` — monotonic completed-frame counter since boot. The determinism
56
  anchor: a screenshot taken at `frame == N` shows the state after N complete
57
  frames.
58
- `paused` — true if the machine is halted (only advances via `/step`).
59
60
### `GET /screenshot`
61
The live screen as a Portable Pixmap.
62
- Response: `image/x-portable-pixmap`, binary **PPM P6**, 8-bit RGB, no alpha.
63
- Pixels are the most recent COMPLETE frame (never a half-scanned frame). The
64
  emulator keeps a snapshot the render thread fills at frame end; the HTTP
65
  thread serves that snapshot.
66
- Dimensions are the platform's native framebuffer (appendix). Clients read
67
  `width`/`height` from the PPM header — do not hardcode.
68
- Reserved: `?frame=N` (block until frame N) — not required in 0.1.
69
70
### `GET /mem?addr=<a>&len=<n>[&bank=<b>]`
71
Raw memory bytes.
72
- `addr` — start address, decimal or `0x`-hex. Interpreted in the platform's
73
  address space (appendix).
74
- `len` — byte count, decimal. Servers MAY cap `len` (recommend ≥ 65536); a
75
  capped request returns what it can with `Content-Length` telling the truth.
76
- `bank` — OPTIONAL bank selector for banked platforms (X16). Omitted → the
77
  machine's currently-selected bank. Ignored by flat-memory platforms.
78
- Response: `application/octet-stream`, exactly the requested bytes.
79
- Reads go through the emulator's debug-read path (no side effects, no I/O
80
  triggers, no access-flag mutation).
81
82
### `GET /regs`
83
CPU registers as JSON. Keys are platform-specific (appendix); values are decimal
84
unless suffixed `_hex`. Example (x16 / 65C02):
85
```json
86
{ "a":0, "x":16, "y":2, "sp":505, "pc":49152, "status":32,
87
  "ram_bank":1, "rom_bank":0 }
88
```
89
90
### `POST /step?frames=<n>`  *(recommended; required for frame-exact tests)*
91
Advance the machine by exactly `n` complete frames (default 1), then halt.
92
- Only meaningful when the machine is paused (see `/pause`). On a free-running
93
  machine, servers SHOULD pause, run `n` frames, and stay paused.
94
- Response: `{ "frame": <new frame counter> }`.
95
- `POST /step?cycles=<n>` MAY be offered for sub-frame stepping.
96
97
### `POST /pause` / `POST /resume`  *(recommended)*
98
Halt / free-run. `{ "paused": true|false }`. A machine launched with a
99
`--control-pause`-style option SHOULD boot paused so tests are deterministic
100
from frame 0.
101
102
### `POST /key?text=<char> | ?code=<n>  [&down=0|1]`  *(0.2)*
103
Inject a key. Query params (like `/mem` and `/step`), no body:
104
- `text` — a single character, mapped to the platform's keyboard.
105
- `code` — a raw platform key code (e.g. fabgl virtual key on Agon, an SDL/PS2
106
  code on others). `text` and `code` are alternatives; provide one.
107
- `down` — `1` presses (holds), `0` releases; **omitted taps** (press then
108
  release). Holding across `/step` frames is how a program that samples input
109
  each frame (e.g. a game) actually sees the key.
110
- Response: `{ "injected": true }`. Missing/unmapped key → 400.
111
112
Injection plus `/step` retires scripted-autoexec input: press → step → assert →
113
release, deterministically.
114
115
### `POST /reset`  *(0.2)*
116
Soft/cold reset the machine. Response `{ "reset": true }`. The `frame` counter
117
MAY reset here (the one case `/status.frame` is allowed to go backwards).
118
119
## Consistency (normative)
120
121
- `/mem`, `/regs`, and `/step` MUST be serviced on the emulator thread at an
122
  instruction boundary — e.g. the HTTP handler enqueues a command that the main
123
  loop drains between instructions/frames and signals back. The HTTP thread MUST
124
  NOT read CPU/RAM state directly while the CPU is executing.
125
- `/screenshot` MUST serve a complete-frame snapshot (double-buffer: render
126
  thread writes the back buffer, swaps under a lock at frame end; HTTP thread
127
  reads the front buffer).
128
- `frame` in `/status` MUST increment once per completed video frame and never
129
  go backwards (except across `/reset`).
130
131
## Versioning
132
133
- Semantic. MAJOR bump = breaking change to an existing endpoint's
134
  request/response. MINOR = additive (new endpoint / optional field). PATCH =
135
  clarification.
136
- Clients read `/status.contract` and fail fast on a MAJOR mismatch.
137
138
---
139
140
## Per-platform appendix
141
142
### Commander X16 — `x16emu`
143
- Flag: `-controlport <N>` (single-dash, matching x16emu's `-dump` style).
144
- Framebuffer: **640×480**, native order BGRA8888 (`framebuffer[i*4+2]=R`,
145
  `+1=G`, `+0=B`); the server swizzles to PPM RGB.
146
- Address space: 16-bit CPU (`0x0000–0xFFFF`). The banked window `0xA000–0xBFFF`
147
  is selected by `bank` → the emulator's `read6502(addr, bank)` / banked RAM
148
  (`BRAM`). `addr < 0xA000` or `≥ 0xC000` ignores `bank`.
149
- Registers: `a x y` (8), `sp pc` (16), `status` (8), plus `ram_bank`,
150
  `rom_bank`. Source: the `regs` struct + `memory_get_ram_bank/rom_bank()`.
151
- Frame source: `video.c` `frame_count`.
152
153
### Neo6502 — neo6502 emulator
154
- Flag: `--control-port <N>`.
155
- Address space: flat 64 KB; `bank` ignored.
156
- Registers: 6502 `a x y sp pc status`.
157
- Framebuffer: native resolution per the emulator's video mode.
158
159
### Agon (Console8 / Light 2) — FAB Agon Emulator
160
- **Adoption:** conform-only (Rust emulator — keeps its own HTTP server, matches
161
  this contract rather than vendoring the C core).
162
- Flag: `--control-port <N>`.
163
- Address space: eZ80 24-bit, flat 16 MB (`addr` up to `0xFFFFFF`); `bank`
164
  tolerated and ignored. Program load base `0x040000`.
165
- Registers (ADL) — `/regs` example (16-bit regs; `mbase` 8-bit; `adl` 0/1):
166
  ```json
167
  { "af":0, "bc":0, "de":0, "hl":0, "ix":0, "iy":0,
168
    "sp":0, "pc":262144, "mbase":0, "adl":1 }
169
  ```
170
- Framebuffer: from `vgabuf` (the VDP output); the offscreen path already keeps
171
  video alive (the FAB equivalent of x16's `SDL_VIDEODRIVER=dummy`).
172
- Input: `/key?text=c` maps the char to a fabgl virtual key (`ascii2vk`) and
173
  delivers it via `sendVKeyEventToFabgl`; `?code=<vk>` passes a raw fabgl vkey.
174
  The control thread queues events; the render thread delivers them (same
175
  thread as normal keyboard input). `/reset` triggers the soft-reset atomic.
176
177
---
178
179
## Adoption paths
180
181
An emulator conforms by exposing exactly the HTTP surface above — nothing else
182
is required. Two ways to get there:
183
184
- **Vendor the shared core** (`core/retro_control.c` + `.h`) — for C/C++
185
  emulators. Implement the four backend callbacks, wire three loop hooks, call
186
  `retro_control_start(port, &backend)`; the socket loop, HTTP parsing, PPM/JSON
187
  encoding and pause/step control come for free. The Commander X16 fork does this.
188
- **Conform directly** — for emulators in other languages (the FAB Agon emulator
189
  is Rust). Keep your own HTTP server; just match the contract and pass the
190
  conformance suite. Sharing the C core is an optimization, not a requirement —
191
  the contract is the asset.
192
193
Either way, `conformance/` is the arbiter: pass it and you conform.
194
195
## In this repo
196
197
- `core/retro_control.{c,h}` — portable server + backend vtable (vendor path).
198
- `client/emu_control.py` — the shared pytest client.
199
- `conformance/` — a platform-agnostic pytest that proves an emulator conforms.