diff --git a/include/cart.h b/include/cart.h index 5818b55..ff019d6 100644 --- a/include/cart.h +++ b/include/cart.h @@ -59,6 +59,7 @@ typedef struct { void cart_load_state(const cart_state*); void cart_save_state(cart_state*); +bool cart_init(); bool cart_load(char *cart); @@ -73,4 +74,4 @@ bool cart_need_save(); bool cart_battery_load(); bool cart_battery_save(); -u8 cart_get_rom_bank(); \ No newline at end of file +u8 cart_get_rom_bank(); diff --git a/include/cpu.h b/include/cpu.h index 6b2008c..2958334 100644 --- a/include/cpu.h +++ b/include/cpu.h @@ -67,7 +67,7 @@ u8 cpu_read_reg8(reg_type rt); void cpu_set_reg8(reg_type rt, u8 val); -void fetch_data(); +void fetch_data(cpu_context*); u8 cpu_get_ie_register(); void cpu_set_ie_register(u8 ie); @@ -79,4 +79,4 @@ void cpu_set_int_flags(u8 value); void inst_to_str(cpu_context *ctx, char *str); -void cpu_set_flags(cpu_context *ctx, int8_t z, int8_t n, int8_t h, int8_t c); \ No newline at end of file +void cpu_set_flags(cpu_context *ctx, int8_t z, int8_t n, int8_t h, int8_t c); diff --git a/lib/audio.c b/lib/audio.c index 0b9ba83..beccd6f 100644 --- a/lib/audio.c +++ b/lib/audio.c @@ -134,7 +134,7 @@ void audio_init(){ if (err != paNoError) goto error; output_parameters.device = Pa_GetDefaultOutputDevice(); if(output_parameters.device == paNoDevice) { - fprintf(stderr, "No default audio device!\n"); + fprintf(stderr, "No default audio device! Number of devices: %d\n", Pa_GetDeviceCount()); goto error; } output_parameters.channelCount = 2; @@ -157,6 +157,7 @@ void audio_init(){ error: Pa_Terminate(); fprintf(stderr, "portaudio stream error\n\tError Number: %d\n\tError Message: %s\n", err, Pa_GetErrorText(err)); + exit(1); } void sq1_sweep() { @@ -1004,4 +1005,4 @@ void audio_load_state(const audio_state* state) { ctx = state->ctx; ctx.left_audio_buffer = malloc(sizeof(float) * FRAMES_PER_BUFFER); ctx.right_audio_buffer = malloc(sizeof(float) * FRAMES_PER_BUFFER); -} \ No newline at end of file +} diff --git a/lib/cart.c b/lib/cart.c index 0a9d979..371eb7f 100644 --- a/lib/cart.c +++ b/lib/cart.c @@ -287,6 +287,7 @@ bool cart_load(char *cart) { if(ctx.battery) { cart_battery_load(); } + return true; } @@ -493,11 +494,21 @@ void cart_write(u16 address, u8 value){ } bool cart_battery_load(){ +#ifdef __windows__ char *filename = strrchr(ctx.filename, '\\'); +#else + char *filename = strrchr(ctx.filename, '/'); +#endif filename++; char fn[1048]; +#ifdef __windows__ char *profile = getenv("USERPROFILE"); sprintf(fn, "%s/Documents/gbemu/saves/%s.battery", profile, filename); +#else + char *profile = getenv("HOME"); + sprintf(fn, "%s/.config/gbemu/saves/%s.battery", profile, filename); +#endif + printf("loading %s\n", fn); FILE *fp = fopen(fn, "rb"); if(!fp) { @@ -510,11 +521,20 @@ bool cart_battery_load(){ } bool cart_battery_save(){ +#ifdef __windows__ char *filename = strrchr(ctx.filename, '\\'); +#else + char *filename = strrchr(ctx.filename, '/'); +#endif filename++; char fn[1048]; +#ifdef __windows__ char *profile = getenv("USERPROFILE"); sprintf(fn, "%s/Documents/gbemu/saves/%s.battery", profile, filename); +#else + char *profile = getenv("HOME"); + sprintf(fn, "%s/.config/gbemu/saves/%s.battery", profile, filename); +#endif FILE *fp = fopen(fn, "wb"); if(!fp) { diff --git a/lib/emu.c b/lib/emu.c index 3f21323..22c1966 100644 --- a/lib/emu.c +++ b/lib/emu.c @@ -16,10 +16,15 @@ #else #include #include +#include #endif static emu_context ctx; +#ifdef __windows__ static HANDLE thread; +#else +static pthread_t thread; +#endif emu_context *emu_get_context() { return &ctx; @@ -61,7 +66,12 @@ void sleep_ms(int milis) { void emu_stop() { ctx.running = false; +#ifdef __windows__ WaitForSingleObject(thread, INFINITE); +#else + pthread_join(thread, NULL); + +#endif } void emu_reset() { @@ -74,7 +84,11 @@ void emu_reset() { } void emu_start() { +#ifdef __windows__ thread = CreateThread(NULL, 0, cpu_run, NULL, 0, NULL); +#else + pthread_create(&thread, NULL, cpu_run, NULL); +#endif if(!thread) { fprintf(stderr, "Unable to create main CPU thread!\n"); } @@ -92,6 +106,7 @@ int emu_run(int argc, char **argv) { } char fn[1048]; +#ifdef __windows__ char *profile = getenv("USERPROFILE"); sprintf(fn, "%s\\Documents\\gbemu", profile); CreateDirectory(fn, NULL); @@ -99,21 +114,22 @@ int emu_run(int argc, char **argv) { CreateDirectory(fn, NULL); sprintf(fn, "%s\\Documents\\gbemu\\states", profile); CreateDirectory(fn, NULL); +#else + char *profile = getenv("HOME"); + sprintf(fn, "%s/.config/gbemu", profile); + mkdir(fn, 0755); + sprintf(fn, "%s/.config/gbemu/saves", profile); + mkdir(fn, 0755); + sprintf(fn, "%s/.config/gbemu/states", profile); + mkdir(fn, 0755); +#endif printf("Cart loaded..\n"); ctx.app_path = argv[0]; ui_init(); -#ifdef _WIN32 emu_reset(); emu_start(); -#else - pthread_t t1; - if(pthread_create(&t1, NULL, cpu_run, NULL)) { - fprintf(stderr, "Unable to create main CPU thread!\n"); - return -1; - } -#endif u32 prev_frame = 0; debug_init(); diff --git a/lib/state.c b/lib/state.c index c396f5d..b1ab269 100644 --- a/lib/state.c +++ b/lib/state.c @@ -1,5 +1,10 @@ #include +#include +#ifdef __windows__ #include +#else +#include +#endif void state_save(save_state* state) { printf("Saving state\n"); @@ -33,10 +38,17 @@ void state_save_file(u8 slot) { char *filename = strrchr(cart_get_context()->filename, '\\'); filename++; char fn[1048]; +#ifdef __windows__ char *profile = getenv("USERPROFILE"); sprintf(fn, "%s/Documents/gbemu/states/%s", profile, filename); CreateDirectory(fn, NULL); sprintf(fn, "%s/Documents/gbemu/states/%s/%d.state", profile, filename, slot); +#else + char *profile = getenv("HOME"); + sprintf(fn, "%s/.config/gbemu/states/%s", profile, filename); + mkdir(fn, 0755); + sprintf(fn, "%s/.config/gbemu/states/%s/%d.state", profile, filename, slot); +#endif FILE *fp = fopen(fn, "wb"); if(!fp) { @@ -53,8 +65,13 @@ void state_load_file(u8 slot) { char *filename = strrchr(cart_get_context()->filename, '\\'); filename++; char fn[1048]; +#ifdef __windows__ char *profile = getenv("USERPROFILE"); sprintf(fn, "%s/Documents/gbemu/states/%s/%d.state", profile, filename, slot); +#else + char *profile = getenv("HOME"); + sprintf(fn, "%s/.config/gbemu/states/%s/%d.state", profile, filename, slot); +#endif FILE *fp = fopen(fn, "rb"); if(!fp) { @@ -65,4 +82,4 @@ void state_load_file(u8 slot) { fread(state, sizeof(*state), 1, fp); fclose(fp); state_load(state); -} \ No newline at end of file +} diff --git a/lib/ui.c b/lib/ui.c index 74b23e9..601182c 100644 --- a/lib/ui.c +++ b/lib/ui.c @@ -46,11 +46,11 @@ void ui_init(){ //SDL_JoystickOpen(0); SDL_GameControllerOpen(0); - char buffer[1024]; - strcpy(buffer, emu_get_context()->app_path); - *strrchr(buffer, '\\') = 0; - strcat(buffer,"/Sans.ttf"); - sans = TTF_OpenFont(buffer, 24); + //char buffer[1024]; + //strcpy(buffer, emu_get_context()->app_path); + //*strrchr(buffer, '\\') = 0; + //strcat(buffer,"/Sans.ttf"); + sans = TTF_OpenFont("Sans.ttf", 24); if ( !sans ) { printf("Failed to load font: %s\n", TTF_GetError()); }