Skip to content

Commit

Permalink
[debugger] Memory editor save to binary and text files. #4
Browse files Browse the repository at this point in the history
  • Loading branch information
drhelius committed Feb 17, 2025
1 parent 047ce82 commit 82666b7
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
47 changes: 38 additions & 9 deletions platforms/shared/desktop/gui_debug_memeditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1544,25 +1544,54 @@ void MemEditor::SetValueToSelection(int value)
}
}

void MemEditor::SaveToFile(const char* file_path)
void MemEditor::SaveToTextFile(const char* file_path)
{
int size = m_mem_size * m_mem_word;
int row = m_bytes_per_row * m_mem_word;

int total_bytes = m_mem_size * m_mem_word;
int row_bytes = m_bytes_per_row * m_mem_word;
FILE* file = fopen(file_path, "w");

if (file)
{
for (int i = 0; i < (size - 1); i++)
int row_count = (total_bytes + row_bytes - 1) / row_bytes;
for (int r = 0; r < row_count; r++)
{
fprintf(file, "%02X ", m_mem_data[i]);
int current_address = m_mem_base_addr + (r * m_bytes_per_row);

fprintf(file, m_hex_addr_format, current_address);
fprintf(file, ": ");

if ((i % row) == (row - 1))
fprintf(file, "\n");
int row_start = r * row_bytes;
int row_end = row_start + row_bytes;
if (row_end > total_bytes)
row_end = total_bytes;

if (m_mem_word == 1)
for (int i = row_start; i < row_end; i++)
fprintf(file, "%02X ", m_mem_data[i]);
else if (m_mem_word == 2)
{
int word_count = (row_end - row_start) / 2;
uint16_t* mem_data_16 = (uint16_t*)m_mem_data;
int word_start = row_start / 2;
for (int i = 0; i < word_count; i++)
fprintf(file, "%04X ", mem_data_16[word_start + i]);
}
fprintf(file, "\n");
}

fprintf(file, "%02X", m_mem_data[(size - 1)]);
fclose(file);
}
}

void MemEditor::SaveToBinaryFile(const char* file_path)
{
int size = m_mem_size * m_mem_word;

FILE* file = fopen(file_path, "wb");

if (file)
{
fwrite(m_mem_data, m_mem_word, size, file);
fclose(file);
}
}
Expand Down
3 changes: 2 additions & 1 deletion platforms/shared/desktop/gui_debug_memeditor.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ class MemEditor
void SelectAll();
void ClearSelection();
void SetValueToSelection(int value);
void SaveToFile(const char* file_path);
void SaveToTextFile(const char* file_path);
void SaveToBinaryFile(const char* file_path);
void AddBookmark();
void RemoveBookmarks();
std::vector<Bookmark>* GetBookmarks();
Expand Down
16 changes: 12 additions & 4 deletions platforms/shared/desktop/gui_debug_memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,12 @@ void gui_debug_memory_goto(int editor, int address)
mem_edit[mem_edit_select].JumpToAddress(address);
}

void gui_debug_memory_save_dump(const char* file_path)
void gui_debug_memory_save_dump(const char* file_path, bool binary)
{
mem_edit[current_mem_edit].SaveToFile(file_path);
if (binary)
mem_edit[current_mem_edit].SaveToBinaryFile(file_path);
else
mem_edit[current_mem_edit].SaveToTextFile(file_path);
}

static void draw_tabs(void)
Expand Down Expand Up @@ -216,9 +219,14 @@ static void memory_editor_menu(void)

if (ImGui::BeginMenu("File"))
{
if (ImGui::MenuItem("Save Memory As..."))
if (ImGui::MenuItem("Save Memory As Text..."))
{
gui_file_dialog_save_memory_dump();
gui_file_dialog_save_memory_dump(false);
}

if (ImGui::MenuItem("Save Memory As Binary..."))
{
gui_file_dialog_save_memory_dump(true);
}

ImGui::EndMenu();
Expand Down
2 changes: 1 addition & 1 deletion platforms/shared/desktop/gui_debug_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ EXTERN void gui_debug_memory_copy(void);
EXTERN void gui_debug_memory_paste(void);
EXTERN void gui_debug_memory_select_all(void);
EXTERN void gui_debug_memory_goto(int editor, int address);
EXTERN void gui_debug_memory_save_dump(const char* file_path);
EXTERN void gui_debug_memory_save_dump(const char* file_path, bool binary);

#undef GUI_DEBUG_MEMORY_IMPORT
#undef EXTERN
Expand Down
6 changes: 3 additions & 3 deletions platforms/shared/desktop/gui_filedialogs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ void gui_file_dialog_save_screenshot(void)
}
}

void gui_file_dialog_save_memory_dump(void)
void gui_file_dialog_save_memory_dump(bool binary)
{
nfdchar_t *outPath;
nfdfilteritem_t filterItem[1] = { { "Memory Dump Files", "txt" } };
nfdfilteritem_t filterItem[1] = { { "Memory Dump Files", binary ? "bin" : "txt" } };
nfdsavedialogu8args_t args = { };
args.filterList = filterItem;
args.filterCount = 1;
Expand All @@ -255,7 +255,7 @@ void gui_file_dialog_save_memory_dump(void)
nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY)
{
gui_debug_memory_save_dump(outPath);
gui_debug_memory_save_dump(outPath, binary);
NFD_FreePath(outPath);
}
else if (result != NFD_CANCEL)
Expand Down
2 changes: 1 addition & 1 deletion platforms/shared/desktop/gui_filedialogs.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ EXTERN void gui_file_dialog_choose_savestate_path(void);
EXTERN void gui_file_dialog_choose_screenshot_path(void);
EXTERN void gui_file_dialog_load_symbols(void);
EXTERN void gui_file_dialog_save_screenshot(void);
EXTERN void gui_file_dialog_save_memory_dump(void);
EXTERN void gui_file_dialog_save_memory_dump(bool binary);
EXTERN void gui_file_dialog_save_disassembler(bool full);
EXTERN void gui_file_dialog_save_log(void);

Expand Down

0 comments on commit 82666b7

Please sign in to comment.