tis100

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

commit c1e767700fc14fbe385a9a8d9ad0cfa4d5315c35
parent d07257c9e92e8697915aaf40070c6228a5855104
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue, 25 Jul 2023 02:52:33 +0200

Add label support to curses source view

Diffstat:
Masm.c | 6+++---
Dout | 0
Mtest/test.asm | 4+---
Mtis-curses.c | 25+++++++++++++++++--------
Mtpu.c | 11++++++++---
Mtpu.h | 3++-
6 files changed, 31 insertions(+), 18 deletions(-)

diff --git a/asm.c b/asm.c @@ -266,7 +266,7 @@ tpu_validate(struct tpu *tpu) for (i = 0; i < TPU_MAX_INST; i++) { if (tpu->insts[i].ops[0].type == OP_LABEL) { - dst = label_map_get(&tpu->labels, + dst = label_map_get(&tpu->label_map, tpu->insts[i].ops[0].val.label); if (dst == TPU_MAX_INST) die("load: tpu X%lu Y%lu, label '%s' not defined", @@ -394,9 +394,9 @@ tis_load(struct tis *tis, const char *filepath) tok_next_in(&tokenizer, TOK_NL, -1); break; case TOK_LABEL: - if (!label_map_add(&tpu->labels, + if (!label_map_add(&tpu->label_map, tokenizer.tokstr, tpu->inst_cnt)) - die("load: line %lu, duplicate label", + die("load: line %lu, duplicate label (pos)", tokenizer.lineno); break; case TOK_NL: diff --git a/out b/out diff --git a/test/test.asm b/test/test.asm @@ -6,11 +6,9 @@ tpu X1 Y1 end tpu X2 Y1 -start: mov LEFT, ACC - add 5 + add 1 mov ACC, RIGHT - jmp start end tpu X3 Y1 diff --git a/tis-curses.c b/tis-curses.c @@ -172,7 +172,8 @@ tui_draw_tpu(struct tpu *tpu) char linebuf[TPU_INPUT_COLS + 1]; struct tpu_port *port; int sx, sy, x, y, w, h; - int i, idle, min; + int off, start, inst; + int idle; attr_t attr;; attr = (tpu_sel == tpu && input_mode == TPU_NAV) ? A_BOLD : 0; @@ -183,11 +184,21 @@ tui_draw_tpu(struct tpu *tpu) WACS_D_ULCORNER, WACS_D_URCORNER, WACS_D_LLCORNER, WACS_D_LRCORNER); - min = MIN(tpu->pc, MAX(0, (int) tpu->inst_cnt - TPU_INPUT_ROWS)); - for (i = 0; i < MIN((int) tpu->inst_cnt - min, TPU_INPUT_ROWS); i++) { - asm_print_inst(linebuf, sizeof(linebuf), &tpu->insts[min + i]); - tui_draw_text(sx + 2, sy + 1 + i, i == tpu->pc ? A_STANDOUT : 0, - "%-*s", TPU_INPUT_COLS, linebuf); + if (tpu->inst_cnt > 0) { + start = MAX(0, MIN(tpu->pc - 4, (int) tpu->inst_cnt - TPU_INPUT_ROWS)); + inst = start; + for (off = 0; off < TPU_INPUT_ROWS && inst < tpu->inst_cnt; ) { + if (tpu->label_map.labels[inst]) { + tui_draw_text(sx + 2, sy + 1 + off, A_DIM, + "%s:", tpu->label_map.labels[inst]); + off++; + } + asm_print_inst(linebuf, sizeof(linebuf), &tpu->insts[inst]); + tui_draw_text(sx + 2, sy + 1 + off, + inst == tpu->pc ? A_STANDOUT : 0, + "%-*s", TPU_INPUT_COLS, linebuf); + inst++; off++; + } } x = sx + TPU_W - TPU_INFO_W; @@ -334,11 +345,9 @@ tui_seek(struct tpu *tpu, int dx, int dy) for (; link; link = link->next) { if (tpu && tpu != link->tpu) continue; - x = tpu_pos_x(link->tpu); if (minx == -1 || x < minx) minx = x; if (maxx == -1 || x > maxx) maxx = x; - y = tpu_pos_y(link->tpu); if (miny == -1 || y < miny) miny = y; if (maxy == -1 || y > maxy) maxy = y; diff --git a/tpu.c b/tpu.c @@ -62,6 +62,7 @@ djb_hash(const char *str) void label_map_init(struct label_map *map) { + memset(map->labels, 0, sizeof(char *) * TPU_MAX_INST); memset(map->buckets, 0, sizeof(void *) * LABEL_MAP_BUCKETS); } @@ -103,6 +104,8 @@ label_map_add(struct label_map *map, const char *name, size_t pc) pos = label_map_link_pos(map, name); if (*pos) return false; + if (map->labels[pc]) return false; + *pos = link = malloc(sizeof(struct label_map_link)); if (!link) die("malloc:"); link->label = strdup(name); @@ -112,6 +115,8 @@ label_map_add(struct label_map *map, const char *name, size_t pc) link->pc = pc; link->next = NULL; + map->labels[pc] = link->label; + return true; } @@ -163,7 +168,7 @@ tpu_init(struct tpu *tpu) tpu->acc = 0; tpu->bak = 0; tpu->inst_cnt = 0; - label_map_init(&tpu->labels); + label_map_init(&tpu->label_map); tpu->last = -1; tpu->io_port = -1; @@ -176,7 +181,7 @@ tpu_deinit(struct tpu *tpu) { int i; - label_map_deinit(&tpu->labels); + label_map_deinit(&tpu->label_map); for (i = 0; i < TPU_MAX_INST; i++) { if (tpu->insts[i].ops[0].type == OP_LABEL) free(tpu->insts[i].ops[0].val.label); @@ -353,7 +358,7 @@ tpu_jmp_label(struct tpu *tpu, const char *label) { size_t pc; - pc = label_map_get(&tpu->labels, label); + pc = label_map_get(&tpu->label_map, label); if (pc >= TPU_MAX_INST) abort(); tpu->pc = (uint8_t) pc; } diff --git a/tpu.h b/tpu.h @@ -46,6 +46,7 @@ struct label_map_link { }; struct label_map { + char *labels[TPU_MAX_INST]; /* borrowed from label_map_links */ struct label_map_link *buckets[LABEL_MAP_BUCKETS]; }; @@ -83,7 +84,7 @@ struct tpu { uint8_t acc, bak; uint8_t pc; - struct label_map labels; + struct label_map label_map; struct tpu_inst insts[TPU_MAX_INST]; size_t inst_cnt; };