|
|
1.1 root 1: /*
1.1.1.3 root 2: Hatari - stMemory.c
1.1 root 3:
1.1.1.15 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.1.3 root 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.17 root 21: #include "m68000.h"
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.15 root 40: static 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:
1.1.1.17 root 61: if ( STMemory_CheckAreaType ( addr, len, ABFLAG_RAM ) )
1.1.1.13 root 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: {
1.1.1.17 root 70: if ( STMemory_CheckAreaType ( addr, 1, ABFLAG_RAM ) )
1.1.1.13 root 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.17 root 102: int screensize, limit;
103: int memtop, phystop;
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:
1.1.1.14 root 132: /* Fill in magic numbers to bypass TOS' memory tests for faster boot or
1.1.1.17 root 133: * if VDI resolution is enabled or if more than 4 MB of ram are used
134: * or if TT RAM added in Falcon mode.
1.1.1.14 root 135: * (for highest compatibility, those tests should not be bypassed in
136: * the common STF/STE cases as some programs like "Yolanda" rely on
137: * the RAM content after those tests) */
1.1.1.17 root 138: if ( ConfigureParams.System.bFastBoot
139: || bUseVDIRes
140: || ( ConfigureParams.Memory.nMemorySize > 4 && !bIsEmuTOS )
1.1.1.18! root 141: || ( Config_IsMachineTT() && ConfigureParams.System.bAddressSpace24 && !bIsEmuTOS )
! 142: || ( Config_IsMachineFalcon() && TTmemory && !bIsEmuTOS) )
1.1.1.14 root 143: {
144: /* Write magic values to sysvars to signal valid config */
145: STMemory_WriteLong(0x420, 0x752019f3); /* memvalid */
146: STMemory_WriteLong(0x43a, 0x237698aa); /* memval2 */
147: STMemory_WriteLong(0x51a, 0x5555aaaa); /* memval3 */
1.1.1.17 root 148:
149: /* If ST RAM detection is bypassed, we must also force TT RAM config if enabled */
150: if ( TTmemory )
151: STMemory_WriteLong ( 0x5a4 , 0x01000000 + TTmem_size ); /* ramtop */
152: else
153: STMemory_WriteLong ( 0x5a4 , 0 ); /* ramtop */
154: STMemory_WriteLong ( 0x5a8 , 0x1357bd13 ); /* ramvalid */
155:
156: /* On Falcon, set bit6=1 at $ff8007 to simulate a warm start */
157: /* (else memory detection is not skipped after a cold start/reset) */
1.1.1.18! root 158: if (Config_IsMachineFalcon())
1.1.1.17 root 159: STMemory_WriteByte ( 0xff8007, IoMem_ReadByte(0xff8007) | 0x40 );
160:
161: /* On TT, set bit0=1 at $ff8e09 to simulate a warm start */
162: /* (else memory detection is not skipped after a cold start/reset) */
1.1.1.18! root 163: if (Config_IsMachineTT())
1.1.1.17 root 164: STMemory_WriteByte ( 0xff8e09, IoMem_ReadByte(0xff8e09) | 0x01 );
1.1.1.14 root 165: }
1.1.1.6 root 166:
1.1.1.17 root 167: /* Set memory size, adjust for extra VDI screens if needed. */
1.1.1.7 root 168: screensize = VDIWidth * VDIHeight / 8 * VDIPlanes;
1.1.1.14 root 169: /* Use 32 kiB in normal screen mode or when the screen size is smaller than 32 kiB */
1.1.1.7 root 170: if (!bUseVDIRes || screensize < 0x8000)
171: screensize = 0x8000;
1.1.1.17 root 172: /* mem top - upper end of user memory (right before the screen memory)
173: * memtop / phystop must be dividable by 512 or TOS crashes */
1.1.1.7 root 174: memtop = (STRamEnd - screensize) & 0xfffffe00;
1.1.1.17 root 175: /* phys top - 32k gap causes least issues with apps & TOS
176: * as that's the largest _common_ screen size. EmuTOS behavior
177: * depends on machine type.
178: *
179: * TODO: what to do about _native_ TT & Videl resolutions
180: * which size is >32k? Should memtop be adapted also for
181: * those?
182: */
183: switch (ConfigureParams.System.nMachineType)
184: {
185: case MACHINE_FALCON:
186: /* TOS v4 doesn't work with VDI mode (yet), and
187: * EmuTOS works with correct gap, so use that */
188: phystop = STRamEnd;
189: break;
190: case MACHINE_TT:
191: /* For correct TOS v3 memory detection, phystop should be
192: * at the end of memory, not at memtop + 32k.
193: *
194: * However:
195: * - TOS v3 crashes/hangs if phystop-memtop gap is larger
196: * than largest real HW screen size (150k)
197: * - NVDI hangs if gap is larger than 32k in any other than
198: * monochrome mode
199: */
200: if (VDIPlanes == 1)
201: limit = 1280*960/8;
202: else
203: limit = 0x8000;
204: if (screensize > limit)
205: {
206: phystop = memtop + limit;
207: fprintf(stderr, "WARNING: too large VDI mode for TOS v3 memory detection to work correctly!\n");
208: }
209: else
210: phystop = STRamEnd;
211: break;
212: default:
213: phystop = memtop + 0x8000;
214: }
1.1.1.7 root 215: STMemory_WriteLong(0x436, memtop);
1.1.1.17 root 216: STMemory_WriteLong(0x42e, phystop);
217: if (bUseVDIRes)
218: fprintf(stderr, "VDI mode memtop: 0x%x, phystop: 0x%x (screensize: %d kB, memtop->phystop: %d kB)\n",
219: memtop, phystop, (screensize+511) / 1024, (phystop-memtop+511) / 1024);
1.1.1.6 root 220:
221: /* Set memory controller byte according to different memory sizes */
222: /* Setting per bank: %00=128k %01=512k %10=2Mb %11=reserved. - e.g. %1010 means 4Mb */
223: if (ConfigureParams.Memory.nMemorySize <= 4)
224: nMemControllerByte = MemControllerTable[ConfigureParams.Memory.nMemorySize];
225: else
226: nMemControllerByte = 0x0f;
227: STMemory_WriteByte(0x424, nMemControllerByte);
228: IoMem_WriteByte(0xff8001, nMemControllerByte);
229:
1.1.1.18! root 230: if (Config_IsMachineFalcon())
1.1.1.7 root 231: {
1.1.1.13 root 232: /* Set the Falcon memory and monitor configuration register:
233:
1.1.1.15 root 234: $ffff8006.b [R] 76543210 Monitor-memory
235: ||||||||
236: |||||||+- RAM Wait Status
237: ||||||| 0 = 1 Wait (default)
238: ||||||| 1 = 0 Wait
239: ||||||+-- Video Bus size ???
240: |||||| 0 = 16 Bit
241: |||||| 1 = 32 Bit (default)
242: ||||++--- ROM Wait Status
243: |||| 00 = Reserved
244: |||| 01 = 2 Wait (default)
245: |||| 10 = 1 Wait
246: |||| 11 = 0 Wait
247: ||++----- Falcon Memory
248: || 00 = 1 MB
249: || 01 = 4 MB
250: || 10 = 14 MB
251: || 11 = no boot !
252: ++------- Monitor-Typ
253: 00 - Monochrome (SM124)
254: 01 - Color (SC1224)
255: 10 - VGA Color
256: 11 - Television
1.1.1.13 root 257:
258: Bit 1 seems not to be well documented. It's used by TOS at bootup to compute the memory size.
259: After some tests, I get the following RAM values (Bits 5, 4, 1 are involved) :
260:
261: 00 = 512 Ko 20 = 8192 Ko
262: 02 = 1024 Ko 22 = 14366 Ko
263: 10 = 2048 Ko 30 = Illegal
264: 12 = 4096 Ko 32 = Illegal
265:
266: I use these values for Hatari's emulation.
267: I also set the bit 3 and 2 at value 01 are mentioned in the register description.
268: */
269:
270: if (ConfigureParams.Memory.nMemorySize == 14) /* 14 Meg */
271: nFalcSysCntrl = 0x26;
272: else if (ConfigureParams.Memory.nMemorySize == 8) /* 8 Meg */
273: nFalcSysCntrl = 0x24;
274: else if (ConfigureParams.Memory.nMemorySize == 4) /* 4 Meg */
1.1.1.7 root 275: nFalcSysCntrl = 0x16;
1.1.1.13 root 276: else if (ConfigureParams.Memory.nMemorySize == 2) /* 2 Meg */
1.1.1.7 root 277: nFalcSysCntrl = 0x14;
1.1.1.13 root 278: else if (ConfigureParams.Memory.nMemorySize == 1) /* 1 Meg */
1.1.1.7 root 279: nFalcSysCntrl = 0x06;
280: else
1.1.1.13 root 281: nFalcSysCntrl = 0x04; /* 512 Ko */
282:
1.1.1.8 root 283: switch(ConfigureParams.Screen.nMonitorType) {
1.1.1.13 root 284: case MONITOR_TYPE_TV:
285: nFalcSysCntrl |= FALCON_MONITOR_TV;
286: break;
287: case MONITOR_TYPE_VGA:
288: nFalcSysCntrl |= FALCON_MONITOR_VGA;
289: break;
290: case MONITOR_TYPE_RGB:
291: nFalcSysCntrl |= FALCON_MONITOR_RGB;
292: break;
293: case MONITOR_TYPE_MONO:
294: nFalcSysCntrl |= FALCON_MONITOR_MONO;
295: break;
1.1.1.7 root 296: }
297: STMemory_WriteByte(0xff8006, nFalcSysCntrl);
298: }
299:
1.1.1.6 root 300: /* Set TOS floppies */
301: STMemory_WriteWord(0x446, nBootDrive); /* Boot up on A(0) or C(2) */
302:
1.1.1.16 root 303: /* Create connected drives mask (only for harddrives, don't change floppy drive detected by TOS) */
1.1.1.10 root 304: ConnectedDriveMask = STMemory_ReadLong(0x4c2); // Get initial drive mask (see what TOS thinks)
305: if (GEMDOS_EMU_ON)
1.1.1.6 root 306: {
1.1.1.10 root 307: for (i = 0; i < MAX_HARDDRIVES; i++)
308: {
309: if (emudrives[i] != NULL) // Is this GEMDOS drive enabled?
1.1.1.12 root 310: ConnectedDriveMask |= (1 << emudrives[i]->drive_number);
1.1.1.10 root 311: }
1.1.1.6 root 312: }
313: /* Set connected drives system variable.
314: * NOTE: some TOS images overwrite this value, see 'OpCode_SysInit', too */
315: STMemory_WriteLong(0x4c2, ConnectedDriveMask);
316: }
1.1.1.17 root 317:
318:
319: /**
320: * Check that the region of 'size' starting at 'addr' is entirely inside
321: * a memory bank of the same memory type
322: */
323: bool STMemory_CheckAreaType ( Uint32 addr , int size , int mem_type )
324: {
325: addrbank *pBank;
326:
327: pBank = &get_mem_bank ( addr );
328:
329: if ( ( pBank->flags & mem_type ) == 0 )
330: {
331: fprintf(stderr, "pBank flags mismatch: 0x%x & 0x%x (RAM = 0x%x)\n", pBank->flags, mem_type, ABFLAG_RAM);
332: return false;
333: }
334:
335: return pBank->check ( addr , size );
336: }
337:
338:
339: /**
340: * Check if an address points to a memory region that causes bus error
341: * This is used for blitter and other DMA chips that should not cause
342: * a bus error when accessing such regions (on the contrary of the CPU)
343: * Returns true if region gives bus error
344: */
345: bool STMemory_CheckRegionBusError ( Uint32 addr )
346: {
347: return memory_region_bus_error ( addr );
348: }
349:
350:
351: /**
352: * Convert an address in the ST memory space to a direct pointer
353: * in the host memory.
354: *
355: * NOTE : Using this function to get a direct pointer to the memory should
356: * only be used after doing a call to valid_address or STMemory_CheckAreaType
357: * to ensure we don't try to access a non existing memory region.
358: * Basically, this function should be used only for addr in RAM or in ROM
359: */
360: void *STMemory_STAddrToPointer ( Uint32 addr )
361: {
362: Uint8 *p;
363:
364: if ( ConfigureParams.System.bAddressSpace24 == true )
365: addr &= 0x00ffffff; /* Only keep the 24 lowest bits */
366:
367: p = get_real_address ( addr );
368: return (void *)p;
369: }
370:
371:
372:
373: /**
374: * Those functions are directly accessing the memory of the corresponding
375: * bank, without calling its dedicated access handlers (they won't generate
376: * bus errors or address errors or update IO values)
377: * They are only used for internal work of the emulation, such as debugger,
378: * log to print the content of memory, intercepting gemdos/bios calls, ...
379: *
380: * These functions are not used by the CPU emulation itself, see memory.c
381: * for the functions that emulate real memory accesses.
382: */
383:
384: /**
385: * Write long/word/byte into memory.
386: * NOTE - value will be converted to 68000 endian
387: */
388: void STMemory_Write ( Uint32 addr , Uint32 val , int size )
389: {
390: addrbank *pBank;
391: Uint8 *p;
392:
393: //printf ( "mem direct write %x %x %d\n" , addr , val , size );
394: pBank = &get_mem_bank ( addr );
395:
396: if ( pBank->baseaddr == NULL )
397: return; /* No real memory, do nothing */
398:
399: addr -= pBank->start & pBank->mask;
400: addr &= pBank->mask;
401: p = pBank->baseaddr + addr;
402:
403: /* We modify the memory, so we flush the instr/data caches if needed */
404: M68000_Flush_All_Caches ( addr , size );
405:
406: if ( size == 4 )
407: do_put_mem_long ( p , val );
408: else if ( size == 2 )
409: do_put_mem_word ( p , (Uint16)val );
410: else
411: *p = (Uint8)val;
412: }
413:
414: void STMemory_WriteLong ( Uint32 addr , Uint32 val )
415: {
416: STMemory_Write ( addr , val , 4 );
417: }
418:
419: void STMemory_WriteWord ( Uint32 addr , Uint16 val )
420: {
421: STMemory_Write ( addr , (Uint32)val , 2 );
422: }
423:
424: void STMemory_WriteByte ( Uint32 addr , Uint8 val )
425: {
426: STMemory_Write ( addr , (Uint32)val , 1 );
427: }
428:
429:
430: /**
431: * Read long/word/byte from memory.
432: * NOTE - value will be converted to 68000 endian
433: */
434: Uint32 STMemory_Read ( Uint32 addr , int size )
435: {
436: addrbank *pBank;
437: Uint8 *p;
438:
439: //printf ( "mem direct read %x %d\n" , addr , size );
440: pBank = &get_mem_bank ( addr );
441:
442: if ( pBank->baseaddr == NULL )
443: return 0; /* No real memory, return 0 */
444:
445: addr -= pBank->start & pBank->mask;
446: addr &= pBank->mask;
447: p = pBank->baseaddr + addr;
448:
449: if ( size == 4 )
450: return do_get_mem_long ( p );
451: else if ( size == 2 )
452: return (Uint32)do_get_mem_word ( p );
453: else
454: return (Uint32)*p;
455: }
456:
457: Uint32 STMemory_ReadLong ( Uint32 addr )
458: {
459: return (Uint32) STMemory_Read ( addr , 4 );
460: }
461:
462: Uint16 STMemory_ReadWord ( Uint32 addr )
463: {
464: return (Uint16)STMemory_Read ( addr , 2 );
465: }
466:
467: Uint8 STMemory_ReadByte ( Uint32 addr )
468: {
469: return (Uint8)STMemory_Read ( addr , 1 );
470: }
471:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.