nesemu/debugger/memory_view.c

65 lines
2.0 KiB
C

#include <curses.h>
#include <panel.h>
#include <assert.h>
#include "memory_view.h"
//
// Created by william on 1/6/24.
//
void write_line(WINDOW *window, int line, address base_address, byte *data) {
mvwprintw(window, line + 2, 1, "[%04x]", base_address);
for (int i = 0; i <= MEMORY_VIEW_LINE_BYTE_COUNT; i++) {
mvwprintw(window, line + 2, 8 + i * 3, "%02x", data[i]);
}
}
void memory_view_init(MemoryView *view, ram ram) {
WINDOW *window = newwin(MEMORY_VIEW_HEIGHT, MEMORY_VIEW_WIDTH, 0, 0);
box(window, 0, 0);
mvwprintw(window, 0, 1, " MEMORY VIEW ");
mvwprintw(window, 1, 1, " +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f");
view->panel = new_panel(window);
view->base_address = 0xfc00;
memory_view_print(view, ram);
}
void memory_view_print(MemoryView *view, ram ram) {
for (int line = 0; line <= MEMORY_VIEW_LINE_COUNT; line++) {
address line_address = view->base_address + line * (MEMORY_VIEW_LINE_BYTE_COUNT + 1);
byte *data = &ram[line_address];
write_line(view->panel->win, line, line_address, data);
}
}
void memory_view_goto(MemoryView *view, address target, ram ram) {
assert(target < RAM_SIZE);
address max_base_address = RAM_SIZE - MEMORY_VIEW_BYTE_COUNT;
if (target > max_base_address) {
target = max_base_address;
}
view->base_address = target;
memory_view_print(view, ram);
}
void memory_view_scroll(MemoryView *view, char direction, ram ram) {
assert(direction == MEMORY_VIEW_DIRECTION_DOWN || direction == MEMORY_VIEW_DIRECTION_UP);
int offset = 0;
if (direction == MEMORY_VIEW_DIRECTION_DOWN && view->base_address > MEMORY_VIEW_LINE_BYTE_COUNT) {
offset -= MEMORY_VIEW_LINE_BYTE_COUNT + 1;
}
if (direction == MEMORY_VIEW_DIRECTION_UP && view->base_address < RAM_SIZE - MEMORY_VIEW_BYTE_COUNT) {
offset += MEMORY_VIEW_LINE_BYTE_COUNT + 1;
}
address target = view->base_address + offset;
memory_view_goto(view, target, ram);
}