Annotation of hatari/src/tos.c, revision 1.1.1.8

1.1       root        1: /*
1.1.1.6   root        2:   Hatari - tos.c
1.1       root        3: 
1.1.1.6   root        4:   This file is distributed under the GNU Public License, version 2 or at
                      5:   your option any later version. Read the file gpl.txt for details.
1.1       root        6: 
1.1.1.6   root        7:   Load TOS image file into ST memory, fix/setup for emulator.
                      8: 
                      9:   The Atari ST TOS needs to be patched to help with emulation. Eg, it references
                     10:   the MMU chip to set memory size. This is patched to the sizes we need without
                     11:   the complicated emulation of hardware which is not needed (as yet). We also
                     12:   patch DMA devices and Hard Drives.
                     13:   NOTE: TOS versions 1.06 and 1.62 were not designed for use on a real STfm.
                     14:   These were for the STe machine ONLY. They access the DMA/Microwire addresses
                     15:   on boot-up which (correctly) cause a bus-error on Hatari as they would in a
                     16:   real STfm. If a user tries to select any of these images we bring up an error.
1.1       root       17: */
1.1.1.8 ! root       18: char TOS_rcsid[] = "Hatari $Id: tos.c,v 1.20 2004/04/23 15:33:59 thothy Exp $";
1.1       root       19: 
1.1.1.8 ! root       20: #include <SDL_endian.h>
1.1.1.4   root       21: 
1.1       root       22: #include "main.h"
                     23: #include "cart.h"
                     24: #include "debug.h"
                     25: #include "dialog.h"
                     26: #include "errlog.h"
                     27: #include "file.h"
                     28: #include "floppy.h"
1.1.1.4   root       29: #include "gemdos.h"
                     30: #include "hdc.h"
1.1       root       31: #include "m68000.h"
                     32: #include "memAlloc.h"
                     33: #include "memorySnapShot.h"
                     34: #include "stMemory.h"
                     35: #include "tos.h"
                     36: #include "vdi.h"
                     37: 
                     38: 
                     39: /* Settings for differnt memory sizes */
1.1.1.6   root       40: static MEMORY_INFO MemoryInfo[] =
                     41: {
                     42:   { 0x80000,  0x01, 0x00080000 },    /* MEMORYSIZE_512 */
                     43:   { 0x100000, 0x05, 0x00100000 },    /* MEMORYSIZE_1024 */
                     44:   { 0x200000, 0x02, 0x00200000 },    /* MEMORYSIZE_2MB */
                     45:   { 0x400000, 0x0A, 0x00400000 }     /* MEMORYSIZE_4MB */
1.1       root       46: };
                     47: 
1.1.1.4   root       48: /* Bit masks of connected drives(we support up to C,D,E,F,G,H) */
1.1.1.6   root       49: unsigned int ConnectedDriveMaskList[] =
                     50: {
1.1       root       51:   0x03,  /* DRIVELIST_NONE  A,B         */
                     52:   0x07,  /* DRIVELIST_C    A,B,C       */
                     53:   0x0F,  /* DRIVELIST_CD    A,B,C,D     */
                     54:   0x1F,  /* DRIVELIST_CDE  A,B,C,D,E   */
                     55:   0x3F,  /* DRIVELIST_CDEF  A,B,C,D,E,F */
1.1.1.4   root       56:   0x7F,  /* DRIVELIST_CDEFG  A,B,C,D,E,F,G */
                     57:   0xFF,  /* DRIVELIST_CDEFGH  A,B,C,D,E,F,G,H */
1.1       root       58: };
                     59: 
1.1.1.6   root       60: unsigned short int TosVersion;          /* eg, 0x0100, 0x0102 */
                     61: unsigned long TosAddress, TosSize;      /* Address in ST memory and size of TOS image */
                     62: BOOL bTosImageLoaded = FALSE;           /* Successfully loaded a TOS image? */
1.1.1.7   root       63: BOOL bRamTosImage;                      /* TRUE if we loaded a RAM TOS image */
1.1       root       64: unsigned int ConnectedDriveMask=0x03;   /* Bit mask of connected drives, eg 0x7 is A,B,C */
                     65: 
                     66: /* Possible TOS file extensions to scan for */
1.1.1.6   root       67: char *pszTosNameExts[] =
                     68: {
1.1       root       69:   ".img",
                     70:   ".rom",
                     71:   ".tos",
                     72:   NULL
                     73: };
                     74: 
                     75: 
1.1.1.6   root       76: /* Flags that define if a TOS patch should be applied */
                     77: enum
1.1       root       78: {
1.1.1.6   root       79:   TP_ALWAYS,            /* Patch should alway be applied */
                     80:   TP_HD_ON,             /* Apply patch only if HD emulation is on */
                     81:   TP_HD_OFF             /* Apply patch only if HD emulation is off */
                     82: };
1.1.1.3   root       83: 
1.1.1.6   root       84: /* This structure is used for patching the TOS ROMs */
1.1.1.7   root       85: typedef struct
1.1       root       86: {
1.1.1.6   root       87:   Uint16 Version;       /* TOS version number */
                     88:   Sint16 Country;       /* TOS country code: -1 if it does not matter, 0=US, 1=Germany, 2=France, etc. */
                     89:   char *pszName;        /* Name of the patch */
1.1.1.7   root       90:   int Flags;            /* When should the patch be applied? (see enum above) */
1.1.1.6   root       91:   Uint32 Address;       /* Where the patch should be applied */
                     92:   Uint32 OldData;       /* Expected first 4 old bytes */
                     93:   Uint32 Size;          /* Length of the patch */
1.1.1.7   root       94:   void *pNewData;       /* Pointer to the new bytes */
1.1.1.6   root       95: } TOS_PATCH;
                     96: 
                     97: static char pszHdvInit[] = "hdv_init - initialize drives";
                     98: static char pszHdvBoot[] = "hdv_boot - load boot sector";
                     99: static char pszDmaBoot[] = "boot from DMA bus";
1.1.1.8 ! root      100: //static char pszSetConDrv[] = "set connected drives mask";
        !           101: //static char pszClrConDrv[] = "clear connected drives mask";
1.1.1.6   root      102: static char pszMouse[] = "working mouse in big screen resolutions";
                    103: static char pszRomCheck[] = "ROM checksum";
                    104: static char pszNoSteHw[] = "disable STE hardware access";
                    105: 
                    106: static Uint8 pRtsOpcode[] = { 0x4E, 0x75 };  /* 0x4E75 = RTS */
                    107: static Uint8 pNopOpcodes[] = { 0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71,
                    108:         0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71,
                    109:         0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71, 0x4E, 0x71 };  /* 0x4E71 = NOP */
                    110: static Uint8 pMouseOpcode[] = { 0xD3, 0xC1 };  /* "ADDA.L D1,A1" (instead of "ADDA.W D1,A1") */
                    111: static Uint8 pRomCheckOpcode[] = { 0x60, 0x00, 0x00, 0x98 };  /* BRA $e00894 */
                    112: static Uint8 pBraOpcode[] = { 0x60 };  /* 0x60XX = BRA */
1.1       root      113: 
1.1.1.6   root      114: /* The patches for the TOS: */
                    115: static TOS_PATCH TosPatches[] =
                    116: {
                    117:   { 0x100, -1, pszHdvInit, TP_ALWAYS, 0xFC0D60, 0x4E56FFF0, 2, pRtsOpcode },
                    118:   { 0x100, -1, pszHdvBoot, TP_ALWAYS, 0xFC1384, 0x4EB900FC, 6, pNopOpcodes }, /* JSR $FC0AF8 */
                    119:   { 0x100, -1, pszDmaBoot, TP_HD_OFF, 0xFC03D6, 0x610000D0, 4, pNopOpcodes }, /* BSR $FC04A8 */
                    120: 
                    121:   { 0x102, -1, pszHdvInit, TP_ALWAYS, 0xFC0F44, 0x4E56FFF0, 2, pRtsOpcode },
                    122:   { 0x102, -1, pszHdvBoot, TP_ALWAYS, 0xFC1568, 0x4EB900FC, 6, pNopOpcodes }, /* JSR $FC0C2E */
1.1.1.7   root      123:   //{ 0x102, -1, pszClrConDrv, TP_HD_OFF, 0xFC0302, 0x42B90000, 6, pNopOpcodes }, /* CLR.L $4C2 */
1.1.1.6   root      124:   { 0x102, -1, pszDmaBoot, TP_HD_OFF, 0xFC0472, 0x610000E4, 4, pNopOpcodes }, /* BSR.W $FC0558 */
                    125:   { 0x102, 0, pszMouse, TP_ALWAYS, 0xFD0030, 0xD2C147F9, 2, pMouseOpcode },
                    126:   { 0x102, 1, pszMouse, TP_ALWAYS, 0xFD008A, 0xD2C147F9, 2, pMouseOpcode },
                    127:   { 0x102, 2, pszMouse, TP_ALWAYS, 0xFD00A8, 0xD2C147F9, 2, pMouseOpcode },
                    128:   { 0x102, 3, pszMouse, TP_ALWAYS, 0xFD0030, 0xD2C147F9, 2, pMouseOpcode },
                    129:   { 0x102, 6, pszMouse, TP_ALWAYS, 0xFCFEF0, 0xD2C147F9, 2, pMouseOpcode },
                    130:   { 0x102, 8, pszMouse, TP_ALWAYS, 0xFCFEFE, 0xD2C147F9, 2, pMouseOpcode },
                    131: 
                    132:   { 0x104, -1, pszHdvInit, TP_ALWAYS, 0xFC16BA, 0x4E56FFF0, 2, pRtsOpcode },
                    133:   { 0x104, -1, pszHdvBoot, TP_ALWAYS, 0xFC1CCE, 0x4EB900FC, 6, pNopOpcodes }, /* JSR $FC0BD8 */
1.1.1.7   root      134:   //{ 0x104, -1, pszClrConDrv, TP_HD_OFF, 0xFC02E6, 0x42AD04C2, 4, pNopOpcodes }, /* CLR.L $4C2(A5) */
1.1.1.6   root      135:   { 0x104, -1, pszDmaBoot, TP_HD_OFF, 0xFC0466, 0x610000E4, 4, pNopOpcodes }, /* BSR.W $FC054C */
                    136: 
1.1.1.7   root      137:   //{ 0x205, -1, pszClrConDrv, TP_HD_OFF, 0xE002FC, 0x42B804C2, 4, pNopOpcodes }, /* CLR.L $4C2 */
1.1.1.6   root      138:   { 0x205, -1, pszDmaBoot, TP_HD_OFF, 0xE006AE, 0x610000E4, 4, pNopOpcodes }, /* BSR.W $E00794 */
                    139:   { 0x205, 0, pszHdvInit, TP_ALWAYS, 0xE0468C, 0x4E56FFF0, 2, pRtsOpcode },
                    140:   { 0x205, 1, pszHdvInit, TP_ALWAYS, 0xE046E6, 0x4E56FFF0, 2, pRtsOpcode },
                    141:   { 0x205, 2, pszHdvInit, TP_ALWAYS, 0xE04704, 0x4E56FFF0, 2, pRtsOpcode },
                    142:   { 0x205, 4, pszHdvInit, TP_ALWAYS, 0xE04712, 0x4E56FFF0, 2, pRtsOpcode },
                    143:   { 0x205, 5, pszHdvInit, TP_ALWAYS, 0xE046F4, 0x4E56FFF0, 2, pRtsOpcode },
                    144:   { 0x205, 6, pszHdvInit, TP_ALWAYS, 0xE04704, 0x4E56FFF0, 2, pRtsOpcode },
                    145:   { 0x205, 0, pszHdvBoot, TP_ALWAYS, 0xE04CA0, 0x4EB900E0, 6, pNopOpcodes }, /* JSR $E00E8E */
                    146:   { 0x205, 1, pszHdvBoot, TP_ALWAYS, 0xE04CFA, 0x4EB900E0, 6, pNopOpcodes },
                    147:   { 0x205, 2, pszHdvBoot, TP_ALWAYS, 0xE04D18, 0x4EB900E0, 6, pNopOpcodes },
                    148:   { 0x205, 4, pszHdvBoot, TP_ALWAYS, 0xE04D26, 0x4EB900E0, 6, pNopOpcodes },
                    149:   { 0x205, 5, pszHdvBoot, TP_ALWAYS, 0xE04D08, 0x4EB900E0, 6, pNopOpcodes },
                    150:   { 0x205, 6, pszHdvBoot, TP_ALWAYS, 0xE04D18, 0x4EB900E0, 6, pNopOpcodes },
                    151:   /* An unpatched TOS 2.05 only works on STEs, so apply some anti-STE patches... */
                    152:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00096, 0x42788900, 4, pNopOpcodes }, /* CLR.W $FFFF8900 */
                    153:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE0009E, 0x31D88924, 4, pNopOpcodes }, /* MOVE.W (A0)+,$FFFF8924 */
                    154:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE000A6, 0x09D10AA9, 28, pNopOpcodes },
                    155:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE003A0, 0x30389200, 4, pNopOpcodes }, /* MOVE.W $ffff9200,D0 */
                    156:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE004EA, 0x61000CBC, 4, pNopOpcodes },
                    157:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00508, 0x61000C9E, 4, pNopOpcodes },
                    158:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE007A0, 0x631E2F3C, 1, pBraOpcode },
                    159:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00928, 0x10388901, 4, pNopOpcodes }, /* MOVE.B $FFFF8901,D0 */
                    160:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00944, 0xB0388901, 4, pNopOpcodes }, /* CMP.B $FFFF8901,D0 */
                    161:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00950, 0x67024601, 1, pBraOpcode },
                    162:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00968, 0x61000722, 4, pNopOpcodes },
                    163:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00CF2, 0x1038820D, 4, pNopOpcodes }, /* MOVE.B $FFFF820D,D0 */
                    164:   { 0x205, -1, pszNoSteHw, TP_ALWAYS, 0xE00E00, 0x1038820D, 4, pNopOpcodes }, /* MOVE.B $FFFF820D,D0 */
                    165:   { 0x205, 0, pszNoSteHw, TP_ALWAYS, 0xE03038, 0x31C0860E, 4, pNopOpcodes },
                    166:   { 0x205, 0, pszNoSteHw, TP_ALWAYS, 0xE034A8, 0x31C0860E, 4, pNopOpcodes },
                    167:   { 0x205, 0, pszNoSteHw, TP_ALWAYS, 0xE034F6, 0x31E90002, 6, pNopOpcodes },
                    168: 
                    169:   /* E007FA  MOVE.L  #$1FFFE,D7  Run checksums on 2xROMs (skip) */
                    170:   /* Checksum is total of TOS ROM image, but get incorrect results */
                    171:   /* as we've changed bytes in the ROM! So, just skip anyway! */
                    172:   { 0x206, -1, pszRomCheck, TP_ALWAYS, 0xE007FA, 0x2E3C0001, 4, pRomCheckOpcode },
1.1.1.7   root      173:   // { 0x206, -1, pszClrConDrv, TP_HD_OFF, 0xE00362, 0x42B804C2, 4, pNopOpcodes }, /* CLR.L $4C2 */
1.1.1.6   root      174:   { 0x206, -1, pszDmaBoot, TP_HD_OFF, 0xE00898, 0x610000E0, 4, pNopOpcodes }, /* BSR.W $E0097A */
                    175:   { 0x206, 0, pszHdvInit, TP_ALWAYS, 0xE0518E, 0x4E56FFF0, 2, pRtsOpcode },
                    176:   { 0x206, 1, pszHdvInit, TP_ALWAYS, 0xE051E8, 0x4E56FFF0, 2, pRtsOpcode },
                    177:   { 0x206, 2, pszHdvInit, TP_ALWAYS, 0xE05206, 0x4E56FFF0, 2, pRtsOpcode },
                    178:   { 0x206, 3, pszHdvInit, TP_ALWAYS, 0xE0518E, 0x4E56FFF0, 2, pRtsOpcode },
                    179:   { 0x206, 6, pszHdvInit, TP_ALWAYS, 0xE05206, 0x4E56FFF0, 2, pRtsOpcode },
                    180:   { 0x206, 8, pszHdvInit, TP_ALWAYS, 0xE05214, 0x4E56FFF0, 2, pRtsOpcode },
                    181:   { 0x206, 0, pszHdvBoot, TP_ALWAYS, 0xE05944, 0x4EB900E0, 6, pNopOpcodes }, /* JSR  $E011DC */
                    182:   { 0x206, 1, pszHdvBoot, TP_ALWAYS, 0xE0599E, 0x4EB900E0, 6, pNopOpcodes },
                    183:   { 0x206, 2, pszHdvBoot, TP_ALWAYS, 0xE059BC, 0x4EB900E0, 6, pNopOpcodes },
                    184:   { 0x206, 3, pszHdvBoot, TP_ALWAYS, 0xE05944, 0x4EB900E0, 6, pNopOpcodes },
                    185:   { 0x206, 6, pszHdvBoot, TP_ALWAYS, 0xE059BC, 0x4EB900E0, 6, pNopOpcodes },
                    186:   { 0x206, 8, pszHdvBoot, TP_ALWAYS, 0xE059CA, 0x4EB900E0, 6, pNopOpcodes },
1.1       root      187: 
1.1.1.6   root      188:   { 0, 0, NULL, 0, 0, 0, 0, NULL }
                    189: };
1.1       root      190: 
                    191: 
                    192: 
1.1.1.6   root      193: /*-----------------------------------------------------------------------*/
                    194: /*
                    195:   Save/Restore snapshot of local variables ('MemorySnapShot_Store' handles type)
                    196: */
                    197: void TOS_MemorySnapShot_Capture(BOOL bSave)
                    198: {
                    199:   /* Save/Restore details */
                    200:   MemorySnapShot_Store(&TosVersion, sizeof(TosVersion));
                    201:   MemorySnapShot_Store(&TosAddress, sizeof(TosAddress));
                    202:   MemorySnapShot_Store(&TosSize, sizeof(TosSize));
                    203:   MemorySnapShot_Store(&ConnectedDriveMask, sizeof(ConnectedDriveMask));
1.1       root      204: }
                    205: 
1.1.1.3   root      206: 
                    207: /*-----------------------------------------------------------------------*/
1.1       root      208: /*
1.1.1.4   root      209:   Patch TOS to skip some TOS setup code which we don't support/need.
1.1       root      210: 
                    211:   So, how do we find these addresses when we have no commented source code?
1.1.1.6   root      212:   - Hdv_init: Scan start of TOS for table of move.l <addr>,$46A(a5), around 0x224 bytes in
                    213:     and look at the first entry - that's the hdv_init address.
                    214:   - Hdv_boot: Scan start of TOS for table of move.l <addr>,$47A(a5), and look for 5th entry,
                    215:     that's the hdv_boot address. The function starts with link,movem,jsr.
                    216:   - Boot from DMA bus: again scan at start of rom for tst.w $482, boot call will be just above it.
                    217:   - Clear connected drives: search for 'clr.w' and '$4c2' to find, may use (a5) in which case op-code
                    218:     is only 4 bytes and also note this is only do on TOS > 1.00
                    219: 
                    220:   If we use hard disk emulation, we also need to force set condrv ($4c2),
                    221:   because the ACSI driver (if any) will reset it. This is done after the DMA
                    222:   bus boot (when the driver loads), replacing the RTS with our own routine which
                    223:   sets condrv and then RTSes.
1.1       root      224: */
1.1.1.6   root      225: static void TOS_FixRom(void)
1.1       root      226: {
1.1.1.6   root      227:   int nGoodPatches, nBadPatches;
                    228:   short TosCountry;
                    229:   BOOL bHdIsOn;
                    230:   TOS_PATCH *pPatch;
1.1       root      231: 
1.1.1.6   root      232:   /* Check for EmuTOS first since we can not patch it */
                    233:   if(STMemory_ReadLong(TosAddress+0x2c) == 0x45544F53)      /* 0x45544F53 = 'ETOS' */
                    234:   {
                    235:     fprintf(stderr, "Detected EmuTOS, skipping TOS patches.\n");
                    236:     return;
                    237:   }
1.1       root      238: 
1.1.1.7   root      239:   /* We also can't patch RAM TOS images (yet) */
                    240:   if(bRamTosImage)
                    241:   {
                    242:     fprintf(stderr, "RAM TOS image --> skipping TOS patches.\n");
                    243:     return;
                    244:   }
                    245: 
1.1.1.6   root      246:   nGoodPatches = nBadPatches = 0;
                    247:   TosCountry = STMemory_ReadWord(TosAddress+28)>>1;   /* TOS country code */
                    248:   bHdIsOn = (ACSI_EMU_ON || GEMDOS_EMU_ON);
                    249:   pPatch = TosPatches;
1.1       root      250: 
1.1.1.6   root      251:   /* Apply TOS patches: */
                    252:   while(pPatch->Version)
                    253:   {
1.1.1.7   root      254:     /* Only apply patches that suit to the actual TOS  version: */
1.1.1.6   root      255:     if(pPatch->Version == TosVersion
                    256:        && (pPatch->Country == TosCountry || pPatch->Country == -1))
                    257:     {
                    258:       /* Make sure that we really patch the right place by comparing data: */
                    259:       if(STMemory_ReadLong(pPatch->Address) == pPatch->OldData)
1.1.1.5   root      260:       {
1.1.1.6   root      261:         /* Only apply the patch if it is really needed: */
                    262:         if(pPatch->Flags == TP_ALWAYS || (pPatch->Flags == TP_HD_ON && bHdIsOn)
                    263:            || (pPatch->Flags == TP_HD_OFF && !bHdIsOn))
1.1.1.5   root      264:         {
1.1.1.6   root      265:           /* Now we can really apply the patch! */
                    266:           /*fprintf(stderr, "Applying TOS patch '%s'.\n", pPatch->pszName);*/
                    267:           memcpy(&STRam[pPatch->Address], pPatch->pNewData, pPatch->Size);
1.1.1.5   root      268:         }
1.1.1.6   root      269:         else
1.1.1.5   root      270:         {
1.1.1.6   root      271:           /*fprintf(stderr, "Skipped patch '%s'.\n", pPatch->pszName);*/
1.1.1.5   root      272:         }
1.1.1.6   root      273:         nGoodPatches += 1;
1.1.1.5   root      274:       }
                    275:       else
                    276:       {
1.1.1.8 ! root      277:         fprintf(stderr, "Failed to apply TOS patch '%s' at %x (expected %x, found %x).\n",
1.1.1.7   root      278:                 pPatch->pszName, pPatch->Address, pPatch->OldData, STMemory_ReadLong(pPatch->Address));
1.1.1.6   root      279:         nBadPatches += 1;
1.1.1.5   root      280:       }
1.1.1.6   root      281:     }
                    282:     pPatch += 1;
                    283:   }
1.1       root      284: 
1.1.1.6   root      285:   fprintf(stderr, "Applied %i TOS patches, %i patches failed.\n",
                    286:           nGoodPatches, nBadPatches);
1.1       root      287: 
1.1.1.6   root      288:   /* Modify assembler loaded into cartridge area */
                    289:   switch(TosVersion)
                    290:   {
                    291:     case 0x0100:  Cart_WriteHdvAddress(0x167A);  break;
                    292:     case 0x0102:  Cart_WriteHdvAddress(0x16DA);  break;
                    293:     case 0x0104:  Cart_WriteHdvAddress(0x181C);  break;
                    294:     case 0x0205:  Cart_WriteHdvAddress(0x1410);  break;
                    295:     case 0x0206:  Cart_WriteHdvAddress(0x1644);  break;
1.1       root      296:   }
1.1.1.6   root      297: 
1.1       root      298: }
                    299: 
1.1.1.3   root      300: 
                    301: /*-----------------------------------------------------------------------*/
1.1       root      302: /*
                    303:   Set default memory configuration, connected floppies and memory size
                    304: */
1.1.1.6   root      305: static void TOS_SetDefaultMemoryConfig(void)
1.1       root      306: {
1.1.1.3   root      307:   /* As TOS checks hardware for memory size + connected devices on boot-up */
                    308:   /* we set these values ourselves and fill in the magic numbers so TOS */
                    309:   /* skips these tests which would crash the emulator as the reference the MMU */
                    310: 
                    311:   /* Fill in magic numbers, so TOS does not try to reference MMU */
1.1.1.6   root      312:   STMemory_WriteLong(0x420, 0x752019f3);        /* memvalid - configuration is valid */
                    313:   STMemory_WriteLong(0x43a, 0x237698aa);        /* another magic # */
                    314:   STMemory_WriteLong(0x51a, 0x5555aaaa);        /* and another */
1.1       root      315: 
1.1.1.3   root      316:   /* Set memory size, adjust for extra VDI screens if needed */
1.1.1.5   root      317:   if (bUseVDIRes)
                    318:   {
1.1.1.3   root      319:     /* This is enough for 1024x768x16colour (0x60000) */
1.1.1.6   root      320:     STMemory_WriteLong(0x436, MemoryInfo[ConfigureParams.Memory.nMemorySize].PhysTop-0x60000);  /* mem top - upper end of user memory (before 32k screen) */
                    321:     STMemory_WriteLong(0x42e, MemoryInfo[ConfigureParams.Memory.nMemorySize].PhysTop-0x58000);  /* phys top */
1.1       root      322:   }
1.1.1.5   root      323:   else
                    324:   {
1.1.1.6   root      325:     STMemory_WriteLong(0x436, MemoryInfo[ConfigureParams.Memory.nMemorySize].PhysTop-0x8000);   /* mem top - upper end of user memory(before 32k screen) */
                    326:     STMemory_WriteLong(0x42e, MemoryInfo[ConfigureParams.Memory.nMemorySize].PhysTop);          /* phys top */
1.1       root      327:   }
1.1.1.6   root      328:   STMemory_WriteByte(0x424, MemoryInfo[ConfigureParams.Memory.nMemorySize].MemoryConfig);
                    329:   STMemory_WriteByte(0xff8001, MemoryInfo[ConfigureParams.Memory.nMemorySize].MemoryConfig);
1.1       root      330: 
1.1.1.7   root      331:   /* Set memory range */
1.1.1.3   root      332:   STRamEnd = MemoryInfo[ConfigureParams.Memory.nMemorySize].MemoryEnd;  /* Set end of RAM */
                    333: 
                    334:   /* Set TOS floppies */
1.1.1.6   root      335:   STMemory_WriteWord(0x446, nBootDrive);          /* Boot up on A(0) or C(2) */
                    336:   STMemory_WriteWord(0x4a6, 0x2);                 /* Connected floppies A,B (0 or 2) */
1.1.1.4   root      337: 
1.1.1.3   root      338:   ConnectedDriveMask = ConnectedDriveMaskList[ConfigureParams.HardDisc.nDriveList];
1.1.1.4   root      339: 
1.1.1.6   root      340:   STMemory_WriteLong(0x4c2, ConnectedDriveMask);  /* Drives A,B and C - NOTE some TOS images overwrite value, see 'TOS_ConnectedDrive_OpCode' */
1.1       root      341: 
1.1.1.3   root      342:   /* Mirror ROM boot vectors */
1.1.1.6   root      343:   STMemory_WriteLong(0x00, STMemory_ReadLong(TosAddress));
                    344:   STMemory_WriteLong(0x04, STMemory_ReadLong(TosAddress+4));
1.1.1.7   root      345: 
                    346:   /* Initialize the memory banks: */
                    347:   memory_uninit();
                    348:   memory_init(STRamEnd, 0, TosAddress);
1.1.1.6   root      349: }
                    350: 
                    351: 
                    352: /*-----------------------------------------------------------------------*/
                    353: /*
                    354:   Load TOS Rom image file into ST memory space and fix image so can emulate correctly
                    355:   Pre TOS 1.06 are loaded at 0xFC0000 with later ones at 0xE00000
                    356: */
                    357: int TOS_LoadImage(void)
                    358: {
                    359:   void *pTosFile = NULL;
                    360: 
                    361:   bTosImageLoaded = FALSE;
                    362: 
                    363:   /* Load TOS image into memory so we can check it's vesion */
                    364:   TosVersion = 0;
                    365:   pTosFile = File_Read(ConfigureParams.TOSGEM.szTOSImageFileName, NULL, NULL, pszTosNameExts);
                    366:   TosSize = File_Length(ConfigureParams.TOSGEM.szTOSImageFileName);
                    367: 
                    368:   if(pTosFile && TosSize>0)
                    369:   {
1.1.1.7   root      370:     /* Check for RAM TOS images first: */
1.1.1.8 ! root      371:     if(SDL_SwapBE32(*(Uint32 *)pTosFile) == 0x46FC2700)
1.1.1.7   root      372:     {
                    373:       fprintf(stderr, "Warning: Detected a RAM TOS - this will probably not work very well!\n");
                    374:       /* RAM TOS images have a 256 bytes loader function before the real image
                    375:        * starts, so we simply skip the first 256 bytes here: */
                    376:       TosSize -= 0x100;
                    377:       memmove(pTosFile, pTosFile + 0x100, TosSize);
                    378:       bRamTosImage = TRUE;
                    379:     }
                    380:     else
                    381:     {
                    382:       bRamTosImage = FALSE;
                    383:     }
                    384: 
1.1.1.6   root      385:     /* Now, look at start of image to find Version number and address */
1.1.1.8 ! root      386:     TosVersion = SDL_SwapBE16(*(Uint16 *)((Uint32)pTosFile+2));
        !           387:     TosAddress = SDL_SwapBE32(*(Uint32 *)((Uint32)pTosFile+8));
1.1.1.6   root      388: 
                    389:     /* Check for reasonable TOS version: */
                    390:     if(TosVersion<0x100 || TosVersion>0x500 || TosSize>1024*1024L
1.1.1.7   root      391:        || (!bRamTosImage && TosAddress!=0xe00000 && TosAddress!=0xfc0000))
1.1.1.6   root      392:     {
                    393:       Main_Message("Your TOS seems not to be a valid TOS ROM file!\n", PROG_NAME);
1.1.1.7   root      394:       fprintf(stderr,"(TOS version %x, address %lx)\n", TosVersion, TosAddress);
1.1.1.6   root      395:       return -2;
                    396:     }
                    397: 
                    398:     /* TOSes 1.06 and 1.62 are for the STe ONLY and so don't run on a real STfm. */
                    399:     /* They access illegal memory addresses which don't exist on a real machine and cause the OS */
                    400:     /* to lock up. So, if user selects one of these, show an error */
                    401:     if(TosVersion==0x0106 || TosVersion==0x0162)
                    402:     {
                    403:       Main_Message("TOS versions 1.06 and 1.62 are NOT valid STfm images.\n\n"
                    404:                    "These were only designed for use on the STe range of machines.\n", PROG_NAME /*,MB_OK|MB_ICONINFORMATION*/);
                    405:       return -3;
                    406:     }
                    407: 
                    408:     /* Copy loaded image into ST memory */
                    409:     memcpy((void *)((unsigned long)STRam+TosAddress), pTosFile, TosSize);
                    410:   }
                    411:   else
                    412:   {
                    413:     char err_txt[256];
                    414:     strcpy(err_txt, "Can not load TOS file:\n ");
                    415:     strncat(err_txt, ConfigureParams.TOSGEM.szTOSImageFileName, 256-32);
                    416:     strcat(err_txt, "\n");
                    417:     Main_Message(err_txt, PROG_NAME);
                    418:     return -1;
                    419:   }
                    420: 
                    421:   fprintf(stderr, "Loaded TOS version %i.%c%c, starting at $%lx, "
                    422:           "country code = %i, %s\n", TosVersion>>8, '0'+((TosVersion>>4)&0x0f),
                    423:           '0'+(TosVersion&0x0f), TosAddress, STMemory_ReadWord(TosAddress+28)>>1,
                    424:           (STMemory_ReadWord(TosAddress+28)&1)?"PAL":"NTSC");
                    425: 
                    426:   /* Are we allowed VDI under this TOS? */
                    427:   if(TosVersion == 0x0100 && bUseVDIRes)
                    428:   {
                    429:     /* Warn user (exit if need to) */
                    430:     Main_Message("To use GEM extended resolutions, you must select a TOS >= 1.02.",
                    431:                  PROG_NAME /*,MB_OK|MB_ICONINFORMATION*/);
                    432:     /* And select non VDI */
                    433:     bUseVDIRes = ConfigureParams.TOSGEM.bUseExtGEMResolutions = FALSE;
                    434:   }
                    435: 
                    436:   /* Fix TOS image, modify code for emulation */
                    437:   TOS_FixRom();
                    438: 
                    439:   /* Set connected devices, memory configuration */
                    440:   TOS_SetDefaultMemoryConfig();
                    441: 
                    442:   /* and free loaded image */
                    443:   Memory_Free(pTosFile);
                    444: 
                    445:   bTosImageLoaded = TRUE;
                    446:   return 0;
1.1       root      447: }

unix.superglobalmegacorp.com

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