blob: cfaa0970e75a600be28a9f2770791b305165c851 [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"
swissChilibb478f12020-08-07 20:45:07 -070018#include "screen.h"
swissChilidbbd5402020-08-07 15:07:39 -070019
20#define WINDOW_WIDTH 720
21#define WINDOW_HEIGHT 640
22#define MAX_VERTEX_MEMORY 512 * 1024
23#define MAX_ELEMENT_MEMORY 128 * 1024
24
25void gui(cpu_t *cpu)
26{
27 SDL_Window *win;
28 SDL_GLContext glContext;
29 int win_width, win_height;
30 bool running = true;
31 bool cpu_running = false;
32
33 struct nk_context *ctx;
swissChilic51e9222020-08-07 16:09:14 -070034 struct nk_colorf bg =
35 {
36 .r = 0.29f,
37 .g = 0.28f,
38 .b = 0.50f,
39 .a = 1.0f,
40 };
41 struct nk_color selected =
42 {
43 .r = 28,
44 .g = 234,
45 .b = 79,
46 .a = 255,
47 };
48
swissChilibb478f12020-08-07 20:45:07 -070049 uint16_t disas_start = 0x600,
50 disas_end = 0x600 + 32;
51
52 uint8_t screen_scale = 4;
swissChilidbbd5402020-08-07 15:07:39 -070053
54 SDL_SetHint(SDL_HINT_VIDEO_HIGHDPI_DISABLED, "0");
55 SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER|SDL_INIT_EVENTS);
56 SDL_GL_SetAttribute (SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
57 SDL_GL_SetAttribute (SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
58 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
59 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
60 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
61 win = SDL_CreateWindow("6502",
62 SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
63 WINDOW_WIDTH, WINDOW_HEIGHT, SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_ALLOW_HIGHDPI);
64 glContext = SDL_GL_CreateContext(win);
65 SDL_GetWindowSize(win, &win_width, &win_height);
66
67 /* OpenGL setup */
68 glViewport(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT);
69 glewExperimental = 1;
70 if (glewInit() != GLEW_OK) {
71 fprintf(stderr, "Failed to setup GLEW\n");
72 exit(1);
73 }
74
75 ctx = nk_sdl_init(win);
76
77 struct nk_font_atlas *atlas;
78 nk_sdl_font_stash_begin(&atlas);
79 struct nk_font *font = nk_font_atlas_add_default(atlas, 16, NULL);
80 nk_sdl_font_stash_end();
81 //nk_style_load_all_cursors(ctx, atlas->cursors);
82 nk_style_set_font(ctx, &font->handle);
83
swissChilidbbd5402020-08-07 15:07:39 -070084 while (running)
85 {
86 SDL_Event evt;
87 nk_input_begin(ctx);
88 while (SDL_PollEvent(&evt))
89 {
90 if (evt.type == SDL_QUIT) goto cleanup;
91 nk_sdl_handle_event(&evt);
92 }
93 nk_input_end(ctx);
94
95 if (cpu_running && cpu->running)
96 step(cpu);
97
98 if (!cpu->running)
99 cpu_running = false;
100
swissChilibb478f12020-08-07 20:45:07 -0700101 if (nk_begin(ctx, "Registers", nk_rect(50, 300, 500, 90),
swissChilidbbd5402020-08-07 15:07:39 -0700102 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
103 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
104 {
105 nk_layout_row_dynamic(ctx, 30, 4);
swissChilidbbd5402020-08-07 15:07:39 -0700106 cpu->pc = nk_propertyi(ctx, "PC", 0, cpu->pc, 0xFFFF, 1, 20.0f);
107 cpu->regs[A] = nk_propertyi(ctx, "A", 0, cpu->regs[A], 0xFF, 1, 20.0f);
108 cpu->regs[X] = nk_propertyi(ctx, "X", 0, cpu->regs[X], 0xFF, 1, 20.0f);
109 cpu->regs[Y] = nk_propertyi(ctx, "Y", 0, cpu->regs[Y], 0xFF, 1, 20.0f);
110 }
111 nk_end(ctx);
112
swissChilibb478f12020-08-07 20:45:07 -0700113 if (nk_begin(ctx, "Screen", nk_rect(50, 400, 150, 220),
114 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
115 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
116 {
117 nk_layout_row_dynamic(ctx, 24, 1);
118 screen_scale = nk_propertyi(ctx, "Scale", 1, screen_scale, 8, 1, 1);
119
120 nk_layout_row_static(ctx, screen_scale * 32, screen_scale * 32, 1);
121 screen(ctx, cpu->mem + CPU_FB_ADDR, screen_scale);
122 }
123 nk_end(ctx);
124
125 if (nk_begin(ctx, "Disassembler", nk_rect(330, 50, 300, 200),
swissChilic51e9222020-08-07 16:09:14 -0700126 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
127 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
128 {
129 nk_layout_row_dynamic(ctx, 30, 2);
130 disas_start = nk_propertyi(ctx, "Start", 0, disas_start, 0xFFFF, 1, 20.0f);
131 disas_end = nk_propertyi(ctx, "End", 0, disas_end, 0xFFFF, 1, 20.0f);
132
133 uint16_t pc = cpu->pc;
134
135 for (cpu->pc = disas_start; cpu->pc < disas_end;)
136 {
137 nk_layout_row_begin(ctx, NK_STATIC, 24, 2);
138
139 uint16_t this_pc = cpu->pc;
140
141 char addr[6];
142 sprintf(addr, "$%x", this_pc);
143
144 nk_layout_row_push(ctx, 48);
145 nk_label(ctx, addr, NK_TEXT_LEFT);
146
147 nk_layout_row_push(ctx, 120);
148 char *line = disas_step(cpu);
149 if (pc == this_pc)
150 {
151 nk_label_colored(ctx, line, NK_TEXT_LEFT, selected);
152 }
153 else
154 {
155 nk_label(ctx, line, NK_TEXT_LEFT);
156 }
157 free(line);
158 }
159
160 cpu->pc = pc;
161 }
162 nk_end(ctx);
163
swissChilidbbd5402020-08-07 15:07:39 -0700164 if (nk_begin(ctx, "Debugger", nk_rect(50, 50, 230, 150),
165 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
166 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
167 {
168 nk_layout_row_dynamic(ctx, 30, 2);
169 nk_label(ctx, cpu->running ? "CPU Running" : "CPU Halted", NK_TEXT_LEFT);
170 if (nk_button_label(ctx, "Reset"))
171 {
172 puts("cpu reset");
173 reset(cpu);
174 }
175
176 nk_layout_row_dynamic(ctx, 30, 2);
177 if (nk_button_label(ctx, "Step"))
178 {
179 printf("step pressed!\n");
180 step(cpu);
181 }
182
183 if (nk_button_label(ctx, cpu_running ? "Stop" : "Run"))
184 {
185 cpu_running = !cpu_running;
186 puts(cpu_running ? "cpu running" : "cpu stopped");
187 }
188 }
189 nk_end(ctx);
190
191 SDL_GetWindowSize(win, &win_width, &win_height);
192 glViewport(0, 0, win_width, win_height);
193 glClear(GL_COLOR_BUFFER_BIT);
194 glClearColor(bg.r, bg.g, bg.b, bg.a);
195 nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
196 SDL_GL_SwapWindow(win);
197 }
198
199cleanup:
200 nk_sdl_shutdown();
201 SDL_GL_DeleteContext(glContext);
202 SDL_DestroyWindow(win);
203 SDL_Quit();
204}