ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/uae_cpu/newcpu.cpp
Revision: 1.16
Committed: 2002-09-17T16:05:39Z (22 years, 2 months ago) by gbeauche
Branch: MAIN
Changes since 1.15: +62 -5 lines
Log Message:
- Changes to support 68040 -> x86 dynamic translator
- Globalize FLIGHT_RECORDER, possibly used in compiler/ sources as well

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