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
|
/* very simple grid node with 32bit private region, does not currently signal
* being busy or anything. I think I might have to refine the message passing
* interface I have, but this is good enough.
*
* Each node should have a router beneath it, just to simplify my life. A router
* is basically a bus with a fallback ascension path.
*/
#include <stdbool.h>
#include <gran/grid/node.h>
struct grid_node {
struct component component;
uint8_t u, v, x, y;
struct component *left, *right, *up, *down, *ascend, *lower;
struct component *send;
struct packet pkt;
bool busy;
};
static stat grid_clock(struct grid_node *grid)
{
if (!grid->busy)
return OK;
stat r = SEND(grid, grid->send, grid->pkt);
if (r == EBUSY)
return OK;
grid->busy = false;
return r;
}
static stat grid_receive(struct grid_node *grid, struct component *from, struct packet pkt)
{
if (grid->busy)
return EBUSY;
uint8_t u;
uint8_t v;
uint8_t x;
uint8_t y;
uint64_t addr = pkt.to;
addr_grid(addr, NULL, &x, &y, &u, &v);
grid->busy = true;
grid->pkt = pkt;
if (grid->u == u && grid->v == v && grid->x == x && grid->y == y) {
if (!grid->lower)
goto nosuch;
grid->send = grid->lower;
return OK;
}
if (grid->u != u || grid->v != v) {
if (!grid->ascend)
goto nosuch;
grid->send = grid->ascend;
return OK;
}
if (y < grid->y) {
if (!grid->down)
goto nosuch;
grid->send = grid->down;
return OK;
}
if (y > grid->y) {
if (!grid->up)
goto nosuch;
grid->send = grid->up;
return OK;
}
if (x < grid->x) {
if (!grid->left)
goto nosuch;
grid->send = grid->left;
return OK;
}
if (x > grid->x) {
if (!grid->right)
return EBUS;
grid->send = grid->right;
return OK;
}
nosuch:
grid->send = from;
grid->pkt = response(pkt);
set_flags(&grid->pkt, PACKET_ERROR);
return OK;
}
struct component *create_grid_node(uint8_t u, uint8_t v, uint8_t x, uint8_t y)
{
struct grid_node *node = calloc(1, sizeof(struct grid_node));
if (!node)
return NULL;
node->component.receive = (receive_callback)grid_receive;
node->component.clock = (clock_callback)grid_clock;
node->u = u;
node->v = v;
node->x = x;
node->y = y;
return (struct component *)node;
}
stat grid_node_connect(struct component *node,
struct component *left, struct component *right,
struct component *up, struct component *down,
struct component *lower, struct component *ascend)
{
struct grid_node *n = (struct grid_node *)node;
n->left = left;
n->right = right;
n->up = up;
n->down = down;
n->lower = lower;
n->ascend = ascend;
return OK;
}
|