Debian Codegraph @

terps/bocfel/patches.cpp

raw symbols SCIP
1 // Copyright 2017-2025 Chris Spiegel.
2 //
3 // SPDX-License-Identifier: MIT
4
5 #include <algorithm>
6 #include <cstring>
7 #include <fstream>
8 #include <functional>
9 #include <ios>
10 #include <iostream>
11 #include <sstream>
12 #include <string>
13 #include <vector>
14
15 #include "patches.h"
16 #include "memory.h"
17 #include "process.h"
18 #include "types.h"
19 #include "util.h"
20 #include "zterp.h"
21
22 #ifdef ZTERP_GLK
23 extern "C" {
24 #include <glk.h>
25 }
26 #endif
27
28 struct Replacement {
29 uint32_t addr;
30 uint32_t n;
31 std::vector<uint8_t> in;
32 std::vector<uint8_t> out;
33 std::function<bool()> active = [] { return true; };
34 };
35
36 struct Patch {
37 std::string title;
38 const char (&serial)[sizeof header.serial + 1];
39 uint16_t release;
40 uint16_t checksum;
41 std::vector<Replacement> replacements;
42 };
43
44 // The Bureaucracy patch assumes that timed input is available, as it
45 // uses that to simulate a sleep. If timed input is not available,
46 // though, it will instead just be a regular @read_char, which will
47 // block till the user hits a key. This is worse behavior than the
48 // default, which is to effectively not sleep at all. Timed input is
49 // only available with Glk, and then only if the Glk implementation
50 // supports timers.
51 #ifdef ZTERP_GLK
52 static bool bureaucracy_active()
53 {
54 return glk_gestalt(gestalt_Timer, 0);
55 }
56 #else
57 static bool bureaucracy_active()
58 {
59 return false;
60 }
61 #endif
62
63 static std::vector<Patch> base_patches = {
64 // In Arthur, there is a routine called INIT-STATUS-LINE which does this:
65 //
66 // <PUTB ,K-DIROUT-TBL 0 !\ >
67 // <SET N </ <WINGET 1 ,K-W-XSIZE> ,GL-SPACE-WIDTH>>
68 // <COPYT ,K-DIROUT-TBL <ZREST ,K-DIROUT-TBL 1> <- .N>>
69 //
70 // The intent is to fill K-DIROUT-TBL with as many spaces as there
71 // are characters in a line, but K-DIROUT-TBL is only 255 bytes
72 // wide. If K-W-XSIZE / GL-SPACE-WIDTH is greater than 255, it will
73 // overrun the buffer. This patch pulls the width from the value at
74 // 0x21 in the header, which is the width of the screen in
75 // characters, capped at 255.
76 {
77 "Arthur", "890606", 54, 0x8e4a,
78 {
79 {
80 0x9e29, 10,
81 {0xbe, 0x13, 0x5f, 0x01, 0x03, 0x00, 0x77, 0x00, 0x1f, 0x01},
82 {0x10, 0x00, 0x21, 0x01, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4},
83 }
84 }
85 },
86
87 {
88 "Arthur", "890622", 63, 0x45eb,
89 {
90 {
91 0x9e31, 10,
92 {0xbe, 0x13, 0x5f, 0x01, 0x03, 0x00, 0x77, 0x00, 0x1f, 0x01},
93 {0x10, 0x00, 0x21, 0x01, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4}
94 }
95 }
96 },
97
98 {
99 "Arthur", "890714", 74, 0xd526,
100 {
101 {
102 0x9de8, 10,
103 {0xbe, 0x13, 0x5f, 0x01, 0x03, 0x00, 0x77, 0x00, 0x1f, 0x01},
104 {0x10, 0x00, 0x21, 0x01, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4, 0xb4}
105 }
106 }
107 },
108
109 // There are several patches for Beyond Zork.
110 //
111 // In four places it tries to treat a dictionary word as an object.
112 // The offending code looks like:
113 //
114 // <REPLACE-SYN? ,W?CIRCLET ,W?FILM ,W?ZZZP>
115 // <REPLACE-ADJ? ,W?CIRCLET ,W?SWIRLING ,W?ZZZP>
116 //
117 // Here “,W?CIRCLET” is the dictionary word “circlet”; what was
118 // intended is “,CIRCLET”, the object. Given that the object number
119 // of the circlet is easily discoverable, these calls can be
120 // rewritten to use the object number instead of the incorrect
121 // dictionary entry.
122 //
123 // Beyond Zork also uses platform-specific hacks to display
124 // character graphics.
125 //
126 // On IBM PC, if the interpreter clears the pictures bit,
127 // Beyond Zork assumes code page 437 is active, and it uses arrows
128 // and box-drawing characters which are invalid or gibberish in
129 // ZSCII.
130 //
131 // On Apple IIc, in much the same way, Beyond Zork prints out values
132 // which are valid MouseText, but are gibberish in ZSCII.
133 //
134 // To work around this, replace invalid characters with valid
135 // characters. Beyond Zork uses font 1 with IBM and font 3 with
136 // Apple, so while the IBM patches have to use a crude conversion
137 // (e.g. “U” and “D” for up and down arrows, and “+” for all
138 // box-drawing corners), the Apple patches convert to appropriate
139 // font 3 values.
140 //
141 // Beyond Zork, like Trinity, uses a routine called CARRIAGE-RETURNS
142 // which dumps newlines in an attempt to force a [MORE] prompt; see
143 // the Trinity section for more information.
144 {
145 "Beyond Zork", "870915", 47, 0x3ff4,
146 {
147 // Circlet object
148 { 0x2f8e6, 2, {0xa3, 0x9a}, {0x01, 0x86} },
149 { 0x2f8f0, 2, {0xa3, 0x9a}, {0x01, 0x86} },
150 { 0x2f902, 2, {0xa3, 0x9a}, {0x01, 0x86} },
151 { 0x2f90c, 2, {0xa3, 0x9a}, {0x01, 0x86} },
152
153 // IBM characters
154 { 0xe58f, 1, {0xda}, {0x2b} },
155 { 0xe595, 1, {0xc4}, {0x2d} },
156 { 0xe59d, 1, {0xbf}, {0x2b} },
157 { 0xe5a6, 1, {0xc0}, {0x2b} },
158 { 0xe5ac, 1, {0xc4}, {0x2d} },
159 { 0xe5b4, 1, {0xd9}, {0x2b} },
160 { 0xe5c0, 1, {0xb3}, {0x7c} },
161 { 0xe5c9, 1, {0xb3}, {0x7c} },
162 { 0x3b211, 1, {0x18}, {0x55} },
163 { 0x3b244, 1, {0x19}, {0x44} },
164 { 0x3b340, 1, {0x1b}, {0x4c} },
165 { 0x3b37f, 1, {0x1a}, {0x52} },
166
167 // Apple IIc characters
168 { 0xe5ee, 1, {0x4c}, {0x4b} },
169 { 0xe5fd, 1, {0x5a}, {0x4e} },
170 { 0xe604, 1, {0x5f}, {0x4d} },
171 { 0xe86a, 1, {0x5a}, {0x4e} },
172 { 0xe8a6, 1, {0x5f}, {0x4d} },
173 { 0x3b220, 1, {0x4b}, {0x5c} },
174 { 0x3b253, 1, {0x4a}, {0x5d} },
175 { 0x3b34f, 1, {0x48}, {0x21} },
176 { 0x3b38e, 1, {0x55}, {0x22} },
177
178 #ifdef ZTERP_GLK
179 // CARRIAGE-RETURNS
180 {
181 0x3cafb, 11,
182 {0x8f, 0x42, 0xdf, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83, 0xbb},
183 {0xf6, 0x7f, 0x01, 0x00, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83},
184 },
185 #endif
186 },
187 },
188
189 {
190 "Beyond Zork", "870917", 49, 0x24d6,
191 {
192 // Circlet object
193 { 0x2f8b6, 2, {0xa3, 0x9c}, {0x01, 0x86} },
194 { 0x2f8c0, 2, {0xa3, 0x9c}, {0x01, 0x86} },
195 { 0x2f8d2, 2, {0xa3, 0x9c}, {0x01, 0x86} },
196 { 0x2f8dc, 2, {0xa3, 0x9c}, {0x01, 0x86} },
197
198 // IBM characters
199 { 0xe4c7, 1, {0xda}, {0x2b} },
200 { 0xe4cd, 1, {0xc4}, {0x2d} },
201 { 0xe4d5, 1, {0xbf}, {0x2b} },
202 { 0xe4de, 1, {0xc0}, {0x2b} },
203 { 0xe4e4, 1, {0xc4}, {0x2d} },
204 { 0xe4ec, 1, {0xd9}, {0x2b} },
205 { 0xe4f8, 1, {0xb3}, {0x7c} },
206 { 0xe501, 1, {0xb3}, {0x7c} },
207 { 0x3b1e1, 1, {0x18}, {0x55} },
208 { 0x3b214, 1, {0x19}, {0x44} },
209 { 0x3b310, 1, {0x1b}, {0x4c} },
210 { 0x3b34f, 1, {0x1a}, {0x52} },
211
212 // Apple IIc characters
213 { 0xe526, 1, {0x4c}, {0x4b} },
214 { 0xe535, 1, {0x5a}, {0x4e} },
215 { 0xe53c, 1, {0x5f}, {0x4d} },
216 { 0xe7a2, 1, {0x5a}, {0x4e} },
217 { 0xe7de, 1, {0x5f}, {0x4d} },
218 { 0x3b1f0, 1, {0x4b}, {0x5c} },
219 { 0x3b223, 1, {0x4a}, {0x5d} },
220 { 0x3b31f, 1, {0x48}, {0x21} },
221 { 0x3b35e, 1, {0x55}, {0x22} },
222
223 #ifdef ZTERP_GLK
224 // CARRIAGE-RETURNS
225 {
226 0x3cac7, 11,
227 {0x8f, 0x42, 0xbd, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83, 0xbb},
228 {0xf6, 0x7f, 0x01, 0x00, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83},
229 },
230 #endif
231 },
232 },
233
234 {
235 "Beyond Zork", "870923", 51, 0x0cbe,
236 {
237 // Circlet object
238 { 0x2f762, 2, {0xa3, 0x8d}, {0x01, 0x86} },
239 { 0x2f76c, 2, {0xa3, 0x8d}, {0x01, 0x86} },
240 { 0x2f77e, 2, {0xa3, 0x8d}, {0x01, 0x86} },
241 { 0x2f788, 2, {0xa3, 0x8d}, {0x01, 0x86} },
242
243 // IBM characters
244 { 0xe4af, 1, {0xda}, {0x2b} },
245 { 0xe4b5, 1, {0xc4}, {0x2d} },
246 { 0xe4bd, 1, {0xbf}, {0x2b} },
247 { 0xe4c6, 1, {0xc0}, {0x2b} },
248 { 0xe4cc, 1, {0xc4}, {0x2d} },
249 { 0xe4d4, 1, {0xd9}, {0x2b} },
250 { 0xe4e0, 1, {0xb3}, {0x7c} },
251 { 0xe4e9, 1, {0xb3}, {0x7c} },
252 { 0x3b08d, 1, {0x18}, {0x55} },
253 { 0x3b0c0, 1, {0x19}, {0x44} },
254 { 0x3b1bc, 1, {0x1b}, {0x4c} },
255 { 0x3b1fb, 1, {0x1a}, {0x52} },
256
257 // Apple IIc characters
258 { 0xe50e, 1, {0x4c}, {0x4b} },
259 { 0xe51d, 1, {0x5a}, {0x4e} },
260 { 0xe524, 1, {0x5f}, {0x4d} },
261 { 0xe78a, 1, {0x5a}, {0x4e} },
262 { 0xe7c6, 1, {0x5f}, {0x4d} },
263 { 0x3b09c, 1, {0x4b}, {0x5c} },
264 { 0x3b0cf, 1, {0x4a}, {0x5d} },
265 { 0x3b1cb, 1, {0x48}, {0x21} },
266 { 0x3b20a, 1, {0x55}, {0x22} },
267
268 #ifdef ZTERP_GLK
269 // CARRIAGE-RETURNS
270 {
271 0x3c967, 11,
272 {0x8f, 0x42, 0xb7, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83, 0xbb},
273 {0xf6, 0x7f, 0x01, 0x00, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83},
274 },
275 #endif
276 },
277 },
278
279 {
280 "Beyond Zork", "871221", 57, 0xc5ad,
281 {
282 // Circlet object
283 { 0x2fc72, 2, {0xa3, 0xba}, {0x01, 0x87} },
284 { 0x2fc7c, 2, {0xa3, 0xba}, {0x01, 0x87} },
285 { 0x2fc8e, 2, {0xa3, 0xba}, {0x01, 0x87} },
286 { 0x2fc98, 2, {0xa3, 0xba}, {0x01, 0x87} },
287
288 // IBM characters
289 { 0xe58b, 1, {0xda}, {0x2b} },
290 { 0xe591, 1, {0xc4}, {0x2d} },
291 { 0xe599, 1, {0xbf}, {0x2b} },
292 { 0xe5a2, 1, {0xc0}, {0x2b} },
293 { 0xe5a8, 1, {0xc4}, {0x2d} },
294 { 0xe5b0, 1, {0xd9}, {0x2b} },
295 { 0xe5bc, 1, {0xb3}, {0x7c} },
296 { 0xe5c5, 1, {0xb3}, {0x7c} },
297 { 0x3b1dd, 1, {0x18}, {0x55} },
298 { 0x3b210, 1, {0x19}, {0x44} },
299 { 0x3b30c, 1, {0x1b}, {0x4c} },
300 { 0x3b34b, 1, {0x1a}, {0x52} },
301
302 // Apple IIc characters
303 { 0xe5ea, 1, {0x4c}, {0x4b} },
304 { 0xe5f9, 1, {0x5a}, {0x4e} },
305 { 0xe600, 1, {0x5f}, {0x4d} },
306 { 0xe866, 1, {0x5a}, {0x4e} },
307 { 0xe8a2, 1, {0x5f}, {0x4d} },
308 { 0x3b1ec, 1, {0x4b}, {0x5c} },
309 { 0x3b21f, 1, {0x4a}, {0x5d} },
310 { 0x3b31b, 1, {0x48}, {0x21} },
311 { 0x3b35a, 1, {0x55}, {0x22} },
312
313 #ifdef ZTERP_GLK
314 // CARRIAGE-RETURNS
315 {
316 0x3ca37, 11,
317 {0x8f, 0x44, 0x55, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83, 0xbb},
318 {0xf6, 0x7f, 0x01, 0x00, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83},
319 },
320 #endif
321 },
322 },
323
324 {
325 "Beyond Zork", "880610", 60, 0xa49d,
326 {
327 // Circlet object
328 { 0x2fbfe, 2, {0xa3, 0xc0}, {0x01, 0x87} },
329 { 0x2fc08, 2, {0xa3, 0xc0}, {0x01, 0x87} },
330 { 0x2fc1a, 2, {0xa3, 0xc0}, {0x01, 0x87} },
331 { 0x2fc24, 2, {0xa3, 0xc0}, {0x01, 0x87} },
332
333 // IBM characters
334 { 0xe4e3, 1, {0xda}, {0x2b} },
335 { 0xe4e9, 1, {0xc4}, {0x2d} },
336 { 0xe4f1, 1, {0xbf}, {0x2b} },
337 { 0xe4fa, 1, {0xc0}, {0x2b} },
338 { 0xe500, 1, {0xc4}, {0x2d} },
339 { 0xe508, 1, {0xd9}, {0x2b} },
340 { 0xe514, 1, {0xb3}, {0x7c} },
341 { 0xe51d, 1, {0xb3}, {0x7c} },
342 { 0x3b169, 1, {0x18}, {0x55} },
343 { 0x3b19c, 1, {0x19}, {0x44} },
344 { 0x3b298, 1, {0x1b}, {0x4c} },
345 { 0x3b2d7, 1, {0x1a}, {0x52} },
346
347 // Apple IIc characters
348 { 0xe542, 1, {0x4c}, {0x4b} },
349 { 0xe551, 1, {0x5a}, {0x4e} },
350 { 0xe558, 1, {0x5f}, {0x4d} },
351 { 0xe7be, 1, {0x5a}, {0x4e} },
352 { 0xe7fa, 1, {0x5f}, {0x4d} },
353 { 0x3b178, 1, {0x4b}, {0x5c} },
354 { 0x3b1ab, 1, {0x4a}, {0x5d} },
355 { 0x3b2a7, 1, {0x48}, {0x21} },
356 { 0x3b2e6, 1, {0x55}, {0x22} },
357
358 #ifdef ZTERP_GLK
359 // CARRIAGE-RETURNS
360 {
361 0x3c9c3, 11,
362 {0x8f, 0x44, 0x2b, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83, 0xbb},
363 {0xf6, 0x7f, 0x01, 0x00, 0xed, 0x3f, 0xff, 0xff, 0x7b, 0x13, 0x83},
364 },
365 #endif
366 },
367 },
368
369 // Bureaucracy has a routine meant to sleep for a certain amount of
370 // time, but it implements this using a busy loop based on the
371 // target machine, each machine having its own iteration count which
372 // should approximate the number of seconds passed in. This doesn’t
373 // work on modern machines at all, as they’re far too fast. A
374 // replacement is provided here which does the following:
375 //
376 // [ Delay n;
377 // @mul n $0a -> n;
378 // @read_char 1 n Pause -> sp;
379 // @rtrue;
380 // ];
381 //
382 // [ Pause;
383 // @rtrue;
384 // ];
385 {
386 "Bureaucracy", "870212", 86, 0xe024,
387 {
388 {
389 0x2128c, 18,
390 {0x02, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x1e, 0x00, 0xd0, 0x2f, 0xde, 0x5c, 0x00, 0x02, 0xd6, 0x2f, 0x03},
391 {0x01, 0x00, 0x01, 0x56, 0x01, 0x0a, 0x01, 0xf6, 0x63, 0x01, 0x01, 0x84, 0xa7, 0x00, 0xb0, 0x00, 0x00, 0xb0},
392 bureaucracy_active,
393 }
394 }
395 },
396 {
397 "Bureaucracy", "870602", 116, 0xfc65,
398 {
399 {
400 0x212d0, 18,
401 {0x02, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x1e, 0x00, 0xd0, 0x2f, 0xde, 0x64, 0x00, 0x02, 0xd6, 0x2f, 0x03},
402 {0x01, 0x00, 0x01, 0x56, 0x01, 0x0a, 0x01, 0xf6, 0x63, 0x01, 0x01, 0x84, 0xb8, 0x00, 0xb0, 0x00, 0x00, 0xb0},
403 bureaucracy_active,
404 }
405 }
406 },
407 {
408 "Bureaucracy", "880521", 160, 0x07f0,
409 {
410 {
411 0x212c8, 18,
412 {0x02, 0x00, 0x01, 0x00, 0x00, 0x10, 0x00, 0x1e, 0x00, 0xd0, 0x2f, 0xdc, 0xce, 0x00, 0x02, 0xd6, 0x2f, 0x03},
413 {0x01, 0x00, 0x01, 0x56, 0x01, 0x0a, 0x01, 0xf6, 0x63, 0x01, 0x01, 0x84, 0xb6, 0x00, 0xb0, 0x00, 0x00, 0xb0},
414 bureaucracy_active,
415 }
416 }
417 },
418
419 // Journey doubles up the word “essence” when examining Praxix’s
420 // pouch: originally the game printed short descriptions when the
421 // screen was narrow enough, which didn’t include the word
422 // “essence”, so it always just printed out an extra “essence”,
423 // knowing that it wasn’t shown. However, release 51 changed this so
424 // that the long descriptions, which included “essence”, were
425 // unconditionally printed in this circumstance; but the extra
426 // “essence” remained. This patches out the printing of that extra
427 // word by replacing it with @nop.
428 {
429 "Journey", "890522", 51, 0x4f59,
430 {{ 0x9d9f, 3, {0xb2, 0x80, 0x3e}, {0xb4, 0xb4, 0xb4} }},
431 },
432 {
433 "Journey", "890526", 54, 0x5707,
434 {{ 0x9e2f, 3, {0xb2, 0x80, 0x3e}, {0xb4, 0xb4, 0xb4} }},
435 },
436 {
437 "Journey", "890616", 77, 0xb136,
438 {{ 0xa09f, 3, {0xb2, 0x80, 0x3e}, {0xb4, 0xb4, 0xb4} }},
439 },
440 {
441 "Journey", "890627", 79, 0xff04,
442 {{ 0xa10b, 3, {0xb2, 0x80, 0x3e}, {0xb4, 0xb4, 0xb4} }},
443 },
444 {
445 "Journey", "890706", 83, 0xd2b8,
446 {{ 0xa0ef, 3, {0xb2, 0x80, 0x3e}, {0xb4, 0xb4, 0xb4} }},
447 },
448
449 // Journey has a CHANGE-FONT routine it uses to change the font, but
450 // it only supports fonts 3 and 4, so when it wants to set the font
451 // back to 1, nothing happens. This is worked around here by,
452 // instead of executing RFALSE when the font is unsupported, jumping
453 // to the <FONT 1> call at the end of the routine.
454 //
455 // This fix was created by Petter Sjölund.
456
457 {
458 "Journey", "890522", 51, 0x4f59,
459 {{ 0x471f, 1, {0x40}, {0x4b} }},
460 },
461 {
462 "Journey", "890526", 54, 0x5707,
463 {{ 0x4727, 1, {0x40}, {0x4b} }},
464 },
465 {
466 "Journey", "890616", 77, 0xb136,
467 {{ 0x474f, 1, {0x40}, {0x4b} }},
468 },
469 {
470 "Journey", "890627", 79, 0xff04,
471 {{ 0x4757, 1, {0x40}, {0x4b} }},
472 },
473 {
474 "Journey", "890706", 83, 0xd2b8,
475 {{ 0x4757, 1, {0x40}, {0x4b} }},
476 },
477
478 // This is in a routine which iterates over all attributes of an
479 // object, but due to an off-by-one error, attribute 48 (0x30) is
480 // included, which is not valid, as the last attribute is 47 (0x2f);
481 // there are 48 attributes but the count starts at 0.
482 {
483 "Sherlock", "871214", 21, 0x79b2,
484 {{ 0x223ac, 1, {0x30}, {0x2f} }},
485 },
486 {
487 "Sherlock", "880112", 22, 0xcb96,
488 {{ 0x225a4, 1, {0x30}, {0x2f} }},
489 },
490 {
491 "Sherlock", "880127", 26, 0x26ba,
492 {{ 0x22818, 1, {0x30}, {0x2f} }},
493 },
494 {
495 "Sherlock", "880324", 4, 0x7086,
496 {{ 0x22498, 1, {0x30}, {0x2f} }},
497 },
498
499 // The operands to @get_prop here are swapped, so swap them back.
500 {
501 "Stationfall", "870326", 87, 0x71ae,
502 {{ 0xd3d4, 3, {0x31, 0x0c, 0x73}, {0x51, 0x73, 0x0c} }},
503 },
504 {
505 "Stationfall", "870430", 107, 0x2871,
506 {{ 0xe3fe, 3, {0x31, 0x0c, 0x77}, {0x51, 0x77, 0x0c} }},
507 },
508
509 // Trinity does the following after the prologue:
510 //
511 // <CARRIAGE-RETURNS>
512 // <CLEAR -1>
513 //
514 // Where CARRIAGE-RETURNS prints 23 newlines, trying to get a [MORE]
515 // prompt to be displayed; this assumes specific interpreter
516 // behavior as well as screen size. It doesn’t work with Glk, as the
517 // screen is cleared without a [MORE] prompt, preventing the text
518 // from being seen. As a workaround, replace the call to
519 // CARRIAGE-RETURNS with “@read_char 1 -> sp”, which will force the
520 // screen to pause before clearing.
521 //
522 // This isn’t ideal since the user isn’t given an indication that a
523 // keypress is needed, but it won’t take much time to figure it out.
524 //
525 // It also assumes a specific behavior from Glk (immediately
526 // clearing the screen without printing [MORE]), although most major
527 // Glk implementations have this behavior:
528 // garglk, Spatterlight, glkterm, winglk, xglk, dosglk
529 //
530 // In addition, even if a Glk implementation does work “properly” in
531 // this case, the fact that 23 newlines are used means that it’ll
532 // only work if the user’s screen is not too tall. This obviously
533 // was done with the expectation that it’d be running on an 80x25
534 // screen.
535 //
536 // In the end, the slight inconvenience of a “silent” @read_char is
537 // worth it for the benefit of not losing game text.
538 #ifdef ZTERP_GLK
539 {
540 "Trinity", "860509", 11, 0xfaae,
541 {{ 0x2d453, 4, {0x88, 0x44, 0x33, 0x00}, {0xf6, 0x7f, 0x01, 0x00}}},
542 },
543 {
544 "Trinity", "860926", 12, 0x16ab,
545 {{ 0x2d487, 4, {0x88, 0x44, 0x30, 0x00}, {0xf6, 0x7f, 0x01, 0x00}}},
546 },
547 {
548 "Trinity", "870628", 15, 0xf112,
549 {{ 0x2d25f, 4, {0x88, 0x44, 0x12, 0x00}, {0xf6, 0x7f, 0x01, 0x00}}},
550 },
551 #endif
552
553 // The Solid Gold (V5) version of Wishbringer calls @show_status, but
554 // that is an illegal instruction outside of V3. Convert it to @nop.
555 {
556 "Wishbringer", "880706", 23, 0x4222,
557 {{ 0x1f910, 1, {0xbc}, {0xb4} }},
558 },
559
560 // Two Zork Zero beta releases, 242-880830 and 242-880901, target an
561 // earlier, unreleased version 6 Z-machine whose @get_wind_prop
562 // (WINGET) opcode takes three operands instead of two, one of which
563 // being a table to write the result to. But the table only has one
564 // element, and all calls to WINGET go through a wrapper that
565 // effectively simulates the standard WINGET opcode:
566 //
567 // <CONSTANT WTBL <LTABLE 0>>
568 //
569 // <ROUTINE WINPROP (WIN PROP)
570 // <WINGET .WIN ,WTBL .PROP>
571 // <GET ,WTBL 1>>
572 //
573 // Rewrite this routine to:
574 //
575 // <ROUTINE WINPROP (WIN PROP)
576 // <WINGET .WIN .PROP>>
577 //
578 // i.e. convert the non-standard WINGET call to the standard version.
579 //
580 // WINPUT/@put_wind_prop is non-standard in a similar way, but since
581 // that opcode is unsupported by Bocfel, and because it decodes just
582 // fine, there’s currently no need to patch it.
583 //
584 // See https://intfiction.org/t/illegal-use-of-parameters-in-opcode-get-wind-prop-zork0-242/75353
585 {
586 "Zork Zero", "880830", 242, 0xbf47,
587 {
588 {
589 0x1cc2b, 5,
590 {0x8b, 0x01, 0x70, 0x8b, 0x02},
591 {0xaf, 0x01, 0x02, 0x00, 0xb8},
592 },
593 },
594 },
595 {
596 "Zork Zero", "880901", 242, 0xbbab,
597 {
598 {
599 0x1cc2b, 5,
600 {0x8b, 0x01, 0x70, 0x8b, 0x02},
601 {0xaf, 0x01, 0x02, 0x00, 0xb8},
602 },
603 },
604 },
605
606 #ifdef ZTERP_STATIC_PATCH_FILE
607 #include ZTERP_STATIC_PATCH_FILE
608 #endif
609 };
610
611 // These patches help with the V6 hacks.
612 static std::vector<Patch> v6_patches = {
613 // There are two V6 hack patches for Arthur:
614 //
615 // 1. In the intro, two images are shown in immediate succession:
616 //
617 // <RT-CENTER-PIC ,K-PIC-SWORD>
618 // <RT-CENTER-PIC ,K-PIC-SWORD-MERLIN>
619 //
620 // This is presumably under the assmption that drawing is slow, so
621 // it will look like a small animation. On modern systems
622 // K-PIC-SWORD won’t be seen in this sequence, so this patch
623 // rewrites the code to add a 1s sleep call via @read_char. There
624 // are two calls to @set_cursor (to hide the cursor) which have no
625 // effect in Bocfel, giving 8 total bytes to work with, which is
626 // enough to add the new call. The following:
627 //
628 // <RT-CENTER-PIC ,K-PIC-SWORD-MERLIN> | call_2n #19234 #03
629 // <CURSET -1> ;"Make cursor go away." | set_cursor #ffff
630 // <INPUT 1 150 ,RT-STOP-READ> | read_char #01 #96 #11d64 -> -(SP)
631 // <CURSET -2> ;"Make cursor come back." | set_cursor #fffe
632 //
633 // is replaced with:
634 //
635 // <INPUT 1 10 ,RT-STOP-READ> | read_char #01 #0a #11d64 -> -(SP)
636 // <RT-CENTER-PIC ,K-PIC-SWORD-MERLIN> | call_2n #19234 #03
637 // <INPUT 1 150 ,RT-STOP-READ> | read_char #01 #96 #11d64 -> -(SP)
638 // <NOOP> | nop
639 //
640 // (The routine addresses above represent the 74-890714 version but
641 // the approach is the same for all versions).
642 //
643 // 2. Parser messages are meant to be displayed on the bottom of the
644 // screen, but since Bocfel doesn’t have real V6 window support, the
645 // messages are displayed inline as with most other Infocom games.
646 // However, the messages are printed in reverse video (in fact, the
647 // current color is looked up with @get_wind_prop, @set_colour is
648 // called with the values swapped, the message is printed, and then
649 // @set_colour puts things back). This might look OK with the parser
650 // messages in a separate window, but it’s jarring interleaved with
651 // user input, so this removes the calls to @set_colour entirely.
652 {
653 "Arthur", "890606", 54, 0x8e4a,
654 {
655 // Merlin.
656 {
657 0x11418, 20,
658 {0xda, 0x1f, 0x49, 0xae, 0x03, 0xef, 0x3f, 0xff, 0xff, 0xf6, 0x53, 0x01, 0x96, 0x21, 0xd4, 0x00, 0xef, 0x3f, 0xff, 0xfe},
659 {0xf6, 0x53, 0x01, 0x0a, 0x21, 0xd4, 0x00, 0xda, 0x1f, 0x49, 0xae, 0x03, 0xf6, 0x53, 0x01, 0x96, 0x21, 0xd4, 0x00, 0xb4}
660 },
661
662 // Reverse video.
663 { 0x117bf, 3, {0x7b, 0x0d, 0x0c}, {0xb4, 0xb4, 0xb4} },
664 { 0x117cb, 3, {0x7b, 0x0c, 0x0d}, {0xb4, 0xb4, 0xb4} },
665 }
666 },
667
668 {
669 "Arthur", "890622", 63, 0x45eb,
670 {
671 // Merlin.
672 {
673 0x1147e, 20,
674 {0xda, 0x1f, 0x3f, 0x2e, 0x03, 0xef, 0x3f, 0xff, 0xff, 0xf6, 0x53, 0x01, 0x96, 0x22, 0x04, 0x00, 0xef, 0x3f, 0xff, 0xfe},
675 {0xf6, 0x53, 0x01, 0x0a, 0x22, 0x04, 0x00, 0xda, 0x1f, 0x3f, 0x2e, 0x03, 0xf6, 0x53, 0x01, 0x96, 0x22, 0x04, 0x00, 0xb4}
676 },
677
678 // Reverse video.
679 { 0x11853, 3, {0x7b, 0x0d, 0x0c}, {0xb4, 0xb4, 0xb4} },
680 { 0x1185f, 3, {0x7b, 0x0c, 0x0d}, {0xb4, 0xb4, 0xb4} },
681 }
682 },
683
684 {
685 "Arthur", "890714", 74, 0xd526,
686 {
687 // Merlin.
688 {
689 0x10e76, 20,
690 {0xda, 0x1f, 0x3d, 0xb1, 0x03, 0xef, 0x3f, 0xff, 0xff, 0xf6, 0x53, 0x01, 0x96, 0x20, 0x7d, 0x00, 0xef, 0x3f, 0xff, 0xfe},
691 {0xf6, 0x53, 0x01, 0x0a, 0x20, 0x7d, 0x00, 0xda, 0x1f, 0x3d, 0xb1, 0x03, 0xf6, 0x53, 0x01, 0x96, 0x20, 0x7d, 0x00, 0xb4}
692 },
693
694 // Reverse video.
695 { 0x1124b, 3, {0x7b, 0x0d, 0x0c}, {0xb4, 0xb4, 0xb4} },
696 { 0x11257, 3, {0x7b, 0x0c, 0x0d}, {0xb4, 0xb4, 0xb4} },
697 },
698 },
699
700 // There are several V6 hack patches for Shogun:
701 //
702 // 1. Avoid calling a function that prints too many newlines during
703 // interludes.
704 //
705 // 2. Shogun uses either @set_colour or @set_text_style for reverse
706 // video in the status line, but it only uses @set_text_style on
707 // Amiga. Unconditionally use @set_text_style for better results.
708 //
709 // 3. Shogun clears the window right after printing the title
710 // screen, rendering it invisible without proper V6 support. Ignore
711 // that request.
712 //
713 // 4. Shogun has a GET-FROM-MENU routine which is used to present a
714 // menu (e.g. at the start of the game) from which the user selects
715 // an entry. This menu does not work properly with Glk, so a custom
716 // opcode replacement is used to present a working menu.
717 //
718 // 5. Two Shogun releases split the window for the title screen such
719 // that the actual text displayed is in a tiny main window and hard
720 // to deal with. Without proper V6 support, the split is
721 // problematic, so just don’t do it.
722 //
723 // 6. The same two Shogun releases try to calculate offsets into a
724 // window to draw the maze; but with Glk windows, the maze should
725 // always be offset by 0 (which is then adjusted by the block width
726 // of 7). Overwrite the assignment to these offsets with @nop.
727 {
728 "Shogun", "890314", 292, 0x69b8,
729 {
730 // Newlines.
731 { 0x11ef1, 1, {0xda}, {0xb0} },
732
733 // Reverse video.
734 { 0x11be5, 3, {0x7b, 0xde, 0x2c}, {0xf1, 0x7f, 0x01} },
735
736 // Title clear.
737 { 0x10bcf, 3, {0xed, 0x7f, 0x00}, {0xb4, 0xb4, 0xb4} },
738
739 // Game start menu.
740 {
741 0x10bd5, 5,
742 {0xec, 0x00, 0x7f, 0x15, 0x1c},
743 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x01},
744 },
745
746 // Game over menu.
747 {
748 0x12914, 4,
749 {0xe0, 0x08, 0x15, 0x1c},
750 {0xb4, 0xbe, SHOGUN_MENU_EXT, 0x23},
751 },
752
753 // End of scene menu.
754 {
755 0x13caf, 4,
756 {0xe0, 0x08, 0x15, 0x1c},
757 {0xb4, 0xbe, SHOGUN_MENU_EXT, 0x23},
758 },
759
760 // Color menu.
761 {
762 0x1750d, 5,
763 {0xec, 0x00, 0xbf, 0x15, 0x1c},
764 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x02},
765 },
766
767 // Title split.
768 { 0x10bc9, 3, {0xea, 0xbf, 0x00}, {0xb4, 0xb4, 0xb4} },
769
770 // Maze offset.
771 { 0x3d7b1, 4, {0x57, 0x00, 0x02, 0xb6}, {0xb4, 0xb4, 0xb4, 0xb4 } },
772 { 0x3d7c3, 4, {0x57, 0x00, 0x02, 0x6d}, {0xb4, 0xb4, 0xb4, 0xb4 } },
773 }
774 },
775
776 {
777 "Shogun", "890321", 295, 0x5c0b,
778 {
779 // Newlines.
780 { 0x120a9, 1, {0xda}, {0xb0} },
781
782 // Reverse video.
783 { 0x10a85, 4, {0x41, 0x40, 0x04, 0x46}, {0xf1, 0x7f, 0x01, 0xb0} },
784
785 // Title clear.
786 { 0x10d14, 3, {0xed, 0x7f, 0x00}, {0xb4, 0xb4, 0xb4} },
787
788 // Game start menu.
789 {
790 0x10d1a, 5,
791 {0xec, 0x00, 0x7f, 0x15, 0x66},
792 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x01},
793 },
794
795 // Game over menu.
796 {
797 0x12acc, 4,
798 {0xe0, 0x08, 0x15, 0x66},
799 {0xb4, 0xbe, SHOGUN_MENU_EXT, 0x23},
800 },
801
802 // End of scene menu.
803 {
804 0x13e6f, 4,
805 {0xe0, 0x08, 0x15, 0x66},
806 {0xb4, 0xbe, SHOGUN_MENU_EXT, 0x23},
807 },
808
809 // Color menu.
810 {
811 0x176e5, 5,
812 {0xec, 0x00, 0xbf, 0x15, 0x66},
813 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x02},
814 },
815
816 // Title split.
817 { 0x10d0e, 3, {0xea, 0xbf, 0x00}, {0xb4, 0xb4, 0xb4} },
818
819 // Maze offset.
820 { 0x3d999, 4, {0x57, 0x00, 0x02, 0xb8}, {0xb4, 0xb4, 0xb4, 0xb4 } },
821 { 0x3d9ab, 4, {0x57, 0x00, 0x02, 0x6e}, {0xb4, 0xb4, 0xb4, 0xb4 } },
822 }
823 },
824
825 {
826 "Shogun", "890510", 311, 0xe200,
827 {
828 // Newlines.
829 { 0x12625, 1, {0xda}, {0xb0} },
830
831 // Reverse video.
832 { 0x117d1, 4, {0x41, 0x43, 0x04, 0x46}, {0xf1, 0x7f, 0x01, 0xb0} },
833
834 // Title clear.
835 { 0x10cb0, 3, {0xed, 0x7f, 0x00}, {0xb4, 0xb4, 0xb4} },
836
837 // Game start menu.
838 {
839 0x10cb6, 5,
840 {0xec, 0x00, 0x7f, 0x16, 0x95},
841 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x01},
842 },
843
844 // Game over menu.
845 {
846 0x130ab, 4,
847 {0xe0, 0x00, 0x16, 0x95},
848 {0xb4, 0xbe, SHOGUN_MENU_EXT, 0x03},
849 },
850
851 // End of scene menu.
852 {
853 0x14499, 9,
854 {0xf9, 0x08, 0x16, 0x95, 0x00, 0xc2, 0x00, 0x20, 0x53},
855 {0xbe, SHOGUN_MENU_EXT, 0x23, 0x00, 0xc2, 0x00, 0x20, 0x53, 0x02},
856 },
857
858 // Color menu.
859 {
860 0x17d69, 5,
861 {0xec, 0x00, 0xbf, 0x16, 0x95},
862 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x02},
863 },
864 }
865 },
866
867 {
868 "Shogun", "890706", 322, 0x5c88,
869 {
870 // Newlines.
871 { 0x12771, 1, {0xda}, {0xb0} },
872
873 // Reverse video.
874 { 0x11865, 4, {0x41, 0x43, 0x04, 0x46}, {0xf1, 0x7f, 0x01, 0xb0} },
875
876 // Title clear.
877 { 0x10d44, 3, {0xed, 0x7f, 0x00}, {0xb4, 0xb4, 0xb4} },
878
879 // Game start menu.
880 {
881 0x10d4a, 5,
882 {0xec, 0x00, 0x7f, 0x16, 0xba},
883 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x01},
884 },
885
886 // Game over menu.
887 {
888 0x131e7, 4,
889 {0xe0, 0x00, 0x16, 0xba},
890 {0xb4, 0xbe, SHOGUN_MENU_EXT, 0x03},
891 },
892
893 // End of scene menu.
894 {
895 0x145e1, 9,
896 {0xf9, 0x08, 0x16, 0xba, 0x00, 0xc2, 0x00, 0x20, 0xa5},
897 {0xbe, SHOGUN_MENU_EXT, 0x23, 0x00, 0xc2, 0x00, 0x20, 0xa5, 0x02},
898 },
899
900 // Color menu.
901 {
902 0x17edd, 5,
903 {0xec, 0x00, 0xbf, 0x16, 0xba},
904 {0xb4, 0xb4, 0xbe, SHOGUN_MENU_EXT, 0x02},
905 },
906 }
907 },
908
909 // There are two V6 hack patches for Journey:
910 //
911 // 1. The DIAL-GRAPHICS routine calls GRAPHIC with the specified
912 // arrow and location; since the same arrows are used for both
913 // dials, the offset for the arrows can’t be determined just by the
914 // picture number. Instead rewrite the calls to a custom
915 // zjourney_dial(), passing 0 for the left arrow, and 1 for the
916 // right.
917 //
918 // 2. When the interpreter number is set to Amiga, Journey draws a
919 // box around the entire screen. This is not possible with Glk (at
920 // least not in a way that wouldn’t require loads of special-
921 // casing); and even worse, Bocfel hacks around some Journey/Glk
922 // issues by pretending the screen height is 6, so that Journey
923 // won’t expand the upper window to extend across the whole screen,
924 // with the side effect that the calculation of the border is
925 // broken, causing an apparent hang that is effectively this, but
926 // slow, since it’s interpreted:
927 //
928 // uint16_t val = 1;
929 // while (val++ != 0) { }
930 //
931 // Border drawing is controlled by the global variable BORDER-FLAG,
932 // which is only set for Amiga. This patch ensures that BORDER-FLAG
933 // is never set, so the game never tries to draw the border.
934 {
935 "Journey", "890316", 26, 0x27cc,
936 {
937 // DIAL-GRAPHICS
938 {
939 0x30095, 6,
940 {0xf9, 0x59, 0xcc, 0x00, 0x00, 0x00},
941 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x00, 0xb4}
942 },
943 {
944 0x300a3, 6,
945 {0xe0, 0x58, 0xcc, 0x00, 0x00, 0xff},
946 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x01, 0xb0}
947 },
948
949 // Amiga hang
950 { 0x4b3e, 3, {0x0d, 0xab, 0x01}, {0x0d, 0xab, 0x00} },
951 }
952 },
953
954 {
955 "Journey", "890322", 30, 0x985b,
956 {
957 // DIAL-GRAPHICS
958 {
959 0x30189, 6,
960 {0xf9, 0x59, 0xcc, 0x00, 0x00, 0x00},
961 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x00, 0xb4}
962 },
963 {
964 0x30197, 6,
965 {0xe0, 0x58, 0xcc, 0x00, 0x00, 0xff},
966 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x01, 0xb0}
967 },
968
969 // Amiga hang
970 { 0x4bea, 3, {0x0d, 0xaa, 0x01}, {0x0d, 0xaa, 0x00} },
971 }
972 },
973
974 {
975 "Journey", "890616", 77, 0xb136,
976 {
977 // DIAL-GRAPHICS
978 {
979 0x30771, 6,
980 {0xf9, 0x59, 0xf1, 0x00, 0x00, 0x00},
981 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x00, 0xb4}
982 },
983 {
984 0x3077f, 6,
985 {0xe0, 0x58, 0xf1, 0x00, 0x00, 0xff},
986 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x01, 0xb0}
987 },
988
989 // Amiga hang
990 { 0x4dcf, 3, {0x0d, 0xad, 0x01}, {0x0d, 0xad, 0x00} },
991 }
992 },
993
994 {
995 "Journey", "890706", 83, 0xd2b8,
996 {
997 // DIAL-GRAPHICS
998 {
999 0x307b9, 6,
1000 {0xf9, 0x59, 0xef, 0x00, 0x00, 0x00},
1001 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x00, 0xb4}
1002 },
1003 {
1004 0x307c7, 6,
1005 {0xe0, 0x58, 0xef, 0x00, 0x00, 0xff},
1006 {0xbe, JOURNEY_DIAL_EXT, 0x9f, 0x97, 0x01, 0xb0}
1007 },
1008
1009 // Amiga hang
1010 { 0x4dcf, 3, {0x0d, 0xaf, 0x01}, {0x0d, 0xaf, 0x00} },
1011 }
1012 },
1013
1014 // There are two V6 hack patches for Zork Zero:
1015 //
1016 // 1. In Fanucci, Zork Zero displays labels under each card
1017 // (DISCARD, 1, 2, 3, 4). This is done in a graphics window, though,
1018 // and Glk doesn’t support text in graphics windows. It would
1019 // probably be possible to create a new text window just below the
1020 // graphics window and put this text in there, but since it’s not
1021 // crucial to the game, as you can either use the mouse to click, or
1022 // just infer/look up which cards are which, jump over the entire
1023 // sequence of moving the cursor and writing text. This is necessary
1024 // because, without it, output such as the jester’s commentary is
1025 // not visible.
1026 //
1027 // 2. Zork Zero only allows the compass to be clicked for certain
1028 // machine types (interpreter numbers). But the mouse works under
1029 // Glk regardless (if the Glk implementation supports mouse clicks).
1030 // This bypasses the mouse check, allowing the compass rose to be
1031 // clicked no matter which interpreter number is selected.
1032 {
1033 "Zork Zero", "881019", 296, 0x8c61,
1034 {
1035 // Fanucci.
1036 { 0x2aa6d, 3, {0xef, 0xaf, 0x02}, {0x8c, 0x00, 0x3e} },
1037
1038 // Compass rose.
1039 { 0x1d3cc, 4, {0x41, 0x00, 0x03, 0x62}, {0xb4, 0xb4, 0xb4, 0xb4} },
1040 },
1041 },
1042
1043 {
1044 "Zork Zero", "890323", 366, 0xc5cd,
1045 {
1046 // Fanucci.
1047 { 0x2964c, 3, {0xef, 0xaf, 0x02}, {0x8c, 0x00, 0x3e} },
1048
1049 // Compass rose.
1050 { 0x1baee, 3, {0xa0, 0x00, 0xce}, {0xb4, 0xb4, 0xb4} },
1051 },
1052 },
1053
1054 {
1055 "Zork Zero", "890602", 383, 0x6f7f,
1056 {
1057 // Fanucci.
1058 { 0x29e87, 3, {0xef, 0xaf, 0x03}, {0x8c, 0x00, 0x3e} },
1059
1060 // Compass rose.
1061 { 0x1bfa1, 3, {0xa0, 0x00, 0xce}, {0xb4, 0xb4, 0xb4} },
1062 },
1063 },
1064
1065 {
1066 "Zork Zero", "890714", 393, 0x791c,
1067 {
1068 // Fanucci.
1069 { 0x2a127, 3, {0xef, 0xaf, 0x03}, {0x8c, 0x00, 0x3e} },
1070
1071 // Compass rose.
1072 { 0x1c20d, 3, {0xa0, 0x00, 0xce}, {0xb4, 0xb4, 0xb4} },
1073 },
1074 },
1075 };
1076
1077 static bool apply_patch(const Replacement &r)
1078 {
1079 if (r.addr >= header.static_start &&
1080 r.addr + r.n < memory_size &&
1081 std::memcmp(&memory[r.addr], r.in.data(), r.n) == 0) {
1082
1083 std::memcpy(&memory[r.addr], r.out.data(), r.n);
1084
1085 return true;
1086 }
1087
1088 return false;
1089 }
1090
1091 static bool sanity_check(const Patch &patch)
1092 {
1093 bool ok = true;
1094
1095 for (const auto &replacement : patch.replacements) {
1096 if (replacement.in.size() != replacement.n ||
1097 replacement.out.size() != replacement.n)
1098 {
1099 std::ostringstream ss;
1100 ss << "patch consistency error: " <<
1101 patch.title << " (" <<
1102 patch.release << "-" << patch.serial <<
1103 " @0x" << std::hex << replacement.addr <<
1104 ")";
1105
1106 std::cerr << ss.str() << std::endl;
1107 ok = false;
1108 }
1109 }
1110
1111 return ok;
1112 }
1113
1114 static void apply_patches(const std::vector<Patch> &patches)
1115 {
1116 for (const auto &patch : patches) {
1117 if (sanity_check(patch) &&
1118 std::memcmp(patch.serial, header.serial, sizeof header.serial) == 0 &&
1119 patch.release == header.release &&
1120 patch.checksum == header.checksum) {
1121
1122 for (const auto &replacement : patch.replacements) {
1123 if (replacement.active()) {
1124 if (!apply_patch(replacement)) {
1125 std::ostringstream ss;
1126 ss << "internal patch error: patch for " <<
1127 patch.title << " (" <<
1128 patch.release << "-" << patch.serial <<
1129 " @0x" << std::hex << replacement.addr <<
1130 ") is not valid: byte sequence not found";
1131 std::cerr << ss.str() << std::endl;
1132 }
1133 }
1134 }
1135 }
1136 }
1137 }
1138
1139 void apply_patches()
1140 {
1141 apply_patches(base_patches);
1142 }
1143
1144 void apply_v6_patches()
1145 {
1146 apply_patches(v6_patches);
1147 }
1148
1149 // User patches have the form:
1150 //
1151 // addr count original... replacement...
1152 //
1153 // For example, one of the Stationfall fixes would look like this:
1154 //
1155 // 0xe3fe 3 0x31 0x0c 0x77 0x51 0x77 0x0c
1156 //
1157 // Square brackets and commas are treated as whitespace for the actual
1158 // bytes, so for convenience this could also be written:
1159 //
1160 // 0xe3fe 3 [0x31, 0x0c, 0x77] [0x51, 0x77, 0x0c]
1161 void apply_user_patch(std::string patchstr)
1162 {
1163 uint32_t addr, count;
1164 std::vector<uint8_t> in, out;
1165
1166 std::replace_if(patchstr.begin(), patchstr.end(),
1167 [](char c) { return c == '[' || c == ']' || c == ','; },
1168 ' ');
1169
1170 std::istringstream ss(rtrim(patchstr));
1171
1172 if (!(ss >> std::hex >> addr) ||
1173 !(ss >> std::dec >> count))
1174 {
1175 throw PatchStatus::SyntaxError("unable to read address and count");
1176 }
1177
1178 ss >> std::hex;
1179
1180 auto read_into = [&count, &ss](std::vector<uint8_t> &vec){
1181 for (uint32_t i = 0; i < count; i++) {
1182 unsigned int byte;
1183 if (!(ss >> byte)) {
1184 throw PatchStatus::SyntaxError("not enough bytes");
1185 }
1186 if (byte > 255) {
1187 throw PatchStatus::SyntaxError("invalid byte: must be 0-255");
1188 }
1189 vec.push_back(byte);
1190 }
1191 };
1192
1193 read_into(in);
1194 read_into(out);
1195
1196 if (ss.peek() != std::char_traits<char>::eof()) {
1197 throw PatchStatus::SyntaxError("unexpected characters at end");
1198 }
1199
1200 Replacement replacement = {
1201 addr,
1202 count,
1203 in,
1204 out,
1205 };
1206
1207 if (!apply_patch(replacement)) {
1208 throw PatchStatus::NotFound();
1209 }
1210 }
1211
1212 // A patch file is somewhat similar to a user configuration file, but
1213 // contains *only* patches. It uses grouping as in the configuration
1214 // file, and the patch syntax is the same, but without the “patch” key.
1215 // For example, a patch file for the first couple of IBM characters in
1216 // Beyond Zork would look like:
1217 //
1218 // # Comments are allowed.
1219 // [57-871221]
1220 // 0xe58b 1 [da] [2b]
1221 // 0xe591 1 [c4] [2d]
1222 void patch_load_file(const std::string &file)
1223 {
1224 std::ifstream f(file);
1225
1226 if (!f.is_open()) {
1227 return;
1228 }
1229
1230 parse_grouped_file(f, [&file](const std::string &line, int lineno) {
1231 try {
1232 apply_user_patch(line);
1233 } catch (const PatchStatus::SyntaxError &e) {
1234 std::cerr << file << ":" << lineno << ": patch file syntax error" << ": " << e.what() << std::endl;
1235 } catch (const PatchStatus::NotFound &) {
1236 std::cerr << file << ":" << lineno << ": patch file byte sequence not found" << std::endl;
1237 }
1238 });
1239 }