aboutsummaryrefslogtreecommitdiff
path: root/src/mesh/node2d.c
blob: 81ef8dc2fcfaf06deb44793aef02a362651d84f4 (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
#include <gran/mesh/node2d.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 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 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]

struct reg {
	struct packet pkt;
	bool busy;
};

struct node2d {
	struct component component;
	uint16_t elems;
	uint16_t x, y;

	uint64_t timestamp;

	struct reg *in; /* countedby[elems + 4] */
	struct reg *out; /* countedby[elems + 4] */
	struct component **ports; /* countedby[elems + 4] */
};

static void clock_outputs(struct node2d *n)
{
	for (int i = 0; i < n->elems + 4; ++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;
	}
}

static void copy_reg(struct reg *r, struct reg *s)
{
	assert(s->busy);
	if (r->busy)
		return;

	r->pkt = s->pkt;
	r->busy = true;
	s->busy = false;
}

static void propagate(struct reg *out,
		size_t count, struct reg *in[static count],
		bool (*sel)(struct reg *r, void *data), void *data)
{
	struct reg *r = NULL;
	for (size_t i = 0; i < count; ++i) {
		if (!in[i] || !in[i]->busy)
			continue;

		if (!sel(in[i], data))
			continue;

		if (!r || r->pkt.timestamp > in[i]->pkt.timestamp)
			r = in[i];
	}

	if (!r)
		return;

	copy_reg(out, r);
}

struct sel_helper {
	uint8_t x, y;
	uint16_t elem;
};

static bool north_sel(struct reg *r, void *data)
{
	uint8_t y = 0;
	struct sel_helper *helper = data;
	addr_mesh2d(r->pkt.to, NULL, &y, 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_mesh2d(r->pkt.to, NULL, &y, 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_mesh2d(r->pkt.to, &x, &y, 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_mesh2d(r->pkt.to, &x, &y, NULL, NULL);
	return y == helper->y && x < helper->x;
}

static bool elem_sel(struct reg *r, void *data)
{
	uint16_t elem = 0;
	uint8_t x = 0, y = 0;
	struct sel_helper *helper = data;
	addr_mesh2d(r->pkt.to, &x, &y, &elem, NULL);
	return x == helper->x && y == helper->y && elem == helper->elem;
}

static stat node2d_clock(struct node2d *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,
	};

	struct reg *north[] = {r, &east_in(n),  &south_in(n), &west_in(n)};
	struct reg *east[]  = {r, &north_in(n), &south_in(n), &west_in(n)};
	struct reg *south[] = {r, &north_in(n), &east_in(n),  &west_in(n)};
	struct reg *west[]  = {r, &north_in(n), &east_in(n),  &south_in(n)};

	propagate(&north_out(n), 4, north, north_sel, &helper);
	propagate(&east_out(n),  4, east,  east_sel,  &helper);
	propagate(&south_out(n), 4, south, south_sel, &helper);
	propagate(&west_out(n),  4, west,  west_sel,  &helper);

	struct reg *all[] = {r, &north_in(n), &east_in(n), &south_in(n), &west_in(n)};
	for (int i = 0; i < n->elems; ++i) {
		helper.elem = i;
		propagate(&n->out[i], 5, all, elem_sel, &helper);
	}

	return OK;
}

static stat reg_busy(struct reg *r, struct packet pkt)
{
	bool busy = r->busy;
	if (!busy) {
		r->pkt = pkt;
		r->busy = true;
	}

	return busy ? EBUSY : OK;
}

static stat node2d_receive(struct node2d *n, struct component *from, struct packet pkt)
{
	for (int i = 0; i < n->elems + 4; ++i) {
		if (from != n->ports[i])
			continue;

		if (i < n->elems)
			pkt.timestamp = n->timestamp;

		return reg_busy(&n->in[i], pkt);
	}

	abort();
	return OK;
}

struct component *create_mesh_node2d(uint8_t x, uint8_t y, uint16_t elems)
{
	struct node2d *n = calloc(1, sizeof(struct node2d));
	if (!n)
		return NULL;

	n->in = calloc(elems + 4, sizeof(struct reg));
	if (!n->in) {
		free(n);
		return NULL;
	}

	n->out = calloc(elems + 4, sizeof(struct reg));
	if (!n->out) {
		free(n->in);
		free(n);
		return NULL;
	}

	n->ports = calloc(elems + 4, sizeof(struct component *));
	if (!n->ports) {
		free(n->out);
		free(n->in);
		free(n);
		return NULL;
	}

	n->component.receive = (receive_callback)node2d_receive;
	n->component.clock = (clock_callback)node2d_clock;
	n->elems = elems;
	n->x = x;
	n->y = y;
	return (struct component *)n;
}

stat mesh_node2d_connect(struct component *c, struct component *e, uint16_t elem)
{
	struct node2d *n = (struct node2d *)c;
	if (elem >= n->elems)
		return ENOSUCH;

	if (n->ports[elem])
		return EEXISTS;

	n->ports[elem] = e;
	return OK;
}

stat mesh_node2d_connect_north(struct component *c, struct component *e)
{
	struct node2d *n = (struct node2d *)c;
	if (north_port(n))
		return EEXISTS;

	north_port(n) = e;
	return OK;
}

stat mesh_node2d_connect_east(struct component *c, struct component *e)
{
	struct node2d *n = (struct node2d *)c;
	if (east_port(n))
		return EEXISTS;

	east_port(n) = e;
	return OK;
}

stat mesh_node2d_connect_south(struct component *c, struct component *e)
{
	struct node2d *n = (struct node2d *)c;
	if (south_port(n))
		return EEXISTS;

	south_port(n) = e;
	return OK;
}

stat mesh_node2d_connect_west(struct component *c, struct component *e)
{
	struct node2d *n = (struct node2d *)c;
	if (west_port(n))
		return EEXISTS;

	west_port(n) = e;
	return OK;
}