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;
99 auto it = hexEditors.begin();
100 auto et = hexEditors.end();
103 auto name = std::string((*it)->getDebuggableName());
107 }
while (it != et && (*it)->getDebuggableName() == name);
108 buf.appendf(
"hexEditor.%s=%d\n", name.c_str(), count);
120 static constexpr std::string_view prefix =
"hexEditor.";
124 }
else if (name.starts_with(prefix)) {
125 if (
auto r = StringOp::stringTo<unsigned>(value)) {
126 auto debuggableName = std::string(name.substr(prefix.size()));
128 auto index = std::distance(b, e);
129 for (
auto i :
xrange(*r)) {
130 e = hexEditors.insert(e, std::make_unique<DebuggableEditor>(
manager, debuggableName, index + i));
139 setDisassemblyScrollY = disassemblyScrollY;
144 auto createHexEditor = [&](
const std::string& name) {
147 for (
auto it = b; it != e; ++it) {
154 auto index = std::distance(b, e);
155 auto it = hexEditors.insert(e, std::make_unique<DebuggableEditor>(
manager, name, index));
159 im::Menu(
"Debugger", motherBoard !=
nullptr, [&]{
160 ImGui::MenuItem(
"Tool bar",
nullptr, &showControl);
161 ImGui::MenuItem(
"Disassembly",
nullptr, &showDisassembly);
162 ImGui::MenuItem(
"CPU registers",
nullptr, &showRegisters);
163 ImGui::MenuItem(
"CPU flags",
nullptr, &showFlags);
164 ImGui::MenuItem(
"Slots",
nullptr, &showSlots);
165 ImGui::MenuItem(
"Stack",
nullptr, &showStack);
167 bool memoryOpen = (it != hexEditors.end()) && (*it)->open;
168 if (ImGui::MenuItem(
"Memory",
nullptr, &memoryOpen)) {
170 createHexEditor(
"memory");
172 assert(it != hexEditors.end());
178 ImGui::MenuItem(
"Symbol manager",
nullptr, &
manager.
symbols->show);
181 ImGui::MenuItem(
"VDP bitmap viewer",
nullptr, &
manager.
bitmap->showBitmapViewer);
183 ImGui::MenuItem(
"VDP sprite viewer",
nullptr, &
manager.
sprite->show);
184 ImGui::MenuItem(
"VDP register viewer",
nullptr, &
manager.
vdpRegs->show);
185 ImGui::MenuItem(
"Palette editor",
nullptr, &
manager.
palette->window.open);
189 auto debuggables = to_vector<std::pair<std::string, Debuggable*>>(debugger.getDebuggables());
191 for (
const auto& [name, debuggable] : debuggables) {
192 if (ImGui::Selectable(
strCat(name,
" ...").c_str())) {
193 createHexEditor(name);
203 showDisassembly =
true;
204 setDisassemblyScrollY.reset();
209 if (!motherBoard)
return;
215 drawControl(cpuInterface, *motherBoard);
216 drawDisassembly(regs, cpuInterface, debugger, *motherBoard, time);
217 drawSlots(cpuInterface, debugger);
218 drawStack(regs, cpuInterface, time);
231void ImGuiDebugger::actionStepIn(MSXCPUInterface& cpuInterface)
233 cpuInterface.doStep();
235void ImGuiDebugger::actionStepOver()
239void ImGuiDebugger::actionStepOut()
243void ImGuiDebugger::actionStepBack()
246 [&](
const TclObject&) { syncDisassemblyWithPC =
true; });
249void ImGuiDebugger::checkShortcuts(MSXCPUInterface& cpuInterface, MSXMotherBoard& motherBoard)
254 if (shortcuts.checkShortcut(DEBUGGER_BREAK_CONTINUE)) {
255 actionBreakContinue(cpuInterface);
256 }
else if (shortcuts.checkShortcut(DEBUGGER_STEP_IN)) {
257 actionStepIn(cpuInterface);
258 }
else if (shortcuts.checkShortcut(DEBUGGER_STEP_OVER)) {
260 }
else if (shortcuts.checkShortcut(DEBUGGER_STEP_OUT)) {
262 }
else if (shortcuts.checkShortcut(DEBUGGER_STEP_BACK)) {
264 }
else if (shortcuts.checkShortcut(DISASM_TOGGLE_BREAKPOINT)) {
265 actionToggleBp(motherBoard);
269void ImGuiDebugger::drawControl(MSXCPUInterface& cpuInterface, MSXMotherBoard& motherBoard)
271 if (!showControl)
return;
272 im::Window(
"Debugger tool bar", &showControl, [&]{
273 checkShortcuts(cpuInterface, motherBoard);
276 const auto* font = ImGui::GetFont();
278 DEBUGGER_ICON_RUN, DEBUGGER_ICON_BREAK,
279 DEBUGGER_ICON_STEP_IN, DEBUGGER_ICON_STEP_OVER,
280 DEBUGGER_ICON_STEP_OUT, DEBUGGER_ICON_STEP_BACK,
282 const auto*
g = font->FindGlyph(c);
283 maxIconSize =
max(maxIconSize,
gl::vec2{
g->X1 -
g->X0,
g->Y1 -
g->Y0});
286 auto ButtonGlyph = [&](
const char*
id, ImWchar glyph,
Shortcuts::ID sid) {
291 if (shortcut.keyChord == ImGuiKey_None)
return id;
299 if (
auto breakContinueIcon = breaked ? DEBUGGER_ICON_RUN : DEBUGGER_ICON_BREAK;
300 ButtonGlyph(
"run", breakContinueIcon, DEBUGGER_BREAK_CONTINUE)) {
301 actionBreakContinue(cpuInterface);
303 const auto& style = ImGui::GetStyle();
304 ImGui::SameLine(0.0f, 2.0f * style.ItemSpacing.x);
307 if (ButtonGlyph(
"step-in", DEBUGGER_ICON_STEP_IN, DEBUGGER_STEP_IN)) {
308 actionStepIn(cpuInterface);
312 if (ButtonGlyph(
"step-over", DEBUGGER_ICON_STEP_OVER, DEBUGGER_STEP_OVER)) {
317 if (ButtonGlyph(
"step-out", DEBUGGER_ICON_STEP_OUT, DEBUGGER_STEP_OUT)) {
322 if (ButtonGlyph(
"step-back", DEBUGGER_ICON_STEP_BACK, DEBUGGER_STEP_BACK)) {
329[[nodiscard]]
static std::pair<const MSXRom*, Debuggable*> getRomBlocks(Debugger& debugger,
const MSXDevice* device)
331 Debuggable* debuggable =
nullptr;
332 const auto* rom =
dynamic_cast<const MSXRom*
>(device);
333 if (rom && !
dynamic_cast<const RomPlain*
>(rom)) {
334 debuggable = debugger.findDebuggable(rom->getName() +
" romblocks");
336 return {rom, debuggable};
341 std::optional<int>
ss;
346 uint16_t addr,
bool wantSs =
true,
bool wantSeg =
true)
349 int page = addr / 0x4000;
357 if (
const auto* mapper =
dynamic_cast<const MSXMemoryMapperBase*
>(device)) {
358 result.
seg = mapper->getSelectedSegment(narrow<uint8_t>(page));
359 }
else if (
auto [_, romBlocks] = getRomBlocks(debugger, device); romBlocks) {
360 result.
seg = romBlocks->read(addr);
366[[nodiscard]]
static TclObject toTclExpression(
const CurrentSlot& slot)
368 std::string result =
strCat(
"[pc_in_slot ", slot.ps);
374 if (slot.seg)
strAppend(result,
' ', *slot.seg);
376 return TclObject(result);
379[[nodiscard]]
static bool addrInSlot(
380 const ParsedSlotCond& slot, MSXCPUInterface& cpuInterface, Debugger& debugger, uint16_t addr)
382 if (!slot.hasPs)
return true;
384 auto current = getCurrentSlot(cpuInterface, debugger, addr, slot.hasSs, slot.hasSeg);
385 if (slot.ps != current.ps)
return false;
386 if (slot.hasSs && current.ss && (slot.ss != current.ss))
return false;
387 if (slot.hasSeg && current.seg && (slot.seg != current.seg))
return false;
403 if (!bp.addr || *bp.addr != addr)
continue;
407 bool enabled = (bp.id > 0) && bp.wantEnable;
412 result.
anyInSlot |= addrInSlot(bpSlot, cpuInterface, debugger, addr);
414 result.
anyComplex |= !bpSlot.
rest.empty() || (bp.cmd.getString() !=
"debug break");
419static void toggleBp(uint16_t addr,
const BpLine& bpLine, std::vector<ImGuiBreakPoints::GuiItem>& guiBps,
420 MSXCPUInterface& cpuInterface, Debugger& debugger,
421 std::optional<BreakPoint>& addBp, std::optional<unsigned>& removeBpId)
423 if (bpLine.count != 0) {
426 if (bpLine.count == 1) {
427 auto& bp = guiBps[bpLine.idx];
431 guiBps.erase(guiBps.begin() + bpLine.idx);
436 auto slot = getCurrentSlot(cpuInterface, debugger, addr);
437 addBp.emplace(addr, TclObject(
"debug break"), toTclExpression(slot),
false);
440void ImGuiDebugger::actionToggleBp(MSXMotherBoard& motherBoard)
442 auto pc = motherBoard.getCPU().getRegisters().getPC();
443 auto& cpuInterface = motherBoard.getCPUInterface();
444 auto& debugger = motherBoard.getDebugger();
447 auto bpLine = examineBpLine(pc, guiBps, cpuInterface, debugger);
449 std::optional<BreakPoint> addBp;
450 std::optional<unsigned> removeBpId;
451 toggleBp(pc, bpLine, guiBps, cpuInterface, debugger, addBp, removeBpId);
453 cpuInterface.insertBreakPoint(std::move(*addBp));
456 cpuInterface.removeBreakPoint(*removeBpId);
460void ImGuiDebugger::drawDisassembly(CPURegs& regs, MSXCPUInterface& cpuInterface, Debugger& debugger,
461 MSXMotherBoard& motherBoard, EmuTime::param time)
463 if (!showDisassembly)
return;
464 ImGui::SetNextWindowSize({340, 540}, ImGuiCond_FirstUseEver);
465 im::Window(
"Disassembly", &showDisassembly, [&]{
466 checkShortcuts(cpuInterface, motherBoard);
468 std::optional<BreakPoint> addBp;
469 std::optional<unsigned> removeBpId;
471 auto pc = regs.getPC();
477 int flags = ImGuiTableFlags_RowBg |
478 ImGuiTableFlags_BordersV |
479 ImGuiTableFlags_BordersOuterV |
480 ImGuiTableFlags_Resizable |
481 ImGuiTableFlags_Hideable |
482 ImGuiTableFlags_Reorderable |
483 ImGuiTableFlags_ScrollY |
484 ImGuiTableFlags_ScrollX;
486 ImGui::TableSetupScrollFreeze(0, 1);
487 ImGui::TableSetupColumn(
"bp", ImGuiTableColumnFlags_WidthFixed);
488 ImGui::TableSetupColumn(
"address", ImGuiTableColumnFlags_NoHide);
489 ImGui::TableSetupColumn(
"opcode", ImGuiTableColumnFlags_WidthFixed, widthOpcode);
490 ImGui::TableSetupColumn(
"mnemonic", ImGuiTableColumnFlags_WidthStretch | ImGuiTableColumnFlags_NoHide);
491 ImGui::TableHeadersRow();
494 auto textSize = ImGui::GetTextLineHeight();
496 std::string mnemonic;
497 std::string opcodesStr;
498 std::vector<std::string_view> candidates;
499 std::array<uint8_t, 4> opcodes;
500 ImGuiListClipper clipper;
501 clipper.Begin(0x10000);
503 clipper.IncludeItemsByIndex(*gotoTarget, *gotoTarget + 4);
505 std::optional<unsigned> nextGotoTarget;
506 while (clipper.Step()) {
507 auto addr16 =
instructionBoundary(cpuInterface, narrow<uint16_t>(clipper.DisplayStart), time);
508 for (
int row = clipper.DisplayStart; row < clipper.DisplayEnd; ++row) {
509 unsigned addr = addr16;
510 ImGui::TableNextRow();
511 if (addr >= 0x10000)
continue;
512 im::ID(narrow<int>(addr), [&]{
513 if (gotoTarget && addr >= *gotoTarget) {
515 ImGui::SetScrollHereY(0.25f);
518 if (
bool rowAtPc = !syncDisassemblyWithPC && (addr == pc);
522 bool bpRightClick =
false;
523 if (ImGui::TableNextColumn()) {
524 auto bpLine = examineBpLine(addr16, guiBps, cpuInterface, debugger);
525 bool hasBp = bpLine.count != 0;
526 bool multi = bpLine.count > 1;
528 auto calcColor = [](
bool enabled,
bool inSlot) {
529 auto [r,
g,b] = enabled ? std::tuple{0xE0, 0x00, 0x00}
530 : std::tuple{0x70, 0x70, 0x70};
531 auto a = inSlot ? 0xFF : 0x60;
532 return IM_COL32(r,
g, b, a);
534 auto colIn = calcColor(bpLine.anyEnabled, bpLine.anyInSlot);
535 auto colOut = ImGui::GetColorU32(ImGuiCol_WindowBg);
537 auto* drawList = ImGui::GetWindowDrawList();
538 gl::vec2 scrn = ImGui::GetCursorScreenPos();
539 auto center = scrn + textSize *
gl::vec2(multi ? 0.3f : 0.5f, 0.5f);
540 auto radiusIn = 0.4f * textSize;
541 auto radiusOut = 0.5f * textSize;
544 auto center2 = center + textSize *
gl::vec2(0.4f, 0.0f);
545 drawList->AddCircleFilled(center2, radiusOut, colOut);
546 auto colIn2 = calcColor(!bpLine.anyDisabled, bpLine.anyInSlot);
547 drawList->AddCircleFilled(center2, radiusIn, colIn2);
549 drawList->AddCircleFilled(center, radiusOut, colOut);
550 drawList->AddCircleFilled(center, radiusIn, colIn);
551 if (bpLine.anyComplex) {
552 auto d = 0.3f * textSize;
553 auto c = IM_COL32(0, 0, 0, 192);
554 auto t = 0.2f * textSize;
559 if (ImGui::InvisibleButton(
"##bp-button", {-FLT_MIN, textSize})) {
560 toggleBp(addr16, bpLine, guiBps, cpuInterface, debugger, addBp, removeBpId);
562 bpRightClick = hasBp && ImGui::IsItemClicked(ImGuiMouseButton_Right);
563 if (bpRightClick) ImGui::OpenPopup(
"bp-context");
571 std::optional<uint16_t> mnemonicAddr;
572 std::span<const Symbol* const> mnemonicLabels;
573 auto len =
dasm(cpuInterface, addr16, opcodes, mnemonic, time,
574 [&](std::string& output, uint16_t a) {
577 if (!mnemonicLabels.empty()) {
578 strAppend(output, mnemonicLabels[cycleLabelsCounter % mnemonicLabels.size()]->name);
580 appendAddrAsHex(output, a);
584 if ((addr < pc) && (pc < (addr + len))) {
587 mnemonicAddr.reset();
590 assert((1 <= len) && (len <= 3));
593 [&](
unsigned i) {
return strCat(
'#', hex_string<2>(opcodes[i])); }),
597 if (ImGui::TableNextColumn()) {
598 bool focusScrollToAddress =
false;
599 bool focusRunToAddress =
false;
602 auto pos = ImGui::GetCursorPos();
603 ImGui::Selectable(
"##row",
false,
604 ImGuiSelectableFlags_SpanAllColumns | ImGuiSelectableFlags_AllowOverlap);
607 if (shortcuts.checkShortcut(DISASM_GOTO_ADDR)) {
608 ImGui::OpenPopup(
"disassembly-context");
609 focusScrollToAddress =
true;
611 if (shortcuts.checkShortcut(DISASM_RUN_TO_ADDR)) {
612 ImGui::OpenPopup(
"disassembly-context");
613 focusRunToAddress =
true;
615 if (!bpRightClick && ImGui::IsItemClicked(ImGuiMouseButton_Right)) {
616 ImGui::OpenPopup(
"disassembly-context");
619 auto addrStr =
tmpStrCat(hex_string<4>(addr));
621 auto addrToolTip = [&](
const std::string& str) {
624 return strCat(
"0x", hex_string<4>(*a));
626 return std::string{};
630 if (ImGui::MenuItem(
"Scroll to PC")) {
635 ImGui::Checkbox(
"Follow PC while running", &followPC);
638 ImGui::AlignTextToFramePadding();
641 if (focusScrollToAddress) ImGui::SetKeyboardFocusHere();
642 if (ImGui::InputText(
"##goto", &gotoAddr, ImGuiInputTextFlags_EnterReturnsTrue)) {
647 addrToolTip(gotoAddr);
651 auto runTo =
strCat(
"Run to here (address 0x", addrStr,
')');
652 if (ImGui::MenuItem(runTo.c_str())) {
656 ImGui::AlignTextToFramePadding();
659 if (focusRunToAddress) ImGui::SetKeyboardFocusHere();
660 if (ImGui::InputText(
"##run", &runToAddr, ImGuiInputTextFlags_EnterReturnsTrue)) {
665 addrToolTip(runToAddr);
669 auto setPc =
strCat(
"Set PC to 0x", addrStr);
670 if (ImGui::MenuItem(setPc.c_str())) {
675 enum class Priority {
681 Priority currentPriority = MISSING_BOTH;
683 auto add = [&](
const Symbol* sym, Priority newPriority) {
684 if (newPriority < currentPriority)
return;
685 if (newPriority > currentPriority) candidates.clear();
686 currentPriority = newPriority;
687 candidates.push_back(sym->name);
690 auto slot = getCurrentSlot(cpuInterface, debugger, addr16);
691 auto psss = (slot.ss.value_or(0) << 2) + slot.ps;
692 auto addrLabels = symbolManager.
lookupValue(addr16);
693 for (
const Symbol* symbol: addrLabels) {
695 if (symbol->slot && *symbol->slot != psss)
continue;
696 if (symbol->segment && *symbol->segment != slot.seg)
continue;
698 if (symbol->slot && symbol->segment == slot.seg) {
699 add(symbol, SLOT_AND_SEGMENT);
700 }
else if (!symbol->slot && !symbol->segment) {
701 add(symbol, MISSING_BOTH);
703 add(symbol, MISSING_ONE);
706 ImGui::SetCursorPos(pos);
708 ImGui::TextUnformatted(candidates.empty() ? addrStr : candidates[cycleLabelsCounter % candidates.size()]);
710 if (!addrLabels.empty()) {
712 std::string tip(addrStr);
713 if (addrLabels.size() > 1) {
714 strAppend(tip,
"\nmultiple possibilities (click to cycle):\n",
719 ImGui::SetCursorPos(pos);
720 if (ImGui::InvisibleButton(
"##addrButton", {-FLT_MIN, textSize})) {
721 ++cycleLabelsCounter;
726 if (ImGui::TableNextColumn()) {
728 for (
auto i :
xrange(len)) {
729 strAppend(opcodesStr, hex_string<2>(opcodes[i]),
' ');
732 ImGui::TextUnformatted(opcodesStr.data(), opcodesStr.data() + 3 * len - 1);
736 if (ImGui::TableNextColumn()) {
737 auto pos = ImGui::GetCursorPos();
739 ImGui::TextUnformatted(mnemonic);
742 ImGui::SetCursorPos(pos);
743 if (ImGui::InvisibleButton(
"##mnemonicButton", {-FLT_MIN, textSize})) {
744 if (!mnemonicLabels.empty()) {
745 ++cycleLabelsCounter;
748 if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
749 nextGotoTarget = *mnemonicAddr;
751 if (!mnemonicLabels.empty()) {
753 auto tip =
strCat(
'#', hex_string<4>(*mnemonicAddr));
754 if (mnemonicLabels.size() > 1) {
755 strAppend(tip,
"\nmultiple possibilities (click to cycle):\n",
767 gotoTarget = nextGotoTarget;
768 if (syncDisassemblyWithPC) {
769 syncDisassemblyWithPC =
false;
771 auto itemHeight = ImGui::GetTextLineHeightWithSpacing();
772 auto winHeight = ImGui::GetWindowHeight();
773 auto lines = std::max(1,
int(winHeight / itemHeight) - 1);
777 ImGui::SetScrollY(topAddr * itemHeight);
778 }
else if (setDisassemblyScrollY) {
779 ImGui::SetScrollY(*setDisassemblyScrollY);
780 setDisassemblyScrollY.reset();
782 disassemblyScrollY = ImGui::GetScrollY();
786 cpuInterface.insertBreakPoint(std::move(*addBp));
789 cpuInterface.removeBreakPoint(*removeBpId);
794void ImGuiDebugger::drawSlots(MSXCPUInterface& cpuInterface, Debugger& debugger)
796 if (!showSlots)
return;
798 int flags = ImGuiTableFlags_BordersInnerV |
799 ImGuiTableFlags_Resizable |
800 ImGuiTableFlags_Reorderable |
801 ImGuiTableFlags_Hideable |
802 ImGuiTableFlags_ContextMenuInBody;
804 ImGui::TableSetupColumn(
"Page");
805 ImGui::TableSetupColumn(
"Address", ImGuiTableColumnFlags_DefaultHide);
806 ImGui::TableSetupColumn(
"Slot");
807 ImGui::TableSetupColumn(
"Segment");
808 ImGui::TableHeadersRow();
810 for (
auto page :
xrange(uint8_t(4))) {
811 auto addr = 0x4000 * page;
812 if (ImGui::TableNextColumn()) {
815 if (ImGui::TableNextColumn()) {
818 if (ImGui::TableNextColumn()) {
819 int ps = cpuInterface.getPrimarySlot(page);
820 if (cpuInterface.isExpanded(ps)) {
821 int ss = cpuInterface.getSecondarySlot(page);
827 if (ImGui::TableNextColumn()) {
828 const auto* device = cpuInterface.getVisibleMSXDevice(page);
831 }
else if (
auto [rom, romBlocks] = getRomBlocks(debugger, device); romBlocks) {
834 char separator =
'R';
835 for (
int offset = 0; offset < 0x4000; offset += blockSize) {
836 strAppend(text, separator, romBlocks->read(addr + offset));
852void ImGuiDebugger::drawStack(
const CPURegs& regs,
const MSXCPUInterface& cpuInterface, EmuTime::param time)
854 if (!showStack)
return;
856 auto line = ImGui::GetTextLineHeightWithSpacing();
857 ImGui::SetNextWindowSize(ImVec2(0.0f, 12 * line), ImGuiCond_FirstUseEver);
859 auto sp = regs.getSP();
861 int flags = ImGuiTableFlags_ScrollY |
862 ImGuiTableFlags_BordersInnerV |
863 ImGuiTableFlags_Resizable |
864 ImGuiTableFlags_Reorderable |
865 ImGuiTableFlags_Hideable |
866 ImGuiTableFlags_ContextMenuInBody;
868 ImGui::TableSetupScrollFreeze(0, 1);
869 ImGui::TableSetupColumn(
"Address");
870 ImGui::TableSetupColumn(
"Offset", ImGuiTableColumnFlags_DefaultHide);
871 ImGui::TableSetupColumn(
"Value", ImGuiTableColumnFlags_NoHide);
872 ImGui::TableHeadersRow();
876 auto offset = 2 * row;
877 auto addr = sp + offset;
878 if (ImGui::TableNextColumn()) {
881 if (ImGui::TableNextColumn()) {
882 ImGui::Text(
"SP+%X", offset);
884 if (ImGui::TableNextColumn()) {
885 auto l = cpuInterface.peekMem(narrow_cast<uint16_t>(addr + 0), time);
886 auto h = cpuInterface.peekMem(narrow_cast<uint16_t>(addr + 1), time);
887 auto value = narrow<uint16_t>(256 * h + l);
895void ImGuiDebugger::drawRegisters(CPURegs& regs)
897 if (!showRegisters)
return;
898 im::Window(
"CPU registers", &showRegisters, [&]{
901 const auto& style = ImGui::GetStyle();
902 auto padding = 2 * style.FramePadding.x;
904 auto edit16 = [&](std::string_view label, std::string_view high, std::string_view low,
auto getter,
auto setter) {
905 uint16_t value = getter();
907 ImGui::AlignTextToFramePadding();
910 ImGui::SetNextItemWidth(width16);
911 if (ImGui::InputScalar(
tmpStrCat(
"##", label).c_str(), ImGuiDataType_U16, &value,
nullptr,
nullptr,
"%04X")) {
917 "Bin: ", bin_string<4>(value >> 12),
' ', bin_string<4>(value >> 8),
' ',
918 bin_string<4>(value >> 4),
' ', bin_string<4>(value >> 0),
"\n"
919 "Dec: ", dec_string<5>(value),
'\n',
920 high,
": ", dec_string<3>(value >> 8),
" ", low,
": ", dec_string<3>(value & 0xff));
923 auto edit8 = [&](std::string_view label,
auto getter,
auto setter) {
924 uint8_t value = getter();
926 ImGui::AlignTextToFramePadding();
929 ImGui::SetNextItemWidth(width16);
930 if (ImGui::InputScalar(
tmpStrCat(
"##", label).c_str(), ImGuiDataType_U8, &value,
nullptr,
nullptr,
"%02X")) {
936 "Bin: ", bin_string<4>(value >> 4),
' ', bin_string<4>(value >> 0),
"\n"
937 "Dec: ", dec_string<3>(value),
'\n');
941 edit16(
"AF",
"A",
"F", [&]{
return regs.getAF(); }, [&](uint16_t value) { regs.setAF(value); });
942 ImGui::SameLine(0.0f, 20.0f);
943 edit16(
"AF'",
"A'",
"F'", [&]{
return regs.getAF2(); }, [&](uint16_t value) { regs.setAF2(value); });
945 edit16(
"BC",
"B",
"C", [&]{
return regs.getBC(); }, [&](uint16_t value) { regs.setBC(value); });
946 ImGui::SameLine(0.0f, 20.0f);
947 edit16(
"BC'",
"B'",
"C'", [&]{
return regs.getBC2(); }, [&](uint16_t value) { regs.setBC2(value); });
949 edit16(
"DE",
"D",
"E", [&]{
return regs.getDE(); }, [&](uint16_t value) { regs.setDE(value); });
950 ImGui::SameLine(0.0f, 20.0f);
951 edit16(
"DE'",
"D'",
"E'", [&]{
return regs.getDE2(); }, [&](uint16_t value) { regs.setDE2(value); });
953 edit16(
"HL",
"H",
"L", [&]{
return regs.getHL(); }, [&](uint16_t value) { regs.setHL(value); });
954 ImGui::SameLine(0.0f, 20.0f);
955 edit16(
"HL'",
"H'",
"L'", [&]{
return regs.getHL2(); }, [&](uint16_t value) { regs.setHL2(value); });
957 edit16(
"IX",
"IXh",
"IXl", [&]{
return regs.getIX(); }, [&](uint16_t value) { regs.setIX(value); });
958 ImGui::SameLine(0.0f, 20.0f);
959 edit16(
"IY ",
"IYh",
"IYl", [&]{
return regs.getIY(); }, [&](uint16_t value) { regs.setIY(value); });
961 edit16(
"PC",
"PCh",
"PCl", [&]{
return regs.getPC(); }, [&](uint16_t value) { regs.setPC(value); });
962 ImGui::SameLine(0.0f, 20.0f);
963 edit16(
"SP ",
"SPh",
"SPl", [&]{
return regs.getSP(); }, [&](uint16_t value) { regs.setSP(value); });
965 edit8(
"I ", [&]{
return regs.getI(); }, [&](uint8_t value) { regs.setI(value); });
966 ImGui::SameLine(0.0f, 20.0f);
967 edit8(
"R ", [&]{
return regs.getR(); }, [&](uint8_t value) { regs.setR(value); });
969 ImGui::AlignTextToFramePadding();
972 ImGui::SetNextItemWidth(width16);
973 if (uint8_t
im = regs.getIM();
974 ImGui::InputScalar(
"##IM", ImGuiDataType_U8, &
im,
nullptr,
nullptr,
"%d")) {
975 if (
im <= 2) regs.setIM(
im);
978 ImGui::SameLine(0.0f, 20.0f);
979 ImGui::AlignTextToFramePadding();
980 if (
bool ei = regs.getIFF1();
981 ImGui::Selectable(ei ?
"EI" :
"DI", false, ImGuiSelectableFlags_AllowDoubleClick)) {
982 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
991void ImGuiDebugger::drawFlags(CPURegs& regs)
993 if (!showFlags)
return;
995 auto [sizeH1_, sizeH2_, sizeV_] = [&]{
1004 auto sizeH1 = sizeH1_;
auto sizeH2 = sizeH2_;
auto sizeV = sizeV_;
1006 auto f = regs.getF();
1008 auto draw = [&](
const char* name, uint8_t bit,
const char* val0 =
nullptr,
const char* val1 =
nullptr) {
1011 if (flagsLayout == 0) {
1014 s = (f & bit) ? val1 : val0;
1017 s =
strCat(name,
':', (f & bit) ?
'1' :
'0');
1022 s =
strCat(name,
' ', (f & bit) ?
'1' :
'0');
1024 strAppend(s,
" (", (f & bit) ? val1 : val0,
')');
1029 if (ImGui::Selectable(s.c_str(), false, ImGuiSelectableFlags_AllowDoubleClick, sz)) {
1030 if (ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) {
1035 simpleToolTip(
"double-click to toggle, right-click to configure");
1036 if (flagsLayout == 0) {
1038 ImGui::SameLine(0.0f, sizeH1.x);
1042 draw(
"S", 0x80,
" P",
" M");
1043 draw(
"Z", 0x40,
"NZ",
" Z");
1044 if (showXYFlags) draw(
"Y", 0x20);
1046 if (showXYFlags) draw(
"X", 0x08);
1047 draw(
"P", 0x04,
"PO",
"PE");
1049 draw(
"C", 0x01,
"NC",
" C");
1053 ImGui::RadioButton(
"horizontal", &flagsLayout, 0);
1054 ImGui::RadioButton(
"vertical", &flagsLayout, 1);
1056 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
std::unique_ptr< ImGuiSpriteViewer > sprite
Shortcuts & getShortcuts()
std::unique_ptr< ImGuiBitmapViewer > bitmap
std::unique_ptr< ImGuiCharacter > character
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 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 auto xrange(T e)