diff options
author | Paul Oliver <contact@pauloliver.dev> | 2024-02-29 02:39:32 +0100 |
---|---|---|
committer | Paul Oliver <contact@pauloliver.dev> | 2024-04-16 02:28:53 +0200 |
commit | 5daf52d92c472ebf2a675cb2d27ca3e3fbdf0034 (patch) | |
tree | 36f58b9fd17f38724ff5a5263ac9a326a857f76e /src/ui/daemon.c |
Initial
Diffstat (limited to 'src/ui/daemon.c')
-rw-r--r-- | src/ui/daemon.c | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/ui/daemon.c b/src/ui/daemon.c new file mode 100644 index 0000000..f74713d --- /dev/null +++ b/src/ui/daemon.c @@ -0,0 +1,63 @@ +// Project: Salis +// Author: Paul Oliver +// Email: contact@pauloliver.dev + +/* + * Implements a minimal UI for the Salis simulator with minimal output and + * interruptible via OS signals. Ideal for running Salis in the background. + */ + +#include <signal.h> +#include <unistd.h> + +volatile bool g_running; +u64 g_step_block; + +void sig_handler(int signo) { + switch (signo) { + case SIGINT: + case SIGTERM: + printf("signal received, stopping simulator...\n"); + g_running = false; + break; + } +} + +void step_block() { + 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; + } + + printf("simulator running on step '%#lx'\n", g_steps); +} + +int main() { +#if ACTION == ACT_NEW + salis_init(); +#elif ACTION == ACT_LOAD + salis_load(); +#endif + + g_running = true; + g_step_block = 1; + + signal(SIGINT, sig_handler); + signal(SIGTERM, sig_handler); + + while (g_running) { + step_block(); + } + + salis_save(SIM_PATH); + salis_free(); + + return 0; +} |