ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/uae_cpu/newcpu.cpp
Revision: 1.13
Committed: 2002-09-01T15:17:13Z (21 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.12: +66 -78 lines
Log Message:
- Merge with Basilisk II/JIT cpu core, interpretive part for now
- Clean use of USE_PREFETCH_BUFFER macro and dependent bits

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