1 /*
2  * gencpu.c - m68k emulation generator
3  *
4  * Copyright (c) 2009 ARAnyM dev team (see AUTHORS)
5  *
6  * Inspired by Christian Bauer's Basilisk II
7  *
8  * This file is part of the ARAnyM project which builds a new and powerful
9  * TOS/FreeMiNT compatible virtual machine running on almost any hardware.
10  *
11  * ARAnyM is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * ARAnyM is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with ARAnyM; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25 /*
26  * UAE - The Un*x Amiga Emulator
27  *
28  * MC68000 emulation generator
29  *
30  * This is a fairly stupid program that generates a lot of case labels that
31  * can be #included in a switch statement.
32  * As an alternative, it can generate functions that handle specific
33  * MC68000 instructions, plus a prototype header file and a function pointer
34  * array to look up the function for an opcode.
35  * Error checking is bad, an illegal table68k file will cause the program to
36  * call abort().
37  * The generated code is sometimes sub-optimal, an optimizing compiler should
38  * take care of this.
39  *
40  * Copyright 1995, 1996 Bernd Schmidt
41  */
42 
43 #define CC_FOR_BUILD 1
44 
45 #include "sysdeps.h"
46 #include "readcpu.h"
47 
48 #include <ctype.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <assert.h>
53 #undef abort
54 
55 #define BOOL_TYPE "int"
56 #define VERIFY_MMU_GENAMODE	0
57 
58 static FILE *headerfile;
59 static FILE *stblfile;
60 static FILE *functblfile;
61 
62 static int using_prefetch;
63 static int using_exception_3;
64 static int cpu_level;
65 
66 /* For the current opcode, the next lower level that will have different code.
67  * Initialized to -1 for each opcode. If it remains unchanged, indicates we
68  * are done with that opcode.  */
69 static int next_cpu_level;
70 
71 static int *opcode_map;
72 static int *opcode_next_clev;
73 static int *opcode_last_postfix;
74 static unsigned long *counts;
75 
76 #define GENA_GETV_NO_FETCH	0
77 #define GENA_GETV_FETCH		1
78 #define GENA_GETV_FETCH_ALIGN 2
79 #define GENA_MOVEM_DO_INC	0
80 #define GENA_MOVEM_NO_INC	1
81 #define GENA_MOVEM_MOVE16	2
82 
83 #define XLATE_LOG	0
84 #define XLATE_PHYS	1
85 #define XLATE_SFC	2
86 #define XLATE_DFC	3
87 static char * mem_prefix[4] = { "", "phys_", "sfc_", "dfc_" };
88 
89 /* Define the minimal 680x0 where NV flags are not affected by xBCD instructions.  */
90 #define xBCD_KEEPS_N_FLAG 4
91 #define xBCD_KEEPS_V_FLAG 3
92 
read_counts(void)93 static void read_counts (void)
94 {
95     FILE *file;
96     unsigned long opcode, count, total;
97     char name[20];
98     int nr = 0;
99     memset (counts, 0, 65536 * sizeof *counts);
100 
101     file = fopen ("frequent.68k", "r");
102     if (file) {
103 	int c = fscanf (file, "Total: %lu\n", &total);
104 	assert(c == 1);
105 	while (fscanf (file, "%lx: %lu %s\n", &opcode, &count, name) == 3) {
106 	    opcode_next_clev[nr] = 4;
107 	    opcode_last_postfix[nr] = -1;
108 	    opcode_map[nr++] = opcode;
109 	    counts[opcode] = count;
110 	}
111 	fclose (file);
112     }
113     if (nr == nr_cpuop_funcs)
114 	return;
115     for (opcode = 0; opcode < 0x10000; opcode++) {
116 	if (table68k[opcode].handler == -1 && table68k[opcode].mnemo != i_ILLG
117 	    && counts[opcode] == 0)
118 	{
119 	    opcode_next_clev[nr] = 4;
120 	    opcode_last_postfix[nr] = -1;
121 	    opcode_map[nr++] = opcode;
122 	    counts[opcode] = count;
123 	}
124     }
125     if (nr != nr_cpuop_funcs)
126 	abort ();
127 }
128 
129 static char endlabelstr[80];
130 static int endlabelno = 0;
131 static int need_endlabel;
132 
133 static int n_braces = 0;
134 static int m68k_pc_offset = 0;
135 
start_brace(void)136 static void start_brace (void)
137 {
138     n_braces++;
139     printf ("{");
140 }
141 
close_brace(void)142 static void close_brace (void)
143 {
144     assert (n_braces > 0);
145     n_braces--;
146     printf ("}");
147 }
148 
finish_braces(void)149 static void finish_braces (void)
150 {
151     while (n_braces > 0)
152 	close_brace ();
153 }
154 
pop_braces(int to)155 static void pop_braces (int to)
156 {
157     while (n_braces > to)
158 	close_brace ();
159 }
160 
bit_size(int size)161 static int bit_size (int size)
162 {
163     switch (size) {
164      case sz_byte: return 8;
165      case sz_word: return 16;
166      case sz_long: return 32;
167      default: abort ();
168     }
169     return 0;
170 }
171 
bit_mask(int size)172 static const char *bit_mask (int size)
173 {
174     switch (size) {
175      case sz_byte: return "0xff";
176      case sz_word: return "0xffff";
177      case sz_long: return "0xffffffff";
178      default: abort ();
179     }
180     return 0;
181 }
182 
gen_nextilong(void)183 static const char *gen_nextilong (void)
184 {
185     static char buffer[80];
186     int r = m68k_pc_offset;
187 
188     m68k_pc_offset += 4;
189 
190     if (using_prefetch)
191 	sprintf (buffer, "get_ilong_prefetch(%d)", r);
192     else
193 	sprintf (buffer, "get_ilong(%d)", r);
194     return buffer;
195 }
196 
gen_nextiword(void)197 static const char *gen_nextiword (void)
198 {
199     static char buffer[80];
200     int r = m68k_pc_offset;
201 
202     m68k_pc_offset += 2;
203 
204     if (using_prefetch)
205 	sprintf (buffer, "get_iword_prefetch(%d)", r);
206     else
207 	sprintf (buffer, "get_iword(%d)", r);
208     return buffer;
209 }
210 
gen_nextibyte(void)211 static const char *gen_nextibyte (void)
212 {
213     static char buffer[80];
214     int r = m68k_pc_offset;
215     m68k_pc_offset += 2;
216 
217     if (using_prefetch)
218 	sprintf (buffer, "get_ibyte_prefetch(%d)", r);
219     else
220 	sprintf (buffer, "get_ibyte(%d)", r);
221     return buffer;
222 }
223 
fill_prefetch_0(void)224 static void fill_prefetch_0 (void)
225 {
226     if (using_prefetch)
227 	printf ("fill_prefetch_0 ();\n");
228 }
229 
fill_prefetch_2(void)230 static void fill_prefetch_2 (void)
231 {
232     if (using_prefetch)
233 	printf ("fill_prefetch_2 ();\n");
234 }
235 
swap_opcode(void)236 static void swap_opcode (void)
237 {
238   printf("#if defined(HAVE_GET_WORD_UNSWAPPED) && !defined(FULLMMU)\n");
239   printf ("\topcode = do_byteswap_16(opcode);\n");
240   printf("#endif\n");
241 }
242 
real_opcode(int * have)243 static void real_opcode (int *have)
244 {
245 	if (!*have)
246 	{
247 		printf("#if defined(HAVE_GET_WORD_UNSWAPPED) && !defined(FULLMMU)\n");
248 		printf ("\tuae_u32 real_opcode = do_byteswap_16(opcode);\n");
249 		printf("#else\n");
250 		printf ("\tuae_u32 real_opcode = opcode;\n");
251 		printf("#endif\n");
252 		*have = 1;
253 	}
254 }
255 
sync_m68k_pc(void)256 static void sync_m68k_pc (void)
257 {
258     if (m68k_pc_offset == 0)
259 	return;
260     printf ("m68k_incpc(%d);\n", m68k_pc_offset);
261     switch (m68k_pc_offset) {
262      case 0:
263 	/*fprintf (stderr, "refilling prefetch at 0\n"); */
264 	break;
265      case 2:
266 	fill_prefetch_2 ();
267 	break;
268      default:
269 	fill_prefetch_0 ();
270 	break;
271     }
272     m68k_pc_offset = 0;
273 }
274 
gen_set_fault_pc(void)275 static void gen_set_fault_pc (void)
276 {
277     sync_m68k_pc();
278     printf ("regs.fault_pc = m68k_getpc ();\n");
279     m68k_pc_offset = 0;
280 }
281 
282 /* getv == 1: fetch data; getv != 0: check for odd address. If movem != 0,
283  * the calling routine handles Apdi and Aipi modes.
284  * gb-- movem == 2 means the same thing but for a MOVE16 instruction */
285 
286 /* fixup indicates if we want to fix up adress registers in pre decrement
287  * or post increment mode now (0) or later (1). A value of 2 will then be
288  * used to do the actual fix up. This allows to do all memory readings
289  * before any register is modified, and so to rerun operation without
290  * side effect in case a bus fault is generated by any memory access.
291  * XJ - 2006/11/13 */
genamode2(amodes mode,char * reg,wordsizes size,char * name,int getv,int movem,int xlateflag,int fixup)292 static void genamode2 (amodes mode, char *reg, wordsizes size, char *name, int getv, int movem, int xlateflag, int fixup)
293 {
294     if (fixup != 2)
295     {
296     start_brace ();
297     switch (mode) {
298      case Dreg:
299 	if (movem)
300 	    abort ();
301 	if (getv == GENA_GETV_FETCH)
302 	    switch (size) {
303 	     case sz_byte:
304 		printf("\n#if defined(AMIGA) && !defined(WARPUP)\n");
305 		/* sam: I don't know why gcc.2.7.2.1 produces a code worse */
306 		/* if it is not done like that: */
307 		printf ("\tuae_s8 %s = ((uae_u8*)&m68k_dreg(regs, %s))[3];\n", name, reg);
308 		printf("#else\n");
309 		printf ("\tuae_s8 %s = m68k_dreg(regs, %s);\n", name, reg);
310 		printf("#endif\n");
311 		break;
312 	     case sz_word:
313 		printf("\n#if defined(AMIGA) && !defined(WARPUP)\n");
314 		printf ("\tuae_s16 %s = ((uae_s16*)&m68k_dreg(regs, %s))[1];\n", name, reg);
315 		printf("#else\n");
316 		printf ("\tuae_s16 %s = m68k_dreg(regs, %s);\n", name, reg);
317 		printf("#endif\n");
318 		break;
319 	     case sz_long:
320 		printf ("\tuae_s32 %s = m68k_dreg(regs, %s);\n", name, reg);
321 		break;
322 	     default:
323 		abort ();
324 	    }
325 	return;
326      case Areg:
327 	if (movem)
328 	    abort ();
329 	if (getv == GENA_GETV_FETCH)
330 	    switch (size) {
331 	     case sz_word:
332 		printf ("\tuae_s16 %s = m68k_areg(regs, %s);\n", name, reg);
333 		break;
334 	     case sz_long:
335 		printf ("\tuae_s32 %s = m68k_areg(regs, %s);\n", name, reg);
336 		break;
337 	     default:
338 		abort ();
339 	    }
340 	return;
341      case Aind:
342 	printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
343 	break;
344      case Aipi:
345 	printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
346 	break;
347      case Apdi:
348 	switch (size) {
349 	 case sz_byte:
350 	    if (movem)
351 		printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
352 	    else
353 		printf ("\tuaecptr %sa = m68k_areg(regs, %s) - areg_byteinc[%s];\n", name, reg, reg);
354 	    break;
355 	 case sz_word:
356 	    if (movem)
357 		printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
358 	    else
359 		printf ("\tuaecptr %sa = m68k_areg(regs, %s) - 2;\n", name, reg);
360 	    break;
361 	 case sz_long:
362 	    if (movem)
363 		printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
364 	    else
365 		printf ("\tuaecptr %sa = m68k_areg(regs, %s) - 4;\n", name, reg);
366 	    break;
367 	 default:
368 	    abort ();
369 	}
370 	break;
371      case Ad16:
372 	printf ("\tuaecptr %sa = m68k_areg(regs, %s) + (uae_s32)(uae_s16)%s;\n", name, reg, gen_nextiword ());
373 	break;
374      case Ad8r:
375 	if (cpu_level > 1) {
376 	    if (next_cpu_level < 1)
377 		next_cpu_level = 1;
378 	    sync_m68k_pc ();
379 	    start_brace ();
380 	    printf ("\tuaecptr %sa = get_disp_ea_020(m68k_areg(regs, %s), next_iword());\n", name, reg);
381 	} else
382 	    printf ("\tuaecptr %sa = get_disp_ea_000(m68k_areg(regs, %s), %s);\n", name, reg, gen_nextiword ());
383 
384 	break;
385      case PC16:
386 	printf ("\tuaecptr %sa = m68k_getpc () + %d;\n", name, m68k_pc_offset);
387 	printf ("\t%sa += (uae_s32)(uae_s16)%s;\n", name, gen_nextiword ());
388 	break;
389      case PC8r:
390 	if (cpu_level > 1) {
391 	    if (next_cpu_level < 1)
392 		next_cpu_level = 1;
393 	    sync_m68k_pc ();
394 	    start_brace ();
395 	    printf ("\tuaecptr tmppc = m68k_getpc();\n");
396 	    printf ("\tuaecptr %sa = get_disp_ea_020(tmppc, next_iword());\n", name);
397 	} else {
398 	    printf ("\tuaecptr tmppc = m68k_getpc() + %d;\n", m68k_pc_offset);
399 	    printf ("\tuaecptr %sa = get_disp_ea_000(tmppc, %s);\n", name, gen_nextiword ());
400 	}
401 
402 	break;
403      case absw:
404 	printf ("\tuaecptr %sa = (uae_s32)(uae_s16)%s;\n", name, gen_nextiword ());
405 	break;
406      case absl:
407 	printf ("\tuaecptr %sa = %s;\n", name, gen_nextilong ());
408 	break;
409      case imm:
410 	if (getv != GENA_GETV_FETCH)
411 	    abort ();
412 	switch (size) {
413 	 case sz_byte:
414 	    printf ("\tuae_s8 %s = %s;\n", name, gen_nextibyte ());
415 	    break;
416 	 case sz_word:
417 	    printf ("\tuae_s16 %s = %s;\n", name, gen_nextiword ());
418 	    break;
419 	 case sz_long:
420 	    printf ("\tuae_s32 %s = %s;\n", name, gen_nextilong ());
421 	    break;
422 	 default:
423 	    abort ();
424 	}
425 	return;
426      case imm0:
427 	if (getv != GENA_GETV_FETCH)
428 	    abort ();
429 	printf ("\tuae_s8 %s = %s;\n", name, gen_nextibyte ());
430 	return;
431      case imm1:
432 	if (getv != GENA_GETV_FETCH)
433 	    abort ();
434 	printf ("\tuae_s16 %s = %s;\n", name, gen_nextiword ());
435 	return;
436      case imm2:
437 	if (getv != GENA_GETV_FETCH)
438 	    abort ();
439 	printf ("\tuae_s32 %s = %s;\n", name, gen_nextilong ());
440 	return;
441      case immi:
442 	if (getv != GENA_GETV_FETCH)
443 	    abort ();
444 	printf ("\tuae_u32 %s = %s;\n", name, reg);
445 	return;
446      default:
447 	abort ();
448     }
449 
450     /* We get here for all non-reg non-immediate addressing modes to
451      * actually fetch the value. */
452 
453     if (using_exception_3 && getv != GENA_GETV_NO_FETCH && size != sz_byte) {
454 	printf ("\tif ((%sa & 1) != 0) {\n", name);
455 	printf ("\t\tlast_fault_for_exception_3 = %sa;\n", name);
456 	printf ("\t\tlast_op_for_exception_3 = opcode;\n");
457 	printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + %d;\n", m68k_pc_offset);
458 	printf ("\t\tException(3, 0);\n");
459 	printf ("\t\tgoto %s;\n", endlabelstr);
460 	printf ("\t}\n");
461 	need_endlabel = 1;
462 	start_brace ();
463     }
464 
465     if (getv == GENA_GETV_FETCH) {
466 	switch (size) {
467 	 case sz_byte: break;
468 	 case sz_word: break;
469 	 case sz_long: break;
470 	 default: abort ();
471 	}
472 	start_brace ();
473 	printf("\n#ifdef FULLMMU\n");
474 	switch (size) {
475 	case sz_byte: printf ("\tuae_s8 %s = %sget_byte(%sa);\n", name, mem_prefix[xlateflag], name); break;
476 	case sz_word: printf ("\tuae_s16 %s = %sget_word(%sa);\n", name, mem_prefix[xlateflag], name); break;
477 	case sz_long: printf ("\tuae_s32 %s = %sget_long(%sa);\n", name, mem_prefix[xlateflag], name); break;
478 	 default: abort ();
479 	}
480 	printf("#else\n");
481 	switch (size) {
482 	case sz_byte: printf ("\tuae_s8 %s = phys_get_byte(%sa);\n", name, name); break;
483 	case sz_word: printf ("\tuae_s16 %s = phys_get_word(%sa);\n", name, name); break;
484 	case sz_long: printf ("\tuae_s32 %s = phys_get_long(%sa);\n", name, name); break;
485 	 default: abort ();
486 	}
487 	printf("#endif\n");
488     }
489 
490     /* We now might have to fix up the register for pre-dec or post-inc
491      * addressing modes. */
492     if (!movem)
493 	switch (mode) {
494 	 case Aipi:
495 	    if (fixup == 1)
496 	    {
497 		printf ("\tfixup.flag = 1;\n");
498 		printf ("\tfixup.reg = %s;\n", reg);
499 		printf ("\tfixup.value = m68k_areg(regs, %s);\n", reg);
500 	    }
501 	    switch (size) {
502 	     case sz_byte:
503 		printf ("\tm68k_areg(regs, %s) += areg_byteinc[%s];\n", reg, reg);
504 		break;
505 	     case sz_word:
506 		printf ("\tm68k_areg(regs, %s) += 2;\n", reg);
507 		break;
508 	     case sz_long:
509 		printf ("\tm68k_areg(regs, %s) += 4;\n", reg);
510 		break;
511 	     default:
512 		abort ();
513 	    }
514 	    break;
515 	 case Apdi:
516 	    if (fixup == 1)
517 	    {
518 		printf ("\tfixup.flag = 1;\n");
519 		printf ("\tfixup.reg = %s;\n", reg);
520 		printf ("\tfixup.value = m68k_areg(regs, %s);\n", reg);
521 	    }
522 	    printf ("\tm68k_areg (regs, %s) = %sa;\n", reg, name);
523 	    break;
524 	 default:
525 	    break;
526 	}
527 
528     }
529     else /* (fixup != 2) */
530     {
531     if (!movem)
532 	switch (mode) {
533 	 case Aipi:
534 	 case Apdi:
535 	    printf ("\tfixup.flag = 0;\n");
536 	    break;
537 	 default:
538 	    break;
539 	}
540     }
541 }
542 
genamode(amodes mode,char * reg,wordsizes size,char * name,int getv,int movem,int xlateflag)543 static void genamode (amodes mode, char *reg, wordsizes size, char *name, int getv, int movem, int xlateflag)
544 {
545 	genamode2 (mode, reg, size, name, getv, movem, xlateflag, 0);
546 }
547 
genastore(char * from,amodes mode,char * reg,wordsizes size,char * to,int xlateflag)548 static void genastore (char *from, amodes mode, char *reg, wordsizes size, char *to, int xlateflag)
549 {
550     switch (mode) {
551      case Dreg:
552 	switch (size) {
553 	 case sz_byte:
554 	    printf ("\tm68k_dreg(regs, %s) = (m68k_dreg(regs, %s) & ~0xff) | ((%s) & 0xff);\n", reg, reg, from);
555 	    break;
556 	 case sz_word:
557 	    printf ("\tm68k_dreg(regs, %s) = (m68k_dreg(regs, %s) & ~0xffff) | ((%s) & 0xffff);\n", reg, reg, from);
558 	    break;
559 	 case sz_long:
560 	    printf ("\tm68k_dreg(regs, %s) = (%s);\n", reg, from);
561 	    break;
562 	 default:
563 	    abort ();
564 	}
565 	break;
566      case Areg:
567 	switch (size) {
568 	 case sz_word:
569 	    fprintf (stderr, "Foo\n");
570 	    printf ("\tm68k_areg(regs, %s) = (uae_s32)(uae_s16)(%s);\n", reg, from);
571 	    break;
572 	 case sz_long:
573 	    printf ("\tm68k_areg(regs, %s) = (%s);\n", reg, from);
574 	    break;
575 	 default:
576 	    abort ();
577 	}
578 	break;
579      case Aind:
580      case Aipi:
581      case Apdi:
582      case Ad16:
583      case Ad8r:
584      case absw:
585      case absl:
586      case PC16:
587      case PC8r:
588 	gen_set_fault_pc ();
589 	printf("#ifdef FULLMMU\n");
590 	switch (size) {
591 	 case sz_byte:
592 	    printf ("\t%sput_byte(%sa,%s);\n", mem_prefix[xlateflag], to, from);
593 	    printf("#else\n");
594 	    printf ("\tput_byte(%sa,%s);\n", to, from);
595 	    break;
596 	 case sz_word:
597 	    if (cpu_level < 2 && (mode == PC16 || mode == PC8r))
598 		abort ();
599 	    printf ("\t%sput_word(%sa,%s);\n", mem_prefix[xlateflag], to, from);
600 	    printf("#else\n");
601 	    printf ("\tput_word(%sa,%s);\n", to, from);
602 	    break;
603 	 case sz_long:
604 	    if (cpu_level < 2 && (mode == PC16 || mode == PC8r))
605 		abort ();
606 	    printf ("\t%sput_long(%sa,%s);\n", mem_prefix[xlateflag], to, from);
607 	    printf("#else\n");
608 	    printf ("\tput_long(%sa,%s);\n", to, from);
609 	    break;
610 	 default:
611 	    abort ();
612 	}
613 	printf("#endif\n");
614 	break;
615      case imm:
616      case imm0:
617      case imm1:
618      case imm2:
619      case immi:
620 	abort ();
621 	break;
622      default:
623 	abort ();
624     }
625 }
626 
genmovemel(uae_u16 opcode)627 static void genmovemel (uae_u16 opcode)
628 {
629     char getcode1[100];
630     char getcode2[100];
631     int size = table68k[opcode].size == sz_long ? 4 : 2;
632 
633     if (table68k[opcode].size == sz_long) {
634 		strcpy (getcode1, "");
635 		strcpy (getcode2, "get_long(srca)");
636     } else {
637 		strcpy (getcode1, "(uae_s32)(uae_s16)");
638 		strcpy (getcode2, "get_word(srca)");
639     }
640 
641     printf ("\tuae_u16 mask = %s;\n", gen_nextiword ());
642     printf ("\tunsigned int dmask = mask & 0xff, amask = (mask >> 8) & 0xff;\n");
643     genamode (table68k[opcode].dmode, "dstreg", table68k[opcode].size, "src", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_NO_INC, XLATE_LOG);
644     start_brace ();
645     printf("\n#ifdef FULLMMU\n");
646     printf ("\twhile (dmask) { m68k_dreg(regs, movem_index1[dmask]) = %s%s; srca += %d; dmask = movem_next[dmask]; }\n",
647 	    getcode1, getcode2, size);
648     printf ("\twhile (amask) { m68k_areg(regs, movem_index1[amask]) = %s%s; srca += %d; amask = movem_next[amask]; }\n",
649 	    getcode1, getcode2, size);
650     printf("#else\n");
651     printf ("\twhile (dmask) { m68k_dreg(regs, movem_index1[dmask]) = %sphys_%s; srca += %d; dmask = movem_next[dmask]; }\n",
652 	    getcode1, getcode2, size);
653     printf ("\twhile (amask) { m68k_areg(regs, movem_index1[amask]) = %sphys_%s; srca += %d; amask = movem_next[amask]; }\n",
654 	    getcode1, getcode2, size);
655     printf("#endif\n");
656 
657     if (table68k[opcode].dmode == Aipi)
658 	printf ("\tm68k_areg(regs, dstreg) = srca;\n");
659 }
660 
genmovemle(uae_u16 opcode)661 static void genmovemle (uae_u16 opcode)
662 {
663     char putcode[100];
664     int size = table68k[opcode].size == sz_long ? 4 : 2;
665 
666     if (table68k[opcode].size == sz_long) {
667 	strcpy (putcode, "put_long(srca,");
668     } else {
669 	strcpy (putcode, "put_word(srca,");
670     }
671 
672     printf ("\tuae_u16 mask = %s;\n", gen_nextiword ());
673     genamode (table68k[opcode].dmode, "dstreg", table68k[opcode].size, "src",
674 			GENA_GETV_FETCH_ALIGN, GENA_MOVEM_NO_INC, XLATE_LOG);
675     sync_m68k_pc ();
676 
677     start_brace ();
678     if (table68k[opcode].dmode == Apdi) {
679 	printf ("\tuae_u16 amask = mask & 0xff, dmask = (mask >> 8) & 0xff;\n");
680 	printf("#ifdef FULLMMU\n");
681 	printf ("\twhile (amask) { srca -= %d; %s m68k_areg(regs, movem_index2[amask])); amask = movem_next[amask]; }\n",
682 		size, putcode);
683 	printf ("\twhile (dmask) { srca -= %d; %s m68k_dreg(regs, movem_index2[dmask])); dmask = movem_next[dmask]; }\n",
684 		size, putcode);
685 	printf("#else\n");
686 	printf ("\twhile (amask) { srca -= %d; phys_%s m68k_areg(regs, movem_index2[amask])); amask = movem_next[amask]; }\n",
687 		size, putcode);
688 	printf ("\twhile (dmask) { srca -= %d; phys_%s m68k_dreg(regs, movem_index2[dmask])); dmask = movem_next[dmask]; }\n",
689 		size, putcode);
690 	printf("#endif\n");
691 	printf ("\tm68k_areg(regs, dstreg) = srca;\n");
692     } else {
693 	printf ("\tuae_u16 dmask = mask & 0xff, amask = (mask >> 8) & 0xff;\n");
694 	printf("#ifdef FULLMMU\n");
695 	printf ("\twhile (dmask) { %s m68k_dreg(regs, movem_index1[dmask])); srca += %d; dmask = movem_next[dmask]; }\n",
696 		putcode, size);
697 	printf ("\twhile (amask) { %s m68k_areg(regs, movem_index1[amask])); srca += %d; amask = movem_next[amask]; }\n",
698 		putcode, size);
699 	printf("#else\n");
700 	printf ("\twhile (dmask) { phys_%s m68k_dreg(regs, movem_index1[dmask])); srca += %d; dmask = movem_next[dmask]; }\n",
701 		putcode, size);
702 	printf ("\twhile (amask) { phys_%s m68k_areg(regs, movem_index1[amask])); srca += %d; amask = movem_next[amask]; }\n",
703 		putcode, size);
704 	printf("#endif\n");
705     }
706 }
707 
duplicate_carry(void)708 static void duplicate_carry (void)
709 {
710     printf ("\tCOPY_CARRY();\n");
711 }
712 
713 typedef enum {
714     flag_logical_noclobber, flag_logical, flag_add, flag_sub, flag_cmp, flag_addx, flag_subx, flag_z, flag_zn,
715     flag_av, flag_sv
716 } flagtypes;
717 
genflags_normal(flagtypes type,wordsizes size,char * value,char * src,char * dst)718 static void genflags_normal (flagtypes type, wordsizes size, char *value, char *src, char *dst)
719 {
720     char vstr[100], sstr[100], dstr[100];
721     char usstr[100], udstr[100];
722     char unsstr[100], undstr[100];
723 
724     switch (size) {
725      case sz_byte:
726 	strcpy (vstr, "((uae_s8)(");
727 	strcpy (usstr, "((uae_u8)(");
728 	break;
729      case sz_word:
730 	strcpy (vstr, "((uae_s16)(");
731 	strcpy (usstr, "((uae_u16)(");
732 	break;
733      case sz_long:
734 	strcpy (vstr, "((uae_s32)(");
735 	strcpy (usstr, "((uae_u32)(");
736 	break;
737      default:
738 	abort ();
739     }
740     strcpy (unsstr, usstr);
741 
742     strcpy (sstr, vstr);
743     strcpy (dstr, vstr);
744     strcat (vstr, value);
745     strcat (vstr, "))");
746     strcat (dstr, dst);
747     strcat (dstr, "))");
748     strcat (sstr, src);
749     strcat (sstr, "))");
750 
751     strcpy (udstr, usstr);
752     strcat (udstr, dst);
753     strcat (udstr, "))");
754     strcat (usstr, src);
755     strcat (usstr, "))");
756 
757     strcpy (undstr, unsstr);
758     strcat (unsstr, "-");
759     strcat (undstr, "~");
760     strcat (undstr, dst);
761     strcat (undstr, "))");
762     strcat (unsstr, src);
763     strcat (unsstr, "))");
764 
765     switch (type) {
766      case flag_logical_noclobber:
767      case flag_logical:
768      case flag_z:
769      case flag_zn:
770      case flag_av:
771      case flag_sv:
772      case flag_addx:
773      case flag_subx:
774 	break;
775 
776      case flag_add:
777 	printf ("uae_u32 %s = %s + %s;\n", value, dstr, sstr);
778 	break;
779      case flag_sub:
780      case flag_cmp:
781 	printf ("uae_u32 %s = %s - %s;\n", value, dstr, sstr);
782 	break;
783     }
784 
785     switch (type) {
786      case flag_logical_noclobber:
787      case flag_logical:
788      case flag_z:
789      case flag_zn:
790 	break;
791 
792      case flag_add:
793      case flag_sub:
794      case flag_addx:
795      case flag_subx:
796      case flag_cmp:
797      case flag_av:
798      case flag_sv:
799 	printf ("\t" BOOL_TYPE " flgs = %s < 0;\n", sstr);
800 	printf ("\t" BOOL_TYPE " flgo = %s < 0;\n", dstr);
801 	printf ("\t" BOOL_TYPE " flgn = %s < 0;\n", vstr);
802 	break;
803     }
804 
805     switch (type) {
806      case flag_logical:
807 	printf ("\tCLEAR_CZNV();\n");
808 	printf ("\tSET_ZFLG (%s == 0);\n", vstr);
809 	printf ("\tSET_NFLG (%s < 0);\n", vstr);
810 	break;
811      case flag_logical_noclobber:
812 	printf ("\tSET_ZFLG (%s == 0);\n", vstr);
813 	printf ("\tSET_NFLG (%s < 0);\n", vstr);
814 	break;
815      case flag_av:
816 	printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n");
817 	break;
818      case flag_sv:
819 	printf ("\tSET_VFLG ((flgs ^ flgo) & (flgn ^ flgo));\n");
820 	break;
821      case flag_z:
822 	printf ("\tSET_ZFLG (GET_ZFLG () & (%s == 0));\n", vstr);
823 	break;
824      case flag_zn:
825 	printf ("\tSET_ZFLG (GET_ZFLG () & (%s == 0));\n", vstr);
826 	printf ("\tSET_NFLG (%s < 0);\n", vstr);
827 	break;
828      case flag_add:
829 	printf ("\tSET_ZFLG (%s == 0);\n", vstr);
830 	printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n");
831 	printf ("\tSET_CFLG (%s < %s);\n", undstr, usstr);
832 	duplicate_carry ();
833 	printf ("\tSET_NFLG (flgn != 0);\n");
834 	break;
835      case flag_sub:
836 	printf ("\tSET_ZFLG (%s == 0);\n", vstr);
837 	printf ("\tSET_VFLG ((flgs ^ flgo) & (flgn ^ flgo));\n");
838 	printf ("\tSET_CFLG (%s > %s);\n", usstr, udstr);
839 	duplicate_carry ();
840 	printf ("\tSET_NFLG (flgn != 0);\n");
841 	break;
842      case flag_addx:
843 	printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n"); /* minterm SON: 0x42 */
844 	printf ("\tSET_CFLG (flgs ^ ((flgs ^ flgo) & (flgo ^ flgn)));\n"); /* minterm SON: 0xD4 */
845 	duplicate_carry ();
846 	break;
847      case flag_subx:
848 	printf ("\tSET_VFLG ((flgs ^ flgo) & (flgo ^ flgn));\n"); /* minterm SON: 0x24 */
849 	printf ("\tSET_CFLG (flgs ^ ((flgs ^ flgn) & (flgo ^ flgn)));\n"); /* minterm SON: 0xB2 */
850 	duplicate_carry ();
851 	break;
852      case flag_cmp:
853 	printf ("\tSET_ZFLG (%s == 0);\n", vstr);
854 	printf ("\tSET_VFLG ((flgs != flgo) && (flgn != flgo));\n");
855 	printf ("\tSET_CFLG (%s > %s);\n", usstr, udstr);
856 	printf ("\tSET_NFLG (flgn != 0);\n");
857 	break;
858     }
859 }
860 
genflags(flagtypes type,wordsizes size,char * value,char * src,char * dst)861 static void genflags (flagtypes type, wordsizes size, char *value, char *src, char *dst)
862 {
863     /* Temporarily deleted 68k/ARM flag optimizations.  I'd prefer to have
864        them in the appropriate m68k.h files and use just one copy of this
865        code here.  The API can be changed if necessary.  */
866     int done = 0;
867 
868     start_brace ();
869     printf("\n#ifdef OPTIMIZED_FLAGS\n");
870     switch (type) {
871      case flag_add:
872      case flag_sub:
873 	printf ("\tuae_u32 %s;\n", value);
874 	break;
875      default:
876 	break;
877     }
878 
879     /* At least some of those casts are fairly important! */
880     switch (type) {
881      case flag_logical_noclobber:
882 	printf ("\t{uae_u32 oldcznv = GET_CZNV() & ~(FLAGVAL_Z | FLAGVAL_N);\n");
883 	if (strcmp (value, "0") == 0) {
884 	    printf ("\tSET_CZNV (olcznv | FLAGVAL_Z);\n");
885 	} else {
886 	    switch (size) {
887 	     case sz_byte: printf ("\toptflag_testb ((uae_s8)(%s));\n", value); break;
888 	     case sz_word: printf ("\toptflag_testw ((uae_s16)(%s));\n", value); break;
889 	     case sz_long: printf ("\toptflag_testl ((uae_s32)(%s));\n", value); break;
890 	    }
891 	    printf ("\tIOR_CZNV (oldcznv);\n");
892 	}
893 	printf ("\t}\n");
894 	done = 1;
895 	break;
896 
897      case flag_logical:
898 	if (strcmp (value, "0") == 0) {
899 	    printf ("\tSET_CZNV (FLAGVAL_Z);\n");
900 	} else {
901 	    switch (size) {
902 	     case sz_byte: printf ("\toptflag_testb ((uae_s8)(%s));\n", value); break;
903 	     case sz_word: printf ("\toptflag_testw ((uae_s16)(%s));\n", value); break;
904 	     case sz_long: printf ("\toptflag_testl ((uae_s32)(%s));\n", value); break;
905 	    }
906 	}
907 	done = 1;
908 	break;
909 
910      case flag_add:
911 	switch (size) {
912 	 case sz_byte: printf ("\toptflag_addb (%s, (uae_s8)(%s), (uae_s8)(%s));\n", value, src, dst); break;
913 	 case sz_word: printf ("\toptflag_addw (%s, (uae_s16)(%s), (uae_s16)(%s));\n", value, src, dst); break;
914 	 case sz_long: printf ("\toptflag_addl (%s, (uae_s32)(%s), (uae_s32)(%s));\n", value, src, dst); break;
915 	}
916 	done = 1;
917 	break;
918 
919      case flag_sub:
920 	switch (size) {
921 	 case sz_byte: printf ("\toptflag_subb (%s, (uae_s8)(%s), (uae_s8)(%s));\n", value, src, dst); break;
922 	 case sz_word: printf ("\toptflag_subw (%s, (uae_s16)(%s), (uae_s16)(%s));\n", value, src, dst); break;
923 	 case sz_long: printf ("\toptflag_subl (%s, (uae_s32)(%s), (uae_s32)(%s));\n", value, src, dst); break;
924 	}
925 	done = 1;
926 	break;
927 
928      case flag_cmp:
929 	switch (size) {
930 	 case sz_byte: printf ("\toptflag_cmpb ((uae_s8)(%s), (uae_s8)(%s));\n", src, dst); break;
931 	 case sz_word: printf ("\toptflag_cmpw ((uae_s16)(%s), (uae_s16)(%s));\n", src, dst); break;
932 	 case sz_long: printf ("\toptflag_cmpl ((uae_s32)(%s), (uae_s32)(%s));\n", src, dst); break;
933 	}
934 	done = 1;
935 	break;
936 
937      default:
938 	break;
939     }
940     if (done)
941 	printf("#else\n");
942     else
943     	printf("#endif\n");
944     genflags_normal (type, size, value, src, dst);
945     if (done)
946 	printf("#endif\n");
947 }
948 
force_range_for_rox(const char * var,wordsizes size)949 static void force_range_for_rox (const char *var, wordsizes size)
950 {
951     /* Could do a modulo operation here... which one is faster? */
952     switch (size) {
953      case sz_long:
954 	printf ("\tif (%s >= 33) %s -= 33;\n", var, var);
955 	break;
956      case sz_word:
957 	printf ("\tif (%s >= 34) %s -= 34;\n", var, var);
958 	printf ("\tif (%s >= 17) %s -= 17;\n", var, var);
959 	break;
960      case sz_byte:
961 	printf ("\tif (%s >= 36) %s -= 36;\n", var, var);
962 	printf ("\tif (%s >= 18) %s -= 18;\n", var, var);
963 	printf ("\tif (%s >= 9) %s -= 9;\n", var, var);
964 	break;
965     }
966 }
967 
cmask(wordsizes size)968 static const char *cmask (wordsizes size)
969 {
970     switch (size) {
971      case sz_byte: return "0x80";
972      case sz_word: return "0x8000";
973      case sz_long: return "0x80000000";
974      default: abort (); return NULL;
975     }
976 }
977 
source_is_imm1_8(struct instr * i)978 static int source_is_imm1_8 (struct instr *i)
979 {
980     return i->stype == 3;
981 }
982 
gen_opcode(unsigned long int opcode)983 static void gen_opcode (unsigned long int opcode)
984 {
985     struct instr *curi = table68k + opcode;
986 
987     start_brace ();
988 #if 0
989     printf ("uae_u8 *m68k_pc = m68k_getpc();\n");
990 #endif
991     m68k_pc_offset = 2;
992     switch (curi->plev) {
993      case 0: /* not privileged */
994 	break;
995      case 1: /* unprivileged only on 68000 */
996 	if (cpu_level == 0)
997 	    break;
998 	if (next_cpu_level < 0)
999 	    next_cpu_level = 0;
1000 
1001 	/* fall through */
1002      case 2: /* priviledged */
1003 	printf ("if (!regs.s) { Exception(8,0); goto %s; }\n", endlabelstr);
1004 	need_endlabel = 1;
1005 	start_brace ();
1006 	break;
1007      case 3: /* privileged if size == word */
1008 	if (curi->size == sz_byte)
1009 	    break;
1010 	printf ("if (!regs.s) { Exception(8,0); goto %s; }\n", endlabelstr);
1011 	need_endlabel = 1;
1012 	start_brace ();
1013 	break;
1014     }
1015     switch (curi->mnemo) {
1016      case i_OR:
1017      case i_AND:
1018      case i_EOR:
1019 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1020 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1021 	printf ("\tsrc %c= dst;\n", curi->mnemo == i_OR ? '|' : curi->mnemo == i_AND ? '&' : '^');
1022 	genflags (flag_logical, curi->size, "src", "", "");
1023 	genastore ("src", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1024 	break;
1025      case i_ORSR:
1026      case i_EORSR:
1027 	printf ("\tMakeSR();\n");
1028 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1029 	if (curi->size == sz_byte) {
1030 	    printf ("\tsrc &= 0xFF;\n");
1031 	}
1032 	printf ("\tregs.sr %c= src;\n", curi->mnemo == i_EORSR ? '^' : '|');
1033 	printf ("\tMakeFromSR();\n");
1034 	break;
1035      case i_ANDSR:
1036 	printf ("\tMakeSR();\n");
1037 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1038 	if (curi->size == sz_byte) {
1039 	    printf ("\tsrc |= 0xFF00;\n");
1040 	}
1041 	printf ("\tregs.sr &= src;\n");
1042 	printf ("\tMakeFromSR();\n");
1043 	break;
1044      case i_SUB:
1045 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1046 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1047 	start_brace ();
1048 	genflags (flag_sub, curi->size, "newv", "src", "dst");
1049 	genastore ("newv", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1050 	break;
1051      case i_SUBA:
1052 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1053 	genamode (curi->dmode, "dstreg", sz_long, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1054 	start_brace ();
1055 	printf ("\tuae_u32 newv = dst - src;\n");
1056 	genastore ("newv", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1057 	break;
1058      case i_SUBX:
1059 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 1);
1060 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1061 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 2);
1062 	start_brace ();
1063 	printf ("\tuae_u32 newv = dst - src - (GET_XFLG () ? 1 : 0);\n");
1064 	genflags (flag_subx, curi->size, "newv", "src", "dst");
1065 	genflags (flag_zn, curi->size, "newv", "", "");
1066 	genastore ("newv", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1067 	break;
1068      case i_SBCD:
1069 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 1);
1070 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1071 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 2);
1072 	start_brace ();
1073 	printf ("\tuae_u16 newv_lo = (dst & 0xF) - (src & 0xF) - (GET_XFLG () ? 1 : 0);\n");
1074 	printf ("\tuae_u16 newv_hi = (dst & 0xF0) - (src & 0xF0);\n");
1075 	printf ("\tuae_u16 newv, tmp_newv;\n");
1076 	printf ("\tint bcd = 0;\n");
1077 	printf ("\tnewv = tmp_newv = newv_hi + newv_lo;\n");
1078 	printf ("\tif (newv_lo & 0xF0) { newv -= 6; bcd = 6; };\n");
1079 	printf ("\tif ((((dst & 0xFF) - (src & 0xFF) - (GET_XFLG () ? 1 : 0)) & 0x100) > 0xFF) { newv -= 0x60; }\n");
1080 	printf ("\tSET_CFLG ((((dst & 0xFF) - (src & 0xFF) - bcd - (GET_XFLG () ? 1 : 0)) & 0x300) > 0xFF);\n");
1081 	duplicate_carry ();
1082 	/* Manual says bits NV are undefined though a real 68030 doesn't change V and 68040/060 don't change both */
1083 	if (cpu_level >= xBCD_KEEPS_N_FLAG) {
1084 		if (next_cpu_level < xBCD_KEEPS_N_FLAG)
1085 			next_cpu_level = xBCD_KEEPS_N_FLAG - 1;
1086 		genflags (flag_z, curi->size, "newv", "", "");
1087 	} else {
1088 		genflags (flag_zn, curi->size, "newv", "", "");
1089 	}
1090 	if (cpu_level >= xBCD_KEEPS_V_FLAG) {
1091 		if (next_cpu_level < xBCD_KEEPS_V_FLAG)
1092 			next_cpu_level = xBCD_KEEPS_V_FLAG - 1;
1093 	} else {
1094 		printf ("\tSET_VFLG ((tmp_newv & 0x80) != 0 && (newv & 0x80) == 0);\n");
1095 	}
1096 	genastore ("newv", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1097 	break;
1098      case i_ADD:
1099 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1100 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1101 	start_brace ();
1102 	genflags (flag_add, curi->size, "newv", "src", "dst");
1103 	genastore ("newv", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1104 	break;
1105      case i_ADDA:
1106 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1107 	genamode (curi->dmode, "dstreg", sz_long, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1108 	start_brace ();
1109 	printf ("\tuae_u32 newv = dst + src;\n");
1110 	genastore ("newv", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1111 	break;
1112      case i_ADDX:
1113 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 1);
1114 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1115 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 2);
1116 	start_brace ();
1117 	printf ("\tuae_u32 newv = dst + src + (GET_XFLG () ? 1 : 0);\n");
1118 	genflags (flag_addx, curi->size, "newv", "src", "dst");
1119 	genflags (flag_zn, curi->size, "newv", "", "");
1120 	genastore ("newv", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1121 	break;
1122      case i_ABCD:
1123 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 1);
1124 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1125 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 2);
1126 	start_brace ();
1127 	printf ("\tuae_u16 newv_lo = (src & 0xF) + (dst & 0xF) + (GET_XFLG () ? 1 : 0);\n");
1128 	printf ("\tuae_u16 newv_hi = (src & 0xF0) + (dst & 0xF0);\n");
1129 	printf ("\tuae_u16 newv, tmp_newv;\n");
1130 	printf ("\tint cflg;\n");
1131 	printf ("\tnewv = tmp_newv = newv_hi + newv_lo;\n");
1132 	printf ("\tif (newv_lo > 9) { newv += 6; }\n");
1133 	printf ("\tcflg = (newv & 0x3F0) > 0x90;\n");
1134 	printf ("\tif (cflg) newv += 0x60;\n");
1135 	printf ("\tSET_CFLG (cflg);\n");
1136 	duplicate_carry ();
1137 	/* Manual says bits NV are undefined though a real 68030 doesn't change V and 68040/060 don't change both */
1138 	if (cpu_level >= xBCD_KEEPS_N_FLAG) {
1139 		if (next_cpu_level < xBCD_KEEPS_N_FLAG)
1140 			next_cpu_level = xBCD_KEEPS_N_FLAG - 1;
1141 		genflags (flag_z, curi->size, "newv", "", "");
1142 	} else {
1143 		genflags (flag_zn, curi->size, "newv", "", "");
1144 	}
1145 	if (cpu_level >= xBCD_KEEPS_V_FLAG) {
1146 		if (next_cpu_level < xBCD_KEEPS_V_FLAG)
1147 			next_cpu_level = xBCD_KEEPS_V_FLAG - 1;
1148 	} else {
1149 		printf ("\tSET_VFLG ((tmp_newv & 0x80) == 0 && (newv & 0x80) != 0);\n");
1150 	}
1151 	genastore ("newv", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1152 	break;
1153      case i_NEG:
1154 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1155 	start_brace ();
1156 	genflags (flag_sub, curi->size, "dst", "src", "0");
1157 	genastore ("dst", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1158 	break;
1159      case i_NEGX:
1160 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1161 	start_brace ();
1162 	printf ("\tuae_u32 newv = 0 - src - (GET_XFLG () ? 1 : 0);\n");
1163 	genflags (flag_subx, curi->size, "newv", "src", "0");
1164 	genflags (flag_zn, curi->size, "newv", "", "");
1165 	genastore ("newv", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1166 	break;
1167      case i_NBCD:
1168 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1169 	start_brace ();
1170 	printf ("\tuae_u16 newv_lo = - (src & 0xF) - (GET_XFLG () ? 1 : 0);\n");
1171 	printf ("\tuae_u16 newv_hi = - (src & 0xF0);\n");
1172 	printf ("\tuae_u16 newv;\n");
1173 	printf ("\tint cflg, tmp_newv;\n");
1174 	printf ("\ttmp_newv = newv_hi + newv_lo;\n");
1175 	printf ("\tif (newv_lo > 9) { newv_lo -= 6; }\n");
1176 	printf ("\tnewv = newv_hi + newv_lo;\n");
1177 	printf ("\tcflg = (newv & 0x1F0) > 0x90;\n");
1178 	printf ("\tif (cflg) newv -= 0x60;\n");
1179 	printf ("\tSET_CFLG (cflg);\n");
1180 	duplicate_carry();
1181 	/* Manual says bits NV are undefined though a real 68030 doesn't change V and 68040/060 don't change both */
1182 	if (cpu_level >= xBCD_KEEPS_N_FLAG) {
1183 		if (next_cpu_level < xBCD_KEEPS_N_FLAG)
1184 			next_cpu_level = xBCD_KEEPS_N_FLAG - 1;
1185 		genflags (flag_z, curi->size, "newv", "", "");
1186 	} else {
1187 		genflags (flag_zn, curi->size, "newv", "", "");
1188 	}
1189 	if (cpu_level >= xBCD_KEEPS_V_FLAG) {
1190 		if (next_cpu_level < xBCD_KEEPS_V_FLAG)
1191 			next_cpu_level = xBCD_KEEPS_V_FLAG - 1;
1192 	} else {
1193 		printf ("\tSET_VFLG ((tmp_newv & 0x80) != 0 && (newv & 0x80) == 0);\n");
1194 	}
1195 	genastore ("newv", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1196 	break;
1197      case i_CLR:
1198 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1199 	genflags (flag_logical, curi->size, "0", "", "");
1200 	genastore ("0", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1201 	break;
1202      case i_NOT:
1203 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1204 	start_brace ();
1205 	printf ("\tuae_u32 dst = ~src;\n");
1206 	genflags (flag_logical, curi->size, "dst", "", "");
1207 	genastore ("dst", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1208 	break;
1209      case i_TST:
1210 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1211 	genflags (flag_logical, curi->size, "src", "", "");
1212 	break;
1213      case i_BTST:
1214 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1215 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1216 	if (curi->size == sz_byte)
1217 	    printf ("\tsrc &= 7;\n");
1218 	else
1219 	    printf ("\tsrc &= 31;\n");
1220 	printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1221 	break;
1222      case i_BCHG:
1223 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1224 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1225 	if (curi->size == sz_byte)
1226 	    printf ("\tsrc &= 7;\n");
1227 	else
1228 	    printf ("\tsrc &= 31;\n");
1229 	printf ("\tdst ^= (1 << src);\n");
1230 	printf ("\tSET_ZFLG (((uae_u32)dst & (1 << src)) >> src);\n");
1231 	genastore ("dst", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1232 	break;
1233      case i_BCLR:
1234 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1235 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1236 	if (curi->size == sz_byte)
1237 	    printf ("\tsrc &= 7;\n");
1238 	else
1239 	    printf ("\tsrc &= 31;\n");
1240 	printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1241 	printf ("\tdst &= ~(1 << src);\n");
1242 	genastore ("dst", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1243 	break;
1244      case i_BSET:
1245 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1246 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1247 	if (curi->size == sz_byte)
1248 	    printf ("\tsrc &= 7;\n");
1249 	else
1250 	    printf ("\tsrc &= 31;\n");
1251 	printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1252 	printf ("\tdst |= (1 << src);\n");
1253 	genastore ("dst", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1254 	break;
1255      case i_CMPM:
1256      case i_CMP:
1257 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1258 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1259 	start_brace ();
1260 	genflags (flag_cmp, curi->size, "newv", "src", "dst");
1261 	break;
1262      case i_CMPA:
1263 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1264 	genamode (curi->dmode, "dstreg", sz_long, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1265 	start_brace ();
1266 	genflags (flag_cmp, sz_long, "newv", "src", "dst");
1267 	break;
1268 	/* The next two are coded a little unconventional, but they are doing
1269 	 * weird things... */
1270      case i_MVPRM:
1271 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1272 
1273 	printf ("\tuaecptr memp = m68k_areg(regs, dstreg) + (uae_s32)(uae_s16)%s;\n", gen_nextiword ());
1274 	if (curi->size == sz_word) {
1275 	    printf ("\tput_byte(memp, src >> 8); put_byte(memp + 2, src);\n");
1276 	} else {
1277 	    printf ("\tput_byte(memp, src >> 24); put_byte(memp + 2, src >> 16);\n");
1278 	    printf ("\tput_byte(memp + 4, src >> 8); put_byte(memp + 6, src);\n");
1279 	}
1280 	break;
1281      case i_MVPMR:
1282 	printf ("\tuaecptr memp = m68k_areg(regs, srcreg) + (uae_s32)(uae_s16)%s;\n", gen_nextiword ());
1283 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1284 	if (curi->size == sz_word) {
1285 	    printf ("\tuae_u16 val  = get_byte(memp) << 8;\n");
1286 	    printf ("\t        val |= get_byte(memp + 2);\n");
1287 	} else {
1288 	    printf ("\tuae_u32 val  = get_byte(memp) << 24;\n");
1289 	    printf ("\t        val |= get_byte(memp + 2) << 16;\n");
1290 	    printf ("\t        val |= get_byte(memp + 4) << 8;\n");
1291 	    printf ("\t        val |= get_byte(memp + 6);\n");
1292 	}
1293 	genastore ("val", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1294 	break;
1295      case i_MOVE:
1296 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 1);
1297 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1298 	genamode2 (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 2);
1299 	genflags (flag_logical, curi->size, "src", "", "");
1300 	genastore ("src", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1301 	break;
1302      case i_MOVEA:
1303 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1304 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1305 	if (curi->size == sz_word) {
1306 	    printf ("\tuae_u32 val = (uae_s32)(uae_s16)src;\n");
1307 	} else {
1308 	    printf ("\tuae_u32 val = src;\n");
1309 	}
1310 	genastore ("val", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1311 	break;
1312      case i_MVSR2:
1313 	genamode (curi->smode, "srcreg", sz_word, "src", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1314 	printf ("\tMakeSR();\n");
1315 	if (curi->size == sz_byte)
1316 	    genastore ("regs.sr & 0xff", curi->smode, "srcreg", sz_word, "src", XLATE_LOG);
1317 	else
1318 	    genastore ("regs.sr", curi->smode, "srcreg", sz_word, "src", XLATE_LOG);
1319 	break;
1320      case i_MV2SR:
1321 	genamode (curi->smode, "srcreg", sz_word, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1322 	if (curi->size == sz_byte)
1323 	    printf ("\tMakeSR();\n\tregs.sr &= 0xFF00;\n\tregs.sr |= src & 0xFF;\n");
1324 	else {
1325 	    printf ("\tregs.sr = src;\n");
1326 	}
1327 	printf ("\tMakeFromSR();\n");
1328 	break;
1329      case i_SWAP:
1330 	genamode (curi->smode, "srcreg", sz_long, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1331 	start_brace ();
1332 	printf ("\tuae_u32 dst = ((src >> 16)&0xFFFF) | ((src&0xFFFF)<<16);\n");
1333 	genflags (flag_logical, sz_long, "dst", "", "");
1334 	genastore ("dst", curi->smode, "srcreg", sz_long, "src", XLATE_LOG);
1335 	break;
1336      case i_EXG:
1337 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1338 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1339 	genastore ("dst", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1340 	genastore ("src", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1341 	break;
1342      case i_EXT:
1343 	genamode (curi->smode, "srcreg", sz_long, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1344 	start_brace ();
1345 	switch (curi->size) {
1346 	case sz_byte: printf ("\tuae_u32 dst = (uae_s32)(uae_s8)src;\n"); break;
1347 	case sz_word: printf ("\tuae_u16 dst = (uae_s16)(uae_s8)src;\n"); break;
1348 	case sz_long: printf ("\tuae_u32 dst = (uae_s32)(uae_s16)src;\n"); break;
1349 	default: abort ();
1350 	}
1351 	genflags (flag_logical,
1352 		  curi->size == sz_word ? sz_word : sz_long, "dst", "", "");
1353 	genastore ("dst", curi->smode, "srcreg",
1354 		   curi->size == sz_word ? sz_word : sz_long, "src", XLATE_LOG);
1355 	break;
1356      case i_MVMEL:
1357 	genmovemel (opcode);
1358 	break;
1359      case i_MVMLE:
1360 	genmovemle (opcode);
1361 	break;
1362      case i_TRAP:
1363 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1364 	gen_set_fault_pc ();
1365 	printf ("\tException(src+32,0);\n");
1366 	break;
1367      case i_MVR2USP:
1368 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1369 	printf ("\tregs.usp = src;\n");
1370 	break;
1371      case i_MVUSP2R:
1372 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1373 	genastore ("regs.usp", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1374 	break;
1375      case i_RESET:
1376 	printf ("\tAtariReset();\n");
1377 	break;
1378      case i_NOP:
1379 	break;
1380      case i_STOP:
1381 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1382 	/*
1383 	 * STOP undocumented features:
1384 	 * if SR is not set:
1385 	 * 68000 (68010?): Update SR, increase PC and then cause privilege violation exception (handled in newcpu)
1386 	 * 68000 (68010?): Traced STOP also runs 4 cycles faster.
1387 	 * 68020 68030: STOP works normally
1388 	 * 68040 68060: Immediate privilege violation exception
1389 	 */
1390 	printf ("\tuae_u16 sr = src;\n");
1391 	if (cpu_level >= 4) {
1392 		printf("\tif (!(sr & 0x2000)) {\n");
1393 		printf ("m68k_incpc(%d);\n", m68k_pc_offset);
1394 		printf("\t\tException(8,0); goto %s;\n", endlabelstr);
1395 		printf("\t}\n");
1396 	}
1397 	printf("\tregs.sr = sr;\n");
1398 	printf ("\tMakeFromSR();\n");
1399 	printf ("\tm68k_setstopped(1);\n");
1400 	sync_m68k_pc ();
1401 	/* STOP does not prefetch anything */
1402 	/* did_prefetch = -1; */
1403 	break;
1404      case i_RTE:
1405 	if (cpu_level == 0) {
1406 	    genamode (Aipi, "7", sz_word, "sr", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1407 	    genamode (Aipi, "7", sz_long, "pc", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1408 	    printf ("\tregs.sr = sr; m68k_setpc_rte(pc);\n");
1409 	    fill_prefetch_0 ();
1410 	    printf ("\tMakeFromSR();\n");
1411 	} else {
1412 	    int old_brace_level = n_braces;
1413 	    if (next_cpu_level < 0)
1414 		next_cpu_level = 0;
1415 	    printf ("\tuae_u16 newsr; uae_u32 newpc; for (;;) {\n");
1416 	    genamode (Aipi, "7", sz_word, "sr", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1417 	    genamode (Aipi, "7", sz_long, "pc", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1418 	    genamode (Aipi, "7", sz_word, "format", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1419 	    printf ("\tnewsr = sr; newpc = pc;\n");
1420 	    printf ("\tif ((format & 0xF000) == 0x0000) { break; }\n");
1421 	    printf ("\telse if ((format & 0xF000) == 0x1000) { ; }\n");
1422 	    printf ("\telse if ((format & 0xF000) == 0x2000) { m68k_areg(regs, 7) += 4; break; }\n");
1423 //	    printf ("\telse if ((format & 0xF000) == 0x3000) { m68k_areg(regs, 7) += 4; break; }\n");
1424 	    printf ("\telse if ((format & 0xF000) == 0x7000) { m68k_areg(regs, 7) += 52; break; }\n");
1425 	    printf ("\telse if ((format & 0xF000) == 0x8000) { m68k_areg(regs, 7) += 50; break; }\n");
1426 	    printf ("\telse if ((format & 0xF000) == 0x9000) { m68k_areg(regs, 7) += 12; break; }\n");
1427 	    printf ("\telse if ((format & 0xF000) == 0xa000) { m68k_areg(regs, 7) += 24; break; }\n");
1428 	    printf ("\telse if ((format & 0xF000) == 0xb000) { m68k_areg(regs, 7) += 84; break; }\n");
1429 	    printf ("\telse { Exception(14,0); goto %s; }\n", endlabelstr);
1430 	    printf ("\tregs.sr = newsr; MakeFromSR();\n}\n");
1431 	    pop_braces (old_brace_level);
1432 	    printf ("\tregs.sr = newsr; MakeFromSR();\n");
1433 	    printf ("\tm68k_setpc_rte(newpc);\n");
1434 	    fill_prefetch_0 ();
1435 	    need_endlabel = 1;
1436 	}
1437 	/* PC is set and prefetch filled. */
1438 	m68k_pc_offset = 0;
1439 	break;
1440      case i_RTD:
1441 	genamode (curi->smode, "srcreg", curi->size, "offs", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1442 	genamode (Aipi, "7", sz_long, "pc", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1443 	printf ("\tm68k_areg(regs, 7) += offs;\n");
1444 	printf ("\tm68k_setpc_rte(pc);\n");
1445 	fill_prefetch_0 ();
1446 	/* PC is set and prefetch filled. */
1447 	m68k_pc_offset = 0;
1448 	break;
1449      case i_LINK:
1450 	genamode (curi->dmode, "dstreg", curi->size, "offs", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1451 	genamode (Apdi, "7", sz_long, "old", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1452 	genamode (curi->smode, "srcreg", sz_long, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1453 	genastore ("m68k_areg(regs, 7)", curi->smode, "srcreg", sz_long, "src", XLATE_LOG);
1454 	printf ("\tm68k_areg(regs, 7) += offs;\n");
1455 	genastore ("src", Apdi, "7", sz_long, "old", XLATE_LOG);
1456 	break;
1457      case i_UNLK:
1458 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1459 	printf ("\tm68k_areg(regs, 7) = src;\n");
1460 	genamode (Aipi, "7", sz_long, "old", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1461 	genastore ("old", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1462 	break;
1463      case i_RTS:
1464 	printf ("\tm68k_do_rts();\n");
1465 	fill_prefetch_0 ();
1466 	m68k_pc_offset = 0;
1467 	break;
1468      case i_TRAPV:
1469 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
1470 	sync_m68k_pc ();
1471 	printf ("\tif (GET_VFLG ()) { Exception(7,oldpc); goto %s; }\n", endlabelstr);
1472 	need_endlabel = 1;
1473 	break;
1474      case i_RTR:
1475 	printf ("\tMakeSR();\n");
1476 	genamode2 (Aipi, "7", sz_word, "sr", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 1);
1477 	genamode (Aipi, "7", sz_long, "pc", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1478 	genamode2 (Aipi, "7", sz_word, "sr", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG, 2);
1479 	printf ("\tregs.sr &= 0xFF00; sr &= 0xFF;\n");
1480 	printf ("\tregs.sr |= sr; m68k_setpc(pc);\n");
1481 	fill_prefetch_0 ();
1482 	printf ("\tMakeFromSR();\n");
1483 	m68k_pc_offset = 0;
1484 	break;
1485      case i_JSR:
1486 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_NO_FETCH, GENA_MOVEM_DO_INC, XLATE_PHYS);
1487 	printf ("\tm68k_do_jsr(m68k_getpc() + %d, srca);\n", m68k_pc_offset);
1488 	fill_prefetch_0 ();
1489 	m68k_pc_offset = 0;
1490 	break;
1491      case i_JMP:
1492 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_NO_FETCH, GENA_MOVEM_DO_INC, XLATE_PHYS);
1493 	printf ("\tm68k_setpc(srca);\n");
1494 	fill_prefetch_0 ();
1495 	m68k_pc_offset = 0;
1496 	break;
1497      case i_BSR:
1498 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_PHYS);
1499 	printf ("\tuae_s32 s = (uae_s32)src + 2;\n");
1500 	if (using_exception_3) {
1501 	    printf ("\tif (src & 1) {\n");
1502 	    printf ("\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1503 	    printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + s;\n");
1504 	    printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1505 	    printf ("\t}\n");
1506 	    need_endlabel = 1;
1507 	}
1508 	printf ("\tm68k_do_bsr(m68k_getpc() + %d, s);\n", m68k_pc_offset);
1509 	fill_prefetch_0 ();
1510 	m68k_pc_offset = 0;
1511 	break;
1512      case i_Bcc:
1513 	if (curi->size == sz_long) {
1514 	    if (cpu_level < 2) {
1515 		printf ("\tm68k_incpc(2);\n");
1516 		printf ("\tif (!cctrue(%d)) goto %s;\n", curi->cc, endlabelstr);
1517 		printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1518 		printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + 1;\n");
1519 		printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1520 		need_endlabel = 1;
1521 	    } else {
1522 		if (next_cpu_level < 1)
1523 		    next_cpu_level = 1;
1524 	    }
1525 	}
1526 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_PHYS);
1527 	printf ("\tif (!cctrue(%d)) goto didnt_jump_%lx;\n", curi->cc, opcode);
1528 	if (using_exception_3) {
1529 	    printf ("\tif (src & 1) {\n");
1530 	    printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1531 	    printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + 2 + (uae_s32)src;\n");
1532 	    printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1533 	    printf ("\t}\n");
1534 	    need_endlabel = 1;
1535 	}
1536 	printf ("\tm68k_incpc ((uae_s32)src + 2);\n");
1537 	fill_prefetch_0 ();
1538 	printf ("return;\n");
1539 	printf ("didnt_jump_%lx:;\n", opcode);
1540 	need_endlabel = 1;
1541 	break;
1542      case i_LEA:
1543 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_NO_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1544 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1545 	genastore ("srca", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
1546 	break;
1547      case i_PEA:
1548 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_NO_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1549 	genamode (Apdi, "7", sz_long, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1550 	genastore ("srca", Apdi, "7", sz_long, "dst", XLATE_LOG);
1551 	break;
1552      case i_DBcc:
1553 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1554 	genamode (curi->dmode, "dstreg", curi->size, "offs", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1555 
1556 	printf ("\tif (!cctrue(%d)) {\n", curi->cc);
1557 	genastore ("(src-1)", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1558 
1559 	printf ("\t\tif (src) {\n");
1560 	if (using_exception_3) {
1561 	    printf ("\t\t\tif (offs & 1) {\n");
1562 	    printf ("\t\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1563 	    printf ("\t\t\tlast_fault_for_exception_3 = m68k_getpc() + 2 + (uae_s32)offs + 2;\n");
1564 	    printf ("\t\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1565 	    printf ("\t\t}\n");
1566 	    need_endlabel = 1;
1567 	}
1568 	printf ("\t\t\tm68k_incpc((uae_s32)offs + 2);\n");
1569 	fill_prefetch_0 ();
1570 	printf ("return;\n");
1571 	printf ("\t\t}\n");
1572 	printf ("\t}\n");
1573 	need_endlabel = 1;
1574 	break;
1575      case i_Scc:
1576 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1577 	start_brace ();
1578 	printf ("\tint val = cctrue(%d) ? 0xff : 0;\n", curi->cc);
1579 	genastore ("val", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
1580 	break;
1581      case i_DIVU:
1582 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
1583 	genamode (curi->smode, "srcreg", sz_word, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1584 	genamode (curi->dmode, "dstreg", sz_long, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1585 	sync_m68k_pc ();
1586 	/* Clear V flag when dividing by zero - Alcatraz Odyssey demo depends
1587 	 * on this (actually, it's doing a DIVS).  */
1588 	printf ("\tif (src == 0) { SET_VFLG (0); Exception (5, oldpc); goto %s; } else {\n", endlabelstr);
1589 	printf ("\tuae_u32 newv = (uae_u32)dst / (uae_u32)(uae_u16)src;\n");
1590 	printf ("\tuae_u32 rem = (uae_u32)dst %% (uae_u32)(uae_u16)src;\n");
1591 	/* The N flag appears to be set each time there is an overflow.
1592 	 * Weird. */
1593 	printf ("\tif (newv > 0xffff) { SET_VFLG (1); SET_NFLG (1); SET_CFLG (0); } else\n\t{\n");
1594 	genflags (flag_logical, sz_word, "newv", "", "");
1595 	printf ("\tnewv = (newv & 0xffff) | ((uae_u32)rem << 16);\n");
1596 	genastore ("newv", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1597 	printf ("\t}\n");
1598 	printf ("\t}\n");
1599 	need_endlabel = 1;
1600 	break;
1601      case i_DIVS:
1602 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
1603 	genamode (curi->smode, "srcreg", sz_word, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1604 	genamode (curi->dmode, "dstreg", sz_long, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1605 	sync_m68k_pc ();
1606 	printf ("\tif (src == 0) { SET_VFLG (0); Exception(5,oldpc); goto %s; } else {\n", endlabelstr);
1607 	printf ("\tuae_s32 newv = (uae_s32)dst / (uae_s32)(uae_s16)src;\n");
1608 	printf ("\tuae_u16 rem = (uae_s32)dst %% (uae_s32)(uae_s16)src;\n");
1609 	printf ("\tif ((newv & 0xffff8000) != 0 && (newv & 0xffff8000) != 0xffff8000) { SET_VFLG (1); SET_NFLG (1); SET_CFLG (0); } else\n\t{\n");
1610 	printf ("\tif (((uae_s16)rem < 0) != ((uae_s32)dst < 0)) rem = -rem;\n");
1611 	genflags (flag_logical, sz_word, "newv", "", "");
1612 	printf ("\tnewv = (newv & 0xffff) | ((uae_u32)rem << 16);\n");
1613 	genastore ("newv", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1614 	printf ("\t}\n");
1615 	printf ("\t}\n");
1616 	need_endlabel = 1;
1617 	break;
1618      case i_MULU:
1619 	genamode (curi->smode, "srcreg", sz_word, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1620 	genamode (curi->dmode, "dstreg", sz_word, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1621 	start_brace ();
1622 	printf ("\tuae_u32 newv = (uae_u32)(uae_u16)dst * (uae_u32)(uae_u16)src;\n");
1623 	genflags (flag_logical, sz_long, "newv", "", "");
1624 	genastore ("newv", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1625 	break;
1626      case i_MULS:
1627 	genamode (curi->smode, "srcreg", sz_word, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1628 	genamode (curi->dmode, "dstreg", sz_word, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1629 	start_brace ();
1630 	printf ("\tuae_u32 newv = (uae_s32)(uae_s16)dst * (uae_s32)(uae_s16)src;\n");
1631 	genflags (flag_logical, sz_long, "newv", "", "");
1632 	genastore ("newv", curi->dmode, "dstreg", sz_long, "dst", XLATE_LOG);
1633 	break;
1634      case i_CHK:
1635 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
1636 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1637 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1638 	printf ("\tif ((uae_s32)dst < 0) { SET_NFLG (1); Exception(6,oldpc); goto %s; }\n", endlabelstr);
1639 	printf ("\telse if (dst > src) { SET_NFLG (0); Exception(6,oldpc); goto %s; }\n", endlabelstr);
1640 	need_endlabel = 1;
1641 	break;
1642 
1643      case i_CHK2:
1644 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
1645 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1646 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
1647 	printf ("\t{uae_s32 upper,lower,reg = regs.regs[(extra >> 12) & 15];\n");
1648 	switch (curi->size) {
1649 	 case sz_byte:
1650 	    printf ("\tlower=(uae_s32)(uae_s8)get_byte(dsta); upper = (uae_s32)(uae_s8)get_byte(dsta+1);\n");
1651 	    printf ("\tif ((extra & 0x8000) == 0) reg = (uae_s32)(uae_s8)reg;\n");
1652 	    break;
1653 	 case sz_word:
1654 	    printf ("\tlower=(uae_s32)(uae_s16)get_word(dsta); upper = (uae_s32)(uae_s16)get_word(dsta+2);\n");
1655 	    printf ("\tif ((extra & 0x8000) == 0) reg = (uae_s32)(uae_s16)reg;\n");
1656 	    break;
1657 	 case sz_long:
1658 	    printf ("\tlower=get_long(dsta); upper = get_long(dsta+4);\n");
1659 	    break;
1660 	 default:
1661 	    abort ();
1662 	}
1663 	printf ("\tSET_ZFLG (upper == reg || lower == reg);\n");
1664 	printf ("\tSET_CFLG_ALWAYS (lower <= upper ? reg < lower || reg > upper : reg > upper || reg < lower);\n");
1665 	printf ("\tif ((extra & 0x800) && GET_CFLG ()) { Exception(6,oldpc); goto %s; }\n}\n", endlabelstr);
1666 	need_endlabel = 1;
1667 	break;
1668 
1669      case i_ASR:
1670 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1671 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1672 	start_brace ();
1673 	switch (curi->size) {
1674 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1675 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1676 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1677 	 default: abort ();
1678 	}
1679 	printf ("\tuae_u32 sign = (%s & val) >> %d;\n", cmask (curi->size), bit_size (curi->size) - 1);
1680 	printf ("\tcnt &= 63;\n");
1681 	printf ("\tCLEAR_CZNV();\n");
1682 	printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1683 	printf ("\t\tval = %s & (uae_u32)-sign;\n", bit_mask (curi->size));
1684 	printf ("\t\tSET_CFLG (sign);\n");
1685 	duplicate_carry ();
1686 	if (source_is_imm1_8 (curi))
1687 	    printf ("\t} else {\n");
1688 	else
1689 	    printf ("\t} else if (cnt > 0) {\n");
1690 	printf ("\t\tval >>= cnt - 1;\n");
1691 	printf ("\t\tSET_CFLG (val & 1);\n");
1692 	duplicate_carry ();
1693 	printf ("\t\tval >>= 1;\n");
1694 	printf ("\t\tval |= (%s << (%d - cnt)) & (uae_u32)-sign;\n",
1695 		bit_mask (curi->size),
1696 		bit_size (curi->size));
1697 	printf ("\t\tval &= %s;\n", bit_mask (curi->size));
1698 	printf ("\t}\n");
1699 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1700 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1701 	break;
1702      case i_ASL:
1703 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1704 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1705 	start_brace ();
1706 	switch (curi->size) {
1707 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1708 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1709 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1710 	 default: abort ();
1711 	}
1712 	printf ("\tcnt &= 63;\n");
1713 	printf ("\tCLEAR_CZNV();\n");
1714 	printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1715 	printf ("\t\tSET_VFLG (val != 0);\n");
1716 	printf ("\t\tSET_CFLG (cnt == %d ? val & 1 : 0);\n",
1717 		bit_size (curi->size));
1718 	duplicate_carry ();
1719 	printf ("\t\tval = 0;\n");
1720 	if (source_is_imm1_8 (curi))
1721 	    printf ("\t} else {\n");
1722 	else
1723 	    printf ("\t} else if (cnt > 0) {\n");
1724 	printf ("\t\tuae_u32 mask = (%s << (%d - cnt)) & %s;\n",
1725 		bit_mask (curi->size),
1726 		bit_size (curi->size) - 1,
1727 		bit_mask (curi->size));
1728 	printf ("\t\tSET_VFLG ((val & mask) != mask && (val & mask) != 0);\n");
1729 	printf ("\t\tval <<= cnt - 1;\n");
1730 	printf ("\t\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1731 	duplicate_carry ();
1732 	printf ("\t\tval <<= 1;\n");
1733 	printf ("\t\tval &= %s;\n", bit_mask (curi->size));
1734 	printf ("\t}\n");
1735 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1736 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1737 	break;
1738      case i_LSR:
1739 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1740 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1741 	start_brace ();
1742 	switch (curi->size) {
1743 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1744 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1745 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1746 	 default: abort ();
1747 	}
1748 	printf ("\tcnt &= 63;\n");
1749 	printf ("\tCLEAR_CZNV();\n");
1750 	printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1751 	printf ("\t\tSET_CFLG ((cnt == %d) & (val >> %d));\n",
1752 		bit_size (curi->size), bit_size (curi->size) - 1);
1753 	duplicate_carry ();
1754 	printf ("\t\tval = 0;\n");
1755 	if (source_is_imm1_8 (curi))
1756 	    printf ("\t} else {\n");
1757 	else
1758 	    printf ("\t} else if (cnt > 0) {\n");
1759 	printf ("\t\tval >>= cnt - 1;\n");
1760 	printf ("\t\tSET_CFLG (val & 1);\n");
1761 	duplicate_carry ();
1762 	printf ("\t\tval >>= 1;\n");
1763 	printf ("\t}\n");
1764 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1765 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1766 	break;
1767      case i_LSL:
1768 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1769 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1770 	start_brace ();
1771 	switch (curi->size) {
1772 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1773 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1774 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1775 	 default: abort ();
1776 	}
1777 	printf ("\tcnt &= 63;\n");
1778 	printf ("\tCLEAR_CZNV();\n");
1779 	printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1780 	printf ("\t\tSET_CFLG (cnt == %d ? val & 1 : 0);\n",
1781 		bit_size (curi->size));
1782 	duplicate_carry ();
1783 	printf ("\t\tval = 0;\n");
1784 	if (source_is_imm1_8 (curi))
1785 	    printf ("\t} else {\n");
1786 	else
1787 	    printf ("\t} else if (cnt > 0) {\n");
1788 	printf ("\t\tval <<= (cnt - 1);\n");
1789 	printf ("\t\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1790 	duplicate_carry ();
1791 	printf ("\t\tval <<= 1;\n");
1792 	printf ("\tval &= %s;\n", bit_mask (curi->size));
1793 	printf ("\t}\n");
1794 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1795 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1796 	break;
1797      case i_ROL:
1798 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1799 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1800 	start_brace ();
1801 	switch (curi->size) {
1802 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1803 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1804 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1805 	 default: abort ();
1806 	}
1807 	printf ("\tcnt &= 63;\n");
1808 	printf ("\tCLEAR_CZNV();\n");
1809 	if (source_is_imm1_8 (curi))
1810 	    printf ("{");
1811 	else
1812 	    printf ("\tif (cnt > 0) {\n");
1813 	printf ("\tuae_u32 loval;\n");
1814 	printf ("\tcnt &= %d;\n", bit_size (curi->size) - 1);
1815 	printf ("\tloval = val >> (%d - cnt);\n", bit_size (curi->size));
1816 	printf ("\tval <<= cnt;\n");
1817 	printf ("\tval |= loval;\n");
1818 	printf ("\tval &= %s;\n", bit_mask (curi->size));
1819 	printf ("\tSET_CFLG (val & 1);\n");
1820 	printf ("}\n");
1821 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1822 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1823 	break;
1824      case i_ROR:
1825 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1826 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1827 	start_brace ();
1828 	switch (curi->size) {
1829 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1830 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1831 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1832 	 default: abort ();
1833 	}
1834 	printf ("\tcnt &= 63;\n");
1835 	printf ("\tCLEAR_CZNV();\n");
1836 	if (source_is_imm1_8 (curi))
1837 	    printf ("{");
1838 	else
1839 	    printf ("\tif (cnt > 0) {");
1840 	printf ("\tuae_u32 hival;\n");
1841 	printf ("\tcnt &= %d;\n", bit_size (curi->size) - 1);
1842 	printf ("\thival = val << (%d - cnt);\n", bit_size (curi->size));
1843 	printf ("\tval >>= cnt;\n");
1844 	printf ("\tval |= hival;\n");
1845 	printf ("\tval &= %s;\n", bit_mask (curi->size));
1846 	printf ("\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1847 	printf ("\t}\n");
1848 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1849 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1850 	break;
1851      case i_ROXL:
1852 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1853 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1854 	start_brace ();
1855 	switch (curi->size) {
1856 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1857 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1858 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1859 	 default: abort ();
1860 	}
1861 	printf ("\tcnt &= 63;\n");
1862 	printf ("\tCLEAR_CZNV();\n");
1863 	if (source_is_imm1_8 (curi))
1864 	    printf ("{");
1865 	else {
1866 	    force_range_for_rox ("cnt", curi->size);
1867 	    printf ("\tif (cnt > 0) {\n");
1868 	}
1869 	printf ("\tcnt--;\n");
1870 	printf ("\t{\n\tuae_u32 carry;\n");
1871 	printf ("\tuae_u32 loval = val >> (%d - cnt);\n", bit_size (curi->size) - 1);
1872 	printf ("\tcarry = loval & 1;\n");
1873 	printf ("\tval = (((val << 1) | GET_XFLG ()) << cnt) | (loval >> 1);\n");
1874 	printf ("\tSET_XFLG (carry);\n");
1875 	printf ("\tval &= %s;\n", bit_mask (curi->size));
1876 	printf ("\t} }\n");
1877 	printf ("\tSET_CFLG (GET_XFLG ());\n");
1878 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1879 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1880 	break;
1881      case i_ROXR:
1882 	genamode (curi->smode, "srcreg", curi->size, "cnt", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1883 	genamode (curi->dmode, "dstreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1884 	start_brace ();
1885 	switch (curi->size) {
1886 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1887 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1888 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1889 	 default: abort ();
1890 	}
1891 	printf ("\tcnt &= 63;\n");
1892 	printf ("\tCLEAR_CZNV();\n");
1893 	if (source_is_imm1_8 (curi))
1894 	    printf ("{");
1895 	else {
1896 	    force_range_for_rox ("cnt", curi->size);
1897 	    printf ("\tif (cnt > 0) {\n");
1898 	}
1899 	printf ("\tcnt--;\n");
1900 	printf ("\t{\n\tuae_u32 carry;\n");
1901 	printf ("\tuae_u32 hival = (val << 1) | GET_XFLG ();\n");
1902 	printf ("\thival <<= (%d - cnt);\n", bit_size (curi->size) - 1);
1903 	printf ("\tval >>= cnt;\n");
1904 	printf ("\tcarry = val & 1;\n");
1905 	printf ("\tval >>= 1;\n");
1906 	printf ("\tval |= hival;\n");
1907 	printf ("\tSET_XFLG (carry);\n");
1908 	printf ("\tval &= %s;\n", bit_mask (curi->size));
1909 	printf ("\t} }\n");
1910 	printf ("\tSET_CFLG (GET_XFLG ());\n");
1911 	genflags (flag_logical_noclobber, curi->size, "val", "", "");
1912 	genastore ("val", curi->dmode, "dstreg", curi->size, "data", XLATE_LOG);
1913 	break;
1914      case i_ASRW:
1915 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1916 	start_brace ();
1917 	switch (curi->size) {
1918 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1919 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1920 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1921 	 default: abort ();
1922 	}
1923 	printf ("\tuae_u32 sign = %s & val;\n", cmask (curi->size));
1924 	printf ("\tuae_u32 cflg = val & 1;\n");
1925 	printf ("\tval = (val >> 1) | sign;\n");
1926 	genflags (flag_logical, curi->size, "val", "", "");
1927 	printf ("\tSET_CFLG (cflg);\n");
1928 	duplicate_carry ();
1929 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
1930 	break;
1931      case i_ASLW:
1932 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1933 	start_brace ();
1934 	switch (curi->size) {
1935 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1936 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1937 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1938 	 default: abort ();
1939 	}
1940 	printf ("\tuae_u32 sign = %s & val;\n", cmask (curi->size));
1941 	printf ("\tuae_u32 sign2;\n");
1942 	printf ("\tval <<= 1;\n");
1943 	genflags (flag_logical, curi->size, "val", "", "");
1944 	printf ("\tsign2 = %s & val;\n", cmask (curi->size));
1945 	printf ("\tSET_CFLG (sign != 0);\n");
1946 	duplicate_carry ();
1947 
1948 	printf ("\tSET_VFLG (GET_VFLG () | (sign2 != sign));\n");
1949 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
1950 	break;
1951      case i_LSRW:
1952 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1953 	start_brace ();
1954 	switch (curi->size) {
1955 	 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1956 	 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1957 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1958 	 default: abort ();
1959 	}
1960 	printf ("\tuae_u32 carry = val & 1;\n");
1961 	printf ("\tval >>= 1;\n");
1962 	genflags (flag_logical, curi->size, "val", "", "");
1963 	printf ("SET_CFLG (carry);\n");
1964 	duplicate_carry ();
1965 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
1966 	break;
1967      case i_LSLW:
1968 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1969 	start_brace ();
1970 	switch (curi->size) {
1971 	 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1972 	 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1973 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1974 	 default: abort ();
1975 	}
1976 	printf ("\tuae_u32 carry = val & %s;\n", cmask (curi->size));
1977 	printf ("\tval <<= 1;\n");
1978 	genflags (flag_logical, curi->size, "val", "", "");
1979 	printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
1980 	duplicate_carry ();
1981 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
1982 	break;
1983      case i_ROLW:
1984 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
1985 	start_brace ();
1986 	switch (curi->size) {
1987 	 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1988 	 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1989 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1990 	 default: abort ();
1991 	}
1992 	printf ("\tuae_u32 carry = val & %s;\n", cmask (curi->size));
1993 	printf ("\tval <<= 1;\n");
1994 	printf ("\tif (carry)  val |= 1;\n");
1995 	genflags (flag_logical, curi->size, "val", "", "");
1996 	printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
1997 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
1998 	break;
1999      case i_RORW:
2000 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2001 	start_brace ();
2002 	switch (curi->size) {
2003 	 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
2004 	 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
2005 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
2006 	 default: abort ();
2007 	}
2008 	printf ("\tuae_u32 carry = val & 1;\n");
2009 	printf ("\tval >>= 1;\n");
2010 	printf ("\tif (carry) val |= %s;\n", cmask (curi->size));
2011 	genflags (flag_logical, curi->size, "val", "", "");
2012 	printf ("SET_CFLG (carry);\n");
2013 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
2014 	break;
2015      case i_ROXLW:
2016 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2017 	start_brace ();
2018 	switch (curi->size) {
2019 	 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
2020 	 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
2021 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
2022 	 default: abort ();
2023 	}
2024 	printf ("\tuae_u32 carry = val & %s;\n", cmask (curi->size));
2025 	printf ("\tval <<= 1;\n");
2026 	printf ("\tif (GET_XFLG ()) val |= 1;\n");
2027 	genflags (flag_logical, curi->size, "val", "", "");
2028 	printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
2029 	duplicate_carry ();
2030 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
2031 	break;
2032      case i_ROXRW:
2033 	genamode (curi->smode, "srcreg", curi->size, "data", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2034 	start_brace ();
2035 	switch (curi->size) {
2036 	 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
2037 	 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
2038 	 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
2039 	 default: abort ();
2040 	}
2041 	printf ("\tuae_u32 carry = val & 1;\n");
2042 	printf ("\tval >>= 1;\n");
2043 	printf ("\tif (GET_XFLG ()) val |= %s;\n", cmask (curi->size));
2044 	genflags (flag_logical, curi->size, "val", "", "");
2045 	printf ("SET_CFLG (carry);\n");
2046 	duplicate_carry ();
2047 	genastore ("val", curi->smode, "srcreg", curi->size, "data", XLATE_LOG);
2048 	break;
2049      case i_MOVEC2:
2050 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2051 	start_brace ();
2052 	printf ("\tint regno = (src >> 12) & 15;\n");
2053 	printf ("\tuae_u32 *regp = regs.regs + regno;\n");
2054 	printf ("\tif (!m68k_movec2(src & 0xFFF, regp)) goto %s;\n", endlabelstr);
2055 	break;
2056      case i_MOVE2C:
2057 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2058 	start_brace ();
2059 	printf ("\tint regno = (src >> 12) & 15;\n");
2060 	printf ("\tuae_u32 *regp = regs.regs + regno;\n");
2061 	printf ("\tif (!m68k_move2c(src & 0xFFF, regp)) goto %s;\n", endlabelstr);
2062 	break;
2063      case i_CAS:
2064 	{
2065 	    int old_brace_level;
2066 	    genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2067 	    genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2068 	    start_brace ();
2069 	    printf ("\tint ru = (src >> 6) & 7;\n");
2070 	    printf ("\tint rc = src & 7;\n");
2071 	    genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc)", "dst");
2072 	    sync_m68k_pc ();
2073 	    printf ("\tif (GET_ZFLG ())");
2074 	    old_brace_level = n_braces;
2075 	    start_brace ();
2076 	    genastore ("(m68k_dreg(regs, ru))", curi->dmode, "dstreg", curi->size, "dst", XLATE_LOG);
2077 	    pop_braces (old_brace_level);
2078 	    printf ("else");
2079 	    start_brace ();
2080 	    switch (curi->size) {
2081 	    case sz_byte:
2082 		printf ("\tm68k_dreg(regs, rc) = (m68k_dreg(regs, rc) & ~0xff) | (dst & 0xff);\n");
2083 		break;
2084 	    case sz_word:
2085 		printf ("\tm68k_dreg(regs, rc) = (m68k_dreg(regs, rc) & ~0xffff) | (dst & 0xffff);\n");
2086 		break;
2087 	    default:
2088 		printf ("\tm68k_dreg(regs, rc) = dst;\n");
2089 		break;
2090 	    }
2091 	    pop_braces (old_brace_level);
2092 	}
2093 	break;
2094      case i_CAS2:
2095 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2096 	printf ("\tuae_u32 rn1 = regs.regs[(extra >> 28) & 15];\n");
2097 	printf ("\tuae_u32 rn2 = regs.regs[(extra >> 12) & 15];\n");
2098 	if (curi->size == sz_word) {
2099 	    int old_brace_level = n_braces;
2100 	    printf ("\tuae_u32 rc1 = (extra >> 16) & 7;\n");
2101 	    printf ("\tuae_u32 rc2 = extra & 7;\n");
2102 	    printf ("\tuae_u16 dst1 = get_word(rn1), dst2 = get_word(rn2);\n");
2103 	    genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc1)", "dst1");
2104 	    printf ("\tif (GET_ZFLG ()) {\n");
2105 	    genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc2)", "dst2");
2106 	    printf ("\tif (GET_ZFLG ()) {\n");
2107 	    printf ("\tput_word(rn1, m68k_dreg(regs, (extra >> 22) & 7));\n");
2108 	    printf ("\tput_word(rn2, m68k_dreg(regs, (extra >> 6) & 7));\n");
2109 	    printf ("\t}}\n");
2110 	    pop_braces (old_brace_level);
2111 	    printf ("\tif (! GET_ZFLG ()) {\n");
2112 	    printf ("\tm68k_dreg(regs, rc2) = (m68k_dreg(regs, rc2) & ~0xffff) | (dst2 & 0xffff);\n");
2113 	    printf ("\tm68k_dreg(regs, rc1) = (m68k_dreg(regs, rc1) & ~0xffff) | (dst1 & 0xffff);\n");
2114 	    printf ("\t}\n");
2115 	} else {
2116 	    int old_brace_level = n_braces;
2117 	    printf ("\tuae_u32 rc1 = (extra >> 16) & 7;\n");
2118 	    printf ("\tuae_u32 rc2 = extra & 7;\n");
2119 	    printf ("\tuae_u32 dst1 = get_long(rn1), dst2 = get_long(rn2);\n");
2120 	    genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc1)", "dst1");
2121 	    printf ("\tif (GET_ZFLG ()) {\n");
2122 	    genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc2)", "dst2");
2123 	    printf ("\tif (GET_ZFLG ()) {\n");
2124 	    printf ("\tput_long(rn1, m68k_dreg(regs, (extra >> 22) & 7));\n");
2125 	    printf ("\tput_long(rn2, m68k_dreg(regs, (extra >> 6) & 7));\n");
2126 	    printf ("\t}}\n");
2127 	    pop_braces (old_brace_level);
2128 	    printf ("\tif (! GET_ZFLG ()) {\n");
2129 	    printf ("\tm68k_dreg(regs, rc2) = dst2;\n");
2130 	    printf ("\tm68k_dreg(regs, rc1) = dst1;\n");
2131 	    printf ("\t}\n");
2132 	}
2133 	break;
2134      case i_MOVES:
2135 	{
2136 		int old_brace_level;
2137 
2138 		genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2139 		start_brace();
2140 		printf ("\tif (extra & 0x0800)\n");	/* from reg to ea */
2141 		{
2142 			int old_m68k_pc_offset = m68k_pc_offset;
2143 			/* use DFC */
2144 			old_brace_level = n_braces;
2145 			start_brace ();
2146 			printf ("\tuae_u32 src = regs.regs[(extra >> 12) & 15];\n");
2147 			genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_DFC);
2148 			genastore ("src", curi->dmode, "dstreg", curi->size, "dst", XLATE_DFC);
2149 			pop_braces (old_brace_level);
2150 			m68k_pc_offset = old_m68k_pc_offset;
2151 		}
2152 		printf ("else");	/* from ea to reg */
2153 		{
2154 			/* use SFC */
2155 			start_brace ();
2156 			genamode (curi->dmode, "dstreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_SFC);
2157 			printf ("\tif (extra & 0x8000) {\n");	/* address/data */
2158 			switch (curi->size) {
2159 				case sz_byte: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = (uae_s32)(uae_s8)src;\n"); break;
2160 				case sz_word: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = (uae_s32)(uae_s16)src;\n"); break;
2161 				case sz_long: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = src;\n"); break;
2162 				default: abort ();
2163 			}
2164 			printf ("\t} else {\n");
2165 			genastore ("src", Dreg, "(extra >> 12) & 7", curi->size, "", XLATE_LOG);
2166 			printf ("\t}\n");
2167 			sync_m68k_pc();
2168 			pop_braces (old_brace_level);
2169 		}
2170 	}
2171 	break;
2172      case i_BKPT:		/* only needed for hardware emulators */
2173 	sync_m68k_pc ();
2174 	printf ("\top_illg(opcode);\n");
2175 	break;
2176      case i_CALLM:		/* not present in 68030 */
2177 	sync_m68k_pc ();
2178 	printf ("\top_illg(opcode);\n");
2179 	break;
2180      case i_RTM:		/* not present in 68030 */
2181 	sync_m68k_pc ();
2182 	printf ("\top_illg(opcode);\n");
2183 	break;
2184      case i_TRAPcc:
2185 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
2186 	if (curi->smode != am_unknown && curi->smode != am_illg)
2187 	    genamode (curi->smode, "srcreg", curi->size, "dummy", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2188 	sync_m68k_pc ();
2189 	printf ("\tif (cctrue(%d)) { Exception(7,oldpc); goto %s; }\n", curi->cc, endlabelstr);
2190 	need_endlabel = 1;
2191 	break;
2192      case i_DIVL:
2193 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
2194 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2195 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2196 	sync_m68k_pc ();
2197 	printf ("\tm68k_divl(opcode, dst, extra, oldpc);\n");
2198 	break;
2199      case i_MULL:
2200 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2201 	genamode (curi->dmode, "dstreg", curi->size, "dst", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2202 	sync_m68k_pc ();
2203 	printf ("\tm68k_mull(opcode, dst, extra);\n");
2204 	break;
2205      case i_BFTST:
2206      case i_BFEXTU:
2207      case i_BFCHG:
2208      case i_BFEXTS:
2209      case i_BFCLR:
2210      case i_BFFFO:
2211      case i_BFSET:
2212      case i_BFINS:
2213 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2214 	genamode (curi->dmode, "dstreg", sz_long, "dst", GENA_GETV_FETCH_ALIGN, GENA_MOVEM_DO_INC, XLATE_LOG);
2215 	start_brace ();
2216 	printf ("\tuae_u32 bdata[2];");
2217 	printf ("\tuae_s32 offset = extra & 0x800 ? m68k_dreg(regs, (extra >> 6) & 7) : (extra >> 6) & 0x1f;\n");
2218 	printf ("\tint width = (((extra & 0x20 ? m68k_dreg(regs, extra & 7) : extra) -1) & 0x1f) +1;\n");
2219 	if (curi->dmode == Dreg) {
2220 	    printf ("\tuae_u32 tmp = m68k_dreg(regs, dstreg);\n");
2221 	    printf ("\toffset &= 0x1f;\n");
2222 	    printf ("\ttmp = (tmp << offset) | (tmp >> (32 - offset));\n");
2223 	    printf ("\tbdata[0] = tmp & ((1 << (32 - width)) - 1);\n");
2224 	} else {
2225 	    printf ("\tuae_u32 tmp;\n");
2226 	    printf ("\tdsta += offset >> 3;\n");
2227 	    printf ("\ttmp = get_bitfield(dsta, bdata, offset, width);\n");
2228 	}
2229 	printf ("\tSET_NFLG_ALWAYS (((uae_s32)tmp) < 0 ? 1 : 0);\n");
2230 	if (curi->mnemo == i_BFEXTS)
2231 		printf ("\ttmp = (uae_s32)tmp >> (32 - width);\n");
2232 	else
2233 		printf ("\ttmp >>= (32 - width);\n");
2234 	printf ("\tSET_ZFLG (tmp == 0); SET_VFLG (0); SET_CFLG (0);\n");
2235 	switch (curi->mnemo) {
2236 	 case i_BFTST:
2237 	    break;
2238 	 case i_BFEXTU:
2239 	 case i_BFEXTS:
2240 	    printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = tmp;\n");
2241 	    break;
2242 	 case i_BFCHG:
2243 	    printf ("\ttmp = tmp ^ (0xffffffffu >> (32 - width));\n");
2244 	    break;
2245 	 case i_BFCLR:
2246 	    printf ("\ttmp = 0;\n");
2247 	    break;
2248 	 case i_BFFFO:
2249 	    printf ("\t{ uae_u32 mask = 1 << (width-1);\n");
2250 	    printf ("\twhile (mask) { if (tmp & mask) break; mask >>= 1; offset++; }}\n");
2251 	    printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = offset;\n");
2252 	    break;
2253 	 case i_BFSET:
2254 	    printf ("\ttmp = 0xffffffffu >> (32 - width);\n");
2255 	    break;
2256 	 case i_BFINS:
2257 	    printf ("\ttmp = m68k_dreg(regs, (extra >> 12) & 7);\n");
2258 	    printf ("\ttmp = tmp & (0xffffffffu >> (32 - width));\n");
2259 	    printf ("\tSET_NFLG_ALWAYS (tmp & (1 << (width - 1)) ? 1 : 0);\n");
2260 	    printf ("\tSET_ZFLG (tmp == 0);\n");
2261 	    break;
2262 	 default:
2263 	    break;
2264 	}
2265 	if (curi->mnemo == i_BFCHG
2266 	    || curi->mnemo == i_BFCLR
2267 	    || curi->mnemo == i_BFSET
2268 	    || curi->mnemo == i_BFINS) {
2269 	    if (curi->dmode == Dreg) {
2270 		printf ("\ttmp = bdata[0] | (tmp << (32 - width));\n");
2271 	        printf ("\tm68k_dreg(regs, dstreg) = (tmp >> offset) | (tmp << (32 - offset));\n");
2272 	    } else {
2273 		printf ("\tput_bitfield(dsta, bdata, tmp, offset, width);\n");
2274 	    }
2275 	}
2276 	break;
2277      case i_PACK:
2278 	if (curi->smode == Dreg) {
2279 	    printf ("\tuae_u16 val = m68k_dreg(regs, srcreg) + %s;\n", gen_nextiword ());
2280 	    printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & 0xffffff00) | ((val >> 4) & 0xf0) | (val & 0xf);\n");
2281 	} else {
2282 	    printf ("\tuae_u16 val;\n");
2283 	    printf ("\tval = (uae_u16)get_byte(m68k_areg(regs, srcreg) - areg_byteinc[srcreg]);\n");
2284 	    printf ("\tval = (val | ((uae_u16)get_byte(m68k_areg(regs, srcreg) - 2 * areg_byteinc[srcreg]) << 8)) + %s;\n", gen_nextiword ());
2285 	    printf ("\tm68k_areg(regs, srcreg) -= 2;\n");
2286 	    printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2287 	    gen_set_fault_pc ();
2288 	    printf ("\tput_byte(m68k_areg(regs, dstreg),((val >> 4) & 0xf0) | (val & 0xf));\n");
2289 	}
2290 	break;
2291      case i_UNPK:
2292 	if (curi->smode == Dreg) {
2293 	    printf ("\tuae_u16 val = m68k_dreg(regs, srcreg);\n");
2294 	    printf ("\tval = (((val << 4) & 0xf00) | (val & 0xf)) + %s;\n", gen_nextiword ());
2295 	    printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & 0xffff0000) | (val & 0xffff);\n");
2296 	} else {
2297 	    printf ("\tuae_u16 val;\n");
2298 	    printf ("\tval = (uae_u16)get_byte(m68k_areg(regs, srcreg) - areg_byteinc[srcreg]);\n");
2299 	    printf ("\tval = (((val << 4) & 0xf00) | (val & 0xf)) + %s;\n", gen_nextiword ());
2300 	    printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2301 	    printf ("\tm68k_areg(regs, dstreg) -= 2;\n");
2302 	    gen_set_fault_pc ();
2303 	    printf ("\tput_word(m68k_areg(regs, dstreg), val);\n");
2304 	}
2305 	break;
2306      case i_TAS:
2307 	genamode (curi->smode, "srcreg", curi->size, "src", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2308 	genflags (flag_logical, curi->size, "src", "", "");
2309 	printf ("\tsrc |= 0x80;\n");
2310 	genastore ("src", curi->smode, "srcreg", curi->size, "src", XLATE_LOG);
2311 	break;
2312      case i_FPP:
2313 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2314 	sync_m68k_pc ();
2315 	swap_opcode ();
2316 	printf ("\tfpuop_arithmetic(opcode, extra);\n");
2317 	break;
2318      case i_FDBcc:
2319 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2320 	sync_m68k_pc ();
2321 	swap_opcode ();
2322 	printf ("\tfpuop_dbcc(opcode, extra);\n");
2323 	break;
2324      case i_FScc:
2325 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2326 	sync_m68k_pc ();
2327 	swap_opcode ();
2328 	printf ("\tfpuop_scc(opcode, extra);\n");
2329 	break;
2330      case i_FTRAPcc:
2331 	sync_m68k_pc ();
2332 	start_brace ();
2333 	printf ("\tuaecptr oldpc = m68k_getpc();\n");
2334 	printf ("\tuae_u16 extra = %s;\n", gen_nextiword());
2335 	if (curi->smode != am_unknown && curi->smode != am_illg)
2336 	    genamode (curi->smode, "srcreg", curi->size, "dummy", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2337 	sync_m68k_pc ();
2338 	swap_opcode ();
2339 	printf ("\tfpuop_trapcc(opcode, oldpc, extra);\n");
2340 	break;
2341      case i_FBcc:
2342 	sync_m68k_pc ();
2343 	start_brace ();
2344 	printf ("\tuaecptr pc = m68k_getpc();\n");
2345 	genamode (curi->dmode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2346 	sync_m68k_pc ();
2347 	swap_opcode ();
2348 	printf ("\tfpuop_bcc(opcode, pc, extra);\n");
2349 	break;
2350      case i_FSAVE:
2351 	sync_m68k_pc ();
2352 	swap_opcode ();
2353 	printf ("\tfpuop_save(opcode);\n");
2354 	break;
2355      case i_FRESTORE:
2356 	sync_m68k_pc ();
2357 	swap_opcode ();
2358 	printf ("\tfpuop_restore(opcode);\n");
2359 	break;
2360      case i_CINVL:
2361 	printf ("\tflush_internals();\n");
2362         printf("#ifdef USE_JIT\n");
2363  	printf ("\tif (opcode&0x80)\n"
2364 		"\t\tflush_icache(31);\n");
2365 	printf("#endif\n");
2366 	break;
2367      case i_CINVP:
2368 	printf ("\tflush_internals();\n");
2369         printf("#ifdef USE_JIT\n");
2370 	printf ("\tif (opcode&0x80)\n"
2371 		"\t\tflush_icache(32);\n");
2372 	printf("#endif\n");
2373 	break;
2374      case i_CINVA:
2375 	printf ("\tflush_internals();\n");
2376         printf("#ifdef USE_JIT\n");
2377 	printf ("\tif (opcode&0x80)\n"
2378 		"\t\tflush_icache(33);\n");
2379 	printf("#endif\n");
2380 	break;
2381      case i_CPUSHL:
2382 	printf ("\tflush_internals();\n");
2383         printf("#ifdef USE_JIT\n");
2384 	printf ("\tif (opcode&0x80)\n"
2385 		"\t\tflush_icache(41);\n");
2386 	printf("#endif\n");
2387 	break;
2388      case i_CPUSHP:
2389 	printf ("\tflush_internals();\n");
2390         printf("#ifdef USE_JIT\n");
2391 	printf ("\tif (opcode&0x80)\n"
2392 		"\t\tflush_icache(42);\n");
2393 	printf("#endif\n");
2394 	break;
2395      case i_CPUSHA:
2396 	printf ("\tflush_internals();\n");
2397         printf("#ifdef USE_JIT\n");
2398 	printf ("\tif (opcode&0x80)\n"
2399 		"\t\tflush_icache(43);\n");
2400 	printf("#endif\n");
2401 	break;
2402      case i_MOVE16:
2403 	 if ((opcode & 0xfff8) == 0xf620) {
2404 	     /* MOVE16 (Ax)+,(Ay)+ */
2405 	     printf ("\tuaecptr mems = m68k_areg(regs, srcreg) & ~15, memd;\n");
2406 	     printf ("\tdstreg = (%s >> 12) & 7;\n", gen_nextiword());
2407 	     printf ("\tmemd = m68k_areg(regs, dstreg) & ~15;\n");
2408 	     printf ("\tput_long(memd, get_long(mems));\n");
2409 	     printf ("\tput_long(memd+4, get_long(mems+4));\n");
2410 	     printf ("\tput_long(memd+8, get_long(mems+8));\n");
2411 	     printf ("\tput_long(memd+12, get_long(mems+12));\n");
2412 	     printf ("\tif (srcreg != dstreg)\n");
2413 	     printf ("\tm68k_areg(regs, srcreg) += 16;\n");
2414 	     printf ("\tm68k_areg(regs, dstreg) += 16;\n");
2415 	 } else {
2416 	     /* Other variants */
2417 	     genamode (curi->smode, "srcreg", curi->size, "mems", GENA_GETV_NO_FETCH, GENA_MOVEM_MOVE16, XLATE_LOG);
2418 	     genamode (curi->dmode, "dstreg", curi->size, "memd", GENA_GETV_NO_FETCH, GENA_MOVEM_MOVE16, XLATE_LOG);
2419 	     printf ("\tmemsa &= ~15;\n");
2420 	     printf ("\tmemda &= ~15;\n");
2421 	     printf ("\tput_long(memda, get_long(memsa));\n");
2422 	     printf ("\tput_long(memda+4, get_long(memsa+4));\n");
2423 	     printf ("\tput_long(memda+8, get_long(memsa+8));\n");
2424 	     printf ("\tput_long(memda+12, get_long(memsa+12));\n");
2425 	     if ((opcode & 0xfff8) == 0xf600)
2426                  printf ("\tm68k_areg(regs, srcreg) += 16;\n");
2427 	     else if ((opcode & 0xfff8) == 0xf608)
2428 		 printf ("\tm68k_areg(regs, dstreg) += 16;\n");
2429 	 }
2430 	 break;
2431      case i_MMUOP:
2432 	genamode (curi->smode, "srcreg", curi->size, "extra", GENA_GETV_FETCH, GENA_MOVEM_DO_INC, XLATE_LOG);
2433 	sync_m68k_pc ();
2434 	swap_opcode ();
2435 	printf ("\tmmu_op(opcode,extra);\n");
2436 	break;
2437 
2438      case i_EMULOP_RETURN:
2439 	printf ("\tm68k_emulop_return();\n");
2440 	m68k_pc_offset = 0;
2441 	break;
2442 
2443      case i_EMULOP:
2444 	printf ("\n");
2445 	swap_opcode ();
2446 	printf ("\tm68k_emulop(opcode);\n");
2447 	break;
2448 
2449      case i_NATFEAT_ID:
2450 	printf ("\n");
2451 	printf ("\tm68k_natfeat_id();\n");
2452 	break;
2453 
2454      case i_NATFEAT_CALL:
2455 	printf ("\n");
2456 	printf ("\tm68k_natfeat_call();\n");
2457 	break;
2458 
2459      default:
2460 	abort ();
2461 	break;
2462     }
2463     finish_braces ();
2464     sync_m68k_pc ();
2465 }
2466 
generate_includes(FILE * f)2467 static void generate_includes (FILE * f)
2468 {
2469     fprintf (f, "#include \"sysdeps.h\"\n");
2470     fprintf (f, "#include \"m68k.h\"\n");
2471     fprintf (f, "#include \"memory-uae.h\"\n");
2472     fprintf (f, "#include \"readcpu.h\"\n");
2473     fprintf (f, "#include \"newcpu.h\"\n");
2474     fprintf (f, "#ifdef USE_JIT\n");
2475     fprintf (f, "#include \"compiler/compemu.h\"\n");
2476     fprintf (f, "#endif\n");
2477     fprintf (f, "#include \"fpu/fpu.h\"\n");
2478     fprintf (f, "#include \"cputbl.h\"\n");
2479     fprintf (f, "#include \"cpu_emulation.h\"\n");
2480     fprintf (f, "#include \"debug.h\"\n");
2481 
2482     fprintf (f, "#define SET_CFLG_ALWAYS(x) SET_CFLG(x)\n");
2483     fprintf (f, "#define SET_NFLG_ALWAYS(x) SET_NFLG(x)\n");
2484     fprintf (f, "#define CPUFUNC_FF(x) x##_ff\n");
2485     fprintf (f, "#define CPUFUNC_NF(x) x##_nf\n");
2486     fprintf (f, "#define CPUFUNC(x) CPUFUNC_FF(x)\n");
2487 
2488     fprintf (f, "#ifdef NOFLAGS\n");
2489     fprintf (f, "# include \"noflags.h\"\n");
2490     fprintf (f, "#endif\n");
2491 }
2492 
2493 static int postfix;
2494 
2495 struct gencputbl {
2496     char handler[80];
2497     uae_u16 specific;
2498     uae_u16 opcode;
2499     int namei;
2500 };
2501 struct gencputbl cpustbl[65536];
2502 static int n_cpustbl;
2503 
decodeEA(amodes mode,wordsizes size)2504 static char *decodeEA (amodes mode, wordsizes size)
2505 {
2506 	static char buffer[80];
2507 
2508 	buffer[0] = 0;
2509 	switch (mode){
2510 	case Dreg:
2511 		strcpy (buffer,"Dn");
2512 		break;
2513 	case Areg:
2514 		strcpy (buffer,"An");
2515 		break;
2516 	case Aind:
2517 		strcpy (buffer,"(An)");
2518 		break;
2519 	case Aipi:
2520 		strcpy (buffer,"(An)+");
2521 		break;
2522 	case Apdi:
2523 		strcpy (buffer,"-(An)");
2524 		break;
2525 	case Ad16:
2526 		strcpy (buffer,"(d16,An)");
2527 		break;
2528 	case Ad8r:
2529 		strcpy (buffer,"(d8,An,Xn)");
2530 		break;
2531 	case PC16:
2532 		strcpy (buffer,"(d16,PC)");
2533 		break;
2534 	case PC8r:
2535 		strcpy (buffer,"(d8,PC,Xn)");
2536 		break;
2537 	case absw:
2538 		strcpy (buffer,"(xxx).W");
2539 		break;
2540 	case absl:
2541 		strcpy (buffer,"(xxx).L");
2542 		break;
2543 	case imm:
2544 		switch (size){
2545 		case sz_byte:
2546 			strcpy (buffer,"#<data>.B");
2547 			break;
2548 		case sz_word:
2549 			strcpy (buffer,"#<data>.W");
2550 			break;
2551 		case sz_long:
2552 			strcpy (buffer,"#<data>.L");
2553 			break;
2554 		default:
2555 			break;
2556 		}
2557 		break;
2558 	case imm0:
2559 		strcpy (buffer,"#<data>.B");
2560 		break;
2561 	case imm1:
2562 		strcpy (buffer,"#<data>.W");
2563 		break;
2564 	case imm2:
2565 		strcpy (buffer,"#<data>.L");
2566 		break;
2567 	case immi:
2568 		strcpy (buffer,"#<data>");
2569 		break;
2570 
2571 	default:
2572 		break;
2573 	}
2574 	return buffer;
2575 }
2576 
outopcode(const char * name,int opcode)2577 static char *outopcode (const char *name, int opcode)
2578 {
2579 	static char out[100];
2580 	struct instr *ins;
2581 
2582 	ins = &table68k[opcode];
2583 	strcpy (out, name);
2584 	if (ins->smode == immi)
2585 		strcat (out, "Q");
2586 	if (ins->size == sz_byte)
2587 		strcat (out,".B");
2588 	if (ins->size == sz_word)
2589 		strcat (out,".W");
2590 	if (ins->size == sz_long)
2591 		strcat (out,".L");
2592 	strcat (out," ");
2593 	if (ins->suse)
2594 		strcat (out, decodeEA (ins->smode, ins->size));
2595 	if (ins->duse) {
2596 		if (ins->suse) strcat (out,",");
2597 		strcat (out, decodeEA (ins->dmode, ins->size));
2598 	}
2599 	return out;
2600 }
2601 
2602 
generate_one_opcode(int rp)2603 static void generate_one_opcode (int rp)
2604 {
2605     int i;
2606     uae_u16 smsk, dmsk;
2607     int opcode = opcode_map[rp];
2608 	int have_realopcode = 0;
2609 	const char *name;
2610 
2611     if (table68k[opcode].mnemo == i_ILLG
2612 	|| table68k[opcode].clev > cpu_level)
2613 	return;
2614 
2615     for (i = 0; lookuptab[i].name[0]; i++) {
2616 	if (table68k[opcode].mnemo == lookuptab[i].mnemo)
2617 	    break;
2618     }
2619 
2620     if (table68k[opcode].handler != -1)
2621 	return;
2622 
2623     name = lookuptab[i].name;
2624     if (opcode_next_clev[rp] != cpu_level) {
2625     	sprintf(cpustbl[n_cpustbl].handler, "CPUFUNC(op_%x_%d)", opcode, opcode_last_postfix[rp]);
2626     	cpustbl[n_cpustbl].specific = 0;
2627     	cpustbl[n_cpustbl].opcode = opcode;
2628     	cpustbl[n_cpustbl].namei = i;
2629 	fprintf (stblfile, "{ %s, %d, %d }, /* %s */\n", cpustbl[n_cpustbl].handler, cpustbl[n_cpustbl].specific, opcode, name);
2630 	n_cpustbl++;
2631 	return;
2632     }
2633 
2634 	if (table68k[opcode].flagdead == 0)
2635 	/* force to the "ff" variant since the instruction doesn't set at all the condition codes */
2636     sprintf (cpustbl[n_cpustbl].handler, "CPUFUNC_FF(op_%x_%d)", opcode, postfix);
2637 	else
2638     sprintf (cpustbl[n_cpustbl].handler, "CPUFUNC(op_%x_%d)", opcode, postfix);
2639    	cpustbl[n_cpustbl].specific = 0;
2640    	cpustbl[n_cpustbl].opcode = opcode;
2641    	cpustbl[n_cpustbl].namei = i;
2642 	fprintf (stblfile, "{ %s, %d, %d }, /* %s */\n", cpustbl[n_cpustbl].handler, cpustbl[n_cpustbl].specific, opcode, name);
2643 	n_cpustbl++;
2644 
2645     fprintf (headerfile, "extern cpuop_func op_%x_%d_nf;\n", opcode, postfix);
2646     fprintf (headerfile, "extern cpuop_func op_%x_%d_ff;\n", opcode, postfix);
2647 
2648     printf ("/* %s */\n", outopcode (name, opcode));
2649     printf ("void REGPARAM2 CPUFUNC(op_%x_%d)(uae_u32 opcode) /* %s */\n{\n", opcode, postfix, name);
2650     printf ("\tcpuop_begin();\n");
2651 	/* gb-- The "nf" variant for an instruction that doesn't set the condition
2652 	   codes at all is the same as the "ff" variant, so we don't need the "nf"
2653 	   variant to be compiled since it is mapped to the "ff" variant in the
2654 	   smalltbl. */
2655     if (table68k[opcode].flagdead == 0)
2656 	printf ("#ifndef NOFLAGS\n");
2657 
2658     switch (table68k[opcode].stype) {
2659      case 0: smsk = 7; break;
2660      case 1: smsk = 255; break;
2661      case 2: smsk = 15; break;
2662      case 3: smsk = 7; break;
2663      case 4: smsk = 7; break;
2664      case 5: smsk = 63; break;
2665      case 6: smsk = 255; break;
2666      case 7: smsk = 3; break;
2667      default: abort ();
2668     }
2669     dmsk = 7;
2670 
2671     next_cpu_level = -1;
2672     if (table68k[opcode].suse
2673 	&& table68k[opcode].smode != imm && table68k[opcode].smode != imm0
2674 	&& table68k[opcode].smode != imm1 && table68k[opcode].smode != imm2
2675 	&& table68k[opcode].smode != absw && table68k[opcode].smode != absl
2676 	&& table68k[opcode].smode != PC8r && table68k[opcode].smode != PC16
2677 	/* gb-- We don't want to fetch the EmulOp code since the EmulOp()
2678 	   routine uses the whole opcode value. Maybe all the EmulOps
2679 	   could be expanded out but I don't think it is an improvement */
2680 	&& table68k[opcode].stype != 6
2681 	)
2682     {
2683 	if (table68k[opcode].spos == -1) {
2684 	    if (((int) table68k[opcode].sreg) >= 128)
2685 		printf ("\tuae_u32 srcreg = (uae_s32)(uae_s8)%d;\n", (int) table68k[opcode].sreg);
2686 	    else
2687 		printf ("\tuae_u32 srcreg = %d;\n", (int) table68k[opcode].sreg);
2688 	} else {
2689 	    char source[100];
2690 	    int pos = table68k[opcode].spos;
2691 
2692 #if 0
2693 	    /* Check that we can do the little endian optimization safely.  */
2694 	    if (pos < 8 && (smsk >> (8 - pos)) != 0)
2695 		abort ();
2696 #endif
2697 		real_opcode(&have_realopcode);
2698 	    if (pos)
2699 		sprintf (source, "((real_opcode >> %d) & %d)", pos, smsk);
2700 	    else
2701 		sprintf (source, "(real_opcode & %d)", smsk);
2702 	    if (table68k[opcode].stype == 3)
2703 		printf ("\tuae_u32 srcreg = imm8_table[%s];\n", source);
2704 	    else if (table68k[opcode].stype == 1)
2705 		printf ("\tuae_u32 srcreg = (uae_s32)(uae_s8)%s;\n", source);
2706 	    else
2707 		printf ("\tuae_u32 srcreg = %s;\n", source);
2708 	}
2709     }
2710     if (table68k[opcode].duse
2711 	/* Yes, the dmode can be imm, in case of LINK or DBcc */
2712 	&& table68k[opcode].dmode != imm && table68k[opcode].dmode != imm0
2713 	&& table68k[opcode].dmode != imm1 && table68k[opcode].dmode != imm2
2714 	&& table68k[opcode].dmode != absw && table68k[opcode].dmode != absl)
2715     {
2716 	if (table68k[opcode].dpos == -1) {
2717 	    if (((int) table68k[opcode].dreg) >= 128)
2718 		printf ("\tuae_u32 dstreg = (uae_s32)(uae_s8)%d;\n", (int) table68k[opcode].dreg);
2719 	    else
2720 		printf ("\tuae_u32 dstreg = %d;\n", (int) table68k[opcode].dreg);
2721 	} else {
2722 	    int pos = table68k[opcode].dpos;
2723 #if 0
2724 	    /* Check that we can do the little endian optimization safely.  */
2725 	    if (pos < 8 && (dmsk >> (8 - pos)) != 0)
2726 		abort ();
2727 #endif
2728 		real_opcode(&have_realopcode);
2729 	    if (pos)
2730 		printf ("\tuae_u32 dstreg = (real_opcode >> %d) & %d;\n",
2731 			pos, dmsk);
2732 	    else
2733 		printf ("\tuae_u32 dstreg = real_opcode & %d;\n", dmsk);
2734 	}
2735     }
2736     need_endlabel = 0;
2737     endlabelno++;
2738     sprintf (endlabelstr, "endlabel%d", endlabelno);
2739     gen_opcode (opcode);
2740     if (need_endlabel)
2741 	printf ("%s: ;\n", endlabelstr);
2742     if (table68k[opcode].flagdead == 0)
2743 	printf ("\n#endif\n");
2744     printf ("\tcpuop_end();\n");
2745     printf ("}\n");
2746     opcode_next_clev[rp] = next_cpu_level;
2747     opcode_last_postfix[rp] = postfix;
2748 }
2749 
generate_func(void)2750 static void generate_func (void)
2751 {
2752     int i, j, rp;
2753 
2754     using_prefetch = 0;
2755     using_exception_3 = 0;
2756 
2757     for (i = 0; i < 1; i++) {
2758 	cpu_level = 4 - i;
2759 	postfix = i;
2760 	fprintf (stblfile, "const struct cputbl CPUFUNC(op_smalltbl_%d)[] = {\n", postfix);
2761 
2762 	/* sam: this is for people with low memory (eg. me :)) */
2763 	printf ("\n"
2764 		"#if !defined(PART_1) && !defined(PART_2) && "
2765 	 	"!defined(PART_3) && !defined(PART_4) && "
2766 		"!defined(PART_5) && !defined(PART_6) && "
2767 		"!defined(PART_7) && !defined(PART_8)"
2768 		"\n"
2769 	        "#define PART_1 1\n"
2770 	        "#define PART_2 1\n"
2771 	        "#define PART_3 1\n"
2772 	        "#define PART_4 1\n"
2773 	        "#define PART_5 1\n"
2774 	        "#define PART_6 1\n"
2775 	        "#define PART_7 1\n"
2776 	        "#define PART_8 1\n"
2777 	        "#endif\n\n");
2778 	rp = 0;
2779 	n_cpustbl = 0;
2780 	for(j=1;j<=8;++j) {
2781 		int k = (j*nr_cpuop_funcs)/8;
2782 		printf ("#ifdef PART_%d\n",j);
2783 		for (; rp < k; rp++)
2784 		   generate_one_opcode (rp);
2785 		printf ("#endif\n\n");
2786 	}
2787 	fprintf (stblfile, "{ 0, 0, 0 }};\n");
2788     }
2789 }
2790 
2791 static struct {
2792 	const char *handler;
2793 	const char *name;
2794 } cpufunctbl[65536];
2795 static char const op_illg_1[] = "op_illg_1";
2796 static char const illegal[] = "ILLEGAL";
2797 
generate_functbl(void)2798 static void generate_functbl (void)
2799 {
2800 	int i;
2801 	unsigned int opcode;
2802 	int cpu_level = 4;
2803 	struct gencputbl *tbl = cpustbl;
2804 
2805 	for (opcode = 0; opcode < 65536; opcode++)
2806 	{
2807 		cpufunctbl[opcode].handler = op_illg_1;
2808 		cpufunctbl[opcode].name = illegal;
2809 	}
2810 	for (i = 0; i < n_cpustbl; i++)
2811 	{
2812 		if (! tbl[i].specific)
2813 		{
2814 			cpufunctbl[tbl[i].opcode].handler = tbl[i].handler;
2815 			cpufunctbl[tbl[i].opcode].name = lookuptab[tbl[i].namei].name;
2816 		}
2817 	}
2818 	for (opcode = 0; opcode < 65536; opcode++)
2819 	{
2820 		const char *f;
2821 
2822 		if (table68k[opcode].mnemo == i_ILLG || (unsigned)table68k[opcode].clev > (unsigned)cpu_level)
2823 			continue;
2824 
2825 		if (table68k[opcode].handler != -1)
2826 		{
2827 			f = cpufunctbl[table68k[opcode].handler].handler;
2828 			if (f == op_illg_1)
2829 				abort();
2830 			cpufunctbl[opcode].handler = f;
2831 			cpufunctbl[opcode].name = cpufunctbl[table68k[opcode].handler].name;
2832 		}
2833 	}
2834 	for (i = 0; i < n_cpustbl; i++)
2835 	{
2836 		if (tbl[i].specific)
2837 		{
2838 			cpufunctbl[tbl[i].opcode].handler = tbl[i].handler;
2839 			cpufunctbl[tbl[i].opcode].name = lookuptab[tbl[i].namei].name;
2840 		}
2841 	}
2842 
2843 	fprintf(functblfile, "\n");
2844 	fprintf(functblfile, "cpuop_func *cpufunctbl[65536] = {\n");
2845 	fprintf(functblfile, "#if !defined(HAVE_GET_WORD_UNSWAPPED) || defined(FULLMMU)\n");
2846 	for (opcode = 0; opcode < 65536; opcode++)
2847 	{
2848 		fprintf(functblfile, "\t%s%s /* %s */\n", cpufunctbl[opcode].handler, opcode < 65535 ? "," : "", cpufunctbl[opcode].name);
2849 	}
2850 	fprintf(functblfile, "#else\n");
2851 	for (opcode = 0; opcode < 65536; opcode++)
2852 	{
2853 		unsigned int map = do_byteswap_16(opcode);
2854 		fprintf(functblfile, "\t%s%s /* %s */\n", cpufunctbl[map].handler, opcode < 65535 ? "," : "", cpufunctbl[map].name);
2855 	}
2856 	fprintf(functblfile, "#endif\n");
2857 	fprintf(functblfile, "};\n");
2858 }
2859 
2860 #if (defined(OS_cygwin) || defined(OS_mingw)) && defined(EXTENDED_SIGSEGV)
cygwin_mingw_abort()2861 void cygwin_mingw_abort()
2862 {
2863 #undef abort
2864 	abort();
2865 }
2866 #endif
2867 
main(void)2868 int main(void)
2869 {
2870     init_table68k ();
2871 
2872     opcode_map = (int *) malloc (sizeof (int) * nr_cpuop_funcs);
2873     opcode_last_postfix = (int *) malloc (sizeof (int) * nr_cpuop_funcs);
2874     opcode_next_clev = (int *) malloc (sizeof (int) * nr_cpuop_funcs);
2875     counts = (unsigned long *) malloc (65536 * sizeof (unsigned long));
2876     read_counts ();
2877 
2878     /* It would be a lot nicer to put all in one file (we'd also get rid of
2879      * cputbl.h that way), but cpuopti can't cope.  That could be fixed, but
2880      * I don't dare to touch the 68k version.  */
2881 
2882     if ((headerfile = fopen ("cputbl.h", "wb")) == NULL)
2883     	abort();
2884     if ((stblfile = fopen ("cpustbl.cpp", "wb")) == NULL)
2885     	abort();
2886     if ((functblfile = fopen ("cpufunctbl.cpp", "wb")) == NULL)
2887     	abort();
2888     if (freopen ("cpuemu.cpp", "wb", stdout) == NULL)
2889     	abort();
2890 
2891     generate_includes (stdout);
2892     fprintf(stdout, "#ifdef HAVE_CFLAG_NO_REDZONE\n");
2893     fprintf(stdout, "#ifndef NOFLAGS\n");
2894     fprintf(stdout, "#pragma GCC option \"-mno-red-zone\"\n");
2895     fprintf(stdout, "#endif\n");
2896     fprintf(stdout, "#endif\n");
2897     generate_includes (stblfile);
2898     generate_includes (functblfile);
2899     generate_func ();
2900     generate_functbl ();
2901     free (table68k);
2902     fclose(headerfile);
2903     fclose(stblfile);
2904     fclose(functblfile);
2905     return 0;
2906 }
2907