-
Notifications
You must be signed in to change notification settings - Fork 0
/
chip.py
195 lines (175 loc) · 7.74 KB
/
chip.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
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
import os
from typing import Union
from config import ROUTER_CONFIG
from node.grid import Grid, GridType
from node.hub import Hub
from node.tile import Tile
try:
__location__ = os.path.realpath(
os.path.join(os.getcwd(), os.path.dirname(__file__)))
except:
__location__ = '/Users/tommy/Documents/tommy/nr-router'
class ChipSection():
def __init__(self, start_point: tuple, width: int, height: int, unit: float, radius: float):
self.start_point: tuple = start_point
self.width: int = width
self.height: int = height
self.unit: int = unit
self.hypo_unit: int = int(unit * 2) - 1 # int(unit*2) - 1 # 2 * unit - 1 # unit * math.sqrt(2)
self.radius: int = radius
self.grid: list[list[Grid]] = []
self.tile: list[list[Tile]] = []
self.hub: list[Hub] = []
self.hub_gap: int = 1040 // ROUTER_CONFIG.HUB_NUM
def init_grid(self, grid_type=GridType.GRID, ref_pin=None, corner_pin=None):
self.grid = []
for i in range(self.width // self.unit + 1):
self.grid.append([])
for j in range(self.height // self.unit + 1):
if ref_pin and [i, j] in ref_pin:
self.grid[i].append(Grid(i * self.unit + self.start_point[0], j * self.unit + self.start_point[1], i, j, GridType.REF))
elif corner_pin and [i, j] in corner_pin:
self.grid[i].append(Grid(i * self.unit + self.start_point[0], j * self.unit + self.start_point[1], i, j, GridType.CORNER))
else:
self.grid[i].append(Grid(i * self.unit + self.start_point[0], j * self.unit + self.start_point[1], i, j, grid_type))
def init_tile(self):
self.tile = []
for i in range(self.width // self.unit):
self.tile.append([])
for j in range(self.height // self.unit):
self.tile[i].append(Tile(i * self.unit + self.start_point[0] + (self.unit / 2),
j * self.unit + self.start_point[1] + (self.unit / 2), i, j))
def init_hub(self, y: float):
self.hub = []
for i in range((self.width // self.unit) + (ROUTER_CONFIG.HUB_NUM - 1) * (self.width // self.unit - 1) + ROUTER_CONFIG.HUB_NUM):
if i % ROUTER_CONFIG.HUB_NUM == 0:
# grid
self.hub.append(Hub(self.grid[i // ROUTER_CONFIG.HUB_NUM][0].real_x, y, 0, i))
else:
# tile
offset = self.radius + (i % ROUTER_CONFIG.HUB_NUM) * self.hub_gap
self.hub.append(Hub(self.grid[i // ROUTER_CONFIG.HUB_NUM][0].real_x + offset, y, 1, i))
def get_grid(self, x: int, y: int) -> Union[Grid, None]:
try:
return self.grid[x][y]
except:
return None
def is_edge_grid(self, x: int, y: int) -> bool:
if x == len(self.grid) or y == len(self.grid[0]):
return True
if x == 0 or y == 0:
return False
class Chip():
def __init__(self, ewd_name: str, ewd_content: str = None):
self.ewd_name = ewd_name
self.ewd_content = ewd_content
self.ewd_config_end = 0
self.radius = 0
self.electrode_shape_library = {}
self.electrode_shape_count = 0
# contactpad_list = [[x, y], etc.]
self.contactpad_list: list[list] = []
# electrode_list = [[shape, x, y], etc.]
self.electrode_list: list[list] = []
self.top_section: ChipSection = None
self.mid_section: ChipSection = None
self.bottom_section: ChipSection = None
def setup(self):
"""Read ewd file and setup chip.
"""
if self.ewd_content is None:
self.ewd_content = self.read_ewd()
else:
self.ewd_content: str = self.ewd_content.split('\n')
self.get_config()
self.get_position()
self.init_section()
def read_ewd(self) -> str:
"""Read ewd file.
Returns:
str: ewd input content
"""
ewd_input = []
dir = os.path.join(__location__, 'ewd/' + self.ewd_name)
readfile = open(dir, "r")
for line in readfile:
ewd_input.append(line)
return ewd_input
def get_config(self):
"""Get chip config from ewd file.
"""
content: list[str] = self.ewd_content
for line in content:
if line.split()[0] == "#ENDOFDEFINITION#":
self.ewd_config_end = content.index(line)
break
elif line.split()[0] == "contactpad" and line.split()[1] == "circle":
self.radius = int(line.split()[3])
elif len(line.split()) > 1 and line.split()[1] == "path":
shape_name = line.split()[0]
shape_scope = []
for i in range(2, len(line.split())-1, 2):
t_x = line.split()[i][1:]
t_y = line.split()[i+1]
shape_scope.append((t_x, t_y))
shape_scope.append((int(line.split()[2][1:]), int(line.split()[3])))
self.electrode_shape_library[shape_name] = shape_scope
self.electrode_shape_count = len(self.electrode_shape_library.keys())
def get_position(self):
"""Get contact pad and electrode position from ewd file.
"""
content: list[str] = self.ewd_content[self.ewd_config_end + 1:]
for line in content:
if line.split()[0] == "#ENDOFLAYOUT#":
break
true_x = float(line.split()[1])
true_y = float(line.split()[2])
# contact pad
if line.split()[0] == "contactpad":
self.contactpad_list.append([true_x, true_y])
# electrodes
elif line.split()[0] in self.electrode_shape_library:
self.electrode_list.append([line.split()[0], true_x, true_y])
def init_section(self):
"""Init chip section.
"""
self.top_section = ChipSection(
start_point = ROUTER_CONFIG.TOP_START_POINT,
width = ROUTER_CONFIG.CONTACT_PAD_GAP * 31,
height = ROUTER_CONFIG.CONTACT_PAD_GAP * 3,
unit = ROUTER_CONFIG.CONTACT_PAD_GAP,
radius = ROUTER_CONFIG.CONTACT_PAD_RADIUS
)
self.top_section.init_grid(
grid_type = GridType.CONTACT_PAD,
ref_pin = ROUTER_CONFIG.TOP_SECTION_REF_PIN,
corner_pin = ROUTER_CONFIG.TOP_SECTION_CORNER_PIN
)
self.top_section.init_tile()
self.top_section.init_hub(
y = (ROUTER_CONFIG.MID_START_POINT[1] + ROUTER_CONFIG.CONTACT_PAD_GAP * 3 + ROUTER_CONFIG.CONTACT_PAD_RADIUS) // 2
)
self.mid_section = ChipSection(
start_point = ROUTER_CONFIG.MID_START_POINT,
width = ROUTER_CONFIG.ELECTRODE_SECTION[0],
height = ROUTER_CONFIG.ELECTRODE_SECTION[1],
unit = ROUTER_CONFIG.TILE_UNIT,
radius = ROUTER_CONFIG.CONTACT_PAD_RADIUS
)
self.mid_section.init_grid()
self.bottom_section = ChipSection(
start_point = ROUTER_CONFIG.BOTTOM_START_POINT,
width = ROUTER_CONFIG.CONTACT_PAD_GAP * 31,
height = ROUTER_CONFIG.CONTACT_PAD_GAP * 3,
unit = ROUTER_CONFIG.CONTACT_PAD_GAP,
radius = ROUTER_CONFIG.CONTACT_PAD_RADIUS
)
self.bottom_section.init_grid(
grid_type = GridType.CONTACT_PAD,
ref_pin = ROUTER_CONFIG.BOTTOM_SECTION_REF_PIN,
corner_pin = ROUTER_CONFIG.BOTTOM_SECTION_CORNER_PIN
)
self.bottom_section.init_tile()
self.bottom_section.init_hub(
y = (ROUTER_CONFIG.BOTTOM_START_POINT[1] - ROUTER_CONFIG.CONTACT_PAD_RADIUS + ROUTER_CONFIG.MID_START_POINT[1] + ROUTER_CONFIG.ELECTRODE_SECTION[1]) // 2
)