--- hatari/src/uae-cpu/memory.c 2019/04/01 07:13:13 1.1.1.9 +++ hatari/src/uae-cpu/memory.c 2019/04/09 08:54:38 1.1.1.17 @@ -7,37 +7,50 @@ * * Adaptation to Hatari by Thomas Huth * - * This file is distributed under the GNU Public License, version 2 or at - * your option any later version. Read the file gpl.txt for details. + * This file is distributed under the GNU General Public License, version 2 + * or at your option any later version. Read the file gpl.txt for details. */ -const char Memory_rcsid[] = "Hatari $Id: memory.c,v 1.1.1.9 2019/04/01 07:13:13 root Exp $"; +const char Memory_fileid[] = "Hatari memory.c : " __DATE__ " " __TIME__; +#include +#include "config.h" #include "sysdeps.h" #include "hatari-glue.h" #include "maccess.h" #include "memory.h" -#include "../includes/main.h" -#include "../includes/tos.h" -#include "../includes/ioMem.h" -#include "../includes/reset.h" -#include "../includes/stMemory.h" -#include "../includes/m68000.h" + +#include "main.h" +#include "tos.h" +#include "ide.h" +#include "ioMem.h" +#include "reset.h" +#include "stMemory.h" +#include "m68000.h" + #include "newcpu.h" +/* Set illegal_mem to 1 for debug output: */ +#define illegal_mem 1 + +static int illegal_count = 50; + static uae_u32 STmem_size, TTmem_size = 0; static uae_u32 TTmem_mask; #define STmem_start 0x00000000 #define ROMmem_start 0x00E00000 +#define IdeMem_start 0x00F00000 #define IOmem_start 0x00FF0000 #define TTmem_start 0x01000000 +#define IdeMem_size 65536 #define IOmem_size 65536 #define ROMmem_size (0x00FF0000 - 0x00E00000) /* So we cover both possible ROM regions + cartridge */ #define STmem_mask 0x00ffffff #define ROMmem_mask 0x00ffffff +#define IdeMem_mask (IdeMem_size - 1) #define IOmem_mask (IOmem_size - 1) @@ -80,6 +93,17 @@ static int STmem_check (uaecptr addr, ua static uae_u8 *STmem_xlate (uaecptr addr) REGPARAM; +static void print_illegal_counted(const char *txt, uaecptr addr) +{ + if (!illegal_mem || illegal_count <= 0) + return; + + write_log("%s at %08lx\n", txt, (long)addr); + if (--illegal_count == 0) + write_log("Suppressing further messages about illegal memory accesses.\n"); +} + + /* A dummy bank that only contains zeros */ static uae_u32 dummy_lget(uaecptr addr) @@ -145,53 +169,47 @@ static uae_u8 *dummy_xlate(uaecptr addr) static uae_u32 BusErrMem_lget(uaecptr addr) { - if (illegal_mem) - write_log ("Bus error lget at %08lx\n", (long)addr); + print_illegal_counted("Bus error lget", addr); - M68000_BusError(addr, 1); + M68000_BusError(addr, BUS_ERROR_READ); return 0; } static uae_u32 BusErrMem_wget(uaecptr addr) { - if (illegal_mem) - write_log ("Bus error wget at %08lx\n", (long)addr); + print_illegal_counted("Bus error wget", addr); - M68000_BusError(addr, 1); + M68000_BusError(addr, BUS_ERROR_READ); return 0; } static uae_u32 BusErrMem_bget(uaecptr addr) { - if (illegal_mem) - write_log ("Bus error bget at %08lx\n", (long)addr); + print_illegal_counted("Bus error bget", addr); - M68000_BusError(addr, 1); + M68000_BusError(addr, BUS_ERROR_READ); return 0; } static void BusErrMem_lput(uaecptr addr, uae_u32 l) { - if (illegal_mem) - write_log ("Bus error lput at %08lx\n", (long)addr); + print_illegal_counted("Bus error lput", addr); - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); } static void BusErrMem_wput(uaecptr addr, uae_u32 w) { - if (illegal_mem) - write_log ("Bus error wput at %08lx\n", (long)addr); + print_illegal_counted("Bus error wput", addr); - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); } static void BusErrMem_bput(uaecptr addr, uae_u32 b) { - if (illegal_mem) - write_log ("Bus error bput at %08lx\n", (long)addr); + print_illegal_counted("Bus error bput", addr); - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); } static int BusErrMem_check(uaecptr addr, uae_u32 size) @@ -285,7 +303,7 @@ static uae_u32 SysMem_lget(uaecptr addr) { if(addr < 0x800 && !regs.s) { - M68000_BusError(addr, 1); + M68000_BusError(addr, BUS_ERROR_READ); return 0; } @@ -299,7 +317,7 @@ static uae_u32 SysMem_wget(uaecptr addr) { if(addr < 0x800 && !regs.s) { - M68000_BusError(addr, 1); + M68000_BusError(addr, BUS_ERROR_READ); return 0; } @@ -313,7 +331,7 @@ static uae_u32 SysMem_bget(uaecptr addr) { if(addr < 0x800 && !regs.s) { - M68000_BusError(addr, 1); + M68000_BusError(addr, BUS_ERROR_READ); return 0; } @@ -326,7 +344,7 @@ static void SysMem_lput(uaecptr addr, ua { if(addr < 0x8 || (addr < 0x800 && !regs.s)) { - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); return; } @@ -340,7 +358,7 @@ static void SysMem_wput(uaecptr addr, ua { if(addr < 0x8 || (addr < 0x800 && !regs.s)) { - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); return; } @@ -354,7 +372,7 @@ static void SysMem_bput(uaecptr addr, ua { if(addr < 0x8 || (addr < 0x800 && !regs.s)) { - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); return; } @@ -368,6 +386,10 @@ static void SysMem_bput(uaecptr addr, ua * **** Void memory **** * Between the ST-RAM end and the 4 MB barrier, there is a void memory space: * Reading always returns the same value and writing does nothing at all. + * [NP] : this is not correct, reading does not always return 0, when there's + * no memory, it will return the latest data that was read on the bus. + * In many cases, this will return the word that was just read in the 68000's + * prefetch register to decode the next opcode (tested on a real STF) */ static uae_u32 VoidMem_lget(uaecptr addr) @@ -477,7 +499,7 @@ static uae_u8 *TTmem_xlate(uaecptr addr) /* **** ROM memory **** */ -static uae_u8 *ROMmemory; +uae_u8 *ROMmemory; static uae_u32 ROMmem_lget(uaecptr addr) { @@ -502,26 +524,23 @@ static uae_u32 ROMmem_bget(uaecptr addr) static void ROMmem_lput(uaecptr addr, uae_u32 b) { - if (illegal_mem) - write_log ("Illegal ROMmem lput at %08lx\n", (long)addr); + print_illegal_counted("Illegal ROMmem lput", (long)addr); - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); } static void ROMmem_wput(uaecptr addr, uae_u32 b) { - if (illegal_mem) - write_log ("Illegal ROMmem wput at %08lx\n", (long)addr); + print_illegal_counted("Illegal ROMmem wput", (long)addr); - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); } static void ROMmem_bput(uaecptr addr, uae_u32 b) { - if (illegal_mem) - write_log ("Illegal ROMmem bput at %08lx\n", (long)addr); + print_illegal_counted("Illegal ROMmem bput", (long)addr); - M68000_BusError(addr, 0); + M68000_BusError(addr, BUS_ERROR_WRITE); } static int ROMmem_check(uaecptr addr, uae_u32 size) @@ -539,10 +558,30 @@ static uae_u8 *ROMmem_xlate(uaecptr addr } +/* IDE controller IO memory */ +/* see also ide.c */ + +static uae_u8 *IdeMemory; + +static int IdeMem_check(uaecptr addr, uae_u32 size) +{ + addr -= IdeMem_start; + addr &= IdeMem_mask; + return (addr + size) <= IdeMem_size; +} + +static uae_u8 *IdeMem_xlate(uaecptr addr) +{ + addr -= IdeMem_start; + addr &= IdeMem_mask; + return IdeMemory + addr; +} + + /* Hardware IO memory */ -/* see also intercept.c */ +/* see also ioMem.c */ -static uae_u8 *IOmemory; +uae_u8 *IOmemory; static int IOmem_check(uaecptr addr, uae_u32 size) { @@ -611,6 +650,13 @@ static addrbank ROMmem_bank = ROMmem_xlate, ROMmem_check }; +static addrbank IdeMem_bank = +{ + Ide_Mem_lget, Ide_Mem_wget, Ide_Mem_bget, + Ide_Mem_lput, Ide_Mem_wput, Ide_Mem_bput, + IdeMem_xlate, IdeMem_check +}; + static addrbank IOmem_bank = { IoMem_lget, IoMem_wget, IoMem_bget, @@ -639,24 +685,41 @@ void memory_init(uae_u32 nNewSTMemSize, /*write_log("memory_init: STmem_size=$%x, TTmem_size=$%x, ROM-Start=$%x,\n", STmem_size, TTmem_size, nNewRomMemStart);*/ -/* // STmemory is currently directly defined to STRam for speed reasons! - STmemory = STRam; -*/ - ROMmemory = STRam + ROMmem_start; - IOmemory = STRam + IOmem_start; +#if ENABLE_SMALL_MEM -/* - while (! STmemory && STmem_size > 512*1024) { + /* Allocate memory for ROM areas and IO memory space (0xE00000 - 0xFFFFFF) */ + ROMmemory = malloc(2*1024*1024); + if (!ROMmemory) { + fprintf(stderr, "Out of memory (ROM/IO mem)!\n"); + SDL_Quit(); + exit(1); + } + IdeMemory = ROMmemory + 0x100000; + IOmemory = ROMmemory + 0x1f0000; + + /* Allocate memory for normal ST RAM */ + STmemory = malloc(STmem_size); + while (!STmemory && STmem_size > 512*1024) { STmem_size >>= 1; STmemory = (uae_u8 *)malloc (STmem_size); if (STmemory) - fprintf (stderr, "Reducing STmem size to %dkb\n", STmem_size >> 10); + write_log ("Reducing STmem size to %dkb\n", STmem_size >> 10); } - if (! STmemory) { + if (!STmemory) { write_log ("virtual memory exhausted (STmemory)!\n"); - abort (); + SDL_Quit(); + exit(1); } -*/ + +#else + + /* STmemory points to the 16 MiB STRam array, we just have to set up + * the remaining pointers here: */ + ROMmemory = STRam + ROMmem_start; + IdeMemory = STRam + IdeMem_start; + IOmemory = STRam + IOmem_start; + +#endif init_mem_banks(); @@ -701,8 +764,13 @@ void memory_init(uae_u32 nNewSTMemSize, /* IO memory: */ map_banks(&IOmem_bank, IOmem_start>>16, 0x1); + /* IDE controller memory region: */ + map_banks(&IdeMem_bank, IdeMem_start >> 16, 0x1); /* IDE controller on the Falcon */ + /* Illegal memory regions cause a bus error on the ST: */ - map_banks(&BusErrMem_bank, 0xF00000 >> 16, 0xA); /* IDE controller on the Falcon */ + map_banks(&BusErrMem_bank, 0xF10000 >> 16, 0x9); + + illegal_count = 50; } @@ -712,11 +780,24 @@ void memory_init(uae_u32 nNewSTMemSize, void memory_uninit (void) { /* Here, we free allocated memory from memory_init */ - if(TTmem_size > 0) - { - free(TTmemory); - TTmemory = NULL; + if (TTmem_size > 0) { + free(TTmemory); + TTmemory = NULL; } + +#if ENABLE_SMALL_MEM + + if (STmemory) { + free(STmemory); + STmemory = NULL; + } + + if (ROMmemory) { + free(ROMmemory); + ROMmemory = NULL; + } + +#endif /* ENABLE_SMALL_MEM */ } @@ -731,7 +812,7 @@ void map_banks (addrbank *bank, int star return; } /* Some ROMs apparently require a 24 bit address space... */ - if (address_space_24) + if (currprefs.address_space_24) endhioffs = 0x10000; for (hioffs = 0; hioffs < endhioffs; hioffs += 0x100) for (bnr = start; bnr < start+size; bnr++)