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
|
#include <gran/torus3d/node.h>
struct reg {
struct packet pkt;
bool busy;
};
struct port {
struct reg r[2];
};
struct torus3d_node {
struct component component;
uint8_t x, y, z /*, w for 4D but that might be a bit overkill*/;
struct component *x_next, *y_next, *z_next,
*x_prev, *y_prev, *z_prev,
*child;
struct port port_x, port_y, port_z;
struct reg x_in, y_in, z_in, child_in;
bool prio;
};
static stat port_receive(struct torus3d_node *torus3d, struct port *port, struct reg *reg)
{
if (!reg->busy)
return OK;
/* source */
uint8_t sx, sy, sz;
addr_torus3d(reg->pkt.from, &sx, &sy, &sz, NULL);
/* dst */
uint8_t dx, dy, dz;
addr_torus3d(reg->pkt.to, &dx, &dy, &dz, NULL);
/* Dally/spiral routing though I'm a bit unsure if this works for 2D/3D
* toruses (1D seems to work, 2D not so much atm) */
int chan = is_set(®->pkt, PACKET_DONE);
if (port->r[chan].busy)
return OK;
printf("(%d, %d, %d) to (%d, %d, %d) via (%d, %d, %d) c %d\n",
sx, sy, sz,
dx, dy, dz,
torus3d->x, torus3d->y, torus3d->z,
chan);
port->r[chan].pkt = reg->pkt;
port->r[chan].busy = true;
reg->busy = false;
return OK;
}
static stat reg_receive(struct reg *r, struct packet pkt)
{
if (r->busy)
return EBUSY;
r->pkt = pkt;
r->busy = true;
return OK;
}
enum match {
MX = (1 << 0),
MY = (1 << 1),
MZ = (1 << 2),
NX = (1 << 3),
NY = (1 << 4),
NZ = (1 << 5),
};
static void maybe_route_reg(struct torus3d_node *torus3d, struct reg *reg, enum match m, uint64_t *oldest, struct packet **pkt, bool **busy)
{
if (!reg->busy)
return;
uint8_t x, y, z;
addr_torus3d(reg->pkt.to, &x, &y, &z, NULL);
if ((m & MX) && x != torus3d->x)
return;
if ((m & MY) && y != torus3d->y)
return;
if ((m & MZ) && z != torus3d->z)
return;
if ((m & NX) && x == torus3d->x)
return;
if ((m & NY) && y == torus3d->y)
return;
if ((m & NZ) && z == torus3d->z)
return;
if (reg->pkt.timestamp < *oldest) {
*oldest = reg->pkt.timestamp;
*busy = ®->busy;
*pkt = ®->pkt;
}
}
static stat route(struct torus3d_node *torus3d, struct component *next, enum match m)
{
bool prio = torus3d->prio;
uint64_t oldest = -1; struct packet *pkt = NULL; bool *busy = NULL;
/* prioritise current priority port */
maybe_route_reg(torus3d, &torus3d->port_x.r[prio], m, &oldest, &pkt, &busy);
maybe_route_reg(torus3d, &torus3d->port_y.r[prio], m, &oldest, &pkt, &busy);
maybe_route_reg(torus3d, &torus3d->port_z.r[prio], m, &oldest, &pkt, &busy);
/* if no suitable match found, check other ports as well */
if (pkt == NULL) {
maybe_route_reg(torus3d, &torus3d->port_x.r[!prio], m, &oldest, &pkt, &busy);
maybe_route_reg(torus3d, &torus3d->port_y.r[!prio], m, &oldest, &pkt, &busy);
maybe_route_reg(torus3d, &torus3d->port_z.r[!prio], m, &oldest, &pkt, &busy);
}
maybe_route_reg(torus3d, &torus3d->child_in, m, &oldest, &pkt, &busy);
/* no suitable match */
if (pkt == NULL)
return OK;
assert(busy);
stat ret = SEND(torus3d, next, *pkt);
if (ret == EBUSY)
return OK;
*busy = false;
return OK;
}
static stat torus3d_clock(struct torus3d_node *torus3d)
{
torus3d->prio = !torus3d->prio;
stat ret = OK;
if ((ret = port_receive(torus3d, &torus3d->port_x, &torus3d->x_in)))
return ret;
if ((ret = port_receive(torus3d, &torus3d->port_y, &torus3d->y_in)))
return ret;
if ((ret = port_receive(torus3d, &torus3d->port_z, &torus3d->z_in)))
return ret;
if ((ret = route(torus3d, torus3d->x_next, NX)))
return ret;
if ((ret = route(torus3d, torus3d->y_next, NY | MX)))
return ret;
if ((ret = route(torus3d, torus3d->z_next, NZ | MX | MY)))
return ret;
if ((ret = route(torus3d, torus3d->child, MX | MY | MZ)))
return ret;
return OK;
}
static stat torus3d_receive(struct torus3d_node *torus3d, struct component *from, struct packet pkt)
{
if (from == torus3d->child)
return reg_receive(&torus3d->child_in, pkt);
if (from == torus3d->x_prev)
return reg_receive(&torus3d->x_in, pkt);
if (from == torus3d->y_prev)
return reg_receive(&torus3d->y_in, pkt);
if (from == torus3d->z_prev)
return reg_receive(&torus3d->z_in, pkt);
abort();
return OK;
}
struct component *create_torus3d_node(uint8_t x, uint8_t y, uint8_t z)
{
struct torus3d_node *node = calloc(1, sizeof(struct torus3d_node));
if (!node)
return NULL;
node->component.receive = (receive_callback)torus3d_receive;
node->component.clock = (clock_callback)torus3d_clock;
node->x = x;
node->y = y;
node->z = z;
return (struct component *)node;
}
stat torus3d_node_connect(struct component *node,
struct component *x_in,
struct component *y_in,
struct component *z_in,
struct component *child,
struct component *x_out,
struct component *y_out,
struct component *z_out)
{
struct torus3d_node *n = (struct torus3d_node *)node;
n->x_prev = x_in;
n->y_prev = y_in;
n->z_prev = z_in;
n->child = child;
n->x_next = x_out;
n->y_next = y_out;
n->z_next = z_out;
return OK;
}
|