2 * UAE - The Un*x Amiga Emulator - CPU core
4 * MC68000 emulation generator
6 * This is a fairly stupid program that generates a lot of case labels that
7 * can be #included in a switch statement.
8 * As an alternative, it can generate functions that handle specific
9 * MC68000 instructions, plus a prototype header file and a function pointer
10 * array to look up the function for an opcode.
11 * Error checking is bad, an illegal table68k file will cause the program to
13 * The generated code is sometimes sub-optimal, an optimizing compiler should
16 * The source for the insn timings is Markt & Technik's Amiga Magazin 8/1992.
18 * Copyright 1995, 1996, 1997, 1998, 1999, 2000 Bernd Schmidt
20 * Adaptation to Hatari and better cpu timings by Thomas Huth
21 * Adaptation to Virtual Jaguar by James Hammons
23 * This file is distributed under the GNU Public License, version 3 or at
24 * your option any later version. Read the file GPLv3 for details.
29 /* 2007/03/xx [NP] Use add_cycles.pl to set 'CurrentInstrCycles' in each opcode. */
30 /* 2007/04/09 [NP] Correct CLR : on 68000, CLR reads the memory before clearing it (but we should */
31 /* not add cycles for reading). This means CLR can give 2 wait states (one for */
32 /* read and one for right) (clr.b $fa1b.w in Decade's Demo Main Menu). */
33 /* 2007/04/14 [NP] - Although dest -(an) normally takes 2 cycles, this is not the case for move : */
34 /* move dest (an), (an)+ and -(an) all take the same time (curi->dmode == Apdi) */
35 /* (Syntax Terror Demo Reset). */
36 /* - Scc takes 6 cycles instead of 4 if the result is true (Ventura Demo Loader). */
37 /* - Store the family of the current opcode into OpcodeFamily : used to check */
38 /* instruction pairing on ST into m68000.c */
39 /* 2007/04/17 [NP] Add support for cycle accurate MULU (No Cooper Greeting Screen). */
40 /* 2007/04/24 [NP] BCLR #n,Dx takes 12 cycles instead of 14 if n<16 (ULM Demo Menu). */
41 /* 2007/04/25 [NP] On ST, d8(An,Xn) and d8(PC,Xn) take 2 cycles more than the official 68000's */
42 /* table (ULM Demo Menu). */
43 /* 2007/11/12 [NP] Add refill_prefetch for i_ADD to fix Transbeauce 2 demo self modified code. */
44 /* Ugly hack, we need better prefetch emulation (switch to winuae gencpu.c) */
45 /* 2007/11/25 [NP] In i_DBcc, in case of address error, last_addr_for_exception_3 should be */
46 /* pc+4, not pc+2 (Transbeauce 2 demo) (e.g. 'dbf d0,#$fff5'). */
47 /* This means the value pushed on the frame stack should be the address of the */
48 /* instruction following the one generating the address error. */
49 /* FIXME : this should be the case for i_BSR and i_BCC too (need to check on */
51 /* 2007/11/28 [NP] Backport DIVS/DIVU cycles exact routines from WinUAE (original work by Jorge */
52 /* Cwik, pasti@fxatari.com). */
53 /* 2007/12/08 [NP] In case of CHK/CHK2 exception, PC stored on the stack wasn't pointing to the */
54 /* next instruction but to the current CHK/CHK2 instruction (Transbeauce 2 demo). */
55 /* We need to call 'sync_m68k_pc' before calling 'Exception'. */
56 /* 2007/12/09 [NP] CHK.L (e.g. $4700) doesn't exist on 68000 and should be considered as an illegal*/
57 /* instruction (Transbeauce 2 demo) -> change in table68k. */
58 /* 2008/01/24 [NP] BCLR Dy,Dx takes 8 cycles instead of 10 if Dy<16 (Fullshade in Anomaly Demos). */
59 /* 2008/01/26 [NP] On ST, d8(An,Xn) takes 2 cycles more when used with ADDA/SUBA (ULM Demo Menu) */
60 /* but not when used with MOVE (e.g. 'move.l 0(a5,d1),(a4)' takes 26 cycles and so */
61 /* can pair with a lsr) (Anomaly Demo Intro). */
62 /* 2008/04/26 [NP] Handle sz_byte for Areg in genamode, as 'move.b a1,(a0)' ($1089) is possible */
63 /* on ST (fix Blood Money on Superior 65) */
64 /* 2010/04/05 [NP] On ST, d8(An,Xn) takes 2 cycles more (which can generate pairing). */
65 /* Use BusCyclePenalty to properly handle the 2/4 cycles added in that case when */
66 /* addressing mode is Ad8r or PC8r (ULM Demo Menu, Anomaly Demo Intro, DHS */
67 /* Sommarhack 2010) (see m68000.h) */
70 //const char GenCpu_fileid[] = "Hatari gencpu.c : " __DATE__ " " __TIME__;
78 #define BOOL_TYPE "int"
80 static FILE *headerfile;
81 static FILE *stblfile;
83 static int using_prefetch;
84 static int using_exception_3;
87 char exactCpuCycles[256]; /* Space to store return string for exact cpu cycles */
89 long nCurInstrCycPos; /* Stores where we have to patch in the current cycles value */
91 /* For the current opcode, the next lower level that will have different code.
92 * Initialized to -1 for each opcode. If it remains unchanged, indicates we
93 * are done with that opcode. */
94 static int next_cpu_level;
95 static int *opcode_map;
96 static int *opcode_next_clev;
97 static int *opcode_last_postfix;
98 static unsigned long *counts;
101 static void read_counts (void)
104 unsigned long opcode, count, total;
107 memset (counts, 0, 65536 * sizeof *counts);
109 file = fopen ("frequent.68k", "r");
111 if (fscanf (file, "Total: %lu\n", &total) == EOF) {
112 perror("read_counts");
114 while (fscanf (file, "%lx: %lu %s\n", &opcode, &count, name) == 3) {
115 opcode_next_clev[nr] = 4;
116 opcode_last_postfix[nr] = -1;
117 opcode_map[nr++] = opcode;
118 counts[opcode] = count;
122 if (nr == nr_cpuop_funcs)
124 for (opcode = 0; opcode < 0x10000; opcode++) {
125 if (table68k[opcode].handler == -1 && table68k[opcode].mnemo != i_ILLG
126 && counts[opcode] == 0)
128 opcode_next_clev[nr] = 4;
129 opcode_last_postfix[nr] = -1;
130 opcode_map[nr++] = opcode;
131 counts[opcode] = count;
134 if (nr != nr_cpuop_funcs)
138 static char endlabelstr[80];
139 static int endlabelno = 0;
140 static int need_endlabel;
142 static int n_braces = 0;
143 static int m68k_pc_offset = 0;
144 static int insn_n_cycles;
146 static void start_brace (void)
152 static void close_brace (void)
154 assert (n_braces > 0);
159 static void finish_braces (void)
165 static void pop_braces (int to)
167 while (n_braces > to)
171 static int bit_size (int size)
174 case sz_byte: return 8;
175 case sz_word: return 16;
176 case sz_long: return 32;
182 static const char *bit_mask (int size)
185 case sz_byte: return "0xff";
186 case sz_word: return "0xffff";
187 case sz_long: return "0xffffffff";
193 static const char *gen_nextilong (void)
195 static char buffer[80];
196 int r = m68k_pc_offset;
202 sprintf (buffer, "get_ilong_prefetch(%d)", r);
204 sprintf (buffer, "get_ilong(%d)", r);
208 static const char *gen_nextiword (void)
210 static char buffer[80];
211 int r = m68k_pc_offset;
217 sprintf (buffer, "get_iword_prefetch(%d)", r);
219 sprintf (buffer, "get_iword(%d)", r);
223 static const char *gen_nextibyte (void)
225 static char buffer[80];
226 int r = m68k_pc_offset;
232 sprintf (buffer, "get_ibyte_prefetch(%d)", r);
234 sprintf (buffer, "get_ibyte(%d)", r);
238 static void fill_prefetch_0 (void)
241 printf ("fill_prefetch_0 ();\n");
244 static void fill_prefetch_2 (void)
247 printf ("fill_prefetch_2 ();\n");
250 static void sync_m68k_pc(void)
252 if (m68k_pc_offset == 0)
255 printf("m68k_incpc(%d);\n", m68k_pc_offset);
257 switch (m68k_pc_offset)
260 /*fprintf (stderr, "refilling prefetch at 0\n"); */
273 /* getv == 1: fetch data; getv != 0: check for odd address. If movem != 0,
274 * the calling routine handles Apdi and Aipi modes.
275 * gb-- movem == 2 means the same thing but for a MOVE16 instruction */
276 static void genamode(amodes mode, const char * reg, wordsizes size,
277 const char * name, int getv, int movem)
288 printf ("\tint8_t %s = m68k_dreg(regs, %s);\n", name, reg);
291 printf ("\tint16_t %s = m68k_dreg(regs, %s);\n", name, reg);
294 printf ("\tint32_t %s = m68k_dreg(regs, %s);\n", name, reg);
305 case sz_byte: // [NP] Areg with .b is possible in MOVE source */
306 printf ("\tint8_t %s = m68k_areg(regs, %s);\n", name, reg);
309 printf ("\tint16_t %s = m68k_areg(regs, %s);\n", name, reg);
312 printf ("\tint32_t %s = m68k_areg(regs, %s);\n", name, reg);
319 printf ("\tuint32_t %sa = m68k_areg(regs, %s);\n", name, reg);
322 printf ("\tuint32_t %sa = m68k_areg(regs, %s);\n", name, reg);
329 printf ("\tuint32_t %sa = m68k_areg(regs, %s);\n", name, reg);
331 printf ("\tuint32_t %sa = m68k_areg(regs, %s) - areg_byteinc[%s];\n", name, reg, reg);
334 printf ("\tuint32_t %sa = m68k_areg(regs, %s) - %d;\n", name, reg, movem ? 0 : 2);
337 printf ("\tuint32_t %sa = m68k_areg(regs, %s) - %d;\n", name, reg, movem ? 0 : 4);
344 printf ("\tuint32_t %sa = m68k_areg(regs, %s) + (int32_t)(int16_t)%s;\n", name, reg, gen_nextiword ());
349 if (next_cpu_level < 1)
353 /* This would ordinarily be done in gen_nextiword, which we bypass. */
355 printf ("\tuint32_t %sa = get_disp_ea_020(m68k_areg(regs, %s), next_iword());\n", name, reg);
357 printf ("\tuint32_t %sa = get_disp_ea_000(m68k_areg(regs, %s), %s);\n", name, reg, gen_nextiword ());
359 printf ("\tBusCyclePenalty += 2;\n");
363 printf ("\tuint32_t %sa = m68k_getpc () + %d;\n", name, m68k_pc_offset);
364 printf ("\t%sa += (int32_t)(int16_t)%s;\n", name, gen_nextiword ());
369 if (next_cpu_level < 1)
373 /* This would ordinarily be done in gen_nextiword, which we bypass. */
375 printf ("\tuint32_t tmppc = m68k_getpc();\n");
376 printf ("\tuint32_t %sa = get_disp_ea_020(tmppc, next_iword());\n", name);
378 printf ("\tuint32_t tmppc = m68k_getpc() + %d;\n", m68k_pc_offset);
379 printf ("\tuint32_t %sa = get_disp_ea_000(tmppc, %s);\n", name, gen_nextiword ());
381 printf ("\tBusCyclePenalty += 2;\n");
385 printf ("\tuint32_t %sa = (int32_t)(int16_t)%s;\n", name, gen_nextiword ());
388 printf ("\tuint32_t %sa = %s;\n", name, gen_nextilong ());
395 printf ("\tint8_t %s = %s;\n", name, gen_nextibyte ());
398 printf ("\tint16_t %s = %s;\n", name, gen_nextiword ());
401 printf ("\tint32_t %s = %s;\n", name, gen_nextilong ());
410 printf ("\tint8_t %s = %s;\n", name, gen_nextibyte ());
415 printf ("\tint16_t %s = %s;\n", name, gen_nextiword ());
420 printf ("\tint32_t %s = %s;\n", name, gen_nextilong ());
425 printf ("\tuint32_t %s = %s;\n", name, reg);
431 /* We get here for all non-reg non-immediate addressing modes to
432 * actually fetch the value. */
434 if (using_exception_3 && getv != 0 && size != sz_byte) {
435 printf ("\tif ((%sa & 1) != 0) {\n", name);
436 printf ("\t\tlast_fault_for_exception_3 = %sa;\n", name);
437 printf ("\t\tlast_op_for_exception_3 = opcode;\n");
438 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + %d;\n", m68k_pc_offset);
439 printf ("\t\tException(3, 0, M68000_EXC_SRC_CPU);\n");
440 printf ("\t\tgoto %s;\n", endlabelstr);
448 case sz_byte: insn_n_cycles += 4; break;
449 case sz_word: insn_n_cycles += 4; break;
450 case sz_long: insn_n_cycles += 8; break;
455 case sz_byte: printf ("\tint8_t %s = m68k_read_memory_8(%sa);\n", name, name); break;
456 case sz_word: printf ("\tint16_t %s = m68k_read_memory_16(%sa);\n", name, name); break;
457 case sz_long: printf ("\tint32_t %s = m68k_read_memory_32(%sa);\n", name, name); break;
462 /* We now might have to fix up the register for pre-dec or post-inc
463 * addressing modes. */
469 printf ("\tm68k_areg(regs, %s) += areg_byteinc[%s];\n", reg, reg);
472 printf ("\tm68k_areg(regs, %s) += 2;\n", reg);
475 printf ("\tm68k_areg(regs, %s) += 4;\n", reg);
482 printf ("\tm68k_areg (regs, %s) = %sa;\n", reg, name);
489 static void genastore (const char *from, amodes mode, const char *reg,
490 wordsizes size, const char *to)
496 printf ("\tm68k_dreg(regs, %s) = (m68k_dreg(regs, %s) & ~0xff) | ((%s) & 0xff);\n", reg, reg, from);
499 printf ("\tm68k_dreg(regs, %s) = (m68k_dreg(regs, %s) & ~0xffff) | ((%s) & 0xffff);\n", reg, reg, from);
502 printf ("\tm68k_dreg(regs, %s) = (%s);\n", reg, from);
511 fprintf (stderr, "Foo\n");
512 printf ("\tm68k_areg(regs, %s) = (int32_t)(int16_t)(%s);\n", reg, from);
515 printf ("\tm68k_areg(regs, %s) = (%s);\n", reg, from);
535 printf ("\tm68k_write_memory_8(%sa,%s);\n", to, from);
539 if (cpu_level < 2 && (mode == PC16 || mode == PC8r))
541 printf ("\tm68k_write_memory_16(%sa,%s);\n", to, from);
545 if (cpu_level < 2 && (mode == PC16 || mode == PC8r))
547 printf ("\tm68k_write_memory_32(%sa,%s);\n", to, from);
566 static void genmovemel (uint16_t opcode)
569 int bMovemLong = (table68k[opcode].size == sz_long);
570 int size = bMovemLong ? 4 : 2;
573 strcpy (getcode, "m68k_read_memory_32(srca)");
575 strcpy (getcode, "(int32_t)(int16_t)m68k_read_memory_16(srca)");
578 printf ("\tuint16_t mask = %s;\n", gen_nextiword ());
579 printf ("\tunsigned int dmask = mask & 0xff, amask = (mask >> 8) & 0xff;\n");
580 printf ("\tretcycles = 0;\n");
581 genamode (table68k[opcode].dmode, "dstreg", table68k[opcode].size, "src", 2, 1);
583 printf ("\twhile (dmask) { m68k_dreg(regs, movem_index1[dmask]) = %s;"
584 " srca += %d; dmask = movem_next[dmask]; retcycles+=%d; }\n",
585 getcode, size, (bMovemLong ? 8 : 4));
586 printf ("\twhile (amask) { m68k_areg(regs, movem_index1[amask]) = %s;"
587 " srca += %d; amask = movem_next[amask]; retcycles+=%d; }\n",
588 getcode, size, (bMovemLong ? 8 : 4));
590 if (table68k[opcode].dmode == Aipi)
591 printf ("\tm68k_areg(regs, dstreg) = srca;\n");
593 /* Better cycles - experimental! (Thothy) */
594 switch(table68k[opcode].dmode)
596 case Aind: insn_n_cycles=12; break;
597 case Aipi: insn_n_cycles=12; break;
598 case Ad16: insn_n_cycles=16; break;
599 case Ad8r: insn_n_cycles=18; break;
600 case absw: insn_n_cycles=16; break;
601 case absl: insn_n_cycles=20; break;
602 case PC16: insn_n_cycles=16; break;
603 case PC8r: insn_n_cycles=18; break;
605 sprintf(exactCpuCycles," return (%i+retcycles);", insn_n_cycles);
608 static void genmovemle (uint16_t opcode)
611 int bMovemLong = (table68k[opcode].size == sz_long);
612 int size = bMovemLong ? 4 : 2;
615 strcpy (putcode, "m68k_write_memory_32(srca,");
617 strcpy (putcode, "m68k_write_memory_16(srca,");
620 printf ("\tuint16_t mask = %s;\n", gen_nextiword ());
621 printf ("\tretcycles = 0;\n");
622 genamode (table68k[opcode].dmode, "dstreg", table68k[opcode].size, "src", 2, 1);
627 if (table68k[opcode].dmode == Apdi) {
628 printf ("\tuint16_t amask = mask & 0xff, dmask = (mask >> 8) & 0xff;\n");
629 printf ("\twhile (amask) { srca -= %d; %s m68k_areg(regs, movem_index2[amask]));"
630 " amask = movem_next[amask]; retcycles+=%d; }\n",
631 size, putcode, (bMovemLong ? 8 : 4));
632 printf ("\twhile (dmask) { srca -= %d; %s m68k_dreg(regs, movem_index2[dmask]));"
633 " dmask = movem_next[dmask]; retcycles+=%d; }\n",
634 size, putcode, (bMovemLong ? 8 : 4));
635 printf ("\tm68k_areg(regs, dstreg) = srca;\n");
637 printf ("\tuint16_t dmask = mask & 0xff, amask = (mask >> 8) & 0xff;\n");
638 printf ("\twhile (dmask) { %s m68k_dreg(regs, movem_index1[dmask])); srca += %d;"
639 " dmask = movem_next[dmask]; retcycles+=%d; }\n",
640 putcode, size, (bMovemLong ? 8 : 4));
641 printf ("\twhile (amask) { %s m68k_areg(regs, movem_index1[amask])); srca += %d;"
642 " amask = movem_next[amask]; retcycles+=%d; }\n",
643 putcode, size, (bMovemLong ? 8 : 4));
646 /* Better cycles - experimental! (Thothy) */
647 switch(table68k[opcode].dmode)
649 case Aind: insn_n_cycles=8; break;
650 case Apdi: insn_n_cycles=8; break;
651 case Ad16: insn_n_cycles=12; break;
652 case Ad8r: insn_n_cycles=14; break;
653 case absw: insn_n_cycles=12; break;
654 case absl: insn_n_cycles=16; break;
656 sprintf(exactCpuCycles," return (%i+retcycles);", insn_n_cycles);
660 static void duplicate_carry (void)
662 printf ("\tCOPY_CARRY;\n");
667 flag_logical_noclobber, flag_logical, flag_add, flag_sub, flag_cmp, flag_addx, flag_subx, flag_zn,
672 static void genflags_normal (flagtypes type, wordsizes size, const char *value,
673 const char *src, const char *dst)
675 char vstr[100], sstr[100], dstr[100];
676 char usstr[100], udstr[100];
677 char unsstr[100], undstr[100];
681 strcpy (vstr, "((int8_t)(");
682 strcpy (usstr, "((uint8_t)(");
685 strcpy (vstr, "((int16_t)(");
686 strcpy (usstr, "((uint16_t)(");
689 strcpy (vstr, "((int32_t)(");
690 strcpy (usstr, "((uint32_t)(");
695 strcpy (unsstr, usstr);
699 strcat (vstr, value);
706 strcpy (udstr, usstr);
708 strcat (udstr, "))");
710 strcat (usstr, "))");
712 strcpy (undstr, unsstr);
713 strcat (unsstr, "-");
714 strcat (undstr, "~");
715 strcat (undstr, dst);
716 strcat (undstr, "))");
717 strcat (unsstr, src);
718 strcat (unsstr, "))");
721 case flag_logical_noclobber:
732 printf ("uint32_t %s = %s + %s;\n", value, dstr, sstr);
737 printf ("uint32_t %s = %s - %s;\n", value, dstr, sstr);
742 case flag_logical_noclobber:
755 printf ("\t" BOOL_TYPE " flgs = %s < 0;\n", sstr);
756 printf ("\t" BOOL_TYPE " flgo = %s < 0;\n", dstr);
757 printf ("\t" BOOL_TYPE " flgn = %s < 0;\n", vstr);
763 printf ("\tCLEAR_CZNV;\n");
764 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
765 printf ("\tSET_NFLG (%s < 0);\n", vstr);
767 case flag_logical_noclobber:
768 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
769 printf ("\tSET_NFLG (%s < 0);\n", vstr);
772 printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n");
775 printf ("\tSET_VFLG ((flgs ^ flgo) & (flgn ^ flgo));\n");
778 printf ("\tSET_ZFLG (GET_ZFLG & (%s == 0));\n", vstr);
779 printf ("\tSET_NFLG (%s < 0);\n", vstr);
782 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
783 printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n");
784 printf ("\tSET_CFLG (%s < %s);\n", undstr, usstr);
786 printf ("\tSET_NFLG (flgn != 0);\n");
789 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
790 printf ("\tSET_VFLG ((flgs ^ flgo) & (flgn ^ flgo));\n");
791 printf ("\tSET_CFLG (%s > %s);\n", usstr, udstr);
793 printf ("\tSET_NFLG (flgn != 0);\n");
796 printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n"); /* minterm SON: 0x42 */
797 printf ("\tSET_CFLG (flgs ^ ((flgs ^ flgo) & (flgo ^ flgn)));\n"); /* minterm SON: 0xD4 */
801 printf ("\tSET_VFLG ((flgs ^ flgo) & (flgo ^ flgn));\n"); /* minterm SON: 0x24 */
802 printf ("\tSET_CFLG (flgs ^ ((flgs ^ flgn) & (flgo ^ flgn)));\n"); /* minterm SON: 0xB2 */
806 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
807 printf ("\tSET_VFLG ((flgs != flgo) && (flgn != flgo));\n");
808 printf ("\tSET_CFLG (%s > %s);\n", usstr, udstr);
809 printf ("\tSET_NFLG (flgn != 0);\n");
814 static void genflags (flagtypes type, wordsizes size, const char *value,
815 const char *src, const char *dst)
817 /* Temporarily deleted 68k/ARM flag optimizations. I'd prefer to have
818 them in the appropriate m68k.h files and use just one copy of this
819 code here. The API can be changed if necessary. */
820 #ifdef OPTIMIZED_FLAGS
825 printf ("\tuint32_t %s;\n", value);
832 /* At least some of those casts are fairly important! */
834 case flag_logical_noclobber:
835 printf ("\t{uint32_t oldcznv = GET_CZNV & ~(FLAGVAL_Z | FLAGVAL_N);\n");
836 if (strcmp (value, "0") == 0) {
837 printf ("\tSET_CZNV (olcznv | FLAGVAL_Z);\n");
840 case sz_byte: printf ("\toptflag_testb ((int8_t)(%s));\n", value); break;
841 case sz_word: printf ("\toptflag_testw ((int16_t)(%s));\n", value); break;
842 case sz_long: printf ("\toptflag_testl ((int32_t)(%s));\n", value); break;
844 printf ("\tIOR_CZNV (oldcznv);\n");
849 if (strcmp (value, "0") == 0) {
850 printf ("\tSET_CZNV (FLAGVAL_Z);\n");
853 case sz_byte: printf ("\toptflag_testb ((int8_t)(%s));\n", value); break;
854 case sz_word: printf ("\toptflag_testw ((int16_t)(%s));\n", value); break;
855 case sz_long: printf ("\toptflag_testl ((int32_t)(%s));\n", value); break;
862 case sz_byte: printf ("\toptflag_addb (%s, (int8_t)(%s), (int8_t)(%s));\n", value, src, dst); break;
863 case sz_word: printf ("\toptflag_addw (%s, (int16_t)(%s), (int16_t)(%s));\n", value, src, dst); break;
864 case sz_long: printf ("\toptflag_addl (%s, (int32_t)(%s), (int32_t)(%s));\n", value, src, dst); break;
870 case sz_byte: printf ("\toptflag_subb (%s, (int8_t)(%s), (int8_t)(%s));\n", value, src, dst); break;
871 case sz_word: printf ("\toptflag_subw (%s, (int16_t)(%s), (int16_t)(%s));\n", value, src, dst); break;
872 case sz_long: printf ("\toptflag_subl (%s, (int32_t)(%s), (int32_t)(%s));\n", value, src, dst); break;
878 case sz_byte: printf ("\toptflag_cmpb ((int8_t)(%s), (int8_t)(%s));\n", src, dst); break;
879 case sz_word: printf ("\toptflag_cmpw ((int16_t)(%s), (int16_t)(%s));\n", src, dst); break;
880 case sz_long: printf ("\toptflag_cmpl ((int32_t)(%s), (int32_t)(%s));\n", src, dst); break;
889 genflags_normal (type, size, value, src, dst);
892 static void force_range_for_rox (const char *var, wordsizes size)
894 /* Could do a modulo operation here... which one is faster? */
897 printf ("\tif (%s >= 33) %s -= 33;\n", var, var);
900 printf ("\tif (%s >= 34) %s -= 34;\n", var, var);
901 printf ("\tif (%s >= 17) %s -= 17;\n", var, var);
904 printf ("\tif (%s >= 36) %s -= 36;\n", var, var);
905 printf ("\tif (%s >= 18) %s -= 18;\n", var, var);
906 printf ("\tif (%s >= 9) %s -= 9;\n", var, var);
911 static const char *cmask (wordsizes size)
914 case sz_byte: return "0x80";
915 case sz_word: return "0x8000";
916 case sz_long: return "0x80000000";
921 static int source_is_imm1_8 (struct instr *i)
923 return i->stype == 3;
928 static void gen_opcode (unsigned long int opcode)
931 char *amodenames[] = { "Dreg", "Areg", "Aind", "Aipi", "Apdi", "Ad16", "Ad8r",
932 "absw", "absl", "PC16", "PC8r", "imm", "imm0", "imm1", "imm2", "immi", "am_unknown", "am_illg"};
935 struct instr *curi = table68k + opcode;
938 /* Store the family of the instruction (used to check for pairing on ST)
939 * and leave some space for patching in the current cycles later */
940 printf ("\tOpcodeFamily = %d; CurrentInstrCycles = \n", curi->mnemo);
941 nCurInstrCycPos = ftell(stdout) - 5;
946 switch (curi->plev) {
947 case 0: /* not privileged */
949 case 1: /* unprivileged only on 68000 */
952 if (next_cpu_level < 0)
956 case 2: /* priviledged */
957 printf ("if (!regs.s) { Exception(8,0,M68000_EXC_SRC_CPU); goto %s; }\n", endlabelstr);
961 case 3: /* privileged if size == word */
962 if (curi->size == sz_byte)
964 printf ("if (!regs.s) { Exception(8,0,M68000_EXC_SRC_CPU); goto %s; }\n", endlabelstr);
970 /* Build the opcodes: */
971 switch (curi->mnemo) {
975 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
976 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
977 printf ("\tsrc %c= dst;\n", curi->mnemo == i_OR ? '|' : curi->mnemo == i_AND ? '&' : '^');
978 genflags (flag_logical, curi->size, "src", "", "");
979 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
980 if(curi->size==sz_long && curi->dmode==Dreg)
983 if(curi->smode==Dreg || curi->smode==Areg || (curi->smode>=imm && curi->smode<=immi))
987 /* Output the CPU cycles: */
988 fprintf(stderr,"MOVE, size %i: ",curi->size);
989 fprintf(stderr," %s ->",amodenames[curi->smode]);
990 fprintf(stderr," %s ",amodenames[curi->dmode]);
991 fprintf(stderr," Cycles: %i\n",insn_n_cycles);
996 printf ("\tMakeSR();\n");
997 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
998 if (curi->size == sz_byte) {
999 printf ("\tsrc &= 0xFF;\n");
1001 printf ("\tregs.sr %c= src;\n", curi->mnemo == i_EORSR ? '^' : '|');
1002 printf ("\tMakeFromSR();\n");
1006 printf ("\tMakeSR();\n");
1007 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1008 if (curi->size == sz_byte) {
1009 printf ("\tsrc |= 0xFF00;\n");
1011 printf ("\tregs.sr &= src;\n");
1012 printf ("\tMakeFromSR();\n");
1016 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1017 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1019 genflags (flag_sub, curi->size, "newv", "src", "dst");
1020 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
1021 if(curi->size==sz_long && curi->dmode==Dreg)
1024 if(curi->smode==Dreg || curi->smode==Areg || (curi->smode>=imm && curi->smode<=immi))
1029 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1030 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1032 printf ("\tuint32_t newv = dst - src;\n");
1033 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1034 if(curi->size==sz_long && curi->smode!=Dreg && curi->smode!=Areg && !(curi->smode>=imm && curi->smode<=immi))
1040 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1041 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1043 printf ("\tuint32_t newv = dst - src - (GET_XFLG ? 1 : 0);\n");
1044 genflags (flag_subx, curi->size, "newv", "src", "dst");
1045 genflags (flag_zn, curi->size, "newv", "", "");
1046 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
1047 if(curi->smode==Dreg && curi->size==sz_long)
1049 if(curi->smode==Apdi)
1051 if(curi->size==sz_long)
1058 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1059 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1061 printf ("\tuint16_t newv_lo = (dst & 0xF) - (src & 0xF) - (GET_XFLG ? 1 : 0);\n");
1062 printf ("\tuint16_t newv_hi = (dst & 0xF0) - (src & 0xF0);\n");
1063 printf ("\tuint16_t newv, tmp_newv;\n");
1064 printf ("\tint bcd = 0;\n");
1065 printf ("\tnewv = tmp_newv = newv_hi + newv_lo;\n");
1066 printf ("\tif (newv_lo & 0xF0) { newv -= 6; bcd = 6; };\n");
1067 printf ("\tif ((((dst & 0xFF) - (src & 0xFF) - (GET_XFLG ? 1 : 0)) & 0x100) > 0xFF) { newv -= 0x60; }\n");
1068 printf ("\tSET_CFLG ((((dst & 0xFF) - (src & 0xFF) - bcd - (GET_XFLG ? 1 : 0)) & 0x300) > 0xFF);\n");
1070 genflags (flag_zn, curi->size, "newv", "", "");
1071 printf ("\tSET_VFLG ((tmp_newv & 0x80) != 0 && (newv & 0x80) == 0);\n");
1072 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
1073 if(curi->smode==Dreg) insn_n_cycles=6;
1074 if(curi->smode==Apdi) insn_n_cycles=18;
1077 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1078 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1080 printf("\trefill_prefetch (m68k_getpc(), 2);\n"); // FIXME [NP] For Transbeauce 2 demo, need better prefetch emulation
1081 genflags (flag_add, curi->size, "newv", "src", "dst");
1082 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
1083 if(curi->size==sz_long && curi->dmode==Dreg)
1086 if(curi->smode==Dreg || curi->smode==Areg || (curi->smode>=imm && curi->smode<=immi))
1091 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1092 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1094 printf ("\tuint32_t newv = dst + src;\n");
1095 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1096 if(curi->size==sz_long && curi->smode!=Dreg && curi->smode!=Areg && !(curi->smode>=imm && curi->smode<=immi))
1102 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1103 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1105 printf ("\tuint32_t newv = dst + src + (GET_XFLG ? 1 : 0);\n");
1106 genflags (flag_addx, curi->size, "newv", "src", "dst");
1107 genflags (flag_zn, curi->size, "newv", "", "");
1108 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
1109 if(curi->smode==Dreg && curi->size==sz_long)
1111 if(curi->smode==Apdi)
1113 if(curi->size==sz_long)
1120 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1121 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1123 printf ("\tuint16_t newv_lo = (src & 0xF) + (dst & 0xF) + (GET_XFLG ? 1 : 0);\n");
1124 printf ("\tuint16_t newv_hi = (src & 0xF0) + (dst & 0xF0);\n");
1125 printf ("\tuint16_t newv, tmp_newv;\n");
1126 printf ("\tint cflg;\n");
1127 printf ("\tnewv = tmp_newv = newv_hi + newv_lo;");
1128 printf ("\tif (newv_lo > 9) { newv += 6; }\n");
1129 printf ("\tcflg = (newv & 0x3F0) > 0x90;\n");
1130 printf ("\tif (cflg) newv += 0x60;\n");
1131 printf ("\tSET_CFLG (cflg);\n");
1133 genflags (flag_zn, curi->size, "newv", "", "");
1134 printf ("\tSET_VFLG ((tmp_newv & 0x80) == 0 && (newv & 0x80) != 0);\n");
1135 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
1136 if(curi->smode==Dreg) insn_n_cycles=6;
1137 if(curi->smode==Apdi) insn_n_cycles=18;
1140 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1142 genflags (flag_sub, curi->size, "dst", "src", "0");
1143 genastore ("dst", curi->smode, "srcreg", curi->size, "src");
1144 if(curi->size==sz_long && curi->smode==Dreg) insn_n_cycles += 2;
1147 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1149 printf ("\tuint32_t newv = 0 - src - (GET_XFLG ? 1 : 0);\n");
1150 genflags (flag_subx, curi->size, "newv", "src", "0");
1151 genflags (flag_zn, curi->size, "newv", "", "");
1152 genastore ("newv", curi->smode, "srcreg", curi->size, "src");
1153 if(curi->size==sz_long && curi->smode==Dreg) insn_n_cycles += 2;
1156 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1158 printf ("\tuint16_t newv_lo = - (src & 0xF) - (GET_XFLG ? 1 : 0);\n");
1159 printf ("\tuint16_t newv_hi = - (src & 0xF0);\n");
1160 printf ("\tuint16_t newv;\n");
1161 printf ("\tint cflg;\n");
1162 printf ("\tif (newv_lo > 9) { newv_lo -= 6; }\n");
1163 printf ("\tnewv = newv_hi + newv_lo;");
1164 printf ("\tcflg = (newv & 0x1F0) > 0x90;\n");
1165 printf ("\tif (cflg) newv -= 0x60;\n");
1166 printf ("\tSET_CFLG (cflg);\n");
1168 genflags (flag_zn, curi->size, "newv", "", "");
1169 genastore ("newv", curi->smode, "srcreg", curi->size, "src");
1170 if(curi->smode==Dreg) insn_n_cycles += 2;
1173 genamode (curi->smode, "srcreg", curi->size, "src", 2, 0);
1175 /* [NP] CLR does a read before the write only on 68000 */
1176 /* but there's no cycle penalty for doing the read */
1177 if ( curi->smode != Dreg ) // only if destination is memory
1179 if (curi->size==sz_byte)
1180 printf ("\tint8_t src = m68k_read_memory_8(srca);\n");
1181 else if (curi->size==sz_word)
1182 printf ("\tint16_t src = m68k_read_memory_16(srca);\n");
1183 else if (curi->size==sz_long)
1184 printf ("\tint32_t src = m68k_read_memory_32(srca);\n");
1187 genflags (flag_logical, curi->size, "0", "", "");
1188 genastore ("0", curi->smode, "srcreg", curi->size, "src");
1189 if(curi->size==sz_long)
1191 if(curi->smode==Dreg)
1196 if(curi->smode!=Dreg)
1200 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1202 printf ("\tuint32_t dst = ~src;\n");
1203 genflags (flag_logical, curi->size, "dst", "", "");
1204 genastore ("dst", curi->smode, "srcreg", curi->size, "src");
1205 if(curi->size==sz_long && curi->smode==Dreg) insn_n_cycles += 2;
1208 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1209 genflags (flag_logical, curi->size, "src", "", "");
1212 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1213 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1214 if (curi->size == sz_byte)
1215 printf ("\tsrc &= 7;\n");
1217 printf ("\tsrc &= 31;\n");
1218 printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1219 if(curi->dmode==Dreg) insn_n_cycles += 2;
1222 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1223 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1224 if (curi->size == sz_byte)
1225 printf ("\tsrc &= 7;\n");
1227 printf ("\tsrc &= 31;\n");
1228 printf ("\tdst ^= (1 << src);\n");
1229 printf ("\tSET_ZFLG (((uint32_t)dst & (1 << src)) >> src);\n");
1230 genastore ("dst", curi->dmode, "dstreg", curi->size, "dst");
1231 if(curi->dmode==Dreg) insn_n_cycles += 4;
1234 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1235 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1236 if (curi->size == sz_byte)
1237 printf ("\tsrc &= 7;\n");
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");
1243 if(curi->dmode==Dreg) insn_n_cycles += 6;
1244 /* [NP] BCLR #n,Dx takes 12 cycles instead of 14 if n<16 */
1245 if((curi->smode==imm1) && (curi->dmode==Dreg))
1246 printf ("\tif ( src < 16 ) { m68k_incpc(4); return 12; }\n");
1247 /* [NP] BCLR Dy,Dx takes 8 cycles instead of 10 if Dy<16 */
1248 if((curi->smode==Dreg) && (curi->dmode==Dreg))
1249 printf ("\tif ( src < 16 ) { m68k_incpc(2); return 8; }\n");
1252 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1253 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1254 if (curi->size == sz_byte)
1255 printf ("\tsrc &= 7;\n");
1257 printf ("\tsrc &= 31;\n");
1258 printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1259 printf ("\tdst |= (1 << src);\n");
1260 genastore ("dst", curi->dmode, "dstreg", curi->size, "dst");
1261 if(curi->dmode==Dreg) insn_n_cycles += 4;
1265 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1266 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1268 genflags (flag_cmp, curi->size, "newv", "src", "dst");
1269 if(curi->size==sz_long && curi->dmode==Dreg)
1273 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1274 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1276 genflags (flag_cmp, sz_long, "newv", "src", "dst");
1279 /* The next two are coded a little unconventional, but they are doing
1280 * weird things... */
1282 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1284 printf ("\tuint32_t memp = m68k_areg(regs, dstreg) + (int32_t)(int16_t)%s;\n", gen_nextiword ());
1285 if (curi->size == sz_word) {
1286 printf ("\tm68k_write_memory_8(memp, src >> 8); m68k_write_memory_8(memp + 2, src);\n");
1288 printf ("\tm68k_write_memory_8(memp, src >> 24); m68k_write_memory_8(memp + 2, src >> 16);\n");
1289 printf ("\tm68k_write_memory_8(memp + 4, src >> 8); m68k_write_memory_8(memp + 6, src);\n");
1291 if(curi->size==sz_long) insn_n_cycles=24; else insn_n_cycles=16;
1294 printf ("\tuint32_t memp = m68k_areg(regs, srcreg) + (int32_t)(int16_t)%s;\n", gen_nextiword ());
1295 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1296 if (curi->size == sz_word) {
1297 printf ("\tuint16_t val = (m68k_read_memory_8(memp) << 8) + m68k_read_memory_8(memp + 2);\n");
1299 printf ("\tuint32_t val = (m68k_read_memory_8(memp) << 24) + (m68k_read_memory_8(memp + 2) << 16)\n");
1300 printf (" + (m68k_read_memory_8(memp + 4) << 8) + m68k_read_memory_8(memp + 6);\n");
1302 genastore ("val", curi->dmode, "dstreg", curi->size, "dst");
1303 if(curi->size==sz_long) insn_n_cycles=24; else insn_n_cycles=16;
1306 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1307 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1309 /* [NP] genamode counts 2 cycles if dest is -(An), this is wrong. */
1310 /* For move dest (An), (An)+ and -(An) take the same time */
1311 /* (for other instr, dest -(An) really takes 2 cycles more) */
1312 if ( curi->dmode == Apdi )
1313 insn_n_cycles -= 2; /* correct the wrong cycle count for -(An) */
1315 genflags (flag_logical, curi->size, "src", "", "");
1316 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
1319 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1320 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1321 if (curi->size == sz_word) {
1322 printf ("\tuint32_t val = (int32_t)(int16_t)src;\n");
1324 printf ("\tuint32_t val = src;\n");
1326 genastore ("val", curi->dmode, "dstreg", sz_long, "dst");
1328 case i_MVSR2: /* Move from SR */
1329 genamode (curi->smode, "srcreg", sz_word, "src", 2, 0);
1330 printf ("\tMakeSR();\n");
1331 if (curi->size == sz_byte)
1332 genastore ("regs.sr & 0xff", curi->smode, "srcreg", sz_word, "src");
1334 genastore ("regs.sr", curi->smode, "srcreg", sz_word, "src");
1335 if (curi->smode==Dreg) insn_n_cycles += 2; else insn_n_cycles += 4;
1337 case i_MV2SR: /* Move to SR */
1338 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1339 if (curi->size == sz_byte)
1340 printf ("\tMakeSR();\n\tregs.sr &= 0xFF00;\n\tregs.sr |= src & 0xFF;\n");
1342 printf ("\tregs.sr = src;\n");
1344 printf ("\tMakeFromSR();\n");
1348 genamode (curi->smode, "srcreg", sz_long, "src", 1, 0);
1350 printf ("\tuint32_t dst = ((src >> 16)&0xFFFF) | ((src&0xFFFF)<<16);\n");
1351 genflags (flag_logical, sz_long, "dst", "", "");
1352 genastore ("dst", curi->smode, "srcreg", sz_long, "src");
1355 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1356 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1357 genastore ("dst", curi->smode, "srcreg", curi->size, "src");
1358 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
1362 genamode (curi->smode, "srcreg", sz_long, "src", 1, 0);
1364 switch (curi->size) {
1365 case sz_byte: printf ("\tuint32_t dst = (int32_t)(int8_t)src;\n"); break;
1366 case sz_word: printf ("\tuint16_t dst = (int16_t)(int8_t)src;\n"); break;
1367 case sz_long: printf ("\tuint32_t dst = (int32_t)(int16_t)src;\n"); break;
1370 genflags (flag_logical,
1371 curi->size == sz_word ? sz_word : sz_long, "dst", "", "");
1372 genastore ("dst", curi->smode, "srcreg",
1373 curi->size == sz_word ? sz_word : sz_long, "src");
1376 genmovemel (opcode);
1379 genmovemle (opcode);
1382 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1384 printf ("\tException(src+32,0,M68000_EXC_SRC_CPU);\n");
1388 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1389 printf ("\tregs.usp = src;\n");
1392 genamode (curi->smode, "srcreg", curi->size, "src", 2, 0);
1393 genastore ("regs.usp", curi->smode, "srcreg", curi->size, "src");
1396 //JLH:Not needed printf ("\tcustomreset();\n");
1397 insn_n_cycles = 132; /* I am not so sure about this!? - Thothy */
1402 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1403 printf ("\tregs.sr = src;\n");
1404 printf ("\tMakeFromSR();\n");
1405 printf ("\tm68k_setstopped(1);\n");
1409 if (cpu_level == 0) {
1410 genamode (Aipi, "7", sz_word, "sr", 1, 0);
1411 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1412 printf ("\tregs.sr = sr; m68k_setpc_rte(pc);\n");
1414 printf ("\tMakeFromSR();\n");
1416 int old_brace_level = n_braces;
1417 if (next_cpu_level < 0)
1419 printf ("\tuint16_t newsr; uint32_t newpc; for (;;) {\n");
1420 genamode (Aipi, "7", sz_word, "sr", 1, 0);
1421 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1422 genamode (Aipi, "7", sz_word, "format", 1, 0);
1423 printf ("\tnewsr = sr; newpc = pc;\n");
1424 printf ("\tif ((format & 0xF000) == 0x0000) { break; }\n");
1425 printf ("\telse if ((format & 0xF000) == 0x1000) { ; }\n");
1426 printf ("\telse if ((format & 0xF000) == 0x2000) { m68k_areg(regs, 7) += 4; break; }\n");
1427 printf ("\telse if ((format & 0xF000) == 0x8000) { m68k_areg(regs, 7) += 50; break; }\n");
1428 printf ("\telse if ((format & 0xF000) == 0x9000) { m68k_areg(regs, 7) += 12; break; }\n");
1429 printf ("\telse if ((format & 0xF000) == 0xa000) { m68k_areg(regs, 7) += 24; break; }\n");
1430 printf ("\telse if ((format & 0xF000) == 0xb000) { m68k_areg(regs, 7) += 84; break; }\n");
1431 printf ("\telse { Exception(14,0,M68000_EXC_SRC_CPU); goto %s; }\n", endlabelstr);
1432 printf ("\tregs.sr = newsr; MakeFromSR();\n}\n");
1433 pop_braces (old_brace_level);
1434 printf ("\tregs.sr = newsr; MakeFromSR();\n");
1435 printf ("\tm68k_setpc_rte(newpc);\n");
1439 /* PC is set and prefetch filled. */
1444 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1445 genamode (curi->smode, "srcreg", curi->size, "offs", 1, 0);
1446 printf ("\tm68k_areg(regs, 7) += offs;\n");
1447 printf ("\tm68k_setpc_rte(pc);\n");
1449 /* PC is set and prefetch filled. */
1453 genamode (Apdi, "7", sz_long, "old", 2, 0);
1454 genamode (curi->smode, "srcreg", sz_long, "src", 1, 0);
1455 genastore ("src", Apdi, "7", sz_long, "old");
1456 genastore ("m68k_areg(regs, 7)", curi->smode, "srcreg", sz_long, "src");
1457 genamode (curi->dmode, "dstreg", curi->size, "offs", 1, 0);
1458 printf ("\tm68k_areg(regs, 7) += offs;\n");
1461 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1462 printf ("\tm68k_areg(regs, 7) = src;\n");
1463 genamode (Aipi, "7", sz_long, "old", 1, 0);
1464 genastore ("old", curi->smode, "srcreg", curi->size, "src");
1467 printf ("\tm68k_do_rts();\n");
1474 printf ("\tif (GET_VFLG) { Exception(7,m68k_getpc(),M68000_EXC_SRC_CPU); goto %s; }\n", endlabelstr);
1478 printf ("\tMakeSR();\n");
1479 genamode (Aipi, "7", sz_word, "sr", 1, 0);
1480 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1481 printf ("\tregs.sr &= 0xFF00; sr &= 0xFF;\n");
1482 printf ("\tregs.sr |= sr; m68k_setpc(pc);\n");
1484 printf ("\tMakeFromSR();\n");
1489 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1490 printf ("\tuint32_t oldpc = m68k_getpc () + %d;\n", m68k_pc_offset);
1491 if (using_exception_3) {
1492 printf ("\tif (srca & 1) {\n");
1493 printf ("\t\tlast_addr_for_exception_3 = oldpc;\n");
1494 printf ("\t\tlast_fault_for_exception_3 = srca;\n");
1495 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0,M68000_EXC_SRC_CPU); goto %s;\n", endlabelstr);
1499 printf ("\tm68k_do_jsr(m68k_getpc() + %d, srca);\n", m68k_pc_offset);
1504 case Aind: insn_n_cycles=16; break;
1505 case Ad16: insn_n_cycles=18; break;
1506 case Ad8r: insn_n_cycles=22; break;
1507 case absw: insn_n_cycles=18; break;
1508 case absl: insn_n_cycles=20; break;
1509 case PC16: insn_n_cycles=18; break;
1510 case PC8r: insn_n_cycles=22; break;
1514 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1515 if (using_exception_3) {
1516 printf ("\tif (srca & 1) {\n");
1517 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 6;\n");
1518 printf ("\t\tlast_fault_for_exception_3 = srca;\n");
1519 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0,M68000_EXC_SRC_CPU); goto %s;\n", endlabelstr);
1523 printf ("\tm68k_setpc(srca);\n");
1528 case Aind: insn_n_cycles=8; break;
1529 case Ad16: insn_n_cycles=10; break;
1530 case Ad8r: insn_n_cycles=14; break;
1531 case absw: insn_n_cycles=10; break;
1532 case absl: insn_n_cycles=12; break;
1533 case PC16: insn_n_cycles=10; break;
1534 case PC8r: insn_n_cycles=14; break;
1538 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1539 printf ("\tint32_t s = (int32_t)src + 2;\n");
1540 if (using_exception_3) {
1541 printf ("\tif (src & 1) {\n");
1542 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n"); // [NP] FIXME should be +4, not +2 (same as DBcc) ?
1543 printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + s;\n");
1544 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0,M68000_EXC_SRC_CPU); goto %s;\n", endlabelstr);
1548 printf ("\tm68k_do_bsr(m68k_getpc() + %d, s);\n", m68k_pc_offset);
1554 if (curi->size == sz_long) {
1555 if (cpu_level < 2) {
1556 printf ("\tm68k_incpc(2);\n");
1557 printf ("\tif (!cctrue(%d)) goto %s;\n", curi->cc, endlabelstr);
1558 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1559 printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + 1;\n");
1560 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0,M68000_EXC_SRC_CPU); goto %s;\n", endlabelstr);
1563 if (next_cpu_level < 1)
1567 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1568 printf ("\tif (!cctrue(%d)) goto didnt_jump;\n", curi->cc);
1569 if (using_exception_3) {
1570 printf ("\tif (src & 1) {\n");
1571 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n"); // [NP] FIXME should be +4, not +2 (same as DBcc) ?
1572 printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + 2 + (int32_t)src;\n");
1573 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0,M68000_EXC_SRC_CPU); goto %s;\n", endlabelstr);
1577 printf ("\tm68k_incpc ((int32_t)src + 2);\n");
1579 printf ("\treturn 10;\n");
1580 printf ("didnt_jump:;\n");
1582 insn_n_cycles = (curi->size == sz_byte) ? 8 : 12;
1585 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1586 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1587 genastore ("srca", curi->dmode, "dstreg", curi->size, "dst");
1588 /* Set correct cycles: According to the M68K User Manual, LEA takes 12
1589 * cycles in Ad8r and PC8r mode, but it takes 14 (or 16) cycles on a real ST: */
1590 if (curi->smode == Ad8r || curi->smode == PC8r)
1594 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1595 genamode (Apdi, "7", sz_long, "dst", 2, 0);
1596 genastore ("srca", Apdi, "7", sz_long, "dst");
1597 /* Set correct cycles: */
1600 case Aind: insn_n_cycles=12; break;
1601 case Ad16: insn_n_cycles=16; break;
1602 /* Note: according to the M68K User Manual, PEA takes 20 cycles for
1603 * the Ad8r mode, but on a real ST, it takes 22 (or 24) cycles! */
1604 case Ad8r: insn_n_cycles=22; break;
1605 case absw: insn_n_cycles=16; break;
1606 case absl: insn_n_cycles=20; break;
1607 case PC16: insn_n_cycles=16; break;
1608 /* Note: PEA with PC8r takes 20 cycles according to the User Manual,
1609 * but it takes 22 (or 24) cycles on a real ST: */
1610 case PC8r: insn_n_cycles=22; break;
1614 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1615 genamode (curi->dmode, "dstreg", curi->size, "offs", 1, 0);
1617 printf ("\tif (!cctrue(%d)) {\n\t", curi->cc);
1618 genastore ("(src-1)", curi->smode, "srcreg", curi->size, "src");
1620 printf ("\t\tif (src) {\n");
1621 if (using_exception_3) {
1622 printf ("\t\t\tif (offs & 1) {\n");
1623 printf ("\t\t\tlast_addr_for_exception_3 = m68k_getpc() + 2 + 2;\n"); // [NP] last_addr is pc+4, not pc+2
1624 printf ("\t\t\tlast_fault_for_exception_3 = m68k_getpc() + 2 + (int32_t)offs + 2;\n");
1625 printf ("\t\t\tlast_op_for_exception_3 = opcode; Exception(3,0,M68000_EXC_SRC_CPU); goto %s;\n", endlabelstr);
1629 printf ("\t\t\tm68k_incpc((int32_t)offs + 2);\n");
1631 printf ("\t\t\treturn 10;\n");
1632 printf ("\t\t} else {\n\t\t\t");
1634 int tmp_offset = m68k_pc_offset;
1635 sync_m68k_pc(); /* not so nice to call it here... */
1636 m68k_pc_offset = tmp_offset;
1638 printf ("\t\t\treturn 14;\n");
1645 genamode (curi->smode, "srcreg", curi->size, "src", 2, 0);
1647 printf ("\tint val = cctrue(%d) ? 0xff : 0;\n", curi->cc);
1648 genastore ("val", curi->smode, "srcreg", curi->size, "src");
1649 if (curi->smode!=Dreg) insn_n_cycles += 4;
1651 { /* [NP] if result is TRUE, we return 6 instead of 4 */
1652 printf ("\tif (val) { m68k_incpc(2) ; return 4+2; }\n");
1656 printf ("\tuint32_t oldpc = m68k_getpc();\n");
1657 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1658 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1660 /* Clear V flag when dividing by zero - Alcatraz Odyssey demo depends
1661 * on this (actually, it's doing a DIVS). */
1662 printf ("\tif (src == 0) { SET_VFLG (0); Exception (5, oldpc,M68000_EXC_SRC_CPU); goto %s; } else {\n", endlabelstr);
1663 printf ("\tuint32_t newv = (uint32_t)dst / (uint32_t)(uint16_t)src;\n");
1664 printf ("\tuint32_t rem = (uint32_t)dst %% (uint32_t)(uint16_t)src;\n");
1665 /* The N flag appears to be set each time there is an overflow.
1667 printf ("\tif (newv > 0xffff) { SET_VFLG (1); SET_NFLG (1); SET_CFLG (0); } else\n\t{\n");
1668 genflags (flag_logical, sz_word, "newv", "", "");
1669 printf ("\tnewv = (newv & 0xffff) | ((uint32_t)rem << 16);\n");
1670 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1673 // insn_n_cycles += 136;
1674 printf ("\tretcycles = getDivu68kCycles((uint32_t)dst, (uint16_t)src);\n");
1675 sprintf(exactCpuCycles," return (%i+retcycles);", insn_n_cycles);
1679 printf ("\tuint32_t oldpc = m68k_getpc();\n");
1680 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1681 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1683 printf ("\tif (src == 0) { SET_VFLG (0); Exception(5,oldpc,M68000_EXC_SRC_CPU); goto %s; } else {\n", endlabelstr);
1684 printf ("\tint32_t newv = (int32_t)dst / (int32_t)(int16_t)src;\n");
1685 printf ("\tuint16_t rem = (int32_t)dst %% (int32_t)(int16_t)src;\n");
1686 printf ("\tif ((newv & 0xffff8000) != 0 && (newv & 0xffff8000) != 0xffff8000) { SET_VFLG (1); SET_NFLG (1); SET_CFLG (0); } else\n\t{\n");
1687 printf ("\tif (((int16_t)rem < 0) != ((int32_t)dst < 0)) rem = -rem;\n");
1688 genflags (flag_logical, sz_word, "newv", "", "");
1689 printf ("\tnewv = (newv & 0xffff) | ((uint32_t)rem << 16);\n");
1690 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1693 // insn_n_cycles += 154;
1694 printf ("\tretcycles = getDivs68kCycles((int32_t)dst, (int16_t)src);\n");
1695 sprintf(exactCpuCycles," return (%i+retcycles);", insn_n_cycles);
1699 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1700 genamode (curi->dmode, "dstreg", sz_word, "dst", 1, 0);
1702 printf ("\tuint32_t newv = (uint32_t)(uint16_t)dst * (uint32_t)(uint16_t)src;\n");
1703 genflags (flag_logical, sz_long, "newv", "", "");
1704 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1705 /* [NP] number of cycles is 38 + 2n + ea time ; n is the number of 1 bits in src */
1706 insn_n_cycles += 38-4; /* insn_n_cycles is already initialized to 4 instead of 0 */
1707 printf ("\twhile (src) { if (src & 1) retcycles++; src = (uint16_t)src >> 1; }\n");
1708 sprintf(exactCpuCycles," return (%i+retcycles*2);", insn_n_cycles);
1711 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1712 genamode (curi->dmode, "dstreg", sz_word, "dst", 1, 0);
1714 printf ("\tuint32_t newv = (int32_t)(int16_t)dst * (int32_t)(int16_t)src;\n");
1715 printf ("\tuint32_t src2;\n");
1716 genflags (flag_logical, sz_long, "newv", "", "");
1717 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1718 /* [NP] number of cycles is 38 + 2n + ea time ; n is the number of 01 or 10 patterns in src expanded to 17 bits */
1719 insn_n_cycles += 38-4; /* insn_n_cycles is already initialized to 4 instead of 0 */
1720 printf ("\tsrc2 = ((uint32_t)src) << 1;\n");
1721 printf ("\twhile (src2) { if ( ( (src2 & 3) == 1 ) || ( (src2 & 3) == 2 ) ) retcycles++; src2 >>= 1; }\n");
1722 sprintf(exactCpuCycles," return (%i+retcycles*2);", insn_n_cycles);
1725 printf ("\tuint32_t oldpc = m68k_getpc();\n");
1726 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1727 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1729 printf ("\tif ((int32_t)dst < 0) { SET_NFLG (1); Exception(6,oldpc,M68000_EXC_SRC_CPU); goto %s; }\n", endlabelstr);
1730 printf ("\telse if (dst > src) { SET_NFLG (0); Exception(6,oldpc,M68000_EXC_SRC_CPU); goto %s; }\n", endlabelstr);
1736 printf ("\tuint32_t oldpc = m68k_getpc();\n");
1737 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1738 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1739 printf ("\t{int32_t upper,lower,reg = regs.regs[(extra >> 12) & 15];\n");
1740 switch (curi->size) {
1742 printf ("\tlower=(int32_t)(int8_t)m68k_read_memory_8(dsta); upper = (int32_t)(int8_t)m68k_read_memory_8(dsta+1);\n");
1743 printf ("\tif ((extra & 0x8000) == 0) reg = (int32_t)(int8_t)reg;\n");
1746 printf ("\tlower=(int32_t)(int16_t)m68k_read_memory_16(dsta); upper = (int32_t)(int16_t)m68k_read_memory_16(dsta+2);\n");
1747 printf ("\tif ((extra & 0x8000) == 0) reg = (int32_t)(int16_t)reg;\n");
1750 printf ("\tlower=m68k_read_memory_32(dsta); upper = m68k_read_memory_32(dsta+4);\n");
1755 printf ("\tSET_ZFLG (upper == reg || lower == reg);\n");
1756 printf ("\tSET_CFLG (lower <= upper ? reg < lower || reg > upper : reg > upper || reg < lower);\n");
1758 printf ("\tif ((extra & 0x800) && GET_CFLG) { Exception(6,oldpc,M68000_EXC_SRC_CPU); goto %s; }\n}\n", endlabelstr);
1763 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1764 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1766 switch (curi->size) {
1767 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1768 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1769 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1772 printf ("\tuint32_t sign = (%s & val) >> %d;\n", cmask (curi->size), bit_size (curi->size) - 1);
1773 printf ("\tcnt &= 63;\n");
1774 printf ("\tretcycles = cnt;\n");
1775 printf ("\tCLEAR_CZNV;\n");
1776 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1777 printf ("\t\tval = %s & (uint32_t)-sign;\n", bit_mask (curi->size));
1778 printf ("\t\tSET_CFLG (sign);\n");
1780 if (source_is_imm1_8 (curi))
1781 printf ("\t} else {\n");
1783 printf ("\t} else if (cnt > 0) {\n");
1784 printf ("\t\tval >>= cnt - 1;\n");
1785 printf ("\t\tSET_CFLG (val & 1);\n");
1787 printf ("\t\tval >>= 1;\n");
1788 printf ("\t\tval |= (%s << (%d - cnt)) & (uint32_t)-sign;\n",
1789 bit_mask (curi->size),
1790 bit_size (curi->size));
1791 printf ("\t\tval &= %s;\n", bit_mask (curi->size));
1793 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1794 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1795 if(curi->size==sz_long)
1796 strcpy(exactCpuCycles," return (8+retcycles*2);");
1798 strcpy(exactCpuCycles," return (6+retcycles*2);");
1801 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1802 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1804 switch (curi->size) {
1805 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1806 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1807 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1810 printf ("\tcnt &= 63;\n");
1811 printf ("\tretcycles = cnt;\n");
1812 printf ("\tCLEAR_CZNV;\n");
1813 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1814 printf ("\t\tSET_VFLG (val != 0);\n");
1815 printf ("\t\tSET_CFLG (cnt == %d ? val & 1 : 0);\n",
1816 bit_size (curi->size));
1818 printf ("\t\tval = 0;\n");
1819 if (source_is_imm1_8 (curi))
1820 printf ("\t} else {\n");
1822 printf ("\t} else if (cnt > 0) {\n");
1823 printf ("\t\tuint32_t mask = (%s << (%d - cnt)) & %s;\n",
1824 bit_mask (curi->size),
1825 bit_size (curi->size) - 1,
1826 bit_mask (curi->size));
1827 printf ("\t\tSET_VFLG ((val & mask) != mask && (val & mask) != 0);\n");
1828 printf ("\t\tval <<= cnt - 1;\n");
1829 printf ("\t\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1831 printf ("\t\tval <<= 1;\n");
1832 printf ("\t\tval &= %s;\n", bit_mask (curi->size));
1834 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1835 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1836 if(curi->size==sz_long)
1837 strcpy(exactCpuCycles," return (8+retcycles*2);");
1839 strcpy(exactCpuCycles," return (6+retcycles*2);");
1842 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1843 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1845 switch (curi->size) {
1846 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1847 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1848 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1851 printf ("\tcnt &= 63;\n");
1852 printf ("\tretcycles = cnt;\n");
1853 printf ("\tCLEAR_CZNV;\n");
1854 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1855 printf ("\t\tSET_CFLG ((cnt == %d) & (val >> %d));\n",
1856 bit_size (curi->size), bit_size (curi->size) - 1);
1858 printf ("\t\tval = 0;\n");
1859 if (source_is_imm1_8 (curi))
1860 printf ("\t} else {\n");
1862 printf ("\t} else if (cnt > 0) {\n");
1863 printf ("\t\tval >>= cnt - 1;\n");
1864 printf ("\t\tSET_CFLG (val & 1);\n");
1866 printf ("\t\tval >>= 1;\n");
1868 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1869 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1870 if(curi->size==sz_long)
1871 strcpy(exactCpuCycles," return (8+retcycles*2);");
1873 strcpy(exactCpuCycles," return (6+retcycles*2);");
1876 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1877 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1879 switch (curi->size) {
1880 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1881 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1882 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1885 printf ("\tcnt &= 63;\n");
1886 printf ("\tretcycles = cnt;\n");
1887 printf ("\tCLEAR_CZNV;\n");
1888 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1889 printf ("\t\tSET_CFLG (cnt == %d ? val & 1 : 0);\n",
1890 bit_size (curi->size));
1892 printf ("\t\tval = 0;\n");
1893 if (source_is_imm1_8 (curi))
1894 printf ("\t} else {\n");
1896 printf ("\t} else if (cnt > 0) {\n");
1897 printf ("\t\tval <<= (cnt - 1);\n");
1898 printf ("\t\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1900 printf ("\t\tval <<= 1;\n");
1901 printf ("\tval &= %s;\n", bit_mask (curi->size));
1903 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1904 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1905 if(curi->size==sz_long)
1906 strcpy(exactCpuCycles," return (8+retcycles*2);");
1908 strcpy(exactCpuCycles," return (6+retcycles*2);");
1911 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1912 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1914 switch (curi->size) {
1915 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1916 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1917 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1920 printf ("\tcnt &= 63;\n");
1921 printf ("\tretcycles = cnt;\n");
1922 printf ("\tCLEAR_CZNV;\n");
1923 if (source_is_imm1_8 (curi))
1926 printf ("\tif (cnt > 0) {\n");
1927 printf ("\tuint32_t loval;\n");
1928 printf ("\tcnt &= %d;\n", bit_size (curi->size) - 1);
1929 printf ("\tloval = val >> (%d - cnt);\n", bit_size (curi->size));
1930 printf ("\tval <<= cnt;\n");
1931 printf ("\tval |= loval;\n");
1932 printf ("\tval &= %s;\n", bit_mask (curi->size));
1933 printf ("\tSET_CFLG (val & 1);\n");
1935 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1936 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1937 if(curi->size==sz_long)
1938 strcpy(exactCpuCycles," return (8+retcycles*2);");
1940 strcpy(exactCpuCycles," return (6+retcycles*2);");
1943 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1944 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1946 switch (curi->size) {
1947 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1948 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1949 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1952 printf ("\tcnt &= 63;\n");
1953 printf ("\tretcycles = cnt;\n");
1954 printf ("\tCLEAR_CZNV;\n");
1955 if (source_is_imm1_8 (curi))
1958 printf ("\tif (cnt > 0) {");
1959 printf ("\tuint32_t hival;\n");
1960 printf ("\tcnt &= %d;\n", bit_size (curi->size) - 1);
1961 printf ("\thival = val << (%d - cnt);\n", bit_size (curi->size));
1962 printf ("\tval >>= cnt;\n");
1963 printf ("\tval |= hival;\n");
1964 printf ("\tval &= %s;\n", bit_mask (curi->size));
1965 printf ("\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1967 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1968 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1969 if(curi->size==sz_long)
1970 strcpy(exactCpuCycles," return (8+retcycles*2);");
1972 strcpy(exactCpuCycles," return (6+retcycles*2);");
1975 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1976 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1978 switch (curi->size) {
1979 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
1980 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
1981 case sz_long: printf ("\tuint32_t val = data;\n"); break;
1984 printf ("\tcnt &= 63;\n");
1985 printf ("\tretcycles = cnt;\n");
1986 printf ("\tCLEAR_CZNV;\n");
1987 if (source_is_imm1_8 (curi))
1990 force_range_for_rox ("cnt", curi->size);
1991 printf ("\tif (cnt > 0) {\n");
1993 printf ("\tcnt--;\n");
1994 printf ("\t{\n\tuint32_t carry;\n");
1995 printf ("\tuint32_t loval = val >> (%d - cnt);\n", bit_size (curi->size) - 1);
1996 printf ("\tcarry = loval & 1;\n");
1997 printf ("\tval = (((val << 1) | GET_XFLG) << cnt) | (loval >> 1);\n");
1998 printf ("\tSET_XFLG (carry);\n");
1999 printf ("\tval &= %s;\n", bit_mask (curi->size));
2001 printf ("\tSET_CFLG (GET_XFLG);\n");
2002 genflags (flag_logical_noclobber, curi->size, "val", "", "");
2003 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
2004 if(curi->size==sz_long)
2005 strcpy(exactCpuCycles," return (8+retcycles*2);");
2007 strcpy(exactCpuCycles," return (6+retcycles*2);");
2010 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
2011 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
2013 switch (curi->size) {
2014 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
2015 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
2016 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2019 printf ("\tcnt &= 63;\n");
2020 printf ("\tretcycles = cnt;\n");
2021 printf ("\tCLEAR_CZNV;\n");
2022 if (source_is_imm1_8 (curi))
2025 force_range_for_rox ("cnt", curi->size);
2026 printf ("\tif (cnt > 0) {\n");
2028 printf ("\tcnt--;\n");
2029 printf ("\t{\n\tuint32_t carry;\n");
2030 printf ("\tuint32_t hival = (val << 1) | GET_XFLG;\n");
2031 printf ("\thival <<= (%d - cnt);\n", bit_size (curi->size) - 1);
2032 printf ("\tval >>= cnt;\n");
2033 printf ("\tcarry = val & 1;\n");
2034 printf ("\tval >>= 1;\n");
2035 printf ("\tval |= hival;\n");
2036 printf ("\tSET_XFLG (carry);\n");
2037 printf ("\tval &= %s;\n", bit_mask (curi->size));
2039 printf ("\tSET_CFLG (GET_XFLG);\n");
2040 genflags (flag_logical_noclobber, curi->size, "val", "", "");
2041 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
2042 if(curi->size==sz_long)
2043 strcpy(exactCpuCycles," return (8+retcycles*2);");
2045 strcpy(exactCpuCycles," return (6+retcycles*2);");
2048 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2050 switch (curi->size) {
2051 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
2052 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
2053 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2056 printf ("\tuint32_t sign = %s & val;\n", cmask (curi->size));
2057 printf ("\tuint32_t cflg = val & 1;\n");
2058 printf ("\tval = (val >> 1) | sign;\n");
2059 genflags (flag_logical, curi->size, "val", "", "");
2060 printf ("\tSET_CFLG (cflg);\n");
2062 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2065 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2067 switch (curi->size) {
2068 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
2069 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
2070 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2073 printf ("\tuint32_t sign = %s & val;\n", cmask (curi->size));
2074 printf ("\tuint32_t sign2;\n");
2075 printf ("\tval <<= 1;\n");
2076 genflags (flag_logical, curi->size, "val", "", "");
2077 printf ("\tsign2 = %s & val;\n", cmask (curi->size));
2078 printf ("\tSET_CFLG (sign != 0);\n");
2081 printf ("\tSET_VFLG (GET_VFLG | (sign2 != sign));\n");
2082 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2085 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2087 switch (curi->size) {
2088 case sz_byte: printf ("\tuint32_t val = (uint8_t)data;\n"); break;
2089 case sz_word: printf ("\tuint32_t val = (uint16_t)data;\n"); break;
2090 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2093 printf ("\tuint32_t carry = val & 1;\n");
2094 printf ("\tval >>= 1;\n");
2095 genflags (flag_logical, curi->size, "val", "", "");
2096 printf ("SET_CFLG (carry);\n");
2098 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2101 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2103 switch (curi->size) {
2104 case sz_byte: printf ("\tuint8_t val = data;\n"); break;
2105 case sz_word: printf ("\tuint16_t val = data;\n"); break;
2106 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2109 printf ("\tuint32_t carry = val & %s;\n", cmask (curi->size));
2110 printf ("\tval <<= 1;\n");
2111 genflags (flag_logical, curi->size, "val", "", "");
2112 printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
2114 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2117 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2119 switch (curi->size) {
2120 case sz_byte: printf ("\tuint8_t val = data;\n"); break;
2121 case sz_word: printf ("\tuint16_t val = data;\n"); break;
2122 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2125 printf ("\tuint32_t carry = val & %s;\n", cmask (curi->size));
2126 printf ("\tval <<= 1;\n");
2127 printf ("\tif (carry) val |= 1;\n");
2128 genflags (flag_logical, curi->size, "val", "", "");
2129 printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
2130 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2133 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2135 switch (curi->size) {
2136 case sz_byte: printf ("\tuint8_t val = data;\n"); break;
2137 case sz_word: printf ("\tuint16_t val = data;\n"); break;
2138 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2141 printf ("\tuint32_t carry = val & 1;\n");
2142 printf ("\tval >>= 1;\n");
2143 printf ("\tif (carry) val |= %s;\n", cmask (curi->size));
2144 genflags (flag_logical, curi->size, "val", "", "");
2145 printf ("SET_CFLG (carry);\n");
2146 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2149 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2151 switch (curi->size) {
2152 case sz_byte: printf ("\tuint8_t val = data;\n"); break;
2153 case sz_word: printf ("\tuint16_t val = data;\n"); break;
2154 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2157 printf ("\tuint32_t carry = val & %s;\n", cmask (curi->size));
2158 printf ("\tval <<= 1;\n");
2159 printf ("\tif (GET_XFLG) val |= 1;\n");
2160 genflags (flag_logical, curi->size, "val", "", "");
2161 printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
2163 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2166 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
2168 switch (curi->size) {
2169 case sz_byte: printf ("\tuint8_t val = data;\n"); break;
2170 case sz_word: printf ("\tuint16_t val = data;\n"); break;
2171 case sz_long: printf ("\tuint32_t val = data;\n"); break;
2174 printf ("\tuint32_t carry = val & 1;\n");
2175 printf ("\tval >>= 1;\n");
2176 printf ("\tif (GET_XFLG) val |= %s;\n", cmask (curi->size));
2177 genflags (flag_logical, curi->size, "val", "", "");
2178 printf ("SET_CFLG (carry);\n");
2180 genastore ("val", curi->smode, "srcreg", curi->size, "data");
2183 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
2185 printf ("\tint regno = (src >> 12) & 15;\n");
2186 printf ("\tuint32_t *regp = regs.regs + regno;\n");
2187 printf ("\tif (! m68k_movec2(src & 0xFFF, regp)) goto %s;\n", endlabelstr);
2190 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
2192 printf ("\tint regno = (src >> 12) & 15;\n");
2193 printf ("\tuint32_t *regp = regs.regs + regno;\n");
2194 printf ("\tif (! m68k_move2c(src & 0xFFF, regp)) goto %s;\n", endlabelstr);
2198 int old_brace_level;
2199 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
2200 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
2202 printf ("\tint ru = (src >> 6) & 7;\n");
2203 printf ("\tint rc = src & 7;\n");
2204 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc)", "dst");
2205 printf ("\tif (GET_ZFLG)");
2206 old_brace_level = n_braces;
2208 genastore ("(m68k_dreg(regs, ru))", curi->dmode, "dstreg", curi->size, "dst");
2209 pop_braces (old_brace_level);
2212 printf ("m68k_dreg(regs, rc) = dst;\n");
2213 pop_braces (old_brace_level);
2217 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2218 printf ("\tuint32_t rn1 = regs.regs[(extra >> 28) & 15];\n");
2219 printf ("\tuint32_t rn2 = regs.regs[(extra >> 12) & 15];\n");
2220 if (curi->size == sz_word) {
2221 int old_brace_level = n_braces;
2222 printf ("\tuint16_t dst1 = m68k_read_memory_16(rn1), dst2 = m68k_read_memory_16(rn2);\n");
2223 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, (extra >> 16) & 7)", "dst1");
2224 printf ("\tif (GET_ZFLG) {\n");
2225 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, extra & 7)", "dst2");
2226 printf ("\tif (GET_ZFLG) {\n");
2227 printf ("\tm68k_write_memory_16(rn1, m68k_dreg(regs, (extra >> 22) & 7));\n");
2228 printf ("\tm68k_write_memory_16(rn1, m68k_dreg(regs, (extra >> 6) & 7));\n");
2230 pop_braces (old_brace_level);
2231 printf ("\tif (! GET_ZFLG) {\n");
2232 printf ("\tm68k_dreg(regs, (extra >> 22) & 7) = (m68k_dreg(regs, (extra >> 22) & 7) & ~0xffff) | (dst1 & 0xffff);\n");
2233 printf ("\tm68k_dreg(regs, (extra >> 6) & 7) = (m68k_dreg(regs, (extra >> 6) & 7) & ~0xffff) | (dst2 & 0xffff);\n");
2236 int old_brace_level = n_braces;
2237 printf ("\tuint32_t dst1 = m68k_read_memory_32(rn1), dst2 = m68k_read_memory_32(rn2);\n");
2238 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, (extra >> 16) & 7)", "dst1");
2239 printf ("\tif (GET_ZFLG) {\n");
2240 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, extra & 7)", "dst2");
2241 printf ("\tif (GET_ZFLG) {\n");
2242 printf ("\tm68k_write_memory_32(rn1, m68k_dreg(regs, (extra >> 22) & 7));\n");
2243 printf ("\tm68k_write_memory_32(rn1, m68k_dreg(regs, (extra >> 6) & 7));\n");
2245 pop_braces (old_brace_level);
2246 printf ("\tif (! GET_ZFLG) {\n");
2247 printf ("\tm68k_dreg(regs, (extra >> 22) & 7) = dst1;\n");
2248 printf ("\tm68k_dreg(regs, (extra >> 6) & 7) = dst2;\n");
2252 case i_MOVES: /* ignore DFC and SFC because we have no MMU */
2254 int old_brace_level;
2255 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2256 printf ("\tif (extra & 0x800)\n");
2257 old_brace_level = n_braces;
2259 printf ("\tuint32_t src = regs.regs[(extra >> 12) & 15];\n");
2260 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
2261 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
2262 pop_braces (old_brace_level);
2265 genamode (curi->dmode, "dstreg", curi->size, "src", 1, 0);
2266 printf ("\tif (extra & 0x8000) {\n");
2267 switch (curi->size) {
2268 case sz_byte: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = (int32_t)(int8_t)src;\n"); break;
2269 case sz_word: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = (int32_t)(int16_t)src;\n"); break;
2270 case sz_long: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = src;\n"); break;
2273 printf ("\t} else {\n");
2274 genastore ("src", Dreg, "(extra >> 12) & 7", curi->size, "");
2276 pop_braces (old_brace_level);
2279 case i_BKPT: /* only needed for hardware emulators */
2281 printf ("\top_illg(opcode);\n");
2283 case i_CALLM: /* not present in 68030 */
2285 printf ("\top_illg(opcode);\n");
2287 case i_RTM: /* not present in 68030 */
2289 printf ("\top_illg(opcode);\n");
2292 if (curi->smode != am_unknown && curi->smode != am_illg)
2293 genamode (curi->smode, "srcreg", curi->size, "dummy", 1, 0);
2294 printf ("\tif (cctrue(%d)) { Exception(7,m68k_getpc(),M68000_EXC_SRC_CPU); goto %s; }\n", curi->cc, endlabelstr);
2300 printf ("\tuint32_t oldpc = m68k_getpc();\n");
2301 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2302 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
2304 printf ("\tm68k_divl(opcode, dst, extra, oldpc);\n");
2307 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2308 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
2310 printf ("\tm68k_mull(opcode, dst, extra);\n");
2320 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2321 genamode (curi->dmode, "dstreg", sz_long, "dst", 2, 0);
2323 printf ("\tint32_t offset = extra & 0x800 ? m68k_dreg(regs, (extra >> 6) & 7) : (extra >> 6) & 0x1f;\n");
2324 printf ("\tint width = (((extra & 0x20 ? m68k_dreg(regs, extra & 7) : extra) -1) & 0x1f) +1;\n");
2325 if (curi->dmode == Dreg) {
2326 printf ("\tuint32_t tmp = m68k_dreg(regs, dstreg) << (offset & 0x1f);\n");
2328 printf ("\tuint32_t tmp,bf0,bf1;\n");
2329 printf ("\tdsta += (offset >> 3) | (offset & 0x80000000 ? ~0x1fffffff : 0);\n");
2330 printf ("\tbf0 = m68k_read_memory_32(dsta);bf1 = m68k_read_memory_8(dsta+4) & 0xff;\n");
2331 printf ("\ttmp = (bf0 << (offset & 7)) | (bf1 >> (8 - (offset & 7)));\n");
2333 printf ("\ttmp >>= (32 - width);\n");
2334 printf ("\tSET_NFLG (tmp & (1 << (width-1)) ? 1 : 0);\n");
2335 printf ("\tSET_ZFLG (tmp == 0); SET_VFLG (0); SET_CFLG (0);\n");
2336 switch (curi->mnemo) {
2340 printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = tmp;\n");
2343 printf ("\ttmp = ~tmp;\n");
2346 printf ("\tif (GET_NFLG) tmp |= width == 32 ? 0 : (-1 << width);\n");
2347 printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = tmp;\n");
2350 printf ("\ttmp = 0;\n");
2353 printf ("\t{ uint32_t mask = 1 << (width-1);\n");
2354 printf ("\twhile (mask) { if (tmp & mask) break; mask >>= 1; offset++; }}\n");
2355 printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = offset;\n");
2358 printf ("\ttmp = 0xffffffff;\n");
2361 printf ("\ttmp = m68k_dreg(regs, (extra >> 12) & 7);\n");
2362 printf ("\tSET_NFLG (tmp & (1 << (width - 1)) ? 1 : 0);\n");
2363 printf ("\tSET_ZFLG (tmp == 0);\n");
2368 if (curi->mnemo == i_BFCHG
2369 || curi->mnemo == i_BFCLR
2370 || curi->mnemo == i_BFSET
2371 || curi->mnemo == i_BFINS)
2373 printf ("\ttmp <<= (32 - width);\n");
2374 if (curi->dmode == Dreg) {
2375 printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & ((offset & 0x1f) == 0 ? 0 :\n");
2376 printf ("\t\t(0xffffffff << (32 - (offset & 0x1f))))) |\n");
2377 printf ("\t\t(tmp >> (offset & 0x1f)) |\n");
2378 printf ("\t\t(((offset & 0x1f) + width) >= 32 ? 0 :\n");
2379 printf (" (m68k_dreg(regs, dstreg) & ((uint32_t)0xffffffff >> ((offset & 0x1f) + width))));\n");
2381 printf ("\tbf0 = (bf0 & (0xff000000 << (8 - (offset & 7)))) |\n");
2382 printf ("\t\t(tmp >> (offset & 7)) |\n");
2383 printf ("\t\t(((offset & 7) + width) >= 32 ? 0 :\n");
2384 printf ("\t\t (bf0 & ((uint32_t)0xffffffff >> ((offset & 7) + width))));\n");
2385 printf ("\tm68k_write_memory_32(dsta,bf0 );\n");
2386 printf ("\tif (((offset & 7) + width) > 32) {\n");
2387 printf ("\t\tbf1 = (bf1 & (0xff >> (width - 32 + (offset & 7)))) |\n");
2388 printf ("\t\t\t(tmp << (8 - (offset & 7)));\n");
2389 printf ("\t\tm68k_write_memory_8(dsta+4,bf1);\n");
2395 if (curi->smode == Dreg) {
2396 printf ("\tuint16_t val = m68k_dreg(regs, srcreg) + %s;\n", gen_nextiword ());
2397 printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & 0xffffff00) | ((val >> 4) & 0xf0) | (val & 0xf);\n");
2399 printf ("\tuint16_t val;\n");
2400 printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2401 printf ("\tval = (uint16_t)m68k_read_memory_8(m68k_areg(regs, srcreg));\n");
2402 printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2403 printf ("\tval = (val | ((uint16_t)m68k_read_memory_8(m68k_areg(regs, srcreg)) << 8)) + %s;\n", gen_nextiword ());
2404 printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2405 printf ("\tm68k_write_memory_8(m68k_areg(regs, dstreg),((val >> 4) & 0xf0) | (val & 0xf));\n");
2409 if (curi->smode == Dreg) {
2410 printf ("\tuint16_t val = m68k_dreg(regs, srcreg);\n");
2411 printf ("\tval = (((val << 4) & 0xf00) | (val & 0xf)) + %s;\n", gen_nextiword ());
2412 printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & 0xffff0000) | (val & 0xffff);\n");
2414 printf ("\tuint16_t val;\n");
2415 printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2416 printf ("\tval = (uint16_t)m68k_read_memory_8(m68k_areg(regs, srcreg));\n");
2417 printf ("\tval = (((val << 4) & 0xf00) | (val & 0xf)) + %s;\n", gen_nextiword ());
2418 printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2419 printf ("\tm68k_write_memory_8(m68k_areg(regs, dstreg),val);\n");
2420 printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2421 printf ("\tm68k_write_memory_8(m68k_areg(regs, dstreg),val >> 8);\n");
2425 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
2426 genflags (flag_logical, curi->size, "src", "", "");
2427 printf ("\tsrc |= 0x80;\n");
2428 genastore ("src", curi->smode, "srcreg", curi->size, "src");
2429 if( curi->smode!=Dreg ) insn_n_cycles += 2;
2432 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2434 printf ("\tfpp_opp(opcode,extra);\n");
2437 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2439 printf ("\tfdbcc_opp(opcode,extra);\n");
2442 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2444 printf ("\tfscc_opp(opcode,extra);\n");
2449 printf ("\tuint32_t oldpc = m68k_getpc();\n");
2450 if (curi->smode != am_unknown && curi->smode != am_illg)
2451 genamode (curi->smode, "srcreg", curi->size, "dummy", 1, 0);
2453 printf ("\tftrapcc_opp(opcode,oldpc);\n");
2458 printf ("\tuint32_t pc = m68k_getpc();\n");
2459 genamode (curi->dmode, "srcreg", curi->size, "extra", 1, 0);
2461 printf ("\tfbcc_opp(opcode,pc,extra);\n");
2465 printf ("\tfsave_opp(opcode);\n");
2469 printf ("\tfrestore_opp(opcode);\n");
2480 if ((opcode & 0xfff8) == 0xf620) {
2481 /* MOVE16 (Ax)+,(Ay)+ */
2482 printf ("\tuint32_t mems = m68k_areg(regs, srcreg) & ~15, memd;\n");
2483 printf ("\tdstreg = (%s >> 12) & 7;\n", gen_nextiword());
2484 printf ("\tmemd = m68k_areg(regs, dstreg) & ~15;\n");
2485 printf ("\tm68k_write_memory_32(memd, m68k_read_memory_32(mems));\n");
2486 printf ("\tm68k_write_memory_32(memd+4, m68k_read_memory_32(mems+4));\n");
2487 printf ("\tm68k_write_memory_32(memd+8, m68k_read_memory_32(mems+8));\n");
2488 printf ("\tm68k_write_memory_32(memd+12, m68k_read_memory_32(mems+12));\n");
2489 printf ("\tif (srcreg != dstreg)\n");
2490 printf ("\tm68k_areg(regs, srcreg) += 16;\n");
2491 printf ("\tm68k_areg(regs, dstreg) += 16;\n");
2493 /* Other variants */
2494 genamode (curi->smode, "srcreg", curi->size, "mems", 0, 2);
2495 genamode (curi->dmode, "dstreg", curi->size, "memd", 0, 2);
2496 printf ("\tmemsa &= ~15;\n");
2497 printf ("\tmemda &= ~15;\n");
2498 printf ("\tm68k_write_memory_32(memda, m68k_read_memory_32(memsa));\n");
2499 printf ("\tm68k_write_memory_32(memda+4, m68k_read_memory_32(memsa+4));\n");
2500 printf ("\tm68k_write_memory_32(memda+8, m68k_read_memory_32(memsa+8));\n");
2501 printf ("\tm68k_write_memory_32(memda+12, m68k_read_memory_32(memsa+12));\n");
2502 if ((opcode & 0xfff8) == 0xf600)
2503 printf ("\tm68k_areg(regs, srcreg) += 16;\n");
2504 else if ((opcode & 0xfff8) == 0xf608)
2505 printf ("\tm68k_areg(regs, dstreg) += 16;\n");
2510 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2512 printf ("\tmmu_op(opcode,extra);\n");
2522 static void generate_includes(FILE * f)
2524 //JLH:no fprintf(f, "#include \"sysdeps.h\"\n");
2525 //JLH:no fprintf(f, "#include \"hatari-glue.h\"\n");
2526 //JLH:no fprintf(f, "#include \"maccess.h\"\n");
2527 //JLH:no fprintf(f, "#include \"memory.h\"\n");
2528 //JLH:no fprintf(f, "#include \"newcpu.h\"\n");
2529 fprintf(f, "#include \"cpudefs.h\"\n");
2530 fprintf(f, "#include \"cpuextra.h\"\n");
2531 fprintf(f, "#include \"inlines.h\"\n");
2532 fprintf(f, "#include \"cputbl.h\"\n");
2533 fprintf(f, "#define CPUFUNC(x) x##_ff\n"
2535 "#include \"noflags.h\"\n"
2539 // JLH: Since this is stuff that should be generated in a file that creates
2540 // constants, it's in here now. :-P
2541 static void GenerateTables(FILE * f)
2545 fprintf(f, "\nconst int areg_byteinc[] = { 1, 1, 1, 1, 1, 1, 1, 2 };\n");
2546 fprintf(f, "const int imm8_table[] = { 8, 1, 2, 3, 4, 5, 6, 7 };\n\n");
2547 fprintf(f, "const int movem_index1[256] = {\n");
2549 for(i=0; i<256; i++)
2555 fprintf(f, "0x%02X, ", j);
2561 fprintf(f, "};\n\n");
2562 fprintf(f, "const int movem_index2[256] = {\n");
2564 for(i=0; i<256; i++)
2570 fprintf(f, "0x%02X, ", 7 - j);
2576 fprintf(f, "};\n\n");
2577 fprintf(f, "const int movem_next[256] = {\n");
2579 for(i=0; i<256; i++)
2585 fprintf(f, "0x%02X, ", i & (~(1 << j)));
2591 fprintf(f, "};\n\n");
2596 static void generate_one_opcode (int rp)
2599 uint16_t smsk, dmsk;
2600 long int opcode = opcode_map[rp];
2602 exactCpuCycles[0] = 0; /* Default: not used */
2604 if (table68k[opcode].mnemo == i_ILLG
2605 || table68k[opcode].clev > cpu_level)
2608 for (i = 0; lookuptab[i].name[0]; i++) {
2609 if (table68k[opcode].mnemo == lookuptab[i].mnemo)
2613 if (table68k[opcode].handler != -1)
2616 if (opcode_next_clev[rp] != cpu_level) {
2617 fprintf (stblfile, "{ CPUFUNC(op_%lx_%d), 0, %ld }, /* %s */\n", opcode, opcode_last_postfix[rp],
2618 opcode, lookuptab[i].name);
2621 fprintf (stblfile, "{ CPUFUNC(op_%lx_%d), 0, %ld }, /* %s */\n", opcode, postfix, opcode, lookuptab[i].name);
2622 fprintf (headerfile, "extern cpuop_func op_%lx_%d_nf;\n", opcode, postfix);
2623 fprintf (headerfile, "extern cpuop_func op_%lx_%d_ff;\n", opcode, postfix);
2624 printf ("unsigned long CPUFUNC(op_%lx_%d)(uint32_t opcode) /* %s */\n{\n", opcode, postfix, lookuptab[i].name);
2626 switch (table68k[opcode].stype) {
2627 case 0: smsk = 7; break;
2628 case 1: smsk = 255; break;
2629 case 2: smsk = 15; break;
2630 case 3: smsk = 7; break;
2631 case 4: smsk = 7; break;
2632 case 5: smsk = 63; break;
2633 case 7: smsk = 3; break;
2638 next_cpu_level = -1;
2639 if (table68k[opcode].suse
2640 && table68k[opcode].smode != imm && table68k[opcode].smode != imm0
2641 && table68k[opcode].smode != imm1 && table68k[opcode].smode != imm2
2642 && table68k[opcode].smode != absw && table68k[opcode].smode != absl
2643 && table68k[opcode].smode != PC8r && table68k[opcode].smode != PC16)
2645 if (table68k[opcode].spos == -1) {
2646 if (((int) table68k[opcode].sreg) >= 128)
2647 printf ("\tuint32_t srcreg = (int32_t)(int8_t)%d;\n", (int) table68k[opcode].sreg);
2649 printf ("\tuint32_t srcreg = %d;\n", (int) table68k[opcode].sreg);
2652 int pos = table68k[opcode].spos;
2655 sprintf (source, "((opcode >> %d) & %d)", pos, smsk);
2657 sprintf (source, "(opcode & %d)", smsk);
2659 if (table68k[opcode].stype == 3)
2660 printf ("\tuint32_t srcreg = imm8_table[%s];\n", source);
2661 else if (table68k[opcode].stype == 1)
2662 printf ("\tuint32_t srcreg = (int32_t)(int8_t)%s;\n", source);
2664 printf ("\tuint32_t srcreg = %s;\n", source);
2667 if (table68k[opcode].duse
2668 /* Yes, the dmode can be imm, in case of LINK or DBcc */
2669 && table68k[opcode].dmode != imm && table68k[opcode].dmode != imm0
2670 && table68k[opcode].dmode != imm1 && table68k[opcode].dmode != imm2
2671 && table68k[opcode].dmode != absw && table68k[opcode].dmode != absl)
2673 if (table68k[opcode].dpos == -1) {
2674 if (((int) table68k[opcode].dreg) >= 128)
2675 printf ("\tuint32_t dstreg = (int32_t)(int8_t)%d;\n", (int) table68k[opcode].dreg);
2677 printf ("\tuint32_t dstreg = %d;\n", (int) table68k[opcode].dreg);
2679 int pos = table68k[opcode].dpos;
2681 /* Check that we can do the little endian optimization safely. */
2682 if (pos < 8 && (dmsk >> (8 - pos)) != 0)
2686 printf ("\tuint32_t dstreg = (opcode >> %d) & %d;\n",
2689 printf ("\tuint32_t dstreg = opcode & %d;\n", dmsk);
2694 sprintf (endlabelstr, "endlabel%d", endlabelno);
2695 if(table68k[opcode].mnemo==i_ASR || table68k[opcode].mnemo==i_ASL || table68k[opcode].mnemo==i_LSR || table68k[opcode].mnemo==i_LSL
2696 || table68k[opcode].mnemo==i_ROL || table68k[opcode].mnemo==i_ROR || table68k[opcode].mnemo==i_ROXL || table68k[opcode].mnemo==i_ROXR
2697 || table68k[opcode].mnemo==i_MVMEL || table68k[opcode].mnemo==i_MVMLE
2698 || table68k[opcode].mnemo==i_MULU || table68k[opcode].mnemo==i_MULS
2699 || table68k[opcode].mnemo==i_DIVU || table68k[opcode].mnemo==i_DIVS )
2700 printf("\tunsigned int retcycles = 0;\n");
2701 gen_opcode (opcode);
2703 printf ("%s: ;\n", endlabelstr);
2705 if (strlen(exactCpuCycles) > 0)
2706 printf("%s\n",exactCpuCycles);
2708 printf ("return %d;\n", insn_n_cycles);
2709 /* Now patch in the instruction cycles at the beginning of the function: */
2710 fseek(stdout, nCurInstrCycPos, SEEK_SET);
2711 printf("%d;", insn_n_cycles);
2712 fseek(stdout, 0, SEEK_END);
2715 opcode_next_clev[rp] = next_cpu_level;
2716 opcode_last_postfix[rp] = postfix;
2719 static void generate_func(void)
2724 using_exception_3 = 0;
2726 // for(i=0; i<6; i++)
2727 //For some reason, this doesn't work properly. Seems something is making a bad
2728 //assumption somewhere.
2729 //and it's probably in opcode_next_clev[rp]...
2735 for(rp=0; rp<nr_cpuop_funcs; rp++)
2736 opcode_next_clev[rp] = 0;
2742 using_exception_3 = 1;
2744 for(rp=0; rp<nr_cpuop_funcs; rp++)
2745 opcode_next_clev[rp] = 0;
2749 fprintf(stblfile, "const struct cputbl CPUFUNC(op_smalltbl_%d)[] = {\n", postfix);
2751 /* sam: this is for people with low memory (eg. me :)) */
2753 "#if !defined(PART_1) && !defined(PART_2) && "
2754 "!defined(PART_3) && !defined(PART_4) && "
2755 "!defined(PART_5) && !defined(PART_6) && "
2756 "!defined(PART_7) && !defined(PART_8)"
2758 "#define PART_1 1\n"
2759 "#define PART_2 1\n"
2760 "#define PART_3 1\n"
2761 "#define PART_4 1\n"
2762 "#define PART_5 1\n"
2763 "#define PART_6 1\n"
2764 "#define PART_7 1\n"
2765 "#define PART_8 1\n"
2772 int k = (j * nr_cpuop_funcs) / 8;
2773 printf("#ifdef PART_%d\n", j);
2776 generate_one_opcode(rp);
2778 printf ("#endif\n\n");
2781 fprintf(stblfile, "{ 0, 0, 0 }};\n");
2785 int main(int argc, char ** argv)
2790 opcode_map = (int *)malloc(sizeof(int) * nr_cpuop_funcs);
2791 opcode_last_postfix = (int *)malloc(sizeof(int) * nr_cpuop_funcs);
2792 opcode_next_clev = (int *)malloc(sizeof(int) * nr_cpuop_funcs);
2793 counts = (unsigned long *)malloc(65536 * sizeof(unsigned long));
2796 /* It would be a lot nicer to put all in one file (we'd also get rid of
2797 * cputbl.h that way), but cpuopti can't cope. That could be fixed, but
2798 * I don't dare to touch the 68k version. */
2800 headerfile = fopen("cputbl.h", "wb");
2801 stblfile = fopen("cpustbl.c", "wb");
2803 if (freopen("cpuemu.c", "wb", stdout) == NULL)
2809 generate_includes(stdout);
2810 generate_includes(stblfile);
2812 GenerateTables(stdout);