-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_bookshelf.py
executable file
·350 lines (320 loc) · 8.22 KB
/
load_bookshelf.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import sys
import yaml
import json
import numpy as np
import math
import ast
import utils
import shapely
from shapely.geometry import Point, Polygon
from shapely.affinity import translate, rotate
def read_pl(fname):
"""
Read & parse .pl (placement) file
:param fname: .pl filename
"""
with open(fname,'r') as f:
lines = f.read().splitlines()
lines = lines[5:]
print(lines)
components = {}
board_pins = {}
bp = 0
for line in lines:
if line == '':
bp = 1
continue
if bp == 0:
l = line.split()
cname = l[0]
dims = l[5]+l[6]
dims = [float(i.strip()) for i in dims[1:-1].split(",")]
x = float(l[1].strip())
y = float(l[2].strip())
poly = Polygon([[x,y], \
[x + dims[0],y], \
[x + dims[0],y+dims[1]], \
[x,y+dims[1]]])
components[cname] = poly
else:
l = line.split()
pname = l[0]
coords = Point([float(l[1].strip()),float(l[2].strip())])
board_pins[pname] = coords
return components, board_pins
def read_pl2(fname,components):
"""
Read & parse .pl (placement) file
:param fname: .pl filename
"""
with open(fname,'r') as f:
lines = f.read().splitlines()
lines = lines[5:]
bp = 0
comp2rot = {}
board_pins = {}
for line in lines:
if line == '':
bp = 1
continue
else:
if bp == 0:
l = line.split()
pname = l[0].strip()
minx, miny, maxx, maxy = components[pname].bounds
newx,newy = (float(l[1]),float(l[2]))
r = l[4]
comp2rot[pname] = r
rot2deg = {'N':0,'S':180,'E':90,'W':270}
components[pname] = rotate(components[pname],rot2deg[r])
minx, miny, maxx, maxy = components[pname].bounds
components[pname] = translate(components[pname],newx-minx,newy-miny)
else:
l = line.split()
pname = l[0]
coords = Point([float(l[1].strip()),float(l[2].strip())])
board_pins[pname] = coords
return components, comp2rot, board_pins
def read_nets(fname,components,board_pins):
"""
Read & parse .nets (netlist) file
:param fname: .nets filename
"""
nets = []
mod2net = {} # {component: [nets]}
i = -1
with open(fname,'r') as f:
lines = f.read().splitlines()
target_ibdex = [i for i, s in enumerate(lines) if 'NetDegree' in s][0]
lines = lines[target_ibdex:]
t = -1
for line in lines:
if '#' in line:
continue
if 'NetDegree' in line:
i += 1
nets.append([])
continue
l = line.split()
if len(l) < 3:
pin_name = l[0]
pin = board_pins[pin_name]
nets[i].append([pin_name,pin])
else:
pin_name = l[0]
#if t == 0:
local_pin_loc = [float(l[3].replace('%',''))/100.0 + 0.5, float(l[4].replace('%',''))/100.0 + 0.5]
#pinx,piny = utils.pin_pos([pin_name, local_pin_loc], components)
#print(pin_name, local_pin_loc, pinx, piny)
#pin = Point([pinx,piny])
"""
else:
minx,miny,maxx,maxy = components[pin_name].bounds
w = (maxx - minx)
h = (maxy - miny)
pinx = (float(l[3][1:]) - minx)/(100*w) + 0.5
piny = (float(l[4][1:]) - miny)/(100*h) + 0.5
local_pin_loc = [pinx, piny]
"""
pin = local_pin_loc
nets[i].append([pin_name, pin])
if pin_name in mod2net:
mod2net[pin_name].append(i)
else:
mod2net[pin_name] = [i]
return nets, mod2net
def read_nets2(fname,components,board_pins):
"""
Read & parse .nets (netlist) file
:param fname: .nets filename
"""
nets = []
mod2net = {} # {component: [nets]}
i = -1
with open(fname,'r') as f:
lines = f.read().splitlines()
target_ibdex = [i for i, s in enumerate(lines) if 'NetDegree' in s][0]
lines = lines[target_ibdex:]
t = -1
for line in lines:
if '#' in line:
continue
if 'NetDegree' in line:
i += 1
nets.append([])
continue
l = line.split()
if len(l) < 3:
pin_name = l[0]
pin = board_pins[pin_name]
nets[i].append([pin_name,pin])
else:
pin_name = l[0]
local_pin_loc = [float(l[3]), float(l[4])]
pin = local_pin_loc
nets[i].append([pin_name, pin])
if pin_name in mod2net:
mod2net[pin_name].append(i)
else:
mod2net[pin_name] = [i]
return nets, mod2net
def read_blocks(fname):
"""
Read & parse .blocks file
:param fname: .blocks filename
"""
blocks = {}
with open(fname, 'r') as f:
lines = f.read().splitlines()[9:]
components = {}
bp = 0
for line in lines:
if line == '':
bp = 1
continue
if bp == 0:
l = line.split()
cname = l[0]
vstring = ' '.join(l[3:])
vstring = '[' + vstring.replace(') (', '),(') + ']'
vertices = ast.literal_eval(vstring)
poly = Polygon(vertices)
components[cname] = poly
return components
def read_nodes(fname):
"""
Read & parse .nodes (blocks) file
:param fname: .nodes filename
"""
blocks = {}
with open(fname, 'r') as f:
lines = f.read().splitlines()
target_ibdex = [i for i, s in enumerate(lines) if 'NumTerminals' in s][0]
lines = lines[target_ibdex+2:]
components = {}
bp = 0
for line in lines:
if line == '':
bp = 1
continue
if bp == 0:
l = line.split()
cname = l[0]
w = float(l[1])
h = float(l[2])
poly = Polygon([[0,0],[w,0],[w,h],[0,h]])
components[cname] = poly
return components
def write_pl(fname,components,board_pins):
with open(fname,'w') as f:
f.write('UMICH blocks 1.0\n')
f.write('\n')
f.write('\n')
f.write('\n')
for cname in components:
component = components[cname]
f.write(cname)
f.write('\t')
minx,miny,maxx,maxy = component.bounds
f.write(str(minx))
f.write('\t')
f.write(str(miny))
f.write('\t')
f.write('DIMS = (' + str(maxx - minx) + ', ' + str(maxy - miny) + ')')
f.write(' : N\n')
f.write('\n')
for pname in board_pins:
if pname in components:
pass
pin = board_pins[pname]
f.write(pname)
f.write('\t')
f.write(str(pin.x))
f.write('\t')
f.write(str(pin.y))
f.write('\t')
f.write(' : N\n')
def write_nodes(fname,components,board_pins):
with open(fname,'w') as f:
f.write('UCLA blocks 1.0\n')
f.write('\n')
f.write('\n')
f.write('\n')
f.write('NumNodes\t :\t ' + str(len(set([c for c in components]))))
f.write('\n')
f.write('NumTerminals\t :\t ' + str(len(set([b for b in board_pins if b not in components]))))
f.write('\n')
for cname in components:
component = components[cname]
f.write(cname)
f.write('\t')
minx,miny,maxx,maxy = component.bounds
f.write(str(maxx-minx))
f.write('\t')
f.write(str(maxy-miny))
f.write('\n')
def write_newnets(fname,nets,components):
with open(fname,'w') as f:
f.write('UCLA nets 1.0\n')
f.write('\n')
f.write('\n')
f.write('\n')
f.write('NumNets\t :\t ' + str(len(nets)))
f.write('\n')
f.write('NumPins\t :\t ' + str(len(set([pin[0] for net in nets for pin in net]))))
f.write('\n')
for net in nets:
f.write('NetDegree : ' + str(len(net)) + '\n')
for pin in net:
pinname, pinloc = pin
if isinstance(pinloc, list): # component pin
pinx,piny = utils.pin_pos(pin, components)
f.write(pinname + ' B : ' + str(round(pinx,2)) + ' ' + str(round(piny,2)) + '\n')
else: # terminal pin
f.write(pinname + ' B \n')
def write_newpl(fname,components,board_pins):
with open(fname,'w') as f:
f.write('UCLA pl 1.0\n')
f.write('\n')
f.write('\n')
f.write('\n')
for cname in components:
component = components[cname]
f.write(cname)
f.write('\t')
minx,miny,maxx,maxy = component.bounds
f.write(str(minx))
f.write('\t')
f.write(str(miny))
f.write('\t')
#f.write('DIMS = (' + str(maxx - minx) + ', ' + str(maxy - miny) + ')')
f.write(': N\n')
f.write('\n')
for pname in board_pins:
if pname in components:
pass
pin = board_pins[pname]
f.write(pname)
f.write('\t')
f.write(str(pin.x))
f.write('\t')
f.write(str(pin.y))
f.write('\t')
f.write(' : N\n')
#=blocksfile = '/Users/orange3xchicken/Downloads/merrill_place_example_1.blocks'
#blk = read_blocks(blocksfile)
#print(blk)
#print(len(blk))
#plfile = sys.argv[1]
#netsfile = sys.argv[2]
#blocksfile = sys.argv[3]
"""
components: dictionary of placed component-polygons indexed by their name
placed_components: dictionary of placed component-polygons indexed by their name
board_pins: dictionary of static point-pins indexed by their name
nets: list of k nets, each net is a list of n point-pins
"""
#components = read_blocks(blocksfile)
#placed_components, board_pins = read_pl(plfile)
#nets = read_nets(netsfile, components, board_pins)
#print('pcb data loaded')