36#include <imgui_stdlib.h>
41using namespace std::literals;
47 , symbolManager(manager.getReactor().getSymbolManager())
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]GFlQ#;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#";
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;
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());
91 syncDisassemblyWithPC =
true;
95static void openOrCreate(
ImGuiManager& manager, std::vector<std::unique_ptr<T>>& viewers)
98 if (
auto it =
ranges::find(viewers,
false, &T::show); it != viewers.end()) {
103 viewers.push_back(std::make_unique<T>(manager, viewers.size()));
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()));
115 auto it = hexEditors.begin();
116 auto et = hexEditors.end();
119 auto name = std::string((*it)->getDebuggableName());
123 }
while (it != et && (*it)->getDebuggableName() == name);
124 buf.appendf(
"hexEditor.%s=%d\n", name.c_str(), count);
130 bitmapViewers.clear();
132 spriteViewers.clear();
138 static constexpr std::string_view hexEditorPrefix =
"hexEditor.";
142 }
else if (name ==
"bitmapViewers") {
143 if (
auto r = StringOp::stringTo<unsigned>(value)) {
146 }
else if (name ==
"tileViewers") {
147 if (
auto r = StringOp::stringTo<unsigned>(value)) {
150 }
else if (name ==
"spriteViewers") {
151 if (
auto r = StringOp::stringTo<unsigned>(value)) {
154 }
else if (name.starts_with(hexEditorPrefix)) {
155 if (
auto r = StringOp::stringTo<unsigned>(value)) {
156 auto debuggableName = std::string(name.substr(hexEditorPrefix.size()));
158 auto index = std::distance(b, e);
159 for (
auto i :
xrange(*r)) {
160 e = hexEditors.insert(e, std::make_unique<DebuggableEditor>(
manager, debuggableName, index + i));
169 setDisassemblyScrollY = disassemblyScrollY;
174 auto createHexEditor = [&](
const std::string& name) {
177 for (
auto it = b; it != e; ++it) {
184 auto index = std::distance(b, e);
185 auto it = hexEditors.insert(e, std::make_unique<DebuggableEditor>(
manager, name, index));
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);
197 bool memoryOpen = (it != hexEditors.end()) && (*it)->open;
198 if (ImGui::MenuItem(
"Memory",
nullptr, &memoryOpen)) {
200 createHexEditor(
"memory");
202 assert(it != hexEditors.end());
208 ImGui::MenuItem(
"Symbol manager",
nullptr, &
manager.
symbols->show);
211 if (ImGui::MenuItem(
"VDP bitmap viewer ...")) {
212 openOrCreate(
manager, bitmapViewers);
214 if (ImGui::MenuItem(
"VDP tile viewer ...")) {
215 openOrCreate(
manager, tileViewers);
217 if (ImGui::MenuItem(
"VDP sprite viewer ...")) {
218 openOrCreate(
manager, spriteViewers);
220 ImGui::MenuItem(
"VDP register viewer",
nullptr, &
manager.
vdpRegs->show);
221 ImGui::MenuItem(
"Palette editor",
nullptr, &
manager.
palette->window.open);
225 auto debuggables = to_vector<std::pair<std::string, Debuggable*>>(debugger.getDebuggables());
227 for (
const auto& [name, debuggable] : debuggables) {
228 if (ImGui::Selectable(
strCat(name,
" ...").c_str())) {
229 createHexEditor(name);
239 showDisassembly =
true;
240 setDisassemblyScrollY.reset();
245 if (!motherBoard)
return;
251 drawControl(cpuInterface, *motherBoard);
252 drawDisassembly(regs, cpuInterface, debugger, *motherBoard, time);
253 drawSlots(cpuInterface, debugger);
254 drawStack(regs, cpuInterface, time);
267void ImGuiDebugger::actionStepIn(MSXCPUInterface& cpuInterface)
269 cpuInterface.doStep();
271void ImGuiDebugger::actionStepOver()
275void ImGuiDebugger::actionStepOut()
279void ImGuiDebugger::actionStepBack()
282 [&](
const TclObject&) { syncDisassemblyWithPC =
true; });
285void ImGuiDebugger::checkShortcuts(MSXCPUInterface& cpuInterface, MSXMotherBoard& motherBoard)
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)) {
296 }
else if (shortcuts.checkShortcut(DEBUGGER_STEP_OUT)) {
298 }
else if (shortcuts.checkShortcut(DEBUGGER_STEP_BACK)) {
300 }
else if (shortcuts.checkShortcut(DISASM_TOGGLE_BREAKPOINT)) {
301 actionToggleBp(motherBoard);
305void ImGuiDebugger::drawControl(MSXCPUInterface& cpuInterface, MSXMotherBoard& motherBoard)
307 if (!showControl)
return;
308 im::Window(
"Debugger tool bar", &showControl, [&]{
309 checkShortcuts(cpuInterface, motherBoard);
312 const auto* font = ImGui::GetFont();
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,
318 const auto*
g = font->FindGlyph(c);
319 maxIconSize =
max(maxIconSize,
gl::vec2{
g->X1 -
g->X0,
g->Y1 -
g->Y0});
322 auto ButtonGlyph = [&](
const char*
id, ImWchar glyph,
Shortcuts::ID sid) {
327 if (shortcut.keyChord == ImGuiKey_None)
return id;
335 if (
auto breakContinueIcon = breaked ? DEBUGGER_ICON_RUN : DEBUGGER_ICON_BREAK;
336 ButtonGlyph(
"run", breakContinueIcon, DEBUGGER_BREAK_CONTINUE)) {
337 actionBreakContinue(cpuInterface);
339 const auto& style = ImGui::GetStyle();
340 ImGui::SameLine(0.0f, 2.0f * style.ItemSpacing.x);
343 if (ButtonGlyph(
"step-in", DEBUGGER_ICON_STEP_IN, DEBUGGER_STEP_IN)) {
344 actionStepIn(cpuInterface);
348 if (ButtonGlyph(
"step-over", DEBUGGER_ICON_STEP_OVER, DEBUGGER_STEP_OVER)) {
353 if (ButtonGlyph(
"step-out", DEBUGGER_ICON_STEP_OUT, DEBUGGER_STEP_OUT)) {
358 if (ButtonGlyph(
"step-back", DEBUGGER_ICON_STEP_BACK, DEBUGGER_STEP_BACK)) {
365[[nodiscard]]
static std::pair<const MSXRom*, Debuggable*> getRomBlocks(Debugger& debugger,
const MSXDevice* device)
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");
372 return {rom, debuggable};
377 std::optional<int>
ss;
382 uint16_t addr,
bool wantSs =
true,
bool wantSeg =
true)
385 int page = addr / 0x4000;
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);
402[[nodiscard]]
static TclObject toTclExpression(
const CurrentSlot& slot)
404 std::string result =
strCat(
"[pc_in_slot ", slot.ps);
410 if (slot.seg)
strAppend(result,
' ', *slot.seg);
412 return TclObject(result);
415[[nodiscard]]
static bool addrInSlot(
416 const ParsedSlotCond& slot, MSXCPUInterface& cpuInterface, Debugger& debugger, uint16_t addr)
418 if (!slot.hasPs)
return true;
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;
439 if (!bp.addr || *bp.addr != addr)
continue;
443 bool enabled = (bp.id > 0) && bp.wantEnable;
448 result.
anyInSlot |= addrInSlot(bpSlot, cpuInterface, debugger, addr);
450 result.
anyComplex |= !bpSlot.
rest.empty() || (bp.cmd.getString() !=
"debug break");
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)
459 if (bpLine.count != 0) {
462 if (bpLine.count == 1) {
463 auto& bp = guiBps[bpLine.idx];
467 guiBps.erase(guiBps.begin() + bpLine.idx);
472 auto slot = getCurrentSlot(cpuInterface, debugger, addr);
473 addBp.emplace(addr, TclObject(
"debug break"), toTclExpression(slot),
false);
476void ImGuiDebugger::actionToggleBp(MSXMotherBoard& motherBoard)
478 auto pc = motherBoard.getCPU().getRegisters().getPC();
479 auto& cpuInterface = motherBoard.getCPUInterface();
480 auto& debugger = motherBoard.getDebugger();
483 auto bpLine = examineBpLine(pc, guiBps, cpuInterface, debugger);
485 std::optional<BreakPoint> addBp;
486 std::optional<unsigned> removeBpId;
487 toggleBp(pc, bpLine, guiBps, cpuInterface, debugger, addBp, removeBpId);
489 cpuInterface.insertBreakPoint(std::move(*addBp));
492 cpuInterface.removeBreakPoint(*removeBpId);
496void ImGuiDebugger::drawDisassembly(CPURegs& regs, MSXCPUInterface& cpuInterface, Debugger& debugger,
497 MSXMotherBoard& motherBoard, EmuTime::param time)
499 if (!showDisassembly)
return;
500 ImGui::SetNextWindowSize({340, 540}, ImGuiCond_FirstUseEver);
501 im::Window(
"Disassembly", &showDisassembly, [&]{
502 checkShortcuts(cpuInterface, motherBoard);
504 std::optional<BreakPoint> addBp;
505 std::optional<unsigned> removeBpId;
507 auto pc = regs.getPC();
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;
522 ImGui::TableSetupScrollFreeze(0, 1);
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();
530 auto textSize = ImGui::GetTextLineHeight();
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;
537 clipper.Begin(0x10000);
539 clipper.IncludeItemsByIndex(*gotoTarget, *gotoTarget + 4);
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) {
551 ImGui::SetScrollHereY(0.25f);
554 if (
bool rowAtPc = !syncDisassemblyWithPC && (addr == pc);
558 bool bpRightClick =
false;
559 if (ImGui::TableNextColumn()) {
560 auto bpLine = examineBpLine(addr16, guiBps, cpuInterface, debugger);
561 bool hasBp = bpLine.count != 0;
562 bool multi = bpLine.count > 1;
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);
570 auto colIn = calcColor(bpLine.anyEnabled, bpLine.anyInSlot);
571 auto colOut = ImGui::GetColorU32(ImGuiCol_WindowBg);
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;
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);
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;
595 if (ImGui::InvisibleButton(
"##bp-button", {-FLT_MIN, textSize})) {
596 toggleBp(addr16, bpLine, guiBps, cpuInterface, debugger, addBp, removeBpId);
598 bpRightClick = hasBp && ImGui::IsItemClicked(ImGuiMouseButton_Right);
599 if (bpRightClick) ImGui::OpenPopup(
"bp-context");
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) {
613 if (!mnemonicLabels.empty()) {
614 strAppend(output, mnemonicLabels[cycleLabelsCounter % mnemonicLabels.size()]->name);
616 appendAddrAsHex(output, a);
620 if ((addr < pc) && (pc < (addr + len))) {
623 mnemonicAddr.reset();
626 assert((1 <= len) && (len <= 3));
629 [&](
unsigned i) {
return strCat(
'#', hex_string<2>(opcodes[i])); }),
633 if (ImGui::TableNextColumn()) {
634 bool focusScrollToAddress =
false;
635 bool focusRunToAddress =
false;
638 auto pos = ImGui::GetCursorPos();
639 ImGui::Selectable(
"##row",
false,
640 ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap);
643 if (shortcuts.checkShortcut(DISASM_GOTO_ADDR)) {
644 ImGui::OpenPopup(
"disassembly-context");
645 focusScrollToAddress =
true;
647 if (shortcuts.checkShortcut(DISASM_RUN_TO_ADDR)) {
648 ImGui::OpenPopup(
"disassembly-context");
649 focusRunToAddress =
true;
651 if (!bpRightClick && ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
652 ImGui::OpenPopup(
"disassembly-context");
655 auto addrStr =
tmpStrCat(hex_string<4>(addr));
657 auto addrToolTip = [&](
const std::string& str) {
660 return strCat(
"0x", hex_string<4>(*a));
662 return std::string{};
666 if (ImGui::MenuItem(
"Scroll to PC")) {
671 ImGui::Checkbox(
"Follow PC while running", &followPC);
674 ImGui::AlignTextToFramePadding();
677 if (focusScrollToAddress) ImGui::SetKeyboardFocusHere();
678 if (ImGui::InputText(
"##goto", &gotoAddr, ImGuiInputTextFlags_EnterReturnsTrue)) {
683 addrToolTip(gotoAddr);
687 auto runTo =
strCat(
"Run to here (address 0x", addrStr,
')');
688 if (ImGui::MenuItem(runTo.c_str())) {
692 ImGui::AlignTextToFramePadding();
695 if (focusRunToAddress) ImGui::SetKeyboardFocusHere();
696 if (ImGui::InputText(
"##run", &runToAddr, ImGuiInputTextFlags_EnterReturnsTrue)) {
701 addrToolTip(runToAddr);
705 auto setPc =
strCat(
"Set PC to 0x", addrStr);
706 if (ImGui::MenuItem(setPc.c_str())) {
711 enum class Priority {
717 Priority currentPriority = MISSING_BOTH;
719 auto add = [&](
const Symbol* sym, Priority newPriority) {
720 if (newPriority < currentPriority)
return;
721 if (newPriority > currentPriority) candidates.clear();
722 currentPriority = newPriority;
723 candidates.push_back(sym->name);
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) {
731 if (symbol->slot && *symbol->slot != psss)
continue;
732 if (symbol->segment && *symbol->segment != slot.seg)
continue;
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);
739 add(symbol, MISSING_ONE);
742 ImGui::SetCursorPos(pos);
744 ImGui::TextUnformatted(candidates.empty() ? addrStr : candidates[cycleLabelsCounter % candidates.size()]);
746 if (!addrLabels.empty()) {
748 std::string tip(addrStr);
749 if (addrLabels.size() > 1) {
750 strAppend(tip,
"\nmultiple possibilities (click to cycle):\n",
755 ImGui::SetCursorPos(pos);
756 if (ImGui::InvisibleButton(
"##addrButton", {-FLT_MIN, textSize})) {
757 ++cycleLabelsCounter;
762 if (ImGui::TableNextColumn()) {
764 for (
auto i :
xrange(len)) {
765 strAppend(opcodesStr, hex_string<2>(opcodes[i]),
' ');
768 ImGui::TextUnformatted(opcodesStr.data(), opcodesStr.data() + 3 * len - 1);
772 if (ImGui::TableNextColumn()) {
773 auto pos = ImGui::GetCursorPos();
775 ImGui::TextUnformatted(mnemonic);
778 ImGui::SetCursorPos(pos);
779 if (ImGui::InvisibleButton(
"##mnemonicButton", {-FLT_MIN, textSize})) {
780 if (!mnemonicLabels.empty()) {
781 ++cycleLabelsCounter;
784 if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
785 nextGotoTarget = *mnemonicAddr;
787 if (!mnemonicLabels.empty()) {
789 auto tip =
strCat(
'#', hex_string<4>(*mnemonicAddr));
790 if (mnemonicLabels.size() > 1) {
791 strAppend(tip,
"\nmultiple possibilities (click to cycle):\n",
803 gotoTarget = nextGotoTarget;
804 if (syncDisassemblyWithPC) {
805 syncDisassemblyWithPC =
false;
807 auto itemHeight = ImGui::GetTextLineHeightWithSpacing();
808 auto winHeight = ImGui::GetWindowHeight();
809 auto lines = std::max(1,
int(winHeight / itemHeight) - 1);
813 ImGui::SetScrollY(topAddr * itemHeight);
814 }
else if (setDisassemblyScrollY) {
815 ImGui::SetScrollY(*setDisassemblyScrollY);
816 setDisassemblyScrollY.reset();
818 disassemblyScrollY = ImGui::GetScrollY();
822 cpuInterface.insertBreakPoint(std::move(*addBp));
825 cpuInterface.removeBreakPoint(*removeBpId);
830void ImGuiDebugger::drawSlots(MSXCPUInterface& cpuInterface, Debugger& debugger)
832 if (!showSlots)
return;
834 int flags = ImGuiTableFlags_BordersInnerV |
835 ImGuiTableFlags_Resizable |
836 ImGuiTableFlags_Reorderable |
837 ImGuiTableFlags_Hideable |
838 ImGuiTableFlags_ContextMenuInBody;
840 ImGui::TableSetupColumn(
"Page");
841 ImGui::TableSetupColumn(
"Address", ImGuiTableColumnFlags_DefaultHide);
842 ImGui::TableSetupColumn(
"Slot");
843 ImGui::TableSetupColumn(
"Segment");
844 ImGui::TableHeadersRow();
846 for (
auto page :
xrange(uint8_t(4))) {
847 auto addr = 0x4000 * page;
848 if (ImGui::TableNextColumn()) {
851 if (ImGui::TableNextColumn()) {
854 if (ImGui::TableNextColumn()) {
855 int ps = cpuInterface.getPrimarySlot(page);
856 if (cpuInterface.isExpanded(ps)) {
857 int ss = cpuInterface.getSecondarySlot(page);
863 if (ImGui::TableNextColumn()) {
864 const auto* device = cpuInterface.getVisibleMSXDevice(page);
867 }
else if (
auto [rom, romBlocks] = getRomBlocks(debugger, device); romBlocks) {
870 char separator =
'R';
871 for (
int offset = 0; offset < 0x4000; offset += blockSize) {
872 strAppend(text, separator, romBlocks->read(addr + offset));
888void ImGuiDebugger::drawStack(
const CPURegs& regs,
const MSXCPUInterface& cpuInterface, EmuTime::param time)
890 if (!showStack)
return;
892 auto line = ImGui::GetTextLineHeightWithSpacing();
893 ImGui::SetNextWindowSize(ImVec2(0.0f, 12 * line), ImGuiCond_FirstUseEver);
895 auto sp = regs.getSP();
897 int flags = ImGuiTableFlags_ScrollY |
898 ImGuiTableFlags_BordersInnerV |
899 ImGuiTableFlags_Resizable |
900 ImGuiTableFlags_Reorderable |
901 ImGuiTableFlags_Hideable |
902 ImGuiTableFlags_ContextMenuInBody;
904 ImGui::TableSetupScrollFreeze(0, 1);
905 ImGui::TableSetupColumn(
"Address");
906 ImGui::TableSetupColumn(
"Offset", ImGuiTableColumnFlags_DefaultHide);
907 ImGui::TableSetupColumn(
"Value", ImGuiTableColumnFlags_NoHide);
908 ImGui::TableHeadersRow();
912 auto offset = 2 * row;
913 auto addr = sp + offset;
914 if (ImGui::TableNextColumn()) {
917 if (ImGui::TableNextColumn()) {
918 ImGui::Text(
"SP+%X", offset);
920 if (ImGui::TableNextColumn()) {
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);
931void ImGuiDebugger::drawRegisters(CPURegs& regs)
933 if (!showRegisters)
return;
934 im::Window(
"CPU registers", &showRegisters, [&]{
937 const auto& style = ImGui::GetStyle();
938 auto padding = 2 * style.FramePadding.x;
940 auto edit16 = [&](std::string_view label, std::string_view high, std::string_view low,
auto getter,
auto setter) {
941 uint16_t value = getter();
943 ImGui::AlignTextToFramePadding();
946 ImGui::SetNextItemWidth(width16);
947 if (ImGui::InputScalar(
tmpStrCat(
"##", label).c_str(), ImGuiDataType_U16, &value,
nullptr,
nullptr,
"%04X")) {
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));
959 auto edit8 = [&](std::string_view label,
auto getter,
auto setter) {
960 uint8_t value = getter();
962 ImGui::AlignTextToFramePadding();
965 ImGui::SetNextItemWidth(width16);
966 if (ImGui::InputScalar(
tmpStrCat(
"##", label).c_str(), ImGuiDataType_U8, &value,
nullptr,
nullptr,
"%02X")) {
972 "Bin: ", bin_string<4>(value >> 4),
' ', bin_string<4>(value >> 0),
"\n"
973 "Dec: ", dec_string<3>(value),
'\n');
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); });
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); });
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); });
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); });
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); });
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); });
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); });
1005 ImGui::AlignTextToFramePadding();
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);
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)) {
1027void ImGuiDebugger::drawFlags(CPURegs& regs)
1029 if (!showFlags)
return;
1031 auto [sizeH1_, sizeH2_, sizeV_] = [&]{
1040 auto sizeH1 = sizeH1_;
auto sizeH2 = sizeH2_;
auto sizeV = sizeV_;
1042 auto f = regs.getF();
1044 auto draw = [&](
const char* name, uint8_t bit,
const char* val0 =
nullptr,
const char* val1 =
nullptr) {
1047 if (flagsLayout == 0) {
1050 s = (f & bit) ? val1 : val0;
1053 s =
strCat(name,
':', (f & bit) ?
'1' :
'0');
1058 s =
strCat(name,
' ', (f & bit) ?
'1' :
'0');
1060 strAppend(s,
" (", (f & bit) ? val1 : val0,
')');
1065 if (ImGui::Selectable(s.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick, sz)) {
1066 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
1071 simpleToolTip(
"double-click to toggle, right-click to configure");
1072 if (flagsLayout == 0) {
1074 ImGui::SameLine(0.0f, sizeH1.x);
1078 draw(
"S", 0x80,
" P",
" M");
1079 draw(
"Z", 0x40,
"NZ",
" Z");
1080 if (showXYFlags) draw(
"Y", 0x20);
1082 if (showXYFlags) draw(
"X", 0x08);
1083 draw(
"P", 0x04,
"PO",
"PE");
1085 draw(
"C", 0x01,
"NC",
" C");
1089 ImGui::RadioButton(
"horizontal", &flagsLayout, 0);
1090 ImGui::RadioButton(
"vertical", &flagsLayout, 1);
1092 ImGui::Checkbox(
"show undocumented", &showXYFlags);
std::string_view getDebuggableName() const
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
auto getPrimarySlot(int page) const
MSXDevice * getVisibleMSXDevice(int page)
bool isExpanded(int ps) const
auto getSecondarySlot(int page) const
EmuTime::param getCurrentTime() const
Convenience method: This is the same as getScheduler().getCurrentTime().
MSXCPUInterface & getCPUInterface()
static unsigned getBlockSize(RomType type)
const Shortcut & getShortcut(ID id) const
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....
detail::Joiner< Collection, Separator > join(Collection &&col, Separator &&sep)
auto CalcTextSize(std::string_view str)
void TextUnformatted(const std::string &str)
constexpr vecN< N, T > max(const vecN< N, T > &x, const vecN< N, T > &y)
void Table(const char *str_id, int column, ImGuiTableFlags flags, const ImVec2 &outer_size, float inner_width, std::invocable<> auto next)
void Window(const char *name, bool *p_open, ImGuiWindowFlags flags, std::invocable<> auto next)
void ID(const char *str_id, std::invocable<> auto next)
void PopupContextWindow(const char *str_id, ImGuiPopupFlags popup_flags, std::invocable<> auto next)
bool Menu(const char *label, bool enabled, std::invocable<> auto next)
void Disabled(bool b, std::invocable<> auto next)
void Group(std::invocable<> auto next)
void Font(ImFont *font, std::invocable<> auto next)
void Indent(float indent_w, std::invocable<> auto next)
void ListClipper(size_t count, int forceIndex, float lineHeight, std::invocable< int > auto next)
void Popup(const char *str_id, ImGuiWindowFlags flags, std::invocable<> auto next)
This file implemented 3 utility functions:
bool loadOnePersistent(std::string_view name, zstring_view value, C &c, const std::tuple< Elements... > &tup)
void simpleToolTip(std::string_view desc)
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.
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...
bool ButtonWithCenteredGlyph(ImWchar glyph, gl::vec2 maxGlyphSize)
TclObject makeTclList(Args &&... args)
unsigned dasm(std::span< const uint8_t > opcode, uint16_t pc, std::string &dest, function_ref< void(std::string &, uint16_t)> appendAddr)
Disassemble.
auto find(InputRange &&range, const T &value)
auto equal_range(ForwardRange &&range, const T &value, Compare comp={})
constexpr void sort(RandomAccessRange &&range)
auto lower_bound(ForwardRange &&range, const T &value, Compare comp={}, Proj proj={})
constexpr auto transform(Range &&range, UnaryOp op)
TemporaryString tmpStrCat(Ts &&... ts)
void strAppend(std::string &result, Ts &&...ts)
constexpr void repeat(T n, Op op)
Repeat the given operation 'op' 'n' times.
constexpr auto xrange(T e)