-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathverilog2phy.py
More file actions
573 lines (507 loc) · 19.8 KB
/
Copy pathverilog2phy.py
File metadata and controls
573 lines (507 loc) · 19.8 KB
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
import json
import numpy as np
class Rectangle:
"""
Representation of a physical rectangle object.
Parameters
----------
layer : str
Layout layer the rectangle exists on.
left_x : float
X-coordinate of left edge.
bot_y : float
Y-coordinate of bottom edge.
right_x : float
X-coordinate of right edge.
top_y : float
Y-coordinate of top edge.
orientation : {'horizontal', 'vertical'}
Orientation of the rectangle.
purpose : str
Layer purpose of the rectangle.
Attributes
----------
centroid
center
layer : str
Layout layer the rectangle exists on.
coords : list[float]
Rectangle coordinates. List follows the format of [left_x, bot_y,
right_x, top_y].
purpose : str
Layer purpose of the rectangle.
orientation : {'horizontal', 'vertical'}
Orientation of the rectangle.
"""
def __init__(self, layer, left_x, bot_y, right_x, top_y, orientation=None, purpose=['drawing']):
self.layer = layer
# Fixme: May want to change these rounding precisions to be a parameter
self.coords = [round(left_x, 3), round(bot_y, 3), round(right_x, 3), round(top_y, 3)]
self.purpose = purpose
self.orientation = orientation
def scale(self, scale_factor):
coord_arr = np.asarray(self.coords) * scale_factor
self.coords = coord_arr.tolist()
@property
def centroid(self):
"""
Centroid of the rectangle.
Returns
-------
tuple(float, float)
Coordinates of the rectangle's centroid.
"""
return (round((self.coords[0] + self.coords[2]) / 2, 3), round((self.coords[1] + self.coords[3]) / 2, 3))
@property
def center(self):
"""
Center coordinate of the rectangle. Depending on the orientation,
this could be either the x or y coordinate
Returns
-------
float
Center of the rectangle on the relevant side depending on
orientation. If orientation not specified, returns the centroid.
"""
if self.orientation == 'horizontal':
return self.centroid[1]
elif self.orientation == 'vertical':
return self.centroid[0]
else:
return self.centroid
class Label:
"""
Representation of a physical label object.
Parameters
----------
text : str
Text to be displayed on the label.
layer : str
Layout layer the label exists on.
postiion : list[float, float], tuple(float, float)
Coordinates for label location.
show : bool
True if label to actually be drawn. Default is True.
Attributes
----------
text : str
purpose : str
layer : str
coords : list[float, float]
show : bool
"""
def __init__(self, text, layer, position, show=True):
self.text = text
self.purpose = ['label']
self.layer = layer
self.coords = [round(position[0], 3), round(position[1], 3)]
self.show = show
def scale(self, scale_factor):
"""
Scales location coordinates by given scale factor.
Parameters
----------
scale_factor : float
Scaling factor to apply to coordinates.
Returns
-------
"""
coord_arr = np.asarray(self.coords) * scale_factor
self.coords = coord_arr.tolist()
class PHYObject:
"""
Generic Physical Object class. Parent to all other complex physical
objects.
Parameters
----------
name : str
Name of the object.
Attributes
----------
name : str
Name of the object.
purpose : str
Layer purpose of the object.
phys_objs : list
List of Primitive physical objects or Generic physical objects
that ths object contains.
rects : dict
Dictionary of Rectangle objects. Keyed by center coordinate value.
"""
def __init__(self, name):
self.name = name
self.purpose = None
self.phys_objs = []
self.rects = {}
def add_rect(self, layer, left_x=0, bot_y=0, right_x=0, top_y=0, purpose=['drawing']):
"""
Adds a Rectangle object to the list of child objects.
Parameters
----------
layer : str
left_x : float
X-coordinate of the left edge.
bot_y : float
Y-coordinate of the bottom edge.
right_x : float
X-coordinate of the right edge.
top_y : float
Y-coordinate of the top edge.
purpose : str
Layer purpose of the Rectangle.
Returns
-------
"""
rect_obj = Rectangle(layer, left_x, bot_y, right_x, top_y, purpose=purpose)
self.phys_objs.append(rect_obj)
self.rects[rect_obj.centroid] = rect_obj
def scale(self, scale_factor):
"""
Scales all objects associated with Pin by scale factor.
Parameters
----------
scale_factor : float
Factor by which to scale all physical objects in this Pin.
Returns
-------
"""
for phy_obj in self.phys_objs:
phy_obj.scale(scale_factor)
class PHYPortPin(PHYObject):
"""
Port Pin PHYObject.
Parameters
----------
pin_dict : dict
Pin descriptor dictionary. This dictionary will contain all the
relevant information regarding the pin direction, related power/
ground pins, and pin name.
layer : str
Layer the Pin object exists on.
side : str
Side of the design that the pin should exist on.
x_width : float
X dimension width of the pin shape.
y_width : float
Y dimension width of the pin shape.
center : float, optional
Center coordinate of the pin. Whether this is an X or Y coordinate
depends on side. This option actually doesn't serve any purpose.
bus_idx : int, optional
Index of bus element that this pin represents. This is only needed
if the pin is part of a bus. This index is appended onto the name
of the pin for labeling.
Attributes
----------
pin_dict : dict
Pin descriptor dictionary. This dictionary will contain all the
relevant information regarding the pin direction, related power/
ground pins, and pin name.
name : str
Name of the pin.
direction : {'input', 'output', 'inout'}
Port direction of pin.
layer : str
Layer the Pin object exists on.
side : str
Side of the design that the pin should exist on.
x_width : float
X dimension width of the pin shape.
y_width : float
Y dimension width of the pin shape.
center : float
Center coordinate of the pin.
related_power_pin : str
Name of the related power pin to this pin.
related_ground_pin : str
Name of the related ground pin to this pin.
bus_idx : int
Index of bus element that this pin represents. This is only needed
if the pin is part of a bus. This index is appended onto the name
of the pin for labeling.
block_structure : dict
This attribute is not used.
labels : list[Label]
List of Label objects associated with this pin object.
"""
def __init__(self, pin_dict, layer, side, x_width, y_width, center=None, bus_idx=None):
super().__init__(pin_dict['name'])
self.pin_dict = pin_dict
self.direction = pin_dict['direction']
self.layer = layer
self.side = side
self.x_width = x_width
self.y_width = y_width
self.center = center
self.related_power_pin = pin_dict.get('power_pin', None)
self.related_ground_pin = pin_dict.get('ground_pin', None)
# self.is_bus = pin_dict.get("is_bus", False)
if isinstance(bus_idx, int):
self.bus_idx = bus_idx
self.name = self.name + f'[{self.bus_idx}]'
self.block_structure = {}
self.labels = []
def add_rect(self, layer, left_x=0, bot_y=0, right_x=0, top_y=0, purpose=['drawing', 'pin']):
"""
Adds a Rectangle object to list of objects. This is different from
parent method add_rect in that this method calculates upper right
coordinate from x/y width attributes.
Parameters
----------
layer : str
Layout layer the Rectangle exists on.
left_x : float
X-coordinate of the left edge.
bot_y : float
Y-coordinate of the bottom edge.
right_x : float
X-coordinate of the right edge.
top_y : float
Y-coordinate of the top edge.
purpose : str
Layer purpose of the Rectangle.
Returns
-------
"""
right_x = left_x + self.x_width
top_y = bot_y + self.y_width
orientation = 'horizontal' if self.side in ['left', 'right'] else 'vertical'
rect_obj = Rectangle(layer, left_x, bot_y, right_x, top_y, orientation, purpose=purpose)
if rect_obj.center not in self.rects.keys():
self.phys_objs.append(rect_obj)
self.rects[rect_obj.center] = rect_obj
self.add_label(layer, ((right_x + left_x) / 2, (top_y + bot_y) / 2))
def add_label(self, layer, position):
"""
Adds a Label object to the pin.
Parameters
----------
layer : str
Layout layer the Label exists on.
position : list[float, float], tuple(float, float)
Coordinates of the Label object.
Returns
-------
"""
label_obj = Label(self.name, layer, position, show=True)
self.phys_objs.append(label_obj)
self.labels.append(label_obj)
class PHYDesign:
"""Python representation of a PHY Design. Right now this just consumes
VerilogModules.
Parameters
----------
verilog_module : VerilogModule
VerilogModule object that will be processed into a PHYDesign.
techfile : str, Path
Path to the HAMMER tech.json for the technology the PHYDesign is to
be made in.
spec_dict : dict
Dictionary of options and specifications for the design. Please
consult spec_dict_schema.yaml for an example of the spec_dict
schema.
Attributes
----------
verilog_pin_dict : dict
Copy of VerilogModule pin dictionary.
verilog_pg_pin_dict : dict
Copy of VerilogModule PG pin dictionary.
name : str
Name of the module.
spec_dict : dict
Input specification dictionary.
pins : list[PHYPortPin]
List of PortPin physical objects in this design.
pg_pins : list[PHYPortPin]
List of PortPin PG pin physical objects in this design.
defaults : dict
Default specifications to be overwritten.
specs : Specification
Specification() object used as common location to find spec dict.
x_width : float
Top-level x-coordinate width of design.
y_width : float
Top-level y-coordinate width of design.
polygons : dict
Flat hierarchy dictionary of polygons in design.
"""
def __init__(self, verilog_module, techfile, spec_dict=None):
self.verilog_pin_dict = verilog_module.pins
self.verilog_pg_pin_dict = verilog_module.power_pins
self.name = verilog_module.name
techfile = str(techfile) if not isinstance(techfile, str) else techfile
filetype = techfile.split('.')[-1].lower() # This normally expects a HAMMER tech.json
if filetype == 'json':
self._extract_tech_json_info(techfile)
elif filetype == 'yaml' or filetype == 'yml':
raise NotImplementedError
else:
raise ValueError(f"Unrecognized File Type .{filetype}")
self.spec_dict = spec_dict
self.pins = []
self.pg_pins = []
self.phys_objs = []
self.defaults = {'origin': [0, 0],
'units': 1e-6,
'precision': 1e-9,
'input_side': 'left',
'output_side': 'right',
'pin_margin': False,
'symmetry': 'X Y',
'site': 'core',
'design_boundary' : (10,10),
'bound_box': [0, 0, 10, 10],
'pins': {'h_layer': "M2",
'v_layer': "M3",
'pin_length': 1
},
'pg_pins': {'h_layer': "M2",
'v_layer': "M3",
'pwr_pin': {'layer': 'M3',
'center': None,
'side': 'top'},
'gnd_pin': {'layer': 'M3',
'center': None,
'side': 'top'}}
}
self.specs = self.defaults
if spec_dict:
self.specs.update(spec_dict)
self.x_width = self.specs.get('xwidth', None)
self.y_width = self.specs.get('ywidth', None)
# self.aspect_ratio = self.specs_dict.get('aspect_ratio', None)
self.polygons = {'pins': self.pins,
'pg_pins': self.pg_pins}
self.sig_figs = int(-(np.log10(self.specs['units']) - np.log10(self.specs['precision'])))
def _extract_tech_json_info(self, techfile):
"""
Extracts routing information from tech JSON to dictionaries.
Parameters
----------
techfile : Path
Path to tech.json file.
Returns
-------
"""
with open(techfile) as file:
self.tech_dict = json.load(file)
stackups = self.tech_dict['stackups'][0]['metals']
self.metals = {}
for layer in stackups:
self.metals[layer['name']] = layer
def add_pg_pin_objects(self):
"""
..deprecated : 0.9
This method has been superceded by the pin placer.
Returns
-------
"""
power_pin_name = self.verilog_pg_pin_dict['power_pin']
ground_pin_name = self.verilog_pg_pin_dict['ground_pin']
pg_pin_specs = self.specs['pg_pins']
pwr_pin_specs = self.specs['pg_pins']['pwr_pin']
gnd_pin_specs = self.specs['pg_pins']['gnd_pin']
if pwr_pin_specs['side'] == 'top' or pwr_pin_specs['side'] == 'bottom':
if not pwr_pin_specs.get('xwidth', None):
pwr_pin_specs['xwidth'] = self.metals[pg_pin_specs['v_layer']]['min_width']
if not pwr_pin_specs.get('ywidth', None):
pwr_pin_specs['ywidth'] = self.specs['pins']['pin_length']
else:
if not pwr_pin_specs.get('ywidth', None):
pwr_pin_specs['ywidth'] = self.metals[pg_pin_specs['v_layer']]['min_width']
if not pwr_pin_specs.get('xwidth', None):
pwr_pin_specs['xwidth'] = self.specs['pins']['pin_length']
if gnd_pin_specs['side'] == 'top' or gnd_pin_specs['side'] == 'bottom':
if not gnd_pin_specs.get('xwidth', None):
gnd_pin_specs['xwidth'] = self.metals[gnd_pin_specs['h_layer']]['min_width']
if not gnd_pin_specs.get('ywidth', None):
gnd_pin_specs['ywidth'] = self.specs['pins']['pin_length']
else:
if not gnd_pin_specs.get('ywidth', None):
gnd_pin_specs['ywidth'] = self.metals[gnd_pin_specs['h_layer']]['min_width']
if not gnd_pin_specs.get('xwidth', None):
gnd_pin_specs['xwidth'] = self.specs['pins']['pin_length']
pwr_pin_dict = {'name': power_pin_name,
'direction': 'inout',
'is_analog': False}
gnd_pin_dict = {'name': ground_pin_name,
'direction': 'inout',
'is_analog': False}
pwr_pin = PHYPortPin(pwr_pin_dict, pwr_pin_specs['layer'],
pwr_pin_specs['side'],
round(pwr_pin_specs['xwidth'], 3),
round(pwr_pin_specs['ywidth'], 3),
center=pg_pin_specs['pwr_pin'].get('center', None))
gnd_pin = PHYPortPin(gnd_pin_dict, pg_pin_specs['gnd_pin']['layer'],
pg_pin_specs['gnd_pin']['side'],
round(pg_pin_specs['gnd_pin']['xwidth'], 3),
round(pg_pin_specs['gnd_pin']['ywidth'], 3),
center=pg_pin_specs['gnd_pin'].get('center', None))
pwr_pin.type = 'POWER'
gnd_pin.type = 'GROUND'
self.pg_pins['pwr'] = pwr_pin
self.pg_pins['gnd'] = gnd_pin
self.phys_objs.append(pwr_pin)
self.phys_objs.append(gnd_pin)
def add_pin_objects(self):
"""
..deprecated : 0.9
This method has been superceded by the pin placer.
Returns
-------
"""
self.n_inputs = 0
self.n_outputs = 0
for pin_name, pin_info in self.verilog_pin_dict.items():
pin_spec = self.specs['pins'].get(pin_name, None)
direction = pin_info['direction']
# Pin side definitions
if pin_spec:
side = pin_spec.get('side', None)
else:
side = None
if not side:
side = self.specs.get(f'{direction}_side', None)
if not side:
side = self.specs['input_side'] if direction == 'input' else self.specs['output_side']
# Pin Layer definitions
if side == 'left' or side == 'right':
layer = self.specs['pins']['h_layer']
if pin_spec:
x_width = pin_spec.get('x_width', self.specs['pins']['pin_length'])
y_width = pin_spec.get('y_width', self.metals[layer]['min_width'])
else:
x_width = self.specs['pins']['pin_length']
y_width = self.metals[layer]['min_width']
else:
layer = self.specs['pins']['v_layer']
if pin_spec:
x_width = pin_spec.get('x_width', self.specs['pins']['pin_length'])
y_width = pin_spec.get('y_width', self.metals[layer]['min_width'])
else:
x_width = 'x_width', self.specs['pins']['pin_length']
y_width = 'y_width', self.metals[layer]['min_width']
if pin_info.get('is_bus', None):
self.n_inputs += (direction == 'input') * (pin_info['bus_max'] + 1)
self.n_outputs += (direction == 'output') * (pin_info['bus_max'] + 1)
for pin in range(pin_info['bus_max'] + 1):
self.pins[pin_name + '[' + str(pin) + ']'] = PHYPortPin(pin_info, layer, side, round(x_width, 3),
round(y_width, 3),
bus_idx=pin)
else:
self.n_inputs += direction == 'input'
self.n_outputs += direction == 'output'
self.pins[pin_name] = PHYPortPin(pin_info, layer, side, round(x_width, 3), round(y_width, 3))
self.phys_objs += list(self.pins.values())
def define_design_boundaries(self):
pass
def scale(self, scale_factor = 1):
scaled = []
for obj in self.phys_objs:
obj.scale(scale_factor)
try:
self.x_width = self.specs['x_width']
self.y_width = self.specs['y_width']
except KeyError:
self.x_width = round(self.x_width * scale_factor, 3)
self.y_width = round(self.y_width * scale_factor, 3)