summaryrefslogtreecommitdiffstats
path: root/tpu.c
blob: 3fb88bc76e356e8cacdbffac593cfaf6c56ed4e8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
#include "tpu.h"
#include "util.h"

#include <stdbool.h>
#include <stddef.h>
#include <string.h>

/* NOTES:
 * do two passes (one with only movs) for 'async' ports, result is the same
 */

static enum tpu_port_dir
op_to_dir(enum tpu_inst_op_type op)
{
	return op - OP_LEFT;
}

static enum tpu_port_dir
opposite_dir(enum tpu_port_dir dir)
{
	switch (dir) {
	case DIR_UP:
		return DIR_DOWN;
	case DIR_DOWN:
		return DIR_UP;
	case DIR_LEFT:
		return DIR_RIGHT;
	case DIR_RIGHT:
		return DIR_LEFT;
	}
}

static uint32_t
djb_hash(const char *str)
{
	uint32_t hash;
	const char *c;

	hash = 5381;
	for (c = str; *c; c++)
		hash = 33 * hash + (uint8_t) *c;

	return hash;
}

void
label_map_init(struct label_map *map)
{
	memset(map->buckets, 0, sizeof(void *) * LABEL_MAP_BUCKETS);
}

void
label_map_deinit(struct label_map *map)
{
	struct label_map_link *link, *next;
	size_t i;

	for (i = 0; i < LABEL_MAP_BUCKETS; i++) {
		link = map->buckets[i];
		while (link) {
			next = link->next;
			free(link->label);
			free(link);
			link = next;
		}
	}
}

static struct label_map_link **
label_map_link_pos(struct label_map *map, const char *name)
{
	struct label_map_link **link;

	link = &map->buckets[djb_hash(name) % LABEL_MAP_BUCKETS];
	while (*link && strcmp((*link)->label, name))
		link = &(*link)->next;

	return link;
}

bool
label_map_add(struct label_map *map, const char *name, size_t pc)
{
	struct label_map_link **pos, *link;

	pos = label_map_link_pos(map, name);
	if (*pos) return false;

	*pos = link = malloc(sizeof(struct label_map_link));
	if (!link) die("malloc:");
	link->label = strdup(name);
	if (!link->label) die("strdup:");
	link->pc = pc;
	link->next = NULL;

	return true;
}

size_t
label_map_get(struct label_map *map, const char *name)
{
	struct label_map_link **link;

	link = label_map_link_pos(map, name);
	if (!*link) return TPU_MAX_INST;

	return (*link)->pc;
}

void
tpu_init(struct tpu *tpu)
{
	size_t i;

	tpu->x = 0;
	tpu->y = 0;
	tpu->last = -1;
	tpu->acc = 0;
	tpu->bak = 0;
	tpu->inst_cnt = 0;
	tpu->pc = 0;
	label_map_init(&tpu->labels);
	for (i = 0; i < 4; i++) {
		tpu->ports[i].attached = false;
		tpu->ports[i].dst_port = NULL;
		tpu->ports[i].dst_tpu = NULL;
		tpu->ports[i].read = false;
		tpu->ports[i].write = false;
		tpu->ports[i].type = PORT_BIDI;
		tpu->ports[i].val = 0;
	}
}

void
tpu_deinit(struct tpu *tpu)
{
	int i;

	label_map_deinit(&tpu->labels);
	for (i = 0; i < TPU_MAX_INST; i++) {
		if (tpu->insts[i].ops[0].type == OP_LABEL)
			free(tpu->insts[i].ops[0].label);
	}
}

struct tpu_inst *
tpu_current_inst(struct tpu *tpu)
{
	if (tpu->pc >= tpu->inst_cnt)
		return NULL;
	return &tpu->insts[tpu->pc];
}

void
tpu_init_ports(struct tpu *tpu, struct tpu_map *map)
{
	struct tpu *neighbor;
	enum tpu_port_dir odir;
	size_t x, y;
	int i;

	for (i = 0; i < 4; i++) {
		switch (i) {
		case DIR_LEFT:
			x = tpu->x - 1;
			y = tpu->y;
			break;
		case DIR_RIGHT:
			x = tpu->x + 1;
			y = tpu->y;
			break;
		case DIR_UP:
			x = tpu->x;
			y = tpu->y - 1;
			break;
		case DIR_DOWN:
			x = tpu->x;
			y = tpu->y + 1;
			break;
		}

		neighbor = tpu_map_get(map, x, y);
		if (neighbor) {
			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];
		}
	}
}

bool
tpu_add_inst(struct tpu *tpu, enum tpu_inst_type inst_type,
	int op1, uint8_t op1_lit, int op2, uint8_t op2_lit)
{
	struct tpu_inst *inst;

	inst = calloc(1, sizeof(struct tpu_inst));
	if (!inst) die("malloc:");
	inst->type = inst_type;

	if (op2 > 0) {
		inst->ops[1].type = (enum tpu_inst_op_type) op2;
		inst->ops[1].lit = op2_lit;
		inst->ops[0].type = (enum tpu_inst_op_type) op1;
		inst->ops[0].lit = op1_lit;
		inst->opcnt = 2;
	} else if (op1 > 0) {
		inst->ops[0].type = (enum tpu_inst_op_type) op1;
		inst->ops[0].lit = op1_lit;
		inst->opcnt = 1;
	} else {
		inst->opcnt = 0;
	}

	switch (inst->type) {
	case INST_NOP: case INST_SAV:
	case INST_SWP: case INST_NEG:
		return inst->opcnt == 0;
	case INST_ADD: case INST_SUB:
		return inst->opcnt == 1;
	case INST_JMP: case INST_JEZ: case INST_JNZ:
	case INST_JGZ: case INST_JLZ: case INST_JRO:
		if (inst->opcnt != 1) return false;
		if (inst->ops[0].type != OP_LABEL) return false;
		break;
	case INST_MOV:
		if (inst->opcnt != 2) return false;
		if (inst->ops[1].type == OP_LIT) return false;
		break;
	}

	return true;
}

void
tpu_ports_clear(struct tpu *tpu)
{
	int i;

	for (i = 0; i < 4; i++) {
		tpu->ports[i].read = false;
		tpu->ports[i].write = false;
	}
}

int
tpu_exec_get(struct tpu *tpu, struct tpu_inst_op *op)
{
	struct tpu_port *port;
	uint8_t lit;
	int i;

	switch (op->type) {
	case OP_ACC:
		lit = tpu->acc;
		break;
	case OP_BAK:
		lit = tpu->bak;
		break;
	case OP_NIL:
		lit = 0;
		break;
	case OP_LEFT: case OP_RIGHT: case OP_UP: case OP_DOWN:
		port = &tpu->ports[op_to_dir(op->type)];
		port->read = true;
		if (!port->attached || !port->dst_port->write)
			return -1;
		tpu->last = (int) op_to_dir(op->type);
		lit = port->dst_port->val;
		break;
	case OP_ANY:
		for (i = 0; i < 4; i++) {
			port = &tpu->ports[i];
			port->read = true;
			if (port->attached && port->dst_port->write) {
				tpu->last = i;
				lit = port->dst_port->val;
				break;
			}
		}
		if (i == 4) return -1;
		break;
	case OP_LAST:
		if (tpu->last < 0) return 0;
		port = &tpu->ports[tpu->last];
		port->read = true;
		if (!port->attached || !port->dst_port->write)
			return -1;
		lit = port->dst_port->val;
		break;
	case OP_LIT:
		lit = op->lit;
		break;
	default:
		abort();
	}

	tpu_ports_clear(tpu);

	return (int) lit;
}

bool
tpu_exec_put(struct tpu *tpu, struct tpu_inst_op *op, uint8_t lit)
{
	struct tpu_port *port;
	int i;

	switch (op->type) {
	case OP_BAK:
		tpu->bak = lit;
		break;
	case OP_NIL:
		break;
	case OP_LEFT: case OP_RIGHT: case OP_UP: case OP_DOWN:
		port = &tpu->ports[op_to_dir(op->type)];
		port->write = true;
		if (!port->attached || !port->dst_port->write)
			return false;
		lit = port->dst_port->val;
		tpu->last = (int) op_to_dir(op->type);
		break;
	case OP_ANY:
		for (i = 0; i < 4; i++) {
			port = &tpu->ports[i];
			port->write = true;
			if (port->attached && port->dst_port->write) {
				port->dst_port->val = lit;
				tpu->last = i;
				break;
			}
		}
		if (i == 4) return false;
		break;
	case OP_LAST:
		if (tpu->last < 0) break;
		port = &tpu->ports[tpu->last];
		port->write = true;
		if (!port->attached || !port->dst_port->write)
			return false;
		break;
	default:
		abort();
	}

	tpu_ports_clear(tpu);

	return true;
}

enum tpu_status
tpu_exec_mov(struct tpu *tpu, struct tpu_inst *inst)
{
	int lit;

	if (inst->type != INST_MOV) abort();

	lit = tpu_exec_get(tpu, &inst->ops[0]);
	if (lit < 0) return STATUS_READ;

	if (!tpu_exec_put(tpu, &inst->ops[0], (uint8_t) lit))
		return STATUS_WRITE;

	tpu->pc += 1;
	return STATUS_RUN;
}

enum tpu_status
tpu_exec(struct tpu *tpu, struct tpu_inst *inst)
{
	uint8_t lit;
	int val;

	switch (inst->type) {
	case INST_NOP:
		tpu->pc += 1;
		return STATUS_RUN;
	case INST_MOV:
		return tpu_exec_mov(tpu, inst);
	case INST_SWP:
		lit = tpu->acc;
		tpu->acc = tpu->bak;
		tpu->bak = lit;
		tpu->pc += 1;
		return STATUS_RUN;
	case INST_SAV:
		tpu->bak = tpu->acc;
		tpu->pc += 1;
		return STATUS_RUN;
	case INST_ADD:
		val = tpu_exec_get(tpu, &inst->ops[0]);
		if (val < 0) return STATUS_READ;
		tpu->acc += (uint8_t) val;
		tpu->pc += 1;
		return STATUS_RUN;
	case INST_SUB:
		val = tpu_exec_get(tpu, &inst->ops[0]);
		if (val < 0) return STATUS_READ;
		tpu->acc -= (uint8_t) val;
		tpu->pc += 1;
		return STATUS_RUN;
	case INST_NEG:
		tpu->acc = -tpu->acc;
		tpu->pc += 1;
		return STATUS_RUN;
	case INST_JMP: 
		tpu->pc = label_map_get(&tpu->labels, inst->ops[0].label);
		if (tpu->pc >= TPU_MAX_INST) abort();
		return STATUS_RUN;
	case INST_JEZ:
		if (tpu->acc == 0) {
			tpu->pc = label_map_get(&tpu->labels,
				inst->ops[0].label);
			if (tpu->pc >= TPU_MAX_INST) abort();
		} else {
			tpu->pc += 1;
		}
		return STATUS_RUN;
	case INST_JNZ:
		if (tpu->acc != 0) {
			tpu->pc = label_map_get(&tpu->labels,
				inst->ops[0].label);
			if (tpu->pc >= TPU_MAX_INST) abort();
		} else {
			tpu->pc += 1;
		}
		return STATUS_RUN;
	case INST_JGZ:
		if (tpu->acc > 0) {
			tpu->pc = label_map_get(&tpu->labels,
				inst->ops[0].label);
			if (tpu->pc >= TPU_MAX_INST) abort();
		} else {
			tpu->pc += 1;
		}
		return STATUS_RUN;
	case INST_JLZ:
		if (tpu->acc < 0) {
			tpu->pc = label_map_get(&tpu->labels,
				inst->ops[0].label);
			if (tpu->pc >= TPU_MAX_INST) abort();
		} else {
			tpu->pc += 1;
		}
		return STATUS_RUN;
	case INST_JRO:
		tpu->pc += inst->ops[0].lit;
		if (tpu->pc >= tpu->inst_cnt) tpu->pc = 0;
		return STATUS_RUN;
	default:
		abort();
	}
}

void
tpu_step(struct tpu *tpu)
{
	if (tpu->pc >= tpu->inst_cnt) return;
	tpu_exec(tpu, &tpu->insts[tpu->pc]);
	if (tpu->pc >= tpu->inst_cnt) tpu->pc = 0;
}

void
tpu_map_init(struct tpu_map *map)
{
	memset(map->buckets, 0, sizeof(void *) * TPU_MAP_BUCKETS);
}

void
tpu_map_deinit(struct tpu_map *map)
{
	struct tpu_map_link *link, *next;
	size_t i;

	for (i = 0; i < TPU_MAP_BUCKETS; i++) {
		link = map->buckets[i];
		while (link) {
			next = link->next;
			tpu_deinit(link->tpu);
			free(link->tpu);
			free(link);
			link = next;
		}
	}
}

static struct tpu_map_link **
tpu_map_link_pos(struct tpu_map *map, size_t x, size_t y)
{
	struct tpu_map_link **link;
	size_t i;

	i = (x + y) % TPU_MAP_BUCKETS;
	link = &map->buckets[i];
	while (*link && !((*link)->x == x && (*link)->y == y))
		link = &(*link)->next;

	return link;
}

void
tpu_map_add(struct tpu_map *map, struct tpu *tpu)
{
	struct tpu_map_link **pos, *link;

	pos = tpu_map_link_pos(map, tpu->x, tpu->y);
	*pos = link = malloc(sizeof(struct tpu_map_link));
	if (!link) die("malloc:");
	link->tpu = tpu;
	link->x = tpu->x;
	link->y = tpu->y;
	link->next = NULL;
}

struct tpu *
tpu_map_get(struct tpu_map *map, size_t x, size_t y)
{
	struct tpu_map_link **link;

	if (x < 0 || y < 0) return NULL;

	link = tpu_map_link_pos(map, x, y);
	if (!*link) return NULL;

	return (*link)->tpu;
}