ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/uae_cpu/newcpu.cpp
Revision: 1.1.1.1 (vendor branch)
Committed: 1999-10-03T14:16:26Z (25 years, 1 month ago) by cebix
Branch: cebix
CVS Tags: release-0_7-2, snapshot-21101999, start
Changes since 1.1: +0 -0 lines
Log Message:
Imported sources

File Contents

# Content
1 /*
2 * UAE - The Un*x Amiga Emulator
3 *
4 * MC68000 emulation
5 *
6 * (c) 1995 Bernd Schmidt
7 */
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "sysdeps.h"
14
15 #include "cpu_emulation.h"
16 #include "main.h"
17 #include "emul_op.h"
18
19 extern int intlev(void); // From baisilisk_glue.cpp
20
21 #include "m68k.h"
22 #include "memory.h"
23 #include "readcpu.h"
24 #include "newcpu.h"
25 #include "compiler.h"
26
27 int quit_program = 0;
28 int debugging = 0;
29 struct flag_struct regflags;
30
31 /* Opcode of faulting instruction */
32 uae_u16 last_op_for_exception_3;
33 /* PC at fault time */
34 uaecptr last_addr_for_exception_3;
35 /* Address that generated the exception */
36 uaecptr last_fault_for_exception_3;
37
38 int areg_byteinc[] = { 1,1,1,1,1,1,1,2 };
39 int imm8_table[] = { 8,1,2,3,4,5,6,7 };
40
41 int movem_index1[256];
42 int movem_index2[256];
43 int movem_next[256];
44
45 int fpp_movem_index1[256];
46 int fpp_movem_index2[256];
47 int fpp_movem_next[256];
48
49 cpuop_func *cpufunctbl[65536];
50
51 #define COUNT_INSTRS 0
52
53 #if COUNT_INSTRS
54 static unsigned long int instrcount[65536];
55 static uae_u16 opcodenums[65536];
56
57 static int compfn (const void *el1, const void *el2)
58 {
59 return instrcount[*(const uae_u16 *)el1] < instrcount[*(const uae_u16 *)el2];
60 }
61
62 static char *icountfilename (void)
63 {
64 char *name = getenv ("INSNCOUNT");
65 if (name)
66 return name;
67 return COUNT_INSTRS == 2 ? "frequent.68k" : "insncount";
68 }
69
70 void dump_counts (void)
71 {
72 FILE *f = fopen (icountfilename (), "w");
73 unsigned long int total;
74 int i;
75
76 write_log ("Writing instruction count file...\n");
77 for (i = 0; i < 65536; i++) {
78 opcodenums[i] = i;
79 total += instrcount[i];
80 }
81 qsort (opcodenums, 65536, sizeof(uae_u16), compfn);
82
83 fprintf (f, "Total: %lu\n", total);
84 for (i=0; i < 65536; i++) {
85 unsigned long int cnt = instrcount[opcodenums[i]];
86 struct instr *dp;
87 struct mnemolookup *lookup;
88 if (!cnt)
89 break;
90 dp = table68k + opcodenums[i];
91 for (lookup = lookuptab;lookup->mnemo != dp->mnemo; lookup++)
92 ;
93 fprintf (f, "%04x: %lu %s\n", opcodenums[i], cnt, lookup->name);
94 }
95 fclose (f);
96 }
97 #else
98 void dump_counts (void)
99 {
100 }
101 #endif
102
103 int broken_in;
104
105 static __inline__ unsigned int cft_map (unsigned int f)
106 {
107 #ifndef HAVE_GET_WORD_UNSWAPPED
108 return f;
109 #else
110 return ((f >> 8) & 255) | ((f & 255) << 8);
111 #endif
112 }
113
114 static unsigned long REGPARAM2 op_illg_1 (uae_u32 opcode) REGPARAM;
115
116 static unsigned long REGPARAM2 op_illg_1 (uae_u32 opcode)
117 {
118 op_illg (cft_map (opcode));
119 return 4;
120 }
121
122 static void build_cpufunctbl (void)
123 {
124 int i;
125 unsigned long opcode;
126 int cpu_level = (FPUType ? 3 : CPUType >= 2 ? 2 : CPUType == 1 ? 1 : 0);
127 struct cputbl *tbl = (cpu_level == 3 ? op_smalltbl_0
128 : cpu_level == 2 ? op_smalltbl_1
129 : cpu_level == 1 ? op_smalltbl_2
130 : op_smalltbl_3);
131
132 for (opcode = 0; opcode < 65536; opcode++)
133 cpufunctbl[cft_map (opcode)] = op_illg_1;
134 for (i = 0; tbl[i].handler != NULL; i++) {
135 if (! tbl[i].specific)
136 cpufunctbl[cft_map (tbl[i].opcode)] = tbl[i].handler;
137 }
138 for (opcode = 0; opcode < 65536; opcode++) {
139 cpuop_func *f;
140
141 if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > cpu_level)
142 continue;
143
144 if (table68k[opcode].handler != -1) {
145 f = cpufunctbl[cft_map (table68k[opcode].handler)];
146 if (f == op_illg_1)
147 abort();
148 cpufunctbl[cft_map (opcode)] = f;
149 }
150 }
151 for (i = 0; tbl[i].handler != NULL; i++) {
152 if (tbl[i].specific)
153 cpufunctbl[cft_map (tbl[i].opcode)] = tbl[i].handler;
154 }
155 }
156
157 void init_m68k (void)
158 {
159 int i;
160
161 for (i = 0 ; i < 256 ; i++) {
162 int j;
163 for (j = 0 ; j < 8 ; j++) {
164 if (i & (1 << j)) break;
165 }
166 movem_index1[i] = j;
167 movem_index2[i] = 7-j;
168 movem_next[i] = i & (~(1 << j));
169 }
170 for (i = 0 ; i < 256 ; i++) {
171 int j;
172 for (j = 7 ; j >= 0 ; j--) {
173 if (i & (1 << j)) break;
174 }
175 fpp_movem_index1[i] = j;
176 fpp_movem_index2[i] = 7-j;
177 fpp_movem_next[i] = i & (~(1 << j));
178 }
179 #if COUNT_INSTRS
180 {
181 FILE *f = fopen (icountfilename (), "r");
182 memset (instrcount, 0, sizeof instrcount);
183 if (f) {
184 uae_u32 opcode, count, total;
185 char name[20];
186 write_log ("Reading instruction count file...\n");
187 fscanf (f, "Total: %lu\n", &total);
188 while (fscanf (f, "%lx: %lu %s\n", &opcode, &count, name) == 3) {
189 instrcount[opcode] = count;
190 }
191 fclose(f);
192 }
193 }
194 #endif
195 read_table68k ();
196 do_merges ();
197
198 build_cpufunctbl ();
199 }
200
201 struct regstruct regs, lastint_regs;
202 static struct regstruct regs_backup[16];
203 static int backup_pointer = 0;
204 static long int m68kpc_offset;
205 int lastint_no;
206
207 #define get_ibyte_1(o) get_byte(regs.pc + (regs.pc_p - regs.pc_oldp) + (o) + 1)
208 #define get_iword_1(o) get_word(regs.pc + (regs.pc_p - regs.pc_oldp) + (o))
209 #define get_ilong_1(o) get_long(regs.pc + (regs.pc_p - regs.pc_oldp) + (o))
210
211 uae_s32 ShowEA (int reg, amodes mode, wordsizes size, char *buf)
212 {
213 uae_u16 dp;
214 uae_s8 disp8;
215 uae_s16 disp16;
216 int r;
217 uae_u32 dispreg;
218 uaecptr addr;
219 uae_s32 offset = 0;
220 char buffer[80];
221
222 switch (mode){
223 case Dreg:
224 sprintf (buffer,"D%d", reg);
225 break;
226 case Areg:
227 sprintf (buffer,"A%d", reg);
228 break;
229 case Aind:
230 sprintf (buffer,"(A%d)", reg);
231 break;
232 case Aipi:
233 sprintf (buffer,"(A%d)+", reg);
234 break;
235 case Apdi:
236 sprintf (buffer,"-(A%d)", reg);
237 break;
238 case Ad16:
239 disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
240 addr = m68k_areg(regs,reg) + (uae_s16)disp16;
241 sprintf (buffer,"(A%d,$%04x) == $%08lx", reg, disp16 & 0xffff,
242 (long unsigned int)addr);
243 break;
244 case Ad8r:
245 dp = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
246 disp8 = dp & 0xFF;
247 r = (dp & 0x7000) >> 12;
248 dispreg = dp & 0x8000 ? m68k_areg(regs,r) : m68k_dreg(regs,r);
249 if (!(dp & 0x800)) dispreg = (uae_s32)(uae_s16)(dispreg);
250 dispreg <<= (dp >> 9) & 3;
251
252 if (dp & 0x100) {
253 uae_s32 outer = 0, disp = 0;
254 uae_s32 base = m68k_areg(regs,reg);
255 char name[10];
256 sprintf (name,"A%d, ",reg);
257 if (dp & 0x80) { base = 0; name[0] = 0; }
258 if (dp & 0x40) dispreg = 0;
259 if ((dp & 0x30) == 0x20) { disp = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
260 if ((dp & 0x30) == 0x30) { disp = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
261 base += disp;
262
263 if ((dp & 0x3) == 0x2) { outer = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
264 if ((dp & 0x3) == 0x3) { outer = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
265
266 if (!(dp & 4)) base += dispreg;
267 if (dp & 3) base = get_long (base);
268 if (dp & 4) base += dispreg;
269
270 addr = base + outer;
271 sprintf (buffer,"(%s%c%d.%c*%d+%ld)+%ld == $%08lx", name,
272 dp & 0x8000 ? 'A' : 'D', (int)r, dp & 0x800 ? 'L' : 'W',
273 1 << ((dp >> 9) & 3),
274 disp,outer,
275 (long unsigned int)addr);
276 } else {
277 addr = m68k_areg(regs,reg) + (uae_s32)((uae_s8)disp8) + dispreg;
278 sprintf (buffer,"(A%d, %c%d.%c*%d, $%02x) == $%08lx", reg,
279 dp & 0x8000 ? 'A' : 'D', (int)r, dp & 0x800 ? 'L' : 'W',
280 1 << ((dp >> 9) & 3), disp8,
281 (long unsigned int)addr);
282 }
283 break;
284 case PC16:
285 addr = m68k_getpc () + m68kpc_offset;
286 disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
287 addr += (uae_s16)disp16;
288 sprintf (buffer,"(PC,$%04x) == $%08lx", disp16 & 0xffff,(long unsigned int)addr);
289 break;
290 case PC8r:
291 addr = m68k_getpc () + m68kpc_offset;
292 dp = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
293 disp8 = dp & 0xFF;
294 r = (dp & 0x7000) >> 12;
295 dispreg = dp & 0x8000 ? m68k_areg(regs,r) : m68k_dreg(regs,r);
296 if (!(dp & 0x800)) dispreg = (uae_s32)(uae_s16)(dispreg);
297 dispreg <<= (dp >> 9) & 3;
298
299 if (dp & 0x100) {
300 uae_s32 outer = 0,disp = 0;
301 uae_s32 base = addr;
302 char name[10];
303 sprintf (name,"PC, ");
304 if (dp & 0x80) { base = 0; name[0] = 0; }
305 if (dp & 0x40) dispreg = 0;
306 if ((dp & 0x30) == 0x20) { disp = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
307 if ((dp & 0x30) == 0x30) { disp = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
308 base += disp;
309
310 if ((dp & 0x3) == 0x2) { outer = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
311 if ((dp & 0x3) == 0x3) { outer = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
312
313 if (!(dp & 4)) base += dispreg;
314 if (dp & 3) base = get_long (base);
315 if (dp & 4) base += dispreg;
316
317 addr = base + outer;
318 sprintf (buffer,"(%s%c%d.%c*%d+%ld)+%ld == $%08lx", name,
319 dp & 0x8000 ? 'A' : 'D', (int)r, dp & 0x800 ? 'L' : 'W',
320 1 << ((dp >> 9) & 3),
321 disp,outer,
322 (long unsigned int)addr);
323 } else {
324 addr += (uae_s32)((uae_s8)disp8) + dispreg;
325 sprintf (buffer,"(PC, %c%d.%c*%d, $%02x) == $%08lx", dp & 0x8000 ? 'A' : 'D',
326 (int)r, dp & 0x800 ? 'L' : 'W', 1 << ((dp >> 9) & 3),
327 disp8, (long unsigned int)addr);
328 }
329 break;
330 case absw:
331 sprintf (buffer,"$%08lx", (long unsigned int)(uae_s32)(uae_s16)get_iword_1 (m68kpc_offset));
332 m68kpc_offset += 2;
333 break;
334 case absl:
335 sprintf (buffer,"$%08lx", (long unsigned int)get_ilong_1 (m68kpc_offset));
336 m68kpc_offset += 4;
337 break;
338 case imm:
339 switch (size){
340 case sz_byte:
341 sprintf (buffer,"#$%02x", (unsigned int)(get_iword_1 (m68kpc_offset) & 0xff));
342 m68kpc_offset += 2;
343 break;
344 case sz_word:
345 sprintf (buffer,"#$%04x", (unsigned int)(get_iword_1 (m68kpc_offset) & 0xffff));
346 m68kpc_offset += 2;
347 break;
348 case sz_long:
349 sprintf (buffer,"#$%08lx", (long unsigned int)(get_ilong_1 (m68kpc_offset)));
350 m68kpc_offset += 4;
351 break;
352 default:
353 break;
354 }
355 break;
356 case imm0:
357 offset = (uae_s32)(uae_s8)get_iword_1 (m68kpc_offset);
358 m68kpc_offset += 2;
359 sprintf (buffer,"#$%02x", (unsigned int)(offset & 0xff));
360 break;
361 case imm1:
362 offset = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset);
363 m68kpc_offset += 2;
364 sprintf (buffer,"#$%04x", (unsigned int)(offset & 0xffff));
365 break;
366 case imm2:
367 offset = (uae_s32)get_ilong_1 (m68kpc_offset);
368 m68kpc_offset += 4;
369 sprintf (buffer,"#$%08lx", (long unsigned int)offset);
370 break;
371 case immi:
372 offset = (uae_s32)(uae_s8)(reg & 0xff);
373 sprintf (buffer,"#$%08lx", (long unsigned int)offset);
374 break;
375 default:
376 break;
377 }
378 if (buf == 0)
379 printf ("%s", buffer);
380 else
381 strcat (buf, buffer);
382 return offset;
383 }
384
385 /* The plan is that this will take over the job of exception 3 handling -
386 * the CPU emulation functions will just do a longjmp to m68k_go whenever
387 * they hit an odd address. */
388 static int verify_ea (int reg, amodes mode, wordsizes size, uae_u32 *val)
389 {
390 uae_u16 dp;
391 uae_s8 disp8;
392 uae_s16 disp16;
393 int r;
394 uae_u32 dispreg;
395 uaecptr addr;
396 uae_s32 offset = 0;
397
398 switch (mode){
399 case Dreg:
400 *val = m68k_dreg (regs, reg);
401 return 1;
402 case Areg:
403 *val = m68k_areg (regs, reg);
404 return 1;
405
406 case Aind:
407 case Aipi:
408 addr = m68k_areg (regs, reg);
409 break;
410 case Apdi:
411 addr = m68k_areg (regs, reg);
412 break;
413 case Ad16:
414 disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
415 addr = m68k_areg(regs,reg) + (uae_s16)disp16;
416 break;
417 case Ad8r:
418 addr = m68k_areg (regs, reg);
419 d8r_common:
420 dp = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
421 disp8 = dp & 0xFF;
422 r = (dp & 0x7000) >> 12;
423 dispreg = dp & 0x8000 ? m68k_areg(regs,r) : m68k_dreg(regs,r);
424 if (!(dp & 0x800)) dispreg = (uae_s32)(uae_s16)(dispreg);
425 dispreg <<= (dp >> 9) & 3;
426
427 if (dp & 0x100) {
428 uae_s32 outer = 0, disp = 0;
429 uae_s32 base = addr;
430 if (dp & 0x80) base = 0;
431 if (dp & 0x40) dispreg = 0;
432 if ((dp & 0x30) == 0x20) { disp = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
433 if ((dp & 0x30) == 0x30) { disp = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
434 base += disp;
435
436 if ((dp & 0x3) == 0x2) { outer = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
437 if ((dp & 0x3) == 0x3) { outer = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
438
439 if (!(dp & 4)) base += dispreg;
440 if (dp & 3) base = get_long (base);
441 if (dp & 4) base += dispreg;
442
443 addr = base + outer;
444 } else {
445 addr += (uae_s32)((uae_s8)disp8) + dispreg;
446 }
447 break;
448 case PC16:
449 addr = m68k_getpc () + m68kpc_offset;
450 disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
451 addr += (uae_s16)disp16;
452 break;
453 case PC8r:
454 addr = m68k_getpc () + m68kpc_offset;
455 goto d8r_common;
456 case absw:
457 addr = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset);
458 m68kpc_offset += 2;
459 break;
460 case absl:
461 addr = get_ilong_1 (m68kpc_offset);
462 m68kpc_offset += 4;
463 break;
464 case imm:
465 switch (size){
466 case sz_byte:
467 *val = get_iword_1 (m68kpc_offset) & 0xff;
468 m68kpc_offset += 2;
469 break;
470 case sz_word:
471 *val = get_iword_1 (m68kpc_offset) & 0xffff;
472 m68kpc_offset += 2;
473 break;
474 case sz_long:
475 *val = get_ilong_1 (m68kpc_offset);
476 m68kpc_offset += 4;
477 break;
478 default:
479 break;
480 }
481 return 1;
482 case imm0:
483 *val = (uae_s32)(uae_s8)get_iword_1 (m68kpc_offset);
484 m68kpc_offset += 2;
485 return 1;
486 case imm1:
487 *val = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset);
488 m68kpc_offset += 2;
489 return 1;
490 case imm2:
491 *val = get_ilong_1 (m68kpc_offset);
492 m68kpc_offset += 4;
493 return 1;
494 case immi:
495 *val = (uae_s32)(uae_s8)(reg & 0xff);
496 return 1;
497 default:
498 addr = 0;
499 break;
500 }
501 if ((addr & 1) == 0)
502 return 1;
503
504 last_addr_for_exception_3 = m68k_getpc () + m68kpc_offset;
505 last_fault_for_exception_3 = addr;
506 return 0;
507 }
508
509 uae_u32 get_disp_ea_020 (uae_u32 base, uae_u32 dp)
510 {
511 int reg = (dp >> 12) & 15;
512 uae_s32 regd = regs.regs[reg];
513 if ((dp & 0x800) == 0)
514 regd = (uae_s32)(uae_s16)regd;
515 regd <<= (dp >> 9) & 3;
516 if (dp & 0x100) {
517 uae_s32 outer = 0;
518 if (dp & 0x80) base = 0;
519 if (dp & 0x40) regd = 0;
520
521 if ((dp & 0x30) == 0x20) base += (uae_s32)(uae_s16)next_iword();
522 if ((dp & 0x30) == 0x30) base += next_ilong();
523
524 if ((dp & 0x3) == 0x2) outer = (uae_s32)(uae_s16)next_iword();
525 if ((dp & 0x3) == 0x3) outer = next_ilong();
526
527 if ((dp & 0x4) == 0) base += regd;
528 if (dp & 0x3) base = get_long (base);
529 if (dp & 0x4) base += regd;
530
531 return base + outer;
532 } else {
533 return base + (uae_s32)((uae_s8)dp) + regd;
534 }
535 }
536
537 uae_u32 get_disp_ea_000 (uae_u32 base, uae_u32 dp)
538 {
539 int reg = (dp >> 12) & 15;
540 uae_s32 regd = regs.regs[reg];
541 #if 1
542 if ((dp & 0x800) == 0)
543 regd = (uae_s32)(uae_s16)regd;
544 return base + (uae_s8)dp + regd;
545 #else
546 /* Branch-free code... benchmark this again now that
547 * things are no longer inline. */
548 uae_s32 regd16;
549 uae_u32 mask;
550 mask = ((dp & 0x800) >> 11) - 1;
551 regd16 = (uae_s32)(uae_s16)regd;
552 regd16 &= mask;
553 mask = ~mask;
554 base += (uae_s8)dp;
555 regd &= mask;
556 regd |= regd16;
557 return base + regd;
558 #endif
559 }
560
561 void MakeSR (void)
562 {
563 #if 0
564 assert((regs.t1 & 1) == regs.t1);
565 assert((regs.t0 & 1) == regs.t0);
566 assert((regs.s & 1) == regs.s);
567 assert((regs.m & 1) == regs.m);
568 assert((XFLG & 1) == XFLG);
569 assert((NFLG & 1) == NFLG);
570 assert((ZFLG & 1) == ZFLG);
571 assert((VFLG & 1) == VFLG);
572 assert((CFLG & 1) == CFLG);
573 #endif
574 regs.sr = ((regs.t1 << 15) | (regs.t0 << 14)
575 | (regs.s << 13) | (regs.m << 12) | (regs.intmask << 8)
576 | (GET_XFLG << 4) | (GET_NFLG << 3) | (GET_ZFLG << 2) | (GET_VFLG << 1)
577 | GET_CFLG);
578 }
579
580 void MakeFromSR (void)
581 {
582 int oldm = regs.m;
583 int olds = regs.s;
584
585 regs.t1 = (regs.sr >> 15) & 1;
586 regs.t0 = (regs.sr >> 14) & 1;
587 regs.s = (regs.sr >> 13) & 1;
588 regs.m = (regs.sr >> 12) & 1;
589 regs.intmask = (regs.sr >> 8) & 7;
590 SET_XFLG ((regs.sr >> 4) & 1);
591 SET_NFLG ((regs.sr >> 3) & 1);
592 SET_ZFLG ((regs.sr >> 2) & 1);
593 SET_VFLG ((regs.sr >> 1) & 1);
594 SET_CFLG (regs.sr & 1);
595 if (CPUType >= 2) {
596 if (olds != regs.s) {
597 if (olds) {
598 if (oldm)
599 regs.msp = m68k_areg(regs, 7);
600 else
601 regs.isp = m68k_areg(regs, 7);
602 m68k_areg(regs, 7) = regs.usp;
603 } else {
604 regs.usp = m68k_areg(regs, 7);
605 m68k_areg(regs, 7) = regs.m ? regs.msp : regs.isp;
606 }
607 } else if (olds && oldm != regs.m) {
608 if (oldm) {
609 regs.msp = m68k_areg(regs, 7);
610 m68k_areg(regs, 7) = regs.isp;
611 } else {
612 regs.isp = m68k_areg(regs, 7);
613 m68k_areg(regs, 7) = regs.msp;
614 }
615 }
616 } else {
617 if (olds != regs.s) {
618 if (olds) {
619 regs.isp = m68k_areg(regs, 7);
620 m68k_areg(regs, 7) = regs.usp;
621 } else {
622 regs.usp = m68k_areg(regs, 7);
623 m68k_areg(regs, 7) = regs.isp;
624 }
625 }
626 }
627
628 regs.spcflags |= SPCFLAG_INT;
629 if (regs.t1 || regs.t0)
630 regs.spcflags |= SPCFLAG_TRACE;
631 else
632 regs.spcflags &= ~(SPCFLAG_TRACE | SPCFLAG_DOTRACE);
633 }
634
635 void Exception(int nr, uaecptr oldpc)
636 {
637 compiler_flush_jsr_stack();
638 MakeSR();
639 if (!regs.s) {
640 regs.usp = m68k_areg(regs, 7);
641 if (CPUType >= 2)
642 m68k_areg(regs, 7) = regs.m ? regs.msp : regs.isp;
643 else
644 m68k_areg(regs, 7) = regs.isp;
645 regs.s = 1;
646 }
647 if (CPUType > 0) {
648 if (nr == 2 || nr == 3) {
649 int i;
650 /* @@@ this is probably wrong (?) */
651 for (i = 0 ; i < 12 ; i++) {
652 m68k_areg(regs, 7) -= 2;
653 put_word (m68k_areg(regs, 7), 0);
654 }
655 m68k_areg(regs, 7) -= 2;
656 put_word (m68k_areg(regs, 7), 0xa000 + nr * 4);
657 } else if (nr ==5 || nr == 6 || nr == 7 || nr == 9) {
658 m68k_areg(regs, 7) -= 4;
659 put_long (m68k_areg(regs, 7), oldpc);
660 m68k_areg(regs, 7) -= 2;
661 put_word (m68k_areg(regs, 7), 0x2000 + nr * 4);
662 } else if (regs.m && nr >= 24 && nr < 32) {
663 m68k_areg(regs, 7) -= 2;
664 put_word (m68k_areg(regs, 7), nr * 4);
665 m68k_areg(regs, 7) -= 4;
666 put_long (m68k_areg(regs, 7), m68k_getpc ());
667 m68k_areg(regs, 7) -= 2;
668 put_word (m68k_areg(regs, 7), regs.sr);
669 regs.sr |= (1 << 13);
670 regs.msp = m68k_areg(regs, 7);
671 m68k_areg(regs, 7) = regs.isp;
672 m68k_areg(regs, 7) -= 2;
673 put_word (m68k_areg(regs, 7), 0x1000 + nr * 4);
674 } else {
675 m68k_areg(regs, 7) -= 2;
676 put_word (m68k_areg(regs, 7), nr * 4);
677 }
678 } else {
679 if (nr == 2 || nr == 3) {
680 m68k_areg(regs, 7) -= 12;
681 /* ??????? */
682 if (nr == 3) {
683 put_long (m68k_areg(regs, 7), last_fault_for_exception_3);
684 put_word (m68k_areg(regs, 7)+4, last_op_for_exception_3);
685 put_long (m68k_areg(regs, 7)+8, last_addr_for_exception_3);
686 }
687 write_log ("Exception!\n");
688 goto kludge_me_do;
689 }
690 }
691 m68k_areg(regs, 7) -= 4;
692 put_long (m68k_areg(regs, 7), m68k_getpc ());
693 kludge_me_do:
694 m68k_areg(regs, 7) -= 2;
695 put_word (m68k_areg(regs, 7), regs.sr);
696 m68k_setpc (get_long (regs.vbr + 4*nr));
697 fill_prefetch_0 ();
698 regs.t1 = regs.t0 = regs.m = 0;
699 regs.spcflags &= ~(SPCFLAG_TRACE | SPCFLAG_DOTRACE);
700 }
701
702 static void Interrupt(int nr)
703 {
704 assert(nr < 8 && nr >= 0);
705 lastint_regs = regs;
706 lastint_no = nr;
707 Exception(nr+24, 0);
708
709 regs.intmask = nr;
710 regs.spcflags |= SPCFLAG_INT;
711 }
712
713 static int caar, cacr;
714
715 void m68k_move2c (int regno, uae_u32 *regp)
716 {
717 if (CPUType == 1 && (regno & 0x7FF) > 1)
718 op_illg (0x4E7B);
719 else
720 switch (regno) {
721 case 0: regs.sfc = *regp & 7; break;
722 case 1: regs.dfc = *regp & 7; break;
723 case 2: cacr = *regp & 0x3; break; /* ignore C and CE */
724 case 0x800: regs.usp = *regp; break;
725 case 0x801: regs.vbr = *regp; break;
726 case 0x802: caar = *regp &0xfc; break;
727 case 0x803: regs.msp = *regp; if (regs.m == 1) m68k_areg(regs, 7) = regs.msp; break;
728 case 0x804: regs.isp = *regp; if (regs.m == 0) m68k_areg(regs, 7) = regs.isp; break;
729 default:
730 op_illg (0x4E7B);
731 break;
732 }
733 }
734
735 void m68k_movec2 (int regno, uae_u32 *regp)
736 {
737 if (CPUType == 1 && (regno & 0x7FF) > 1)
738 op_illg (0x4E7A);
739 else
740 switch (regno) {
741 case 0: *regp = regs.sfc; break;
742 case 1: *regp = regs.dfc; break;
743 case 2: *regp = cacr; break;
744 case 0x800: *regp = regs.usp; break;
745 case 0x801: *regp = regs.vbr; break;
746 case 0x802: *regp = caar; break;
747 case 0x803: *regp = regs.m == 1 ? m68k_areg(regs, 7) : regs.msp; break;
748 case 0x804: *regp = regs.m == 0 ? m68k_areg(regs, 7) : regs.isp; break;
749 default:
750 op_illg (0x4E7A);
751 break;
752 }
753 }
754
755 static __inline__ int
756 div_unsigned(uae_u32 src_hi, uae_u32 src_lo, uae_u32 div, uae_u32 *quot, uae_u32 *rem)
757 {
758 uae_u32 q = 0, cbit = 0;
759 int i;
760
761 if (div <= src_hi) {
762 return 1;
763 }
764 for (i = 0 ; i < 32 ; i++) {
765 cbit = src_hi & 0x80000000ul;
766 src_hi <<= 1;
767 if (src_lo & 0x80000000ul) src_hi++;
768 src_lo <<= 1;
769 q = q << 1;
770 if (cbit || div <= src_hi) {
771 q |= 1;
772 src_hi -= div;
773 }
774 }
775 *quot = q;
776 *rem = src_hi;
777 return 0;
778 }
779
780 void m68k_divl (uae_u32 opcode, uae_u32 src, uae_u16 extra, uaecptr oldpc)
781 {
782 #if defined(uae_s64)
783 if (src == 0) {
784 Exception (5, oldpc);
785 return;
786 }
787 if (extra & 0x800) {
788 /* signed variant */
789 uae_s64 a = (uae_s64)(uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
790 uae_s64 quot, rem;
791
792 if (extra & 0x400) {
793 a &= 0xffffffffu;
794 a |= (uae_s64)m68k_dreg(regs, extra & 7) << 32;
795 }
796 rem = a % (uae_s64)(uae_s32)src;
797 quot = a / (uae_s64)(uae_s32)src;
798 if ((quot & UVAL64(0xffffffff80000000)) != 0
799 && (quot & UVAL64(0xffffffff80000000)) != UVAL64(0xffffffff80000000))
800 {
801 SET_VFLG (1);
802 SET_NFLG (1);
803 SET_CFLG (0);
804 } else {
805 if (((uae_s32)rem < 0) != ((uae_s64)a < 0)) rem = -rem;
806 SET_VFLG (0);
807 SET_CFLG (0);
808 SET_ZFLG (((uae_s32)quot) == 0);
809 SET_NFLG (((uae_s32)quot) < 0);
810 m68k_dreg(regs, extra & 7) = rem;
811 m68k_dreg(regs, (extra >> 12) & 7) = quot;
812 }
813 } else {
814 /* unsigned */
815 uae_u64 a = (uae_u64)(uae_u32)m68k_dreg(regs, (extra >> 12) & 7);
816 uae_u64 quot, rem;
817
818 if (extra & 0x400) {
819 a &= 0xffffffffu;
820 a |= (uae_u64)m68k_dreg(regs, extra & 7) << 32;
821 }
822 rem = a % (uae_u64)src;
823 quot = a / (uae_u64)src;
824 if (quot > 0xffffffffu) {
825 SET_VFLG (1);
826 SET_NFLG (1);
827 SET_CFLG (0);
828 } else {
829 SET_VFLG (0);
830 SET_CFLG (0);
831 SET_ZFLG (((uae_s32)quot) == 0);
832 SET_NFLG (((uae_s32)quot) < 0);
833 m68k_dreg(regs, extra & 7) = rem;
834 m68k_dreg(regs, (extra >> 12) & 7) = quot;
835 }
836 }
837 #else
838 if (src == 0) {
839 Exception (5, oldpc);
840 return;
841 }
842 if (extra & 0x800) {
843 /* signed variant */
844 uae_s32 lo = (uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
845 uae_s32 hi = lo < 0 ? -1 : 0;
846 uae_s32 save_high;
847 uae_u32 quot, rem;
848 uae_u32 sign;
849
850 if (extra & 0x400) {
851 hi = (uae_s32)m68k_dreg(regs, extra & 7);
852 }
853 save_high = hi;
854 sign = (hi ^ src);
855 if (hi < 0) {
856 hi = ~hi;
857 lo = -lo;
858 if (lo == 0) hi++;
859 }
860 if ((uae_s32)src < 0) src = -src;
861 if (div_unsigned(hi, lo, src, &quot, &rem) ||
862 (sign & 0x80000000) ? quot > 0x80000000 : quot > 0x7fffffff) {
863 SET_VFLG (1);
864 SET_NFLG (1);
865 SET_CFLG (0);
866 } else {
867 if (sign & 0x80000000) quot = -quot;
868 if (((uae_s32)rem < 0) != (save_high < 0)) rem = -rem;
869 SET_VFLG (0);
870 SET_CFLG (0);
871 SET_ZFLG (((uae_s32)quot) == 0);
872 SET_NFLG (((uae_s32)quot) < 0);
873 m68k_dreg(regs, extra & 7) = rem;
874 m68k_dreg(regs, (extra >> 12) & 7) = quot;
875 }
876 } else {
877 /* unsigned */
878 uae_u32 lo = (uae_u32)m68k_dreg(regs, (extra >> 12) & 7);
879 uae_u32 hi = 0;
880 uae_u32 quot, rem;
881
882 if (extra & 0x400) {
883 hi = (uae_u32)m68k_dreg(regs, extra & 7);
884 }
885 if (div_unsigned(hi, lo, src, &quot, &rem)) {
886 SET_VFLG (1);
887 SET_NFLG (1);
888 SET_CFLG (0);
889 } else {
890 SET_VFLG (0);
891 SET_CFLG (0);
892 SET_ZFLG (((uae_s32)quot) == 0);
893 SET_NFLG (((uae_s32)quot) < 0);
894 m68k_dreg(regs, extra & 7) = rem;
895 m68k_dreg(regs, (extra >> 12) & 7) = quot;
896 }
897 }
898 #endif
899 }
900
901 static __inline__ void
902 mul_unsigned(uae_u32 src1, uae_u32 src2, uae_u32 *dst_hi, uae_u32 *dst_lo)
903 {
904 uae_u32 r0 = (src1 & 0xffff) * (src2 & 0xffff);
905 uae_u32 r1 = ((src1 >> 16) & 0xffff) * (src2 & 0xffff);
906 uae_u32 r2 = (src1 & 0xffff) * ((src2 >> 16) & 0xffff);
907 uae_u32 r3 = ((src1 >> 16) & 0xffff) * ((src2 >> 16) & 0xffff);
908 uae_u32 lo;
909
910 lo = r0 + ((r1 << 16) & 0xffff0000ul);
911 if (lo < r0) r3++;
912 r0 = lo;
913 lo = r0 + ((r2 << 16) & 0xffff0000ul);
914 if (lo < r0) r3++;
915 r3 += ((r1 >> 16) & 0xffff) + ((r2 >> 16) & 0xffff);
916 *dst_lo = lo;
917 *dst_hi = r3;
918 }
919
920 void m68k_mull (uae_u32 opcode, uae_u32 src, uae_u16 extra)
921 {
922 #if defined(uae_s64)
923 if (extra & 0x800) {
924 /* signed variant */
925 uae_s64 a = (uae_s64)(uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
926
927 a *= (uae_s64)(uae_s32)src;
928 SET_VFLG (0);
929 SET_CFLG (0);
930 SET_ZFLG (a == 0);
931 SET_NFLG (a < 0);
932 if (extra & 0x400)
933 m68k_dreg(regs, extra & 7) = a >> 32;
934 else if ((a & UVAL64(0xffffffff80000000)) != 0
935 && (a & UVAL64(0xffffffff80000000)) != UVAL64(0xffffffff80000000))
936 {
937 SET_VFLG (1);
938 }
939 m68k_dreg(regs, (extra >> 12) & 7) = (uae_u32)a;
940 } else {
941 /* unsigned */
942 uae_u64 a = (uae_u64)(uae_u32)m68k_dreg(regs, (extra >> 12) & 7);
943
944 a *= (uae_u64)src;
945 SET_VFLG (0);
946 SET_CFLG (0);
947 SET_ZFLG (a == 0);
948 SET_NFLG (((uae_s64)a) < 0);
949 if (extra & 0x400)
950 m68k_dreg(regs, extra & 7) = a >> 32;
951 else if ((a & UVAL64(0xffffffff00000000)) != 0) {
952 SET_VFLG (1);
953 }
954 m68k_dreg(regs, (extra >> 12) & 7) = (uae_u32)a;
955 }
956 #else
957 if (extra & 0x800) {
958 /* signed variant */
959 uae_s32 src1,src2;
960 uae_u32 dst_lo,dst_hi;
961 uae_u32 sign;
962
963 src1 = (uae_s32)src;
964 src2 = (uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
965 sign = (src1 ^ src2);
966 if (src1 < 0) src1 = -src1;
967 if (src2 < 0) src2 = -src2;
968 mul_unsigned((uae_u32)src1,(uae_u32)src2,&dst_hi,&dst_lo);
969 if (sign & 0x80000000) {
970 dst_hi = ~dst_hi;
971 dst_lo = -dst_lo;
972 if (dst_lo == 0) dst_hi++;
973 }
974 SET_VFLG (0);
975 SET_CFLG (0);
976 SET_ZFLG (dst_hi == 0 && dst_lo == 0);
977 SET_NFLG (((uae_s32)dst_hi) < 0);
978 if (extra & 0x400)
979 m68k_dreg(regs, extra & 7) = dst_hi;
980 else if ((dst_hi != 0 || (dst_lo & 0x80000000) != 0)
981 && ((dst_hi & 0xffffffff) != 0xffffffff
982 || (dst_lo & 0x80000000) != 0x80000000))
983 {
984 SET_VFLG (1);
985 }
986 m68k_dreg(regs, (extra >> 12) & 7) = dst_lo;
987 } else {
988 /* unsigned */
989 uae_u32 dst_lo,dst_hi;
990
991 mul_unsigned(src,(uae_u32)m68k_dreg(regs, (extra >> 12) & 7),&dst_hi,&dst_lo);
992
993 SET_VFLG (0);
994 SET_CFLG (0);
995 SET_ZFLG (dst_hi == 0 && dst_lo == 0);
996 SET_NFLG (((uae_s32)dst_hi) < 0);
997 if (extra & 0x400)
998 m68k_dreg(regs, extra & 7) = dst_hi;
999 else if (dst_hi != 0) {
1000 SET_VFLG (1);
1001 }
1002 m68k_dreg(regs, (extra >> 12) & 7) = dst_lo;
1003 }
1004 #endif
1005 }
1006 static char* ccnames[] =
1007 { "T ","F ","HI","LS","CC","CS","NE","EQ",
1008 "VC","VS","PL","MI","GE","LT","GT","LE" };
1009
1010 void m68k_reset (void)
1011 {
1012 m68k_areg (regs, 7) = 0x2000;
1013 m68k_setpc (ROMBaseMac + 0x2a);
1014 fill_prefetch_0 ();
1015 regs.kick_mask = 0xF80000;
1016 regs.s = 1;
1017 regs.m = 0;
1018 regs.stopped = 0;
1019 regs.t1 = 0;
1020 regs.t0 = 0;
1021 SET_ZFLG (0);
1022 SET_XFLG (0);
1023 SET_CFLG (0);
1024 SET_VFLG (0);
1025 SET_NFLG (0);
1026 regs.spcflags = 0;
1027 regs.intmask = 7;
1028 regs.vbr = regs.sfc = regs.dfc = 0;
1029 regs.fpcr = regs.fpsr = regs.fpiar = 0;
1030 }
1031
1032 unsigned long REGPARAM2 op_illg (uae_u32 opcode)
1033 {
1034 uaecptr pc = m68k_getpc ();
1035
1036 compiler_flush_jsr_stack ();
1037
1038 if ((opcode & 0xFF00) == 0x7100) {
1039 struct M68kRegisters r;
1040 int i;
1041
1042 // Return from Execute68k()?
1043 if (opcode == M68K_EXEC_RETURN) {
1044 regs.spcflags |= SPCFLAG_BRK;
1045 quit_program = 1;
1046 return 4;
1047 }
1048
1049 // Call EMUL_OP opcode
1050 for (i=0; i<8; i++) {
1051 r.d[i] = m68k_dreg(regs, i);
1052 r.a[i] = m68k_areg(regs, i);
1053 }
1054 MakeSR();
1055 r.sr = regs.sr;
1056 EmulOp(opcode, &r);
1057 for (i=0; i<8; i++) {
1058 m68k_dreg(regs, i) = r.d[i];
1059 m68k_areg(regs, i) = r.a[i];
1060 }
1061 regs.sr = r.sr;
1062 MakeFromSR();
1063 m68k_incpc(2);
1064 fill_prefetch_0 ();
1065 return 4;
1066 }
1067
1068 if ((opcode & 0xF000) == 0xA000) {
1069 Exception(0xA,0);
1070 return 4;
1071 }
1072
1073 if ((opcode & 0xF000) == 0xF000) {
1074 Exception(0xB,0);
1075 return 4;
1076 }
1077
1078 write_log ("Illegal instruction: %04x at %08lx\n", opcode, pc);
1079 Exception (4,0);
1080 return 4;
1081 }
1082
1083 void mmu_op(uae_u32 opcode, uae_u16 extra)
1084 {
1085 if ((extra & 0xB000) == 0) { /* PMOVE instruction */
1086
1087 } else if ((extra & 0xF000) == 0x2000) { /* PLOAD instruction */
1088 } else if ((extra & 0xF000) == 0x8000) { /* PTEST instruction */
1089 } else
1090 op_illg (opcode);
1091 }
1092
1093 static int n_insns = 0, n_spcinsns = 0;
1094
1095 static uaecptr last_trace_ad = 0;
1096
1097 static void do_trace (void)
1098 {
1099 if (regs.t0) {
1100 uae_u16 opcode;
1101 /* should also include TRAP, CHK, SR modification FPcc */
1102 /* probably never used so why bother */
1103 /* We can afford this to be inefficient... */
1104 m68k_setpc (m68k_getpc ());
1105 fill_prefetch_0 ();
1106 opcode = get_word (regs.pc);
1107 if (opcode == 0x4e72 /* RTE */
1108 || opcode == 0x4e74 /* RTD */
1109 || opcode == 0x4e75 /* RTS */
1110 || opcode == 0x4e77 /* RTR */
1111 || opcode == 0x4e76 /* TRAPV */
1112 || (opcode & 0xffc0) == 0x4e80 /* JSR */
1113 || (opcode & 0xffc0) == 0x4ec0 /* JMP */
1114 || (opcode & 0xff00) == 0x6100 /* BSR */
1115 || ((opcode & 0xf000) == 0x6000 /* Bcc */
1116 && cctrue((opcode >> 8) & 0xf))
1117 || ((opcode & 0xf0f0) == 0x5050 /* DBcc */
1118 && !cctrue((opcode >> 8) & 0xf)
1119 && (uae_s16)m68k_dreg(regs, opcode & 7) != 0))
1120 {
1121 last_trace_ad = m68k_getpc ();
1122 regs.spcflags &= ~SPCFLAG_TRACE;
1123 regs.spcflags |= SPCFLAG_DOTRACE;
1124 }
1125 } else if (regs.t1) {
1126 last_trace_ad = m68k_getpc ();
1127 regs.spcflags &= ~SPCFLAG_TRACE;
1128 regs.spcflags |= SPCFLAG_DOTRACE;
1129 }
1130 }
1131
1132
1133 static int do_specialties (void)
1134 {
1135 /*n_spcinsns++;*/
1136 run_compiled_code();
1137 if (regs.spcflags & SPCFLAG_DOTRACE) {
1138 Exception (9,last_trace_ad);
1139 }
1140 while (regs.spcflags & SPCFLAG_STOP) {
1141 if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)){
1142 int intr = intlev ();
1143 regs.spcflags &= ~(SPCFLAG_INT | SPCFLAG_DOINT);
1144 if (intr != -1 && intr > regs.intmask) {
1145 Interrupt (intr);
1146 regs.stopped = 0;
1147 regs.spcflags &= ~SPCFLAG_STOP;
1148 }
1149 }
1150 }
1151 if (regs.spcflags & SPCFLAG_TRACE)
1152 do_trace ();
1153
1154 if (regs.spcflags & SPCFLAG_DOINT) {
1155 int intr = intlev ();
1156 regs.spcflags &= ~SPCFLAG_DOINT;
1157 if (intr != -1 && intr > regs.intmask) {
1158 Interrupt (intr);
1159 regs.stopped = 0;
1160 }
1161 }
1162 if (regs.spcflags & SPCFLAG_INT) {
1163 regs.spcflags &= ~SPCFLAG_INT;
1164 regs.spcflags |= SPCFLAG_DOINT;
1165 }
1166 if (regs.spcflags & (SPCFLAG_BRK | SPCFLAG_MODE_CHANGE)) {
1167 regs.spcflags &= ~(SPCFLAG_BRK | SPCFLAG_MODE_CHANGE);
1168 return 1;
1169 }
1170 return 0;
1171 }
1172
1173 static void m68k_run_1 (void)
1174 {
1175 for (;;) {
1176 int cycles;
1177 uae_u32 opcode = GET_OPCODE;
1178 #if 0
1179 if (get_ilong (0) != do_get_mem_long (&regs.prefetch)) {
1180 debugging = 1;
1181 return;
1182 }
1183 #endif
1184 /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */
1185 /* regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/
1186 #if COUNT_INSTRS == 2
1187 if (table68k[cft_map (opcode)].handler != -1)
1188 instrcount[table68k[cft_map (opcode)].handler]++;
1189 #elif COUNT_INSTRS == 1
1190 instrcount[opcode]++;
1191 #endif
1192 #if defined(X86_ASSEMBLYxxx)
1193 __asm__ __volatile__("\tcall *%%ebx"
1194 : "=&a" (cycles) : "b" (cpufunctbl[opcode]), "0" (opcode)
1195 : "%edx", "%ecx",
1196 "%esi", "%edi", "%ebp", "memory", "cc");
1197 #else
1198 cycles = (*cpufunctbl[opcode])(opcode);
1199 #endif
1200 /*n_insns++;*/
1201 if (regs.spcflags) {
1202 if (do_specialties ())
1203 return;
1204 }
1205 }
1206 }
1207
1208 #ifdef X86_ASSEMBLYxxx
1209 static __inline__ void m68k_run1 (void)
1210 {
1211 /* Work around compiler bug: GCC doesn't push %ebp in m68k_run_1. */
1212 __asm__ __volatile__ ("pushl %%ebp\n\tcall *%0\n\tpopl %%ebp" : : "r" (m68k_run_1) : "%eax", "%edx", "%ecx", "memory", "cc");
1213 }
1214 #else
1215 #define m68k_run1 m68k_run_1
1216 #endif
1217
1218 int in_m68k_go = 0;
1219
1220 void m68k_go (int may_quit)
1221 {
1222 // m68k_go() must be reentrant for Execute68k() and Execute68kTrap() to work
1223 /*
1224 if (in_m68k_go || !may_quit) {
1225 write_log("Bug! m68k_go is not reentrant.\n");
1226 abort();
1227 }
1228 */
1229 in_m68k_go++;
1230 for (;;) {
1231 if (quit_program > 0) {
1232 if (quit_program == 1)
1233 break;
1234 quit_program = 0;
1235 m68k_reset ();
1236 }
1237 m68k_run1();
1238 }
1239 if (debugging) {
1240 uaecptr nextpc;
1241 m68k_dumpstate(&nextpc);
1242 exit(1);
1243 }
1244 in_m68k_go--;
1245 }
1246
1247 static void m68k_verify (uaecptr addr, uaecptr *nextpc)
1248 {
1249 uae_u32 opcode, val;
1250 struct instr *dp;
1251
1252 opcode = get_iword_1(0);
1253 last_op_for_exception_3 = opcode;
1254 m68kpc_offset = 2;
1255
1256 if (cpufunctbl[cft_map (opcode)] == op_illg_1) {
1257 opcode = 0x4AFC;
1258 }
1259 dp = table68k + opcode;
1260
1261 if (dp->suse) {
1262 if (!verify_ea (dp->sreg, (amodes)dp->smode, (wordsizes)dp->size, &val)) {
1263 Exception (3, 0);
1264 return;
1265 }
1266 }
1267 if (dp->duse) {
1268 if (!verify_ea (dp->dreg, (amodes)dp->dmode, (wordsizes)dp->size, &val)) {
1269 Exception (3, 0);
1270 return;
1271 }
1272 }
1273 }
1274
1275 void m68k_disasm (uaecptr addr, uaecptr *nextpc, int cnt)
1276 {
1277 uaecptr newpc = 0;
1278 m68kpc_offset = addr - m68k_getpc ();
1279 while (cnt-- > 0) {
1280 char instrname[20],*ccpt;
1281 int opwords;
1282 uae_u32 opcode;
1283 struct mnemolookup *lookup;
1284 struct instr *dp;
1285 printf ("%08lx: ", m68k_getpc () + m68kpc_offset);
1286 for (opwords = 0; opwords < 5; opwords++){
1287 printf ("%04x ", get_iword_1 (m68kpc_offset + opwords*2));
1288 }
1289 opcode = get_iword_1 (m68kpc_offset);
1290 m68kpc_offset += 2;
1291 if (cpufunctbl[cft_map (opcode)] == op_illg_1) {
1292 opcode = 0x4AFC;
1293 }
1294 dp = table68k + opcode;
1295 for (lookup = lookuptab;lookup->mnemo != dp->mnemo; lookup++)
1296 ;
1297
1298 strcpy (instrname, lookup->name);
1299 ccpt = strstr (instrname, "cc");
1300 if (ccpt != 0) {
1301 strncpy (ccpt, ccnames[dp->cc], 2);
1302 }
1303 printf ("%s", instrname);
1304 switch (dp->size){
1305 case sz_byte: printf (".B "); break;
1306 case sz_word: printf (".W "); break;
1307 case sz_long: printf (".L "); break;
1308 default: printf (" "); break;
1309 }
1310
1311 if (dp->suse) {
1312 newpc = m68k_getpc () + m68kpc_offset;
1313 newpc += ShowEA (dp->sreg, (amodes)dp->smode, (wordsizes)dp->size, 0);
1314 }
1315 if (dp->suse && dp->duse)
1316 printf (",");
1317 if (dp->duse) {
1318 newpc = m68k_getpc () + m68kpc_offset;
1319 newpc += ShowEA (dp->dreg, (amodes)dp->dmode, (wordsizes)dp->size, 0);
1320 }
1321 if (ccpt != 0) {
1322 if (cctrue(dp->cc))
1323 printf (" == %08lx (TRUE)", newpc);
1324 else
1325 printf (" == %08lx (FALSE)", newpc);
1326 } else if ((opcode & 0xff00) == 0x6100) /* BSR */
1327 printf (" == %08lx", newpc);
1328 printf ("\n");
1329 }
1330 if (nextpc)
1331 *nextpc = m68k_getpc () + m68kpc_offset;
1332 }
1333
1334 void m68k_dumpstate (uaecptr *nextpc)
1335 {
1336 int i;
1337 for (i = 0; i < 8; i++){
1338 printf ("D%d: %08lx ", i, m68k_dreg(regs, i));
1339 if ((i & 3) == 3) printf ("\n");
1340 }
1341 for (i = 0; i < 8; i++){
1342 printf ("A%d: %08lx ", i, m68k_areg(regs, i));
1343 if ((i & 3) == 3) printf ("\n");
1344 }
1345 if (regs.s == 0) regs.usp = m68k_areg(regs, 7);
1346 if (regs.s && regs.m) regs.msp = m68k_areg(regs, 7);
1347 if (regs.s && regs.m == 0) regs.isp = m68k_areg(regs, 7);
1348 printf ("USP=%08lx ISP=%08lx MSP=%08lx VBR=%08lx\n",
1349 regs.usp,regs.isp,regs.msp,regs.vbr);
1350 printf ("T=%d%d S=%d M=%d X=%d N=%d Z=%d V=%d C=%d IMASK=%d\n",
1351 regs.t1, regs.t0, regs.s, regs.m,
1352 GET_XFLG, GET_NFLG, GET_ZFLG, GET_VFLG, GET_CFLG, regs.intmask);
1353 for (i = 0; i < 8; i++){
1354 printf ("FP%d: %g ", i, regs.fp[i]);
1355 if ((i & 3) == 3) printf ("\n");
1356 }
1357 printf ("N=%d Z=%d I=%d NAN=%d\n",
1358 (regs.fpsr & 0x8000000) != 0,
1359 (regs.fpsr & 0x4000000) != 0,
1360 (regs.fpsr & 0x2000000) != 0,
1361 (regs.fpsr & 0x1000000) != 0);
1362
1363 m68k_disasm(m68k_getpc (), nextpc, 1);
1364 if (nextpc)
1365 printf ("next PC: %08lx\n", *nextpc);
1366 }