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