diff options
Diffstat (limited to 'ui/daemon/ui.c')
| -rw-r--r-- | ui/daemon/ui.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/ui/daemon/ui.c b/ui/daemon/ui.c new file mode 100644 index 0000000..3a4f5c2 --- /dev/null +++ b/ui/daemon/ui.c @@ -0,0 +1,104 @@ +volatile bool g_running; +uint64_t g_step_block; + +void info_impl(const char *restrict fmt, ...) { + assert(fmt); + + time_t t = time(NULL); + struct tm tm = *localtime(&t); + printf( + "\r%d-%02d-%02d %02d:%02d:%02d -- \033[1;34mINFO\033[0m ", + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec + ); + + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + + printf("\n"); +} + +void warn_impl(const char *restrict fmt, ...) { + assert(fmt); + + time_t t = time(NULL); + struct tm tm = *localtime(&t); + printf( + "\r%d-%02d-%02d %02d:%02d:%02d -- \033[1;33mWARN\033[0m ", + tm.tm_year + 1900, + tm.tm_mon + 1, + tm.tm_mday, + tm.tm_hour, + tm.tm_min, + tm.tm_sec + ); + + va_list args; + va_start(args, fmt); + vprintf(fmt, args); + va_end(args); + + printf("\n"); +} + +void sig_handler(int signo) { + (void)signo; + + if (g_running) { + g_warn("Signal received, will stop simulator soon..."); + g_running = false; + } +} + +void step_block(void) { + clock_t beg = clock(); + salis_step(g_step_block - (g_steps % g_step_block)); + clock_t end = clock(); + + if ((end - beg) < (CLOCKS_PER_SEC * 4)) { + g_step_block <<= 1; + } + + if ((end - beg) >= (CLOCKS_PER_SEC * 2) && g_step_block != 1) { + g_step_block >>= 1; + } + + float secs = (float)(end - beg) / (float)CLOCKS_PER_SEC; + float steps_per_sec = (float)g_step_block / secs; + + g_info("Simulator running on step %#lx @%.1f steps/s", g_steps, steps_per_sec); +} + +int main(void) { + g_info = info_impl; + g_warn = warn_impl; + +#if defined(COMMAND_NEW) + salis_init(); +#elif defined(COMMAND_LOAD) + salis_load(); +#endif + + g_running = true; + g_step_block = 1; + + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + + while (g_running) { + step_block(); + } + + g_info("Saving simulation..."); + salis_save(SIM_PATH); + salis_free(); + + g_info("Exiting salis..."); + return 0; +} |
