Repository navigation
Expand file tree
/
Copy pathmag2rect.py
More file actions
executable file
·236 lines (199 loc) · 6.88 KB
/
Copy pathmag2rect.py
File metadata and controls
executable file
·236 lines (199 loc) · 6.88 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
#!/usr/bin/env python3
"""python python mag2rect <mag_file> <mag layers/rect layers>
Written by: Ruslan Dashkin
Modified by: Rajit Manohar
the script takes a magic file and translates it into a rect file with
the same filename in the same location.
:param <mag_file>: the path of the magic file to be converted
"""
import sys
import re
from collections import defaultdict
if (len(sys.argv) != 2):
print ("Usage python mag2rect <mag_file>")
sys.exit (1)
if (sys.argv[1][-4:] != ".mag"):
print ("Expecting .mag extension for filename (replaced by .rect for output)")
sys.exit (1)
# both files have the same name
mag_file = open(sys.argv[1], "r")
rect_file = open(sys.argv[1][:-3] + "rect", "w")
#
# map from mag -> rect format
#
# - metal names are m#, so shorten any long names
# - contacts also use short names
# - contacts also have vias
# - substrate diff is abbreviated as well
# - these are conventions from layout.conf
#
# unspecified layers : default is the identity map
#
rect_map = {
'metal1' : ['m1'],
'metal2' : ['m2'],
'metal3' : ['m3'],
'ndcontact' : ['ndc','ndiffusion', 'm1'],
'ndc' : ['ndc','ndiffusion', 'm1'],
'pdcontact' : ['pdc','pdiffusion', 'm1' ],
'pdc' : ['pdc','pdiffusion', 'm1'],
'polycontact' : ['pc','polysilicon', 'm1'],
'pc' : ['pc','polysilicon', 'm1' ],
'm2contact' : ['m2c','m1','m2', 'v1'],
'm2c' : ['m2c','m1','m2', 'v1'],
'psubstratepdiff':['ppdiff'],
'psc' : ['psc', 'ppdiff', 'm1' ],
'nsubstratendiff':['nndiff'],
'nsc' : ['nsc', 'nndiff', 'm1' ],
}
#
# These are for parsing a mag file
#
# layer starts with << layername >>, extract into the "layer" named group.
parse_layer = r'<<\s+(?P<layer>\w+)\s+>>'
# rectangles (xl,yl,xh,yh)
parse_coord = r'rect\s+(?P<xl>[0-9-]+)\s+(?P<yl>[0-9-]+)\s+(?P<xh>[0-9-]+)\s+(?P<yh>[0-9-]+)'
# label is rlabel followed by the layer followed by coordinates
# followed by the name of the label
parse_label = r'rlabel\s*(?P<layer>\w+)\s*(?P<xl>[0-9-]+)\s+(?P<yl>[0-9-]+)\s+(?P<xh>[0-9-]+)\s+(?P<yh>[0-9-]+)\s+\d+\s+(?P<label>[a-zA-Z0-9_()#]+)'
#label_map = defaultdict(list)
label_map = {}
label_layer = {}
labels_flag = 0
hash_need = 0
cur_layer_mag = ''
cur_layer = []
cur_bbox = ()
cur_label = ''
#
# Step 1: collect all the labels and their locations
#
# process labels
for line in mag_file:
# skip line if not layer with label
chk_line = re.search(parse_layer, line)
if (chk_line):
if (chk_line.group('layer').find('label') != -1):
labels_flag = 1
else:
labels_flag = 0
if (labels_flag == 0):
continue
#skip if no label parsed
res = re.search(parse_label, line)
if (res):
# parse and save to local vars
# extract lower left coordinate, label name, and layer name
cur_bbox = (int(res.group('xl')),int(res.group('yl')))
cur_label = res.group('label')
cur_layer = res.group('layer')
# map layer to list of layers
if cur_layer in rect_map:
cur_layer = rect_map[cur_layer][0]
# check for align_ labels; these are alignment markers
box_str = str(cur_bbox[0]) + " " + str(cur_bbox[1]) + " "
box_str = box_str + str(int(res.group('xh'))) + " " + str(int(res.group('yh')))
if cur_label == "align_BBOX":
rect_file.write("rect # $align " + box_str + "\n")
continue
elif cur_label.startswith("alignl_"):
rect_file.write("rect $l:" + cur_label[7:] + " $align " + box_str + "\n")
continue
elif cur_label.startswith("alignr_"):
rect_file.write("rect $r:" + cur_label[7:] + " $align " + box_str + "\n")
continue
elif cur_label.startswith("alignt_"):
rect_file.write("rect $t:" + cur_label[7:] + " $align " + box_str + "\n")
continue
elif cur_label.startswith("alignb_"):
rect_file.write("rect $b:" + cur_label[7:] + " $align " + box_str + "\n")
continue
#
# Special label name processing:
# these used to be needed; not any more
#
# ?? invalidate label for resricted addition?
#if (cur_label.find('restr_') != -1):
# cur_label = '#'
# Remove coordinate from label; used to filter internal nets
if (cur_label.find(res.group('xl')) != -1):
cur_label = cur_label.replace('_' + res.group('xl'), '')
if (cur_label.find(str(int(res.group('xl'))-1)) != -1):
cur_label = cur_label.replace('_' + str(int(res.group('xl'))-1), '')
# arrays are in square brackets; reformat labels
cur_label = cur_label.replace('(','[')
cur_label = cur_label.replace(')',']')
# add # to internal nets
if (cur_label[0].isdigit() == True):
hash_need = 1
for char in cur_label:
if (char.isdigit() != True):
hash_need = 0
break
if (hash_need == 1):
cur_label = '#' + cur_label
hash_need = 0
# put label in list
label_layer[cur_bbox] = cur_layer
label_map[cur_bbox] = cur_label
#
# Second pass: emit the .rect file, now that we have collected all the labels
#
# start from the beginning again
mag_file.seek(0)
cur_layer = []
cur_label = ''
cur_bbox = ()
skip_layer = 0
# process all rectangles/layer content
for line in mag_file:
# read layer and map, stop processing if arriving at
# labels/end, skip on checkpaint
if (re.search(parse_layer, line)):
res = re.search(parse_layer, line)
cur_layer_mag = res.group('layer')
skip_layer = 0
if (cur_layer_mag.find('labels') != -1):
skip_layer = 1
continue
elif (cur_layer_mag.find('error') != -1):
skip_layer = 1
continue
elif (cur_layer_mag.find('checkpaint') != -1):
skip_layer = 1
continue
elif (cur_layer_mag.find('end') != -1):
break
if cur_layer_mag in rect_map:
cur_layer = rect_map[cur_layer_mag]
else:
cur_layer = [ cur_layer_mag ]
# we haven't arrived at a geometry layer, so skip.
if skip_layer == 1:
continue
# get label for coordinates
res = re.search(parse_coord, line)
if res:
# this is a rectangle
cur_bbox = (int(res.group('xl')),int(res.group('yl')),int(res.group('xh')),int(res.group('yh')))
# search for label inside the rectangle and on the same layer
for key in label_map:
if (key[0] >= cur_bbox[0] and key[0] <= cur_bbox[2]):
if (key[1] >= cur_bbox[1] and key[1] <= cur_bbox[3]):
if (label_layer[key] == cur_layer[0]):
cur_label = label_map[key]
break
else:
cur_label = '#'
else:
cur_label = '#'
#skip line if cororinates were not parsed
if (len(cur_bbox) == 0):
continue
if (len(cur_label) == 0):
continue
# for each layer associated with the rectangle write line to rect file
for layer in cur_layer:
rect_file.write("rect " + cur_label + " " + layer + " " + str(cur_bbox[0]) + " " + str(cur_bbox[1]) + " " + str(cur_bbox[2]) + " " + str(cur_bbox[3]) + " " + "\n")
cur_bbox = ()
cur_label = ''