2 #-----------------------------------------------------------
3 # Python script which writes a SPICE-format netlist.
4 # Replaces the code formerly in "netlist.c" (deprecated).
5 # Python scripting is now the preferred method for handling
6 # netlist output formats.
7 #-----------------------------------------------------------
9 # Select the device string corresponding to the given prefix.
10 # Return the body of the string, or an empty string if the prefix doesn't match.
12 def select(sstr
, prefix
):
14 if sstr
.startswith(prefix
):
15 ltext
+= sstr
[len(prefix
) + 1:]
18 # Generate an ASCII string from an xcircuit string (list)
20 def textprint(slist
, params
):
27 except AttributeError: # must be a string
30 elif x
== 'Underline':
34 else: # is a dictionary; will have only one key
36 lfont
= x
[x
.keys()[0]]
37 if lfont
.startswith('Symbol'):
41 if lfont
.endswith('ISO'):
45 elif f
== 'Parameter':
46 ltext
+= textprint(params
[x
[x
.keys()[0]]], [])
47 else: # text: SPICE translates "mu" to "u"
48 for y
in x
[x
.keys()[0]]:
60 ltext
+= '/' + str(ord(y
))
66 # Flatten the netlist and write to the output
68 def recurseflat(outfile
, ckt
, clist
):
70 v
= ckt
['calls'] # calls to subcircuits
71 except KeyError: # A bottom-level circuit element
76 if z
['name'] == y
['name']:
77 # copy the object and substitute net names into subcircuit ports
79 lobj
['ports'] = y
['ports']
80 recurseflat(outfile
, lobj
, clist
)
89 lstr
= select(textprint(u
, []), 'spice')
91 outfile
.write('device: ' + lstr
+ '\n')
93 # Top of the flattened-circuit writing routine
104 outfile
=open(topname
, 'w')
110 outfile
.write('*SPICE flattened circuit "' + topname
+ '"')
111 outfile
.write(' from XCircuit v' + str(xc_version
))
112 outfile
.write(' (Python script "flatspice.py")\n')
114 # print global variables
116 for x
in g
: # 'globals' is a list of strings
117 outfile
.write('.GLOBAL ' + textprint(x
, []) + '\n')
120 recurseflat(outfile
, top
, c
)
121 outfile
.write('.end\n')
124 # Key binding and menu button for the spice netlist output
125 # bind('Alt_F', 'writespiceflat')
126 newbutton('Netlist', 'Write Flattened Spice', 'writespiceflat')