From 3d99773ddbb1cb1cfd31e5903792e64778c6d7a6 Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Fri, 24 Feb 2017 15:14:00 +0200 Subject: [PATCH] Changed how the Cocoa port initializes GB_gameboy_t --- Cocoa/Document.m | 42 +++++++++++++++++++++--------------------- Core/gb.c | 8 +++++--- Core/gb.h | 7 ++++++- 3 files changed, 32 insertions(+), 25 deletions(-) diff --git a/Cocoa/Document.m b/Cocoa/Document.m index 575da10..7d3781e 100644 --- a/Cocoa/Document.m +++ b/Cocoa/Document.m @@ -110,7 +110,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, volatile bool stopping; NSConditionLock *has_debugger_input; NSMutableArray *debugger_input_queue; - volatile bool is_inited; } - (instancetype)init { @@ -118,12 +117,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, if (self) { has_debugger_input = [[NSConditionLock alloc] initWithCondition:0]; debugger_input_queue = [[NSMutableArray alloc] init]; - if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) { - [self initDMG]; - } - else { - [self initCGB]; - } } return self; } @@ -131,15 +124,15 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, - (void) initDMG { GB_init(&gb); - GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"dmg_boot" ofType:@"bin"] UTF8String]); [self initCommon]; + GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"dmg_boot" ofType:@"bin"] UTF8String]); } - (void) initCGB { GB_init_cgb(&gb); - GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"cgb_boot" ofType:@"bin"] UTF8String]); [self initCommon]; + GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"cgb_boot" ofType:@"bin"] UTF8String]); } @@ -153,6 +146,7 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, GB_set_rgb_encode_callback(&gb, rgbEncode); GB_set_camera_get_pixel_callback(&gb, cameraGetPixel); GB_set_camera_update_request_callback(&gb, cameraRequestUpdate); + [self loadROM]; } - (void) vblank @@ -213,7 +207,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, { bool was_cgb = gb.is_cgb; [self stop]; - is_inited = false; /* Back up user's breakpoints/watchpoints */ typeof(gb.breakpoints) breakpoints = gb.breakpoints; @@ -301,6 +294,13 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, [titleView addSubview: self.feedSaveButton]; self.feedSaveButton.frame = (NSRect){{268, 2}, {48, 17}}; + if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) { + [self initDMG]; + } + else { + [self initCGB]; + } + [self start]; } @@ -364,17 +364,17 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type { - if (is_inited++) { - return YES; - } - GB_load_rom(&gb, [fileName UTF8String]); - GB_load_battery(&gb, [[[fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); - GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]); - GB_debugger_load_symbol_file(&gb, [[[fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]); - return YES; } +- (void) loadROM +{ + GB_load_rom(&gb, [self.fileName UTF8String]); + GB_load_battery(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sav"] UTF8String]); + GB_debugger_load_symbol_file(&gb, [[[NSBundle mainBundle] pathForResource:@"registers" ofType:@"sym"] UTF8String]); + GB_debugger_load_symbol_file(&gb, [[[self.fileName stringByDeletingPathExtension] stringByAppendingPathExtension:@"sym"] UTF8String]); +} + - (void)close { [[NSUserDefaults standardUserDefaults] setInteger:self.mainWindow.frame.size.width forKey:@"LastWindowWidth"]; @@ -619,19 +619,19 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height, - (uint8_t) readMemory:(uint16_t)addr { - while (!is_inited); + while (!GB_is_inited(&gb)); return GB_read_memory(&gb, addr); } - (void) writeMemory:(uint16_t)addr value:(uint8_t)value { - while (!is_inited); + while (!GB_is_inited(&gb)); GB_write_memory(&gb, addr, value); } - (void) performAtomicBlock: (void (^)())block { - while (!is_inited); + while (!GB_is_inited(&gb)); bool was_running = running && !gb.debug_stopped; if (was_running) { [self stop]; diff --git a/Core/gb.c b/Core/gb.c index 0232cf4..6f6ec64 100755 --- a/Core/gb.c +++ b/Core/gb.c @@ -92,7 +92,6 @@ static char *default_async_input_callback(GB_gameboy_t *gb) void GB_init(GB_gameboy_t *gb) { memset(gb, 0, sizeof(*gb)); - gb->magic = (uintptr_t)'SAME'; gb->version = GB_STRUCT_VERSION; gb->ram = malloc(gb->ram_size = 0x2000); memset(gb->ram, 0, gb->ram_size); @@ -115,12 +114,12 @@ void GB_init(GB_gameboy_t *gb) gb->io_registers[GB_IO_OBP0] = gb->io_registers[GB_IO_OBP1] = 0xFF; gb->io_registers[GB_IO_JOYP] = 0xF; gb->io_registers[GB_IO_SC] = 0x7E; + gb->magic = (uintptr_t)'SAME'; } void GB_init_cgb(GB_gameboy_t *gb) { memset(gb, 0, sizeof(*gb)); - gb->magic = (uintptr_t)'SAME'; gb->version = GB_STRUCT_VERSION; gb->ram = malloc(gb->ram_size = 0x2000 * 8); memset(gb->ram, 0, gb->ram_size); @@ -140,10 +139,12 @@ void GB_init_cgb(GB_gameboy_t *gb) gb->io_registers[GB_IO_OBP0] = gb->io_registers[GB_IO_OBP1] = 0xFF; gb->io_registers[GB_IO_JOYP] = 0xF; gb->io_registers[GB_IO_SC] = 0x7C; + gb->magic = 'SAME'; } void GB_free(GB_gameboy_t *gb) { + gb->magic = 0; if (gb->ram) { free(gb->ram); } @@ -174,6 +175,7 @@ void GB_free(GB_gameboy_t *gb) gb->reversed_symbol_map.buckets[i] = next; } } + memset(gb, 0, sizeof(*gb)); } int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) @@ -551,4 +553,4 @@ void GB_disconnect_serial(GB_gameboy_t *gb) /* Reset any internally-emulated device. Currently, only the printer. */ memset(&gb->printer, 0, sizeof(gb->printer)); -} \ No newline at end of file +} diff --git a/Core/gb.h b/Core/gb.h index 1d8f341..84e1367 100644 --- a/Core/gb.h +++ b/Core/gb.h @@ -216,7 +216,7 @@ typedef struct GB_gameboy_s { /* The magic makes sure a state file is: - Indeed a SameBoy state file. - Has the same endianess has the current platform. */ - uint32_t magic; + volatile uint32_t magic; /* The version field makes sure we don't load save state files with a completely different structure. This happens when struct fields are removed/resized in an backward incompatible manner. */ uint32_t version; @@ -518,4 +518,9 @@ uint8_t GB_serial_get_data(GB_gameboy_t *gb); void GB_serial_set_data(GB_gameboy_t *gb, uint8_t data); void GB_disconnect_serial(GB_gameboy_t *gb); + +static inline bool GB_is_inited(GB_gameboy_t *gb) +{ + return gb->magic == 'SAME'; +} #endif /* GB_h */