From 76bb37145d1bd0124915b4b4c56c73bd600380e1 Mon Sep 17 00:00:00 2001 From: cyberwarriorx Date: Sun, 15 Oct 2006 20:45:17 +0000 Subject: [PATCH] -Renamed the various *Init/*DeInit functions to *_Init/*_DeInit so it better matches the naming convention of the rest of the functions -Changed the way NDS_LoadROM works. It still needs -some- work(cflash is broken for now), but basically the idea is to cut down on duplicate code and instead have the emulator core do most of the work. -Added a GPU Reset function. Once the reset function for the other chips, etc. are working, we can finally do a proper reset function. -Linux port is probably broken again(sorry!) --- desmume/src/GPU.c | 68 +++++++++++-------- desmume/src/GPU.h | 10 +-- desmume/src/MMU.c | 4 +- desmume/src/NDSSystem.c | 120 +++++++++++++++++++++++++++++----- desmume/src/NDSSystem.h | 8 ++- desmume/src/windows/main.c | 102 ++++++++++++----------------- desmume/src/windows/oamView.c | 2 +- desmume/src/windows/oamView.h | 2 +- 8 files changed, 202 insertions(+), 114 deletions(-) diff --git a/desmume/src/GPU.c b/desmume/src/GPU.c index 1d43ccb5e..39ac89754 100644 --- a/desmume/src/GPU.c +++ b/desmume/src/GPU.c @@ -120,36 +120,43 @@ GPU::GPU(u8 l) : lcd(l) } #endif -GPU * GPUInit(u8 l) +GPU * GPU_Init(u8 l) { GPU * g; - g = (GPU *) malloc(sizeof(GPU)); - memset(g, 0, sizeof(GPU)); + if ((g = (GPU *) malloc(sizeof(GPU))) == NULL) + return NULL; - g->lcd = l; - g->core = l; - g->BGSize[0][0] = g->BGSize[1][0] = g->BGSize[2][0] = g->BGSize[3][0] = 256; - g->BGSize[0][1] = g->BGSize[1][1] = g->BGSize[2][1] = g->BGSize[3][1] = 256; - g->dispBG[0] = g->dispBG[1] = g->dispBG[2] = g->dispBG[3] = TRUE; - - g->spriteRender = sprite1D; - - if(g->core == GPU_SUB) - { - g->oam = (OAM *)(ARM9Mem.ARM9_OAM + 0x400); - g->sprMem = ARM9Mem.ARM9_BOBJ; - } - else - { - g->oam = (OAM *)(ARM9Mem.ARM9_OAM); - g->sprMem = ARM9Mem.ARM9_AOBJ; - } + GPU_Reset(g, l); return g; } -void GPUDeInit(GPU * gpu) +void GPU_Reset(GPU *g, u8 l) +{ + memset(g, 0, sizeof(GPU)); + + g->lcd = l; + g->core = l; + g->BGSize[0][0] = g->BGSize[1][0] = g->BGSize[2][0] = g->BGSize[3][0] = 256; + g->BGSize[0][1] = g->BGSize[1][1] = g->BGSize[2][1] = g->BGSize[3][1] = 256; + g->dispBG[0] = g->dispBG[1] = g->dispBG[2] = g->dispBG[3] = TRUE; + + g->spriteRender = sprite1D; + + if(g->core == GPU_SUB) + { + g->oam = (OAM *)(ARM9Mem.ARM9_OAM + 0x400); + g->sprMem = ARM9Mem.ARM9_BOBJ; + } + else + { + g->oam = (OAM *)(ARM9Mem.ARM9_OAM); + g->sprMem = ARM9Mem.ARM9_AOBJ; + } +} + +void GPU_DeInit(GPU * gpu) { free(gpu); } @@ -1470,12 +1477,17 @@ void sprite2D(GPU * gpu, u16 l, u16 * dst, u8 * prioTab) } } -void ScreenInit(void) { - MainScreen.gpu = GPUInit(0); - SubScreen.gpu = GPUInit(1); +void Screen_Init(void) { + MainScreen.gpu = GPU_Init(0); + SubScreen.gpu = GPU_Init(1); } -void ScreenDeInit(void) { - GPUDeInit(MainScreen.gpu); - GPUDeInit(SubScreen.gpu); +void Screen_Reset(void) { + GPU_Reset(MainScreen.gpu, 0); + GPU_Reset(SubScreen.gpu, 1); +} + +void Screen_DeInit(void) { + GPU_DeInit(MainScreen.gpu); + GPU_DeInit(SubScreen.gpu); } diff --git a/desmume/src/GPU.h b/desmume/src/GPU.h index cdd1205f5..d303ffabe 100644 --- a/desmume/src/GPU.h +++ b/desmume/src/GPU.h @@ -97,8 +97,9 @@ struct _GPU extern u16 GPU_screen[2*256*192]; -GPU * GPUInit(u8 l); -void GPUDeInit(GPU *); +GPU * GPU_Init(u8 l); +void GPU_Reset(GPU *g, u8 l); +void GPU_DeInit(GPU *); void textBG(GPU * gpu, u8 num, u16 * DST); void rotBG(GPU * gpu, u8 num, u16 * DST); @@ -119,8 +120,9 @@ typedef struct { extern Screen MainScreen; extern Screen SubScreen; -void ScreenInit(void); -void ScreenDeInit(void); +void Screen_Init(void); +void Screen_Reset(void); +void Screen_DeInit(void); static INLINE void GPU_ligne(Screen * screen, u16 l) { diff --git a/desmume/src/MMU.c b/desmume/src/MMU.c index d676967c1..488bb6aaf 100644 --- a/desmume/src/MMU.c +++ b/desmume/src/MMU.c @@ -233,7 +233,7 @@ u32 MMU_ARM7_WAIT32[16]={ 1, 1, 1, 1, 1, 1, 1, 1, 8, 8, 5, 1, 1, 1, 1, 1, }; -void MMUInit(void) { +void MMU_Init(void) { int i; LOG("MMU init\n"); @@ -271,7 +271,7 @@ void MMUInit(void) { mc_alloc(&MMU.bupmem, 65536); // For now we're use 512Kbit support. Eventually this should be detected when rom is loaded } -void MMUDeInit(void) { +void MMU_DeInit(void) { LOG("MMU deinit\n"); } diff --git a/desmume/src/NDSSystem.c b/desmume/src/NDSSystem.c index b0bec797a..f3af8146f 100644 --- a/desmume/src/NDSSystem.c +++ b/desmume/src/NDSSystem.c @@ -24,41 +24,34 @@ NDSSystem nds; -void NDSInit(void) { +void NDS_Init(void) { nds.ARM9Cycle = 0; nds.ARM7Cycle = 0; nds.cycles = 0; - MMUInit(); + MMU_Init(); nds.nextHBlank = 3168; nds.VCount = 0; nds.lignerendu = FALSE; - ScreenInit(); + Screen_Init(); armcpu_new(&NDS_ARM7,1); armcpu_new(&NDS_ARM9,0); } -void NDSDeInit(void) { +void NDS_DeInit(void) { if(MMU.CART_ROM != MMU.UNUSED_RAM) - { - // free(MMU.CART_ROM); IT'S UP TO THE GUI TO LOAD/UNLOAD AND MALLOC/FREE ROM >( - MMU_unsetRom(); - } + NDS_FreeROM(); + nds.nextHBlank = 3168; - ScreenDeInit(); - MMUDeInit(); + Screen_DeInit(); + MMU_DeInit(); } -BOOL NDS_loadROM(u8 * rom, u32 mask) +BOOL NDS_SetROM(u8 * rom, u32 mask) { u32 i; - if(MMU.CART_ROM != MMU.UNUSED_RAM) - { - // free(MMU.CART_ROM); IT'S UP TO THE GUI TO LOAD/UNLOAD AND MALLOC/FREE ROM >( - MMU_unsetRom(); - } MMU_setRom(rom, mask); NDS_header * header = (NDS_header *)MMU.CART_ROM; @@ -219,3 +212,98 @@ void debug() //if(NDS_ARM9.instruction==0) execute = FALSE; //if((NDS_ARM9.R[15]>>28)) execute = FALSE; } + +#define DSGBA_EXTENSTION ".ds.gba" +#define DSGBA_LOADER_SIZE 512 +enum +{ + ROM_NDS = 0, + ROM_DSGBA +}; + +int NDS_LoadROM(const char *filename) +{ + int i; + int type; + const char *p = filename; + FILE *file; + u32 size, mask; + u8 *data; + + if (filename == NULL) + return -1; + + type = ROM_NDS; + + p += strlen(p); + p -= strlen(DSGBA_EXTENSTION); + + if(memcmp(p, DSGBA_EXTENSTION, strlen(DSGBA_EXTENSTION)) == 0) + type = ROM_DSGBA; + + if ((file = fopen(filename, "rb")) == NULL) + return -1; + + fseek(file, 0, SEEK_END); + size = ftell(file); + fseek(file, 0, SEEK_SET); + + if(type == ROM_DSGBA) + { + fseek(file, DSGBA_LOADER_SIZE, SEEK_SET); + size -= DSGBA_LOADER_SIZE; + } + + mask = size; + mask |= (mask >>1); + mask |= (mask >>2); + mask |= (mask >>4); + mask |= (mask >>8); + mask |= (mask >>16); + + // Make sure old ROM is freed first(at least this way we won't be eating + // up a ton of ram before the old ROM is freed) + if(MMU.CART_ROM != MMU.UNUSED_RAM) + NDS_FreeROM(); + + if ((data = (u8*)malloc(mask + 1)) == NULL) + { + fclose(file); + return -1; + } + + i = fread(data, 1, size, file); + + fclose(file); + + MMU_unsetRom(); + NDS_Reset(); + NDS_SetROM(data, mask); + +/* // Will be added later + strcpy(szRomPath, dirname((char *) filename)); + cflash_close(); + cflash_init(); +*/ + + return i; +} + +void NDS_FreeROM(void) +{ + if (MMU.CART_ROM != MMU.UNUSED_RAM) + free(MMU.CART_ROM); + MMU_unsetRom(); +} + +void NDS_Reset(void) +{ + BOOL oldexecute=execute; + int i; + + execute = FALSE; + + // Reset emulation here + + execute = oldexecute; +} diff --git a/desmume/src/NDSSystem.h b/desmume/src/NDSSystem.h index bdeeb0e63..19d5ca5e8 100644 --- a/desmume/src/NDSSystem.h +++ b/desmume/src/NDSSystem.h @@ -183,12 +183,16 @@ extern NDSSystem nds; void NDSInit(void); void NDSDeInit(void); -BOOL NDS_loadROM(u8 * rom, u32 mask); +BOOL NDS_SetROM(u8 * rom, u32 mask); NDS_header * NDS_getROMHeader(void); void NDS_setTouchPos(u16 x, u16 y); void NDS_releasTouch(void); - + +int NDS_LoadROM(const char *filename); +void NDS_FreeROM(void); +void NDS_Reset(void); + static INLINE void NDS_ARM9HBlankInt(void) { if(((u16 *)ARM9Mem.ARM9_REG)[0x0004>>1]&0x10) diff --git a/desmume/src/windows/main.c b/desmume/src/windows/main.c index 4ed799f8a..eff377309 100644 --- a/desmume/src/windows/main.c +++ b/desmume/src/windows/main.c @@ -38,7 +38,7 @@ #include "../debug.h" #include "mapview.h" #include "../saves.h" -#include "cflash.h" +#include "../cflash.h" #include "ConfigKeys.h" #ifdef RENDER3D @@ -101,7 +101,7 @@ DWORD WINAPI run( LPVOID lpParameter) QueryPerformanceFrequency((LARGE_INTEGER *)&freq); QueryPerformanceCounter((LARGE_INTEGER *)&count); nextcount = count + freq; - + while(!finished) { while(execute) @@ -139,59 +139,13 @@ DWORD WINAPI run( LPVOID lpParameter) } BOOL LoadROM(char * filename) -{ - HANDLE File; - u32 FileSize; - u32 nblu = 0; - u32 mask; - u8 *ROM; - - if(!strlen(filename)) return FALSE; - - if((*filename)=='\"') - { - ++filename; - filename[strlen(filename)-1] = '\0'; - } - - if(strncmp(filename + strlen(filename)-3, "nds", 3)) - { - MessageBox(hwnd,"Error, not a nds file","Error",MB_OK); - return FALSE; - } - - File = CreateFile(filename, - GENERIC_READ | FILE_FLAG_OVERLAPPED, - FILE_SHARE_READ, - NULL,OPEN_EXISTING, - FILE_ATTRIBUTE_NORMAL, - NULL); - - if(File == INVALID_HANDLE_VALUE) - { - MessageBox(hwnd,"Error opening the file","Error",MB_OK); - return FALSE; - } - +{ execute = FALSE; - - mask = FileSize = GetFileSize(File, NULL) - 1; - mask |= (mask >>1); - mask |= (mask >>2); - mask |= (mask >>4); - mask |= (mask >>8); - mask |= (mask >>16); - if ((ROM = (u8 *)malloc((mask+1)*sizeof(u8))) == NULL) - { - CloseHandle(File); - return FALSE; - } + if (NDS_LoadROM(filename) > 0) + return TRUE; - ReadFile(File, ROM, FileSize, &nblu, NULL); - NDS_loadROM(ROM, mask); - CloseHandle(File); - return TRUE; + return FALSE; } int WriteBMP(const char *filename,u16 *bmp){ @@ -283,7 +237,7 @@ int WINAPI WinMain (HINSTANCE hThisInstance, LogStart(); #endif - NDSInit(); + NDS_Init(); //ARM7 BIOS IRQ HANDLER MMU_writeWord(1, 0x00, 0xE25EF002); @@ -317,8 +271,18 @@ int WINAPI WinMain (HINSTANCE hThisInstance, CreateThread(NULL, 0, run, NULL, 0, &threadID); - if(LoadROM(lpszArgument)) execute = TRUE; - + if(LoadROM(lpszArgument)) + { + EnableMenuItem(menu, IDM_EXEC, MF_GRAYED); + EnableMenuItem(menu, IDM_PAUSE, MF_ENABLED); + execute = TRUE; + } + else + { + EnableMenuItem(menu, IDM_EXEC, MF_ENABLED); + EnableMenuItem(menu, IDM_PAUSE, MF_GRAYED); + } + while (GetMessage (&messages, NULL, 0, 0)) { // Translate virtual-key messages into character messages @@ -356,7 +320,12 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM char filename[MAX_PATH] = ""; DragQueryFile((HDROP)wParam,0,filename,MAX_PATH); DragFinish((HDROP)wParam); - if(LoadROM(filename)) execute = TRUE; + if(LoadROM(filename)) + { + EnableMenuItem(menu, IDM_EXEC, MF_GRAYED); + EnableMenuItem(menu, IDM_PAUSE, MF_ENABLED); + execute = TRUE; + } } return 0; case WM_KEYDOWN: @@ -534,20 +503,28 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM } LOG("%s\r\n", filename); - + +/* // This should be moved to NDSSystem.c + // Added for FAT generation // Mic if (ofn.nFileOffset>0) { strncpy(szRomPath,filename,ofn.nFileOffset-1); cflash_close(); cflash_init(); - } + } strcpy(SavName,filename); romnum+=1; +*/ - if(LoadROM(filename)) execute = FALSE; + if(LoadROM(filename)) + { + EnableMenuItem(menu, IDM_EXEC, MF_GRAYED); + EnableMenuItem(menu, IDM_PAUSE, MF_ENABLED); + execute = TRUE; + } } return 0; case IDM_PRINTSCREEN: @@ -803,12 +780,17 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM } return 0; case IDM_EXEC: + EnableMenuItem(menu, IDM_EXEC, MF_GRAYED); + EnableMenuItem(menu, IDM_PAUSE, MF_ENABLED); execute = TRUE; return 0; case IDM_PAUSE: + EnableMenuItem(menu, IDM_EXEC, MF_ENABLED); + EnableMenuItem(menu, IDM_PAUSE, MF_GRAYED); execute = FALSE; return 0; case IDM_RESET: + NDS_Reset(); return 0; case IDM_CONFIG: { @@ -825,7 +807,7 @@ LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM return DefWindowProc (hwnd, message, wParam, lParam); } - NDSDeInit(); + NDS_DeInit(); return 0; } diff --git a/desmume/src/windows/oamView.c b/desmume/src/windows/oamView.c index e409d95fd..9d14b219b 100644 --- a/desmume/src/windows/oamView.c +++ b/desmume/src/windows/oamView.c @@ -23,7 +23,7 @@ #include #include "resource.h" #include "oamView.h" -#include "../arm9/GPU.h" +#include "../GPU.h" #include "../MMU.h" diff --git a/desmume/src/windows/oamView.h b/desmume/src/windows/oamView.h index 5a2b2868f..784372aa7 100644 --- a/desmume/src/windows/oamView.h +++ b/desmume/src/windows/oamView.h @@ -23,7 +23,7 @@ #define OAMVIEW_H #include "CWindow.h" -#include "../arm9/GPU.h" +#include "../GPU.h" typedef struct {