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
* *
@ -292,23 +217,23 @@ static void VMAllocGBA( void )
****************************************************************************/ ****************************************************************************/
static void VMFindFree( void ) static void VMFindFree( void )
{ {
int i; int i;
vmpageno++; vmpageno++;
vmpageno &= MAXVMMASK; vmpageno &= MAXVMMASK;
if ( vmpageno == 0 ) vmpageno++; if ( vmpageno == 0 ) vmpageno++;
for ( i = 1; i < MAXVMPAGE; i++ ) for ( i = 1; i < MAXVMPAGE; i++ )
{ {
/** Remove any other pointer to this vmpage **/ /** Remove any other pointer to this vmpage **/
if ( vmpage[i].pageno == vmpageno ) if ( vmpage[i].pageno == vmpageno )
{ {
vmpage[i].pageptr = NULL; vmpage[i].pageptr = NULL;
vmpage[i].pagetype = MEM_UN; vmpage[i].pagetype = MEM_UN;
vmpage[i].pageno = -1; vmpage[i].pageno = -1;
break; break;
} }
} }
} }
/**************************************************************************** /****************************************************************************
@ -318,11 +243,10 @@ static void VMFindFree( void )
****************************************************************************/ ****************************************************************************/
static void VMAllocate( int pageid ) static void VMAllocate( int pageid )
{ {
VMFindFree(); VMFindFree();
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;
} }
/**************************************************************************** /****************************************************************************
@ -332,41 +256,22 @@ static void VMAllocate( int pageid )
****************************************************************************/ ****************************************************************************/
static void VMInit( void ) static void VMInit( void )
{ {
int i; int i;
/** Clear down pointers **/ /** Clear down pointers **/
memset(&vmpage, 0, sizeof(VMPAGE) * MAXVMPAGE); memset(&vmpage, 0, sizeof(VMPAGE) * MAXVMPAGE);
for ( i = 0; i < MAXVMPAGE; i++ ) for ( i = 0; i < MAXVMPAGE; i++ )
{ {
vmpage[i].pageno = -1; vmpage[i].pageno = -1;
vmpage[i].pagetype = MEM_UN; vmpage[i].pagetype = MEM_UN;
} }
/** Allocate physical **/ /** Allocate physical **/
if ( rombase == NULL ) if ( rombase == NULL )
rombase = (char *)memalign(32, MAXROM); rombase = (char *)memalign(32, MAXROM);
vmpageno = 0;
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;
vmpageno = 0;
rom = (u8 *)rombase;
} }
/**************************************************************************** /****************************************************************************
@ -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;
@ -455,175 +361,165 @@ int VMCPULoadROM(int method)
****************************************************************************/ ****************************************************************************/
static void VMNewPage( int pageid ) static void VMNewPage( int pageid )
{ {
int res; int res;
tb_t start,end; tb_t start,end;
char msg[512]; char msg[512];
mftb(&start); mftb(&start);
res = fseek( romfile, pageid << VMSHIFTBITS, SEEK_SET ); res = fseek( romfile, pageid << VMSHIFTBITS, SEEK_SET );
if ( ! res ) if ( ! res )
{ {
sprintf(msg, "Seek error! - Offset %08x %d\n", pageid << VMSHIFTBITS, res); sprintf(msg, "Seek error! - Offset %08x %d\n", pageid << VMSHIFTBITS, res);
WaitPrompt(msg); WaitPrompt(msg);
VMClose(); VMClose();
return; return;
} }
VMAllocate( pageid ); VMAllocate( pageid );
res = fread( vmpage[pageid].pageptr, 1, 1 << VMSHIFTBITS, romfile ); res = fread( vmpage[pageid].pageptr, 1, 1 << VMSHIFTBITS, romfile );
if ( res != ( 1 << VMSHIFTBITS ) ) if ( res != ( 1 << VMSHIFTBITS ) )
{ {
sprintf(msg, "Error reading! %d bytes only\n", res); sprintf(msg, "Error reading! %d bytes only\n", res);
WaitPrompt(msg); WaitPrompt(msg);
VMClose(); VMClose();
return; return;
} }
mftb(&end); mftb(&end);
loadtimeradjust += tb_diff_msec(&end, &start); loadtimeradjust += tb_diff_msec(&end, &start);
#if 0 #if 0
if ( pageid == 0x1FE ) if ( pageid == 0x1FE )
{ {
vmpage[pageid].pageptr[0x209C] = 0xDF; vmpage[pageid].pageptr[0x209C] = 0xDF;
vmpage[pageid].pageptr[0x209D] = 0xFA; vmpage[pageid].pageptr[0x209D] = 0xFA;
vmpage[pageid].pageptr[0x209E] = 0x47; vmpage[pageid].pageptr[0x209E] = 0x47;
vmpage[pageid].pageptr[0x209F] = 0x70; vmpage[pageid].pageptr[0x209F] = 0x70;
} }
printf("VMNP : %02x %04x %08x [%02x%02x%02x%02x] [%02x%02x%02x%02x]\n", vmpageno, pageid, printf("VMNP : %02x %04x %08x [%02x%02x%02x%02x] [%02x%02x%02x%02x]\n", vmpageno, pageid,
(u32)(vmpage[pageid].pageptr - rombase), vmpage[pageid].pageptr[0], vmpage[pageid].pageptr[1], (u32)(vmpage[pageid].pageptr - rombase), vmpage[pageid].pageptr[0], vmpage[pageid].pageptr[1],
vmpage[pageid].pageptr[2], vmpage[pageid].pageptr[3], vmpage[pageid].pageptr[2], vmpage[pageid].pageptr[3],
vmpage[pageid].pageptr[0xfffc], vmpage[pageid].pageptr[0xfffd], vmpage[pageid].pageptr[0xfffc], vmpage[pageid].pageptr[0xfffd],
vmpage[pageid].pageptr[0xfffe], vmpage[pageid].pageptr[0xffff] ); vmpage[pageid].pageptr[0xfffe], vmpage[pageid].pageptr[0xffff] );
#endif #endif
} }
/**************************************************************************** /****************************************************************************
* VMRead32 * VMRead32
* *
* Return a 32bit value * Return a 32bit value
****************************************************************************/ ****************************************************************************/
u32 VMRead32( u32 address ) u32 VMRead32( u32 address )
{ {
int pageid; int pageid;
u32 badaddress; u32 badaddress;
char msg[512]; char msg[512];
//printf("VM32 : Request %08x\n", address); //printf("VM32 : Request %08x\n", address);
if ( address >= GBAROMSize ) if ( address >= GBAROMSize )
{ {
badaddress = ( ( ( address >> 1 ) & 0xffff ) << 16 ) | ( ( ( address + 2 ) >> 1 ) & 0xffff ); badaddress = ( ( ( address >> 1 ) & 0xffff ) << 16 ) | ( ( ( address + 2 ) >> 1 ) & 0xffff );
return badaddress; return badaddress;
} }
if ( !useVM ) pageid = address >> VMSHIFTBITS;
return READ32LE((rom + address));
/** Use VM **/ switch( vmpage[pageid].pagetype )
pageid = address >> VMSHIFTBITS; {
case MEM_UN:
switch( vmpage[pageid].pagetype )
{
case MEM_UN:
VMNewPage(pageid); VMNewPage(pageid);
case MEM_VM: case MEM_VM:
return READ32LE( vmpage[pageid].pageptr + ( address & VMSHIFTMASK ) ); return READ32LE( vmpage[pageid].pageptr + ( address & VMSHIFTMASK ) );
default: default:
sprintf(msg, "VM32 : Unknown page type! (%d) [%d]", vmpage[pageid].pagetype, pageid); sprintf(msg, "VM32 : Unknown page type! (%d) [%d]", vmpage[pageid].pagetype, pageid);
WaitPrompt(msg); WaitPrompt(msg);
VMClose(); VMClose();
return 0; return 0;
} }
/* Can never get here ... but stops gcc bitchin' */ /* Can never get here ... but stops gcc bitchin' */
return 0; return 0;
} }
/**************************************************************************** /****************************************************************************
* VMRead16 * VMRead16
* *
* Return a 16bit value * Return a 16bit value
****************************************************************************/ ****************************************************************************/
u16 VMRead16( u32 address ) u16 VMRead16( u32 address )
{ {
int pageid; int pageid;
//printf("VM16 : Request %08x\n", address); //printf("VM16 : Request %08x\n", address);
if ( address >= GBAROMSize ) if ( address >= GBAROMSize )
{ {
return ( address >> 1 ) & 0xffff; return ( address >> 1 ) & 0xffff;
} }
if ( !useVM ) pageid = address >> VMSHIFTBITS;
return READ16LE((rom + address));
pageid = address >> VMSHIFTBITS; switch( vmpage[pageid].pagetype )
{
switch( vmpage[pageid].pagetype ) case MEM_UN:
{
case MEM_UN:
VMNewPage(pageid); VMNewPage(pageid);
case MEM_VM: case MEM_VM:
return READ16LE( vmpage[pageid].pageptr + ( address & VMSHIFTMASK ) ); return READ16LE( vmpage[pageid].pageptr + ( address & VMSHIFTMASK ) );
default: default:
WaitPrompt((char*) "VM16 : Unknown page type!"); WaitPrompt((char*) "VM16 : Unknown page type!");
VMClose(); VMClose();
return 0; return 0;
} }
/* Can never get here ... but stops gcc bitchin' */ /* Can never get here ... but stops gcc bitchin' */
return 0; return 0;
} }
/**************************************************************************** /****************************************************************************
* VMRead8 * VMRead8
* *
* Return 8bit value * Return 8bit value
****************************************************************************/ ****************************************************************************/
u8 VMRead8( u32 address ) u8 VMRead8( u32 address )
{ {
int pageid; int pageid;
//printf("VM8 : Request %08x\n", address); //printf("VM8 : Request %08x\n", address);
if ( address >= GBAROMSize ) if ( address >= GBAROMSize )
{ {
return ( address >> 1 ) & 0xff; return ( address >> 1 ) & 0xff;
} }
if ( !useVM ) pageid = address >> VMSHIFTBITS;
return (u8)rom[address];
pageid = address >> VMSHIFTBITS; switch( vmpage[pageid].pagetype )
{
switch( vmpage[pageid].pagetype ) case MEM_UN:
{
case MEM_UN:
VMNewPage(pageid); VMNewPage(pageid);
case MEM_VM: case MEM_VM:
return (u8)vmpage[pageid].pageptr[ (address & VMSHIFTMASK) ]; return (u8)vmpage[pageid].pageptr[ (address & VMSHIFTMASK) ];
default: default:
WaitPrompt((char*) "VM8 : Unknown page type!"); WaitPrompt((char*) "VM8 : Unknown page type!");
VMClose(); VMClose();
return 0; return 0;
} }
/* Can never get here ... but stops gcc bitchin' */ /* Can never get here ... but stops gcc bitchin' */
return 0; return 0;
} }

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;
} }