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-14 21:59:13 -05:00
|
|
|
// Created by william on 6/1/24.
|
2024-01-07 16:20:37 -05:00
|
|
|
//
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_write_line(MemoryView *view, int line, address base_address, byte *data) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_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;
|
2024-01-09 14:46:20 -05:00
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
cursor_enable(cursor);
|
2024-01-09 14:46:20 -05:00
|
|
|
}
|
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_handle_key_down(InteractWindow *window, int keycode) {
|
2024-01-07 16:20:37 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_init(InteractWindow *interact, ram ram, int x, int y) {
|
|
|
|
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 = &memory_view_cursor_move;
|
|
|
|
interact->handle_key_down = &memory_view_handle_key_down;
|
|
|
|
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
|
|
|
|
2024-01-09 14:46:20 -05:00
|
|
|
memory_view_print(view);
|
2024-01-14 21:59:13 -05:00
|
|
|
memory_view_cursor_init(view);
|
2024-01-07 16:20:37 -05:00
|
|
|
}
|
|
|
|
|
2024-01-09 14:46:20 -05:00
|
|
|
void memory_view_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
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
memory_view_write_line(view, line, line_address, data);
|
2024-01-07 16:20:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-09 14:46:20 -05:00
|
|
|
void memory_view_goto(MemoryView *view, address target) {
|
2024-01-07 16:20:37 -05:00
|
|
|
assert(target < RAM_SIZE);
|
|
|
|
|
|
|
|
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;
|
|
|
|
memory_view_print(view);
|
2024-01-07 16:20:37 -05:00
|
|
|
}
|
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_scroll(MemoryView *view, int direction) {
|
|
|
|
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;
|
2024-01-09 14:46:20 -05:00
|
|
|
memory_view_goto(view, target);
|
|
|
|
}
|
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_cursor_move(InteractWindow *window, int horizontal, int vertical) {
|
|
|
|
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
|
2024-01-09 14:46:20 -05:00
|
|
|
memory_view_scroll(view, vertical);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-01-14 21:59:13 -05:00
|
|
|
void memory_view_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
|
|
|
}
|