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
|
#include <gran/mesh/node3d.h>
#define north_port(n) (n)->ports[(n)->elems + 0]
#define east_port(n) (n)->ports[(n)->elems + 1]
#define south_port(n) (n)->ports[(n)->elems + 2]
#define west_port(n) (n)->ports[(n)->elems + 3]
#define up_port(n) (n)->ports[(n)->elems + 4]
#define down_port(n) (n)->ports[(n)->elems + 5]
#define north_in(n) (n)->in[(n)->elems + 0]
#define east_in(n) (n)->in[(n)->elems + 1]
#define south_in(n) (n)->in[(n)->elems + 2]
#define west_in(n) (n)->in[(n)->elems + 3]
#define up_in(n) (n)->in[(n)->elems + 4]
#define down_in(n) (n)->in[(n)->elems + 5]
#define north_out(n) (n)->out[(n)->elems + 0]
#define east_out(n) (n)->out[(n)->elems + 1]
#define south_out(n) (n)->out[(n)->elems + 2]
#define west_out(n) (n)->out[(n)->elems + 3]
#define up_out(n) (n)->out[(n)->elems + 4]
#define down_out(n) (n)->out[(n)->elems + 5]
struct node3d {
struct component component;
uint8_t x, y, z;
uint8_t elems;
uint64_t timestamp;
struct reg *in; /* countedby[elems + 6] */
struct reg *out; /* countedby[elems + 6] */
struct component **ports; /* countedby[elems + 6] */
};
static void node3d_destroy(struct node3d *n)
{
free(n->in);
free(n->out);
free(n->ports);
free(n);
}
static void clock_outputs(struct node3d *n)
{
for (int i = 0; i < n->elems + 6; ++i) {
if (!n->out[i].busy)
continue;
stat ret = SEND(n, n->ports[i], n->out[i].pkt);
if (ret == EBUSY)
continue;
n->out[i].busy = false;
}
}
struct sel_helper {
uint8_t x, y, z, elem;
};
static bool north_sel(struct reg *r, void *data)
{
uint8_t y = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, NULL, &y, NULL, NULL, NULL);
return y > helper->y;
}
static bool south_sel(struct reg *r, void *data)
{
uint8_t y = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, NULL, &y, NULL, NULL, NULL);
return y < helper->y;
}
static bool east_sel(struct reg *r, void *data)
{
uint8_t x = 0, y = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, &x, &y, NULL, NULL, NULL);
return y == helper->y && x > helper->x;
}
static bool west_sel(struct reg *r, void *data)
{
uint8_t x = 0, y = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, &x, &y, NULL, NULL, NULL);
return y == helper->y && x < helper->x;
}
static bool up_sel(struct reg *r, void *data)
{
uint8_t x = 0, y = 0, z = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, &x, &y, &z, NULL, NULL);
return y == helper->y && x == helper->x && z > helper->z;
}
static bool down_sel(struct reg *r, void *data)
{
uint8_t x = 0, y = 0, z = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, &x, &y, &z, NULL, NULL);
return y == helper->y && x == helper->x && z < helper->z;
}
static bool elem_sel(struct reg *r, void *data)
{
uint8_t x = 0, y = 0, z = 0, elem = 0;
struct sel_helper *helper = data;
addr_mesh3d(r->pkt.to, &x, &y, &z, &elem, NULL);
return x == helper->x && y == helper->y && z == helper->z &&
elem == helper->elem;
}
static stat node3d_clock(struct node3d *n)
{
n->timestamp++;
clock_outputs(n);
/* select oldest packet to process */
struct reg *r = NULL;
for (int i = 0; i < n->elems; ++i) {
if (!n->in[i].busy)
continue;
if (!r || r->pkt.timestamp > n->in[i].pkt.timestamp)
r = &n->in[i];
};
struct sel_helper helper = {
.elem = 0,
.x = n->x,
.y = n->y,
.z = n->z
};
struct reg *north[] = {r, &east_in(n), &south_in(n),
&west_in(n), &up_in(n), &down_in(n)};
struct reg *east[] = {r, &north_in(n), &south_in(n),
&west_in(n), &up_in(n), &down_in(n)};
struct reg *south[] = {r, &north_in(n), &east_in(n),
&west_in(n), &up_in(n), &down_in(n)};
struct reg *west[] = {r, &north_in(n), &east_in(n),
&south_in(n), &up_in(n), &down_in(n)};
struct reg *up[] = {r, &north_in(n), &east_in(n),
&west_in(n), &south_in(n), &down_in(n)};
struct reg *down[] = {r, &north_in(n), &east_in(n),
&west_in(n), &south_in(n), &up_in(n)};
propagate(&north_out(n), 6, north, north_sel, &helper);
propagate(&east_out(n), 6, east, east_sel, &helper);
propagate(&south_out(n), 6, south, south_sel, &helper);
propagate(&west_out(n), 6, west, west_sel, &helper);
propagate(&up_out(n), 6, up, up_sel, &helper);
propagate(&down_out(n), 6, down, down_sel, &helper);
struct reg *all[] = {r, &north_in(n), &east_in(n), &south_in(n),
&west_in(n), &up_in(n), &down_in(n)};
for (int i = 0; i < n->elems; ++i) {
helper.elem = i;
propagate(&n->out[i], 7, all, elem_sel, &helper);
}
return OK;
}
static stat node3d_receive(struct node3d *n, struct component *from,
struct packet pkt)
{
for (int i = 0; i < n->elems + 6; ++i) {
if (from != n->ports[i])
continue;
if (i < n->elems)
pkt.timestamp = n->timestamp;
return place_reg(&n->in[i], pkt);
}
abort();
return OK;
}
struct component *create_mesh_node3d(uint8_t x, uint8_t y, uint8_t z,
uint8_t elems)
{
struct node3d *n = calloc(1, sizeof(struct node3d));
if (!n)
return NULL;
n->in = calloc(elems + 6, sizeof(struct reg));
if (!n->in) {
node3d_destroy(n);
return NULL;
}
n->out = calloc(elems + 6, sizeof(struct reg));
if (!n->out) {
node3d_destroy(n);
return NULL;
}
n->ports = calloc(elems + 6, sizeof(struct component*));
if (!n->ports) {
node3d_destroy(n);
return NULL;
}
n->component.destroy = (destroy_callback)node3d_destroy;
n->component.receive = (receive_callback)node3d_receive;
n->component.clock = (clock_callback)node3d_clock;
n->elems = elems;
n->x = x;
n->y = y;
n->z = z;
return (struct component *)n;
}
stat mesh_node3d_connect(struct component *c, struct component *e,
uint8_t elem)
{
struct node3d *n = (struct node3d *)c;
if (elem >= n->elems)
return ENOSUCH;
if (n->ports[elem])
return EEXISTS;
n->ports[elem] = e;
return OK;
}
stat mesh_node3d_connect_north(struct component *c, struct component *e)
{
struct node3d *n = (struct node3d *)c;
if (north_port(n))
return EEXISTS;
north_port(n) = e;
return OK;
}
stat mesh_node3d_connect_east(struct component *c, struct component *e)
{
struct node3d *n = (struct node3d *)c;
if (east_port(n))
return EEXISTS;
east_port(n) = e;
return OK;
}
stat mesh_node3d_connect_south(struct component *c, struct component *e)
{
struct node3d *n = (struct node3d *)c;
if (south_port(n))
return EEXISTS;
south_port(n) = e;
return OK;
}
stat mesh_node3d_connect_west(struct component *c, struct component *e)
{
struct node3d *n = (struct node3d *)c;
if (west_port(n))
return EEXISTS;
west_port(n) = e;
return OK;
}
stat mesh_node3d_connect_up(struct component *c, struct component *e)
{
struct node3d *n = (struct node3d *)c;
if (up_port(n))
return EEXISTS;
up_port(n) = e;
return OK;
}
stat mesh_node3d_connect_down(struct component *c, struct component *e)
{
struct node3d *n = (struct node3d *)c;
if (down_port(n))
return EEXISTS;
down_port(n) = e;
return OK;
}
|