Debian Codegraph @

terps/magnetic/Generic/emu.c

raw symbols SCIP
1 /****************************************************************************\
2 *
3 * Magnetic - Magnetic Scrolls Interpreter.
4 *
5 * Written by Niclas Karlsson <[email protected]>,
6 * David Kinder <[email protected]>,
7 * Stefan Meier <[email protected]> and
8 * Paul David Doherty <[email protected]>
9 *
10 * Copyright (C) 1997-2023 Niclas Karlsson
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
25 *
26 * History:
27 *
28 * 0.1 123096 : Making the 68000 emulation work. Converting C64 6510 code to
29 * -010297 C isn't all that much fun. Don't mention the word "BUG".
30 *
31 * 0.2 010397 : Debugging, debugging, debugging... Probably quite a lot left
32 * as well. Haven't tried it out in practice yet.
33 *
34 * 0.3 010497 : String decoding now works. Trying out on the real thing - more
35 * bugs, modest success. Implementing some very specialized
36 * instructions. 0xA0FF gives the word nightmare a whole new
37 * meaning.
38 *
39 * 0.4 010597 : Getting better... More specialized instructions coming up. The
40 * output routines are up and running as well. Occasional
41 * emulation bugs still show up due to my MS-style program
42 * developing techniques ;-) First playable version [The Pawn].
43 *
44 * 0.5 010697 : Trying out with Jinxter. Sigh... Back to reverse engineering
45 * (10+ new instructions, separate dictionary and two string
46 * banks - these guys sure had some space problems).
47 *
48 * 0.6 010997 : Guild of Thieves seems to work now as well. Jinxter still has
49 * a small problem with the parsing (I know of _one_ different
50 * reaction) - probably due to the not-so-nice dict_lookup2().
51 * Load/Save implemented.
52 *
53 * 0.7 011097 : The last (major) bug is now fixed (an 8-bit variable instead
54 * of a 16-bit one messed up the adjectives). Small cleanup.
55 *
56 * 0.8 011397 : Now also runs Fish and Corruption. Small status-bar fix.
57 *
58 * 0.9 011497 : New single-file format. Improved user friendliness.
59 *
60 * 0.91 011597 : More cleaning. Random number range fixed. Other minor bugfixes.
61 *
62 * 0.92 011897 : Major dict_lookup() overhaul. Minor cleanups. <= 64K memory
63 * blocks. Seems like the file format wasn't that great after all
64 * (most suitable for one big block).
65 *
66 * 0.93 012097 : New file format. Fast restarts (no loading). UNDO implemented.
67 * Subtle but fatal bug in Write_string() removed [Corruption].
68 *
69 * 0.94 012397 : Emulation bug (rotations) fixed. Prng slightly improved, or
70 * at least changed. :-) Admode $xx(PC,[A|D]x) implemented.
71 *
72 * 0.95 012597 : Another one bites the dust - bit operations with register
73 * source were broken. __MSDOS__ ifdef inserted (changes by
74 * David Kinder). Error reporting improved (initiative by
75 * Paul David Doherty).
76 *
77 * 0.96 012697 : No flag changes after MOVE xx,Ax (version>1) - Duhh...
78 *
79 * 0.97 020497 : Small output handling bug (0x7E-handling) for version>=3
80 *
81 * 0.98 040497 : Pain, agony... Another bit operation bug spotted and removed.
82 * A difference between version 0 and the rest (MOVEM.W) caused
83 * problems with Myth. Also, findproperties behaved badly with
84 * bit 14 set (version 3 only?). dict_lookup() didn't recognise
85 * composite words properly.
86 *
87 * 0.99 041697 : ADDQ/SUBQ xx,Ax doesn't set flags for (version>=3) [corruption]
88 * Small dict_lookup() fix (must be checked thoroughly once more).
89 * Difference between versions in findproperties caused problems
90 * with Jinxter. [light match/unicorn]. Integrated gfx-handling.
91 *
92 * 0.9A 050397 : = instead of == caused problems. Sign error in do_findprop()
93 * Stupid A0F1 quirk removed. SAVEMEM flag added.
94 *
95 * 0.9B 050997 : Small ms_showpic() modification.
96 *
97 * 0.9C 051297 : Fixes for running Magnetic Windows games.
98 *
99 * 0.9D 080297 : Minor improvements to save some memory. Bug fix: Last picture
100 * couldn't be extracted when SAVEMEM was set. Use time() to get
101 * initial random seed.
102 *
103 * 1.0 082897 : First public release.
104 *
105 * 2.0 081599 : [DK] Fixed Wonderland bug: ADDA and SUBA don't change
106 * the condition codes.
107 * [DK] Changed the A0EF line A opcode to do nothing, rather
108 * than exit the interpreter. This opcode is used by the
109 * PRINTER command available in some games.
110 *
111 * 2.0 082799 : [DK] Merged in Stefan Jokisch's MS-DOS changes.
112 * : [DK] Changed MS-DOS ifdefs to also depend on __BORLANDC__.
113 *
114 * 2.0 092699 : [DK] ADDA and SUBA always operate on the entire address
115 * : register, rather than using the opcode size.
116 *
117 * 2.0 101699 : [DK] Changed handling of string buffer to cope with
118 * : MS-DOS limitations.
119 *
120 * 2.0 102399 : [DK] Fixed spurious capitalization and leading white
121 * space problems.
122 *
123 * 2.0 102499 : [DK] Hopefully fixed problem in dict_lookup() with
124 * dictionary words ending with a space character.
125 *
126 * 2.0 102699 : [SM] Added code to line_a DF for displaying pictures
127 * in version 4 games. Also modified line_a E1 (readline)
128 * for additional security check to avoid false DF actions
129 *
130 * 2.0 102799 : [SM] Changed line_a DF for game locations where the
131 * picture request is not the first DF after keyboard input.
132 *
133 * 2.0 102999 : [SM] Another slight modification of DF for Collection games
134 * compatibility
135 *
136 * 2.0 103099 : [DK] Fixed "#undo" for version 4 games.
137 *
138 * 2.0 110299 : [SM] One more fix to DF code for image indexing
139 *
140 * 2.0 110799 : [DK] Disabled calls to ms_statuschar() for version 4 games.
141 * Disabled printing of spurious '@' characters to the status
142 * bar (Spectrum Pawn 2.3)
143 *
144 * 2.0 110999 : [DK] Added an extra allowed return code to ms_getchar()
145 * of 1, which means that the calling opcode should be
146 * terminated immediately. This is needed to prevent possible
147 * corruption of the game's memory in interpreters which
148 * allow a new game to be loaded without restarting
149 * (e.g. the Windows version).
150 *
151 * 2.0 112099 : [DK] Allowed all games to have a code segment greater than
152 * 65536 bytes. This is needed for Acorn Jinxter 1.3 to run.
153 *
154 * 2.0 112199 : [NK] Fixed dictionary code for Guild of Thieves 1.3.
155 *
156 * 2.0 112199 : [PDD] Made whether or not a game has a separate dictionary
157 * independant of the version number.
158 *
159 * 2.0 121399 : [DK] Changed fix for spurious capitalization to apply to
160 * all game versions.
161 *
162 * 2.0 011500 : [DK] Added support for a new graphics format for the
163 * Magnetic Windows games.
164 *
165 * 2.0 012900 : [DK] Fixed a bug in ms_extract2 and tidied up ms_showpic
166 * and ms_extract.
167 *
168 * 2.0 020500 : [DK] For v4, ms_showpic() is only called if pic_index > 0.
169 *
170 * 2.0 021100 : [DK] Updated #define for MSDOS to include lfopen().
171 *
172 * 2.0 022700 : [DK] A0E3 appears to be used to turn windows on and off.
173 * If it is called with d1 == 0 then graphics are turned off.
174 * [DK] Fixed a problem with v4 graphics not showing after
175 * loading a new .mag file into the interpreter.
176 *
177 * 2.0 040900 : [DK] Completely changed the graphics format for v4 games.
178 *
179 * 2.0 041500 : [DK] ms_get_anim_frame() now returns a masking flag.
180 *
181 * 2.0 041700 : [SM] ms_get_anim_frame() now returns the frame mask. One bug
182 * removed that messed up the frame sequence.
183 *
184 * 2.0 042200 : [DK] More animation work.
185 *
186 * 2.0 050400 : [SM] Even more animation work.
187 *
188 * 2.0 050500 : [SM] Oh no, more animation work.
189 *
190 * 2.0 050700 : [DK] A little tidying up.
191 *
192 * 2.0 060300 : [DK] More tidying and adding type casts to prevent
193 * compiler warnings.
194 *
195 * 2.0 061800 : [DK] Merged in Stefan's latest animation changes.
196 *
197 * 2.0 070900 : [DK] Figured out what the final argument to the animation
198 * command 0x01 means, with the result that nearly all the
199 * animations in Wonderland play correctly.
200 *
201 * 2.0 072300 : [DK] Added an extra check to A0E3 to stop graphics being
202 * accidentally turned off in the Collection Vol. 1 games.
203 * Added animation command 0x00 (stop) for Corruption.
204 *
205 * 2.0 080600 : [SM] Minor change to A0DF code for preventing mysterious
206 * crash in MW-Fish!
207 *
208 * 2.0 081700 : [SM] Adjusted lower code offsets for A0DF, now the missing
209 * Wonderland image comes up correctly.
210 *
211 * 2.0 081700 : [SM] Changed 03 animation command to act as "repeat comannd"
212 * with Collection games and "Stop" for Wonderland.
213 *
214 * 2.0 081700 : [SM] Changes to the 0x05 animation command for "outpal"
215 * picture.
216 *
217 * 2.0 082800 : [DK] Added ms_anim_is_repeating(), plus a few more minor
218 * corrections.
219 *
220 * 2.0 092300 : [DK] Added a workaround for problems with Wonderland's
221 * "catter" animation.
222 *
223 * 2.1 032002 : [SM] Added support for Magnetic Windows' online hints.
224 *
225 * 2.1 040202 : [SM] Added small A0DF workaround for non-supported functions.
226 *
227 * 2.1 042002 : [DK] Reset hint variables in ms_freemem().
228 *
229 * 2.2 020303 : [DK] Cleaned up hint code.
230 *
231 * 2.2 022303 : [DK] Added fallback hint support if ms_showhints() returns 0.
232 * [DK] Added argument to ms_getchar() to indicate if #undo
233 * should be translated.
234 *
235 * 2.2 022503 : [DK] Memory for hints is only allocated when needed.
236 *
237 * 2.2 031203 : [DK] Reduced the memory used for hints to about 45k, which
238 * seems to be small enough for MS-DOS.
239 *
240 * 2.3 080722 : [SM] Added support for Wonderland music scores. The scores
241 * are identified by the accompanying picture name.
242 *
243 * 2.3 080723 : [SM] Added #sound control command to toggle sound support
244 * on interpreter level
245 *
246 * 2.3 080725 : [SM] Added isSoundEnabled to check sound status on game level
247 *
248 * 2.3 080804 : [SM] line_A DF code completely rewritten. SoundEnabled,
249 * basetable and gfx_table removed
250 *
251 * 2.3 080811 : [DK] Changed prototype for ms_sndextract and removed
252 * memory leaks.
253 *
254 * 2.3 080811 : [SM] line_A DF rewrite broke hints support. Fixed.
255 *
256 * 2.3 080812 : [DK] Changed prototype for ms_playmusic and removed the
257 * need for ms_sndextract as an externally visible function.
258 *
259 * 2.3.1 181223 : [DK] Fixed a buffer over-run problem.
260 *
261 \****************************************************************************/
262
263 #include <ctype.h>
264 #include <stdio.h>
265 #include <stdlib.h>
266 #include <string.h>
267 #include <stdarg.h>
268 #include <time.h>
269 #include "defs.h"
270
271 #if defined(__MSDOS__) && defined(__BORLANDC__)
272
273 #include <alloc.h>
274
275 #define fread(p,s,n,f) lfread(p,s,n,f)
276 #define fopen(f,m) lfopen(f,m)
277 #define malloc(s) farmalloc(s)
278 #define free(p) farfree(p)
279
280 extern long lfread(void far *, long, long, FILE far *);
281 extern FILE far * lfopen(const char far *, const char far *);
282
283 #endif
284
285 type32 dreg[8], areg[8], i_count, string_size, rseed = 0, pc, arg1i, mem_size;
286 type16 properties, fl_sub, fl_tab, fl_size, fp_tab, fp_size;
287 type8 zflag, nflag, cflag, vflag, byte1, byte2, regnr, admode, opsize;
288 type8 *arg1, *arg2, is_reversible, running = 0, tmparg[4] = {0, 0, 0, 0};
289 type8 lastchar = 0, version = 0, sd = 0;
290 type8 *decode_table, *restart = 0, *code = 0, *string = 0, *string2 = 0;
291 type8 *string3 = 0, *dict = 0;
292 type8 quick_flag = 0, gfx_ver = 0, *gfx_buf = 0, *gfx_data = 0;
293 type8 *gfx2_hdr = 0, *gfx2_buf = 0;
294 type8s *gfx2_name = 0;
295 type16 gfx2_hsize = 0;
296 FILE *gfx_fp = 0;
297 type8 *snd_buf = 0, *snd_hdr = 0;
298 type16 snd_hsize = 0;
299 FILE *snd_fp = 0;
300
301 const type8s undo_ok[] = "\n[Previous turn undone.]";
302 const type8s undo_fail[] = "\n[You can't \"undo\" what hasn't been done!]";
303 type32 undo_regs[2][18], undo_pc, undo_size;
304 type8 *undo[2] = {0, 0}, undo_stat[2] = {0, 0};
305 type16 gfxtable = 0, table_dist = 0;
306 type16 v4_id = 0, next_table = 1;
307
308 struct picture
309 {
310 type8 * data;
311 type32 data_size;
312 type16 width;
313 type16 height;
314 type16 wbytes;
315 type16 plane_step;
316 type8 * mask;
317 };
318
319 #ifndef NO_ANIMATION
320
321 struct lookup
322 {
323 type16s flag;
324 type16s count;
325 };
326
327 #define MAX_POSITIONS 20
328 #define MAX_ANIMS 200
329 #define MAX_FRAMES 20
330
331 struct picture anim_frame_table[MAX_ANIMS];
332 type16 pos_table_size = 0;
333 type16 pos_table_count[MAX_POSITIONS];
334 struct ms_position pos_table[MAX_POSITIONS][MAX_ANIMS];
335 type8 * command_table = 0;
336 type16s command_index = -1;
337 struct lookup anim_table[MAX_POSITIONS];
338 type16s pos_table_index = -1;
339 type16s pos_table_max = -1;
340 struct ms_position pos_array[MAX_FRAMES];
341 type8 anim_repeat = 0;
342
343 #endif
344
345 /* Hint support */
346 #define MAX_HINTS 260
347 #define MAX_HCONTENTS 30000
348 struct ms_hint* hints = 0;
349 type8* hint_contents = 0;
350 const type8s no_hints[] = "[Hints are not available.]\n";
351 const type8s not_supported[] = "[This function is not supported.]\n";
352
353 #if defined(LOGEMU) || defined(LOGGFX) || defined(LOGHNT)
354 FILE *dbg_log;
355 #ifndef LOG_FILE
356 #error LOG_FILE must be defined to be the name of the log file.
357 #endif
358 #endif
359
360 /* prototypes */
361 type32 read_reg(int, int);
362 void write_reg(int, int, type32);
363
364 #define MAX_STRING_SIZE 0xFF00
365 #define MAX_PICTURE_SIZE 0xC800
366 #define MAX_MUSIC_SIZE 0x4E20
367
368 #ifdef LOGEMU
369 void out(char *format,...)
370 {
371 va_list a;
372
373 va_start(a, format);
374 vfprintf(dbg_log, format, a);
375 va_end(a);
376 }
377 #endif
378 #if defined(LOGGFX) || defined(LOGHNT)
379 void out2(char *format,...)
380 {
381 va_list a;
382
383 va_start(a, format);
384 vfprintf(dbg_log, format, a);
385 va_end(a);
386 fflush(dbg_log);
387 }
388 #endif
389
390 /* Convert virtual pointer to effective pointer */
391 type8 *effective(type32 ptr)
392 {
393 if ((version < 4) && (mem_size == 0x10000))
394 return &(code[ptr & 0xffff]);
395 if (ptr >= mem_size)
396 {
397 ms_fatal("Outside memory experience");
398 return code;
399 }
400 #if defined(__MSDOS__) && defined(__BORLANDC__)
401 return (type8 *) (((type8 huge *) code) + ptr);
402 #else
403 return &(code[ptr]);
404 #endif
405 }
406
407 type32 read_l(type8 * ptr)
408 {
409 return (type32) ((type32) ptr[0] << 24 | (type32) ptr[1] << 16 | (type32) ptr[2] << 8 | (type32) ptr[3]);
410 }
411
412 type16 read_w(type8 * ptr)
413 {
414 return (type16) (ptr[0] << 8 | ptr[1]);
415 }
416
417 void write_l(type8 * ptr, type32 val)
418 {
419 ptr[3] = (type8) val;
420 val >>= 8;
421 ptr[2] = (type8) val;
422 val >>= 8;
423 ptr[1] = (type8) val;
424 val >>= 8;
425 ptr[0] = (type8) val;
426 }
427
428 void write_w(type8 * ptr, type16 val)
429 {
430 ptr[1] = (type8) val;
431 val >>= 8;
432 ptr[0] = (type8) val;
433 }
434
435 type32 read_l2(type8 * ptr)
436 {
437 return ((type32) ptr[1] << 24 | (type32) ptr[0] << 16 | (type32) ptr[3] << 8 | (type32) ptr[2]);
438 }
439
440 type16 read_w2(type8 * ptr)
441 {
442 return (type16) (ptr[1] << 8 | ptr[0]);
443 }
444
445 /* Standard rand - for equal cross-platform behaviour */
446
447 void ms_seed(type32 seed)
448 {
449 rseed = seed;
450 }
451
452 type32 rand_emu(void)
453 {
454 rseed = 1103515245L * rseed + 12345L;
455 return rseed & 0x7fffffffL;
456 }
457
458 void ms_freemem(void)
459 {
460 if (code)
461 free(code);
462 if (string)
463 free(string);
464 if (string2)
465 free(string2);
466 if (string3)
467 free(string3);
468 if (dict)
469 free(dict);
470 if (undo[0])
471 free(undo[0]);
472 if (undo[1])
473 free(undo[1]);
474 if (restart)
475 free(restart);
476 code = string = string2 = string3 = dict = undo[0] = undo[1] = restart = 0;
477 if (gfx_data)
478 free(gfx_data);
479 if (gfx_buf)
480 free(gfx_buf);
481 if (gfx2_hdr)
482 free(gfx2_hdr);
483 if (gfx2_buf)
484 free(gfx2_buf);
485 if (gfx_fp)
486 fclose(gfx_fp);
487 gfx_data = gfx_buf = gfx2_hdr = gfx2_buf = 0;
488 gfx2_name = 0;
489 gfx_fp = 0;
490 gfx_ver = 0;
491 gfxtable = table_dist = 0;
492 #ifndef NO_ANIMATION
493 pos_table_size = 0;
494 command_index = 0;
495 anim_repeat = 0;
496 pos_table_index = -1;
497 pos_table_max = -1;
498 #endif
499 lastchar = 0;
500 if (hints)
501 free(hints);
502 if (hint_contents)
503 free(hint_contents);
504 hints = 0;
505 hint_contents = 0;
506 if (snd_hdr)
507 free(snd_hdr);
508 if (snd_buf)
509 free(snd_buf);
510 snd_hdr = 0;
511 snd_hsize = 0;
512 snd_buf = 0;
513 }
514
515 type8 ms_is_running(void)
516 {
517 return running;
518 }
519
520 type8 ms_is_magwin(void)
521 {
522 return (version == 4) ? 1 : 0;
523 }
524
525 void ms_stop(void)
526 {
527 running = 0;
528 }
529
530 type8 init_gfx1(type8* header)
531 {
532 #ifdef SAVEMEM
533 type32 i;
534 #endif
535
536 if (!(gfx_buf = malloc(MAX_PICTURE_SIZE)))
537 {
538 fclose(gfx_fp);
539 gfx_fp = 0;
540 return 1;
541 }
542 #ifdef SAVEMEM
543 if (!(gfx_data = malloc(128)))
544 {
545 #else
546 if (!(gfx_data = malloc(read_l(header + 4) - 8)))
547 {
548 #endif
549 free(gfx_buf);
550 fclose(gfx_fp);
551 gfx_buf = 0;
552 gfx_fp = 0;
553 return 1;
554 }
555 #ifdef SAVEMEM
556 if (!fread(gfx_data, 128, 1, gfx_fp))
557 {
558 #else
559 if (!fread(gfx_data, read_l(header + 4) - 8, 1, gfx_fp))
560 {
561 #endif
562 free(gfx_data);
563 free(gfx_buf);
564 fclose(gfx_fp);
565 gfx_data = gfx_buf = 0;
566 gfx_fp = 0;
567 return 1;
568 }
569
570 #ifdef SAVEMEM
571 for (i = 0; i < 128; i += 4)
572 if (!read_l(gfx_data + i))
573 write_l(gfx_data + i, read_l(header + 4));
574 #else
575 fclose(gfx_fp);
576 gfx_fp = 0;
577 #endif
578
579 gfx_ver = 1;
580 return 2;
581 }
582
583 type8 init_gfx2(type8* header)
584 {
585 if (!(gfx_buf = malloc(MAX_PICTURE_SIZE)))
586 {
587 fclose(gfx_fp);
588 gfx_fp = 0;
589 return 1;
590 }
591
592 gfx2_hsize = read_w(header + 4);
593 if (!(gfx2_hdr = malloc(gfx2_hsize)))
594 {
595 free(gfx_buf);
596 fclose(gfx_fp);
597 gfx_buf = 0;
598 gfx_fp = 0;
599 return 1;
600 }
601
602 fseek(gfx_fp, 6, SEEK_SET);
603 if (!fread(gfx2_hdr, gfx2_hsize, 1, gfx_fp))
604 {
605 free(gfx_buf);
606 free(gfx2_hdr);
607 fclose(gfx_fp);
608 gfx_buf = 0;
609 gfx2_hdr = 0;
610 gfx_fp = 0;
611 return 1;
612 }
613
614 gfx_ver = 2;
615 return 2;
616 }
617
618 type8 init_snd(type8* header)
619 {
620 if (!(snd_buf = malloc(MAX_MUSIC_SIZE)))
621 {
622 fclose(snd_fp);
623 snd_fp = 0;
624 return 1;
625 }
626
627 snd_hsize = read_w(header + 4);
628 if (!(snd_hdr = malloc(snd_hsize)))
629 {
630 free(snd_buf);
631 fclose(snd_fp);
632 snd_buf = 0;
633 snd_fp = 0;
634 return 1;
635 }
636
637 fseek(snd_fp, 6, SEEK_SET);
638 if (!fread(snd_hdr, snd_hsize, 1, snd_fp))
639 {
640 free(snd_buf);
641 free(snd_hdr);
642 fclose(snd_fp);
643 snd_buf = 0;
644 snd_hdr = 0;
645 snd_fp = 0;
646 return 1;
647 }
648
649 return 2;
650 }
651
652 /* zero all registers and flags and load the game */
653
654 type8 ms_init(type8s * name, type8s * gfxname, type8s * hntname, type8s * sndname)
655 {
656 FILE *fp;
657 type8 header[42], header2[8],header3[4];
658 type32 i, dict_size, string2_size, code_size, dec;
659
660 #if defined(LOGEMU) || defined(LOGGFX) || defined(LOGHNT)
661 dbg_log = fopen(LOG_FILE, "wt");
662 #endif
663 ms_stop();
664 if (!name)
665 {
666 if (!restart)
667 return 0;
668 else
669 {
670 memcpy(code, restart, undo_size);
671 undo_stat[0] = undo_stat[1] = 0;
672 ms_showpic(0, 0);
673 }
674 }
675 else
676 {
677 undo_stat[0] = undo_stat[1] = 0;
678 ms_seed((type32)time(0));
679 if (!(fp = fopen(name, "rb")))
680 return 0;
681 if ((fread(header, 1, 42, fp) != 42) || (read_l(header) != 0x4d615363))
682 {
683 fclose(fp);
684 return 0;
685 }
686 if (read_l(header + 8) != 42)
687 {
688 fclose(fp);
689 return 0;
690 }
691 ms_freemem();
692 version = header[13];
693 code_size = read_l(header + 14);
694 string_size = read_l(header + 18);
695 string2_size = read_l(header + 22);
696 dict_size = read_l(header + 26);
697 undo_size = read_l(header + 34);
698 undo_pc = read_l(header + 38);
699
700 if ((version < 4) && (code_size < 65536))
701 mem_size = 65536;
702 else
703 mem_size = code_size;
704
705 /* Some C libraries don't like malloc(0), so make
706 sure that undo_size is always positive. */
707 if (undo_size == 0)
708 undo_size = 8;
709
710 sd = (type8)((dict_size != 0L) ? 1 : 0); /* if (sd) => separate dict */
711
712 if (!(code = malloc(mem_size)) || !(string2 = malloc(string2_size)) ||
713 !(restart = malloc(undo_size)) || (sd &&
714 !(dict = malloc(dict_size))))
715 {
716 ms_freemem();
717 fclose(fp);
718 return 0;
719 }
720 if (string_size > MAX_STRING_SIZE)
721 {
722 if (!(string = malloc(MAX_STRING_SIZE)) ||
723 !(string3 = malloc(string_size - MAX_STRING_SIZE)))
724 {
725 ms_freemem();
726 fclose(fp);
727 return 0;
728 }
729 }
730 else
731 {
732 if (!(string = malloc(string_size)))
733 {
734 ms_freemem();
735 fclose(fp);
736 return 0;
737 }
738 }
739 if (!(undo[0] = malloc(undo_size)) || !(undo[1] = malloc(undo_size)))
740 {
741 ms_freemem();
742 fclose(fp);
743 return 0;
744 }
745 if (fread(code, 1, code_size, fp) != code_size)
746 {
747 ms_freemem();
748 fclose(fp);
749 return 0;
750 }
751 memcpy(restart, code, undo_size); /* fast restarts */
752 if (string_size > MAX_STRING_SIZE)
753 {
754 if (fread(string, 1, MAX_STRING_SIZE, fp) != MAX_STRING_SIZE)
755 {
756 ms_freemem();
757 fclose(fp);
758 return 0;
759 }
760 if (fread(string3, 1, string_size - MAX_STRING_SIZE, fp) != string_size - MAX_STRING_SIZE)
761 {
762 ms_freemem();
763 fclose(fp);
764 return 0;
765 }
766 }
767 else
768 {
769 if (fread(string, 1, string_size, fp) != string_size)
770 {
771 ms_freemem();
772 fclose(fp);
773 return 0;
774 }
775 }
776 if (fread(string2, 1, string2_size, fp) != string2_size)
777 {
778 ms_freemem();
779 fclose(fp);
780 return 0;
781 }
782 if (sd && fread(dict, 1, dict_size, fp) != dict_size)
783 {
784 ms_freemem();
785 fclose(fp);
786 return 0;
787 }
788 dec = read_l(header + 30);
789 if (dec >= string_size)
790 decode_table = string2 + dec - string_size;
791 else
792 {
793 if (dec >= MAX_STRING_SIZE)
794 decode_table = string3 + dec - MAX_STRING_SIZE;
795 else
796 decode_table = string + dec;
797 }
798 fclose(fp);
799 }
800
801 for (i = 0; i < 8; i++)
802 dreg[i] = areg[i] = 0;
803 write_reg(8 + 7, 2, 0xfffe); /* Stack-pointer, -2 due to MS-DOS segments */
804 pc = 0;
805 zflag = nflag = cflag = vflag = 0;
806 i_count = 0;
807 running = 1;
808
809 if (!name)
810 return (type8) (gfx_buf ? 2 : 1); /* Restarted */
811
812 if (version == 4)
813 {
814 /* Try loading a hint file */
815 FILE *hnt_fp;
816 if (hntname && (hnt_fp = fopen(hntname, "rb")))
817 {
818 if ((fread(&header3, 1, 4, hnt_fp) == 4) && (read_l(header3) == 0x4D614874))
819 {
820 type8 buf[8];
821 type16 i,j,blkcnt,elcnt,ntype,elsize,conidx;
822
823 /* Allocate memory for hints */
824 hints = malloc(MAX_HINTS * sizeof(struct ms_hint));
825 hint_contents = malloc(MAX_HCONTENTS);
826 if ((hints != 0) && (hint_contents != 0))
827 {
828 /* Read number of blocks */
829 fread(&buf, 1, 2, hnt_fp);
830 blkcnt = read_w2(buf);
831 #ifdef LOGHNT
832 out2("Blocks: %d\n",blkcnt);
833 #endif
834 conidx = 0;
835 for (i = 0; i < blkcnt; i++)
836 {
837 #ifdef LOGHNT
838 out2("\nBlock No. %d\n",i);
839 #endif
840 /* Read number of elements */
841 fread(&buf, 1, 2, hnt_fp);
842 elcnt = read_w2(buf);
843 #ifdef LOGHNT
844 out2("Elements: %d\n",elcnt);
845 #endif
846 hints[i].elcount = elcnt;
847
848 /* Read node type */
849 fread(&buf, 1, 2, hnt_fp);
850 ntype = read_w2(buf);
851 #ifdef LOGHNT
852 if (ntype == 1)
853 out2("Type: Node\n");
854 else
855 out2("Type: Leaf\n");
856 #endif
857 hints[i].nodetype = ntype;
858 hints[i].content = hint_contents+conidx;
859 #ifdef LOGHNT
860 out2("Elements:\n");
861 #endif
862 for (j = 0; j < elcnt; j++)
863 {
864 fread(&buf, 1, 2, hnt_fp);
865 elsize = read_w2(buf);
866 fread(hint_contents+conidx, 1, elsize, hnt_fp);
867 hint_contents[conidx+elsize-1] = '\0';
868 #ifdef LOGHNT
869 out2("%s\n",hint_contents+conidx);
870 #endif
871 conidx += elsize;
872 }
873
874 /* Do we need a jump table? */
875 if (ntype == 1)
876 {
877 #ifdef LOGHNT
878 out2("Jump to block:\n");
879 #endif
880 for (j = 0; j < elcnt; j++)
881 {
882 fread(&buf, 1, 2, hnt_fp);
883 hints[i].links[j] = read_w2(buf);
884 #ifdef LOGHNT
885 out2("%d\n",hints[i].links[j]);
886 #endif
887 }
888 }
889
890 /* Read the parent block */
891 fread(&buf, 1, 2, hnt_fp);
892 hints[i].parent = read_w2(buf);
893 #ifdef LOGHNT
894 out2("Parent: %d\n",hints[i].parent);
895 #endif
896 }
897 }
898 else
899 {
900 if (hints)
901 free(hints);
902 if (hint_contents)
903 free(hint_contents);
904 hints = 0;
905 hint_contents = 0;
906 }
907 }
908 fclose(hnt_fp);
909 }
910
911 /* Try loading a music file */
912 if (sndname && (snd_fp = fopen(sndname, "rb")))
913 {
914 if (fread(&header2, 1, 8, snd_fp) != 8)
915 {
916 fclose(snd_fp);
917 snd_fp = 0;
918 }
919 else
920 {
921 if (read_l(header2) == 0x4D615364) /* MaSd */
922 {
923 init_snd(header2);
924 #ifdef LOGSND
925 out2("Sound file loaded.\n");
926 #endif
927 }
928 }
929 }
930 }
931
932 if (!gfxname || !(gfx_fp = fopen(gfxname, "rb")))
933 return 1;
934 if (fread(&header2, 1, 8, gfx_fp) != 8)
935 {
936 fclose(gfx_fp);
937 gfx_fp = 0;
938 return 1;
939 }
940
941 if (version < 4 && read_l(header2) == 0x4D615069) /* MaPi */
942 return init_gfx1(header2);
943 else if (version == 4 && read_l(header2) == 0x4D615032) /* MaP2 */
944 return init_gfx2(header2);
945 fclose(gfx_fp);
946 gfx_fp = 0;
947 return 1;
948 }
949
950 type8 is_blank(type16 line, type16 width)
951 {
952 type32s i;
953
954 for (i = line * width; i < (line + 1) * width; i++)
955 if (gfx_buf[i])
956 return 0;
957 return 1;
958 }
959
960 type8 *ms_extract1(type8 pic, type16 * w, type16 * h, type16 * pal)
961 {
962 type8 *decode_table, *data, bit, val, *buffer;
963 type16 tablesize, count;
964 type32 i, j, datasize, upsize, offset;
965
966 offset = read_l(gfx_data + 4 * pic);
967 #ifdef SAVEMEM
968 if (fseek(gfx_fp, offset, SEEK_SET) < 0)
969 return 0;
970 datasize = read_l(gfx_data + 4 * (pic + 1)) - offset;
971 if (!(buffer = malloc(datasize)))
972 return 0;
973 if (fread(buffer, 1, datasize, gfx_fp) != datasize)
974 return 0;
975 #else
976 buffer = gfx_data + offset - 8;
977 #endif
978
979 for (i = 0; i < 16; i++)
980 pal[i] = read_w(buffer + 0x1c + 2 * i);
981 w[0] = (type16)(read_w(buffer + 4) - read_w(buffer + 2));
982 h[0] = read_w(buffer + 6);
983
984 tablesize = read_w(buffer + 0x3c);
985 datasize = read_l(buffer + 0x3e);
986 decode_table = buffer + 0x42;
987 data = decode_table + tablesize * 2 + 2;
988 upsize = h[0] * w[0];
989
990 for (i = 0, j = 0, count = 0, val = 0, bit = 7; i < upsize; i++, count--)
991 {
992 if (!count)
993 {
994 count = tablesize;
995 while (count < 0x80)
996 {
997 if (data[j] & (1 << bit))
998 count = decode_table[2 * count];
999 else
1000 count = decode_table[2 * count + 1];
1001 if (!bit)
1002 j++;
1003 bit = (type8)(bit ? bit - 1 : 7);
1004 }
1005 count &= 0x7f;
1006 if (count >= 0x10)
1007 count -= 0x10;
1008 else
1009 {
1010 val = (type8)count;
1011 count = 1;
1012 }
1013 }
1014 gfx_buf[i] = val;
1015 }
1016 for (j = w[0]; j < upsize; j++)
1017 gfx_buf[j] ^= gfx_buf[j - w[0]];
1018
1019 #ifdef SAVEMEM
1020 free(buffer);
1021 #endif
1022 for (; h[0] > 0 && is_blank((type16)(h[0] - 1), w[0]); h[0]--);
1023 for (i = 0; h[0] > 0 && is_blank((type16)i, w[0]); h[0]--, i++);
1024 return gfx_buf + i * w[0];
1025 }
1026
1027 type16s find_name_in_header(type8s * name, type8 upper)
1028 {
1029 type16s header_pos = 0;
1030 type8s pic_name[8];
1031 type8 i;
1032
1033 for (i = 0; i < 8; i++)
1034 pic_name[i] = 0;
1035 strncpy(pic_name,name,6);
1036 if (upper)
1037 {
1038 for (i = 0; i < 8; i++)
1039 pic_name[i] = (type8s)toupper(pic_name[i]);
1040 }
1041
1042 while (header_pos < gfx2_hsize)
1043 {
1044 type8s* hname = (type8s*)(gfx2_hdr + header_pos);
1045 if (strncmp(hname,pic_name,6) == 0)
1046 return header_pos;
1047 header_pos += 16;
1048 }
1049 return -1;
1050 }
1051
1052 void extract_frame(struct picture * pic)
1053 {
1054 type32 i, x, y, bit_x, mask, ywb, yw, value, values[4];
1055
1056 if (pic->width * pic->height > MAX_PICTURE_SIZE)
1057 {
1058 ms_fatal("picture too large");
1059 return;
1060 }
1061
1062 for (y = 0; y < pic->height; y++)
1063 {
1064 ywb = y * pic->wbytes;
1065 yw = y * pic->width;
1066
1067 for (x = 0; x < pic->width; x++)
1068 {
1069 if ((x % 8) == 0)
1070 {
1071 for (i = 0; i < 4; i++)
1072 values[i] = pic->data[ywb + (x / 8) + (pic->plane_step * i)];
1073 }
1074
1075 bit_x = 7 - (x & 7);
1076 mask = 1 << bit_x;
1077 value = ((values[0] & mask) >> bit_x) << 0|
1078 ((values[1] & mask) >> bit_x) << 1|
1079 ((values[2] & mask) >> bit_x) << 2|
1080 ((values[3] & mask) >> bit_x) << 3;
1081 value &= 15;
1082
1083 gfx_buf[yw + x] = (type8)value;
1084 }
1085 }
1086 }
1087
1088 type8 *ms_extract2(type8s * name, type16 * w, type16 * h, type16 * pal, type8 * is_anim)
1089 {
1090 struct picture main_pic;
1091 type32 offset = 0, length = 0, i;
1092 type16s header_pos = -1;
1093 #ifndef NO_ANIMATION
1094 type8* anim_data;
1095 type32 j;
1096 #endif
1097
1098 if (is_anim != 0)
1099 *is_anim = 0;
1100 gfx2_name = name;
1101
1102 #ifndef NO_ANIMATION
1103 pos_table_size = 0;
1104 #endif
1105
1106 #ifdef NO_ANIMATION
1107 /* Find the uppercase (no animation) version of the picture first. */
1108 header_pos = find_name_in_header(name,1);
1109 #endif
1110 if (header_pos < 0)
1111 header_pos = find_name_in_header(name,0);
1112 if (header_pos < 0)
1113 return 0;
1114
1115 offset = read_l(gfx2_hdr + header_pos + 8);
1116 length = read_l(gfx2_hdr + header_pos + 12);
1117
1118 if (offset != 0)
1119 {
1120 if (gfx2_buf)
1121 {
1122 free(gfx2_buf);
1123 gfx2_buf = 0;
1124 }
1125
1126 gfx2_buf = malloc(length);
1127 if (!gfx2_buf)
1128 return 0;
1129
1130 if (fseek(gfx_fp, offset, SEEK_SET) < 0)
1131 {
1132 free(gfx2_buf);
1133 gfx2_buf = 0;
1134 return 0;
1135 }
1136
1137 if (!fread(gfx2_buf, length, 1, gfx_fp))
1138 {
1139 free(gfx2_buf);
1140 gfx2_buf = 0;
1141 return 0;
1142 }
1143
1144 for (i = 0; i < 16; i++)
1145 pal[i] = read_w2(gfx2_buf + 4 + (2 * i));
1146
1147 main_pic.data = gfx2_buf + 48;
1148 main_pic.data_size = read_l2(gfx2_buf + 38);
1149 main_pic.width = read_w2(gfx2_buf + 42);
1150 main_pic.height = read_w2(gfx2_buf + 44);
1151 main_pic.wbytes = (type16)(main_pic.data_size / main_pic.height);
1152 main_pic.plane_step = (type16)(main_pic.wbytes / 4);
1153 main_pic.mask = (type8*)0;
1154 extract_frame(&main_pic);
1155
1156 *w = main_pic.width;
1157 *h = main_pic.height;
1158
1159 #ifndef NO_ANIMATION
1160 /* Check for an animation */
1161 anim_data = gfx2_buf + 48 + main_pic.data_size;
1162 if ((anim_data[0] != 0xD0) || (anim_data[1] != 0x5E))
1163 {
1164 type8 *current;
1165 type16 frame_count, command_count;
1166 type16 value1, value2;
1167
1168 if (is_anim != 0)
1169 *is_anim = 1;
1170
1171 current = anim_data + 6;
1172 frame_count = read_w2(anim_data + 2);
1173 if (frame_count > MAX_ANIMS)
1174 {
1175 ms_fatal("animation frame array too short");
1176 return 0;
1177 }
1178
1179 /* Loop through each animation frame */
1180 for (i = 0; i < frame_count; i++)
1181 {
1182 anim_frame_table[i].data = current + 10;
1183 anim_frame_table[i].data_size = read_l2(current);
1184 anim_frame_table[i].width = read_w2(current + 4);
1185 anim_frame_table[i].height = read_w2(current + 6);
1186 anim_frame_table[i].wbytes = (type16)(anim_frame_table[i].data_size / anim_frame_table[i].height);
1187 anim_frame_table[i].plane_step = (type16)(anim_frame_table[i].wbytes / 4);
1188 anim_frame_table[i].mask = (type8*)0;
1189
1190 current += anim_frame_table[i].data_size + 12;
1191 value1 = read_w2(current - 2);
1192 value2 = read_w2(current);
1193
1194 /* Get the mask */
1195 if ((value1 == anim_frame_table[i].width) && (value2 == anim_frame_table[i].height))
1196 {
1197 type16 skip;
1198
1199 anim_frame_table[i].mask = (type8*)(current + 4);
1200 skip = read_w2(current + 2);
1201 current += skip + 6;
1202 }
1203 }
1204
1205 /* Get the positioning tables */
1206 pos_table_size = read_w2(current - 2);
1207 if (pos_table_size > MAX_POSITIONS)
1208 {
1209 ms_fatal("animation position array too short");
1210 return 0;
1211 }
1212
1213 #ifdef LOGGFX_EXT
1214 out2("POSITION TABLE DUMP\n");
1215 #endif
1216 for (i = 0; i < pos_table_size; i++)
1217 {
1218 pos_table_count[i] = read_w2(current + 2);
1219 current += 4;
1220
1221 if (pos_table_count[i] > MAX_ANIMS)
1222 {
1223 ms_fatal("animation position array too short");
1224 return 0;
1225 }
1226
1227 for (j = 0; j < pos_table_count[i]; j++)
1228 {
1229 pos_table[i][j].x = read_w2(current);
1230 pos_table[i][j].y = read_w2(current + 2);
1231 pos_table[i][j].number = read_w2(current + 4) - 1;
1232 current += 8;
1233 #ifdef LOGGFX_EXT
1234 out2("Position entry: Table: %d Entry: %d X: %d Y: %d Frame: %d\n",
1235 i,j,pos_table[i][j].x,pos_table[i][j].y,pos_table[i][j].number);
1236 #endif
1237 }
1238 }
1239
1240 /* Get the command sequence table */
1241 command_count = read_w2(current);
1242 command_table = current + 2;
1243
1244 for (i = 0; i < MAX_POSITIONS; i++)
1245 {
1246 anim_table[i].flag = -1;
1247 anim_table[i].count = -1;
1248 }
1249 command_index = 0;
1250 anim_repeat = 0;
1251 pos_table_index = -1;
1252 pos_table_max = -1;
1253 }
1254 #endif
1255 return gfx_buf;
1256 }
1257 return 0;
1258 }
1259
1260 type8 *ms_extract(type32 pic, type16 * w, type16 * h, type16 * pal, type8 * is_anim)
1261 {
1262 if (is_anim)
1263 *is_anim = 0;
1264
1265 if (gfx_buf)
1266 {
1267 switch (gfx_ver)
1268 {
1269 case 1:
1270 return ms_extract1((type8)pic,w,h,pal);
1271 case 2:
1272 return ms_extract2((type8s*)(code + pic),w,h,pal,is_anim);
1273 }
1274 }
1275 return 0;
1276 }
1277
1278 type8 ms_animate(struct ms_position ** positions, type16 * count)
1279 {
1280 #ifndef NO_ANIMATION
1281 type8 got_anim = 0;
1282 type16 i, j, ttable;
1283
1284 if ((gfx_buf == 0) || (gfx2_buf == 0) || (gfx_ver != 2))
1285 return 0;
1286 if ((pos_table_size == 0) || (command_index < 0))
1287 return 0;
1288
1289 *count = 0;
1290 *positions = (struct ms_position*)0;
1291
1292 while (got_anim == 0)
1293 {
1294 if (pos_table_max >= 0)
1295 {
1296 if (pos_table_index < pos_table_max)
1297 {
1298 for (i = 0; i < pos_table_size; i++)
1299 {
1300 if (anim_table[i].flag > -1)
1301 {
1302 if (*count >= MAX_FRAMES)
1303 {
1304 ms_fatal("returned animation array too short");
1305 return 0;
1306 }
1307
1308 pos_array[*count] = pos_table[i][anim_table[i].flag];
1309 #ifdef LOGGFX_EXT
1310 out2("Adding frame %d to buffer\n",pos_array[*count].number);
1311 #endif
1312 (*count)++;
1313
1314 if (anim_table[i].flag < (pos_table_count[i]-1))
1315 anim_table[i].flag++;
1316 if (anim_table[i].count > 0)
1317 anim_table[i].count--;
1318 else
1319 anim_table[i].flag = -1;
1320 }
1321 }
1322 if (*count > 0)
1323 {
1324 *positions = pos_array;
1325 got_anim = 1;
1326 }
1327 pos_table_index++;
1328 }
1329 }
1330
1331 if (got_anim == 0)
1332 {
1333 type8 command = command_table[command_index];
1334 command_index++;
1335
1336 pos_table_max = -1;
1337 pos_table_index = -1;
1338
1339 switch (command)
1340 {
1341 case 0x00:
1342 command_index = -1;
1343 return 0;
1344 case 0x01:
1345 #ifdef LOGGFX_EXT
1346 out2("Load Frame Table: %d Start at: %d Count: %d\n",
1347 command_table[command_index],command_table[command_index+1],
1348 command_table[command_index+2]);
1349 #endif
1350 ttable = command_table[command_index];
1351 command_index++;
1352
1353 if (ttable-1 >= MAX_POSITIONS)
1354 {
1355 ms_fatal("animation table too short");
1356 return 0;
1357 }
1358
1359 anim_table[ttable-1].flag = (type16s)(command_table[command_index]-1);
1360 command_index++;
1361 anim_table[ttable-1].count = (type16s)(command_table[command_index]-1);
1362 command_index++;
1363
1364 /* Workaround for Wonderland "catter" animation */
1365 if (v4_id == 0)
1366 {
1367 if (strcmp(gfx2_name,"catter") == 0)
1368 {
1369 if (command_index == 96)
1370 anim_table[ttable-1].count = 9;
1371 if (command_index == 108)
1372 anim_table[ttable-1].flag = -1;
1373 if (command_index == 156)
1374 anim_table[ttable-1].flag = -1;
1375 }
1376 }
1377 break;
1378 case 0x02:
1379 #ifdef LOGGFX_EXT
1380 out2("Animate: %d\n", command_table[command_index]);
1381 #endif
1382 pos_table_max = command_table[command_index];
1383 pos_table_index = 0;
1384 command_index++;
1385 break;
1386 case 0x03:
1387 #ifdef LOGGFX_EXT
1388 out2("Stop/Repeat Param: %d\n", command_table[command_index]);
1389 command_index = -1;
1390 return 0;
1391 #else
1392 if (v4_id == 0)
1393 {
1394 command_index = -1;
1395 return 0;
1396 }
1397 else
1398 {
1399 command_index = 0;
1400 anim_repeat = 1;
1401 pos_table_index = -1;
1402 pos_table_max = -1;
1403 for (j = 0; j < MAX_POSITIONS; j++)
1404 {
1405 anim_table[j].flag = -1;
1406 anim_table[j].count = -1;
1407 }
1408 }
1409 break;
1410 #endif
1411 case 0x04:
1412 #ifdef LOGGFX_EXT
1413 out2("Unknown Command: %d Prop1: %d Prop2: %d WARNING:not parsed\n", command,
1414 command_table[command_index],command_table[command_index+1]);
1415 #endif
1416 command_index += 3;
1417 return 0;
1418 case 0x05:
1419 ttable = next_table;
1420 command_index++;
1421
1422 anim_table[ttable-1].flag = 0;
1423 anim_table[ttable-1].count = command_table[command_index];
1424
1425 pos_table_max = command_table[command_index];
1426 pos_table_index = 0;
1427 command_index++;
1428 command_index++;
1429 next_table++;
1430 break;
1431 default:
1432 ms_fatal("unknown animation command");
1433 command_index = -1;
1434 return 0;
1435 }
1436 }
1437 }
1438 #ifdef LOGGFX_EXT
1439 out2("ms_animate() returning %d frames\n",*count);
1440 #endif
1441 return got_anim;
1442 #else
1443 return 0;
1444 #endif
1445 }
1446
1447 type8 * ms_get_anim_frame(type16s number, type16 * width, type16 * height, type8 ** mask)
1448 {
1449 #ifndef NO_ANIMATION
1450 if (number >= 0)
1451 {
1452 extract_frame(anim_frame_table + number);
1453 *width = anim_frame_table[number].width;
1454 *height = anim_frame_table[number].height;
1455 *mask = anim_frame_table[number].mask;
1456 return gfx_buf;
1457 }
1458 #endif
1459 return 0;
1460 }
1461
1462 type8 ms_anim_is_repeating(void)
1463 {
1464 #ifndef NO_ANIMATION
1465 return anim_repeat;
1466 #else
1467 return 0;
1468 #endif
1469 }
1470
1471 type16s find_name_in_sndheader(type8s * name)
1472 {
1473 type16s header_pos = 0;
1474
1475 while (header_pos < snd_hsize)
1476 {
1477 type8s* hname = (type8s*)(snd_hdr + header_pos);
1478 if (strcmp(hname,name) == 0)
1479 return header_pos;
1480 header_pos += 18;
1481 }
1482 return -1;
1483 }
1484
1485 type8 *sound_extract(type8s * name, type32 * length, type16 * tempo)
1486 {
1487 type32 offset = 0;
1488 type16s header_pos = -1;
1489
1490 if (header_pos < 0)
1491 header_pos = find_name_in_sndheader(name);
1492 if (header_pos < 0)
1493 return 0;
1494
1495 *tempo = read_w(snd_hdr + header_pos + 8);
1496 offset = read_l(snd_hdr + header_pos + 10);
1497 *length = read_l(snd_hdr + header_pos + 14);
1498
1499 if (offset != 0)
1500 {
1501 if (!snd_buf)
1502 return 0;
1503 if (fseek(snd_fp, offset, SEEK_SET) < 0)
1504 return 0;
1505 if (!fread(snd_buf, (int)(*length), 1, snd_fp))
1506 return 0;
1507 return snd_buf;
1508 }
1509 return 0;
1510 }
1511
1512 void save_undo(void)
1513 {
1514 type8 *tmp, i;
1515 type32 tmp32;
1516
1517 tmp = undo[0]; /* swap buffers */
1518 undo[0] = undo[1];
1519 undo[1] = tmp;
1520
1521 for (i = 0; i < 18; i++)
1522 {
1523 tmp32 = undo_regs[0][i];
1524 undo_regs[0][i] = undo_regs[1][i];
1525 undo_regs[1][i] = tmp32;
1526 }
1527
1528 memcpy(undo[1], code, undo_size);
1529 for (i = 0; i < 8; i++)
1530 {
1531 undo_regs[1][i] = dreg[i];
1532 undo_regs[1][8 + i] = areg[i];
1533 }
1534 undo_regs[1][16] = i_count;
1535 undo_regs[1][17] = pc; /* status flags intentionally omitted */
1536
1537 undo_stat[0] = undo_stat[1];
1538 undo_stat[1] = 1;
1539 }
1540
1541 type8 ms_undo(void)
1542 {
1543 type8 i;
1544
1545 ms_flush();
1546 if (!undo_stat[0])
1547 return 0;
1548
1549 undo_stat[0] = undo_stat[1] = 0;
1550 memcpy(code, undo[0], undo_size);
1551 for (i = 0; i < 8; i++)
1552 {
1553 dreg[i] = undo_regs[0][i];
1554 areg[i] = undo_regs[0][8 + i];
1555 }
1556 i_count = undo_regs[0][16];
1557 pc = undo_regs[0][17]; /* status flags intentionally omitted */
1558 return 1;
1559 }
1560
1561 #ifdef LOGEMU
1562 void log_status(void)
1563 {
1564 int j;
1565
1566 fprintf(dbg_log, "\nD0:");
1567 for (j = 0; j < 8; j++)
1568 fprintf(dbg_log, " %8.8x", read_reg(j, 3));
1569 fprintf(dbg_log, "\nA0:");
1570 for (j = 0; j < 8; j++)
1571 fprintf(dbg_log, " %8.8x", read_reg(8 + j, 3));
1572 fprintf(dbg_log, "\nPC=%5.5x (%8.8x) ZCNV=%d%d%d%d - %d instructions\n\n",
1573 pc, code, zflag & 1, cflag & 1, nflag & 1, vflag & 1, i_count);
1574 }
1575 #endif
1576
1577 void ms_status(void)
1578 {
1579 int j;
1580
1581 fprintf(stderr, "D0:");
1582 for (j = 0; j < 8; j++)
1583 fprintf(stderr, " %8.8lx", (long) read_reg(j, 3));
1584 fprintf(stderr, "\nA0:");
1585 for (j = 0; j < 8; j++)
1586 fprintf(stderr, " %8.8lx", (long) read_reg(8 + j, 3));
1587 fprintf(stderr, "\nPC=%5.5lx ZCNV=%d%d%d%d - %ld instructions\n",
1588 (long) pc, zflag & 1, cflag & 1, nflag & 1, vflag & 1, (long) i_count);
1589 }
1590
1591 type32 ms_count(void)
1592 {
1593 return i_count;
1594 }
1595
1596 /* align register pointer for word/byte accesses */
1597
1598 type8 *reg_align(type8 * ptr, type8 size)
1599 {
1600 if (size == 1)
1601 ptr += 2;
1602 if (size == 0)
1603 ptr += 3;
1604 return ptr;
1605 }
1606
1607 type32 read_reg(int i, int s)
1608 {
1609 type8 *ptr;
1610
1611 if (i > 15)
1612 {
1613 ms_fatal("invalid register in read_reg");
1614 return 0;
1615 }
1616 if (i < 8)
1617 ptr = (type8 *) & dreg[i];
1618 else
1619 ptr = (type8 *) & areg[i - 8];
1620
1621 switch (s)
1622 {
1623 case 0:
1624 return reg_align(ptr, 0)[0];
1625 case 1:
1626 return read_w(reg_align(ptr, 1));
1627 default:
1628 return read_l(ptr);
1629 }
1630 }
1631
1632 void write_reg(int i, int s, type32 val)
1633 {
1634 type8 *ptr;
1635
1636 if (i > 15)
1637 {
1638 ms_fatal("invalid register in write_reg");
1639 return;
1640 }
1641 if (i < 8)
1642 ptr = (type8 *) & dreg[i];
1643 else
1644 ptr = (type8 *) & areg[i - 8];
1645
1646 switch (s)
1647 {
1648 case 0:
1649 reg_align(ptr, 0)[0] = (type8)val;
1650 break;
1651 case 1:
1652 write_w(reg_align(ptr, 1), (type16)val);
1653 break;
1654 default:
1655 write_l(ptr, val);
1656 break;
1657 }
1658 }
1659
1660 /* [35c4] */
1661
1662 void char_out(type8 c)
1663 {
1664 static type8 big = 0, period = 0, pipe = 0;
1665
1666 if (c == 0xff)
1667 {
1668 big = 1;
1669 return;
1670 }
1671 c &= 0x7f;
1672 if (read_reg(3, 0))
1673 {
1674 if (c == 0x5f || c == 0x40)
1675 c = 0x20;
1676 if ((c >= 'a') && (c <= 'z'))
1677 c &= 0xdf;
1678 if (version < 4)
1679 ms_statuschar(c);
1680 return;
1681 }
1682 if (c == 0x5e)
1683 c = 0x0a;
1684 if (c == 0x40)
1685 {
1686 if (read_reg(2, 0))
1687 return;
1688 else
1689 c = 0x73;
1690 }
1691 if (version < 3 && c == 0x7e)
1692 {
1693 lastchar = 0x7e;
1694 c = 0x0a;
1695 }
1696 if (((c > 0x40) && (c < 0x5b)) || ((c > 0x60) && (c < 0x7b)))
1697 {
1698 if (big)
1699 {
1700 c &= 0xdf;
1701 big = 0;
1702 }
1703 if (period)
1704 char_out(0x20);
1705 }
1706 period = 0;
1707 if (version < 4)
1708 {
1709 if ((c == 0x2e) || (c == 0x3f) || (c == 0x21) || (c == 0x0a))
1710 big = 1;
1711 else if (c == 0x22)
1712 big = 0;
1713 }
1714 else
1715 {
1716 if ((c == 0x20) && (lastchar == 0x0a))
1717 return;
1718 if ((c == 0x2e) || (c == 0x3f) || (c == 0x21) || (c == 0x0a))
1719 big = 1;
1720 else if (c == 0x22)
1721 big = 0;
1722 }
1723 if (((c == 0x20) || (c == 0x0a)) && (c == lastchar))
1724 return;
1725 if (version < 3)
1726 {
1727 if (pipe)
1728 {
1729 pipe = 0;
1730 return;
1731 }
1732 if (c == 0x7c)
1733 {
1734 pipe = 1;
1735 return;
1736 }
1737 }
1738 else
1739 {
1740 if (c == 0x7e)
1741 {
1742 c = 0x0a;
1743 if (lastchar != 0x0a)
1744 char_out(0x0a);
1745 }
1746 }
1747 lastchar = c;
1748 if (c == 0x5f)
1749 c = 0x20;
1750 if ((c == 0x2e) || (c == 0x2c) || (c == 0x3b) || (c == 0x3a) || (c == 0x21) || (c == 0x3f))
1751 period = 1;
1752 ms_putchar(c);
1753 }
1754
1755
1756 /* extract addressing mode information [1c6f] */
1757
1758 void set_info(type8 b)
1759 {
1760 regnr = (type8)(b & 0x07);
1761 admode = (type8)((b >> 3) & 0x07);
1762 opsize = (type8)(b >> 6);
1763 }
1764
1765 /* read a word and increase pc */
1766
1767 void read_word(void)
1768 {
1769 type8 *epc;
1770
1771 epc = effective(pc);
1772 byte1 = epc[0];
1773 byte2 = epc[1];
1774 pc += 2;
1775 }
1776
1777 /* get addressing mode and set arg1 [1c84] */
1778
1779 void set_arg1(void)
1780 {
1781 type8 tmp[2], l1c;
1782
1783 is_reversible = 1;
1784 switch (admode)
1785 {
1786 case 0:
1787 arg1 = reg_align((type8 *) & dreg[regnr], opsize); /* Dx */
1788 is_reversible = 0;
1789 #ifdef LOGEMU
1790 out(" d%.1d", regnr);
1791 #endif
1792 break;
1793 case 1:
1794 arg1 = reg_align((type8 *) & areg[regnr], opsize); /* Ax */
1795 is_reversible = 0;
1796 #ifdef LOGEMU
1797 out(" a%.1d", regnr);
1798 #endif
1799 break;
1800 case 2:
1801 arg1i = read_reg(8 + regnr, 2); /* (Ax) */
1802 #ifdef LOGEMU
1803 out(" (a%.1d)", regnr);
1804 #endif
1805 break;
1806 case 3:
1807 arg1i = read_reg(8 + regnr, 2); /* (Ax)+ */
1808 write_reg(8 + regnr, 2, read_reg(8 + regnr, 2) + (1 << opsize));
1809 #ifdef LOGEMU
1810 out(" (a%.1d)+", regnr);
1811 #endif
1812 break;
1813 case 4:
1814 write_reg(8 + regnr, 2, read_reg(8 + regnr, 2) - (1 << opsize));
1815 arg1i = read_reg(8 + regnr, 2); /* -(Ax) */
1816 #ifdef LOGEMU
1817 out(" -(a%.1d)", regnr);
1818 #endif
1819 break;
1820 case 5:
1821 {
1822 type16s i = (type16s) read_w(effective(pc));
1823 arg1i = read_reg(8 + regnr, 2) + i;
1824 pc += 2; /* offset.w(Ax) */
1825 #ifdef LOGEMU
1826 out(" %X(a%.1d)", i, regnr);
1827 #endif
1828 }
1829 break;
1830 case 6:
1831 tmp[0] = byte1;
1832 tmp[1] = byte2;
1833 read_word(); /* offset.b(Ax, Dx/Ax) [1d1c] */
1834 #ifdef LOGEMU
1835 out(" %.2X(a%.1d,", (int) byte2, regnr);
1836 #endif
1837 arg1i = read_reg(regnr + 8, 2) + (type8s) byte2;
1838 #ifdef LOGEMU
1839 if ((byte1 >> 4) > 8)
1840 out("a%.1d", (byte1 >> 4) - 8);
1841 else
1842 out("d%.1d", byte1 >> 4);
1843 #endif
1844 if (byte1 & 0x08)
1845 {
1846 #ifdef LOGEMU
1847 out(".l)");
1848 #endif
1849 arg1i += (type32s) read_reg((byte1 >> 4), 2);
1850 }
1851 else
1852 {
1853 #ifdef LOGEMU
1854 out(".w)");
1855 #endif
1856 arg1i += (type16s) read_reg((byte1 >> 4), 1);
1857 }
1858 byte1 = tmp[0];
1859 byte2 = tmp[1];
1860 break;
1861 case 7: /* specials */
1862 switch (regnr)
1863 {
1864 case 0:
1865 arg1i = read_w(effective(pc)); /* $xxxx.W */
1866 pc += 2;
1867 #ifdef LOGEMU
1868 out(" %.4X.w", arg1i);
1869 #endif
1870 break;
1871 case 1:
1872 arg1i = read_l(effective(pc)); /* $xxxx */
1873 pc += 4;
1874 #ifdef LOGEMU
1875 out(" %.4X", arg1i);
1876 #endif
1877 break;
1878 case 2:
1879 arg1i = (type16s) read_w(effective(pc)) + pc; /* $xxxx(PC) */
1880 pc += 2;
1881 #ifdef LOGEMU
1882 out(" %.4X(pc)", arg1i);
1883 #endif
1884 break;
1885 case 3:
1886 l1c = effective(pc)[0]; /* $xx(PC,A/Dx) */
1887 #ifdef LOGEMU
1888 out(" ???2", arg1i);
1889 #endif
1890 if (l1c & 0x08)
1891 arg1i = pc + (type32s) read_reg((l1c >> 4), 2);
1892 else
1893 arg1i = pc + (type16s) read_reg((l1c >> 4), 1);
1894 l1c = effective(pc)[1];
1895 pc += 2;
1896 arg1i += (type8s) l1c;
1897 break;
1898 case 4:
1899 arg1i = pc; /* #$xxxx */
1900 if (opsize == 0)
1901 arg1i += 1;
1902 pc += 2;
1903 if (opsize == 2)
1904 pc += 2;
1905 #ifdef LOGEMU
1906 out(" #%.4X", arg1i);
1907 #endif
1908 break;
1909 }
1910 break;
1911 }
1912 if (is_reversible)
1913 arg1 = effective(arg1i);
1914 }
1915
1916 /* get addressing mode and set arg2 [1bc5] */
1917
1918 void set_arg2_nosize(int use_dx, type8 b)
1919 {
1920 if (use_dx)
1921 arg2 = (type8 *) dreg;
1922 else
1923 arg2 = (type8 *) areg;
1924 arg2 += (b & 0x0e) << 1;
1925 }
1926
1927 void set_arg2(int use_dx, type8 b)
1928 {
1929 set_arg2_nosize(use_dx, b);
1930 arg2 = reg_align(arg2, opsize);
1931 }
1932
1933 /* [1b9e] */
1934
1935 void swap_args(void)
1936 {
1937 type8 *tmp;
1938
1939 tmp = arg1;
1940 arg1 = arg2;
1941 arg2 = tmp;
1942 }
1943
1944 /* [1cdc] */
1945
1946 void push(type32 c)
1947 {
1948 write_reg(15, 2, read_reg(15, 2) - 4);
1949 write_l(effective(read_reg(15, 2)), c);
1950 }
1951
1952 /* [1cd1] */
1953
1954 type32 pop(void)
1955 {
1956 type32 c;
1957
1958 c = read_l(effective(read_reg(15, 2)));
1959 write_reg(15, 2, read_reg(15, 2) + 4);
1960 return c;
1961 }
1962
1963 /* check addressing mode and get argument [2e85] */
1964
1965 void get_arg(void)
1966 {
1967 #ifdef LOGEMU
1968 out(" %.4X", pc);
1969 #endif
1970 set_info(byte2);
1971 arg2 = effective(pc);
1972 pc += 2;
1973 if (opsize == 2)
1974 pc += 2;
1975 if (opsize == 0)
1976 arg2 += 1;
1977 set_arg1();
1978 }
1979
1980 void set_flags(void)
1981 {
1982 type16 i;
1983 type32 j;
1984
1985 zflag = nflag = 0;
1986 switch (opsize)
1987 {
1988 case 0:
1989 if (arg1[0] > 127)
1990 nflag = 0xff;
1991 if (arg1[0] == 0)
1992 zflag = 0xff;
1993 break;
1994 case 1:
1995 i = read_w(arg1);
1996 if (i == 0)
1997 zflag = 0xff;
1998 if ((i >> 15) > 0)
1999 nflag = 0xff;
2000 break;
2001 case 2:
2002 j = read_l(arg1);
2003 if (j == 0)
2004 zflag = 0xff;
2005 if ((j >> 31) > 0)
2006 nflag = 0xff;
2007 break;
2008 }
2009 }
2010
2011 /* [263a] */
2012
2013 int condition(type8 b)
2014 {
2015 switch (b & 0x0f)
2016 {
2017 case 0:
2018 return 0xff;
2019 case 1:
2020 return 0x00;
2021 case 2:
2022 return (zflag | cflag) ^ 0xff;
2023 case 3:
2024 return (zflag | cflag);
2025 case 4:
2026 return cflag ^ 0xff;
2027 case 5:
2028 return cflag;
2029 case 6:
2030 return zflag ^ 0xff;
2031 case 7:
2032 return zflag;
2033 case 8:
2034 return vflag ^ 0xff;
2035 case 9:
2036 return vflag;
2037 case 10:
2038 case 12:
2039 return nflag ^ 0xff;
2040 case 11:
2041 case 13:
2042 return nflag;
2043 case 14:
2044 return (zflag | nflag) ^ 0xff;
2045 case 15:
2046 return (zflag | nflag);
2047 }
2048 return 0x00;
2049 }
2050
2051 /* [26dc] */
2052
2053 void branch(type8 b)
2054 {
2055 if (b == 0)
2056 pc += (type16s) read_w(effective(pc));
2057 else
2058 pc += (type8s) b;
2059 #ifdef LOGEMU
2060 out(" %.4X", pc);
2061 #endif
2062 }
2063
2064 /* [2869] */
2065
2066 void do_add(type8 adda)
2067 {
2068 if (adda)
2069 {
2070 if (opsize == 0)
2071 write_l(arg1, read_l(arg1) + (type8s) arg2[0]);
2072 if (opsize == 1)
2073 write_l(arg1, read_l(arg1) + (type16s) read_w(arg2));
2074 if (opsize == 2)
2075 write_l(arg1, read_l(arg1) + (type32s) read_l(arg2));
2076 }
2077 else
2078 {
2079 cflag = 0;
2080 if (opsize == 0)
2081 {
2082 arg1[0] += arg2[0];
2083 if (arg2[0] > arg1[0])
2084 cflag = 0xff;
2085 }
2086 if (opsize == 1)
2087 {
2088 write_w(arg1, (type16)(read_w(arg1) + read_w(arg2)));
2089 if (read_w(arg2) > read_w(arg1))
2090 cflag = 0xff;
2091 }
2092 if (opsize == 2)
2093 {
2094 write_l(arg1, read_l(arg1) + read_l(arg2));
2095 if (read_l(arg2) > read_l(arg1))
2096 cflag = 0xff;
2097 }
2098 if (version < 3 || !quick_flag)
2099 {
2100 /* Corruption onwards */
2101 vflag = 0;
2102 set_flags();
2103 }
2104 }
2105 }
2106
2107 /* [2923] */
2108
2109 void do_sub(type8 suba)
2110 {
2111 if (suba)
2112 {
2113 if (opsize == 0)
2114 write_l(arg1, read_l(arg1) - (type8s) arg2[0]);
2115 if (opsize == 1)
2116 write_l(arg1, read_l(arg1) - (type16s) read_w(arg2));
2117 if (opsize == 2)
2118 write_l(arg1, read_l(arg1) - (type32s) read_l(arg2));
2119 }
2120 else
2121 {
2122 cflag = 0;
2123 if (opsize == 0)
2124 {
2125 if (arg2[0] > arg1[0])
2126 cflag = 0xff;
2127 arg1[0] -= arg2[0];
2128 }
2129 if (opsize == 1)
2130 {
2131 if (read_w(arg2) > read_w(arg1))
2132 cflag = 0xff;
2133 write_w(arg1, (type16)(read_w(arg1) - read_w(arg2)));
2134 }
2135 if (opsize == 2)
2136 {
2137 if (read_l(arg2) > read_l(arg1))
2138 cflag = 0xff;
2139 write_l(arg1, read_l(arg1) - read_l(arg2));
2140 }
2141 if (version < 3 || !quick_flag)
2142 {
2143 /* Corruption onwards */
2144 vflag = 0;
2145 set_flags();
2146 }
2147 }
2148 }
2149
2150 /* [283b] */
2151
2152 void do_eor(void)
2153 {
2154 if (opsize == 0)
2155 arg1[0] ^= arg2[0];
2156 if (opsize == 1)
2157 write_w(arg1, (type16)(read_w(arg1) ^ read_w(arg2)));
2158 if (opsize == 2)
2159 write_l(arg1, read_l(arg1) ^ read_l(arg2));
2160 cflag = vflag = 0;
2161 set_flags();
2162 }
2163
2164 /* [280d] */
2165
2166 void do_and(void)
2167 {
2168 if (opsize == 0)
2169 arg1[0] &= arg2[0];
2170 if (opsize == 1)
2171 write_w(arg1, (type16)(read_w(arg1) & read_w(arg2)));
2172 if (opsize == 2)
2173 write_l(arg1, read_l(arg1) & read_l(arg2));
2174 cflag = vflag = 0;
2175 set_flags();
2176 }
2177
2178 /* [27df] */
2179
2180 void do_or(void)
2181 {
2182 if (opsize == 0)
2183 arg1[0] |= arg2[0];
2184 if (opsize == 1)
2185 write_w(arg1, (type16)(read_w(arg1) | read_w(arg2)));
2186 if (opsize == 2)
2187 write_l(arg1, read_l(arg1) | read_l(arg2));
2188 cflag = vflag = 0;
2189 set_flags(); /* [1c2b] */
2190 }
2191
2192 /* [289f] */
2193
2194 void do_cmp(void)
2195 {
2196 type8 *tmp;
2197
2198 tmp = arg1;
2199 tmparg[0] = arg1[0];
2200 if (opsize != 0)
2201 tmparg[1] = arg1[1];
2202 if (opsize == 2)
2203 {
2204 tmparg[2] = arg1[2];
2205 tmparg[3] = arg1[3];
2206 }
2207 arg1 = tmparg;
2208 quick_flag = 0;
2209 do_sub(0);
2210 arg1 = tmp;
2211 }
2212
2213 /* [2973] */
2214
2215 void do_move(void)
2216 {
2217
2218 if (opsize == 0)
2219 arg1[0] = arg2[0];
2220 if (opsize == 1)
2221 write_w(arg1, read_w(arg2));
2222 if (opsize == 2)
2223 write_l(arg1, read_l(arg2));
2224 if (version < 2 || admode != 1)
2225 {
2226 /* Jinxter: no flags if destination Ax */
2227 cflag = vflag = 0;
2228 set_flags();
2229 }
2230 }
2231
2232 type8 do_btst(type8 a)
2233 {
2234 a &= admode ? 0x7 : 0x1f;
2235 while (admode == 0 && a >= 8)
2236 {
2237 a -= 8;
2238 arg1 -= 1;
2239 }
2240 zflag = 0;
2241 if ((arg1[0] & (1 << a)) == 0)
2242 zflag = 0xff;
2243 return a;
2244 }
2245
2246 /* bit operation entry point [307c] */
2247
2248 void do_bop(type8 b, type8 a)
2249 {
2250 #ifdef LOGEMU
2251 out("bop (%.2x,%.2x) ", (int) b, (int) a);
2252 #endif
2253 b = b & 0xc0;
2254 a = do_btst(a);
2255 #ifdef LOGEMU
2256 if (b == 0x00)
2257 out("no bop???");
2258 #endif
2259 if (b == 0x40)
2260 {
2261 arg1[0] ^= (1 << a); /* bchg */
2262 #ifdef LOGEMU
2263 out("bchg");
2264 #endif
2265 }
2266 if (b == 0x80)
2267 {
2268 arg1[0] &= ((1 << a) ^ 0xff); /* bclr */
2269 #ifdef LOGEMU
2270 out("bclr");
2271 #endif
2272 }
2273 if (b == 0xc0)
2274 {
2275 arg1[0] |= (1 << a); /* bset */
2276 #ifdef LOGEMU
2277 out("bset");
2278 #endif
2279 }
2280 }
2281
2282 void check_btst(void)
2283 {
2284 #ifdef LOGEMU
2285 out("btst");
2286 #endif
2287 set_info((type8)(byte2 & 0x3f));
2288 set_arg1();
2289 set_arg2(1, byte1);
2290 do_bop(byte2, arg2[0]);
2291 }
2292
2293 void check_lea(void)
2294 {
2295 #ifdef LOGEMU
2296 out("lea");
2297 #endif
2298 if ((byte2 & 0xc0) == 0xc0)
2299 {
2300 set_info(byte2);
2301 opsize = 2;
2302 set_arg1();
2303 set_arg2(0, byte1);
2304 write_w(arg2, 0);
2305 if (is_reversible)
2306 write_l(arg2, arg1i);
2307 else
2308 ms_fatal("illegal addressing mode for LEA");
2309 }
2310 else
2311 {
2312 ms_fatal("unimplemented instruction CHK");
2313 }
2314 }
2315
2316 /* [33cc] */
2317
2318 void check_movem(void)
2319 {
2320 type8 l1c;
2321
2322 #ifdef LOGEMU
2323 out("movem");
2324 #endif
2325 set_info((type8)(byte2 - 0x40));
2326 read_word();
2327 for (l1c = 0; l1c < 8; l1c++)
2328 {
2329 if (byte2 & 1 << l1c)
2330 {
2331 set_arg1();
2332 if (opsize == 2)
2333 write_l(arg1, read_reg(15 - l1c, 2));
2334 if (opsize == 1)
2335 write_w(arg1, (type16)read_reg(15 - l1c, 1));
2336 }
2337 }
2338 for (l1c = 0; l1c < 8; l1c++)
2339 {
2340 if (byte1 & 1 << l1c)
2341 {
2342 set_arg1();
2343 if (opsize == 2)
2344 write_l(arg1, read_reg(7 - l1c, 2));
2345 if (opsize == 1)
2346 write_w(arg1, (type16)read_reg(7 - l1c, 1));
2347 }
2348 }
2349 }
2350
2351 /* [3357] */
2352
2353 void check_movem2(void)
2354 {
2355 type8 l1c;
2356
2357 #ifdef LOGEMU
2358 out("movem (2)");
2359 #endif
2360 set_info((type8)(byte2 - 0x40));
2361 read_word();
2362 for (l1c = 0; l1c < 8; l1c++)
2363 {
2364 if (byte2 & 1 << l1c)
2365 {
2366 set_arg1();
2367 if (opsize == 2)
2368 write_reg(l1c, 2, read_l(arg1));
2369 if (opsize == 1)
2370 write_reg(l1c, 1, read_w(arg1));
2371 }
2372 }
2373 for (l1c = 0; l1c < 8; l1c++)
2374 {
2375 if (byte1 & 1 << l1c)
2376 {
2377 set_arg1();
2378 if (opsize == 2)
2379 write_reg(8 + l1c, 2, read_l(arg1));
2380 if (opsize == 1)
2381 write_reg(8 + l1c, 1, read_w(arg1));
2382 }
2383 }
2384 }
2385
2386 /* [30e4] in Jinxter, ~540 lines of 6510 spaghetti-code */
2387 /* The mother of all bugs, but hey - no gotos used :-) */
2388
2389 void dict_lookup(void)
2390 {
2391 type16 dtab, doff, output, output_bak, bank, word, output2;
2392 type16 tmp16, i, obj_adj, adjlist, adjlist_bak;
2393 type8 c, c2, c3, flag, matchlen, longest, flag2;
2394 type8 restart = 0, accept = 0;
2395
2396 /*
2397 dtab=A5.W ;dict_table offset <L22>
2398 output=output_bak=A2.W ;output <L24>
2399 A5.W=A6.W ;input word
2400 doff=A3.W ;lookup offset (doff) <L1C>
2401 adjlist=A0.W ;adjlist <L1E>
2402 */
2403
2404 dtab = (type16)read_reg(8 + 5, 1); /* used by version>0 */
2405 output = (type16)read_reg(8 + 2, 1);
2406 write_reg(8 + 5, 1, read_reg(8 + 6, 1));
2407 doff = (type16)read_reg(8 + 3, 1);
2408 adjlist = (type16)read_reg(8 + 0, 1);
2409
2410 bank = (type16)read_reg(6, 0); /* l2d */
2411 flag = 0; /* l2c */
2412 word = 0; /* l26 */
2413 matchlen = 0; /* l2e */
2414 longest = 0; /* 30e2 */
2415 write_reg(0, 1, 0); /* apostroph */
2416
2417 while ((c = sd ? dict[doff] : effective(doff)[0]) != 0x81)
2418 {
2419 if (c >= 0x80)
2420 {
2421 if (c == 0x82)
2422 {
2423 flag = matchlen = 0;
2424 word = 0;
2425 write_reg(8 + 6, 1, read_reg(8 + 5, 1));
2426 bank++;
2427 doff++;
2428 continue;
2429 }
2430 c3 = c;
2431 c &= 0x5f;
2432 c2 = effective(read_reg(8 + 6, 1))[0] & 0x5f;
2433 if (c2 == c)
2434 {
2435 write_reg(8 + 6, 1, read_reg(8 + 6, 1) + 1);
2436 c = effective(read_reg(8 + 6, 1))[0];
2437 if ((!c) || (c == 0x20) || (c == 0x27) || (!version && (matchlen > 6)))
2438 {
2439 if (c == 0x27)
2440 {
2441 write_reg(8 + 6, 1, read_reg(8 + 6, 1) + 1);
2442 write_reg(0, 1, 0x200 + effective(read_reg(8 + 6, 1))[0]);
2443 }
2444 if ((version < 4) || (c3 != 0xa0))
2445 accept = 1;
2446 }
2447 else
2448 restart = 1;
2449 }
2450 else if (!version && matchlen > 6 && !c2)
2451 accept = 1;
2452 else
2453 restart = 1;
2454 }
2455 else
2456 {
2457 c &= 0x5f;
2458 c2 = effective(read_reg(8 + 6, 1))[0] & 0x5f;
2459 if ((c2 == c && c) || (version && !c2 && (c == 0x5f)))
2460 {
2461 if (version && !c2 && (c == 0x5f))
2462 flag = 0x80;
2463 matchlen++;
2464 write_reg(8 + 6, 1, read_reg(8 + 6, 1) + 1);
2465 doff++;
2466 }
2467 else if (!version && matchlen > 6 && !c2)
2468 accept = 1;
2469 else
2470 restart = 1;
2471 }
2472 if (accept)
2473 {
2474 effective(read_reg(8 + 2, 1))[0] = (version) ? flag : 0;
2475 effective(read_reg(8 + 2, 1))[1] = (type8)bank;
2476 write_w(effective(read_reg(8 + 2, 1) + 2), word);
2477 write_reg(8 + 2, 1, read_reg(8 + 2, 1) + 4);
2478 if (matchlen >= longest)
2479 longest = matchlen;
2480 restart = 1;
2481 accept = 0;
2482 }
2483 if (restart)
2484 {
2485 write_reg(8 + 6, 1, read_reg(8 + 5, 1));
2486 flag = matchlen = 0;
2487 word++;
2488 if (sd)
2489 while (dict[doff++] < 0x80);
2490 else
2491 while (effective(doff++)[0] < 0x80);
2492 restart = 0;
2493 }
2494 }
2495 write_w(effective(read_reg(8 + 2, 1)), 0xffff);
2496
2497 if (version)
2498 {
2499 /* version > 0 */
2500 output_bak = output; /* check synonyms */
2501 while ((c = effective(output)[1]) != 0xff)
2502 {
2503 if (c == 0x0b)
2504 {
2505 if (sd)
2506 tmp16 = read_w(&dict[dtab + read_w(effective(output + 2)) * 2]);
2507 else
2508 tmp16 = read_w(effective(dtab + read_w(effective(output + 2)) * 2));
2509 effective(output)[1] = tmp16 & 0x1f;
2510 write_w(effective(output + 2), (type16)(tmp16 >> 5));
2511 }
2512 output += 4;
2513 }
2514 output = output_bak;
2515 }
2516
2517 /* l22 = output2, l1e = adjlist, l20 = obj_adj, l26 = word, l2f = c2 */
2518 /* l1c = adjlist_bak, 333C = i, l2d = bank, l2c = flag, l30e3 = flag2 */
2519
2520 write_reg(1, 1, 0); /* D1.W=0 [32B5] */
2521 flag2 = 0;
2522 output_bak = output;
2523 output2 = output;
2524 while ((bank = effective(output2)[1]) != 0xff)
2525 {
2526 obj_adj = (type16)read_reg(8 + 1, 1); /* A1.W - obj_adj, ie. adjs for this word */
2527 write_reg(1, 0, 0); /* D1.B=0 */
2528 flag = effective(output2)[0]; /* flag */
2529 word = read_w(effective(output2 + 2)); /* wordnumber */
2530 output2 += 4; /* next match */
2531 if ((read_w(effective(obj_adj))) && (bank == 6))
2532 {
2533 /* Any adjectives? */
2534 if ((i = word) != 0)
2535 {
2536 /* Find list of valid adjs */
2537 do
2538 {
2539 while (effective(adjlist++)[0]);
2540 }
2541 while (--i > 0);
2542 }
2543 adjlist_bak = adjlist;
2544 do
2545 {
2546 adjlist = adjlist_bak;
2547 c2 = effective(obj_adj)[1]; /* given adjective */
2548 if ((tmp16 = read_w(effective(obj_adj))) != 0)
2549 {
2550 obj_adj += 2;
2551 while ((c = effective(adjlist++)[0]) && (c - 3 != c2));
2552 if (c - 3 != c2)
2553 write_reg(1, 0, 1); /* invalid adjective */
2554 }
2555 }
2556 while (tmp16 && !read_reg(1, 0));
2557 adjlist = (type16)read_reg(8 + 0, 1);
2558 }
2559 if (!read_reg(1, 0))
2560 {
2561 /* invalid_flag */
2562 flag2 |= flag;
2563 effective(output)[0] = flag2;
2564 effective(output)[1] = (type8)bank;
2565 write_w(effective(output + 2), word);
2566 output += 4;
2567 }
2568 }
2569 write_reg(8 + 2, 1, output);
2570 output = output_bak;
2571
2572 if (flag2 & 0x80)
2573 {
2574 tmp16 = output;
2575 output -= 4;
2576 do
2577 {
2578 output += 4;
2579 c = effective(output)[0];
2580 }
2581 while (!(c & 0x80));
2582 write_l(effective(tmp16), read_l(effective(output)) & 0x7fffffff);
2583 write_reg(8 + 2, 2, tmp16 + 4);
2584 if (longest > 1)
2585 {
2586 write_reg(8 + 5, 1, read_reg(8 + 5, 1) + longest - 2);
2587 }
2588 }
2589 write_reg(8 + 6, 1, read_reg(8 + 5, 1) + 1);
2590 }
2591
2592 /* A0=findproperties(D0) [2b86], properties_ptr=[2b78] A0FE */
2593
2594 void do_findprop(void)
2595 {
2596 type16 tmp;
2597
2598 if ((version > 2) && ((read_reg(0, 1) & 0x3fff) > fp_size))
2599 {
2600 tmp = (type16)(((fp_size - (read_reg(0, 1) & 0x3fff)) ^ 0xffff) << 1);
2601 tmp += fp_tab;
2602 tmp = read_w(effective(tmp));
2603 }
2604 else
2605 {
2606 if (version < 2)
2607 write_reg(0, 2, read_reg(0, 2) & 0x7fff);
2608 else
2609 write_reg(0, 1, read_reg(0, 1) & 0x7fff);
2610 tmp = (type16)read_reg(0, 1);
2611 }
2612 tmp &= 0x3fff;
2613 write_reg(8 + 0, 2, tmp * 14 + properties);
2614 }
2615
2616 void write_string(void)
2617 {
2618 static type32 offset_bak;
2619 static type8 mask_bak;
2620 type8 c, b, mask;
2621 type16 ptr;
2622 type32 offset;
2623
2624 if (!cflag)
2625 {
2626 /* new string */
2627 ptr = (type16)read_reg(0, 1);
2628 if (!ptr)
2629 offset = 0;
2630 else
2631 {
2632 offset = read_w(&decode_table[0x100 + 2 * ptr]);
2633 if (read_w(&decode_table[0x100]))
2634 {
2635 if (ptr >= read_w(&decode_table[0x100]))
2636 offset += string_size;
2637 }
2638 }
2639 mask = 1;
2640 }
2641 else
2642 {
2643 offset = offset_bak;
2644 mask = mask_bak;
2645 }
2646 do
2647 {
2648 c = 0;
2649 while (c < 0x80)
2650 {
2651 if (offset >= string_size)
2652 b = string2[offset - string_size];
2653 else
2654 {
2655 if (offset >= MAX_STRING_SIZE)
2656 b = string3[offset - MAX_STRING_SIZE];
2657 else
2658 b = string[offset];
2659 }
2660 if (b & mask)
2661 c = decode_table[0x80 + c];
2662 else
2663 c = decode_table[c];
2664 mask <<= 1;
2665 if (!mask)
2666 {
2667 mask = 1;
2668 offset++;
2669 }
2670 }
2671 c &= 0x7f;
2672 if (c && ((c != 0x40) || (lastchar != 0x20)))
2673 char_out(c);
2674 }
2675 while (c && ((c != 0x40) || (lastchar != 0x20)));
2676 cflag = c ? 0xff : 0;
2677 if (c)
2678 {
2679 offset_bak = offset;
2680 mask_bak = mask;
2681 }
2682 }
2683
2684 void output_number(type16 number)
2685 {
2686 type16 tens = number / 10;
2687
2688 if (tens > 0)
2689 ms_putchar('0'+tens);
2690 number -= (tens*10);
2691 ms_putchar('0'+number);
2692 }
2693
2694 type16 output_text(const type8* text)
2695 {
2696 type16 i;
2697
2698 for (i = 0; text[i] != 0; i++)
2699 ms_putchar(text[i]);
2700 return i;
2701 }
2702
2703 type16s hint_input(void)
2704 {
2705 type8 c1, c2, c3;
2706
2707 output_text(">>");
2708 ms_flush();
2709
2710 do
2711 {
2712 c1 = ms_getchar(0);
2713 }
2714 while (c1 == '\n');
2715 if (c1 == 1)
2716 return -1; /* New game loaded, abort hints */
2717
2718 c2 = ms_getchar(0);
2719 if (c2 == 1)
2720 return -1;
2721
2722 /* Get and discard any remaining characters */
2723 c3 = c2;
2724 while (c3 != '\n')
2725 {
2726 c3 = ms_getchar(0);
2727 if (c3 == 1)
2728 return -1;
2729 }
2730 ms_putchar('\n');
2731
2732 if ((c1 >= '0') && (c1 <= '9'))
2733 {
2734 type16 number = c1 - '0';
2735 if ((c2 >= '0') && (c2 <= '9'))
2736 {
2737 number *= 10;
2738 number += c2 - '0';
2739 }
2740 return number;
2741 }
2742
2743 if ((c1 >= 'A') && (c1 <= 'Z'))
2744 c1 = 'a' + (c1 - 'A');
2745 if ((c1 >= 'a') && (c1 <= 'z'))
2746 {
2747 switch (c1)
2748 {
2749 case 'e':
2750 return -2; /* End hints */
2751 case 'n':
2752 return -3; /* Next hint */
2753 case 'p':
2754 return -4; /* Show parent hint list */
2755 }
2756 }
2757 return 0;
2758 }
2759
2760 type16 show_hints_text(struct ms_hint* hints, type16 index)
2761 {
2762 type16 i = 0, j = 0;
2763 type16s input;
2764 struct ms_hint* hint = hints+index;
2765
2766 while (1)
2767 {
2768 switch (hint->nodetype)
2769 {
2770
2771 case 1: /* folders */
2772 output_text("Hint categories:\n");
2773 for (i = 0, j = 0; i < hint->elcount; i++)
2774 {
2775 output_number(i+1);
2776 output_text(". ");
2777 j += output_text(hint->content+j)+1;
2778 ms_putchar('\n');
2779 }
2780 output_text("Enter hint category number, ");
2781 if (hint->parent != 0xffff)
2782 output_text("P for the parent hint menu, ");
2783 output_text("or E to end hints.\n");
2784
2785 input = hint_input();
2786 switch (input)
2787 {
2788 case -1: /* A new game is being loaded */
2789 return 1;
2790 case -2: /* End hints */
2791 return 1;
2792 case -4: /* Show parent hint list */
2793 if (hint->parent != 0xffff)
2794 return 0;
2795 default:
2796 if ((input > 0) && (input <= hint->elcount))
2797 {
2798 if (show_hints_text(hints,hint->links[input-1]) == 1)
2799 return 1;
2800 }
2801 break;
2802 }
2803 break;
2804
2805 case 2: /* hints */
2806 if (i < hint->elcount)
2807 {
2808 output_number(i+1);
2809 output_text(". ");
2810 j += output_text(hint->content+j)+1;
2811
2812 if (i == hint->elcount-1)
2813 {
2814 output_text("\nNo more hints.\n");
2815 return 0; /* Last hint */
2816 }
2817 else
2818 {
2819 output_text("\nEnter N for the next hint, ");
2820 output_text("P for the parent hint menu, ");
2821 output_text("or E to end hints.\n");
2822 }
2823
2824 input = hint_input();
2825 switch (input)
2826 {
2827 case -1: /* A new game is being loaded */
2828 return 1;
2829 case -2: /* End hints */
2830 return 1;
2831 case -3:
2832 i++;
2833 break;
2834 case -4: /* Show parent hint list */
2835 return 0;
2836 }
2837 }
2838 else
2839 return 0;
2840 break;
2841 }
2842 }
2843 return 0;
2844 }
2845
2846 void do_line_a(void)
2847 {
2848 type8 l1c;
2849 type8s *str;
2850 type16 ptr, ptr2, tmp16, dtype;
2851 type32 a1reg, tmp32;
2852
2853 #ifdef LOGGFX
2854 /*
2855 if (((byte2-0xdd) == 4) || ((byte2-0xdd) == 13))
2856 out2("--> %d\n",byte2-0xdd);
2857 else
2858 out2("LINE A %d\n",byte2-0xdd);
2859 */
2860 #endif
2861 if ((byte2 < 0xdd) || (version < 4 && byte2 < 0xe4) || (version < 2 && byte2 < 0xed))
2862 {
2863 ms_flush(); /* flush output-buffer */
2864 rand_emu(); /* Increase game randomness */
2865 l1c = ms_getchar(1); /* 0 means UNDO */
2866 if (l1c == 1)
2867 return;
2868 if (l1c)
2869 write_reg(1, 2, l1c); /* d1=getkey() */
2870 else
2871 {
2872 if ((l1c = ms_undo()) != 0)
2873 output_text(undo_ok);
2874 else
2875 output_text(undo_fail);
2876 if (!l1c)
2877 write_reg(1, 2, '\n');
2878 }
2879 }
2880 else
2881 switch (byte2 - 0xdd)
2882 {
2883
2884 case 0: /* A0DD - Won't probably be needed at all */
2885 break;
2886
2887 case 1: /* A0DE */
2888 write_reg(1, 0, 1); /* Should remove the manual check */
2889 break;
2890
2891 case 2: /* A0DF */
2892 a1reg = (type32)read_reg(9,2);
2893 dtype = (code+a1reg+2)[0];
2894
2895 switch (dtype)
2896 {
2897 case 7: /* Picture */
2898 #ifdef LOGGFX
2899 out2("PICTURE IS %s\n", code + a1reg + 3);
2900 #endif
2901 /* gfx mode = normal, df is not called if graphics are off */
2902 ms_showpic(a1reg + 3, 2);
2903 break;
2904
2905 case 10: /* Open window commands */
2906 switch ((code+a1reg+3)[0])
2907 {
2908 case 4: /* Help/Hints */
2909 if (hints != 0)
2910 {
2911 if (ms_showhints(hints) == 0)
2912 show_hints_text(hints,0);
2913 }
2914 else
2915 output_text(no_hints);
2916 break;
2917 case 0: /* Carried items */
2918 case 1: /* Room items */
2919 case 2: /* Map */
2920 case 3: /* Compass */
2921 output_text(not_supported);
2922 break;
2923 }
2924 break;
2925
2926 case 13: /* Music */
2927 switch ((code+a1reg+3)[0])
2928 {
2929 case 0: /* stop music */
2930 ms_playmusic(0,0,0);
2931 break;
2932 default: /* play music */
2933 #ifdef LOGSND
2934 out2("MUSIC IS %s\n", code + a1reg + 3);
2935 #endif
2936 {
2937 type32 length = 0;
2938 type16 tempo = 0;
2939 type8* midi = sound_extract(code + a1reg + 3,&length,&tempo);
2940 if (midi != NULL)
2941 ms_playmusic(midi,length,tempo);
2942 }
2943 break;
2944 }
2945 break;
2946 }
2947 break;
2948
2949 case 3: /* A0E0 */
2950 /* printf("A0E0 stubbed\n"); */
2951 break;
2952
2953 case 4: /* A0E1 Read from keyboard to (A1), status in D1 (0 for ok) */
2954 ms_flush();
2955 rand_emu();
2956 tmp32 = read_reg(8 + 1, 2);
2957 str = (type8s*)effective(tmp32);
2958 tmp16 = 0;
2959 do
2960 {
2961 if (!(l1c = ms_getchar(1)))
2962 {
2963 if ((l1c = ms_undo()) != 0)
2964 output_text(undo_ok);
2965 else
2966 output_text(undo_fail);
2967 if (!l1c)
2968 {
2969 tmp16 = 0;
2970 str[tmp16++] = '\n';
2971 l1c = '\n';
2972 output_text("\n>");
2973 }
2974 else
2975 {
2976 ms_putchar('\n');
2977 return;
2978 }
2979 }
2980 else
2981 {
2982 if (l1c == 1)
2983 return;
2984 str[tmp16++] = l1c;
2985 #ifdef LOGGFX
2986 out2("%c",l1c);
2987 #endif
2988 }
2989 }
2990 while (l1c != '\n' && tmp16 < 256);
2991 write_reg(8 + 1, 2, tmp32 + tmp16 - 1);
2992 if (tmp16 != 256 && tmp16 != 1)
2993 write_reg(1, 1, 0);
2994 else
2995 write_reg(1, 1, 1);
2996 break;
2997
2998 case 5: /* A0E2 */
2999 /* printf("A0E2 stubbed\n"); */
3000 break;
3001
3002 case 6: /* A0E3 */
3003 if (read_reg(1, 2) == 0)
3004 {
3005 if ((version < 4) || (read_reg(6, 2) == 0))
3006 ms_showpic(0, 0);
3007 }
3008 /* printf("\nMoves: %u\n",read_reg(0,1)); */
3009 break;
3010
3011 case 7: /* A0E4 sp+=4, RTS */
3012 write_reg(8 + 7, 1, read_reg(8 + 7, 1) + 4);
3013 pc = pop();
3014 break;
3015
3016 case 8: /* A0E5 set z, RTS */
3017 case 9: /* A0E6 clear z, RTS */
3018 pc = pop();
3019 zflag = (byte2 == 0xe5) ? 0xff : 0;
3020 break;
3021
3022 case 10: /* A0E7 set z */
3023 zflag = 0xff;
3024 break;
3025
3026 case 11: /* A0E8 clear z */
3027 zflag = 0;
3028 break;
3029
3030 case 12: /* A0E9 [3083 - j] */
3031 ptr = (type16)read_reg(8 + 0, 1);
3032 ptr2 = (type16)read_reg(8 + 1, 1);
3033 do
3034 {
3035 l1c = dict[ptr2++];
3036 effective(ptr++)[0] = l1c;
3037 }
3038 while ((l1c & 0x80) == 0);
3039 write_reg(8 + 0, 1, ptr);
3040 write_reg(8 + 1, 1, ptr2);
3041 break;
3042
3043 case 13: /* A0EA A1=write_dictword(A1,D1=output_mode) */
3044 ptr = (type16)read_reg(8 + 1, 1);
3045 tmp32 = read_reg(3, 0);
3046 write_reg(3, 0, read_reg(1, 0));
3047 do
3048 {
3049 l1c = dict[ptr++];
3050 char_out(l1c);
3051 }
3052 while (l1c < 0x80);
3053 write_reg(8 + 1, 1, ptr);
3054 write_reg(3, 0, tmp32);
3055 break;
3056
3057 case 14: /* A0EB [3037 - j] */
3058 dict[read_reg(8 + 1, 1)] = (type8)read_reg(1, 0);
3059 break;
3060
3061 case 15: /* A0EC */
3062 write_reg(1, 0, dict[read_reg(8 + 1, 1)]);
3063 break;
3064
3065 case 16:
3066 ms_stop(); /* infinite loop A0ED */
3067 break;
3068 case 17:
3069 if (!ms_init(0, 0, 0, 0))
3070 ms_stop(); /* restart game ie. pc, sp etc. A0EE */
3071 break;
3072 case 18: /* printer A0EF */
3073 break;
3074 case 19:
3075 ms_showpic(read_reg(0, 0), (type8)read_reg(1, 0)); /* Do_picture(D0) A0F0 */
3076 break;
3077 case 20:
3078 ptr = (type16)read_reg(8 + 1, 1); /* A1=nth_string(A1,D0) A0F1 */
3079 tmp32 = read_reg(0, 1);
3080 while (tmp32-- > 0)
3081 {
3082 while (effective(ptr++)[0]);
3083 }
3084 write_reg(8 + 1, 1, ptr);
3085 break;
3086
3087 case 21: /* [2a43] A0F2 */
3088 cflag = 0;
3089 write_reg(0, 1, read_reg(2, 1));
3090 do_findprop();
3091 ptr = (type16)read_reg(8 + 0, 1);
3092 while (read_reg(2, 1) > 0)
3093 {
3094 if (read_w(effective(ptr + 12)) & 0x3fff)
3095 {
3096 cflag = 0xff;
3097 break;
3098 }
3099 if (read_reg(2, 1) == (read_reg(4, 1) & 0x7fff))
3100 {
3101 cflag = 0xff;
3102 break;
3103 }
3104 ptr -= 0x0e;
3105 write_reg(2, 1, read_reg(2, 1) - 1);
3106 }
3107 break;
3108
3109 case 22:
3110 char_out((type8)read_reg(1, 0)); /* A0F3 */
3111 break;
3112
3113 case 23: /* D7=Save_(filename A0) D1 bytes starting from A1 A0F4 */
3114 str = (version < 4) ? (type8s*)effective(read_reg(8 + 0, 1)) : 0;
3115 write_reg(7, 0, ms_save_file(str, effective(read_reg(8 + 1, 1)),
3116 (type16)read_reg(1, 1)));
3117 break;
3118
3119 case 24: /* D7=Load_(filename A0) D1 bytes starting from A1 A0F5 */
3120 str = (version < 4) ? (type8s*)effective(read_reg(8 + 0, 1)) : 0;
3121 write_reg(7, 0, ms_load_file(str, effective(read_reg(8 + 1, 1)),
3122 (type16)read_reg(1, 1)));
3123 break;
3124
3125 case 25: /* D1=Random(0..D1-1) [3748] A0F6 */
3126 l1c = (type8)read_reg(1, 0);
3127 write_reg(1, 1, rand_emu() % (l1c ? l1c : 1));
3128 break;
3129
3130 case 26: /* D0=Random(0..255) [3742] A0F7 */
3131 tmp16 = (type16)rand_emu();
3132 write_reg(0, 0, tmp16 + (tmp16 >> 8));
3133 break;
3134
3135 case 27: /* write string [D0] [2999] A0F8 */
3136 write_string();
3137 break;
3138
3139 case 28: /* Z,D0=Get_inventory_item(D0) [2a9e] A0F9 */
3140 zflag = 0;
3141 ptr = (type16)read_reg(0, 1);
3142 do
3143 {
3144 write_reg(0, 1, ptr);
3145 do
3146 {
3147 do_findprop();
3148 ptr2 = (type16)read_reg(8 + 0, 1); /* object properties */
3149 if ((effective(ptr2)[5]) & 1)
3150 break; /* is_described or so */
3151 l1c = effective(ptr2)[6]; /* some_flags */
3152 tmp16 = read_w(effective(ptr2 + 8)); /* parent_object */
3153 if (!l1c)
3154 {
3155 /* ordinary object? */
3156 if (!tmp16)
3157 zflag = 0xff; /* return if parent()=player */
3158 break; /* otherwise try next */
3159 }
3160 if (l1c & 0xcc)
3161 break; /* skip worn, bodypart, room, hidden */
3162 if (tmp16 == 0)
3163 {
3164 /* return if parent()=player? */
3165 zflag = 0xff;
3166 break;
3167 }
3168 write_reg(0, 1, tmp16); /* else look at parent() */
3169 }
3170 while (1);
3171 ptr--;
3172 }
3173 while ((!zflag) && ptr);
3174 write_reg(0, 1, ptr + 1);
3175 break;
3176
3177 case 29: /* [2b18] A0FA */
3178 ptr = (type16)read_reg(8, 1);
3179 do
3180 {
3181 if (read_reg(5, 0))
3182 {
3183 l1c = ((type32)((read_w(effective(ptr)) & 0x3fff)) == read_reg(2, 1));
3184 }
3185 else
3186 {
3187 l1c = (effective(ptr)[0] == read_reg(2, 0));
3188 }
3189 if (read_reg(3, 1) == read_reg(4, 1))
3190 {
3191 cflag = 0;
3192 write_reg(8, 1, ptr);
3193 }
3194 else
3195 {
3196 write_reg(3, 1, read_reg(3, 1) + 1);
3197 ptr += 0x0e;
3198 if (l1c)
3199 {
3200 cflag = 0xff;
3201 write_reg(8, 1, ptr);
3202 }
3203 }
3204 }
3205 while ((!l1c) && (read_reg(3, 1) != read_reg(4, 1)));
3206 break;
3207
3208 case 30: /* [2bd1] A0FB */
3209 ptr = (type16)read_reg(8 + 1, 1);
3210 do
3211 {
3212 if (dict)
3213 while (dict[ptr++] < 0x80);
3214 else
3215 while (effective(ptr++)[0] < 0x80);
3216 write_reg(2, 1, read_reg(2, 1) - 1);
3217 }
3218 while (read_reg(2, 1));
3219 write_reg(8 + 1, 1, ptr);
3220 break;
3221
3222 case 31: /* [2c3b] A0FC */
3223 ptr = (type16)read_reg(8 + 0, 1);
3224 ptr2 = (type16)read_reg(8 + 1, 1);
3225 do
3226 {
3227 if (dict)
3228 while (dict[ptr++] < 0x80);
3229 else
3230 while (effective(ptr++)[0] < 0x80);
3231 while (effective(ptr2++)[0]);
3232 write_reg(0, 1, read_reg(0, 1) - 1);
3233 }
3234 while (read_reg(0, 1));
3235 write_reg(8 + 0, 1, ptr);
3236 write_reg(8 + 1, 1, ptr2);
3237 break;
3238
3239 case 32: /* Set properties pointer from A0 [2b7b] A0FD */
3240 properties = (type16)read_reg(8 + 0, 1);
3241 if (version > 0)
3242 fl_sub = (type16)read_reg(8 + 3, 1);
3243 if (version > 1)
3244 {
3245 fl_tab = (type16)read_reg(8 + 5, 1);
3246 fl_size = (type16)read_reg(7, 1) + 1;
3247 /* A3 [routine], A5 [table] and D7 [table-size] */
3248 }
3249 if (version > 2)
3250 {
3251 fp_tab = (type16)read_reg(8 + 6, 1);
3252 fp_size = (type16)read_reg(6, 1);
3253 }
3254 break;
3255
3256 case 33: /* A0FE */
3257 do_findprop();
3258 break;
3259
3260 case 34: /* Dictionary_lookup A0FF */
3261 dict_lookup();
3262 break;
3263 }
3264 }
3265
3266 /* emulate an instruction [1b7e] */
3267
3268 type8 ms_rungame(void)
3269 {
3270 type8 l1c;
3271 type16 ptr;
3272 type32 tmp32;
3273 #ifdef LOGEMU
3274 static int stat = 0;
3275 #endif
3276
3277 if (!running)
3278 return running;
3279 if (pc == undo_pc)
3280 save_undo();
3281
3282 #ifdef LOGEMU
3283 if (pc == 0x0000)
3284 stat = 0;
3285 if (stat)
3286 {
3287 log_status();
3288 fflush(dbg_log);
3289 }
3290
3291 fprintf(dbg_log, "%.8X: ", pc);
3292 #endif
3293 i_count++;
3294 read_word();
3295 switch (byte1 >> 1)
3296 {
3297
3298 /* 00-0F */
3299 case 0x00:
3300 if (byte1 == 0x00)
3301 {
3302 if (byte2 == 0x3c || byte2 == 0x7c)
3303 {
3304 /* OR immediate to CCR (30D9) */
3305 read_word();
3306 #ifdef LOGEMU
3307 out("or_ccr #%.2x", byte2);
3308 #endif
3309 if (byte2 & 0x01)
3310 cflag = 0xff;
3311 if (byte2 & 0x02)
3312 vflag = 0xff;
3313 if (byte2 & 0x04)
3314 zflag = 0xff;
3315 if (byte2 & 0x08)
3316 nflag = 0xff;
3317 }
3318 else
3319 {
3320 /* OR [27df] */
3321 #ifdef LOGEMU
3322 out("or");
3323 #endif
3324 get_arg();
3325 do_or();
3326 }
3327 }
3328 else
3329 check_btst();
3330 break;
3331
3332 case 0x01:
3333 if (byte1 == 0x02)
3334 {
3335 if (byte2 == 0x3c || byte2 == 0x7c)
3336 {
3337 /* AND immediate to CCR */
3338 read_word();
3339 #ifdef LOGEMU
3340 out("and_ccr #%.2x", byte2);
3341 #endif
3342 if (!(byte2 & 0x01))
3343 cflag = 0;
3344 if (!(byte2 & 0x02))
3345 vflag = 0;
3346 if (!(byte2 & 0x04))
3347 zflag = 0;
3348 if (!(byte2 & 0x08))
3349 nflag = 0;
3350 }
3351 else
3352 {
3353 /* AND */
3354 #ifdef LOGEMU
3355 out("and");
3356 #endif
3357 get_arg();
3358 do_and();
3359 }
3360 }
3361 else
3362 check_btst();
3363 break;
3364
3365 case 0x02:
3366 if (byte1 == 0x04)
3367 {
3368 /* SUB */
3369 #ifdef LOGEMU
3370 out("sub");
3371 #endif
3372 get_arg();
3373 do_sub(0);
3374 }
3375 else
3376 check_btst();
3377 break;
3378
3379 case 0x03:
3380 if (byte1 == 0x06)
3381 {
3382 /* ADD */
3383 #ifdef LOGEMU
3384 out("addi");
3385 #endif
3386 get_arg();
3387 do_add(0);
3388 }
3389 else
3390 check_btst();
3391 break;
3392
3393 case 0x04:
3394 if (byte1 == 0x08)
3395 {
3396 /* bit operations (immediate) */
3397 set_info((type8)(byte2 & 0x3f));
3398 l1c = (effective(pc))[1];
3399 pc += 2;
3400 set_arg1();
3401 do_bop(byte2, l1c);
3402 }
3403 else
3404 check_btst();
3405 break;
3406
3407 case 0x05:
3408 if (byte1 == 0x0a)
3409 {
3410 if (byte2 == 0x3c || byte2 == 0x7c)
3411 {
3412 /* EOR immediate to CCR */
3413 read_word();
3414 #ifdef LOGEMU
3415 out("eor_ccr #%.2X", byte2);
3416 #endif
3417 if (byte2 & 0x01)
3418 cflag ^= 0xff;
3419 if (byte2 & 0x02)
3420 vflag ^= 0xff;
3421 if (byte2 & 0x04)
3422 zflag ^= 0xff;
3423 if (byte2 & 0x08)
3424 nflag ^= 0xff;
3425 }
3426 else
3427 {
3428 /* EOR */
3429 #ifdef LOGEMU
3430 out("eor");
3431 #endif
3432 get_arg();
3433 do_eor();
3434 }
3435 }
3436 else
3437 check_btst();
3438 break;
3439
3440 case 0x06:
3441 if (byte1 == 0x0c)
3442 {
3443 /* CMP */
3444 #ifdef LOGEMU
3445 out("cmp");
3446 #endif
3447 get_arg();
3448 do_cmp();
3449 }
3450 else
3451 check_btst();
3452 break;
3453
3454 case 0x07:
3455 check_btst();
3456 break;
3457
3458 /* 10-1F [3327] MOVE.B */
3459 case 0x08: case 0x09: case 0x0a: case 0x0b:
3460 case 0x0c: case 0x0d: case 0x0e: case 0x0f:
3461
3462 #ifdef LOGEMU
3463 out("move.b");
3464 #endif
3465 set_info((type8)(byte2 & 0x3f));
3466 set_arg1();
3467 swap_args();
3468 l1c = (byte1 >> 1 & 0x07) | (byte2 >> 3 & 0x18) | (byte1 << 5 & 0x20);
3469 set_info(l1c);
3470 set_arg1();
3471 do_move();
3472 break;
3473
3474 /* 20-2F [32d1] MOVE.L */
3475 case 0x10: case 0x11: case 0x12: case 0x13:
3476 case 0x14: case 0x15: case 0x16: case 0x17:
3477
3478 #ifdef LOGEMU
3479 out("move.l");
3480 #endif
3481 set_info((type8)((byte2 & 0x3f) | 0x80));
3482 set_arg1();
3483 swap_args();
3484 l1c = (byte1 >> 1 & 0x07) | (byte2 >> 3 & 0x18) | (byte1 << 5 & 0x20);
3485 set_info((type8)(l1c | 0x80));
3486 set_arg1();
3487 do_move();
3488 break;
3489
3490 /* 30-3F [3327] MOVE.W */
3491 case 0x18: case 0x19: case 0x1a: case 0x1b:
3492 case 0x1c: case 0x1d: case 0x1e: case 0x1f:
3493
3494 #ifdef LOGEMU
3495 out("move.w");
3496 #endif
3497 set_info((type8)((byte2 & 0x3f) | 0x40));
3498 set_arg1();
3499 swap_args();
3500 l1c = (byte1 >> 1 & 0x07) | (byte2 >> 3 & 0x18) | (byte1 << 5 & 0x20);
3501 set_info((type8)(l1c | 0x40));
3502 set_arg1();
3503 do_move();
3504 break;
3505
3506 /* 40-4F various commands */
3507
3508 case 0x20:
3509 if (byte1 == 0x40)
3510 {
3511 /* [31d5] */
3512 ms_fatal("unimplemented instructions NEGX and MOVE SR,xx");
3513 }
3514 else
3515 check_lea();
3516 break;
3517
3518 case 0x21:
3519 if (byte1 == 0x42)
3520 {
3521 /* [3188] */
3522 if ((byte2 & 0xc0) == 0xc0)
3523 {
3524 ms_fatal("unimplemented instruction MOVE CCR,xx");
3525 }
3526 else
3527 {
3528 /* CLR */
3529 #ifdef LOGEMU
3530 out("clr");
3531 #endif
3532 set_info(byte2);
3533 set_arg1();
3534 if (opsize == 0)
3535 arg1[0] = 0;
3536 if (opsize == 1)
3537 write_w(arg1, 0);
3538 if (opsize == 2)
3539 write_l(arg1, 0);
3540 nflag = cflag = 0;
3541 zflag = 0xff;
3542 }
3543 }
3544 else
3545 check_lea();
3546 break;
3547
3548 case 0x22:
3549 if (byte1 == 0x44)
3550 {
3551 /* [31a0] */
3552 if ((byte2 & 0xc0) == 0xc0)
3553 {
3554 /* MOVE to CCR */
3555 #ifdef LOGEMU
3556 out("move_ccr");
3557 #endif
3558 zflag = nflag = cflag = vflag = 0;
3559 set_info((type8)(byte2 & 0x7f));
3560 set_arg1();
3561 byte2 = arg1[1];
3562 if (byte2 & 0x01)
3563 cflag = 0xff;
3564 if (byte2 & 0x02)
3565 vflag = 0xff;
3566 if (byte2 & 0x04)
3567 zflag = 0xff;
3568 if (byte2 & 0x08)
3569 nflag = 0xff;
3570 }
3571 else
3572 {
3573 #ifdef LOGEMU
3574 out("neg");
3575 #endif
3576 set_info(byte2); /* NEG */
3577 set_arg1();
3578 cflag = 0xff;
3579 if (opsize == 0)
3580 {
3581 arg1[0] = (-arg1[0]);
3582 cflag = arg1[0] ? 0xff : 0;
3583 }
3584 if (opsize == 1)
3585 {
3586 write_w(arg1, (type16)(-1 * read_w(arg1)));
3587 cflag = read_w(arg1) ? 0xff : 0;
3588 }
3589 if (opsize == 2)
3590 {
3591 write_l(arg1, -1 * read_l(arg1));
3592 cflag = read_l(arg1) ? 0xff : 0;
3593 }
3594 vflag = 0;
3595 set_flags();
3596 }
3597 }
3598 else
3599 check_lea();
3600 break;
3601
3602 case 0x23:
3603 if (byte1 == 0x46)
3604 {
3605 if ((byte2 & 0xc0) == 0xc0)
3606 {
3607 ms_fatal("unimplemented instruction MOVE xx,SR");
3608 }
3609 else
3610 {
3611 #ifdef LOGEMU
3612 out("not");
3613 #endif
3614 set_info(byte2); /* NOT */
3615 set_arg1();
3616 tmparg[0] = tmparg[1] = tmparg[2] = tmparg[3] = 0xff;
3617 arg2 = tmparg;
3618 do_eor();
3619 }
3620 }
3621 else
3622 check_lea();
3623 break;
3624
3625 case 0x24:
3626 if (byte1 == 0x48)
3627 {
3628 if ((byte2 & 0xf8) == 0x40)
3629 {
3630 #ifdef LOGEMU
3631 out("swap");
3632 #endif
3633 opsize = 2; /* SWAP */
3634 admode = 0;
3635 regnr = byte2 & 0x07;
3636 set_arg1();
3637 tmp32 = read_w(arg1);
3638 write_w(arg1, read_w(arg1 + 2));
3639 write_w(arg1 + 2, (type16)tmp32);
3640 set_flags();
3641 }
3642 else if ((byte2 & 0xf8) == 0x80)
3643 {
3644 #ifdef LOGEMU
3645 out("ext.w");
3646 #endif
3647 opsize = 1; /* EXT.W */
3648 admode = 0;
3649 regnr = byte2 & 0x07;
3650 set_arg1();
3651 if (arg1[1] > 0x7f)
3652 arg1[0] = 0xff;
3653 else
3654 arg1[0] = 0;
3655 set_flags();
3656 }
3657 else if ((byte2 & 0xf8) == 0xc0)
3658 {
3659 #ifdef LOGEMU
3660 out("ext.l");
3661 #endif
3662 opsize = 2; /* EXT.L */
3663 admode = 0;
3664 regnr = byte2 & 0x07;
3665 set_arg1();
3666 if (read_w(arg1 + 2) > 0x7fff)
3667 write_w(arg1, 0xffff);
3668 else
3669 write_w(arg1, 0);
3670 set_flags();
3671 }
3672 else if ((byte2 & 0xc0) == 0x40)
3673 {
3674 #ifdef LOGEMU
3675 out("pea");
3676 #endif
3677 set_info((type8)((byte2 & 0x3f) | 0x80)); /* PEA */
3678 set_arg1();
3679 if (is_reversible)
3680 push(arg1i);
3681 else
3682 ms_fatal("illegal addressing mode for PEA");
3683 }
3684 else
3685 {
3686 check_movem(); /* MOVEM */
3687 }
3688 }
3689 else
3690 check_lea();
3691 break;
3692
3693 case 0x25:
3694 if (byte1 == 0x4a)
3695 {
3696 /* [3219] TST */
3697 if ((byte2 & 0xc0) == 0xc0)
3698 {
3699 ms_fatal("unimplemented instruction TAS");
3700 }
3701 else
3702 {
3703 #ifdef LOGEMU
3704 out("tst");
3705 #endif
3706 set_info(byte2);
3707 set_arg1();
3708 cflag = vflag = 0;
3709 set_flags();
3710 }
3711 }
3712 else
3713 check_lea();
3714 break;
3715
3716 case 0x26:
3717 if (byte1 == 0x4c)
3718 check_movem2(); /* [3350] MOVEM.L (Ax)+,A/Dx */
3719 else
3720 check_lea(); /* LEA */
3721 break;
3722
3723 case 0x27:
3724 if (byte1 == 0x4e)
3725 {
3726 /* [3290] */
3727 if (byte2 == 0x75)
3728 {
3729 /* RTS */
3730 #ifdef LOGEMU
3731 out("rts\n");
3732 #endif
3733 pc = pop();
3734 }
3735 else if (byte2 == 0x71)
3736 {
3737 /* NOP */
3738 #ifdef LOGEMU
3739 out("nop");
3740 #endif
3741 }
3742 else if ((byte2 & 0xc0) == 0xc0)
3743 {
3744 /* indir JMP */
3745 #ifdef LOGEMU
3746 out("jmp");
3747 #endif
3748 set_info((type8)(byte2 | 0xc0));
3749 set_arg1();
3750 if (is_reversible)
3751 pc = arg1i;
3752 else
3753 ms_fatal("illegal addressing mode for JMP");
3754 }
3755 else if ((byte2 & 0xc0) == 0x80)
3756 {
3757 #ifdef LOGEMU
3758 out("jsr");
3759 #endif
3760 set_info((type8)(byte2 | 0xc0)); /* indir JSR */
3761 set_arg1();
3762 push(pc);
3763 if (is_reversible)
3764 pc = arg1i;
3765 else
3766 ms_fatal("illegal addressing mode for JSR");
3767 }
3768 else
3769 {
3770 ms_fatal("unimplemented instructions 0x4EXX");
3771 }
3772 }
3773 else
3774 check_lea(); /* LEA */
3775 break;
3776
3777 /* 50-5F [2ed5] ADDQ/SUBQ/Scc/DBcc */
3778 case 0x28: case 0x29: case 0x2a: case 0x2b:
3779 case 0x2c: case 0x2d: case 0x2e: case 0x2f:
3780
3781 if ((byte2 & 0xc0) == 0xc0)
3782 {
3783 set_info((type8)(byte2 & 0x3f));
3784 set_arg1();
3785 if (admode == 1)
3786 {
3787 /* DBcc */
3788 #ifdef LOGEMU
3789 out("dbcc");
3790 #endif
3791 if (condition(byte1) == 0)
3792 {
3793 arg1 = (arg1 - (type8 *) areg) + (type8 *) dreg - 1;
3794 write_w(arg1, (type16)(read_w(arg1) - 1));
3795 if (read_w(arg1) != 0xffff)
3796 branch(0);
3797 else
3798 pc += 2;
3799 }
3800 else
3801 pc += 2;
3802 }
3803 else
3804 {
3805 /* Scc */
3806 #ifdef LOGEMU
3807 out("scc");
3808 #endif
3809 arg1[0] = condition(byte1) ? 0xff : 0;
3810 }
3811 }
3812 else
3813 {
3814 set_info(byte2);
3815 set_arg1();
3816 quick_flag = (admode == 1) ? 0xff : 0;
3817 l1c = byte1 >> 1 & 0x07;
3818 tmparg[0] = tmparg[1] = tmparg[2] = 0;
3819 tmparg[3] = l1c ? l1c : 8;
3820 arg2 = reg_align(tmparg, opsize);
3821 {
3822 #ifdef LOGEMU
3823 type32s outnum = 0;
3824 switch (opsize)
3825 {
3826 case 0:
3827 outnum = (type8s) arg2[0];
3828 break;
3829 case 1:
3830 outnum = (type16s) read_w(arg2);
3831 break;
3832 case 2:
3833 outnum = (type32s) read_l(arg2);
3834 break;
3835 }
3836 #endif
3837 if ((byte1 & 0x01) == 1)
3838 {
3839 #ifdef LOGEMU
3840 out("subq #%.8X", outnum);
3841 #endif
3842 do_sub(0); /* SUBQ */
3843 }
3844 else
3845 {
3846 #ifdef LOGEMU
3847 out("addq #%.8X", outnum);
3848 #endif
3849 do_add(0); /* ADDQ */
3850 }
3851 }
3852 }
3853 break;
3854
3855 /* 60-6F [26ba] Bcc */
3856
3857 case 0x30:
3858 if (byte1 == 0x61)
3859 {
3860 /* BRA, BSR */
3861 #ifdef LOGEMU
3862 out("bsr");
3863 #endif
3864 if (byte2 == 0)
3865 push(pc + 2);
3866 else
3867 push(pc);
3868 }
3869 #ifdef LOGEMU
3870 else
3871 out("bra");
3872 #endif
3873 if ((byte1 == 0x60) && (byte2 == 0xfe))
3874 {
3875 ms_flush(); /* flush stdout */
3876 ms_stop(); /* infinite loop - just exit */
3877 }
3878 branch(byte2);
3879 break;
3880
3881 case 0x31: case 0x32: case 0x33: case 0x34:
3882 case 0x35: case 0x36: case 0x37:
3883
3884 if (condition(byte1) == 0)
3885 {
3886 #ifdef LOGEMU
3887 out("beq.s");
3888 #endif
3889 if (byte2 == 0)
3890 pc += 2;
3891 }
3892 else
3893 {
3894 #ifdef LOGEMU
3895 out("bra");
3896 #endif
3897 branch(byte2);
3898 }
3899 break;
3900
3901 /* 70-7F [260a] MOVEQ */
3902 case 0x38: case 0x39: case 0x3a: case 0x3b:
3903 case 0x3c: case 0x3d: case 0x3e: case 0x3f:
3904
3905 #ifdef LOGEMU
3906 out("moveq");
3907 #endif
3908 arg1 = (type8 *) & dreg[byte1 >> 1 & 0x07];
3909 if (byte2 > 127)
3910 nflag = arg1[0] = arg1[1] = arg1[2] = 0xff;
3911 else
3912 nflag = arg1[0] = arg1[1] = arg1[2] = 0;
3913 arg1[3] = byte2;
3914 zflag = byte2 ? 0 : 0xff;
3915 break;
3916
3917 /* 80-8F [2f36] */
3918 case 0x40: case 0x41: case 0x42: case 0x43:
3919 case 0x44: case 0x45: case 0x46: case 0x47:
3920
3921 if ((byte2 & 0xc0) == 0xc0)
3922 {
3923 ms_fatal("unimplemented instructions DIVS and DIVU");
3924 }
3925 else if (((byte2 & 0xf0) == 0) && ((byte1 & 0x01) != 0))
3926 {
3927 ms_fatal("unimplemented instruction SBCD");
3928 }
3929 else
3930 {
3931 #ifdef LOGEMU
3932 out("or?");
3933 #endif
3934 set_info(byte2);
3935 set_arg1();
3936 set_arg2(1, byte1);
3937 if ((byte1 & 0x01) == 0)
3938 swap_args();
3939 do_or();
3940 }
3941 break;
3942
3943 /* 90-9F [3005] SUB */
3944 case 0x48: case 0x49: case 0x4a: case 0x4b:
3945 case 0x4c: case 0x4d: case 0x4e: case 0x4f:
3946
3947 #ifdef LOGEMU
3948 out("sub");
3949 #endif
3950 quick_flag = 0;
3951 if ((byte2 & 0xc0) == 0xc0)
3952 {
3953 if ((byte1 & 0x01) == 1)
3954 set_info((type8)(byte2 & 0xbf));
3955 else
3956 set_info((type8)(byte2 & 0x7f));
3957 set_arg1();
3958 set_arg2_nosize(0, byte1);
3959 swap_args();
3960 do_sub(1);
3961 }
3962 else
3963 {
3964 set_info(byte2);
3965 set_arg1();
3966 set_arg2(1, byte1);
3967 if ((byte1 & 0x01) == 0)
3968 swap_args();
3969 do_sub(0);
3970 }
3971 break;
3972
3973 /* A0-AF various special commands [LINE_A] */
3974
3975 case 0x50: case 0x56: case 0x57: /* [2521] */
3976 do_line_a();
3977 #ifdef LOGEMU
3978 out("LINE_A A0%.2X", byte2);
3979 #endif
3980 break;
3981
3982 case 0x51:
3983 #ifdef LOGEMU
3984 out("rts\n");
3985 #endif
3986 pc = pop(); /* RTS */
3987 break;
3988
3989 case 0x52:
3990 #ifdef LOGEMU
3991 out("bsr");
3992 #endif
3993 if (byte2 == 0)
3994 push(pc + 2); /* BSR */
3995 else
3996 push(pc);
3997 branch(byte2);
3998 break;
3999
4000 case 0x53:
4001 if ((byte2 & 0xc0) == 0xc0)
4002 {
4003 /* TST [321d] */
4004 ms_fatal("unimplemented instructions LINE_A #$6C0-#$6FF");
4005 }
4006 else
4007 {
4008 #ifdef LOGEMU
4009 out("tst");
4010 #endif
4011 set_info(byte2);
4012 set_arg1();
4013 cflag = vflag = 0;
4014 set_flags();
4015 }
4016 break;
4017
4018 case 0x54:
4019 check_movem();
4020 break;
4021
4022 case 0x55:
4023 check_movem2();
4024 break;
4025
4026 /* B0-BF [2fe4] */
4027 case 0x58: case 0x59: case 0x5a: case 0x5b:
4028 case 0x5c: case 0x5d: case 0x5e: case 0x5f:
4029
4030 if ((byte2 & 0xc0) == 0xc0)
4031 {
4032 #ifdef LOGEMU
4033 out("cmp");
4034 #endif
4035 if ((byte1 & 0x01) == 1)
4036 set_info((type8)(byte2 & 0xbf));
4037 else
4038 set_info((type8)(byte2 & 0x7f));
4039 set_arg1();
4040 set_arg2(0, byte1);
4041 swap_args();
4042 do_cmp(); /* CMP */
4043 }
4044 else
4045 {
4046 if ((byte1 & 0x01) == 0)
4047 {
4048 #ifdef LOGEMU
4049 out("cmp");
4050 #endif
4051 set_info(byte2);
4052 set_arg1();
4053 set_arg2(1, byte1);
4054 swap_args();
4055 do_cmp(); /* CMP */
4056 }
4057 else
4058 {
4059 #ifdef LOGEMU
4060 out("eor");
4061 #endif
4062 set_info(byte2);
4063 set_arg1();
4064 set_arg2(1, byte1);
4065 do_eor(); /* EOR */
4066 }
4067 }
4068 break;
4069
4070 /* C0-CF [2f52] EXG, AND */
4071 case 0x60: case 0x61: case 0x62: case 0x63:
4072 case 0x64: case 0x65: case 0x66: case 0x67:
4073
4074 if ((byte1 & 0x01) == 0)
4075 {
4076 if ((byte2 & 0xc0) == 0xc0)
4077 {
4078 ms_fatal("unimplemented instruction MULU");
4079 }
4080 else
4081 {
4082 /* AND */
4083 #ifdef LOGEMU
4084 out("and");
4085 #endif
4086 set_info(byte2);
4087 set_arg1();
4088 set_arg2(1, byte1);
4089 if ((byte1 & 0x01) == 0)
4090 swap_args();
4091 do_and();
4092 }
4093 }
4094 else
4095 {
4096 if ((byte2 & 0xf8) == 0x40)
4097 {
4098 #ifdef LOGEMU
4099 out("exg (dx)");
4100 #endif
4101 opsize = 2; /* EXG Dx,Dx */
4102 set_arg2(1, (type8)(byte2 << 1));
4103 swap_args();
4104 set_arg2(1, byte1);
4105 tmp32 = read_l(arg1);
4106 write_l(arg1, read_l(arg2));
4107 write_l(arg2, tmp32);
4108 }
4109 else if ((byte2 & 0xf8) == 0x48)
4110 {
4111 opsize = 2; /* EXG Ax,Ax */
4112 #ifdef LOGEMU
4113 out("exg (ax)");
4114 #endif
4115 set_arg2(0, (type8)(byte2 << 1));
4116 swap_args();
4117 set_arg2(0, byte1);
4118 tmp32 = read_l(arg1);
4119 write_l(arg1, read_l(arg2));
4120 write_l(arg2, tmp32);
4121 }
4122 else if ((byte2 & 0xf8) == 0x88)
4123 {
4124 opsize = 2; /* EXG Dx,Ax */
4125 #ifdef LOGEMU
4126 out("exg (dx,ax)");
4127 #endif
4128 set_arg2(0, (type8)(byte2 << 1));
4129 swap_args();
4130 set_arg2(1, byte1);
4131 tmp32 = read_l(arg1);
4132 write_l(arg1, read_l(arg2));
4133 write_l(arg2, tmp32);
4134 }
4135 else
4136 {
4137 if ((byte2 & 0xc0) == 0xc0)
4138 {
4139 ms_fatal("unimplemented instruction MULS");
4140 }
4141 else
4142 {
4143 set_info(byte2);
4144 set_arg1();
4145 set_arg2(1, byte1);
4146 if ((byte1 & 0x01) == 0)
4147 swap_args();
4148 do_and();
4149 }
4150 }
4151 }
4152 break;
4153
4154 /* D0-DF [2fc8] ADD */
4155 case 0x68: case 0x69: case 0x6a: case 0x6b:
4156 case 0x6c: case 0x6d: case 0x6e: case 0x6f:
4157
4158 #ifdef LOGEMU
4159 out("add");
4160 #endif
4161 quick_flag = 0;
4162 if ((byte2 & 0xc0) == 0xc0)
4163 {
4164 if ((byte1 & 0x01) == 1)
4165 set_info((type8)(byte2 & 0xbf));
4166 else
4167 set_info((type8)(byte2 & 0x7f));
4168 set_arg1();
4169 set_arg2_nosize(0, byte1);
4170 swap_args();
4171 do_add(1);
4172 }
4173 else
4174 {
4175 set_info(byte2);
4176 set_arg1();
4177 set_arg2(1, byte1);
4178 if ((byte1 & 0x01) == 0)
4179 swap_args();
4180 do_add(0);
4181 }
4182 break;
4183
4184 /* E0-EF [3479] LSR ASL ROR ROL */
4185 case 0x70: case 0x71: case 0x72: case 0x73:
4186 case 0x74: case 0x75: case 0x76: case 0x77:
4187
4188 #ifdef LOGEMU
4189 out("lsr,asl,ror,rol");
4190 #endif
4191 if ((byte2 & 0xc0) == 0xc0)
4192 {
4193 set_info((type8)(byte2 & 0xbf)); /* OP Dx */
4194 set_arg1();
4195 l1c = 1; /* steps=1 */
4196 byte2 = (byte1 >> 1) & 0x03;
4197 }
4198 else
4199 {
4200 set_info((type8)(byte2 & 0xc7));
4201 set_arg1();
4202 if ((byte2 & 0x20) == 0)
4203 {
4204 /* immediate */
4205 l1c = (byte1 >> 1) & 0x07;
4206 if (l1c == 0)
4207 l1c = 8;
4208 }
4209 else
4210 {
4211 l1c = (type8)read_reg(byte1 >> 1 & 0x07, 0);
4212 }
4213 byte2 = (byte2 >> 3) & 0x03;
4214 }
4215 if ((byte1 & 0x01) == 0)
4216 {
4217 /* right */
4218 while (l1c-- > 0)
4219 {
4220 if (opsize == 0)
4221 {
4222 cflag = arg1[0] & 0x01 ? 0xff : 0;
4223 arg1[0] >>= 1;
4224 if (cflag && (byte2 == 3))
4225 arg1[0] |= 0x80;
4226 }
4227 if (opsize == 1)
4228 {
4229 cflag = read_w(arg1) & 0x01 ? 0xff : 0;
4230 write_w(arg1, (type16)(read_w(arg1) >> 1));
4231 if (cflag && (byte2 == 3))
4232 write_w(arg1, (type16)(read_w(arg1) | ((type16) 1 << 15)));
4233 }
4234 if (opsize == 2)
4235 {
4236 cflag = read_l(arg1) & 0x01 ? 0xff : 0;
4237 write_l(arg1, read_l(arg1) >> 1);
4238 if (cflag && (byte2 == 3))
4239 write_l(arg1, read_l(arg1) | ((type32) 1 << 31));
4240 }
4241 }
4242 }
4243 else
4244 {
4245 /* left */
4246 while (l1c-- > 0)
4247 {
4248 if (opsize == 0)
4249 {
4250 cflag = arg1[0] & 0x80 ? 0xff : 0; /* [3527] */
4251 arg1[0] <<= 1;
4252 if (cflag && (byte2 == 3))
4253 arg1[0] |= 0x01;
4254 }
4255 if (opsize == 1)
4256 {
4257 cflag = read_w(arg1) & ((type16) 1 << 15) ? 0xff : 0;
4258 write_w(arg1, (type16)(read_w(arg1) << 1));
4259 if (cflag && (byte2 == 3))
4260 write_w(arg1, (type16)(read_w(arg1) | 0x01));
4261 }
4262 if (opsize == 2)
4263 {
4264 cflag = read_l(arg1) & ((type32) 1 << 31) ? 0xff : 0;
4265 write_l(arg1, read_l(arg1) << 1);
4266 if (cflag && (byte2 == 3))
4267 write_l(arg1, read_l(arg1) | 0x01);
4268 }
4269 }
4270 }
4271 set_flags();
4272 break;
4273
4274 /* F0-FF [24f3] LINE_F */
4275 case 0x78: case 0x79: case 0x7a: case 0x7b:
4276 case 0x7c: case 0x7d: case 0x7e: case 0x7f:
4277
4278 if (version == 0)
4279 {
4280 /* hardcoded jump */
4281 char_out(l1c = (type8)read_reg(1, 0));
4282 }
4283 else if (version == 1)
4284 {
4285 /* single programmable shortcut */
4286 push(pc);
4287 pc = fl_sub;
4288 }
4289 else
4290 {
4291 /* programmable shortcuts from table */
4292 #ifdef LOGEMU
4293 out("LINK: %.2X,%.2X", byte1, byte2);
4294 #endif
4295 ptr = (byte1 & 7) << 8 | byte2;
4296 if (ptr >= fl_size)
4297 {
4298 if ((byte1 & 8) == 0)
4299 push(pc);
4300 ptr = byte1 << 8 | byte2 | 0x0800;
4301 ptr = fl_tab + 2 * (ptr ^ 0xffff);
4302 pc = (type32) ptr + (type16s) read_w(effective(ptr));
4303 }
4304 else
4305 {
4306 push(pc);
4307 pc = fl_sub;
4308 }
4309 }
4310 break;
4311
4312 default:
4313 ms_fatal("Constants aren't and variables don't");
4314 break;
4315 }
4316 #ifdef LOGEMU
4317 fprintf(dbg_log, "\n");
4318 #endif
4319 return running;
4320 }