blob: af4623ef7df6a44ba442acae1983f00a88fb406e [file] [log] [blame]
Tim Edwards22f014d2022-01-26 10:37:03 -05001#!/usr/bin/env python3
2#
3# insert_layer.py: For the given install path, library name, and cellname,
4# find the Magic layout of the cell, and add the specified layer geometry
5# as given by a layer name and a string with four coordinate values.
6# The layer is added to the layout in both the mag/ (full) and maglef/
7# (abstract) directories. Option "-maglef" or "-mag" will restrict the
8# use to only the view indicated by the option.
9#
10# e.g.:
11#
12# insert_layer.py /path/to/sky130A \
13# sky130_fd_io sky130_fd_sc_hd__a21bo_1 pwell "29 17 69 157" -mag
14
15import os
16import re
17import sys
18
19def addlayer(filename, layer, geometry):
20 with open(filename, 'r') as ifile:
21 magtext = ifile.read().splitlines()
22
23 layerrex = re.compile('<< ' + layer + ' >>')
24 sectionrex = re.compile('<< ')
25 labelsrex = re.compile('<< labels >>')
26 endrex = re.compile('<< end >>')
27
28 in_layer = False
29 done = False
30
31 with open(filename, 'w') as ofile:
32 for line in magtext:
33 if not done and not in_layer:
34 # Handle case in which layer did not already exist in file
35 lmatch = labelsrex.match(line)
36 ematch = endrex.match(line)
37 if lmatch or ematch:
38 print('<< ' + layer + '>>', file=ofile)
39 print('rect ' + geometry, file=ofile)
40 done = True
41
42 lmatch = layerrex.match(line)
43 if lmatch:
44 in_layer = True
45 elif in_layer:
46 smatch = sectionrex.match(line)
47 if smatch:
48 print('rect ' + geometry, file=ofile)
49 in_layer = False
50 done = True
51
52 print(line, file=ofile)
53
54def usage():
55 print("insert_layer.py <path_to_pdk> <libname> <cellname> <layer> <geometry> [option]")
56 print(" options:")
57 print(" -mag do only for the view in the mag/ directory")
58 print(" -maglef do only for the view in the maglef/ directory")
59 return 0
60
61if __name__ == '__main__':
62
63 options = []
64 arguments = []
65 for item in sys.argv[1:]:
66 if item.find('-', 0) == 0:
67 options.append(item)
68 else:
69 arguments.append(item)
70
71 if len(arguments) < 5:
72 print("Not enough options given to insert_layer.py.")
73 usage()
74 sys.exit(0)
75
76 source = arguments[0]
77 libname = arguments[1]
78 cellname = arguments[2]
79 layer = arguments[3]
80 geometry = arguments[4]
81
82 # Diagnostic
83 print('insert_layer.py:')
84 print(' source = ' + source)
85 print(' library = ' + libname)
86 print(' cell = ' + cellname)
87 print(' layer = ' + layer)
88 print(' geometry = ' + geometry)
89
90 fail = 0
91
92 efformat = True if '-ef_format' in options else False
93
94 domag = True
95 domaglef = True
96 if '-mag' in options and '-maglef' not in options:
97 domaglef = False
98 if '-maglef' in options and '-mag' not in options:
99 domag = False
100
101 if domag:
102 if efformat:
103 filename = source + '/libs.ref/mag/' + libname + '/' + cellname + '.mag'
104 else:
105 filename = source + '/libs.ref/' + libname + '/mag/' + cellname + '.mag'
106
107 if os.path.isfile(filename):
108 addlayer(filename, layer, geometry)
109 else:
110 fail += 1
111 else:
112 fail += 1
113
114 if domaglef:
115 if efformat:
116 filename = source + '/libs.ref/maglef/' + libname + '/' + cellname + '.mag'
117 else:
118 filename = source + '/libs.ref/' + libname + '/maglef/' + cellname + '.mag'
119
120 if os.path.isfile(filename):
121 addlayer(filename, layer, geometry)
122 else:
123 fail += 1
124 else:
125 fail += 1
126
127 if fail == 2:
128 print('Error: No layout file in either mag/ or maglef/', file=sys.stderr)
129 if efformat:
130 print('(' + source + '/libs.ref/mag[lef]/' + libname +
131 '/' + cellname + '.mag)', file=sys.stderr)
132 else:
133 print('(' + source + '/libs.ref/' + libname + '/mag[lef]/'
134 + cellname + '.mag)', file=sys.stderr)
135