Initial commit at Tue Apr 25 08:36:02 EDT 2017 by tim on stravinsky
[xcircuit.git] / lib / tcl / sue_xc.tcl
blob695cb0b2c5256081dff221cd57b2aa7129d05ab0
1 #------------------------------------------
2 # sue_xc.tcl
3 #------------------------------------------
4 # This script should be sourced into
5 # XCircuit and provides the capability to
6 # translate .sue files into XCircuit
7 # schematics. This script works properly
8 # with XCircuit version 3.2.24 or newer.
9 #------------------------------------------
10 # The primary routine is "make_sue_all",
11 # which is a TCL procedure to be run from
12 # the XCircuit command line in the directory
13 # containing .sue format files. Without
14 # options, it creates a single XCircuit
15 # PostScript output file "sue_gates.ps",
16 # containing all of the gate symbols and
17 # associated schematics in a single file.
18 #------------------------------------------
19 # Written by R. Timothy Edwards
20 # 8/23/04
21 # MultiGiG, Inc.
22 # Scotts Valley, CA
23 #------------------------------------------
25 global fscale
26 set fscale 1.6
28 #------------------------------------------------------------
29 # scale an {x y} list value from SUE units to XCircuit units
30 #------------------------------------------------------------
32 proc scale_coord {coord} {
33 global fscale
34 set x [lindex $coord 0]
35 set y [lindex $coord 1]
36 set newc [lreplace $coord 0 1 [expr {int($x * $fscale)}] \
37 [expr {int(-$y * $fscale)}]]
38 return $newc
41 #------------------------------------------------------------
42 # make and make_wire: create the schematic elements
43 #------------------------------------------------------------
45 proc make {type args} {
46 global fscale
48 if {[llength $args] == 1} {
49 set args [lindex $args 0]
52 # default values
53 set flipped {}
54 set angle 0
55 set width 1
56 set length 1
57 set name bad_element
58 set origin {0 0}
59 set instance_params {}
61 foreach {key value} $args {
62 switch -- $key {
63 -orient {
64 switch -- $value {
65 RXY {
66 set angle 180
68 RX {
69 set flipped horizontal
71 RY {
72 set flipped vertical
73 set angle 180
75 R270 {
76 set angle 270
78 R90 {
79 set angle 90
81 R0 {
82 # do nothing
86 -W {
87 set width $value
89 -L {
90 set length $value
92 -origin {
93 set origin $value
95 -name {
96 set name $value
98 -text {
99 set name $value
101 default {
102 lappend instance_params [list [string range $key 1 end] $value]
107 set origin [scale_coord $origin]
109 switch -- $type {
110 pmos {
111 set newgate [instance make pMOS $origin]
112 # SUE pMOS is wider than xcircuit pMOS.
113 set x1 [lindex $origin 0]
114 set s1 [lreplace $origin 0 0 [expr {$x1 - 64}]]
115 set s2 [lreplace $origin 0 0 [expr {$x1 - 96}]]
116 set newpoly [polygon make 2 $s1 $s2]
117 select $newgate
118 parameter set width $width -forward
119 parameter set length $length -forward
120 select [list $newgate $newpoly]
121 rotate $angle $origin
122 if {$flipped != {}} {
123 select [list $newgate $newpoly]
124 flip $flipped $origin
128 nmos {
129 set newgate [instance make nMOS $origin]
130 # SUE nMOS is wider than xcircuit nMOS.
131 set x1 [lindex $origin 0]
132 set s1 [lreplace $origin 0 0 [expr {$x1 - 64}]]
133 set s2 [lreplace $origin 0 0 [expr {$x1 - 96}]]
134 set newpoly [polygon make 2 $s1 $s2]
135 select $newgate
136 parameter set width $width -forward
137 parameter set length $length -forward
138 select [list $newgate $newpoly]
139 rotate $angle $origin
140 if {$flipped != {}} {
141 select [list $newgate $newpoly]
142 flip $flipped $origin
146 input -
147 output -
148 name_net -
149 name_net_s {
150 set newtext [label make pin $name $origin]
151 rotate $newtext $angle $origin
152 if {$flipped != {}} {
153 flip $newtext $flipped $origin
157 global {
158 set newtext [label make global $name $origin]
159 rotate $newtext $angle $origin
160 if {$flipped != {}} {
161 flip $newtext $flipped $origin
165 text {
166 set newtext [list $name]
167 while {[set rp [string first \n $newtext]] >= 0} {
168 set newtext [string replace $newtext $rp $rp "\} \{return\} \{"]
169 set rp [string first \n $newtext]
171 set newtext [label make normal $newtext $origin]
172 rotate $newtext $angle $origin
173 if {$flipped != {}} {
174 flip $newtext $flipped $origin
178 # Default behavior is to generate an object instance of the
179 # given name. This assumes that these are only objects that
180 # have been defined in .sue files already.
182 default {
183 set newgate [instance make $type $origin]
184 select $newgate
185 rotate $angle $origin
186 if {$flipped != {}} {
187 select $newgate
188 flip $flipped $origin
190 if {$instance_params != {}} {
191 select $newgate
192 foreach pair $instance_params {
193 set key [lindex $pair 0]
194 set value [lindex $pair 1]
195 parameter set $key $value -forward
197 deselect
201 deselect
204 #------------------------------------------------------------
205 # Draw text on the schematic
206 #------------------------------------------------------------
208 proc make_text {args} {
209 make text $args
212 #------------------------------------------------------------
213 # Draw a wire into the schematic
214 #------------------------------------------------------------
216 proc make_wire {x1 y1 x2 y2} {
217 global fscale
218 # Scale the origin from SUE units to XCircuit units
219 set sx1 [expr {int($x1 * $fscale)}]
220 set sy1 [expr {int(-$y1 * $fscale)}]
221 set sx2 [expr {int($x2 * $fscale)}]
222 set sy2 [expr {int(-$y2 * $fscale)}]
223 polygon make 2 [list $sx1 $sy1] [list $sx2 $sy2]
226 proc make_line {args} {
227 eval "make_wire $args"
230 #------------------------------------------------------------
231 # icon_*: create the symbol
232 #------------------------------------------------------------
234 #------------------------------------------------------------
235 # default parameters (deferred)
236 #------------------------------------------------------------
238 proc icon_setup {icon_args params} {
239 global icon_params
240 set icon_params [concat $icon_params $params]
243 #------------------------------------------------------------
244 # pins
245 #------------------------------------------------------------
247 proc icon_term {args} {
248 set pintype "no_pin"
249 set origin {0 0}
250 set name "bad_pin_name"
252 foreach {key value} $args {
253 switch -- $key {
254 -type {
255 set pintype $value
257 -origin {
258 set origin $value
260 -name {
261 set name $value
265 set newtext [label make pin $name $origin]
268 #------------------------------------------------------------
269 # instance parameters and symbol text labels
270 #------------------------------------------------------------
272 proc icon_property {args} {
274 set proptype {}
275 set origin {0 0}
276 set name "bad_parameter"
278 foreach {key value} $args {
279 switch -- $key {
280 -origin {
281 set origin $value
283 -name {
284 set name $value
286 -type {
287 set proptype $value
289 -size {
290 # label size. Ignore, for now.
292 -label {
293 label make normal "$value" [scale_coord $origin]
299 #------------------------------------------------------------
300 # Line drawing on the symbol
301 #------------------------------------------------------------
303 proc icon_line {args} {
304 set coords {}
305 set i 0
306 foreach {x y} $args {
307 set s [scale_coord [list $x $y]]
308 lappend coords $s
309 incr i
311 eval "polygon make $i $coords"
314 #------------------------------------------------------------
315 # Main routine: Load the .sue file for the indicated
316 # gate. Draw the schematic and the (user library) symbol,
317 # and associate them.
318 #------------------------------------------------------------
320 proc make_sue_gate {name} {
321 global icon_params
322 source ${name}.sue
324 # Go to a new page unless the current one is empty
325 while {[llength [object parts]] > 0} {
326 set p [page]
327 incr p
328 while {[catch {page $p}]} {
329 page make
333 # Evaluate the symbol. Generate the symbol in xcircuit.
334 # Then clear the page to make the schematic
335 set icon_params {}
336 eval "ICON_${name}"
337 set hlist [object parts]
338 object make $name $hlist
339 set hlist [object parts]
340 push $hlist
341 foreach pair $icon_params {
342 set key [lindex $pair 0]
343 set value [lindex $pair 1]
344 switch -- $key {
345 origin -
346 orient {
347 # Do nothing for now. These are library instance values
348 # in xcircuit, and could be set as such.
350 default {
351 parameter make substring $key [list $value]
356 delete $hlist
358 eval "SCHEMATIC_${name}"
359 wm withdraw .select
360 schematic associate $name
361 zoom view
364 #------------------------------------------------------------
365 # Top-level routine: Find all the .sue files in the
366 # current directory and generate a library from them
367 #------------------------------------------------------------
369 proc make_all_sue {{name sue_gates}} {
370 set files [glob \*.sue]
371 foreach i $files {
372 set filename [file tail [file root $i]]
373 make_sue_gate $filename
374 page filename $name
375 page orientation 90
376 page encapsulation full
377 page fit true
378 if {[page scale] > 1.0} {
379 page fit false
380 page scale 1.0