openMSX
ImGuiDebugger.cc
Go to the documentation of this file.
1#include "ImGuiDebugger.hh"
2
4#include "ImGuiBreakPoints.hh"
5#include "ImGuiCharacter.hh"
6#include "ImGuiCpp.hh"
7#include "ImGuiManager.hh"
8#include "ImGuiPalette.hh"
10#include "ImGuiSymbols.hh"
11#include "ImGuiUtils.hh"
12#include "ImGuiVdpRegs.hh"
13#include "ImGuiWatchExpr.hh"
14
15#include "CPURegs.hh"
16#include "Dasm.hh"
17#include "Debuggable.hh"
18#include "Debugger.hh"
19#include "MSXCPU.hh"
20#include "MSXCPUInterface.hh"
22#include "MSXMotherBoard.hh"
23#include "RomPlain.hh"
24#include "RomInfo.hh"
25#include "SymbolManager.hh"
26
27#include "narrow.hh"
28#include "join.hh"
29#include "ranges.hh"
30#include "stl.hh"
31#include "strCat.hh"
32#include "StringOp.hh"
33#include "view.hh"
34
35#include <imgui.h>
36#include <imgui_stdlib.h>
37
38#include <cstdint>
39#include <vector>
40
41using namespace std::literals;
42
43namespace openmsx {
44
46 : ImGuiPart(manager_)
47 , symbolManager(manager.getReactor().getSymbolManager())
48{
49}
50
52
53// TODO quick and dirty, doesn't look right yet
54// Created from a .ttf file via:
55// binary_to_compressed_c openmsx-debugger-icons.ttf myIcons > myfont.cpp
56// https://github.com/ocornut/imgui/blob/master/misc/fonts/binary_to_compressed_c.cpp
57static const char icons_compressed_base85[1815+1] =
58 "7])#######;9u/('/###W),##.f1$#Q6>##%[n42)[KU%G5)=-<NE/1aNV=BZrPSb]->>#ICi9.o`(*HGsO2(b6O3L+lQS%,5LsCC,H3AnAMeNA*&#Gb';9Cs3BMNvSN`sr1dD4Eo3R/"
59 "w/)8eXHkG2V3dW.;h^`I@cj`W)a4&>ZW%q/o6Q<Bou@GMQuuoJH`;-*0iR/Go%fr6jMWVRReK?-qmnUCC'Hv'<J]p]`;`Y5@C=GH?o6O+j#)*Mp]G&#70lQ#;0xfLQ7wj#O?DX-@1>F%"
60 "5[)<#8JFgL6SgV-xew@'uB5X7If]8QR3O%b'kS`a)%vW-iXx6*BfW>-'lB#$1fs-$R'Mk+(lB#$ckf&,peu.:J/PQ'-MYY#jFs9)*lXJgaDe8Qw(Y:v47Y&#*r[w'WQ1x'1/V:m^U4+3"
61 "7GJcM604,NJ:-##O]-x-e:k(NX@$##]jP]4ACh^X0(^fLHl/o#g]L7#N'+&#L)Qv$t@0+*I5^+4]77<.aQ9k0$g`,3$+h,3Pqji06rs-$=uG=3tq$>MOAQY>@'.`&K8P>#g>AvL9q*<#"
62 ")EEjLr^PA#m.Us-KRb'4g)Cb4%IuD#GKRL2KkRP/N7R20t3wK#-8/$jJA5U;0viS&[5AC>uIVS%3^oh24GbM(hIL>#xP%U#WE323lpd].B[A30GK_+,sX/D=dn5q&0####GBO&#.1d7$"
63 "3g1$#prgo.-<Tv->;gF4LZ>x#<)]L(/p^I*^nr?#&Y@C#cF&@'wvPJ(?nU^#pRpQ0gF*j0EPtdVN'+9%k*l)*0qi<%>=+dVKrdx0Iu(kLgk4DWX?/YGZv9dMb/q8/+fMO#W+WBOM>#`Q"
64 "xR--3bO1F*.D,G4rFuG-hwhH-AI7q.3B3I):[(cJ[pl6&]3KI%)B1se0fv]M6T3kuGSoBJNCZY#q+er?Y,8v,5cNY5*`$s$#]YV-[@i?#=@[s$G+TC4P#l8./=]s$1Pev6jlje3&-Xf-"
65 "e](jr145d3?;Rv$ZUvC%h5%fqxC$Y@8^CY$O,@H),W'Z-W-Rh27<C[Krsf;$.wg<L9br,4%.6v,>=+dVIIx:/@JJH)Z:Nu.iUkJ2tpPm:t0(Q$@OEp%Upn;%j&5QCa)iS0%5YY#IX$_]"
66 "2^x^]Zrju5Fhf-*i+YV-6'PA#(cK+*mtq;/qwkj1?f6<.0MbI),VW/23>%&4m$ie3%&AA4l'eq7+jkA#dlfj-&$+&^S8VTd.^AN-,CeM0l,'hoodPir`IofLX=$##5C.%T=UYF%^Yk*$"
67 "/)m<-]'LS-&%.20'>uu#OYU7#euQ['GPSw8P`&:)AF14+&YF&#WW`>$Mbk-$I&l-$Bp0.$eo4wLW(I3#Ys%(#U-4&#MXN1#EIc>#ik*$M0xSfLRq)$#@>[A-$Mg;-'Spr-gN<;Ni;-##"
68 "Jer=--`/,Mv_d##tg`=-5X`=-8sG<-*fG<-+C`T.3f1$#mrG<-8X`=-/#XN0Ht*A#e5>##_RFgLX%1kLF=`T.3xL$#)6xU.R,_'#RA`T.T5$C#<rG<-M2(@-xrG<-DKx>-#EfT%8nT`3"
69 "]kU.G-i@+H=DpTCuAo6D<B?hD-rI+HXhdxFL7-AFY'auGt$EJ1m,(@'un@VHdt6L#s[DYGB0-gD.X`'/4+auGpkn+H5-xF-^%/(#B_=?-kQU@-QRt7/uJ6(#Cm[*b[^es/c:@bHdN4/#"
70 "d>?L-NNei.Fn@-#afh]%mxtLF//5UCvs.>B,(rE-i<cM:^0[eF/1u'#.mk.#[5C/#-)IqLR`4rL;wXrLX.lrL1x.qLf.K-#J#`5/%$S-#c7]nLNM-X.Q####F/LMTxS&=u<7L8#";
71
72static constexpr ImWchar DEBUGGER_ICON_MIN = 0xead1;
73static constexpr ImWchar DEBUGGER_ICON_MAX = 0xeb8f;
74static constexpr ImWchar DEBUGGER_ICON_RUN = 0xead3;
75static constexpr ImWchar DEBUGGER_ICON_BREAK = 0xead1;
76static constexpr ImWchar DEBUGGER_ICON_STEP_IN = 0xead4;
77static constexpr ImWchar DEBUGGER_ICON_STEP_OUT = 0xead5;
78static constexpr ImWchar DEBUGGER_ICON_STEP_OVER = 0xead6;
79static constexpr ImWchar DEBUGGER_ICON_STEP_BACK = 0xeb8f;
80
82{
83 auto& io = ImGui::GetIO();
84 ImFontConfig icons_config; icons_config.MergeMode = true; icons_config.PixelSnapH = true;
85 static constexpr std::array<ImWchar, 3> ranges = {DEBUGGER_ICON_MIN, DEBUGGER_ICON_MAX, 0};
86 io.Fonts->AddFontFromMemoryCompressedBase85TTF(icons_compressed_base85, 20.0f, &icons_config, ranges.data());
87}
88
90{
91 syncDisassemblyWithPC = true;
92}
93
94template<typename T>
95static void openOrCreate(ImGuiManager& manager, std::vector<std::unique_ptr<T>>& viewers)
96{
97 // prefer to reuse a previously closed viewer
98 if (auto it = ranges::find(viewers, false, &T::show); it != viewers.end()) {
99 (*it)->show = true;
100 return;
101 }
102 // or create a new one
103 viewers.push_back(std::make_unique<T>(manager, viewers.size()));
104}
105
106void ImGuiDebugger::save(ImGuiTextBuffer& buf)
107{
108 savePersistent(buf, *this, persistentElements);
109
110 buf.appendf("bitmapViewers=%d\n", narrow<int>(bitmapViewers.size()));
111 buf.appendf("tileViewers=%d\n", narrow<int>(tileViewers.size()));
112 buf.appendf("spriteViewers=%d\n", narrow<int>(spriteViewers.size()));
113
114 // TODO in the future use c++23 std::chunk_by
115 auto it = hexEditors.begin();
116 auto et = hexEditors.end();
117 while (it != et) {
118 int count = 0;
119 auto name = std::string((*it)->getDebuggableName());
120 do {
121 ++it;
122 ++count;
123 } while (it != et && (*it)->getDebuggableName() == name);
124 buf.appendf("hexEditor.%s=%d\n", name.c_str(), count);
125 }
126}
127
129{
130 bitmapViewers.clear();
131 tileViewers.clear();
132 spriteViewers.clear();
133 hexEditors.clear();
134}
135
136void ImGuiDebugger::loadLine(std::string_view name, zstring_view value)
137{
138 static constexpr std::string_view hexEditorPrefix = "hexEditor.";
139
140 if (loadOnePersistent(name, value, *this, persistentElements)) {
141 // already handled
142 } else if (name == "bitmapViewers") {
143 if (auto r = StringOp::stringTo<unsigned>(value)) {
144 repeat(*r, [&] { openOrCreate(manager, bitmapViewers); });
145 }
146 } else if (name == "tileViewers") {
147 if (auto r = StringOp::stringTo<unsigned>(value)) {
148 repeat(*r, [&] { openOrCreate(manager, tileViewers); });
149 }
150 } else if (name == "spriteViewers") {
151 if (auto r = StringOp::stringTo<unsigned>(value)) {
152 repeat(*r, [&] { openOrCreate(manager, spriteViewers); });
153 }
154 } else if (name.starts_with(hexEditorPrefix)) {
155 if (auto r = StringOp::stringTo<unsigned>(value)) {
156 auto debuggableName = std::string(name.substr(hexEditorPrefix.size()));
157 auto [b, e] = ranges::equal_range(hexEditors, debuggableName, {}, &DebuggableEditor::getDebuggableName);
158 auto index = std::distance(b, e); // expected to be 0, but be robust against imgui.ini changes
159 for (auto i : xrange(*r)) {
160 e = hexEditors.insert(e, std::make_unique<DebuggableEditor>(manager, debuggableName, index + i));
161 ++e;
162 }
163 }
164 }
165}
166
168{
169 setDisassemblyScrollY = disassemblyScrollY;
170}
171
173{
174 auto createHexEditor = [&](const std::string& name) {
175 // prefer to reuse a previously closed editor
176 auto [b, e] = ranges::equal_range(hexEditors, name, {}, &DebuggableEditor::getDebuggableName);
177 for (auto it = b; it != e; ++it) {
178 if (!(*it)->open) {
179 (*it)->open = true;
180 return;
181 }
182 }
183 // or create a new one
184 auto index = std::distance(b, e);
185 auto it = hexEditors.insert(e, std::make_unique<DebuggableEditor>(manager, name, index));
186 (*it)->open = true;
187 };
188
189 im::Menu("Debugger", motherBoard != nullptr, [&]{
190 ImGui::MenuItem("Tool bar", nullptr, &showControl);
191 ImGui::MenuItem("Disassembly", nullptr, &showDisassembly);
192 ImGui::MenuItem("CPU registers", nullptr, &showRegisters);
193 ImGui::MenuItem("CPU flags", nullptr, &showFlags);
194 ImGui::MenuItem("Slots", nullptr, &showSlots);
195 ImGui::MenuItem("Stack", nullptr, &showStack);
196 auto it = ranges::lower_bound(hexEditors, "memory", {}, &DebuggableEditor::getDebuggableName);
197 bool memoryOpen = (it != hexEditors.end()) && (*it)->open;
198 if (ImGui::MenuItem("Memory", nullptr, &memoryOpen)) {
199 if (memoryOpen) {
200 createHexEditor("memory");
201 } else {
202 assert(it != hexEditors.end());
203 (*it)->open = false;
204 }
205 }
206 ImGui::Separator();
207 ImGui::MenuItem("Breakpoints", nullptr, &manager.breakPoints->show);
208 ImGui::MenuItem("Symbol manager", nullptr, &manager.symbols->show);
209 ImGui::MenuItem("Watch expression", nullptr, &manager.watchExpr->show);
210 ImGui::Separator();
211 if (ImGui::MenuItem("VDP bitmap viewer ...")) {
212 openOrCreate(manager, bitmapViewers);
213 }
214 if (ImGui::MenuItem("VDP tile viewer ...")) {
215 openOrCreate(manager, tileViewers);
216 }
217 if (ImGui::MenuItem("VDP sprite viewer ...")) {
218 openOrCreate(manager, spriteViewers);
219 }
220 ImGui::MenuItem("VDP register viewer", nullptr, &manager.vdpRegs->show);
221 ImGui::MenuItem("Palette editor", nullptr, &manager.palette->window.open);
222 ImGui::Separator();
223 im::Menu("Add hex editor", [&]{
224 const auto& debugger = motherBoard->getDebugger();
225 auto debuggables = to_vector<std::pair<std::string, Debuggable*>>(debugger.getDebuggables());
226 ranges::sort(debuggables, StringOp::caseless{}, [](const auto& p) { return p.first; }); // sort on name
227 for (const auto& [name, debuggable] : debuggables) {
228 if (ImGui::Selectable(strCat(name, " ...").c_str())) {
229 createHexEditor(name);
230 }
231 }
232 });
233 });
234}
235
236void ImGuiDebugger::setGotoTarget(uint16_t target)
237{
238 gotoTarget = target;
239 showDisassembly = true;
240 setDisassemblyScrollY.reset(); // don't restore initial scroll position
241}
242
244{
245 if (!motherBoard) return;
246
247 auto& regs = motherBoard->getCPU().getRegisters();
248 auto& cpuInterface = motherBoard->getCPUInterface();
249 auto& debugger = motherBoard->getDebugger();
250 auto time = motherBoard->getCurrentTime();
251 drawControl(cpuInterface, *motherBoard);
252 drawDisassembly(regs, cpuInterface, debugger, *motherBoard, time);
253 drawSlots(cpuInterface, debugger);
254 drawStack(regs, cpuInterface, time);
255 drawRegisters(regs);
256 drawFlags(regs);
257}
258
259void ImGuiDebugger::actionBreakContinue(MSXCPUInterface& cpuInterface)
260{
262 cpuInterface.doContinue();
263 } else {
264 cpuInterface.doBreak();
265 }
266}
267void ImGuiDebugger::actionStepIn(MSXCPUInterface& cpuInterface)
268{
269 cpuInterface.doStep();
270}
271void ImGuiDebugger::actionStepOver()
272{
273 manager.executeDelayed(TclObject("step_over"));
274}
275void ImGuiDebugger::actionStepOut()
276{
277 manager.executeDelayed(TclObject("step_out"));
278}
279void ImGuiDebugger::actionStepBack()
280{
281 manager.executeDelayed(TclObject("step_back"),
282 [&](const TclObject&) { syncDisassemblyWithPC = true; });
283}
284
285void ImGuiDebugger::checkShortcuts(MSXCPUInterface& cpuInterface, MSXMotherBoard& motherBoard)
286{
287 using enum Shortcuts::ID;
288 const auto& shortcuts = manager.getShortcuts();
289
290 if (shortcuts.checkShortcut(DEBUGGER_BREAK_CONTINUE)) {
291 actionBreakContinue(cpuInterface);
292 } else if (shortcuts.checkShortcut(DEBUGGER_STEP_IN)) {
293 actionStepIn(cpuInterface);
294 } else if (shortcuts.checkShortcut(DEBUGGER_STEP_OVER)) {
295 actionStepOver();
296 } else if (shortcuts.checkShortcut(DEBUGGER_STEP_OUT)) {
297 actionStepOut();
298 } else if (shortcuts.checkShortcut(DEBUGGER_STEP_BACK)) {
299 actionStepBack();
300 } else if (shortcuts.checkShortcut(DISASM_TOGGLE_BREAKPOINT)) {
301 actionToggleBp(motherBoard);
302 }
303}
304
305void ImGuiDebugger::drawControl(MSXCPUInterface& cpuInterface, MSXMotherBoard& motherBoard)
306{
307 if (!showControl) return;
308 im::Window("Debugger tool bar", &showControl, [&]{
309 checkShortcuts(cpuInterface, motherBoard);
310
311 gl::vec2 maxIconSize;
312 const auto* font = ImGui::GetFont();
313 for (auto c : {
314 DEBUGGER_ICON_RUN, DEBUGGER_ICON_BREAK,
315 DEBUGGER_ICON_STEP_IN, DEBUGGER_ICON_STEP_OVER,
316 DEBUGGER_ICON_STEP_OUT, DEBUGGER_ICON_STEP_BACK,
317 }) {
318 const auto* g = font->FindGlyph(c);
319 maxIconSize = max(maxIconSize, gl::vec2{g->X1 - g->X0, g->Y1 - g->Y0});
320 }
321
322 auto ButtonGlyph = [&](const char* id, ImWchar glyph, Shortcuts::ID sid) {
323 bool result = ButtonWithCenteredGlyph(glyph, maxIconSize);
324 simpleToolTip([&]() -> std::string {
325 const auto& shortcuts = manager.getShortcuts();
326 const auto& shortcut = shortcuts.getShortcut(sid);
327 if (shortcut.keyChord == ImGuiKey_None) return id;
328 return strCat(id, " (", getKeyChordName(shortcut.keyChord), ')');
329 });
330 return result;
331 };
332
333 bool breaked = MSXCPUInterface::isBreaked();
334 using enum Shortcuts::ID;
335 if (auto breakContinueIcon = breaked ? DEBUGGER_ICON_RUN : DEBUGGER_ICON_BREAK;
336 ButtonGlyph("run", breakContinueIcon, DEBUGGER_BREAK_CONTINUE)) {
337 actionBreakContinue(cpuInterface);
338 }
339 const auto& style = ImGui::GetStyle();
340 ImGui::SameLine(0.0f, 2.0f * style.ItemSpacing.x);
341
342 im::Disabled(!breaked, [&]{
343 if (ButtonGlyph("step-in", DEBUGGER_ICON_STEP_IN, DEBUGGER_STEP_IN)) {
344 actionStepIn(cpuInterface);
345 }
346 ImGui::SameLine();
347
348 if (ButtonGlyph("step-over", DEBUGGER_ICON_STEP_OVER, DEBUGGER_STEP_OVER)) {
349 actionStepOver();
350 }
351 ImGui::SameLine();
352
353 if (ButtonGlyph("step-out", DEBUGGER_ICON_STEP_OUT, DEBUGGER_STEP_OUT)) {
354 actionStepOut();
355 }
356 ImGui::SameLine();
357
358 if (ButtonGlyph("step-back", DEBUGGER_ICON_STEP_BACK, DEBUGGER_STEP_BACK)) {
359 actionStepBack();
360 }
361 });
362 });
363}
364
365[[nodiscard]] static std::pair<const MSXRom*, Debuggable*> getRomBlocks(Debugger& debugger, const MSXDevice* device)
366{
367 Debuggable* debuggable = nullptr;
368 const auto* rom = dynamic_cast<const MSXRom*>(device);
369 if (rom && !dynamic_cast<const RomPlain*>(rom)) {
370 debuggable = debugger.findDebuggable(rom->getName() + " romblocks");
371 }
372 return {rom, debuggable};
373}
374
376 int ps;
377 std::optional<int> ss;
378 std::optional<int> seg;
379};
380[[nodiscard]] static CurrentSlot getCurrentSlot(
381 MSXCPUInterface& cpuInterface, Debugger& debugger,
382 uint16_t addr, bool wantSs = true, bool wantSeg = true)
383{
384 CurrentSlot result;
385 int page = addr / 0x4000;
386 result.ps = cpuInterface.getPrimarySlot(page);
387
388 if (wantSs && cpuInterface.isExpanded(result.ps)) {
389 result.ss = cpuInterface.getSecondarySlot(page);
390 }
391 if (wantSeg) {
392 const auto* device = cpuInterface.getVisibleMSXDevice(page);
393 if (const auto* mapper = dynamic_cast<const MSXMemoryMapperBase*>(device)) {
394 result.seg = mapper->getSelectedSegment(narrow<uint8_t>(page));
395 } else if (auto [_, romBlocks] = getRomBlocks(debugger, device); romBlocks) {
396 result.seg = romBlocks->read(addr);
397 }
398 }
399 return result;
400}
401
402[[nodiscard]] static TclObject toTclExpression(const CurrentSlot& slot)
403{
404 std::string result = strCat("[pc_in_slot ", slot.ps);
405 if (slot.ss) {
406 strAppend(result, ' ', *slot.ss);
407 } else {
408 if (slot.seg) strAppend(result, " X");
409 }
410 if (slot.seg) strAppend(result, ' ', *slot.seg);
411 strAppend(result, ']');
412 return TclObject(result);
413}
414
415[[nodiscard]] static bool addrInSlot(
416 const ParsedSlotCond& slot, MSXCPUInterface& cpuInterface, Debugger& debugger, uint16_t addr)
417{
418 if (!slot.hasPs) return true; // no ps specified -> always ok
419
420 auto current = getCurrentSlot(cpuInterface, debugger, addr, slot.hasSs, slot.hasSeg);
421 if (slot.ps != current.ps) return false;
422 if (slot.hasSs && current.ss && (slot.ss != current.ss)) return false;
423 if (slot.hasSeg && current.seg && (slot.seg != current.seg)) return false;
424 return true;
425}
426
427struct BpLine {
428 int count = 0;
429 int idx = -1; // only valid when count=1
430 bool anyEnabled = false;
431 bool anyDisabled = false;
432 bool anyInSlot = false;
433 bool anyComplex = false;
434};
435static BpLine examineBpLine(uint16_t addr, std::span<const ImGuiBreakPoints::GuiItem> bps, MSXCPUInterface& cpuInterface, Debugger& debugger)
436{
437 BpLine result;
438 for (auto [i, bp] : enumerate(bps)) {
439 if (!bp.addr || *bp.addr != addr) continue;
440 ++result.count;
441 result.idx = int(i);
442
443 bool enabled = (bp.id > 0) && bp.wantEnable;
444 result.anyEnabled |= enabled;
445 result.anyDisabled |= !enabled;
446
447 ParsedSlotCond bpSlot("pc_in_slot", bp.cond.getString());
448 result.anyInSlot |= addrInSlot(bpSlot, cpuInterface, debugger, addr);
449
450 result.anyComplex |= !bpSlot.rest.empty() || (bp.cmd.getString() != "debug break");
451 }
452 return result;
453}
454
455static void toggleBp(uint16_t addr, const BpLine& bpLine, std::vector<ImGuiBreakPoints::GuiItem>& guiBps,
456 MSXCPUInterface& cpuInterface, Debugger& debugger,
457 std::optional<BreakPoint>& addBp, std::optional<unsigned>& removeBpId)
458{
459 if (bpLine.count != 0) {
460 // only allow to remove single breakpoints,
461 // others can be edited via the breakpoint viewer
462 if (bpLine.count == 1) {
463 auto& bp = guiBps[bpLine.idx];
464 if (bp.id > 0) {
465 removeBpId = bp.id; // schedule removal
466 } else {
467 guiBps.erase(guiBps.begin() + bpLine.idx);
468 }
469 }
470 } else {
471 // schedule creation of new bp
472 auto slot = getCurrentSlot(cpuInterface, debugger, addr);
473 addBp.emplace(addr, TclObject("debug break"), toTclExpression(slot), false);
474 }
475}
476void ImGuiDebugger::actionToggleBp(MSXMotherBoard& motherBoard)
477{
478 auto pc = motherBoard.getCPU().getRegisters().getPC();
479 auto& cpuInterface = motherBoard.getCPUInterface();
480 auto& debugger = motherBoard.getDebugger();
481 auto& guiBps = manager.breakPoints->getBps();
482
483 auto bpLine = examineBpLine(pc, guiBps, cpuInterface, debugger);
484
485 std::optional<BreakPoint> addBp;
486 std::optional<unsigned> removeBpId;
487 toggleBp(pc, bpLine, guiBps, cpuInterface, debugger, addBp, removeBpId);
488 if (addBp) {
489 cpuInterface.insertBreakPoint(std::move(*addBp));
490 }
491 if (removeBpId) {
492 cpuInterface.removeBreakPoint(*removeBpId);
493 }
494}
495
496void ImGuiDebugger::drawDisassembly(CPURegs& regs, MSXCPUInterface& cpuInterface, Debugger& debugger,
497 MSXMotherBoard& motherBoard, EmuTime::param time)
498{
499 if (!showDisassembly) return;
500 ImGui::SetNextWindowSize({340, 540}, ImGuiCond_FirstUseEver);
501 im::Window("Disassembly", &showDisassembly, [&]{
502 checkShortcuts(cpuInterface, motherBoard);
503
504 std::optional<BreakPoint> addBp;
505 std::optional<unsigned> removeBpId;
506
507 auto pc = regs.getPC();
508 if (followPC && !MSXCPUInterface::isBreaked()) {
509 gotoTarget = pc;
510 }
511
512 auto widthOpcode = ImGui::CalcTextSize("12 34 56 78"sv).x;
513 int flags = ImGuiTableFlags_RowBg |
514 ImGuiTableFlags_BordersV |
515 ImGuiTableFlags_BordersOuterV |
516 ImGuiTableFlags_Resizable |
517 ImGuiTableFlags_Hideable |
518 ImGuiTableFlags_Reorderable |
519 ImGuiTableFlags_ScrollY |
520 ImGuiTableFlags_ScrollX;
521 im::Table("table", 4, flags, [&]{
522 ImGui::TableSetupScrollFreeze(0, 1); // Make top row always visible
523 ImGui::TableSetupColumn("bp", ImGuiTableColumnFlags_WidthFixed);
524 ImGui::TableSetupColumn("address", ImGuiTableColumnFlags_NoHide);
525 ImGui::TableSetupColumn("opcode", ImGuiTableColumnFlags_WidthFixed, widthOpcode);
526 ImGui::TableSetupColumn("mnemonic", ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_NoHide);
527 ImGui::TableHeadersRow();
528
529 auto& guiBps = manager.breakPoints->getBps();
530 auto textSize = ImGui::GetTextLineHeight();
531
532 std::string mnemonic;
533 std::string opcodesStr;
534 std::vector<std::string_view> candidates;
535 std::array<uint8_t, 4> opcodes;
536 ImGuiListClipper clipper; // only draw the actually visible rows
537 clipper.Begin(0x10000);
538 if (gotoTarget) {
539 clipper.IncludeItemsByIndex(*gotoTarget, *gotoTarget + 4);
540 }
541 std::optional<unsigned> nextGotoTarget;
542 while (clipper.Step()) {
543 auto addr16 = instructionBoundary(cpuInterface, narrow<uint16_t>(clipper.DisplayStart), time);
544 for (int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) {
545 unsigned addr = addr16;
546 ImGui::TableNextRow();
547 if (addr >= 0x10000) continue;
548 im::ID(narrow<int>(addr), [&]{
549 if (gotoTarget && addr >= *gotoTarget) {
550 gotoTarget = {};
551 ImGui::SetScrollHereY(0.25f);
552 }
553
554 if (bool rowAtPc = !syncDisassemblyWithPC && (addr == pc);
555 rowAtPc) {
556 ImGui::TableSetBgColor(ImGuiTableBgTarget_RowBg1, getColor(imColor::YELLOW_BG));
557 }
558 bool bpRightClick = false;
559 if (ImGui::TableNextColumn()) { // bp
560 auto bpLine = examineBpLine(addr16, guiBps, cpuInterface, debugger);
561 bool hasBp = bpLine.count != 0;
562 bool multi = bpLine.count > 1;
563 if (hasBp) {
564 auto calcColor = [](bool enabled, bool inSlot) {
565 auto [r,g,b] = enabled ? std::tuple{0xE0, 0x00, 0x00}
566 : std::tuple{0x70, 0x70, 0x70};
567 auto a = inSlot ? 0xFF : 0x60;
568 return IM_COL32(r, g, b, a);
569 };
570 auto colIn = calcColor(bpLine.anyEnabled, bpLine.anyInSlot);
571 auto colOut = ImGui::GetColorU32(ImGuiCol_WindowBg);
572
573 auto* drawList = ImGui::GetWindowDrawList();
574 gl::vec2 scrn = ImGui::GetCursorScreenPos();
575 auto center = scrn + textSize * gl::vec2(multi ? 0.3f : 0.5f, 0.5f);
576 auto radiusIn = 0.4f * textSize;
577 auto radiusOut = 0.5f * textSize;
578
579 if (multi) {
580 auto center2 = center + textSize * gl::vec2(0.4f, 0.0f);
581 drawList->AddCircleFilled(center2, radiusOut, colOut);
582 auto colIn2 = calcColor(!bpLine.anyDisabled, bpLine.anyInSlot);
583 drawList->AddCircleFilled(center2, radiusIn, colIn2);
584 }
585 drawList->AddCircleFilled(center, radiusOut, colOut);
586 drawList->AddCircleFilled(center, radiusIn, colIn);
587 if (bpLine.anyComplex) {
588 auto d = 0.3f * textSize;
589 auto c = IM_COL32(0, 0, 0, 192);
590 auto t = 0.2f * textSize;
591 drawList->AddLine(center - gl::vec2(d, 0.0f), center + gl::vec2(d, 0.0f), c, t);
592 drawList->AddLine(center - gl::vec2(0.0f, d), center + gl::vec2(0.0f, d), c, t);
593 }
594 }
595 if (ImGui::InvisibleButton("##bp-button", {-FLT_MIN, textSize})) {
596 toggleBp(addr16, bpLine, guiBps, cpuInterface, debugger, addBp, removeBpId);
597 } else {
598 bpRightClick = hasBp && ImGui::IsItemClicked(ImGuiMouseButton_Right);
599 if (bpRightClick) ImGui::OpenPopup("bp-context");
600 im::Popup("bp-context", [&]{
601 manager.breakPoints->paintBpTab(cpuInterface, debugger, addr16);
602 });
603 }
604 }
605
606 mnemonic.clear();
607 std::optional<uint16_t> mnemonicAddr;
608 std::span<const Symbol* const> mnemonicLabels;
609 auto len = dasm(cpuInterface, addr16, opcodes, mnemonic, time,
610 [&](std::string& output, uint16_t a) {
611 mnemonicAddr = a;
612 mnemonicLabels = symbolManager.lookupValue(a);
613 if (!mnemonicLabels.empty()) {
614 strAppend(output, mnemonicLabels[cycleLabelsCounter % mnemonicLabels.size()]->name);
615 } else {
616 appendAddrAsHex(output, a);
617 }
618 });
619 assert(len >= 1);
620 if ((addr < pc) && (pc < (addr + len))) {
621 // pc is strictly inside current instruction,
622 // replace the just disassembled instruction with "db #..."
623 mnemonicAddr.reset();
624 mnemonicLabels = {};
625 len = pc - addr;
626 assert((1 <= len) && (len <= 3));
627 mnemonic = strCat("db ", join(
629 [&](unsigned i) { return strCat('#', hex_string<2>(opcodes[i])); }),
630 ','));
631 }
632
633 if (ImGui::TableNextColumn()) { // addr
634 bool focusScrollToAddress = false;
635 bool focusRunToAddress = false;
636
637 // do the full-row-selectable stuff in a column that cannot be hidden
638 auto pos = ImGui::GetCursorPos();
639 ImGui::Selectable("##row", false,
640 ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap);
641 using enum Shortcuts::ID;
642 auto& shortcuts = manager.getShortcuts();
643 if (shortcuts.checkShortcut(DISASM_GOTO_ADDR)) {
644 ImGui::OpenPopup("disassembly-context");
645 focusScrollToAddress = true;
646 }
647 if (shortcuts.checkShortcut(DISASM_RUN_TO_ADDR)) {
648 ImGui::OpenPopup("disassembly-context");
649 focusRunToAddress = true;
650 }
651 if (!bpRightClick && ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
652 ImGui::OpenPopup("disassembly-context");
653 }
654
655 auto addrStr = tmpStrCat(hex_string<4>(addr));
656 im::Popup("disassembly-context", [&]{
657 auto addrToolTip = [&](const std::string& str) {
658 simpleToolTip([&]{
659 if (auto a = symbolManager.parseSymbolOrValue(str)) {
660 return strCat("0x", hex_string<4>(*a));
661 }
662 return std::string{};
663 });
664 };
665
666 if (ImGui::MenuItem("Scroll to PC")) {
667 nextGotoTarget = pc;
668 }
669
670 im::Indent([&]{
671 ImGui::Checkbox("Follow PC while running", &followPC);
672 });
673
674 ImGui::AlignTextToFramePadding();
675 ImGui::TextUnformatted("Scroll to address:"sv);
676 ImGui::SameLine();
677 if (focusScrollToAddress) ImGui::SetKeyboardFocusHere();
678 if (ImGui::InputText("##goto", &gotoAddr, ImGuiInputTextFlags_EnterReturnsTrue)) {
679 if (auto a = symbolManager.parseSymbolOrValue(gotoAddr)) {
680 nextGotoTarget = *a;
681 }
682 }
683 addrToolTip(gotoAddr);
684
685 ImGui::Separator();
686
687 auto runTo = strCat("Run to here (address 0x", addrStr, ')');
688 if (ImGui::MenuItem(runTo.c_str())) {
689 manager.executeDelayed(makeTclList("run_to", addr));
690 }
691
692 ImGui::AlignTextToFramePadding();
693 ImGui::TextUnformatted("Run to address:"sv);
694 ImGui::SameLine();
695 if (focusRunToAddress) ImGui::SetKeyboardFocusHere();
696 if (ImGui::InputText("##run", &runToAddr, ImGuiInputTextFlags_EnterReturnsTrue)) {
697 if (auto a = symbolManager.parseSymbolOrValue(runToAddr)) {
698 manager.executeDelayed(makeTclList("run_to", *a));
699 }
700 }
701 addrToolTip(runToAddr);
702
703 ImGui::Separator();
704
705 auto setPc = strCat("Set PC to 0x", addrStr);
706 if (ImGui::MenuItem(setPc.c_str())) {
707 regs.setPC(addr16);
708 }
709 });
710
711 enum class Priority {
712 MISSING_BOTH = 0, // from lowest to highest
713 MISSING_ONE,
714 SLOT_AND_SEGMENT
715 };
716 using enum Priority;
717 Priority currentPriority = MISSING_BOTH;
718 candidates.clear();
719 auto add = [&](const Symbol* sym, Priority newPriority) {
720 if (newPriority < currentPriority) return; // we already have a better candidate
721 if (newPriority > currentPriority) candidates.clear(); // drop previous candidates, we found a better one
722 currentPriority = newPriority;
723 candidates.push_back(sym->name); // cycle symbols in the same priority level
724 };
725
726 auto slot = getCurrentSlot(cpuInterface, debugger, addr16);
727 auto psss = (slot.ss.value_or(0) << 2) + slot.ps;
728 auto addrLabels = symbolManager.lookupValue(addr16);
729 for (const Symbol* symbol: addrLabels) {
730 // skip symbols with any mismatch
731 if (symbol->slot && *symbol->slot != psss) continue;
732 if (symbol->segment && *symbol->segment != slot.seg) continue;
733 // the info that's present does match
734 if (symbol->slot && symbol->segment == slot.seg) {
735 add(symbol, SLOT_AND_SEGMENT);
736 } else if (!symbol->slot && !symbol->segment) {
737 add(symbol, MISSING_BOTH);
738 } else {
739 add(symbol, MISSING_ONE);
740 }
741 }
742 ImGui::SetCursorPos(pos);
744 ImGui::TextUnformatted(candidates.empty() ? addrStr : candidates[cycleLabelsCounter % candidates.size()]);
745 });
746 if (!addrLabels.empty()) {
747 simpleToolTip([&]{
748 std::string tip(addrStr);
749 if (addrLabels.size() > 1) {
750 strAppend(tip, "\nmultiple possibilities (click to cycle):\n",
751 join(view::transform(addrLabels, &Symbol::name), ' '));
752 }
753 return tip;
754 });
755 ImGui::SetCursorPos(pos);
756 if (ImGui::InvisibleButton("##addrButton", {-FLT_MIN, textSize})) {
757 ++cycleLabelsCounter;
758 }
759 }
760 }
761
762 if (ImGui::TableNextColumn()) { // opcode
763 opcodesStr.clear();
764 for (auto i : xrange(len)) {
765 strAppend(opcodesStr, hex_string<2>(opcodes[i]), ' ');
766 }
768 ImGui::TextUnformatted(opcodesStr.data(), opcodesStr.data() + 3 * len - 1);
769 });
770 }
771
772 if (ImGui::TableNextColumn()) { // mnemonic
773 auto pos = ImGui::GetCursorPos();
775 ImGui::TextUnformatted(mnemonic);
776 });
777 if (mnemonicAddr) {
778 ImGui::SetCursorPos(pos);
779 if (ImGui::InvisibleButton("##mnemonicButton", {-FLT_MIN, textSize})) {
780 if (!mnemonicLabels.empty()) {
781 ++cycleLabelsCounter;
782 }
783 }
784 if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
785 nextGotoTarget = *mnemonicAddr;
786 }
787 if (!mnemonicLabels.empty()) {
788 simpleToolTip([&]{
789 auto tip = strCat('#', hex_string<4>(*mnemonicAddr));
790 if (mnemonicLabels.size() > 1) {
791 strAppend(tip, "\nmultiple possibilities (click to cycle):\n",
792 join(view::transform(mnemonicLabels, &Symbol::name), ' '));
793 }
794 return tip;
795 });
796 }
797 }
798 }
799 addr16 += len;
800 });
801 }
802 }
803 gotoTarget = nextGotoTarget;
804 if (syncDisassemblyWithPC) {
805 syncDisassemblyWithPC = false; // only once
806
807 auto itemHeight = ImGui::GetTextLineHeightWithSpacing();
808 auto winHeight = ImGui::GetWindowHeight();
809 auto lines = std::max(1, int(winHeight / itemHeight) - 1); // approx
810
811 auto topAddr = nInstructionsBefore(cpuInterface, pc, time, narrow<int>(lines / 4) + 1);
812
813 ImGui::SetScrollY(topAddr * itemHeight);
814 } else if (setDisassemblyScrollY) {
815 ImGui::SetScrollY(*setDisassemblyScrollY);
816 setDisassemblyScrollY.reset();
817 }
818 disassemblyScrollY = ImGui::GetScrollY();
819 });
820 // only add/remove bp's after drawing (can't change list of bp's while iterating over it)
821 if (addBp) {
822 cpuInterface.insertBreakPoint(std::move(*addBp));
823 }
824 if (removeBpId) {
825 cpuInterface.removeBreakPoint(*removeBpId);
826 }
827 });
828}
829
830void ImGuiDebugger::drawSlots(MSXCPUInterface& cpuInterface, Debugger& debugger)
831{
832 if (!showSlots) return;
833 im::Window("Slots", &showSlots, [&]{
834 int flags = ImGuiTableFlags_BordersInnerV |
835 ImGuiTableFlags_Resizable |
836 ImGuiTableFlags_Reorderable |
837 ImGuiTableFlags_Hideable |
838 ImGuiTableFlags_ContextMenuInBody;
839 im::Table("table", 4, flags, [&]{
840 ImGui::TableSetupColumn("Page");
841 ImGui::TableSetupColumn("Address", ImGuiTableColumnFlags_DefaultHide);
842 ImGui::TableSetupColumn("Slot");
843 ImGui::TableSetupColumn("Segment");
844 ImGui::TableHeadersRow();
845
846 for (auto page : xrange(uint8_t(4))) {
847 auto addr = 0x4000 * page;
848 if (ImGui::TableNextColumn()) { // page
849 ImGui::StrCat(page);
850 }
851 if (ImGui::TableNextColumn()) { // address
852 ImGui::StrCat("0x", hex_string<4>(addr));
853 }
854 if (ImGui::TableNextColumn()) { // slot
855 int ps = cpuInterface.getPrimarySlot(page);
856 if (cpuInterface.isExpanded(ps)) {
857 int ss = cpuInterface.getSecondarySlot(page);
858 ImGui::StrCat(ps, '-', ss);
859 } else {
860 ImGui::StrCat(' ', ps);
861 }
862 }
863 if (ImGui::TableNextColumn()) { // segment
864 const auto* device = cpuInterface.getVisibleMSXDevice(page);
865 if (const auto* mapper = dynamic_cast<const MSXMemoryMapperBase*>(device)) {
866 ImGui::StrCat(mapper->getSelectedSegment(page));
867 } else if (auto [rom, romBlocks] = getRomBlocks(debugger, device); romBlocks) {
868 if (unsigned blockSize = RomInfo::getBlockSize(rom->getRomType())) {
869 std::string text;
870 char separator = 'R';
871 for (int offset = 0; offset < 0x4000; offset += blockSize) {
872 strAppend(text, separator, romBlocks->read(addr + offset));
873 separator = '/';
874 }
876 } else {
878 }
879 } else {
881 }
882 }
883 }
884 });
885 });
886}
887
888void ImGuiDebugger::drawStack(const CPURegs& regs, const MSXCPUInterface& cpuInterface, EmuTime::param time)
889{
890 if (!showStack) return;
891
892 auto line = ImGui::GetTextLineHeightWithSpacing();
893 ImGui::SetNextWindowSize(ImVec2(0.0f, 12 * line), ImGuiCond_FirstUseEver);
894 im::Window("stack", &showStack, [&]{
895 auto sp = regs.getSP();
896
897 int flags = ImGuiTableFlags_ScrollY |
898 ImGuiTableFlags_BordersInnerV |
899 ImGuiTableFlags_Resizable |
900 ImGuiTableFlags_Reorderable |
901 ImGuiTableFlags_Hideable |
902 ImGuiTableFlags_ContextMenuInBody;
903 im::Table("table", 3, flags, [&]{
904 ImGui::TableSetupScrollFreeze(0, 1); // Make top row always visible
905 ImGui::TableSetupColumn("Address");
906 ImGui::TableSetupColumn("Offset", ImGuiTableColumnFlags_DefaultHide);
907 ImGui::TableSetupColumn("Value", ImGuiTableColumnFlags_NoHide);
908 ImGui::TableHeadersRow();
909
911 im::ListClipper(std::min(128, (0x10000 - sp) / 2), [&](int row) {
912 auto offset = 2 * row;
913 auto addr = sp + offset;
914 if (ImGui::TableNextColumn()) { // address
915 ImGui::StrCat(hex_string<4>(addr));
916 }
917 if (ImGui::TableNextColumn()) { // offset
918 ImGui::Text("SP+%X", offset);
919 }
920 if (ImGui::TableNextColumn()) { // value
921 auto l = cpuInterface.peekMem(narrow_cast<uint16_t>(addr + 0), time);
922 auto h = cpuInterface.peekMem(narrow_cast<uint16_t>(addr + 1), time);
923 auto value = narrow<uint16_t>(256 * h + l);
924 ImGui::StrCat(hex_string<4>(value));
925 }
926 });
927 });
928 });
929}
930
931void ImGuiDebugger::drawRegisters(CPURegs& regs)
932{
933 if (!showRegisters) return;
934 im::Window("CPU registers", &showRegisters, [&]{
936
937 const auto& style = ImGui::GetStyle();
938 auto padding = 2 * style.FramePadding.x;
939 auto width16 = ImGui::CalcTextSize("FFFF"sv).x + padding;
940 auto edit16 = [&](std::string_view label, std::string_view high, std::string_view low, auto getter, auto setter) {
941 uint16_t value = getter();
942 im::Group([&]{
943 ImGui::AlignTextToFramePadding();
945 ImGui::SameLine();
946 ImGui::SetNextItemWidth(width16);
947 if (ImGui::InputScalar(tmpStrCat("##", label).c_str(), ImGuiDataType_U16, &value, nullptr, nullptr, "%04X")) {
948 setter(value);
949 }
950 });
951 simpleToolTip([&]{
952 return strCat(
953 "Bin: ", bin_string<4>(value >> 12), ' ', bin_string<4>(value >> 8), ' ',
954 bin_string<4>(value >> 4), ' ', bin_string<4>(value >> 0), "\n"
955 "Dec: ", dec_string<5>(value), '\n',
956 high, ": ", dec_string<3>(value >> 8), " ", low, ": ", dec_string<3>(value & 0xff));
957 });
958 };
959 auto edit8 = [&](std::string_view label, auto getter, auto setter) {
960 uint8_t value = getter();
961 im::Group([&]{
962 ImGui::AlignTextToFramePadding();
964 ImGui::SameLine();
965 ImGui::SetNextItemWidth(width16);
966 if (ImGui::InputScalar(tmpStrCat("##", label).c_str(), ImGuiDataType_U8, &value, nullptr, nullptr, "%02X")) {
967 setter(value);
968 }
969 });
970 simpleToolTip([&]{
971 return strCat(
972 "Bin: ", bin_string<4>(value >> 4), ' ', bin_string<4>(value >> 0), "\n"
973 "Dec: ", dec_string<3>(value), '\n');
974 });
975 };
976
977 edit16("AF", "A", "F", [&]{ return regs.getAF(); }, [&](uint16_t value) { regs.setAF(value); });
978 ImGui::SameLine(0.0f, 20.0f);
979 edit16("AF'", "A'", "F'", [&]{ return regs.getAF2(); }, [&](uint16_t value) { regs.setAF2(value); });
980
981 edit16("BC", "B", "C", [&]{ return regs.getBC(); }, [&](uint16_t value) { regs.setBC(value); });
982 ImGui::SameLine(0.0f, 20.0f);
983 edit16("BC'", "B'", "C'", [&]{ return regs.getBC2(); }, [&](uint16_t value) { regs.setBC2(value); });
984
985 edit16("DE", "D", "E", [&]{ return regs.getDE(); }, [&](uint16_t value) { regs.setDE(value); });
986 ImGui::SameLine(0.0f, 20.0f);
987 edit16("DE'", "D'", "E'", [&]{ return regs.getDE2(); }, [&](uint16_t value) { regs.setDE2(value); });
988
989 edit16("HL", "H", "L", [&]{ return regs.getHL(); }, [&](uint16_t value) { regs.setHL(value); });
990 ImGui::SameLine(0.0f, 20.0f);
991 edit16("HL'", "H'", "L'", [&]{ return regs.getHL2(); }, [&](uint16_t value) { regs.setHL2(value); });
992
993 edit16("IX", "IXh", "IXl", [&]{ return regs.getIX(); }, [&](uint16_t value) { regs.setIX(value); });
994 ImGui::SameLine(0.0f, 20.0f);
995 edit16("IY ", "IYh", "IYl", [&]{ return regs.getIY(); }, [&](uint16_t value) { regs.setIY(value); });
996
997 edit16("PC", "PCh", "PCl", [&]{ return regs.getPC(); }, [&](uint16_t value) { regs.setPC(value); });
998 ImGui::SameLine(0.0f, 20.0f);
999 edit16("SP ", "SPh", "SPl", [&]{ return regs.getSP(); }, [&](uint16_t value) { regs.setSP(value); });
1000
1001 edit8("I ", [&]{ return regs.getI(); }, [&](uint8_t value) { regs.setI(value); });
1002 ImGui::SameLine(0.0f, 20.0f);
1003 edit8("R ", [&]{ return regs.getR(); }, [&](uint8_t value) { regs.setR(value); });
1004
1005 ImGui::AlignTextToFramePadding();
1006 ImGui::TextUnformatted("IM"sv);
1007 ImGui::SameLine();
1008 ImGui::SetNextItemWidth(width16);
1009 if (uint8_t im = regs.getIM();
1010 ImGui::InputScalar("##IM", ImGuiDataType_U8, &im, nullptr, nullptr, "%d")) {
1011 if (im <= 2) regs.setIM(im);
1012 }
1013
1014 ImGui::SameLine(0.0f, 20.0f);
1015 ImGui::AlignTextToFramePadding();
1016 if (bool ei = regs.getIFF1();
1017 ImGui::Selectable(ei ? "EI" : "DI", false, ImGuiSelectableFlags_AllowDoubleClick)) {
1018 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
1019 regs.setIFF1(!ei);
1020 regs.setIFF2(!ei);
1021 }
1022 }
1023 simpleToolTip("double-click to toggle");
1024 });
1025}
1026
1027void ImGuiDebugger::drawFlags(CPURegs& regs)
1028{
1029 if (!showFlags) return;
1030 im::Window("CPU flags", &showFlags, [&]{
1031 auto [sizeH1_, sizeH2_, sizeV_] = [&]{
1033 return std::tuple{
1034 ImGui::CalcTextSize("NC"sv),
1035 ImGui::CalcTextSize("X:0"sv),
1036 ImGui::CalcTextSize("C 0 (NC)"sv)
1037 };
1038 }();
1039 // clang workaround
1040 auto sizeH1 = sizeH1_; auto sizeH2 = sizeH2_; auto sizeV = sizeV_;
1041
1042 auto f = regs.getF();
1043
1044 auto draw = [&](const char* name, uint8_t bit, const char* val0 = nullptr, const char* val1 = nullptr) {
1045 std::string s;
1046 ImVec2 sz;
1047 if (flagsLayout == 0) {
1048 // horizontal
1049 if (val0) {
1050 s = (f & bit) ? val1 : val0;
1051 sz = sizeH1;
1052 } else {
1053 s = strCat(name, ':', (f & bit) ? '1' : '0');
1054 sz = sizeH2;
1055 }
1056 } else {
1057 // vertical
1058 s = strCat(name, ' ', (f & bit) ? '1' : '0');
1059 if (val0) {
1060 strAppend(s, " (", (f & bit) ? val1 : val0, ')');
1061 }
1062 sz = sizeV;
1063 }
1065 if (ImGui::Selectable(s.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick, sz)) {
1066 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
1067 regs.setF(f ^ bit);
1068 }
1069 }
1070 });
1071 simpleToolTip("double-click to toggle, right-click to configure");
1072 if (flagsLayout == 0) {
1073 // horizontal
1074 ImGui::SameLine(0.0f, sizeH1.x);
1075 }
1076 };
1077
1078 draw("S", 0x80, " P", " M");
1079 draw("Z", 0x40, "NZ", " Z");
1080 if (showXYFlags) draw("Y", 0x20);
1081 draw("H", 0x10);
1082 if (showXYFlags) draw("X", 0x08);
1083 draw("P", 0x04, "PO", "PE");
1084 draw("N", 0x02);
1085 draw("C", 0x01, "NC", " C");
1086
1088 ImGui::TextUnformatted("Layout"sv);
1089 ImGui::RadioButton("horizontal", &flagsLayout, 0);
1090 ImGui::RadioButton("vertical", &flagsLayout, 1);
1091 ImGui::Separator();
1092 ImGui::Checkbox("show undocumented", &showXYFlags);
1093 });
1094 });
1095}
1096
1097} // namespace openmsx
uintptr_t id
int g
TclObject t
std::string_view getDebuggableName() const
void loadEnd() override
void loadStart() override
void showMenu(MSXMotherBoard *motherBoard) override
ImGuiDebugger(ImGuiManager &manager)
void loadLine(std::string_view name, zstring_view value) override
void setGotoTarget(uint16_t target)
void paint(MSXMotherBoard *motherBoard) override
void save(ImGuiTextBuffer &buf) override
std::unique_ptr< ImGuiBreakPoints > breakPoints
std::unique_ptr< ImGuiVdpRegs > vdpRegs
std::unique_ptr< ImGuiWatchExpr > watchExpr
std::unique_ptr< ImGuiPalette > palette
Shortcuts & getShortcuts()
void executeDelayed(std::function< void()> action)
std::unique_ptr< ImGuiSymbols > symbols
ImGuiManager & manager
Definition ImGuiPart.hh:30
auto getPrimarySlot(int page) const
MSXDevice * getVisibleMSXDevice(int page)
bool isExpanded(int ps) const
auto getSecondarySlot(int page) const
CPURegs & getRegisters()
Definition MSXCPU.cc:343
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
MSXCPUInterface & getCPUInterface()
static unsigned getBlockSize(RomType type)
Definition RomInfo.cc:201
const Shortcut & getShortcut(ID id) const
Definition Shortcuts.cc:91
std::span< Symbol const *const > lookupValue(uint16_t value)
std::optional< uint16_t > parseSymbolOrValue(std::string_view s) const
Like std::string_view, but with the extra guarantee that it refers to a zero-terminated string.
constexpr auto enumerate(Iterable &&iterable)
Heavily inspired by Nathan Reed's blog post: Python-Like enumerate() In C++17 http://reedbeta....
Definition enumerate.hh:28
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
Definition join.hh:60
void StrCat(Ts &&...ts)
Definition ImGuiUtils.hh:45
auto CalcTextSize(std::string_view str)
Definition ImGuiUtils.hh:39
void TextUnformatted(const std::string &str)
Definition ImGuiUtils.hh:26
vecN< 2, float > vec2
Definition gl_vec.hh:382
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
Definition gl_vec.hh:449
Definition ImGuiCpp.hh:60
void Table(const char *str_id, int column, ImGuiTableFlags flags, const ImVec2 &outer_size, float inner_width, std::invocable<> auto next)
Definition ImGuiCpp.hh:455
void Window(const char *name, bool *p_open, ImGuiWindowFlags flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:63
void ID(const char *str_id, std::invocable<> auto next)
Definition ImGuiCpp.hh:244
void PopupContextWindow(const char *str_id, ImGuiPopupFlags popup_flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:438
bool Menu(const char *label, bool enabled, std::invocable<> auto next)
Definition ImGuiCpp.hh:359
void Disabled(bool b, std::invocable<> auto next)
Definition ImGuiCpp.hh:506
void Group(std::invocable<> auto next)
Definition ImGuiCpp.hh:236
void Font(ImFont *font, std::invocable<> auto next)
Definition ImGuiCpp.hh:131
void Indent(float indent_w, std::invocable<> auto next)
Definition ImGuiCpp.hh:224
void ListClipper(size_t count, int forceIndex, float lineHeight, std::invocable< int > auto next)
Definition ImGuiCpp.hh:538
void Popup(const char *str_id, ImGuiWindowFlags flags, std::invocable<> auto next)
Definition ImGuiCpp.hh:391
This file implemented 3 utility functions:
Definition Autofire.cc:11
bool loadOnePersistent(std::string_view name, zstring_view value, C &c, const std::tuple< Elements... > &tup)
void simpleToolTip(std::string_view desc)
Definition ImGuiUtils.hh:79
void savePersistent(ImGuiTextBuffer &buf, C &c, const std::tuple< Elements... > &tup)
ImU32 getColor(imColor col)
std::string getKeyChordName(ImGuiKeyChord keyChord)
uint16_t instructionBoundary(const MSXCPUInterface &interface, uint16_t addr, EmuTime::param time)
This is only an heuristic to display instructions in a debugger disassembly view.
Definition Dasm.cc:196
uint16_t nInstructionsBefore(const MSXCPUInterface &interface, uint16_t addr, EmuTime::param time, int n)
Get the start address of the 'n'th instruction before the instruction containing the byte at the give...
Definition Dasm.cc:203
bool ButtonWithCenteredGlyph(ImWchar glyph, gl::vec2 maxGlyphSize)
TclObject makeTclList(Args &&... args)
Definition TclObject.hh:293
unsigned dasm(std::span< const uint8_t > opcode, uint16_t pc, std::string &dest, function_ref< void(std::string &, uint16_t)> appendAddr)
Disassemble.
Definition Dasm.cc:38
auto find(InputRange &&range, const T &value)
Definition ranges.hh:162
auto equal_range(ForwardRange &&range, const T &value, Compare comp={})
Definition ranges.hh:135
constexpr void sort(RandomAccessRange &&range)
Definition ranges.hh:51
auto lower_bound(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
Definition ranges.hh:117
constexpr auto transform(Range &&range, UnaryOp op)
Definition view.hh:520
std::string strCat()
Definition strCat.hh:703
TemporaryString tmpStrCat(Ts &&... ts)
Definition strCat.hh:742
void strAppend(std::string &result, Ts &&...ts)
Definition strCat.hh:752
std::optional< int > seg
std::optional< int > ss
std::string name
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
Definition xrange.hh:147
constexpr auto xrange(T e)
Definition xrange.hh:132