Annotation of hatari/src/stMemory.c, revision 1.1.1.13

1.1       root        1: /*
1.1.1.3   root        2:   Hatari - stMemory.c
1.1       root        3: 
1.1.1.3   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.
                      6: 
1.1.1.4   root        7:   ST Memory access functions.
1.1       root        8: */
1.1.1.10  root        9: const char STMemory_fileid[] = "Hatari stMemory.c : " __DATE__ " " __TIME__;
1.1       root       10: 
1.1.1.4   root       11: #include "stMemory.h"
1.1.1.6   root       12: #include "configuration.h"
                     13: #include "floppy.h"
1.1.1.10  root       14: #include "gemdos.h"
1.1.1.12  root       15: #include "ioMem.h"
1.1.1.10  root       16: #include "log.h"
1.1.1.7   root       17: #include "memory.h"
1.1.1.12  root       18: #include "memorySnapShot.h"
                     19: #include "tos.h"
                     20: #include "vdi.h"
1.1.1.7   root       21: 
1.1.1.2   root       22: 
1.1.1.7   root       23: /* STRam points to our ST Ram. Unless the user enabled SMALL_MEM where we have
                     24:  * to save memory, this includes all TOS ROM and IO hardware areas for ease
                     25:  * and emulation speed - so we create a 16 MiB array directly here.
                     26:  * But when the user turned on ENABLE_SMALL_MEM, this only points to a malloc'ed
                     27:  * buffer with the ST RAM; the ROM and IO memory will be handled separately. */
                     28: #if ENABLE_SMALL_MEM
                     29: Uint8 *STRam;
                     30: #else
                     31: Uint8 STRam[16*1024*1024];
                     32: #endif
1.1.1.4   root       33: 
1.1.1.7   root       34: Uint32 STRamEnd;            /* End of ST Ram, above this address is no-mans-land and ROM/IO memory */
1.1       root       35: 
1.1.1.2   root       36: 
1.1.1.7   root       37: /**
                     38:  * Clear section of ST's memory space.
                     39:  */
1.1.1.5   root       40: void STMemory_Clear(Uint32 StartAddress, Uint32 EndAddress)
1.1       root       41: {
1.1.1.5   root       42:        memset(&STRam[StartAddress], 0, EndAddress-StartAddress);
1.1       root       43: }
                     44: 
1.1.1.13! root       45: /**
        !            46:  * Copy given memory area safely to Atari RAM.
        !            47:  * If the memory area isn't fully within RAM, only the valid parts are written.
        !            48:  * Useful for all kinds of IO operations.
        !            49:  * 
        !            50:  * addr - destination Atari RAM address
        !            51:  * src - source Hatari memory address
        !            52:  * len - number of bytes to copy
        !            53:  * name - name / description if this memory copy for error messages
        !            54:  * 
        !            55:  * Return true if whole copy was safe / valid.
        !            56:  */
        !            57: bool STMemory_SafeCopy(Uint32 addr, Uint8 *src, unsigned int len, const char *name)
        !            58: {
        !            59:        Uint32 end;
        !            60: 
        !            61:        if (STMemory_ValidArea(addr, len))
        !            62:        {
        !            63:                memcpy(&STRam[addr], src, len);
        !            64:                return true;
        !            65:        }
        !            66:        Log_Printf(LOG_WARN, "Invalid '%s' RAM range 0x%x+%i!\n", name, addr, len);
        !            67: 
        !            68:        for (end = addr + len; addr < end; addr++, src++)
        !            69:        {
        !            70:                if (STMemory_ValidArea(addr, 1))
        !            71:                        STRam[addr] = *src;
        !            72:        }
        !            73:        return false;
        !            74: }
1.1.1.6   root       75: 
1.1.1.12  root       76: /**
                     77:  * Save/Restore snapshot of RAM / ROM variables
                     78:  * ('MemorySnapShot_Store' handles type)
                     79:  */
                     80: void STMemory_MemorySnapShot_Capture(bool bSave)
                     81: {
                     82:        MemorySnapShot_Store(&STRamEnd, sizeof(STRamEnd));
                     83: 
                     84:        /* Only save/restore area of memory machine is set to, eg 1Mb */
                     85:        MemorySnapShot_Store(STRam, STRamEnd);
                     86: 
                     87:        /* And Cart/TOS/Hardware area */
                     88:        MemorySnapShot_Store(&RomMem[0xE00000], 0x200000);
                     89: }
                     90: 
                     91: 
1.1.1.7   root       92: /**
                     93:  * Set default memory configuration, connected floppies, memory size and
                     94:  * clear the ST-RAM area.
                     95:  * As TOS checks hardware for memory size + connected devices on boot-up
                     96:  * we set these values ourselves and fill in the magic numbers so TOS
                     97:  * skips these tests.
                     98:  */
1.1.1.6   root       99: void STMemory_SetDefaultConfig(void)
                    100: {
                    101:        int i;
1.1.1.7   root      102:        int screensize;
                    103:        int memtop;
1.1.1.6   root      104:        Uint8 nMemControllerByte;
1.1.1.13! root      105:        Uint8 nFalcSysCntrl;
        !           106: 
1.1.1.6   root      107:        static const int MemControllerTable[] =
                    108:        {
                    109:                0x01,   /* 512 KiB */
                    110:                0x05,   /* 1 MiB */
                    111:                0x02,   /* 2 MiB */
                    112:                0x06,   /* 2.5 MiB */
                    113:                0x0A    /* 4 MiB */
                    114:        };
                    115: 
1.1.1.7   root      116:        if (bRamTosImage)
                    117:        {
                    118:                /* Clear ST-RAM, excluding the RAM TOS image */
                    119:                STMemory_Clear(0x00000000, TosAddress);
                    120:                STMemory_Clear(TosAddress+TosSize, STRamEnd);
                    121:        }
1.1.1.6   root      122:        else
1.1.1.7   root      123:        {
                    124:                /* Clear whole ST-RAM */
                    125:                STMemory_Clear(0x00000000, STRamEnd);
                    126:        }
1.1.1.6   root      127: 
                    128:        /* Mirror ROM boot vectors */
                    129:        STMemory_WriteLong(0x00, STMemory_ReadLong(TosAddress));
                    130:        STMemory_WriteLong(0x04, STMemory_ReadLong(TosAddress+4));
                    131: 
                    132:        /* Fill in magic numbers, so TOS does not try to reference MMU */
                    133:        STMemory_WriteLong(0x420, 0x752019f3);            /* memvalid - configuration is valid */
                    134:        STMemory_WriteLong(0x43a, 0x237698aa);            /* another magic # */
                    135:        STMemory_WriteLong(0x51a, 0x5555aaaa);            /* and another */
                    136: 
1.1.1.7   root      137:        /* Set memory size, adjust for extra VDI screens if needed.
                    138:         * Note: TOS seems to set phys_top-0x8000 as the screen base
                    139:         * address - so we have to move phys_top down in VDI resolution
                    140:         * mode, although there is more "physical" ST RAM available. */
                    141:        screensize = VDIWidth * VDIHeight / 8 * VDIPlanes;
                    142:         /* Use 32 kiB in normal screen mode or when the screen size is smaller than 32 kiB */
                    143:        if (!bUseVDIRes || screensize < 0x8000)
                    144:                screensize = 0x8000;
                    145:        /* mem top - upper end of user memory (right before the screen memory).
                    146:         * Note: memtop / phystop must be dividable by 512, or TOS crashes */
                    147:        memtop = (STRamEnd - screensize) & 0xfffffe00;
                    148:        STMemory_WriteLong(0x436, memtop);
                    149:        /* phys top - This must be memtop + 0x8000 to make TOS happy */
                    150:        STMemory_WriteLong(0x42e, memtop+0x8000);
1.1.1.6   root      151: 
                    152:        /* Set memory controller byte according to different memory sizes */
                    153:        /* Setting per bank: %00=128k %01=512k %10=2Mb %11=reserved. - e.g. %1010 means 4Mb */
                    154:        if (ConfigureParams.Memory.nMemorySize <= 4)
                    155:                nMemControllerByte = MemControllerTable[ConfigureParams.Memory.nMemorySize];
                    156:        else
                    157:                nMemControllerByte = 0x0f;
                    158:        STMemory_WriteByte(0x424, nMemControllerByte);
                    159:        IoMem_WriteByte(0xff8001, nMemControllerByte);
                    160: 
1.1.1.7   root      161:        if (ConfigureParams.System.nMachineType == MACHINE_FALCON)
                    162:        {
1.1.1.13! root      163:                /* Set the Falcon memory and monitor configuration register:
        !           164: 
        !           165:          $ffff8006.b [R]  76543210  Monitor-memory
        !           166:                           ||||||||
        !           167:                           |||||||+- RAM Wait Status
        !           168:                           |||||||   0 =  1 Wait (default)
        !           169:                           |||||||   1 =  0 Wait
        !           170:                           ||||||+-- Video Bus size ???
        !           171:                           ||||||    0 = 16 Bit
        !           172:                           ||||||    1 = 32 Bit (default)
        !           173:                           ||||++--- ROM Wait Status
        !           174:                           ||||      00 = Reserved
        !           175:                           ||||      01 =  2 Wait (default)
        !           176:                           ||||      10 =  1 Wait
        !           177:                           ||||      11 =  0 Wait
        !           178:                           ||++----- Falcon Memory
        !           179:                           ||        00 =  1 MB
        !           180:                           ||        01 =  4 MB
        !           181:                           ||        10 = 14 MB
        !           182:                           ||        11 = no boot !
        !           183:                           ++------- Monitor-Typ
        !           184:                                     00 - Monochrome (SM124)
        !           185:                                     01 - Color (SC1224)
        !           186:                                     10 - VGA Color
        !           187:                                     11 - Television
        !           188: 
        !           189:                Bit 1 seems not to be well documented. It's used by TOS at bootup to compute the memory size.
        !           190:                After some tests, I get the following RAM values (Bits 5, 4, 1 are involved) :
        !           191: 
        !           192:                00 =  512 Ko    20 = 8192 Ko
        !           193:                02 = 1024 Ko    22 = 14366 Ko
        !           194:                10 = 2048 Ko    30 = Illegal
        !           195:                12 = 4096 Ko    32 = Illegal
        !           196: 
        !           197:                I use these values for Hatari's emulation.
        !           198:                I also set the bit 3 and 2 at value 01 are mentioned in the register description.
        !           199:                */
        !           200: 
        !           201:                if (ConfigureParams.Memory.nMemorySize == 14)     /* 14 Meg */
        !           202:                        nFalcSysCntrl = 0x26;
        !           203:                else if (ConfigureParams.Memory.nMemorySize == 8) /* 8 Meg */
        !           204:                        nFalcSysCntrl = 0x24;
        !           205:                else if (ConfigureParams.Memory.nMemorySize == 4) /* 4 Meg */
1.1.1.7   root      206:                        nFalcSysCntrl = 0x16;
1.1.1.13! root      207:                else if (ConfigureParams.Memory.nMemorySize == 2) /* 2 Meg */
1.1.1.7   root      208:                        nFalcSysCntrl = 0x14;
1.1.1.13! root      209:                else if (ConfigureParams.Memory.nMemorySize == 1) /* 1 Meg */
1.1.1.7   root      210:                        nFalcSysCntrl = 0x06;
                    211:                else
1.1.1.13! root      212:                        nFalcSysCntrl = 0x04;                     /* 512 Ko */
        !           213: 
1.1.1.8   root      214:                switch(ConfigureParams.Screen.nMonitorType) {
1.1.1.13! root      215:                        case MONITOR_TYPE_TV:
        !           216:                                nFalcSysCntrl |= FALCON_MONITOR_TV;
        !           217:                                break;
        !           218:                        case MONITOR_TYPE_VGA:
        !           219:                                nFalcSysCntrl |= FALCON_MONITOR_VGA;
        !           220:                                break;
        !           221:                        case MONITOR_TYPE_RGB:
        !           222:                                nFalcSysCntrl |= FALCON_MONITOR_RGB;
        !           223:                                break;
        !           224:                        case MONITOR_TYPE_MONO:
        !           225:                                nFalcSysCntrl |= FALCON_MONITOR_MONO;
        !           226:                                break;
1.1.1.7   root      227:                }
                    228:                STMemory_WriteByte(0xff8006, nFalcSysCntrl);
                    229:        }
                    230: 
1.1.1.6   root      231:        /* Set TOS floppies */
                    232:        STMemory_WriteWord(0x446, nBootDrive);          /* Boot up on A(0) or C(2) */
                    233: 
                    234:        /* Create connected drives mask: */
1.1.1.10  root      235:        ConnectedDriveMask = STMemory_ReadLong(0x4c2);  // Get initial drive mask (see what TOS thinks)
                    236:        ConnectedDriveMask |= 0x03;                     // Always use A: and B:
                    237:        if (GEMDOS_EMU_ON)
1.1.1.6   root      238:        {
1.1.1.10  root      239:                for (i = 0; i < MAX_HARDDRIVES; i++)
                    240:                {
                    241:                        if (emudrives[i] != NULL)     // Is this GEMDOS drive enabled?
1.1.1.12  root      242:                                ConnectedDriveMask |= (1 << emudrives[i]->drive_number);
1.1.1.10  root      243:                }
1.1.1.6   root      244:        }
                    245:        /* Set connected drives system variable.
                    246:         * NOTE: some TOS images overwrite this value, see 'OpCode_SysInit', too */
                    247:        STMemory_WriteLong(0x4c2, ConnectedDriveMask);
                    248: }

unix.superglobalmegacorp.com

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