// Hardware-tweak configuration window. // See config_window.h for the public contract. #include "config_window.h" #include "emu_state.h" #include "imgui.h" #include "backends/imgui_impl_sdl2.h" #include "backends/imgui_impl_sdlrenderer2.h" #include #include #include namespace { SDL_Window *g_window = nullptr; SDL_Renderer *g_renderer = nullptr; ImGuiContext *g_imgui = nullptr; bool g_visible = false; Uint32 g_window_id = 0; // Reset every tweakable to its EmuState constructor default. // Kept as one function so the body and emu_state.h stay obviously in sync. void reset_to_defaults(EmuState *s) { s->bat_soc_pct = 80; s->bat_vcell_mv = 3950; s->bat_temp_c10 = 250; s->bat_current_ma = 0; s->bat_capacity_mah = 3500; s->bat_full_cap_mah = 4000; s->bat_design_cap_mah = 4310; s->bat_ocv_mv = 3960; s->bat_v_empty_mv = 3300; s->bat_min_volt_mv = 3500; s->bat_max_volt_mv = 4200; s->bat_age_pct = 100; s->bat_cycles = 0; s->soc_temp_c10 = 420; s->pcb_temp_c10 = 350; s->chg_vbus_stat = 0; s->chg_chrg_stat = 0; s->chg_power_good = false; s->chg_input_current_ma = 2000; s->chg_input_voltage_mv = 4280; s->chg_system_min_mv = 3500; s->chg_fast_current_ma = 1856; s->chg_charge_voltage_mv = 4208; s->chg_thermal_c = 60; s->usb_pd_inserted = false; s->usb_pd_voltage_mv = 5000; s->usb_pd_amperage_ma = 1500; s->pmic_otp = 0x35; s->is_mariko = false; s->pmic_silicon_rev = 0x5B; // CID3, measured on a real console s->pmic_es_rev = 0x81; // CID5 s->cpu_pmic_version = 0x1C; s->pmic_cnfg1_32k = 0xFC; // 32K_OK | 32K_OUT0_EN s->bt_radio = BT_RADIO_HEALTHY; s->wifi_radio = WIFI_RADIO_HEALTHY; s->sd_inserted = true; s->sd_cid_manfid = 0x03; s->sd_cid_oemid = 0x5344; // "SD" s->sd_cid_prod_name = 0x3155445355ULL; // "USDU1" s->sd_cid_hwrev = 1; s->sd_cid_fwrev = 0; s->sd_cid_serial = 0xC0FFEE42; s->sd_cid_month = 10; s->sd_cid_year = 2024; s->emmc_cid_manfid = 0x90; s->emmc_cid_oemid = 0x01; s->emmc_cid_prod_name = 0x326134474248ULL; // "HBG4a2" s->emmc_cid_prv = 0x07; s->emmc_cid_serial = 0x12345678; s->emmc_cid_month = 6; s->emmc_cid_year = 2017; s->init_fuse_defaults(); // resets all 256 fuse words s->dram_vendor = 1; // Samsung s->dram_rev_id1 = 2; s->dram_rev_id2 = 0; s->dram_density = 0x18; // 4 GB die s->panel_id_raw = 0x099310; // JDI LAM062M109A rev 0x93 s->touch_panel_idx = 0; // NISSHA NFT-K12D s->touch_fw_id = 0x32000001; // 4CD60D/1 s->touch_ftb_ver = 0x0123; s->touch_fw_rev = 0x4567; s->backlight = 100; s->rotation_override = -1; } // ============================================================================ // INI persistence // // On startup main.cpp tries to load rcm_emu.ini from cwd; the load is // additive, so any field absent from the file keeps whatever EmuState held // (typically the constructor default or init_fuse_defaults() value). The // "Save configuration" button writes the current values back to the same // file. Fuses are written as `0xNNN=HHHHHHHH` rows, all 256 words; the rest // of the state is grouped into [section] blocks matching the M-window // layout. // ============================================================================ // Schema. Every saveable field appears here exactly once. The macro is // expanded by save and load into matching write / parse code, so adding a // new tweakable means appending one line and nothing else. // // Args: (atomic_field, section, key, signed) — `signed` picks decimal vs // unsigned-decimal formatting on save and signed-vs-unsigned strtoll on load. #define EMU_STATE_INI_FIELDS(X) \ /* Battery (MAX17050) */ \ X(bat_soc_pct, "battery", "soc_pct", 0) \ X(bat_vcell_mv, "battery", "vcell_mv", 0) \ X(bat_temp_c10, "battery", "temp_c10", 1) \ X(bat_current_ma, "battery", "current_ma", 1) \ X(bat_capacity_mah, "battery", "capacity_mah", 0) \ X(bat_full_cap_mah, "battery", "full_cap_mah", 0) \ X(bat_design_cap_mah, "battery", "design_cap_mah", 0) \ X(bat_ocv_mv, "battery", "ocv_mv", 0) \ X(bat_v_empty_mv, "battery", "v_empty_mv", 0) \ X(bat_min_volt_mv, "battery", "min_volt_mv", 0) \ X(bat_max_volt_mv, "battery", "max_volt_mv", 0) \ X(bat_age_pct, "battery", "age_pct", 0) \ X(bat_cycles, "battery", "cycles", 0) \ /* Thermal (TMP451) */ \ X(soc_temp_c10, "thermal", "soc_temp_c10", 1) \ X(pcb_temp_c10, "thermal", "pcb_temp_c10", 1) \ /* Charger (BQ24193) */ \ X(chg_vbus_stat, "charger", "vbus_stat", 0) \ X(chg_chrg_stat, "charger", "chrg_stat", 0) \ X(chg_power_good, "charger", "power_good", 0) \ X(chg_input_current_ma,"charger", "input_current_ma",0) \ X(chg_input_voltage_mv,"charger", "input_voltage_mv",0) \ X(chg_system_min_mv, "charger", "system_min_mv", 0) \ X(chg_fast_current_ma, "charger", "fast_current_ma", 0) \ X(chg_charge_voltage_mv,"charger","charge_voltage_mv",0) \ X(chg_thermal_c, "charger", "thermal_c", 0) \ /* USB-PD (BM92T36) */ \ X(usb_pd_inserted, "usb_pd", "inserted", 0) \ X(usb_pd_voltage_mv, "usb_pd", "voltage_mv", 0) \ X(usb_pd_amperage_ma, "usb_pd", "amperage_ma", 0) \ X(usb_host, "usb", "host", 0) \ X(usb_nbd_port, "usb", "nbd_port", 0) \ /* SoC / PMIC */ \ X(pmic_otp, "soc", "pmic_otp", 0) \ X(is_mariko, "soc", "is_mariko", 0) \ X(pmic_silicon_rev, "soc", "pmic_silicon_rev",0) \ X(pmic_es_rev, "soc", "pmic_es_rev", 0) \ X(cpu_pmic_version, "soc", "cpu_pmic_version",0) \ X(pmic_cnfg1_32k, "soc", "pmic_cnfg1_32k", 0) \ /* Storage — SD (SDMMC1 CMD2) */ \ X(sd_inserted, "sd", "inserted", 0) \ X(sd_cid_manfid, "sd", "cid_manfid", 0) \ X(sd_cid_oemid, "sd", "cid_oemid", 0) \ X(sd_cid_prod_name, "sd", "cid_prod_name", 0) \ X(sd_cid_hwrev, "sd", "cid_hwrev", 0) \ X(sd_cid_fwrev, "sd", "cid_fwrev", 0) \ X(sd_cid_serial, "sd", "cid_serial", 0) \ X(sd_cid_month, "sd", "cid_month", 0) \ X(sd_cid_year, "sd", "cid_year", 0) \ /* Storage — eMMC (SDMMC4 CMD2) */ \ X(emmc_cid_manfid, "emmc", "cid_manfid", 0) \ X(emmc_cid_oemid, "emmc", "cid_oemid", 0) \ X(emmc_cid_prod_name, "emmc", "cid_prod_name", 0) \ X(emmc_cid_prv, "emmc", "cid_prv", 0) \ X(emmc_cid_serial, "emmc", "cid_serial", 0) \ X(emmc_cid_month, "emmc", "cid_month", 0) \ X(emmc_cid_year, "emmc", "cid_year", 0) \ /* Display panel (DSI MIPI_DCS_GET_DISPLAY_ID) */ \ X(panel_id_raw, "display", "panel_id_raw", 0) \ /* Touch panel (FTS4) */ \ X(touch_panel_idx, "touch", "panel_idx", 0) \ X(touch_fw_id, "touch", "fw_id", 0) \ X(touch_ftb_ver, "touch", "ftb_ver", 0) \ X(touch_fw_rev, "touch", "fw_rev", 0) \ /* DRAM modules (EMC mode-register stubs) */ \ X(dram_vendor, "dram", "vendor", 0) \ X(dram_rev_id1, "dram", "rev_id1", 0) \ X(dram_rev_id2, "dram", "rev_id2", 0) \ X(dram_density, "dram", "density", 0) bool config_window_save_ini_impl(const EmuState *s, const char *path) { FILE *f = fopen(path, "w"); if (!f) return false; fprintf(f, "# rcm_emu hardware config\n" "# Auto-loaded on startup. Edit by hand or via the M-window\n" "# 'Save configuration' button.\n"); const char *cur_section = ""; auto write_header = [&](const char *section) { if (strcmp(section, cur_section) != 0) { fprintf(f, "\n[%s]\n", section); cur_section = section; } }; #define X(field, section, key, is_signed) \ write_header(section); \ if (is_signed) fprintf(f, "%s=%lld\n", key, (long long)s->field.load()); \ else fprintf(f, "%s=%llu\n", key, (unsigned long long)s->field.load()); EMU_STATE_INI_FIELDS(X) #undef X // Plain (non-atomic) fields. write_header("display"); fprintf(f, "backlight=%u\n", (unsigned)s->backlight); fprintf(f, "rotation_override=%d\n", (int)s->rotation_override); // Bluetooth radio mode. Deliberately NOT a schema row: the schema parses // with strtoull, so "faulty" would silently become 0 = healthy and the // faulty path would be unreachable from the ini with nothing to warn you. // Must stay above the [fuses] header below, or the next load would find // it inside the fuse dump. write_header("bluetooth"); fprintf(f, "radio=%s\n", bt_radio_name(s->bt_radio.load())); // Same reasoning for the WLAN half: a separate device on PCIe with its // own failure modes, and the same string-vs-schema trap. write_header("wifi"); fprintf(f, "radio=%s\n", wifi_radio_name(s->wifi_radio.load())); // Fuses are dumped wholesale — small enough (256 × 4 bytes) and keeps the // load path branch-free. Zero words too: loading layers the file over // init_fuse_defaults(), so a fuse cleared in the UI and left out here // came back at its default on the next start. fprintf(f, "\n[fuses]\n"); for (size_t i = 0; i < EmuState::FUSE_WORDS; i++) { uint32_t v = s->fuse_word[i].load(); fprintf(f, "0x%03X=0x%08X\n", (unsigned)(i * 4), v); } fclose(f); return true; } // In-place trim. Returns the new start (skips leading whitespace) and writes // a NUL where trailing whitespace begins. char *trim(char *s) { while (*s == ' ' || *s == '\t') s++; size_t n = strlen(s); while (n > 0 && (s[n-1] == ' ' || s[n-1] == '\t' || s[n-1] == '\n' || s[n-1] == '\r')) { s[--n] = 0; } return s; } bool config_window_load_ini_impl(EmuState *s, const char *path) { FILE *f = fopen(path, "r"); if (!f) return false; char line[256]; char section[64] = ""; while (fgets(line, sizeof(line), f)) { char *p = trim(line); if (*p == 0 || *p == '#' || *p == ';') continue; if (*p == '[') { char *end = strchr(p + 1, ']'); if (!end) continue; size_t n = (size_t)(end - p - 1); if (n >= sizeof(section)) n = sizeof(section) - 1; memcpy(section, p + 1, n); section[n] = 0; continue; } char *eq = strchr(p, '='); if (!eq) continue; *eq = 0; char *key = trim(p); char *val = trim(eq + 1); // Schema-driven dispatch. #define X(field, sec, k, is_signed) \ if (strcmp(section, sec) == 0 && strcmp(key, k) == 0) { \ if (is_signed) s->field.store((decltype(s->field.load()))strtoll(val, nullptr, 0)); \ else s->field.store((decltype(s->field.load()))strtoull(val, nullptr, 0)); \ continue; \ } EMU_STATE_INI_FIELDS(X) #undef X // Plain fields and the fuse range. if (strcmp(section, "display") == 0) { if (strcmp(key, "backlight") == 0) { s->backlight = (uint32_t)strtoul(val, nullptr, 0); continue; } if (strcmp(key, "rotation_override") == 0) { s->rotation_override = (int32_t)strtol(val, nullptr, 0); continue; } } if (strcmp(section, "bluetooth") == 0 && strcmp(key, "radio") == 0) { uint8_t mode = BT_RADIO_HEALTHY; if (bt_radio_parse(val, &mode)) { s->bt_radio.store(mode); } else { fprintf(stderr, "[config] Unknown [bluetooth] radio value '%s';" " expected healthy, faulty or absent\n", val); } continue; } if (strcmp(section, "wifi") == 0 && strcmp(key, "radio") == 0) { uint8_t mode = WIFI_RADIO_HEALTHY; if (wifi_radio_parse(val, &mode)) { s->wifi_radio.store(mode); } else { fprintf(stderr, "[config] Unknown [wifi] radio value '%s';" " expected healthy, faulty or absent\n", val); } continue; } if (strcmp(section, "fuses") == 0) { uint32_t off = (uint32_t)strtoul(key, nullptr, 0); if (off < EmuState::FUSE_WORDS * 4 && (off & 3) == 0) { s->fuse_at(off).store((uint32_t)strtoul(val, nullptr, 0)); } continue; } } fclose(f); return true; } // Helper: int slider that reads/writes an atomic. template bool atomic_slider_int(const char *label, std::atomic &a, int lo, int hi, const char *fmt = "%d") { int v = (int)a.load(); if (ImGui::SliderInt(label, &v, lo, hi, fmt)) { a.store((T)v); return true; } return false; } // Helper: float slider for a temperature stored as °C × 10. The atomic // holds tenths so MAX17050/TMP451 register encodings stay simple, but the // user sees the actual decimal Celsius value. template bool atomic_slider_temp(const char *label, std::atomic &a, float lo_c, float hi_c) { float v = (float)a.load() / 10.0f; if (ImGui::SliderFloat(label, &v, lo_c, hi_c, "%.1f \xC2\xB0""C")) { a.store((T)(v * 10.0f)); return true; } return false; } // Vendor name + hex tuple. Hex is what gets written; the name is just a UI // label for known IDs. List mirrors Hekate's gui_info.c switch statements // for SD (sd_storage.cid.manfid) and eMMC (emmc_storage.cid.manfid). struct VendorEntry { uint8_t id; const char *name; }; static const VendorEntry kSdVendors[] = { {0x00,"Fake"}, {0x01,"Panasonic"}, {0x02,"Toshiba"}, {0x03,"SanDisk"}, {0x06,"Ritek"}, {0x09,"ATP"}, {0x13,"Kingmax"}, {0x19,"Dynacard"}, {0x1A,"Power Quotient"}, {0x1B,"Samsung"}, {0x1D,"AData"}, {0x27,"Phison"}, {0x28,"Barun Electronics"}, {0x31,"Silicon Power"}, {0x41,"Kingston"}, {0x51,"STEC"}, {0x5D,"SwissBit"}, {0x61,"Netlist"}, {0x63,"Cactus"}, {0x73,"Bongiovi"}, {0x74,"Jiaelec"}, {0x76,"Patriot"}, {0x82,"Jiang Tay"}, {0x83,"Netcom"}, {0x84,"Strontium"}, {0x9C,"Barun / Sony"}, {0x9F,"Taishin"}, {0xAD,"Longsys"}, }; static const VendorEntry kEmmcVendors[] = { {0x11,"Toshiba"}, {0x15,"Samsung"}, {0x45,"SanDisk"}, {0x89,"Silicon Motion"}, {0x90,"SK Hynix"}, }; // Combo populated with `entries`; selecting one writes its hex to `target`. // If the current value doesn't match any entry, the combo's preview shows // "Custom (0xNN)". The hex is editable via the matching InputScalar above // or below this widget. static void vendor_preset_combo(const char *label, std::atomic &target, const VendorEntry *entries, size_t n) { uint8_t cur = target.load(); char preview[40]; const char *match = nullptr; for (size_t i = 0; i < n; i++) if (entries[i].id == cur) { match = entries[i].name; break; } if (match) snprintf(preview, sizeof(preview), "%s (0x%02X)", match, cur); else snprintf(preview, sizeof(preview), "Custom (0x%02X)", cur); if (ImGui::BeginCombo(label, preview)) { for (size_t i = 0; i < n; i++) { char item[40]; snprintf(item, sizeof(item), "%s (0x%02X)", entries[i].name, entries[i].id); bool sel = (entries[i].id == cur); if (ImGui::Selectable(item, sel)) target.store(entries[i].id); if (sel) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } } // Helper: hex input that reads/writes a uint32 atomic. bool atomic_hex_input(const char *label, std::atomic &a) { uint32_t v = a.load(); if (ImGui::InputScalar(label, ImGuiDataType_U32, &v, nullptr, nullptr, "%08X", ImGuiInputTextFlags_CharsHexadecimal)) { a.store(v); return true; } return false; } void build_ui(EmuState *state) { // Fill the OS window with a single ImGui window (no decorations). int win_w = 0, win_h = 0; SDL_GetWindowSize(g_window, &win_w, &win_h); ImGui::SetNextWindowPos(ImVec2(0, 0)); ImGui::SetNextWindowSize(ImVec2((float)win_w, (float)win_h)); ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize | ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoBringToFrontOnFocus; ImGui::Begin("##cfg", nullptr, flags); if (ImGui::CollapsingHeader("Battery (MAX17050)", ImGuiTreeNodeFlags_DefaultOpen)) { atomic_slider_int("SOC (%)", state->bat_soc_pct, 0, 100, "%d %%"); atomic_slider_int("Voltage (mV)", state->bat_vcell_mv, 3000, 4400, "%d mV"); atomic_slider_int("OCV (mV)", state->bat_ocv_mv, 3000, 4400, "%d mV"); atomic_slider_temp ("Temp", state->bat_temp_c10, -20.0f, 80.0f); int cur = (int)state->bat_current_ma.load(); if (ImGui::SliderInt("Current (mA)", &cur, -3000, 3000, "%d mA")) { state->bat_current_ma.store((int16_t)cur); } atomic_slider_int("RepCap (mAh)", state->bat_capacity_mah, 0, 6000, "%d mAh"); atomic_slider_int("FullCAP (mAh)", state->bat_full_cap_mah, 0, 6000, "%d mAh"); atomic_slider_int("DesignCap (mAh)", state->bat_design_cap_mah, 0, 6000, "%d mAh"); atomic_slider_int("V_empty (mV)", state->bat_v_empty_mv, 2500, 3800, "%d mV"); atomic_slider_int("Min volt (mV)", state->bat_min_volt_mv, 2500, 4400, "%d mV"); atomic_slider_int("Max volt (mV)", state->bat_max_volt_mv, 2500, 4400, "%d mV"); atomic_slider_int ("Age (%)", state->bat_age_pct, 0, 100, "%d %%"); atomic_slider_int("Cycles", state->bat_cycles, 0, 2000); } if (ImGui::CollapsingHeader("Thermal (TMP451)", ImGuiTreeNodeFlags_DefaultOpen)) { atomic_slider_temp("SoC temp", state->soc_temp_c10, 0.0f, 110.0f); atomic_slider_temp("PCB temp", state->pcb_temp_c10, 0.0f, 80.0f); } if (ImGui::CollapsingHeader("SoC / PMIC", ImGuiTreeNodeFlags_DefaultOpen)) { // SoC generation drives APB_MISC_GP_HIDREV (1=Erista, 2=Mariko). // Selecting it also flips the PMIC OTP byte to match — they go // together on real hardware. const char *gen_items[] = {"Erista (T210)", "Mariko (T210B01)"}; int gidx = state->is_mariko.load() ? 1 : 0; if (ImGui::Combo("SoC", &gidx, gen_items, IM_ARRAYSIZE(gen_items))) { state->is_mariko.store(gidx == 1); state->pmic_otp.store(gidx == 1 ? 0x53 : 0x35); } const char *otp_items[] = {"Erista (0x35)", "Mariko (0x53)"}; int idx = (state->pmic_otp.load() == 0x53) ? 1 : 0; if (ImGui::Combo("PMIC OTP", &idx, otp_items, IM_ARRAYSIZE(otp_items))) { state->pmic_otp.store(idx == 1 ? 0x53 : 0x35); } // Whole bytes (CID3 reads 0x5B on real silicon), so the range has to // span 0..255 - a 0..15 slider silently clamped these to a nibble. atomic_slider_int("MAX77620 CID3 (Si rev)", state->pmic_silicon_rev, 0, 255); atomic_slider_int("MAX77620 CID5 (ES rev)", state->pmic_es_rev, 0, 255); atomic_slider_int("MAX77621 chipid", state->cpu_pmic_version, 0, 255); // CNFG1_32K is the radio's low-power oscillator config. 0xFC and 0xDC // are both measured on healthy consoles of both generations; the // difference is per unit, so it is a value, not a generation switch. atomic_slider_int("MAX77620 CNFG1_32K", state->pmic_cnfg1_32k, 0, 255, "0x%02X"); } if (ImGui::CollapsingHeader("Bluetooth (CYW4356)")) { // Selected before the probe runs; press 'a' on the payload's pager to // re-run the sweep after changing it, or Reboot for a clean POR. const char *bt_items[] = {"Healthy (answers HCI)", "Faulty (never comes out of POR)", "Absent (no module fitted)"}; int bt = (int)state->bt_radio.load(); if (ImGui::Combo("Radio", &bt, bt_items, IM_ARRAYSIZE(bt_items))) { state->bt_radio.store((uint8_t)bt); } ImGui::TextDisabled("Faulty reproduces the 2110-1118 console: BT_HOST_WAKE\n" "low, chip RTS_N never asserts, no HCI on any arm."); } if (ImGui::CollapsingHeader("Wi-Fi (CYW4356 WLAN, PCIe)")) { // The other half of the same package. Independent of the Bluetooth // setting on purpose: a console can answer HCI perfectly and still // have a dead WLAN core, which is precisely the case the PCIe probe // exists to separate. const char *wifi_items[] = {"Healthy (enumerates, ChipID 4356)", "Faulty (enumerates, backplane dead)", "Absent (link never trains)"}; int wifi = (int)state->wifi_radio.load(); if (ImGui::Combo("WLAN core", &wifi, wifi_items, IM_ARRAYSIZE(wifi_items))) { state->wifi_radio.store((uint8_t)wifi); } ImGui::TextDisabled("Faulty is the console that shows 14E4:43EC on the bus\n" "and still has no Wi-Fi: front-end alive, die is not."); } if (ImGui::CollapsingHeader("Charger (BQ24193)", ImGuiTreeNodeFlags_DefaultOpen)) { const char *vbus_items[] = {"None", "USB SDP", "Adapter", "OTG"}; int vbus = (int)state->chg_vbus_stat.load(); if (ImGui::Combo("VBUS", &vbus, vbus_items, IM_ARRAYSIZE(vbus_items))) { state->chg_vbus_stat.store((uint8_t)vbus); } const char *chrg_items[] = {"Not charging", "Pre-charge", "Fast charge", "Done"}; int chrg = (int)state->chg_chrg_stat.load(); if (ImGui::Combo("Charge state", &chrg, chrg_items, IM_ARRAYSIZE(chrg_items))) { state->chg_chrg_stat.store((uint8_t)chrg); } bool pg = state->chg_power_good.load(); if (ImGui::Checkbox("Power good", &pg)) { state->chg_power_good.store(pg); } // ---- Charger limits (encoded into BQ24193 regs 0x00/0x01/0x02/0x04/0x06) ---- // InputCurrentLimit is a quantized 8-value table in the chip; expose it // as a Combo so the displayed value always matches what Hekate reads back. const char *iinlim_items[] = {"100 mA","150 mA","500 mA","900 mA","1200 mA","1500 mA","2000 mA","3000 mA"}; const uint16_t iinlim_vals[] = {100,150,500,900,1200,1500,2000,3000}; int iidx = 6; for (int i = 0; i < 8; i++) if (state->chg_input_current_ma.load() == iinlim_vals[i]) { iidx = i; break; } if (ImGui::Combo("Input current limit", &iidx, iinlim_items, IM_ARRAYSIZE(iinlim_items))) { state->chg_input_current_ma.store(iinlim_vals[iidx]); } // The four remaining limits are linearly quantized; sliders are fine. atomic_slider_int("Input voltage limit (mV)", state->chg_input_voltage_mv, 3880, 5080, "%d mV"); atomic_slider_int("System min voltage (mV)", state->chg_system_min_mv, 3000, 3700, "%d mV"); atomic_slider_int("Fast charge current (mA)", state->chg_fast_current_ma, 512, 4544, "%d mA"); atomic_slider_int("Charge voltage limit (mV)", state->chg_charge_voltage_mv, 3504, 4512, "%d mV"); const char *therm_items[] = {"60 \xC2\xB0""C","80 \xC2\xB0""C","100 \xC2\xB0""C","120 \xC2\xB0""C"}; const uint8_t therm_vals[] = {60, 80, 100, 120}; int tidx = 0; for (int i = 0; i < 4; i++) if (state->chg_thermal_c.load() == therm_vals[i]) { tidx = i; break; } if (ImGui::Combo("Thermal regulation", &tidx, therm_items, IM_ARRAYSIZE(therm_items))) { state->chg_thermal_c.store(therm_vals[tidx]); } } if (ImGui::CollapsingHeader("Storage")) { bool ins = state->sd_inserted.load(); if (ImGui::Checkbox("SD card inserted", &ins)) { state->sd_inserted.store(ins); } ImGui::TextDisabled("SD CID (read by Hekate at SD init -- press 'Reboot' to apply):"); // manfid: vendor preset + free hex entry vendor_preset_combo("Vendor preset", state->sd_cid_manfid, kSdVendors, IM_ARRAYSIZE(kSdVendors)); { uint32_t v = state->sd_cid_manfid.load(); if (ImGui::InputScalar("Manfid (hex)", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", ImGuiInputTextFlags_CharsHexadecimal)) { state->sd_cid_manfid.store((uint8_t)(v & 0xFF)); } } // OEM ID (2 ASCII chars) { uint16_t raw = state->sd_cid_oemid.load(); char buf[3] = { (char)(raw >> 8), (char)(raw & 0xFF), 0 }; if (ImGui::InputText("OEM ID (2 ASCII)", buf, sizeof(buf))) { uint16_t packed = ((uint16_t)(uint8_t)buf[0] << 8) | (uint16_t)(uint8_t)buf[1]; state->sd_cid_oemid.store(packed); } } // Product name (5 ASCII chars, packed in low 5 bytes of uint64) { uint64_t raw = state->sd_cid_prod_name.load(); char buf[6]; for (int i = 0; i < 5; i++) buf[i] = (char)((raw >> (i * 8)) & 0xFF); buf[5] = 0; if (ImGui::InputText("Product (5 ASCII)", buf, sizeof(buf))) { uint64_t packed = 0; for (int i = 0; i < 5; i++) packed |= (uint64_t)(uint8_t)buf[i] << (i * 8); state->sd_cid_prod_name.store(packed); } } atomic_slider_int ("HW rev", state->sd_cid_hwrev, 0, 15); atomic_slider_int ("FW rev", state->sd_cid_fwrev, 0, 15); // Serial as 8-digit hex { uint32_t sn = state->sd_cid_serial.load(); if (ImGui::InputScalar("Serial", ImGuiDataType_U32, &sn, nullptr, nullptr, "%08X", ImGuiInputTextFlags_CharsHexadecimal)) { state->sd_cid_serial.store(sn); } } atomic_slider_int ("Month", state->sd_cid_month, 1, 12); atomic_slider_int ("Year", state->sd_cid_year, 2000, 2099); ImGui::Spacing(); ImGui::TextDisabled("eMMC CID (SDMMC4 -- press 'Reboot' to apply):"); vendor_preset_combo("eMMC vendor preset", state->emmc_cid_manfid, kEmmcVendors, IM_ARRAYSIZE(kEmmcVendors)); { uint32_t v = state->emmc_cid_manfid.load(); if (ImGui::InputScalar("eMMC Manfid (hex)", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", ImGuiInputTextFlags_CharsHexadecimal)) { state->emmc_cid_manfid.store((uint8_t)(v & 0xFF)); } } { uint32_t v = state->emmc_cid_oemid.load(); if (ImGui::InputScalar("eMMC OEM ID", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", ImGuiInputTextFlags_CharsHexadecimal)) { state->emmc_cid_oemid.store((uint8_t)(v & 0xFF)); } } { uint64_t raw = state->emmc_cid_prod_name.load(); char buf[7]; for (int i = 0; i < 6; i++) buf[i] = (char)((raw >> (i * 8)) & 0xFF); buf[6] = 0; if (ImGui::InputText("eMMC Product (6 ASCII)", buf, sizeof(buf))) { uint64_t packed = 0; for (int i = 0; i < 6; i++) packed |= (uint64_t)(uint8_t)buf[i] << (i * 8); state->emmc_cid_prod_name.store(packed); } } { uint32_t v = state->emmc_cid_prv.load(); if (ImGui::InputScalar("eMMC Prod Rev", ImGuiDataType_U32, &v, nullptr, nullptr, "%02X", ImGuiInputTextFlags_CharsHexadecimal)) { state->emmc_cid_prv.store((uint8_t)(v & 0xFF)); } } { uint32_t sn = state->emmc_cid_serial.load(); if (ImGui::InputScalar("eMMC Serial", ImGuiDataType_U32, &sn, nullptr, nullptr, "%08X", ImGuiInputTextFlags_CharsHexadecimal)) { state->emmc_cid_serial.store(sn); } } atomic_slider_int ("eMMC Month", state->emmc_cid_month, 1, 12); // 2025 is the upper bound: Hekate adds +16 to the parsed year only // when it's strictly < 2010, so raw offsets 13..15 (2010..2012) never // get bumped and eMMC years 2026..2028 are unreachable. atomic_slider_int ("eMMC Year", state->emmc_cid_year, 2013, 2025); } if (ImGui::CollapsingHeader("USB-PD (BM92T36)", ImGuiTreeNodeFlags_DefaultOpen)) { bool ins = state->usb_pd_inserted.load(); if (ImGui::Checkbox("Cable inserted", &ins)) { state->usb_pd_inserted.store(ins); } atomic_slider_int("PDO voltage (mV)", state->usb_pd_voltage_mv, 5000, 20000, "%d mV"); atomic_slider_int("PDO amperage (mA)", state->usb_pd_amperage_ma, 500, 3000, "%d mA"); } if (ImGui::CollapsingHeader("USB device mode")) { // The PC in t210/usb.cpp: it enumerates a gadget the payload brings up. bool host = state->usb_host.load(); if (ImGui::Checkbox("PC on the USB-C port (--usb-host)", &host)) { state->usb_host.store(host); } // Mass storage served over NBD instead (--usb-host nbd[:port]); // takes effect when the next disk is attached. bool nbd = state->usb_nbd_port.load() != 0; if (ImGui::Checkbox("Serve mass storage over NBD", &nbd)) { state->usb_nbd_port.store(nbd ? 10809 : 0); } if (nbd) { atomic_slider_int("NBD port", state->usb_nbd_port, 1024, 65535, "%d"); ImGui::TextDisabled("127.0.0.1 only; the disk is ejected when the client disconnects."); } else { ImGui::TextDisabled("Mass storage is read back, written back unchanged and ejected;\nHID is polled."); } } if (ImGui::CollapsingHeader("Display")) { int bl = (int)state->backlight; if (ImGui::SliderInt("Backlight", &bl, 0, 255)) { state->backlight = (uint32_t)bl; } const char *rot_items[] = {"Auto", "0\xC2\xB0", "90\xC2\xB0", "180\xC2\xB0", "270\xC2\xB0"}; int rot_idx = state->rotation_override + 1; // -1..3 -> 0..4 if (ImGui::Combo("Rotation", &rot_idx, rot_items, IM_ARRAYSIZE(rot_items))) { state->rotation_override = rot_idx - 1; } } if (ImGui::CollapsingHeader("Display panel (DSI)")) { // Panel ID raw bytes are read by Hekate via MIPI_DCS_GET_DISPLAY_ID. // The decoded ID is ((raw >> 8) & 0xFF00) | (raw & 0xFF). // Decoded ID is `((raw >> 8) & 0xFF00) | (raw & 0xFF)`, which means // the high byte of the decoded ID lives in raw[23:16] (MSB of raw) // and the low byte lives in raw[7:0]. raw[15:8] is the rev byte that // Hekate uses to disambiguate AZ1/AZ2/AZ3 etc. struct PanelEntry { uint32_t raw; const char *name; }; static const PanelEntry kPanels[] = { {0x099310, "JDI LAM062M109A (10 93 09)"}, {0x269310, "JDI LPM062M326A (10 93 26)"}, {0x0F9320, "InnoLux P062CCA-AZ1 (20 93 0F)"}, {0x0F9330, "AUO A062TAN00 (30 93 0F)"}, {0x109320, "InnoLux 2J055IA-27A (20 93 10)"}, {0x109330, "AUO A055TAN01 (30 93 10)"}, {0x109340, "Sharp LQ055T1SW10 (40 93 10)"}, {0x205050, "Samsung AMS699VC01 (50 50 20)"}, {0xCCCCCC, "Failed (sentinel CC CC CC)"}, }; uint32_t cur = state->panel_id_raw.load() & 0xFFFFFF; const char *match = "Custom"; for (const auto &p : kPanels) if (p.raw == cur) { match = p.name; break; } char preview[64]; snprintf(preview, sizeof(preview), "%s", match); if (ImGui::BeginCombo("Panel preset", preview)) { for (const auto &p : kPanels) { bool sel = (p.raw == cur); if (ImGui::Selectable(p.name, sel)) state->panel_id_raw.store(p.raw); if (sel) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } atomic_hex_input("Raw 24-bit", state->panel_id_raw); ImGui::TextDisabled("Hekate prints \"ID: b0 b1 b2\" with b0 = raw & 0xFF, etc."); } if (ImGui::CollapsingHeader("Touch panel (FTS4)")) { // Hekate pairs each FW ID with a specific panel idx. Keeping the two // selectors in sync avoids landing in the "Error" pairing branch. // Index aligns with the panel combo: kPairedFw[idx] is the FW ID // Hekate marks "Paired" for that panel. static constexpr uint32_t kPairedFw[] = { 0x32000001, // 0: NISSHA NFT-K12D -> 4CD60D/1 0x32000102, // 1: GiS GGM6 B2X -> 4CD60D/2 0x32000302, // 2: NISSHA NBF-K9A -> 4CD60D/3 0x32000402, // 3: GiS 5.5" -> 4CD60D/4 0x32000501, // 4: Samsung TSP -> 4CD60D/5 0x00100100, // 5: GiS VA 6.2" -> 4CD60D/0 (Hekate stores idx -1) }; const char *kPanelNames[] = { "0 - NISSHA NFT-K12D", "1 - GiS GGM6 B2X", "2 - NISSHA NBF-K9A", "3 - GiS 5.5\"", "4 - Samsung TSP", "5 - GiS VA 6.2\"", }; int idx = (int)state->touch_panel_idx.load(); if (ImGui::Combo("Panel", &idx, kPanelNames, IM_ARRAYSIZE(kPanelNames))) { state->touch_panel_idx.store((uint8_t)idx); state->touch_fw_id.store(kPairedFw[idx]); } struct FwEntry { uint32_t id; const char *label; uint8_t panel_idx; }; static const FwEntry kFwIds[] = { {0x00100100, "4CD60D/0 (GiS VA 6.2\")", 5}, {0x32000001, "4CD60D/1 (NISSHA NFT-K12D)", 0}, {0x32000102, "4CD60D/2 (GiS GGM6 B2X)", 1}, {0x32000302, "4CD60D/3 (NISSHA NBF-K9A)", 2}, {0x32000402, "4CD60D/4 (GiS 5.5\")", 3}, {0x32000501, "4CD60D/5 (Samsung TSP)", 4}, }; uint32_t cur_fw = state->touch_fw_id.load(); const char *fw_match = "Custom"; for (const auto &e : kFwIds) if (e.id == cur_fw) { fw_match = e.label; break; } if (ImGui::BeginCombo("FW preset", fw_match)) { for (const auto &e : kFwIds) { bool sel = (e.id == cur_fw); if (ImGui::Selectable(e.label, sel)) { state->touch_fw_id.store(e.id); state->touch_panel_idx.store(e.panel_idx); } if (sel) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } atomic_hex_input("FW ID raw", state->touch_fw_id); uint16_t ftb = state->touch_ftb_ver.load(); if (ImGui::InputScalar("FTB ver", ImGuiDataType_U16, &ftb, nullptr, nullptr, "%04X", ImGuiInputTextFlags_CharsHexadecimal)) state->touch_ftb_ver.store(ftb); // Hekate prints fw_rev byte-swapped, so we expose the value the user // sees on screen and swap on store. byte_swap_16 is self-inverse. uint16_t raw = state->touch_fw_rev.load(); uint16_t shown = (uint16_t)((raw >> 8) | (raw << 8)); if (ImGui::InputScalar("FW rev", ImGuiDataType_U16, &shown, nullptr, nullptr, "%04X", ImGuiInputTextFlags_CharsHexadecimal)) state->touch_fw_rev.store((uint16_t)((shown >> 8) | (shown << 8))); } if (ImGui::CollapsingHeader("Fuses (advanced)")) { // Each row corresponds to a FUSE_BASE offset in Hekate's // bdk/soc/fuse.h. Defaults match a typical Erista golden sample. atomic_hex_input("0x100 PRODUCTION_MODE", state->fuse_at(0x100)); atomic_hex_input("0x110 SKU_INFO (0x83 ODIN; Minerva needs it)", state->fuse_at(0x110)); atomic_hex_input("0x114 CPU_SPEEDO_0", state->fuse_at(0x114)); atomic_hex_input("0x118 CPU_IDDQ", state->fuse_at(0x118)); atomic_hex_input("0x128 OPT_FT_REV", state->fuse_at(0x128)); atomic_hex_input("0x12C CPU_SPEEDO_1", state->fuse_at(0x12C)); atomic_hex_input("0x130 CPU_SPEEDO_2", state->fuse_at(0x130)); atomic_hex_input("0x134 SOC_SPEEDO_0", state->fuse_at(0x134)); atomic_hex_input("0x138 SOC_SPEEDO_1 (BROM rev)", state->fuse_at(0x138)); atomic_hex_input("0x13C SOC_SPEEDO_2", state->fuse_at(0x13C)); atomic_hex_input("0x140 SOC_IDDQ", state->fuse_at(0x140)); atomic_hex_input("0x148 FA", state->fuse_at(0x148)); atomic_hex_input("0x190 OPT_CP_REV", state->fuse_at(0x190)); atomic_hex_input("0x1A0 SECURITY_MODE", state->fuse_at(0x1A0)); atomic_hex_input("0x1C0 RESERVED_SW", state->fuse_at(0x1C0)); ImGui::Separator(); ImGui::TextDisabled("Reserved ODM (anti-downgrade + keygen rev)"); ImGui::TextDisabled("ODM7 popcount = burnt anti-downgrade fuse count."); ImGui::TextDisabled("HOS 1.0.0=1, 7.0.0=9, 8.1.0=10, 13.2.1=16, 22.0.0=23"); atomic_hex_input("0x1C8 RESERVED_ODM0", state->fuse_at(0x1C8)); atomic_hex_input("0x1CC RESERVED_ODM1", state->fuse_at(0x1CC)); atomic_hex_input("0x1D0 RESERVED_ODM2 (Mariko keygen rev: low 5 bits)", state->fuse_at(0x1D0)); atomic_hex_input("0x1D4 RESERVED_ODM3", state->fuse_at(0x1D4)); atomic_hex_input("0x1D8 RESERVED_ODM4 (DRAM ID in bits 7:3)", state->fuse_at(0x1D8)); atomic_hex_input("0x1DC RESERVED_ODM5", state->fuse_at(0x1DC)); atomic_hex_input("0x1E0 RESERVED_ODM6", state->fuse_at(0x1E0)); atomic_hex_input("0x1E4 RESERVED_ODM7 (anti-downgrade)", state->fuse_at(0x1E4)); if (ImGui::Button("Seed ODM7 for HOS 13.2.1 (16 fuses, 0x0000FFFF)")) { state->fuse_at(0x1E4).store(0x0000FFFFu); } ImGui::SameLine(); if (ImGui::Button("Seed ODM7 for HOS 22.0.0 (23 fuses, 0x007FFFFF)")) { state->fuse_at(0x1E4).store(0x007FFFFFu); } if (ImGui::Button("Clear ODM7 (0 fuses, fresh console)")) { state->fuse_at(0x1E4).store(0u); } ImGui::Separator(); atomic_hex_input("0x200 OPT_VENDOR_CODE", state->fuse_at(0x200)); atomic_hex_input("0x204 OPT_FAB_CODE", state->fuse_at(0x204)); atomic_hex_input("0x208 OPT_LOT_CODE_0", state->fuse_at(0x208)); atomic_hex_input("0x210 OPT_WAFER_ID", state->fuse_at(0x210)); atomic_hex_input("0x214 OPT_X_COORDINATE", state->fuse_at(0x214)); atomic_hex_input("0x218 OPT_Y_COORDINATE", state->fuse_at(0x218)); atomic_hex_input("0x228 GPU_IDDQ", state->fuse_at(0x228)); ImGui::Separator(); ImGui::TextDisabled("Public key (8 words, byte-swapped on display)"); atomic_hex_input("0x164 PK0", state->fuse_at(0x164)); atomic_hex_input("0x168 PK1", state->fuse_at(0x168)); atomic_hex_input("0x16C PK2", state->fuse_at(0x16C)); atomic_hex_input("0x170 PK3", state->fuse_at(0x170)); atomic_hex_input("0x174 PK4", state->fuse_at(0x174)); atomic_hex_input("0x178 PK5", state->fuse_at(0x178)); atomic_hex_input("0x17C PK6", state->fuse_at(0x17C)); atomic_hex_input("0x180 PK7", state->fuse_at(0x180)); ImGui::Separator(); ImGui::TextDisabled("SBK (4 words) + DK (1 word). --prod-keys overrides at runtime."); atomic_hex_input("0x1A4 SBK0", state->fuse_at(0x1A4)); atomic_hex_input("0x1A8 SBK1", state->fuse_at(0x1A8)); atomic_hex_input("0x1AC SBK2", state->fuse_at(0x1AC)); atomic_hex_input("0x1B0 SBK3", state->fuse_at(0x1B0)); atomic_hex_input("0x1B4 DK", state->fuse_at(0x1B4)); ImGui::TextDisabled("DRAM ID = (ODM4 >> 3) & 0x1F (T210B01 also OR's bits 14:12 << 5)"); } if (ImGui::CollapsingHeader("DRAM modules")) { // MR5 vendor codes per Hekate gui_info.c. static const VendorEntry kDramVendors[] = { {0, "Unknown"}, {1, "Samsung"}, {6, "Hynix"}, {255, "Micron"}, }; vendor_preset_combo("Vendor (MR5)", state->dram_vendor, kDramVendors, IM_ARRAYSIZE(kDramVendors)); uint8_t v = state->dram_vendor.load(); if (ImGui::InputScalar("Vendor raw", ImGuiDataType_U8, &v, nullptr, nullptr, "%u")) state->dram_vendor.store(v); // Hekate prints the rev as "%X.%02X" with MR6 to the left of the dot // and MR7 to the right (e.g. MR6=2, MR7=0 -> "2.00"; MR6=1, MR7=0x0C // -> "1.0C"). Surface both bytes as decimal nibbles so the mapping is // obvious in the UI. int r1 = (int)state->dram_rev_id1.load(); if (ImGui::SliderInt("Rev major (MR6)", &r1, 0, 15, "%d")) state->dram_rev_id1.store((uint8_t)r1); int r2 = (int)state->dram_rev_id2.load(); if (ImGui::SliderInt("Rev minor (MR7)", &r2, 0, 255, "0x%02X")) state->dram_rev_id2.store((uint8_t)r2); ImGui::TextDisabled("Hekate displays \".\" (e.g. 2.00)."); // Density lives in MR8 bits 5:2. Hekate's gui_info.c only decodes // codes 2-6; anything higher (3-8 GB dies, JEDEC codes 8-11) is shown // as "Unk (N)". Restrict the combo to what Hekate prints; the MR8 raw // input below covers custom codes if you want to exercise that branch. struct DensityEntry { uint8_t code; const char *name; }; static const DensityEntry kDensities[] = { {2, "512 MB / die"}, {3, "768 MB / die"}, {4, "1 GB / die"}, {5, "1.5 GB / die"}, {6, "2 GB / die"}, }; uint8_t d = state->dram_density.load(); uint8_t cur = (d & 0x3C) >> 2; const char *cur_name = "Custom"; for (const auto &e : kDensities) if (e.code == cur) { cur_name = e.name; break; } char preview[40]; snprintf(preview, sizeof(preview), "%s (code %u)", cur_name, cur); if (ImGui::BeginCombo("Density (MR8)", preview)) { for (const auto &e : kDensities) { char item[40]; snprintf(item, sizeof(item), "%s (code %u)", e.name, e.code); bool sel = (e.code == cur); if (ImGui::Selectable(item, sel)) state->dram_density.store((uint8_t)((d & ~0x3C) | (e.code << 2))); if (sel) ImGui::SetItemDefaultFocus(); } ImGui::EndCombo(); } if (ImGui::InputScalar("MR8 raw", ImGuiDataType_U8, &d, nullptr, nullptr, "0x%02X", ImGuiInputTextFlags_CharsHexadecimal)) state->dram_density.store(d); ImGui::TextDisabled("Total = die size x ranks x channels (Hekate prints \" x \")."); } if (ImGui::CollapsingHeader("Emulation")) { bool paused = state->paused.load(); if (ImGui::Checkbox("Paused", &paused)) { state->paused.store(paused); } ImGui::SameLine(); if (ImGui::Button("Reboot")) { state->reboot_cold = true; // the user's reset is a power cycle state->reboot_requested.store(true); } ImGui::Text("emu_usec: %llu", (unsigned long long)state->emu_usec); ImGui::Text("insn_count: %llu", (unsigned long long)state->insn_count); ImGui::Separator(); ImGui::TextDisabled("Buttons (visual; SDL keys still work):"); bool vu = state->btn_vol_up.load(); bool vd = state->btn_vol_down.load(); bool pw = state->btn_power.load(); if (ImGui::Checkbox("VOL+", &vu)) state->btn_vol_up.store(vu); ImGui::SameLine(); if (ImGui::Checkbox("VOL-", &vd)) state->btn_vol_down.store(vd); ImGui::SameLine(); if (ImGui::Checkbox("POWER", &pw)) state->btn_power.store(pw); } ImGui::Separator(); if (ImGui::Button("Reset to defaults")) { reset_to_defaults(state); } ImGui::SameLine(); static double g_save_msg_until = 0.0; static bool g_save_ok = true; if (ImGui::Button("Save configuration")) { g_save_ok = config_window_save_ini_impl(state, "rcm_emu.ini"); g_save_msg_until = ImGui::GetTime() + 2.5; } if (ImGui::GetTime() < g_save_msg_until) { ImGui::SameLine(); if (g_save_ok) ImGui::TextColored(ImVec4(0.4f, 1.0f, 0.4f, 1.0f), "Saved to rcm_emu.ini"); else ImGui::TextColored(ImVec4(1.0f, 0.5f, 0.5f, 1.0f), "Save failed (check cwd is writable)"); } ImGui::End(); } } // namespace bool config_window_save_ini(const EmuState *state, const char *path) { return config_window_save_ini_impl(state, path); } bool config_window_load_ini(EmuState *state, const char *path) { return config_window_load_ini_impl(state, path); } bool config_window_init() { g_window = SDL_CreateWindow("rcm_emu - Hardware Config", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 480, 720, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE); if (!g_window) { fprintf(stderr, "[config_window] SDL_CreateWindow failed: %s\n", SDL_GetError()); return false; } // No PRESENTVSYNC: this window renders on the emulation thread, and // vsync would stall the emulated CPU for up to a frame per present. g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED); // Same fallback as the main window: no GPU renderer (a headless run, a // VM without GL) should not cost the window altogether. if (!g_renderer) g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_SOFTWARE); if (!g_renderer) { fprintf(stderr, "[config_window] SDL_CreateRenderer failed: %s\n", SDL_GetError()); SDL_DestroyWindow(g_window); g_window = nullptr; return false; } IMGUI_CHECKVERSION(); g_imgui = ImGui::CreateContext(); ImGui::StyleColorsDark(); if (!ImGui_ImplSDL2_InitForSDLRenderer(g_window, g_renderer)) { fprintf(stderr, "[config_window] ImGui SDL2 backend init failed\n"); ImGui::DestroyContext(g_imgui); g_imgui = nullptr; return false; } if (!ImGui_ImplSDLRenderer2_Init(g_renderer)) { fprintf(stderr, "[config_window] ImGui SDLRenderer2 backend init failed\n"); ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(g_imgui); g_imgui = nullptr; return false; } g_window_id = SDL_GetWindowID(g_window); g_visible = false; return true; } void config_window_shutdown() { if (g_imgui) { // Critical: explicitly switch to *our* context first. After // console_window_shutdown destroys its context, ImGui's "current" // pointer can be NULL or stale, and ImGui_ImplSDLRenderer2_Shutdown // pulls BackendRendererUserData off the current context's IO. Skip // this and the assert at imgui_impl_sdlrenderer2.cpp:292 fires // because bd resolves to null. ImGui::SetCurrentContext(g_imgui); ImGuiIO &io = ImGui::GetIO(); if (io.BackendRendererUserData) ImGui_ImplSDLRenderer2_Shutdown(); if (io.BackendPlatformUserData) ImGui_ImplSDL2_Shutdown(); ImGui::DestroyContext(g_imgui); g_imgui = nullptr; } if (g_renderer) { SDL_DestroyRenderer(g_renderer); g_renderer = nullptr; } if (g_window) { SDL_DestroyWindow(g_window); g_window = nullptr; } g_window_id = 0; g_visible = false; } bool config_window_handle_event(const SDL_Event &ev) { if (!g_imgui) return false; ImGui::SetCurrentContext(g_imgui); ImGui_ImplSDL2_ProcessEvent(&ev); // Hide on close-button click rather than tearing the window down — the M // key flips it back on. if (ev.type == SDL_WINDOWEVENT && ev.window.windowID == g_window_id && ev.window.event == SDL_WINDOWEVENT_CLOSE) { SDL_HideWindow(g_window); g_visible = false; } return true; } void config_window_toggle() { if (!g_window) return; g_visible = !g_visible; if (g_visible) { SDL_ShowWindow(g_window); SDL_RaiseWindow(g_window); } else { SDL_HideWindow(g_window); } } bool config_window_is_visible() { return g_visible; } Uint32 config_window_id() { return g_window_id; } void config_window_render(EmuState *state) { if (!g_visible || !g_imgui) return; ImGui::SetCurrentContext(g_imgui); ImGui_ImplSDLRenderer2_NewFrame(); ImGui_ImplSDL2_NewFrame(); ImGui::NewFrame(); build_ui(state); ImGui::Render(); SDL_SetRenderDrawColor(g_renderer, 30, 30, 30, 255); SDL_RenderClear(g_renderer); ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), g_renderer); SDL_RenderPresent(g_renderer); }