linux compat

This commit is contained in:
2025-06-13 15:33:13 -06:00
parent 84952c134c
commit 800dcb2706
7 changed files with 74 additions and 19 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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() {

View File

@ -288,6 +288,7 @@ bool cart_load(char *cart) {
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) {

View File

@ -16,10 +16,15 @@
#else
#include <pthread.h>
#include <unistd.h>
#include <sys/stat.h>
#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();

View File

@ -1,5 +1,10 @@
#include <state.h>
#include <string.h>
#ifdef __windows__
#include <windows.h>
#else
#include <sys/stat.h>
#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) {

View File

@ -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());
}