Skip to content

Commit 72c98f4

Browse files
committed
rg_system: Removed app->lowMemoryMode
It's too arbitrary. Applications can decide for themselves if they want to disable features by checking `rg_system_get_stats().totalMemory` or `freeMemory`.
1 parent 1e2290b commit 72c98f4

File tree

9 files changed

+43
-42
lines changed

9 files changed

+43
-42
lines changed

components/retro-go/rg_system.c

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -162,19 +162,21 @@ static void update_memory_statistics(void)
162162
#ifdef ESP_PLATFORM
163163
multi_heap_info_t heap_info;
164164
heap_caps_get_info(&heap_info, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
165+
statistics.totalMemoryInt = heap_info.total_free_bytes + heap_info.total_allocated_bytes;
165166
statistics.freeMemoryInt = heap_info.total_free_bytes;
166167
statistics.freeBlockInt = heap_info.largest_free_block;
167-
statistics.totalMemoryInt = heap_info.total_free_bytes + heap_info.total_allocated_bytes;
168168
heap_caps_get_info(&heap_info, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
169+
statistics.totalMemoryExt = heap_info.total_free_bytes + heap_info.total_allocated_bytes;
169170
statistics.freeMemoryExt = heap_info.total_free_bytes;
170171
statistics.freeBlockExt = heap_info.largest_free_block;
171-
statistics.totalMemoryExt = heap_info.total_free_bytes + heap_info.total_allocated_bytes;
172-
172+
// FIXME: We should check all the tasks' HWM to be better informed!
173173
statistics.freeStackMain = uxTaskGetStackHighWaterMark(tasks[0].handle);
174174
#else
175-
statistics.freeMemoryInt = statistics.freeBlockInt = statistics.totalMemoryInt = (1 << 28);
176-
statistics.freeMemoryExt = statistics.freeBlockExt = statistics.totalMemoryExt = (1 << 28);
175+
statistics.freeMemoryInt = statistics.freeBlockInt = statistics.totalMemoryInt = 0x40000000;
177176
#endif
177+
statistics.totalMemory = statistics.totalMemoryInt + statistics.totalMemoryExt;
178+
statistics.freeMemory = statistics.freeMemoryInt + statistics.freeMemoryExt;
179+
statistics.freeBlock = RG_MAX(statistics.freeBlockInt, statistics.freeBlockExt);
178180
}
179181

180182
static void update_statistics(void)
@@ -401,7 +403,6 @@ rg_app_t *rg_system_init(const rg_config_t *config)
401403
.tickTimeout = 3000000,
402404
.frameTime = 1000000 / 60,
403405
.frameskip = 1, // This can be overriden on a per-app basis if needed, do not set 0 here!
404-
.lowMemoryMode = false,
405406
.enWatchdog = true,
406407
.isColdBoot = true,
407408
.isLauncher = false,
@@ -431,6 +432,7 @@ rg_app_t *rg_system_init(const rg_config_t *config)
431432
printf("%s %s (%s)\n", app.name, app.version, app.buildDate);
432433
printf(" built for: %s. type: %s\n", RG_TARGET_NAME, app.isRelease ? "release" : "dev");
433434
printf("========================================================\n\n");
435+
update_memory_statistics(); // Do this early in case any of our init routines needs to know
434436

435437
#ifdef RG_I2C_GPIO_DRIVER
436438
rg_i2c_init();
@@ -479,19 +481,13 @@ rg_app_t *rg_system_init(const rg_config_t *config)
479481
memset(&panicTrace, 0, sizeof(panicTrace));
480482
panicTraceCleared = true;
481483

482-
update_memory_statistics();
483-
app.lowMemoryMode = statistics.totalMemoryExt == 0;
484-
485484
app.indicatorsMask = rg_settings_get_number(NS_GLOBAL, SETTING_INDICATOR_MASK, app.indicatorsMask);
486485
app.romPath = app.bootArgs ?: ""; // For whatever reason some of our code isn't NULL-aware, sigh..
487486

488487
rg_gui_draw_hourglass();
489488

490489
if (config)
491490
{
492-
app.sampleRate = config->sampleRate;
493-
app.tickRate = config->frameRate;
494-
// app.frameskip = config->frameSkip;
495491
if (config->storageRequired && !rg_storage_ready())
496492
{
497493
rg_display_clear(C_SKY_BLUE);
@@ -503,31 +499,30 @@ rg_app_t *rg_system_init(const rg_config_t *config)
503499
// show rom picking dialog
504500
// app.romPath = rg_gui_file_picker(_("Choose ROM"), RG_BASE_PATH_ROMS, NULL, true, false);
505501
}
506-
if (config->mallocAlwaysInternal > 0)
507-
{
508502
#ifdef ESP_PLATFORM
503+
if (config->mallocAlwaysInternal > 0)
509504
heap_caps_malloc_extmem_enable(config->mallocAlwaysInternal);
510505
#endif
511-
}
506+
app.sampleRate = config->sampleRate;
507+
app.tickRate = config->frameRate;
508+
// app.frameskip = config->frameSkip;
512509
app.isLauncher = config->isLauncher;
513510
app.handlers = config->handlers;
514511
}
515512

516513
if (app.sampleRate > 0)
517-
{
518514
rg_audio_init(app.sampleRate);
519-
}
520515

521516
rg_system_set_tick_rate(app.tickRate);
522517
rg_system_set_timezone(rg_settings_get_string(NS_GLOBAL, SETTING_TIMEZONE, "EST+5"));
523518
rg_system_load_time();
524519

525-
if (app.lowMemoryMode)
526-
rg_gui_alert("External memory not detected", "Boot will continue but it will surely crash...");
527-
528520
if (app.bootFlags & RG_BOOT_ONCE)
529521
update_boot_config(RG_APP_LAUNCHER, NULL, NULL, 0, 0);
530522

523+
if (statistics.totalMemory < 0x200000)
524+
rg_gui_alert("External memory not detected", "Boot will continue but it will surely crash...");
525+
531526
rg_task_create("rg_sysmon", &system_monitor_task, NULL, 3 * 1024, RG_TASK_PRIORITY_5, -1);
532527
app.initialized = true;
533528

components/retro-go/rg_system.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,14 +138,16 @@ typedef struct
138138
// Instead, consider adding `disableThing`.
139139
typedef struct
140140
{
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;
141+
int sampleRate; // Audio sample rate
142+
int frameRate; // Frame rate (should match how often rg_system_tick is called per second)
143+
// int frameSkip; // Initial frame skip value (-1 to disable auto frameskip)
144+
// int tickRate; // For now retro-go doesn't distinguish between tick rate and frame rate...
145+
bool storageRequired; // Will refuse to continue if storage mount fails
146+
bool romRequired; // Will show a file picker if no ROM is configured
147+
bool isLauncher; // Set to true if app is launcher
148+
rg_handlers_t handlers; // App handlers for certain actions (save states, screenshot, etc)
149+
// Lower-level tweaks
150+
int mallocAlwaysInternal; // See heap_caps_malloc_extmem_enable
149151
} rg_config_t;
150152

151153
typedef struct
@@ -164,7 +166,6 @@ typedef struct
164166
int tickTimeout;
165167
int frameTime;
166168
int frameskip;
167-
bool lowMemoryMode;
168169
bool enWatchdog;
169170
bool isColdBoot;
170171
bool isLauncher;
@@ -191,10 +192,13 @@ typedef struct
191192
int uptime;
192193
int totalMemoryInt;
193194
int totalMemoryExt;
195+
int totalMemory;
194196
int freeMemoryInt;
195197
int freeMemoryExt;
198+
int freeMemory;
196199
int freeBlockInt;
197200
int freeBlockExt;
201+
int freeBlock;
198202
int freeStackMain;
199203
} rg_stats_t;
200204

fmsx/main/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,7 @@ void app_main(void)
423423
.sampleRate = AUDIO_SAMPLE_RATE,
424424
.frameRate = 55, // This is probably not right, but the emulator outputs 440 samples per frame??
425425
.storageRequired = true,
426+
.romRequired = false,
426427
.handlers = {
427428
.loadState = &load_state_handler,
428429
.saveState = &save_state_handler,

launcher/main/applications.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,6 @@ void applications_init(void)
697697
// Special app to bootstrap native esp32 binaries from the SD card
698698
// application("Bootstrap", "apps", "bin elf", "bootstrap", 0);
699699

700-
if (!rg_system_get_app()->lowMemoryMode)
700+
if (rg_system_get_stats().freeMemory > 0x100000)
701701
crc_cache_init();
702702
}

launcher/main/gui.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ void gui_init(bool cold_boot)
4545
gui.browse = gui.start_screen == START_SCREEN_BROWSER || (gui.start_screen == START_SCREEN_AUTO && !cold_boot);
4646
gui.theme = &gui.themes[gui.color_theme % RG_COUNT(gui.themes)];
4747
gui.http_lock = false;
48-
gui.low_memory_mode = rg_system_get_app()->lowMemoryMode;
4948
gui.surface = rg_surface_create(gui.width, gui.height, RG_PIXEL_565_LE, MEM_SLOW);
49+
gui.low_memory_mode = rg_system_get_stats().freeMemory < 0x100000;
5050
gui_update_theme();
5151
}
5252

launcher/main/main.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,15 +449,14 @@ void app_main(void)
449449
app = rg_system_init(&(const rg_config_t){
450450
.sampleRate = 32000,
451451
.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,
456452
.storageRequired = true,
457453
.isLauncher = true,
458454
.handlers.event = &event_handler,
459455
.handlers.options = &options_handler,
460456
.handlers.about = &about_handler,
457+
// The launcher makes a lot of small allocations and it sometimes fills internal RAM,
458+
// causing the SD Card driver to stop working.
459+
.mallocAlwaysInternal = 1024,
461460
});
462461
app->configNs = "launcher";
463462

prboom-go/main/main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -532,10 +532,6 @@ void app_main()
532532
const rg_config_t config = {
533533
.sampleRate = AUDIO_SAMPLE_RATE,
534534
.frameRate = TICRATE,
535-
// Some things might be nice to place in internal RAM, but I do not have time to find such
536-
// structures. So for now, prefer external RAM for most things except the framebuffer which
537-
// is allocated below.
538-
.mallocAlwaysInternal = 1, // I want 0 but 0 will be ignored, so 1 it is!
539535
.storageRequired = true,
540536
.romRequired = false,
541537
.handlers = {
@@ -546,6 +542,10 @@ void app_main()
546542
.event = &event_handler,
547543
.options = &options_handler,
548544
},
545+
// Some things might be nice to place in internal RAM, but I do not have time to find such
546+
// structures. So for now, prefer external RAM for most things except the framebuffer which
547+
// is allocated below.
548+
.mallocAlwaysInternal = 1, // I want 0 but 0 will be ignored, so 1 it is!
549549
};
550550
app = rg_system_init(&config);
551551

snes9x/main/main_snes.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,16 @@ void app_main(void)
344344
{
345345
const rg_config_t config = {
346346
.sampleRate = AUDIO_SAMPLE_RATE,
347+
.frameRate = 60, // Will be adjusted later if a PAL ROM is loaded
347348
.storageRequired = true,
348349
.romRequired = true,
349-
.mallocAlwaysInternal = 0x10000,
350350
.handlers.loadState = &load_state_handler,
351351
.handlers.saveState = &save_state_handler,
352352
.handlers.reset = &reset_handler,
353353
.handlers.screenshot = &screenshot_handler,
354354
.handlers.event = &event_handler,
355355
.handlers.options = &options_handler,
356+
.mallocAlwaysInternal = 0x10000,
356357
};
357358
app = rg_system_init(&config);
358359

tools/mkfw.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ def create_firmware(fw_type, partitions, icon_file, name, version, target):
6161

6262
def create_image(chip_type, partitions, bootloader_file, name, version, target):
6363
bootloader_offset, table_offset, prog_offset = {
64+
"esp32": (0x1000, 0x8000, 0x10000),
6465
"esp32s3": (0x0000, 0x8000, 0x10000),
6566
"esp32p4": (0x2000, 0x8000, 0x10000),
66-
"esp32": (0x1000, 0x8000, 0x10000),
6767
}.get(chip_type)
6868
partitions = [
6969
(1, 0x02, "nvs", 0x9000, 0x4000, b""),
@@ -90,7 +90,7 @@ def create_image(chip_type, partitions, bootloader_file, name, version, target):
9090
# (Maybe I could hide it somewhere else to avoid changing the size of the image)
9191
output_data += struct.pack(
9292
"<8s28s28s28sII156s",
93-
"RG_IMG_0".encode(), # Magic number "RG01"
93+
"RG_IMG_0".encode(), # Magic number
9494
name.encode(), # Project name
9595
version.encode(), # Project version
9696
target.encode(), # Project target device
@@ -119,6 +119,7 @@ def create_image(chip_type, partitions, bootloader_file, name, version, target):
119119
else:
120120
partitions = get_partitions(args.partitions, 0x10000)
121121
data = create_image(args.type, partitions, args.bootloader, args.name, args.version, args.target)
122+
122123
with open(args.output_file, "wb") as fp:
123124
fp.write(data)
124125

0 commit comments

Comments
 (0)