Annotation of hatari/src/uae-cpu/newcpu.c, revision 1.1.1.14

1.1       root        1:  /*
1.1.1.2   root        2:   * UAE - The Un*x Amiga Emulator - CPU core
1.1       root        3:   *
                      4:   * MC68000 emulation
                      5:   *
                      6:   * (c) 1995 Bernd Schmidt
1.1.1.2   root        7:   *
                      8:   * Adaptation to Hatari by Thomas Huth
                      9:   *
1.1.1.6   root       10:   * This file is distributed under the GNU Public License, version 2 or at
                     11:   * your option any later version. Read the file gpl.txt for details.
1.1       root       12:   */
1.1.1.12  root       13: 
                     14: 
                     15: /* 2007/11/12  [NP]    Add HATARI_TRACE_CPU_DISASM.                                                    */
                     16: /* 2007/11/15  [NP]    In MakeFromSR, writes to m and t0 should be ignored and set to 0 if cpu < 68020 */
                     17: /* 2007/11/26  [NP]    We set BusErrorPC in m68k_run_1 instead of M68000_BusError, else the BusErrorPC */
                     18: /*                     will not point to the opcode that generated the bus error.                      */
                     19: /*                     Huge debug/work on Exceptions 2/3 stack frames, result is more accurate and     */
                     20: /*                     allow to pass the very tricky Transbeauce 2 Demo's protection.                  */
                     21: /* 2007/11/28  [NP]    Backport DIVS/DIVU cycles exact routines from WinUAE (original work by Jorge    */
                     22: /*                     Cwik, [email protected]).                                                       */
                     23: /* 2007/12/06  [NP]    The PC stored in the stack frame for the bus error is complex to emulate,       */
                     24: /*                     because it doesn't necessarily point to the next instruction after the one that */
                     25: /*                     triggered the bus error. In the case of the Transbeauce 2 Demo, after           */
                     26: /*                     'move.l $0.w,$24.w', PC is incremented of 4 bytes, not 6, and stored in the     */
                     27: /*                     stack. Special case to decrement PC of 2 bytes if opcode is '21f8'.             */
                     28: /*                     This should be fixed with a real model.                                         */
                     29: /* 2007/12/07  [NP]    If Trace is enabled and a group 2 exception occurs (such as CHK), the trace     */
                     30: /*                     handler should be called after the group 2's handler. If a bus error, address   */
                     31: /*                     error or illegal occurs while Trace is enabled, the trace handler should not be */
                     32: /*                     called after this instruction (Transbeauce 2 Demo, Phaleon Demo).               */
                     33: /*                     This means that if a CHK is executed while trace bit was set, we must set PC    */
                     34: /*                     to CHK handler, turn trace off in the internal SR, but we must still call the   */
                     35: /*                     trace handler one last time with the PC set to the CHK's handler (even if       */
                     36: /*                     trace mode is internally turned off while processing an exception). Once trace  */
                     37: /*                     handler is finished (RTE), we return to the CHK's handler.                      */
                     38: /*                     This is true for DIV BY 0, CHK, TRAPV and TRAP.                                 */
                     39: /*                     Backport exception_trace() from WinUAE to handle this behaviour (used in        */
                     40: /*                     Transbeauce 2 demo).                                                            */
                     41: /* 2007/12/09  [NP]    'dc.w $a' should not be used to call 'OpCode_SysInit' but should give an illegal*/
                     42: /*                     instruction (Transbeauce 2 demo).                                               */
                     43: /*                     Instead of always replacing the illegal instructions $8, $a and $c by the       */
                     44: /*                     3 functions required for HD emulation, we now do it in cart.c only if the       */
                     45: /*                     built-in cartridge image is loaded.                                             */
                     46: /*                     YEAH! Hatari is now the first emulator to pass the Transbeauce 2 protection :)  */
                     47: /* 2007/12/18  [NP]    More precise timings for HBL, VBL and MFP interrupts. On ST, these interrupts   */
                     48: /*                     are taking 56 cycles instead of the 44 cycles in the 68000's documentation.     */
                     49: /* 2007/12/24  [NP]    If an interrupt (HBL, VBL) is pending after intruction 'n' was processed, the   */
                     50: /*                     exception should be called before instr. 'n+1' is processed, not after (else the*/
                     51: /*                     interrupt's handler is delayed by one 68000's instruction, which could break    */
                     52: /*                     some demos with too strict timings) (ACF's Demo Main Menu).                     */
                     53: /*                     We call the interrupt if ( SPCFLAG_INT | SPCFLAG_DOINT ) is set, not only if    */
                     54: /*                     SPCFLAG_DOINT is set (as it was already the case when handling 'STOP').         */
                     55: /* 2007/12/25  [NP]    FIXME When handling exceptions' cycles, using nr >= 64 to determine if this is  */
                     56: /*                     an MFP exception could be wrong if the MFP VR was set to another value than the */
                     57: /*                     default $40 (this could be a problem with programs requiring a precise cycles   */
                     58: /*                     calculation while changing VR, but no such programs were encountered so far).   */
1.1.1.13  root       59: /*                     -> FIXED, see 2008/10/05                                                        */
                     60: /* 2008/04/17  [NP]    In m68k_run_1/m68k_run_2, add the wait state cycles before testing if content   */
                     61: /*                     of PendingInterruptCount is <= 0 (else the int could happen a few cycles earlier*/
                     62: /*                     than expected in some rare cases (reading $fffa21 in BIG Demo Screen 1)).       */
                     63: /* 2008/09/14  [NP]    Add the value of the new PC in the exception's log.                             */
                     64: /* 2008/09/14  [NP]    Correct cycles for TRAP are 34 not 38 (4 more cycles were counted because cpuemu*/
                     65: /*                     returns 4 and Exception() adds 34) (Phaleon / Illusion Demo by Next).           */
                     66: /*                     FIXME : Others exception cycles may be wrong too.                               */
                     67: /* 2008/10/05  [NP]    Add a parameter 'ExceptionSource' to Exception(). This allows to know the source*/
                     68: /*                     of the exception (video, mfp, cpu) and properly handle MFP interrupts. Since    */
                     69: /*                     it's possible to change the vector base in $fffa17, MFP int vectors can overlap */
                     70: /*                     the 'normal' 68000 ones and the exception number is not enough to decide.       */
                     71: /*                     We need ExceptionSource to remove the ambiguity.                                */
                     72: /*                     Fix High Fidelity Dreams by Aura which sets MFP vector base to $c0 instead of   */
                     73: /*                     $100. In that case, timer B int becomes exception nr 56 and conflicts with the  */
                     74: /*                     'MMU config error' exception, which takes 4 cycles instead of 56 cycles for MFP.*/
                     75: /* 2008/11/18  [NP]    In 'do_specialties()', when the cpu is in the STOP state, we must test all      */
                     76: /*                     possible int handlers while PendingInterruptCount <= 0 without increasing the   */
                     77: /*                     cpu cycle counter. In the case where both an MFP int and an HBL occur at the    */
1.1.1.14! root       78: /*                     same time for example, the HBL was delayed by 4 cycles if no MFP exception      */
1.1.1.13  root       79: /*                     was triggered, which was wrong (this happened mainly with the TOS timer D that  */
                     80: /*                     expires very often). Such precision is required for very recent hardscroll      */
                     81: /*                     techniques that use 'stop' to stay in sync with the video shifter.              */
1.1.1.14! root       82: /* 2008/11/23  [NP]    In 'do_specialties()', when in STOP state, we must first test for a pending     */
        !            83: /*                     interrupt that would exit the STOP state immediatly, without doing a 'while'    */
        !            84: /*                     loop until 'SPCFLAG_INT' or 'SPCFLAG_DOINT' are set.                            */
        !            85: /* 2008/11/29  [NP]    Call 'InterruptAddJitter()' when a video interrupt happens to precisely emulate */
        !            86: /*                     the jitter happening on the Atari (see video.c for the jitter patterns).        */
        !            87: /*                     FIXME : Pattern is not always correct when handling pending interrupt in STOP   */
        !            88: /*                     state, but this should be harmless as no program has been found using this.     */
        !            89: /* 2008/12/05  [NP]    On Atari it takes 56 cycles to process an interrupt. During that time, a higher */
        !            90: /*                     level interrupt could happen and we must execute it before the previous int     */
        !            91: /*                     (see m68k_run_1()).                                                             */
        !            92: /*                     This is the case for the VBL which can interrupt the last HBL of a screen       */
        !            93: /*                     (end of line 312) at various point (from 0 to 8 cycles).                        */
        !            94: /*                     This fixes the fullscreen tunnel in Suretrip 49% by Checkpoint, which uses a    */
        !            95: /*                     really buggy vbl/hbl combination, even on a real ST. Also fixes sample sound    */
        !            96: /*                     in Swedish New Year's TCB screen.                                               */
        !            97: /* 2008/12/11  [NP]    Extract interrupt handling from do_specialties() in do_specialties_interrupt()  */
        !            98: /*                     and factorize some code. In m68k_run_1 when testing for multiple interrupts at  */
        !            99: /*                     the same time, call do_specialties_interrupt() to check only the special flags  */
        !           100: /*                     related to interrupts (MFP and video) (else, this caused problem when the TRACE */
        !           101: /*                     flag was set).                                                                  */
        !           102: /* 2008/12/14  [NP]    In m68k_run_1(), we should check for simultaneous ints only if the cpu is not   */
        !           103: /*                     in the STOP state after the last instruction was executed. Else, the call to    */
        !           104: /*                     do_specialties_interrupt() could acknowledge the interrupt and we would never   */
        !           105: /*                     exit the STOP state in do_specialties() just after (the problem can happen if   */
        !           106: /*                     the TOS timer D expires just at the same time as the STOP instruction).         */
        !           107: /*                     Fix regression since 2008/12/11 in the hidden screen from ULM in Oh Crickey...  */
        !           108: /* 2008/12/20  [NP]    In m68k_run_1(), when checking interrupts and STOP mode, we should test         */
        !           109: /*                     PendingInterruptCount before regs.spcflags to have a faster evaluation of the   */
        !           110: /*                     'while' condition (PendingInterruptCount <= 0 is true less often than STOP==0)  */
1.1.1.12  root      111: 
                    112: 
                    113: 
1.1.1.14! root      114: 
        !           115: const char NewCpu_rcsid[] = "Hatari $Id: newcpu.c,v 1.64 2008-12-14 15:19:27 npomarede Exp $";
1.1       root      116: 
                    117: #include "sysdeps.h"
                    118: #include "hatari-glue.h"
                    119: #include "maccess.h"
                    120: #include "memory.h"
                    121: #include "newcpu.h"
1.1.1.5   root      122: #include "../includes/main.h"
1.1.1.10  root      123: #include "../includes/log.h"
1.1.1.7   root      124: #include "../includes/m68000.h"
1.1.1.12  root      125: #include "../includes/int.h"
1.1.1.8   root      126: #include "../includes/mfp.h"
1.1       root      127: #include "../includes/tos.h"
1.1.1.5   root      128: #include "../includes/vdi.h"
                    129: #include "../includes/cart.h"
                    130: #include "../includes/debugui.h"
1.1.1.8   root      131: #include "../includes/bios.h"
                    132: #include "../includes/xbios.h"
1.1.1.12  root      133: #include "../includes/video.h"
1.1.1.13  root      134: #include "../includes/options.h"
1.1       root      135: 
1.1.1.12  root      136: //#define DEBUG_PREFETCH
1.1       root      137: 
                    138: struct flag_struct regflags;
                    139: 
                    140: /* Opcode of faulting instruction */
                    141: uae_u16 last_op_for_exception_3;
                    142: /* PC at fault time */
                    143: uaecptr last_addr_for_exception_3;
                    144: /* Address that generated the exception */
                    145: uaecptr last_fault_for_exception_3;
                    146: 
1.1.1.11  root      147: const int areg_byteinc[] = { 1,1,1,1,1,1,1,2 };
                    148: const int imm8_table[] = { 8,1,2,3,4,5,6,7 };
1.1       root      149: 
                    150: int movem_index1[256];
                    151: int movem_index2[256];
                    152: int movem_next[256];
                    153: 
                    154: int fpp_movem_index1[256];
                    155: int fpp_movem_index2[256];
                    156: int fpp_movem_next[256];
                    157: 
                    158: cpuop_func *cpufunctbl[65536];
                    159: 
1.1.1.12  root      160: int OpcodeFamily;
1.1.1.6   root      161: 
1.1       root      162: #define COUNT_INSTRS 0
                    163: 
                    164: #if COUNT_INSTRS
                    165: static unsigned long int instrcount[65536];
                    166: static uae_u16 opcodenums[65536];
                    167: 
                    168: static int compfn (const void *el1, const void *el2)
                    169: {
                    170:     return instrcount[*(const uae_u16 *)el1] < instrcount[*(const uae_u16 *)el2];
                    171: }
                    172: 
                    173: static char *icountfilename (void)
                    174: {
                    175:     char *name = getenv ("INSNCOUNT");
                    176:     if (name)
                    177:        return name;
                    178:     return COUNT_INSTRS == 2 ? "frequent.68k" : "insncount";
                    179: }
                    180: 
                    181: void dump_counts (void)
                    182: {
                    183:     FILE *f = fopen (icountfilename (), "w");
                    184:     unsigned long int total;
                    185:     int i;
                    186: 
                    187:     write_log ("Writing instruction count file...\n");
                    188:     for (i = 0; i < 65536; i++) {
                    189:        opcodenums[i] = i;
                    190:        total += instrcount[i];
                    191:     }
                    192:     qsort (opcodenums, 65536, sizeof(uae_u16), compfn);
                    193: 
                    194:     fprintf (f, "Total: %lu\n", total);
                    195:     for (i=0; i < 65536; i++) {
                    196:        unsigned long int cnt = instrcount[opcodenums[i]];
                    197:        struct instr *dp;
                    198:        struct mnemolookup *lookup;
                    199:        if (!cnt)
                    200:            break;
                    201:        dp = table68k + opcodenums[i];
                    202:        for (lookup = lookuptab;lookup->mnemo != dp->mnemo; lookup++)
                    203:            ;
                    204:        fprintf (f, "%04x: %lu %s\n", opcodenums[i], cnt, lookup->name);
                    205:     }
                    206:     fclose (f);
                    207: }
                    208: #else
                    209: void dump_counts (void)
                    210: {
                    211: }
                    212: #endif
                    213: 
                    214: 
                    215: static unsigned long op_illg_1 (uae_u32 opcode) REGPARAM;
                    216: 
                    217: static unsigned long REGPARAM2 op_illg_1 (uae_u32 opcode)
                    218: {
1.1.1.6   root      219:     op_illg (opcode);
1.1       root      220:     return 4;
                    221: }
                    222: 
1.1.1.4   root      223: 
                    224: void build_cpufunctbl(void)
1.1       root      225: {
                    226:     int i;
                    227:     unsigned long opcode;
1.1.1.12  root      228:     const struct cputbl *tbl = (currprefs.cpu_level == 4 ? op_smalltbl_0_ff
                    229:                              : currprefs.cpu_level == 3 ? op_smalltbl_1_ff
                    230:                              : currprefs.cpu_level == 2 ? op_smalltbl_2_ff
                    231:                              : currprefs.cpu_level == 1 ? op_smalltbl_3_ff
                    232:                              : ! currprefs.cpu_compatible ? op_smalltbl_4_ff
1.1.1.11  root      233:                              : op_smalltbl_5_ff);
1.1       root      234: 
1.1.1.10  root      235:     Log_Printf(LOG_DEBUG, "Building CPU function table (%d %d %d).\n",
1.1.1.12  root      236:                   currprefs.cpu_level, currprefs.cpu_compatible, currprefs.address_space_24);
1.1       root      237: 
                    238:     for (opcode = 0; opcode < 65536; opcode++)
1.1.1.6   root      239:        cpufunctbl[opcode] = op_illg_1;
1.1       root      240:     for (i = 0; tbl[i].handler != NULL; i++) {
                    241:        if (! tbl[i].specific)
1.1.1.6   root      242:            cpufunctbl[tbl[i].opcode] = tbl[i].handler;
1.1       root      243:     }
                    244:     for (opcode = 0; opcode < 65536; opcode++) {
                    245:        cpuop_func *f;
                    246: 
1.1.1.12  root      247:        if (table68k[opcode].mnemo == i_ILLG || table68k[opcode].clev > currprefs.cpu_level)
1.1       root      248:            continue;
                    249: 
                    250:        if (table68k[opcode].handler != -1) {
1.1.1.6   root      251:            f = cpufunctbl[table68k[opcode].handler];
1.1       root      252:            if (f == op_illg_1)
                    253:                abort();
1.1.1.6   root      254:            cpufunctbl[opcode] = f;
1.1       root      255:        }
                    256:     }
                    257:     for (i = 0; tbl[i].handler != NULL; i++) {
                    258:        if (tbl[i].specific)
1.1.1.6   root      259:            cpufunctbl[tbl[i].opcode] = tbl[i].handler;
1.1       root      260:     }
                    261: }
                    262: 
                    263: 
                    264: 
                    265: void init_m68k (void)
                    266: {
                    267:     int i;
                    268: 
                    269:     for (i = 0 ; i < 256 ; i++) {
                    270:        int j;
                    271:        for (j = 0 ; j < 8 ; j++) {
                    272:                if (i & (1 << j)) break;
                    273:        }
                    274:        movem_index1[i] = j;
                    275:        movem_index2[i] = 7-j;
                    276:        movem_next[i] = i & (~(1 << j));
                    277:     }
                    278:     for (i = 0 ; i < 256 ; i++) {
                    279:        int j;
                    280:        for (j = 7 ; j >= 0 ; j--) {
                    281:                if (i & (1 << j)) break;
                    282:        }
                    283:        fpp_movem_index1[i] = 7-j;
                    284:        fpp_movem_index2[i] = j;
                    285:        fpp_movem_next[i] = i & (~(1 << j));
                    286:     }
                    287: #if COUNT_INSTRS
                    288:     {
                    289:        FILE *f = fopen (icountfilename (), "r");
                    290:        memset (instrcount, 0, sizeof instrcount);
                    291:        if (f) {
                    292:            uae_u32 opcode, count, total;
                    293:            char name[20];
                    294:            write_log ("Reading instruction count file...\n");
                    295:            fscanf (f, "Total: %lu\n", &total);
                    296:            while (fscanf (f, "%lx: %lu %s\n", &opcode, &count, name) == 3) {
                    297:                instrcount[opcode] = count;
                    298:            }
                    299:            fclose(f);
                    300:        }
                    301:     }
                    302: #endif
                    303:     write_log ("Building CPU table for configuration: 68");
1.1.1.12  root      304:     if (currprefs.address_space_24 && currprefs.cpu_level > 1)
1.1       root      305:         write_log ("EC");
1.1.1.12  root      306:     switch (currprefs.cpu_level) {
1.1       root      307:     case 1:
                    308:         write_log ("010");
                    309:         break;
                    310:     case 2:
                    311:         write_log ("020");
                    312:         break;
                    313:     case 3:
                    314:         write_log ("020/881");
                    315:         break;
                    316:     case 4:
                    317:         /* Who is going to miss the MMU anyway...? :-)  */
                    318:         write_log ("040");
                    319:         break;
                    320:     default:
                    321:         write_log ("000");
                    322:         break;
                    323:     }
1.1.1.12  root      324:     if (currprefs.cpu_compatible)
1.1       root      325:         write_log (" (compatible mode)");
                    326:     write_log ("\n");
1.1.1.7   root      327: 
1.1       root      328:     read_table68k ();
                    329:     do_merges ();
                    330: 
1.1.1.10  root      331:     Log_Printf(LOG_DEBUG, "%d CPU functions\n", nr_cpuop_funcs);
1.1       root      332: 
                    333:     build_cpufunctbl ();
                    334: }
                    335: 
1.1.1.4   root      336: 
1.1.1.8   root      337: /* not used ATM:
1.1       root      338: static struct regstruct regs_backup[16];
                    339: static int backup_pointer = 0;
1.1.1.10  root      340: struct regstruct lastint_regs;
                    341: int lastint_no;
1.1.1.8   root      342: */
1.1.1.10  root      343: struct regstruct regs;
1.1       root      344: static long int m68kpc_offset;
1.1.1.10  root      345: 
1.1       root      346: 
                    347: #define get_ibyte_1(o) get_byte(regs.pc + (regs.pc_p - regs.pc_oldp) + (o) + 1)
                    348: #define get_iword_1(o) get_word(regs.pc + (regs.pc_p - regs.pc_oldp) + (o))
                    349: #define get_ilong_1(o) get_long(regs.pc + (regs.pc_p - regs.pc_oldp) + (o))
                    350: 
                    351: uae_s32 ShowEA (FILE *f, int reg, amodes mode, wordsizes size, char *buf)
                    352: {
                    353:     uae_u16 dp;
                    354:     uae_s8 disp8;
                    355:     uae_s16 disp16;
                    356:     int r;
                    357:     uae_u32 dispreg;
                    358:     uaecptr addr;
                    359:     uae_s32 offset = 0;
                    360:     char buffer[80];
                    361: 
                    362:     switch (mode){
                    363:      case Dreg:
                    364:        sprintf (buffer,"D%d", reg);
                    365:        break;
                    366:      case Areg:
                    367:        sprintf (buffer,"A%d", reg);
                    368:        break;
                    369:      case Aind:
                    370:        sprintf (buffer,"(A%d)", reg);
                    371:        break;
                    372:      case Aipi:
                    373:        sprintf (buffer,"(A%d)+", reg);
                    374:        break;
                    375:      case Apdi:
                    376:        sprintf (buffer,"-(A%d)", reg);
                    377:        break;
                    378:      case Ad16:
                    379:        disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    380:        addr = m68k_areg(regs,reg) + (uae_s16)disp16;
                    381:        sprintf (buffer,"(A%d,$%04x) == $%08lx", reg, disp16 & 0xffff,
                    382:                                        (unsigned long)addr);
                    383:        break;
                    384:      case Ad8r:
                    385:        dp = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    386:        disp8 = dp & 0xFF;
                    387:        r = (dp & 0x7000) >> 12;
                    388:        dispreg = dp & 0x8000 ? m68k_areg(regs,r) : m68k_dreg(regs,r);
                    389:        if (!(dp & 0x800)) dispreg = (uae_s32)(uae_s16)(dispreg);
                    390:        dispreg <<= (dp >> 9) & 3;
                    391: 
                    392:        if (dp & 0x100) {
                    393:            uae_s32 outer = 0, disp = 0;
                    394:            uae_s32 base = m68k_areg(regs,reg);
                    395:            char name[10];
                    396:            sprintf (name,"A%d, ",reg);
                    397:            if (dp & 0x80) { base = 0; name[0] = 0; }
                    398:            if (dp & 0x40) dispreg = 0;
                    399:            if ((dp & 0x30) == 0x20) { disp = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
                    400:            if ((dp & 0x30) == 0x30) { disp = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
                    401:            base += disp;
                    402: 
                    403:            if ((dp & 0x3) == 0x2) { outer = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
                    404:            if ((dp & 0x3) == 0x3) { outer = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
                    405: 
                    406:            if (!(dp & 4)) base += dispreg;
                    407:            if (dp & 3) base = get_long (base);
                    408:            if (dp & 4) base += dispreg;
                    409: 
                    410:            addr = base + outer;
                    411:            sprintf (buffer,"(%s%c%d.%c*%d+%ld)+%ld == $%08lx", name,
                    412:                    dp & 0x8000 ? 'A' : 'D', (int)r, dp & 0x800 ? 'L' : 'W',
                    413:                    1 << ((dp >> 9) & 3),
1.1.1.5   root      414:                    (long)disp, (long)outer, (unsigned long)addr);
1.1       root      415:        } else {
                    416:          addr = m68k_areg(regs,reg) + (uae_s32)((uae_s8)disp8) + dispreg;
                    417:          sprintf (buffer,"(A%d, %c%d.%c*%d, $%02x) == $%08lx", reg,
                    418:               dp & 0x8000 ? 'A' : 'D', (int)r, dp & 0x800 ? 'L' : 'W',
                    419:               1 << ((dp >> 9) & 3), disp8,
                    420:               (unsigned long)addr);
                    421:        }
                    422:        break;
                    423:      case PC16:
                    424:        addr = m68k_getpc () + m68kpc_offset;
                    425:        disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    426:        addr += (uae_s16)disp16;
                    427:        sprintf (buffer,"(PC,$%04x) == $%08lx", disp16 & 0xffff,(unsigned long)addr);
                    428:        break;
                    429:      case PC8r:
                    430:        addr = m68k_getpc () + m68kpc_offset;
                    431:        dp = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    432:        disp8 = dp & 0xFF;
                    433:        r = (dp & 0x7000) >> 12;
                    434:        dispreg = dp & 0x8000 ? m68k_areg(regs,r) : m68k_dreg(regs,r);
                    435:        if (!(dp & 0x800)) dispreg = (uae_s32)(uae_s16)(dispreg);
                    436:        dispreg <<= (dp >> 9) & 3;
                    437: 
                    438:        if (dp & 0x100) {
                    439:            uae_s32 outer = 0,disp = 0;
                    440:            uae_s32 base = addr;
                    441:            char name[10];
                    442:            sprintf (name,"PC, ");
                    443:            if (dp & 0x80) { base = 0; name[0] = 0; }
                    444:            if (dp & 0x40) dispreg = 0;
                    445:            if ((dp & 0x30) == 0x20) { disp = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
                    446:            if ((dp & 0x30) == 0x30) { disp = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
                    447:            base += disp;
                    448: 
                    449:            if ((dp & 0x3) == 0x2) { outer = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
                    450:            if ((dp & 0x3) == 0x3) { outer = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
                    451: 
                    452:            if (!(dp & 4)) base += dispreg;
                    453:            if (dp & 3) base = get_long (base);
                    454:            if (dp & 4) base += dispreg;
                    455: 
                    456:            addr = base + outer;
                    457:            sprintf (buffer,"(%s%c%d.%c*%d+%ld)+%ld == $%08lx", name,
                    458:                    dp & 0x8000 ? 'A' : 'D', (int)r, dp & 0x800 ? 'L' : 'W',
                    459:                    1 << ((dp >> 9) & 3),
1.1.1.5   root      460:                    (long)disp, (long)outer, (unsigned long)addr);
1.1       root      461:        } else {
                    462:          addr += (uae_s32)((uae_s8)disp8) + dispreg;
                    463:          sprintf (buffer,"(PC, %c%d.%c*%d, $%02x) == $%08lx", dp & 0x8000 ? 'A' : 'D',
                    464:                (int)r, dp & 0x800 ? 'L' : 'W',  1 << ((dp >> 9) & 3),
                    465:                disp8, (unsigned long)addr);
                    466:        }
                    467:        break;
                    468:      case absw:
                    469:        sprintf (buffer,"$%08lx", (unsigned long)(uae_s32)(uae_s16)get_iword_1 (m68kpc_offset));
                    470:        m68kpc_offset += 2;
                    471:        break;
                    472:      case absl:
                    473:        sprintf (buffer,"$%08lx", (unsigned long)get_ilong_1 (m68kpc_offset));
                    474:        m68kpc_offset += 4;
                    475:        break;
                    476:      case imm:
                    477:        switch (size){
                    478:         case sz_byte:
                    479:            sprintf (buffer,"#$%02x", (unsigned int)(get_iword_1 (m68kpc_offset) & 0xff));
                    480:            m68kpc_offset += 2;
                    481:            break;
                    482:         case sz_word:
                    483:            sprintf (buffer,"#$%04x", (unsigned int)(get_iword_1 (m68kpc_offset) & 0xffff));
                    484:            m68kpc_offset += 2;
                    485:            break;
                    486:         case sz_long:
                    487:            sprintf (buffer,"#$%08lx", (unsigned long)(get_ilong_1 (m68kpc_offset)));
                    488:            m68kpc_offset += 4;
                    489:            break;
                    490:         default:
                    491:            break;
                    492:        }
                    493:        break;
                    494:      case imm0:
                    495:        offset = (uae_s32)(uae_s8)get_iword_1 (m68kpc_offset);
                    496:        m68kpc_offset += 2;
                    497:        sprintf (buffer,"#$%02x", (unsigned int)(offset & 0xff));
                    498:        break;
                    499:      case imm1:
                    500:        offset = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset);
                    501:        m68kpc_offset += 2;
                    502:        sprintf (buffer,"#$%04x", (unsigned int)(offset & 0xffff));
                    503:        break;
                    504:      case imm2:
                    505:        offset = (uae_s32)get_ilong_1 (m68kpc_offset);
                    506:        m68kpc_offset += 4;
                    507:        sprintf (buffer,"#$%08lx", (unsigned long)offset);
                    508:        break;
                    509:      case immi:
                    510:        offset = (uae_s32)(uae_s8)(reg & 0xff);
                    511:        sprintf (buffer,"#$%08lx", (unsigned long)offset);
                    512:        break;
                    513:      default:
                    514:        break;
                    515:     }
                    516:     if (buf == 0)
                    517:        fprintf (f, "%s", buffer);
                    518:     else
                    519:        strcat (buf, buffer);
                    520:     return offset;
                    521: }
                    522: 
1.1.1.8   root      523: 
1.1       root      524: /* The plan is that this will take over the job of exception 3 handling -
                    525:  * the CPU emulation functions will just do a longjmp to m68k_go whenever
                    526:  * they hit an odd address. */
1.1.1.8   root      527: #if 0
1.1       root      528: static int verify_ea (int reg, amodes mode, wordsizes size, uae_u32 *val)
                    529: {
                    530:     uae_u16 dp;
                    531:     uae_s8 disp8;
                    532:     uae_s16 disp16;
                    533:     int r;
                    534:     uae_u32 dispreg;
                    535:     uaecptr addr;
1.1.1.5   root      536:     /*uae_s32 offset = 0;*/
1.1       root      537: 
                    538:     switch (mode){
                    539:      case Dreg:
                    540:        *val = m68k_dreg (regs, reg);
                    541:        return 1;
                    542:      case Areg:
                    543:        *val = m68k_areg (regs, reg);
                    544:        return 1;
                    545: 
                    546:      case Aind:
                    547:      case Aipi:
                    548:        addr = m68k_areg (regs, reg);
                    549:        break;
                    550:      case Apdi:
                    551:        addr = m68k_areg (regs, reg);
                    552:        break;
                    553:      case Ad16:
                    554:        disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    555:        addr = m68k_areg(regs,reg) + (uae_s16)disp16;
                    556:        break;
                    557:      case Ad8r:
                    558:        addr = m68k_areg (regs, reg);
                    559:      d8r_common:
                    560:        dp = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    561:        disp8 = dp & 0xFF;
                    562:        r = (dp & 0x7000) >> 12;
                    563:        dispreg = dp & 0x8000 ? m68k_areg(regs,r) : m68k_dreg(regs,r);
                    564:        if (!(dp & 0x800)) dispreg = (uae_s32)(uae_s16)(dispreg);
                    565:        dispreg <<= (dp >> 9) & 3;
                    566: 
                    567:        if (dp & 0x100) {
                    568:            uae_s32 outer = 0, disp = 0;
                    569:            uae_s32 base = addr;
                    570:            if (dp & 0x80) base = 0;
                    571:            if (dp & 0x40) dispreg = 0;
                    572:            if ((dp & 0x30) == 0x20) { disp = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
                    573:            if ((dp & 0x30) == 0x30) { disp = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
                    574:            base += disp;
                    575: 
                    576:            if ((dp & 0x3) == 0x2) { outer = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset); m68kpc_offset += 2; }
                    577:            if ((dp & 0x3) == 0x3) { outer = get_ilong_1 (m68kpc_offset); m68kpc_offset += 4; }
                    578: 
                    579:            if (!(dp & 4)) base += dispreg;
                    580:            if (dp & 3) base = get_long (base);
                    581:            if (dp & 4) base += dispreg;
                    582: 
                    583:            addr = base + outer;
                    584:        } else {
                    585:          addr += (uae_s32)((uae_s8)disp8) + dispreg;
                    586:        }
                    587:        break;
                    588:      case PC16:
                    589:        addr = m68k_getpc () + m68kpc_offset;
                    590:        disp16 = get_iword_1 (m68kpc_offset); m68kpc_offset += 2;
                    591:        addr += (uae_s16)disp16;
                    592:        break;
                    593:      case PC8r:
                    594:        addr = m68k_getpc () + m68kpc_offset;
                    595:        goto d8r_common;
                    596:      case absw:
                    597:        addr = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset);
                    598:        m68kpc_offset += 2;
                    599:        break;
                    600:      case absl:
                    601:        addr = get_ilong_1 (m68kpc_offset);
                    602:        m68kpc_offset += 4;
                    603:        break;
                    604:      case imm:
                    605:        switch (size){
                    606:         case sz_byte:
                    607:            *val = get_iword_1 (m68kpc_offset) & 0xff;
                    608:            m68kpc_offset += 2;
                    609:            break;
                    610:         case sz_word:
                    611:            *val = get_iword_1 (m68kpc_offset) & 0xffff;
                    612:            m68kpc_offset += 2;
                    613:            break;
                    614:         case sz_long:
                    615:            *val = get_ilong_1 (m68kpc_offset);
                    616:            m68kpc_offset += 4;
                    617:            break;
                    618:         default:
                    619:            break;
                    620:        }
                    621:        return 1;
                    622:      case imm0:
                    623:        *val = (uae_s32)(uae_s8)get_iword_1 (m68kpc_offset);
                    624:        m68kpc_offset += 2;
                    625:        return 1;
                    626:      case imm1:
                    627:        *val = (uae_s32)(uae_s16)get_iword_1 (m68kpc_offset);
                    628:        m68kpc_offset += 2;
                    629:        return 1;
                    630:      case imm2:
                    631:        *val = get_ilong_1 (m68kpc_offset);
                    632:        m68kpc_offset += 4;
                    633:        return 1;
                    634:      case immi:
                    635:        *val = (uae_s32)(uae_s8)(reg & 0xff);
                    636:        return 1;
                    637:      default:
                    638:        addr = 0;
                    639:        break;
                    640:     }
                    641:     if ((addr & 1) == 0)
                    642:        return 1;
                    643: 
                    644:     last_addr_for_exception_3 = m68k_getpc () + m68kpc_offset;
                    645:     last_fault_for_exception_3 = addr;
                    646:     return 0;
                    647: }
1.1.1.8   root      648: #endif
                    649: 
1.1       root      650: 
                    651: uae_u32 get_disp_ea_020 (uae_u32 base, uae_u32 dp)
                    652: {
                    653:     int reg = (dp >> 12) & 15;
                    654:     uae_s32 regd = regs.regs[reg];
                    655:     if ((dp & 0x800) == 0)
                    656:        regd = (uae_s32)(uae_s16)regd;
                    657:     regd <<= (dp >> 9) & 3;
                    658:     if (dp & 0x100) {
                    659:        uae_s32 outer = 0;
                    660:        if (dp & 0x80) base = 0;
                    661:        if (dp & 0x40) regd = 0;
                    662: 
                    663:        if ((dp & 0x30) == 0x20) base += (uae_s32)(uae_s16)next_iword();
                    664:        if ((dp & 0x30) == 0x30) base += next_ilong();
                    665: 
                    666:        if ((dp & 0x3) == 0x2) outer = (uae_s32)(uae_s16)next_iword();
                    667:        if ((dp & 0x3) == 0x3) outer = next_ilong();
                    668: 
                    669:        if ((dp & 0x4) == 0) base += regd;
                    670:        if (dp & 0x3) base = get_long (base);
                    671:        if (dp & 0x4) base += regd;
                    672: 
                    673:        return base + outer;
                    674:     } else {
                    675:        return base + (uae_s32)((uae_s8)dp) + regd;
                    676:     }
                    677: }
                    678: 
                    679: uae_u32 get_disp_ea_000 (uae_u32 base, uae_u32 dp)
                    680: {
                    681:     int reg = (dp >> 12) & 15;
                    682:     uae_s32 regd = regs.regs[reg];
                    683: #if 1
                    684:     if ((dp & 0x800) == 0)
                    685:        regd = (uae_s32)(uae_s16)regd;
                    686:     return base + (uae_s8)dp + regd;
                    687: #else
                    688:     /* Branch-free code... benchmark this again now that
                    689:      * things are no longer inline.  */
                    690:     uae_s32 regd16;
                    691:     uae_u32 mask;
                    692:     mask = ((dp & 0x800) >> 11) - 1;
                    693:     regd16 = (uae_s32)(uae_s16)regd;
                    694:     regd16 &= mask;
                    695:     mask = ~mask;
                    696:     base += (uae_s8)dp;
                    697:     regd &= mask;
                    698:     regd |= regd16;
                    699:     return base + regd;
                    700: #endif
                    701: }
                    702: 
1.1.1.8   root      703: 
                    704: /* Create the Status Register from the flags */
1.1       root      705: void MakeSR (void)
                    706: {
                    707: #if 0
                    708:     assert((regs.t1 & 1) == regs.t1);
                    709:     assert((regs.t0 & 1) == regs.t0);
                    710:     assert((regs.s & 1) == regs.s);
                    711:     assert((regs.m & 1) == regs.m);
                    712:     assert((XFLG & 1) == XFLG);
                    713:     assert((NFLG & 1) == NFLG);
                    714:     assert((ZFLG & 1) == ZFLG);
                    715:     assert((VFLG & 1) == VFLG);
                    716:     assert((CFLG & 1) == CFLG);
                    717: #endif
                    718:     regs.sr = ((regs.t1 << 15) | (regs.t0 << 14)
                    719:               | (regs.s << 13) | (regs.m << 12) | (regs.intmask << 8)
                    720:               | (GET_XFLG << 4) | (GET_NFLG << 3) | (GET_ZFLG << 2) | (GET_VFLG << 1)
                    721:               | GET_CFLG);
                    722: }
                    723: 
1.1.1.8   root      724: 
                    725: /* Set up the flags from Status Register */
1.1       root      726: void MakeFromSR (void)
                    727: {
                    728:     int oldm = regs.m;
                    729:     int olds = regs.s;
                    730: 
                    731:     regs.t1 = (regs.sr >> 15) & 1;
                    732:     regs.t0 = (regs.sr >> 14) & 1;
                    733:     regs.s = (regs.sr >> 13) & 1;
                    734:     regs.m = (regs.sr >> 12) & 1;
                    735:     regs.intmask = (regs.sr >> 8) & 7;
                    736:     SET_XFLG ((regs.sr >> 4) & 1);
                    737:     SET_NFLG ((regs.sr >> 3) & 1);
                    738:     SET_ZFLG ((regs.sr >> 2) & 1);
                    739:     SET_VFLG ((regs.sr >> 1) & 1);
                    740:     SET_CFLG (regs.sr & 1);
1.1.1.12  root      741:     if (currprefs.cpu_level >= 2) {
1.1       root      742:        if (olds != regs.s) {
                    743:            if (olds) {
                    744:                if (oldm)
                    745:                    regs.msp = m68k_areg(regs, 7);
                    746:                else
                    747:                    regs.isp = m68k_areg(regs, 7);
                    748:                m68k_areg(regs, 7) = regs.usp;
                    749:            } else {
                    750:                regs.usp = m68k_areg(regs, 7);
                    751:                m68k_areg(regs, 7) = regs.m ? regs.msp : regs.isp;
                    752:            }
                    753:        } else if (olds && oldm != regs.m) {
                    754:            if (oldm) {
                    755:                regs.msp = m68k_areg(regs, 7);
                    756:                m68k_areg(regs, 7) = regs.isp;
                    757:            } else {
                    758:                regs.isp = m68k_areg(regs, 7);
                    759:                m68k_areg(regs, 7) = regs.msp;
                    760:            }
                    761:        }
                    762:     } else {
1.1.1.12  root      763:        /* [NP] If cpu < 68020, m and t0 are ignored and should be set to 0 */
                    764:        regs.t0 = 0;
                    765:        regs.m = 0;
                    766: 
1.1       root      767:        if (olds != regs.s) {
                    768:            if (olds) {
                    769:                regs.isp = m68k_areg(regs, 7);
                    770:                m68k_areg(regs, 7) = regs.usp;
                    771:            } else {
                    772:                regs.usp = m68k_areg(regs, 7);
                    773:                m68k_areg(regs, 7) = regs.isp;
                    774:            }
                    775:        }
                    776:     }
                    777: 
1.1.1.8   root      778:     /* Pending interrupts can occur again after a write to the SR: */
                    779:     set_special (SPCFLAG_DOINT);
1.1       root      780:     if (regs.t1 || regs.t0)
                    781:        set_special (SPCFLAG_TRACE);
                    782:     else
1.1.1.6   root      783:        /* Keep SPCFLAG_DOTRACE, we still want a trace exception for
                    784:           SR-modifying instructions (including STOP).  */
                    785:        unset_special (SPCFLAG_TRACE);
1.1       root      786: }
                    787: 
1.1.1.5   root      788: 
1.1.1.12  root      789: static void exception_trace (int nr)
                    790: {
                    791:     unset_special (SPCFLAG_TRACE | SPCFLAG_DOTRACE);           
                    792:     if (regs.t1 && !regs.t0) {
                    793:         /* trace stays pending if exception is div by zero, chk,
                    794:          * trapv or trap #x
                    795:          */
                    796:         if (nr == 5 || nr == 6 || nr == 7 || (nr >= 32 && nr <= 47))
                    797:             set_special (SPCFLAG_DOTRACE);
                    798:     }
                    799:     regs.t1 = regs.t0 = regs.m = 0;
                    800: }
                    801: 
                    802: 
1.1.1.14! root      803: /*
        !           804:  * Compute the number of jitter cycles to add when a video interrupt occurs
        !           805:  * (this is specific to the Atari ST)
        !           806:  */
        !           807: static void InterruptAddJitter (int Level , int Pending)
        !           808: {
        !           809:     int cycles = 0;
        !           810: 
        !           811:     if ( Level == 2 )                          /* HBL */
        !           812:       {
        !           813:         if ( Pending )
        !           814:          cycles = HblJitterArrayPending[ HblJitterIndex ];
        !           815:        else
        !           816:          cycles = HblJitterArray[ HblJitterIndex ];
        !           817:       }
        !           818:     
        !           819:     else if ( Level == 4 )                     /* VBL */
        !           820:       {
        !           821:         if ( Pending )
        !           822:          cycles = VblJitterArrayPending[ VblJitterIndex ];
        !           823:        else
        !           824:          cycles = VblJitterArray[ VblJitterIndex ];
        !           825:       }
        !           826: 
        !           827: //fprintf ( stderr , "jitter %d\n" , cycles );
        !           828: //cycles=0;
        !           829:     if ( cycles > 0 )                          /* no need to call M68000_AddCycles if cycles == 0 */
        !           830:       M68000_AddCycles ( cycles );
        !           831: }
        !           832: 
        !           833: 
1.1.1.13  root      834: /* Handle exceptions. We need a special case to handle MFP exceptions */
                    835: /* on Atari ST, because it's possible to change the MFP's vector base */
                    836: /* and get a conflict with 'normal' cpu exceptions. */
                    837: void Exception(int nr, uaecptr oldpc, int ExceptionSource)
1.1       root      838: {
                    839:     uae_u32 currpc = m68k_getpc ();
                    840: 
1.1.1.2   root      841:     /*if( nr>=2 && nr<10 )  fprintf(stderr,"Exception (-> %i bombs)!\n",nr);*/
1.1       root      842: 
1.1.1.7   root      843:     /* Intercept VDI exception (Trap #2 with D0 = 0x73) */
1.1.1.13  root      844:     if ( ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP )
1.1.1.5   root      845:       {
1.1.1.13  root      846:         if(bUseVDIRes && nr == 0x22 && regs.regs[0] == 0x73)
                    847:         {
                    848:           if(!VDI())
                    849:           {
                    850:             /* Set 'PC' as address of 'VDI_OPCODE' illegal instruction
                    851:              * This will call OpCode_VDI after completion of Trap call!
                    852:              * Use to modify return structure from VDI */
                    853:             VDI_OldPC = currpc;
                    854:             currpc = CART_VDI_OPCODE_ADDR;
                    855:           }
                    856:         }
                    857:     
                    858:         if (bBiosIntercept)
                    859:         {
                    860:           /* Intercept BIOS or XBIOS trap (Trap #13 or #14) */
                    861:           if (nr == 0x2d)
                    862:           {
                    863:             /* Intercept BIOS calls */
                    864:             if (Bios())  return;
                    865:           }
                    866:           else if (nr == 0x2e)
                    867:           {
                    868:             /* Intercept XBIOS calls */
                    869:             if (XBios())  return;
                    870:           }
                    871:         }
1.1.1.5   root      872:       }
1.1.1.8   root      873: 
1.1       root      874:     MakeSR();
                    875: 
1.1.1.8   root      876:     /* Change to supervisor mode if necessary */
1.1       root      877:     if (!regs.s) {
                    878:        regs.usp = m68k_areg(regs, 7);
1.1.1.12  root      879:        if (currprefs.cpu_level >= 2)
1.1       root      880:            m68k_areg(regs, 7) = regs.m ? regs.msp : regs.isp;
                    881:        else
                    882:            m68k_areg(regs, 7) = regs.isp;
                    883:        regs.s = 1;
                    884:     }
1.1.1.8   root      885: 
                    886:     /* Build additional exception stack frame for 68010 and higher */
1.1.1.13  root      887:     /* (special case for MFP) */
1.1.1.12  root      888:     if (currprefs.cpu_level > 0) {
1.1.1.13  root      889:         if (ExceptionSource == M68000_EXCEPTION_SRC_INT_MFP) {
                    890:            m68k_areg(regs, 7) -= 2;
                    891:            put_word (m68k_areg(regs, 7), nr * 4);      /* MFP interrupt, 'nr' can be in a different range depending on $fffa17 */
                    892:         }
                    893:         else if (nr == 2 || nr == 3) {
1.1       root      894:            int i;
                    895:            /* @@@ this is probably wrong (?) */
                    896:            for (i = 0 ; i < 12 ; i++) {
                    897:                m68k_areg(regs, 7) -= 2;
                    898:                put_word (m68k_areg(regs, 7), 0);
                    899:            }
                    900:            m68k_areg(regs, 7) -= 2;
                    901:            put_word (m68k_areg(regs, 7), 0xa000 + nr * 4);
                    902:        } else if (nr ==5 || nr == 6 || nr == 7 || nr == 9) {
                    903:            m68k_areg(regs, 7) -= 4;
                    904:            put_long (m68k_areg(regs, 7), oldpc);
                    905:            m68k_areg(regs, 7) -= 2;
                    906:            put_word (m68k_areg(regs, 7), 0x2000 + nr * 4);
                    907:        } else if (regs.m && nr >= 24 && nr < 32) {
                    908:            m68k_areg(regs, 7) -= 2;
                    909:            put_word (m68k_areg(regs, 7), nr * 4);
                    910:            m68k_areg(regs, 7) -= 4;
                    911:            put_long (m68k_areg(regs, 7), currpc);
                    912:            m68k_areg(regs, 7) -= 2;
                    913:            put_word (m68k_areg(regs, 7), regs.sr);
                    914:            regs.sr |= (1 << 13);
                    915:            regs.msp = m68k_areg(regs, 7);
                    916:            m68k_areg(regs, 7) = regs.isp;
                    917:            m68k_areg(regs, 7) -= 2;
                    918:            put_word (m68k_areg(regs, 7), 0x1000 + nr * 4);
                    919:        } else {
                    920:            m68k_areg(regs, 7) -= 2;
                    921:            put_word (m68k_areg(regs, 7), nr * 4);
                    922:        }
                    923:     }
1.1.1.3   root      924: 
                    925:     /* Push PC on stack: */
1.1       root      926:     m68k_areg(regs, 7) -= 4;
                    927:     put_long (m68k_areg(regs, 7), currpc);
1.1.1.3   root      928:     /* Push SR on stack: */
1.1       root      929:     m68k_areg(regs, 7) -= 2;
                    930:     put_word (m68k_areg(regs, 7), regs.sr);
1.1.1.3   root      931: 
1.1.1.13  root      932:     HATARI_TRACE ( HATARI_TRACE_CPU_EXCEPTION , "cpu exception %d currpc %x buspc %x newpc %x fault_e3 %x op_e3 %hx addr_e3 %x\n" ,
                    933:        nr, currpc, BusErrorPC, get_long (regs.vbr + 4*nr), last_fault_for_exception_3, last_op_for_exception_3, last_addr_for_exception_3 );
1.1.1.12  root      934: 
1.1.1.3   root      935:     /* 68000 bus/address errors: */
1.1.1.13  root      936:     if (currprefs.cpu_level==0 && (nr==2 || nr==3) && (ExceptionSource != M68000_EXCEPTION_SRC_INT_MFP) ) {
1.1.1.12  root      937:        uae_u16 specialstatus = 1;
                    938: 
1.1.1.8   root      939:        /* Special status word emulation isn't perfect yet... :-( */
                    940:        if (regs.sr & 0x2000)
                    941:            specialstatus |= 0x4;
1.1.1.3   root      942:        m68k_areg(regs, 7) -= 8;
                    943:        if (nr == 3) {    /* Address error */
1.1.1.12  root      944:            specialstatus |= ( last_op_for_exception_3 & (~0x1f) );     /* [NP] unused bits of specialstatus are those of the last opcode ! */
1.1.1.8   root      945:            put_word (m68k_areg(regs, 7), specialstatus);
1.1.1.3   root      946:            put_long (m68k_areg(regs, 7)+2, last_fault_for_exception_3);
                    947:            put_word (m68k_areg(regs, 7)+6, last_op_for_exception_3);
                    948:            put_long (m68k_areg(regs, 7)+10, last_addr_for_exception_3);
1.1.1.8   root      949:            if (bEnableDebug) {
                    950:              fprintf(stderr,"Address Error at address $%x, PC=$%x\n",last_fault_for_exception_3,currpc);
                    951:              DebugUI();
                    952:            }
1.1.1.3   root      953:        }
1.1.1.8   root      954:        else {    /* Bus error */
1.1.1.12  root      955:            specialstatus |= ( get_word(BusErrorPC) & (~0x1f) );        /* [NP] unused bits of special status are those of the last opcode ! */
1.1.1.8   root      956:            if (bBusErrorReadWrite)
                    957:              specialstatus |= 0x10;
                    958:            put_word (m68k_areg(regs, 7), specialstatus);
1.1.1.10  root      959:            put_long (m68k_areg(regs, 7)+2, BusErrorAddress);
1.1.1.12  root      960:            put_word (m68k_areg(regs, 7)+6, get_word(BusErrorPC));      /* Opcode */
                    961: 
                    962:            /* [NP] PC stored in the stack frame is not necessarily pointing to the next instruction ! */
                    963:            /* FIXME : we should have a proper model for this, in the meantime we handle specific cases */
                    964:            if ( get_word(BusErrorPC) == 0x21f8 )                       /* move.l $0.w,$24.w (Transbeauce 2 loader) */ 
                    965:              put_long (m68k_areg(regs, 7)+10, currpc-2);               /* correct PC is 2 bytes less than usual value */
1.1.1.8   root      966:            /* Check for double bus errors: */
                    967:            if (regs.spcflags & SPCFLAG_BUSERROR) {
                    968:              fprintf(stderr, "Detected double bus error at address $%x, PC=$%lx => CPU halted!\n",
1.1.1.10  root      969:                      BusErrorAddress, (long)currpc);
1.1.1.8   root      970:              unset_special(SPCFLAG_BUSERROR);
                    971:              if (bEnableDebug)
                    972:                DebugUI();
                    973:              regs.intmask = 7;
                    974:              m68k_setstopped(TRUE);
                    975:              return;
                    976:            }
1.1.1.10  root      977:            if (bEnableDebug && BusErrorAddress!=0xff8a00) {
                    978:              fprintf(stderr,"Bus Error at address $%x, PC=$%lx\n", BusErrorAddress, (long)currpc);
1.1.1.8   root      979:              DebugUI();
                    980:            }
                    981:        }
1.1.1.3   root      982:     }
                    983: 
1.1.1.8   root      984:     /* Set PC and flags */
                    985:     if (bEnableDebug && get_long (regs.vbr + 4*nr) == 0) {
                    986:         write_log("Uninitialized exception handler #%i!\n", nr);
1.1.1.13  root      987:        DebugUI();
1.1.1.8   root      988:     }
1.1       root      989:     m68k_setpc (get_long (regs.vbr + 4*nr));
                    990:     fill_prefetch_0 ();
1.1.1.12  root      991:     /* Handle trace flags depending on current state */
                    992:     exception_trace (nr);
1.1.1.6   root      993: 
1.1.1.13  root      994:     /* Handle exception cycles (special case for MFP) */
                    995:     if ( ExceptionSource == M68000_EXCEPTION_SRC_INT_MFP ) 
                    996:     {
                    997:       M68000_AddCycles(44+12);                 /* MFP interrupt, 'nr' can be in a different range depending on $fffa17 */
                    998:     }
                    999:     else if (nr >= 24 && nr <= 31)
1.1.1.7   root     1000:     {
1.1.1.12  root     1001:       if ( ( nr == 26 ) || ( nr == 28 ) )      /* HBL or VBL */
                   1002:         M68000_AddCycles(44+12);               /* Video Interrupt */
                   1003:       else
                   1004:         M68000_AddCycles(44+4);                        /* Other Interrupts */
1.1.1.7   root     1005:     }
                   1006:     else if(nr >= 32 && nr <= 47)
                   1007:     {
1.1.1.13  root     1008:       M68000_AddCycles(34-4);                  /* Trap (total is 34, but cpuemu.c already adds 4) */
1.1.1.7   root     1009:     }
                   1010:     else switch(nr)
                   1011:     {
1.1.1.12  root     1012:       case 2: M68000_AddCycles(50); break;     /* Bus error */
                   1013:       case 3: M68000_AddCycles(50); break;     /* Address error */
                   1014:       case 4: M68000_AddCycles(34); break;     /* Illegal instruction */
                   1015:       case 5: M68000_AddCycles(38); break;     /* Div by zero */
                   1016:       case 6: M68000_AddCycles(40); break;     /* CHK */
                   1017:       case 7: M68000_AddCycles(34); break;     /* TRAPV */
                   1018:       case 8: M68000_AddCycles(34); break;     /* Privilege violation */
                   1019:       case 9: M68000_AddCycles(34); break;     /* Trace */
                   1020:       case 10: M68000_AddCycles(34); break;    /* Line-A - probably wrong */
                   1021:       case 11: M68000_AddCycles(34); break;    /* Line-F - probably wrong */
1.1.1.7   root     1022:       default:
1.1.1.8   root     1023:         /* FIXME: Add right cycles value for MFP interrupts and copro exceptions ... */
1.1.1.7   root     1024:         if(nr < 64)
1.1.1.12  root     1025:           M68000_AddCycles(4);                 /* Coprocessor and unassigned exceptions (???) */
1.1.1.7   root     1026:         else
1.1.1.13  root     1027:           M68000_AddCycles(44+12);             /* Must be a MFP interrupt, should be processed above */
1.1.1.7   root     1028:         break;
1.1.1.6   root     1029:     }
1.1.1.14! root     1030: 
1.1       root     1031: }
                   1032: 
1.1.1.7   root     1033: 
1.1.1.14! root     1034: static void Interrupt(int nr , int Pending)
1.1       root     1035: {
                   1036:     assert(nr < 8 && nr >= 0);
1.1.1.10  root     1037:     /*lastint_regs = regs;*/
                   1038:     /*lastint_no = nr;*/
1.1.1.13  root     1039: 
                   1040:     /* [NP] On Hatari, only video ints are using SPCFLAG_INT (see m68000.c) */
                   1041:     /* TODO : to be really precise, we should use a global variable to store the last ExceptionSource */
                   1042:     /* passed to M68000_Exception, instead of hardcoding M68000_EXCEPTION_SRC_INT_VIDEO here */
                   1043:     Exception(nr+24, 0, M68000_EXCEPTION_SRC_INT_VIDEO);
1.1       root     1044: 
                   1045:     regs.intmask = nr;
                   1046:     set_special (SPCFLAG_INT);
1.1.1.14! root     1047: 
        !          1048:     /* Handle Atari ST's specific jitter for hbl/vbl */
        !          1049:     InterruptAddJitter ( nr , Pending );
1.1       root     1050: }
                   1051: 
1.1.1.7   root     1052: 
1.1.1.12  root     1053: uae_u32 caar, cacr;
1.1.1.8   root     1054: static uae_u32 itt0, itt1, dtt0, dtt1, tc, mmusr, urp, srp;
1.1       root     1055: 
1.1.1.7   root     1056: 
1.1.1.12  root     1057: static int movec_illg (int regno)
                   1058: {
                   1059:     int regno2 = regno & 0x7ff;
                   1060:     if (currprefs.cpu_level == 1) { /* 68010 */
                   1061:        if (regno2 < 2)
                   1062:            return 0;
                   1063:        return 1;
                   1064:     }
                   1065:     if (currprefs.cpu_level == 2 || currprefs.cpu_level == 3) { /* 68020 */
                   1066:        if (regno == 3) return 1; /* 68040 only */
                   1067:         /* 4 is >=68040, but 0x804 is in 68020 */
                   1068:         if (regno2 < 4 || regno == 0x804)
                   1069:            return 0;
                   1070:        return 1;
                   1071:     }
                   1072:     if (currprefs.cpu_level >= 4) { /* 68040 */
                   1073:        if (regno == 0x802) return 1; /* 68020 only */
                   1074:        if (regno2 < 8) return 0;
                   1075:        if (currprefs.cpu_level == 6 && regno2 == 8) /* 68060 only */
                   1076:            return 0;
                   1077:        return 1;
                   1078:     }
                   1079:     return 1;
                   1080: }
                   1081: 
1.1       root     1082: int m68k_move2c (int regno, uae_u32 *regp)
                   1083: {
1.1.1.12  root     1084:     if (movec_illg (regno)) {
1.1       root     1085:        op_illg (0x4E7B);
                   1086:        return 0;
                   1087:     } else {
                   1088:        switch (regno) {
                   1089:        case 0: regs.sfc = *regp & 7; break;
                   1090:        case 1: regs.dfc = *regp & 7; break;
1.1.1.12  root     1091:        case 2: cacr = *regp & (currprefs.cpu_level < 4 ? 0x3 : 0x80008000); break;
1.1       root     1092:        case 3: tc = *regp & 0xc000; break;
                   1093:          /* Mask out fields that should be zero.  */
                   1094:        case 4: itt0 = *regp & 0xffffe364; break;
                   1095:        case 5: itt1 = *regp & 0xffffe364; break;
                   1096:        case 6: dtt0 = *regp & 0xffffe364; break;
                   1097:        case 7: dtt1 = *regp & 0xffffe364; break;
1.1.1.7   root     1098: 
1.1       root     1099:        case 0x800: regs.usp = *regp; break;
                   1100:        case 0x801: regs.vbr = *regp; break;
1.1.1.12  root     1101:        case 0x802: caar = *regp & 0xfc; break;
1.1       root     1102:        case 0x803: regs.msp = *regp; if (regs.m == 1) m68k_areg(regs, 7) = regs.msp; break;
                   1103:        case 0x804: regs.isp = *regp; if (regs.m == 0) m68k_areg(regs, 7) = regs.isp; break;
1.1.1.6   root     1104:        case 0x805: mmusr = *regp; break;
                   1105:        case 0x806: urp = *regp; break;
                   1106:        case 0x807: srp = *regp; break;
1.1       root     1107:        default:
                   1108:            op_illg (0x4E7B);
                   1109:            return 0;
                   1110:        }
                   1111:     }
                   1112:     return 1;
                   1113: }
                   1114: 
                   1115: int m68k_movec2 (int regno, uae_u32 *regp)
                   1116: {
1.1.1.12  root     1117:     if (movec_illg (regno)) {
1.1       root     1118:        op_illg (0x4E7A);
                   1119:        return 0;
                   1120:     } else {
                   1121:        switch (regno) {
                   1122:        case 0: *regp = regs.sfc; break;
                   1123:        case 1: *regp = regs.dfc; break;
1.1.1.12  root     1124:        case 2: *regp = cacr; break;
1.1       root     1125:        case 3: *regp = tc; break;
                   1126:        case 4: *regp = itt0; break;
                   1127:        case 5: *regp = itt1; break;
                   1128:        case 6: *regp = dtt0; break;
                   1129:        case 7: *regp = dtt1; break;
                   1130:        case 0x800: *regp = regs.usp; break;
                   1131:        case 0x801: *regp = regs.vbr; break;
1.1.1.12  root     1132:        case 0x802: *regp = caar; break;
1.1       root     1133:        case 0x803: *regp = regs.m == 1 ? m68k_areg(regs, 7) : regs.msp; break;
                   1134:        case 0x804: *regp = regs.m == 0 ? m68k_areg(regs, 7) : regs.isp; break;
                   1135:        case 0x805: *regp = mmusr; break;
1.1.1.6   root     1136:        case 0x806: *regp = urp; break;
                   1137:        case 0x807: *regp = srp; break;
1.1       root     1138:        default:
                   1139:            op_illg (0x4E7A);
                   1140:            return 0;
                   1141:        }
                   1142:     }
                   1143:     return 1;
                   1144: }
                   1145: 
                   1146: STATIC_INLINE int
1.1.1.10  root     1147: div_unsigned(uae_u32 src_hi, uae_u32 src_lo, uae_u32 ndiv, uae_u32 *quot, uae_u32 *rem)
1.1       root     1148: {
                   1149:        uae_u32 q = 0, cbit = 0;
                   1150:        int i;
                   1151: 
1.1.1.10  root     1152:        if (ndiv <= src_hi) {
1.1       root     1153:            return 1;
                   1154:        }
                   1155:        for (i = 0 ; i < 32 ; i++) {
                   1156:                cbit = src_hi & 0x80000000ul;
                   1157:                src_hi <<= 1;
                   1158:                if (src_lo & 0x80000000ul) src_hi++;
                   1159:                src_lo <<= 1;
                   1160:                q = q << 1;
1.1.1.10  root     1161:                if (cbit || ndiv <= src_hi) {
1.1       root     1162:                        q |= 1;
1.1.1.10  root     1163:                        src_hi -= ndiv;
1.1       root     1164:                }
                   1165:        }
                   1166:        *quot = q;
                   1167:        *rem = src_hi;
                   1168:        return 0;
                   1169: }
                   1170: 
                   1171: void m68k_divl (uae_u32 opcode, uae_u32 src, uae_u16 extra, uaecptr oldpc)
                   1172: {
                   1173: #if defined(uae_s64)
                   1174:     if (src == 0) {
1.1.1.13  root     1175:        Exception (5, oldpc,M68000_EXCEPTION_SRC_CPU);
1.1       root     1176:        return;
                   1177:     }
                   1178:     if (extra & 0x800) {
                   1179:        /* signed variant */
                   1180:        uae_s64 a = (uae_s64)(uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
                   1181:        uae_s64 quot, rem;
                   1182: 
                   1183:        if (extra & 0x400) {
                   1184:            a &= 0xffffffffu;
                   1185:            a |= (uae_s64)m68k_dreg(regs, extra & 7) << 32;
                   1186:        }
                   1187:        rem = a % (uae_s64)(uae_s32)src;
                   1188:        quot = a / (uae_s64)(uae_s32)src;
                   1189:        if ((quot & UVAL64(0xffffffff80000000)) != 0
                   1190:            && (quot & UVAL64(0xffffffff80000000)) != UVAL64(0xffffffff80000000))
                   1191:        {
                   1192:            SET_VFLG (1);
                   1193:            SET_NFLG (1);
                   1194:            SET_CFLG (0);
                   1195:        } else {
                   1196:            if (((uae_s32)rem < 0) != ((uae_s64)a < 0)) rem = -rem;
                   1197:            SET_VFLG (0);
                   1198:            SET_CFLG (0);
                   1199:            SET_ZFLG (((uae_s32)quot) == 0);
                   1200:            SET_NFLG (((uae_s32)quot) < 0);
                   1201:            m68k_dreg(regs, extra & 7) = rem;
                   1202:            m68k_dreg(regs, (extra >> 12) & 7) = quot;
                   1203:        }
                   1204:     } else {
                   1205:        /* unsigned */
                   1206:        uae_u64 a = (uae_u64)(uae_u32)m68k_dreg(regs, (extra >> 12) & 7);
                   1207:        uae_u64 quot, rem;
                   1208: 
                   1209:        if (extra & 0x400) {
                   1210:            a &= 0xffffffffu;
                   1211:            a |= (uae_u64)m68k_dreg(regs, extra & 7) << 32;
                   1212:        }
                   1213:        rem = a % (uae_u64)src;
                   1214:        quot = a / (uae_u64)src;
                   1215:        if (quot > 0xffffffffu) {
                   1216:            SET_VFLG (1);
                   1217:            SET_NFLG (1);
                   1218:            SET_CFLG (0);
                   1219:        } else {
                   1220:            SET_VFLG (0);
                   1221:            SET_CFLG (0);
                   1222:            SET_ZFLG (((uae_s32)quot) == 0);
                   1223:            SET_NFLG (((uae_s32)quot) < 0);
                   1224:            m68k_dreg(regs, extra & 7) = rem;
                   1225:            m68k_dreg(regs, (extra >> 12) & 7) = quot;
                   1226:        }
                   1227:     }
                   1228: #else
                   1229:     if (src == 0) {
1.1.1.13  root     1230:        Exception (5, oldpc,M68000_EXCEPTION_SRC_CPU);
1.1       root     1231:        return;
                   1232:     }
                   1233:     if (extra & 0x800) {
                   1234:        /* signed variant */
                   1235:        uae_s32 lo = (uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
                   1236:        uae_s32 hi = lo < 0 ? -1 : 0;
                   1237:        uae_s32 save_high;
                   1238:        uae_u32 quot, rem;
                   1239:        uae_u32 sign;
                   1240: 
                   1241:        if (extra & 0x400) {
                   1242:            hi = (uae_s32)m68k_dreg(regs, extra & 7);
                   1243:        }
                   1244:        save_high = hi;
                   1245:        sign = (hi ^ src);
                   1246:        if (hi < 0) {
                   1247:            hi = ~hi;
                   1248:            lo = -lo;
                   1249:            if (lo == 0) hi++;
                   1250:        }
                   1251:        if ((uae_s32)src < 0) src = -src;
                   1252:        if (div_unsigned(hi, lo, src, &quot, &rem) ||
                   1253:            (sign & 0x80000000) ? quot > 0x80000000 : quot > 0x7fffffff) {
                   1254:            SET_VFLG (1);
                   1255:            SET_NFLG (1);
                   1256:            SET_CFLG (0);
                   1257:        } else {
                   1258:            if (sign & 0x80000000) quot = -quot;
                   1259:            if (((uae_s32)rem < 0) != (save_high < 0)) rem = -rem;
                   1260:            SET_VFLG (0);
                   1261:            SET_CFLG (0);
                   1262:            SET_ZFLG (((uae_s32)quot) == 0);
                   1263:            SET_NFLG (((uae_s32)quot) < 0);
                   1264:            m68k_dreg(regs, extra & 7) = rem;
                   1265:            m68k_dreg(regs, (extra >> 12) & 7) = quot;
                   1266:        }
                   1267:     } else {
                   1268:        /* unsigned */
                   1269:        uae_u32 lo = (uae_u32)m68k_dreg(regs, (extra >> 12) & 7);
                   1270:        uae_u32 hi = 0;
                   1271:        uae_u32 quot, rem;
                   1272: 
                   1273:        if (extra & 0x400) {
                   1274:            hi = (uae_u32)m68k_dreg(regs, extra & 7);
                   1275:        }
                   1276:        if (div_unsigned(hi, lo, src, &quot, &rem)) {
                   1277:            SET_VFLG (1);
                   1278:            SET_NFLG (1);
                   1279:            SET_CFLG (0);
                   1280:        } else {
                   1281:            SET_VFLG (0);
                   1282:            SET_CFLG (0);
                   1283:            SET_ZFLG (((uae_s32)quot) == 0);
                   1284:            SET_NFLG (((uae_s32)quot) < 0);
                   1285:            m68k_dreg(regs, extra & 7) = rem;
                   1286:            m68k_dreg(regs, (extra >> 12) & 7) = quot;
                   1287:        }
                   1288:     }
                   1289: #endif
                   1290: }
                   1291: 
                   1292: STATIC_INLINE void
                   1293: mul_unsigned(uae_u32 src1, uae_u32 src2, uae_u32 *dst_hi, uae_u32 *dst_lo)
                   1294: {
                   1295:        uae_u32 r0 = (src1 & 0xffff) * (src2 & 0xffff);
                   1296:        uae_u32 r1 = ((src1 >> 16) & 0xffff) * (src2 & 0xffff);
                   1297:        uae_u32 r2 = (src1 & 0xffff) * ((src2 >> 16) & 0xffff);
                   1298:        uae_u32 r3 = ((src1 >> 16) & 0xffff) * ((src2 >> 16) & 0xffff);
                   1299:        uae_u32 lo;
                   1300: 
                   1301:        lo = r0 + ((r1 << 16) & 0xffff0000ul);
                   1302:        if (lo < r0) r3++;
                   1303:        r0 = lo;
                   1304:        lo = r0 + ((r2 << 16) & 0xffff0000ul);
                   1305:        if (lo < r0) r3++;
                   1306:        r3 += ((r1 >> 16) & 0xffff) + ((r2 >> 16) & 0xffff);
                   1307:        *dst_lo = lo;
                   1308:        *dst_hi = r3;
                   1309: }
                   1310: 
                   1311: void m68k_mull (uae_u32 opcode, uae_u32 src, uae_u16 extra)
                   1312: {
                   1313: #if defined(uae_s64)
                   1314:     if (extra & 0x800) {
                   1315:        /* signed variant */
                   1316:        uae_s64 a = (uae_s64)(uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
                   1317: 
                   1318:        a *= (uae_s64)(uae_s32)src;
                   1319:        SET_VFLG (0);
                   1320:        SET_CFLG (0);
                   1321:        SET_ZFLG (a == 0);
                   1322:        SET_NFLG (a < 0);
                   1323:        if (extra & 0x400)
                   1324:            m68k_dreg(regs, extra & 7) = a >> 32;
                   1325:        else if ((a & UVAL64(0xffffffff80000000)) != 0
                   1326:                 && (a & UVAL64(0xffffffff80000000)) != UVAL64(0xffffffff80000000))
                   1327:        {
                   1328:            SET_VFLG (1);
                   1329:        }
                   1330:        m68k_dreg(regs, (extra >> 12) & 7) = (uae_u32)a;
                   1331:     } else {
                   1332:        /* unsigned */
                   1333:        uae_u64 a = (uae_u64)(uae_u32)m68k_dreg(regs, (extra >> 12) & 7);
                   1334: 
                   1335:        a *= (uae_u64)src;
                   1336:        SET_VFLG (0);
                   1337:        SET_CFLG (0);
                   1338:        SET_ZFLG (a == 0);
                   1339:        SET_NFLG (((uae_s64)a) < 0);
                   1340:        if (extra & 0x400)
                   1341:            m68k_dreg(regs, extra & 7) = a >> 32;
                   1342:        else if ((a & UVAL64(0xffffffff00000000)) != 0) {
                   1343:            SET_VFLG (1);
                   1344:        }
                   1345:        m68k_dreg(regs, (extra >> 12) & 7) = (uae_u32)a;
                   1346:     }
                   1347: #else
                   1348:     if (extra & 0x800) {
                   1349:        /* signed variant */
                   1350:        uae_s32 src1,src2;
                   1351:        uae_u32 dst_lo,dst_hi;
                   1352:        uae_u32 sign;
                   1353: 
                   1354:        src1 = (uae_s32)src;
                   1355:        src2 = (uae_s32)m68k_dreg(regs, (extra >> 12) & 7);
                   1356:        sign = (src1 ^ src2);
                   1357:        if (src1 < 0) src1 = -src1;
                   1358:        if (src2 < 0) src2 = -src2;
                   1359:        mul_unsigned((uae_u32)src1,(uae_u32)src2,&dst_hi,&dst_lo);
                   1360:        if (sign & 0x80000000) {
                   1361:                dst_hi = ~dst_hi;
                   1362:                dst_lo = -dst_lo;
                   1363:                if (dst_lo == 0) dst_hi++;
                   1364:        }
                   1365:        SET_VFLG (0);
                   1366:        SET_CFLG (0);
                   1367:        SET_ZFLG (dst_hi == 0 && dst_lo == 0);
                   1368:        SET_NFLG (((uae_s32)dst_hi) < 0);
                   1369:        if (extra & 0x400)
                   1370:            m68k_dreg(regs, extra & 7) = dst_hi;
                   1371:        else if ((dst_hi != 0 || (dst_lo & 0x80000000) != 0)
                   1372:                 && ((dst_hi & 0xffffffff) != 0xffffffff
                   1373:                     || (dst_lo & 0x80000000) != 0x80000000))
                   1374:        {
                   1375:            SET_VFLG (1);
                   1376:        }
                   1377:        m68k_dreg(regs, (extra >> 12) & 7) = dst_lo;
                   1378:     } else {
                   1379:        /* unsigned */
                   1380:        uae_u32 dst_lo,dst_hi;
                   1381: 
                   1382:        mul_unsigned(src,(uae_u32)m68k_dreg(regs, (extra >> 12) & 7),&dst_hi,&dst_lo);
                   1383: 
                   1384:        SET_VFLG (0);
                   1385:        SET_CFLG (0);
                   1386:        SET_ZFLG (dst_hi == 0 && dst_lo == 0);
                   1387:        SET_NFLG (((uae_s32)dst_hi) < 0);
                   1388:        if (extra & 0x400)
                   1389:            m68k_dreg(regs, extra & 7) = dst_hi;
                   1390:        else if (dst_hi != 0) {
                   1391:            SET_VFLG (1);
                   1392:        }
                   1393:        m68k_dreg(regs, (extra >> 12) & 7) = dst_lo;
                   1394:     }
                   1395: #endif
                   1396: }
1.1.1.6   root     1397: 
1.1       root     1398: 
                   1399: void m68k_reset (void)
                   1400: {
                   1401:     regs.s = 1;
                   1402:     regs.m = 0;
                   1403:     regs.stopped = 0;
                   1404:     regs.t1 = 0;
                   1405:     regs.t0 = 0;
                   1406:     SET_ZFLG (0);
                   1407:     SET_XFLG (0);
                   1408:     SET_CFLG (0);
                   1409:     SET_VFLG (0);
                   1410:     SET_NFLG (0);
1.1.1.7   root     1411:     regs.spcflags &= SPCFLAG_MODE_CHANGE;   /* Clear specialflags except mode-change */
1.1       root     1412:     regs.intmask = 7;
                   1413:     regs.vbr = regs.sfc = regs.dfc = 0;
                   1414:     regs.fpcr = regs.fpsr = regs.fpiar = 0;
1.1.1.7   root     1415: 
                   1416:     m68k_areg(regs, 7) = get_long(0);
                   1417:     m68k_setpc(get_long(4));
                   1418:     refill_prefetch (m68k_getpc(), 0);
1.1       root     1419: }
                   1420: 
1.1.1.8   root     1421: 
1.1       root     1422: unsigned long REGPARAM2 op_illg (uae_u32 opcode)
                   1423: {
1.1.1.8   root     1424: #if 0
1.1       root     1425:     uaecptr pc = m68k_getpc ();
1.1.1.8   root     1426: #endif
1.1.1.6   root     1427:     if ((opcode & 0xF000) == 0xF000) {
1.1.1.13  root     1428:        Exception(0xB,0,M68000_EXCEPTION_SRC_CPU);
1.1       root     1429:        return 4;
1.1.1.6   root     1430:     }
                   1431:     if ((opcode & 0xF000) == 0xA000) {
1.1.1.13  root     1432:        Exception(0xA,0,M68000_EXCEPTION_SRC_CPU);
1.1       root     1433:        return 4;
1.1.1.6   root     1434:     }
1.1.1.3   root     1435: #if 0
1.1.1.6   root     1436:     write_log ("Illegal instruction: %04x at %08lx\n", opcode, (long)pc);
1.1       root     1437: #endif
1.1.1.13  root     1438:     Exception (4,0,M68000_EXCEPTION_SRC_CPU);
1.1       root     1439:     return 4;
                   1440: }
                   1441: 
1.1.1.8   root     1442: 
1.1       root     1443: void mmu_op(uae_u32 opcode, uae_u16 extra)
                   1444: {
                   1445:     if ((opcode & 0xFE0) == 0x0500) {
                   1446:        /* PFLUSH */
                   1447:        mmusr = 0;
                   1448:        write_log ("PFLUSH\n");
                   1449:     } else if ((opcode & 0x0FD8) == 0x548) {
                   1450:        /* PTEST */
                   1451:        write_log ("PTEST\n");
                   1452:     } else
                   1453:        op_illg (opcode);
                   1454: }
                   1455: 
                   1456: 
                   1457: static uaecptr last_trace_ad = 0;
                   1458: 
                   1459: static void do_trace (void)
                   1460: {
1.1.1.12  root     1461:     if (regs.t0 && currprefs.cpu_level >= 2) {
1.1       root     1462:        uae_u16 opcode;
                   1463:        /* should also include TRAP, CHK, SR modification FPcc */
                   1464:        /* probably never used so why bother */
                   1465:        /* We can afford this to be inefficient... */
                   1466:        m68k_setpc (m68k_getpc ());
                   1467:        fill_prefetch_0 ();
                   1468:        opcode = get_word (regs.pc);
                   1469:        if (opcode == 0x4e72            /* RTE */
                   1470:            || opcode == 0x4e74                 /* RTD */
                   1471:            || opcode == 0x4e75                 /* RTS */
                   1472:            || opcode == 0x4e77                 /* RTR */
                   1473:            || opcode == 0x4e76                 /* TRAPV */
                   1474:            || (opcode & 0xffc0) == 0x4e80      /* JSR */
                   1475:            || (opcode & 0xffc0) == 0x4ec0      /* JMP */
                   1476:            || (opcode & 0xff00) == 0x6100  /* BSR */
                   1477:            || ((opcode & 0xf000) == 0x6000     /* Bcc */
                   1478:                && cctrue((opcode >> 8) & 0xf))
                   1479:            || ((opcode & 0xf0f0) == 0x5050 /* DBcc */
                   1480:                && !cctrue((opcode >> 8) & 0xf)
                   1481:                && (uae_s16)m68k_dreg(regs, opcode & 7) != 0))
                   1482:        {
                   1483:            last_trace_ad = m68k_getpc ();
                   1484:            unset_special (SPCFLAG_TRACE);
                   1485:            set_special (SPCFLAG_DOTRACE);
                   1486:        }
                   1487:     } else if (regs.t1) {
                   1488:        last_trace_ad = m68k_getpc ();
                   1489:        unset_special (SPCFLAG_TRACE);
                   1490:        set_special (SPCFLAG_DOTRACE);
                   1491:     }
                   1492: }
                   1493: 
                   1494: 
1.1.1.8   root     1495: /*
                   1496:  * Handle special flags
                   1497:  */
1.1.1.14! root     1498: 
        !          1499: static bool do_specialties_interrupt (int Pending)
        !          1500: {
        !          1501:     /* Check for MFP ints first (level 6) */
        !          1502:     if (regs.spcflags & SPCFLAG_MFP) {
        !          1503:        if ( MFP_CheckPendingInterrupts() == TRUE )
        !          1504:          return TRUE;                                  /* MFP exception was generated, no higher interrupt can happen */
        !          1505:     }
        !          1506: 
        !          1507:     /* No MFP int, check for VBL/HBL ints (levels 4/2) */
        !          1508:     if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) {
        !          1509:        int intr = intlev ();
        !          1510:        /* SPCFLAG_DOINT will be enabled again in MakeFromSR to handle pending interrupts! */
        !          1511: //     unset_special (SPCFLAG_DOINT);
        !          1512:        unset_special (SPCFLAG_INT | SPCFLAG_DOINT);
        !          1513:        if (intr != -1 && intr > regs.intmask) {
        !          1514:            Interrupt (intr , Pending);                 /* process the interrupt and add pending jitter if necessary */
        !          1515:            return TRUE;
        !          1516:        }
        !          1517:     }
        !          1518: 
        !          1519:     return FALSE;                                      /* no interrupt was found */
        !          1520: }
        !          1521: 
        !          1522: 
1.1       root     1523: static int do_specialties (void)
                   1524: {
1.1.1.7   root     1525:     if(regs.spcflags & SPCFLAG_BUSERROR) {
                   1526:        /* We can not execute bus errors directly in the memory handler
                   1527:         * functions since the PC should point to the address of the next
                   1528:         * instruction, so we're executing the bus errors here: */
1.1.1.8   root     1529:        unset_special(SPCFLAG_BUSERROR);
1.1.1.13  root     1530:        Exception(2,0,M68000_EXCEPTION_SRC_CPU);
1.1.1.7   root     1531:     }
                   1532: 
1.1.1.8   root     1533:     if(regs.spcflags & SPCFLAG_EXTRA_CYCLES) {
                   1534:        /* Add some extra cycles to simulate a wait state */
                   1535:        unset_special(SPCFLAG_EXTRA_CYCLES);
1.1.1.11  root     1536:        M68000_AddCycles(nWaitStateCycles);
1.1.1.12  root     1537:        nWaitStateCycles = 0;
1.1.1.8   root     1538:     }
                   1539: 
1.1       root     1540:     if (regs.spcflags & SPCFLAG_DOTRACE) {
1.1.1.13  root     1541:        Exception (9,last_trace_ad,M68000_EXCEPTION_SRC_CPU);
1.1       root     1542:     }
1.1.1.8   root     1543: 
1.1.1.14! root     1544: 
        !          1545:     /* Handle the STOP instruction */
        !          1546:     if ( regs.spcflags & SPCFLAG_STOP ) {
        !          1547:         /* We first test if there's a pending interrupt that would */
        !          1548:         /* allow to immediatly leave the STOP state */
        !          1549:         if ( do_specialties_interrupt ( TRUE ) ) {             /* test if there's an interrupt and add pending jitter */
        !          1550:             regs.stopped = 0;
        !          1551:             unset_special (SPCFLAG_STOP);
        !          1552:         }
        !          1553: #if 0
        !          1554:        if (regs.spcflags & SPCFLAG_MFP)                        /* MFP int */
        !          1555:            MFP_CheckPendingInterrupts();
        !          1556:        
        !          1557:        if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) {    /* VBL/HBL ints */
        !          1558:            int intr = intlev ();
        !          1559:            unset_special (SPCFLAG_INT | SPCFLAG_DOINT);
        !          1560:            if (intr != -1 && intr > regs.intmask) {
        !          1561:                Interrupt (intr , TRUE);                /* process the interrupt and add pending jitter */
        !          1562:                regs.stopped = 0;
        !          1563:                unset_special (SPCFLAG_STOP);
        !          1564:            }
        !          1565:        }
        !          1566: #endif
        !          1567: 
        !          1568:        /* No pending int, we have to wait for the next matching int */
        !          1569:        while (regs.spcflags & SPCFLAG_STOP) {
        !          1570: 
        !          1571:            /* Take care of quit event if needed */
        !          1572:            if (regs.spcflags & SPCFLAG_BRK)
        !          1573:                return 1;
        !          1574:        
        !          1575:            M68000_AddCycles(4);
        !          1576:        
        !          1577:            /* It is possible one or more ints happen at the same time */
        !          1578:            /* We must process them during the same cpu cycle until the special INT flag is set */
        !          1579:            while (PendingInterruptCount<=0 && PendingInterruptFunction) {
        !          1580:                /* 1st, we call the interrupt handler */
        !          1581:                CALL_VAR(PendingInterruptFunction);
        !          1582:                
        !          1583:                /* Then we check if this handler triggered an interrupt to process */
        !          1584:                if ( do_specialties_interrupt ( FALSE ) ) {     /* test if there's an interrupt and add non pending jitter */
1.1.1.13  root     1585:                    regs.stopped = 0;
                   1586:                    unset_special (SPCFLAG_STOP);
1.1.1.14! root     1587:                    break;
        !          1588:                }
        !          1589: #if 0          
        !          1590:                /* Then we check if this handler triggered an MFP int to process */
        !          1591:                if (regs.spcflags & SPCFLAG_MFP)
        !          1592:                    MFP_CheckPendingInterrupts();
        !          1593:        
        !          1594:                if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) {
        !          1595:                    int intr = intlev ();
        !          1596:                    unset_special (SPCFLAG_INT | SPCFLAG_DOINT);
        !          1597:                    if (intr != -1 && intr > regs.intmask) {
        !          1598:                        Interrupt (intr , FALSE);       /* process the interrupt and add non pending jitter */
        !          1599:                        regs.stopped = 0;
        !          1600:                        unset_special (SPCFLAG_STOP);
        !          1601:                        break;
        !          1602:                    }
        !          1603:                }
        !          1604: #endif
1.1.1.6   root     1605:            }
1.1       root     1606:        }
                   1607:     }
1.1.1.8   root     1608: 
1.1.1.14! root     1609: 
1.1       root     1610:     if (regs.spcflags & SPCFLAG_TRACE)
                   1611:        do_trace ();
                   1612: 
1.1.1.12  root     1613: //    if (regs.spcflags & SPCFLAG_DOINT) {
                   1614:     /* [NP] pending int should be processed now, not after the current instr */
1.1.1.14! root     1615:     /* so we check for (SPCFLAG_INT | SPCFLAG_DOINT), not just for SPCFLAG_DOINT */
        !          1616:                
        !          1617:     if ( do_specialties_interrupt ( FALSE ) ) {        /* test if there's an interrupt and add non pending jitter */
        !          1618:         regs.stopped = 0;                      /* [NP] useless ? */
        !          1619:     }
        !          1620:     if (regs.spcflags & SPCFLAG_INT) {
        !          1621:        unset_special (SPCFLAG_INT);
        !          1622:        set_special (SPCFLAG_DOINT);
        !          1623:     }
        !          1624: #if 0
1.1.1.12  root     1625:     if (regs.spcflags & (SPCFLAG_INT | SPCFLAG_DOINT)) {
1.1.1.6   root     1626:        int intr = intlev ();
1.1.1.8   root     1627:        /* SPCFLAG_DOINT will be enabled again in MakeFromSR to handle pending interrupts! */
1.1.1.12  root     1628: //     unset_special (SPCFLAG_DOINT);
                   1629:        unset_special (SPCFLAG_INT | SPCFLAG_DOINT);
1.1.1.6   root     1630:        if (intr != -1 && intr > regs.intmask) {
1.1.1.14! root     1631:            Interrupt (intr , FALSE);           /* call Interrupt() with Pending=FALSE, not necessarily true but harmless */
        !          1632:            regs.stopped = 0;                   /* [NP] useless ? */
1.1.1.6   root     1633:        }
1.1       root     1634:     }
                   1635:     if (regs.spcflags & SPCFLAG_INT) {
                   1636:        unset_special (SPCFLAG_INT);
                   1637:        set_special (SPCFLAG_DOINT);
                   1638:     }
1.1.1.8   root     1639: 
                   1640:     if (regs.spcflags & SPCFLAG_MFP) {          /* Check for MFP interrupts */
                   1641:        MFP_CheckPendingInterrupts();
                   1642:     }
1.1.1.14! root     1643: #endif
1.1.1.8   root     1644: 
1.1       root     1645:     if (regs.spcflags & (SPCFLAG_BRK | SPCFLAG_MODE_CHANGE)) {
1.1.1.8   root     1646:        unset_special(SPCFLAG_MODE_CHANGE);
1.1       root     1647:        return 1;
                   1648:     }
1.1.1.8   root     1649: 
1.1       root     1650:     return 0;
                   1651: }
                   1652: 
1.1.1.3   root     1653: 
1.1       root     1654: /* It's really sad to have two almost identical functions for this, but we
                   1655:    do it all for performance... :( */
                   1656: static void m68k_run_1 (void)
                   1657: {
                   1658: #ifdef DEBUG_PREFETCH
                   1659:     uae_u8 saved_bytes[20];
                   1660:     uae_u16 *oldpcp;
                   1661: #endif
1.1.1.8   root     1662: 
                   1663:     for (;;) {
1.1       root     1664:        int cycles;
                   1665:        uae_u32 opcode = get_iword_prefetch (0);
1.1.1.8   root     1666: 
1.1       root     1667: #ifdef DEBUG_PREFETCH
                   1668:        if (get_ilong (0) != do_get_mem_long (&regs.prefetch)) {
                   1669:            fprintf (stderr, "Prefetch differs from memory.\n");
                   1670:            debugging = 1;
                   1671:            return;
                   1672:        }
                   1673:        oldpcp = regs.pc_p;
                   1674:        memcpy (saved_bytes, regs.pc_p, 20);
                   1675: #endif
                   1676: 
                   1677:        /*m68k_dumpstate(stderr, NULL);*/
1.1.1.12  root     1678:        if ( HATARI_TRACE_LEVEL ( HATARI_TRACE_CPU_DISASM ) )
                   1679:          {
                   1680:            int nFrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO);;
                   1681:            int nLineCycles = nFrameCycles % nCyclesPerLine;
                   1682:            HATARI_TRACE_PRINT ( "video_cyc=%6d %3d@%3d : " , nFrameCycles, nLineCycles, nHBL );
                   1683:            m68k_disasm(stderr, m68k_getpc (), NULL, 1);
                   1684:          }
1.1       root     1685: 
                   1686:        /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */
                   1687: /*     regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/
                   1688: #if COUNT_INSTRS == 2
                   1689:        if (table68k[opcode].handler != -1)
                   1690:            instrcount[table68k[opcode].handler]++;
                   1691: #elif COUNT_INSTRS == 1
                   1692:        instrcount[opcode]++;
                   1693: #endif
1.1.1.2   root     1694: 
1.1.1.12  root     1695:        /* In case of a Bus Error, we need the PC of the instruction that caused */
                   1696:        /* the error to build the exception stack frame */
                   1697:        BusErrorPC = m68k_getpc();
                   1698: 
1.1.1.6   root     1699:        cycles = (*cpufunctbl[opcode])(opcode);
                   1700: 
1.1       root     1701: #ifdef DEBUG_PREFETCH
                   1702:        if (memcmp (saved_bytes, oldpcp, 20) != 0) {
1.1.1.12  root     1703:            fprintf (stderr, "Self-modifying code detected %x.\n" , m68k_getpc() );
1.1       root     1704:            set_special (SPCFLAG_BRK);
                   1705:            debugging = 1;
                   1706:        }
                   1707: #endif
1.1.1.2   root     1708: 
1.1.1.12  root     1709:        M68000_AddCyclesWithPairing(cycles);
1.1.1.13  root     1710:        if (regs.spcflags & SPCFLAG_EXTRA_CYCLES) {
                   1711:          /* Add some extra cycles to simulate a wait state */
                   1712:          unset_special(SPCFLAG_EXTRA_CYCLES);
                   1713:          M68000_AddCycles(nWaitStateCycles);
                   1714:          nWaitStateCycles = 0;
                   1715:        }
                   1716: 
1.1.1.14! root     1717: #if 0
        !          1718:        while (PendingInterruptCount <= 0 && PendingInterruptFunction)
1.1.1.8   root     1719:          CALL_VAR(PendingInterruptFunction);
                   1720: 
1.1       root     1721:        if (regs.spcflags) {
                   1722:            if (do_specialties ())
                   1723:                return;
                   1724:        }
1.1.1.14! root     1725: #else
        !          1726:        /* We can have several interrupts at the same time before the next CPU instruction */
        !          1727:        /* We must check for pending interrupt and call do_specialties_interrupt() only */
        !          1728:        /* if the cpu is not in the STOP state. Else, the int could be acknowledged now */
        !          1729:        /* and prevent exiting the STOP state when calling do_specialties() after. */
        !          1730:        /* For performance, we first test PendingInterruptCount, then regs.spcflags */
        !          1731:        while ( ( PendingInterruptCount <= 0 ) && ( PendingInterruptFunction ) && ( ( regs.spcflags & SPCFLAG_STOP ) == 0 ) )
        !          1732:          {
        !          1733:            CALL_VAR(PendingInterruptFunction);         /* call the interrupt handler */
        !          1734:            do_specialties_interrupt ( FALSE );         /* test if there's an mfp/video interrupt and add non pending jitter */
        !          1735: #if 0
        !          1736:                  if ( regs.spcflags & ( SPCFLAG_MFP | SPCFLAG_INT ) ) {        /* only check mfp/video interrupts */
        !          1737:                    if (do_specialties ())                      /* check if this latest int has higher priority */
        !          1738:                        return;
        !          1739:                  }
        !          1740: #endif
        !          1741:          }
        !          1742: 
        !          1743:        if (regs.spcflags) {
        !          1744:            if (do_specialties ())
        !          1745:                return;
        !          1746:        }
        !          1747: #endif
        !          1748: 
1.1       root     1749:     }
                   1750: }
                   1751: 
                   1752: 
                   1753: /* Same thing, but don't use prefetch to get opcode.  */
                   1754: static void m68k_run_2 (void)
                   1755: {
1.1.1.8   root     1756:     for (;;) {
1.1       root     1757:        int cycles;
                   1758:        uae_u32 opcode = get_iword (0);
                   1759: 
                   1760:        /*m68k_dumpstate(stderr, NULL);*/
1.1.1.12  root     1761:        if ( HATARI_TRACE_LEVEL ( HATARI_TRACE_CPU_DISASM ) )
                   1762:          {
                   1763:            int nFrameCycles = Cycles_GetCounter(CYCLES_COUNTER_VIDEO);;
                   1764:            int nLineCycles = nFrameCycles % nCyclesPerLine;
                   1765:            HATARI_TRACE_PRINT ( "video_cyc=%6d %3d@%3d : " , nFrameCycles, nLineCycles, nHBL );
                   1766:            m68k_disasm(stderr, m68k_getpc (), NULL, 1);
                   1767:          }
                   1768: 
1.1       root     1769:        /* assert (!regs.stopped && !(regs.spcflags & SPCFLAG_STOP)); */
                   1770: /*     regs_backup[backup_pointer = (backup_pointer + 1) % 16] = regs;*/
                   1771: #if COUNT_INSTRS == 2
                   1772:        if (table68k[opcode].handler != -1)
                   1773:            instrcount[table68k[opcode].handler]++;
                   1774: #elif COUNT_INSTRS == 1
                   1775:        instrcount[opcode]++;
                   1776: #endif
1.1.1.2   root     1777: 
1.1.1.6   root     1778:        cycles = (*cpufunctbl[opcode])(opcode);
                   1779: 
1.1.1.8   root     1780:        M68000_AddCycles(cycles);
1.1.1.13  root     1781:        if (regs.spcflags & SPCFLAG_EXTRA_CYCLES) {
                   1782:          /* Add some extra cycles to simulate a wait state */
                   1783:          unset_special(SPCFLAG_EXTRA_CYCLES);
                   1784:          M68000_AddCycles(nWaitStateCycles);
                   1785:          nWaitStateCycles = 0;
                   1786:        }
                   1787: 
1.1.1.12  root     1788:        while (PendingInterruptCount <= 0 && PendingInterruptFunction)
1.1.1.8   root     1789:          CALL_VAR(PendingInterruptFunction);
                   1790: 
1.1       root     1791:        if (regs.spcflags) {
                   1792:            if (do_specialties ())
                   1793:                return;
                   1794:        }
                   1795:     }
                   1796: }
                   1797: 
                   1798: 
                   1799: void m68k_go (int may_quit)
                   1800: {
1.1.1.8   root     1801:     static int in_m68k_go = 0;
                   1802: 
1.1       root     1803:     if (in_m68k_go || !may_quit) {
                   1804:        write_log ("Bug! m68k_go is not reentrant.\n");
                   1805:        abort ();
                   1806:     }
                   1807: 
                   1808:     in_m68k_go++;
1.1.1.8   root     1809:     while (!(regs.spcflags & SPCFLAG_BRK)) {
1.1.1.12  root     1810:         if(currprefs.cpu_compatible)
1.1.1.2   root     1811:           m68k_run_1();
                   1812:          else
                   1813:           m68k_run_2();
1.1       root     1814:     }
1.1.1.8   root     1815:     unset_special(SPCFLAG_BRK);
1.1       root     1816:     in_m68k_go--;
                   1817: }
                   1818: 
1.1.1.8   root     1819: 
                   1820: /*
1.1       root     1821: static void m68k_verify (uaecptr addr, uaecptr *nextpc)
                   1822: {
                   1823:     uae_u32 opcode, val;
                   1824:     struct instr *dp;
                   1825: 
                   1826:     opcode = get_iword_1(0);
                   1827:     last_op_for_exception_3 = opcode;
                   1828:     m68kpc_offset = 2;
                   1829: 
1.1.1.6   root     1830:     if (cpufunctbl[opcode] == op_illg_1) {
1.1       root     1831:        opcode = 0x4AFC;
                   1832:     }
                   1833:     dp = table68k + opcode;
                   1834: 
                   1835:     if (dp->suse) {
                   1836:        if (!verify_ea (dp->sreg, dp->smode, dp->size, &val)) {
1.1.1.13  root     1837:            Exception (3, 0,M68000_EXCEPTION_SRC_CPU);
1.1       root     1838:            return;
                   1839:        }
                   1840:     }
                   1841:     if (dp->duse) {
                   1842:        if (!verify_ea (dp->dreg, dp->dmode, dp->size, &val)) {
1.1.1.13  root     1843:            Exception (3, 0,M68000_EXCEPTION_SRC_CPU);
1.1       root     1844:            return;
                   1845:        }
                   1846:     }
                   1847: }
1.1.1.8   root     1848: */
                   1849: 
1.1       root     1850: 
                   1851: void m68k_disasm (FILE *f, uaecptr addr, uaecptr *nextpc, int cnt)
                   1852: {
1.1.1.11  root     1853:     static const char * const ccnames[] =
1.1.1.8   root     1854:         { "T ","F ","HI","LS","CC","CS","NE","EQ",
                   1855:           "VC","VS","PL","MI","GE","LT","GT","LE" };
                   1856: 
1.1       root     1857:     uaecptr newpc = 0;
                   1858:     m68kpc_offset = addr - m68k_getpc ();
                   1859:     while (cnt-- > 0) {
                   1860:        char instrname[20],*ccpt;
                   1861:        int opwords;
                   1862:        uae_u32 opcode;
1.1.1.11  root     1863:        const struct mnemolookup *lookup;
1.1       root     1864:        struct instr *dp;
                   1865:        fprintf (f, "%08lx: ", m68k_getpc () + m68kpc_offset);
                   1866:        for (opwords = 0; opwords < 5; opwords++){
                   1867:            fprintf (f, "%04x ", get_iword_1 (m68kpc_offset + opwords*2));
                   1868:        }
                   1869:        opcode = get_iword_1 (m68kpc_offset);
                   1870:        m68kpc_offset += 2;
1.1.1.6   root     1871:        if (cpufunctbl[opcode] == op_illg_1) {
1.1       root     1872:            opcode = 0x4AFC;
                   1873:        }
                   1874:        dp = table68k + opcode;
                   1875:        for (lookup = lookuptab;lookup->mnemo != dp->mnemo; lookup++)
                   1876:            ;
                   1877: 
                   1878:        strcpy (instrname, lookup->name);
                   1879:        ccpt = strstr (instrname, "cc");
                   1880:        if (ccpt != 0) {
                   1881:            strncpy (ccpt, ccnames[dp->cc], 2);
                   1882:        }
                   1883:        fprintf (f, "%s", instrname);
                   1884:        switch (dp->size){
                   1885:         case sz_byte: fprintf (f, ".B "); break;
                   1886:         case sz_word: fprintf (f, ".W "); break;
                   1887:         case sz_long: fprintf (f, ".L "); break;
                   1888:         default: fprintf (f, "   "); break;
                   1889:        }
                   1890: 
                   1891:        if (dp->suse) {
                   1892:            newpc = m68k_getpc () + m68kpc_offset;
                   1893:            newpc += ShowEA (f, dp->sreg, dp->smode, dp->size, 0);
                   1894:        }
                   1895:        if (dp->suse && dp->duse)
                   1896:            fprintf (f, ",");
                   1897:        if (dp->duse) {
                   1898:            newpc = m68k_getpc () + m68kpc_offset;
                   1899:            newpc += ShowEA (f, dp->dreg, dp->dmode, dp->size, 0);
                   1900:        }
                   1901:        if (ccpt != 0) {
                   1902:            if (cctrue(dp->cc))
1.1.1.5   root     1903:                fprintf (f, " == %08lx (TRUE)", (long)newpc);
1.1       root     1904:            else
1.1.1.5   root     1905:                fprintf (f, " == %08lx (FALSE)", (long)newpc);
1.1       root     1906:        } else if ((opcode & 0xff00) == 0x6100) /* BSR */
1.1.1.5   root     1907:            fprintf (f, " == %08lx", (long)newpc);
1.1       root     1908:        fprintf (f, "\n");
                   1909:     }
                   1910:     if (nextpc)
                   1911:        *nextpc = m68k_getpc () + m68kpc_offset;
                   1912: }
                   1913: 
                   1914: void m68k_dumpstate (FILE *f, uaecptr *nextpc)
                   1915: {
                   1916:     int i;
                   1917:     for (i = 0; i < 8; i++){
1.1.1.5   root     1918:        fprintf (f, "D%d: %08lx ", i, (long)m68k_dreg(regs, i));
1.1       root     1919:        if ((i & 3) == 3) fprintf (f, "\n");
                   1920:     }
                   1921:     for (i = 0; i < 8; i++){
1.1.1.5   root     1922:        fprintf (f, "A%d: %08lx ", i, (long)m68k_areg(regs, i));
1.1       root     1923:        if ((i & 3) == 3) fprintf (f, "\n");
                   1924:     }
                   1925:     if (regs.s == 0) regs.usp = m68k_areg(regs, 7);
                   1926:     if (regs.s && regs.m) regs.msp = m68k_areg(regs, 7);
                   1927:     if (regs.s && regs.m == 0) regs.isp = m68k_areg(regs, 7);
                   1928:     fprintf (f, "USP=%08lx ISP=%08lx MSP=%08lx VBR=%08lx\n",
1.1.1.5   root     1929:             (long)regs.usp,(long)regs.isp,(long)regs.msp,(long)regs.vbr);
1.1       root     1930:     fprintf (f, "T=%d%d S=%d M=%d X=%d N=%d Z=%d V=%d C=%d IMASK=%d\n",
                   1931:             regs.t1, regs.t0, regs.s, regs.m,
                   1932:             GET_XFLG, GET_NFLG, GET_ZFLG, GET_VFLG, GET_CFLG, regs.intmask);
                   1933:     for (i = 0; i < 8; i++){
                   1934:        fprintf (f, "FP%d: %g ", i, regs.fp[i]);
                   1935:        if ((i & 3) == 3) fprintf (f, "\n");
                   1936:     }
                   1937:     fprintf (f, "N=%d Z=%d I=%d NAN=%d\n",
                   1938:             (regs.fpsr & 0x8000000) != 0,
                   1939:             (regs.fpsr & 0x4000000) != 0,
                   1940:             (regs.fpsr & 0x2000000) != 0,
                   1941:             (regs.fpsr & 0x1000000) != 0);
1.1.1.12  root     1942:     if (currprefs.cpu_compatible)
1.1       root     1943:        fprintf (f, "prefetch %08lx\n", (unsigned long)do_get_mem_long(&regs.prefetch));
                   1944: 
                   1945:     m68k_disasm (f, m68k_getpc (), nextpc, 1);
                   1946:     if (nextpc)
1.1.1.5   root     1947:        fprintf (f, "next PC: %08lx\n", (long)*nextpc);
1.1       root     1948: }
1.1.1.12  root     1949: 
                   1950: 
                   1951: /*
                   1952: 
                   1953:  The routines below take dividend and divisor as parameters.
                   1954:  They return 0 if division by zero, or exact number of cycles otherwise.
                   1955: 
                   1956:  The number of cycles returned assumes a register operand.
                   1957:  Effective address time must be added if memory operand.
                   1958: 
                   1959:  For 68000 only (not 68010, 68012, 68020, etc).
                   1960:  Probably valid for 68008 after adding the extra prefetch cycle.
                   1961: 
                   1962: 
                   1963:  Best and worst cases are for register operand:
                   1964:  (Note the difference with the documented range.)
                   1965: 
                   1966: 
                   1967:  DIVU:
                   1968: 
                   1969:  Overflow (always): 10 cycles.
                   1970:  Worst case: 136 cycles.
                   1971:  Best case: 76 cycles.
                   1972: 
                   1973: 
                   1974:  DIVS:
                   1975: 
                   1976:  Absolute overflow: 16-18 cycles.
                   1977:  Signed overflow is not detected prematurely.
                   1978: 
                   1979:  Worst case: 156 cycles.
                   1980:  Best case without signed overflow: 122 cycles.
                   1981:  Best case with signed overflow: 120 cycles
                   1982: 
                   1983: 
                   1984:  */
                   1985: 
                   1986: 
                   1987: //
                   1988: // DIVU
                   1989: // Unsigned division
                   1990: //
                   1991: 
                   1992: STATIC_INLINE int getDivu68kCycles_2 (uae_u32 dividend, uae_u16 divisor)
                   1993: {
                   1994:     int mcycles;
                   1995:     uae_u32 hdivisor;
                   1996:     int i;
                   1997: 
                   1998:     if (divisor == 0)
                   1999:        return 0;
                   2000: 
                   2001:     // Overflow
                   2002:     if ((dividend >> 16) >= divisor)
                   2003:        return (mcycles = 5) * 2;
                   2004: 
                   2005:     mcycles = 38;
                   2006:     hdivisor = divisor << 16;
                   2007: 
                   2008:     for (i = 0; i < 15; i++) {
                   2009:        uae_u32 temp;
                   2010:        temp = dividend;
                   2011: 
                   2012:        dividend <<= 1;
                   2013: 
                   2014:        // If carry from shift
                   2015:        if ((uae_s32)temp < 0)
                   2016:            dividend -= hdivisor;
                   2017:        else {
                   2018:            mcycles += 2;
                   2019:            if (dividend >= hdivisor) {
                   2020:                dividend -= hdivisor;
                   2021:                mcycles--;
                   2022:            }
                   2023:        }
                   2024:     }
                   2025:     return mcycles * 2;
                   2026: }
                   2027: int getDivu68kCycles (uae_u32 dividend, uae_u16 divisor)
                   2028: {
                   2029:     int v = getDivu68kCycles_2 (dividend, divisor) - 4;
                   2030: //    write_log ("U%d ", v);
                   2031:     return v;
                   2032: }
                   2033: 
                   2034: //
                   2035: // DIVS
                   2036: // Signed division
                   2037: //
                   2038: 
                   2039: STATIC_INLINE int getDivs68kCycles_2 (uae_s32 dividend, uae_s16 divisor)
                   2040: {
                   2041:     int mcycles;
                   2042:     uae_u32 aquot;
                   2043:     int i;
                   2044: 
                   2045:     if (divisor == 0)
                   2046:        return 0;
                   2047: 
                   2048:     mcycles = 6;
                   2049: 
                   2050:     if (dividend < 0)
                   2051:        mcycles++;
                   2052: 
                   2053:     // Check for absolute overflow
                   2054:     if (((uae_u32)abs (dividend) >> 16) >= (uae_u16)abs (divisor))
                   2055:        return (mcycles + 2) * 2;
                   2056: 
                   2057:     // Absolute quotient
                   2058:     aquot = (uae_u32) abs (dividend) / (uae_u16)abs (divisor);
                   2059: 
                   2060:     mcycles += 55;
                   2061: 
                   2062:     if (divisor >= 0) {
                   2063:        if (dividend >= 0)
                   2064:            mcycles--;
                   2065:        else
                   2066:            mcycles++;
                   2067:     }
                   2068: 
                   2069:     // Count 15 msbits in absolute of quotient
                   2070: 
                   2071:     for (i = 0; i < 15; i++) {
                   2072:        if ((uae_s16)aquot >= 0)
                   2073:            mcycles++;
                   2074:        aquot <<= 1;
                   2075:     }
                   2076: 
                   2077:     return mcycles * 2;
                   2078: }
                   2079: int getDivs68kCycles (uae_s32 dividend, uae_s16 divisor)
                   2080: {
                   2081:     int v = getDivs68kCycles_2 (dividend, divisor) - 4;
                   2082: //    write_log ("S%d ", v);
                   2083:     return v;
                   2084: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.