tis100

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

tpu.h (4170B)


      1#pragma once
      2
      3#include <stdio.h>
      4#include <stdint.h>
      5#include <stdbool.h>
      6#include <stdlib.h>
      7
      8#define TPU_MAP_BUCKETS 64
      9#define LABEL_MAP_BUCKETS 64
     10#define TPU_MAX_INST_CNT 15
     11#define TPU_MAX_ROWS 15
     12#define TPU_MAX_COLS 18
     13#define TIS_MAX_IO_PORTS 36
     14
     15/* what tpu will attempt next (order important) */
     16enum tpu_mode {
     17	MODE_IDLE, MODE_RUN, MODE_READ, MODE_WRITE
     18};
     19
     20enum tpu_inst_type {
     21	INST_NOP, INST_MOV, INST_SWP, INST_SAV,
     22	INST_ADD, INST_SUB, INST_NEG, INST_JMP,
     23	INST_JEZ, INST_JNZ, INST_JGZ, INST_JLZ,
     24	INST_JRO
     25};
     26
     27enum tpu_inst_op_type {
     28	OP_ACC, OP_NIL, OP_LEFT, OP_RIGHT, OP_UP, OP_DOWN,
     29	OP_ANY, OP_LAST, OP_LIT, OP_LABEL
     30};
     31
     32enum tpu_port_dir {
     33	DIR_LEFT, DIR_RIGHT, DIR_UP, DIR_DOWN
     34};
     35
     36enum tpu_port_type {
     37	PORT_IN = 0b01, PORT_OUT = 0b10, PORT_BIDI = 0b11
     38};
     39
     40union tpu_inst_op_val {
     41	int lit;
     42	char *label;
     43};
     44
     45struct label_map_link {
     46	int pc;
     47	bool prefix;
     48	struct label_map_link *next;
     49};
     50
     51struct label {
     52	char *str;
     53	bool prefix;
     54};
     55
     56struct label_map {
     57	struct label labels[TPU_MAX_INST_CNT];
     58	struct label_map_link *buckets[LABEL_MAP_BUCKETS];
     59};
     60
     61struct tpu_inst_op {
     62	enum tpu_inst_op_type type;
     63	union tpu_inst_op_val val;
     64};
     65
     66struct tpu_inst {
     67	enum tpu_inst_type type;
     68	struct tpu_inst_op ops[2];
     69	unsigned opcnt;
     70};
     71
     72struct tpu_port {
     73	struct tpu *tpu;
     74	enum tpu_port_type type;
     75
     76	/* set only if managed by io port */
     77	struct tpu_io_port *io;
     78
     79	/* NOTE: dont access dst_port during execution,
     80	 * values are transfered in post execution phase */
     81	struct tpu_port *dst_port;
     82	bool attached;
     83
     84	int in, out;
     85	bool avail, writing;
     86
     87	bool reset_in;
     88};
     89
     90struct tpu {
     91	enum tpu_mode mode;
     92	bool idle;
     93
     94	bool disabled;
     95
     96	int x, y;
     97
     98	struct tpu_port ports[4];
     99	int io_port;
    100	int last;
    101
    102	size_t steps;
    103	size_t idle_steps;
    104
    105	int acc, bak;
    106	int pc;
    107
    108	struct label_map label_map;
    109	struct tpu_inst insts[TPU_MAX_INST_CNT];
    110	size_t inst_cnt, rows;
    111};
    112
    113struct tpu_map_link {
    114	int x, y;
    115	struct tpu *tpu;
    116	struct tpu_map_link *next;
    117};
    118
    119struct tpu_map {
    120	struct tpu_map_link *buckets[TPU_MAP_BUCKETS];
    121};
    122
    123struct tpu_io_port {
    124	struct tpu_port port;
    125	enum tpu_port_dir dir;
    126	enum tpu_port_type type;
    127	size_t io_step;
    128	FILE *file;
    129	int x, y;
    130	char c;
    131};
    132
    133struct tis {
    134	struct tpu_map tpu_map;
    135	struct tpu_io_port *in_ports[TIS_MAX_IO_PORTS];
    136	struct tpu_io_port *out_ports[TIS_MAX_IO_PORTS];
    137	size_t steps;
    138	bool show_stats;
    139	char *asm_filepath;
    140};
    141
    142struct tis_stats {
    143	size_t steps;
    144	size_t nodes;
    145	size_t insts;
    146	float idle;
    147};
    148
    149void label_map_init(struct label_map *map);
    150void label_map_deinit(struct label_map *map);
    151bool label_map_add(struct label_map *map, const char *name, int pc, bool prefix);
    152int label_map_get(struct label_map *map, const char *name);
    153
    154void tpu_port_init(struct tpu_port *port, struct tpu *tpu);
    155void tpu_port_deinit(struct tpu_port *port);
    156
    157void tpu_io_port_init(struct tpu_io_port *io_port, char c,
    158	enum tpu_port_dir dir, enum tpu_port_type type, int x, int y);
    159void tpu_io_port_deinit(struct tpu_io_port *io_port);
    160void tpu_io_port_attach(struct tpu_io_port *io_port, FILE *file);
    161
    162void tpu_init(struct tpu *tpu);
    163void tpu_deinit(struct tpu *tpu);
    164struct tpu_inst *tpu_current_inst(struct tpu *tpu);
    165void tpu_init_ports(struct tpu *tpu, struct tpu_map *map);
    166void tpu_update_ports(struct tpu *tpu);
    167bool tpu_set_inst(struct tpu *tpu, int pc, enum tpu_inst_type inst,
    168	unsigned opcnt, struct tpu_inst_op op1, struct tpu_inst_op op2);
    169struct tpu_inst *tpu_add_inst(struct tpu *tpu, enum tpu_inst_type inst,
    170	unsigned opcnt, struct tpu_inst_op op1, struct tpu_inst_op op2);
    171void tpu_clear_ports(struct tpu *tpu);
    172enum tpu_mode tpu_exec(struct tpu *tpu, struct tpu_inst *inst);
    173void tpu_step(struct tpu *tpu);
    174
    175void tpu_map_init(struct tpu_map *map);
    176void tpu_map_deinit(struct tpu_map *map);
    177bool tpu_map_add(struct tpu_map *map, struct tpu *tpu);
    178struct tpu *tpu_map_get(struct tpu_map *map, int x, int y);
    179
    180void tis_init(struct tis *tis);
    181void tis_deinit(struct tis *tis);
    182bool tis_step(struct tis *tis);
    183void tis_communicate(struct tis *tis);
    184struct tis_stats tis_gen_stats(struct tis *tis);
    185
    186extern const char *dir_reprs[];
    187extern const char *mode_reprs[];
    188extern const char *inst_reprs[];
    189extern const char *op_reprs[];