Skip to content

Commit c545943

Browse files
committed
rg_system: rg_system_init now takes a config struct
This will allow adding configuration options in the future without ever changing the function's signature.
1 parent a67f5eb commit c545943

File tree

10 files changed

+185
-150
lines changed

10 files changed

+185
-150
lines changed

components/retro-go/rg_system.c

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ static void system_monitor_task(void *arg)
278278

279279
// Auto frameskip
280280
// TODO: Use a rolling average of frameTimes instead of this mess
281-
if (app.tickRate > 0 && statistics.ticks > app.tickRate * 2)
281+
if (app.tickRate > 0 && statistics.ticks > app.tickRate * 2 && app.frameskip > -1) // -1 disables auto frameskip
282282
{
283283
float speed = statistics.speedPercent / app.speed;
284284
// We don't fully go back to 0 frameskip because if we dip below 95% once, we're clearly
@@ -379,20 +379,7 @@ static void platform_init(void)
379379
#endif
380380
}
381381

382-
rg_app_t *rg_system_reinit(int sampleRate, const rg_handlers_t *handlers, void *_unused)
383-
{
384-
if (!app.initialized)
385-
return rg_system_init(sampleRate, handlers, NULL);
386-
387-
app.sampleRate = sampleRate;
388-
if (handlers)
389-
app.handlers = *handlers;
390-
rg_audio_set_sample_rate(app.sampleRate);
391-
392-
return &app;
393-
}
394-
395-
rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_unused)
382+
rg_app_t *rg_system_init(const rg_config_t *config)
396383
{
397384
RG_ASSERT(app.initialized == false, "rg_system_init() was already called.");
398385
bool enterRecoveryMode = false;
@@ -408,7 +395,7 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
408395
.bootFlags = 0,
409396
.indicatorsMask = (1 << RG_INDICATOR_POWER_LOW),
410397
.speed = 1.f,
411-
.sampleRate = sampleRate,
398+
.sampleRate = 0,
412399
.tickRate = 60,
413400
.tickTimeout = 3000000,
414401
.frameTime = 1000000 / 60,
@@ -498,20 +485,40 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
498485
app.romPath = app.bootArgs ?: ""; // For whatever reason some of our code isn't NULL-aware, sigh..
499486

500487
rg_gui_draw_hourglass();
501-
rg_audio_init(sampleRate);
502488

503-
rg_system_set_timezone(rg_settings_get_string(NS_GLOBAL, SETTING_TIMEZONE, "EST+5"));
504-
rg_system_load_time();
489+
if (config)
490+
{
491+
app.sampleRate = config->sampleRate;
492+
app.tickRate = config->frameRate;
493+
// app.frameskip = config->frameSkip;
494+
if (config->mallocAlwaysInternal > 0)
495+
{
496+
#ifdef ESP_PLATFORM
497+
heap_caps_malloc_extmem_enable(config->mallocAlwaysInternal);
498+
#endif
499+
}
500+
if (config->storageRequired && !rg_storage_ready())
501+
{
502+
rg_display_clear(C_SKY_BLUE);
503+
rg_gui_alert(_("SD Card Error"), _("Storage mount failed.\nMake sure the card is FAT32."));
504+
rg_system_exit();
505+
}
506+
if (config->romRequired && !app.romPath && !*app.romPath)
507+
{
508+
// show rom picking dialog
509+
}
510+
app.isLauncher = config->isLauncher;
511+
app.handlers = config->handlers;
512+
}
505513

506-
// Do these last to not interfere with panic handling above
507-
if (handlers)
508-
app.handlers = *handlers;
514+
if (app.sampleRate > 0)
515+
{
516+
rg_audio_init(app.sampleRate);
517+
}
509518

510-
#ifdef RG_ENABLE_PROFILING
511-
RG_LOGI("Profiling has been enabled at compile time!\n");
512-
profile = rg_alloc(sizeof(*profile), MEM_SLOW);
513-
profile->lock = rg_mutex_create();
514-
#endif
519+
rg_system_set_tick_rate(app.tickRate);
520+
rg_system_set_timezone(rg_settings_get_string(NS_GLOBAL, SETTING_TIMEZONE, "EST+5"));
521+
rg_system_load_time();
515522

516523
if (app.lowMemoryMode)
517524
rg_gui_alert("External memory not detected", "Boot will continue but it will surely crash...");
@@ -522,6 +529,12 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
522529
rg_task_create("rg_sysmon", &system_monitor_task, NULL, 3 * 1024, RG_TASK_PRIORITY_5, -1);
523530
app.initialized = true;
524531

532+
#ifdef RG_ENABLE_PROFILING
533+
RG_LOGI("Profiling has been enabled at compile time!");
534+
profile = rg_alloc(sizeof(*profile), MEM_SLOW);
535+
profile->lock = rg_mutex_create();
536+
#endif
537+
525538
update_memory_statistics();
526539
RG_LOGI("Available memory: %d/%d + %d/%d", statistics.freeMemoryInt / 1024, statistics.totalMemoryInt / 1024,
527540
statistics.freeMemoryExt / 1024, statistics.totalMemoryExt / 1024);
@@ -530,6 +543,16 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
530543
return &app;
531544
}
532545

546+
rg_app_t *rg_system_reinit(int sampleRate, const rg_handlers_t *handlers, void *_unused)
547+
{
548+
RG_ASSERT(app.initialized, "App not initialized");
549+
rg_audio_set_sample_rate(app.sampleRate);
550+
app.sampleRate = sampleRate;
551+
if (handlers)
552+
app.handlers = *handlers;
553+
return &app;
554+
}
555+
533556
// FIXME: None of this is threadsafe. It works for now, but eventually it needs fixing...
534557

535558
#ifdef ESP_PLATFORM
@@ -810,7 +833,10 @@ rg_stats_t rg_system_get_stats(void)
810833
void rg_system_set_tick_rate(int tickRate)
811834
{
812835
app.tickRate = tickRate;
813-
app.frameTime = 1000000 / (app.tickRate * app.speed);
836+
if (tickRate > 0)
837+
app.frameTime = 1000000 / (app.tickRate * app.speed);
838+
else
839+
app.frameTime = 1000000;
814840
}
815841

816842
int rg_system_get_tick_rate(void)

components/retro-go/rg_system.h

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,20 @@ typedef struct
133133
void (*about)(rg_gui_option_t *dest); // Add extra options to rg_gui_about_menu()
134134
} rg_handlers_t;
135135

136+
// Note: Always make sure that a value of 0 represents a reasonable default...
137+
// For example: Do not add `enableThing` if it must default to `1` when unspecified
138+
// Instead, consider adding `disableThing`.
136139
typedef struct
137140
{
138-
uint8_t id;
139-
bool is_used;
140-
bool is_lastused;
141-
size_t size;
142-
time_t mtime;
143-
char preview[RG_PATH_MAX];
144-
char file[RG_PATH_MAX];
145-
} rg_emu_slot_t;
146-
147-
typedef struct
148-
{
149-
size_t total;
150-
size_t used;
151-
rg_emu_slot_t *lastused;
152-
rg_emu_slot_t *latest;
153-
rg_emu_slot_t slots[];
154-
} rg_emu_states_t;
141+
int sampleRate;
142+
int frameRate; // tickRate
143+
// int frameSkip;
144+
int mallocAlwaysInternal;
145+
bool storageRequired;
146+
bool romRequired;
147+
bool isLauncher;
148+
rg_handlers_t handlers;
149+
} rg_config_t;
155150

156151
typedef struct
157152
{
@@ -203,7 +198,7 @@ typedef struct
203198
int freeStackMain;
204199
} rg_stats_t;
205200

206-
rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_unused);
201+
rg_app_t *rg_system_init(const rg_config_t *config);
207202
rg_app_t *rg_system_reinit(int sampleRate, const rg_handlers_t *handlers, void *_unused);
208203
void rg_system_panic(const char *context, const char *message) __attribute__((noreturn));
209204
void rg_system_shutdown(void) __attribute__((noreturn));
@@ -229,6 +224,7 @@ bool rg_system_save_trace(const char *filename, bool append);
229224
void rg_system_event(int event, void *data);
230225
int64_t rg_system_timer(void);
231226
rg_app_t *rg_system_get_app(void);
227+
// rg_config_t rg_system_get_config(void);
232228
rg_stats_t rg_system_get_stats(void);
233229

234230
// Speed and Overclock
@@ -276,6 +272,26 @@ void rg_mutex_free(rg_mutex_t *mutex);
276272
bool rg_mutex_give(rg_mutex_t *mutex);
277273
bool rg_mutex_take(rg_mutex_t *mutex, int timeoutMS);
278274

275+
typedef struct
276+
{
277+
uint8_t id;
278+
bool is_used;
279+
bool is_lastused;
280+
size_t size;
281+
time_t mtime;
282+
char preview[RG_PATH_MAX];
283+
char file[RG_PATH_MAX];
284+
} rg_emu_slot_t;
285+
286+
typedef struct
287+
{
288+
size_t total;
289+
size_t used;
290+
rg_emu_slot_t *lastused;
291+
rg_emu_slot_t *latest;
292+
rg_emu_slot_t slots[];
293+
} rg_emu_states_t;
294+
279295
char *rg_emu_get_path(rg_path_type_t type, const char *arg);
280296
bool rg_emu_save_state(uint8_t slot);
281297
bool rg_emu_load_state(uint8_t slot);

fmsx/main/main.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -419,18 +419,19 @@ static void options_handler(rg_gui_option_t *dest)
419419

420420
void app_main(void)
421421
{
422-
const rg_handlers_t handlers = {
423-
.loadState = &load_state_handler,
424-
.saveState = &save_state_handler,
425-
.reset = &reset_handler,
426-
.screenshot = &screenshot_handler,
427-
.event = &event_handler,
428-
.options = &options_handler,
429-
};
430-
431-
app = rg_system_init(AUDIO_SAMPLE_RATE, &handlers, NULL);
432-
// This is probably not right, but the emulator outputs 440 samples per frame??
433-
rg_system_set_tick_rate(55);
422+
app = rg_system_init(&(const rg_config_t){
423+
.sampleRate = AUDIO_SAMPLE_RATE,
424+
.frameRate = 55, // This is probably not right, but the emulator outputs 440 samples per frame??
425+
.storageRequired = true,
426+
.handlers = {
427+
.loadState = &load_state_handler,
428+
.saveState = &save_state_handler,
429+
.reset = &reset_handler,
430+
.screenshot = &screenshot_handler,
431+
.event = &event_handler,
432+
.options = &options_handler,
433+
},
434+
});
434435

435436
updates[0] = rg_surface_create(WIDTH, HEIGHT, RG_PIXEL_565_BE, MEM_FAST);
436437
updates[1] = rg_surface_create(WIDTH, HEIGHT, RG_PIXEL_565_BE, MEM_FAST);

gbsp/main/main.c

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,17 +119,20 @@ static void options_handler(rg_gui_option_t *dest)
119119

120120
void app_main(void)
121121
{
122-
const rg_handlers_t handlers = {
123-
.loadState = &load_state_handler,
124-
.saveState = &save_state_handler,
125-
.reset = &reset_handler,
126-
.screenshot = &screenshot_handler,
127-
.event = &event_handler,
128-
.options = &options_handler,
129-
};
130-
131-
app = rg_system_init(AUDIO_SAMPLE_RATE, &handlers, NULL);
132-
// app = rg_system_init(AUDIO_SAMPLE_RATE * 0.7, &handlers, NULL);
122+
app = rg_system_init(&(const rg_config_t){
123+
.sampleRate = AUDIO_SAMPLE_RATE,
124+
.frameRate = 60,
125+
.storageRequired = true,
126+
.romRequired = true,
127+
.handlers = {
128+
.loadState = &load_state_handler,
129+
.saveState = &save_state_handler,
130+
.reset = &reset_handler,
131+
.screenshot = &screenshot_handler,
132+
.event = &event_handler,
133+
.options = &options_handler,
134+
},
135+
});
133136
// rg_system_set_overclock(2);
134137

135138
sound_master_enable = rg_settings_get_number(NS_APP, SETTING_SOUND_EMULATION, true);

gwenesis/main/main.c

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,20 @@ static void options_handler(rg_gui_option_t *dest)
251251

252252
void app_main(void)
253253
{
254-
const rg_handlers_t handlers = {
255-
.loadState = &load_state_handler,
256-
.saveState = &save_state_handler,
257-
.reset = &reset_handler,
258-
.screenshot = &screenshot_handler,
259-
.event = &event_handler,
260-
.options = &options_handler,
254+
const rg_config_t config = {
255+
.sampleRate = AUDIO_SAMPLE_RATE / 2,
256+
.frameRate = 60,
257+
.storageRequired = true,
258+
.romRequired = true,
259+
.handlers.loadState = &load_state_handler,
260+
.handlers.saveState = &save_state_handler,
261+
.handlers.reset = &reset_handler,
262+
.handlers.screenshot = &screenshot_handler,
263+
.handlers.event = &event_handler,
264+
.handlers.options = &options_handler,
261265
};
262-
263-
app = rg_system_init(AUDIO_SAMPLE_RATE / 2, &handlers, NULL);
266+
app = rg_system_init(&config);
267+
app->frameskip = 2;
264268

265269
yfm_enabled = rg_settings_get_number(NS_APP, SETTING_YFM_EMULATION, 1);
266270
sn76489_enabled = rg_settings_get_number(NS_APP, SETTING_SN76489_EMULATION, 0);
@@ -313,9 +317,6 @@ void app_main(void)
313317
rg_emu_load_state(app->saveSlot);
314318
}
315319

316-
rg_system_set_tick_rate(60);
317-
app->frameskip = 2;
318-
319320
extern unsigned char gwenesis_vdp_regs[0x20];
320321
extern unsigned int gwenesis_vdp_status;
321322
extern unsigned short CRAM565[256];

launcher/main/main.c

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@
55
#include <stdlib.h>
66
#include <string.h>
77

8-
#ifdef ESP_PLATFORM
9-
#include <esp_heap_caps.h>
10-
#endif
11-
128
#include "applications.h"
139
#include "bookmarks.h"
1410
#include "browser.h"
@@ -450,34 +446,24 @@ static void about_handler(rg_gui_option_t *dest)
450446

451447
void app_main(void)
452448
{
453-
const rg_handlers_t handlers = {
454-
.event = &event_handler,
455-
.options = &options_handler,
456-
.about = &about_handler,
457-
};
458-
459-
app = rg_system_init(32000, &handlers, NULL);
449+
app = rg_system_init(&(const rg_config_t){
450+
.sampleRate = 32000,
451+
.frameRate = 0,
452+
// The launcher makes a lot of small allocations and it sometimes fills internal RAM, causing the SD Card driver to
453+
// stop working. Lowering CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL and manually using rg_alloc to do internal allocs when
454+
// needed is a better solution, but that would have to be done for every app. This is a good workaround for now.
455+
.mallocAlwaysInternal = 1024,
456+
.storageRequired = true,
457+
.isLauncher = true,
458+
.handlers.event = &event_handler,
459+
.handlers.options = &options_handler,
460+
.handlers.about = &about_handler,
461+
});
460462
app->configNs = "launcher";
461-
app->isLauncher = true;
462463

463-
if (!rg_storage_ready())
464-
{
465-
rg_display_clear(C_SKY_BLUE);
466-
rg_gui_alert(_("SD Card Error"), _("Storage mount failed.\nMake sure the card is FAT32."));
467-
}
468-
else
469-
{
470-
rg_storage_mkdir(RG_BASE_PATH_CACHE);
471-
rg_storage_mkdir(RG_BASE_PATH_CONFIG);
472-
try_migrate();
473-
}
474-
475-
#ifdef ESP_PLATFORM
476-
// The launcher makes a lot of small allocations and it sometimes fills internal RAM, causing the SD Card driver to
477-
// stop working. Lowering CONFIG_SPIRAM_MALLOC_ALWAYSINTERNAL and manually using rg_alloc to do internal allocs when
478-
// needed is a better solution, but that would have to be done for every app. This is a good workaround for now.
479-
heap_caps_malloc_extmem_enable(1024);
480-
#endif
464+
rg_storage_mkdir(RG_BASE_PATH_CACHE);
465+
rg_storage_mkdir(RG_BASE_PATH_CONFIG);
466+
try_migrate();
481467

482468
retro_loop();
483469
}

0 commit comments

Comments
 (0)