| 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 version is |
| 7 | * RETRO_CONTROL_CONTRACT (below); the server advertises the highest level whose |
| 8 | * backend callbacks are all present. |
| 9 | * |
| 10 | * Integration is three calls in the emulator's main loop: |
| 11 | * retro_control_service(); // once per iteration (cheap) |
| 12 | * if (!retro_control_running()) { idle; } // gate advancing the machine |
| 13 | * ... run one instruction / frame ... |
| 14 | * if (frame_completed) retro_control_on_frame(); |
| 15 | * |
| 16 | * Threading: the server runs on its own thread but never touches machine state |
| 17 | * there. State-reading endpoints (/mem, /regs, /screenshot) are executed inside |
| 18 | * retro_control_service() ON THE EMULATOR THREAD, so every read is consistent. |
| 19 | * The backend callbacks are therefore called from the emulator thread (except |
| 20 | * get_frame_count, which the server may also read for /status — make it a plain |
| 21 | * atomic-ish counter read). |
| 22 | */ |
| 23 | |
| 24 | |
| 25 | |
| 26 | |
| 27 | |
| 28 | |
| 29 | /* Highest contract the core implements. The server advertises the highest level |
| 30 | * whose backend callbacks are all present: 0.6.0 needs get_perf + set_perf (on |
| 31 | * top of 0.5); 0.5.0 needs set_pad + get_pad (on top of 0.4); 0.4.0 needs |
| 32 | * set_pointer + get_pointer (on top of 0.3); 0.3.0 needs inject_key + |
| 33 | * write_mem + capture_audio; 0.2.0 needs inject_key; else 0.1.0. Keeps a |
| 34 | * partial backend honest. /status.capabilities lists each group a backend |
| 35 | * serves whatever level it reaches, so a client can ask for one directly. */ |
| 36 | |
| 37 | |
| 38 | /* Native framebuffer pixel layout; the server swizzles to PPM RGB. */ |
| 39 | typedef enum |
| 40 | |
| 41 | |
| 42 | |
| 43 | retro_pix_fmt_t; |
| 44 | |
| 45 | typedef struct |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | retro_framebuffer_t; |
| 51 | |
| 52 | /* Key action for inject_key (0.2). */ |
| 53 | typedef enum |
| 54 | |
| 55 | |
| 56 | |
| 57 | retro_key_action_t; |
| 58 | |
| 59 | /* The emulator implements these. Any may be NULL: the matching endpoint then |
| 60 | * returns 501 Not Implemented. */ |
| 61 | typedef struct retro_control_backend |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | retro_control_backend_t; |
| 162 | |
| 163 | /* Start the server thread bound to 127.0.0.1:port. Non-blocking; 0 on success, |
| 164 | * negative on error. backend must outlive the server. */ |
| 165 | int ; |
| 166 | |
| 167 | /* Drain a pending control request. Call once per main-loop iteration, at an |
| 168 | * instruction/frame boundary. Cheap no-op when idle and when control is off. */ |
| 169 | void ; |
| 170 | |
| 171 | /* Nonzero => the machine should advance this iteration. Returns 1 always when |
| 172 | * control is off or the machine is free-running; 0 when paused with no steps |
| 173 | * left. */ |
| 174 | int ; |
| 175 | |
| 176 | /* Call when a video frame completes. Drives /step (decrements the step budget, |
| 177 | * halts and answers when it reaches zero). No-op when control is off. */ |
| 178 | void ; |
| 179 | |
| 180 | /* Stop the server (optional; process exit is fine too). */ |
| 181 | void ; |
| 182 | |
| 183 | /* RETRO_CONTROL_H */ |