-
Notifications
You must be signed in to change notification settings - Fork 1
/
clockrouter.py
99 lines (84 loc) · 2.54 KB
/
clockrouter.py
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
class ClockTree:
def __init__(self):
return
def add_node(self, node_type, x, y, z):
return
def add_edge(self, src_type, sx, sy, sz, dest_type, dx, dy, dz):
print '%s_X%dY%d[%d] -> %s_X%dY%d[%d]'%(
src_type.upper(),
sx,
sy,
sz,
dest_type.upper(),
dx,
dy,
dz
)
return
def add_int_edge(self, node_type, x, y, z, from_dir, to_dir, jump_plane):
print '%s_X%dY%d[%d] %s -> %s'%(
node_type.upper(),
x,
y,
z,
from_dir.upper(),
to_dir.upper()
)
return
class ClockRouter:
def __init__(self, x, y):
# (x, y) is the sector dimensions of the device
self.num_planes = 32
self.x = x
self.y = y
def __get_src_dest_dir(self, sx, sy, dx, dy):
# Can either go "horizonal" or "vertical".
# Can't do diagonal
assert sx == dx or sy == dy
if sx != dx:
if dx > sx:
return 'WEST', 'EAST'
else:
return 'EAST', 'WEST'
else:
if dy > sy:
return 'SOUTH', 'NORTH'
else:
return 'NORTH', 'SOUTH'
def route_swbox_to_swbox(self, tree, sx, sy, dx, dy, plane):
assert sx >= 0 and sx < self.x
assert sy >= 0 and sy < self.y
assert dx >= 0 and dx < self.x
assert dy >= 0 and dy < self.y
assert plane >= 0 and plane < self.num_planes
# lulwut. Y is da source and destination da same.
assert sx != dx or sy != dy
cur_x = sx
cur_y = sy
# What direction will x move to reach from sx to dx
del_x = (dx - sx) / int(abs(dx - sx))
assert del_x == 1 or del_x == -1
final_hor_direction = None
while cur_x != dx:
next_x = cur_x + del_x
tree.add_edge('swbox', cur_x, cur_y, plane, 'swbox', next_x, cur_y, plane)
src_dir, dest_dir = self.__get_src_dest_dir(cur_x, cur_y, next_x, cur_y)
final_hor_direction = dest_dir
tree.add_int_edge('swbox', next_x, cur_y, plane, src_dir, dest_dir, jump_plane=False)
cur_x = next_x
# What direction will y move to reach from sy to dy
del_y = (dy - sy) / int(abs(dy - sy))
assert del_y == 1 or del_y == -1
while cur_y != dy:
next_y = cur_y + del_y
tree.add_edge('swbox', cur_x, cur_y, plane, 'swbox', cur_x, next_y, plane)
src_dir, dest_dir = self.__get_src_dest_dir(cur_x, cur_y, cur_x, next_y)
if final_hor_direction != None:
src_dir = final_hor_direction
final_hor_direction = None
tree.add_int_edge('swbox', cur_x, next_y, plane, src_dir, dest_dir, jump_plane=False)
cur_y = next_y
if __name__ == '__main__':
tree = ClockTree()
router = ClockRouter(9, 11)
router.route_swbox_to_swbox(tree, 8, 8, 0, 0, 0)