blob: ed34655a818c65e0ee14850738f9773398d3a261 [file] [log] [blame]
swissChilidbbd5402020-08-07 15:07:39 -07001#include "gui.h"
swissChili1970cb82020-08-10 13:22:39 -07002#include "common.h"
swissChilidbbd5402020-08-07 15:07:39 -07003
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"
swissChili94ba1f52020-08-08 11:39:10 -070019#undef SCREEN_ONLY_SDL
swissChilibb478f12020-08-07 20:45:07 -070020#include "screen.h"
swissChilidbbd5402020-08-07 15:07:39 -070021
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
swissChilic6b4f7e2020-08-09 16:36:36 -070027typedef struct
swissChilidbbd5402020-08-07 15:07:39 -070028{
swissChilic6b4f7e2020-08-09 16:36:36 -070029 cpu_t *cpu;
30 mqd_t mq;
31} gui_arg_t;
32
swissChili1970cb82020-08-10 13:22:39 -070033static void cmd(mqd_t mq, char *msg)
34{
35 mq_send(mq, msg, strlen(msg) + 1, 2);
36}
37
swissChilic6b4f7e2020-08-09 16:36:36 -070038void gui(gui_arg_t *arg)
39{
40 cpu_t *cpu = arg->cpu;
41 mqd_t mq = arg->mq;
42
43 free(arg);
44
swissChilidbbd5402020-08-07 15:07:39 -070045 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;
swissChilic51e9222020-08-07 16:09:14 -070052 struct nk_colorf bg =
53 {
54 .r = 0.29f,
55 .g = 0.28f,
56 .b = 0.50f,
57 .a = 1.0f,
58 };
swissChilie7ee6da2020-08-08 16:14:21 -070059 struct nk_color
60 selected = nk_rgb(28, 234, 79),
61 red = nk_rgb(226, 56, 76);
swissChilic51e9222020-08-07 16:09:14 -070062
swissChilibb478f12020-08-07 20:45:07 -070063 uint16_t disas_start = 0x600,
64 disas_end = 0x600 + 32;
65
66 uint8_t screen_scale = 4;
swissChilidbbd5402020-08-07 15:07:39 -070067
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) {
swissChili1970cb82020-08-10 13:22:39 -070085 THROW("Failed to setup GLEW\n");
swissChilidbbd5402020-08-07 15:07:39 -070086 }
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
swissChilidbbd5402020-08-07 15:07:39 -070097 while (running)
98 {
99 SDL_Event evt;
100 nk_input_begin(ctx);
101 while (SDL_PollEvent(&evt))
102 {
swissChili4bccd442020-08-11 13:55:10 -0700103 if (evt.type == SDL_QUIT)
104 goto cleanup;
swissChilidbbd5402020-08-07 15:07:39 -0700105 nk_sdl_handle_event(&evt);
106 }
107 nk_input_end(ctx);
108
swissChilidbbd5402020-08-07 15:07:39 -0700109 if (!cpu->running)
110 cpu_running = false;
111
swissChilibb478f12020-08-07 20:45:07 -0700112 if (nk_begin(ctx, "Registers", nk_rect(50, 300, 500, 90),
swissChilidbbd5402020-08-07 15:07:39 -0700113 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
114 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
115 {
116 nk_layout_row_dynamic(ctx, 30, 4);
swissChili1970cb82020-08-10 13:22:39 -0700117 uint16_t pc = nk_propertyi(ctx, "PC", 0, cpu->pc, 0xFFFF, 1, 20.0f);
118 uint8_t rega = nk_propertyi(ctx, "A", 0, cpu->regs[A], 0xFF, 1, 20.0f),
119 regx = nk_propertyi(ctx, "X", 0, cpu->regs[X], 0xFF, 1, 20.0f),
120 regy = nk_propertyi(ctx, "Y", 0, cpu->regs[Y], 0xFF, 1, 20.0f);
swissChilidbbd5402020-08-07 15:07:39 -0700121 }
122 nk_end(ctx);
123
swissChilibb478f12020-08-07 20:45:07 -0700124 if (nk_begin(ctx, "Screen", nk_rect(50, 400, 150, 220),
125 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
126 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
127 {
128 nk_layout_row_dynamic(ctx, 24, 1);
swissChili1970cb82020-08-10 13:22:39 -0700129 screen_scale = nk_propertyi(ctx, "Scale", 1, screen_scale, 16, 1, 1);
swissChili94ba1f52020-08-08 11:39:10 -0700130
swissChilibb478f12020-08-07 20:45:07 -0700131 nk_layout_row_static(ctx, screen_scale * 32, screen_scale * 32, 1);
132 screen(ctx, cpu->mem + CPU_FB_ADDR, screen_scale);
133 }
134 nk_end(ctx);
135
136 if (nk_begin(ctx, "Disassembler", nk_rect(330, 50, 300, 200),
swissChilic51e9222020-08-07 16:09:14 -0700137 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
138 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
139 {
140 nk_layout_row_dynamic(ctx, 30, 2);
141 disas_start = nk_propertyi(ctx, "Start", 0, disas_start, 0xFFFF, 1, 20.0f);
142 disas_end = nk_propertyi(ctx, "End", 0, disas_end, 0xFFFF, 1, 20.0f);
143
swissChili1970cb82020-08-10 13:22:39 -0700144 for (uint16_t pc = disas_start; pc < disas_end;)
swissChilic51e9222020-08-07 16:09:14 -0700145 {
146 nk_layout_row_begin(ctx, NK_STATIC, 24, 2);
147
swissChili1970cb82020-08-10 13:22:39 -0700148 uint16_t cpu_pc = cpu->pc,
149 this_pc = pc;
swissChilic51e9222020-08-07 16:09:14 -0700150
151 char addr[6];
swissChili1970cb82020-08-10 13:22:39 -0700152 sprintf(addr, "$%x", pc);
swissChilic51e9222020-08-07 16:09:14 -0700153
154 nk_layout_row_push(ctx, 48);
155 nk_label(ctx, addr, NK_TEXT_LEFT);
156
157 nk_layout_row_push(ctx, 120);
swissChili1970cb82020-08-10 13:22:39 -0700158 char *line = disas_step(cpu, &pc);
159 if (cpu_pc == this_pc)
swissChilic51e9222020-08-07 16:09:14 -0700160 {
161 nk_label_colored(ctx, line, NK_TEXT_LEFT, selected);
162 }
163 else
164 {
165 nk_label(ctx, line, NK_TEXT_LEFT);
166 }
167 free(line);
168 }
swissChilic51e9222020-08-07 16:09:14 -0700169 }
170 nk_end(ctx);
171
swissChilie7ee6da2020-08-08 16:14:21 -0700172 if (nk_begin(ctx, "Stack", nk_rect(250, 250, 230, 350),
173 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
174 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
175 {
176 nk_layout_row_static(ctx, 24, 48, 2);
swissChilib04a4022020-08-09 12:51:00 -0700177 for (int i = 0xFF; i >= 0; i--)
swissChilie7ee6da2020-08-08 16:14:21 -0700178 {
179 char line[6];
180 sprintf(line, "$%x", 0x100 + i);
181 nk_label(ctx, line, NK_TEXT_LEFT);
182
183 sprintf(line, "$%x", cpu->mem[0x100 + i]);
184 if (i == cpu->regs[SP])
185 {
186 nk_label_colored(ctx, line, NK_TEXT_LEFT, selected);
187 }
188 else
189 {
190 nk_label(ctx, line, NK_TEXT_LEFT);
191 }
192 }
193 }
194 nk_end(ctx);
195
swissChilidbbd5402020-08-07 15:07:39 -0700196 if (nk_begin(ctx, "Debugger", nk_rect(50, 50, 230, 150),
197 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
198 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
199 {
200 nk_layout_row_dynamic(ctx, 30, 2);
201 nk_label(ctx, cpu->running ? "CPU Running" : "CPU Halted", NK_TEXT_LEFT);
202 if (nk_button_label(ctx, "Reset"))
203 {
204 puts("cpu reset");
swissChili1970cb82020-08-10 13:22:39 -0700205 cmd(mq, "reset");
swissChilidbbd5402020-08-07 15:07:39 -0700206 }
207
208 nk_layout_row_dynamic(ctx, 30, 2);
209 if (nk_button_label(ctx, "Step"))
210 {
211 printf("step pressed!\n");
swissChili1970cb82020-08-10 13:22:39 -0700212 cmd(mq, "step");
swissChilidbbd5402020-08-07 15:07:39 -0700213 }
214
215 if (nk_button_label(ctx, cpu_running ? "Stop" : "Run"))
216 {
swissChili1970cb82020-08-10 13:22:39 -0700217 cmd(mq, cpu_running ? "pause" : "run");
swissChilidbbd5402020-08-07 15:07:39 -0700218 cpu_running = !cpu_running;
219 puts(cpu_running ? "cpu running" : "cpu stopped");
220 }
221 }
222 nk_end(ctx);
223
224 SDL_GetWindowSize(win, &win_width, &win_height);
225 glViewport(0, 0, win_width, win_height);
226 glClear(GL_COLOR_BUFFER_BIT);
227 glClearColor(bg.r, bg.g, bg.b, bg.a);
228 nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
229 SDL_GL_SwapWindow(win);
230 }
231
232cleanup:
233 nk_sdl_shutdown();
234 SDL_GL_DeleteContext(glContext);
235 SDL_DestroyWindow(win);
236 SDL_Quit();
swissChili1970cb82020-08-10 13:22:39 -0700237
238 cmd(mq, "quit");
swissChili4bccd442020-08-11 13:55:10 -0700239
240 printf("Cleaned up GUI\n");
swissChilidbbd5402020-08-07 15:07:39 -0700241}
swissChilic6b4f7e2020-08-09 16:36:36 -0700242
243
244void start_gui(mqd_t mq, cpu_t *cpu)
245{
246 pthread_t gui_thread;
247 gui_arg_t *arg = malloc(sizeof(gui_arg_t));
248 arg->cpu = cpu;
249 arg->mq = mq;
250 pthread_create(&gui_thread, NULL, (void *(*)(void *))&gui, arg);
251}