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();
bool LoadGBROM(int method)
{
// cleanup GB memory
if(gbRom != NULL)
gbCleanUp();
gbRom = (u8 *)malloc(1024*1024*4); // allocate 4 MB to GB ROM
bios = (u8 *)calloc(1,0x100);
systemSaveUpdateCounter = SYSTEM_SAVE_NOT_UPDATED;
@ -618,6 +615,9 @@ bool LoadVBAROM(int method)
srcHeight = 0;
srcPitch = 0;
VMClose(); // cleanup GBA memory
gbCleanUp(); // cleanup GB memory
switch( type )
{
case 2:

View File

@ -8,7 +8,6 @@
* GameBoy Advance Virtual Memory Paging
***************************************************************************/
#ifdef HW_RVL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -39,25 +38,99 @@ extern "C" {
unsigned int MEM2Storage = 0x91000000;
static char *gbabase = NULL;
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 )
#ifdef USE_VM
extern u32 loadtimeradjust;
/** 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 FILE* romfile = NULL;
static char *rombase = NULL;
#endif
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
*
@ -65,35 +138,25 @@ extern void CPUUpdateRenderBuffers(bool force);
****************************************************************************/
static void VMAllocGBA( void )
{
gbabase = (char *)memalign(32, GBATOTAL);
memset(gbabase, 0, GBATOTAL);
workRAM = (u8 *)calloc(1, 0x40000);
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 */
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);
}
/****************************************************************************
* VMClose
****************************************************************************/
static void VMClose( void )
{
/* if ( rombase != NULL )
free(rombase);
*/
if ( gbabase != NULL )
free(gbabase);
gbabase = NULL;
if(workRAM == NULL || bios == NULL || internalRAM == NULL ||
paletteRAM == NULL || vram == NULL || oam == NULL ||
pix == NULL || ioMem == NULL)
{
WaitPrompt((char *)"Out of memory!");
VMClose();
}
}
#ifndef USE_VM
/****************************************************************************
* VMCPULoadROM
*
@ -134,6 +197,8 @@ bool VMCPULoadROM(int method)
if(GBAROMSize)
{
flashInit();
eepromInit();
CPUUpdateRenderBuffers( true );
return true;
}
@ -143,148 +208,8 @@ bool VMCPULoadROM(int method)
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
#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
*
@ -322,7 +247,6 @@ static void VMAllocate( int pageid )
vmpage[pageid].pageptr = rombase + ( vmpageno << VMSHIFTBITS );
vmpage[pageid].pagetype = MEM_VM;
vmpage[pageid].pageno = vmpageno;
}
/****************************************************************************
@ -350,25 +274,6 @@ static void VMInit( void )
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
*
@ -424,7 +329,6 @@ int VMCPULoadROM(int method)
// printf("ROM Size %d\n", romfile->fsize);
/* Always use VM, regardless of ROM size */
res = fread(rom, 1, (1 << VMSHIFTBITS), romfile);
if ( res != (1 << VMSHIFTBITS ) )
{
@ -442,6 +346,8 @@ int VMCPULoadROM(int method)
vmpage[0].pageno = 0;
vmpage[0].pagetype = MEM_VM;
flashInit();
eepromInit();
CPUUpdateRenderBuffers( true );
return 1;
@ -504,10 +410,10 @@ static void VMNewPage( int pageid )
}
/****************************************************************************
* VMRead32
*
* Return a 32bit value
****************************************************************************/
* VMRead32
*
* Return a 32bit value
****************************************************************************/
u32 VMRead32( u32 address )
{
int pageid;
@ -521,10 +427,6 @@ u32 VMRead32( u32 address )
return badaddress;
}
if ( !useVM )
return READ32LE((rom + address));
/** Use VM **/
pageid = address >> VMSHIFTBITS;
switch( vmpage[pageid].pagetype )
@ -548,10 +450,10 @@ u32 VMRead32( u32 address )
}
/****************************************************************************
* VMRead16
*
* Return a 16bit value
****************************************************************************/
* VMRead16
*
* Return a 16bit value
****************************************************************************/
u16 VMRead16( u32 address )
{
int pageid;
@ -563,9 +465,6 @@ u16 VMRead16( u32 address )
return ( address >> 1 ) & 0xffff;
}
if ( !useVM )
return READ16LE((rom + address));
pageid = address >> VMSHIFTBITS;
switch( vmpage[pageid].pagetype )
@ -588,10 +487,10 @@ u16 VMRead16( u32 address )
}
/****************************************************************************
* VMRead8
*
* Return 8bit value
****************************************************************************/
* VMRead8
*
* Return 8bit value
****************************************************************************/
u8 VMRead8( u32 address )
{
int pageid;
@ -603,9 +502,6 @@ u8 VMRead8( u32 address )
return ( address >> 1 ) & 0xff;
}
if ( !useVM )
return (u8)rom[address];
pageid = address >> VMSHIFTBITS;
switch( vmpage[pageid].pagetype )

View File

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

View File

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

View File

@ -4026,8 +4026,7 @@ void gbCleanUp()
}
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;
}