swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 1 | #include "gui.h" |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 2 | #include "common.h" |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 3 | |
| 4 | #include <GL/glew.h> |
| 5 | #include <SDL2/SDL.h> |
| 6 | #include <SDL2/SDL_opengl.h> |
| 7 | |
| 8 | #define NK_INCLUDE_FIXED_TYPES |
| 9 | #define NK_INCLUDE_STANDARD_IO |
| 10 | #define NK_INCLUDE_STANDARD_VARARGS |
| 11 | #define NK_INCLUDE_DEFAULT_ALLOCATOR |
| 12 | #define NK_INCLUDE_VERTEX_BUFFER_OUTPUT |
| 13 | #define NK_INCLUDE_FONT_BAKING |
| 14 | #define NK_INCLUDE_DEFAULT_FONT |
| 15 | #define NK_IMPLEMENTATION |
| 16 | #define NK_SDL_GL3_IMPLEMENTATION |
| 17 | #include "nuklear/nuklear.h" |
| 18 | #include "nuklear/demo/sdl_opengl3/nuklear_sdl_gl3.h" |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 19 | #undef SCREEN_ONLY_SDL |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 20 | #include "screen.h" |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 21 | |
| 22 | #define WINDOW_WIDTH 720 |
| 23 | #define WINDOW_HEIGHT 640 |
| 24 | #define MAX_VERTEX_MEMORY 512 * 1024 |
| 25 | #define MAX_ELEMENT_MEMORY 128 * 1024 |
| 26 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 27 | typedef struct |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 28 | { |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 29 | cpu_t *cpu; |
| 30 | mqd_t mq; |
| 31 | } gui_arg_t; |
| 32 | |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 33 | static void cmd(mqd_t mq, char *msg) |
| 34 | { |
| 35 | mq_send(mq, msg, strlen(msg) + 1, 2); |
| 36 | } |
| 37 | |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 38 | void gui(gui_arg_t *arg) |
| 39 | { |
| 40 | cpu_t *cpu = arg->cpu; |
| 41 | mqd_t mq = arg->mq; |
| 42 | |
| 43 | free(arg); |
| 44 | |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 45 | SDL_Window *win; |
| 46 | SDL_GLContext glContext; |
| 47 | int win_width, win_height; |
| 48 | bool running = true; |
| 49 | bool cpu_running = false; |
| 50 | |
| 51 | struct nk_context *ctx; |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 52 | struct nk_colorf bg = |
| 53 | { |
| 54 | .r = 0.29f, |
| 55 | .g = 0.28f, |
| 56 | .b = 0.50f, |
| 57 | .a = 1.0f, |
| 58 | }; |
swissChili | e7ee6da | 2020-08-08 16:14:21 -0700 | [diff] [blame] | 59 | struct nk_color |
| 60 | selected = nk_rgb(28, 234, 79), |
| 61 | red = nk_rgb(226, 56, 76); |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 62 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 63 | uint16_t disas_start = 0x600, |
| 64 | disas_end = 0x600 + 32; |
| 65 | |
| 66 | uint8_t screen_scale = 4; |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 67 | |
| 68 | SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0"); |
| 69 | SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS); |
| 70 | SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG); |
| 71 | SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); |
| 72 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); |
| 73 | SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); |
| 74 | SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); |
| 75 | win = SDL_CreateWindow("6502", |
| 76 | SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
| 77 | WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI); |
| 78 | glContext = SDL_GL_CreateContext(win); |
| 79 | SDL_GetWindowSize(win, &win_width, &win_height); |
| 80 | |
| 81 | /* OpenGL setup */ |
| 82 | glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); |
| 83 | glewExperimental = 1; |
| 84 | if (glewInit() != GLEW_OK) { |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 85 | THROW("Failed to setup GLEW\n"); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | ctx = nk_sdl_init(win); |
| 89 | |
| 90 | struct nk_font_atlas *atlas; |
| 91 | nk_sdl_font_stash_begin(&atlas); |
| 92 | struct nk_font *font = nk_font_atlas_add_default(atlas, 16, NULL); |
| 93 | nk_sdl_font_stash_end(); |
| 94 | //nk_style_load_all_cursors(ctx, atlas->cursors); |
| 95 | nk_style_set_font(ctx, &font->handle); |
| 96 | |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 97 | while (running) |
| 98 | { |
| 99 | SDL_Event evt; |
| 100 | nk_input_begin(ctx); |
| 101 | while (SDL_PollEvent(&evt)) |
| 102 | { |
| 103 | if (evt.type == SDL_QUIT) goto cleanup; |
| 104 | nk_sdl_handle_event(&evt); |
| 105 | } |
| 106 | nk_input_end(ctx); |
| 107 | |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 108 | if (!cpu->running) |
| 109 | cpu_running = false; |
| 110 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 111 | if (nk_begin(ctx, "Registers", nk_rect(50, 300, 500, 90), |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 112 | NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| |
| 113 | NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) |
| 114 | { |
| 115 | nk_layout_row_dynamic(ctx, 30, 4); |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 116 | uint16_t pc = nk_propertyi(ctx, "PC", 0, cpu->pc, 0xFFFF, 1, 20.0f); |
| 117 | uint8_t rega = nk_propertyi(ctx, "A", 0, cpu->regs[A], 0xFF, 1, 20.0f), |
| 118 | regx = nk_propertyi(ctx, "X", 0, cpu->regs[X], 0xFF, 1, 20.0f), |
| 119 | regy = nk_propertyi(ctx, "Y", 0, cpu->regs[Y], 0xFF, 1, 20.0f); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 120 | } |
| 121 | nk_end(ctx); |
| 122 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 123 | if (nk_begin(ctx, "Screen", nk_rect(50, 400, 150, 220), |
| 124 | NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| |
| 125 | NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) |
| 126 | { |
| 127 | nk_layout_row_dynamic(ctx, 24, 1); |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 128 | screen_scale = nk_propertyi(ctx, "Scale", 1, screen_scale, 16, 1, 1); |
swissChili | 94ba1f5 | 2020-08-08 11:39:10 -0700 | [diff] [blame] | 129 | |
swissChili | bb478f1 | 2020-08-07 20:45:07 -0700 | [diff] [blame] | 130 | nk_layout_row_static(ctx, screen_scale * 32, screen_scale * 32, 1); |
| 131 | screen(ctx, cpu->mem + CPU_FB_ADDR, screen_scale); |
| 132 | } |
| 133 | nk_end(ctx); |
| 134 | |
| 135 | if (nk_begin(ctx, "Disassembler", nk_rect(330, 50, 300, 200), |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 136 | NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| |
| 137 | NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) |
| 138 | { |
| 139 | nk_layout_row_dynamic(ctx, 30, 2); |
| 140 | disas_start = nk_propertyi(ctx, "Start", 0, disas_start, 0xFFFF, 1, 20.0f); |
| 141 | disas_end = nk_propertyi(ctx, "End", 0, disas_end, 0xFFFF, 1, 20.0f); |
| 142 | |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 143 | for (uint16_t pc = disas_start; pc < disas_end;) |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 144 | { |
| 145 | nk_layout_row_begin(ctx, NK_STATIC, 24, 2); |
| 146 | |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 147 | uint16_t cpu_pc = cpu->pc, |
| 148 | this_pc = pc; |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 149 | |
| 150 | char addr[6]; |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 151 | sprintf(addr, "$%x", pc); |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 152 | |
| 153 | nk_layout_row_push(ctx, 48); |
| 154 | nk_label(ctx, addr, NK_TEXT_LEFT); |
| 155 | |
| 156 | nk_layout_row_push(ctx, 120); |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 157 | char *line = disas_step(cpu, &pc); |
| 158 | if (cpu_pc == this_pc) |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 159 | { |
| 160 | nk_label_colored(ctx, line, NK_TEXT_LEFT, selected); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | nk_label(ctx, line, NK_TEXT_LEFT); |
| 165 | } |
| 166 | free(line); |
| 167 | } |
swissChili | c51e922 | 2020-08-07 16:09:14 -0700 | [diff] [blame] | 168 | } |
| 169 | nk_end(ctx); |
| 170 | |
swissChili | e7ee6da | 2020-08-08 16:14:21 -0700 | [diff] [blame] | 171 | if (nk_begin(ctx, "Stack", nk_rect(250, 250, 230, 350), |
| 172 | NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| |
| 173 | NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) |
| 174 | { |
| 175 | nk_layout_row_static(ctx, 24, 48, 2); |
swissChili | b04a402 | 2020-08-09 12:51:00 -0700 | [diff] [blame] | 176 | for (int i = 0xFF; i >= 0; i--) |
swissChili | e7ee6da | 2020-08-08 16:14:21 -0700 | [diff] [blame] | 177 | { |
| 178 | char line[6]; |
| 179 | sprintf(line, "$%x", 0x100 + i); |
| 180 | nk_label(ctx, line, NK_TEXT_LEFT); |
| 181 | |
| 182 | sprintf(line, "$%x", cpu->mem[0x100 + i]); |
| 183 | if (i == cpu->regs[SP]) |
| 184 | { |
| 185 | nk_label_colored(ctx, line, NK_TEXT_LEFT, selected); |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | nk_label(ctx, line, NK_TEXT_LEFT); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | nk_end(ctx); |
| 194 | |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 195 | if (nk_begin(ctx, "Debugger", nk_rect(50, 50, 230, 150), |
| 196 | NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE| |
| 197 | NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE)) |
| 198 | { |
| 199 | nk_layout_row_dynamic(ctx, 30, 2); |
| 200 | nk_label(ctx, cpu->running ? "CPU Running" : "CPU Halted", NK_TEXT_LEFT); |
| 201 | if (nk_button_label(ctx, "Reset")) |
| 202 | { |
| 203 | puts("cpu reset"); |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 204 | cmd(mq, "reset"); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | nk_layout_row_dynamic(ctx, 30, 2); |
| 208 | if (nk_button_label(ctx, "Step")) |
| 209 | { |
| 210 | printf("step pressed!\n"); |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 211 | cmd(mq, "step"); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | if (nk_button_label(ctx, cpu_running ? "Stop" : "Run")) |
| 215 | { |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 216 | cmd(mq, cpu_running ? "pause" : "run"); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 217 | cpu_running = !cpu_running; |
| 218 | puts(cpu_running ? "cpu running" : "cpu stopped"); |
| 219 | } |
| 220 | } |
| 221 | nk_end(ctx); |
| 222 | |
| 223 | SDL_GetWindowSize(win, &win_width, &win_height); |
| 224 | glViewport(0, 0, win_width, win_height); |
| 225 | glClear(GL_COLOR_BUFFER_BIT); |
| 226 | glClearColor(bg.r, bg.g, bg.b, bg.a); |
| 227 | nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY); |
| 228 | SDL_GL_SwapWindow(win); |
| 229 | } |
| 230 | |
| 231 | cleanup: |
| 232 | nk_sdl_shutdown(); |
| 233 | SDL_GL_DeleteContext(glContext); |
| 234 | SDL_DestroyWindow(win); |
| 235 | SDL_Quit(); |
swissChili | 1970cb8 | 2020-08-10 13:22:39 -0700 | [diff] [blame] | 236 | |
| 237 | cmd(mq, "quit"); |
swissChili | dbbd540 | 2020-08-07 15:07:39 -0700 | [diff] [blame] | 238 | } |
swissChili | c6b4f7e | 2020-08-09 16:36:36 -0700 | [diff] [blame] | 239 | |
| 240 | |
| 241 | void start_gui(mqd_t mq, cpu_t *cpu) |
| 242 | { |
| 243 | pthread_t gui_thread; |
| 244 | gui_arg_t *arg = malloc(sizeof(gui_arg_t)); |
| 245 | arg->cpu = cpu; |
| 246 | arg->mq = mq; |
| 247 | pthread_create(&gui_thread, NULL, (void *(*)(void *))&gui, arg); |
| 248 | } |