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

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: 
        !           107:        changed_prefs.int_no_unimplemented = true;
        !           108:        changed_prefs.fpu_no_unimplemented = true;
        !           109:        changed_prefs.cpu_compatible = ConfigureParams.System.bCompatibleCpu;
        !           110:        changed_prefs.address_space_24 = ConfigureParams.System.bAddressSpace24;
        !           111:        changed_prefs.cpu_cycle_exact = ConfigureParams.System.bCycleExactCpu;
        !           112:        changed_prefs.fpu_model = ConfigureParams.System.n_FPUType;
        !           113:        changed_prefs.fpu_strict = ConfigureParams.System.bCompatibleFPU;
1.1.1.6   root      114: 
                    115:        /* Set the MMU model by taking the same value as CPU model */
                    116:        /* MMU is only supported for CPU >=68030 */
1.1.1.7 ! root      117:        changed_prefs.mmu_model = 0;                            /* MMU disabled by default */
        !           118:        if (ConfigureParams.System.bMMU && changed_prefs.cpu_model >= 68030)
        !           119:                changed_prefs.mmu_model = changed_prefs.cpu_model;      /* MMU enabled */
1.1       root      120: 
                    121:        init_m68k();
                    122: 
                    123:        return true;
                    124: }
                    125: 
                    126: 
                    127: /**
                    128:  * Deinitialize 680x0 emulation
                    129:  */
                    130: void Exit680x0(void)
                    131: {
                    132:        memory_uninit();
                    133: 
                    134:        free(table68k);
                    135:        table68k = NULL;
                    136: }
                    137: 
1.1.1.6   root      138: 
                    139: /**
                    140:  * Execute a 'NOP' opcode (increment PC by 2 bytes and take care
                    141:  * of prefetch at the CPU level depending on the current CPU mode)
                    142:  * This is used to return from Gemdos / Natfeats interception, by ignoring
                    143:  * the intercepted opcode and executing a NOP instead once the work has been done.
                    144:  */
                    145: static void    CpuDoNOP ( void )
                    146: {
                    147:        (*cpufunctbl[0X4E71])(0x4E71);
                    148: }
                    149: 
                    150: 
1.1       root      151: /**
1.1.1.7 ! root      152:  * Check whether PC is currently in ROM cartridge space - used
        !           153:  * to test whether our "illegal" Hatari opcodes should be handled
        !           154:  * or whether they are just "normal" illegal opcodes.
        !           155:  */
        !           156: static bool is_cart_pc(void)
        !           157: {
        !           158:        Uint32 pc = M68000_GetPC();
        !           159: 
        !           160:        if (ConfigureParams.System.bAddressSpace24 || (pc >> 24) == 0xff)
        !           161:        {
        !           162:                pc &= 0x00ffffff;       /* Mask to 24-bit address */
        !           163:        }
        !           164: 
        !           165:        return pc >= 0xfa0000 && pc < 0xfc0000;
        !           166: }
        !           167: 
        !           168: 
        !           169: /**
1.1       root      170:  * This function will be called at system init by the cartridge routine
                    171:  * (after gemdos init, before booting floppies).
                    172:  * The GEMDOS vector (#$84) is setup and we also initialize the connected
                    173:  * drive mask and Line-A  variables (for an extended VDI resolution) from here.
                    174:  */
1.1.1.7 ! root      175: uae_u32 REGPARAM3 OpCode_SysInit(uae_u32 opcode)
1.1       root      176: {
1.1.1.7 ! root      177:        if (is_cart_pc())
1.1       root      178:        {
1.1.1.7 ! root      179:                /* Add any drives mapped by TOS in the interim */
        !           180:                ConnectedDriveMask |= STMemory_ReadLong(0x4c2);
        !           181:                /* Initialize the connected drive mask */
        !           182:                STMemory_WriteLong(0x4c2, ConnectedDriveMask);
        !           183: 
1.1       root      184:                /* Init on boot - see cart.c */
                    185:                GemDOS_Boot();
                    186: 
                    187:                /* Update LineA for extended VDI res
                    188:                 * D0: LineA base, A1: Font base
                    189:                 */
                    190:                VDI_LineA(regs.regs[0], regs.regs[9]);
1.1.1.7 ! root      191: 
        !           192:                CpuDoNOP ();
        !           193:        }
        !           194:        else
        !           195:        {
        !           196:                LOG_TRACE(TRACE_OS_GEMDOS | TRACE_OS_BASE | TRACE_OS_VDI | TRACE_OS_AES,
        !           197:                          "SYSINIT opcode invoked outside of cartridge space\n");
        !           198:                /* illegal instruction */
        !           199:                op_illg(opcode);
        !           200:                fill_prefetch();
1.1       root      201:        }
                    202: 
1.1.1.3   root      203:        return 4 * CYCLE_UNIT / 2;
1.1       root      204: }
                    205: 
                    206: 
                    207: /**
                    208:  * Intercept GEMDOS calls.
                    209:  * Used for GEMDOS HD emulation (see gemdos.c).
                    210:  */
1.1.1.7 ! root      211: uae_u32 REGPARAM3 OpCode_GemDos(uae_u32 opcode)
1.1       root      212: {
1.1.1.7 ! root      213:        if (is_cart_pc())
        !           214:        {
        !           215:                GemDOS_OpCode();    /* handler code in gemdos.c */
        !           216:                CpuDoNOP();
        !           217:        }
        !           218:        else
        !           219:        {
        !           220:                LOG_TRACE(TRACE_OS_GEMDOS, "GEMDOS opcode invoked outside of cartridge space\n");
        !           221:                /* illegal instruction */
        !           222:                op_illg(opcode);
        !           223:                fill_prefetch();
        !           224:        }
1.1       root      225: 
1.1.1.3   root      226:        return 4 * CYCLE_UNIT / 2;
1.1       root      227: }
                    228: 
                    229: 
                    230: /**
                    231:  * This is called after completion of each VDI call
                    232:  */
1.1.1.7 ! root      233: uae_u32 REGPARAM3 OpCode_VDI(uae_u32 opcode)
1.1       root      234: {
1.1.1.4   root      235:        /* this is valid only after VDI trap, called from cartridge code */
1.1.1.7 ! root      236:        if (VDI_OldPC && is_cart_pc())
1.1.1.4   root      237:        {
                    238:                VDI_Complete();
                    239: 
                    240:                /* Set PC back to where originated from to continue instruction decoding */
                    241:                m68k_setpc(VDI_OldPC);
                    242:                VDI_OldPC = 0;
                    243:        }
                    244:        else
                    245:        {
1.1.1.7 ! root      246:                LOG_TRACE(TRACE_OS_VDI, "VDI opcode invoked outside of cartridge space\n");
1.1.1.4   root      247:                /* illegal instruction */
                    248:                op_illg(opcode);
                    249:        }
1.1       root      250: 
1.1.1.6   root      251:        fill_prefetch();
1.1.1.3   root      252:        return 4 * CYCLE_UNIT / 2;
1.1       root      253: }
1.1.1.4   root      254: 
                    255: 
                    256: /**
                    257:  * Emulator Native Features ID opcode interception.
                    258:  */
1.1.1.7 ! root      259: uae_u32 REGPARAM3 OpCode_NatFeat_ID(uae_u32 opcode)
1.1.1.4   root      260: {
                    261:        Uint32 stack = Regs[REG_A7] + SIZE_LONG;        /* skip return address */
                    262: 
1.1.1.7 ! root      263:        if (NatFeat_ID(stack, &(Regs[REG_D0])))
        !           264:        {
1.1.1.6   root      265:                CpuDoNOP ();
1.1.1.4   root      266:        }
                    267:        return 4 * CYCLE_UNIT / 2;
                    268: }
                    269: 
                    270: /**
                    271:  * Emulator Native Features call opcode interception.
                    272:  */
1.1.1.7 ! root      273: uae_u32 REGPARAM3 OpCode_NatFeat_Call(uae_u32 opcode)
1.1.1.4   root      274: {
                    275:        Uint32 stack = Regs[REG_A7] + SIZE_LONG;        /* skip return address */
                    276:        Uint16 SR = M68000_GetSR();
                    277:        bool super;
                    278: 
                    279:        super = ((SR & SR_SUPERMODE) == SR_SUPERMODE);
1.1.1.7 ! root      280:        if (NatFeat_Call(stack, super, &(Regs[REG_D0])))
        !           281:        {
1.1.1.6   root      282:                CpuDoNOP ();
1.1.1.4   root      283:        }
                    284:        return 4 * CYCLE_UNIT / 2;
                    285: }
1.1.1.6   root      286: 
                    287: 
1.1.1.7 ! root      288: TCHAR* buf_out (TCHAR *buffer, int *bufsize, const TCHAR *format, ...)
        !           289: {
        !           290:        va_list parms;
        !           291: 
        !           292:        if (buffer == NULL)
        !           293:        {
        !           294:                return NULL;
        !           295:        }
        !           296: 
        !           297:        va_start (parms, format);
        !           298:        vsnprintf (buffer, (*bufsize) - 1, format, parms);
        !           299:        va_end (parms);
        !           300:        *bufsize -= _tcslen (buffer);
1.1.1.6   root      301: 
1.1.1.7 ! root      302:        return buffer + _tcslen (buffer);
        !           303: }
        !           304: 
        !           305: void error_log(const TCHAR *format, ...)
        !           306: {
        !           307:        va_list parms;
1.1.1.6   root      308: 
1.1.1.7 ! root      309:        va_start(parms, format);
        !           310:        vfprintf(stderr, format, parms);
        !           311:        va_end(parms);
1.1.1.6   root      312: 
1.1.1.7 ! root      313:        if (format[strlen(format) - 1] != '\n')
        !           314:        {
        !           315:                fputc('\n', stderr);
        !           316:        }
1.1.1.6   root      317: }

unix.superglobalmegacorp.com

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