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 09da22f2..2af750ea 160000 --- a/components/genesis/gwenesis +++ b/components/genesis/gwenesis @@ -1 +1 @@ -Subproject commit 09da22f269debcace9e546d7e6d813bfd94bdcba +Subproject commit 2af750ea828e01dde8225d43daad12f5134acb81 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..969b5091 100644 --- a/components/gui/src/gui.cpp +++ b/components/gui/src/gui.cpp @@ -36,6 +36,14 @@ 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) { + 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_, choice > 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 +331,33 @@ 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_, + 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)); + } + // 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 +389,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 +410,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 +529,11 @@ 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(Settings::genesis_cpu_choice(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..3d810a91 100644 --- a/components/settings/include/settings.hpp +++ b/components/settings/include/settings.hpp @@ -26,8 +26,37 @@ 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) + + /// 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, + .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 +110,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..ce84d40d 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(Settings::genesis_cpu_choice(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...");