nesemu/debugger/debugger.c

101 lines
2.8 KiB
C

//
// Created by william on 1/6/24.
//
#include <curses.h>
#include <panel.h>
#include <stdlib.h>
#include "debugger.h"
#include "memory_view.h"
#include "program_view.h"
#include "keys.h"
#include "cpu_view.h"
#include "ppu_view.h"
void debugger_create_window() {
setenv("TERMINFO", "/usr/share/terminfo", 1);
setenv("TERM", "xterm", 1);
initscr();
raw();
noecho();
curs_set(0);
keypad(stdscr, true);
}
LinkedList debugger_create_interactive_windows(System *system) {
LinkedList interactive_windows;
InteractWindow *window;
interactive_windows = linked_list_init(true);
window = malloc(sizeof(InteractWindow));
mv_init(window, system->ram, 0, 0);
linked_list_add(&interactive_windows, window);
window = malloc(sizeof(InteractWindow));
pv_init(window, system, MEMORY_VIEW_WIDTH, 0);
linked_list_add(&interactive_windows, window);
return interactive_windows;
}
void debugger_uninit_interactive_windows(LinkedList *windows) {
linked_list_cursor_reset(windows);
InteractWindow *window = windows->current->data;
for (int i = 0; i < windows->size; i++) {
window_inter_deinit(window);
window = linked_list_next(windows)->data;
}
linked_list_uninit(windows);
}
void start_debugger(System *system) {
CpuView *cpu_view;
PpuView *ppu_view;
LinkedList interactive_windows;
InteractWindow *current_window;
debugger_create_window();
interactive_windows = debugger_create_interactive_windows(system);
current_window = interactive_windows.current->data;
cpu_view = cv_init(&system->cpu, 0, MEMORY_VIEW_HEIGHT);
ppu_view = ppv_init(&system->ppu, CPU_VIEW_WIDTH, MEMORY_VIEW_HEIGHT);
cursor_enable(&current_window->cursor);
update_panels();
doupdate();
int keycode;
while ((keycode = getch()) != KEY_EXIT_DEBUGGER) {
if (keycode == KEY_NEXT_VIEW) {
cursor_disable(&current_window->cursor);
current_window = linked_list_next(&interactive_windows)->data;
cursor_enable(&current_window->cursor);
} else if (keycode == KEY_VIEW_UP) {
current_window->handle_cursor_move(current_window, 0, CURSOR_OFFSET_UP);
} else if (keycode == KEY_VIEW_DOWN) {
current_window->handle_cursor_move(current_window, 0, CURSOR_OFFSET_DOWN);
} else if (keycode == KEY_VIEW_LEFT) {
current_window->handle_cursor_move(current_window, CURSOR_OFFSET_LEFT, 0);
} else if (keycode == KEY_VIEW_RIGHT) {
current_window->handle_cursor_move(current_window, CURSOR_OFFSET_RIGHT, 0);
} else {
current_window->handle_key_down(current_window, keycode);
}
update_panels();
doupdate();
}
debugger_uninit_interactive_windows(&interactive_windows);
cv_uninit(cpu_view);
ppv_uninit(ppu_view);
endwin();
}