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