blob: a03d94871b6ae3d9b0414bfeee096bfb0a1e52b9 [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"
18
19#define WINDOW_WIDTH 720
20#define WINDOW_HEIGHT 640
21#define MAX_VERTEX_MEMORY 512 * 1024
22#define MAX_ELEMENT_MEMORY 128 * 1024
23
24void gui(cpu_t *cpu)
25{
26 SDL_Window *win;
27 SDL_GLContext glContext;
28 int win_width, win_height;
29 bool running = true;
30 bool cpu_running = false;
31
32 struct nk_context *ctx;
swissChilic51e9222020-08-07 16:09:14 -070033 struct nk_colorf bg =
34 {
35 .r = 0.29f,
36 .g = 0.28f,
37 .b = 0.50f,
38 .a = 1.0f,
39 };
40 struct nk_color selected =
41 {
42 .r = 28,
43 .g = 234,
44 .b = 79,
45 .a = 255,
46 };
47
48 uint16_t disas_start = 0,
49 disas_end = 32;
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
98 if (nk_begin(ctx, "Registers", nk_rect(50, 300, 400, 90),
99 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
swissChilic51e9222020-08-07 16:09:14 -0700110 if (nk_begin(ctx, "Disassembler", nk_rect(330, 50, 250, 200),
111 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
112 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
113 {
114 nk_layout_row_dynamic(ctx, 30, 2);
115 disas_start = nk_propertyi(ctx, "Start", 0, disas_start, 0xFFFF, 1, 20.0f);
116 disas_end = nk_propertyi(ctx, "End", 0, disas_end, 0xFFFF, 1, 20.0f);
117
118 uint16_t pc = cpu->pc;
119
120 for (cpu->pc = disas_start; cpu->pc < disas_end;)
121 {
122 nk_layout_row_begin(ctx, NK_STATIC, 24, 2);
123
124 uint16_t this_pc = cpu->pc;
125
126 char addr[6];
127 sprintf(addr, "$%x", this_pc);
128
129 nk_layout_row_push(ctx, 48);
130 nk_label(ctx, addr, NK_TEXT_LEFT);
131
132 nk_layout_row_push(ctx, 120);
133 char *line = disas_step(cpu);
134 if (pc == this_pc)
135 {
136 nk_label_colored(ctx, line, NK_TEXT_LEFT, selected);
137 }
138 else
139 {
140 nk_label(ctx, line, NK_TEXT_LEFT);
141 }
142 free(line);
143 }
144
145 cpu->pc = pc;
146 }
147 nk_end(ctx);
148
swissChilidbbd5402020-08-07 15:07:39 -0700149 if (nk_begin(ctx, "Debugger", nk_rect(50, 50, 230, 150),
150 NK_WINDOW_BORDER|NK_WINDOW_MOVABLE|NK_WINDOW_SCALABLE|
151 NK_WINDOW_MINIMIZABLE|NK_WINDOW_TITLE))
152 {
153 nk_layout_row_dynamic(ctx, 30, 2);
154 nk_label(ctx, cpu->running ? "CPU Running" : "CPU Halted", NK_TEXT_LEFT);
155 if (nk_button_label(ctx, "Reset"))
156 {
157 puts("cpu reset");
158 reset(cpu);
159 }
160
161 nk_layout_row_dynamic(ctx, 30, 2);
162 if (nk_button_label(ctx, "Step"))
163 {
164 printf("step pressed!\n");
165 step(cpu);
166 }
167
168 if (nk_button_label(ctx, cpu_running ? "Stop" : "Run"))
169 {
170 cpu_running = !cpu_running;
171 puts(cpu_running ? "cpu running" : "cpu stopped");
172 }
173 }
174 nk_end(ctx);
175
176 SDL_GetWindowSize(win, &win_width, &win_height);
177 glViewport(0, 0, win_width, win_height);
178 glClear(GL_COLOR_BUFFER_BIT);
179 glClearColor(bg.r, bg.g, bg.b, bg.a);
180 nk_sdl_render(NK_ANTI_ALIASING_ON, MAX_VERTEX_MEMORY, MAX_ELEMENT_MEMORY);
181 SDL_GL_SwapWindow(win);
182 }
183
184cleanup:
185 nk_sdl_shutdown();
186 SDL_GL_DeleteContext(glContext);
187 SDL_DestroyWindow(win);
188 SDL_Quit();
189}