nesemu/debugger/memory_view.c

117 lines
3.5 KiB
C
Raw Normal View History

2024-01-07 16:20:37 -05:00
#include <assert.h>
2024-01-14 21:59:13 -05:00
#include <stdlib.h>
2024-01-07 16:20:37 -05:00
#include "memory_view.h"
2024-01-16 15:46:22 -05:00
#include "dialog.h"
2024-04-03 23:03:35 -04:00
#include "keys.h"
2024-01-07 16:20:37 -05:00
//
2024-01-14 21:59:13 -05:00
// Created by william on 6/1/24.
2024-01-07 16:20:37 -05:00
//
void mv_write_line(MemoryView *view, int line, address base_address, byte *data) {
2024-01-14 21:59:13 -05:00
window_inter_print(view->window, 0, line + 1, "[%04x]", base_address);
2024-01-07 16:20:37 -05:00
2024-01-14 21:59:13 -05:00
for (int i = 0; i <= MEMORY_VIEW_LINE_BYTE_COUNT; i++) {
window_inter_print(view->window, 7 + i * 3, line + 1, "%02x", data[i]);
}
2024-01-09 14:46:20 -05:00
}
void mv_cursor_init(MemoryView *view) {
2024-01-14 21:59:13 -05:00
Cursor *cursor = &view->window->cursor;
window_inter_cursor_init(view->window, 0xf, 0xf);
cursor->min_x = 8;
cursor->min_y = 2;
cursor->multiplier_x = 3;
cursor->width = 2;
2024-01-09 14:46:20 -05:00
}
void mv_handle_key_down(InteractWindow *window, int keycode) {
2024-04-03 23:03:35 -04:00
if (keycode == KEY_GOTO) {
2024-01-16 15:46:22 -05:00
Dialog dialog = dialog_create("Goto Address");
bool cancelled = false;
address input = dialog_get_address(&dialog, &cancelled);
dialog_remove(&dialog);
if (!cancelled) {
MemoryView *view = window->view;
mv_goto(view, input);
mv_cursor_set_addr(view, input);
2024-01-16 15:46:22 -05:00
}
}
2024-01-07 16:20:37 -05:00
}
void mv_init(InteractWindow *interact, ram ram, int x, int y) {
2024-01-14 21:59:13 -05:00
MemoryView *view = malloc(sizeof(MemoryView));
view->window = interact;
2024-01-09 14:46:20 -05:00
view->ram = ram;
view->base_address = 0x0000;
2024-01-14 21:59:13 -05:00
interact->view = view;
interact->handle_cursor_move = &mv_cursor_move;
interact->handle_key_down = &mv_handle_key_down;
interact->deinit = NULL;
2024-01-14 21:59:13 -05:00
window_inter_init(interact, x, y, MEMORY_VIEW_WIDTH, MEMORY_VIEW_HEIGHT, "MEMORY VIEW");
window_inter_print(interact, 0, 0, " +0 +1 +2 +3 +4 +5 +6 +7 +8 +9 +a +b +c +d +e +f");
2024-01-07 16:20:37 -05:00
mv_print(view);
mv_cursor_init(view);
2024-01-07 16:20:37 -05:00
}
void mv_print(MemoryView *view) {
2024-01-07 16:20:37 -05:00
for (int line = 0; line <= MEMORY_VIEW_LINE_COUNT; line++) {
address line_address = view->base_address + line * (MEMORY_VIEW_LINE_BYTE_COUNT + 1);
2024-01-09 14:46:20 -05:00
byte *data = &view->ram[line_address];
2024-01-07 16:20:37 -05:00
mv_write_line(view, line, line_address, data);
2024-01-07 16:20:37 -05:00
}
}
void mv_goto(MemoryView *view, address target) {
2024-04-03 23:03:35 -04:00
assert(target <= RAM_SIZE);
2024-01-07 16:20:37 -05:00
address max_base_address = RAM_SIZE - MEMORY_VIEW_BYTE_COUNT;
if (target > max_base_address) {
target = max_base_address;
}
2024-01-09 14:46:20 -05:00
address line_addr = target & 0xfff0;
view->base_address = line_addr;
mv_print(view);
2024-01-07 16:20:37 -05:00
}
void mv_scroll(MemoryView *view, int direction) {
2024-01-14 21:59:13 -05:00
assert(direction == CURSOR_OFFSET_DOWN || direction == CURSOR_OFFSET_UP);
2024-01-07 16:20:37 -05:00
int offset = 0;
2024-01-14 21:59:13 -05:00
if (direction == CURSOR_OFFSET_UP && view->base_address > MEMORY_VIEW_LINE_BYTE_COUNT) {
2024-01-07 16:20:37 -05:00
offset -= MEMORY_VIEW_LINE_BYTE_COUNT + 1;
}
2024-01-14 21:59:13 -05:00
if (direction == CURSOR_OFFSET_DOWN && view->base_address < RAM_SIZE - MEMORY_VIEW_BYTE_COUNT) {
2024-01-07 16:20:37 -05:00
offset += MEMORY_VIEW_LINE_BYTE_COUNT + 1;
}
address target = view->base_address + offset;
mv_goto(view, target);
2024-01-09 14:46:20 -05:00
}
void mv_cursor_move(InteractWindow *window, int horizontal, int vertical) {
2024-01-14 21:59:13 -05:00
MemoryView *view = (MemoryView *) window->view;
2024-01-09 14:46:20 -05:00
2024-01-14 21:59:13 -05:00
if (vertical == CURSOR_OFFSET_DOWN && view->window->cursor.pos_y == 0xf ||
vertical == CURSOR_OFFSET_UP && view->window->cursor.pos_y == 0) {
// Scroll the view
mv_scroll(view, vertical);
2024-01-09 14:46:20 -05:00
}
2024-01-14 21:59:13 -05:00
// We are not on any edge, move the cursor
cursor_move(&view->window->cursor, horizontal, vertical);
2024-01-09 14:46:20 -05:00
}
void mv_cursor_set_addr(MemoryView *view, address target) {
2024-01-09 14:46:20 -05:00
int view_byte = target - view->base_address;
int x = view_byte & 0x0f;
int y = (view_byte & 0xf0) >> 4;
2024-01-14 21:59:13 -05:00
cursor_set_pos(&view->window->cursor, x, y);
2024-01-07 16:20:37 -05:00
}