-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_window.cpp
More file actions
968 lines (900 loc) · 45.4 KB
/
Copy pathconfig_window.cpp
File metadata and controls
968 lines (900 loc) · 45.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
// 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 <cstdio>
#include <cstdlib>
#include <cstring>
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 = 0;
s->cpu_pmic_version = 0;
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) \
/* 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(cpu_pmic_version, "soc", "cpu_pmic_version",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);
// Fuses are dumped wholesale — small enough (256 × 4 bytes) and keeps the
// load path branch-free.
fprintf(f, "\n[fuses]\n");
for (size_t i = 0; i < EmuState::FUSE_WORDS; i++) {
uint32_t v = s->fuse_word[i].load();
if (v) 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, "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 <typename T>
bool atomic_slider_int(const char *label, std::atomic<T> &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 <typename T>
bool atomic_slider_temp(const char *label, std::atomic<T> &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<uint8_t> &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<uint32_t> &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<uint16_t>("SOC (%)", state->bat_soc_pct, 0, 100, "%d %%");
atomic_slider_int<uint16_t>("Voltage (mV)", state->bat_vcell_mv, 3000, 4400, "%d mV");
atomic_slider_int<uint16_t>("OCV (mV)", state->bat_ocv_mv, 3000, 4400, "%d mV");
atomic_slider_temp<int16_t> ("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<uint16_t>("RepCap (mAh)", state->bat_capacity_mah, 0, 6000, "%d mAh");
atomic_slider_int<uint16_t>("FullCAP (mAh)", state->bat_full_cap_mah, 0, 6000, "%d mAh");
atomic_slider_int<uint16_t>("DesignCap (mAh)", state->bat_design_cap_mah, 0, 6000, "%d mAh");
atomic_slider_int<uint16_t>("V_empty (mV)", state->bat_v_empty_mv, 2500, 3800, "%d mV");
atomic_slider_int<uint16_t>("Min volt (mV)", state->bat_min_volt_mv, 2500, 4400, "%d mV");
atomic_slider_int<uint16_t>("Max volt (mV)", state->bat_max_volt_mv, 2500, 4400, "%d mV");
atomic_slider_int<uint8_t> ("Age (%)", state->bat_age_pct, 0, 100, "%d %%");
atomic_slider_int<uint16_t>("Cycles", state->bat_cycles, 0, 2000);
}
if (ImGui::CollapsingHeader("Thermal (TMP451)", ImGuiTreeNodeFlags_DefaultOpen)) {
atomic_slider_temp<int16_t>("SoC temp", state->soc_temp_c10, 0.0f, 110.0f);
atomic_slider_temp<int16_t>("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);
}
atomic_slider_int<uint8_t>("MAX77620 silicon rev", state->pmic_silicon_rev, 0, 15);
atomic_slider_int<uint8_t>("MAX77621 chipid", state->cpu_pmic_version, 0, 15);
}
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<uint16_t>("Input voltage limit (mV)", state->chg_input_voltage_mv, 3880, 5080, "%d mV");
atomic_slider_int<uint16_t>("System min voltage (mV)", state->chg_system_min_mv, 3000, 3700, "%d mV");
atomic_slider_int<uint16_t>("Fast charge current (mA)", state->chg_fast_current_ma, 512, 4544, "%d mA");
atomic_slider_int<uint16_t>("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<uint8_t> ("HW rev", state->sd_cid_hwrev, 0, 15);
atomic_slider_int<uint8_t> ("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<uint8_t> ("Month", state->sd_cid_month, 1, 12);
atomic_slider_int<uint16_t> ("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<uint8_t> ("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<uint16_t> ("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<uint16_t>("PDO voltage (mV)", state->usb_pd_voltage_mv, 5000, 20000, "%d mV");
atomic_slider_int<uint16_t>("PDO amperage (mA)", state->usb_pd_amperage_ma, 500, 3000, "%d mA");
}
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", 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 \"<major>.<minor as 2 hex>\" (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 \"<n> x <die>\").");
}
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_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;
}
g_renderer = SDL_CreateRenderer(g_window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
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);
}