This repository was archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmagic.py
More file actions
523 lines (472 loc) · 16.3 KB
/
magic.py
File metadata and controls
523 lines (472 loc) · 16.3 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
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
#!/usr/bin/env python2
from __future__ import print_function
import sys, os
import time
import subprocess
import copy
RAWGITURL="https://raw.githubusercontent.com/tdene/Magic/master/"
# "installation" script:
# wget -O - https://raw.githubusercontent.com/tdene/Magic/master/magic.py > magic.py && chmod u+x magic.py && ./magic.py
INFILE=None
OUTFILE=None
NOUPDATE=None
STRETCH=None
FLIP=None
ANALYZE=None
IRSIM=None
NOWELL=None
NOSTRCON=None
JUSTTHIS=None
TS=str(int(time.time()))+'.451' #personal flair
VDD=None
GND=None
_MINDIFCON=10
SCRIPTDIR=os.path.dirname(os.path.realpath(sys.argv[0]))
HOMEDIR=None
def _flatlist(l):
ret=[]
for k in l:
for a in k:
ret.append(a)
return ret
def _intsplit(l):
def eint(x):
try:
return int(x)
except:
return x
return [eint(x) for x in l.split(' ')]
def _npswap(s):
if s in ['polysilicon','nwell','polycontact']:
return s
if s=='nsubstratencontact':
return 'psubstratepcontact'
if s=='psubstratepcontact':
return 'nsubstratencontact'
if s[0]=='n':
return 'p'+s[1:]
if s[0]=='p':
return 'n'+s[1:]
return s
def _topbottom(dic):
top=None; bot=None;
for a in dic:
if a in ['labels','header','nwell']:
continue
for b in dic[a]:
for c in b:
if top==None or top<c[1]:
top=c[1]
if bot==None or bot>c[1]:
bot=c[1]
return (top,bot)
z2n = lambda x: (2*x-1)
def _stretch(dic,line,dr,dl,le,lcont=[]):
f=lambda dr,dl: -1*z2n(dl)*z2n(dr)
for a in dic:
if a in ['header']:
continue
for b in dic[a]:
if a=='labels':
b=b[0]
if z2n(dr)*(line-b[dr][1])<=0 and z2n(dr)*(line-b[1-dr][1])>0:
contflag=a in ['ndcontact','pdcontact']
contflag=contflag and (NOSTRCON or (dl and b[1][1]-b[0][1]<=_MINDIFCON))
shiftflag=a in ['metal1','metal2','m2contact','polycontact','labels']
shiftflag=shiftflag or contflag
if shiftflag:
b[1-dr][1]+=f(dr,dl)
b[dr][1]+=f(dr,dl)
if a in ['ndcontact','pdcontact'] and contflag and not dl:
xy=[b[0][1],b[0][1]-f(dr,dl)]
(x,y)=(min(xy),max(xy))
flag=True
for i in lcont:
if (x,y)[1-dr]==i[dr][1] and b[0][0]==i[0][0]:
i[dr][1]=(x,y)[dr]
flag=False
if flag:
x=[[b[0][0],min(xy)],[b[1][0],max(xy)]]
lcont.append(x)
dic[a[0]+'diffusion'].append(x)
elif (z2n(dr)*(line-b[1-dr][1])<=0):
b[1-dr][1]+=f(dr,dl)
b[dr][1]+=f(dr,dl)
def processArgs():
global NOUPDATE, NOWELL, NOSTRCON, STRETCH, FLIP, INFILE, OUTFILE, ANALYZE, IRSIM, JUSTTHIS
reqinputs=0
for a in range(len(sys.argv)):
b=sys.argv[a]
if a==0:
continue
if a==1 and b in ['-h','-help','h','help']:
print('Usage: magic.py [OPTIONS] <src> <dest>')
print('OPTIONS:')
print('--noupdate\t\tDo not attempt to update script to most current version.')
print('-flip\t\t\tSwaps the design\'s PMOS/NMOS logic.')
print('-nowell\t\t\tRemoves all n-wells (-flip will not work if multiple n-wells are present)\n')
print('-stretch <n/p> <initial size:final size> <p/n> <initial size:final size>')
print('\t\t\tAtempts to stretch all transistors in the circuit by the given integer ratio.')
print('\t\t\tRequires user to specify both n and p stretch ratios, even if one of them is 1:1.')
print('\t\t\tSample usage: ./magic.py -stretch n 10:20 p 2:1 src.mag dest.mag\n')
print('-nostretchcontact\tOptional argument to be used with the -stretch script.')
print('\t\t\tKeeps diffusion contacts the same size, and does not stretch them.')
print('\t\t\tNot really supported; this argument has a high chance of producing errors.\n')
print('-analyze\t\tRuns analyze.sh on a given design.\n')
print('-analyzeAll\tOnly runs analyze.sh on the input file.\n')
print('-irsim\t\t\tPerforms and prints an IRSIM analysis.')
print('-help\t\t\tPrints this page.')
sys.exit(0)
if b=='--noupdate':
NOUPDATE=True
if b=='-analyze':
ANALYZE=True
JUSTTHIS=True
if not reqinputs:
reqinputs=1
if b=='-analyzeAll':
ANALYZE=True
if b=='-irsim':
IRSIM=True
if not reqinputs:
reqinputs=1
if b=='-flip':
FLIP=True
reqinputs=2
if b=='-nowell':
NOWELL=True
if b=='-stretch':
try:
assert sys.argv[a+1] in ['p','n']
assert sys.argv[a+3] in ['p','n']
STRETCH={
sys.argv[a+1]:[int(x) for x in sys.argv[a+2].split(':')],
sys.argv[a+3]:[int(x) for x in sys.argv[a+4].split(':')]
}
except:
print('Malformed -stretch arguments')
sys.exit(1)
reqinputs=2
if b=='-nostretchcontact':
NOSTRCON=True
def _validFile(x,rec=True):
for a in [x,os.path.join(HOMEDIR,x),os.path.join(HOMEDIR,x,'magic',x)]:
if os.path.isfile(a):
return a
if os.path.isfile(a+'.mag'):
return a+'.mag'
return None
if reqinputs==2 and a>len(sys.argv)-3:
x=2-(len(sys.argv)-a)
if not x:
tmp=_validFile(b)
else:
b_=os.path.abspath(b)
if SCRIPTDIR not in b_ and HOMEDIR not in b_:
print("I don't want to let you scatter output files in random places.")
sys.exit(1)
if not x and not tmp:
print('The last two arguments must be input and output files.')
sys.exit(1)
if x:
OUTFILE=os.path.abspath(b)
else:
INFILE=os.path.abspath(tmp)
if reqinputs==1 and a>len(sys.argv)-2:
tmp=_validFile(b)
if(not tmp):
print('The last argument must be a valid file name.')
sys.exit(1)
INFILE=os.path.abspath(tmp)
def findHome():
global HOMEDIR
# Helper
getSubdirs=lambda d: [x for x in os.listdir(d) if os.path.isdir(os.path.join(d,x))]
# Check if a dir is "HOMEDIR"
def isHome(d):
# Get all subdirectories
dirs=getSubdirs(d)
# Get all subdirectories of subdirectories
for x in getSubdirs(os.path.join(d,dirs[0])):
# If one of them is 'magic' or 'sue', we're golden
if x in ['magic','lvs','sue']:
return True
return False
# Check SCRIPTDIR
if isHome(SCRIPTDIR):
HOMEDIR=SCRIPTDIR
return
# Check cwd
if isHome(os.getcwd()):
HOMEDIR=os.getcwd()
return
# Check one folder down from SCRIPTDIR
for x in getSubdirs(SCRIPTDIR):
x=os.path.join(SCRIPTDIR,x)
if isHome(x):
HOMEDIR=x
return
# Check one folder down from cwd
for x in getSubdirs(os.getcwd()):
x=os.path.join(os.getcwd(),x)
if isHome(x):
HOMEDIR=x
return
# Check one folder up from cwd
x=os.path.abspath(os.path.join(os.getcwd(),'..'))
if isHome(x):
HOMEDIR=x
return
# Check two folders up from cwd
x=os.path.abspath(os.path.join(os.getcwd(),'..'))
if isHome(x):
HOMEDIR=x
return
# Give up
print('Folder structure not recognized. Scripts that rely on multiple tools will fail.')
HOMEDIR=os.getcwd()
def update():
server=None; cliver=None;
try:
server=subprocess.Popen(["wget","-qO","-",RAWGITURL+"version.txt"],stdout=subprocess.PIPE).communicate()[0]
except:
print("\"Distribution server\" is offline.")
return
try:
with open(os.path.expanduser('~/.teo'),'r') as f:
cliver=f.read()
except:
print("Cannot find local version number.\n")
cliver=1
if server!=None and cliver!="0\n" and server!=cliver:
print("Updating script.\n")
subprocess.call(["wget",RAWGITURL+"magic.py","-O",os.path.join(SCRIPTDIR,"magic.py")])
subprocess.call(["chmod","u+x","magic.py"])
subprocess.call(["wget",RAWGITURL+"analyze.sh","-O",os.path.join(SCRIPTDIR,"analyze.sh")])
subprocess.call(["chmod","u+x","analyze.sh"])
subprocess.call(["wget",RAWGITURL+"version.txt","-O",os.path.expanduser("~/.teo")])
print("Restarting script.\n")
os.execl(sys.executable,sys.executable,*sys.argv)
sys.exit(0)
def readMagic(f,dic):
global VDD, GND
with open(f,'r') as f:
cur=None
for n,l in enumerate(f):
l=l.replace('\n','').replace('\r','')
if n<3:
if n==0:
dic['header']=[]
if n==2:
l='timestamp '+str(TS)
dic['header'].append(l)
continue
if l=='<< end >>':
break
if l=='<< nwell >>' and NOWELL:
cur=None; continue;
if l[:2]=='<<':
cur=l[3:-3]
dic[cur]=[]
if l[:4]=='rect':
cord=_intsplit(l[5:])
if cur:
dic[cur].append([cord[:2],cord[2:]])
if l[:6]=='rlabel':
cord=_intsplit(l[7:])
dic[cur].append([[cord[1:3],cord[3:5]],[cord[0],cord[5],cord[6]]])
if 'vdd' in cord[6].lower():
VDD=cord[6]
if 'gnd' in cord[6].lower():
GND=cord[6]
return dic
def writeMagic(f,dic):
with open(f,'w') as f:
head=dic.pop('header')
lbls=dic.pop('labels')
for a in head:
print(a,file=f)
for a in dic:
print('<< '+a+' >>',file=f)
for b in dic[a]:
print('rect',*_flatlist(b),file=f,sep=' ')
print('<< labels >>',file=f)
for a in lbls:
print('rlabel',a[1][0],*_flatlist(a[0]),file=f,sep=' ',end=' ')
print(*a[1][1:],file=f,sep=' ')
print('<< end >>',file=f)
global INFILE; INFILE=OUTFILE;
def flip():
dic={}
readMagic(INFILE,dic)
dicret={}
for a in dic:
if a=='header':
dicret[a]=dic[a]
continue
# Assumes single, rectangular, nwell
if a=='nwell':
(top,bot)=_topbottom(dic)
b=dic[a][0]
tmp=b[0][1]
b[0][1]=(bot+top)-b[1][1]
b[1][1]=tmp
for b in dic[a]:
if a=='labels':
if b[1][1] in [1,5]:
b[1][1]=6-b[1][1]
if b[1][2]==VDD:
b[1][2]=GND
elif b[1][2]==GND:
b[1][2]=VDD
b=b[0]
sys.stdout.flush()
tmp=b[0][1]
b[0][1]=-b[1][1]
b[1][1]=-tmp
dicret[_npswap(a)]=dic[a]
writeMagic(OUTFILE,dicret)
def stretch():
def findLine(a,dr):
def restrict(x):
for i in dic['restrictedArea']:
if z2n(dr)*(x-i[dr][1])<=0 and z2n(dr)*(x-i[1-dr][1])>0:
return True
return False
ret=a[dr][1]
while restrict(ret):
ret-=z2n(dr)
if z2n(dr)*(ret-a[not dr][1])<0:
print("Transistor stretching failed. Best attempt returned.")
return ret
dic={}
readMagic(INFILE,dic)
lcont=[]
tlens={'n':{},'p':{}}
for idx,a in enumerate(dic['ntransistor']):
tlens['n'][idx]=a[1][1]-a[0][1]
for idx,a in enumerate(dic['ptransistor']):
tlens['p'][idx]=a[1][1]-a[0][1]
niter=sorted(tlens['n'].iteritems(),key=lambda (k,v): v)
piter=sorted(tlens['p'].iteritems(),key=lambda (k,v): v)
dic['restrictedArea']=[]
for idx,le in niter:
dl=STRETCH['n'][1]<STRETCH['n'][0]
a=dic['ntransistor'][idx]
while(a[1][1]-a[0][1]!=le*STRETCH['n'][1]/STRETCH['n'][0]):
line=findLine(a,False)
_stretch(dic,line,False,dl,lcont)
dic['restrictedArea'].append(copy.deepcopy(a))
for idx,le in piter:
dl=STRETCH['p'][1]<STRETCH['p'][0]
a=dic['ptransistor'][idx]
while(a[1][1]-a[0][1]!=le*STRETCH['p'][1]/STRETCH['p'][0]):
line=findLine(a,True)
_stretch(dic,line,True,dl,lcont)
dic['restrictedArea'].append(copy.deepcopy(a))
del dic['restrictedArea']
writeMagic(OUTFILE,dic)
def analyze():
os.chdir(SCRIPTDIR)
subdirs=[x for x in os.listdir(SCRIPTDIR)
if os.path.isdir(os.path.join(SCRIPTDIR,x))]
if JUSTTHIS:
if OUTFILE:
subdirs=[os.path.basename(OUTFILE)]
elif INFILE:
subdirs=[os.path.basename(INFILE)]
else:
print('This error should be unreachable')
sys.exit(1)
if subdirs[0][-4:]=='.mag':
subdirs[0]=subdirs[0][:-4]
for x in subdirs:
if x not in ['output','calibre']:
subprocess.call([os.path.join(SCRIPTDIR,'analyze.sh'),x])
def irsim():
# Copy the global INFILE
inf=INFILE
# If we get a mag file input
if inf[-4:]=='.mag':
# Save cwd
tmp=os.getcwd()
# Navigate to the file's folder
os.chdir(os.path.dirname(inf))
# Check if there is a .sim file
if not os.path.isfile(inf[:-4]+'.sim'):
# Make a .sim file if necessary
subprocess.call(['ext4mag',inf])
# Change input to the .sim file
inf=inf[:-4]+'.sim'
# Reset everything
os.chdir(tmp)
# Get list of inputs/outputs
inouts=set()
with open(inf,'r') as f:
for l in f:
# Assume that all inputs/outputs will be connected to FETs
if l[0] in ['p','n']:
l=_intsplit(l)
for x in l:
if isinstance(x,str):
# Weed out virtual nets
if x[-1]!='#' and x not in ['p','n']:
inouts.add(x)
# Remove Vdd / GND
toRemove=[]
for x in inouts:
if x.lower() in ['vdd','gnd']:
toRemove.append(x)
for x in toRemove:
inouts.remove(x)
# Ask the user to distinguish ins from outs
# We can probably do this automatically, based on the transistors
# On the to-do list
print('\nThe .sim file contains the following inputs/outputs:')
print(*inouts,sep=' ')
while(True):
outs=raw_input('Please tell me which of them are actually outputs. Separate with spaces.\n')
outs=_intsplit(outs)
for x in outs:
if x not in inouts:
print('You had typos. Here is the list again.')
print(*inouts,sep=' ')
continue
break
# Split off inputs
ins=[]
for x in inouts:
if x not in outs:
ins.append(x)
with open('.tmp.cmd','w') as f:
print('stepsize 50',file=f)
print('analyzer',*inouts,sep=' ',file=f)
print('vector','ins',*ins,sep=' ',file=f)
for x in range(2**len(ins)):
b=bin(x)[2:]
b='0'*(len(ins)-len(b))+b
print('setvector','ins',b,sep=' ',file=f)
print('s',file=f)
print('simtime left [simtime begin]',file=f)
print('simtime right [simtime end]',file=f)
simfile=os.path.basename(inf[:-4])+'_sim.ps'
simfile=os.path.join(HOMEDIR,'output',simfile)
print('print','file',simfile,sep=' ',file=f)
print('exit',end='',file=f)
# Call irsim
subprocess.call(['irsim','/classes/ecen4303F18/scmos30.prm',inf,'-.tmp.cmd'])
# Clean up .tmp.cmd
subprocess.call(['rm','.tmp.cmd'])
if __name__ == '__main__':
findHome()
processArgs()
if not NOUPDATE:
update()
if FLIP:
flip()
if STRETCH:
stretch()
if ANALYZE:
analyze()
if IRSIM:
irsim()