Skip to content

Commit 2b5f9ac

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 2b5f9ac

File tree

10 files changed

+192
-158
lines changed

10 files changed

+192
-158
lines changed

components/retro-go/rg_system.c

Lines changed: 61 additions & 36 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,10 +395,10 @@ 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,
412-
.tickRate = 60,
398+
.sampleRate = 0,
399+
.tickRate = 0,
413400
.tickTimeout = 3000000,
414-
.frameTime = 1000000 / 60,
401+
.frameTime = 1000000,
415402
.frameskip = 1, // This can be overriden on a per-app basis if needed, do not set 0 here!
416403
.lowMemoryMode = false,
417404
.enWatchdog = true,
@@ -491,30 +478,44 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
491478
memset(&panicTrace, 0, sizeof(panicTrace));
492479
panicTraceCleared = true;
493480

494-
update_memory_statistics();
495-
app.lowMemoryMode = statistics.totalMemoryExt == 0;
496-
497481
app.indicatorsMask = rg_settings_get_number(NS_GLOBAL, SETTING_INDICATOR_MASK, app.indicatorsMask);
498482
app.romPath = app.bootArgs ?: ""; // For whatever reason some of our code isn't NULL-aware, sigh..
499483

500484
rg_gui_draw_hourglass();
501-
rg_audio_init(sampleRate);
502485

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

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
511+
if (app.sampleRate > 0)
512+
{
513+
rg_audio_init(app.sampleRate);
514+
}
515515

516-
if (app.lowMemoryMode)
517-
rg_gui_alert("External memory not detected", "Boot will continue but it will surely crash...");
516+
rg_system_set_tick_rate(app.tickRate);
517+
rg_system_set_timezone(rg_settings_get_string(NS_GLOBAL, SETTING_TIMEZONE, "EST+5"));
518+
rg_system_load_time();
518519

519520
if (app.bootFlags & RG_BOOT_ONCE)
520521
update_boot_config(RG_APP_LAUNCHER, NULL, NULL, 0, 0);
@@ -523,13 +524,34 @@ rg_app_t *rg_system_init(int sampleRate, const rg_handlers_t *handlers, void *_u
523524
app.initialized = true;
524525

525526
update_memory_statistics();
527+
528+
app.lowMemoryMode = statistics.totalMemoryExt == 0;
529+
if (app.lowMemoryMode)
530+
rg_gui_alert("External memory not detected", "Boot will continue but it will surely crash...");
531+
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+
526538
RG_LOGI("Available memory: %d/%d + %d/%d", statistics.freeMemoryInt / 1024, statistics.totalMemoryInt / 1024,
527539
statistics.freeMemoryExt / 1024, statistics.totalMemoryExt / 1024);
528540
RG_LOGI("Retro-Go ready.\n\n");
529541

530542
return &app;
531543
}
532544

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

535557
#ifdef ESP_PLATFORM
@@ -810,7 +832,10 @@ rg_stats_t rg_system_get_stats(void)
810832
void rg_system_set_tick_rate(int tickRate)
811833
{
812834
app.tickRate = tickRate;
813-
app.frameTime = 1000000 / (app.tickRate * app.speed);
835+
if (tickRate > 0)
836+
app.frameTime = 1000000 / (app.tickRate * app.speed);
837+
else
838+
app.frameTime = 1000000;
814839
}
815840

816841
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];

0 commit comments

Comments
 (0)