-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrect
More file actions
executable file
·328 lines (298 loc) · 10.4 KB
/
rect
File metadata and controls
executable file
·328 lines (298 loc) · 10.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
#!/usr/bin/python3
import os
import sys
hasGDSTK = True
try:
import gdstk
except ImportError:
hasGDSTK = False
def stripComments(line):
escape = False
inString = False
for i, c in enumerate(line):
if not escape and (c == "\"" or c == "\'"):
inString = not inString
elif inString and c == "\\":
escape = not escape
elif not inString and c == "#":
return line[0:i]
else:
escape = False
return line
def loadActConf(path):
result = dict()
stack = [result]
with open(path, "r") as fptr:
for number, line in enumerate(fptr):
if "#" in line:
line = line[0:line.index("#")]
args = [arg.strip() for arg in line.strip().split(" ")]
if len(args) > 0:
if args[0] == "include":
result = result | loadActConf(args[1][1:-1])
elif args[0] == "begin":
stack[-1][args[1]] = dict()
stack.append(stack[-1][args[1]])
elif args[0] == "end":
stack.pop()
elif args[0] == "string":
stack[-1][args[1]] = args[2][1:-1]
elif args[0] == "int":
stack[-1][args[1]] = int(args[2])
elif args[0] == "real":
stack[-1][args[1]] = float(args[2])
elif args[0] == "int_table":
stack[-1][args[1]] = [int(arg) for arg in args[2:]]
elif args[0] == "string_table":
stack[-1][args[1]] = [arg[1:-1] for arg in args[2:]]
return result
def queryGDS(conf, rectLayer):
gds = []
gds_bloat = []
if rectLayer in conf["materials"]:
if "gds" in conf["materials"][rectLayer]:
gds = conf["materials"][rectLayer]["gds"]
if "gds_bloat" in conf["materials"][rectLayer]:
gds_bloat = conf["materials"][rectLayer]["gds_bloat"]
elif rectLayer in conf["materials"]["metal"]:
if rectLayer+"_gds" in conf["materials"]["metal"]:
gds = conf["materials"]["metal"][rectLayer+"_gds"]
if rectLayer+"_gds_bloat" in conf["materials"]["metal"]:
gds_bloat = conf["materials"]["metal"][rectLayer+"_gds_bloat"]
else:
subkey = ""
for key, value in conf["vias"].items():
if key.endswith("_name"):
if value == rectLayer:
subkey = key[0:-5]
break
if subkey != "":
if subkey+"_gds" in conf["vias"]:
gds = conf["vias"][subkey+"_gds"]
if subkey+"_gds_bloat" in conf["vias"]:
gds_bloat = conf["vias"][subkey+"_gds_bloat"]
else:
print("unrecognized layer {rectLayer}")
return zip(gds, gds_bloat)
TAP, FILL, CELL, BLOCK = range(4)
class Rect:
def __init__(self, label, layer, bounds, hint="", isInput=False, isOutput=False):
self.label = label
self.layer = layer
self.bounds = [int(bound) for bound in bounds]
self.hint = hint
self.isInput = isInput
self.isOutput = isOutput
def isIn(searches, string):
for search in searches:
if search in string:
return True
return False
class Cell:
def __init__(self, name, kind, bbox, rects):
self.name = name
self.kind = kind
self.bbox = bbox
self.rects = rects
def readCell(path):
name = os.path.splitext(os.path.basename(path))[0]
kind = BLOCK
if "welltap" in name.lower():
kind = TAP
elif "fill" in name.lower():
kind = FILL
elif "cell" in name.lower():
kind = CELL
# left, bottom, right, top
bbox = [0, 0, 1, 1]
rects = []
with open(path, "r") as rf:
for number, line in enumerate(rf):
args = [arg.strip() for arg in line.split(" ")]
if args[0] == "bbox":
bbox = [int(arg) for arg in args[1:]]
else:
isInput = (args[0] == "inrect")
isOutput = (args[0] == "outrect")
hint = ""
if len(args) >= 8:
hint = args[7]
rects.append(Rect(args[1], args[2], [int(arg) for arg in args[3:7]], hint, isInput, isOutput))
return Cell(name, kind, bbox, rects)
def writeLayerMap(path, conf):
layers = zip(conf["gds"]["layers"], conf["gds"]["major"], conf["gds"]["minor"])
with open(path, "w") as fptr:
for layer in layers:
name = layer[0]
purpose = ""
if "." in name:
name, purpose = layer[0].rsplit(".", 1)
if "via" in name and purpose in ["drawing", "dg", "drw"]:
print(f"{name} VIA {layer[1]} {layer[2]}", file=fptr)
elif purpose in ["drawing", "dg", "drw"]:
print(f"{name} LEFOBS {layer[1]} {layer[2]}", file=fptr)
elif purpose in ["label", "ll", "lbl"]:
print(f"NAME {name}/PINNAME {layer[1]} {layer[2]}", file=fptr)
print(f"NAME {name}/PIN {layer[1]} {layer[2]}", file=fptr)
print(f"NAME {name}/LEFPINNAME {layer[1]} {layer[2]}", file=fptr)
print(f"NAME {name}/LEFPIN {layer[1]} {layer[2]}", file=fptr)
elif purpose in ["net", "nt"]:
print(f"{name} NET {layer[1]} {layer[2]}", file=fptr)
elif purpose in ["pin", "pin1", "pn"]:
print(f"{name} PIN,LEFPIN {layer[1]} {layer[2]}", file=fptr)
elif purpose in ["blockage", "be", "blo"]:
print(f"{name} BLOCKAGE {layer[1]} {layer[2]}", file=fptr)
if "prb" in name.lower():
print(f"DIEAREA ALL {layer[1]} {layer[2]}", file=fptr)
def writeGDS(path, conf, cell):
if not hasGDSTK:
print("skipping gds, enable gds support by installing gdstk:")
print("pip install gdstk")
return
scale = conf["general"]["scale"]
numMetals = conf["general"]["metals"]
layers = {layer: (major, minor) for layer, major, minor in zip(conf["gds"]["layers"], conf["gds"]["major"], conf["gds"]["minor"])}
lib = gdstk.Library(unit=scale*1e-9, precision=scale*1e-9)
gdsCell = lib.new_cell(cell.name)
bndry = None
for layer, idx in layers.items():
name = layer
purpose = ""
if "." in name:
name, purpose = layer.rsplit(".", 1)
if (name == "text" and purpose in ["drawing", "dg", "drw"]) or name == "outline" or name == "areaid_sc":
gdsCell.add(gdstk.rectangle((cell.bbox[0], cell.bbox[1]), (cell.bbox[2], cell.bbox[3]), layer=idx[0], datatype=idx[1]))
for rect in cell.rects:
gds = queryGDS(conf, rect.layer)
labelWritten = False
for layerName, bloat in gds:
name = layerName
purpose = ""
if "." in name:
name, purpose = layerName.rsplit(".", 1)
if layerName in layers:
idx = layers[layerName]
gdsCell.add(gdstk.rectangle(((rect.bounds[0]-bloat), (rect.bounds[1]-bloat)), ((rect.bounds[2]+bloat), (rect.bounds[3]+bloat)), layer=idx[0], datatype=idx[1]))
if rect.label and rect.label != "#" and not labelWritten:
gdsCell.add(gdstk.Label(rect.label, ((rect.bounds[0] + rect.bounds[2])/2, (rect.bounds[1] + rect.bounds[3])/2), layer=idx[0], texttype=idx[1]))
labelWritten = True
lib.write_gds(path)
def writeLEF(path, conf, cell):
# See https://github.com/KLayout/klayout/blob/766dd675c11d98b2461c448035197f6e934cb497/src/plugins/streamers/lefdef/db_plugin/dbLEFDEFImporter.cc#L1085
# Purpose Name = Placement Code
# LEFPIN = LEFPIN
# PIN = PIN
# LEFPINNAME = LEFLABEL
# PINNAME = LABEL
# FILL = FILL
# FILLOPC = FILLOPC
# LEFOBS = OBS
# SPNET = SPNET
# NET = NET
# VIA = VIA
# BLOCKAGE = BLK
# ALL = [LEFPIN, PIN, FILL, FILLOPC, OBS, SPNET, NET, VIA]
scale = conf["general"]["scale"]
numMetals = conf["general"]["metals"]
with open(path, "w") as fptr:
print(f"MACRO {cell.name}", file=fptr)
if cell.kind == TAP:
print("CLASS CORE WELLTAP ;", file=fptr)
elif cell.kind == FILL:
print("CLASS CORE SPACER ;", file=fptr)
elif cell.kind == CELL:
print("CLASS CORE ;", file=fptr)
else:
print("CLASS BLOCK ;", file=fptr)
print(f"\tORIGIN {-cell.bbox[0]*scale} {-cell.bbox[1]*scale} ;", file=fptr)
print(f"\tFOREIGN {cell.name} {cell.bbox[0]*scale} {cell.bbox[1]*scale} ;", file=fptr)
print(f"\tSIZE {(cell.bbox[2]-cell.bbox[0])*scale} BY {(cell.bbox[3]-cell.bbox[1])*scale} ;", file=fptr)
print(f"\tSYMMETRY X Y ;", file=fptr)
if cell.kind in [FILL, TAP, CELL]:
print("\tSITE CoreSite ;", file=fptr)
for rect in cell.rects:
if rect.isInput or rect.isOutput:
direction = "INOUT"
if not rect.isInput:
direction = "OUTPUT"
elif not rect.isOutput:
direction = "INPUT"
print(f"\tPIN {rect.label}", file=fptr)
if isIn(["vnsub", "vpsub", "vddsub", "vsssub", "vddb", "vssb"], rect.label.lower()):
print("\t\tDIRECTION INOUT ;", file=fptr)
print("\t\tUSE POWER ;", file=fptr)
elif isIn(["vdd", "pwr"], rect.label.lower()):
print("\t\tDIRECTION INOUT ;", file=fptr)
print("\t\tUSE POWER ;", file=fptr)
if cell.kind in [FILL, TAP, CELL]:
print("\t\tSHAPE ABUTMENT ;", file=fptr)
elif isIn(["gnd", "vss"], rect.label.lower()):
print("\t\tDIRECTION INOUT ;", file=fptr)
print("\t\tUSE GROUND ;", file=fptr)
if cell.kind in [FILL, TAP, CELL]:
print("\t\tSHAPE ABUTMENT ;", file=fptr)
else:
print(f"\t\tDIRECTION {direction} ;", file=fptr)
print(f"\t\tUSE SIGNAL ;", file=fptr)
print("\t\tPORT", file=fptr)
gds = queryGDS(conf, rect.layer)
for layer, bloat in gds:
name = layer
purpose = ""
if "." in name:
name, purpose = layer.rsplit(".", 1)
print(f"\t\t\tLAYER {name} ;", file=fptr)
print(f"\t\t\t\tRECT {(rect.bounds[0]-bloat)*scale} {(rect.bounds[1]-bloat)*scale} {(rect.bounds[2]+bloat)*scale} {(rect.bounds[3]+bloat)*scale} ;", file=fptr)
print("\t\tEND", file=fptr)
print(f"\tEND {rect.label}", file=fptr)
print("\tOBS", file=fptr)
for rect in cell.rects:
#if not rect.isInput and not rect.isOutput:
gds = queryGDS(conf, rect.layer)
for layer, bloat in gds:
name = layer
purpose = ""
if "." in name:
name, purpose = layer.rsplit(".", 1)
print(f"\t\tLAYER {name} ;", file=fptr)
print(f"\t\t\tRECT {(rect.bounds[0]-bloat)*scale} {(rect.bounds[1]-bloat)*scale} {(rect.bounds[2]+bloat)*scale} {(rect.bounds[3]+bloat)*scale} ;", file=fptr)
print("\tEND", file=fptr)
print(f"END {cell.name}", file=fptr)
print("", file=fptr)
def print_help():
print("Usage: rect2lef.py [options] <input.rect>")
print("\t-T<tech>\tidentify the technology used for this translation.")
print("\t-lm\temit the layermap")
print("\t-gds\twrite the gds.")
if __name__ == "__main__":
if len(sys.argv) <= 2 or sys.argv[1] == '--help' or sys.argv[1] == '-h':
print_help()
else:
rectPath = None
doLM = False
doGDS = False
techName = "sky130"
actHome = os.environ.get('ACT_HOME', "/opt/cad")
for arg in sys.argv[1:]:
if arg[0] == '-':
if arg[1] == 'T':
techName = arg[2:]
elif arg == "-lm":
doLM = True
elif arg == "-gds":
doGDS = True
else:
print(f"error: unrecognized option '{arg}'")
print("")
print_help()
sys.exit()
elif not rectPath:
rectPath = arg
conf = loadActConf(actHome + "/conf/" + techName + "/layout.conf")
if rectPath:
cell = readCell(rectPath)
writeLEF(cell.name + ".lef", conf, cell)
if doLM:
writeLayerMap("layermap.txt", conf)
if doGDS:
writeGDS(cell.name + ".gds", conf, cell)