From be3ec4a072a0909f2dbaa1b885cffed741e00431 Mon Sep 17 00:00:00 2001 From: rheiny Date: Mon, 5 Feb 2007 20:41:23 +0000 Subject: [PATCH] Cleaned up some code. --- src/drivers/common/config.cpp | 216 +++++++++++++++++++++------------- src/drivers/common/config.h | 4 +- src/drivers/win/args.cpp | 9 +- src/drivers/win/config.cpp | 17 ++- src/drivers/win/input.cpp | 3 + src/drivers/win/main.cpp | 159 +++++++++++++++++-------- src/fceu.cpp | 23 +++- src/file.cpp | 35 ++++-- src/palette.cpp | 4 +- src/ppu.cpp | 62 +++++----- src/video.cpp | 62 ++++++---- src/x6502.cpp | 27 ++++- 12 files changed, 404 insertions(+), 217 deletions(-) diff --git a/src/drivers/common/config.cpp b/src/drivers/common/config.cpp index 00ed9852..6a2642ea 100644 --- a/src/drivers/common/config.cpp +++ b/src/drivers/common/config.cpp @@ -50,103 +50,149 @@ static int FReadString(FILE *fp, char *str, int n) static void GetValueR(FILE *fp, char *str, void *v, int c) { - char buf[256]; - int s; + char buf[256]; + int s; - while(FReadString(fp,buf,256)) - { - fread(&s,1,4,fp); - if(!strcmp(str, buf)) - { - if(!c) // String, allocate some memory. - { - if(!(*(char **)v=(char*)malloc(s))) - goto gogl; - fread(*(char **)v,1,s,fp); - continue; - } - else if(s>c || sc || s=0;x--) - { - if(BaseDirectory[x]=='\\' || BaseDirectory[x]=='/') - {BaseDirectory[x]=0;break;} - } + // Search for the last / or \ in the directory and terminate the string there + for(i = strlen(BaseDirectory); i >= 0 ; i--) + { + if(BaseDirectory[i]=='\\' || BaseDirectory[i]=='/') + { + BaseDirectory[i] = 0; + return; + } + } } static int exiting=0; @@ -333,7 +362,6 @@ static void DriverKill(void) } void FCEUD_Update(uint8 *XBuf, int32 *Buffer, int Count); - void ApplyDefaultCommandMapping(void); @@ -347,25 +375,28 @@ void _updateMemWatch() { HANDLE mapGameMemBlock; HANDLE mapRAM; uint32 *BotInput; -void win_AllocBuffers(uint8 **GameMemBlock, uint8 **RAM) { +void win_AllocBuffers(uint8 **GameMemBlock, uint8 **RAM) +{ mapGameMemBlock = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE, 0, 131072,"fceu.GameMemBlock"); + if(mapGameMemBlock == NULL || GetLastError() == ERROR_ALREADY_EXISTS) { - //mbg 7/38/06 - is this the proper error handling? + //mbg 7/28/06 - is this the proper error handling? //do we need to indicate to user somehow that this failed in this emu instance? CloseHandle(mapGameMemBlock); + mapGameMemBlock = NULL; *GameMemBlock = (uint8 *) malloc(131072); *RAM = (uint8 *) malloc(2048); } else { - *GameMemBlock = (uint8 *)MapViewOfFile(mapGameMemBlock, FILE_MAP_WRITE, 0, 0, 0); + *GameMemBlock = (uint8 *)MapViewOfFile(mapGameMemBlock, FILE_MAP_WRITE, 0, 0, 0); // set up shared memory mappings - mapRAM = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE, 0, 0x800,"fceu.RAM"); - *RAM = (uint8 *)MapViewOfFile(mapRAM, FILE_MAP_WRITE, 0, 0, 0); + mapRAM = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE, 0, 0x800,"fceu.RAM"); + *RAM = (uint8 *)MapViewOfFile(mapRAM, FILE_MAP_WRITE, 0, 0, 0); } // Give RAM pointer to state structure @@ -412,6 +443,13 @@ void win_FreeBuffers(uint8 *GameMemBlock, uint8 *RAM) { } #endif +void do_exit() +{ + DriverKill(); + timeEndPeriod(1); + FCEUI_Kill(); +} + int main(int argc,char *argv[]) { char *t; @@ -424,61 +462,89 @@ int main(int argc,char *argv[]) InitCommonControls(); if(!FCEUI_Initialize()) - goto doexito; + { + do_exit(); + return 1; + } ApplyDefaultCommandMapping(); srand(GetTickCount()); // rand() is used for some GUI sillyness. - fceu_hInstance=GetModuleHandle(0); + fceu_hInstance = GetModuleHandle(0); + // Get the base directory GetBaseDirectory(); + // Load the config information sprintf(TempArray,"%s\\fceu98.cfg",BaseDirectory); LoadConfig(TempArray); - t=ParseArgies(argc,argv); + // Parse the commandline arguments + t = ParseArgies(argc, argv); + /* Bleh, need to find a better place for this. */ { - palyo&=1; + palyo &= 1; FCEUI_SetVidSystem(palyo); - genie&=1; + + genie &= 1; FCEUI_SetGameGenie(genie); - fullscreen&=1; - soundo&=1; + + fullscreen &= 1; + soundo &= 1; + FCEUI_SetSoundVolume(soundvolume); FCEUI_SetSoundQuality(soundquality); } + ParseGIInput(NULL); /* Since a game doesn't have to be loaded before the GUI can be used, make sure the temporary input type variables are set. */ + + // Initialize default directories CreateDirs(); SetDirs(); DoVideoConfigFix(); DoTimingConfigFix(); - if(eoptions&EO_CPALETTE) + if(eoptions & EO_CPALETTE) + { FCEUI_SetPaletteArray(cpalette); + } - if(!t) fullscreen=0; + if(!t) + { + fullscreen=0; + } CreateMainWindow(); if(!InitDInput()) - goto doexito; + { + do_exit(); + return 1; + } if(!DriverInitialize()) - goto doexito; + { + do_exit(); + return 1; + } UpdateMenu(); if(t) + { ALoad(t); - else if(eoptions&EO_FOAFTERSTART) + } + else if(eoptions & EO_FOAFTERSTART) + { LoadNewGamey(hAppWnd, 0); + } doloopy: UpdateFCEUWindow(); @@ -514,7 +580,6 @@ doloopy: if(!exiting) goto doloopy; - doexito: DriverKill(); timeEndPeriod(1); FCEUI_Kill(); diff --git a/src/fceu.cpp b/src/fceu.cpp index bdee8934..2a301a71 100644 --- a/src/fceu.cpp +++ b/src/fceu.cpp @@ -176,14 +176,20 @@ uint8 *RAM; //--------- //windows might need to allocate these differently, so we have some special code -static void AllocBuffers() { - #ifdef _USE_SHARED_MEMORY_ +static void AllocBuffers() +{ + +#ifdef _USE_SHARED_MEMORY_ + void win_AllocBuffers(uint8 **GameMemBlock, uint8 **RAM); win_AllocBuffers(&GameMemBlock, &RAM); - #else + +#else + GameMemBlock = (uint8*)FCEU_gmalloc(131072); RAM = (uint8*)FCEU_gmalloc(0x800); - #endif + +#endif } static void FreeBuffers() { @@ -347,21 +353,30 @@ FCEUGI *FCEUI_LoadGame(const char *name, int OverwriteVidMode) } +/** +* Return: Flag that indicates whether the function was succesful or not. +**/ int FCEUI_Initialize(void) { if(!FCEU_InitVirtualVideo()) + { return 0; + } AllocBuffers(); + // Initialize some parts of the settings structure memset(&FSettings,0,sizeof(FSettings)); FSettings.UsrFirstSLine[0]=8; FSettings.UsrFirstSLine[1]=0; FSettings.UsrLastSLine[0]=231; FSettings.UsrLastSLine[1]=239; FSettings.SoundVolume=100; + FCEUPPU_Init(); + X6502_Init(); + return 1; } diff --git a/src/file.cpp b/src/file.cpp index 6930549f..bb718e4c 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -588,27 +588,38 @@ static char FileExt[2048]; /* Includes the . character, as in ".nes" */ static char FileBaseDirectory[2048]; +/** +* Updates the base directory +**/ void FCEUI_SetBaseDirectory(const char *dir) { - strncpy(BaseDirectory,dir,2047); - BaseDirectory[2047]=0; + strncpy(BaseDirectory, dir, sizeof(BaseDirectory)); + + // TODO: Necessary? + BaseDirectory[2047] = 0; } static char *odirs[FCEUIOD__COUNT]={0,0,0,0,0}; // odirs, odors. ^_^ void FCEUI_SetDirOverride(int which, char *n) { -// FCEU_PrintError("odirs[%d]=%s->%s", which, odirs[which], n); -if(which < FCEUIOD__COUNT) - odirs[which]=n; + // FCEU_PrintError("odirs[%d]=%s->%s", which, odirs[which], n); + if (which < FCEUIOD__COUNT) + { + odirs[which] = n; + } - if(GameInfo) /* Rebuild cache of present states/movies. */ - { - if(which==FCEUIOD_STATE) - FCEUSS_CheckStates(); - else if(which == FCEUIOD_MISC) - FCEUMOV_CheckMovies(); - } + if(GameInfo) /* Rebuild cache of present states/movies. */ + { + if(which==FCEUIOD_STATE) + { + FCEUSS_CheckStates(); + } + else if(which == FCEUIOD_MISC) + { + FCEUMOV_CheckMovies(); + } + } } #ifndef HAVE_ASPRINTF diff --git a/src/palette.cpp b/src/palette.cpp index 8d5449b7..fcf30e1d 100644 --- a/src/palette.cpp +++ b/src/palette.cpp @@ -267,8 +267,8 @@ void WritePalette(void) void FCEUI_GetNTSCTH(int *tint, int *hue) { - *tint=ntsctint; - *hue=ntschue; + *tint = ntsctint; + *hue = ntschue; } static int controlselect=0; diff --git a/src/ppu.cpp b/src/ppu.cpp index 6bb6f424..b43bdfde 100644 --- a/src/ppu.cpp +++ b/src/ppu.cpp @@ -64,38 +64,39 @@ static uint32 ppulut3[128]; static void makeppulut(void) { - int x; - int y; + int x; + int y; + int cc,xo,pixel; - for(x=0;x<256;x++) - { - ppulut1[x]=0; - for(y=0;y<8;y++) - ppulut1[x]|=((x>>(7-y))&1)<<(y*4); - ppulut2[x]=ppulut1[x]<<1; - } - { + for(x=0;x<256;x++) + { + ppulut1[x] = 0; + + for(y=0;y<8;y++) + { + ppulut1[x] |= ((x>>(7-y))&1)<<(y*4); + } - int cc,xo,pixel; + ppulut2[x] = ppulut1[x] << 1; + } - for(cc=0;cc<16;cc++) - { - for(xo=0;xo<8;xo++) - { - ppulut3[xo|(cc<<3)]=0; - for(pixel=0;pixel<8;pixel++) - { - int shiftr; - shiftr=(pixel+xo)/8; - shiftr*=2; - ppulut3[xo|(cc<<3)]|=(( cc>>shiftr )&3)<<(2+pixel*4); - } -// printf("%08x\n",ppulut3[xo|(cc<<3)]); - } - } - - } + for(cc=0;cc<16;cc++) + { + for(xo=0;xo<8;xo++) + { + ppulut3[ xo | ( cc << 3 ) ] = 0; + + for(pixel=0;pixel<8;pixel++) + { + int shiftr; + shiftr = ( pixel + xo ) / 8; + shiftr *= 2; + ppulut3[ xo | (cc<<3) ] |= ( ( cc >> shiftr ) & 3 ) << ( 2 + pixel * 4 ); + } + // printf("%08x\n",ppulut3[xo|(cc<<3)]); + } + } } static int ppudead=1; @@ -1212,9 +1213,12 @@ void FCEUPPU_SetVideoSystem(int w) } } +/** +* Initializes the PPU +**/ void FCEUPPU_Init(void) { - makeppulut(); + makeppulut(); } void FCEUPPU_Reset(void) diff --git a/src/video.cpp b/src/video.cpp index 42dbe761..eb8db600 100644 --- a/src/video.cpp +++ b/src/video.cpp @@ -74,46 +74,62 @@ void FCEU_KillVirtualVideo(void) //} } +/** +* Return: Flag that indicates whether the function was succesful or not. +* +* TODO: This function is Windows-only. It should probably be moved. +**/ int FCEU_InitVirtualVideo(void) { - if(!XBuf) /* Some driver code may allocate XBuf externally. */ - /* 256 bytes per scanline, * 240 scanline maximum, +8 for alignment, - */ + if(!XBuf) /* Some driver code may allocate XBuf externally. */ + /* 256 bytes per scanline, * 240 scanline maximum, +8 for alignment, + */ + #ifdef _USE_SHARED_MEMORY_ - mapXBuf = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE, 0, 256 * 256 + 8,"fceu.XBuf"); + + mapXBuf = CreateFileMapping((HANDLE)0xFFFFFFFF,NULL,PAGE_READWRITE, 0, 256 * 256 + 8, "fceu.XBuf"); + if(mapXBuf == NULL || GetLastError() == ERROR_ALREADY_EXISTS) { CloseHandle(mapXBuf); mapXBuf = NULL; - XBuf= (uint8*) (FCEU_malloc(256 * 256 + 8)); - XBackBuf= (uint8*) (FCEU_malloc(256 * 256 + 8)); + XBuf = (uint8*) (FCEU_malloc(256 * 256 + 8)); + XBackBuf = (uint8*) (FCEU_malloc(256 * 256 + 8)); } else { - XBuf = (uint8 *)MapViewOfFile(mapXBuf, FILE_MAP_WRITE, 0, 0, 0); - + XBuf = (uint8 *)MapViewOfFile(mapXBuf, FILE_MAP_WRITE, 0, 0, 0); XBackBuf = (uint8*) (FCEU_malloc(256 * 256 + 8)); } if (!XBuf || !XBackBuf) + { return 0; + } + #else - if(!(XBuf= (uint8*) (FCEU_malloc(256 * 256 + 8))) || - !(XBackBuf= (uint8*) (FCEU_malloc(256 * 256 + 8)))) - return 0; -#endif //_USE_SHARED_MEMORY_ - xbsave=XBuf; - if(sizeof(uint8*)==4) - { - uint32 m; - m=(uint32)XBuf; - m=(4-m)&3; - XBuf+=m; - } - memset(XBuf,128,256*256); //*240); - memset(XBackBuf,128,256*256); - return 1; + if(!(XBuf= (uint8*) (FCEU_malloc(256 * 256 + 8))) || + !(XBackBuf= (uint8*) (FCEU_malloc(256 * 256 + 8)))) + { + return 0; + } + +#endif //_USE_SHARED_MEMORY_ + + xbsave = XBuf; + + if( sizeof(uint8*) == 4 ) + { + uint32 m = (uint32)XBuf; + m = ( 4 - m) & 3; + XBuf+=m; + } + + memset(XBuf,128,256*256); //*240); + memset(XBackBuf,128,256*256); + + return 1; } int howlong; diff --git a/src/x6502.cpp b/src/x6502.cpp index bb1d9de7..367b991b 100644 --- a/src/x6502.cpp +++ b/src/x6502.cpp @@ -363,16 +363,31 @@ void X6502_Reset(void) { _IRQlow=FCEU_IQRESET; } - +/** +* Initializes the 6502 CPU +**/ void X6502_Init(void) { - int x; + unsigned int i; + // Initialize the CPU structure memset((void *)&X,0,sizeof(X)); - for(x=0;x<256;x++) - if(!x) ZNTable[x]=Z_FLAG; - else if(x&0x80) ZNTable[x]=N_FLAG; - else ZNTable[x]=0; + + for(i = 0; i < sizeof(ZNTable); i++) + { + if(!i) + { + ZNTable[i] = Z_FLAG; + } + else if ( i & 0x80 ) + { + ZNTable[i] = N_FLAG; + } + else + { + ZNTable[i] = 0; + } + } } void X6502_Power(void)