-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathhierarchy_layout.py
More file actions
2431 lines (2106 loc) · 95.4 KB
/
hierarchy_layout.py
File metadata and controls
2431 lines (2106 loc) · 95.4 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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# See LICENSE for licensing information.
#
# Copyright (c) 2016-2024 Regents of the University of California and The Board
# of Regents for the Oklahoma Agricultural and Mechanical College
# (acting for and on behalf of Oklahoma State University)
# All rights reserved.
#
import sys
import os
import re
from math import sqrt
from openram import debug
from openram.gdsMill import gdsMill
from openram import tech
from openram.tech import drc, GDS
from openram.tech import layer as tech_layer
from openram.tech import layer_indices as tech_layer_indices
from openram.tech import preferred_directions
from openram.tech import layer_stacks as tech_layer_stacks
from openram.tech import active_stack as tech_active_stack
from openram.sram_factory import factory
from openram import OPTS
from .vector import vector
from .pin_layout import pin_layout
from .utils import round_to_grid, ceil
from . import geometry
try:
from openram.tech import special_purposes
except ImportError:
special_purposes = {}
class layout():
"""
Class consisting of a set of objs and instances for a module
This provides a set of useful generic types for hierarchy
management. If a module is a custom designed cell, it will read from
the GDS and spice files and perform LVS/DRC. If it is dynamically
generated, it should implement a constructor to create the
layout/netlist and perform LVS/DRC.
"""
def __init__(self, name, cell_name):
# This gets set in both spice and layout so either can be called first.
self.name = name
self.cell_name = cell_name
self.gds_file = OPTS.openram_tech + "gds_lib/" + cell_name + ".gds"
self.is_library_cell = os.path.isfile(self.gds_file)
self.width = None
self.height = None
self.bounding_box = None # The rectangle shape
self.bbox = None # The ll, ur coords
# Holds module/cell layout instances
self.insts = []
# Set of names to check for duplicates
self.inst_names = set()
# Holds all other objects (labels, geometries, etc)
self.objs = []
# This is a mapping of internal pin names to cell pin names
# If the key is not found, the internal pin names is assumed
self.pin_names = {}
# Holds name->pin_layout map for all pins
self.pin_map = {}
# List of modules we have already visited
self.visited = []
self.gds_read()
if "contact" not in self.name:
if not hasattr(layout, "_drc_constants"):
layout._drc_constants = True
layout.setup_drc_constants()
layout.setup_contacts()
layout.setup_layer_constants()
@classmethod
def setup_drc_constants(layout):
"""
These are some DRC constants used in many places
in the compiler.
"""
# Make some local rules for convenience
for rule in drc.keys():
# Single layer width rules
match = re.search(r"minwidth_(.*)", rule)
if match:
if match.group(1) == "active_contact":
setattr(layout, "contact_width", drc(match.group(0)))
else:
setattr(layout, match.group(1) + "_width", drc(match.group(0)))
# Single layer area rules
match = re.search(r"minarea_(.*)", rule)
if match:
setattr(layout, match.group(0), drc(match.group(0)))
# Single layer spacing rules
match = re.search(r"(.*)_to_(.*)", rule)
if match and match.group(1) == match.group(2):
setattr(layout, match.group(1) + "_space", drc(match.group(0)))
elif match and match.group(1) != match.group(2):
if match.group(2) == "poly_active":
setattr(layout, match.group(1) + "_to_contact",
drc(match.group(0)))
else:
setattr(layout, match.group(0), drc(match.group(0)))
match = re.search(r"(.*)_enclose_(.*)", rule)
if match:
setattr(layout, match.group(0), drc(match.group(0)))
match = re.search(r"(.*)_extend_(.*)", rule)
if match:
setattr(layout, match.group(0), drc(match.group(0)))
# Create the maximum well extend active that gets used
# by cells to extend the wells for interaction with other cells
layout.well_extend_active = 0
if "nwell" in tech_layer:
layout.well_extend_active = max(layout.well_extend_active, layout.nwell_extend_active)
if "pwell" in tech_layer:
layout.well_extend_active = max(layout.well_extend_active, layout.pwell_extend_active)
# The active offset is due to the well extension
if "pwell" in tech_layer:
layout.pwell_enclose_active = drc("pwell_enclose_active")
else:
layout.pwell_enclose_active = 0
if "nwell" in tech_layer:
layout.nwell_enclose_active = drc("nwell_enclose_active")
else:
layout.nwell_enclose_active = 0
# Use the max of either so that the poly gates will align properly
layout.well_enclose_active = max(layout.pwell_enclose_active,
layout.nwell_enclose_active,
layout.active_space)
# These are for debugging previous manual rules
level=99
debug.info(level, "poly_width".format(layout.poly_width))
debug.info(level, "poly_space".format(layout.poly_space))
debug.info(level, "m1_width".format(layout.m1_width))
debug.info(level, "m1_space".format(layout.m1_space))
debug.info(level, "m2_width".format(layout.m2_width))
debug.info(level, "m2_space".format(layout.m2_space))
debug.info(level, "m3_width".format(layout.m3_width))
debug.info(level, "m3_space".format(layout.m3_space))
debug.info(level, "m4_width".format(layout.m4_width))
debug.info(level, "m4_space".format(layout.m4_space))
debug.info(level, "active_width".format(layout.active_width))
debug.info(level, "active_space".format(layout.active_space))
debug.info(level, "contact_width".format(layout.contact_width))
debug.info(level, "poly_to_active".format(layout.poly_to_active))
debug.info(level, "poly_extend_active".format(layout.poly_extend_active))
debug.info(level, "poly_to_contact".format(layout.poly_to_contact))
debug.info(level, "active_contact_to_gate".format(layout.active_contact_to_gate))
debug.info(level, "poly_contact_to_gate".format(layout.poly_contact_to_gate))
debug.info(level, "well_enclose_active".format(layout.well_enclose_active))
debug.info(level, "implant_enclose_active".format(layout.implant_enclose_active))
debug.info(level, "implant_space".format(layout.implant_space))
@classmethod
def setup_layer_constants(layout):
"""
These are some layer constants used
in many places in the compiler.
"""
try:
from openram.tech import power_grid
layout.pwr_grid_layers = [power_grid[0], power_grid[2]]
except ImportError:
layout.pwr_grid_layers = ["m3", "m4"]
for layer_id in tech_layer_indices:
key = "{}_stack".format(layer_id)
# Set the stack as a local helper
try:
layer_stack = getattr(tech, key)
setattr(layout, key, layer_stack)
except AttributeError:
pass
# Skip computing the pitch for non-routing layers
if layer_id in ["active", "nwell", "pwell"]:
continue
# Add the pitch
setattr(layout,
"{}_pitch".format(layer_id),
layout.compute_pitch(layer_id, True))
# Add the non-preferrd pitch (which has vias in the "wrong" way)
setattr(layout,
"{}_nonpref_pitch".format(layer_id),
layout.compute_pitch(layer_id, False))
level=99
for name in tech_layer_indices:
if name == "active":
continue
try:
debug.info(level, "{0} width {1} space {2}".format(name,
getattr(layout, "{}_width".format(name)),
getattr(layout, "{}_space".format(name))))
debug.info(level, "pitch {0} nonpref {1}".format(getattr(layout, "{}_pitch".format(name)),
getattr(layout, "{}_nonpref_pitch".format(name))))
except AttributeError:
pass
@staticmethod
def compute_pitch(layer, preferred=True):
"""
This is the preferred direction pitch
i.e. we take the minimum or maximum contact dimension
"""
# Find the layer stacks this is used in
pitches = []
for stack in tech_layer_stacks:
# Compute the pitch with both vias above and below (if they exist)
if stack[0] == layer:
pitches.append(layout.compute_layer_pitch(stack, preferred))
if stack[2] == layer:
pitches.append(layout.compute_layer_pitch(stack[::-1], True))
return max(pitches)
@staticmethod
def get_preferred_direction(layer):
return preferred_directions[layer]
@staticmethod
def compute_layer_pitch(layer_stack, preferred):
(layer1, via, layer2) = layer_stack
try:
if layer1 == "poly" or layer1 == "active":
contact1 = getattr(layout, layer1 + "_contact")
else:
contact1 = getattr(layout, layer1 + "_via")
except AttributeError:
contact1 = getattr(layout, layer2 + "_via")
if preferred:
if preferred_directions[layer1] == "V":
contact_width = contact1.first_layer_width
else:
contact_width = contact1.first_layer_height
else:
if preferred_directions[layer1] == "V":
contact_width = contact1.first_layer_height
else:
contact_width = contact1.first_layer_width
layer_space = getattr(layout, layer1 + "_space")
pitch = contact_width + layer_space
return round_to_grid(pitch)
@classmethod
def setup_contacts(layout):
# Set up a static for each layer to be used for measurements
# unless we are a contact class!
for layer_stack in tech_layer_stacks:
(layer1, via, layer2) = layer_stack
cont = factory.create(module_type="contact",
layer_stack=layer_stack)
module = sys.modules[__name__]
# Also create a contact that is just the first layer
if layer1 == "poly" or layer1 == "active":
setattr(layout, layer1 + "_contact", cont)
else:
setattr(layout, layer1 + "_via", cont)
# Set up a static for each well contact for measurements
if "nwell" in tech_layer:
cont = factory.create(module_type="contact",
layer_stack=tech_active_stack,
implant_type="n",
well_type="n")
module = sys.modules[__name__]
setattr(layout, "nwell_contact", cont)
if "pwell" in tech_layer:
cont = factory.create(module_type="contact",
layer_stack=tech_active_stack,
implant_type="p",
well_type="p")
module = sys.modules[__name__]
setattr(layout, "pwell_contact", cont)
############################################################
# GDS layout
############################################################
def offset_all_coordinates(self, offset=None):
"""
This function is called after everything is placed to
shift the origin in the lowest left corner
"""
if not offset:
offset = vector(0, 0)
ll = self.find_lowest_coords()
real_offset = ll + offset
self.translate_all(real_offset)
return real_offset
def offset_x_coordinates(self, offset=None):
"""
This function is called after everything is placed to
shift the origin to the furthest left point.
Y offset is unchanged.
"""
if not offset:
offset = vector(0, 0)
ll = self.find_lowest_coords()
real_offset = ll.scale(1, 0) + offset
self.translate_all(real_offset)
return real_offset
def get_gate_offset(self, x_offset, height, inv_num):
"""
Gets the base offset and y orientation of stacked rows of gates
assuming a minwidth metal1 vdd/gnd rail. Input is which gate
in the stack from 0..n
"""
if (inv_num % 2 == 0):
base_offset = vector(x_offset, inv_num * height)
y_dir = 1
else:
# we lose a rail after every 2 gates
base_offset = vector(x_offset,
(inv_num + 1) * height - \
(inv_num % 2) * drc["minwidth_m1"])
y_dir = -1
return (base_offset, y_dir)
def find_lowest_coords(self):
"""
Finds the lowest set of 2d cartesian coordinates within
this layout
"""
lowestx = lowesty = sys.maxsize
if len(self.objs) > 0:
lowestx = min(min(obj.lx() for obj in self.objs if obj.name != "label"), lowestx)
lowesty = min(min(obj.by() for obj in self.objs if obj.name != "label"), lowesty)
if len(self.insts) > 0:
lowestx = min(min(inst.lx() for inst in self.insts), lowestx)
lowesty = min(min(inst.by() for inst in self.insts), lowesty)
if len(self.pin_map) > 0:
for pin_set in self.pin_map.values():
if len(pin_set) == 0:
continue
lowestx = min(min(pin.lx() for pin in pin_set), lowestx)
lowesty = min(min(pin.by() for pin in pin_set), lowesty)
return vector(lowestx, lowesty)
def find_highest_coords(self):
"""
Finds the highest set of 2d cartesian coordinates within
this layout
"""
highestx = highesty = -sys.maxsize - 1
if len(self.objs) > 0:
highestx = max(max(obj.rx() for obj in self.objs if obj.name != "label"), highestx)
highesty = max(max(obj.uy() for obj in self.objs if obj.name != "label"), highesty)
if len(self.insts) > 0:
highestx = max(max(inst.rx() for inst in self.insts), highestx)
highesty = max(max(inst.uy() for inst in self.insts), highesty)
if len(self.pin_map) > 0:
for pin_set in self.pin_map.values():
if len(pin_set) == 0:
continue
highestx = max(max(pin.rx() for pin in pin_set), highestx)
highesty = max(max(pin.uy() for pin in pin_set), highesty)
return vector(highestx, highesty)
def find_highest_layer_coords(self, layer):
"""
Finds the highest set of 2d cartesian coordinates within
this layout on a layer
"""
# Only consider the layer not the purpose for now
layerNumber = tech_layer[layer][0]
try:
highestx = max(obj.rx() for obj in self.objs if obj.layerNumber == layerNumber)
except ValueError:
highestx =0
try:
highesty = max(obj.uy() for obj in self.objs if obj.layerNumber == layerNumber)
except ValueError:
highesty = 0
for inst in self.insts:
# This really should be rotated/mirrored etc...
subcoord = inst.mod.find_highest_layer_coords(layer) + inst.offset
highestx = max(highestx, subcoord.x)
highesty = max(highesty, subcoord.y)
return vector(highestx, highesty)
def find_lowest_layer_coords(self, layer):
"""
Finds the highest set of 2d cartesian coordinates within
this layout on a layer
"""
# Only consider the layer not the purpose for now
layerNumber = tech_layer[layer][0]
try:
lowestx = min(obj.lx() for obj in self.objs if obj.layerNumber == layerNumber)
except ValueError:
lowestx = 0
try:
lowesty = min(obj.by() for obj in self.objs if obj.layerNumber == layerNumber)
except ValueError:
lowesty = 0
for inst in self.insts:
# This really should be rotated/mirrored etc...
subcoord = inst.mod.find_lowest_layer_coords(layer) + inst.offset
lowestx = min(lowestx, subcoord.x)
lowesty = min(lowesty, subcoord.y)
return vector(lowestx, lowesty)
def translate_all(self, offset):
"""
Translates all objects, instances, and pins by the given (x,y) offset
"""
for obj in self.objs:
obj.offset = vector(obj.offset - offset)
for inst in self.insts:
inst.offset = vector(inst.offset - offset)
# The instances have a precomputed boundary that we need to update.
if inst.__class__.__name__ == "instance":
inst.compute_boundary(inst.offset, inst.mirror, inst.rotate)
for pin_name in self.pin_map.keys():
# All the pins are absolute coordinates that need to be updated.
pin_list = self.pin_map[pin_name]
for pin in pin_list:
pin.rect = [pin.ll() - offset, pin.ur() - offset]
def add_inst(self, name, mod, offset=[0, 0], mirror="R0", rotate=0):
""" Adds an instance of a mod to this module """
# Contacts are not really instances, so skip them
if "contact" not in mod.name:
# Check that the instance name is unique
debug.check(name not in self.inst_names, "Duplicate named instance in {0}: {1}".format(self.cell_name, name))
self.mods.add(mod)
self.inst_names.add(name)
self.insts.append(geometry.instance(name, mod, offset, mirror, rotate))
debug.info(3, "adding instance {}".format(self.insts[-1]))
# This is commented out for runtime reasons
# debug.info(4, "instance list: " + ",".join(x.name for x in self.insts))
return self.insts[-1]
def get_inst(self, name):
""" Retrieve an instance by name """
for inst in self.insts:
if inst.name == name:
return inst
return None
def add_flat_inst(self, name, mod, offset=[0, 0]):
""" Copies all of the items in instance into this module """
for item in mod.objs:
item.offset += offset
self.objs.append(item)
for item in mod.insts:
item.offset += offset
self.insts.append(item)
debug.check(len(item.mod.pins) == 0, "Cannot add flat instance with subinstances.")
self.connect_inst([])
debug.info(3, "adding flat instance {}".format(name))
return None
def add_rect(self, layer, offset, width=None, height=None):
"""
Adds a rectangle on a given layer,offset with width and height
"""
if not width:
width = drc["minwidth_{}".format(layer)]
if not height:
height = drc["minwidth_{}".format(layer)]
lpp = tech_layer[layer]
self.objs.append(geometry.rectangle(lpp,
offset,
width,
height))
return self.objs[-1]
def add_rect_center(self, layer, offset, width=None, height=None):
"""
Adds a rectangle on a given layer at the center
point with width and height
"""
if not width:
width = drc["minwidth_{}".format(layer)]
if not height:
height = drc["minwidth_{}".format(layer)]
lpp = tech_layer[layer]
corrected_offset = offset - vector(0.5 * width, 0.5 * height)
self.objs.append(geometry.rectangle(lpp,
corrected_offset,
width,
height))
return self.objs[-1]
def add_segment_center(self, layer, start, end, width=None):
"""
Add a min-width rectanglular segment using center
line on the start to end point
"""
if not width:
width = drc["minwidth_{}".format(layer)]
if start.x != end.x and start.y != end.y:
debug.error("Nonrectilinear center rect!", -1)
elif start.x != end.x:
offset = vector(0, 0.5 * width)
return self.add_rect(layer,
start - offset,
end.x - start.x,
width)
else:
offset = vector(0.5 * width, 0)
return self.add_rect(layer,
start - offset,
width,
end.y - start.y)
def get_tx_insts(self, tx_type=None):
"""
Return a list of the instances of given tx type.
"""
tx_list = []
for i in self.insts:
try:
if tx_type and i.mod.tx_type == tx_type:
tx_list.append(i)
elif not tx_type:
if i.mod.tx_type == "nmos" or i.mod.tx_type == "pmos":
tx_list.append(i)
except AttributeError:
pass
return tx_list
def get_pin(self, text):
"""
Return the pin or list of pins
"""
name = self.get_pin_name(text)
try:
if len(self.pin_map[name]) > 1:
debug.error("Should use a pin iterator since more than one pin {}".format(text), -1)
# If we have one pin, return it and not the list.
# Otherwise, should use get_pins()
any_pin = next(iter(self.pin_map[name]))
return any_pin
except Exception:
self.gds_write("missing_pin.gds")
debug.error("No pin found with name {0} on {1}. Saved as missing_pin.gds.".format(name, self.cell_name), -1)
def get_pins(self, text):
"""
Return a pin list (instead of a single pin)
"""
name = self.get_pin_name(text)
if name in self.pin_map.keys():
return self.pin_map[name]
else:
return set()
def add_pin_names(self, pin_dict):
"""
Create a mapping from internal pin names to external pin names.
"""
self.pin_names = pin_dict
self.original_pin_names = {y: x for (x, y) in self.pin_names.items()}
def get_pin_name(self, text):
""" Return the custom cell pin name """
if text in self.pin_names:
return self.pin_names[text]
else:
return text
def get_original_pin_names(self):
""" Return the internal cell pin name """
# This uses the hierarchy_spice pins (in order)
return [self.get_original_pin_name(x) for x in self.pins]
def get_original_pin_name(self, text):
""" Return the internal cell pin names in custom port order """
if text in self.original_pin_names:
return self.original_pin_names[text]
else:
return text
def get_pin_names(self):
"""
Return a pin list of all pins
"""
return list(self.pins)
def copy_layout_pin(self, instance, pin_name, new_name="", relative_offset=vector(0, 0)):
"""
Create a copied version of the layout pin at the current level.
You can optionally rename the pin to a new name.
You can optionally add an offset vector by which to move the pin.
"""
pins = instance.get_pins(pin_name)
if len(pins) == 0:
debug.warning("Could not find pin {0} on {1}".format(pin_name, instance.mod.name))
for pin in pins:
if new_name == "":
new_name = pin_name
self.add_layout_pin(new_name,
pin.layer,
pin.ll() + relative_offset,
pin.width(),
pin.height())
def connect_row_locs(self, from_layer, to_layer, locs, name=None, full=False):
"""
Connects left/right rows that are aligned on the given layer.
"""
bins = {}
for loc in locs:
y = pin.y
try:
bins[y].append(loc)
except KeyError:
bins[y] = [loc]
for y, v in bins.items():
# Not enough to route a pin, so just copy them
if len(v) < 2:
continue
if full:
left_x = 0
right_x = self.width
else:
left_x = min([loc.x for loc in v])
right_x = max([loc.x for loc in v])
left_pos = vector(left_x, y)
right_pos = vector(right_x, y)
# Make sure to add vias to the new route
for loc in v:
self.add_via_stack_center(from_layer=from_layer,
to_layer=to_layer,
offset=loc,
min_area=True)
if name:
self.add_layout_pin_segment_center(text=name,
layer=to_layer,
start=left_pos,
end=right_pos)
else:
self.add_segment_center(layer=to_layer,
start=left_pos,
end=right_pos)
def connect_row_pins(self, layer, pins, name=None, full=False, round=False):
"""
Connects left/right rows that are aligned.
"""
bins = {}
for pin in pins:
y = pin.cy()
if round:
y = round_to_grid(y)
try:
bins[y].append(pin)
except KeyError:
bins[y] = [pin]
for y, v in bins.items():
# Not enough to route a pin, so just copy them
if len(v) < 2:
continue
if full:
left_x = 0
right_x = self.width
else:
left_x = min([pin.lx() for pin in v])
right_x = max([pin.rx() for pin in v])
left_pos = vector(left_x, y)
right_pos = vector(right_x, y)
# Make sure to add vias to the new route
for pin in v:
self.add_via_stack_center(from_layer=pin.layer,
to_layer=layer,
offset=pin.center(),
min_area=True)
if name:
self.add_layout_pin_segment_center(text=name,
layer=layer,
start=left_pos,
end=right_pos)
else:
self.add_segment_center(layer=layer,
start=left_pos,
end=right_pos)
def connect_col_locs(self, from_layer, to_layer, locs, name=None, full=False):
"""
Connects top/bot columns that are aligned.
"""
bins = {}
for loc in locs:
x = loc.x
try:
bins[x].append(loc)
except KeyError:
bins[x] = [loc]
for x, v in bins.items():
# Not enough to route a pin, so just copy them
if len(v) < 2:
continue
if full:
bot_y = 0
top_y = self.height
else:
bot_y = min([loc.y for loc in v])
top_y = max([loc.y for loc in v])
top_pos = vector(x, top_y)
bot_pos = vector(x, bot_y)
# Make sure to add vias to the new route
for loc in v:
self.add_via_stack_center(from_layer=from_layer,
to_layer=to_layer,
offset=loc,
min_area=True)
if name:
self.add_layout_pin_segment_center(text=name,
layer=to_layer,
start=top_pos,
end=bot_pos)
else:
self.add_segment_center(layer=to_layer,
start=top_pos,
end=bot_pos)
def connect_col_pins(self, layer, pins, name=None, full=False, round=False, directions="pref"):
"""
Connects top/bot columns that are aligned.
"""
bins = {}
for pin in pins:
x = pin.cx()
if round:
x = round_to_grid(x)
try:
bins[x].append(pin)
except KeyError:
bins[x] = [pin]
for x, v in bins.items():
# Not enough to route a pin, so just copy them
if len(v) < 2:
continue
if full:
bot_y = 0
top_y = self.height
else:
bot_y = min([pin.by() for pin in v])
top_y = max([pin.uy() for pin in v])
top_pos = vector(x, top_y)
bot_pos = vector(x, bot_y)
# Make sure to add vias to the new route
for pin in v:
self.add_via_stack_center(from_layer=pin.layer,
to_layer=layer,
offset=pin.center(),
min_area=True,
directions=directions)
if name:
self.add_layout_pin_segment_center(text=name,
layer=layer,
start=top_pos,
end=bot_pos)
else:
self.add_segment_center(layer=layer,
start=top_pos,
end=bot_pos)
def get_metal_layers(self, from_layer, to_layer):
from_id = tech_layer_indices[from_layer]
to_id = tech_layer_indices[to_layer]
layer_list = [x for x in tech_layer_indices.keys() if tech_layer_indices[x] > from_id and tech_layer_indices[x] < to_id]
return layer_list
def route_vertical_pins(self, name, insts=None, layer=None, xside="cx", yside="cy", full_width=True):
"""
Route together all of the pins of a given name that vertically align.
Uses local_insts if insts not specified.
Uses center of pin by default, or right or left if specified.
TODO: Add equally spaced option for IR drop min, right now just 2
"""
bins = {}
if not insts:
insts = self.local_insts
for inst in insts:
for pin in inst.get_pins(name):
x = getattr(pin, xside)()
try:
bins[x].append((inst,pin))
except KeyError:
bins[x] = [(inst,pin)]
for x, v in bins.items():
# Not enough to route a pin, so just copy them
if len(v) < 2:
debug.warning("Pins don't align well so copying pins instead of connecting with pin.")
for inst,pin in v:
self.add_layout_pin(pin.name,
pin.layer,
pin.ll(),
pin.width(),
pin.height())
continue
last_via = None
pin_layer = None
for inst,pin in v:
if layer:
pin_layer = layer
else:
pin_layer = self.supply_stack[2]
y = getattr(pin, yside)()
last_via = self.add_via_stack_center(from_layer=pin.layer,
to_layer=pin_layer,
offset=vector(x, y))
if last_via:
via_width=last_via.mod.second_layer_width
via_height=last_via.mod.second_layer_height
else:
via_width=None
via_height=0
bot_y = min([pin.by() for (inst,pin) in v])
top_y = max([pin.uy() for (inst,pin) in v])
if full_width:
bot_y = min(0, bot_y)
top_y = max(self.height, top_y)
top_pos = vector(x, top_y + 0.5 * via_height)
bot_pos = vector(x, bot_y - 0.5 * via_height)
# self.add_layout_pin_rect_ends(name=name,
# layer=pin_layer,
# start=top_pos,
# end=bot_pos,
# width=via_width)
self.add_layout_pin_segment_center(text=name,
layer=pin_layer,
start=top_pos,
end=bot_pos,
width=via_width)
def add_layout_pin_rect_ends(self, name, layer, start, end, width=None):
# This adds pins on the end connected by a segment
top_rect = self.add_layout_pin_rect_center(text=name,
layer=layer,
offset=start)
bot_rect = self.add_layout_pin_rect_center(text=name,
layer=layer,
offset=end)
# This is made to not overlap with the pin above
# so that the power router will only select a small pin.
# Otherwise it adds big blockages over the rails.
if start.y != end.y:
self.add_segment_center(layer=layer,
start=bot_rect.uc(),
end=top_rect.bc())
else:
self.add_segment_center(layer=layer,
start=bot_rect.rc(),
end=top_rect.lc())
return (bot_rect, top_rect)
def route_horizontal_pins(self, name, insts=None, layer=None, xside="cx", yside="cy", full_width=True, new_name=None):
"""
Route together all of the pins of a given name that horizontally align.
Uses local_insts if insts not specified.
Uses center of pin by default, or top or botom if specified.
New top level pin can be renamed with new_name, otherwise the new pin will keep the same name as old pins
TODO: Add equally spaced option for IR drop min, right now just 2
"""
if new_name is not None:
pin_name = new_name
else:
pin_name = name
bins = {}
if not insts:
insts = self.local_insts
for inst in insts:
for pin in inst.get_pins(name):
y = getattr(pin, yside)()
try:
bins[y].append((inst,pin))
except KeyError:
bins[y] = [(inst,pin)]
# Filter the small bins
for y, v in bins.items():
if len(v) < 2:
debug.warning("Pins don't align well so copying pins instead of connecting with pin.")
for inst,pin in v:
self.add_layout_pin(pin.name,
pin.layer,
pin.ll(),
pin.width(),
pin.height())
continue
last_via = None
pin_layer = None
for inst,pin in v:
if layer:
pin_layer = layer
else:
pin_layer = self.supply_stack[0]
x = getattr(pin, xside)()
last_via = self.add_via_stack_center(from_layer=pin.layer,
to_layer=pin_layer,
offset=vector(x, y),
min_area=True)
if last_via: