Skip to content

Commit

Permalink
WIP on Disassembly
Browse files Browse the repository at this point in the history
  • Loading branch information
emoon committed Mar 29, 2024
1 parent 6145f5a commit c95323b
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 36 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ add_executable(quaesar
uae_src/blit.h
)

# Recursively glob for all .cpp files in the src directory
file(GLOB_RECURSE CROSS_FILES "src/*.cpp")
target_sources(quaesar PRIVATE ${CROSS_FILES})

if (APPLE OR LINUX OR UNIX)
target_compile_options(quaesar PRIVATE -DUAE=1 -D_cdecl= -DFILEFLAG_WRITE=1 -DOS_NAME=\"linux\")
target_compile_options(quaesar PRIVATE -DUSHORT=uint16_t -DHWND=uint32_t -DHRESULT=uint32_t -DWPARAM=uint16_t -DLPARAM=uint32_t)
Expand Down
28 changes: 2 additions & 26 deletions src/debugger/debugger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ Debugger* Debugger_create() {
ImGuiStyle& style = ImGui::GetStyle();
style.WindowPadding = ImVec2(8.00f, 8.00f);
style.FramePadding = ImVec2(5.00f, 2.00f);
style.CellPadding = ImVec2(6.00f, 6.00f);
style.CellPadding = ImVec2(6.00f, 2.00f);
style.ItemSpacing = ImVec2(6.00f, 6.00f);
style.ItemInnerSpacing = ImVec2(6.00f, 6.00f);
style.TouchExtraPadding = ImVec2(0.00f, 0.00f);
Expand Down Expand Up @@ -158,7 +158,7 @@ Debugger* Debugger_create() {
}

debugger->memory_view = new MemoryView();
debugger->d_view = DisassemblyView_create();
debugger->d_view = DisassemblyView_create(debugger->capstone);

return debugger;
}
Expand All @@ -171,30 +171,6 @@ static void draw_debugger_window(Debugger* self) {

static bool p_open = true;

uae_u32 pc = M68K_GETPC;
uae_u32 offset = 40;
uae_u32 count_bytes = 80;

uae_u8* pc_addr = memory_get_real_address(pc);
uae_u32 start_disasm = pc - offset;

// TODO: better
cs_insn* insn = nullptr;
size_t count = cs_disasm(self->capstone, pc_addr - offset, count_bytes, start_disasm, 0, &insn);

printf("-----------------------------------------\n");

for (size_t j = 0; j < count; j++) {
if (insn[j].address == pc) {
printf("0x%" PRIx64 ":>\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
} else {
printf("0x%" PRIx64 ":\t%s\t%s\n", insn[j].address, insn[j].mnemonic, insn[j].op_str);
}
// print_insn_detail(&insn[j]);
}

printf("%08x\n", M68K_GETPC);

const ImGuiViewport* viewport = ImGui::GetMainViewport();
ImGui::SetNextWindowPos(viewport->WorkPos);
ImGui::SetNextWindowSize(viewport->WorkSize);
Expand Down
88 changes: 79 additions & 9 deletions src/debugger/disassembly_view.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#include "disassembly_view.h"
#include <dear_imgui/imgui.h>
#include <capstone/capstone.h>
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "memory.h"
#include "newcpu.h"

struct DisassemblyView {
int foo;
csh capstone;
};

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DisassemblyView* DisassemblyView_create() {
DisassemblyView* DisassemblyView_create(csh capstone) {
DisassemblyView* view = new DisassemblyView();
view->capstone = capstone;
return view;
}

Expand All @@ -20,21 +27,84 @@ void DisassemblyView_destroy(DisassemblyView* self) {

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

static void draw_disassembly(DisassemblyView* self) {
ImDrawList* draw_list = ImGui::GetWindowDrawList();

uae_u32 pc = M68K_GETPC;
uae_u32 offset = 40;
uae_u32 count_bytes = 80;

uae_u8* pc_addr = memory_get_real_address(pc);
uae_u32 start_disasm = pc - offset;

cs_insn* insn = nullptr;
size_t count = cs_disasm(self->capstone, pc_addr - offset, count_bytes, start_disasm, 0, &insn);

int max_instruction_width = 20;

ImGuiTableFlags flags = ImGuiTableFlags_Resizable
| ImGuiTableFlags_Reorderable
| ImGuiTableFlags_Hideable
| ImGuiTableFlags_BordersOuter
| ImGuiTableFlags_BordersV;

char buffer[512];

if (ImGui::BeginTable("disassembly", 3, flags)) {
ImGui::TableSetupColumn("Adress");
ImGui::TableSetupColumn("Instruction");
ImGui::TableSetupColumn("Cycles");
ImGui::TableHeadersRow();

for (size_t j = 0; j < count; j++) {
ImGui::TableNextColumn();
ImGui::Text("0x%" PRIx64, insn[j].address);
ImGui::TableNextColumn();

int width = strlen(insn[j].mnemonic);
memcpy(buffer, insn[j].mnemonic, width);

for (int i = 0; i < max_instruction_width - width; i++) {
buffer[width + i] = ' ';
}

strcpy(buffer + max_instruction_width, insn[j].op_str);

ImGui::Text("%s", buffer);
ImGui::TableNextColumn();
}

ImGui::EndTable();
}


//printf("%08x\n", M68K_GETPC);
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void DisassemblyView_update(DisassemblyView* self, const char* name) {
//static bool show_demo_window = true;
//ImGui::ShowDemoWindow(&show_demo_window);

static bool open = true;

float text_height = ImGui::GetTextLineHeight();

if (ImGui::Begin(title, &open, ImGuiWindowFlags_NoScrollbar)) {
ImGui::BeginChild("##scrolling", ImVec2(00.0f, 0.0f), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
ImDrawList* draw_list = ImGui::GetWindowDrawList();
if (ImGui::Begin("Disassembly", &open, ImGuiWindowFlags_NoScrollbar)) {
//ImGui::BeginChild("##scrolling", ImVec2(00.0f, 0.0f), false, ImGuiWindowFlags_NoMove | ImGuiWindowFlags_NoNav);
//ImDrawList* draw_list = ImGui::GetWindowDrawList();
//ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
//ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));

ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0, 0));
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0));


ImGui::PopStyleVar(2);
draw_disassembly(self);

//ImGui::PopStyleVar(2);

//ImGui::EndChild();
}

ImGui::End();
}

4 changes: 3 additions & 1 deletion src/debugger/disassembly_view.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <capstone/capstone.h>

struct DisassemblyView;

DisassemblyView* DisassemblyView_create();
DisassemblyView* DisassemblyView_create(csh capstone);
void DisassemblyView_update(DisassemblyView* self, const char* name);
void DisassemblyView_destroy(DisassemblyView* self);

0 comments on commit c95323b

Please sign in to comment.