| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#include "debug_ui.h" |
| 8 |
#include "debug_ui_panels.h" |
| 9 |
#include "debug_ui_bridge.h" |
| 10 |
#include "debug_ui_settings.h" |
| 11 |
|
| 12 |
#include "imgui.h" |
| 13 |
#include "imgui_internal.h" |
| 14 |
#include "backends/imgui_impl_sdl2.h" |
| 15 |
#include "backends/imgui_impl_sdlrenderer2.h" |
| 16 |
|
| 17 |
#include <cstdarg> |
| 18 |
#include <cstdio> |
| 19 |
#include <cstring> |
| 20 |
|
| 21 |
static SDL_Window *s_window = nullptr; |
| 22 |
static SDL_Renderer *s_renderer = nullptr; |
| 23 |
static bool s_initialized = false; |
| 24 |
static bool s_enabled = false; |
| 25 |
static bool s_build_default_layout = false; |
| 26 |
static bool s_show_demo = false; |
| 27 |
static bool s_show_settings = false; |
| 28 |
|
| 29 |
static void debug_ui_latch_goto(void); |
| 30 |
static void debug_ui_follow_stop_location(void); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
static const char *s_mono_font_candidates[] = { |
| 50 |
#if defined(_WIN32) |
| 51 |
"C:\\Windows\\Fonts\\consola.ttf", |
| 52 |
"C:\\Windows\\Fonts\\CascadiaMono.ttf", |
| 53 |
"C:\\Windows\\Fonts\\lucon.ttf", |
| 54 |
#elif defined(__APPLE__) |
| 55 |
"/System/Library/Fonts/Menlo.ttc", |
| 56 |
"/System/Library/Fonts/Monaco.ttf", |
| 57 |
#else |
| 58 |
"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf", |
| 59 |
"/usr/share/fonts/truetype/liberation/LiberationMono-Regular.ttf", |
| 60 |
"/usr/share/fonts/TTF/DejaVuSansMono.ttf", |
| 61 |
#endif |
| 62 |
}; |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
static const float s_font_sizes[] = { |
| 67 |
13.0f, |
| 68 |
9.0f, 10.0f, 11.0f, 12.0f, 14.0f, 15.0f, 16.0f, 18.0f, |
| 69 |
20.0f, 22.0f, 24.0f, 27.0f, 30.0f, 34.0f, 39.0f, |
| 70 |
}; |
| 71 |
static ImFont *s_fonts[IM_ARRAYSIZE(s_font_sizes)] = {}; |
| 72 |
static bool s_scalable_font = false; |
| 73 |
|
| 74 |
static const char * |
| 75 |
debug_ui_find_mono_font(void) |
| 76 |
{ |
| 77 |
for (int i = 0; i < IM_ARRAYSIZE(s_mono_font_candidates); i++) { |
| 78 |
if (FILE *f = fopen(s_mono_font_candidates[i], "rb")) { |
| 79 |
fclose(f); |
| 80 |
return s_mono_font_candidates[i]; |
| 81 |
} |
| 82 |
} |
| 83 |
return nullptr; |
| 84 |
} |
| 85 |
|
| 86 |
static void |
| 87 |
debug_ui_build_fonts(ImGuiIO &io) |
| 88 |
{ |
| 89 |
const char *path = debug_ui_find_mono_font(); |
| 90 |
s_scalable_font = (path != nullptr); |
| 91 |
|
| 92 |
for (int i = 0; i < IM_ARRAYSIZE(s_font_sizes); i++) { |
| 93 |
if (s_scalable_font) { |
| 94 |
ImFontConfig cfg; |
| 95 |
|
| 96 |
|
| 97 |
cfg.OversampleH = 2; |
| 98 |
cfg.OversampleV = 1; |
| 99 |
cfg.PixelSnapH = false; |
| 100 |
s_fonts[i] = io.Fonts->AddFontFromFileTTF(path, s_font_sizes[i], &cfg); |
| 101 |
} |
| 102 |
if (!s_fonts[i]) { |
| 103 |
|
| 104 |
|
| 105 |
ImFontConfig cfg; |
| 106 |
cfg.SizePixels = s_font_sizes[i]; |
| 107 |
cfg.PixelSnapH = true; |
| 108 |
s_fonts[i] = io.Fonts->AddFontDefault(&cfg); |
| 109 |
if (i == 0) { |
| 110 |
s_scalable_font = false; |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
ImFont * |
| 119 |
debug_ui_font_for_size(float size_px, float *out_size) |
| 120 |
{ |
| 121 |
int best = 0; |
| 122 |
float bestd = 1e9f; |
| 123 |
for (int i = 0; i < IM_ARRAYSIZE(s_font_sizes); i++) { |
| 124 |
const float d = s_font_sizes[i] > size_px ? s_font_sizes[i] - size_px |
| 125 |
: size_px - s_font_sizes[i]; |
| 126 |
if (d < bestd) { |
| 127 |
bestd = d; |
| 128 |
best = i; |
| 129 |
} |
| 130 |
} |
| 131 |
if (out_size) { |
| 132 |
*out_size = s_font_sizes[best]; |
| 133 |
} |
| 134 |
return s_fonts[best]; |
| 135 |
} |
| 136 |
|
| 137 |
float |
| 138 |
debug_ui_font_base_size(void) |
| 139 |
{ |
| 140 |
|
| 141 |
|
| 142 |
return s_font_sizes[0] * debug_ui_settings().ui_scale; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
static ImGuiStyle s_style_base; |
| 155 |
static bool s_style_base_valid = false; |
| 156 |
static float s_applied_ui_scale = -1.0f; |
| 157 |
|
| 158 |
static void |
| 159 |
debug_ui_apply_ui_scale(void) |
| 160 |
{ |
| 161 |
if (!s_style_base_valid) { |
| 162 |
return; |
| 163 |
} |
| 164 |
float want = debug_ui_settings().ui_scale; |
| 165 |
if (want < 0.5f) want = 0.5f; |
| 166 |
if (want > 4.0f) want = 4.0f; |
| 167 |
if (want == s_applied_ui_scale) { |
| 168 |
return; |
| 169 |
} |
| 170 |
s_applied_ui_scale = want; |
| 171 |
|
| 172 |
ImGuiStyle &style = ImGui::GetStyle(); |
| 173 |
style = s_style_base; |
| 174 |
style.ScaleAllSizes(want); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
ImGui::GetIO().FontDefault = debug_ui_font_for_size(s_font_sizes[0] * want, nullptr); |
| 179 |
} |
| 180 |
|
| 181 |
extern "C" void |
| 182 |
debug_ui_init(SDL_Window *win, SDL_Renderer *ren) |
| 183 |
{ |
| 184 |
if (s_initialized) { |
| 185 |
return; |
| 186 |
} |
| 187 |
s_window = win; |
| 188 |
s_renderer = ren; |
| 189 |
|
| 190 |
IMGUI_CHECKVERSION(); |
| 191 |
ImGui::CreateContext(); |
| 192 |
|
| 193 |
ImGuiIO &io = ImGui::GetIO(); |
| 194 |
io.ConfigFlags |= ImGuiConfigFlags_DockingEnable; |
| 195 |
io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
debug_ui_settings_register(); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
debug_ui_build_fonts(io); |
| 210 |
|
| 211 |
ImGui::StyleColorsDark(); |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
{ |
| 220 |
ImGuiStyle &st = ImGui::GetStyle(); |
| 221 |
const ImVec4 sel (0.16f, 0.48f, 0.88f, 1.00f); |
| 222 |
const ImVec4 selDim (0.17f, 0.42f, 0.72f, 1.00f); |
| 223 |
const ImVec4 idle (0.16f, 0.17f, 0.19f, 1.00f); |
| 224 |
const ImVec4 idleDim (0.12f, 0.13f, 0.14f, 1.00f); |
| 225 |
const ImVec4 hover (0.30f, 0.58f, 0.95f, 1.00f); |
| 226 |
const ImVec4 overline (1.00f, 0.72f, 0.25f, 1.00f); |
| 227 |
|
| 228 |
st.Colors[ImGuiCol_Tab] = idle; |
| 229 |
st.Colors[ImGuiCol_TabHovered] = hover; |
| 230 |
st.Colors[ImGuiCol_TabSelected] = sel; |
| 231 |
st.Colors[ImGuiCol_TabSelectedOverline] = overline; |
| 232 |
st.Colors[ImGuiCol_TabDimmed] = idleDim; |
| 233 |
st.Colors[ImGuiCol_TabDimmedSelected] = selDim; |
| 234 |
st.Colors[ImGuiCol_TabDimmedSelectedOverline] = overline; |
| 235 |
st.TabBarOverlineSize = 3.0f; |
| 236 |
st.TabRounding = 3.0f; |
| 237 |
} |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
s_style_base = ImGui::GetStyle(); |
| 242 |
s_style_base_valid = true; |
| 243 |
s_applied_ui_scale = -1.0f; |
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
s_build_default_layout = true; |
| 249 |
if (const char *ini = io.IniFilename) { |
| 250 |
if (FILE *f = fopen(ini, "rb")) { |
| 251 |
fclose(f); |
| 252 |
s_build_default_layout = false; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
ImGui_ImplSDL2_InitForSDLRenderer(win, ren); |
| 257 |
ImGui_ImplSDLRenderer2_Init(ren); |
| 258 |
|
| 259 |
s_initialized = true; |
| 260 |
s_enabled = true; |
| 261 |
} |
| 262 |
|
| 263 |
extern "C" void |
| 264 |
debug_ui_process_event(const SDL_Event *ev) |
| 265 |
{ |
| 266 |
if (!s_initialized || ev == nullptr) { |
| 267 |
return; |
| 268 |
} |
| 269 |
ImGui_ImplSDL2_ProcessEvent(ev); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static void |
| 277 |
menubar_fixed_advance(const char *widest, float start_x) |
| 278 |
{ |
| 279 |
const float w = ImGui::CalcTextSize(widest).x; |
| 280 |
ImGui::SameLine(); |
| 281 |
ImGui::SetCursorPosX(start_x + w + ImGui::GetStyle().ItemSpacing.x); |
| 282 |
} |
| 283 |
|
| 284 |
static void |
| 285 |
menubar_fixed_value(const char *widest, const char *fmt, ...) |
| 286 |
{ |
| 287 |
char buf[64] = ""; |
| 288 |
va_list args; |
| 289 |
va_start(args, fmt); |
| 290 |
vsnprintf(buf, sizeof buf, fmt, args); |
| 291 |
va_end(args); |
| 292 |
|
| 293 |
const float x = ImGui::GetCursorPosX(); |
| 294 |
ImGui::TextUnformatted(buf); |
| 295 |
menubar_fixed_advance(widest, x); |
| 296 |
} |
| 297 |
|
| 298 |
static void |
| 299 |
menubar_fixed_colored(const char *widest, ImVec4 color, const char *text) |
| 300 |
{ |
| 301 |
const float x = ImGui::GetCursorPosX(); |
| 302 |
ImGui::PushStyleColor(ImGuiCol_Text, color); |
| 303 |
ImGui::TextUnformatted(text); |
| 304 |
ImGui::PopStyleColor(); |
| 305 |
menubar_fixed_advance(widest, x); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static bool |
| 318 |
ctrl_button(const char *label, bool enabled, const char *tip) |
| 319 |
{ |
| 320 |
ImGui::BeginDisabled(!enabled); |
| 321 |
const bool clicked = ImGui::Button(label); |
| 322 |
ImGui::EndDisabled(); |
| 323 |
if (ImGui::IsItemHovered(ImGuiHoveredFlags_AllowWhenDisabled)) { |
| 324 |
ImGui::SetTooltip("%s", tip); |
| 325 |
} |
| 326 |
return clicked; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
static void |
| 334 |
debug_ui_draw_control_bar(void) |
| 335 |
{ |
| 336 |
if (!ImGui::BeginMainMenuBar()) { |
| 337 |
return; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
if (ImGui::BeginMenu("View")) { |
| 345 |
for (DebugPanel &panel : debug_ui_panels()) { |
| 346 |
ImGui::MenuItem(panel.name, nullptr, &panel.open); |
| 347 |
} |
| 348 |
ImGui::EndMenu(); |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
if (ImGui::BeginMenu("Layout")) { |
| 354 |
if (ImGui::MenuItem("Reset to Default Layout")) { |
| 355 |
for (DebugPanel &panel : debug_ui_panels()) { |
| 356 |
panel.open = true; |
| 357 |
} |
| 358 |
s_build_default_layout = true; |
| 359 |
} |
| 360 |
ImGui::EndMenu(); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
if (ImGui::BeginMenu("System")) { |
| 366 |
if (ImGui::MenuItem("Reset", "Ctrl+Shift+F5")) { |
| 367 |
smc_requested_reset = true; |
| 368 |
} |
| 369 |
ImGui::SetItemTooltip("Reset the machine, as the SMC would. Breakpoints are kept."); |
| 370 |
if (ImGui::MenuItem("Trigger IRQ")) { |
| 371 |
irq6502(); |
| 372 |
} |
| 373 |
if (ImGui::MenuItem("Trigger NMI")) { |
| 374 |
machine_nmi(); |
| 375 |
} |
| 376 |
ImGui::Separator(); |
| 377 |
|
| 378 |
|
| 379 |
ImGui::MenuItem("Settings...", nullptr, &s_show_settings); |
| 380 |
if (ImGui::IsItemHovered()) { |
| 381 |
ImGui::SetTooltip("Interrupt following, panel auto-switching, memory change\n" |
| 382 |
"highlighting and other preferences."); |
| 383 |
} |
| 384 |
ImGui::EndMenu(); |
| 385 |
} |
| 386 |
|
| 387 |
|
| 388 |
if (ImGui::BeginMenu("Help")) { |
| 389 |
|
| 390 |
|
| 391 |
if (ImGui::BeginMenu("Keyboard Shortcuts")) { |
| 392 |
static const struct { const char *key, *what; } keys[] = { |
| 393 |
{ "F5", "Continue" }, |
| 394 |
{ "Break", "Pause" }, |
| 395 |
{ "F11", "Step Into" }, |
| 396 |
{ "Shift+F11", "Step Out" }, |
| 397 |
{ "F10", "Step Over" }, |
| 398 |
{ "Ctrl+F10", "Run to cursor" }, |
| 399 |
{ "F9", "Toggle breakpoint at PC" }, |
| 400 |
{ "Ctrl+Shift+F5", "Reset machine" }, |
| 401 |
{ "Ctrl+wheel", "Zoom the panel under the cursor" }, |
| 402 |
}; |
| 403 |
if (ImGui::BeginTable("keys", 2, ImGuiTableFlags_SizingFixedFit)) { |
| 404 |
for (const auto &k : keys) { |
| 405 |
ImGui::TableNextRow(); |
| 406 |
ImGui::TableNextColumn(); |
| 407 |
ImGui::TextUnformatted(k.key); |
| 408 |
ImGui::TableNextColumn(); |
| 409 |
ImGui::TextDisabled("%s", k.what); |
| 410 |
} |
| 411 |
ImGui::EndTable(); |
| 412 |
} |
| 413 |
ImGui::EndMenu(); |
| 414 |
} |
| 415 |
ImGui::Separator(); |
| 416 |
ImGui::MenuItem("Show ImGui Demo", nullptr, &s_show_demo); |
| 417 |
ImGui::Separator(); |
| 418 |
ImGui::TextDisabled("Dear ImGui %s", IMGUI_VERSION); |
| 419 |
ImGui::EndMenu(); |
| 420 |
} |
| 421 |
|
| 422 |
ImGui::Separator(); |
| 423 |
|
| 424 |
const bool running = DEBUGIsRunning(); |
| 425 |
const bool paused = DEBUGIsPaused(); |
| 426 |
|
| 427 |
if (ctrl_button("Continue", paused, |
| 428 |
"Continue (F5)\n" |
| 429 |
"Run until the next breakpoint or pause.\n" |
| 430 |
"Only while the machine is halted.")) |
| 431 |
DEBUGContinue(); |
| 432 |
|
| 433 |
ImGui::SameLine(); |
| 434 |
if (ctrl_button("Pause", running, |
| 435 |
"Pause (Break key)\n" |
| 436 |
"Halt the machine wherever it is.\n" |
| 437 |
"Only while it is running.")) |
| 438 |
DEBUGPause(); |
| 439 |
|
| 440 |
ImGui::SameLine(); |
| 441 |
if (ctrl_button("Step Into", paused, |
| 442 |
"Step Into (F11)\n" |
| 443 |
"Execute one source line, entering any call. Without C debug\n" |
| 444 |
"info, or with \"Step by line\" off, one instruction.\n" |
| 445 |
"Only while the machine is halted.")) |
| 446 |
DEBUGStepIntoAuto(); |
| 447 |
ImGui::SameLine(); |
| 448 |
if (ctrl_button("Step Over", paused, |
| 449 |
"Step Over (F10)\n" |
| 450 |
"Execute one source line, running any call to completion.\n" |
| 451 |
"Without C debug info, or with \"Step by line\" off, one\n" |
| 452 |
"instruction. Ctrl+F10 runs to the cursor instead.\n" |
| 453 |
"Only while the machine is halted.")) |
| 454 |
DEBUGStepOverAuto(DEBUG_OWNER_UI); |
| 455 |
ImGui::SameLine(); |
| 456 |
if (ctrl_button("Step Out", paused, |
| 457 |
"Step Out (Shift+F11)\n" |
| 458 |
"Run until the current subroutine returns.\n" |
| 459 |
"Only while the machine is halted.")) |
| 460 |
DEBUGStepOut(DEBUG_OWNER_UI); |
| 461 |
|
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
ImGui::Separator(); |
| 468 |
{ |
| 469 |
const int khz = timing_get_speed_khz(); |
| 470 |
const int native = timing_native_khz(); |
| 471 |
|
| 472 |
ImGui::BeginDisabled(warp_mode); |
| 473 |
if (ImGui::SmallButton("-")) { |
| 474 |
timing_step_speed(-1); |
| 475 |
} |
| 476 |
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Slow emulation down"); |
| 477 |
ImGui::SameLine(); |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
char speed_text[48]; |
| 482 |
if (warp_mode) { |
| 483 |
snprintf(speed_text, sizeof speed_text, "warp"); |
| 484 |
} else if (khz >= 1000) { |
| 485 |
snprintf(speed_text, sizeof speed_text, "%.2fMHz", khz / 1000.0); |
| 486 |
} else { |
| 487 |
snprintf(speed_text, sizeof speed_text, "%dkHz", khz); |
| 488 |
} |
| 489 |
const bool at_native = (khz == native); |
| 490 |
const bool too_fast = (khz > native); |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
const ImVec4 col = warp_mode ? ImVec4(0.95f, 0.70f, 0.30f, 1.0f) |
| 495 |
: too_fast ? ImVec4(1.00f, 0.45f, 0.45f, 1.0f) |
| 496 |
: at_native ? ImGui::GetStyle().Colors[ImGuiCol_Text] |
| 497 |
: ImVec4(0.55f, 0.85f, 1.00f, 1.0f); |
| 498 |
menubar_fixed_colored("888.88MHz", col, speed_text); |
| 499 |
if (ImGui::IsItemHovered()) { |
| 500 |
ImGui::SetTooltip( |
| 501 |
"Target CPU clock. This machine was started at %.2f MHz (-mhz %d),\n" |
| 502 |
"which is 100%%; the current target is %d%% of that.%s\n\n" |
| 503 |
"%.0f cycles/s, roughly %.0f instructions/s (a 6502-family\n" |
| 504 |
"instruction averages about 4 cycles).\n\n" |
| 505 |
"+/- step through real hardware clocks and the rates between them.", |
| 506 |
native / 1000.0, (int)MHZ, timing_get_speed_percent(), |
| 507 |
too_fast ? "\n\nFaster than this machine's clock: timing you measure here\n" |
| 508 |
"is NOT what the real hardware would do." |
| 509 |
: "", |
| 510 |
khz * 1000.0, khz * 1000.0 / 4.0); |
| 511 |
} |
| 512 |
ImGui::SameLine(); |
| 513 |
if (ImGui::SmallButton("+")) { |
| 514 |
timing_step_speed(1); |
| 515 |
} |
| 516 |
if (ImGui::IsItemHovered()) ImGui::SetTooltip("Speed emulation up"); |
| 517 |
|
| 518 |
|
| 519 |
if (!at_native) { |
| 520 |
ImGui::SameLine(); |
| 521 |
if (ImGui::SmallButton("1x")) { |
| 522 |
timing_reset_speed(); |
| 523 |
} |
| 524 |
if (ImGui::IsItemHovered()) |
| 525 |
ImGui::SetTooltip("Back to this machine's own clock (%.2f MHz).", |
| 526 |
native / 1000.0); |
| 527 |
} |
| 528 |
ImGui::EndDisabled(); |
| 529 |
|
| 530 |
ImGui::SameLine(); |
| 531 |
if (warp_mode) { |
| 532 |
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.55f, 0.35f, 0.10f, 1.0f)); |
| 533 |
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.70f, 0.45f, 0.15f, 1.0f)); |
| 534 |
} |
| 535 |
if (ImGui::SmallButton("Warp")) { |
| 536 |
machine_toggle_warp(); |
| 537 |
} |
| 538 |
if (warp_mode) { |
| 539 |
ImGui::PopStyleColor(2); |
| 540 |
} |
| 541 |
if (ImGui::IsItemHovered()) { |
| 542 |
ImGui::SetTooltip("Run with no speed limit (currently %s)", warp_mode ? "ON" : "off"); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
ImGui::Separator(); |
| 551 |
if (running) { |
| 552 |
menubar_fixed_colored("RUNNING", ImVec4(0.40f, 0.85f, 0.40f, 1.0f), "RUNNING"); |
| 553 |
} else if (paused) { |
| 554 |
menubar_fixed_colored("RUNNING", ImVec4(0.95f, 0.70f, 0.30f, 1.0f), "PAUSED"); |
| 555 |
} else { |
| 556 |
menubar_fixed_value("RUNNING", "%s", "-"); |
| 557 |
} |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
ImGui::SameLine(); |
| 562 |
if (cpu_in_interrupt()) { |
| 563 |
char irq_text[16]; |
| 564 |
if (cpu_irq_depth() > 1) |
| 565 |
snprintf(irq_text, sizeof irq_text, "IRQ x%d", cpu_irq_depth()); |
| 566 |
else |
| 567 |
snprintf(irq_text, sizeof irq_text, "IRQ"); |
| 568 |
menubar_fixed_colored("IRQ x88", ImVec4(1.00f, 0.45f, 0.45f, 1.0f), irq_text); |
| 569 |
if (ImGui::IsItemHovered()) { |
| 570 |
ImGui::SetTooltip("Executing inside an interrupt handler (depth %d).\n" |
| 571 |
"Returns to $%04X.", |
| 572 |
cpu_irq_depth(), (unsigned)cpu_irq_return_pc()); |
| 573 |
} |
| 574 |
} else { |
| 575 |
menubar_fixed_value("IRQ x88", "%s", ""); |
| 576 |
} |
| 577 |
|
| 578 |
ImGui::Separator(); |
| 579 |
menubar_fixed_value("PC 88:8888", "PC %02X:%04X", (unsigned)regs.k, (unsigned)regs.pc); |
| 580 |
|
| 581 |
ImGui::Separator(); |
| 582 |
menubar_fixed_value("cyc 8888888888", "cyc %u", (unsigned)clockticks6502); |
| 583 |
ImGui::SameLine(); |
| 584 |
menubar_fixed_value("ins 8888888888", "ins %d", instruction_counter); |
| 585 |
|
| 586 |
ImGui::EndMainMenuBar(); |
| 587 |
} |
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
enum DebugDockSlot { DOCK_CENTER = 0, DOCK_RIGHT, DOCK_BOTTOM, DOCK_SLOT_COUNT }; |
| 594 |
|
| 595 |
struct DebugPanelPlacement { |
| 596 |
const char *window; |
| 597 |
DebugDockSlot slot; |
| 598 |
}; |
| 599 |
|
| 600 |
static const DebugPanelPlacement s_placement[] = { |
| 601 |
{"Disassembly", DOCK_CENTER}, |
| 602 |
{"Source", DOCK_CENTER}, |
| 603 |
{"CPU", DOCK_RIGHT}, |
| 604 |
{"Breakpoints", DOCK_RIGHT}, |
| 605 |
{"Symbols", DOCK_RIGHT}, |
| 606 |
{"Call Stack", DOCK_RIGHT}, |
| 607 |
{"Memory", DOCK_BOTTOM}, |
| 608 |
{"VERA", DOCK_BOTTOM}, |
| 609 |
{"PSG", DOCK_BOTTOM}, |
| 610 |
{"YM2151", DOCK_BOTTOM}, |
| 611 |
{"PCM", DOCK_BOTTOM}, |
| 612 |
{"Performance", DOCK_BOTTOM}, |
| 613 |
|
| 614 |
|
| 615 |
{"I/O", DOCK_BOTTOM}, |
| 616 |
}; |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
static const char *s_slot_anchor[DOCK_SLOT_COUNT] = { "Disassembly", "CPU", "Memory" }; |
| 623 |
|
| 624 |
|
| 625 |
static ImGuiID |
| 626 |
debug_ui_slot_node(DebugDockSlot slot) |
| 627 |
{ |
| 628 |
const char *anchor = s_slot_anchor[slot]; |
| 629 |
if (ImGuiWindow *w = ImGui::FindWindowByName(anchor)) { |
| 630 |
if (w->DockId != 0) { |
| 631 |
return w->DockId; |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
|
| 636 |
if (ImGuiWindowSettings *s = ImGui::FindWindowSettingsByID(ImHashStr(anchor))) { |
| 637 |
return s->DockId; |
| 638 |
} |
| 639 |
return 0; |
| 640 |
} |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
static ImGuiID |
| 647 |
debug_ui_window_tab_id(const char *window) |
| 648 |
{ |
| 649 |
if (ImGuiWindow *w = ImGui::FindWindowByName(window)) { |
| 650 |
return w->TabId; |
| 651 |
} |
| 652 |
return ImHashStr("#TAB", 0, ImHashStr(window)); |
| 653 |
} |
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
static void |
| 664 |
debug_ui_place_new_panel(const char *window) |
| 665 |
{ |
| 666 |
for (const DebugPanelPlacement &p : s_placement) { |
| 667 |
if (strcmp(p.window, window) == 0) { |
| 668 |
if (ImGuiID node = debug_ui_slot_node(p.slot)) { |
| 669 |
ImGui::SetNextWindowDockID(node, ImGuiCond_FirstUseEver); |
| 670 |
} |
| 671 |
return; |
| 672 |
} |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
static void |
| 687 |
debug_ui_build_default_layout(ImGuiID dockspace_id) |
| 688 |
{ |
| 689 |
ImGui::DockBuilderRemoveNode(dockspace_id); |
| 690 |
ImGui::DockBuilderAddNode(dockspace_id, ImGuiDockNodeFlags_DockSpace); |
| 691 |
ImGui::DockBuilderSetNodeSize(dockspace_id, ImGui::GetMainViewport()->WorkSize); |
| 692 |
|
| 693 |
ImGuiID center = dockspace_id; |
| 694 |
ImGuiID right = ImGui::DockBuilderSplitNode(center, ImGuiDir_Right, 0.28f, nullptr, ¢er); |
| 695 |
ImGuiID bottom = ImGui::DockBuilderSplitNode(center, ImGuiDir_Down, 0.34f, nullptr, ¢er); |
| 696 |
|
| 697 |
const ImGuiID node[DOCK_SLOT_COUNT] = { center, right, bottom }; |
| 698 |
for (const DebugPanelPlacement &p : s_placement) { |
| 699 |
ImGui::DockBuilderDockWindow(p.window, node[p.slot]); |
| 700 |
} |
| 701 |
ImGui::DockBuilderFinish(dockspace_id); |
| 702 |
|
| 703 |
|
| 704 |
const char *front[DOCK_SLOT_COUNT] = {}; |
| 705 |
for (const DebugPanelPlacement &p : s_placement) { |
| 706 |
if (front[p.slot] != nullptr) { |
| 707 |
continue; |
| 708 |
} |
| 709 |
front[p.slot] = p.window; |
| 710 |
if (ImGuiDockNode *n = ImGui::DockBuilderGetNode(node[p.slot])) { |
| 711 |
n->SelectedTabId = debug_ui_window_tab_id(p.window); |
| 712 |
} |
| 713 |
} |
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
if (front[DOCK_CENTER]) { |
| 720 |
ImGui::SetWindowFocus(front[DOCK_CENTER]); |
| 721 |
} |
| 722 |
} |
| 723 |
|
| 724 |
extern "C" void |
| 725 |
debug_ui_render(void) |
| 726 |
{ |
| 727 |
if (!s_initialized) { |
| 728 |
return; |
| 729 |
} |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
debug_ui_apply_ui_scale(); |
| 734 |
|
| 735 |
ImGui_ImplSDLRenderer2_NewFrame(); |
| 736 |
ImGui_ImplSDL2_NewFrame(); |
| 737 |
ImGui::NewFrame(); |
| 738 |
|
| 739 |
debug_ui_latch_goto(); |
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
debug_ui_follow_stop_location(); |
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
debug_ui_draw_control_bar(); |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
ImGuiID dockspace_id = ImGui::DockSpaceOverViewport(); |
| 753 |
if (s_build_default_layout) { |
| 754 |
s_build_default_layout = false; |
| 755 |
debug_ui_build_default_layout(dockspace_id); |
| 756 |
} |
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
for (DebugPanel &panel : debug_ui_panels()) { |
| 763 |
if (panel.open && panel.render) { |
| 764 |
debug_ui_place_new_panel(panel.name); |
| 765 |
panel.render(&panel.open); |
| 766 |
} |
| 767 |
} |
| 768 |
|
| 769 |
|
| 770 |
if (s_show_demo) { |
| 771 |
ImGui::ShowDemoWindow(&s_show_demo); |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
debug_ui_draw_settings_window(&s_show_settings); |
| 776 |
|
| 777 |
ImGui::Render(); |
| 778 |
|
| 779 |
ImGuiIO &io = ImGui::GetIO(); |
| 780 |
SDL_RenderSetScale(s_renderer, io.DisplayFramebufferScale.x, io.DisplayFramebufferScale.y); |
| 781 |
SDL_SetRenderDrawColor(s_renderer, 24, 24, 24, 255); |
| 782 |
SDL_RenderClear(s_renderer); |
| 783 |
ImGui_ImplSDLRenderer2_RenderDrawData(ImGui::GetDrawData(), s_renderer); |
| 784 |
SDL_RenderPresent(s_renderer); |
| 785 |
} |
| 786 |
|
| 787 |
extern "C" void |
| 788 |
debug_ui_shutdown(void) |
| 789 |
{ |
| 790 |
if (!s_initialized) { |
| 791 |
return; |
| 792 |
} |
| 793 |
ImGui_ImplSDLRenderer2_Shutdown(); |
| 794 |
ImGui_ImplSDL2_Shutdown(); |
| 795 |
ImGui::DestroyContext(); |
| 796 |
|
| 797 |
s_window = nullptr; |
| 798 |
s_renderer = nullptr; |
| 799 |
s_initialized = false; |
| 800 |
s_enabled = false; |
| 801 |
} |
| 802 |
|
| 803 |
extern "C" bool |
| 804 |
debug_ui_is_enabled(void) |
| 805 |
{ |
| 806 |
return s_enabled; |
| 807 |
} |
| 808 |
|
| 809 |
extern "C" void |
| 810 |
debug_ui_set_enabled(bool enabled) |
| 811 |
{ |
| 812 |
s_enabled = enabled; |
| 813 |
} |
| 814 |
|
| 815 |
extern "C" SDL_Renderer * |
| 816 |
debug_ui_get_renderer(void) |
| 817 |
{ |
| 818 |
return s_renderer; |
| 819 |
} |
| 820 |
|
| 821 |
extern "C" bool |
| 822 |
debug_ui_want_capture_keyboard(void) |
| 823 |
{ |
| 824 |
if (!s_initialized) { |
| 825 |
return false; |
| 826 |
} |
| 827 |
return ImGui::GetIO().WantCaptureKeyboard; |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
extern "C" bool |
| 837 |
debug_ui_want_text_input(void) |
| 838 |
{ |
| 839 |
if (!s_initialized) { |
| 840 |
return false; |
| 841 |
} |
| 842 |
return ImGui::GetIO().WantTextInput; |
| 843 |
} |
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
static uint16_t s_cursor_addr = 0; |
| 851 |
static uint8_t s_cursor_bank = 0; |
| 852 |
static bool s_cursor_valid = false; |
| 853 |
|
| 854 |
extern "C" void |
| 855 |
debug_ui_set_cursor(uint16_t addr, uint8_t bank, bool valid) |
| 856 |
{ |
| 857 |
s_cursor_addr = addr; |
| 858 |
s_cursor_bank = bank; |
| 859 |
s_cursor_valid = valid; |
| 860 |
} |
| 861 |
|
| 862 |
extern "C" bool |
| 863 |
debug_ui_run_to_cursor(void) |
| 864 |
{ |
| 865 |
if (!s_cursor_valid || !DEBUGIsPaused()) { |
| 866 |
return false; |
| 867 |
} |
| 868 |
DEBUGRunTo(s_cursor_addr, s_cursor_bank, DEBUG_OWNER_UI); |
| 869 |
return true; |
| 870 |
} |
| 871 |
|
| 872 |
extern "C" void |
| 873 |
debug_ui_toggle_breakpoint_at_pc(void) |
| 874 |
{ |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
int x16 = -1; |
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
if (regs.pc >= 0xA000 && regs.k == 0) { |
| 884 |
x16 = (regs.pc < 0xC000) ? (int)memory_get_ram_bank() |
| 885 |
: (int)memory_get_rom_bank(); |
| 886 |
} |
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
debug_bp_toggle_for((int)regs.pc, regs.k, x16, DEBUG_OWNER_UI); |
| 892 |
} |
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
static uint16_t s_goto_req_addr = 0; |
| 901 |
static uint8_t s_goto_req_bank = 0; |
| 902 |
static bool s_goto_req_pending = false; |
| 903 |
static uint16_t s_goto_addr = 0; |
| 904 |
static uint8_t s_goto_bank = 0; |
| 905 |
static bool s_goto_active = false; |
| 906 |
|
| 907 |
extern "C" void |
| 908 |
debug_ui_request_goto(uint16_t addr, uint8_t bank) |
| 909 |
{ |
| 910 |
s_goto_req_addr = addr; |
| 911 |
s_goto_req_bank = bank; |
| 912 |
s_goto_req_pending = true; |
| 913 |
} |
| 914 |
|
| 915 |
extern "C" bool |
| 916 |
debug_ui_peek_goto(uint16_t *addr, uint8_t *bank) |
| 917 |
{ |
| 918 |
if (!s_goto_active) { |
| 919 |
return false; |
| 920 |
} |
| 921 |
if (addr) *addr = s_goto_addr; |
| 922 |
if (bank) *bank = s_goto_bank; |
| 923 |
return true; |
| 924 |
} |
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
static void |
| 929 |
debug_ui_latch_goto(void) |
| 930 |
{ |
| 931 |
s_goto_active = s_goto_req_pending; |
| 932 |
if (s_goto_req_pending) { |
| 933 |
s_goto_addr = s_goto_req_addr; |
| 934 |
s_goto_bank = s_goto_req_bank; |
| 935 |
s_goto_req_pending = false; |
| 936 |
} |
| 937 |
} |
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
static void |
| 949 |
debug_ui_follow_stop_location(void) |
| 950 |
{ |
| 951 |
const DebugUiSettings &st = debug_ui_settings(); |
| 952 |
if (!st.auto_switch_disasm && !st.auto_switch_source) { |
| 953 |
return; |
| 954 |
} |
| 955 |
|
| 956 |
static bool had_stop = false; |
| 957 |
static uint16_t last_pc = 0; |
| 958 |
static uint8_t last_k = 0; |
| 959 |
|
| 960 |
if (!DEBUGIsPaused()) { |
| 961 |
had_stop = false; |
| 962 |
return; |
| 963 |
} |
| 964 |
if (had_stop && regs.pc == last_pc && regs.k == last_k) { |
| 965 |
return; |
| 966 |
} |
| 967 |
had_stop = true; |
| 968 |
last_pc = regs.pc; |
| 969 |
last_k = regs.k; |
| 970 |
|
| 971 |
const char *file = nullptr; |
| 972 |
int line = 0; |
| 973 |
const bool has_source = |
| 974 |
(regs.k == 0) && |
| 975 |
dbg_info_addr_to_source_banked(regs.pc, (int)memory_get_ram_bank(), &file, &line) && |
| 976 |
file != nullptr; |
| 977 |
|
| 978 |
if (has_source) { |
| 979 |
if (st.auto_switch_source) { |
| 980 |
ImGui::SetWindowFocus("Source"); |
| 981 |
} |
| 982 |
} else { |
| 983 |
if (st.auto_switch_disasm) { |
| 984 |
ImGui::SetWindowFocus("Disassembly"); |
| 985 |
} |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
static uint16_t s_watch_req_addr = 0; |
| 994 |
static int16_t s_watch_req_bank = -1; |
| 995 |
static uint16_t s_watch_req_len = 1; |
| 996 |
static bool s_watch_req_pending = false; |
| 997 |
|
| 998 |
extern "C" void |
| 999 |
debug_ui_request_watch(uint16_t addr, int16_t bank, uint16_t len) |
| 1000 |
{ |
| 1001 |
s_watch_req_addr = addr; |
| 1002 |
s_watch_req_bank = bank; |
| 1003 |
s_watch_req_len = len ? len : 1; |
| 1004 |
s_watch_req_pending = true; |
| 1005 |
} |
| 1006 |
|
| 1007 |
extern "C" bool |
| 1008 |
debug_ui_take_watch_request(uint16_t *addr, int16_t *bank, uint16_t *len) |
| 1009 |
{ |
| 1010 |
if (!s_watch_req_pending) { |
| 1011 |
return false; |
| 1012 |
} |
| 1013 |
if (addr) *addr = s_watch_req_addr; |
| 1014 |
if (bank) *bank = s_watch_req_bank; |
| 1015 |
if (len) *len = s_watch_req_len; |
| 1016 |
s_watch_req_pending = false; |
| 1017 |
return true; |
| 1018 |
} |
| 1019 |
|