Changed how the Cocoa port initializes GB_gameboy_t

This commit is contained in:
Lior Halphon 2017-02-24 15:14:00 +02:00
parent 39253bec80
commit 3d99773ddb
3 changed files with 32 additions and 25 deletions

View File

@ -110,7 +110,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
volatile bool stopping; volatile bool stopping;
NSConditionLock *has_debugger_input; NSConditionLock *has_debugger_input;
NSMutableArray *debugger_input_queue; NSMutableArray *debugger_input_queue;
volatile bool is_inited;
} }
- (instancetype)init { - (instancetype)init {
@ -118,12 +117,6 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
if (self) { if (self) {
has_debugger_input = [[NSConditionLock alloc] initWithCondition:0]; has_debugger_input = [[NSConditionLock alloc] initWithCondition:0];
debugger_input_queue = [[NSMutableArray alloc] init]; debugger_input_queue = [[NSMutableArray alloc] init];
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) {
[self initDMG];
}
else {
[self initCGB];
}
} }
return self; return self;
} }
@ -131,15 +124,15 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
- (void) initDMG - (void) initDMG
{ {
GB_init(&gb); GB_init(&gb);
GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"dmg_boot" ofType:@"bin"] UTF8String]);
[self initCommon]; [self initCommon];
GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"dmg_boot" ofType:@"bin"] UTF8String]);
} }
- (void) initCGB - (void) initCGB
{ {
GB_init_cgb(&gb); GB_init_cgb(&gb);
GB_load_boot_rom(&gb, [[[NSBundle mainBundle] pathForResource:@"cgb_boot" ofType:@"bin"] UTF8String]);
[self initCommon]; [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_rgb_encode_callback(&gb, rgbEncode);
GB_set_camera_get_pixel_callback(&gb, cameraGetPixel); GB_set_camera_get_pixel_callback(&gb, cameraGetPixel);
GB_set_camera_update_request_callback(&gb, cameraRequestUpdate); GB_set_camera_update_request_callback(&gb, cameraRequestUpdate);
[self loadROM];
} }
- (void) vblank - (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; bool was_cgb = gb.is_cgb;
[self stop]; [self stop];
is_inited = false;
/* Back up user's breakpoints/watchpoints */ /* Back up user's breakpoints/watchpoints */
typeof(gb.breakpoints) breakpoints = gb.breakpoints; 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]; [titleView addSubview: self.feedSaveButton];
self.feedSaveButton.frame = (NSRect){{268, 2}, {48, 17}}; self.feedSaveButton.frame = (NSRect){{268, 2}, {48, 17}};
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"EmulateDMG"]) {
[self initDMG];
}
else {
[self initCGB];
}
[self start]; [self start];
} }
@ -364,15 +364,15 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type - (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)type
{ {
if (is_inited++) {
return YES; 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 - (void)close
@ -619,19 +619,19 @@ static void printImage(GB_gameboy_t *gb, uint32_t *image, uint8_t height,
- (uint8_t) readMemory:(uint16_t)addr - (uint8_t) readMemory:(uint16_t)addr
{ {
while (!is_inited); while (!GB_is_inited(&gb));
return GB_read_memory(&gb, addr); return GB_read_memory(&gb, addr);
} }
- (void) writeMemory:(uint16_t)addr value:(uint8_t)value - (void) writeMemory:(uint16_t)addr value:(uint8_t)value
{ {
while (!is_inited); while (!GB_is_inited(&gb));
GB_write_memory(&gb, addr, value); GB_write_memory(&gb, addr, value);
} }
- (void) performAtomicBlock: (void (^)())block - (void) performAtomicBlock: (void (^)())block
{ {
while (!is_inited); while (!GB_is_inited(&gb));
bool was_running = running && !gb.debug_stopped; bool was_running = running && !gb.debug_stopped;
if (was_running) { if (was_running) {
[self stop]; [self stop];

View File

@ -92,7 +92,6 @@ static char *default_async_input_callback(GB_gameboy_t *gb)
void GB_init(GB_gameboy_t *gb) void GB_init(GB_gameboy_t *gb)
{ {
memset(gb, 0, sizeof(*gb)); memset(gb, 0, sizeof(*gb));
gb->magic = (uintptr_t)'SAME';
gb->version = GB_STRUCT_VERSION; gb->version = GB_STRUCT_VERSION;
gb->ram = malloc(gb->ram_size = 0x2000); gb->ram = malloc(gb->ram_size = 0x2000);
memset(gb->ram, 0, gb->ram_size); 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_OBP0] = gb->io_registers[GB_IO_OBP1] = 0xFF;
gb->io_registers[GB_IO_JOYP] = 0xF; gb->io_registers[GB_IO_JOYP] = 0xF;
gb->io_registers[GB_IO_SC] = 0x7E; gb->io_registers[GB_IO_SC] = 0x7E;
gb->magic = (uintptr_t)'SAME';
} }
void GB_init_cgb(GB_gameboy_t *gb) void GB_init_cgb(GB_gameboy_t *gb)
{ {
memset(gb, 0, sizeof(*gb)); memset(gb, 0, sizeof(*gb));
gb->magic = (uintptr_t)'SAME';
gb->version = GB_STRUCT_VERSION; gb->version = GB_STRUCT_VERSION;
gb->ram = malloc(gb->ram_size = 0x2000 * 8); gb->ram = malloc(gb->ram_size = 0x2000 * 8);
memset(gb->ram, 0, gb->ram_size); 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_OBP0] = gb->io_registers[GB_IO_OBP1] = 0xFF;
gb->io_registers[GB_IO_JOYP] = 0xF; gb->io_registers[GB_IO_JOYP] = 0xF;
gb->io_registers[GB_IO_SC] = 0x7C; gb->io_registers[GB_IO_SC] = 0x7C;
gb->magic = 'SAME';
} }
void GB_free(GB_gameboy_t *gb) void GB_free(GB_gameboy_t *gb)
{ {
gb->magic = 0;
if (gb->ram) { if (gb->ram) {
free(gb->ram); free(gb->ram);
} }
@ -174,6 +175,7 @@ void GB_free(GB_gameboy_t *gb)
gb->reversed_symbol_map.buckets[i] = next; gb->reversed_symbol_map.buckets[i] = next;
} }
} }
memset(gb, 0, sizeof(*gb));
} }
int GB_load_boot_rom(GB_gameboy_t *gb, const char *path) int GB_load_boot_rom(GB_gameboy_t *gb, const char *path)

View File

@ -216,7 +216,7 @@ typedef struct GB_gameboy_s {
/* The magic makes sure a state file is: /* The magic makes sure a state file is:
- Indeed a SameBoy state file. - Indeed a SameBoy state file.
- Has the same endianess has the current platform. */ - 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. /* 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. */ This happens when struct fields are removed/resized in an backward incompatible manner. */
uint32_t version; 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_serial_set_data(GB_gameboy_t *gb, uint8_t data);
void GB_disconnect_serial(GB_gameboy_t *gb); 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 */ #endif /* GB_h */