-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathfrontend.cpp
2878 lines (2338 loc) · 90.9 KB
/
frontend.cpp
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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "frontend.h"
#include <algorithm>
#include <array>
#include <cerrno>
#include <charconv>
#include <climits> // For INT_MAX.
#include <cmath>
#include <cstddef>
#include <filesystem>
#include <forward_list>
#include <functional>
#include <iterator>
#include <list>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <vector>
#include <fmt/core.h>
#include <SDL3/SDL.h>
#include "common/core/clowncommon/clowncommon.h"
#include "common/core/clownmdemu.h"
#include "libraries/imgui/imgui.h"
#include "libraries/imgui/backends/imgui_impl_sdl3.h"
#include "libraries/imgui/backends/imgui_impl_sdlrenderer3.h"
#include "libraries/inih/ini.h"
#include "cd-reader.h"
#include "debug-log.h"
#include "emulator-instance.h"
#include "file-utilities.h"
#include "windows/debug-fm.h"
#include "windows/debug-frontend.h"
#include "windows/debug-log-viewer.h"
#include "windows/debug-m68k.h"
#include "windows/debug-memory.h"
#include "windows/debug-pcm.h"
#include "windows/debug-psg.h"
#include "windows/debug-vdp.h"
#include "windows/debug-z80.h"
#include "windows/disassembler.h"
#include "windows/common/window-with-framebuffer.h"
#ifndef __EMSCRIPTEN__
#define FILE_PATH_SUPPORT
#endif
#define VERSION "v1.1"
using namespace Frontend;
static constexpr unsigned int INITIAL_WINDOW_WIDTH = 320 * 2;
static constexpr unsigned int INITIAL_WINDOW_HEIGHT = 224 * 2;
static constexpr unsigned int FRAMEBUFFER_WIDTH = 320;
static constexpr unsigned int FRAMEBUFFER_HEIGHT = 240 * 2; // *2 because of double-resolution mode.
static constexpr char DEFAULT_TITLE[] = "ClownMDEmu";
struct ControllerInput
{
SDL_JoystickID joystick_instance_id;
Sint16 left_stick_x = 0;
Sint16 left_stick_y = 0;
std::array<bool, 4> left_stick = {false};
std::array<bool, 4> dpad = {false};
bool left_trigger = false;
bool right_trigger = false;
Input input;
ControllerInput(const SDL_JoystickID joystick_instance_id) : joystick_instance_id(joystick_instance_id) {}
};
#ifdef FILE_PATH_SUPPORT
struct RecentSoftware
{
bool is_cd_file;
std::filesystem::path path;
};
#endif
class AboutWindow : public WindowPopup<AboutWindow>
{
private:
using Base = WindowPopup<AboutWindow>;
static constexpr Uint32 window_flags = ImGuiWindowFlags_HorizontalScrollbar;
void DisplayInternal()
{
static const auto licence_clownmdemu = std::to_array<const char>({
#include "licences/clownmdemu.h"
});
static const auto licence_dear_imgui = std::to_array<const char>({
#include "licences/dear-imgui.h"
});
#ifdef __EMSCRIPTEN__
static const auto licence_emscripten_browser_file = std::to_array<const char>({
#include "licences/emscripten-browser-file.h"
});
#endif
#ifdef IMGUI_ENABLE_FREETYPE
static const auto licence_freetype = std::to_array<const char>({
#include "licences/freetype.h"
});
static const auto licence_freetype_bdf = std::to_array<const char>({
#include "licences/freetype-bdf.h"
});
static const auto licence_freetype_pcf = std::to_array<const char>({
#include "licences/freetype-pcf.h"
});
static const auto licence_freetype_fthash = std::to_array<const char>({
#include "licences/freetype-fthash.h"
});
static const auto licence_freetype_ft_hb = std::to_array<const char>({
#include "licences/freetype-ft-hb.h"
});
#endif
static const auto licence_inih = std::to_array<const char>({
#include "licences/inih.h"
});
static const auto licence_noto_sans = std::to_array<const char>({
#include "licences/noto-sans.h"
});
static const auto licence_inconsolata = std::to_array<const char>({
#include "licences/inconsolata.h"
});
const auto monospace_font = GetMonospaceFont();
ImGui::SeparatorText("ClownMDEmu " VERSION);
ImGui::TextUnformatted("This is a Sega Mega Drive (AKA Sega Genesis) emulator. Created by Clownacy.");
constexpr auto DoURL = [](const char* const url)
{
if (ImGui::TextLink(url))
SDL_OpenURL(url);
};
DoURL("https://github.com/Clownacy/clownmdemu-frontend");
DoURL("https://github.com/Clownacy/clownmdemu-core");
ImGui::SeparatorText("Licences");
const auto DoLicence = [&monospace_font]<size_t S>(const std::array<char, S> &text)
{
ImGui::PushFont(monospace_font);
ImGui::TextUnformatted(&text.front(), &text.back());
ImGui::PopFont();
};
if (ImGui::CollapsingHeader("ClownMDEmu"))
DoLicence(licence_clownmdemu);
if (ImGui::CollapsingHeader("Dear ImGui"))
DoLicence(licence_dear_imgui);
#ifdef __EMSCRIPTEN__
if (ImGui::CollapsingHeader("Emscripten Browser File Library"))
DoLicence(licence_emscripten_browser_file);
#endif
#ifdef IMGUI_ENABLE_FREETYPE
if (ImGui::CollapsingHeader("FreeType"))
{
if (ImGui::TreeNode("General"))
{
DoLicence(licence_freetype);
ImGui::TreePop();
}
if (ImGui::TreeNode("BDF Driver"))
{
DoLicence(licence_freetype_bdf);
ImGui::TreePop();
}
if (ImGui::TreeNode("PCF Driver"))
{
DoLicence(licence_freetype_pcf);
ImGui::TreePop();
}
if (ImGui::TreeNode("fthash.c & fthash.h"))
{
DoLicence(licence_freetype_fthash);
ImGui::TreePop();
}
if (ImGui::TreeNode("ft-hb.c & ft-hb.h"))
{
DoLicence(licence_freetype_ft_hb);
ImGui::TreePop();
}
}
#endif
if (ImGui::CollapsingHeader("inih"))
DoLicence(licence_inih);
if (ImGui::CollapsingHeader("Noto Sans"))
DoLicence(licence_noto_sans);
if (ImGui::CollapsingHeader("Inconsolata"))
DoLicence(licence_inconsolata);
}
public:
using Base::WindowPopup;
friend Base;
};
static void Popup_DoTable(const char* const name, const std::function<void()> &callback)
{
ImGui::SeparatorText(name);
if (ImGui::BeginTable(name, 2, ImGuiTableFlags_Borders))
{
ImGui::TableSetupColumn("Property");
ImGui::TableSetupColumn("Value");
ImGui::TableHeadersRow();
callback();
ImGui::EndTable();
}
};
template<typename... T>
static void Popup_DoProperty(ImFont* const font, const char* const label, fmt::format_string<T...> format, T &&...args)
{
ImGui::TableNextColumn();
ImGui::TextUnformatted(label);
ImGui::TableNextColumn();
if (font != nullptr)
ImGui::PushFont(font);
ImGui::TextFormatted(format, std::forward<T>(args)...);
if (font != nullptr)
ImGui::PopFont();
};
class DebugCDC : public WindowPopup<DebugCDC>
{
private:
using Base = WindowPopup<DebugCDC>;
static constexpr Uint32 window_flags = 0;
void DisplayInternal()
{
const auto &cdc = Frontend::emulator->CurrentState().clownmdemu.mega_cd.cdc;
Popup_DoTable("Sector Buffer", [&]()
{
Popup_DoProperty(nullptr, "Total Buffered Sectors", "{}", cdc.buffered_sectors_total);
Popup_DoProperty(nullptr, "Read Index", "{}", cdc.buffered_sectors_read_index);
Popup_DoProperty(nullptr, "Write Index", "{}", cdc.buffered_sectors_write_index);
});
Popup_DoTable("Host Data", [&]()
{
Popup_DoProperty(nullptr, "Bound", "{}", cdc.host_data_bound ? "Yes" : "No");
Popup_DoProperty(nullptr, "Target SUB-CPU", "{}", cdc.host_data_target_sub_cpu ? "Yes" : "No");
Popup_DoProperty(nullptr, "Buffered Sector Index", "{}", cdc.host_data_buffered_sector_index);
Popup_DoProperty(nullptr, "Word Index", "{}", cdc.host_data_word_index);
});
Popup_DoTable("Other", [&]()
{
Popup_DoProperty(nullptr, "Current Sector", "{}", cdc.current_sector);
Popup_DoProperty(nullptr, "Sectors Remaining", "{}", cdc.sectors_remaining);
Popup_DoProperty(nullptr, "Device Destination", "{}", cdc.device_destination);
});
}
public:
using Base::WindowPopup;
friend Base;
};
class DebugCDDA : public WindowPopup<DebugCDDA>
{
private:
using Base = WindowPopup<DebugCDDA>;
static constexpr Uint32 window_flags = 0;
void DisplayInternal()
{
const auto &cdda = Frontend::emulator->CurrentState().clownmdemu.mega_cd.cdda;
Popup_DoTable("Volume", [&]()
{
Popup_DoProperty(nullptr, "Volume", "0x{:03X}", cdda.volume);
Popup_DoProperty(nullptr, "Master Volume", "0x{:03X}", cdda.master_volume);
});
Popup_DoTable("Fade", [&]()
{
Popup_DoProperty(nullptr, "Target Volume", "0x{:03X}", cdda.target_volume);
Popup_DoProperty(nullptr, "Fade Step", "0x{:03X}", cdda.fade_step);
Popup_DoProperty(nullptr, "Fade Remaining", "0x{:03X}", cdda.fade_remaining);
Popup_DoProperty(nullptr, "Fade Direction", "{}", cdda.subtract_fade_step ? "Negative" : "Positive");
});
Popup_DoTable("Other", [&]()
{
Popup_DoProperty(nullptr, "Playing", "{}", cdda.playing ? "True" : "False");
Popup_DoProperty(nullptr, "Paused", "{}", cdda.paused ? "True" : "False");
});
}
public:
using Base::WindowPopup;
friend Base;
};
class DebugOther : public WindowPopup<DebugOther>
{
private:
using Base = WindowPopup<DebugOther>;
static constexpr Uint32 window_flags = 0;
void DisplayInternal()
{
const auto monospace_font = GetMonospaceFont();
const ClownMDEmu_State &clownmdemu_state = Frontend::emulator->CurrentState().clownmdemu;
if (ImGui::BeginTable("Other", 2, ImGuiTableFlags_Borders))
{
ImGui::TableSetupColumn("Property");
ImGui::TableSetupColumn("Value");
ImGui::TableHeadersRow();
ImGui::TableNextColumn();
ImGui::TextUnformatted("Z80 Bank");
ImGui::TableNextColumn();
ImGui::PushFont(monospace_font);
ImGui::TextFormatted("0x{:06X}-0x{:06X}", clownmdemu_state.z80.bank * 0x8000, (clownmdemu_state.z80.bank + 1) * 0x8000);
ImGui::PopFont();
ImGui::TableNextColumn();
ImGui::TextUnformatted("Main 68000 Has Z80 Bus");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.z80.bus_requested ? "Yes" : "No");
ImGui::TableNextColumn();
ImGui::TextUnformatted("Z80 Reset Held");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.z80.reset_held ? "Yes" : "No");
ImGui::TableNextColumn();
ImGui::TextUnformatted("Main 68000 Has Sub 68000 Bus");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.m68k.bus_requested ? "Yes" : "No");
ImGui::TableNextColumn();
ImGui::TextUnformatted("Sub 68000 Reset");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.m68k.reset_held ? "Yes" : "No");
ImGui::TableNextColumn();
ImGui::TextUnformatted("PRG-RAM Bank");
ImGui::TableNextColumn();
ImGui::PushFont(monospace_font);
ImGui::TextFormatted("0x{:05X}-0x{:05X}", clownmdemu_state.mega_cd.prg_ram.bank * 0x20000, (clownmdemu_state.mega_cd.prg_ram.bank + 1) * 0x20000);
ImGui::PopFont();
ImGui::TableNextColumn();
ImGui::TextUnformatted("PRG-RAM Write-Protect");
ImGui::TableNextColumn();
ImGui::PushFont(monospace_font);
ImGui::TextFormatted("0x00000-0x{:05X}", clownmdemu_state.mega_cd.prg_ram.write_protect * 0x200);
ImGui::PopFont();
ImGui::TableNextColumn();
ImGui::TextUnformatted("WORD-RAM Mode");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.word_ram.in_1m_mode ? "1M" : "2M");
ImGui::TableNextColumn();
ImGui::TextUnformatted("DMNA Bit");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.word_ram.dmna ? "Set" : "Clear");
ImGui::TableNextColumn();
ImGui::TextUnformatted("RET Bit");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.word_ram.ret ? "Set" : "Clear");
ImGui::TableNextColumn();
ImGui::TextUnformatted("Boot Mode");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.boot_from_cd ? "CD" : "Cartridge");
ImGui::TableNextColumn();
ImGui::TextUnformatted("68000 Communication Flag");
ImGui::TableNextColumn();
ImGui::PushFont(monospace_font);
ImGui::TextFormatted("0x{:04X}", clownmdemu_state.mega_cd.communication.flag);
ImGui::PopFont();
ImGui::TableNextColumn();
ImGui::TextUnformatted("68000 Communication Command");
ImGui::TableNextColumn();
ImGui::PushFont(monospace_font);
for (std::size_t i = 0; i < std::size(clownmdemu_state.mega_cd.communication.command); i += 2)
ImGui::TextFormatted("0x{:04X} 0x{:04X}", clownmdemu_state.mega_cd.communication.command[i + 0], clownmdemu_state.mega_cd.communication.command[i + 1]);
ImGui::PopFont();
ImGui::TableNextColumn();
ImGui::TextUnformatted("68000 Communication Status");
ImGui::TableNextColumn();
ImGui::PushFont(monospace_font);
for (std::size_t i = 0; i < std::size(clownmdemu_state.mega_cd.communication.status); i += 2)
ImGui::TextFormatted("0x{:04X} 0x{:04X}", clownmdemu_state.mega_cd.communication.status[i + 0], clownmdemu_state.mega_cd.communication.status[i + 1]);
ImGui::PopFont();
ImGui::TableNextColumn();
ImGui::TextUnformatted("SUB-CPU Graphics Interrupt");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.irq.enabled[0] ? "Enabled" : "Disabled");
ImGui::TableNextColumn();
ImGui::TextUnformatted("SUB-CPU Mega Drive Interrupt");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.irq.enabled[1] ? "Enabled" : "Disabled");
ImGui::TableNextColumn();
ImGui::TextUnformatted("SUB-CPU Timer Interrupt");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.irq.enabled[2] ? "Enabled" : "Disabled");
ImGui::TableNextColumn();
ImGui::TextUnformatted("SUB-CPU CDD Interrupt");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.irq.enabled[3] ? "Enabled" : "Disabled");
ImGui::TableNextColumn();
ImGui::TextUnformatted("SUB-CPU CDC Interrupt");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.irq.enabled[4] ? "Enabled" : "Disabled");
ImGui::TableNextColumn();
ImGui::TextUnformatted("SUB-CPU Sub-code Interrupt");
ImGui::TableNextColumn();
ImGui::TextUnformatted(clownmdemu_state.mega_cd.irq.enabled[5] ? "Enabled" : "Disabled");
ImGui::EndTable();
}
Popup_DoTable("Graphics Transformation",
[&]()
{
const auto &graphics = clownmdemu_state.mega_cd.rotation;
Popup_DoProperty(nullptr, "Stamp Size", "{} pixels", graphics.large_stamp ? "32x32" : "16x16");
Popup_DoProperty(nullptr, "Stamp Map Size", "{} pixels", graphics.large_stamp_map ? "4096x4096" : "256x256");
Popup_DoProperty(nullptr, "Repeating Stamp Map", "{}", graphics.repeating_stamp_map ? "Yes" : "No");
Popup_DoProperty(nullptr, "Stamp Map Address", "0x{:X}", graphics.stamp_map_address * 4);
Popup_DoProperty(nullptr, "Image Buffer Height in Tiles", "{}", graphics.image_buffer_height_in_tiles + 1);
Popup_DoProperty(nullptr, "Image Buffer Address", "0x{:X}", graphics.image_buffer_address * 4);
Popup_DoProperty(nullptr, "Image Buffer X Offset", "{}", graphics.image_buffer_x_offset);
Popup_DoProperty(nullptr, "Image Buffer Y Offset", "{}", graphics.image_buffer_y_offset);
Popup_DoProperty(nullptr, "Image Buffer Width", "{}", graphics.image_buffer_width);
Popup_DoProperty(nullptr, "Image Buffer Height", "{}", graphics.image_buffer_height);
}
);
}
public:
using Base::WindowPopup;
friend Base;
};
class DebugToggles : public WindowPopup<DebugToggles>
{
private:
using Base = WindowPopup<DebugToggles>;
static constexpr Uint32 window_flags = 0;
void DisplayInternal()
{
const auto DoButton = [](cc_bool &disabled, const char* const label)
{
ImGui::TableNextColumn();
bool temp = !disabled;
if (ImGui::Checkbox(label, &temp))
disabled = !disabled;
};
ImGui::SeparatorText("VDP");
if (ImGui::BeginTable("VDP Options", 2, ImGuiTableFlags_SizingStretchSame))
{
VDP_Configuration &vdp = Frontend::emulator->GetConfigurationVDP();
DoButton(vdp.sprites_disabled, "Sprite Plane");
DoButton(vdp.window_disabled, "Window Plane");
DoButton(vdp.planes_disabled[0], "Plane A");
DoButton(vdp.planes_disabled[1], "Plane B");
ImGui::EndTable();
}
ImGui::SeparatorText("FM");
if (ImGui::BeginTable("FM Options", 2, ImGuiTableFlags_SizingStretchSame))
{
FM_Configuration &fm = Frontend::emulator->GetConfigurationFM();
char buffer[] = "FM1";
for (auto disabled = std::begin(fm.fm_channels_disabled); disabled != std::end(fm.fm_channels_disabled); ++disabled)
{
buffer[2] = '1' + std::distance(std::begin(fm.fm_channels_disabled), disabled);
DoButton(*disabled, buffer);
}
DoButton(fm.dac_channel_disabled, "DAC");
ImGui::EndTable();
}
ImGui::SeparatorText("PSG");
if (ImGui::BeginTable("PSG Options", 2, ImGuiTableFlags_SizingStretchSame))
{
PSG_Configuration &psg = Frontend::emulator->GetConfigurationPSG();
char buffer[] = "PSG1";
for (auto disabled = std::begin(psg.tone_disabled); disabled != std::end(psg.tone_disabled); ++disabled)
{
buffer[3] = '1' + std::distance(std::begin(psg.tone_disabled), disabled);
DoButton(*disabled, buffer);
}
DoButton(psg.noise_disabled, "PSG Noise");
ImGui::EndTable();
}
ImGui::SeparatorText("PCM");
if (ImGui::BeginTable("PCM Options", 2, ImGuiTableFlags_SizingStretchSame))
{
PCM_Configuration &pcm = Frontend::emulator->GetConfigurationPCM();
char buffer[] = "PCM1";
for (auto disabled = std::begin(pcm.channels_disabled); disabled != std::end(pcm.channels_disabled); ++disabled)
{
buffer[3] = '1' + std::distance(std::begin(pcm.channels_disabled), disabled);
DoButton(*disabled, buffer);
}
ImGui::EndTable();
}
}
public:
using Base::WindowPopup;
friend Base;
};
class OptionsWindow : public WindowPopup<OptionsWindow>
{
private:
using Base = WindowPopup<OptionsWindow>;
static constexpr Uint32 window_flags = 0;
void DisplayInternal()
{
ImGui::SeparatorText("Console");
if (ImGui::BeginTable("Console Options", 3))
{
ImGui::TableNextColumn();
ImGui::TextUnformatted("TV Standard:");
DoToolTip(u8"Some games only work with a certain TV standard.");
ImGui::TableNextColumn();
if (ImGui::RadioButton("NTSC", !Frontend::emulator->GetPALMode()))
if (Frontend::emulator->GetPALMode())
Frontend::SetAudioPALMode(false);
DoToolTip(u8"60 FPS");
ImGui::TableNextColumn();
if (ImGui::RadioButton("PAL", Frontend::emulator->GetPALMode()))
if (!Frontend::emulator->GetPALMode())
Frontend::SetAudioPALMode(true);
DoToolTip(u8"50 FPS");
ImGui::TableNextColumn();
ImGui::TextUnformatted("Region:");
DoToolTip(u8"Some games only work with a certain region.");
ImGui::TableNextColumn();
if (ImGui::RadioButton("Japan", Frontend::emulator->GetDomestic()))
Frontend::emulator->SetDomestic(true);
DoToolTip(u8"Games may show Japanese text.");
ImGui::TableNextColumn();
if (ImGui::RadioButton("Elsewhere", !Frontend::emulator->GetDomestic()))
Frontend::emulator->SetDomestic(false);
DoToolTip(u8"Games may show English text.");
ImGui::EndTable();
}
ImGui::SeparatorText("Video");
if (ImGui::BeginTable("Video Options", 2))
{
ImGui::TableNextColumn();
if (ImGui::Checkbox("V-Sync", &Frontend::use_vsync))
SDL_SetRenderVSync(Frontend::window->GetRenderer(), Frontend::use_vsync);
DoToolTip(u8"Prevents screen tearing.");
ImGui::TableNextColumn();
if (ImGui::Checkbox("Integer Screen Scaling", &Frontend::integer_screen_scaling) && Frontend::integer_screen_scaling)
{
// Reclaim memory used by the upscaled framebuffer, since we won't be needing it anymore.
SDL_DestroyTexture(Frontend::framebuffer_texture_upscaled);
Frontend::framebuffer_texture_upscaled = nullptr;
}
DoToolTip(u8"Preserves pixel aspect ratio,\navoiding non-square pixels.");
ImGui::TableNextColumn();
ImGui::Checkbox("Tall Interlace Mode 2", &Frontend::tall_double_resolution_mode);
DoToolTip(u8"Makes games that use Interlace Mode 2\nfor split-screen not appear squashed.");
ImGui::EndTable();
}
ImGui::SeparatorText("Audio");
if (ImGui::BeginTable("Audio Options", 2))
{
ImGui::TableNextColumn();
bool low_pass_filter = Frontend::emulator->GetLowPassFilter();
if (ImGui::Checkbox("Low-Pass Filter", &low_pass_filter))
Frontend::emulator->SetLowPassFilter(low_pass_filter);
DoToolTip(u8"Makes the audio sound 'softer',\njust like on a real Mega Drive.");
ImGui::TableNextColumn();
bool ladder_effect = !Frontend::emulator->GetConfigurationFM().ladder_effect_disabled;
if (ImGui::Checkbox("Low-Volume Distortion", &ladder_effect))
Frontend::emulator->GetConfigurationFM().ladder_effect_disabled = !ladder_effect;
DoToolTip(u8"Approximates the so-called 'ladder effect' that\nis present in early Mega Drives. Without this,\ncertain sounds in some games will be too quiet.");
ImGui::EndTable();
}
#ifndef __EMSCRIPTEN__
ImGui::SeparatorText("Experimental");
ImGui::Checkbox("Native Windows", &native_windows);
DoToolTip(u8"Use real windows instead of 'fake' windows\nthat are stuck inside the main window.");
#endif
ImGui::SeparatorText("Keyboard Input");
static bool sorted_scancodes_done;
static std::array<SDL_Scancode, SDL_SCANCODE_COUNT> sorted_scancodes; // TODO: `SDL_SCANCODE_COUNT` is an internal macro, so use something standard!
if (!sorted_scancodes_done)
{
sorted_scancodes_done = true;
for (auto scancode = std::begin(sorted_scancodes); scancode != std::end(sorted_scancodes); ++scancode)
*scancode = static_cast<SDL_Scancode>(std::distance(std::begin(sorted_scancodes), scancode));
SDL_qsort(&sorted_scancodes, sorted_scancodes.size(), sizeof(sorted_scancodes[0]),
[](const void* const a, const void* const b)
{
const SDL_Scancode* const binding_1 = static_cast<const SDL_Scancode*>(a);
const SDL_Scancode* const binding_2 = static_cast<const SDL_Scancode*>(b);
return Frontend::keyboard_bindings[*binding_1] - Frontend::keyboard_bindings[*binding_2];
}
);
}
static SDL_Scancode selected_scancode;
static const std::array<const char*, INPUT_BINDING__TOTAL> binding_names = {
"None",
"Control Pad Up",
"Control Pad Down",
"Control Pad Left",
"Control Pad Right",
"Control Pad A",
"Control Pad B",
"Control Pad C",
"Control Pad X",
"Control Pad Y",
"Control Pad Z",
"Control Pad Start",
"Control Pad Mode",
"Pause",
"Reset",
"Fast-Forward",
#ifdef CLOWNMDEMU_FRONTEND_REWINDING
"Rewind",
#endif
"Quick Save State",
"Quick Load State",
"Toggle Fullscreen",
"Toggle Control Pad"
};
if (ImGui::BeginTable("Keyboard Input Options", 2))
{
ImGui::TableNextColumn();
if (ImGui::RadioButton("Control Pad #1", Frontend::keyboard_input.bound_joypad == 0))
Frontend::keyboard_input.bound_joypad = 0;
DoToolTip(u8"Binds the keyboard to Control Pad #1.");
ImGui::TableNextColumn();
if (ImGui::RadioButton("Control Pad #2", Frontend::keyboard_input.bound_joypad == 1))
Frontend::keyboard_input.bound_joypad = 1;
DoToolTip(u8"Binds the keyboard to Control Pad #2.");
ImGui::EndTable();
}
if (ImGui::BeginTable("control pad bindings", 3, ImGuiTableFlags_Borders))
{
ImGui::TableSetupColumn("Key");
ImGui::TableSetupColumn("Action");
ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed);
ImGui::TableHeadersRow();
for (const auto &scancode : sorted_scancodes)
{
if (Frontend::keyboard_bindings[scancode] != INPUT_BINDING_NONE)
{
ImGui::TableNextColumn();
ImGui::TextUnformatted(SDL_GetScancodeName(static_cast<SDL_Scancode>(scancode)));
ImGui::TableNextColumn();
ImGui::TextUnformatted(binding_names[Frontend::keyboard_bindings[scancode]]);
ImGui::TableNextColumn();
ImGui::PushID(&scancode);
if (ImGui::Button("X"))
Frontend::keyboard_bindings[scancode] = INPUT_BINDING_NONE;
ImGui::PopID();
}
}
ImGui::EndTable();
}
if (ImGui::Button("Add Binding"))
ImGui::OpenPopup("Select Key");
static bool scroll_to_add_bindings_button;
if (scroll_to_add_bindings_button)
{
scroll_to_add_bindings_button = false;
ImGui::SetScrollHereY(1.0f);
}
const ImVec2 center = ImGui::GetMainViewport()->GetCenter();
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Select Key", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
{
bool next_menu = false;
ImGui::TextUnformatted("Press the key that you want to bind, or press the Esc key to cancel...");
ImGui::TextUnformatted("(The left Alt key cannot be bound, as it is used to access the menu bar).");
int total_keys;
const bool* const keys_pressed = SDL_GetKeyboardState(&total_keys);
for (int i = 0; i < total_keys; ++i)
{
if (keys_pressed[i] && i != SDL_GetScancodeFromKey(SDLK_LALT, nullptr))
{
ImGui::CloseCurrentPopup();
// The 'escape' key will exit the menu without binding.
if (i != SDL_GetScancodeFromKey(SDLK_ESCAPE, nullptr))
{
next_menu = true;
selected_scancode = static_cast<SDL_Scancode>(i);
}
break;
}
}
if (ImGui::Button("Cancel"))
ImGui::CloseCurrentPopup();
ImGui::EndPopup();
if (next_menu)
ImGui::OpenPopup("Select Action");
}
ImGui::SetNextWindowPos(center, ImGuiCond_Appearing, ImVec2(0.5f, 0.5f));
if (ImGui::BeginPopupModal("Select Action", nullptr, ImGuiWindowFlags_AlwaysAutoResize))
{
bool previous_menu = false;
ImGui::TextFormatted("Selected Key: {}", SDL_GetScancodeName(selected_scancode));
if (ImGui::BeginListBox("##Actions"))
{
for (unsigned int i = INPUT_BINDING_NONE + 1; i < INPUT_BINDING__TOTAL; i = i + 1)
{
if (ImGui::Selectable(binding_names[i]))
{
ImGui::CloseCurrentPopup();
Frontend::keyboard_bindings[selected_scancode] = static_cast<InputBinding>(i);
sorted_scancodes_done = false;
scroll_to_add_bindings_button = true;
}
}
ImGui::EndListBox();
}
if (ImGui::Button("Cancel"))
{
previous_menu = true;
ImGui::CloseCurrentPopup();
}
ImGui::EndPopup();
if (previous_menu)
ImGui::OpenPopup("Select Key");
}
}
public:
using Base::WindowPopup;
friend Base;
};
float Frontend::dpi_scale;
std::optional<EmulatorInstance> Frontend::emulator;
std::optional<WindowWithFramebuffer> Frontend::window;
FileUtilities Frontend::file_utilities;
unsigned int Frontend::frame_counter;
SDL::Texture Frontend::framebuffer_texture_upscaled;
unsigned int Frontend::output_width, Frontend::output_height;
unsigned int Frontend::upscale_width, Frontend::upscale_height;
bool Frontend::use_vsync;
bool Frontend::integer_screen_scaling;
bool Frontend::tall_double_resolution_mode;
bool Frontend::fast_forward_in_progress;
bool Frontend::native_windows;
Input Frontend::keyboard_input;
std::array<InputBinding, SDL_SCANCODE_COUNT > Frontend::keyboard_bindings; // TODO: `SDL_SCANCODE_COUNT` is an internal macro, so use something standard!
static std::array<InputBinding, SDL_SCANCODE_COUNT > keyboard_bindings_cached; // TODO: `SDL_SCANCODE_COUNT` is an internal macro, so use something standard!
static std::array<bool, SDL_SCANCODE_COUNT > key_pressed; // TODO: `SDL_SCANCODE_COUNT` is an internal macro, so use something standard!
static Frontend::FrameRateCallback frame_rate_callback;
static std::forward_list<ControllerInput> controller_input_list;
#ifdef FILE_PATH_SUPPORT
static std::list<RecentSoftware> recent_software_list;
#endif
static std::filesystem::path drag_and_drop_filename;
static bool emulator_has_focus; // Used for deciding when to pass inputs to the emulator.
static bool emulator_paused;
static bool emulator_frame_advance;
static bool quick_save_exists;
static EmulatorInstance::State quick_save_state;
static std::optional<DebugLogViewer> debug_log_window;
static std::optional<DebugToggles> debugging_toggles_window;
static std::optional<Disassembler> m68k_disassembler_window;
static std::optional<DebugFrontend> debug_frontend_window;
static std::optional<DebugM68k::Registers> m68k_status_window;
static std::optional<DebugM68k::Registers> mcd_m68k_status_window;
static std::optional<DebugZ80::Registers> z80_status_window;
static std::optional<DebugMemory> m68k_ram_viewer_window;
static std::optional<DebugMemory> external_ram_viewer_window;
static std::optional<DebugMemory> z80_ram_viewer_window;
static std::optional<DebugMemory> word_ram_viewer_window;
static std::optional<DebugMemory> prg_ram_viewer_window;
static std::optional<DebugMemory> wave_ram_viewer_window;
static std::optional<DebugVDP::Registers> vdp_registers_window;
static std::optional<DebugVDP::SpriteList> sprite_list_window;
static std::optional<DebugMemory> vram_viewer_window;
static std::optional<DebugMemory> cram_viewer_window;
static std::optional<DebugMemory> vsram_viewer_window;
static std::optional<DebugVDP::SpriteViewer> sprite_plane_visualiser_window;
static std::optional<DebugVDP::PlaneViewer> window_plane_visualiser_window;
static std::optional<DebugVDP::PlaneViewer> plane_a_visualiser_window;
static std::optional<DebugVDP::PlaneViewer> plane_b_visualiser_window;
static std::optional<DebugVDP::VRAMViewer> tile_visualiser_window;
static std::optional<DebugVDP::CRAMViewer> colour_visualiser_window;
static std::optional<DebugVDP::StampViewer> stamp_visualiser_window;
static std::optional<DebugVDP::StampMapViewer> stamp_map_visualiser_window;
static std::optional<DebugFM::Registers> fm_status_window;
static std::optional<DebugPSG::Registers> psg_status_window;
static std::optional<DebugPCM::Registers> pcm_status_window;
static std::optional<DebugCDC> cdc_status_window;
static std::optional<DebugCDDA> cdda_status_window;
static std::optional<DebugOther> other_status_window;
static std::optional<OptionsWindow> options_window;
static std::optional<AboutWindow> about_window;
static constexpr auto popup_windows = std::make_tuple(
&debug_log_window,
&debugging_toggles_window,
&m68k_disassembler_window,
&debug_frontend_window,
&m68k_status_window,
&mcd_m68k_status_window,
&z80_status_window,
&m68k_ram_viewer_window,
&external_ram_viewer_window,
&z80_ram_viewer_window,
&word_ram_viewer_window,
&prg_ram_viewer_window,
&wave_ram_viewer_window,
&vdp_registers_window,
&sprite_list_window,
&vram_viewer_window,
&cram_viewer_window,
&vsram_viewer_window,
&sprite_plane_visualiser_window,
&window_plane_visualiser_window,
&plane_a_visualiser_window,
&plane_b_visualiser_window,
&tile_visualiser_window,
&colour_visualiser_window,
&stamp_visualiser_window,
&stamp_map_visualiser_window,
&fm_status_window,
&psg_status_window,
&pcm_status_window,
&cdc_status_window,
&cdda_status_window,
&other_status_window,
&options_window,
&about_window
);
// Manages whether the program exits or not.
static bool quit;
// Used for tracking when to pop the emulation display out into its own little window.
static bool pop_out;
#ifndef NDEBUG
static bool dear_imgui_demo_window;
#endif
static bool emulator_on, emulator_running;
////////////////////////////
// Emulator Functionality //
////////////////////////////
static cc_bool ReadInputCallback(const cc_u8f player_id, const ClownMDEmu_Button button_id)
{
SDL_assert(player_id < 2);
cc_bool value = cc_false;
// Don't use inputs that are for Dear ImGui
if (emulator_has_focus)
{
// First, try to obtain the input from the keyboard.
if (keyboard_input.bound_joypad == player_id)
value |= keyboard_input.buttons[button_id] != 0 ? cc_true : cc_false;
}
if ((ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_NavEnableGamepad) == 0)
{
// Then, try to obtain the input from the controllers.
for (const auto &controller_input : controller_input_list)
if (controller_input.input.bound_joypad == player_id)
value |= controller_input.input.buttons[button_id] != 0 ? cc_true : cc_false;
}