Annotation of hatari/src/cpu/hatari-glue.c, revision 1.1.1.8

1.1       root        1: /*
                      2:   Hatari - hatari-glue.c
                      3: 
1.1.1.4   root        4:   This file is distributed under the GNU General Public License, version 2
                      5:   or at your option any later version. Read the file gpl.txt for details.
1.1       root        6: 
                      7:   This file contains some code to glue the UAE CPU core to the rest of the
                      8:   emulator and Hatari's "illegal" opcodes.
                      9: */
                     10: const char HatariGlue_fileid[] = "Hatari hatari-glue.c : " __DATE__ " " __TIME__;
                     11: 
                     12: 
                     13: #include <stdio.h>
                     14: 
                     15: #include "main.h"
                     16: #include "configuration.h"
                     17: #include "cycInt.h"
                     18: #include "tos.h"
                     19: #include "gemdos.h"
1.1.1.4   root       20: #include "natfeats.h"
1.1       root       21: #include "cart.h"
                     22: #include "vdi.h"
                     23: #include "stMemory.h"
                     24: #include "ikbd.h"
                     25: #include "screen.h"
                     26: #include "video.h"
1.1.1.2   root       27: #include "psg.h"
1.1.1.4   root       28: #include "mfp.h"
                     29: #include "fdc.h"
1.1       root       30: 
                     31: #include "sysdeps.h"
1.1.1.6   root       32: #include "options_cpu.h"
1.1       root       33: #include "maccess.h"
                     34: #include "memory.h"
1.1.1.4   root       35: #include "m68000.h"
1.1       root       36: #include "newcpu.h"
1.1.1.3   root       37: #include "cpu_prefetch.h"
1.1       root       38: #include "hatari-glue.h"
                     39: 
                     40: 
                     41: struct uae_prefs currprefs, changed_prefs;
                     42: 
                     43: int pendingInterrupts = 0;
                     44: 
                     45: 
                     46: /**
                     47:  * Reset custom chips
1.1.1.4   root       48:  * In case the RESET instruction is called, we must reset all the peripherals
                     49:  * connected to the CPU's reset pin.
1.1       root       50:  */
                     51: void customreset(void)
                     52: {
                     53:        pendingInterrupts = 0;
                     54: 
1.1.1.4   root       55:        /* Reset the IKBD */
                     56:        IKBD_Reset ( false );
1.1       root       57: 
                     58:        /* Reseting the GLUE video chip should also set freq/res register to 0 */
                     59:        Video_Reset_Glue ();
1.1.1.2   root       60: 
1.1.1.7   root       61:        /* Reset the YM2149 (stop any sound) */
                     62:        PSG_Reset ();
1.1.1.4   root       63: 
                     64:        /* Reset the MFP */
                     65:        MFP_Reset ();
                     66: 
                     67:        /* Reset the FDC */
1.1.1.5   root       68:        FDC_Reset ( false );
1.1       root       69: }
                     70: 
                     71: 
                     72: /**
                     73:  * Return interrupt number (1 - 7), -1 means no interrupt.
                     74:  * Note that the interrupt stays pending if it can't be executed yet
                     75:  * due to the interrupt level field in the SR.
                     76:  */
                     77: int intlev(void)
                     78: {
1.1.1.6   root       79:        if ( pendingInterrupts & (1 << 6) )             /* MFP/DSP interrupt ? */
                     80:                return 6;
                     81:        else if ( pendingInterrupts & (1 << 4) )        /* VBL interrupt ? */
1.1.1.4   root       82:                return 4;
                     83:        else if ( pendingInterrupts & (1 << 2) )        /* HBL interrupt ? */
                     84:                return 2;
1.1       root       85: 
                     86:        return -1;
                     87: }
                     88: 
                     89: /**
                     90:  * Initialize 680x0 emulation
                     91:  */
                     92: int Init680x0(void)
                     93: {
                     94:        currprefs.cpu_level = changed_prefs.cpu_level = ConfigureParams.System.nCpuLevel;
                     95: 
1.1.1.7   root       96:        switch (currprefs.cpu_level)
                     97:        {
                     98:                case 0 : changed_prefs.cpu_model = 68000; break;
                     99:                case 1 : changed_prefs.cpu_model = 68010; break;
                    100:                case 2 : changed_prefs.cpu_model = 68020; break;
                    101:                case 3 : changed_prefs.cpu_model = 68030; break;
                    102:                case 4 : changed_prefs.cpu_model = 68040; break;
                    103:                case 5 : changed_prefs.cpu_model = 68060; break;
1.1       root      104:                default: fprintf (stderr, "Init680x0() : Error, cpu_level unknown\n");
                    105:        }
1.1.1.7   root      106: 
1.1.1.8 ! root      107:        /* Only 68040/60 can have 'internal' FPU */
        !           108:        if ( ( ConfigureParams.System.n_FPUType == FPU_CPU ) && ( changed_prefs.cpu_model < 68040 ) )
        !           109:                ConfigureParams.System.n_FPUType = FPU_NONE;
        !           110: 
        !           111:        /* 68000/10 can't have an FPU */
        !           112:        if ( ( ConfigureParams.System.n_FPUType != FPU_NONE ) && ( changed_prefs.cpu_model < 68020 ) )
        !           113:        {
        !           114:                Log_Printf(LOG_WARN, "FPU is not supported in 68000/010 configurations, disabling FPU\n");
        !           115:                ConfigureParams.System.n_FPUType = FPU_NONE;
        !           116:        }
        !           117: 
1.1.1.7   root      118:        changed_prefs.int_no_unimplemented = true;
                    119:        changed_prefs.fpu_no_unimplemented = true;
                    120:        changed_prefs.cpu_compatible = ConfigureParams.System.bCompatibleCpu;
                    121:        changed_prefs.address_space_24 = ConfigureParams.System.bAddressSpace24;
                    122:        changed_prefs.cpu_cycle_exact = ConfigureParams.System.bCycleExactCpu;
1.1.1.8 ! root      123:        changed_prefs.cpu_memory_cycle_exact = ConfigureParams.System.bCycleExactCpu;
1.1.1.7   root      124:        changed_prefs.fpu_model = ConfigureParams.System.n_FPUType;
                    125:        changed_prefs.fpu_strict = ConfigureParams.System.bCompatibleFPU;
1.1.1.8 ! root      126:        changed_prefs.fpu_softfloat = ConfigureParams.System.bSoftFloatFPU;
        !           127: 
        !           128:         /* Always emulate instr/data caches for cpu >= 68020 */
        !           129:         changed_prefs.cpu_data_cache = true;
1.1.1.6   root      130: 
                    131:        /* Set the MMU model by taking the same value as CPU model */
                    132:        /* MMU is only supported for CPU >=68030 */
1.1.1.7   root      133:        changed_prefs.mmu_model = 0;                            /* MMU disabled by default */
                    134:        if (ConfigureParams.System.bMMU && changed_prefs.cpu_model >= 68030)
                    135:                changed_prefs.mmu_model = changed_prefs.cpu_model;      /* MMU enabled */
1.1       root      136: 
                    137:        init_m68k();
                    138: 
                    139:        return true;
                    140: }
                    141: 
                    142: 
                    143: /**
                    144:  * Deinitialize 680x0 emulation
                    145:  */
                    146: void Exit680x0(void)
                    147: {
                    148:        memory_uninit();
                    149: 
                    150:        free(table68k);
                    151:        table68k = NULL;
                    152: }
                    153: 
1.1.1.6   root      154: 
                    155: /**
                    156:  * Execute a 'NOP' opcode (increment PC by 2 bytes and take care
                    157:  * of prefetch at the CPU level depending on the current CPU mode)
                    158:  * This is used to return from Gemdos / Natfeats interception, by ignoring
                    159:  * the intercepted opcode and executing a NOP instead once the work has been done.
                    160:  */
                    161: static void    CpuDoNOP ( void )
                    162: {
                    163:        (*cpufunctbl[0X4E71])(0x4E71);
                    164: }
                    165: 
                    166: 
1.1       root      167: /**
1.1.1.7   root      168:  * Check whether PC is currently in ROM cartridge space - used
                    169:  * to test whether our "illegal" Hatari opcodes should be handled
                    170:  * or whether they are just "normal" illegal opcodes.
                    171:  */
                    172: static bool is_cart_pc(void)
                    173: {
                    174:        Uint32 pc = M68000_GetPC();
                    175: 
                    176:        if (ConfigureParams.System.bAddressSpace24 || (pc >> 24) == 0xff)
                    177:        {
                    178:                pc &= 0x00ffffff;       /* Mask to 24-bit address */
                    179:        }
                    180: 
                    181:        return pc >= 0xfa0000 && pc < 0xfc0000;
                    182: }
                    183: 
                    184: 
                    185: /**
1.1       root      186:  * This function will be called at system init by the cartridge routine
                    187:  * (after gemdos init, before booting floppies).
                    188:  * The GEMDOS vector (#$84) is setup and we also initialize the connected
                    189:  * drive mask and Line-A  variables (for an extended VDI resolution) from here.
                    190:  */
1.1.1.7   root      191: uae_u32 REGPARAM3 OpCode_SysInit(uae_u32 opcode)
1.1       root      192: {
1.1.1.7   root      193:        if (is_cart_pc())
1.1       root      194:        {
1.1.1.7   root      195:                /* Add any drives mapped by TOS in the interim */
                    196:                ConnectedDriveMask |= STMemory_ReadLong(0x4c2);
                    197:                /* Initialize the connected drive mask */
                    198:                STMemory_WriteLong(0x4c2, ConnectedDriveMask);
                    199: 
1.1       root      200:                /* Init on boot - see cart.c */
                    201:                GemDOS_Boot();
                    202: 
                    203:                /* Update LineA for extended VDI res
                    204:                 * D0: LineA base, A1: Font base
                    205:                 */
                    206:                VDI_LineA(regs.regs[0], regs.regs[9]);
1.1.1.7   root      207: 
                    208:                CpuDoNOP ();
                    209:        }
                    210:        else
                    211:        {
                    212:                LOG_TRACE(TRACE_OS_GEMDOS | TRACE_OS_BASE | TRACE_OS_VDI | TRACE_OS_AES,
                    213:                          "SYSINIT opcode invoked outside of cartridge space\n");
                    214:                /* illegal instruction */
                    215:                op_illg(opcode);
                    216:                fill_prefetch();
1.1       root      217:        }
                    218: 
1.1.1.3   root      219:        return 4 * CYCLE_UNIT / 2;
1.1       root      220: }
                    221: 
                    222: 
                    223: /**
                    224:  * Intercept GEMDOS calls.
                    225:  * Used for GEMDOS HD emulation (see gemdos.c).
                    226:  */
1.1.1.7   root      227: uae_u32 REGPARAM3 OpCode_GemDos(uae_u32 opcode)
1.1       root      228: {
1.1.1.7   root      229:        if (is_cart_pc())
                    230:        {
                    231:                GemDOS_OpCode();    /* handler code in gemdos.c */
                    232:                CpuDoNOP();
                    233:        }
                    234:        else
                    235:        {
                    236:                LOG_TRACE(TRACE_OS_GEMDOS, "GEMDOS opcode invoked outside of cartridge space\n");
                    237:                /* illegal instruction */
                    238:                op_illg(opcode);
                    239:                fill_prefetch();
                    240:        }
1.1       root      241: 
1.1.1.3   root      242:        return 4 * CYCLE_UNIT / 2;
1.1       root      243: }
                    244: 
                    245: 
                    246: /**
                    247:  * This is called after completion of each VDI call
                    248:  */
1.1.1.7   root      249: uae_u32 REGPARAM3 OpCode_VDI(uae_u32 opcode)
1.1       root      250: {
1.1.1.4   root      251:        /* this is valid only after VDI trap, called from cartridge code */
1.1.1.7   root      252:        if (VDI_OldPC && is_cart_pc())
1.1.1.4   root      253:        {
                    254:                VDI_Complete();
                    255: 
                    256:                /* Set PC back to where originated from to continue instruction decoding */
                    257:                m68k_setpc(VDI_OldPC);
                    258:                VDI_OldPC = 0;
                    259:        }
                    260:        else
                    261:        {
1.1.1.7   root      262:                LOG_TRACE(TRACE_OS_VDI, "VDI opcode invoked outside of cartridge space\n");
1.1.1.4   root      263:                /* illegal instruction */
                    264:                op_illg(opcode);
                    265:        }
1.1       root      266: 
1.1.1.6   root      267:        fill_prefetch();
1.1.1.3   root      268:        return 4 * CYCLE_UNIT / 2;
1.1       root      269: }
1.1.1.4   root      270: 
                    271: 
                    272: /**
                    273:  * Emulator Native Features ID opcode interception.
                    274:  */
1.1.1.7   root      275: uae_u32 REGPARAM3 OpCode_NatFeat_ID(uae_u32 opcode)
1.1.1.4   root      276: {
                    277:        Uint32 stack = Regs[REG_A7] + SIZE_LONG;        /* skip return address */
                    278: 
1.1.1.7   root      279:        if (NatFeat_ID(stack, &(Regs[REG_D0])))
                    280:        {
1.1.1.6   root      281:                CpuDoNOP ();
1.1.1.4   root      282:        }
                    283:        return 4 * CYCLE_UNIT / 2;
                    284: }
                    285: 
                    286: /**
                    287:  * Emulator Native Features call opcode interception.
                    288:  */
1.1.1.7   root      289: uae_u32 REGPARAM3 OpCode_NatFeat_Call(uae_u32 opcode)
1.1.1.4   root      290: {
                    291:        Uint32 stack = Regs[REG_A7] + SIZE_LONG;        /* skip return address */
                    292:        Uint16 SR = M68000_GetSR();
                    293:        bool super;
                    294: 
                    295:        super = ((SR & SR_SUPERMODE) == SR_SUPERMODE);
1.1.1.7   root      296:        if (NatFeat_Call(stack, super, &(Regs[REG_D0])))
                    297:        {
1.1.1.6   root      298:                CpuDoNOP ();
1.1.1.4   root      299:        }
                    300:        return 4 * CYCLE_UNIT / 2;
                    301: }
1.1.1.6   root      302: 
                    303: 
1.1.1.7   root      304: TCHAR* buf_out (TCHAR *buffer, int *bufsize, const TCHAR *format, ...)
                    305: {
                    306:        va_list parms;
                    307: 
                    308:        if (buffer == NULL)
                    309:        {
                    310:                return NULL;
                    311:        }
                    312: 
                    313:        va_start (parms, format);
                    314:        vsnprintf (buffer, (*bufsize) - 1, format, parms);
                    315:        va_end (parms);
                    316:        *bufsize -= _tcslen (buffer);
1.1.1.6   root      317: 
1.1.1.7   root      318:        return buffer + _tcslen (buffer);
                    319: }
                    320: 
                    321: void error_log(const TCHAR *format, ...)
                    322: {
                    323:        va_list parms;
1.1.1.6   root      324: 
1.1.1.7   root      325:        va_start(parms, format);
                    326:        vfprintf(stderr, format, parms);
                    327:        va_end(parms);
1.1.1.6   root      328: 
1.1.1.7   root      329:        if (format[strlen(format) - 1] != '\n')
                    330:        {
                    331:                fputc('\n', stderr);
                    332:        }
1.1.1.6   root      333: }

unix.superglobalmegacorp.com

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