summaryrefslogtreecommitdiff
path: root/ui/curses/ui.c
diff options
context:
space:
mode:
Diffstat (limited to 'ui/curses/ui.c')
-rw-r--r--ui/curses/ui.c1406
1 files changed, 1406 insertions, 0 deletions
diff --git a/ui/curses/ui.c b/ui/curses/ui.c
new file mode 100644
index 0000000..e2ef120
--- /dev/null
+++ b/ui/curses/ui.c
@@ -0,0 +1,1406 @@
+// 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] process page functions
+// [section] world page functions
+// [section] ipc page functions
+// [section] log page functions
+// [section] main print functions
+// [section] event functions
+// [section] main functions
+
+// ----------------------------------------------------------------------------
+// [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_inst.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_POINTERS,
+ 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_ptrs;
+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"⢠⢡⢢⢣⢤⢥⢦⢧⣠⣡⣢⣣⣤⣥⣦⣧⢨⢩⢪⢫⢬⢭⢮⢯⣨⣩⣪⣫⣬⣭⣮⣯⢰⢱⢲⢳⢴⢵⢶⢷⣰⣱⣲⣳⣴⣵⣶⣷⢸⢹⢺⢻⢼⢽⢾⢿⣸⣹⣺⣻⣼⣽⣾⣿"
+);
+
+// ----------------------------------------------------------------------------
+// [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_ptrs = 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_ptrs);
+ 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_ptrs);
+ 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_ptrs);
+ 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_ptrs = 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_inst(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_ptrs(const struct Core *core, uint64_t pos, uint64_t zoom) {
+ assert(core);
+ gfx_clear_array(g_gfx_ptrs);
+
+ for (uint64_t pix = core->pfst; pix <= core->plst; pix++) {
+ uint64_t ipa = arch_proc_get_ip_addr(core, pix);
+ uint64_t spa = arch_proc_get_sp_addr(core, pix);
+ gfx_accumulate_pixel(pos, zoom, ipa, g_gfx_ptrs);
+ gfx_accumulate_pixel(pos, zoom, spa, g_gfx_ptrs);
+ }
+}
+
+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_ptrs(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_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] process page functions
+// ----------------------------------------------------------------------------
+static int tui_proc_pair(uint64_t pix) {
+ if (pix == g_proc_selected) {
+ return PAIR_SELECTED_PROC;
+ }
+
+ if (mvec_proc_is_live(&g_cores[g_core], pix)) {
+ return PAIR_LIVE_PROC;
+ }
+
+ return PAIR_NORMAL;
+}
+
+static const char *tui_proc_state(uint64_t pix) {
+ return mvec_proc_is_live(&g_cores[g_core], pix) ? "live" : "dead";
+}
+
+static void tui_print_process_genome_header(int l) {
+ tui_line(false, l++, PAIR_NORMAL, A_NORMAL, "%s : %18s : %s", "stat", "pix", "genome");
+}
+
+static void tui_print_process_gene(int l, int gcol, uint64_t gidx, uint64_t mba, uint64_t pix, int pair) {
+ assert(gcol >= PANE_WIDTH_AND_MARGIN);
+ assert(gcol < COLS);
+ assert(mvec_proc_is_live(&g_cores[g_core], pix));
+ assert(pair == PAIR_SELECTED_MB1 || pair == PAIR_SELECTED_MB2);
+
+ const struct Core *core = &g_cores[g_core];
+ uint64_t addr = mba + gidx;
+ uint8_t inst = mvec_get_inst(core, addr);
+ wchar_t gsym[2] = { arch_inst_symbol(inst), L'\0' };
+ cchar_t cchar = { 0 };
+ int pair_cell;
+
+ if (arch_proc_get_ip_addr(core, pix) == addr) {
+ pair_cell = PAIR_SELECTED_IP;
+ } else if (arch_proc_get_sp_addr(core, pix) == addr) {
+ pair_cell = PAIR_SELECTED_SP;
+ } else {
+ pair_cell = pair;
+ }
+
+ setcchar(&cchar, gsym, 0, pair_cell, NULL);
+ mvadd_wch(l, gcol, &cchar);
+}
+
+static void tui_print_process_genes(int l, uint64_t pix) {
+ tui_line(true, l, tui_proc_pair(pix), A_NORMAL, "%s : %#18lx :", tui_proc_state(pix), pix);
+
+ if (!mvec_proc_is_live(&g_cores[g_core], pix)) {
+ return;
+ }
+
+ const struct Core *core = &g_cores[g_core];
+ int scol = PANE_WIDTH_AND_MARGIN;
+ int gcol = scol - g_proc_gene_scroll;
+ uint64_t mb0a = arch_proc_get_mb0_addr(core, pix);
+ uint64_t mb0s = arch_proc_get_mb0_size(core, pix);
+ uint64_t mb1a = arch_proc_get_mb1_addr(core, pix);
+ uint64_t mb1s = arch_proc_get_mb1_size(core, pix);
+
+ for (uint64_t gidx = 0; gidx < mb0s && gcol < COLS; ++gidx, ++gcol) {
+ if (gcol >= scol) {
+ tui_print_process_gene(l, gcol, gidx, mb0a, pix, PAIR_SELECTED_MB1);
+ }
+ }
+
+ for (uint64_t gidx = 0; gidx < mb1s && gcol < COLS; ++gidx, ++gcol) {
+ if (gcol >= scol) {
+ tui_print_process_gene(l, gcol, gidx, mb1a, pix, PAIR_SELECTED_MB2);
+ }
+ }
+
+ clrtoeol();
+}
+
+static void tui_print_process_field_header_element(int l, int fidx, const char *name) {
+ assert(fidx >= 0);
+ assert(name);
+
+ if (fidx < (int)g_proc_field_scroll) {
+ return;
+ }
+
+ int foff = fidx - g_proc_field_scroll;
+ int fcol = foff * PROC_FIELD_WIDTH + PANE_WIDTH - 1;
+ tui_field(l, fcol, PAIR_NORMAL, A_NORMAL, " : %18s", name);
+}
+
+static void tui_print_process_field_header(int l) {
+ tui_line(true, l, PAIR_NORMAL, A_NORMAL, "%s : %18s", "stat", "pix");
+ int fidx = 0;
+
+#define ARCH_SPEC_PROC_FIELD(type, name) tui_print_process_field_header_element(l, fidx++, #name);
+ ARCH_SPEC_PROC_FIELDS
+#undef ARCH_SPEC_PROC_FIELD
+}
+
+static void tui_print_process_field_element(int l, int fidx, int fclr, uint64_t field) {
+ assert(fidx >= 0);
+
+ if (fidx < (int)g_proc_field_scroll) {
+ return;
+ }
+
+ int foff = fidx - g_proc_field_scroll;
+ int fcol = foff * PROC_FIELD_WIDTH + PANE_WIDTH - 1;
+ tui_field(l, fcol, fclr, A_NORMAL, " : %#18lx", field);
+}
+
+static void tui_print_process_fields(int l, uint64_t pix) {
+ tui_line(true, l, tui_proc_pair(pix), A_NORMAL, "%s : %#18lx", tui_proc_state(pix), pix);
+ const struct Proc *proc = proc_get(&g_cores[g_core], pix);
+ int fidx = 0;
+ int fclr = tui_proc_pair(pix);
+
+#define ARCH_SPEC_PROC_FIELD(type, name) tui_print_process_field_element(l, fidx++, fclr, proc->name);
+ ARCH_SPEC_PROC_FIELDS
+#undef ARCH_SPEC_PROC_FIELD
+}
+
+static void tui_print_process(int l) {
+ l++;
+
+ tui_line(true, l++, PAIR_HEADER, A_BOLD,
+ "PROCESS [vs:%#lx | ps:%#lx | pf:%#lx | pl:%#lx | fs:%#lx | gs:%#lx]",
+ g_proc_scroll,
+ g_proc_selected,
+ g_cores[g_core].pfst,
+ g_cores[g_core].plst,
+ g_proc_field_scroll,
+ g_proc_gene_scroll
+ );
+
+ uint64_t pix = g_proc_scroll;
+
+ if (g_proc_genes) {
+ tui_print_process_genome_header(l++);
+
+ while (l < LINES) {
+ tui_print_process_genes(l++, pix++);
+ }
+
+ return;
+ } else {
+ tui_print_process_field_header(l++);
+
+ while (l < LINES) {
+ tui_print_process_fields(l++, pix++);
+ }
+ }
+}
+
+// ----------------------------------------------------------------------------
+// [section] world page functions
+// ----------------------------------------------------------------------------
+static void tui_world_resize(void) {
+ assert(g_wrld_zoom);
+ g_vlin = 0;
+ g_vsiz = 0;
+ g_vlin_rng = 0;
+ g_vsiz_rng = 0;
+
+ if (COLS > PANE_WIDTH) {
+ g_vlin = COLS - PANE_WIDTH;
+ g_vsiz = LINES * g_vlin;
+ g_vlin_rng = g_vlin * g_wrld_zoom;
+ g_vsiz_rng = g_vsiz * g_wrld_zoom;
+ gfx_resize(g_vsiz);
+ }
+}
+
+#if defined(ARCH_SPEC_MVEC_LOOP)
+static void tui_print_cell(uint64_t i, uint64_t r, uint64_t x, uint64_t y) {
+#else
+static void tui_print_cell(uint64_t i, uint64_t r, uint64_t x, uint64_t y, uint64_t a) {
+#endif
+ wchar_t inst_nstr[2] = { L'\0', L'\0' };
+ uint64_t inst_avrg = g_gfx_inst[i] / g_wrld_zoom;
+ cchar_t cchar = { 0 };
+ assert(inst_avrg < ARCH_INST_COUNT);
+
+#if !defined(ARCH_SPEC_MVEC_LOOP)
+ if (a >= MVEC_SIZE) {
+ inst_nstr[0] = L' ';
+ } else
+#endif
+ if (g_wrld_zoom == 1) {
+ inst_nstr[0] = arch_inst_symbol((uint8_t)inst_avrg);
+ } else {
+ inst_nstr[0] = g_zoomed_symbols[(uint8_t)inst_avrg];
+ }
+
+ int pair_cell;
+
+#if defined(ARCH_SPEC_MVEC_LOOP)
+ if (g_wcursor_mode && r == (uint64_t)g_wcursor_x && y == (uint64_t)g_wcursor_y) {
+#else
+ if ((g_wcursor_mode && r == (uint64_t)g_wcursor_x && y == (uint64_t)g_wcursor_y) || a >= MVEC_SIZE) {
+#endif
+ pair_cell = PAIR_NORMAL;
+ } else if (g_wcursor_mode && r == (uint64_t)g_wcursor_x && y == (uint64_t)g_wcursor_y) {
+ pair_cell = PAIR_NORMAL;
+ } else if (g_gfx_ipas[i] != 0) {
+ pair_cell = PAIR_SELECTED_IP;
+ } else if (g_gfx_spas[i] != 0) {
+ pair_cell = PAIR_SELECTED_SP;
+ } else if (g_gfx_mb0s[i] != 0) {
+ pair_cell = PAIR_SELECTED_MB1;
+ } else if (g_gfx_mb1s[i] != 0) {
+ pair_cell = PAIR_SELECTED_MB2;
+ } else if (g_gfx_ptrs[i] != 0) {
+ pair_cell = PAIR_POINTERS;
+ } else if (g_gfx_mbst[i] != 0) {
+ pair_cell = PAIR_MEM_BLOCK_START;
+ } else if (g_gfx_mall[i] != 0) {
+ pair_cell = PAIR_ALLOC_CELL;
+ } else {
+ pair_cell = PAIR_FREE_CELL;
+ }
+
+ setcchar(&cchar, inst_nstr, 0, pair_cell, NULL);
+ mvadd_wch(y, x, &cchar);
+}
+
+static void tui_print_wcursor_bar(void) {
+ tui_clear_line(LINES - 1);
+ const struct Core *core = &g_cores[g_core];
+ char cownr[PROC_FIELD_WIDTH];
+ uint64_t cpos = g_vlin * g_wcursor_y + g_wcursor_x;
+ uint64_t caddr = cpos * g_wrld_zoom + g_wrld_pos;
+ uint8_t cinst = mvec_get_inst(core, caddr);
+
+ if (caddr < MVEC_SIZE && mvec_is_alloc(core, caddr)) {
+ g_wcursor_pointed = mvec_get_owner(core, caddr);
+ snprintf(cownr, PROC_FIELD_WIDTH, "%#lx", g_wcursor_pointed);
+ } else {
+ g_wcursor_pointed = (uint64_t)(-1);
+ snprintf(cownr, PROC_FIELD_WIDTH, "-");
+ }
+
+ mvprintw(
+ LINES - 1, 1,
+ "cursor | x:%#x | y:%#x | addr:%#lx | isum:%#lx | iavr:%#lx | mall:%#lx | mbst:%#lx | mnem:<%s> | ownr:%s",
+ g_wcursor_x,
+ g_wcursor_y,
+ caddr,
+ g_gfx_inst[cpos],
+ g_gfx_inst[cpos] / g_wrld_zoom,
+ g_gfx_mall[cpos],
+ g_gfx_mbst[cpos],
+ arch_inst_mnemonic(cinst),
+ cownr
+ );
+}
+
+static void tui_print_world(int l) {
+ l++;
+ tui_line(false, l++, PAIR_HEADER, A_BOLD, "WORLD");
+ tui_ulx_field(l++, "wrlp", g_wrld_pos);
+ tui_ulx_field(l++, "wrlz", g_wrld_zoom);
+ tui_ulx_field(l++, "psel", g_proc_selected);
+ tui_ulx_field(l++, "pabs", g_proc_selected % g_cores[g_core].pcap);
+ tui_ulx_field(l++, "vrng", g_vsiz_rng);
+ tui_str_field(l++, "curs", g_wcursor_mode ? "on" : "off");
+
+ l++;
+ tui_line(false, l++, PAIR_HEADER, A_BOLD, "SELECTED");
+ const struct Proc *psel = proc_get(&g_cores[g_core], g_proc_selected);
+
+#define ARCH_SPEC_PROC_FIELD(type, name) tui_ulx_field(l++, #name, psel->name);
+ ARCH_SPEC_PROC_FIELDS
+#undef ARCH_SPEC_PROC_FIELD
+
+ if (!g_vlin) {
+ return;
+ }
+
+ gfx_render(&g_cores[g_core], g_wrld_pos, g_wrld_zoom, g_proc_selected);
+
+ if (g_wcursor_mode) {
+ int xmax = g_vlin - 1;
+ int ymax = LINES - 2;
+ g_wcursor_x = (g_wcursor_x < xmax) ? g_wcursor_x : xmax;
+ g_wcursor_y = (g_wcursor_y < ymax) ? g_wcursor_y : ymax;
+ }
+
+ for (uint64_t i = 0; i < g_vsiz; ++i) {
+ uint64_t r = i % g_vlin;
+ uint64_t x = r + PANE_WIDTH;
+ uint64_t y = i / g_vlin;
+
+#if defined(ARCH_SPEC_MVEC_LOOP)
+ tui_print_cell(i, r, x, y);
+#else
+ uint64_t a = g_wrld_pos + (i * g_wrld_zoom);
+ tui_print_cell(i, r, x, y, a);
+#endif
+ }
+
+ if (g_wcursor_mode) {
+ tui_print_wcursor_bar();
+ }
+}
+
+// ----------------------------------------------------------------------------
+// [section] ipc page functions
+// ----------------------------------------------------------------------------
+static void tui_print_ipc_field(int l, uint64_t i, int color) {
+ uint8_t iinst = g_cores[g_core].iviv[i];
+ uint64_t iaddr = g_cores[g_core].ivav[i];
+ tui_field(l, PANE_WIDTH, color, A_NORMAL, "%#18x : %#18x : %#18x", i, iinst, iaddr);
+}
+
+static void tui_print_ipc_data(void) {
+ tui_field(0, PANE_WIDTH, PAIR_NORMAL, A_NORMAL, "%18s : %18s : %18s", "ipci", "inst", "addr");
+ int l = 1 - g_ivpt_scroll;
+
+ for (uint64_t i = 0; i < SYNC_INTERVAL; ++i) {
+ if (i == g_cores[g_core].ivpt) {
+ if (l >= 1) {
+ tui_print_ipc_field(l++, i, PAIR_SELECTED_PROC);
+ }
+
+ continue;
+ }
+
+ uint8_t iinst = g_cores[g_core].iviv[i];
+
+ if ((iinst & SALIS_IPCM_FLAG) != 0) {
+ if (l >= 1) {
+ tui_print_ipc_field(l++, i, PAIR_LIVE_PROC);
+ }
+
+ continue;
+ }
+ }
+
+ for (; l < LINES; ++l) {
+ if (l >= 1) {
+ move(l, PANE_WIDTH);
+ clrtoeol();
+ }
+ }
+}
+
+static void tui_print_ipc(int l) {
+ l++;
+ const struct Core *core = &g_cores[g_core];
+ tui_line(true, l++, PAIR_HEADER, A_BOLD, "IPC [%#lx]", g_ivpt_scroll);
+ tui_ulx_field(l++, "ivpt", core->ivpt);
+ tui_ulx_field(l++, "ivpi", core->iviv[core->ivpt]);
+ tui_ulx_field(l++, "ivpa", core->ivav[core->ivpt]);
+ tui_print_ipc_data();
+}
+
+// ----------------------------------------------------------------------------
+// [section] log page functions
+// ----------------------------------------------------------------------------
+static void tui_trace_impl(const char *format, ...) {
+ assert(format);
+ (void)format;
+
+#if defined(LOG_TRACE)
+ va_list args;
+ va_start(args, format);
+ log_trace_to_buffer(g_logs[g_log_ptr], format, args);
+ va_end(args);
+ g_log_levels[g_log_ptr] = TRACE;
+ g_log_cnt++;
+ g_log_ptr = (g_log_ptr + 1) % LOG_LINE_COUNT;
+#endif
+}
+
+static void tui_info_impl(const char *format, ...) {
+ assert(format);
+ va_list args;
+ va_start(args, format);
+ log_info_to_buffer(g_logs[g_log_ptr], format, args);
+ va_end(args);
+ g_log_levels[g_log_ptr] = INFO;
+ g_log_cnt++;
+ g_log_ptr = (g_log_ptr + 1) % LOG_LINE_COUNT;
+}
+
+static void tui_attention_impl(const char *format, ...) {
+ assert(format);
+ va_list args;
+ va_start(args, format);
+ log_attention_to_buffer(g_logs[g_log_ptr], format, args);
+ va_end(args);
+ g_log_levels[g_log_ptr] = ATTENTION;
+ g_log_cnt++;
+ g_log_ptr = (g_log_ptr + 1) % LOG_LINE_COUNT;
+}
+
+static void tui_clear_log_line(int line) {
+ assert(line >= 0 && line < LINES);
+ move(line, PANE_WIDTH);
+ clrtoeol();
+}
+
+static void tui_print_log_line(unsigned lptr, int line) {
+ assert(lptr < LOG_LINE_COUNT);
+ assert(line >= 0 && line < LINES);
+ tui_clear_log_line(line);
+
+ if (strlen(g_logs[lptr])) {
+ int pair = 0;
+
+ switch (g_log_levels[lptr]) {
+ case TRACE:
+ pair = PAIR_LOG_TRACE;
+ break;
+ case INFO:
+ pair = PAIR_LOG_INFO;
+ break;
+ case ATTENTION:
+ pair = PAIR_LOG_ATTENTION;
+ break;
+ default:
+ assert(false);
+ }
+
+ tui_field(line, PANE_WIDTH_AND_MARGIN, pair, A_NORMAL, g_logs[lptr]);
+ }
+}
+
+static void tui_print_log(int l) {
+ l++;
+ tui_line(true, l++, PAIR_HEADER, A_BOLD, "LOG");
+ tui_ulx_field(l++, "lscr", g_log_scroll);
+ tui_ulx_field(l++, "lcnt", g_log_cnt);
+ tui_ulx_field(l++, "lptr", g_log_ptr);
+
+ unsigned lptr = g_log_ptr;
+ int line = LINES + g_log_scroll;
+
+ while (line) {
+ lptr = (lptr - 1 + LOG_LINE_COUNT) % LOG_LINE_COUNT;
+ line--;
+
+ if (line < LINES) {
+ tui_print_log_line(lptr, line);
+ }
+
+ if (lptr == g_log_ptr) {
+ break;
+ }
+ }
+
+ while (line) {
+ line--;
+ tui_clear_log_line(line);
+ }
+}
+
+// ----------------------------------------------------------------------------
+// [section] main print functions
+// ----------------------------------------------------------------------------
+static void tui_print(void) {
+ int l = 1;
+ tui_line(false, l++, PAIR_HEADER, A_BOLD, "SALIS [%d:%d]", g_core, CORES);
+ tui_str_field(l++, "name", NAME);
+ tui_ulx_field(l++, "seed", SEED);
+ tui_str_field(l++, "anc", ANC);
+ tui_str_field(l++, "arch", ARCH);
+ tui_ulx_field(l++, "asav", AUTOSAVE_INTERVAL);
+#if defined(ARCH_SPEC_MUTA_FLIP)
+ tui_str_field(l++, "mflp", "yes");
+#else
+ tui_str_field(l++, "mflp", "no");
+#endif
+ tui_ulx_field(l++, "mrng", MUTA_RANGE);
+ tui_ulx_field(l++, "size", MVEC_SIZE);
+#if defined(ARCH_SPEC_MVEC_LOOP)
+ tui_str_field(l++, "loop", "true");
+#else
+ tui_str_field(l++, "loop", "false");
+#endif
+ tui_ulx_field(l++, "syni", SYNC_INTERVAL);
+#if defined(DATA_PUSH_INTERVAL)
+ tui_ulx_field(l++, "dpsi", DATA_PUSH_INTERVAL);
+#endif
+ tui_ulx_field(l++, "step", g_step);
+ tui_ulx_field(l++, "sync", g_sync);
+ tui_ulx_field(l++, "step", g_step_block);
+ tui_ulx_field(l++, "stps", (uint64_t)g_steps_per_sec);
+
+ switch (g_page) {
+ case PAGE_CORE:
+ tui_print_core(l);
+ break;
+ case PAGE_PROCESS:
+ tui_print_process(l);
+ break;
+ case PAGE_WORLD:
+ tui_print_world(l);
+ break;
+ case PAGE_IPC:
+ tui_print_ipc(l);
+ break;
+ case PAGE_LOG:
+ tui_print_log(l);
+ break;
+ default:
+ break;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// [section] event functions
+// ----------------------------------------------------------------------------
+static void ev_vscroll(int ev) {
+ switch (g_page) {
+ case PAGE_PROCESS:
+ switch (ev) {
+ case 'W':
+ g_proc_scroll += (LINES > PROC_PAGE_LINES) ? LINES - PROC_PAGE_LINES : 0;
+ break;
+ case 'S':
+ g_proc_scroll -= (LINES > PROC_PAGE_LINES) ? LINES - PROC_PAGE_LINES : 0;
+ break;
+ case 'w':
+ g_proc_scroll += 1;
+ break;
+ case 's':
+ g_proc_scroll -= 1;
+ break;
+ case 'q':
+ g_proc_scroll = 0;
+ break;
+ default:
+ break;
+ }
+
+ break;
+ case PAGE_WORLD: {
+ switch (ev) {
+ case 'W':
+ g_wrld_pos += g_vsiz_rng;
+ break;
+ case 'S':
+#if defined(ARCH_SPEC_MVEC_LOOP)
+ g_wrld_pos -= g_vsiz_rng;
+#else
+ if (g_wrld_pos < g_vsiz_rng) {
+ g_wrld_pos = 0;
+ } else {
+ g_wrld_pos -= g_vsiz_rng;
+ }
+#endif
+ break;
+ case 'w':
+ g_wrld_pos += g_vlin_rng;
+ break;
+ case 's':
+#if defined(ARCH_SPEC_MVEC_LOOP)
+ g_wrld_pos -= g_vlin_rng;
+#else
+ if (g_wrld_pos < g_vlin_rng) {
+ g_wrld_pos = 0;
+ } else {
+ g_wrld_pos -= g_vlin_rng;
+ }
+#endif
+ break;
+ case 'q':
+ g_wrld_pos = 0;
+ break;
+ default:
+ break;
+ }
+
+ break;
+ }
+ case PAGE_IPC:
+ switch (ev) {
+ case 'W':
+ g_ivpt_scroll += LINES;
+ break;
+ case 'S':
+ g_ivpt_scroll -= g_ivpt_scroll < (uint64_t)LINES ? g_ivpt_scroll : (uint64_t)LINES;
+ break;
+ case 'w':
+ g_ivpt_scroll += 1;
+ break;
+ case 's':
+ g_ivpt_scroll -= g_ivpt_scroll ? 1 : 0;
+ break;
+ case 'q':
+ g_ivpt_scroll = 0;
+ break;
+ }
+
+ break;
+ case PAGE_LOG:
+ switch (ev) {
+ case 'W':
+ g_log_scroll += LINES;
+ g_log_scroll = g_log_scroll >= LOG_LINE_COUNT ? LOG_LINE_COUNT - 1 : g_log_scroll;
+ break;
+ case 'S':
+ g_log_scroll -= g_log_scroll < (uint64_t)LINES ? g_log_scroll : (uint64_t)LINES;
+ break;
+ case 'w':
+ g_log_scroll += 1;
+ g_log_scroll = g_log_scroll >= LOG_LINE_COUNT ? LOG_LINE_COUNT - 1 : g_log_scroll;
+ break;
+ case 's':
+ g_log_scroll -= g_log_scroll ? 1 : 0;
+ break;
+ case 'q':
+ g_log_scroll = 0;
+ break;
+ }
+
+ break;
+ default:
+ break;
+ }
+}
+
+static void ev_hscroll(int ev) {
+ switch (g_page) {
+ case PAGE_PROCESS: {
+ uint64_t *hs_var = g_proc_genes ? &g_proc_gene_scroll : &g_proc_field_scroll;
+
+ switch (ev) {
+ case 'A':
+ *hs_var = 0;
+ break;
+ case 'a':
+ *hs_var -= *hs_var ? 1 : 0;
+ break;
+ case 'd':
+ (*hs_var)++;
+ break;
+ default:
+ break;
+ }
+
+ break;
+ }
+
+ case PAGE_WORLD:
+ switch (ev) {
+ case 'a':
+#if defined(ARCH_SPEC_MVEC_LOOP)
+ g_wrld_pos -= g_wrld_zoom;
+#else
+ if (g_wrld_pos < g_wrld_zoom) {
+ g_wrld_pos = 0;
+ } else {
+ g_wrld_pos -= g_wrld_zoom;
+ }
+#endif
+ break;
+ case 'd':
+ g_wrld_pos += g_wrld_zoom;
+ break;
+ default:
+ break;
+ }
+
+ break;
+ default:
+ break;
+ }
+}
+
+static void ev_zoom(int ev) {
+ switch (g_page) {
+ case PAGE_WORLD:
+ switch (ev) {
+ case 'x':
+ g_wrld_zoom *= (g_vlin != 0 && g_vsiz_rng < MVEC_SIZE) ? 2 : 1;
+ tui_world_resize();
+ break;
+ case 'z':
+ g_wrld_zoom /= (g_wrld_zoom != 1) ? 2 : 1;
+ tui_world_resize();
+ break;
+ default:
+ break;
+ }
+
+ break;
+ default:
+ break;
+ }
+}
+
+static void ev_move_wcursor(int ev) {
+ switch (ev) {
+ case KEY_UP:
+ g_wcursor_y -= (g_wcursor_y != 0) ? 1 : 0;
+ break;
+ case KEY_DOWN:
+ g_wcursor_y += (g_wcursor_y < LINES - 2) ? 1 : 0;
+ break;
+ case KEY_LEFT:
+ g_wcursor_x -= (g_wcursor_x != 0) ? 1 : 0;
+ break;
+ case KEY_RIGHT:
+ g_wcursor_x += ((uint64_t)g_wcursor_x < g_vlin - 1) ? 1 : 0;
+ break;
+ default:
+ break;
+ }
+}
+
+static void ev_sel_proc(int ev) {
+ if (g_page != PAGE_PROCESS && g_page != PAGE_WORLD) {
+ return;
+ }
+
+ switch (ev) {
+ case 'o':
+ g_proc_selected -= 1;
+ break;
+ case 'p':
+ g_proc_selected += 1;
+ break;
+ case 'f':
+ g_proc_selected = g_cores[g_core].pfst;
+ break;
+ case 'l':
+ g_proc_selected = g_cores[g_core].plst;
+ break;
+ default:
+ break;
+ }
+}
+
+static void ev_goto_sel_proc(void) {
+ switch (g_page) {
+ case PAGE_PROCESS:
+ g_proc_scroll = g_proc_selected;
+ break;
+ case PAGE_WORLD:
+ g_wrld_pos = g_cores[g_core].pvec[g_proc_selected % g_cores[g_core].pcap].mb0a;
+ break;
+ default:
+ break;
+ }
+}
+
+static void ev_handle(void) {
+ int ev = getch();
+
+ if (g_page == PAGE_WORLD && g_wcursor_mode) {
+ switch (ev) {
+ case KEY_UP:
+ case KEY_DOWN:
+ case KEY_LEFT:
+ case KEY_RIGHT:
+ ev_move_wcursor(ev);
+ return;
+ case '\n':
+ if (g_wcursor_pointed != (uint64_t)(-1)) {
+ g_proc_selected = g_wcursor_pointed;
+ }
+
+ break;
+ default:
+ break;
+ }
+ }
+
+ switch (ev) {
+ case NCURSES_CTRL('c'):
+ g_exit = true;
+ break;
+ case KEY_SLEFT:
+ clear();
+ g_core = (g_core + CORES - 1) % CORES;
+ break;
+ case KEY_SRIGHT:
+ clear();
+ g_core = (g_core + 1) % CORES;
+ break;
+ case KEY_LEFT:
+ clear();
+ g_page = (g_page + PAGE_COUNT - 1) % PAGE_COUNT;
+ break;
+ case KEY_RIGHT:
+ clear();
+ g_page = (g_page + 1) % PAGE_COUNT;
+ break;
+ case KEY_RESIZE:
+ clear();
+ tui_line_buff_resize();
+ tui_world_resize();
+
+ if (g_vlin) {
+ while (g_vsiz_rng >= MVEC_SIZE * 2 && g_wrld_zoom != 1) {
+ g_wrld_zoom /= 2;
+ tui_world_resize();
+ }
+ }
+
+ g_wcursor_mode = false;
+ break;
+ case 'W':
+ case 'S':
+ case 'w':
+ case 's':
+ case 'q':
+ ev_vscroll(ev);
+ break;
+ case 'A':
+ case 'a':
+ case 'd':
+ ev_hscroll(ev);
+ break;
+ case 'z':
+ case 'x':
+ ev_zoom(ev);
+ break;
+ case 'o':
+ case 'p':
+ case 'f':
+ case 'l':
+ ev_sel_proc(ev);
+ break;
+ case 'k':
+ ev_goto_sel_proc();
+ break;
+ case 'g':
+ if (g_page == PAGE_PROCESS) {
+ clear();
+ g_proc_genes = !g_proc_genes;
+ }
+
+ break;
+ case 'c':
+ if (g_page == PAGE_WORLD) {
+ clear();
+
+ if (g_vlin == 0) {
+ g_wcursor_mode = false;
+ } else {
+ g_wcursor_mode = !g_wcursor_mode;
+ }
+ }
+
+ break;
+ case ' ':
+ g_running = !g_running;
+ g_steps_per_sec = 0.f;
+ nodelay(stdscr, g_running);
+ break;
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
+ case '0':
+ if (!g_running) {
+ uint64_t cycles = 1 << (((ev - '0') ? (ev - '0') : 10) - 1);
+ salis_step(cycles);
+ }
+
+ break;
+ default:
+ break;
+ }
+}
+
+// ----------------------------------------------------------------------------
+// [section] main functions
+// ----------------------------------------------------------------------------
+static void init(void) {
+ setlocale(LC_ALL, "");
+ initscr();
+ raw();
+ noecho();
+ curs_set(0);
+ keypad(stdscr, TRUE);
+ start_color();
+ init_color(COLOR_BLACK, 0, 0, 0);
+
+ init_pair(PAIR_NORMAL, COLOR_WHITE, COLOR_BLACK);
+ init_pair(PAIR_HEADER, COLOR_BLUE, COLOR_BLACK);
+ init_pair(PAIR_LIVE_PROC, COLOR_BLUE, COLOR_BLACK);
+ init_pair(PAIR_SELECTED_PROC, COLOR_YELLOW, COLOR_BLACK);
+ init_pair(PAIR_FREE_CELL, COLOR_BLUE, COLOR_BLACK);
+ init_pair(PAIR_ALLOC_CELL, COLOR_BLACK, COLOR_BLUE);
+ init_pair(PAIR_MEM_BLOCK_START, COLOR_BLACK, COLOR_CYAN);
+ init_pair(PAIR_POINTERS, COLOR_BLACK, COLOR_WHITE);
+ init_pair(PAIR_SELECTED_MB1, COLOR_BLACK, COLOR_YELLOW);
+ init_pair(PAIR_SELECTED_MB2, COLOR_BLACK, COLOR_GREEN);
+ init_pair(PAIR_SELECTED_IP, COLOR_BLACK, COLOR_RED);
+ init_pair(PAIR_SELECTED_SP, COLOR_BLACK, COLOR_MAGENTA);
+ init_pair(PAIR_LOG_TRACE, COLOR_BLUE, COLOR_BLACK);
+ init_pair(PAIR_LOG_INFO, COLOR_GREEN, COLOR_BLACK);
+ init_pair(PAIR_LOG_ATTENTION, COLOR_YELLOW, COLOR_BLACK);
+
+ log_trace = tui_trace_impl;
+ log_info = tui_info_impl;
+ log_attention = tui_attention_impl;
+
+#if defined(COMMAND_NEW)
+ salis_init();
+#elif defined(COMMAND_LOAD)
+ salis_load();
+#endif
+
+ g_wrld_zoom = 1;
+ g_step_block = 1;
+ tui_line_buff_resize();
+ tui_world_resize();
+}
+
+static void exec(void) {
+ struct timespec ts = {
+ .tv_sec = 0,
+ .tv_nsec = (1000000000 / 60),
+ };
+
+ while (!g_exit) {
+ if (g_running) {
+ salis_start();
+ nanosleep(&ts, NULL);
+ salis_signal_stop();
+ salis_wait();
+ }
+
+ tui_print();
+ ev_handle();
+ }
+}
+
+static void quit(void) {
+ gfx_free();
+ tui_line_buff_free();
+ salis_free();
+ endwin();
+}
+
+int main(void) {
+ init();
+ exec();
+ quit();
+ return 0;
+}