| 1 | // Copyright 2010-2025 Chris Spiegel. |
| 2 | // |
| 3 | // SPDX-License-Identifier: MIT |
| 4 | |
| 5 | #include <array> |
| 6 | #include <functional> |
| 7 | |
| 8 | #ifdef ZTERP_GLK_TICK |
| 9 | extern "C" { |
| 10 | #include <glk.h> |
| 11 | } |
| 12 | #endif |
| 13 | |
| 14 | #include "process.h" |
| 15 | #include "branch.h" |
| 16 | #include "dict.h" |
| 17 | #include "mathop.h" |
| 18 | #include "memory.h" |
| 19 | #include "objects.h" |
| 20 | #include "options.h" |
| 21 | #include "random.h" |
| 22 | #include "screen.h" |
| 23 | #include "sound.h" |
| 24 | #include "stack.h" |
| 25 | #include "types.h" |
| 26 | #include "util.h" |
| 27 | #include "zterp.h" |
| 28 | |
| 29 | #ifdef ZTERP_ZOOM_OPCODES |
| 30 | #include "zoom.h" |
| 31 | #endif |
| 32 | |
| 33 | unsigned long pc; |
| 34 | unsigned long current_instruction; |
| 35 | |
| 36 | std::array<uint16_t, 8> zargs; |
| 37 | int znargs; |
| 38 | |
| 39 | // Track the current processing level: 1 for the “main” loop, 2 if |
| 40 | // inside of an internal call, 3 if inside of an internal call inside of |
| 41 | // an internal call, and so on. |
| 42 | static int processing_level = 0; |
| 43 | |
| 44 | // In general an “internal” call is used for interrupts, and saving is |
| 45 | // not allowed in an interrupt. But the @shogun_menu hack has to use an |
| 46 | // internal call as well, and in that call, saving must work. This flag |
| 47 | // indicates that the current internal call is not actually an |
| 48 | // interrupt. Theoretically this is insufficient because an interrupt |
| 49 | // could be called while inside of @shogun_menu’s internal call, but |
| 50 | // that doesn’t actually happen. |
| 51 | bool interrupt_override = false; |
| 52 | |
| 53 | bool in_interrupt() |
| 54 | { |
| 55 | return processing_level > 1 && !interrupt_override; |
| 56 | } |
| 57 | |
| 58 | // Returns true if decoded, false otherwise (omitted) |
| 59 | static bool decode_base(uint8_t type, uint16_t &loc) |
| 60 | { |
| 61 | switch (type) { |
| 62 | case 0: // Large constant. |
| 63 | loc = word(pc); |
| 64 | pc += 2; |
| 65 | break; |
| 66 | case 1: // Small constant. |
| 67 | loc = byte(pc++); |
| 68 | break; |
| 69 | case 2: // Variable. |
| 70 | loc = variable(byte(pc++)); |
| 71 | break; |
| 72 | default: // Omitted. |
| 73 | return false; |
| 74 | } |
| 75 | |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | static void decode_var(uint8_t types) |
| 80 | { |
| 81 | for (int i = 6; i >= 0; i -= 2) { |
| 82 | uint16_t ret; |
| 83 | if (!decode_base((types >> i) & 0x03, ret)) { |
| 84 | return; |
| 85 | } |
| 86 | zargs[znargs++] = ret; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static std::array<void(*)(), 256> opcodes; |
| 91 | static std::array<void(*)(), 256> ext_opcodes; |
| 92 | |
| 93 | enum class Opcount { |
| 94 | Zero, |
| 95 | One, |
| 96 | Two, |
| 97 | Var, |
| 98 | Ext, |
| 99 | }; |
| 100 | |
| 101 | // This nifty trick is from Frotz. |
| 102 | static void zextended() |
| 103 | { |
| 104 | uint8_t opcode = byte(pc++); |
| 105 | |
| 106 | decode_var(byte(pc++)); |
| 107 | |
| 108 | ext_opcodes[opcode](); |
| 109 | } |
| 110 | |
| 111 | [[noreturn]] |
| 112 | static void illegal_opcode() |
| 113 | { |
| 114 | die("illegal opcode (pc = 0x%lx)", current_instruction); |
| 115 | } |
| 116 | |
| 117 | static void setup_single_opcode(int minver, int maxver, Opcount opcount, int opcode, void (*fn)()) |
| 118 | { |
| 119 | if (zversion < minver || zversion > maxver) { |
| 120 | return; |
| 121 | } |
| 122 | |
| 123 | switch (opcount) { |
| 124 | case Opcount::Zero: |
| 125 | opcodes[opcode + 176] = fn; |
| 126 | break; |
| 127 | case Opcount::One: |
| 128 | opcodes[opcode + 128] = fn; |
| 129 | opcodes[opcode + 144] = fn; |
| 130 | opcodes[opcode + 160] = fn; |
| 131 | break; |
| 132 | case Opcount::Two: |
| 133 | opcodes[opcode + 0] = fn; |
| 134 | opcodes[opcode + 32] = fn; |
| 135 | opcodes[opcode + 64] = fn; |
| 136 | opcodes[opcode + 96] = fn; |
| 137 | opcodes[opcode + 192] = fn; |
| 138 | break; |
| 139 | case Opcount::Var: |
| 140 | opcodes[opcode + 224] = fn; |
| 141 | break; |
| 142 | case Opcount::Ext: |
| 143 | ext_opcodes[opcode] = fn; |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void setup_opcodes() |
| 149 | { |
| 150 | opcodes.fill(illegal_opcode); |
| 151 | |
| 152 | // §14.2.1 |
| 153 | ext_opcodes.fill(znop); |
| 154 | |
| 155 | setup_single_opcode(1, 6, Opcount::Zero, 0x00, zrtrue); |
| 156 | setup_single_opcode(1, 6, Opcount::Zero, 0x01, zrfalse); |
| 157 | setup_single_opcode(1, 6, Opcount::Zero, 0x02, zprint); |
| 158 | setup_single_opcode(1, 6, Opcount::Zero, 0x03, zprint_ret); |
| 159 | setup_single_opcode(1, 6, Opcount::Zero, 0x04, znop); |
| 160 | setup_single_opcode(1, 4, Opcount::Zero, 0x05, zsave); |
| 161 | setup_single_opcode(1, 4, Opcount::Zero, 0x06, zrestore); |
| 162 | setup_single_opcode(1, 6, Opcount::Zero, 0x07, zrestart); |
| 163 | setup_single_opcode(1, 6, Opcount::Zero, 0x08, zret_popped); |
| 164 | setup_single_opcode(1, 4, Opcount::Zero, 0x09, zpop); |
| 165 | setup_single_opcode(5, 6, Opcount::Zero, 0x09, zcatch); |
| 166 | setup_single_opcode(1, 6, Opcount::Zero, 0x0a, zquit); |
| 167 | setup_single_opcode(1, 6, Opcount::Zero, 0x0b, znew_line); |
| 168 | setup_single_opcode(3, 3, Opcount::Zero, 0x0c, zshow_status); |
| 169 | setup_single_opcode(3, 6, Opcount::Zero, 0x0d, zverify); |
| 170 | setup_single_opcode(5, 6, Opcount::Zero, 0x0e, zextended); |
| 171 | setup_single_opcode(5, 6, Opcount::Zero, 0x0f, zpiracy); |
| 172 | |
| 173 | setup_single_opcode(1, 6, Opcount::One, 0x00, zjz); |
| 174 | setup_single_opcode(1, 6, Opcount::One, 0x01, zget_sibling); |
| 175 | setup_single_opcode(1, 6, Opcount::One, 0x02, zget_child); |
| 176 | setup_single_opcode(1, 6, Opcount::One, 0x03, zget_parent); |
| 177 | setup_single_opcode(1, 6, Opcount::One, 0x04, zget_prop_len); |
| 178 | setup_single_opcode(1, 6, Opcount::One, 0x05, zinc); |
| 179 | setup_single_opcode(1, 6, Opcount::One, 0x06, zdec); |
| 180 | setup_single_opcode(1, 6, Opcount::One, 0x07, zprint_addr); |
| 181 | setup_single_opcode(4, 6, Opcount::One, 0x08, zcall_1s); |
| 182 | setup_single_opcode(1, 6, Opcount::One, 0x09, zremove_obj); |
| 183 | setup_single_opcode(1, 6, Opcount::One, 0x0a, zprint_obj); |
| 184 | setup_single_opcode(1, 6, Opcount::One, 0x0b, zret); |
| 185 | setup_single_opcode(1, 6, Opcount::One, 0x0c, zjump); |
| 186 | setup_single_opcode(1, 6, Opcount::One, 0x0d, zprint_paddr); |
| 187 | setup_single_opcode(1, 6, Opcount::One, 0x0e, zload); |
| 188 | setup_single_opcode(1, 4, Opcount::One, 0x0f, znot); |
| 189 | setup_single_opcode(5, 6, Opcount::One, 0x0f, zcall_1n); |
| 190 | |
| 191 | setup_single_opcode(1, 6, Opcount::Two, 0x01, zje); |
| 192 | setup_single_opcode(1, 6, Opcount::Two, 0x02, zjl); |
| 193 | setup_single_opcode(1, 6, Opcount::Two, 0x03, zjg); |
| 194 | setup_single_opcode(1, 6, Opcount::Two, 0x04, zdec_chk); |
| 195 | setup_single_opcode(1, 6, Opcount::Two, 0x05, zinc_chk); |
| 196 | setup_single_opcode(1, 6, Opcount::Two, 0x06, zjin); |
| 197 | setup_single_opcode(1, 6, Opcount::Two, 0x07, ztest); |
| 198 | setup_single_opcode(1, 6, Opcount::Two, 0x08, zor); |
| 199 | setup_single_opcode(1, 6, Opcount::Two, 0x09, zand); |
| 200 | setup_single_opcode(1, 6, Opcount::Two, 0x0a, ztest_attr); |
| 201 | setup_single_opcode(1, 6, Opcount::Two, 0x0b, zset_attr); |
| 202 | setup_single_opcode(1, 6, Opcount::Two, 0x0c, zclear_attr); |
| 203 | setup_single_opcode(1, 6, Opcount::Two, 0x0d, zstore); |
| 204 | setup_single_opcode(1, 6, Opcount::Two, 0x0e, zinsert_obj); |
| 205 | setup_single_opcode(1, 6, Opcount::Two, 0x0f, zloadw); |
| 206 | setup_single_opcode(1, 6, Opcount::Two, 0x10, zloadb); |
| 207 | setup_single_opcode(1, 6, Opcount::Two, 0x11, zget_prop); |
| 208 | setup_single_opcode(1, 6, Opcount::Two, 0x12, zget_prop_addr); |
| 209 | setup_single_opcode(1, 6, Opcount::Two, 0x13, zget_next_prop); |
| 210 | setup_single_opcode(1, 6, Opcount::Two, 0x14, zadd); |
| 211 | setup_single_opcode(1, 6, Opcount::Two, 0x15, zsub); |
| 212 | setup_single_opcode(1, 6, Opcount::Two, 0x16, zmul); |
| 213 | setup_single_opcode(1, 6, Opcount::Two, 0x17, zdiv); |
| 214 | setup_single_opcode(1, 6, Opcount::Two, 0x18, zmod); |
| 215 | setup_single_opcode(4, 6, Opcount::Two, 0x19, zcall_2s); |
| 216 | setup_single_opcode(5, 6, Opcount::Two, 0x1a, zcall_2n); |
| 217 | setup_single_opcode(5, 6, Opcount::Two, 0x1b, zset_colour); |
| 218 | setup_single_opcode(5, 6, Opcount::Two, 0x1c, zthrow); |
| 219 | |
| 220 | setup_single_opcode(1, 6, Opcount::Var, 0x00, zcall); |
| 221 | setup_single_opcode(1, 6, Opcount::Var, 0x01, zstorew); |
| 222 | setup_single_opcode(1, 6, Opcount::Var, 0x02, zstoreb); |
| 223 | setup_single_opcode(1, 6, Opcount::Var, 0x03, zput_prop); |
| 224 | setup_single_opcode(1, 6, Opcount::Var, 0x04, zread); |
| 225 | setup_single_opcode(1, 6, Opcount::Var, 0x05, zprint_char); |
| 226 | setup_single_opcode(1, 6, Opcount::Var, 0x06, zprint_num); |
| 227 | setup_single_opcode(1, 6, Opcount::Var, 0x07, zrandom); |
| 228 | setup_single_opcode(1, 6, Opcount::Var, 0x08, zpush); |
| 229 | setup_single_opcode(1, 6, Opcount::Var, 0x09, zpull); |
| 230 | setup_single_opcode(3, 6, Opcount::Var, 0x0a, zsplit_window); |
| 231 | setup_single_opcode(3, 6, Opcount::Var, 0x0b, zset_window); |
| 232 | setup_single_opcode(4, 6, Opcount::Var, 0x0c, zcall_vs2); |
| 233 | setup_single_opcode(4, 6, Opcount::Var, 0x0d, zerase_window); |
| 234 | setup_single_opcode(4, 6, Opcount::Var, 0x0e, zerase_line); |
| 235 | setup_single_opcode(4, 6, Opcount::Var, 0x0f, zset_cursor); |
| 236 | setup_single_opcode(4, 6, Opcount::Var, 0x10, zget_cursor); |
| 237 | setup_single_opcode(4, 6, Opcount::Var, 0x11, zset_text_style); |
| 238 | setup_single_opcode(4, 6, Opcount::Var, 0x12, znop); // XXX buffer_mode |
| 239 | setup_single_opcode(3, 6, Opcount::Var, 0x13, zoutput_stream); |
| 240 | setup_single_opcode(3, 6, Opcount::Var, 0x14, zinput_stream); |
| 241 | setup_single_opcode(3, 6, Opcount::Var, 0x15, zsound_effect); |
| 242 | setup_single_opcode(4, 6, Opcount::Var, 0x16, zread_char); |
| 243 | setup_single_opcode(4, 6, Opcount::Var, 0x17, zscan_table); |
| 244 | setup_single_opcode(5, 6, Opcount::Var, 0x18, znot); |
| 245 | setup_single_opcode(5, 6, Opcount::Var, 0x19, zcall_vn); |
| 246 | setup_single_opcode(5, 6, Opcount::Var, 0x1a, zcall_vn2); |
| 247 | setup_single_opcode(5, 6, Opcount::Var, 0x1b, ztokenise); |
| 248 | setup_single_opcode(5, 6, Opcount::Var, 0x1c, zencode_text); |
| 249 | setup_single_opcode(5, 6, Opcount::Var, 0x1d, zcopy_table); |
| 250 | setup_single_opcode(5, 6, Opcount::Var, 0x1e, zprint_table); |
| 251 | setup_single_opcode(5, 6, Opcount::Var, 0x1f, zcheck_arg_count); |
| 252 | |
| 253 | setup_single_opcode(5, 6, Opcount::Ext, 0x00, zsave5); |
| 254 | setup_single_opcode(5, 6, Opcount::Ext, 0x01, zrestore5); |
| 255 | setup_single_opcode(5, 6, Opcount::Ext, 0x02, zlog_shift); |
| 256 | setup_single_opcode(5, 6, Opcount::Ext, 0x03, zart_shift); |
| 257 | setup_single_opcode(5, 6, Opcount::Ext, 0x04, zset_font); |
| 258 | #ifndef ZTERP_NO_V6 |
| 259 | setup_single_opcode(6, 6, Opcount::Ext, 0x05, zdraw_picture); |
| 260 | setup_single_opcode(6, 6, Opcount::Ext, 0x06, zpicture_data); |
| 261 | setup_single_opcode(6, 6, Opcount::Ext, 0x07, znop); // XXX erase_picture |
| 262 | setup_single_opcode(6, 6, Opcount::Ext, 0x08, znop); // XXX set_margins |
| 263 | #endif |
| 264 | setup_single_opcode(5, 6, Opcount::Ext, 0x09, zsave_undo); |
| 265 | setup_single_opcode(5, 6, Opcount::Ext, 0x0a, zrestore_undo); |
| 266 | setup_single_opcode(5, 6, Opcount::Ext, 0x0b, zprint_unicode); |
| 267 | setup_single_opcode(5, 6, Opcount::Ext, 0x0c, zcheck_unicode); |
| 268 | setup_single_opcode(5, 6, Opcount::Ext, 0x0d, zset_true_colour); |
| 269 | #ifndef ZTERP_NO_V6 |
| 270 | setup_single_opcode(6, 6, Opcount::Ext, 0x10, znop); // XXX move_window |
| 271 | setup_single_opcode(6, 6, Opcount::Ext, 0x11, znop); // XXX window_size |
| 272 | setup_single_opcode(6, 6, Opcount::Ext, 0x12, znop); // XXX window_style |
| 273 | setup_single_opcode(6, 6, Opcount::Ext, 0x13, zget_wind_prop); |
| 274 | setup_single_opcode(6, 6, Opcount::Ext, 0x14, znop); // XXX scroll_window |
| 275 | setup_single_opcode(6, 6, Opcount::Ext, 0x15, zpop_stack); |
| 276 | setup_single_opcode(6, 6, Opcount::Ext, 0x16, znop); // XXX read_mouse |
| 277 | setup_single_opcode(6, 6, Opcount::Ext, 0x17, znop); // XXX mouse_window |
| 278 | setup_single_opcode(6, 6, Opcount::Ext, 0x18, zpush_stack); |
| 279 | setup_single_opcode(6, 6, Opcount::Ext, 0x19, znop); // XXX put_wind_prop |
| 280 | setup_single_opcode(6, 6, Opcount::Ext, 0x1a, zprint_form); |
| 281 | setup_single_opcode(6, 6, Opcount::Ext, 0x1b, zmake_menu); |
| 282 | setup_single_opcode(6, 6, Opcount::Ext, 0x1c, znop); // XXX picture_table |
| 283 | setup_single_opcode(6, 6, Opcount::Ext, 0x1d, zbuffer_screen); |
| 284 | #endif |
| 285 | |
| 286 | #ifdef ZTERP_ZOOM_OPCODES |
| 287 | // Zoom extensions. |
| 288 | setup_single_opcode(5, 6, Opcount::Ext, 0x80, zstart_timer); |
| 289 | setup_single_opcode(5, 6, Opcount::Ext, 0x81, zstop_timer); |
| 290 | setup_single_opcode(5, 6, Opcount::Ext, 0x82, zread_timer); |
| 291 | setup_single_opcode(5, 6, Opcount::Ext, 0x83, zprint_timer); |
| 292 | #endif |
| 293 | |
| 294 | #ifndef ZTERP_NO_V6 |
| 295 | // V6 hacks. |
| 296 | setup_single_opcode(6, 6, Opcount::Ext, JOURNEY_DIAL_EXT, zjourney_dial); |
| 297 | setup_single_opcode(6, 6, Opcount::Ext, SHOGUN_MENU_EXT, zshogun_menu); |
| 298 | #endif |
| 299 | } |
| 300 | |
| 301 | // The main processing loop. This decodes and dispatches instructions. |
| 302 | // It will be called both at program start and on an internal call. |
| 303 | void process_instructions() |
| 304 | { |
| 305 | static bool handled_autosave = false; |
| 306 | |
| 307 | if (options.autosave && !options.skip_autorestore && !handled_autosave) { |
| 308 | SaveOpcode saveopcode; |
| 309 | SaveType savetype = options.autosave_librarystate ? SaveType::AutosaveLib : SaveType::Autosave; |
| 310 | |
| 311 | handled_autosave = true; |
| 312 | |
| 313 | if (do_restore(savetype, saveopcode)) { |
| 314 | if (savetype == SaveType::Autosave) { |
| 315 | show_message("Continuing last session from autosave"); |
| 316 | } |
| 317 | throw Operation::Restore(saveopcode); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | processing_level++; |
| 322 | |
| 323 | while (true) { |
| 324 | #ifdef ZTERP_GLK_TICK |
| 325 | glk_tick(); |
| 326 | #endif |
| 327 | |
| 328 | current_instruction = pc; |
| 329 | uint8_t opcode = byte(pc++); |
| 330 | |
| 331 | if (opcode < 0x80) { // long 2OP |
| 332 | znargs = 2; |
| 333 | |
| 334 | if ((opcode & 0x40) == 0x40) { |
| 335 | zargs[0] = variable(byte(pc++)); |
| 336 | } else { |
| 337 | zargs[0] = byte(pc++); |
| 338 | } |
| 339 | |
| 340 | if ((opcode & 0x20) == 0x20) { |
| 341 | zargs[1] = variable(byte(pc++)); |
| 342 | } else { |
| 343 | zargs[1] = byte(pc++); |
| 344 | } |
| 345 | } else if (opcode < 0xb0) { // short 1OP |
| 346 | znargs = 1; |
| 347 | |
| 348 | if ((opcode & 0x20) == 0x20) { |
| 349 | zargs[0] = variable(byte(pc++)); |
| 350 | } else if ((opcode & 0x10) == 0x10) { |
| 351 | zargs[0] = byte(pc++); |
| 352 | } else { |
| 353 | zargs[0] = word(pc); |
| 354 | pc += 2; |
| 355 | } |
| 356 | } else if (opcode < 0xc0) { // short 0OP (plus EXT) |
| 357 | znargs = 0; |
| 358 | } else if (opcode == 0xec || opcode == 0xfa) { // Double variable VAR |
| 359 | uint8_t types1 = byte(pc++); |
| 360 | uint8_t types2 = byte(pc++); |
| 361 | |
| 362 | znargs = 0; |
| 363 | decode_var(types1); |
| 364 | decode_var(types2); |
| 365 | } else { // variable 2OP and VAR |
| 366 | znargs = 0; |
| 367 | |
| 368 | decode_var(byte(pc++)); |
| 369 | } |
| 370 | |
| 371 | try { |
| 372 | opcodes[opcode](); |
| 373 | } catch (const Operation::Return &) { |
| 374 | processing_level--; |
| 375 | return; |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | // A wrapper around process_instructions() which is responsible for |
| 381 | // dealing with restart, restore, and quit. |
| 382 | void process_loop() |
| 383 | { |
| 384 | std::function<void()> synthetic_call = nullptr; |
| 385 | |
| 386 | while (true) { |
| 387 | try { |
| 388 | if (synthetic_call != nullptr) { |
| 389 | synthetic_call(); |
| 390 | synthetic_call = nullptr; |
| 391 | } |
| 392 | |
| 393 | processing_level = 0; |
| 394 | process_instructions(); |
| 395 | } catch (const Operation::Restart &) { |
| 396 | start_story(); |
| 397 | } catch (const Operation::Restore &restore) { |
| 398 | switch (restore.saveopcode) { |
| 399 | case SaveOpcode::None: |
| 400 | synthetic_call = nullptr; |
| 401 | break; |
| 402 | case SaveOpcode::Read: |
| 403 | synthetic_call = zread; |
| 404 | break; |
| 405 | case SaveOpcode::ReadChar: |
| 406 | synthetic_call = zread_char; |
| 407 | break; |
| 408 | case SaveOpcode::Save: |
| 409 | if (zversion < 5) { |
| 410 | synthetic_call = zsave; |
| 411 | } else { |
| 412 | synthetic_call = zsave5; |
| 413 | } |
| 414 | break; |
| 415 | case SaveOpcode::Restore: |
| 416 | if (zversion < 5) { |
| 417 | synthetic_call = zrestore; |
| 418 | } else { |
| 419 | synthetic_call = zrestore5; |
| 420 | } |
| 421 | break; |
| 422 | } |
| 423 | } catch (const Operation::Quit &) { |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | } |