blob: 6596bd050bb130760cfe982c9fc6318db4465b30 [file] [log] [blame]
Jeff DiCorpod8253762021-01-24 23:46:40 -08001#!/usr/bin/env python3
Steve Kelly3ab5f032020-12-18 18:35:23 -05002# SPDX-FileCopyrightText: 2020 Efabless Corporation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15# SPDX-License-Identifier: Apache-2.0
16
Jeff DiCorpod8253762021-01-24 23:46:40 -080017#----------------------------------------------------------------------
Tim Edwardsc5522922020-11-30 16:56:58 -050018#
19# set_user_id.py ---
20#
21# Manipulate the magic database, GDS, and verilog source files for the
22# user_id_programming block to set the user ID number.
23#
24# The user ID number is a 32-bit value that is passed to this routine
Jeff DiCorpod8253762021-01-24 23:46:40 -080025# as an 8-digit hex number. If not given as an option, then the script
26# will look for the value of the key "project_id" in the info.yaml file
27# in the project top level directory
Tim Edwardsc5522922020-11-30 16:56:58 -050028#
29# user_id_programming layout map:
30# Positions marked (in microns) for value = 0. For value = 1, move
31# the via 0.92um to the left.
32#
33# Layout grid is 0.46um x 0.34um with half-pitch offset (0.23um, 0.17um)
34#
35# Signal Via position (um)
36# name X Y
37#--------------------------------
38# mask_rev[0] 14.49 9.35
39# mask_rev[1] 16.33 9.35
40# mask_rev[2] 10.35 20.23
41# mask_rev[3] 8.05 9.35
42# mask_rev[4] 28.29 9.35
43# mask_rev[5] 21.85 25.67
44# mask_rev[6] 8.05 20.23
45# mask_rev[7] 20.47 9.35
46# mask_rev[8] 17.25 17.85
47# mask_rev[9] 25.53 12.07
48# mask_rev[10] 22.31 20.23
49# mask_rev[11] 13.11 9.35
50# mask_rev[12] 23.69 23.29
51# mask_rev[13] 24.15 12.07
52# mask_rev[14] 13.57 17.85
53# mask_rev[15] 23.23 6.97
54# mask_rev[16] 24.15 17.85
55# mask_rev[17] 8.51 17.85
56# mask_rev[18] 23.69 20.23
57# mask_rev[19] 10.81 23.29
58# mask_rev[20] 14.95 6.97
59# mask_rev[21] 18.17 23.29
60# mask_rev[22] 21.39 17.85
61# mask_rev[23] 26.45 25.67
62# mask_rev[24] 9.89 17.85
63# mask_rev[25] 15.87 17.85
64# mask_rev[26] 26.45 17.85
65# mask_rev[27] 8.51 6.97
66# mask_rev[28] 10.81 9.35
67# mask_rev[29] 27.83 20.23
68# mask_rev[30] 16.33 23.29
69# mask_rev[31] 8.05 14.79
Jeff DiCorpod8253762021-01-24 23:46:40 -080070#----------------------------------------------------------------------
Tim Edwardsc5522922020-11-30 16:56:58 -050071
72import os
73import sys
74import re
75
76def usage():
Jeff DiCorpod8253762021-01-24 23:46:40 -080077 print("Usage:")
78 print("set_user_id.py [<user_id_value>] [<path_to_project>]")
79 print("")
80 print("where:")
81 print(" <user_id_value> is a character string of eight hex digits, and")
82 print(" <path_to_project> is the path to the project top level directory.")
83 print("")
84 print(" If <user_id_value> is not given, then it must exist in the info.yaml file.")
85 print(" If <path_to_project> is not given, then it is assumed to be the cwd.")
Tim Edwardsc5522922020-11-30 16:56:58 -050086 return 0
87
88if __name__ == '__main__':
89
90 # Coordinate pairs in microns for the zero position on each bit
91 mask_rev = (
92 (14.49, 9.35), (16.33, 9.35), (10.35, 20.23), ( 8.05, 9.35),
93 (28.29, 9.35), (21.85, 25.67), ( 8.05, 20.23), (20.47, 9.35),
94 (17.25, 17.85), (25.53, 12.07), (22.31, 20.23), (13.11, 9.35),
95 (23.69, 23.29), (24.15, 12.07), (13.57, 17.85), (23.23, 6.97),
96 (24.15, 17.85), ( 8.51, 17.85), (23.69, 20.23), (10.81, 23.29),
97 (14.95, 6.97), (18.17, 23.29), (21.39, 17.85), (26.45, 25.67),
98 ( 9.89, 17.85), (15.87, 17.85), (26.45, 17.85), ( 8.51, 6.97),
99 (10.81, 9.35), (27.83, 20.23), (16.33, 23.29), ( 8.05, 14.79));
100
101 optionlist = []
102 arguments = []
103
104 debugmode = False
Jeff DiCorpo3417b4d2021-02-19 01:17:22 -0800105 reportmode = False
Tim Edwardsc5522922020-11-30 16:56:58 -0500106
107 for option in sys.argv[1:]:
108 if option.find('-', 0) == 0:
109 optionlist.append(option)
110 else:
111 arguments.append(option)
112
Jeff DiCorpod8253762021-01-24 23:46:40 -0800113 if len(arguments) > 2:
114 print("Wrong number of arguments given to set_user_id.py.")
Tim Edwardsc5522922020-11-30 16:56:58 -0500115 usage()
116 sys.exit(0)
117
118 if '-debug' in optionlist:
119 debugmode = True
Jeff DiCorpo3417b4d2021-02-19 01:17:22 -0800120 if '-report' in optionlist:
121 reportmode = True
Tim Edwardsc5522922020-11-30 16:56:58 -0500122
Jeff DiCorpod8253762021-01-24 23:46:40 -0800123 user_id_value = None
124 user_project_path = None
Tim Edwardsc5522922020-11-30 16:56:58 -0500125
Jeff DiCorpod8253762021-01-24 23:46:40 -0800126 if len(arguments) > 0:
127 user_id_value = arguments[0]
Tim Edwardsc5522922020-11-30 16:56:58 -0500128
Jeff DiCorpod8253762021-01-24 23:46:40 -0800129 # Convert to binary
130 try:
131 user_id_int = int('0x' + user_id_value, 0)
132 user_id_bits = '{0:032b}'.format(user_id_int)
133 except:
134 user_project_path = arguments[0]
135
136 if len(arguments) == 0:
137 user_project_path = os.getcwd()
138 elif len(arguments) == 2:
Tim Edwardsc5522922020-11-30 16:56:58 -0500139 user_project_path = arguments[1]
Jeff DiCorpod8253762021-01-24 23:46:40 -0800140 elif user_project_path == None:
141 user_project_path = arguments[0]
Tim Edwardsc5522922020-11-30 16:56:58 -0500142 else:
143 user_project_path = os.getcwd()
144
Jeff DiCorpod8253762021-01-24 23:46:40 -0800145 if not os.path.isdir(user_project_path):
146 print('Error: Project path "' + user_project_path + '" does not exist or is not readable.')
147 sys.exit(1)
148
149 # Check for valid directories
150
151 if not user_id_value:
152 if os.path.isfile(user_project_path + '/info.yaml'):
153 with open(user_project_path + '/info.yaml', 'r') as ifile:
154 infolines = ifile.read().splitlines()
155 for line in infolines:
156 kvpair = line.split(':')
157 if len(kvpair) == 2:
158 key = kvpair[0].strip()
159 value = kvpair[1].strip()
160 if key == 'project_id':
161 user_id_value = value.strip('"\'')
162 break
163
164 if not user_id_value:
165 print('Error: No project_id key:value pair found in project info.yaml.')
166 sys.exit(1)
167
168 try:
169 user_id_int = int('0x' + user_id_value, 0)
170 user_id_bits = '{0:032b}'.format(user_id_int)
171 except:
172 print('Error: Cannot parse user ID "' + user_id_value + '" as an 8-digit hex number.')
173 sys.exit(1)
174
175 else:
176 print('Error: No info.yaml file and no user ID argument given.')
177 sys.exit(1)
178
Jeff DiCorpo3417b4d2021-02-19 01:17:22 -0800179 if reportmode:
180 print(str(user_id_int))
181 sys.exit(0)
182
Jeff DiCorpod8253762021-01-24 23:46:40 -0800183 print('Setting project user ID to: ' + user_id_value)
184
Tim Edwardsc5522922020-11-30 16:56:58 -0500185 magpath = user_project_path + '/mag'
186 gdspath = user_project_path + '/gds'
187 vpath = user_project_path + '/verilog'
188 errors = 0
189
Jeff DiCorpod8253762021-01-24 23:46:40 -0800190 if not os.path.isdir(gdspath):
191 print('No directory ' + gdspath + ' found (path to GDS).')
Tim Edwardsc5522922020-11-30 16:56:58 -0500192 sys.exit(1)
Jeff DiCorpod8253762021-01-24 23:46:40 -0800193
194 if not os.path.isdir(vpath):
195 print('No directory ' + vpath + ' found (path to verilog).')
196 sys.exit(1)
197
198 if not os.path.isdir(magpath):
199 print('No directory ' + magpath + ' found (path to magic databases).')
200 sys.exit(1)
201
202 print('Step 1: Modify GDS of the user_id_programming subcell')
203
204 # Bytes leading up to via position are:
205 viarec = "00 06 0d 02 00 43 00 06 0e 02 00 2c 00 2c 10 03 "
206 viabytes = bytes.fromhex(viarec)
207
208 # Read the GDS file. If a backup was made of the zero-value
209 # program, then use it.
210
211 gdsbak = gdspath + '/user_id_prog_zero.gds'
212 gdsfile = gdspath + '/user_id_programming.gds'
213
214 if os.path.isfile(gdsbak):
215 with open(gdsbak, 'rb') as ifile:
216 gdsdata = ifile.read()
217 else:
218 with open(gdsfile, 'rb') as ifile:
219 gdsdata = ifile.read()
220
221 for i in range(0,32):
222 # Ignore any zero bits.
223 if user_id_bits[i] == '0':
224 continue
225
226 coords = mask_rev[i]
227 xum = coords[0]
228 yum = coords[1]
229
230 # Contact is 0.17 x 0.17, so add and subtract 0.085 to get
231 # the corner positions.
232
233 xllum = xum - 0.085
234 yllum = yum - 0.085
235 xurum = xum + 0.085
236 yurum = yum + 0.085
237
238 # Get the 4-byte hex values for the corner coordinates
239 xllnm = round(xllum * 1000)
240 yllnm = round(yllum * 1000)
241 xllhex = '{0:08x}'.format(xllnm)
242 yllhex = '{0:08x}'.format(yllnm)
243 xurnm = round(xurum * 1000)
244 yurnm = round(yurum * 1000)
245 xurhex = '{0:08x}'.format(xurnm)
246 yurhex = '{0:08x}'.format(yurnm)
247
248 # Magic's GDS output for vias always starts at the lower left
249 # corner and goes counterclockwise, repeating the first point.
250 viaoldposdata = viarec + xllhex + yllhex + xurhex + yllhex
251 viaoldposdata += xurhex + yurhex + xllhex + yurhex + xllhex + yllhex
252
253 # For "one" bits, the X position is moved 0.92 microns to the left
254 newxllum = xllum - 0.92
255 newxurum = xurum - 0.92
256
257 # Get the 4-byte hex values for the new corner coordinates
258 newxllnm = round(newxllum * 1000)
259 newxllhex = '{0:08x}'.format(newxllnm)
260 newxurnm = round(newxurum * 1000)
261 newxurhex = '{0:08x}'.format(newxurnm)
262
263 vianewposdata = viarec + newxllhex + yllhex + newxurhex + yllhex
264 vianewposdata += newxurhex + yurhex + newxllhex + yurhex + newxllhex + yllhex
265
266 # Diagnostic
267 if debugmode:
268 print('Bit ' + str(i) + ':')
269 print('Via position ({0:3.2f}, {1:3.2f}) to ({2:3.2f}, {3:3.2f})'.format(xllum, yllum, xurum, yurum))
270 print('Old hex string = ' + viaoldposdata)
271 print('New hex string = ' + vianewposdata)
272
273 # Convert hex strings to byte arrays
274 viaoldbytedata = bytearray.fromhex(viaoldposdata)
275 vianewbytedata = bytearray.fromhex(vianewposdata)
276
277 # Replace the old data with the new
278 if viaoldbytedata not in gdsdata:
279 print('Error: via not found for bit position ' + str(i))
280 errors += 1
281 else:
282 gdsdata = gdsdata.replace(viaoldbytedata, vianewbytedata)
283
284 if errors == 0:
285 # Keep a copy of the original
286 if not os.path.isfile(gdsbak):
287 os.rename(gdsfile, gdsbak)
288
289 with open(gdsfile, 'wb') as ofile:
290 ofile.write(gdsdata)
291
292 print('Done!')
293
294 else:
295 print('There were errors in processing. No file written.')
296 print('Ending process.')
297 sys.exit(1)
298
299 print('Step 2: Add user project ID parameter to verilog.')
300
301 changed = False
302 with open(vpath + '/rtl/caravel.v', 'r') as ifile:
303 vlines = ifile.read().splitlines()
304 outlines = []
305 for line in vlines:
306 oline = re.sub("parameter USER_PROJECT_ID = 32'h[0-9A-F]+;",
307 "parameter USER_PROJECT_ID = 32'h" + user_id_value + ";",
308 line)
309 if oline != line:
310 changed = True
311 outlines.append(oline)
312
313 if changed:
314 with open(vpath + '/rtl/caravel.v', 'w') as ofile:
315 for line in outlines:
316 print(line, file=ofile)
317 print('Done!')
318 else:
319 print('Error: No substitutions done on verilog/rtl/caravel.v.')
320 print('Ending process.')
321 sys.exit(1)
322
323 print('Step 3: Add user project ID text to top level layout.')
324
325 with open(magpath + '/user_id_textblock.mag', 'r') as ifile:
326 maglines = ifile.read().splitlines()
327 outlines = []
328 digit = 0
329 for line in maglines:
330 if 'alphaX_' in line:
331 dchar = user_id_value[digit].upper()
332 oline = re.sub('alpha_[0-9A-F]', 'alpha_' + dchar, line)
333 outlines.append(oline)
334 digit += 1
335 else:
336 outlines.append(line)
337
338 if digit == 8:
339 with open(magpath + '/user_id_textblock.mag', 'w') as ofile:
340 for line in outlines:
341 print(line, file=ofile)
342 print('Done!')
343 elif digit == 0:
344 print('Error: No digits were replaced in the layout.')
345 else:
346 print('Error: Only ' + str(digit) + ' digits were replaced in the layout.')
347
Tim Edwardsc5522922020-11-30 16:56:58 -0500348 sys.exit(0)