blob: 87fdd9e39fe9c0943ccec7d3380b2d86d61ad4f4 [file] [log] [blame]
Tim Edwardsc5522922020-11-30 16:56:58 -05001#!/bin/env python3
2#
3# set_user_id.py ---
4#
5# Manipulate the magic database, GDS, and verilog source files for the
6# user_id_programming block to set the user ID number.
7#
8# The user ID number is a 32-bit value that is passed to this routine
9# as an integer.
10#
11# user_id_programming layout map:
12# Positions marked (in microns) for value = 0. For value = 1, move
13# the via 0.92um to the left.
14#
15# Layout grid is 0.46um x 0.34um with half-pitch offset (0.23um, 0.17um)
16#
17# Signal Via position (um)
18# name X Y
19#--------------------------------
20# mask_rev[0] 14.49 9.35
21# mask_rev[1] 16.33 9.35
22# mask_rev[2] 10.35 20.23
23# mask_rev[3] 8.05 9.35
24# mask_rev[4] 28.29 9.35
25# mask_rev[5] 21.85 25.67
26# mask_rev[6] 8.05 20.23
27# mask_rev[7] 20.47 9.35
28# mask_rev[8] 17.25 17.85
29# mask_rev[9] 25.53 12.07
30# mask_rev[10] 22.31 20.23
31# mask_rev[11] 13.11 9.35
32# mask_rev[12] 23.69 23.29
33# mask_rev[13] 24.15 12.07
34# mask_rev[14] 13.57 17.85
35# mask_rev[15] 23.23 6.97
36# mask_rev[16] 24.15 17.85
37# mask_rev[17] 8.51 17.85
38# mask_rev[18] 23.69 20.23
39# mask_rev[19] 10.81 23.29
40# mask_rev[20] 14.95 6.97
41# mask_rev[21] 18.17 23.29
42# mask_rev[22] 21.39 17.85
43# mask_rev[23] 26.45 25.67
44# mask_rev[24] 9.89 17.85
45# mask_rev[25] 15.87 17.85
46# mask_rev[26] 26.45 17.85
47# mask_rev[27] 8.51 6.97
48# mask_rev[28] 10.81 9.35
49# mask_rev[29] 27.83 20.23
50# mask_rev[30] 16.33 23.29
51# mask_rev[31] 8.05 14.79
52#--------------------------------
53
54import os
55import sys
56import re
57
58def usage():
59 print("set_user_id.py <user_id_value> [<path_to_project>]")
60 return 0
61
62if __name__ == '__main__':
63
64 # Coordinate pairs in microns for the zero position on each bit
65 mask_rev = (
66 (14.49, 9.35), (16.33, 9.35), (10.35, 20.23), ( 8.05, 9.35),
67 (28.29, 9.35), (21.85, 25.67), ( 8.05, 20.23), (20.47, 9.35),
68 (17.25, 17.85), (25.53, 12.07), (22.31, 20.23), (13.11, 9.35),
69 (23.69, 23.29), (24.15, 12.07), (13.57, 17.85), (23.23, 6.97),
70 (24.15, 17.85), ( 8.51, 17.85), (23.69, 20.23), (10.81, 23.29),
71 (14.95, 6.97), (18.17, 23.29), (21.39, 17.85), (26.45, 25.67),
72 ( 9.89, 17.85), (15.87, 17.85), (26.45, 17.85), ( 8.51, 6.97),
73 (10.81, 9.35), (27.83, 20.23), (16.33, 23.29), ( 8.05, 14.79));
74
75 optionlist = []
76 arguments = []
77
78 debugmode = False
79
80 for option in sys.argv[1:]:
81 if option.find('-', 0) == 0:
82 optionlist.append(option)
83 else:
84 arguments.append(option)
85
86 if len(arguments) != 1 and len(arguments) != 2:
87 if len(arguments) != 0:
88 print("Wrong number of arguments given to cleanup_unref.py.")
89 usage()
90 sys.exit(0)
91
92 if '-debug' in optionlist:
93 debugmode = True
94
95 user_id_value = arguments[0]
96
97 # Convert to binary
98 user_id_bits = '{0:032b}'.format(int(user_id_value))
99
100 if len(arguments) == 2:
101 user_project_path = arguments[1]
102 else:
103 user_project_path = os.getcwd()
104
105 magpath = user_project_path + '/mag'
106 gdspath = user_project_path + '/gds'
107 vpath = user_project_path + '/verilog'
108 errors = 0
109
110 if os.path.isdir(gdspath):
111
112 # Bytes leading up to via position are:
113 viarec = "00 06 0d 02 00 43 00 06 0e 02 00 2c 00 2c 10 03 "
114 viabytes = bytes.fromhex(viarec)
115
116 # Read the GDS file. If a backup was made of the zero-value
117 # program, then use it.
118
119 gdsbak = gdspath + '/user_id_prog_zero.gds'
120 gdsfile = gdspath + '/user_id_programming.gds'
121
122 if os.path.isfile(gdsbak):
123 with open(gdsbak, 'rb') as ifile:
124 gdsdata = ifile.read()
125 else:
126 with open(gdsfile, 'rb') as ifile:
127 gdsdata = ifile.read()
128
129 for i in range(0,32):
130 # Ignore any zero bits.
131 if user_id_bits[i] == '0':
132 continue
133
134 coords = mask_rev[i]
135 xum = coords[0]
136 yum = coords[1]
137
138 # Contact is 0.17 x 0.17, so add and subtract 0.085 to get
139 # the corner positions.
140
141 xllum = xum - 0.085
142 yllum = yum - 0.085
143 xurum = xum + 0.085
144 yurum = yum + 0.085
145
146 # Get the 4-byte hex values for the corner coordinates
147 xllnm = round(xllum * 1000)
148 yllnm = round(yllum * 1000)
149 xllhex = '{0:08x}'.format(xllnm)
150 yllhex = '{0:08x}'.format(yllnm)
151 xurnm = round(xurum * 1000)
152 yurnm = round(yurum * 1000)
153 xurhex = '{0:08x}'.format(xurnm)
154 yurhex = '{0:08x}'.format(yurnm)
155
156 # Magic's GDS output for vias always starts at the lower left
157 # corner and goes counterclockwise, repeating the first point.
158 viaoldposdata = viarec + xllhex + yllhex + xurhex + yllhex
159 viaoldposdata += xurhex + yurhex + xllhex + yurhex + xllhex + yllhex
160
161 # For "one" bits, the X position is moved 0.92 microns to the left
162 newxllum = xllum - 0.92
163 newxurum = xurum - 0.92
164
165 # Get the 4-byte hex values for the new corner coordinates
166 newxllnm = round(newxllum * 1000)
167 newxllhex = '{0:08x}'.format(newxllnm)
168 newxurnm = round(newxurum * 1000)
169 newxurhex = '{0:08x}'.format(newxurnm)
170
171 vianewposdata = viarec + newxllhex + yllhex + newxurhex + yllhex
172 vianewposdata += newxurhex + yurhex + newxllhex + yurhex + newxllhex + yllhex
173
174 # Diagnostic
175 if debugmode:
176 print('Bit ' + str(i) + ':')
177 print('Via position ({0:3.2f}, {1:3.2f}) to ({2:3.2f}, {3:3.2f})'.format(xllum, yllum, xurum, yurum))
178 print('Old hex string = ' + viaoldposdata)
179 print('New hex string = ' + vianewposdata)
180
181 # Convert hex strings to byte arrays
182 viaoldbytedata = bytearray.fromhex(viaoldposdata)
183 vianewbytedata = bytearray.fromhex(vianewposdata)
184
185 # Replace the old data with the new
186 if viaoldbytedata not in gdsdata:
187 print('Error: via not found for bit position ' + str(i))
188 errors += 1
189 else:
190 gdsdata = gdsdata.replace(viaoldbytedata, vianewbytedata)
191
192 if errors == 0:
193 # Keep a copy of the original
194 if not os.path.isfile(gdsbak):
195 os.rename(gdsfile, gdsbak)
196
197 with open(gdsfile, 'wb') as ofile:
198 ofile.write(gdsdata)
199
200 print('Done!')
201
202 else:
203 print('There were errors in processing. No file written.')
204 sys.exit(1)
205
206 else:
207 print('No directory ' + gdspath + ' found.')
208 sys.exit(1)
209 sys.exit(0)