From 6725e410c15b12fd432af381eec97d34991edf38 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 13 Nov 2026 13:11:19 -0600 Subject: [PATCH 1/4] Genesis CPU setting (Normal / 125%) and wait-loop skip - Settings v2 (migrated from v1) adds genesis_cpu_percent; the launcher's settings get a "Genesis CPU" row (Normal / 125%), applied when a Genesis game starts. At 125% the 68000 finishes its frame work in time: Sonic 3 no longer drops to half speed for ~3 s when Sonic loses his rings, at the cost of a few drawn frames (the 68000 takes ~1 ms more per frame). - gwenesis: built with M68K_OVERCLOCK_SHIFT (the clock setting) and M68K_IDLE_SKIP (the 68000's wait-for-frame loops are not emulated, recovering most of the overclock's 68000 time). Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/genesis/CMakeLists.txt | 4 ++- components/genesis/gwenesis | 2 +- components/gui/include/gui.hpp | 6 ++++ components/gui/src/gui.cpp | 39 ++++++++++++++++++++++++ components/settings/include/settings.hpp | 29 ++++++++++++++++-- components/settings/src/settings.cpp | 4 +-- main/genesis_cart.hpp | 1 + main/main.cpp | 6 ++-- 8 files changed, 82 insertions(+), 9 deletions(-) diff --git a/components/genesis/CMakeLists.txt b/components/genesis/CMakeLists.txt index 32832989..441c1692 100644 --- a/components/genesis/CMakeLists.txt +++ b/components/genesis/CMakeLists.txt @@ -25,4 +25,6 @@ idf_component_register( # target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-char-subscripts -Wno-attributes -Wno-implicit-fallthrough -Wno-unused-function -Wno-unused-variable -Wno-discarded-qualifiers) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable -Wno-unused-value -Ofast) # target_compile_definitions(${COMPONENT_LIB} PRIVATE GWENESIS_AUDIO_ACCURATE=0 BUILD_TABLES=0 TABLES_FULL=0) -target_compile_definitions(${COMPONENT_LIB} PRIVATE GWENESIS_AUDIO_ACCURATE=0) +# M68K_OVERCLOCK_SHIFT: the 68000 clock setting (genesis_set_cpu_percent()); +# M68K_IDLE_SKIP: skip the 68000's wait-for-frame loops (see m68k_run()) +target_compile_definitions(${COMPONENT_LIB} PRIVATE GWENESIS_AUDIO_ACCURATE=0 M68K_OVERCLOCK_SHIFT=8 M68K_IDLE_SKIP) diff --git a/components/genesis/gwenesis b/components/genesis/gwenesis index d984bd5b..04e54777 160000 --- a/components/genesis/gwenesis +++ b/components/genesis/gwenesis @@ -1 +1 @@ -Subproject commit d984bd5b50d857bdcd2c814dfc4d58635d7c4d44 +Subproject commit 04e547771d57747907cca463df50aa1e454bd9e2 diff --git a/components/gui/include/gui.hpp b/components/gui/include/gui.hpp index 681e90d6..15ee4493 100644 --- a/components/gui/include/gui.hpp +++ b/components/gui/include/gui.hpp @@ -83,6 +83,9 @@ class Gui { void set_resume_last_save(bool on); + /// Genesis 68000 clock, % of stock (100 or 125) + void set_genesis_cpu_percent(int percent); + void clear_rom_list(); void add_rom(const RomInfo& rom); @@ -168,6 +171,7 @@ class Gui { set_brightness(box.brightness()); set_video_setting(BoxEmu::get().video_setting()); set_resume_last_save(AppSettings::get().get_settings().resume_last_save); + set_genesis_cpu_percent(AppSettings::get().get_settings().genesis_cpu_percent); } VideoSetting get_video_setting(); @@ -266,6 +270,8 @@ class Gui { lv_obj_t *resume_button_{nullptr}; lv_obj_t *resume_label_{nullptr}; + lv_obj_t *genesis_cpu_button_{nullptr}; + lv_obj_t *genesis_cpu_label_{nullptr}; // style for buttons lv_style_t button_style_; diff --git a/components/gui/src/gui.cpp b/components/gui/src/gui.cpp index 64653a86..e00b7378 100644 --- a/components/gui/src/gui.cpp +++ b/components/gui/src/gui.cpp @@ -36,6 +36,13 @@ void Gui::set_video_setting(VideoSetting setting) { AppSettings::get().modify([setting](Settings &s) { s.video_setting = static_cast(setting); }); } +void Gui::set_genesis_cpu_percent(int percent) { + AppSettings::get().modify([percent](Settings &s) { s.genesis_cpu_percent = static_cast(percent); }); + if (genesis_cpu_label_) { + lv_label_set_text(genesis_cpu_label_, percent > 100 ? "125%" : "Normal"); + } +} + void Gui::set_resume_last_save(bool on) { AppSettings::get().modify([on](Settings &s) { s.resume_last_save = on; }); if (resume_label_) { @@ -323,6 +330,32 @@ void Gui::init_ui() { lv_obj_add_event_cb(resume_button_, &Gui::event_callback, LV_EVENT_KEY, static_cast(this)); } + // settings: the Genesis 68000 clock (125% removes most in-game slowdown, + // e.g. Sonic losing rings, at the cost of fewer frames drawn) + { + auto *panel = lv_obj_create(ui_settingspanel); + lv_obj_set_height(panel, 50); + lv_obj_set_width(panel, lv_pct(100)); + lv_obj_set_y(panel, 350); + lv_obj_set_align(panel, LV_ALIGN_TOP_MID); + lv_obj_remove_flag(panel, LV_OBJ_FLAG_SCROLLABLE); + auto *label = lv_label_create(panel); + lv_obj_set_align(label, LV_ALIGN_LEFT_MID); + lv_label_set_text(label, "Genesis CPU"); + genesis_cpu_button_ = lv_button_create(panel); + lv_obj_set_height(genesis_cpu_button_, 32); + lv_obj_set_width(genesis_cpu_button_, LV_SIZE_CONTENT); + lv_obj_set_align(genesis_cpu_button_, LV_ALIGN_RIGHT_MID); + lv_obj_add_flag(genesis_cpu_button_, LV_OBJ_FLAG_SCROLL_ON_FOCUS); + lv_obj_remove_flag(genesis_cpu_button_, LV_OBJ_FLAG_SCROLLABLE); + genesis_cpu_label_ = lv_label_create(genesis_cpu_button_); + lv_obj_set_align(genesis_cpu_label_, LV_ALIGN_CENTER); + lv_label_set_text(genesis_cpu_label_, + AppSettings::get().get_settings().genesis_cpu_percent > 100 ? "125%" : "Normal"); + lv_obj_add_event_cb(genesis_cpu_button_, &Gui::event_callback, LV_EVENT_PRESSED, static_cast(this)); + lv_obj_add_event_cb(genesis_cpu_button_, &Gui::event_callback, LV_EVENT_KEY, static_cast(this)); + } + // now do the same events for all the same buttons but for the LV_EVENT_KEY lv_obj_add_event_cb(ui_settingsbutton, &Gui::event_callback, LV_EVENT_KEY, static_cast(this)); lv_obj_add_event_cb(ui_playbutton, &Gui::event_callback, LV_EVENT_KEY, static_cast(this)); @@ -354,6 +387,7 @@ void Gui::init_ui() { lv_group_add_obj(settings_screen_group_, ui_hapticplaybutton); lv_group_add_obj(settings_screen_group_, ui_usb_button); lv_group_add_obj(settings_screen_group_, resume_button_); + lv_group_add_obj(settings_screen_group_, genesis_cpu_button_); // set the focused style for all the buttons to have a red border lv_style_init(&button_style_); @@ -374,6 +408,7 @@ void Gui::init_ui() { lv_obj_add_style(ui_videosettingdropdown, &button_style_, LV_STATE_FOCUSED); lv_obj_add_style(ui_usb_button, &button_style_, LV_STATE_FOCUSED); lv_obj_add_style(resume_button_, &button_style_, LV_STATE_FOCUSED); + lv_obj_add_style(genesis_cpu_button_, &button_style_, LV_STATE_FOCUSED); update_rom_list(); @@ -492,6 +527,10 @@ void Gui::on_pressed(lv_event_t *e) { set_resume_last_save(!AppSettings::get().get_settings().resume_last_save); return; } + if (genesis_cpu_button_ && target == genesis_cpu_button_) { + set_genesis_cpu_percent(AppSettings::get().get_settings().genesis_cpu_percent > 100 ? 100 : 125); + return; + } } void Gui::on_volume(const std::vector& data) { diff --git a/components/settings/include/settings.hpp b/components/settings/include/settings.hpp index 8f4bad37..f765513a 100644 --- a/components/settings/include/settings.hpp +++ b/components/settings/include/settings.hpp @@ -26,8 +26,33 @@ struct SettingsV1 { uint8_t haptic_waveform{12}; ///< DRV2605 effect played when a game starts }; +/// Version 2: adds the Genesis 68000 clock. +struct SettingsV2 { + static constexpr uint32_t VERSION = 2; + uint8_t volume{60}; ///< 0..100 + bool muted{false}; + uint8_t brightness{80}; ///< 0..100 + uint8_t video_setting{1}; ///< VideoSetting (0 original, 1 fit, 2 fill) + bool resume_last_save{true}; ///< starting a game loads the slot it was last saved to / loaded from + std::string last_rom; ///< rom_path of the last game started (selected again at boot) + std::map last_slot; ///< rom_path -> the save slot last saved to / loaded from + uint8_t haptic_waveform{12}; ///< DRV2605 effect played when a game starts + uint8_t genesis_cpu_percent{100}; ///< Genesis 68000 clock, % of stock (125 removes most in-game lag) + + static SettingsV2 migrate(const SettingsV1 &v1) { + return {.volume = v1.volume, + .muted = v1.muted, + .brightness = v1.brightness, + .video_setting = v1.video_setting, + .resume_last_save = v1.resume_last_save, + .last_rom = v1.last_rom, + .last_slot = v1.last_slot, + .haptic_waveform = v1.haptic_waveform}; + } +}; + /// The current settings version. -using Settings = SettingsV1; +using Settings = SettingsV2; /// The settings file and an in-RAM copy of it. Changes are made in RAM /// (modify()) and written when save() is called; the launcher calls save() @@ -81,7 +106,7 @@ class AppSettings { AppSettings() = default; static bool same(const Settings &a, const Settings &b); - using Store = espp::PersistentData; + using Store = espp::PersistentData; mutable std::mutex mutex_; std::unique_ptr data_; Settings fallback_{}; ///< used when init() was not called (no SD card) diff --git a/components/settings/src/settings.cpp b/components/settings/src/settings.cpp index 5f28274c..80737f1f 100644 --- a/components/settings/src/settings.cpp +++ b/components/settings/src/settings.cpp @@ -6,9 +6,9 @@ bool AppSettings::same(const Settings &a, const Settings &b) { return std::tie(a.volume, a.muted, a.brightness, a.video_setting, a.resume_last_save, a.last_rom, a.last_slot, - a.haptic_waveform) == + a.haptic_waveform, a.genesis_cpu_percent) == std::tie(b.volume, b.muted, b.brightness, b.video_setting, b.resume_last_save, b.last_rom, b.last_slot, - b.haptic_waveform); + b.haptic_waveform, b.genesis_cpu_percent); } bool AppSettings::init(const std::string &file_path, const Settings &defaults) { diff --git a/main/genesis_cart.hpp b/main/genesis_cart.hpp index 6074ea67..f5f98c2e 100644 --- a/main/genesis_cart.hpp +++ b/main/genesis_cart.hpp @@ -51,6 +51,7 @@ class GenesisCart : public Cart { case Emulator::SEGA_GENESIS: case Emulator::SEGA_MEGA_DRIVE: logger_.info("genesis::init()"); + genesis_set_cpu_percent(AppSettings::get().get_settings().genesis_cpu_percent); init_genesis(romdata_, rom_size_bytes_); break; default: diff --git a/main/main.cpp b/main/main.cpp index 766cc1b1..82145a1b 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -112,9 +112,9 @@ extern "C" void app_main(void) { emu.mute(s.muted); emu.brightness(std::max(10, s.brightness)); emu.video_setting(static_cast(std::min(s.video_setting, 2))); - logger.info("settings: volume {}{}, brightness {}, video {}, resume {}, haptic {}, last game '{}'", s.volume, - s.muted ? " (muted)" : "", s.brightness, s.video_setting, s.resume_last_save, s.haptic_waveform, - s.last_rom); + logger.info("settings: volume {}{}, brightness {}, video {}, resume {}, haptic {}, genesis cpu {}%, last game '{}'", + s.volume, s.muted ? " (muted)" : "", s.brightness, s.video_setting, s.resume_last_save, + s.haptic_waveform, s.genesis_cpu_percent, s.last_rom); } logger.info("initializing gui..."); From f405a6e65d1a396367e6f5d46f38b84e5761ead9 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 13 Nov 2026 15:37:25 -0600 Subject: [PATCH 2/4] genesis: bump gwenesis (Z80 reset fix for Maximum Carnage, sound runs muted) Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/genesis/gwenesis | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/genesis/gwenesis b/components/genesis/gwenesis index 04e54777..d2dc597c 160000 --- a/components/genesis/gwenesis +++ b/components/genesis/gwenesis @@ -1 +1 @@ -Subproject commit 04e547771d57747907cca463df50aa1e454bd9e2 +Subproject commit d2dc597ca7e3e3df029059ec55ff8bccfa12cd28 From ebfcad1e90bc8ce75c52a95cb1e74e127374935c Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 13 Nov 2026 16:39:17 -0600 Subject: [PATCH 3/4] Pin gwenesis to esp32 (#1 and #2 merged) Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/genesis/gwenesis | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/genesis/gwenesis b/components/genesis/gwenesis index 51307375..2af750ea 160000 --- a/components/genesis/gwenesis +++ b/components/genesis/gwenesis @@ -1 +1 @@ -Subproject commit 513073751c1b4bc8f403b811ce8a4609396f1b44 +Subproject commit 2af750ea828e01dde8225d43daad12f5134acb81 From 32ee4e8dca0cc97b33c904ade14e1438d5bf29a9 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Fri, 13 Nov 2026 17:48:41 -0600 Subject: [PATCH 4/4] Genesis CPU: store and apply only the supported clocks (Normal / 125%) Settings::genesis_cpu_choice() maps any stored value to 100 or 125; the GUI stores, shows and toggles through it and the Genesis cart applies it. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY --- components/gui/src/gui.cpp | 11 +++++++---- components/settings/include/settings.hpp | 4 ++++ main/genesis_cart.hpp | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/components/gui/src/gui.cpp b/components/gui/src/gui.cpp index e00b7378..969b5091 100644 --- a/components/gui/src/gui.cpp +++ b/components/gui/src/gui.cpp @@ -37,9 +37,10 @@ void Gui::set_video_setting(VideoSetting setting) { } void Gui::set_genesis_cpu_percent(int percent) { - AppSettings::get().modify([percent](Settings &s) { s.genesis_cpu_percent = static_cast(percent); }); + const uint8_t choice = Settings::genesis_cpu_choice(percent); + AppSettings::get().modify([choice](Settings &s) { s.genesis_cpu_percent = choice; }); if (genesis_cpu_label_) { - lv_label_set_text(genesis_cpu_label_, percent > 100 ? "125%" : "Normal"); + lv_label_set_text(genesis_cpu_label_, choice > 100 ? "125%" : "Normal"); } } @@ -351,7 +352,8 @@ void Gui::init_ui() { genesis_cpu_label_ = lv_label_create(genesis_cpu_button_); lv_obj_set_align(genesis_cpu_label_, LV_ALIGN_CENTER); lv_label_set_text(genesis_cpu_label_, - AppSettings::get().get_settings().genesis_cpu_percent > 100 ? "125%" : "Normal"); + Settings::genesis_cpu_choice(AppSettings::get().get_settings().genesis_cpu_percent) > 100 + ? "125%" : "Normal"); lv_obj_add_event_cb(genesis_cpu_button_, &Gui::event_callback, LV_EVENT_PRESSED, static_cast(this)); lv_obj_add_event_cb(genesis_cpu_button_, &Gui::event_callback, LV_EVENT_KEY, static_cast(this)); } @@ -528,7 +530,8 @@ void Gui::on_pressed(lv_event_t *e) { return; } if (genesis_cpu_button_ && target == genesis_cpu_button_) { - set_genesis_cpu_percent(AppSettings::get().get_settings().genesis_cpu_percent > 100 ? 100 : 125); + set_genesis_cpu_percent(Settings::genesis_cpu_choice(AppSettings::get().get_settings().genesis_cpu_percent) > 100 + ? 100 : 125); return; } } diff --git a/components/settings/include/settings.hpp b/components/settings/include/settings.hpp index f765513a..3d810a91 100644 --- a/components/settings/include/settings.hpp +++ b/components/settings/include/settings.hpp @@ -39,6 +39,10 @@ struct SettingsV2 { uint8_t haptic_waveform{12}; ///< DRV2605 effect played when a game starts uint8_t genesis_cpu_percent{100}; ///< Genesis 68000 clock, % of stock (125 removes most in-game lag) + /// The supported Genesis clocks are Normal (100) and 125%; a stored value is + /// read through this so anything else (an older or hand-edited file) maps to one + static constexpr uint8_t genesis_cpu_choice(int percent) { return percent > 100 ? 125 : 100; } + static SettingsV2 migrate(const SettingsV1 &v1) { return {.volume = v1.volume, .muted = v1.muted, diff --git a/main/genesis_cart.hpp b/main/genesis_cart.hpp index f5f98c2e..ce84d40d 100644 --- a/main/genesis_cart.hpp +++ b/main/genesis_cart.hpp @@ -51,7 +51,7 @@ class GenesisCart : public Cart { case Emulator::SEGA_GENESIS: case Emulator::SEGA_MEGA_DRIVE: logger_.info("genesis::init()"); - genesis_set_cpu_percent(AppSettings::get().get_settings().genesis_cpu_percent); + genesis_set_cpu_percent(Settings::genesis_cpu_choice(AppSettings::get().get_settings().genesis_cpu_percent)); init_genesis(romdata_, rom_size_bytes_); break; default: