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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
// file : ui/curses/ui.c
// project : Salis-VM
// author : Paul Oliver <contact@pauloliver.dev>
// index:
// [section] includes
// [section] macros/enums
// [section] gfx globals
// [section] tui globals
// [section] gfx functions
// [section] tui functions
// [section] main
// ----------------------------------------------------------------------------
// [section] includes
// ----------------------------------------------------------------------------
#include <assert.h>
#define NCURSES_WIDECHAR 1
#include <curses.h>
#include <locale.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <threads.h>
#include <zlib.h>
#include "arch.h"
#include "arch_spec.h"
#include "compress.h"
#include "logger.h"
#include "salis.h"
// ----------------------------------------------------------------------------
// [section] macros/enums
// ----------------------------------------------------------------------------
#define LOG_LINE_COUNT 0x400
#define NCURSES_CTRL(x) (x & 0x1f)
#define PANE_WIDTH 27
#define PANE_WIDTH_AND_MARGIN (PANE_WIDTH + 2)
#define PROC_FIELD_WIDTH 21
#define PROC_PAGE_LINES 12
#define FPS_MIN 10
#define FPS_MAX 20
enum UIPage {
PAGE_CORE,
PAGE_PROCESS,
PAGE_WORLD,
PAGE_IPC,
PAGE_LOG,
PAGE_COUNT,
};
enum UIColorPair {
PAIR_NORMAL,
PAIR_HEADER,
PAIR_LIVE_PROC,
PAIR_SELECTED_PROC,
PAIR_FREE_CELL,
PAIR_ALLOC_CELL,
PAIR_MEM_BLOCK_START,
PAIR_SELECTED_MB1,
PAIR_SELECTED_MB2,
PAIR_SELECTED_IP,
PAIR_SELECTED_SP,
PAIR_LOG_TRACE,
PAIR_LOG_INFO,
PAIR_LOG_ATTENTION,
};
enum UILogLevel {
TRACE,
INFO,
ATTENTION,
};
// ----------------------------------------------------------------------------
// [section] gfx globals
// ----------------------------------------------------------------------------
static uint64_t g_gfx_vsiz;
static uint64_t *g_gfx_inst;
static uint64_t *g_gfx_mall;
static uint64_t *g_gfx_mbst;
static uint64_t *g_gfx_mb0s;
static uint64_t *g_gfx_mb1s;
static uint64_t *g_gfx_ipas;
static uint64_t *g_gfx_spas;
static const wchar_t *g_zoomed_symbols = (
L"⠀⠁⠂⠃⠄⠅⠆⠇⡀⡁⡂⡃⡄⡅⡆⡇⠈⠉⠊⠋⠌⠍⠎⠏⡈⡉⡊⡋⡌⡍⡎⡏⠐⠑⠒⠓⠔⠕⠖⠗⡐⡑⡒⡓⡔⡕⡖⡗⠘⠙⠚⠛⠜⠝⠞⠟⡘⡙⡚⡛⡜⡝⡞⡟"
L"⠠⠡⠢⠣⠤⠥⠦⠧⡠⡡⡢⡣⡤⡥⡦⡧⠨⠩⠪⠫⠬⠭⠮⠯⡨⡩⡪⡫⡬⡭⡮⡯⠰⠱⠲⠳⠴⠵⠶⠷⡰⡱⡲⡳⡴⡵⡶⡷⠸⠹⠺⠻⠼⠽⠾⠿⡸⡹⡺⡻⡼⡽⡾⡿"
L"⢀⢁⢂⢃⢄⢅⢆⢇⣀⣁⣂⣃⣄⣅⣆⣇⢈⢉⢊⢋⢌⢍⢎⢏⣈⣉⣊⣋⣌⣍⣎⣏⢐⢑⢒⢓⢔⢕⢖⢗⣐⣑⣒⣓⣔⣕⣖⣗⢘⢙⢚⢛⢜⢝⢞⢟⣘⣙⣚⣛⣜⣝⣞⣟"
L"⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿"
);
// ----------------------------------------------------------------------------
// [section] tui globals
// ----------------------------------------------------------------------------
static bool g_exit;
static bool g_running;
static unsigned g_core;
static unsigned g_page;
static char *g_line_buff;
static bool g_proc_genes;
static uint64_t g_proc_scroll;
static uint64_t g_proc_field_scroll;
static uint64_t g_proc_gene_scroll;
static uint64_t g_proc_selected;
static uint64_t g_wrld_pos;
static uint64_t g_wrld_zoom;
static bool g_wcursor_mode;
static int g_wcursor_x;
static int g_wcursor_y;
static uint64_t g_wcursor_pointed;
static uint64_t g_log_cnt;
static unsigned g_log_ptr;
static unsigned g_log_scroll;
static enum UILogLevel g_log_levels[LOG_LINE_COUNT];
static char g_logs[LOG_LINE_COUNT][LOG_BUFFER_SIZE];
static uint64_t g_vlin;
static uint64_t g_vsiz;
static uint64_t g_vlin_rng;
static uint64_t g_vsiz_rng;
static uint64_t g_ivpt_scroll;
static uint64_t g_step_block;
static float g_steps_per_sec;
// ----------------------------------------------------------------------------
// [section] gfx functions
// ----------------------------------------------------------------------------
static void gfx_init(uint64_t vsiz) {
assert(vsiz);
g_gfx_vsiz = vsiz;
g_gfx_inst = calloc(g_gfx_vsiz, sizeof(uint64_t));
g_gfx_mall = calloc(g_gfx_vsiz, sizeof(uint64_t));
g_gfx_mbst = calloc(g_gfx_vsiz, sizeof(uint64_t));
g_gfx_mb0s = calloc(g_gfx_vsiz, sizeof(uint64_t));
g_gfx_mb1s = calloc(g_gfx_vsiz, sizeof(uint64_t));
g_gfx_ipas = calloc(g_gfx_vsiz, sizeof(uint64_t));
g_gfx_spas = calloc(g_gfx_vsiz, sizeof(uint64_t));
assert(g_gfx_inst);
assert(g_gfx_mall);
assert(g_gfx_mbst);
assert(g_gfx_mb0s);
assert(g_gfx_mb1s);
assert(g_gfx_ipas);
assert(g_gfx_spas);
}
static void gfx_free(void) {
if (g_gfx_vsiz == 0) {
return;
}
assert(g_gfx_inst);
assert(g_gfx_mall);
assert(g_gfx_mbst);
assert(g_gfx_mb0s);
assert(g_gfx_mb1s);
assert(g_gfx_ipas);
assert(g_gfx_spas);
g_gfx_vsiz = 0;
free(g_gfx_inst);
free(g_gfx_mall);
free(g_gfx_mbst);
free(g_gfx_mb0s);
free(g_gfx_mb1s);
free(g_gfx_ipas);
free(g_gfx_spas);
g_gfx_inst = NULL;
g_gfx_mall = NULL;
g_gfx_mbst = NULL;
g_gfx_mb0s = NULL;
g_gfx_mb1s = NULL;
g_gfx_ipas = NULL;
g_gfx_spas = NULL;
}
static void gfx_resize(uint64_t vsiz) {
assert(vsiz);
gfx_free();
gfx_init(vsiz);
}
static void gfx_render_inst(const struct Core *core, uint64_t pos, uint64_t zoom) {
assert(core);
for (uint64_t i = 0; i < g_gfx_vsiz; i++) {
g_gfx_inst[i] = 0;
g_gfx_mall[i] = 0;
for (uint64_t j = 0; j < zoom; j++) {
uint64_t addr = pos + (i * zoom) + j;
g_gfx_inst[i] += mvec_get_byte(core, addr);
g_gfx_mall[i] += mvec_is_alloc(core, addr) ? 1 : 0;
}
}
}
static void gfx_clear_array(uint64_t *arry) {
assert(arry);
memset(arry, 0, g_gfx_vsiz * sizeof(uint64_t));
}
#if defined(ARCH_SPEC_MVEC_LOOP)
static void gfx_accumulate_pixel(uint64_t pos, uint64_t zoom, uint64_t pixa, uint64_t *arry) {
assert(arry);
uint64_t beg_mod = pos % MVEC_SIZE;
uint64_t end_mod = beg_mod + (g_gfx_vsiz * zoom);
uint64_t pix_mod = pixa % MVEC_SIZE;
#if !defined(NDEBUG)
uint64_t inc_cnt = 0;
#endif
while (pix_mod < end_mod) {
if (pix_mod >= beg_mod && pix_mod < end_mod) {
uint64_t pixi = (pix_mod - beg_mod) / zoom;
assert(pixi < g_gfx_vsiz);
arry[pixi]++;
#if !defined(NDEBUG)
inc_cnt++;
#endif
}
pix_mod += MVEC_SIZE;
}
#if !defined(NDEBUG)
if (zoom != 1) {
assert(inc_cnt <= 2);
}
#endif
}
#else
static void gfx_accumulate_pixel(uint64_t pos, uint64_t zoom, uint64_t pixa, uint64_t *arry) {
assert(arry);
uint64_t end = pos + (g_gfx_vsiz * zoom);
if (pixa < pos || pixa >= end) {
return;
}
uint64_t pixi = (pixa - pos) / zoom;
assert(pixi < g_gfx_vsiz);
arry[pixi]++;
}
#endif
static void gfx_render_mbst(const struct Core *core, uint64_t pos, uint64_t zoom) {
assert(core);
gfx_clear_array(g_gfx_mbst);
for (uint64_t pix = core->pfst; pix <= core->plst; pix++) {
uint64_t mb0a = arch_proc_get_mb0_addr(core, pix);
uint64_t mb1a = arch_proc_get_mb1_addr(core, pix);
gfx_accumulate_pixel(pos, zoom, mb0a, g_gfx_mbst);
if (arch_proc_get_mb1_size(core, pix)) {
gfx_accumulate_pixel(pos, zoom, mb1a, g_gfx_mbst);
}
}
}
static void gfx_render_mb0s(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) {
assert(core);
gfx_clear_array(g_gfx_mb0s);
if (psel < core->pfst || psel > core->plst) {
return;
}
uint64_t mb0a = arch_proc_get_mb0_addr(core, psel);
uint64_t mb0s = arch_proc_get_mb0_size(core, psel);
for (uint64_t i = 0; i < mb0s; i++) {
gfx_accumulate_pixel(pos, zoom, mb0a + i, g_gfx_mb0s);
}
}
static void gfx_render_mb1s(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) {
assert(core);
gfx_clear_array(g_gfx_mb1s);
if (psel < core->pfst || psel > core->plst) {
return;
}
uint64_t mb1a = arch_proc_get_mb1_addr(core, psel);
uint64_t mb1s = arch_proc_get_mb1_size(core, psel);
for (uint64_t i = 0; i < mb1s; i++) {
gfx_accumulate_pixel(pos, zoom, mb1a + i, g_gfx_mb1s);
}
}
static void gfx_render_ipas(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) {
assert(core);
gfx_clear_array(g_gfx_ipas);
if (psel < core->pfst || psel > core->plst) {
return;
}
gfx_accumulate_pixel(pos, zoom, arch_proc_get_ip_addr(core, psel), g_gfx_ipas);
}
static void gfx_render_spas(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) {
assert(core);
gfx_clear_array(g_gfx_spas);
if (psel < core->pfst || psel > core->plst) {
return;
}
gfx_accumulate_pixel(pos, zoom, arch_proc_get_sp_addr(core, psel), g_gfx_spas);
}
static void gfx_render(const struct Core *core, uint64_t pos, uint64_t zoom, uint64_t psel) {
assert(core);
gfx_render_inst(core, pos, zoom);
gfx_render_mbst(core, pos, zoom);
gfx_render_mb0s(core, pos, zoom, psel);
gfx_render_mb1s(core, pos, zoom, psel);
gfx_render_ipas(core, pos, zoom, psel);
gfx_render_spas(core, pos, zoom, psel);
}
// ----------------------------------------------------------------------------
// [section] tui functions
// ----------------------------------------------------------------------------
static void tui_line_buff_free(void) {
if (g_line_buff) {
free(g_line_buff);
}
g_line_buff = NULL;
}
static void tui_line_buff_resize(void) {
tui_line_buff_free();
g_line_buff = calloc(COLS + 1, sizeof(char));
}
static void tui_line(bool clear, int line, int color, int attr, const char *format, ...) {
assert(line >= 0);
assert(format);
if (line >= LINES) {
return;
}
if (clear) {
move(line, 0);
clrtoeol();
}
va_list args;
va_start(args, format);
attron(COLOR_PAIR(color) | attr);
vsnprintf(g_line_buff, COLS, format, args);
mvprintw(line, 1, "%s", g_line_buff);
attroff(COLOR_PAIR(color) | attr);
va_end(args);
}
static void tui_clear_line(int l) {
tui_line(true, l, PAIR_NORMAL, A_NORMAL, "");
}
static void tui_field(int line, int col, int color, int attr, const char *format, ...) {
assert(line >= 0);
assert(col >= 0);
assert(format);
if (line >= LINES || col >= COLS) {
return;
}
va_list args;
va_start(args, format);
attron(COLOR_PAIR(color) | attr);
vsnprintf(g_line_buff, COLS - col, format, args);
mvprintw(line, col, "%s", g_line_buff);
attroff(COLOR_PAIR(color) | attr);
va_end(args);
}
static void tui_str_field(int l, const char *label, const char *value) {
assert(label);
assert(strlen(label) <= 4);
assert(value);
tui_line(false, l, PAIR_NORMAL, A_NORMAL, "%-4s : %18s", label, value);
}
static void tui_ulx_field(int l, const char *label, uint64_t value) {
assert(label);
assert(strlen(label) <= 4);
tui_line(false, l, PAIR_NORMAL, A_NORMAL, "%-4s : %#18lx", label, value);
}
static void tui_uld_field(int l, const char *label, uint64_t value) {
assert(label);
assert(strlen(label) <= 4);
tui_line(false, l, PAIR_NORMAL, A_NORMAL, "%-4s : %#18ld", label, value);
}
static void tui_print_core(int l) {
tui_line(false, ++l, PAIR_HEADER, A_BOLD, "CORE [%d]", g_core);
tui_ulx_field(++l, "cycl", g_cores[g_core].cycl);
tui_ulx_field(++l, "mall", g_cores[g_core].mall);
tui_ulx_field(++l, "mut0", g_cores[g_core].muta[0]);
tui_ulx_field(++l, "mut1", g_cores[g_core].muta[1]);
tui_ulx_field(++l, "mut2", g_cores[g_core].muta[2]);
tui_ulx_field(++l, "mut3", g_cores[g_core].muta[3]);
tui_ulx_field(++l, "pnum", g_cores[g_core].pnum);
tui_ulx_field(++l, "pcap", g_cores[g_core].pcap);
tui_ulx_field(++l, "pfst", g_cores[g_core].pfst);
tui_ulx_field(++l, "plst", g_cores[g_core].plst);
tui_ulx_field(++l, "pcur", g_cores[g_core].pcur);
tui_ulx_field(++l, "psli", g_cores[g_core].psli);
tui_ulx_field(++l, "ivpt", g_cores[g_core].ivpt);
}
// ----------------------------------------------------------------------------
// [section] main
// ----------------------------------------------------------------------------
int main(void) {
return 0;
}
|