forked from floooh/v6502r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
196 lines (187 loc) · 5.32 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <time.h>
//------------------------------------------------------------------------------
// v6502r.c
// Main source file.
//------------------------------------------------------------------------------
#include "sokol_app.h"
#include "sokol_args.h"
#include "util.h"
#include "gfx.h"
#include "input.h"
#include "ui.h"
#include "ui_asm.h"
#include "pick.h"
#include "trace.h"
#include "sim.h"
#include "segdefs.h"
const char* init_src =
#if defined(CHIP_6502)
"\tlda #0\n"
"loop:\tjsr func\n"
"\tjmp loop\n"
"func:\tinx\n"
"\tdey\n"
"\tinc data\n"
"\tsec\n"
"\tadc #2\n"
"\trts\n"
"data:\tdb $40\n\n";
#elif defined(CHIP_Z80)
"\tld sp,stack\n"
"loop:\tcall func\n"
"\tjr loop\n"
"func:\tld hl,data\n"
"\tinc (hl)\n"
"\tret\n"
"data:\tdb 40h\n"
"stack:\torg 30h\n";
#endif
// timestamp in microsecs of last user activity
static uint64_t user_activity = -1ULL;
static uint64_t get_ts(void) {
struct timespec nowtime;
clock_gettime(CLOCK_REALTIME, &nowtime);
return nowtime.tv_sec * 1000000ul + nowtime.tv_nsec/1000000ul;
}
static void app_init(void) {
util_init();
gfx_init(&(gfx_desc_t){
.seg_vertices = {
[0] = SG_RANGE(seg_vertices_0),
[1] = SG_RANGE(seg_vertices_1),
#if !defined(CHIP_Z80)
[2] = SG_RANGE(seg_vertices_2),
#endif
[3] = SG_RANGE(seg_vertices_3),
[4] = SG_RANGE(seg_vertices_4),
[5] = SG_RANGE(seg_vertices_5),
},
.seg_min_x = seg_min_x,
.seg_min_y = seg_min_y,
.seg_max_x = seg_max_x,
.seg_max_y = seg_max_y,
});
input_init();
ui_init();
pick_init(&(pick_desc_t){
.seg_min_x = seg_min_x,
.seg_min_y = seg_min_y,
.seg_max_x = seg_max_x,
.seg_max_y = seg_max_y,
.layers = {
[0] = {
.verts = (const pick_vertex_t*) seg_vertices_0,
.num_verts = sizeof(seg_vertices_0) / 8,
},
[1] = {
.verts = (const pick_vertex_t*) seg_vertices_1,
.num_verts = sizeof(seg_vertices_1) / 8,
},
#if !defined(CHIP_Z80)
[2] = {
.verts = (const pick_vertex_t*) seg_vertices_2,
.num_verts = sizeof(seg_vertices_2) / 8,
},
#endif
[3] = {
.verts = (const pick_vertex_t*) seg_vertices_3,
.num_verts = sizeof(seg_vertices_3) / 8,
},
[4] = {
.verts = (const pick_vertex_t*) seg_vertices_4,
.num_verts = sizeof(seg_vertices_4) / 8,
},
[5] = {
.verts = (const pick_vertex_t*) seg_vertices_5,
.num_verts = sizeof(seg_vertices_5) / 8,
},
},
.mesh = {
.tris = (const pick_triangle_t*) pick_tris,
.num_tris = sizeof(pick_tris) / 8,
},
.grid = {
.num_cells_x = grid_cells,
.num_cells_y = grid_cells,
.cells = (const pick_cell_t*) pick_grid,
.num_cells = sizeof(pick_grid) / 8,
}
});
trace_init();
sim_init();
ui_asm_put_source("start", (range_t){ .ptr = (void*)init_src, strlen(init_src) });
ui_asm_assemble();
sim_start();
}
static int need_render(void) {
if (user_activity == -1ULL || !sim_get_paused()) return 1;
uint64_t ts = get_ts();
return (ts - user_activity < 200);
}
static void app_frame(void) {
if (!need_render()) {
sapp_input_wait();
return;
}
gfx_new_frame(sapp_widthf(), sapp_heightf());
ui_frame();
sim_frame();
if (ui_is_nodeexplorer_active()) {
ui_write_nodeexplorer_visual_state(gfx_get_nodestate_buffer());
}
else if (ui_is_diffview()) {
const ui_diffview_t diffview = ui_get_diffview();
trace_write_diff_visual_state(diffview.cycle0, diffview.cycle1, gfx_get_nodestate_buffer());
}
else {
sim_write_node_visual_state(gfx_get_nodestate_buffer());
}
const pick_result_t pick_result = pick_dopick(
input_get_mouse_pos(),
gfx_get_display_size(),
gfx_get_offset(),
gfx_get_aspect(),
gfx_get_scale()
);
for (int i = 0; i < pick_result.num_hits; i++) {
if (!sim_is_ignore_picking_highlight_node(pick_result.node_index[i])) {
gfx_highlight_node(pick_result.node_index[i]);
}
}
gfx_begin();
gfx_draw();
ui_draw();
gfx_end();
}
static void app_input(const sapp_event* ev) {
input_handle_event(ev);
user_activity = get_ts();
}
static void app_shutdown(void) {
pick_shutdown();
sim_shutdown();
trace_shutdown();
ui_shutdown();
input_shutdown();
gfx_shutdown();
util_shutdown();
}
sapp_desc sokol_main(int argc, char* argv[]) {
sargs_setup(&(sargs_desc){ .argc = argc, .argv = argv });
return (sapp_desc){
.init_cb = app_init,
.frame_cb = app_frame,
.event_cb = app_input,
.cleanup_cb = app_shutdown,
.width = 900,
.height = 700,
.window_title = WINDOW_TITLE,
.enable_clipboard = true,
.sample_count = 4,
.clipboard_size = 16*1024,
.icon = {
.sokol_default = true
}
};
}