tis100

Reimplementation of Zachtronics TIS-100 as a TUI game
git clone https://git.sinitax.com/sinitax/tis100
Log | Files | Refs | sfeed.txt

tis100.c (1234B)


      1#include "asm.h"
      2#include "tpu.h"
      3#include "util.h"
      4
      5#include <signal.h>
      6#include <stdarg.h>
      7#include <stdio.h>
      8#include <string.h>
      9#include <stdint.h>
     10#include <stdbool.h>
     11#include <stdlib.h>
     12
     13static struct tis tis;
     14
     15int (*cleanup)(void) = NULL;
     16const char *progname = "tis100";
     17
     18static bool
     19io_active(struct tis *tis)
     20{
     21	struct tpu_io_port *io_port;
     22	int i;
     23
     24	for (i = 0; i < TIS_MAX_IO_PORTS; i++) {
     25		io_port = tis->in_ports[i];
     26		if (!io_port || !io_port->file) continue;
     27		if (io_port->port.attached && !io_port->port.writing
     28				&& !feof(io_port->file))
     29			return true;
     30	}
     31
     32	return false;
     33}
     34
     35int
     36main(int argc, const char **argv)
     37{
     38	struct tis_stats stats;
     39	bool idle, prev_idle;
     40
     41	if (argc < 2) {
     42		fprintf(stderr, "Usage: tis100 FILE [IO..]\n");
     43		exit(1);
     44	}
     45
     46	tis_init(&tis);
     47
     48	tis_load(&tis, argv);
     49
     50	idle = false;
     51	while (!idle || !prev_idle || io_active(&tis)) {
     52		prev_idle = idle;
     53		idle = !tis_step(&tis);
     54	}
     55
     56	if (tis.show_stats) {
     57		stats = tis_gen_stats(&tis);
     58		printf("=== stats ===\n");
     59		printf(" cycles: %zu\n", stats.steps);
     60		printf(" nodes: %zu\n", stats.nodes);
     61		printf(" insts: %zu\n", stats.insts);
     62		printf(" idle: %2.1f%%\n", stats.idle * 100);
     63		printf("=============\n");
     64	}
     65
     66	tis_deinit(&tis);
     67}
     68