#include #include #include "memory_view.h" #include "dialog.h" #include "keys.h" #include "../cpu/memory.h" // // Created by william on 6/1/24. // void mv_write_line(MemoryView *view, int line, address base_address, byte *data) { window_inter_print(view->window, 0, line + 1, "[%04x]", base_address); for (int i = 0; i <= MEMORY_VIEW_LINE_BYTE_COUNT; i++) { window_inter_print(view->window, 7 + i * 3, line + 1, "%02x", data[i]); } } void mv_cursor_init(MemoryView *view) { 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; } void mv_handle_key_down(InteractWindow *window, int keycode) { if (keycode == KEY_GOTO) { 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); } } } void mv_init(InteractWindow *interact, int x, int y) { MemoryView *view = malloc(sizeof(MemoryView)); view->window = interact; view->base_address = 0x0000; interact->view = view; interact->handle_cursor_move = &mv_cursor_move; interact->handle_key_down = &mv_handle_key_down; interact->deinit = NULL; 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"); mv_print(view); mv_cursor_init(view); } void mv_print(MemoryView *view) { 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 = mem_get_ptr(line_address); mv_write_line(view, line, line_address, data); } } void mv_goto(MemoryView *view, address target) { assert(target <= RAM_SIZE); address max_base_address = RAM_SIZE - MEMORY_VIEW_BYTE_COUNT; if (target > max_base_address) { target = max_base_address; } address line_addr = target & 0xfff0; view->base_address = line_addr; mv_print(view); } void mv_scroll(MemoryView *view, int direction) { assert(direction == CURSOR_OFFSET_DOWN || direction == CURSOR_OFFSET_UP); int offset = 0; if (direction == CURSOR_OFFSET_UP && view->base_address > MEMORY_VIEW_LINE_BYTE_COUNT) { offset -= MEMORY_VIEW_LINE_BYTE_COUNT + 1; } if (direction == CURSOR_OFFSET_DOWN && view->base_address < RAM_SIZE - MEMORY_VIEW_BYTE_COUNT) { offset += MEMORY_VIEW_LINE_BYTE_COUNT + 1; } address target = view->base_address + offset; mv_goto(view, target); } void mv_cursor_move(InteractWindow *window, int horizontal, int vertical) { MemoryView *view = (MemoryView *) window->view; 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); } // We are not on any edge, move the cursor cursor_move(&view->window->cursor, horizontal, vertical); } void mv_cursor_set_addr(MemoryView *view, address target) { int view_byte = target - view->base_address; int x = view_byte & 0x0f; int y = (view_byte & 0xf0) >> 4; cursor_set_pos(&view->window->cursor, x, y); }