summaryrefslogtreecommitdiffstats
path: root/tpu.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2023-07-26 16:39:38 +0200
committerLouis Burda <quent.burda@gmail.com>2023-07-26 16:52:41 +0200
commit317ef5cfe36ddb9b9ab205826132ddd227a3db01 (patch)
tree7332f964df8852ad15659d0649fc0885204d053c /tpu.c
parent756e5b07f45852fcda14b58822ca66e1c0f05d0c (diff)
downloadtis100-317ef5cfe36ddb9b9ab205826132ddd227a3db01.tar.gz
tis100-317ef5cfe36ddb9b9ab205826132ddd227a3db01.zip
Improve TPU node UI + minor fixes
Diffstat (limited to 'tpu.c')
-rw-r--r--tpu.c38
1 files changed, 16 insertions, 22 deletions
diff --git a/tpu.c b/tpu.c
index 2e91784..e827cdd 100644
--- 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