ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/uae_cpu/newcpu.cpp
Revision: 1.17
Committed: 2002-10-01T09:39:55Z (21 years, 9 months ago) by gbeauche
Branch: MAIN
Changes since 1.16: +9 -36 lines
Log Message:
- Optimize use of quit_program variable. This is a real boolean for B2.
- Remove unused/dead code concerning surroundings of (debugging).
- m68k_compile_execute() is generated and optimized code now.

File Contents

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