blob: f12ba7743a0d4c2301f7a420c21ccb759406262f [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 {
103 if (evt.type == SDL_QUIT) goto cleanup;
104 nk_sdl_handle_event(&evt);
105 }
106 nk_input_end(ctx);
107
swissChilidbbd5402020-08-07 15:07:39 -0700108 if (!cpu->running)
109 cpu_running = false;
110
swissChilibb478f12020-08-07 20:45:07 -0700111 if (nk_begin(ctx, "Registers", nk_rect(50, 300, 500, 90),
swissChilidbbd5402020-08-07 15:07:39 -0700112 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);
swissChili1970cb82020-08-10 13:22:39 -0700116 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);
swissChilidbbd5402020-08-07 15:07:39 -0700120 }
121 nk_end(ctx);
122
swissChilibb478f12020-08-07 20:45:07 -0700123 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);
swissChili1970cb82020-08-10 13:22:39 -0700128 screen_scale = nk_propertyi(ctx, "Scale", 1, screen_scale, 16, 1, 1);
swissChili94ba1f52020-08-08 11:39:10 -0700129
swissChilibb478f12020-08-07 20:45:07 -0700130 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),
swissChilic51e9222020-08-07 16:09:14 -0700136 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
swissChili1970cb82020-08-10 13:22:39 -0700143 for (uint16_t pc = disas_start; pc < disas_end;)
swissChilic51e9222020-08-07 16:09:14 -0700144 {
145 nk_layout_row_begin(ctx, NK_STATIC, 24, 2);
146
swissChili1970cb82020-08-10 13:22:39 -0700147 uint16_t cpu_pc = cpu->pc,
148 this_pc = pc;
swissChilic51e9222020-08-07 16:09:14 -0700149
150 char addr[6];
swissChili1970cb82020-08-10 13:22:39 -0700151 sprintf(addr, "$%x", pc);
swissChilic51e9222020-08-07 16:09:14 -0700152
153 nk_layout_row_push(ctx, 48);
154 nk_label(ctx, addr, NK_TEXT_LEFT);
155
156 nk_layout_row_push(ctx, 120);
swissChili1970cb82020-08-10 13:22:39 -0700157 char *line = disas_step(cpu, &pc);
158 if (cpu_pc == this_pc)
swissChilic51e9222020-08-07 16:09:14 -0700159 {
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 }
swissChilic51e9222020-08-07 16:09:14 -0700168 }
169 nk_end(ctx);
170
swissChilie7ee6da2020-08-08 16:14:21 -0700171 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);
swissChilib04a4022020-08-09 12:51:00 -0700176 for (int i = 0xFF; i >= 0; i--)
swissChilie7ee6da2020-08-08 16:14:21 -0700177 {
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
swissChilidbbd5402020-08-07 15:07:39 -0700195 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");
swissChili1970cb82020-08-10 13:22:39 -0700204 cmd(mq, "reset");
swissChilidbbd5402020-08-07 15:07:39 -0700205 }
206
207 nk_layout_row_dynamic(ctx, 30, 2);
208 if (nk_button_label(ctx, "Step"))
209 {
210 printf("step pressed!\n");
swissChili1970cb82020-08-10 13:22:39 -0700211 cmd(mq, "step");
swissChilidbbd5402020-08-07 15:07:39 -0700212 }
213
214 if (nk_button_label(ctx, cpu_running ? "Stop" : "Run"))
215 {
swissChili1970cb82020-08-10 13:22:39 -0700216 cmd(mq, cpu_running ? "pause" : "run");
swissChilidbbd5402020-08-07 15:07:39 -0700217 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
231cleanup:
232 nk_sdl_shutdown();
233 SDL_GL_DeleteContext(glContext);
234 SDL_DestroyWindow(win);
235 SDL_Quit();
swissChili1970cb82020-08-10 13:22:39 -0700236
237 cmd(mq, "quit");
swissChilidbbd5402020-08-07 15:07:39 -0700238}
swissChilic6b4f7e2020-08-09 16:36:36 -0700239
240
241void 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}