code cleanup/review

This commit is contained in:
dborth 2008-10-18 19:27:43 +00:00
parent bca1afc555
commit 1d9678fac8
5 changed files with 256 additions and 352 deletions

View File

@ -552,11 +552,8 @@ void systemDrawScreen()
extern bool gbUpdateSizes(); extern bool gbUpdateSizes();
bool LoadGBROM(int method) bool LoadGBROM(int method)
{ {
// cleanup GB memory
if(gbRom != NULL)
gbCleanUp();
gbRom = (u8 *)malloc(1024*1024*4); // allocate 4 MB to GB ROM gbRom = (u8 *)malloc(1024*1024*4); // allocate 4 MB to GB ROM
bios = (u8 *)calloc(1,0x100);
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED; systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
@ -618,6 +615,9 @@ bool LoadVBAROM(int method)
srcHeight = 0; srcHeight = 0;
srcPitch = 0; srcPitch = 0;
VMClose(); // cleanup GBA memory
gbCleanUp(); // cleanup GB memory
switch( type ) switch( type )
{ {
case 2: case 2:

View File

@ -8,7 +8,6 @@
* GameBoy Advance Virtual Memory Paging * GameBoy Advance Virtual Memory Paging
***************************************************************************/ ***************************************************************************/
#ifdef HW_RVL
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -39,25 +38,99 @@ extern "C" {
unsigned int MEM2Storage = 0x91000000; unsigned int MEM2Storage = 0x91000000;
static char *gbabase = NULL;
static u32 GBAROMSize = 0; static u32 GBAROMSize = 0;
/** #ifdef USE_VM
* GBA Memory extern u32 loadtimeradjust;
*/
#define WORKRAM 0x40000 /** Setup VM to use small 16kb windows **/
#define BIOS 0x4000 #define VMSHIFTBITS 14
#define INTERNALRAM 0x8000 #define VMSHIFTMASK 0x3FFF
#define PALETTERAM 0x400 #define MAXGBAROM ( 32 * 1024 * 1024 )
#define VRAM 0x20000 #define MAXROM (4 * 1024 * 1024)
#define OAM 0x400 #define MAXVMPAGE ( MAXGBAROM >> VMSHIFTBITS )
#define PIX (4 * 241 * 162) #define MAXVMMASK ( ( MAXROM >> VMSHIFTBITS ) - 1 )
#define IOMEM 0x400
#define GBATOTAL (WORKRAM + BIOS + INTERNALRAM + PALETTERAM + \ typedef struct
VRAM + OAM + PIX + IOMEM ) {
char *pageptr;
int pagetype;
int pageno;
}
VMPAGE;
static VMPAGE vmpage[MAXVMPAGE];
static int vmpageno = 0;
static FILE* romfile = NULL;
static char *rombase = NULL;
#endif
extern void CPUUpdateRenderBuffers(bool force); extern void CPUUpdateRenderBuffers(bool force);
/****************************************************************************
* VMClose
****************************************************************************/
void VMClose()
{
if(vram != NULL)
{
free(vram);
vram = NULL;
}
if(paletteRAM != NULL)
{
free(paletteRAM);
paletteRAM = NULL;
}
if(internalRAM != NULL)
{
free(internalRAM);
internalRAM = NULL;
}
if(workRAM != NULL)
{
free(workRAM);
workRAM = NULL;
}
if(bios != NULL)
{
free(bios);
bios = NULL;
}
if(pix != NULL)
{
free(pix);
pix = NULL;
}
if(oam != NULL)
{
free(oam);
oam = NULL;
}
if(ioMem != NULL)
{
free(ioMem);
ioMem = NULL;
}
#ifdef USE_VM
if (rombase != NULL)
free(rombase);
if (romfile != NULL)
{
fclose(romfile);
romfile = NULL;
}
#endif
}
/**************************************************************************** /****************************************************************************
* VMAllocGBA * VMAllocGBA
* *
@ -65,35 +138,25 @@ extern void CPUUpdateRenderBuffers(bool force);
****************************************************************************/ ****************************************************************************/
static void VMAllocGBA( void ) static void VMAllocGBA( void )
{ {
gbabase = (char *)memalign(32, GBATOTAL); workRAM = (u8 *)calloc(1, 0x40000);
memset(gbabase, 0, GBATOTAL); bios = (u8 *)calloc(1,0x4000);
internalRAM = (u8 *)calloc(1,0x8000);
paletteRAM = (u8 *)calloc(1,0x400);
vram = (u8 *)calloc(1, 0x20000);
oam = (u8 *)calloc(1, 0x400);
pix = (u8 *)calloc(1, 4 * 241 * 162);
ioMem = (u8 *)calloc(1, 0x400);
/* Assign to internal GBA variables */ if(workRAM == NULL || bios == NULL || internalRAM == NULL ||
workRAM = (u8 *)gbabase; paletteRAM == NULL || vram == NULL || oam == NULL ||
bios = (u8 *)(gbabase + WORKRAM); pix == NULL || ioMem == NULL)
internalRAM = (u8 *)(bios + BIOS); {
paletteRAM = (u8 *)(internalRAM + INTERNALRAM); WaitPrompt((char *)"Out of memory!");
vram = (u8 *)(paletteRAM + PALETTERAM); VMClose();
oam = (u8 *)(vram + VRAM); }
pix = (u8 *)(oam + OAM);
ioMem = (u8 *)(pix + PIX);
}
/****************************************************************************
* VMClose
****************************************************************************/
static void VMClose( void )
{
/* if ( rombase != NULL )
free(rombase);
*/
if ( gbabase != NULL )
free(gbabase);
gbabase = NULL;
} }
#ifndef USE_VM
/**************************************************************************** /****************************************************************************
* VMCPULoadROM * VMCPULoadROM
* *
@ -134,6 +197,8 @@ bool VMCPULoadROM(int method)
if(GBAROMSize) if(GBAROMSize)
{ {
flashInit();
eepromInit();
CPUUpdateRenderBuffers( true ); CPUUpdateRenderBuffers( true );
return true; return true;
} }
@ -143,148 +208,8 @@ bool VMCPULoadROM(int method)
return false; return false;
} }
} }
/****************************************************************************
* VMRead32
*
* Return a 32bit value
****************************************************************************/
u32 VMRead32( u32 address )
{
u32 badaddress;
if ( address >= GBAROMSize )
{
badaddress = ( ( ( address >> 1 ) & 0xffff ) << 16 ) | ( ( ( address + 2 ) >> 1 ) & 0xffff );
return badaddress;
}
return READ32LE((rom + address));
}
/****************************************************************************
* VMRead16
*
* Return a 16bit value
****************************************************************************/
u16 VMRead16( u32 address )
{
if ( address >= GBAROMSize )
{
return ( address >> 1 ) & 0xffff;
}
return READ16LE((rom + address));
}
/****************************************************************************
* VMRead8
*
* Return 8bit value
****************************************************************************/
u8 VMRead8( u32 address )
{
if ( address >= GBAROMSize )
{
return ( address >> 1 ) & 0xff;
}
return (u8)rom[address];
}
#else #else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "agb/GBA.h"
#include "Globals.h"
#include "Util.h"
#include "Port.h"
#include "menudraw.h"
#include "filesel.h"
#include "vba.h"
#include "fileop.h"
extern "C" {
#include "tbtime.h"
}
/** Globals **/
extern u32 loadtimeradjust;
#define MEM_BAD 0xff
#define MEM_VM 0x01
#define MEM_UN 0x80
/** Setup VM to use small 16kb windows **/
#define VMSHIFTBITS 14
#define VMSHIFTMASK 0x3FFF
#define MAXGBAROM ( 32 * 1024 * 1024 )
#define MAXROM (4 * 1024 * 1024)
#define MAXVMPAGE ( MAXGBAROM >> VMSHIFTBITS )
#define MAXVMMASK ( ( MAXROM >> VMSHIFTBITS ) - 1 )
typedef struct
{
char *pageptr;
int pagetype;
int pageno;
}
VMPAGE;
static VMPAGE vmpage[MAXVMPAGE];
static int vmpageno = 0;
static char *rombase = NULL;
static char *gbabase = NULL;
static FILE* romfile = NULL;
static int useVM = 1;
static u32 GBAROMSize = 0;
/**
* GBA Memory
*/
#define WORKRAM 0x40000
#define BIOS 0x4000
#define INTERNALRAM 0x8000
#define PALETTERAM 0x400
#define VRAM 0x20000
#define OAM 0x400
#define PIX (4 * 241 * 162)
#define IOMEM 0x400
#define GBATOTAL (WORKRAM + BIOS + INTERNALRAM + PALETTERAM + \
VRAM + OAM + PIX + IOMEM )
extern void CPUUpdateRenderBuffers(bool force);
/****************************************************************************
* VMAllocGBA
*
* Allocate the memory required for GBA.
****************************************************************************/
static void VMAllocGBA( void )
{
gbabase = (char *)memalign(32, GBATOTAL);
memset(gbabase, 0, GBATOTAL);
/* Assign to internal GBA variables */
workRAM = (u8 *)gbabase;
bios = (u8 *)(gbabase + WORKRAM);
internalRAM = (u8 *)(bios + BIOS);
paletteRAM = (u8 *)(internalRAM + INTERNALRAM);
vram = (u8 *)(paletteRAM + PALETTERAM);
oam = (u8 *)(vram + VRAM);
pix = (u8 *)(oam + OAM);
ioMem = (u8 *)(pix + PIX);
}
/**************************************************************************** /****************************************************************************
* VMFindFree * VMFindFree
* *
@ -322,7 +247,6 @@ static void VMAllocate( int pageid )
vmpage[pageid].pageptr = rombase + ( vmpageno << VMSHIFTBITS ); vmpage[pageid].pageptr = rombase + ( vmpageno << VMSHIFTBITS );
vmpage[pageid].pagetype = MEM_VM; vmpage[pageid].pagetype = MEM_VM;
vmpage[pageid].pageno = vmpageno; vmpage[pageid].pageno = vmpageno;
} }
/**************************************************************************** /****************************************************************************
@ -350,25 +274,6 @@ static void VMInit( void )
rom = (u8 *)rombase; rom = (u8 *)rombase;
} }
/****************************************************************************
* VMClose
****************************************************************************/
static void VMClose( void )
{
if ( rombase != NULL )
free(rombase);
if ( gbabase != NULL )
free(gbabase);
if ( romfile != NULL )
fclose(romfile);
rombase = gbabase = NULL;
romfile = NULL;
}
/**************************************************************************** /****************************************************************************
* VMCPULoadROM * VMCPULoadROM
* *
@ -424,7 +329,6 @@ int VMCPULoadROM(int method)
// printf("ROM Size %d\n", romfile->fsize); // printf("ROM Size %d\n", romfile->fsize);
/* Always use VM, regardless of ROM size */
res = fread(rom, 1, (1 << VMSHIFTBITS), romfile); res = fread(rom, 1, (1 << VMSHIFTBITS), romfile);
if ( res != (1 << VMSHIFTBITS ) ) if ( res != (1 << VMSHIFTBITS ) )
{ {
@ -442,6 +346,8 @@ int VMCPULoadROM(int method)
vmpage[0].pageno = 0; vmpage[0].pageno = 0;
vmpage[0].pagetype = MEM_VM; vmpage[0].pagetype = MEM_VM;
flashInit();
eepromInit();
CPUUpdateRenderBuffers( true ); CPUUpdateRenderBuffers( true );
return 1; return 1;
@ -504,10 +410,10 @@ static void VMNewPage( int pageid )
} }
/**************************************************************************** /****************************************************************************
* VMRead32 * VMRead32
* *
* Return a 32bit value * Return a 32bit value
****************************************************************************/ ****************************************************************************/
u32 VMRead32( u32 address ) u32 VMRead32( u32 address )
{ {
int pageid; int pageid;
@ -521,10 +427,6 @@ u32 VMRead32( u32 address )
return badaddress; return badaddress;
} }
if ( !useVM )
return READ32LE((rom + address));
/** Use VM **/
pageid = address >> VMSHIFTBITS; pageid = address >> VMSHIFTBITS;
switch( vmpage[pageid].pagetype ) switch( vmpage[pageid].pagetype )
@ -548,10 +450,10 @@ u32 VMRead32( u32 address )
} }
/**************************************************************************** /****************************************************************************
* VMRead16 * VMRead16
* *
* Return a 16bit value * Return a 16bit value
****************************************************************************/ ****************************************************************************/
u16 VMRead16( u32 address ) u16 VMRead16( u32 address )
{ {
int pageid; int pageid;
@ -563,9 +465,6 @@ u16 VMRead16( u32 address )
return ( address >> 1 ) & 0xffff; return ( address >> 1 ) & 0xffff;
} }
if ( !useVM )
return READ16LE((rom + address));
pageid = address >> VMSHIFTBITS; pageid = address >> VMSHIFTBITS;
switch( vmpage[pageid].pagetype ) switch( vmpage[pageid].pagetype )
@ -588,10 +487,10 @@ u16 VMRead16( u32 address )
} }
/**************************************************************************** /****************************************************************************
* VMRead8 * VMRead8
* *
* Return 8bit value * Return 8bit value
****************************************************************************/ ****************************************************************************/
u8 VMRead8( u32 address ) u8 VMRead8( u32 address )
{ {
int pageid; int pageid;
@ -603,9 +502,6 @@ u8 VMRead8( u32 address )
return ( address >> 1 ) & 0xff; return ( address >> 1 ) & 0xff;
} }
if ( !useVM )
return (u8)rom[address];
pageid = address >> VMSHIFTBITS; pageid = address >> VMSHIFTBITS;
switch( vmpage[pageid].pagetype ) switch( vmpage[pageid].pagetype )

View File

@ -12,9 +12,13 @@
#define __VBAVMHDR__ #define __VBAVMHDR__
bool VMCPULoadROM(int method); bool VMCPULoadROM(int method);
void VMClose();
#ifdef USE_VM
u32 VMRead32( u32 address ); u32 VMRead32( u32 address );
u16 VMRead16( u32 address ); u16 VMRead16( u32 address );
u8 VMRead8( u32 address ); u8 VMRead8( u32 address );
#endif
#endif #endif

View File

@ -75,8 +75,9 @@ u8 inline CPUReadByteQuick( u32 addr )
case 9: case 9:
case 10: case 10:
case 12: case 12:
#ifdef USE_VM
return VMRead8( addr & 0x1FFFFFF ); return VMRead8( addr & 0x1FFFFFF );
#endif
default: default:
return CPUReadByteQuickDef(addr); return CPUReadByteQuickDef(addr);
} }
@ -92,7 +93,9 @@ u16 inline CPUReadHalfWordQuick( u32 addr )
case 9: case 9:
case 10: case 10:
case 12: case 12:
#ifdef USE_VM
return VMRead16( addr & 0x1FFFFFF ); return VMRead16( addr & 0x1FFFFFF );
#endif
default: default:
return CPUReadHalfWordQuickDef(addr); return CPUReadHalfWordQuickDef(addr);
} }
@ -108,7 +111,9 @@ u32 inline CPUReadMemoryQuick( u32 addr )
case 9: case 9:
case 10: case 10:
case 12: case 12:
#ifdef USE_VM
return VMRead32( addr & 0x1FFFFFF ); return VMRead32( addr & 0x1FFFFFF );
#endif
default: default:
return CPUReadMemoryQuickDef(addr); return CPUReadMemoryQuickDef(addr);
} }

View File

@ -4026,8 +4026,7 @@ void gbCleanUp()
} }
if(pix != NULL) { if(pix != NULL) {
// this causes system to CRASH when switching from GB to GBA and then back to GB free(pix);
//free(pix);
pix = NULL; pix = NULL;
} }