forked from efabless/caravel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_density.py
executable file
·616 lines (535 loc) · 21.5 KB
/
check_density.py
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2020 Efabless Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# SPDX-License-Identifier: Apache-2.0
#
# check_density.py ---
#
# Run density checks on the final (filled) GDS.
#
import sys
import os
import re
import select
import subprocess
def usage():
print("Usage:")
print("check_density.py [<path_to_project>] [-keep]")
print("")
print("where:")
print(" <path_to_project> is the path to the project top level directory.")
print("")
print(" If <path_to_project> is not given, then it is assumed to be the cwd.")
print(" If '-keep' is specified, then keep the check script.")
return 0
if __name__ == '__main__':
optionlist = []
arguments = []
debugmode = False
keepmode = False
for option in sys.argv[1:]:
if option.find('-', 0) == 0:
optionlist.append(option)
else:
arguments.append(option)
if len(arguments) > 1:
print("Wrong number of arguments given to check_density.py.")
usage()
sys.exit(0)
if len(arguments) == 1:
user_project_path = arguments[0]
else:
user_project_path = os.getcwd()
# Check for valid user path
if not os.path.isdir(user_project_path):
print('Error: Project path "' + user_project_path + '" does not exist or is not readable.')
sys.exit(1)
# Check for valid user ID
user_id_value = None
if os.path.isfile(user_project_path + '/info.yaml'):
with open(user_project_path + '/info.yaml', 'r') as ifile:
infolines = ifile.read().splitlines()
for line in infolines:
kvpair = line.split(':')
if len(kvpair) == 2:
key = kvpair[0].strip()
value = kvpair[1].strip()
if key == 'project_id':
user_id_value = value.strip('"\'')
break
if user_id_value:
project = 'caravel'
project_with_id = 'caravel_' + user_id_value
else:
print('Error: No project_id found in info.yaml file.')
sys.exit(1)
if '-debug' in optionlist:
debugmode = True
if '-keep' in optionlist:
keepmode = True
magpath = user_project_path + '/mag'
rcfile = magpath + '/.magicrc'
with open(magpath + '/check_density.tcl', 'w') as ofile:
print('#!/bin/env wish', file=ofile)
print('crashbackups stop', file=ofile)
print('drc off', file=ofile)
print('snap internal', file=ofile)
print('set starttime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
print('puts stdout "Started reading GDS: $starttime"', file=ofile)
print('', file=ofile)
print('flush stdout', file=ofile)
print('update idletasks', file=ofile)
# Read final project from .gds
print('gds readonly true', file=ofile)
print('gds rescale false', file=ofile)
print('gds read ../gds/' + project_with_id + '.gds', file=ofile)
print('', file=ofile)
print('set midtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
print('puts stdout "Starting density checks: $midtime"', file=ofile)
print('', file=ofile)
print('flush stdout', file=ofile)
print('update idletasks', file=ofile)
# Get step box dimensions (700um for size and 70um for FOM step)
# Use 350um for stepping on other layers.
print('box values 0 0 0 0', file=ofile)
# print('box size 700um 700um', file=ofile)
# print('set stepbox [box values]', file=ofile)
# print('set stepwidth [lindex $stepbox 2]', file=ofile)
# print('set stepheight [lindex $stepbox 3]', file=ofile)
print('box size 70um 70um', file=ofile)
print('set stepbox [box values]', file=ofile)
print('set stepsizex [lindex $stepbox 2]', file=ofile)
print('set stepsizey [lindex $stepbox 3]', file=ofile)
print('select top cell', file=ofile)
print('expand', file=ofile)
# Modify the box to be inside the seal ring area (shrink 5um)
print('box grow c -5um', file=ofile)
print('set fullbox [box values]', file=ofile)
print('set xmax [lindex $fullbox 2]', file=ofile)
print('set xmin [lindex $fullbox 0]', file=ofile)
print('set fullwidth [expr {$xmax - $xmin}]', file=ofile)
print('set xtiles [expr {int(ceil(($fullwidth + 0.0) / $stepsizex))}]', file=ofile)
print('set ymax [lindex $fullbox 3]', file=ofile)
print('set ymin [lindex $fullbox 1]', file=ofile)
print('set fullheight [expr {$ymax - $ymin}]', file=ofile)
print('set ytiles [expr {int(ceil(($fullheight + 0.0) / $stepsizey))}]', file=ofile)
print('box size $stepsizex $stepsizey', file=ofile)
print('set xbase [lindex $fullbox 0]', file=ofile)
print('set ybase [lindex $fullbox 1]', file=ofile)
print('', file=ofile)
print('puts stdout "XTILES: $xtiles"', file=ofile)
print('puts stdout "YTILES: $ytiles"', file=ofile)
print('', file=ofile)
# Need to know what fraction of a full tile is the last row and column
print('set xfrac [expr {($xtiles * $stepsizex - $fullwidth + 0.0) / $stepsizex}]', file=ofile)
print('set yfrac [expr {($ytiles * $stepsizey - $fullheight + 0.0) / $stepsizey}]', file=ofile)
print('puts stdout "XFRAC: $xfrac"', file=ofile)
print('puts stdout "YFRAC: $yfrac"', file=ofile)
print('cif ostyle density', file=ofile)
# Process density at steps. For efficiency, this is done in 70x70 um
# areas, dumped to a file, and then aggregated into the 700x700 areas.
print('for {set y 0} {$y < $ytiles} {incr y} {', file=ofile)
print(' for {set x 0} {$x < $xtiles} {incr x} {', file=ofile)
print(' set xlo [expr $xbase + $x * $stepsizex]', file=ofile)
print(' set ylo [expr $ybase + $y * $stepsizey]', file=ofile)
print(' set xhi [expr $xlo + $stepsizex]', file=ofile)
print(' set yhi [expr $ylo + $stepsizey]', file=ofile)
print(' box values $xlo $ylo $xhi $yhi', file=ofile)
# Flatten this area
print(' flatten -dobbox -nolabels tile', file=ofile)
print(' load tile', file=ofile)
print(' select top cell', file=ofile)
# Run density check for each layer
print(' puts stdout "Density results for tile x=$x y=$y"', file=ofile)
print(' set fdens [cif list cover fom_all]', file=ofile)
print(' set pdens [cif list cover poly_all]', file=ofile)
print(' set ldens [cif list cover li_all]', file=ofile)
print(' set m1dens [cif list cover m1_all]', file=ofile)
print(' set m2dens [cif list cover m2_all]', file=ofile)
print(' set m3dens [cif list cover m3_all]', file=ofile)
print(' set m4dens [cif list cover m4_all]', file=ofile)
print(' set m5dens [cif list cover m5_all]', file=ofile)
print(' puts stdout "FOM: $fdens"', file=ofile)
print(' puts stdout "POLY: $pdens"', file=ofile)
print(' puts stdout "LI1: $ldens"', file=ofile)
print(' puts stdout "MET1: $m1dens"', file=ofile)
print(' puts stdout "MET2: $m2dens"', file=ofile)
print(' puts stdout "MET3: $m3dens"', file=ofile)
print(' puts stdout "MET4: $m4dens"', file=ofile)
print(' puts stdout "MET5: $m5dens"', file=ofile)
print(' flush stdout', file=ofile)
print(' update idletasks', file=ofile)
print(' load ' + project_with_id, file=ofile)
print(' cellname delete tile', file=ofile)
print(' }', file=ofile)
print('}', file=ofile)
print('set endtime [orig_clock format [orig_clock seconds] -format "%D %T"]', file=ofile)
print('puts stdout "Ended: $endtime"', file=ofile)
print('', file=ofile)
myenv = os.environ.copy()
# Real views are necessary for the DRC checks
myenv['MAGTYPE'] = 'mag'
print('Running density checks on file ' + project_with_id + '.gds', flush=True)
mproc = subprocess.Popen(['magic', '-dnull', '-noconsole',
'-rcfile', rcfile, magpath + '/check_density.tcl'],
stdin = subprocess.DEVNULL,
stdout = subprocess.PIPE,
stderr = subprocess.PIPE,
cwd = magpath,
env = myenv,
universal_newlines = True)
# Use signal to poll the process and generate any output as it arrives
dlines = []
while mproc:
status = mproc.poll()
if status != None:
try:
output = mproc.communicate(timeout=1)
except ValueError:
print('Magic forced stop, status ' + str(status))
sys.exit(1)
else:
outlines = output[0]
errlines = output[1]
for line in outlines.splitlines():
dlines.append(line)
print(line)
for line in errlines.splitlines():
print(line)
print('Magic exited with status ' + str(status))
if int(status) != 0:
sys.exit(int(status))
else:
break
else:
n = 0
while True:
n += 1
if n > 100:
n = 0
status = mproc.poll()
if status != None:
break
sresult = select.select([mproc.stdout, mproc.stderr], [], [], 0.5)[0]
if mproc.stdout in sresult:
outstring = mproc.stdout.readline().strip()
dlines.append(outstring)
print(outstring)
elif mproc.stderr in sresult:
outstring = mproc.stderr.readline().strip()
print(outstring)
else:
break
fomfill = []
polyfill = []
lifill = []
met1fill = []
met2fill = []
met3fill = []
met4fill = []
met5fill = []
xtiles = 0
ytiles = 0
xfrac = 0.0
yfrac = 0.0
for line in dlines:
dpair = line.split(':')
if len(dpair) == 2:
layer = dpair[0]
try:
density = float(dpair[1].strip())
except:
continue
if layer == 'FOM':
fomfill.append(density)
elif layer == 'POLY':
polyfill.append(density)
elif layer == 'LI1':
lifill.append(density)
elif layer == 'MET1':
met1fill.append(density)
elif layer == 'MET2':
met2fill.append(density)
elif layer == 'MET3':
met3fill.append(density)
elif layer == 'MET4':
met4fill.append(density)
elif layer == 'MET5':
met5fill.append(density)
elif layer == 'XTILES':
xtiles = int(dpair[1].strip())
elif layer == 'YTILES':
ytiles = int(dpair[1].strip())
elif layer == 'XFRAC':
xfrac = float(dpair[1].strip())
elif layer == 'YFRAC':
yfrac = float(dpair[1].strip())
if ytiles == 0 or xtiles == 0:
print('Failed to read XTILES or YTILES from output.')
sys.exit(1)
total_tiles = (ytiles - 9) * (xtiles - 9)
print('')
print('Density results (total tiles = ' + str(total_tiles) + '):')
# For FOM, step at 70um intervals (same as 70um check area)
fomstep = 1
# For poly, step only at 700um intervals (10 * 70um check area)
polystep = 10
# For all metals, step only at 350um intervals (5 * 70um check area)
metalstep = 5
# Full areas are 10 x 10 tiles = 100. But the right and top sides are
# not full tiles, so the full area must be prorated.
sideadjust = 90.0 + (10.0 * xfrac)
topadjust = 90.0 + (10.0 * yfrac)
corneradjust = 81.0 + (9.0 * xfrac) + (9.0 * yfrac) + (xfrac * yfrac)
print('')
print('FOM Density:')
for y in range(0, ytiles - 9, fomstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, fomstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
fomaccum = 0
for w in range(y, y + 10):
base = xtiles * w + x
fomaccum += sum(fomfill[base : base + 10])
fomaccum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(fomaccum))
if fomaccum < 0.33:
print('***Error: FOM Density < 33%')
elif fomaccum > 0.57:
print('***Error: FOM Density > 57%')
print('')
print('POLY Density:')
for y in range(0, ytiles - 9, polystep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, polystep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
polyaccum = 0
for w in range(y, y + 10):
base = xtiles * w + x
polyaccum += sum(polyfill[base : base + 10])
polyaccum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(polyaccum))
print('')
print('LI Density:')
for y in range(0, ytiles - 9, metalstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, metalstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
liaccum = 0
for w in range(y, y + 10):
base = xtiles * w + x
liaccum += sum(lifill[base : base + 10])
liaccum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(liaccum))
if liaccum < 0.35:
print('***Error: LI Density < 35%')
elif liaccum > 0.60:
print('***Error: LI Density > 60%')
print('')
print('MET1 Density:')
for y in range(0, ytiles - 9, metalstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, metalstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
met1accum = 0
for w in range(y, y + 10):
base = xtiles * w + x
met1accum += sum(met1fill[base : base + 10])
met1accum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(met1accum))
if met1accum < 0.35:
print('***Error: MET1 Density < 35%')
elif met1accum > 0.60:
print('***Error: MET1 Density > 60%')
print('')
print('MET2 Density:')
for y in range(0, ytiles - 9, metalstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, metalstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
met2accum = 0
for w in range(y, y + 10):
base = xtiles * w + x
met2accum += sum(met2fill[base : base + 10])
met2accum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(met2accum))
if met2accum < 0.35:
print('***Error: MET2 Density < 35%')
elif met2accum > 0.60:
print('***Error: MET2 Density > 60%')
print('')
print('MET3 Density:')
for y in range(0, ytiles - 9, metalstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, metalstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
met3accum = 0
for w in range(y, y + 10):
base = xtiles * w + x
met3accum += sum(met3fill[base : base + 10])
met3accum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(met3accum))
if met3accum < 0.35:
print('***Error: MET3 Density < 35%')
elif met3accum > 0.60:
print('***Error: MET3 Density > 60%')
print('')
print('MET4 Density:')
for y in range(0, ytiles - 9, metalstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, metalstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
met4accum = 0
for w in range(y, y + 10):
base = xtiles * w + x
met4accum += sum(met4fill[base : base + 10])
met4accum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(met4accum))
if met4accum < 0.35:
print('***Error: MET4 Density < 35%')
elif met4accum > 0.60:
print('***Error: MET4 Density > 60%')
print('')
print('MET5 Density:')
for y in range(0, ytiles - 9, metalstep):
if y == ytiles - 10:
atotal = topadjust
else:
atotal = 100.0
for x in range(0, xtiles - 9, metalstep):
if x == xtiles - 10:
if y == ytiles - 10:
atotal = corneradjust
else:
atotal = sideadjust
met5accum = 0
for w in range(y, y + 10):
base = xtiles * w + x
met5accum += sum(met5fill[base : base + 10])
met5accum /= atotal
print('Tile (' + str(x) + ', ' + str(y) + '): ' + str(met5accum))
if met5accum < 0.45:
print('***Error: MET5 Density < 45%')
elif met5accum > 0.76:
print('***Error: MET5 Density > 76%')
print('')
print('Whole-chip density results:')
atotal = ((xtiles - 1.0) * (ytiles - 1.0)) + ((ytiles - 1.0) * xfrac) + ((xtiles - 1.0) * yfrac) + (xfrac * yfrac)
fomaccum = sum(fomfill) / atotal
print('')
print('FOM Density: ' + str(fomaccum))
if fomaccum < 0.33:
print('***Error: FOM Density < 33%')
elif fomaccum > 0.57:
print('***Error: FOM Density > 57%')
polyaccum = sum(polyfill) / atotal
print('')
print('POLY Density: ' + str(polyaccum))
liaccum = sum(lifill) / atotal
print('')
print('LI Density: ' + str(liaccum))
if liaccum < 0.35:
print('***Error: LI Density < 35%')
elif liaccum > 0.60:
print('***Error: LI Density > 60%')
met1accum = sum(met1fill) / atotal
print('')
print('MET1 Density: ' + str(met1accum))
if met1accum < 0.35:
print('***Error: MET1 Density < 35%')
elif met1accum > 0.60:
print('***Error: MET1 Density > 60%')
met2accum = sum(met2fill) / atotal
print('')
print('MET2 Density: ' + str(met2accum))
if met2accum < 0.35:
print('***Error: MET2 Density < 35%')
elif met2accum > 0.60:
print('***Error: MET2 Density > 60%')
met3accum = sum(met3fill) / atotal
print('')
print('MET3 Density: ' + str(met3accum))
if met3accum < 0.35:
print('***Error: MET3 Density < 35%')
elif met3accum > 0.60:
print('***Error: MET3 Density > 60%')
met4accum = sum(met4fill) / atotal
print('')
print('MET4 Density: ' + str(met4accum))
if met4accum < 0.35:
print('***Error: MET4 Density < 35%')
elif met4accum > 0.60:
print('***Error: MET4 Density > 60%')
met5accum = sum(met5fill) / atotal
print('')
print('MET5 Density: ' + str(met5accum))
if met5accum < 0.45:
print('***Error: MET5 Density < 45%')
elif met5accum > 0.76:
print('***Error: MET5 Density > 76%')
if not keepmode:
if os.path.isfile(magpath + '/check_density.tcl'):
os.remove(magpath + '/check_density.tcl')
print('')
print('Done!')
sys.exit(0)