| 1 | /* retro_control.h — portable emulator control server (see ../SPEC.md). |
| 2 | * |
| 3 | * A retro-platform emulator implements the small read-only backend below and |
| 4 | * calls retro_control_start(). The shared server (retro_control.c) owns the |
| 5 | * socket loop, HTTP parsing, PPM/JSON encoding, and control flow |
| 6 | * (pause / step / free-run). It always binds 127.0.0.1. Contract 0.1.0. |
| 7 | * |
| 8 | * Integration is three calls in the emulator's main loop: |
| 9 | * retro_control_service(); // once per iteration (cheap) |
| 10 | * if (!retro_control_running()) { idle; } // gate advancing the machine |
| 11 | * ... run one instruction / frame ... |
| 12 | * if (frame_completed) retro_control_on_frame(); |
| 13 | * |
| 14 | * Threading: the server runs on its own thread but never touches machine state |
| 15 | * there. State-reading endpoints (/mem, /regs, /screenshot) are executed inside |
| 16 | * retro_control_service() ON THE EMULATOR THREAD, so every read is consistent. |
| 17 | * The backend callbacks are therefore called from the emulator thread (except |
| 18 | * get_frame_count, which the server may also read for /status — make it a plain |
| 19 | * atomic-ish counter read). |
| 20 | */ |
| 21 | |
| 22 | |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | /* Native framebuffer pixel layout; the server swizzles to PPM RGB. */ |
| 30 | typedef enum |
| 31 | |
| 32 | |
| 33 | |
| 34 | retro_pix_fmt_t; |
| 35 | |
| 36 | typedef struct |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | retro_framebuffer_t; |
| 42 | |
| 43 | /* The emulator implements these. Any may be NULL: the matching endpoint then |
| 44 | * returns 501 Not Implemented. */ |
| 45 | typedef struct retro_control_backend |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | retro_control_backend_t; |
| 64 | |
| 65 | /* Start the server thread bound to 127.0.0.1:port. Non-blocking; 0 on success, |
| 66 | * negative on error. backend must outlive the server. */ |
| 67 | int ; |
| 68 | |
| 69 | /* Drain a pending control request. Call once per main-loop iteration, at an |
| 70 | * instruction/frame boundary. Cheap no-op when idle and when control is off. */ |
| 71 | void ; |
| 72 | |
| 73 | /* Nonzero => the machine should advance this iteration. Returns 1 always when |
| 74 | * control is off or the machine is free-running; 0 when paused with no steps |
| 75 | * left. */ |
| 76 | int ; |
| 77 | |
| 78 | /* Call when a video frame completes. Drives /step (decrements the step budget, |
| 79 | * halts and answers when it reaches zero). No-op when control is off. */ |
| 80 | void ; |
| 81 | |
| 82 | /* Stop the server (optional; process exit is fine too). */ |
| 83 | void ; |
| 84 | |
| 85 | /* RETRO_CONTROL_H */ |