-
Notifications
You must be signed in to change notification settings - Fork 1
/
sim.c
1098 lines (901 loc) · 24.7 KB
/
sim.c
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* TIS-100 simulator - hthh, 2015
Work in progress. Seems to run most programs accurately at the moment.
Intended to be 100% compatible. This is intended as a fast cycle counter
to augment the TIS-100 game, and make it easier to experiment with
optimizations, or automate solution verification. It is not intended as
a game.
Define "SINGLE_STEP" for some terrible debug output.
(This might be vulnerable to malicious input, I'm not sure.)
License:
This software is dedicated to the public domain by its author.
*/
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
#include <assert.h>
#define TIMEOUT_CYCLES 20000000
#define LINES_PER_NODE 15
#define STACK_NODE_SIZE 15
#define LINE_LENGTH 18
#define TOKENS_PER_LINE 4
#define MAX_USER_NODES 12
#define MAX_IN_NODES 4
#define MAX_OUT_NODES 4
#define MAX_STACK_NODES 2
#define MAX_IMAGE_NODES 1
#define MAX_SAVE_LINES ((LINES_PER_NODE + 2) * MAX_USER_NODES)
#define ARENA_WIDTH 4
#define ARENA_HEIGHT 5
#define ARENA_SIZE (ARENA_WIDTH * ARENA_HEIGHT)
#define ARENA_I(x,y) ((y)*ARENA_WIDTH+(x))
#define IMAGE_WIDTH 30
#define IMAGE_HEIGHT 18
#define IMAGE_SIZE (IMAGE_WIDTH * IMAGE_HEIGHT)
#define ARRAY_LENGTH(x) (sizeof(x)/sizeof((x)[0]))
//#define SINGLE_STEP /* display terrible debug output */
//#define DRAW_IMAGE /* display image when updated */
#define OUTPUT_OUT_NODES /* display output values */
enum opcode_numbers {
OP_NONE = 0, OP_NOP, OP_SWP, OP_SAV, OP_HCF, OP_NEG, OP_ADD, OP_SUB,
OP_JRO, OP_MOV, OP_JMP, OP_JNZ, OP_JEZ, OP_JGZ, OP_JLZ,
};
enum register_numbers {
R_NONE = 0, R_LEFT, R_RIGHT, R_UP, R_DOWN, R_ACC, R_NIL, R_ANY, R_LAST,
};
enum direction_numbers {
D_LEFT, D_RIGHT, D_UP, D_DOWN,
NUM_DIRECTIONS = 4,
};
enum write_states {
WS_RUNNING, WS_WILL_BE_READABLE, WS_READABLE, WS_WILL_BE_RUNNING,
};
enum image_states {
IS_READ_X, IS_READ_Y, IS_READ_PIXELS, IS_COMPLETED,
};
enum step_stages {
S_RUN, S_COMMIT,
};
enum node_types {
N_NONE = 0, N_IN, N_OUT, N_USER, N_STACK, N_IMAGE,
};
typedef unsigned char uint8;
typedef char token[LINE_LENGTH+1];
struct tokens {
int num;
token v[TOKENS_PER_LINE];
};
struct line {
uint8 opcode;
uint8 sreg; // R_NONE if immediate
uint8 dreg; // or jump line number
short immediate;
};
struct prelink_line {
struct line l;
int postlink_label_index;
char label[LINE_LENGTH];
char target[LINE_LENGTH];
};
struct base_node {
uint8 type;
// node output
uint8 write_state;
uint8 write_bits; // directions output is available
int write_value;
struct base_node *neighbors[NUM_DIRECTIONS];
};
struct user_node {
struct base_node b;
struct line lines[LINES_PER_NODE];
int acc;
int bak;
uint8 ip;
uint8 last;
uint8 length;
};
struct in_node {
struct base_node b;
int *values;
int num_values;
int i;
};
struct out_node {
struct base_node b;
struct arena *arena; // to signal completion
int *values;
int num_values;
int i;
const char *output_prefix;
};
struct image_node {
struct base_node b;
struct arena *arena; // to signal completion
uint8 *solution;
int wrong_pixels;
int state;
uint8 cursor_x, cursor_y;
uint8 display[IMAGE_SIZE];
};
struct stack_node {
struct base_node b;
int used;
int values[STACK_NODE_SIZE];
};
// full state
struct arena {
int completed; // number of completed (correct) output nodes
int error; // flag indicating incorrect output
int user_node_count;
int in_node_count;
int out_node_count;
int stack_node_count;
int image_node_count;
struct in_node in_nodes[MAX_IN_NODES];
struct out_node out_nodes[MAX_OUT_NODES];
struct user_node user_nodes[MAX_USER_NODES];
struct stack_node stack_nodes[MAX_STACK_NODES];
struct image_node image_nodes[MAX_IMAGE_NODES];
struct base_node *nodes[ARENA_SIZE];
};
static int direction_opposite(int d) {
return d ^ 1; // heh :)
}
static int op_num_arguments(int opcode_number) {
if (opcode_number == OP_MOV)
return 2;
else if (opcode_number <= OP_NEG)
return 0;
return 1;
}
static int op_has_src(int opcode_number) {
return (opcode_number >= OP_ADD && opcode_number <= OP_MOV);
}
static int op_operand_is_label(int opcode_number) {
return (opcode_number >= OP_JMP && opcode_number <= OP_JLZ);
}
// this is the worst decision i've made all week
static int register_to_direction(int regnum) {
assert(regnum >= R_LEFT && regnum <= R_DOWN);
return regnum - R_LEFT;
}
static int parse_register(char *c) {
if (!strcmp(c, "ACC")) return R_ACC;
if (!strcmp(c, "ANY")) return R_ANY;
if (!strcmp(c, "NIL")) return R_NIL;
if (!strcmp(c, "LEFT")) return R_LEFT;
if (!strcmp(c, "RIGHT")) return R_RIGHT;
if (!strcmp(c, "UP")) return R_UP;
if (!strcmp(c, "DOWN")) return R_DOWN;
if (!strcmp(c, "LAST")) return R_LAST;
return R_NONE;
}
static int parse_opcode(char *name) {
int len = strlen(name);
if (len != 3)
return OP_NONE; // invalid op
// NB: this is little-endian/unaligned only
switch (*(int*)name) {
case 0x504F4E: return OP_NOP;
case 0x505753: return OP_SWP;
case 0x564153: return OP_SAV;
case 0x464348: return OP_HCF;
case 0x47454E: return OP_NEG;
case 0x444441: return OP_ADD;
case 0x425553: return OP_SUB;
case 0x4F524A: return OP_JRO;
case 0x564F4D: return OP_MOV;
case 0x504D4A: return OP_JMP;
case 0x5A4E4A: return OP_JNZ;
case 0x5A454A: return OP_JEZ;
case 0x5A474A: return OP_JGZ;
case 0x5A4C4A: return OP_JLZ;
default: return OP_NONE;
}
}
static char *get_op_name(int opcode) {
switch (opcode) {
case OP_NONE: return "<none>";
case OP_NOP: return "NOP";
case OP_SWP: return "SWP";
case OP_SAV: return "SAV";
case OP_HCF: return "HCF";
case OP_NEG: return "NEG";
case OP_ADD: return "ADD";
case OP_SUB: return "SUB";
case OP_JRO: return "JRO";
case OP_MOV: return "MOV";
case OP_JMP: return "JMP";
case OP_JNZ: return "JNZ";
case OP_JEZ: return "JEZ";
case OP_JGZ: return "JGZ";
case OP_JLZ: return "JLZ";
}
assert(0);
}
static char *get_reg_name(int r) {
switch (r) {
case R_NONE: return "<none>";
case R_ACC: return "ACC";
case R_NIL: return "NIL";
case R_LEFT: return "LEFT";
case R_RIGHT: return "RIGHT";
case R_UP: return "UP";
case R_DOWN: return "DOWN";
case R_ANY: return "ANY";
case R_LAST: return "LAST";
}
assert(0);
}
// utils
static int clamp(int min, int v, int max) {
if (v < min) return min;
if (v > max) return max;
return v;
}
static short tis100_clamp(int v) {
return clamp(-999, v, 999);
}
static int is_tis100_separator(char c) {
// amusingly, in TIS-100, commas are optional and '!' is whitespace
return (c == ' ' || c == ',' || c == '!');
}
static int is_tis100_terminator(char c) {
return (c == '\0' || c == '#' || c == '\r' || c == '\n');
}
// tokenizing
static int tokenize_line(char *line, struct tokens *t) {
char *p = line, *e;
char *o;
assert(strlen(line) <= LINE_LENGTH);
t->num = 0;
// skip leading whitespace
while (*p == ' ' || *p == '!')
p++;
// this handles labels. it is separate from the loop because labels can
// contain commas. (such labels are useless, but can exist in valid code)
for (e = p; *e != ' ' && *e != '!' && !is_tis100_terminator(*e);) {
if (*e++ == ':') {
o = t->v[t->num++];
memcpy(o, p, e-p);
o[e-p] = '\0';
p = e;
break;
}
}
while (!is_tis100_terminator(*p)) {
while (is_tis100_separator(*p))
p++;
if (is_tis100_terminator(*p))
return 1;
if (t->num >= TOKENS_PER_LINE) {
fprintf(stderr, "error: too many tokens to parse line\n");
return 0;
}
o = t->v[t->num++];
while (!is_tis100_terminator(*p) && !is_tis100_separator(*p))
*o++ = *p++;
*o++ = '\0';
}
return 1;
}
// parsing
static int parse_line(char *line, struct prelink_line *out) {
struct tokens tokens;
if (!tokenize_line(line, &tokens))
return 0;
memset(out, 0, sizeof *out);
if (tokens.num == 0)
return 1; // successful parse
int token_index = 0;
char *first = tokens.v[0];
int len = strlen(first);
if (first[len-1] == ':') {
if (len == 1) {
fprintf(stderr, "error: empty label is invalid\n");
return 0;
}
first[len-1] = '\0';
strcpy(out->label, first);
token_index = 1;
}
if (token_index < tokens.num) {
char *op = tokens.v[token_index++];
out->l.opcode = parse_opcode(op);
if (!out->l.opcode) {
fprintf(stderr, "error: invalid opcode '%s'\n", op);
return 0;
}
int num_operands = op_num_arguments(out->l.opcode);
if (num_operands != tokens.num - token_index) {
fprintf(stderr, "error: '%s' expects %d operands\n", op, num_operands);
return 0;
}
if (num_operands) {
if (op_operand_is_label(out->l.opcode)) {
strcpy(out->target, tokens.v[token_index]);
} else {
// validate src
char *src = tokens.v[token_index++];
out->l.sreg = parse_register(src);
if (out->l.sreg == R_NONE) {
char *e = NULL;
long long v = strtoll(src, &e, 10);
if (*src == '+' || *e || v < INT_MIN || v > INT_MAX) {
fprintf(stderr, "error: invalid operand '%s'\n", src);
return 0;
}
out->l.immediate = tis100_clamp(v);
}
if (num_operands == 2) {
char *dst = tokens.v[token_index];
out->l.dreg = parse_register(dst);
if (out->l.dreg == R_NONE) {
fprintf(stderr, "error: invalid register '%s'\n", dst);
return 0;
}
}
}
}
}
return 1;
}
// link labels and initialize user nodes
static int link_node(int count, struct prelink_line *line, struct user_node *node) {
struct line *out = node->lines;
int i, j, k;
// quadratic time techniques because n < 16
for (i = 1; i < count; i++) {
if (line[i].label[0]) {
for (j = 0; j < i; j++) {
if (!strcmp(line[i].label, line[j].label)) {
fprintf(stderr, "error: duplicate label '%s'\n", line[i].label);
return 0;
}
}
}
}
// work out final label addresses and final node length
for (i = 0, j = 0; i < count; i++) {
if (line[i].label[0])
line[i].postlink_label_index = j;
if (line[i].l.opcode)
j++;
}
node->length = j;
for (i = 0, j = 0; i < count; i++) {
if (line[i].l.opcode) {
if (line[i].target[0]) {
line[i].l.dreg = 0xFF;
for (k = 0; k < count; k++) {
if (!strcmp(line[i].target, line[k].label)) {
line[i].l.dreg = line[k].postlink_label_index % node->length;
break;
}
}
if (line[i].l.dreg == 0xFF) {
fprintf(stderr, "error: undefined label '%s'\n", line[i].target);
return 0;
}
}
out[j++] = line[i].l;
}
}
return 1;
}
static int load_user_nodes(char *save_data, struct user_node *user_nodes, int *count) {
char *lines[MAX_SAVE_LINES];
int num_save_lines = 0;
char *p = save_data;
// trim leading whitespace
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n')
p++;
while (*p) {
// trim indentation
while (*p == ' ' || *p == '\t')
p++;
char *e = p;
while (*e != '\r' && *e != '\n' && *e != '\0')
e++;
int l = e - p;
if (l > LINE_LENGTH) {
fprintf(stderr, "error: invalid save file: line too long\n");
return 0;
}
if (num_save_lines >= MAX_SAVE_LINES) {
fprintf(stderr, "error: invalid save file: too many lines\n");
return 0;
}
lines[num_save_lines++] = p;
p = e;
if (*p == '\r')
p++;
if (*p == '\n')
p++;
*e = '\0';
}
if (num_save_lines == 0 || strcmp(lines[0], "@0")) {
fprintf(stderr, "error: invalid save file: should start with '@0'\n");
return 0;
}
struct prelink_line node_lines[LINES_PER_NODE];
int n, node_line = 0, node = 0;
memset(user_nodes, 0, MAX_USER_NODES * sizeof *user_nodes);
for (n = 1; n < num_save_lines; n++) {
if (lines[n][0] == '@') {
// TODO: check the number is as expected
if (!link_node(node_line, node_lines, &user_nodes[node]))
return 0;
node++;
node_line = 0;
if (node >= MAX_USER_NODES) {
fprintf(stderr, "error: invalid save file: too many nodes\n");
return 0;
}
continue;
}
if (node_line >= LINES_PER_NODE) {
if (!lines[n][0])
continue;
fprintf(stderr, "error: invalid save file: too many lines in node\n");
return 0;
}
if (!parse_line(lines[n], &node_lines[node_line++]))
return 0;
}
if (!link_node(node_line, node_lines, &user_nodes[node]))
return 0;
node++;
*count = node;
return 1;
}
// initialize the arena - sets node neighbours and x/y
static void link_arena(struct base_node **arena) {
int x, y;
for (y = 0; y < ARENA_HEIGHT; y++) {
for (x = 0; x < ARENA_WIDTH; x++) {
struct base_node *this = arena[ARENA_I(x, y)];
if (!this)
continue;
if (x > 0) {
struct base_node *left = arena[ARENA_I(x-1, y)];
if (left) {
this->neighbors[D_LEFT] = left;
left->neighbors[D_RIGHT] = this;
}
}
if (y > 0) {
struct base_node *up = arena[ARENA_I(x, y-1)];
if (up) {
this->neighbors[D_UP] = up;
up->neighbors[D_DOWN] = this;
}
}
}
}
}
static void node_write(struct base_node *n, int direction_bits, int value) {
n->write_state = WS_WILL_BE_READABLE;
n->write_bits = direction_bits;
n->write_value = value;
}
static int node_read_from_direction(struct base_node *n, int direction, int *value) {
struct base_node *other = n->neighbors[direction];
if (!other || other->write_state != WS_READABLE ||
(other->write_bits & (1 << direction_opposite(direction))) == 0)
return 0;
*value = other->write_value;
other->write_state = WS_WILL_BE_RUNNING;
return 1;
}
// user node
static int user_node_do_read_from_register(struct user_node *n, int reg, int *value) {
switch (reg) {
case R_NIL:
return 1;
case R_ACC:
*value = n->acc;
return 1;
case R_LAST:
if (!n->last)
return 1;
return node_read_from_direction(&n->b, n->last - 1, value);
case R_UP:
case R_DOWN:
case R_LEFT:
case R_RIGHT:
return node_read_from_direction(&n->b, register_to_direction(reg), value);
case R_ANY: {
// TODO: this is probably inaccurate
int i;
for (i = 0; i < NUM_DIRECTIONS; i++) {
if (node_read_from_direction(&n->b, i, value)) {
n->last = i + 1;
return 1;
}
}
return 0;
}
}
return 0;
}
static void user_node_next_instruction(struct user_node *n) {
n->ip++;
if (n->ip == n->length)
n->ip = 0;
}
static void user_node_step(struct user_node *n, int step) {
if (step == S_COMMIT) {
if (n->b.write_state == WS_WILL_BE_READABLE) {
n->b.write_state = WS_READABLE;
} else if (n->b.write_state == WS_WILL_BE_RUNNING) {
n->b.write_state = WS_RUNNING;
user_node_next_instruction(n);
}
} else if (step == S_RUN) {
if (n->b.write_state != WS_RUNNING)
return; // nothing to do - another node will need to unblock this one
int src_value = 0;
struct line *l = &n->lines[n->ip];
if (op_has_src(l->opcode)) {
if (l->sreg != R_NONE) {
if (!user_node_do_read_from_register(n, l->sreg, &src_value))
return; // abort - retry next cycle
} else
src_value = l->immediate;
}
int branch = 0;
switch (l->opcode) {
case OP_ADD: n->acc = tis100_clamp(n->acc + src_value); break;
case OP_SUB: n->acc = tis100_clamp(n->acc - src_value); break;
case OP_SAV: n->bak = n->acc; break;
case OP_NEG: n->acc = -n->acc; break;
case OP_HCF: abort(); break;
case OP_NOP: break;
case OP_SWP: {
int tmp = n->bak;
n->bak = n->acc;
n->acc = tmp;
} break;
case OP_JMP: branch = 1; break;
case OP_JEZ: branch = (n->acc == 0); break;
case OP_JNZ: branch = (n->acc != 0); break;
case OP_JGZ: branch = (n->acc > 0); break;
case OP_JLZ: branch = (n->acc < 0); break;
case OP_JRO: {
n->ip = clamp(0, n->ip + src_value, n->length - 1);
return; // do not advance
} break;
case OP_MOV: {
switch (l->dreg) {
case R_LEFT:
case R_RIGHT:
case R_UP:
case R_DOWN:
node_write(&n->b, 1 << register_to_direction(l->dreg), src_value);
return; // do not advance
case R_ANY:
node_write(&n->b, 0x0F, src_value);
return; // do not advance
case R_LAST:
if (n->last) {
node_write(&n->b, 1 << (n->last - 1), src_value);
return; // do not advance
}
break;
case R_ACC:
n->acc = src_value;
break;
case R_NIL:
break;
default: assert(0);
}
} break;
default: assert(0);
}
// JRO deals with its own problems
if (branch)
n->ip = l->dreg;
else
user_node_next_instruction(n);
}
}
// in node
static void in_node_step(struct in_node *n, int step) {
if (step == S_COMMIT) {
if (n->b.write_state == WS_WILL_BE_READABLE)
n->b.write_state = WS_READABLE;
else if (n->b.write_state == WS_WILL_BE_RUNNING)
n->b.write_state = WS_RUNNING;
} else if (step == S_RUN) {
if (n->b.write_state == WS_RUNNING && n->i < n->num_values)
node_write(&n->b, 0x0F, n->values[n->i++]);
}
}
// out node
static void out_node_step(struct out_node *n, int step) {
int value = 0, expected;
if (step == S_RUN && n->i < n->num_values && node_read_from_direction(&n->b, D_UP, &value)) {
expected = n->values[n->i++];
if (expected != value)
n->arena->error = 1;
else if (n->i == n->num_values)
n->arena->completed++;
#ifdef OUTPUT_OUT_NODES
if (n->output_prefix)
printf("%s: ", n->output_prefix);
printf("%c %3d %3d\n", expected == value ? ' ' : 'X', expected, value);
#endif
}
}
// image node
static void image_node_step(struct image_node *n, int step) {
int value = 0;
if (step == S_RUN && node_read_from_direction(&n->b, D_UP, &value)) {
if (value < 0 && n->state != IS_COMPLETED) {
n->state = IS_READ_X;
return;
}
switch (n->state) {
case IS_READ_X: {
n->cursor_x = value;
n->state = IS_READ_Y;
} break;
case IS_READ_Y: {
n->cursor_y = value;
n->state = IS_READ_PIXELS;
} break;
case IS_READ_PIXELS: {
if (n->cursor_x < IMAGE_WIDTH && n->cursor_y < IMAGE_HEIGHT && value <= 4) {
int i = IMAGE_WIDTH * n->cursor_y + n->cursor_x;
if (value != n->display[i]) {
if (n->display[i] == n->solution[i]) {
n->wrong_pixels++; // now incorrect
} else if (value == n->solution[i]) {
n->wrong_pixels--; // now correct
if (n->wrong_pixels == 0) {
n->arena->completed++;
n->state = IS_COMPLETED;
}
}
n->display[i] = value;
}
n->cursor_x++;
}
} break;
}
#ifdef DRAW_IMAGE
int x, y;
printf("image:\n");
for (y = 0; y < IMAGE_HEIGHT; y++) {
for (x = 0; x < IMAGE_WIDTH; x++)
putchar(" ?!WR"[n->display[IMAGE_WIDTH * y + x]]);
putchar('\n');
}
#endif
}
}
static void image_node_set_solution(struct image_node *node, uint8 *solution) {
int i;
node->solution = solution;
node->wrong_pixels = 0;
for (i = 0; i < IMAGE_SIZE; i++)
if (solution[i])
node->wrong_pixels++;
}
// stack node
static void stack_node_step(struct stack_node *n, int step) {
if (step == S_RUN) {
int value = 0, d;
if (n->b.write_state == WS_WILL_BE_RUNNING) {
assert(n->used > 0);
n->values[--n->used] = 0;
}
for (d = 0; n->used < STACK_NODE_SIZE && d < NUM_DIRECTIONS; d++)
if (node_read_from_direction(&n->b, d, &value))
n->values[n->used++] = value;
} else if (step == S_COMMIT) {
if (n->used) {
n->b.write_state = WS_READABLE;
n->b.write_bits = 0x0F;
n->b.write_value = n->values[n->used-1];
} else {
n->b.write_state = WS_RUNNING;
}
}
}
// debug output functions
static void sdump_line(struct line *line, char *p) {
*p = '\0';
if (line->opcode != OP_NONE) {
p += sprintf(p, "%s", get_op_name(line->opcode));
if (op_num_arguments(line->opcode)) {
if (op_operand_is_label(line->opcode))
p += sprintf(p, " L%d", line->dreg);
else if (line->sreg)
p += sprintf(p, " %s", get_reg_name(line->sreg));
else
p += sprintf(p, " %d", line->immediate);
if (line->opcode == OP_MOV)
p += sprintf(p, ", %s", get_reg_name(line->dreg));
}
}
}
static void dump_arena(struct base_node **arena) {
#define DASHES "+-----------------------------------------------------------------------------------+"
int x, y, i;
puts(DASHES);
for (y = 1; y < 4; y++) {
for (i = -1; i < LINES_PER_NODE; i++) {
printf("|");
for (x = 0; x < 4; x++) {
struct base_node *n = arena[ARENA_I(x,y)];
const char *v = "";
char buffer[64];
char c = ' ';
if (i == -1) {
if (n && n->type == N_USER) {
struct user_node *un = (struct user_node *) n;
char *p = buffer + sprintf(buffer, "#%3d|%3d|", un->acc, un->bak);
if (n->write_state == WS_READABLE)
sprintf(p, "%3d", n->write_value);
v = buffer;
}
} else {
if (n && n->type == N_USER) {
struct user_node *un = (struct user_node *) n;
if (un->length != 0 && un->ip == i)
c = (n->write_state == WS_READABLE ? '*' : '>');
if (un->lines[i].opcode) {
sdump_line(&un->lines[i], buffer);
v = buffer;
}
}
}
printf("%c%-18s |", c, v);
}
printf("\n");
}
puts(DASHES);
}
}
// loading functions
static int load_user_nodes_filename(const char *filename, struct arena *arena) {
char save_data[4096];
FILE *f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "error: could not open save file '%s'\n", filename);
return 0;
}
int size = fread(save_data, 1, sizeof save_data - 1, f);
save_data[size] = '\0';
if (size == sizeof save_data - 1) {
fprintf(stderr, "error: invalid save file: too large\n");
return 0;
}
if (!load_user_nodes(save_data, arena->user_nodes, &arena->user_node_count))
return 0;
return 1;
}
static int arena_set_layout(struct arena *arena, int *layout) {
int i, user_node_index = 0;
for (i = 0; i < ARENA_SIZE; i++) {
switch (layout[i]) {
case N_NONE: break; // NULL value is already placed
case N_IN: {
struct in_node *n = &arena->in_nodes[arena->in_node_count++];
arena->nodes[i] = &n->b;
} break;
case N_OUT: {
struct out_node *n = &arena->out_nodes[arena->out_node_count++];
n->arena = arena;
arena->nodes[i] = &n->b;
} break;
case N_USER: {
if (user_node_index >= arena->user_node_count) {
fprintf(stderr, "error: not enough nodes in save file\n");
return 0;
}
struct user_node *n = &arena->user_nodes[user_node_index++];
// an empty node is equivalent to a "none" node, but much
// more expensive to simulate
if (n->length != 0)
arena->nodes[i] = &n->b;
} break;
case N_STACK: {
struct stack_node *n = &arena->stack_nodes[arena->stack_node_count++];
arena->nodes[i] = &n->b;
} break;
case N_IMAGE: {
struct image_node *n = &arena->image_nodes[arena->image_node_count++];
n->arena = arena;
arena->nodes[i] = &n->b;
} break;
}
if (arena->nodes[i])
arena->nodes[i]->type = layout[i];
}
if (user_node_index != arena->user_node_count) {
fprintf(stderr, "error: too many nodes in save file\n");
return 0;
}
return 1;
}