aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKimplul <kimi.h.kuparinen@gmail.com>2025-08-09 17:20:05 +0300
committerKimplul <kimi.h.kuparinen@gmail.com>2025-08-09 17:20:05 +0300
commit1853f5d6594bc371e4d8e631c24acd011a620915 (patch)
tree1bfaabd3685d4eacc6595ee6836adddcc37ba841 /src
parent3083284c797fc8fc267144b05c8a58395e4583e3 (diff)
downloadgran-1853f5d6594bc371e4d8e631c24acd011a620915.tar.gz
gran-1853f5d6594bc371e4d8e631c24acd011a620915.zip
refactor mesh3d to match 2d,1d
Diffstat (limited to 'src')
-rw-r--r--src/common.c45
-rw-r--r--src/mesh/node.c0
-rw-r--r--src/mesh/node1d.c232
-rw-r--r--src/mesh/node2d.c87
-rw-r--r--src/mesh/node3d.c364
5 files changed, 363 insertions, 365 deletions
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..41bd145
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,45 @@
+#include <gran/common.h>
+
+stat place_reg(struct reg *r, struct packet pkt)
+{
+ if (r->busy)
+ return EBUSY;
+
+ r->pkt = pkt;
+ r->busy = true;
+ return OK;
+}
+
+stat copy_reg(struct reg *r, struct reg *s)
+{
+ assert(s->busy);
+ if (r->busy)
+ return EBUSY;
+
+ r->pkt = s->pkt;
+ r->busy = true;
+ s->busy = false;
+ return OK;
+}
+
+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);
+}
diff --git a/src/mesh/node.c b/src/mesh/node.c
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/mesh/node.c
diff --git a/src/mesh/node1d.c b/src/mesh/node1d.c
index 23ba2ca..5a3cb06 100644
--- a/src/mesh/node1d.c
+++ b/src/mesh/node1d.c
@@ -1,18 +1,13 @@
#include <gran/mesh/node1d.h>
-#define left_port(n) (n)->ports[(n)->elems + 0]
-#define right_port(n) (n)->ports[(n)->elems + 1]
+#define north_port(n) (n)->ports[(n)->elems + 0]
+#define south_port(n) (n)->ports[(n)->elems + 1]
-#define left_in(n) (n)->in[(n)->elems + 0]
-#define right_in(n) (n)->in[(n)->elems + 1]
+#define north_in(n) (n)->in[(n)->elems + 0]
+#define south_in(n) (n)->in[(n)->elems + 1]
-#define left_out(n) (n)->out[(n)->elems + 0]
-#define right_out(n) (n)->out[(n)->elems + 1]
-
-struct reg {
- struct packet pkt;
- bool busy;
-};
+#define north_out(n) (n)->out[(n)->elems + 0]
+#define south_out(n) (n)->out[(n)->elems + 1]
struct node1d {
struct component component;
@@ -31,31 +26,11 @@ static void node1d_destroy(struct node1d *n)
free(n->in);
free(n->out);
free(n->ports);
+ free(n);
}
-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 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 stat node1d_receive(struct node1d *n, struct component *from, struct packet pkt)
+static stat node1d_receive(struct node1d *n, struct component *from,
+ struct packet pkt)
{
for (int i = 0; i < n->elems + 2; ++i) {
/* add timestamp to packets that originate with us */
@@ -63,7 +38,7 @@ static stat node1d_receive(struct node1d *n, struct component *from, struct pack
pkt.timestamp = n->timestamp;
if (from == n->ports[i])
- return reg_busy(&n->in[i], pkt);
+ return place_reg(&n->in[i], pkt);
}
/* shouldn't be possible */
@@ -85,139 +60,37 @@ static void clock_outputs(struct node1d *n)
}
}
-static void propagate_left(struct node1d *n, struct reg *a, struct reg *b)
-{
- struct reg *sel_a = NULL, *sel_b = NULL;
- if (a && a->busy) {
- uint16_t cluster = 0;
- addr_mesh1d(a->pkt.to, &cluster, NULL, NULL);
-
- if (cluster > n->cluster)
- sel_a = a;
- }
-
- if (b && b->busy) {
- uint16_t cluster = 0;
- addr_mesh1d(b->pkt.to, &cluster, NULL, NULL);
-
- if (cluster > n->cluster)
- sel_b = b;
- }
-
- if (!sel_a && !sel_b)
- return;
-
- if (sel_a && !sel_b) {
- copy_reg(&left_out(n), sel_a);
- return;
- }
-
- if (!sel_a && sel_b) {
- copy_reg(&left_out(n), sel_b);
- return;
- }
+struct sel_helper {
+ uint16_t cluster, elem;
+};
- /* both available, select older */
- if (sel_a->pkt.timestamp < sel_b->pkt.timestamp)
- copy_reg(&left_out(n), sel_a);
- else
- copy_reg(&left_out(n), sel_b);
+static bool north_sel(struct reg *r, void *data)
+{
+ uint16_t cluster = 0;
+ struct sel_helper *helper = data;
+ addr_mesh1d(r->pkt.to, &cluster, NULL, NULL);
+ return cluster > helper->cluster;
}
-static void propagate_right(struct node1d *n, struct reg *a, struct reg *b)
+static bool south_sel(struct reg *r, void *data)
{
- struct reg *sel_a = NULL, *sel_b = NULL;
- if (a && a->busy) {
- uint16_t cluster = 0;
- addr_mesh1d(a->pkt.to, &cluster, NULL, NULL);
-
- if (cluster < n->cluster)
- sel_a = a;
- }
-
- if (b && b->busy) {
- uint16_t cluster = 0;
- addr_mesh1d(b->pkt.to, &cluster, NULL, NULL);
-
- if (cluster < n->cluster)
- sel_b = b;
- }
-
- if (!sel_a && !sel_b)
- return;
-
- if (sel_a && !sel_b) {
- copy_reg(&right_out(n), sel_a);
- return;
- }
-
- if (!sel_a && sel_b) {
- copy_reg(&right_out(n), sel_b);
- return;
- }
-
- /* both available, select older */
- if (sel_a->pkt.timestamp < sel_b->pkt.timestamp)
- copy_reg(&right_out(n), sel_a);
- else
- copy_reg(&right_out(n), sel_b);
+ uint16_t cluster = 0;
+ struct sel_helper *helper = data;
+ addr_mesh1d(r->pkt.to, &cluster, NULL, NULL);
+ return cluster < helper->cluster;
}
-static void propagate(struct node1d *n, int elem, struct reg *a, struct reg *b, struct reg *c)
+static bool elem_sel(struct reg *r, void *data)
{
-
- struct reg *sel_a = NULL, *sel_b = NULL, *sel_c = NULL;
- if (a && a->busy) {
- uint16_t cluster = 0, element = 0;
- addr_mesh1d(a->pkt.to, &cluster, &element, NULL);
-
- if (cluster == n->cluster && element == elem)
- sel_a = a;
- }
-
- if (b && b->busy) {
- uint16_t cluster = 0, element = 0;
- addr_mesh1d(b->pkt.to, &cluster, &element, NULL);
-
- if (cluster == n->cluster && element == elem)
- sel_b = b;
- }
-
- if (c && c->busy) {
- uint16_t cluster = 0, element = 0;
- addr_mesh1d(c->pkt.to, &cluster, &element, NULL);
-
- if (cluster == n->cluster && element == elem)
- sel_c = c;
- }
-
- struct reg *sel_0 = NULL, *sel_1 = NULL;
- if (sel_a && sel_b)
- sel_0 = sel_a->pkt.timestamp < sel_b->pkt.timestamp ? sel_a : sel_b;
- else
- sel_0 = sel_a ? sel_a : sel_b;
-
- if (sel_b && sel_c)
- sel_1 = sel_b->pkt.timestamp < sel_c->pkt.timestamp ? sel_b : sel_c;
- else
- sel_1 = sel_b ? sel_b : sel_c;
-
- struct reg *sel = NULL;
- if (sel_0 && sel_1)
- sel = sel_0->pkt.timestamp < sel_1->pkt.timestamp ? sel_0 : sel_1;
- else
- sel = sel_0 ? sel_0 : sel_1;
-
- if (!sel)
- return;
-
- copy_reg(&n->out[elem], sel);
+ uint16_t cluster = 0, elem = 0;
+ struct sel_helper *helper = data;
+ addr_mesh1d(r->pkt.to, &cluster, &elem, NULL);
+ return cluster == helper->cluster && elem == helper->elem;
}
static stat node1d_clock(struct node1d *n)
{
n->timestamp++;
-
clock_outputs(n);
/* select oldest packet to process */
@@ -230,15 +103,28 @@ static stat node1d_clock(struct node1d *n)
r = &n->in[i];
}
- propagate_left(n, r, &right_in(n));
- propagate_right(n, r, &left_in(n));
- for (int i = 0; i < n->elems; ++i)
- propagate(n, i, r, &right_in(n), &left_in(n));
+ struct sel_helper helper = {
+ .cluster = n->cluster,
+ .elem = 0,
+ };
+
+ struct reg *north[] = {r, &south_in(n)};
+ struct reg *south[] = {r, &north_in(n)};
+
+ propagate(&north_out(n), 2, north, north_sel, &helper);
+ propagate(&south_out(n), 2, south, south_sel, &helper);
+
+ struct reg *all[] = {r, &north_in(n), &south_in(n)};
+ for (int i = 0; i < n->elems; ++i) {
+ helper.elem = i;
+ propagate(&n->out[i], 3, all, elem_sel, &helper);
+ }
return OK;
}
-stat mesh_node1d_connect(struct component *c, struct component *e, uint16_t elem)
+stat mesh_node1d_connect(struct component *c, struct component *e,
+ uint16_t elem)
{
struct node1d *n = (struct node1d *)c;
if (elem >= n->elems)
@@ -251,23 +137,23 @@ stat mesh_node1d_connect(struct component *c, struct component *e, uint16_t elem
return OK;
}
-stat mesh_node1d_connect_left(struct component *c, struct component *e)
+stat mesh_node1d_connect_north(struct component *c, struct component *e)
{
struct node1d *n = (struct node1d *)c;
- if (left_port(n))
+ if (north_port(n))
return EEXISTS;
- left_port(n) = e;
+ north_port(n) = e;
return OK;
}
-stat mesh_node1d_connect_right(struct component *c, struct component *e)
+stat mesh_node1d_connect_south(struct component *c, struct component *e)
{
struct node1d *n = (struct node1d *)c;
- if (right_port(n))
+ if (south_port(n))
return EEXISTS;
- right_port(n) = e;
+ south_port(n) = e;
return OK;
}
@@ -279,22 +165,20 @@ struct component *create_mesh_node1d(uint16_t cluster, uint16_t elems)
n->in = (struct reg *)calloc(elems + 2, sizeof(struct reg));
if (!n->in) {
- free(n);
+ node1d_destroy(n);
return NULL;
}
n->out = (struct reg *)calloc(elems + 2, sizeof(struct reg));
if (!n->out) {
- free(n->in);
- free(n);
+ node1d_destroy(n);
return NULL;
}
- n->ports = (struct component **)calloc(elems + 2, sizeof(struct component *));
+ n->ports = (struct component **)calloc(elems + 2,
+ sizeof(struct component *));
if (!n->ports) {
- free(n->out);
- free(n->in);
- free(n);
+ node1d_destroy(n);
return NULL;
}
diff --git a/src/mesh/node2d.c b/src/mesh/node2d.c
index 81ef8dc..569bd68 100644
--- a/src/mesh/node2d.c
+++ b/src/mesh/node2d.c
@@ -1,24 +1,19 @@
#include <gran/mesh/node2d.h>
#define north_port(n) (n)->ports[(n)->elems + 0]
-#define east_port(n) (n)->ports[(n)->elems + 1]
+#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 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 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 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 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;
-};
+#define west_out(n) (n)->out[(n)->elems + 3]
struct node2d {
struct component component;
@@ -32,6 +27,14 @@ struct node2d {
struct component **ports; /* countedby[elems + 4] */
};
+static void node2d_destroy(struct node2d *n)
+{
+ free(n->in);
+ free(n->out);
+ free(n->ports);
+ free(n);
+}
+
static void clock_outputs(struct node2d *n)
{
for (int i = 0; i < n->elems + 4; ++i) {
@@ -46,39 +49,6 @@ static void clock_outputs(struct node2d *n)
}
}
-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;
@@ -165,18 +135,8 @@ static stat node2d_clock(struct node2d *n)
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)
+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])
@@ -185,7 +145,7 @@ static stat node2d_receive(struct node2d *n, struct component *from, struct pack
if (i < n->elems)
pkt.timestamp = n->timestamp;
- return reg_busy(&n->in[i], pkt);
+ return place_reg(&n->in[i], pkt);
}
abort();
@@ -200,25 +160,23 @@ struct component *create_mesh_node2d(uint8_t x, uint8_t y, uint16_t elems)
n->in = calloc(elems + 4, sizeof(struct reg));
if (!n->in) {
- free(n);
+ node2d_destroy(n);
return NULL;
}
n->out = calloc(elems + 4, sizeof(struct reg));
if (!n->out) {
- free(n->in);
- free(n);
+ node2d_destroy(n);
return NULL;
}
n->ports = calloc(elems + 4, sizeof(struct component *));
if (!n->ports) {
- free(n->out);
- free(n->in);
- free(n);
+ node2d_destroy(n);
return NULL;
}
+ n->component.destroy = (destroy_callback)node2d_destroy;
n->component.receive = (receive_callback)node2d_receive;
n->component.clock = (clock_callback)node2d_clock;
n->elems = elems;
@@ -227,7 +185,8 @@ struct component *create_mesh_node2d(uint8_t x, uint8_t y, uint16_t elems)
return (struct component *)n;
}
-stat mesh_node2d_connect(struct component *c, struct component *e, uint16_t elem)
+stat mesh_node2d_connect(struct component *c, struct component *e,
+ uint16_t elem)
{
struct node2d *n = (struct node2d *)c;
if (elem >= n->elems)
diff --git a/src/mesh/node3d.c b/src/mesh/node3d.c
index d00f762..8c6d776 100644
--- a/src/mesh/node3d.c
+++ b/src/mesh/node3d.c
@@ -1,189 +1,299 @@
#include <gran/mesh/node3d.h>
-struct reg {
- struct packet pkt;
- bool busy;
-};
+#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 component *n, *s, *e, *w, *u, *d, *l;
-
- struct reg n_in, s_in, e_in, w_in, u_in, d_in, l_in;
+ struct reg *in; /* countedby[elems + 6] */
+ struct reg *out; /* countedby[elems + 6] */
+ struct component **ports; /* countedby[elems + 6] */
};
-enum order {
- N, S, E, W, U, D, L
-};
+static void node3d_destroy(struct node3d *n)
+{
+ free(n->in);
+ free(n->out);
+ free(n->ports);
+ free(n);
+}
-static inline void maybe_pick(struct reg *output[7], enum order d, struct reg *r)
+static void clock_outputs(struct node3d *n)
{
- if (output[d] && output[d]->pkt.timestamp < r->pkt.timestamp)
- return;
+ 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;
- output[d] = r;
+ n->out[i].busy = false;
+ }
}
-static stat node3d_clock(struct node3d *node3d)
+struct sel_helper {
+ uint8_t x, y, z, elem;
+};
+
+static bool north_sel(struct reg *r, void *data)
{
- node3d->timestamp++;
+ uint8_t y = 0;
+ struct sel_helper *helper = data;
+ addr_mesh3d(r->pkt.to, NULL, &y, NULL, NULL, NULL);
+ return y > helper->y;
+}
- struct reg *output[7] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
- struct reg *input[7] = {
- &node3d->n_in,
- &node3d->s_in,
- &node3d->e_in,
- &node3d->w_in,
- &node3d->u_in,
- &node3d->d_in,
- &node3d->l_in
- };
+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;
+}
- uint8_t X = node3d->x, Y = node3d->y, Z = node3d->z;
- for (size_t i = 0; i < 7; ++i) {
- struct reg *r = input[i];
- if (!r->busy)
- continue;
+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;
+}
- uint8_t x, y, z;
- addr_mesh3d(r->pkt.to, &x, &y, &z, NULL);
- if (x < X) {
- maybe_pick(output, W, r);
- continue;
- }
+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;
+}
- if (x > X) {
- maybe_pick(output, E, r);
- continue;
- }
+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;
+}
- if (y < Y) {
- maybe_pick(output, S, r);
- continue;
- }
+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;
+}
- if (y > Y) {
- maybe_pick(output, N, r);
- continue;
- }
+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;
+}
- if (z < Z) {
- maybe_pick(output, D, r);
- continue;
- }
+static stat node3d_clock(struct node3d *n)
+{
+ n->timestamp++;
+ clock_outputs(n);
- if (z > Z) {
- maybe_pick(output, U, r);
+ /* select oldest packet to process */
+ struct reg *r = NULL;
+ for (int i = 0; i < n->elems; ++i) {
+ if (!n->in[i].busy)
continue;
- }
- maybe_pick(output, L, r);
- }
+ if (!r || r->pkt.timestamp > n->in[i].pkt.timestamp)
+ r = &n->in[i];
+ };
- struct component *target[7] = {
- node3d->n,
- node3d->s,
- node3d->e,
- node3d->w,
- node3d->u,
- node3d->d,
- node3d->l
+ struct sel_helper helper = {
+ .elem = 0,
+ .x = n->x,
+ .y = n->y,
+ .z = n->z
};
- for (size_t i = 0; i < 7; ++i) {
- if (!output[i])
- continue;
+ struct reg *north[] = {r, &east_in(n), &south_in(n),
+ &west_in(n), &up_in(n), &down_in(n)};
- if (!target[i]) {
- /* for now, should send packet back with an error or something */
- abort();
- }
+ struct reg *east[] = {r, &north_in(n), &south_in(n),
+ &west_in(n), &up_in(n), &down_in(n)};
- stat ret = SEND(node3d, target[i], output[i]->pkt);
- if (ret == EBUSY)
- continue;
+ 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)};
- assert(ret == OK);
- output[i]->busy = false;
+ 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 reg_receive(struct reg *r, struct packet pkt)
+static stat node3d_receive(struct node3d *n, struct component *from,
+ struct packet pkt)
{
- if (r->busy)
- return EBUSY;
+ for (int i = 0; i < n->elems + 6; ++i) {
+ if (from != n->ports[i])
+ continue;
+
+ if (i < n->elems)
+ pkt.timestamp = n->timestamp;
- r->pkt = pkt;
- r->busy = true;
+ return place_reg(&n->in[i], pkt);
+ }
+
+ abort();
return OK;
}
-static stat node3d_receive(struct node3d *node3d, struct component *from, struct packet pkt)
+struct component *create_mesh_node3d(uint8_t x, uint8_t y, uint8_t z, uint8_t elems)
{
- if (from == node3d->l) {
- /* add time when packet entered network */
- pkt.timestamp = node3d->timestamp;
- return reg_receive(&node3d->l_in, pkt);
+ 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;
}
- if (from == node3d->n)
- return reg_receive(&node3d->n_in, pkt);
+ n->out = calloc(elems + 6, sizeof(struct reg));
+ if (!n->out) {
+ node3d_destroy(n);
+ return NULL;
+ }
- if (from == node3d->s)
- return reg_receive(&node3d->s_in, pkt);
+ n->ports = calloc(elems + 6, sizeof(struct component*));
+ if (!n->ports) {
+ node3d_destroy(n);
+ return NULL;
+ }
- if (from == node3d->e)
- return reg_receive(&node3d->e_in, pkt);
+ 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;
+}
- if (from == node3d->w)
- return reg_receive(&node3d->w_in, pkt);
+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 (from == node3d->u)
- return reg_receive(&node3d->u_in, pkt);
+ if (n->ports[elem])
+ return EEXISTS;
- if (from == node3d->d)
- return reg_receive(&node3d->d_in, pkt);
+ n->ports[elem] = e;
+ return OK;
+}
- abort();
+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;
}
-struct component *create_mesh_node3d(uint8_t x, uint8_t y, uint8_t z)
+stat mesh_node3d_connect_east(struct component *c, struct component *e)
{
- struct node3d *node = calloc(1, sizeof(struct node3d));
- if (!node)
- return NULL;
+ 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;
- node->component.receive = (receive_callback)node3d_receive;
- node->component.clock = (clock_callback)node3d_clock;
- node->x = x;
- node->y = y;
- node->z = z;
- return (struct component *)node;
+ up_port(n) = e;
+ return OK;
}
-stat mesh_node3d_connect(struct component *node,
- struct component *n,
- struct component *s,
- struct component *e,
- struct component *w,
- struct component *u,
- struct component *d,
- struct component *l)
+stat mesh_node3d_connect_down(struct component *c, struct component *e)
{
- struct node3d *node3d = (struct node3d *)node;
- node3d->n = n;
- node3d->s = s;
- node3d->e = e;
- node3d->w = w;
- node3d->u = u;
- node3d->d = d;
- node3d->l = l;
+ struct node3d *n = (struct node3d *)c;
+ if (down_port(n))
+ return EEXISTS;
+
+ down_port(n) = e;
return OK;
}