ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/uae_cpu/gencpu.c
Revision: 1.15
Committed: 2001-08-19T16:21:01Z (23 years, 3 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.14: +17 -19 lines
Log Message:
- merge 680x0 emulation core with uae 0.8.17

File Contents

# Content
1 /*
2 * UAE - The Un*x Amiga Emulator
3 *
4 * MC68000 emulation generator
5 *
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
12 * call abort().
13 * The generated code is sometimes sub-optimal, an optimizing compiler should
14 * take care of this.
15 *
16 * Copyright 1995, 1996 Bernd Schmidt
17 */
18
19 #include <ctype.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "sysdeps.h"
25 #include "readcpu.h"
26
27 #if defined(SPARC_V8_ASSEMBLY) || defined(SPARC_V9_ASSEMBLY)
28 #define SPARC_ASSEMBLY 0
29 #endif
30
31 #define BOOL_TYPE "int"
32
33 static FILE *headerfile;
34 static FILE *stblfile;
35
36 static int using_prefetch;
37 static int using_exception_3;
38 static int cpu_level;
39
40 /* For the current opcode, the next lower level that will have different code.
41 * Initialized to -1 for each opcode. If it remains unchanged, indicates we
42 * are done with that opcode. */
43 static int next_cpu_level;
44
45 static int *opcode_map;
46 static int *opcode_next_clev;
47 static int *opcode_last_postfix;
48 static unsigned long *counts;
49
50 static void read_counts (void)
51 {
52 FILE *file;
53 unsigned long opcode, count, total;
54 char name[20];
55 int nr = 0;
56 memset (counts, 0, 65536 * sizeof *counts);
57
58 file = fopen ("frequent.68k", "r");
59 if (file) {
60 fscanf (file, "Total: %lu\n", &total);
61 while (fscanf (file, "%lx: %lu %s\n", &opcode, &count, name) == 3) {
62 opcode_next_clev[nr] = 4;
63 opcode_last_postfix[nr] = -1;
64 opcode_map[nr++] = opcode;
65 counts[opcode] = count;
66 }
67 fclose (file);
68 }
69 if (nr == nr_cpuop_funcs)
70 return;
71 for (opcode = 0; opcode < 0x10000; opcode++) {
72 if (table68k[opcode].handler == -1 && table68k[opcode].mnemo != i_ILLG
73 && counts[opcode] == 0)
74 {
75 opcode_next_clev[nr] = 4;
76 opcode_last_postfix[nr] = -1;
77 opcode_map[nr++] = opcode;
78 counts[opcode] = count;
79 }
80 }
81 if (nr != nr_cpuop_funcs)
82 abort ();
83 }
84
85 static char endlabelstr[80];
86 static int endlabelno = 0;
87 static int need_endlabel;
88
89 static int n_braces = 0;
90 static int m68k_pc_offset = 0;
91 static int insn_n_cycles;
92
93 static void start_brace (void)
94 {
95 n_braces++;
96 printf ("{");
97 }
98
99 static void close_brace (void)
100 {
101 assert (n_braces > 0);
102 n_braces--;
103 printf ("}");
104 }
105
106 static void finish_braces (void)
107 {
108 while (n_braces > 0)
109 close_brace ();
110 }
111
112 static void pop_braces (int to)
113 {
114 while (n_braces > to)
115 close_brace ();
116 }
117
118 static int bit_size (int size)
119 {
120 switch (size) {
121 case sz_byte: return 8;
122 case sz_word: return 16;
123 case sz_long: return 32;
124 default: abort ();
125 }
126 return 0;
127 }
128
129 static const char *bit_mask (int size)
130 {
131 switch (size) {
132 case sz_byte: return "0xff";
133 case sz_word: return "0xffff";
134 case sz_long: return "0xffffffff";
135 default: abort ();
136 }
137 return 0;
138 }
139
140 static const char *gen_nextilong (void)
141 {
142 static char buffer[80];
143 int r = m68k_pc_offset;
144 m68k_pc_offset += 4;
145
146 insn_n_cycles += 4;
147
148 if (using_prefetch)
149 sprintf (buffer, "get_ilong_prefetch(%d)", r);
150 else
151 sprintf (buffer, "get_ilong(%d)", r);
152 return buffer;
153 }
154
155 static const char *gen_nextiword (void)
156 {
157 static char buffer[80];
158 int r = m68k_pc_offset;
159 m68k_pc_offset += 2;
160
161 insn_n_cycles += 2;
162
163 if (using_prefetch)
164 sprintf (buffer, "get_iword_prefetch(%d)", r);
165 else
166 sprintf (buffer, "get_iword(%d)", r);
167 return buffer;
168 }
169
170 static const char *gen_nextibyte (void)
171 {
172 static char buffer[80];
173 int r = m68k_pc_offset;
174 m68k_pc_offset += 2;
175
176 insn_n_cycles += 2;
177
178 if (using_prefetch)
179 sprintf (buffer, "get_ibyte_prefetch(%d)", r);
180 else
181 sprintf (buffer, "get_ibyte(%d)", r);
182 return buffer;
183 }
184
185 static void fill_prefetch_0 (void)
186 {
187 if (using_prefetch)
188 printf ("fill_prefetch_0 ();\n");
189 }
190
191 static void fill_prefetch_2 (void)
192 {
193 if (using_prefetch)
194 printf ("fill_prefetch_2 ();\n");
195 }
196
197 static void swap_opcode (void)
198 {
199 printf ("#ifdef HAVE_GET_WORD_UNSWAPPED\n");
200 printf ("\topcode = ((opcode << 8) & 0xFF00) | ((opcode >> 8) & 0xFF);\n");
201 printf ("#endif\n");
202 }
203
204 static void sync_m68k_pc (void)
205 {
206 if (m68k_pc_offset == 0)
207 return;
208 printf ("m68k_incpc(%d);\n", m68k_pc_offset);
209 switch (m68k_pc_offset) {
210 case 0:
211 /*fprintf (stderr, "refilling prefetch at 0\n"); */
212 break;
213 case 2:
214 fill_prefetch_2 ();
215 break;
216 default:
217 fill_prefetch_0 ();
218 break;
219 }
220 m68k_pc_offset = 0;
221 }
222
223 /* getv == 1: fetch data; getv != 0: check for odd address. If movem != 0,
224 * the calling routine handles Apdi and Aipi modes.
225 * gb-- movem == 2 means the same thing but for a MOVE16 instruction */
226 static void genamode (amodes mode, char *reg, wordsizes size, char *name, int getv, int movem)
227 {
228 start_brace ();
229 switch (mode) {
230 case Dreg:
231 if (movem)
232 abort ();
233 if (getv == 1)
234 switch (size) {
235 case sz_byte:
236 #if defined(AMIGA) && !defined(WARPUP)
237 /* sam: I don't know why gcc.2.7.2.1 produces a code worse */
238 /* if it is not done like that: */
239 printf ("\tuae_s8 %s = ((uae_u8*)&m68k_dreg(regs, %s))[3];\n", name, reg);
240 #else
241 printf ("\tuae_s8 %s = m68k_dreg(regs, %s);\n", name, reg);
242 #endif
243 break;
244 case sz_word:
245 #if defined(AMIGA) && !defined(WARPUP)
246 printf ("\tuae_s16 %s = ((uae_s16*)&m68k_dreg(regs, %s))[1];\n", name, reg);
247 #else
248 printf ("\tuae_s16 %s = m68k_dreg(regs, %s);\n", name, reg);
249 #endif
250 break;
251 case sz_long:
252 printf ("\tuae_s32 %s = m68k_dreg(regs, %s);\n", name, reg);
253 break;
254 default:
255 abort ();
256 }
257 return;
258 case Areg:
259 if (movem)
260 abort ();
261 if (getv == 1)
262 switch (size) {
263 case sz_word:
264 printf ("\tuae_s16 %s = m68k_areg(regs, %s);\n", name, reg);
265 break;
266 case sz_long:
267 printf ("\tuae_s32 %s = m68k_areg(regs, %s);\n", name, reg);
268 break;
269 default:
270 abort ();
271 }
272 return;
273 case Aind:
274 printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
275 break;
276 case Aipi:
277 printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
278 break;
279 case Apdi:
280 switch (size) {
281 case sz_byte:
282 if (movem)
283 printf ("\tuaecptr %sa = m68k_areg(regs, %s);\n", name, reg);
284 else
285 printf ("\tuaecptr %sa = m68k_areg(regs, %s) - areg_byteinc[%s];\n", name, reg, reg);
286 break;
287 case sz_word:
288 printf ("\tuaecptr %sa = m68k_areg(regs, %s) - %d;\n", name, reg, movem ? 0 : 2);
289 break;
290 case sz_long:
291 printf ("\tuaecptr %sa = m68k_areg(regs, %s) - %d;\n", name, reg, movem ? 0 : 4);
292 break;
293 default:
294 abort ();
295 }
296 break;
297 case Ad16:
298 printf ("\tuaecptr %sa = m68k_areg(regs, %s) + (uae_s32)(uae_s16)%s;\n", name, reg, gen_nextiword ());
299 break;
300 case Ad8r:
301 if (cpu_level > 1) {
302 if (next_cpu_level < 1)
303 next_cpu_level = 1;
304 sync_m68k_pc ();
305 start_brace ();
306 printf ("\tuaecptr %sa = get_disp_ea_020(m68k_areg(regs, %s), next_iword());\n", name, reg);
307 } else
308 printf ("\tuaecptr %sa = get_disp_ea_000(m68k_areg(regs, %s), %s);\n", name, reg, gen_nextiword ());
309
310 break;
311 case PC16:
312 printf ("\tuaecptr %sa = m68k_getpc () + %d;\n", name, m68k_pc_offset);
313 printf ("\t%sa += (uae_s32)(uae_s16)%s;\n", name, gen_nextiword ());
314 break;
315 case PC8r:
316 if (cpu_level > 1) {
317 if (next_cpu_level < 1)
318 next_cpu_level = 1;
319 sync_m68k_pc ();
320 start_brace ();
321 printf ("\tuaecptr tmppc = m68k_getpc();\n");
322 printf ("\tuaecptr %sa = get_disp_ea_020(tmppc, next_iword());\n", name);
323 } else {
324 printf ("\tuaecptr tmppc = m68k_getpc() + %d;\n", m68k_pc_offset);
325 printf ("\tuaecptr %sa = get_disp_ea_000(tmppc, %s);\n", name, gen_nextiword ());
326 }
327
328 break;
329 case absw:
330 printf ("\tuaecptr %sa = (uae_s32)(uae_s16)%s;\n", name, gen_nextiword ());
331 break;
332 case absl:
333 printf ("\tuaecptr %sa = %s;\n", name, gen_nextilong ());
334 break;
335 case imm:
336 if (getv != 1)
337 abort ();
338 switch (size) {
339 case sz_byte:
340 printf ("\tuae_s8 %s = %s;\n", name, gen_nextibyte ());
341 break;
342 case sz_word:
343 printf ("\tuae_s16 %s = %s;\n", name, gen_nextiword ());
344 break;
345 case sz_long:
346 printf ("\tuae_s32 %s = %s;\n", name, gen_nextilong ());
347 break;
348 default:
349 abort ();
350 }
351 return;
352 case imm0:
353 if (getv != 1)
354 abort ();
355 printf ("\tuae_s8 %s = %s;\n", name, gen_nextibyte ());
356 return;
357 case imm1:
358 if (getv != 1)
359 abort ();
360 printf ("\tuae_s16 %s = %s;\n", name, gen_nextiword ());
361 return;
362 case imm2:
363 if (getv != 1)
364 abort ();
365 printf ("\tuae_s32 %s = %s;\n", name, gen_nextilong ());
366 return;
367 case immi:
368 if (getv != 1)
369 abort ();
370 printf ("\tuae_u32 %s = %s;\n", name, reg);
371 return;
372 default:
373 abort ();
374 }
375
376 /* We get here for all non-reg non-immediate addressing modes to
377 * actually fetch the value. */
378
379 if (using_exception_3 && getv != 0 && size != sz_byte) {
380 printf ("\tif ((%sa & 1) != 0) {\n", name);
381 printf ("\t\tlast_fault_for_exception_3 = %sa;\n", name);
382 printf ("\t\tlast_op_for_exception_3 = opcode;\n");
383 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + %d;\n", m68k_pc_offset);
384 printf ("\t\tException(3, 0);\n");
385 printf ("\t\tgoto %s;\n", endlabelstr);
386 printf ("\t}\n");
387 need_endlabel = 1;
388 start_brace ();
389 }
390
391 if (getv == 1) {
392 switch (size) {
393 case sz_byte: insn_n_cycles += 2; break;
394 case sz_word: insn_n_cycles += 2; break;
395 case sz_long: insn_n_cycles += 4; break;
396 default: abort ();
397 }
398 start_brace ();
399 switch (size) {
400 case sz_byte: printf ("\tuae_s8 %s = get_byte(%sa);\n", name, name); break;
401 case sz_word: printf ("\tuae_s16 %s = get_word(%sa);\n", name, name); break;
402 case sz_long: printf ("\tuae_s32 %s = get_long(%sa);\n", name, name); break;
403 default: abort ();
404 }
405 }
406
407 /* We now might have to fix up the register for pre-dec or post-inc
408 * addressing modes. */
409 if (!movem)
410 switch (mode) {
411 case Aipi:
412 switch (size) {
413 case sz_byte:
414 printf ("\tm68k_areg(regs, %s) += areg_byteinc[%s];\n", reg, reg);
415 break;
416 case sz_word:
417 printf ("\tm68k_areg(regs, %s) += 2;\n", reg);
418 break;
419 case sz_long:
420 printf ("\tm68k_areg(regs, %s) += 4;\n", reg);
421 break;
422 default:
423 abort ();
424 }
425 break;
426 case Apdi:
427 printf ("\tm68k_areg (regs, %s) = %sa;\n", reg, name);
428 break;
429 default:
430 break;
431 }
432 }
433
434 static void genastore (char *from, amodes mode, char *reg, wordsizes size, char *to)
435 {
436 switch (mode) {
437 case Dreg:
438 switch (size) {
439 case sz_byte:
440 printf ("\tm68k_dreg(regs, %s) = (m68k_dreg(regs, %s) & ~0xff) | ((%s) & 0xff);\n", reg, reg, from);
441 break;
442 case sz_word:
443 printf ("\tm68k_dreg(regs, %s) = (m68k_dreg(regs, %s) & ~0xffff) | ((%s) & 0xffff);\n", reg, reg, from);
444 break;
445 case sz_long:
446 printf ("\tm68k_dreg(regs, %s) = (%s);\n", reg, from);
447 break;
448 default:
449 abort ();
450 }
451 break;
452 case Areg:
453 switch (size) {
454 case sz_word:
455 fprintf (stderr, "Foo\n");
456 printf ("\tm68k_areg(regs, %s) = (uae_s32)(uae_s16)(%s);\n", reg, from);
457 break;
458 case sz_long:
459 printf ("\tm68k_areg(regs, %s) = (%s);\n", reg, from);
460 break;
461 default:
462 abort ();
463 }
464 break;
465 case Aind:
466 case Aipi:
467 case Apdi:
468 case Ad16:
469 case Ad8r:
470 case absw:
471 case absl:
472 case PC16:
473 case PC8r:
474 if (using_prefetch)
475 sync_m68k_pc ();
476 switch (size) {
477 case sz_byte:
478 insn_n_cycles += 2;
479 printf ("\tput_byte(%sa,%s);\n", to, from);
480 break;
481 case sz_word:
482 insn_n_cycles += 2;
483 if (cpu_level < 2 && (mode == PC16 || mode == PC8r))
484 abort ();
485 printf ("\tput_word(%sa,%s);\n", to, from);
486 break;
487 case sz_long:
488 insn_n_cycles += 4;
489 if (cpu_level < 2 && (mode == PC16 || mode == PC8r))
490 abort ();
491 printf ("\tput_long(%sa,%s);\n", to, from);
492 break;
493 default:
494 abort ();
495 }
496 break;
497 case imm:
498 case imm0:
499 case imm1:
500 case imm2:
501 case immi:
502 abort ();
503 break;
504 default:
505 abort ();
506 }
507 }
508
509 static void genmovemel (uae_u16 opcode)
510 {
511 char getcode[100];
512 int size = table68k[opcode].size == sz_long ? 4 : 2;
513
514 if (table68k[opcode].size == sz_long) {
515 strcpy (getcode, "get_long(srca)");
516 } else {
517 strcpy (getcode, "(uae_s32)(uae_s16)get_word(srca)");
518 }
519
520 printf ("\tuae_u16 mask = %s;\n", gen_nextiword ());
521 printf ("\tunsigned int dmask = mask & 0xff, amask = (mask >> 8) & 0xff;\n");
522 genamode (table68k[opcode].dmode, "dstreg", table68k[opcode].size, "src", 2, 1);
523 start_brace ();
524 printf ("\twhile (dmask) { m68k_dreg(regs, movem_index1[dmask]) = %s; srca += %d; dmask = movem_next[dmask]; }\n",
525 getcode, size);
526 printf ("\twhile (amask) { m68k_areg(regs, movem_index1[amask]) = %s; srca += %d; amask = movem_next[amask]; }\n",
527 getcode, size);
528
529 if (table68k[opcode].dmode == Aipi)
530 printf ("\tm68k_areg(regs, dstreg) = srca;\n");
531 }
532
533 static void genmovemle (uae_u16 opcode)
534 {
535 char putcode[100];
536 int size = table68k[opcode].size == sz_long ? 4 : 2;
537 if (table68k[opcode].size == sz_long) {
538 strcpy (putcode, "put_long(srca,");
539 } else {
540 strcpy (putcode, "put_word(srca,");
541 }
542
543 printf ("\tuae_u16 mask = %s;\n", gen_nextiword ());
544 genamode (table68k[opcode].dmode, "dstreg", table68k[opcode].size, "src", 2, 1);
545 if (using_prefetch)
546 sync_m68k_pc ();
547
548 start_brace ();
549 if (table68k[opcode].dmode == Apdi) {
550 printf ("\tuae_u16 amask = mask & 0xff, dmask = (mask >> 8) & 0xff;\n");
551 printf ("\twhile (amask) { srca -= %d; %s m68k_areg(regs, movem_index2[amask])); amask = movem_next[amask]; }\n",
552 size, putcode);
553 printf ("\twhile (dmask) { srca -= %d; %s m68k_dreg(regs, movem_index2[dmask])); dmask = movem_next[dmask]; }\n",
554 size, putcode);
555 printf ("\tm68k_areg(regs, dstreg) = srca;\n");
556 } else {
557 printf ("\tuae_u16 dmask = mask & 0xff, amask = (mask >> 8) & 0xff;\n");
558 printf ("\twhile (dmask) { %s m68k_dreg(regs, movem_index1[dmask])); srca += %d; dmask = movem_next[dmask]; }\n",
559 putcode, size);
560 printf ("\twhile (amask) { %s m68k_areg(regs, movem_index1[amask])); srca += %d; amask = movem_next[amask]; }\n",
561 putcode, size);
562 }
563 }
564
565 static void duplicate_carry (void)
566 {
567 printf ("\tCOPY_CARRY;\n");
568 }
569
570 typedef enum {
571 flag_logical_noclobber, flag_logical, flag_add, flag_sub, flag_cmp, flag_addx, flag_subx, flag_zn,
572 flag_av, flag_sv
573 } flagtypes;
574
575 static void genflags_normal (flagtypes type, wordsizes size, char *value, char *src, char *dst)
576 {
577 char vstr[100], sstr[100], dstr[100];
578 char usstr[100], udstr[100];
579 char unsstr[100], undstr[100];
580
581 switch (size) {
582 case sz_byte:
583 strcpy (vstr, "((uae_s8)(");
584 strcpy (usstr, "((uae_u8)(");
585 break;
586 case sz_word:
587 strcpy (vstr, "((uae_s16)(");
588 strcpy (usstr, "((uae_u16)(");
589 break;
590 case sz_long:
591 strcpy (vstr, "((uae_s32)(");
592 strcpy (usstr, "((uae_u32)(");
593 break;
594 default:
595 abort ();
596 }
597 strcpy (unsstr, usstr);
598
599 strcpy (sstr, vstr);
600 strcpy (dstr, vstr);
601 strcat (vstr, value);
602 strcat (vstr, "))");
603 strcat (dstr, dst);
604 strcat (dstr, "))");
605 strcat (sstr, src);
606 strcat (sstr, "))");
607
608 strcpy (udstr, usstr);
609 strcat (udstr, dst);
610 strcat (udstr, "))");
611 strcat (usstr, src);
612 strcat (usstr, "))");
613
614 strcpy (undstr, unsstr);
615 strcat (unsstr, "-");
616 strcat (undstr, "~");
617 strcat (undstr, dst);
618 strcat (undstr, "))");
619 strcat (unsstr, src);
620 strcat (unsstr, "))");
621
622 switch (type) {
623 case flag_logical_noclobber:
624 case flag_logical:
625 case flag_zn:
626 case flag_av:
627 case flag_sv:
628 case flag_addx:
629 case flag_subx:
630 break;
631
632 case flag_add:
633 start_brace ();
634 printf ("uae_u32 %s = %s + %s;\n", value, dstr, sstr);
635 break;
636 case flag_sub:
637 case flag_cmp:
638 start_brace ();
639 printf ("uae_u32 %s = %s - %s;\n", value, dstr, sstr);
640 break;
641 }
642
643 switch (type) {
644 case flag_logical_noclobber:
645 case flag_logical:
646 case flag_zn:
647 break;
648
649 case flag_add:
650 case flag_sub:
651 case flag_addx:
652 case flag_subx:
653 case flag_cmp:
654 case flag_av:
655 case flag_sv:
656 start_brace ();
657 printf ("\t" BOOL_TYPE " flgs = %s < 0;\n", sstr);
658 printf ("\t" BOOL_TYPE " flgo = %s < 0;\n", dstr);
659 printf ("\t" BOOL_TYPE " flgn = %s < 0;\n", vstr);
660 break;
661 }
662
663 switch (type) {
664 case flag_logical:
665 printf ("\tCLEAR_CZNV;\n");
666 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
667 printf ("\tSET_NFLG (%s < 0);\n", vstr);
668 break;
669 case flag_logical_noclobber:
670 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
671 printf ("\tSET_NFLG (%s < 0);\n", vstr);
672 break;
673 case flag_av:
674 printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n");
675 break;
676 case flag_sv:
677 printf ("\tSET_VFLG ((flgs ^ flgo) & (flgn ^ flgo));\n");
678 break;
679 case flag_zn:
680 printf ("\tSET_ZFLG (GET_ZFLG & (%s == 0));\n", vstr);
681 printf ("\tSET_NFLG (%s < 0);\n", vstr);
682 break;
683 case flag_add:
684 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
685 printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n");
686 printf ("\tSET_CFLG (%s < %s);\n", undstr, usstr);
687 duplicate_carry ();
688 printf ("\tSET_NFLG (flgn != 0);\n");
689 break;
690 case flag_sub:
691 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
692 printf ("\tSET_VFLG ((flgs ^ flgo) & (flgn ^ flgo));\n");
693 printf ("\tSET_CFLG (%s > %s);\n", usstr, udstr);
694 duplicate_carry ();
695 printf ("\tSET_NFLG (flgn != 0);\n");
696 break;
697 case flag_addx:
698 printf ("\tSET_VFLG ((flgs ^ flgn) & (flgo ^ flgn));\n"); /* minterm SON: 0x42 */
699 printf ("\tSET_CFLG (flgs ^ ((flgs ^ flgo) & (flgo ^ flgn)));\n"); /* minterm SON: 0xD4 */
700 duplicate_carry ();
701 break;
702 case flag_subx:
703 printf ("\tSET_VFLG ((flgs ^ flgo) & (flgo ^ flgn));\n"); /* minterm SON: 0x24 */
704 printf ("\tSET_CFLG (flgs ^ ((flgs ^ flgn) & (flgo ^ flgn)));\n"); /* minterm SON: 0xB2 */
705 duplicate_carry ();
706 break;
707 case flag_cmp:
708 printf ("\tSET_ZFLG (%s == 0);\n", vstr);
709 printf ("\tSET_VFLG ((flgs != flgo) && (flgn != flgo));\n");
710 printf ("\tSET_CFLG (%s > %s);\n", usstr, udstr);
711 printf ("\tSET_NFLG (flgn != 0);\n");
712 break;
713 }
714 }
715
716 static void genflags (flagtypes type, wordsizes size, char *value, char *src, char *dst)
717 {
718 /* Temporarily deleted 68k/ARM flag optimizations. I'd prefer to have
719 them in the appropriate m68k.h files and use just one copy of this
720 code here. The API can be changed if necessary. */
721 #ifdef OPTIMIZED_FLAGS
722 switch (type) {
723 case flag_add:
724 case flag_sub:
725 start_brace ();
726 printf ("\tuae_u32 %s;\n", value);
727 break;
728
729 default:
730 break;
731 }
732
733 /* At least some of those casts are fairly important! */
734 switch (type) {
735 case flag_logical_noclobber:
736 printf ("\t{uae_u32 oldcznv = GET_CZNV & ~(FLAGVAL_Z | FLAGVAL_N);\n");
737 if (strcmp (value, "0") == 0) {
738 printf ("\tSET_CZNV (olcznv | FLAGVAL_Z);\n");
739 } else {
740 switch (size) {
741 case sz_byte: printf ("\toptflag_testb ((uae_s8)(%s));\n", value); break;
742 case sz_word: printf ("\toptflag_testw ((uae_s16)(%s));\n", value); break;
743 case sz_long: printf ("\toptflag_testl ((uae_s32)(%s));\n", value); break;
744 }
745 printf ("\tIOR_CZNV (oldcznv);\n");
746 }
747 printf ("\t}\n");
748 return;
749 case flag_logical:
750 if (strcmp (value, "0") == 0) {
751 printf ("\tSET_CZNV (FLAGVAL_Z);\n");
752 } else {
753 switch (size) {
754 case sz_byte: printf ("\toptflag_testb ((uae_s8)(%s));\n", value); break;
755 case sz_word: printf ("\toptflag_testw ((uae_s16)(%s));\n", value); break;
756 case sz_long: printf ("\toptflag_testl ((uae_s32)(%s));\n", value); break;
757 }
758 }
759 return;
760
761 case flag_add:
762 switch (size) {
763 case sz_byte: printf ("\toptflag_addb (%s, (uae_s8)(%s), (uae_s8)(%s));\n", value, src, dst); break;
764 case sz_word: printf ("\toptflag_addw (%s, (uae_s16)(%s), (uae_s16)(%s));\n", value, src, dst); break;
765 case sz_long: printf ("\toptflag_addl (%s, (uae_s32)(%s), (uae_s32)(%s));\n", value, src, dst); break;
766 }
767 return;
768
769 case flag_sub:
770 switch (size) {
771 case sz_byte: printf ("\toptflag_subb (%s, (uae_s8)(%s), (uae_s8)(%s));\n", value, src, dst); break;
772 case sz_word: printf ("\toptflag_subw (%s, (uae_s16)(%s), (uae_s16)(%s));\n", value, src, dst); break;
773 case sz_long: printf ("\toptflag_subl (%s, (uae_s32)(%s), (uae_s32)(%s));\n", value, src, dst); break;
774 }
775 return;
776
777 case flag_cmp:
778 switch (size) {
779 case sz_byte: printf ("\toptflag_cmpb ((uae_s8)(%s), (uae_s8)(%s));\n", src, dst); break;
780 case sz_word: printf ("\toptflag_cmpw ((uae_s16)(%s), (uae_s16)(%s));\n", src, dst); break;
781 case sz_long: printf ("\toptflag_cmpl ((uae_s32)(%s), (uae_s32)(%s));\n", src, dst); break;
782 }
783 return;
784
785 default:
786 break;
787 }
788 #endif
789
790 genflags_normal (type, size, value, src, dst);
791 }
792
793 static void force_range_for_rox (const char *var, wordsizes size)
794 {
795 /* Could do a modulo operation here... which one is faster? */
796 switch (size) {
797 case sz_long:
798 printf ("\tif (%s >= 33) %s -= 33;\n", var, var);
799 break;
800 case sz_word:
801 printf ("\tif (%s >= 34) %s -= 34;\n", var, var);
802 printf ("\tif (%s >= 17) %s -= 17;\n", var, var);
803 break;
804 case sz_byte:
805 printf ("\tif (%s >= 36) %s -= 36;\n", var, var);
806 printf ("\tif (%s >= 18) %s -= 18;\n", var, var);
807 printf ("\tif (%s >= 9) %s -= 9;\n", var, var);
808 break;
809 }
810 }
811
812 static const char *cmask (wordsizes size)
813 {
814 switch (size) {
815 case sz_byte: return "0x80";
816 case sz_word: return "0x8000";
817 case sz_long: return "0x80000000";
818 default: abort ();
819 }
820 }
821
822 static int source_is_imm1_8 (struct instr *i)
823 {
824 return i->stype == 3;
825 }
826
827 static void gen_opcode (unsigned long int opcode)
828 {
829 struct instr *curi = table68k + opcode;
830 insn_n_cycles = 2;
831
832 start_brace ();
833 #if 0
834 printf ("uae_u8 *m68k_pc = regs.pc_p;\n");
835 #endif
836 m68k_pc_offset = 2;
837 switch (curi->plev) {
838 case 0: /* not privileged */
839 break;
840 case 1: /* unprivileged only on 68000 */
841 if (cpu_level == 0)
842 break;
843 if (next_cpu_level < 0)
844 next_cpu_level = 0;
845
846 /* fall through */
847 case 2: /* priviledged */
848 printf ("if (!regs.s) { Exception(8,0); goto %s; }\n", endlabelstr);
849 need_endlabel = 1;
850 start_brace ();
851 break;
852 case 3: /* privileged if size == word */
853 if (curi->size == sz_byte)
854 break;
855 printf ("if (!regs.s) { Exception(8,0); goto %s; }\n", endlabelstr);
856 need_endlabel = 1;
857 start_brace ();
858 break;
859 }
860 switch (curi->mnemo) {
861 case i_OR:
862 case i_AND:
863 case i_EOR:
864 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
865 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
866 printf ("\tsrc %c= dst;\n", curi->mnemo == i_OR ? '|' : curi->mnemo == i_AND ? '&' : '^');
867 genflags (flag_logical, curi->size, "src", "", "");
868 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
869 break;
870 case i_ORSR:
871 case i_EORSR:
872 printf ("\tMakeSR();\n");
873 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
874 if (curi->size == sz_byte) {
875 printf ("\tsrc &= 0xFF;\n");
876 }
877 printf ("\tregs.sr %c= src;\n", curi->mnemo == i_EORSR ? '^' : '|');
878 printf ("\tMakeFromSR();\n");
879 break;
880 case i_ANDSR:
881 printf ("\tMakeSR();\n");
882 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
883 if (curi->size == sz_byte) {
884 printf ("\tsrc |= 0xFF00;\n");
885 }
886 printf ("\tregs.sr &= src;\n");
887 printf ("\tMakeFromSR();\n");
888 break;
889 case i_SUB:
890 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
891 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
892 start_brace ();
893 genflags (flag_sub, curi->size, "newv", "src", "dst");
894 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
895 break;
896 case i_SUBA:
897 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
898 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
899 start_brace ();
900 printf ("\tuae_u32 newv = dst - src;\n");
901 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
902 break;
903 case i_SUBX:
904 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
905 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
906 start_brace ();
907 printf ("\tuae_u32 newv = dst - src - (GET_XFLG ? 1 : 0);\n");
908 genflags (flag_subx, curi->size, "newv", "src", "dst");
909 genflags (flag_zn, curi->size, "newv", "", "");
910 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
911 break;
912 case i_SBCD:
913 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
914 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
915 start_brace ();
916 printf ("\tuae_u16 newv_lo = (dst & 0xF) - (src & 0xF) - (GET_XFLG ? 1 : 0);\n");
917 printf ("\tuae_u16 newv_hi = (dst & 0xF0) - (src & 0xF0);\n");
918 printf ("\tuae_u16 newv, tmp_newv;\n");
919 printf ("\tint bcd = 0;\n");
920 printf ("\tnewv = tmp_newv = newv_hi + newv_lo;\n");
921 printf ("\tif (newv_lo & 0xF0) { newv -= 6; bcd = 6; };\n");
922 printf ("\tif ((((dst & 0xFF) - (src & 0xFF) - (GET_XFLG ? 1 : 0)) & 0x100) > 0xFF) { newv -= 0x60; }\n");
923 printf ("\tSET_CFLG ((((dst & 0xFF) - (src & 0xFF) - bcd - (GET_XFLG ? 1 : 0)) & 0x300) > 0xFF);\n");
924 duplicate_carry ();
925 genflags (flag_zn, curi->size, "newv", "", "");
926 printf ("\tSET_VFLG ((tmp_newv & 0x80) != 0 && (newv & 0x80) == 0);\n");
927 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
928 break;
929 case i_ADD:
930 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
931 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
932 start_brace ();
933 genflags (flag_add, curi->size, "newv", "src", "dst");
934 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
935 break;
936 case i_ADDA:
937 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
938 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
939 start_brace ();
940 printf ("\tuae_u32 newv = dst + src;\n");
941 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
942 break;
943 case i_ADDX:
944 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
945 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
946 start_brace ();
947 printf ("\tuae_u32 newv = dst + src + (GET_XFLG ? 1 : 0);\n");
948 genflags (flag_addx, curi->size, "newv", "src", "dst");
949 genflags (flag_zn, curi->size, "newv", "", "");
950 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
951 break;
952 case i_ABCD:
953 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
954 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
955 start_brace ();
956 printf ("\tuae_u16 newv_lo = (src & 0xF) + (dst & 0xF) + (GET_XFLG ? 1 : 0);\n");
957 printf ("\tuae_u16 newv_hi = (src & 0xF0) + (dst & 0xF0);\n");
958 printf ("\tuae_u16 newv, tmp_newv;\n");
959 printf ("\tint cflg;\n");
960 printf ("\tnewv = tmp_newv = newv_hi + newv_lo;");
961 printf ("\tif (newv_lo > 9) { newv += 6; }\n");
962 printf ("\tcflg = (newv & 0x3F0) > 0x90;\n");
963 printf ("\tif (cflg) newv += 0x60;\n");
964 printf ("\tSET_CFLG (cflg);\n");
965 duplicate_carry ();
966 genflags (flag_zn, curi->size, "newv", "", "");
967 printf ("\tSET_VFLG ((tmp_newv & 0x80) == 0 && (newv & 0x80) != 0);\n");
968 genastore ("newv", curi->dmode, "dstreg", curi->size, "dst");
969 break;
970 case i_NEG:
971 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
972 start_brace ();
973 genflags (flag_sub, curi->size, "dst", "src", "0");
974 genastore ("dst", curi->smode, "srcreg", curi->size, "src");
975 break;
976 case i_NEGX:
977 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
978 start_brace ();
979 printf ("\tuae_u32 newv = 0 - src - (GET_XFLG ? 1 : 0);\n");
980 genflags (flag_subx, curi->size, "newv", "src", "0");
981 genflags (flag_zn, curi->size, "newv", "", "");
982 genastore ("newv", curi->smode, "srcreg", curi->size, "src");
983 break;
984 case i_NBCD:
985 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
986 start_brace ();
987 printf ("\tuae_u16 newv_lo = - (src & 0xF) - (GET_XFLG ? 1 : 0);\n");
988 printf ("\tuae_u16 newv_hi = - (src & 0xF0);\n");
989 printf ("\tuae_u16 newv;\n");
990 printf ("\tint cflg;\n");
991 printf ("\tif (newv_lo > 9) { newv_lo -= 6; }\n");
992 printf ("\tnewv = newv_hi + newv_lo;");
993 printf ("\tcflg = (newv & 0x1F0) > 0x90;\n");
994 printf ("\tif (cflg) newv -= 0x60;\n");
995 printf ("\tSET_CFLG (cflg);\n");
996 duplicate_carry();
997 genflags (flag_zn, curi->size, "newv", "", "");
998 genastore ("newv", curi->smode, "srcreg", curi->size, "src");
999 break;
1000 case i_CLR:
1001 genamode (curi->smode, "srcreg", curi->size, "src", 2, 0);
1002 genflags (flag_logical, curi->size, "0", "", "");
1003 genastore ("0", curi->smode, "srcreg", curi->size, "src");
1004 break;
1005 case i_NOT:
1006 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1007 start_brace ();
1008 printf ("\tuae_u32 dst = ~src;\n");
1009 genflags (flag_logical, curi->size, "dst", "", "");
1010 genastore ("dst", curi->smode, "srcreg", curi->size, "src");
1011 break;
1012 case i_TST:
1013 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1014 genflags (flag_logical, curi->size, "src", "", "");
1015 break;
1016 case i_BTST:
1017 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1018 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1019 if (curi->size == sz_byte)
1020 printf ("\tsrc &= 7;\n");
1021 else
1022 printf ("\tsrc &= 31;\n");
1023 printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1024 break;
1025 case i_BCHG:
1026 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1027 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1028 if (curi->size == sz_byte)
1029 printf ("\tsrc &= 7;\n");
1030 else
1031 printf ("\tsrc &= 31;\n");
1032 printf ("\tdst ^= (1 << src);\n");
1033 printf ("\tSET_ZFLG (((uae_u32)dst & (1 << src)) >> src);\n");
1034 genastore ("dst", curi->dmode, "dstreg", curi->size, "dst");
1035 break;
1036 case i_BCLR:
1037 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1038 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1039 if (curi->size == sz_byte)
1040 printf ("\tsrc &= 7;\n");
1041 else
1042 printf ("\tsrc &= 31;\n");
1043 printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1044 printf ("\tdst &= ~(1 << src);\n");
1045 genastore ("dst", curi->dmode, "dstreg", curi->size, "dst");
1046 break;
1047 case i_BSET:
1048 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1049 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1050 if (curi->size == sz_byte)
1051 printf ("\tsrc &= 7;\n");
1052 else
1053 printf ("\tsrc &= 31;\n");
1054 printf ("\tSET_ZFLG (1 ^ ((dst >> src) & 1));\n");
1055 printf ("\tdst |= (1 << src);\n");
1056 genastore ("dst", curi->dmode, "dstreg", curi->size, "dst");
1057 break;
1058 case i_CMPM:
1059 case i_CMP:
1060 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1061 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1062 start_brace ();
1063 genflags (flag_cmp, curi->size, "newv", "src", "dst");
1064 break;
1065 case i_CMPA:
1066 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1067 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1068 start_brace ();
1069 genflags (flag_cmp, sz_long, "newv", "src", "dst");
1070 break;
1071 /* The next two are coded a little unconventional, but they are doing
1072 * weird things... */
1073 case i_MVPRM:
1074 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1075
1076 printf ("\tuaecptr memp = m68k_areg(regs, dstreg) + (uae_s32)(uae_s16)%s;\n", gen_nextiword ());
1077 if (curi->size == sz_word) {
1078 printf ("\tput_byte(memp, src >> 8); put_byte(memp + 2, src);\n");
1079 } else {
1080 printf ("\tput_byte(memp, src >> 24); put_byte(memp + 2, src >> 16);\n");
1081 printf ("\tput_byte(memp + 4, src >> 8); put_byte(memp + 6, src);\n");
1082 }
1083 break;
1084 case i_MVPMR:
1085 printf ("\tuaecptr memp = m68k_areg(regs, srcreg) + (uae_s32)(uae_s16)%s;\n", gen_nextiword ());
1086 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1087 if (curi->size == sz_word) {
1088 printf ("\tuae_u16 val = (get_byte(memp) << 8) + get_byte(memp + 2);\n");
1089 } else {
1090 printf ("\tuae_u32 val = (get_byte(memp) << 24) + (get_byte(memp + 2) << 16)\n");
1091 printf (" + (get_byte(memp + 4) << 8) + get_byte(memp + 6);\n");
1092 }
1093 genastore ("val", curi->dmode, "dstreg", curi->size, "dst");
1094 break;
1095 case i_MOVE:
1096 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1097 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1098 genflags (flag_logical, curi->size, "src", "", "");
1099 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
1100 break;
1101 case i_MOVEA:
1102 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1103 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1104 if (curi->size == sz_word) {
1105 printf ("\tuae_u32 val = (uae_s32)(uae_s16)src;\n");
1106 } else {
1107 printf ("\tuae_u32 val = src;\n");
1108 }
1109 genastore ("val", curi->dmode, "dstreg", sz_long, "dst");
1110 break;
1111 case i_MVSR2:
1112 genamode (curi->smode, "srcreg", sz_word, "src", 2, 0);
1113 printf ("\tMakeSR();\n");
1114 if (curi->size == sz_byte)
1115 genastore ("regs.sr & 0xff", curi->smode, "srcreg", sz_word, "src");
1116 else
1117 genastore ("regs.sr", curi->smode, "srcreg", sz_word, "src");
1118 break;
1119 case i_MV2SR:
1120 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1121 if (curi->size == sz_byte)
1122 printf ("\tMakeSR();\n\tregs.sr &= 0xFF00;\n\tregs.sr |= src & 0xFF;\n");
1123 else {
1124 printf ("\tregs.sr = src;\n");
1125 }
1126 printf ("\tMakeFromSR();\n");
1127 break;
1128 case i_SWAP:
1129 genamode (curi->smode, "srcreg", sz_long, "src", 1, 0);
1130 start_brace ();
1131 printf ("\tuae_u32 dst = ((src >> 16)&0xFFFF) | ((src&0xFFFF)<<16);\n");
1132 genflags (flag_logical, sz_long, "dst", "", "");
1133 genastore ("dst", curi->smode, "srcreg", sz_long, "src");
1134 break;
1135 case i_EXG:
1136 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1137 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1138 genastore ("dst", curi->smode, "srcreg", curi->size, "src");
1139 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
1140 break;
1141 case i_EXT:
1142 genamode (curi->smode, "srcreg", sz_long, "src", 1, 0);
1143 start_brace ();
1144 switch (curi->size) {
1145 case sz_byte: printf ("\tuae_u32 dst = (uae_s32)(uae_s8)src;\n"); break;
1146 case sz_word: printf ("\tuae_u16 dst = (uae_s16)(uae_s8)src;\n"); break;
1147 case sz_long: printf ("\tuae_u32 dst = (uae_s32)(uae_s16)src;\n"); break;
1148 default: abort ();
1149 }
1150 genflags (flag_logical,
1151 curi->size == sz_word ? sz_word : sz_long, "dst", "", "");
1152 genastore ("dst", curi->smode, "srcreg",
1153 curi->size == sz_word ? sz_word : sz_long, "src");
1154 break;
1155 case i_MVMEL:
1156 genmovemel (opcode);
1157 break;
1158 case i_MVMLE:
1159 genmovemle (opcode);
1160 break;
1161 case i_TRAP:
1162 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1163 sync_m68k_pc ();
1164 printf ("\tException(src+32,0);\n");
1165 m68k_pc_offset = 0;
1166 break;
1167 case i_MVR2USP:
1168 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1169 printf ("\tregs.usp = src;\n");
1170 break;
1171 case i_MVUSP2R:
1172 genamode (curi->smode, "srcreg", curi->size, "src", 2, 0);
1173 genastore ("regs.usp", curi->smode, "srcreg", curi->size, "src");
1174 break;
1175 case i_RESET:
1176 break;
1177 case i_NOP:
1178 break;
1179 case i_STOP:
1180 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1181 printf ("\tregs.sr = src;\n");
1182 printf ("\tMakeFromSR();\n");
1183 printf ("\tm68k_setstopped(1);\n");
1184 break;
1185 case i_RTE:
1186 if (cpu_level == 0) {
1187 genamode (Aipi, "7", sz_word, "sr", 1, 0);
1188 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1189 printf ("\tregs.sr = sr; m68k_setpc_rte(pc);\n");
1190 fill_prefetch_0 ();
1191 printf ("\tMakeFromSR();\n");
1192 } else {
1193 int old_brace_level = n_braces;
1194 if (next_cpu_level < 0)
1195 next_cpu_level = 0;
1196 printf ("\tuae_u16 newsr; uae_u32 newpc; for (;;) {\n");
1197 genamode (Aipi, "7", sz_word, "sr", 1, 0);
1198 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1199 genamode (Aipi, "7", sz_word, "format", 1, 0);
1200 printf ("\tnewsr = sr; newpc = pc;\n");
1201 printf ("\tif ((format & 0xF000) == 0x0000) { break; }\n");
1202 printf ("\telse if ((format & 0xF000) == 0x1000) { ; }\n");
1203 printf ("\telse if ((format & 0xF000) == 0x2000) { m68k_areg(regs, 7) += 4; break; }\n");
1204 printf ("\telse if ((format & 0xF000) == 0x3000) { m68k_areg(regs, 7) += 4; break; }\n");
1205 printf ("\telse if ((format & 0xF000) == 0x7000) { m68k_areg(regs, 7) += 52; break; }\n");
1206 printf ("\telse if ((format & 0xF000) == 0x8000) { m68k_areg(regs, 7) += 50; break; }\n");
1207 printf ("\telse if ((format & 0xF000) == 0x9000) { m68k_areg(regs, 7) += 12; break; }\n");
1208 printf ("\telse if ((format & 0xF000) == 0xa000) { m68k_areg(regs, 7) += 24; break; }\n");
1209 printf ("\telse if ((format & 0xF000) == 0xb000) { m68k_areg(regs, 7) += 84; break; }\n");
1210 printf ("\telse { Exception(14,0); goto %s; }\n", endlabelstr);
1211 printf ("\tregs.sr = newsr; MakeFromSR();\n}\n");
1212 pop_braces (old_brace_level);
1213 printf ("\tregs.sr = newsr; MakeFromSR();\n");
1214 printf ("\tm68k_setpc_rte(newpc);\n");
1215 fill_prefetch_0 ();
1216 need_endlabel = 1;
1217 }
1218 /* PC is set and prefetch filled. */
1219 m68k_pc_offset = 0;
1220 break;
1221 case i_RTD:
1222 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1223 genamode (curi->smode, "srcreg", curi->size, "offs", 1, 0);
1224 printf ("\tm68k_areg(regs, 7) += offs;\n");
1225 printf ("\tm68k_setpc_rte(pc);\n");
1226 fill_prefetch_0 ();
1227 /* PC is set and prefetch filled. */
1228 m68k_pc_offset = 0;
1229 break;
1230 case i_LINK:
1231 genamode (Apdi, "7", sz_long, "old", 2, 0);
1232 genamode (curi->smode, "srcreg", sz_long, "src", 1, 0);
1233 genastore ("src", Apdi, "7", sz_long, "old");
1234 genastore ("m68k_areg(regs, 7)", curi->smode, "srcreg", sz_long, "src");
1235 genamode (curi->dmode, "dstreg", curi->size, "offs", 1, 0);
1236 printf ("\tm68k_areg(regs, 7) += offs;\n");
1237 break;
1238 case i_UNLK:
1239 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1240 printf ("\tm68k_areg(regs, 7) = src;\n");
1241 genamode (Aipi, "7", sz_long, "old", 1, 0);
1242 genastore ("old", curi->smode, "srcreg", curi->size, "src");
1243 break;
1244 case i_RTS:
1245 printf ("\tm68k_do_rts();\n");
1246 fill_prefetch_0 ();
1247 m68k_pc_offset = 0;
1248 break;
1249 case i_TRAPV:
1250 sync_m68k_pc ();
1251 printf ("\tif (GET_VFLG) { Exception(7,m68k_getpc()); goto %s; }\n", endlabelstr);
1252 need_endlabel = 1;
1253 break;
1254 case i_RTR:
1255 printf ("\tMakeSR();\n");
1256 genamode (Aipi, "7", sz_word, "sr", 1, 0);
1257 genamode (Aipi, "7", sz_long, "pc", 1, 0);
1258 printf ("\tregs.sr &= 0xFF00; sr &= 0xFF;\n");
1259 printf ("\tregs.sr |= sr; m68k_setpc(pc);\n");
1260 fill_prefetch_0 ();
1261 printf ("\tMakeFromSR();\n");
1262 m68k_pc_offset = 0;
1263 break;
1264 case i_JSR:
1265 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1266 printf ("\tm68k_do_jsr(m68k_getpc() + %d, srca);\n", m68k_pc_offset);
1267 fill_prefetch_0 ();
1268 m68k_pc_offset = 0;
1269 break;
1270 case i_JMP:
1271 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1272 printf ("\tm68k_setpc(srca);\n");
1273 fill_prefetch_0 ();
1274 m68k_pc_offset = 0;
1275 break;
1276 case i_BSR:
1277 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1278 printf ("\tuae_s32 s = (uae_s32)src + 2;\n");
1279 if (using_exception_3) {
1280 printf ("\tif (src & 1) {\n");
1281 printf ("\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1282 printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + s;\n");
1283 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1284 printf ("\t}\n");
1285 need_endlabel = 1;
1286 }
1287 printf ("\tm68k_do_bsr(m68k_getpc() + %d, s);\n", m68k_pc_offset);
1288 fill_prefetch_0 ();
1289 m68k_pc_offset = 0;
1290 break;
1291 case i_Bcc:
1292 if (curi->size == sz_long) {
1293 if (cpu_level < 2) {
1294 printf ("\tm68k_incpc(2);\n");
1295 printf ("\tif (!cctrue(%d)) goto %s;\n", curi->cc, endlabelstr);
1296 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1297 printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + 1;\n");
1298 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1299 need_endlabel = 1;
1300 } else {
1301 if (next_cpu_level < 1)
1302 next_cpu_level = 1;
1303 }
1304 }
1305 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1306 printf ("\tif (!cctrue(%d)) goto didnt_jump;\n", curi->cc);
1307 if (using_exception_3) {
1308 printf ("\tif (src & 1) {\n");
1309 printf ("\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1310 printf ("\t\tlast_fault_for_exception_3 = m68k_getpc() + 2 + (uae_s32)src;\n");
1311 printf ("\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1312 printf ("\t}\n");
1313 need_endlabel = 1;
1314 }
1315 #ifdef USE_COMPILER
1316 printf ("\tm68k_setpc_bcc(m68k_getpc() + 2 + (uae_s32)src);\n");
1317 #else
1318 printf ("\tm68k_incpc ((uae_s32)src + 2);\n");
1319 #endif
1320 fill_prefetch_0 ();
1321 printf ("\tgoto %s;\n", endlabelstr);
1322 printf ("didnt_jump:;\n");
1323 need_endlabel = 1;
1324 break;
1325 case i_LEA:
1326 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1327 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1328 genastore ("srca", curi->dmode, "dstreg", curi->size, "dst");
1329 break;
1330 case i_PEA:
1331 genamode (curi->smode, "srcreg", curi->size, "src", 0, 0);
1332 genamode (Apdi, "7", sz_long, "dst", 2, 0);
1333 genastore ("srca", Apdi, "7", sz_long, "dst");
1334 break;
1335 case i_DBcc:
1336 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1337 genamode (curi->dmode, "dstreg", curi->size, "offs", 1, 0);
1338
1339 printf ("\tif (!cctrue(%d)) {\n", curi->cc);
1340 genastore ("(src-1)", curi->smode, "srcreg", curi->size, "src");
1341
1342 printf ("\t\tif (src) {\n");
1343 if (using_exception_3) {
1344 printf ("\t\t\tif (offs & 1) {\n");
1345 printf ("\t\t\tlast_addr_for_exception_3 = m68k_getpc() + 2;\n");
1346 printf ("\t\t\tlast_fault_for_exception_3 = m68k_getpc() + 2 + (uae_s32)offs + 2;\n");
1347 printf ("\t\t\tlast_op_for_exception_3 = opcode; Exception(3,0); goto %s;\n", endlabelstr);
1348 printf ("\t\t}\n");
1349 need_endlabel = 1;
1350 }
1351 #ifdef USE_COMPILER
1352 printf ("\t\t\tm68k_setpc_bcc(m68k_getpc() + (uae_s32)offs + 2);\n");
1353 #else
1354 printf ("\t\t\tm68k_incpc((uae_s32)offs + 2);\n");
1355 #endif
1356 fill_prefetch_0 ();
1357 printf ("\t\tgoto %s;\n", endlabelstr);
1358 printf ("\t\t}\n");
1359 printf ("\t}\n");
1360 need_endlabel = 1;
1361 break;
1362 case i_Scc:
1363 genamode (curi->smode, "srcreg", curi->size, "src", 2, 0);
1364 start_brace ();
1365 printf ("\tint val = cctrue(%d) ? 0xff : 0;\n", curi->cc);
1366 genastore ("val", curi->smode, "srcreg", curi->size, "src");
1367 break;
1368 case i_DIVU:
1369 printf ("\tuaecptr oldpc = m68k_getpc();\n");
1370 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1371 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1372 sync_m68k_pc ();
1373 /* Clear V flag when dividing by zero - Alcatraz Odyssey demo depends
1374 * on this (actually, it's doing a DIVS). */
1375 printf ("\tif (src == 0) { SET_VFLG (0); Exception (5, oldpc); goto %s; } else {\n", endlabelstr);
1376 printf ("\tuae_u32 newv = (uae_u32)dst / (uae_u32)(uae_u16)src;\n");
1377 printf ("\tuae_u32 rem = (uae_u32)dst %% (uae_u32)(uae_u16)src;\n");
1378 /* The N flag appears to be set each time there is an overflow.
1379 * Weird. */
1380 printf ("\tif (newv > 0xffff) { SET_VFLG (1); SET_NFLG (1); SET_CFLG (0); } else\n\t{\n");
1381 genflags (flag_logical, sz_word, "newv", "", "");
1382 printf ("\tnewv = (newv & 0xffff) | ((uae_u32)rem << 16);\n");
1383 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1384 printf ("\t}\n");
1385 printf ("\t}\n");
1386 insn_n_cycles += 68;
1387 need_endlabel = 1;
1388 break;
1389 case i_DIVS:
1390 printf ("\tuaecptr oldpc = m68k_getpc();\n");
1391 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1392 genamode (curi->dmode, "dstreg", sz_long, "dst", 1, 0);
1393 sync_m68k_pc ();
1394 printf ("\tif (src == 0) { SET_VFLG (0); Exception(5,oldpc); goto %s; } else {\n", endlabelstr);
1395 printf ("\tuae_s32 newv = (uae_s32)dst / (uae_s32)(uae_s16)src;\n");
1396 printf ("\tuae_u16 rem = (uae_s32)dst %% (uae_s32)(uae_s16)src;\n");
1397 printf ("\tif ((newv & 0xffff8000) != 0 && (newv & 0xffff8000) != 0xffff8000) { SET_VFLG (1); SET_NFLG (1); SET_CFLG (0); } else\n\t{\n");
1398 printf ("\tif (((uae_s16)rem < 0) != ((uae_s32)dst < 0)) rem = -rem;\n");
1399 genflags (flag_logical, sz_word, "newv", "", "");
1400 printf ("\tnewv = (newv & 0xffff) | ((uae_u32)rem << 16);\n");
1401 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1402 printf ("\t}\n");
1403 printf ("\t}\n");
1404 insn_n_cycles += 72;
1405 need_endlabel = 1;
1406 break;
1407 case i_MULU:
1408 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1409 genamode (curi->dmode, "dstreg", sz_word, "dst", 1, 0);
1410 start_brace ();
1411 printf ("\tuae_u32 newv = (uae_u32)(uae_u16)dst * (uae_u32)(uae_u16)src;\n");
1412 genflags (flag_logical, sz_long, "newv", "", "");
1413 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1414 insn_n_cycles += 32;
1415 break;
1416 case i_MULS:
1417 genamode (curi->smode, "srcreg", sz_word, "src", 1, 0);
1418 genamode (curi->dmode, "dstreg", sz_word, "dst", 1, 0);
1419 start_brace ();
1420 printf ("\tuae_u32 newv = (uae_s32)(uae_s16)dst * (uae_s32)(uae_s16)src;\n");
1421 genflags (flag_logical, sz_long, "newv", "", "");
1422 genastore ("newv", curi->dmode, "dstreg", sz_long, "dst");
1423 insn_n_cycles += 32;
1424 break;
1425 case i_CHK:
1426 printf ("\tuaecptr oldpc = m68k_getpc();\n");
1427 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1428 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1429 printf ("\tif ((uae_s32)dst < 0) { SET_NFLG (1); Exception(6,oldpc); goto %s; }\n", endlabelstr);
1430 printf ("\telse if (dst > src) { SET_NFLG (0); Exception(6,oldpc); goto %s; }\n", endlabelstr);
1431 need_endlabel = 1;
1432 break;
1433
1434 case i_CHK2:
1435 printf ("\tuaecptr oldpc = m68k_getpc();\n");
1436 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1437 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1438 printf ("\t{uae_s32 upper,lower,reg = regs.regs[(extra >> 12) & 15];\n");
1439 switch (curi->size) {
1440 case sz_byte:
1441 printf ("\tlower=(uae_s32)(uae_s8)get_byte(dsta); upper = (uae_s32)(uae_s8)get_byte(dsta+1);\n");
1442 printf ("\tif ((extra & 0x8000) == 0) reg = (uae_s32)(uae_s8)reg;\n");
1443 break;
1444 case sz_word:
1445 printf ("\tlower=(uae_s32)(uae_s16)get_word(dsta); upper = (uae_s32)(uae_s16)get_word(dsta+2);\n");
1446 printf ("\tif ((extra & 0x8000) == 0) reg = (uae_s32)(uae_s16)reg;\n");
1447 break;
1448 case sz_long:
1449 printf ("\tlower=get_long(dsta); upper = get_long(dsta+4);\n");
1450 break;
1451 default:
1452 abort ();
1453 }
1454 printf ("\tSET_ZFLG (upper == reg || lower == reg);\n");
1455 printf ("\tSET_CFLG (lower <= upper ? reg < lower || reg > upper : reg > upper || reg < lower);\n");
1456 printf ("\tif ((extra & 0x800) && GET_CFLG) { Exception(6,oldpc); goto %s; }\n}\n", endlabelstr);
1457 need_endlabel = 1;
1458 break;
1459
1460 case i_ASR:
1461 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1462 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1463 start_brace ();
1464 switch (curi->size) {
1465 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1466 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1467 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1468 default: abort ();
1469 }
1470 printf ("\tuae_u32 sign = (%s & val) >> %d;\n", cmask (curi->size), bit_size (curi->size) - 1);
1471 printf ("\tcnt &= 63;\n");
1472 printf ("\tCLEAR_CZNV;\n");
1473 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1474 printf ("\t\tval = %s & (uae_u32)-sign;\n", bit_mask (curi->size));
1475 printf ("\t\tSET_CFLG (sign);\n");
1476 duplicate_carry ();
1477 if (source_is_imm1_8 (curi))
1478 printf ("\t} else {\n");
1479 else
1480 printf ("\t} else if (cnt > 0) {\n");
1481 printf ("\t\tval >>= cnt - 1;\n");
1482 printf ("\t\tSET_CFLG (val & 1);\n");
1483 duplicate_carry ();
1484 printf ("\t\tval >>= 1;\n");
1485 printf ("\t\tval |= (%s << (%d - cnt)) & (uae_u32)-sign;\n",
1486 bit_mask (curi->size),
1487 bit_size (curi->size));
1488 printf ("\t\tval &= %s;\n", bit_mask (curi->size));
1489 printf ("\t}\n");
1490 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1491 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1492 break;
1493 case i_ASL:
1494 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1495 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1496 start_brace ();
1497 switch (curi->size) {
1498 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1499 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1500 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1501 default: abort ();
1502 }
1503 printf ("\tcnt &= 63;\n");
1504 printf ("\tCLEAR_CZNV;\n");
1505 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1506 printf ("\t\tSET_VFLG (val != 0);\n");
1507 printf ("\t\tSET_CFLG (cnt == %d ? val & 1 : 0);\n",
1508 bit_size (curi->size));
1509 duplicate_carry ();
1510 printf ("\t\tval = 0;\n");
1511 if (source_is_imm1_8 (curi))
1512 printf ("\t} else {\n");
1513 else
1514 printf ("\t} else if (cnt > 0) {\n");
1515 printf ("\t\tuae_u32 mask = (%s << (%d - cnt)) & %s;\n",
1516 bit_mask (curi->size),
1517 bit_size (curi->size) - 1,
1518 bit_mask (curi->size));
1519 printf ("\t\tSET_VFLG ((val & mask) != mask && (val & mask) != 0);\n");
1520 printf ("\t\tval <<= cnt - 1;\n");
1521 printf ("\t\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1522 duplicate_carry ();
1523 printf ("\t\tval <<= 1;\n");
1524 printf ("\t\tval &= %s;\n", bit_mask (curi->size));
1525 printf ("\t}\n");
1526 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1527 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1528 break;
1529 case i_LSR:
1530 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1531 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1532 start_brace ();
1533 switch (curi->size) {
1534 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1535 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1536 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1537 default: abort ();
1538 }
1539 printf ("\tcnt &= 63;\n");
1540 printf ("\tCLEAR_CZNV;\n");
1541 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1542 printf ("\t\tSET_CFLG ((cnt == %d) & (val >> %d));\n",
1543 bit_size (curi->size), bit_size (curi->size) - 1);
1544 duplicate_carry ();
1545 printf ("\t\tval = 0;\n");
1546 if (source_is_imm1_8 (curi))
1547 printf ("\t} else {\n");
1548 else
1549 printf ("\t} else if (cnt > 0) {\n");
1550 printf ("\t\tval >>= cnt - 1;\n");
1551 printf ("\t\tSET_CFLG (val & 1);\n");
1552 duplicate_carry ();
1553 printf ("\t\tval >>= 1;\n");
1554 printf ("\t}\n");
1555 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1556 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1557 break;
1558 case i_LSL:
1559 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1560 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1561 start_brace ();
1562 switch (curi->size) {
1563 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1564 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1565 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1566 default: abort ();
1567 }
1568 printf ("\tcnt &= 63;\n");
1569 printf ("\tCLEAR_CZNV;\n");
1570 printf ("\tif (cnt >= %d) {\n", bit_size (curi->size));
1571 printf ("\t\tSET_CFLG (cnt == %d ? val & 1 : 0);\n",
1572 bit_size (curi->size));
1573 duplicate_carry ();
1574 printf ("\t\tval = 0;\n");
1575 if (source_is_imm1_8 (curi))
1576 printf ("\t} else {\n");
1577 else
1578 printf ("\t} else if (cnt > 0) {\n");
1579 printf ("\t\tval <<= (cnt - 1);\n");
1580 printf ("\t\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1581 duplicate_carry ();
1582 printf ("\t\tval <<= 1;\n");
1583 printf ("\tval &= %s;\n", bit_mask (curi->size));
1584 printf ("\t}\n");
1585 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1586 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1587 break;
1588 case i_ROL:
1589 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1590 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1591 start_brace ();
1592 switch (curi->size) {
1593 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1594 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1595 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1596 default: abort ();
1597 }
1598 printf ("\tcnt &= 63;\n");
1599 printf ("\tCLEAR_CZNV;\n");
1600 if (source_is_imm1_8 (curi))
1601 printf ("{");
1602 else
1603 printf ("\tif (cnt > 0) {\n");
1604 printf ("\tuae_u32 loval;\n");
1605 printf ("\tcnt &= %d;\n", bit_size (curi->size) - 1);
1606 printf ("\tloval = val >> (%d - cnt);\n", bit_size (curi->size));
1607 printf ("\tval <<= cnt;\n");
1608 printf ("\tval |= loval;\n");
1609 printf ("\tval &= %s;\n", bit_mask (curi->size));
1610 printf ("\tSET_CFLG (val & 1);\n");
1611 printf ("}\n");
1612 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1613 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1614 break;
1615 case i_ROR:
1616 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1617 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1618 start_brace ();
1619 switch (curi->size) {
1620 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1621 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1622 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1623 default: abort ();
1624 }
1625 printf ("\tcnt &= 63;\n");
1626 printf ("\tCLEAR_CZNV;\n");
1627 if (source_is_imm1_8 (curi))
1628 printf ("{");
1629 else
1630 printf ("\tif (cnt > 0) {");
1631 printf ("\tuae_u32 hival;\n");
1632 printf ("\tcnt &= %d;\n", bit_size (curi->size) - 1);
1633 printf ("\thival = val << (%d - cnt);\n", bit_size (curi->size));
1634 printf ("\tval >>= cnt;\n");
1635 printf ("\tval |= hival;\n");
1636 printf ("\tval &= %s;\n", bit_mask (curi->size));
1637 printf ("\tSET_CFLG ((val & %s) >> %d);\n", cmask (curi->size), bit_size (curi->size) - 1);
1638 printf ("\t}\n");
1639 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1640 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1641 break;
1642 case i_ROXL:
1643 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1644 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1645 start_brace ();
1646 switch (curi->size) {
1647 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1648 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1649 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1650 default: abort ();
1651 }
1652 printf ("\tcnt &= 63;\n");
1653 printf ("\tCLEAR_CZNV;\n");
1654 if (source_is_imm1_8 (curi))
1655 printf ("{");
1656 else {
1657 force_range_for_rox ("cnt", curi->size);
1658 printf ("\tif (cnt > 0) {\n");
1659 }
1660 printf ("\tcnt--;\n");
1661 printf ("\t{\n\tuae_u32 carry;\n");
1662 printf ("\tuae_u32 loval = val >> (%d - cnt);\n", bit_size (curi->size) - 1);
1663 printf ("\tcarry = loval & 1;\n");
1664 printf ("\tval = (((val << 1) | GET_XFLG) << cnt) | (loval >> 1);\n");
1665 printf ("\tSET_XFLG (carry);\n");
1666 printf ("\tval &= %s;\n", bit_mask (curi->size));
1667 printf ("\t} }\n");
1668 printf ("\tSET_CFLG (GET_XFLG);\n");
1669 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1670 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1671 break;
1672 case i_ROXR:
1673 genamode (curi->smode, "srcreg", curi->size, "cnt", 1, 0);
1674 genamode (curi->dmode, "dstreg", curi->size, "data", 1, 0);
1675 start_brace ();
1676 switch (curi->size) {
1677 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1678 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1679 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1680 default: abort ();
1681 }
1682 printf ("\tcnt &= 63;\n");
1683 printf ("\tCLEAR_CZNV;\n");
1684 if (source_is_imm1_8 (curi))
1685 printf ("{");
1686 else {
1687 force_range_for_rox ("cnt", curi->size);
1688 printf ("\tif (cnt > 0) {\n");
1689 }
1690 printf ("\tcnt--;\n");
1691 printf ("\t{\n\tuae_u32 carry;\n");
1692 printf ("\tuae_u32 hival = (val << 1) | GET_XFLG;\n");
1693 printf ("\thival <<= (%d - cnt);\n", bit_size (curi->size) - 1);
1694 printf ("\tval >>= cnt;\n");
1695 printf ("\tcarry = val & 1;\n");
1696 printf ("\tval >>= 1;\n");
1697 printf ("\tval |= hival;\n");
1698 printf ("\tSET_XFLG (carry);\n");
1699 printf ("\tval &= %s;\n", bit_mask (curi->size));
1700 printf ("\t} }\n");
1701 printf ("\tSET_CFLG (GET_XFLG);\n");
1702 genflags (flag_logical_noclobber, curi->size, "val", "", "");
1703 genastore ("val", curi->dmode, "dstreg", curi->size, "data");
1704 break;
1705 case i_ASRW:
1706 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1707 start_brace ();
1708 switch (curi->size) {
1709 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1710 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1711 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1712 default: abort ();
1713 }
1714 printf ("\tuae_u32 sign = %s & val;\n", cmask (curi->size));
1715 printf ("\tuae_u32 cflg = val & 1;\n");
1716 printf ("\tval = (val >> 1) | sign;\n");
1717 genflags (flag_logical, curi->size, "val", "", "");
1718 printf ("\tSET_CFLG (cflg);\n");
1719 duplicate_carry ();
1720 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1721 break;
1722 case i_ASLW:
1723 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1724 start_brace ();
1725 switch (curi->size) {
1726 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1727 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1728 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1729 default: abort ();
1730 }
1731 printf ("\tuae_u32 sign = %s & val;\n", cmask (curi->size));
1732 printf ("\tuae_u32 sign2;\n");
1733 printf ("\tval <<= 1;\n");
1734 genflags (flag_logical, curi->size, "val", "", "");
1735 printf ("\tsign2 = %s & val;\n", cmask (curi->size));
1736 printf ("\tSET_CFLG (sign != 0);\n");
1737 duplicate_carry ();
1738
1739 printf ("\tSET_VFLG (GET_VFLG | (sign2 != sign));\n");
1740 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1741 break;
1742 case i_LSRW:
1743 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1744 start_brace ();
1745 switch (curi->size) {
1746 case sz_byte: printf ("\tuae_u32 val = (uae_u8)data;\n"); break;
1747 case sz_word: printf ("\tuae_u32 val = (uae_u16)data;\n"); break;
1748 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1749 default: abort ();
1750 }
1751 printf ("\tuae_u32 carry = val & 1;\n");
1752 printf ("\tval >>= 1;\n");
1753 genflags (flag_logical, curi->size, "val", "", "");
1754 printf ("SET_CFLG (carry);\n");
1755 duplicate_carry ();
1756 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1757 break;
1758 case i_LSLW:
1759 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1760 start_brace ();
1761 switch (curi->size) {
1762 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1763 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1764 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1765 default: abort ();
1766 }
1767 printf ("\tuae_u32 carry = val & %s;\n", cmask (curi->size));
1768 printf ("\tval <<= 1;\n");
1769 genflags (flag_logical, curi->size, "val", "", "");
1770 printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
1771 duplicate_carry ();
1772 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1773 break;
1774 case i_ROLW:
1775 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1776 start_brace ();
1777 switch (curi->size) {
1778 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1779 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1780 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1781 default: abort ();
1782 }
1783 printf ("\tuae_u32 carry = val & %s;\n", cmask (curi->size));
1784 printf ("\tval <<= 1;\n");
1785 printf ("\tif (carry) val |= 1;\n");
1786 genflags (flag_logical, curi->size, "val", "", "");
1787 printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
1788 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1789 break;
1790 case i_RORW:
1791 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1792 start_brace ();
1793 switch (curi->size) {
1794 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1795 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1796 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1797 default: abort ();
1798 }
1799 printf ("\tuae_u32 carry = val & 1;\n");
1800 printf ("\tval >>= 1;\n");
1801 printf ("\tif (carry) val |= %s;\n", cmask (curi->size));
1802 genflags (flag_logical, curi->size, "val", "", "");
1803 printf ("SET_CFLG (carry);\n");
1804 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1805 break;
1806 case i_ROXLW:
1807 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1808 start_brace ();
1809 switch (curi->size) {
1810 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1811 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1812 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1813 default: abort ();
1814 }
1815 printf ("\tuae_u32 carry = val & %s;\n", cmask (curi->size));
1816 printf ("\tval <<= 1;\n");
1817 printf ("\tif (GET_XFLG) val |= 1;\n");
1818 genflags (flag_logical, curi->size, "val", "", "");
1819 printf ("SET_CFLG (carry >> %d);\n", bit_size (curi->size) - 1);
1820 duplicate_carry ();
1821 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1822 break;
1823 case i_ROXRW:
1824 genamode (curi->smode, "srcreg", curi->size, "data", 1, 0);
1825 start_brace ();
1826 switch (curi->size) {
1827 case sz_byte: printf ("\tuae_u8 val = data;\n"); break;
1828 case sz_word: printf ("\tuae_u16 val = data;\n"); break;
1829 case sz_long: printf ("\tuae_u32 val = data;\n"); break;
1830 default: abort ();
1831 }
1832 printf ("\tuae_u32 carry = val & 1;\n");
1833 printf ("\tval >>= 1;\n");
1834 printf ("\tif (GET_XFLG) val |= %s;\n", cmask (curi->size));
1835 genflags (flag_logical, curi->size, "val", "", "");
1836 printf ("SET_CFLG (carry);\n");
1837 duplicate_carry ();
1838 genastore ("val", curi->smode, "srcreg", curi->size, "data");
1839 break;
1840 case i_MOVEC2:
1841 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1842 start_brace ();
1843 printf ("\tint regno = (src >> 12) & 15;\n");
1844 printf ("\tuae_u32 *regp = regs.regs + regno;\n");
1845 printf ("\tif (! m68k_movec2(src & 0xFFF, regp)) goto %s;\n", endlabelstr);
1846 break;
1847 case i_MOVE2C:
1848 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1849 start_brace ();
1850 printf ("\tint regno = (src >> 12) & 15;\n");
1851 printf ("\tuae_u32 *regp = regs.regs + regno;\n");
1852 printf ("\tif (! m68k_move2c(src & 0xFFF, regp)) goto %s;\n", endlabelstr);
1853 break;
1854 case i_CAS:
1855 {
1856 int old_brace_level;
1857 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
1858 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1859 start_brace ();
1860 printf ("\tint ru = (src >> 6) & 7;\n");
1861 printf ("\tint rc = src & 7;\n");
1862 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, rc)", "dst");
1863 printf ("\tif (GET_ZFLG)");
1864 old_brace_level = n_braces;
1865 start_brace ();
1866 genastore ("(m68k_dreg(regs, ru))", curi->dmode, "dstreg", curi->size, "dst");
1867 pop_braces (old_brace_level);
1868 printf ("else");
1869 start_brace ();
1870 printf ("m68k_dreg(regs, rc) = dst;\n");
1871 pop_braces (old_brace_level);
1872 }
1873 break;
1874 case i_CAS2:
1875 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1876 printf ("\tuae_u32 rn1 = regs.regs[(extra >> 28) & 15];\n");
1877 printf ("\tuae_u32 rn2 = regs.regs[(extra >> 12) & 15];\n");
1878 if (curi->size == sz_word) {
1879 int old_brace_level = n_braces;
1880 printf ("\tuae_u16 dst1 = get_word(rn1), dst2 = get_word(rn2);\n");
1881 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, (extra >> 16) & 7)", "dst1");
1882 printf ("\tif (GET_ZFLG) {\n");
1883 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, extra & 7)", "dst2");
1884 printf ("\tif (GET_ZFLG) {\n");
1885 printf ("\tput_word(rn1, m68k_dreg(regs, (extra >> 22) & 7));\n");
1886 printf ("\tput_word(rn1, m68k_dreg(regs, (extra >> 6) & 7));\n");
1887 printf ("\t}}\n");
1888 pop_braces (old_brace_level);
1889 printf ("\tif (! GET_ZFLG) {\n");
1890 printf ("\tm68k_dreg(regs, (extra >> 22) & 7) = (m68k_dreg(regs, (extra >> 22) & 7) & ~0xffff) | (dst1 & 0xffff);\n");
1891 printf ("\tm68k_dreg(regs, (extra >> 6) & 7) = (m68k_dreg(regs, (extra >> 6) & 7) & ~0xffff) | (dst2 & 0xffff);\n");
1892 printf ("\t}\n");
1893 } else {
1894 int old_brace_level = n_braces;
1895 printf ("\tuae_u32 dst1 = get_long(rn1), dst2 = get_long(rn2);\n");
1896 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, (extra >> 16) & 7)", "dst1");
1897 printf ("\tif (GET_ZFLG) {\n");
1898 genflags (flag_cmp, curi->size, "newv", "m68k_dreg(regs, extra & 7)", "dst2");
1899 printf ("\tif (GET_ZFLG) {\n");
1900 printf ("\tput_long(rn1, m68k_dreg(regs, (extra >> 22) & 7));\n");
1901 printf ("\tput_long(rn1, m68k_dreg(regs, (extra >> 6) & 7));\n");
1902 printf ("\t}}\n");
1903 pop_braces (old_brace_level);
1904 printf ("\tif (! GET_ZFLG) {\n");
1905 printf ("\tm68k_dreg(regs, (extra >> 22) & 7) = dst1;\n");
1906 printf ("\tm68k_dreg(regs, (extra >> 6) & 7) = dst2;\n");
1907 printf ("\t}\n");
1908 }
1909 break;
1910 case i_MOVES: /* ignore DFC and SFC because we have no MMU */
1911 {
1912 int old_brace_level;
1913 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1914 printf ("\tif (extra & 0x800)\n");
1915 old_brace_level = n_braces;
1916 start_brace ();
1917 printf ("\tuae_u32 src = regs.regs[(extra >> 12) & 15];\n");
1918 genamode (curi->dmode, "dstreg", curi->size, "dst", 2, 0);
1919 genastore ("src", curi->dmode, "dstreg", curi->size, "dst");
1920 pop_braces (old_brace_level);
1921 printf ("else");
1922 start_brace ();
1923 genamode (curi->dmode, "dstreg", curi->size, "src", 1, 0);
1924 printf ("\tif (extra & 0x8000) {\n");
1925 switch (curi->size) {
1926 case sz_byte: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = (uae_s32)(uae_s8)src;\n"); break;
1927 case sz_word: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = (uae_s32)(uae_s16)src;\n"); break;
1928 case sz_long: printf ("\tm68k_areg(regs, (extra >> 12) & 7) = src;\n"); break;
1929 default: abort ();
1930 }
1931 printf ("\t} else {\n");
1932 genastore ("src", Dreg, "(extra >> 12) & 7", curi->size, "");
1933 printf ("\t}\n");
1934 pop_braces (old_brace_level);
1935 }
1936 break;
1937 case i_BKPT: /* only needed for hardware emulators */
1938 sync_m68k_pc ();
1939 printf ("\top_illg(opcode);\n");
1940 break;
1941 case i_CALLM: /* not present in 68030 */
1942 sync_m68k_pc ();
1943 printf ("\top_illg(opcode);\n");
1944 break;
1945 case i_RTM: /* not present in 68030 */
1946 sync_m68k_pc ();
1947 printf ("\top_illg(opcode);\n");
1948 break;
1949 case i_TRAPcc:
1950 if (curi->smode != am_unknown && curi->smode != am_illg)
1951 genamode (curi->smode, "srcreg", curi->size, "dummy", 1, 0);
1952 printf ("\tif (cctrue(%d)) { Exception(7,m68k_getpc()); goto %s; }\n", curi->cc, endlabelstr);
1953 need_endlabel = 1;
1954 break;
1955 case i_DIVL:
1956 sync_m68k_pc ();
1957 start_brace ();
1958 printf ("\tuaecptr oldpc = m68k_getpc();\n");
1959 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1960 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1961 sync_m68k_pc ();
1962 printf ("\tm68k_divl(opcode, dst, extra, oldpc);\n");
1963 break;
1964 case i_MULL:
1965 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1966 genamode (curi->dmode, "dstreg", curi->size, "dst", 1, 0);
1967 sync_m68k_pc ();
1968 printf ("\tm68k_mull(opcode, dst, extra);\n");
1969 break;
1970 case i_BFTST:
1971 case i_BFEXTU:
1972 case i_BFCHG:
1973 case i_BFEXTS:
1974 case i_BFCLR:
1975 case i_BFFFO:
1976 case i_BFSET:
1977 case i_BFINS:
1978 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
1979 genamode (curi->dmode, "dstreg", sz_long, "dst", 2, 0);
1980 start_brace ();
1981 printf ("\tuae_s32 offset = extra & 0x800 ? m68k_dreg(regs, (extra >> 6) & 7) : (extra >> 6) & 0x1f;\n");
1982 printf ("\tint width = (((extra & 0x20 ? m68k_dreg(regs, extra & 7) : extra) -1) & 0x1f) +1;\n");
1983 if (curi->dmode == Dreg) {
1984 printf ("\tuae_u32 tmp = m68k_dreg(regs, dstreg) << (offset & 0x1f);\n");
1985 } else {
1986 printf ("\tuae_u32 tmp,bf0,bf1;\n");
1987 printf ("\tdsta += (offset >> 3) | (offset & 0x80000000 ? ~0x1fffffff : 0);\n");
1988 printf ("\tbf0 = get_long(dsta);bf1 = get_byte(dsta+4) & 0xff;\n");
1989 printf ("\ttmp = (bf0 << (offset & 7)) | (bf1 >> (8 - (offset & 7)));\n");
1990 }
1991 printf ("\ttmp >>= (32 - width);\n");
1992 printf ("\tSET_NFLG (tmp & (1 << (width-1)) ? 1 : 0);\n");
1993 printf ("\tSET_ZFLG (tmp == 0); SET_VFLG (0); SET_CFLG (0);\n");
1994 switch (curi->mnemo) {
1995 case i_BFTST:
1996 break;
1997 case i_BFEXTU:
1998 printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = tmp;\n");
1999 break;
2000 case i_BFCHG:
2001 printf ("\ttmp = ~tmp;\n");
2002 break;
2003 case i_BFEXTS:
2004 printf ("\tif (GET_NFLG) tmp |= width == 32 ? 0 : (-1 << width);\n");
2005 printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = tmp;\n");
2006 break;
2007 case i_BFCLR:
2008 printf ("\ttmp = 0;\n");
2009 break;
2010 case i_BFFFO:
2011 printf ("\t{ uae_u32 mask = 1 << (width-1);\n");
2012 printf ("\twhile (mask) { if (tmp & mask) break; mask >>= 1; offset++; }}\n");
2013 printf ("\tm68k_dreg(regs, (extra >> 12) & 7) = offset;\n");
2014 break;
2015 case i_BFSET:
2016 printf ("\ttmp = 0xffffffff;\n");
2017 break;
2018 case i_BFINS:
2019 printf ("\ttmp = m68k_dreg(regs, (extra >> 12) & 7);\n");
2020 break;
2021 default:
2022 break;
2023 }
2024 if (curi->mnemo == i_BFCHG
2025 || curi->mnemo == i_BFCLR
2026 || curi->mnemo == i_BFSET
2027 || curi->mnemo == i_BFINS)
2028 {
2029 printf ("\ttmp <<= (32 - width);\n");
2030 if (curi->dmode == Dreg) {
2031 printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & ((offset & 0x1f) == 0 ? 0 :\n");
2032 printf ("\t\t(0xffffffff << (32 - (offset & 0x1f))))) |\n");
2033 printf ("\t\t(tmp >> (offset & 0x1f)) |\n");
2034 printf ("\t\t(((offset & 0x1f) + width) >= 32 ? 0 :\n");
2035 printf (" (m68k_dreg(regs, dstreg) & ((uae_u32)0xffffffff >> ((offset & 0x1f) + width))));\n");
2036 } else {
2037 printf ("\tbf0 = (bf0 & (0xff000000 << (8 - (offset & 7)))) |\n");
2038 printf ("\t\t(tmp >> (offset & 7)) |\n");
2039 printf ("\t\t(((offset & 7) + width) >= 32 ? 0 :\n");
2040 printf ("\t\t (bf0 & ((uae_u32)0xffffffff >> ((offset & 7) + width))));\n");
2041 printf ("\tput_long(dsta,bf0 );\n");
2042 printf ("\tif (((offset & 7) + width) > 32) {\n");
2043 printf ("\t\tbf1 = (bf1 & (0xff >> (width - 32 + (offset & 7)))) |\n");
2044 printf ("\t\t\t(tmp << (8 - (offset & 7)));\n");
2045 printf ("\t\tput_byte(dsta+4,bf1);\n");
2046 printf ("\t}\n");
2047 }
2048 }
2049 break;
2050 case i_PACK:
2051 if (curi->smode == Dreg) {
2052 printf ("\tuae_u16 val = m68k_dreg(regs, srcreg) + %s;\n", gen_nextiword ());
2053 printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & 0xffffff00) | ((val >> 4) & 0xf0) | (val & 0xf);\n");
2054 } else {
2055 printf ("\tuae_u16 val;\n");
2056 printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2057 printf ("\tval = (uae_u16)get_byte(m68k_areg(regs, srcreg));\n");
2058 printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2059 printf ("\tval = (val | ((uae_u16)get_byte(m68k_areg(regs, srcreg)) << 8)) + %s;\n", gen_nextiword ());
2060 printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2061 printf ("\tput_byte(m68k_areg(regs, dstreg),((val >> 4) & 0xf0) | (val & 0xf));\n");
2062 }
2063 break;
2064 case i_UNPK:
2065 if (curi->smode == Dreg) {
2066 printf ("\tuae_u16 val = m68k_dreg(regs, srcreg);\n");
2067 printf ("\tval = (((val << 4) & 0xf00) | (val & 0xf)) + %s;\n", gen_nextiword ());
2068 printf ("\tm68k_dreg(regs, dstreg) = (m68k_dreg(regs, dstreg) & 0xffff0000) | (val & 0xffff);\n");
2069 } else {
2070 printf ("\tuae_u16 val;\n");
2071 printf ("\tm68k_areg(regs, srcreg) -= areg_byteinc[srcreg];\n");
2072 printf ("\tval = (uae_u16)get_byte(m68k_areg(regs, srcreg));\n");
2073 printf ("\tval = (((val << 4) & 0xf00) | (val & 0xf)) + %s;\n", gen_nextiword ());
2074 printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2075 printf ("\tput_byte(m68k_areg(regs, dstreg),val);\n");
2076 printf ("\tm68k_areg(regs, dstreg) -= areg_byteinc[dstreg];\n");
2077 printf ("\tput_byte(m68k_areg(regs, dstreg),val >> 8);\n");
2078 }
2079 break;
2080 case i_TAS:
2081 genamode (curi->smode, "srcreg", curi->size, "src", 1, 0);
2082 genflags (flag_logical, curi->size, "src", "", "");
2083 printf ("\tsrc |= 0x80;\n");
2084 genastore ("src", curi->smode, "srcreg", curi->size, "src");
2085 break;
2086 case i_FPP:
2087 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2088 sync_m68k_pc ();
2089 swap_opcode ();
2090 printf ("\tfpp_opp(opcode,extra);\n");
2091 break;
2092 case i_FDBcc:
2093 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2094 sync_m68k_pc ();
2095 swap_opcode ();
2096 printf ("\tfdbcc_opp(opcode,extra);\n");
2097 break;
2098 case i_FScc:
2099 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2100 sync_m68k_pc ();
2101 swap_opcode ();
2102 printf ("\tfscc_opp(opcode,extra);\n");
2103 break;
2104 case i_FTRAPcc:
2105 sync_m68k_pc ();
2106 start_brace ();
2107 printf ("\tuaecptr oldpc = m68k_getpc();\n");
2108 if (curi->smode != am_unknown && curi->smode != am_illg)
2109 genamode (curi->smode, "srcreg", curi->size, "dummy", 1, 0);
2110 sync_m68k_pc ();
2111 swap_opcode ();
2112 printf ("\tftrapcc_opp(opcode,oldpc);\n");
2113 break;
2114 case i_FBcc:
2115 sync_m68k_pc ();
2116 start_brace ();
2117 printf ("\tuaecptr pc = m68k_getpc();\n");
2118 genamode (curi->dmode, "srcreg", curi->size, "extra", 1, 0);
2119 sync_m68k_pc ();
2120 swap_opcode ();
2121 printf ("\tfbcc_opp(opcode,pc,extra);\n");
2122 break;
2123 case i_FSAVE:
2124 sync_m68k_pc ();
2125 swap_opcode ();
2126 printf ("\tfsave_opp(opcode);\n");
2127 break;
2128 case i_FRESTORE:
2129 sync_m68k_pc ();
2130 swap_opcode ();
2131 printf ("\tfrestore_opp(opcode);\n");
2132 break;
2133 case i_CINVL:
2134 case i_CINVP:
2135 case i_CINVA:
2136 case i_CPUSHL:
2137 case i_CPUSHP:
2138 case i_CPUSHA:
2139 break;
2140 case i_MOVE16:
2141 if ((opcode & 0xfff8) == 0xf620) {
2142 /* MOVE16 (Ax)+,(Ay)+ */
2143 printf ("\tuaecptr mems = m68k_areg(regs, srcreg) & ~15, memd;\n");
2144 printf ("\tdstreg = (%s >> 12) & 7;\n", gen_nextiword());
2145 printf ("\tmemd = m68k_areg(regs, dstreg) & ~15;\n");
2146 printf ("\tput_long(memd, get_long(mems));\n");
2147 printf ("\tput_long(memd+4, get_long(mems+4));\n");
2148 printf ("\tput_long(memd+8, get_long(mems+8));\n");
2149 printf ("\tput_long(memd+12, get_long(mems+12));\n");
2150 printf ("\tif (srcreg != dstreg)\n");
2151 printf ("\tm68k_areg(regs, srcreg) += 16;\n");
2152 printf ("\tm68k_areg(regs, dstreg) += 16;\n");
2153 }
2154 else {
2155 /* Other variants */
2156 genamode (curi->smode, "srcreg", curi->size, "mems", 0, 2);
2157 genamode (curi->dmode, "dstreg", curi->size, "memd", 0, 2);
2158 printf ("\tmemsa &= ~15;\n");
2159 printf ("\tmemda &= ~15;\n");
2160 printf ("\tput_long(memda, get_long(memsa));\n");
2161 printf ("\tput_long(memda+4, get_long(memsa+4));\n");
2162 printf ("\tput_long(memda+8, get_long(memsa+8));\n");
2163 printf ("\tput_long(memda+12, get_long(memsa+12));\n");
2164 if ((opcode & 0xfff8) == 0xf600)
2165 printf ("\tm68k_areg(regs, srcreg) += 16;\n");
2166 else if ((opcode & 0xfff8) == 0xf608)
2167 printf ("\tm68k_areg(regs, dstreg) += 16;\n");
2168 }
2169 break;
2170 case i_MMUOP:
2171 genamode (curi->smode, "srcreg", curi->size, "extra", 1, 0);
2172 sync_m68k_pc ();
2173 swap_opcode ();
2174 printf ("\tmmu_op(opcode,extra);\n");
2175 break;
2176 default:
2177 abort ();
2178 break;
2179 }
2180 finish_braces ();
2181 sync_m68k_pc ();
2182 }
2183
2184 static void generate_includes (FILE * f)
2185 {
2186 fprintf (f, "#include \"sysdeps.h\"\n");
2187 fprintf (f, "#include \"m68k.h\"\n");
2188 fprintf (f, "#include \"memory.h\"\n");
2189 fprintf (f, "#include \"readcpu.h\"\n");
2190 fprintf (f, "#include \"newcpu.h\"\n");
2191 fprintf (f, "#include \"cputbl.h\"\n");
2192 }
2193
2194 static int postfix;
2195
2196 static void generate_one_opcode (int rp)
2197 {
2198 int i;
2199 uae_u16 smsk, dmsk;
2200 long int opcode = opcode_map[rp];
2201
2202 if (table68k[opcode].mnemo == i_ILLG
2203 || table68k[opcode].clev > cpu_level)
2204 return;
2205
2206 for (i = 0; lookuptab[i].name[0]; i++) {
2207 if (table68k[opcode].mnemo == lookuptab[i].mnemo)
2208 break;
2209 }
2210
2211 if (table68k[opcode].handler != -1)
2212 return;
2213
2214 if (opcode_next_clev[rp] != cpu_level) {
2215 fprintf (stblfile, "{ op_%lx_%d, 0, %ld }, /* %s */\n", opcode, opcode_last_postfix[rp],
2216 opcode, lookuptab[i].name);
2217 return;
2218 }
2219 fprintf (stblfile, "{ op_%lx_%d, 0, %ld }, /* %s */\n", opcode, postfix, opcode, lookuptab[i].name);
2220 fprintf (headerfile, "extern cpuop_func op_%lx_%d;\n", opcode, postfix);
2221 printf ("void REGPARAM2 op_%lx_%d(uae_u32 opcode) /* %s */\n{\n", opcode, postfix, lookuptab[i].name);
2222
2223 switch (table68k[opcode].stype) {
2224 case 0: smsk = 7; break;
2225 case 1: smsk = 255; break;
2226 case 2: smsk = 15; break;
2227 case 3: smsk = 7; break;
2228 case 4: smsk = 7; break;
2229 case 5: smsk = 63; break;
2230 case 7: smsk = 3; break;
2231 default: abort ();
2232 }
2233 dmsk = 7;
2234
2235 next_cpu_level = -1;
2236 if (table68k[opcode].suse
2237 && table68k[opcode].smode != imm && table68k[opcode].smode != imm0
2238 && table68k[opcode].smode != imm1 && table68k[opcode].smode != imm2
2239 && table68k[opcode].smode != absw && table68k[opcode].smode != absl
2240 && table68k[opcode].smode != PC8r && table68k[opcode].smode != PC16)
2241 {
2242 if (table68k[opcode].spos == -1) {
2243 if (((int) table68k[opcode].sreg) >= 128)
2244 printf ("\tuae_u32 srcreg = (uae_s32)(uae_s8)%d;\n", (int) table68k[opcode].sreg);
2245 else
2246 printf ("\tuae_u32 srcreg = %d;\n", (int) table68k[opcode].sreg);
2247 } else {
2248 char source[100];
2249 int pos = table68k[opcode].spos;
2250
2251 #if 0
2252 /* Check that we can do the little endian optimization safely. */
2253 if (pos < 8 && (smsk >> (8 - pos)) != 0)
2254 abort ();
2255 #endif
2256 printf ("#ifdef HAVE_GET_WORD_UNSWAPPED\n");
2257
2258 if (pos < 8 && (smsk >> (8 - pos)) != 0)
2259 sprintf (source, "(((opcode >> %d) | (opcode << %d)) & %d)",
2260 pos ^ 8, 8 - pos, dmsk);
2261 else if (pos != 8)
2262 sprintf (source, "((opcode >> %d) & %d)", pos ^ 8, smsk);
2263 else
2264 sprintf (source, "(opcode & %d)", smsk);
2265
2266 if (table68k[opcode].stype == 3)
2267 printf ("\tuae_u32 srcreg = imm8_table[%s];\n", source);
2268 else if (table68k[opcode].stype == 1)
2269 printf ("\tuae_u32 srcreg = (uae_s32)(uae_s8)%s;\n", source);
2270 else
2271 printf ("\tuae_u32 srcreg = %s;\n", source);
2272
2273 printf ("#else\n");
2274
2275 if (pos)
2276 sprintf (source, "((opcode >> %d) & %d)", pos, smsk);
2277 else
2278 sprintf (source, "(opcode & %d)", smsk);
2279
2280 if (table68k[opcode].stype == 3)
2281 printf ("\tuae_u32 srcreg = imm8_table[%s];\n", source);
2282 else if (table68k[opcode].stype == 1)
2283 printf ("\tuae_u32 srcreg = (uae_s32)(uae_s8)%s;\n", source);
2284 else
2285 printf ("\tuae_u32 srcreg = %s;\n", source);
2286
2287 printf ("#endif\n");
2288 }
2289 }
2290 if (table68k[opcode].duse
2291 /* Yes, the dmode can be imm, in case of LINK or DBcc */
2292 && table68k[opcode].dmode != imm && table68k[opcode].dmode != imm0
2293 && table68k[opcode].dmode != imm1 && table68k[opcode].dmode != imm2
2294 && table68k[opcode].dmode != absw && table68k[opcode].dmode != absl)
2295 {
2296 if (table68k[opcode].dpos == -1) {
2297 if (((int) table68k[opcode].dreg) >= 128)
2298 printf ("\tuae_u32 dstreg = (uae_s32)(uae_s8)%d;\n", (int) table68k[opcode].dreg);
2299 else
2300 printf ("\tuae_u32 dstreg = %d;\n", (int) table68k[opcode].dreg);
2301 } else {
2302 int pos = table68k[opcode].dpos;
2303 #if 0
2304 /* Check that we can do the little endian optimization safely. */
2305 if (pos < 8 && (dmsk >> (8 - pos)) != 0)
2306 abort ();
2307 #endif
2308 printf ("#ifdef HAVE_GET_WORD_UNSWAPPED\n");
2309
2310 if (pos < 8 && (dmsk >> (8 - pos)) != 0)
2311 printf ("\tuae_u32 dstreg = ((opcode >> %d) | (opcode << %d)) & %d;\n",
2312 pos ^ 8, 8 - pos, dmsk);
2313 else if (pos != 8)
2314 printf ("\tuae_u32 dstreg = (opcode >> %d) & %d;\n",
2315 pos ^ 8, dmsk);
2316 else
2317 printf ("\tuae_u32 dstreg = opcode & %d;\n", dmsk);
2318
2319 printf ("#else\n");
2320
2321 if (pos)
2322 printf ("\tuae_u32 dstreg = (opcode >> %d) & %d;\n",
2323 pos, dmsk);
2324 else
2325 printf ("\tuae_u32 dstreg = opcode & %d;\n", dmsk);
2326
2327 printf ("#endif\n");
2328 }
2329 }
2330 need_endlabel = 0;
2331 endlabelno++;
2332 sprintf (endlabelstr, "endlabel%d", endlabelno);
2333 gen_opcode (opcode);
2334 if (need_endlabel)
2335 printf ("%s: ;\n", endlabelstr);
2336 printf ("}\n");
2337 opcode_next_clev[rp] = next_cpu_level;
2338 opcode_last_postfix[rp] = postfix;
2339 }
2340
2341 static void generate_func (void)
2342 {
2343 int i, j, rp;
2344
2345 using_prefetch = 0;
2346 using_exception_3 = 0;
2347 #if !USE_PREFETCH_BUFFER
2348 /* gb-- No need for a prefetch buffer, nor exception 3 handling */
2349 /* Anyway, Basilisk2 does not use the op_smalltbl_5 table... */
2350 for (i = 0; i <= 4; i++) {
2351 #else
2352 for (i = 0; i < 6; i++) {
2353 #endif
2354 cpu_level = 4 - i;
2355 if (i == 5) {
2356 cpu_level = 0;
2357 using_prefetch = 1;
2358 using_exception_3 = 1;
2359 for (rp = 0; rp < nr_cpuop_funcs; rp++)
2360 opcode_next_clev[rp] = 0;
2361 }
2362 postfix = i;
2363 fprintf (stblfile, "struct cputbl op_smalltbl_%d[] = {\n", postfix);
2364
2365 /* sam: this is for people with low memory (eg. me :)) */
2366 printf ("\n"
2367 "#if !defined(PART_1) && !defined(PART_2) && "
2368 "!defined(PART_3) && !defined(PART_4) && "
2369 "!defined(PART_5) && !defined(PART_6) && "
2370 "!defined(PART_7) && !defined(PART_8)"
2371 "\n"
2372 "#define PART_1 1\n"
2373 "#define PART_2 1\n"
2374 "#define PART_3 1\n"
2375 "#define PART_4 1\n"
2376 "#define PART_5 1\n"
2377 "#define PART_6 1\n"
2378 "#define PART_7 1\n"
2379 "#define PART_8 1\n"
2380 "#endif\n\n");
2381
2382 rp = 0;
2383 for(j=1;j<=8;++j) {
2384 int k = (j*nr_cpuop_funcs)/8;
2385 printf ("#ifdef PART_%d\n",j);
2386 for (; rp < k; rp++)
2387 generate_one_opcode (rp);
2388 printf ("#endif\n\n");
2389 }
2390
2391 fprintf (stblfile, "{ 0, 0, 0 }};\n");
2392 }
2393 }
2394
2395 int main (int argc, char **argv)
2396 {
2397 read_table68k ();
2398 do_merges ();
2399
2400 opcode_map = (int *) malloc (sizeof (int) * nr_cpuop_funcs);
2401 opcode_last_postfix = (int *) malloc (sizeof (int) * nr_cpuop_funcs);
2402 opcode_next_clev = (int *) malloc (sizeof (int) * nr_cpuop_funcs);
2403 counts = (unsigned long *) malloc (65536 * sizeof (unsigned long));
2404 read_counts ();
2405
2406 /* It would be a lot nicer to put all in one file (we'd also get rid of
2407 * cputbl.h that way), but cpuopti can't cope. That could be fixed, but
2408 * I don't dare to touch the 68k version. */
2409
2410 headerfile = fopen ("cputbl.h", "wb");
2411 stblfile = fopen ("cpustbl.cpp", "wb");
2412 freopen ("cpuemu.cpp", "wb", stdout);
2413
2414 generate_includes (stdout);
2415 generate_includes (stblfile);
2416
2417 generate_func ();
2418
2419 free (table68k);
2420 return 0;
2421 }