tis100

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

commit 317ef5cfe36ddb9b9ab205826132ddd227a3db01
parent 756e5b07f45852fcda14b58822ca66e1c0f05d0c
Author: Louis Burda <quent.burda@gmail.com>
Date:   Wed, 26 Jul 2023 16:39:38 +0200

Improve TPU node UI + minor fixes

Diffstat:
Atest/ports.asm | 25+++++++++++++++++++++++++
Atest/ports.in | 1+
Dtest/test.asm | 20--------------------
Mtis-curses.c | 86+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
Mtpu.c | 38++++++++++++++++----------------------
5 files changed, 91 insertions(+), 79 deletions(-)

diff --git a/test/ports.asm b/test/ports.asm @@ -0,0 +1,25 @@ +stdin X1 Y1 +stdout X3 Y2 + +tpu X1 Y1 + mov UP, RIGHT +end + +tpu X2 Y1 + mov LEFT, DOWN + mov DOWN, RIGHT +end + +tpu X2 Y2 + mov ANY, ACC + mov ACC, LAST +end + +tpu X3 Y1 + mov LEFT, DOWN +end + +tpu X3 Y2 + mov UP, DOWN +end + diff --git a/test/ports.in b/test/ports.in @@ -0,0 +1 @@ +test diff --git a/test/test.asm b/test/test.asm @@ -1,20 +0,0 @@ -stdin X1 Y1 -stdout X3 Y2 - -tpu X1 Y1 - mov UP, RIGHT -end - -tpu X2 Y1 - mov LEFT, ACC - and 127 - mov ACC, RIGHT -end - -tpu X3 Y1 - mov LEFT, DOWN -end - -tpu X3 Y2 - mov UP, DOWN -end diff --git a/tis-curses.c b/tis-curses.c @@ -42,6 +42,10 @@ enum { COLOR_VAL }; +static const char *mode_repr[] = { + "IDL", "RUN", "REA", "WRI" +}; + static int scrx = 0; static int scry = 0; static int scrw = 80; @@ -60,21 +64,6 @@ static struct tpu *tpu_sel = NULL; int (*cleanup)(void) = endwin; const char *progname = "tis-curses"; -static const char * -tpu_mode_str(struct tpu *tpu) -{ - switch (tpu->status) { - case STATUS_READ: - return "=R="; - case STATUS_WRITE: - return "=W="; - case STATUS_IDLE: - return "=I="; - case STATUS_RUN: - return "=X="; - } -} - static enum tpu_port_dir key_to_dir(int key) { @@ -92,11 +81,19 @@ key_to_dir(int key) } } -static const char * -tpu_last_str(struct tpu *tpu) +static const cchar_t * +dir_to_arrow(enum tpu_port_dir dir) { - if (tpu->last < 0) return "N/A"; - return dir_reprs[tpu->last]; + switch (dir) { + case DIR_UP: + return WACS_UARROW; + case DIR_DOWN: + return WACS_DARROW; + case DIR_LEFT: + return WACS_LARROW; + case DIR_RIGHT: + return WACS_RARROW; + } } static int @@ -226,12 +223,21 @@ tui_draw_tpu(struct tpu *tpu) tui_draw_box(x, (y += TPU_INFO_H - 1), w, h, attr, WACS_D_LTEE, WACS_D_RTEE, WACS_D_LTEE, WACS_D_RTEE); tui_draw_text(x + 2, y + 1, A_BOLD, "LST"); - tui_draw_text(x + 2, y + 2, 0, "%s", tpu_last_str(tpu)); + if (tpu->last < 0) { + tui_draw_text(x + 2, y + 2, 0, "N/A"); + } else { + tui_draw_wch(x + 2, y + 2, 0, + dir_to_arrow((enum tpu_port_dir) tpu->last)); + tui_draw_wch(x + 3, y + 2, 0, + dir_to_arrow((enum tpu_port_dir) tpu->last)); + tui_draw_wch(x + 4, y + 2, 0, + dir_to_arrow((enum tpu_port_dir) tpu->last)); + } tui_draw_box(x, (y += TPU_INFO_H - 1), w, h, attr, WACS_D_LTEE, WACS_D_RTEE, WACS_D_LTEE, WACS_D_RTEE); tui_draw_text(x + 2, y + 1, A_BOLD, "MOD"); - tui_draw_text(x + 2, y + 2, 0, "%s", tpu_mode_str(tpu)); + tui_draw_text(x + 2, y + 2, 0, "%s", mode_repr[tpu->status]); tui_draw_box(x, (y += TPU_INFO_H - 1), w, h, attr, WACS_D_LTEE, WACS_D_RTEE, WACS_D_BTEE, WACS_D_LRCORNER); @@ -292,18 +298,18 @@ tui_draw_tpu(struct tpu *tpu) if (tpu->ports[DIR_DOWN].attached) { port = &tpu->ports[DIR_DOWN]; - if (port->out >= 0) + if (port->in >= 0) tui_draw_text(sx + 9, sy + TPU_H, - A_BOLD, "%03i", port->out); + A_BOLD, "%03i", port->in); if (port->type & PORT_IN) tui_draw_wch(sx + 13, sy + TPU_H, port->in >= 0 ? A_BOLD : 0, WACS_UARROW); if (port->type & PORT_OUT) tui_draw_wch(sx + 15, sy + TPU_H, port->out >= 0 ? A_BOLD : 0, WACS_DARROW); - if (port->in >= 0) + if (port->out >= 0) tui_draw_text(sx + 17, sy + TPU_H, - A_BOLD, "%03i", port->in); + A_BOLD, "%03i", port->out); } } @@ -397,13 +403,15 @@ handlekey(int key) scrx += 4; break; case 's': - if (tis.stdin_port.attached && tis.stdin_port.out < 0) { + if (tis_stdin && tis.stdin_port.attached + && tis.stdin_port.out < 0) { c = getc(tis_stdin); if (c >= 0) tis.stdin_port.out = c; } if (tis.stdout_port.attached && tis.stdout_port.in >= 0) { - putc(tis.stdout_port.in, tis_stdout); + if (tis_stdout) + putc(tis.stdout_port.in, tis_stdout); tis.stdout_port.in = -1; } @@ -450,13 +458,17 @@ reset(int ifd, int argc, char **argv, bool watch) if (inotify_add_watch(ifd, argv[1], IN_MODIFY) < 0) die("inotify_add_watch '%s':", argv[1]); - if (tis_stdin) fclose(tis_stdin); - tis_stdin = fopen(argv[2], "r"); - if (!tis_stdin) die("fopen '%s':", argv[2]); + if (argc >= 3) { + if (tis_stdin) fclose(tis_stdin); + tis_stdin = fopen(argv[2], "r"); + if (!tis_stdin) die("fopen '%s':", argv[2]); + } - if (tis_stdout) fclose(tis_stdout); - tis_stdout = fopen(argv[3], "w+"); - if (!tis_stdout) die("fopen '%s':", argv[3]); + if (argc >= 4) { + if (tis_stdout) fclose(tis_stdout); + tis_stdout = fopen(argv[3], "w+"); + if (!tis_stdout) die("fopen '%s':", argv[3]); + } if (tis.stdin_port.attached) { tpu_sel = tis.stdin_port.dst_tpu; @@ -476,8 +488,8 @@ main(int argc, char **argv) int key; int ifd; - if (argc != 4) { - fprintf(stderr, "Usage: tis-curses FILE STDIN STDOUT\n"); + if (argc < 2 || argc > 4) { + fprintf(stderr, "Usage: tis-curses FILE [STDIN] [STDOUT]\n"); exit(1); } @@ -546,8 +558,8 @@ main(int argc, char **argv) close(ifd); - fclose(tis_stdin); - fclose(tis_stdout); + if (tis_stdin) fclose(tis_stdin); + if (tis_stdout) fclose(tis_stdout); endwin(); } diff --git a/tpu.c b/tpu.c @@ -202,6 +202,8 @@ tpu_init_ports(struct tpu *tpu, struct tpu_map *map) int x, y, i; for (i = 0; i < 4; i++) { + if (tpu->ports[i].attached) continue; + switch (i) { case DIR_LEFT: x = tpu->x - 1; @@ -223,10 +225,13 @@ tpu_init_ports(struct tpu *tpu, struct tpu_map *map) neighbor = tpu_map_get(map, x, y); if (neighbor) { + odir = opposite_dir((enum tpu_port_dir) i); tpu->ports[i].attached = true; tpu->ports[i].dst_tpu = neighbor; - odir = opposite_dir((enum tpu_port_dir) i); tpu->ports[i].dst_port = &neighbor->ports[odir]; + neighbor->ports[odir].attached = true; + neighbor->ports[odir].dst_tpu = tpu; + neighbor->ports[odir].dst_port = &tpu->ports[i]; } } } @@ -356,16 +361,13 @@ tpu_jmp_label(struct tpu *tpu, const char *label) int tpu_exec_get(struct tpu *tpu, struct tpu_inst_op *op) { - uint8_t lit; int i, v; switch (op->type) { case OP_ACC: - lit = tpu->acc; - break; + return tpu->acc; case OP_NIL: - lit = 0; - break; + return 0; case OP_LEFT: case OP_RIGHT: case OP_UP: case OP_DOWN: return tpu_port_read(tpu, op_to_dir(op->type)); case OP_ANY: @@ -373,23 +375,18 @@ tpu_exec_get(struct tpu *tpu, struct tpu_inst_op *op) v = tpu_port_read(tpu, (enum tpu_port_dir) i); if (v >= 0) { tpu->last = i; - break; + return (uint8_t) v; } } - if (i == 4) return -1; - lit = (uint8_t) v; - break; + return -1; case OP_LAST: if (tpu->last < 0) return 0; return tpu_port_read(tpu, (enum tpu_port_dir) tpu->last); case OP_LIT: - lit = op->val.lit; - break; + return op->val.lit; case OP_LABEL: abort(); } - - return (int) lit; } bool @@ -400,29 +397,26 @@ tpu_exec_put(struct tpu *tpu, struct tpu_inst_op *op, uint8_t lit) switch (op->type) { case OP_ACC: tpu->acc = lit; - break; + return true; case OP_NIL: - break; + return true; case OP_LEFT: case OP_RIGHT: case OP_UP: case OP_DOWN: return tpu_port_write(tpu, op_to_dir(op->type), lit); case OP_ANY: for (i = 0; i < 4; i++) { if (tpu_port_write(tpu, (enum tpu_port_dir) i, lit)) { tpu->last = i; - break; + return true; } } - if (i == 4) return false; - break; + return false; case OP_LAST: - if (tpu->last < 0) break; + if (tpu->last < 0) return false; return tpu_port_write(tpu, (enum tpu_port_dir) tpu->last, lit); case OP_LABEL: case OP_LIT: abort(); } - - return true; } enum tpu_status