Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/liblog.m4

raw
1 divert(-1)
2 liblog.m4 Logic gates
3
4 * Circuit_macros Version 11.0, copyright (c) 2026 J. D. Aplevich under *
5 * the LaTeX Project Public Licence in file Licence.txt. The files of *
6 * this distribution may be redistributed or modified provided that this *
7 * copyright notice is included and provided that modifications are clearly *
8 * marked to distinguish them from this distribution. There is no warranty *
9 * whatsoever for these files. *
10
11 define(`liblog_')
12
13 ifdef(`libgen_',,`include(libgen.m4)divert(-1)')
14 ifdef(`libcct_',,`include(libcct.m4)divert(-1)')
15
16 `Notes: ==================================================================
17 Gates other than BUFFER and NOT have an optional integer argument N
18 that sets the number of input locations, which then have labels In1
19 to InN.
20
21 BUFFER and NOT gates have In1 only. If there is a first argument, it
22 is a line specification and the gate is drawn along the line as for a
23 two-terminal element.
24
25 NEGATED inputs are obtained if the second argument is N (uppercase n).
26
27 DEBUGGING: The statement
28 print "`$0'($@)" ;
29 inserted into a macro will display the macro name and current arguments
30 ========================================================================='
31
32 `Nonzero parameter for logic size and grid mesh'
33 define(`L_unit',`(linewid/10)')
34
35 `Dimensions in L_units, also for external use:'
36 define(`G_hht',3) `gate half-height'
37 define(`AND_ht',`(2*G_hht)') `gate heights and widths ...'
38 define(`AND_wd',7)
39 define(`BUF_ht',4)
40 define(`BUF_wd',3.5)
41 define(`OR_rad',7) `OR input radius'
42 define(`XOR_off',1) `XOR and NXOR parameter'
43 define(`N_diam',(3/2)) `not-circle diameter'
44 define(`N_rad',`(N_diam/2)') `not-circle radius'
45 define(`NOT_rad',`(N_rad*L_unit)') `scaled radius eg line chop NOT_rad'
46 define(`N_attr') `not-circle attribs eg shaded "green"'
47 define(`H_ht',2) `Hysteresis symbol dimen'
48 define(`Mx_pins',6) `max number of gate input pins without extensions
49 Possibly 4 is better for negated inputs'
50 define(`FF_wid',12) `Bistable (flipflop)'
51 define(`FF_ht',18)
52 define(`Mux_wid',8) `Multiplexer defaults'
53 define(`Mux_ht',18)
54 define(`Lg_body') `Gate body attributes, e.g., shaded "color"'
55
56 define(`lg_plen',4) `Logic pin'
57 define(`lg_pinsep',`(3*L_unit)') `logic pin separation in drawing units'
58 define(`lg_chipwd',`(18*L_unit)') `default chip width'
59 define(`lg_pintxt',
60 `"ifxfig(`$1',`ifsvg(`svg_small(`$1',75)',`sp_{\scriptsize `$1'}sp_')')"')
61 `Logic pin text with bar where possible'
62 define(`lg_bartxt',`iflatex(`$\overline{\hbox{`$1'}}$',
63 `ifsvg(`svg_ol(`$1')',`$1')')')
64 `The comma has to be at the top level'
65 define(`m4_pattocomma',`patsubst(`$1',`$2',`,')')
66
67 `Scale grid coordinates to world coordinates'
68 define(`grid_',`(vscal_(L_unit,`$1',`$2'))')
69 `Scale and rotate grid coords to world coords'
70 define(`svec_',`vec_(vscal_(L_unit,`$1',`$2'))')
71 `Relative svec_'
72 define(`rsvec_',`Here+svec_(`$1',`$2')')
73
74 `NOT_circle
75 convenience for drawing NOT circles'
76 define(`NOT_circle',`circle diam N_diam*L_unit N_attr')
77
78 `LH_symbol([U|D|L|R|degrees][I],keys)
79 I=inverted
80 logical hysteresis symbol
81 keys: hght=expr; (H_ht)
82 wdth=fraction; (body wdth=frac*hght) '
83 define(`LH_symbol',`[ define(`m4LH',patsubst(`$1',I))dnl
84 pushkeys_(`$2',`hght:H_ht; wdth:0.6;')dnl
85 define(`m4Hs',ifinstr(`$1',I,-)H_ht)setdir_(m4LH,R)dnl
86 line to svec_(H_ht,0) \
87 then to svec_(1.1*H_ht,m4Hs)
88 line from rsvec_((1-m4wdth)*H_ht,0) \
89 to rsvec_(-m4wdth*H_ht,0) \
90 then to rsvec_(-(m4wdth+0.1)*H_ht,-(m4Hs))
91 `$2'; resetdir_ popdef(`m4hght',`m4wdth') ]')
92
93 `LT_symbol(U|D|L|R|degrees,keys)
94 triangle_symbol
95 keys: wdth=expr; (H_ht) '
96 define(`LT_symbol', `[ setdir_(`$1',R) pushkeys_(`$2',`wdth:H_ht')
97 line to svec_(0,m4wdth*5/8) then to svec_(m4wdth,0) \
98 then to svec_(0,-m4wdth*5/8) then to Here
99 `$2'; resetdir_ popdef(`m4wdth') ] ')
100
101 `BOX_gate(inputs,output,swid,sht,label,
102 attributes)
103 drawn in the current direction
104 args 3 and 4 are L_unit values
105 inputs=[P|N]* output=P|N'
106 define(`BOX_gate',`dnl
107 define(`m4m',`ifelse(`$1',,2,len(`$1'))')define(`m4a',`$1')dnl
108 define(`m4h',`ifelse(`$3',,AND_wd,`$3')')dnl
109 define(`m4v',`ifelse(`$4',,AND_wd,`$4')')dnl
110 [ Box: [Outline: line to svec_(0,m4v/2) then to svec_(m4h,m4v/2) \
111 then to svec_(m4h,-m4v/2) then to svec_(0,-m4v/2) then to (0,0) \
112 Lg_body `$6'] \
113 with .Outline.start at (0,0)
114 ifelse(`$5',,,`{ move to Box.n+(0,-5pt__)
115 m4lstring($5,"ifsvg(`svg_small($5,75)',`{\scriptsize$ $5 $}')") }')
116 IOdefs(from svec_(0,m4v/2) to svec_(0,-m4v/2),In,`$1',R)
117 Out: svec_(m4h,0) ifelse(`$2',N,`+svec_(N_diam,0)
118 N_Out: NOT_circle \
119 at Out-svec_(N_rad,0)'); `$7']')
120
121 `AND_gate(n,[N][B],[wid,[ht]],attributes)
122 drawn in the current direction
123 0 <= n <= 16; N=negated inputs, B=box shape
124 or
125 AND_gate(chars,[B],wid,ht,attributes)
126 arg1 is one or more of N|P '
127 define(`AND_gate',`define(`m4arg1',ifelse(`$1',,2,`$1'))dnl
128 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
129 `m4dupstr(ifinstr(`$2',N,N,P),m4arg1,`m4PN')',`define(`m4PN',m4arg1)')dnl
130 ifinstr(`$2',B,
131 `BOX_gate(m4PN,ifinstr(`$2',N,N,P),ifelse(`$3',,,`($3)/(L_unit)'),
132 ifelse(`$4',,,`($4)/(L_unit)'),ifsvg(`&```amp''';',`\&'),Lg_body `$5',`$6')',
133 `AND_gen(m4PN,ifelse(`$2',N,N)IBAONESEC,`$3',`$4',Lg_body `$5',`$6')')')
134
135 `NAND_gate(n,[N][B],[wid,[ht]],attributes)
136 0 <= n <= 16; N=negated inputs, B=box shape
137 or
138 NAND_gate(chars,[B],wid,ht)
139 arg1 is one or more of N|P '
140 define(`NAND_gate',`define(`m4arg1',ifelse(`$1',,2,`$1'))dnl
141 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
142 `m4dupstr(ifinstr(`$2',N,N,P),m4arg1,`m4PN')',`define(`m4PN',m4arg1)')dnl
143 ifinstr(`$2',B,
144 `BOX_gate(m4PN,ifelse(`$2',N,N,P),ifelse(`$3',,,`($3)/(L_unit)'),
145 ifelse(`$4',,,`($4)/(L_unit)'),ifsvg(`&```amp''';',`\&'),Lg_body `$5',`$6')',
146 `AND_gen(m4PN,ifelse(`$2',N,N)IBANONESEC,`$3',`$4',Lg_body `$5',`$6')')')
147
148 `AND_gen(n,chars,[wid,[ht]],attributes)
149 0 <= n <= 16
150 B=base and straight sides; A=Arc;
151 [N]NE,[N]SE,[N]I,[N]N,[N]S=inputs or circles,
152 [N]O=output; C=center location
153 or
154 AND_gen(chars,chars,[wid,[ht]],attributes)
155 arg1 is one or more of N|P
156 eg PPPP defines 4 inputs,
157 NPNP negates the first and third;
158 arg2 as above except [N]I is ignored
159 arg5 contains body attributes '
160 define(`AND_gen',`define(`m4arg1',ifelse(`$1',,2,`$1'))dnl
161 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
162 `m4dupstr(ifinstr(`$2',N,N,P),m4arg1,`m4PN')',`define(`m4PN',m4arg1)')dnl
163 define(`m4hor',`ifelse(`$3',,AND_wd,`($3)/(L_unit)')')dnl
164 define(`m4ver',`ifelse(`$4',,ifelse(`$3',,AND_ht,AND_ht/(AND_wd)*m4hor),
165 `($4)/(L_unit)')')define(`dna_',ifelse(`$2',,IBAONESEC,`$2'))dnl
166 [ sc_draw(`dna_',B,`Bm: line from svec_(m4hor-m4ver/2,-m4ver/2) \
167 to svec_(0,-m4ver/2) \
168 then to svec_(0,m4ver/2) then \
169 to svec_(m4hor-m4ver/2,m4ver/2) Lg_body `$5'
170 ')dnl
171 sc_draw(`dna_',A,`Arc: arc cw rad m4ver/2 \
172 to rsvec_(0,-m4ver) \
173 with .c at rsvec_(0,-m4ver/2) Lg_body `$5'
174 ')dnl
175 sc_draw(`dna_',NNE,
176 `NNE: svec_(m4hor-m4ver/2,0)+svec_(Rect_(m4ver/2+N_diam,45))
177 N_NNE: NOT_circle \
178 at svec_(m4hor-m4ver/2,0)+svec_(Rect_(m4ver/2+N_rad,45))
179 ')dnl
180 sc_draw(`dna_',NSE,
181 `NSE: svec_(m4hor-m4ver/2,0)+svec_(Rect_(m4ver/2+N_diam,-45))
182 N_NSE: NOT_circle \
183 at svec_(m4hor-m4ver/2,0)+svec_(Rect_(m4ver/2+N_rad,-45))
184 ')dnl
185 sc_draw(`dna_',NE, `NE: svec_(m4hor-m4ver/2,0)+svec_(Rect_(m4ver/2,45))
186 ')dnl
187 ifelse(ifinstr(`$1',N,1)`'ifinstr(`$1',P,1),,
188 `sc_draw(`dna_',NI, `m4A_defs(ifelse(`$1',,2,`$1'),N)')
189 ')dnl
190 sc_draw(`dna_',SE, `SE: svec_(m4hor-m4ver/2,0)+svec_(Rect_(m4ver/2,-45))
191 ')dnl
192 sc_draw(`dna_',NN, `N_NN: NOT_circle \
193 at svec_((m4hor-m4ver/2)/2,m4ver/2+N_rad)
194 NN: svec_((m4hor-m4ver/2)/2,m4ver/2+N_diam)
195 ')dnl
196 sc_draw(`dna_',NS, `N_NS: NOT_circle \
197 at svec_((m4hor-m4ver/2)/2,-m4ver/2-N_rad)
198 NS: svec_((m4hor-m4ver/2)/2,-m4ver/2-N_diam)
199 ')dnl
200 sc_draw(`dna_',NO, `N_Out: NOT_circle \
201 at svec_(m4hor+N_rad,0)
202 Out: svec_(m4hor+N_diam,0)
203 ')dnl
204 sc_draw(`dna_',O, `Out: svec_(m4hor,0)
205 ')dnl
206 sc_draw(`dna_',N, `N: svec_(0,m4ver/2)
207 ')dnl
208 ifelse(ifinstr(`$1',N,1)`'ifinstr(`$1',P,1),,
209 `sc_draw(`dna_',I, `m4A_defs(ifelse(`$1',,2,`$1'))')',
210 `m4A_defs(`$1')')
211 sc_draw(`dna_',S, `S: svec_(0,-m4ver/2)
212 ')dnl
213 sc_draw(`dna_',C, `C: svec_(m4hor/2,0)')
214 `$6']')
215
216 `Input locations, flat face
217 m4A_defs(n,[N]) or m4A_defs(string)
218 string = one or more N|P
219 otherwise n inputs (negated if arg2 is N)
220 Input points are defined as Inx
221 and NOT circles as N_Inx where x is integer'
222 `What about the IOdefs macro?'
223 define(`m4A_defs',`define(`m4arg1',ifelse(`$1',,2,`$1'))dnl
224 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
225 `m4dupstr(ifinstr(`$2',N,N,P),m4arg1,`m4AI')define(`m4AnI',m4arg1)',
226 `define(`m4AI',m4arg1)define(`m4AnI',len(m4arg1))')dnl
227 define(`m4m',`m4ver/2/min(m4AnI,Mx_pins-1)*min(m4AnI,3*(Mx_pins-1))')dnl
228 ifelse(eval(m4AnI>Mx_pins),1,
229 `line from svec_(0, m4m) \
230 to svec_(0,m4ver/2)
231 line from svec_(0,-m4m) \
232 to svec_(0,-m4ver/2)')
233 for_(1,m4AnI,1,`ifelse(substr(m4AI,0,1),N,
234 `N_In`'m4x: NOT_circle at \
235 svec_(-N_rad,m4ver/min(m4AnI,Mx_pins-1)*((m4AnI+1)/2-m4x))
236 In`'m4x: svec_(-N_diam,m4ver/min(m4AnI,Mx_pins-1)*((m4AnI+1)/2-m4x)) ',
237 `In`'m4x: svec_(0,m4ver/min(m4AnI,Mx_pins-1)*((m4AnI+1)/2-m4x))')dnl
238 define(`m4AI',substr(m4AI,1)) ') ')
239
240 `OR_gate or NOR_gate or XOR_gate or NXOR_gate
241 drawn in the current direction
242 args = (n,[N][B],wid,ht,attributes)
243 0 <= n <= 16; N=negated inputs, B=box shape
244 or
245 args = (chars,[B],wid,ht,attributes)
246 arg1 is one or more of N|P '
247 define(`OR_gate',`m4_OR(OR,$@)')
248 define(`NOR_gate',`m4_OR(NOR,$@)')
249 define(`XOR_gate',`m4_OR(XOR,$@)')
250 define(`NXOR_gate',`m4_OR(NXOR,$@)')
251
252 define(`m4_OR',`define(`m4arg1',ifelse(`$2',,2,`$2'))dnl
253 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
254 `m4dupstr(ifinstr(`$3',N,N,P),m4arg1,`m4PN')',`define(`m4PN',m4arg1)')dnl
255 ifinstr(`$3',B,
256 `BOX_gate(m4PN,ifinstr(`$3',N,N,P,`$6',`$7'),
257 ifelse(`$4',,,`($4)/(L_unit)'),ifelse(`$5',,,`($5)/(L_unit)'),
258 ifinstr(`$1',XOR,`=',`ifsvg(`>=1',`\geq 1')'),Lg_body `$6',`$7',`$8')',
259 `OR_gen(m4PN,
260 ifinstr(`$1',XOR,P)`'ifelse(`$3',N,N)IBA`'ifelse(substr(`$1',0,1),N,N)ONESEC,
261 shift(shift(shift($@))))')')
262
263 `OR_gen(n,chars,[wid,[ht]],attributes)
264 0 <= n <= 16
265 B=base and straight sides; A=Arc;
266 [N]NE,[N]SE,[N]I,[N]N,[N]S=inputs or circles,
267 [N]P=XOR arc; [N]O=output; C=center
268 or
269 OR_gen(chars,chars,[wid,[ht]],attributes)
270 arg1 is one or more of N|P
271 eg PPPP defines 4 inputs,
272 NPNP negates the first and third;
273 arg2 as above except [N]I is ignored
274 If arg5 contains shaded rgbstring(...)
275 the arguments of rgbstring may not contain
276 parentheses'
277 define(`OR_gen',`[define(`m4arg1',ifelse(`$1',,2,`$1'))dnl
278 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
279 `m4dupstr(ifinstr(`$2',N,N,P),m4arg1,`m4PN')',`define(`m4PN',m4arg1)')dnl
280 define(`m4hor',`ifelse(`$3',,AND_wd,`($3)/(L_unit)')')define(`m4o',0)dnl
281 define(`m4ver',`ifelse(`$4',,ifelse(`$3',,AND_ht,AND_ht/(AND_wd)*m4hor),
282 `($4)/(L_unit)')')define(`dna_',ifelse(`$2',,IBAONESEC,`$2'))dnl
283 define(`m4gfill',`Lg_body `$5'')dnl
284 dnl concave fill, painful code:
285 ifelse(regexp(m4gfill,`^fill \|[^a-z]fill '),-1,
286 `ifelse(regexp(m4gfill,`shaded *"'),-1,
287 `define(`m4osh',`regexp(m4gfill,`.*shaded *\(sprintf([^)]*)\).*$',\1)')
288 define(`m4att',`patsubst(m4gfill,`shaded *sprintf([^)]*)')')',
289 `define(`m4osh',`regexp(m4gfill,`.*shaded *"\([^"]*\)".*$',"\1")')
290 define(`m4att',`patsubst(m4gfill,`shaded *"[^"]*"')')')',
291 `define(`m4osh',`regexp(m4gfill,`.*fill *\([^ ]*\) .*$',`graystring(\1)')')
292 define(`m4att',`patsubst(m4gfill,`fill *[^ ]*[ $]')')')
293 sc_draw(`dna_',P,`define(`m4o',XOR_off*m4ver/(AND_ht))dnl
294 Parc: arc cw from svec_(0,m4ver/2) \
295 to svec_(0,-m4ver/2) \
296 with .c at svec_(-sqrt((OR_rad*m4ver/(AND_ht))^2-(m4ver/2)^2),0)
297 ')dnl
298 sc_draw(`dna_',B,dnl
299 `r = OR_rad*m4ver/(AND_ht)
300 rt = sqrt(r^2-(m4ver/2)^2)
301 ifelse(m4osh,,,`for i=0 to int((r-rt)*L_unit/lthick+1) do {
302 tx = i*lthick/L_unit
303 {arc cw from svec_(tx+m4o-hlth,m4ver/2) to svec_(tx+m4o-hlth,-m4ver/2) \
304 with .c at svec_(tx+m4o-rt,0) outlined m4osh} }
305 {rotbox(m4hor/3*L_unit,m4ver*L_unit,shaded m4osh invis) with .W \
306 at svec_(m4o+r-rt,0)} ')
307 Bt: line from svec_(m4o+m4hor/3,m4ver/2) \
308 to svec_(m4o,m4ver/2) ifdpic(chop 0 chop -hlth) m4att
309 ArcB: arc cw to svec_(m4o,-m4ver/2) with .c at svec_(m4o-rt,0) m4att
310 Bb: line to svec_(m4o+m4hor/3,-m4ver/2) ifdpic(chop -hlth chop 0) m4att
311 ')dnl
312 sc_draw(`dna_',A,`define(`m4m',`((m4hor*2/3)^2-(m4ver/2)^2)/(m4ver)')dnl
313 ifelse(m4osh,,,`{line invis from svec_(m4o+m4hor,0) \
314 to svec_(m4o+m4hor/3, m4ver/2) then to svec_(m4o+m4hor/3,-m4ver/2) \
315 then to svec_(m4o+m4hor,0) shaded m4osh}')
316 ArcN: arc cw from svec_(m4o+m4hor/3, m4ver/2) \
317 to svec_(m4o+m4hor,0) with .c at svec_(m4o+m4hor/3,-m4m) m4gfill
318 ArcS: arc ccw from svec_(m4o+m4hor/3,-m4ver/2) \
319 to svec_(m4o+m4hor,0) with .c at svec_(m4o+m4hor/3,m4m) m4gfill
320 ')dnl
321 sc_draw(`dna_',NNE,
322 `N_NNE: NOT_circle \
323 at svec_(m4o+m4hor/3,-m4m)+svec_(Rect_(m4ver/2+m4m+N_rad,60))
324 NNE: svec_(m4o+m4hor/3,-m4m)+svec_(Rect_(m4ver/2+m4m+N_diam,60))
325 ')dnl
326 sc_draw(`dna_',NSE,
327 `N_NSE: NOT_circle \
328 at svec_(m4o+m4hor/3,m4m)+svec_(Rect_(m4ver/2+m4m+N_rad,-60))
329 NSE: svec_(m4o+m4hor/3,m4m)+svec_(Rect_(m4ver/2+m4m+N_diam,-60))
330 ')dnl
331 sc_draw(`dna_',NE, `NE: svec_(m4o+m4hor/3,-m4m)+svec_(Rect_(m4ver/2+m4m,60))
332 ')dnl
333 sc_draw(`dna_',SE, `SE: svec_(m4o+m4hor/3,m4m)+svec_(Rect_(m4ver/2+m4m,-60))
334 ')dnl
335 ifelse(ifinstr(`$1',N,1)`'ifinstr(`$1',P,1),,
336 `sc_draw(`dna_',NI, `m4O_defs(ifelse(`$1',,2,`$1'),N)')
337 ')dnl
338 sc_draw(`dna_',NN, `N_NN: NOT_circle \
339 at svec_(m4o+m4hor/6,m4ver/2+N_rad)
340 NN: svec_(m4o+m4hor/6,m4ver/2+N_diam)
341 ')dnl
342 sc_draw(`dna_',NS, `N_NS: NOT_circle \
343 at svec_(m4o+m4hor/6,-m4ver/2-N_rad)
344 NS: svec_(m4o+m4hor/6,-m4ver/2-N_diam)
345 ')dnl
346 sc_draw(`dna_',NO, `N_Out: NOT_circle \
347 at svec_(m4o+m4hor+N_rad,0)
348 Out: svec_(m4o+m4hor+N_diam,0)
349 ')dnl
350 sc_draw(`dna_',O, `Out: svec_(m4o+m4hor,0)
351 ')dnl
352 sc_draw(`dna_',N, `N: svec_(m4o+m4hor/6,m4ver/2)
353 ')dnl
354 ifelse(ifinstr(`$1',N,1)`'ifinstr(`$1',P,1),,
355 `sc_draw(`dna_',I, `m4O_defs(ifelse(`$1',,2,`$1'))')',
356 `m4O_defs(`$1')')
357 sc_draw(`dna_',S, `S: svec_(m4o+m4hor/6,-m4ver/2)
358 ')dnl
359 sc_draw(`dna_',C, `C: svec_(m4o+m4hor/2,0)')
360 `$6']')
361
362 `Input locations, curved face
363 m4O_defs(n,[N]) or m4O_defs(string)
364 string = one or more N|P
365 otherwise n inputs (negated if arg2 is N)
366 Input points are defined as Inx
367 and NOT circles as N_Inx where x is integer'
368 define(`m4O_defs',`define(`m4arg1',ifelse(`$1',,2,`$1'))dnl
369 ifelse(ifinstr(m4arg1,N,1)`'ifinstr(m4arg1,P,1),,
370 `m4dupstr(ifinstr(`$2',N,N,P),m4arg1,`m4OI')define(`m4OnI',m4arg1)',
371 `define(`m4OI',m4arg1)define(`m4OnI',len(m4arg1))')dnl
372 define(`m4om',`m4ver/2/min(m4OnI,Mx_pins-1)*min(m4OnI,3*(Mx_pins-1))')dnl
373 ifelse(eval(m4OnI>Mx_pins),1,
374 `arc ccw from svec_(0,m4ver/2) \
375 to svec_(0,m4ver) + M4O_pos(m4om-m4ver) \
376 with .c at svec_(-M4O_dst,m4ver)
377 arc cw from svec_(0,-m4ver/2) \
378 to svec_(0,-m4ver) +M4O_pos(-(m4om-m4ver))\
379 with .c at svec_(-M4O_dst,-m4ver)
380 ')dnl
381 define(`m4on',`(eval((m4OnI-Mx_pins+1)/2))')dnl
382 for_(1,m4OnI,1,
383 `define(`m4oq',`m4ver/2/min(m4OnI,Mx_pins-1)*(m4OnI+1-2*m4x)')dnl
384 In`'m4x: ifelse(eval(m4x<=m4on),1,`svec_(0, m4ver)+M4O_pos(m4oq-m4ver)',
385 eval(m4x>(m4OnI-m4on)),1, `svec_(0,-m4ver)+M4O_pos(m4oq+m4ver)',
386 `M4O_pos(m4oq)')
387 ifelse(substr(m4OI,0,1),N,
388 `N_In`'m4x: NOT_circle \
389 at In`'m4x+svec_(-N_rad,0)
390 In`'m4x: In`'m4x+svec_(-N_diam,0)
391 ')dnl
392 define(`m4OI',substr(m4OI,1))dnl
393 ')
394 ')
395 define(`M4O_dst',`sqrt((OR_rad*m4ver/(AND_ht))^2-(m4ver/2)^2)')
396 define(`M4O_pos',`svec_(-M4O_dst+sqrt((OR_rad*m4ver/(AND_ht))^2-(`$1')^2),
397 `$1')')
398
399 `IOdefs(linespec,label,[P|N]*,L|R)
400 Distribute named locations with optional NOT
401 circles along a line
402 eg IOdefs(up 1,Z,PNN,R) defines Z1 at 1/6
403 along the line, NOT circles N_Z2 and N_Z3 to
404 the right at 1/2 and 5/6 along the line with
405 Z2 and Z3 labeled at their right edges'
406 define(`IOdefs',`define(`m4dm',`ifelse(`$3',,1,len(`$3'))')
407 define(`m4da',`$3')define(`m4dv',`ifelse(`$2',,In,`$2')')
408 M4TL: move `$1'
409 M4PL: vperp(M4TL,L_unit)
410 for_(1,m4dm,1,
411 `m4dv`'m4x: ((m4x-1/2)/m4dm between M4TL.start and M4TL.end) dnl
412 ifelse(substr(m4da,0,1),N,`\
413 ifelse(`$4',R,-,+)(M4PL.x*N_diam,M4PL.y*N_diam)
414 {N_`'m4dv`'m4x: NOT_circle \
415 at m4dv`'m4x ifelse(`$4',R,+,-)(M4PL.x*N_rad,M4PL.y*N_rad)}')
416 define(`m4da',substr(m4da,1))') ')
417
418 `BUFFER_gen(chars, wd, ht,
419 [N|P]*, [N|P]*, [N|P]*, attributes)
420 chars: T=triangle (default),
421 [N]O=output location Out (NO draws circle
422 N_Out);
423 [N]I,[N]N,[N]S,[N]NE,[N]SE input locations
424 C=centre location. Args 4-6 define In, NE,
425 and SE argument sequences'
426 define(`BUFFER_gen',
427 `define(`m4h',`ifelse(`$2',,BUF_wd,`($2)/(L_unit)')')define(`m4y',m4h)dnl
428 define(`m4v',`ifelse(`$3',,BUF_ht,`($3)/(L_unit)')')dnl
429 define(`dna_',ifelse(`$1',,ITOCNESE,`$1'))dnl
430 define(`m4z',`N_rad/vlength(m4h,m4v/2)')dnl
431 [sc_draw(`dna_',T,`Tm: line from svec_(m4h,0) \
432 to svec_(0,-m4v/2) \
433 then to svec_(0,m4v/2) \
434 then to svec_(m4h,0) Lg_body `$7'
435 Tc: svec_(m4h/2,0)
436 ')dnl
437 sc_draw(`dna_',NNE,`N_NNE: NOT_circle \
438 at svec_(m4h/2,m4v/4)+svec_(m4v/2*m4z,m4h*m4z)
439 NNE: svec_(m4h/2,m4v/4)+svec_(m4v*m4z,m4h*2*m4z)
440 ')dnl
441 sc_draw(`dna_',NSE,`N_NSE: NOT_circle \
442 at svec_(m4h/2,-m4v/4)-svec_(-m4v/2*m4z,m4h*m4z)
443 NSE: svec_(m4h/2,-m4v/4)-svec_(-m4v*m4z,m4h*2*m4z)
444 ')dnl
445 sc_draw(`dna_',NI,`define(`m4y',m4y+N_diam) In1: svec_(-N_diam,0)
446 N_In1: NOT_circle \
447 at svec_(-N_rad,0)
448 ')dnl
449 sc_draw(`dna_',NE, `NE: svec_(m4h/2,m4v/4)
450 ')dnl
451 sc_draw(`dna_',SE, `SE: svec_(m4h/2,-m4v/4)
452 ')dnl
453 sc_draw(`dna_',NN, `N_NN: NOT_circle \
454 at svec_(0,m4v/2+N_rad)
455 NN: svec_(0,m4v/2+N_diam)
456 ')dnl
457 sc_draw(`dna_',NS, `N_NS: NOT_circle \
458 at svec_(0,-m4v/2-N_rad)
459 NS: svec_(0,-m4v/2-N_diam)
460 ')dnl
461 sc_draw(`dna_',NO,`define(`m4y',m4y+N_diam) Out: svec_(m4h+N_diam,0)
462 N_Out: NOT_circle \
463 at svec_(m4h+N_rad,0)
464 ')dnl
465 sc_draw(`dna_',N, `N: svec_(0,m4v/2)
466 ')dnl
467 sc_draw(`dna_',S, `S: svec_(0,-m4v/2)
468 ')dnl
469 sc_draw(`dna_',O, `Out: svec_(m4h,0)
470 ')dnl
471 sc_draw(`dna_',I, `In1: (0,0)
472 ')dnl
473 sc_draw(`dna_',C, `C: svec_(m4h/3,0)
474 ')dnl
475 ifelse(`$4',,,`IOdefs(from svec_(0,m4v/2) to svec_(0,-m4v/2),In,`$4',R)')
476 ifelse(`$5',,,`IOdefs(from svec_(0,m4v/2) to svec_(m4h,0),NE,`$5')')
477 ifelse(`$6',,,`IOdefs(from svec_(0,-m4v/2) to svec_(m4h,0),SE,`$6',R)')
478 `$8']')
479
480 `BUFFER_gate(linespec, [N|B], wid, ht,
481 [N|P]*, [N|P]*, attributes)
482 When linespec is blank then the element is
483 composite and In1, Out, C, NE, and SE are
484 defined; otherwise the element is drawn as
485 two-terminal
486 arg2: B=box gate'
487 Args 5 and 6 define NE and SE argument
488 sequences'
489 define(`BUFFER_gate',`ifinstr(`$2',B,
490 `BOX_gate(ifinstr(`$2',N,N,P),P,ifelse(`$3',,,`($3)/(L_unit)'),
491 ifelse(`$4',,,`($4)/(L_unit)'),1,`$5',Lg_body `$7')',
492 `ifelse(`$1',,
493 `BUFFER_gen(ifelse(`$2',N,N)ITOCNESE,`$3',`$4',,`$5',`$6',
494 Lg_body `$7',`$8')',
495 `eleminit_(`$1')
496 { BUFFER_gen(ifelse(`$2',N,N)ITOC,`$3',`$4',,`$5',`$6',Lg_body `$7',`$8') \
497 with .Tc at last line.c }
498 { line to last [].In1; line from last [].Out to 2nd last line.end }
499 line invis to rvec_(rp_len,0)')')')
500
501 `NOT_gate(linespec, [B][N|n], wid, ht,
502 attributes)
503 When linespec is blank then the element is
504 composite and In1, Out, C, NE, and SE are
505 defined; otherwise the element is drawn as
506 two-terminal
507 arg2: B=box gate, N=negated input and
508 output, n=negated input only'
509 define(`NOT_gate',`ifinstr(`$2',B,
510 `BOX_gate(ifinstr(`$2',N,N,`$2',n,N,P),ifinstr(`$2',n,P,N),
511 ifelse(`$3',,,`($3)/(L_unit)'),ifelse(`$4',,,`($4)/(L_unit)'),1,
512 Lg_body `$5',`$6')',
513 `ifelse(`$1',,
514 `BUFFER_gen(ifinstr(`$2',N,N,`$2',n,N)IT`'ifinstr(`$2',n,,N)OCNESE,
515 `$3',`$4',,,,Lg_body `$5',`$6')',
516 `eleminit_(`$1')
517 { BUFFER_gen(ifinstr(`$2',N,N,`$2',n,N)IT`'ifinstr(`$2',n,,N)OC,
518 `$3',`$4',,,,Lg_body `$5',`$6') \
519 with .C at last line.c }
520 { line to last [].In1; line from last [].Out to 2nd last line.end }
521 line invis to rvec_(rp_len,0)')')')
522
523 ###############################
524 `The comprehensive logic pin:
525 lg_pin(location, label, Picname, n|e|s|w [L|M|I|O][N][E], pinno, optlen)
526 label=text (indicating logical pin function, usually)
527 Picname=pic label for referring to the pin
528 n|e|s|w=orientation (north, east, south, west)
529 L=active low out; M=active low in; I=inward arrow; O=outward arrow
530 N=negated (NOT-circle); E=edge trigger'
531 define(`lg_pin',`ifelse(`$1',,,`move to $1')
532 define(`dna_',`substr(`$4',1)')define(`m4lE',)define(`m4lch',0)dnl
533 define(`m4ld',`ifelse(`$4',,e,`substr(`$4',0,1)')')dnl
534 define(`m4lph',`ifelse(m4ld,n,0,m4ld,w,-1,m4ld,s,0,1)')dnl
535 define(`m4lpv',`ifelse(m4ld,n,1,m4ld,w,0,m4ld,s,-1,0)')dnl
536 define(`m4lpl',`ifelse(`$6',,`lg_plen',(`$6')/L_unit)')dnl
537 sc_draw(`dna_',E,`define(`m4lE',1)dnl
538 { line from rsvec_(lp_xy(0,N_rad)) \
539 to rsvec_(lp_xy(-N_diam*sqrt(3)/2,0)) then to rsvec_(lp_xy(0,-N_rad)) }')
540 ifelse(`$2',,,
541 `{ lg_pintxt(`$2') ifelse(m4ld,w,`ljust_', m4ld,n,`below_',
542 m4ld,s,`above_',`rjust_') at Here dnl
543 ifxfig(`+(lp_xy(-0.72bp__,0))') dnl
544 ifelse(m4lE,1,`+svec_(lp_xy(-N_diam*sqrt(3)/2,0))') }')
545 sc_draw(`dna_',N,`define(`m4lch',N_diam*L_unit)
546 { NOT_circle \
547 at rsvec_(lp_xy(N_rad,0)) }')
548 sc_draw(`dna_',L,`define(`m4lch',N_rad*2.5*L_unit)
549 {line from rsvec_(lp_xy(0,
550 ifelse(m4ld,w,-,m4ld,s,-)N_rad*3/2)) to rsvec_(lp_xy(N_rad*2.5,0)) \
551 then to Here }')
552 sc_draw(`dna_',M,`define(`m4lch',N_rad*2.5*L_unit)
553 { line to rsvec_(lp_xy(N_rad*2.5,
554 ifelse(m4ld,w,-,m4ld,s,-)N_rad*3/2)) then to rsvec_(lp_xy(N_rad*2.5,0)) \
555 then to Here}')
556 {ifelse(`$3',,,`$3':) line to rsvec_(lp_xy(m4lpl,0)) chop m4lch chop 0 dnl
557 ifinstr(dna_,I,` <- wid linethick*5.6bp__ ht linethick*7.2bp__ ')dnl
558 ifinstr(dna_,O,` -> wid linethick*5.6bp__ ht linethick*7.2bp__ ')
559 ifelse(`$5',,,`move to last line.c; lg_pintxt(`$5') dnl
560 ifelse(m4ld,n,`rjust_', m4ld,w,`above_',m4ld,e,`above_',`rjust_')')
561 } ')
562 define(`lp_xy',`vrot_(`$1',`$2',m4lph,m4lpv)')
563 `Mux(ni, label,
564 [L][B|H|X][N[n]|S[n]][[N]OE],
565 wid, ht, attributes)
566 ni = number of inputs (default 2)
567 The input pins are lines named
568 In0, In1, ... In<ni-1>
569 chars:
570 L reverses pin numbering,
571 B pin number labels in binary form,
572 H pin number labels in hex form,
573 X disable pin labels
574 N[n] puts pin Sel or pins Sel0 .. Seln
575 at the top (with respect to the drawing
576 direction)
577 S[n] puts the Sel inputs at the
578 bottom (default)
579 OE adds the OE input, NOE negated '
580 define(`Mux',`[
581 m4Mwid = ifelse(`$4',,Mux_wid,((`$4')/(L_unit)))
582 m4Mht = ifelse(`$5',,Mux_ht,((`$5')/(L_unit)))
583 W: (0,0)
584 C: svec_(m4Mwid/2,0)
585 E: svec_(m4Mwid,0)
586 NW: svec_(0,m4Mht/2)
587 SW: svec_(0,-m4Mht/2)
588 NE: svec_(m4Mwid,m4Mht/4)
589 SE: svec_(m4Mwid,-m4Mht/4)
590 Line: line from W to NW then to NE then to SE then to SW then to W \
591 Lg_body `$6'
592 ifelse(`$2',,,`"ifsvg(`svg_small($2,75)',`\scriptsize $2')" at C')
593 lg_pin(E,,Out,e)
594 define(`m4Mdna',`$3')define(`m4MOE')dnl
595 sc_draw(`m4Mdna',L,`define(`m4ML',-)',`define(`m4ML')')dnl
596 define(`m4MN')define(`m4MSel',S)dnl
597 sc_draw(`m4Mdna',B,`define(`m4MN',B)')dnl
598 sc_draw(`m4Mdna',H,`define(`m4MN',H)')dnl
599 sc_draw(`m4Mdna',X,`define(`m4MN',X)')dnl
600 sc_draw(`m4Mdna',NOE,`define(`m4MOE',N)')dnl
601 sc_draw(`m4Mdna',OE,`define(`m4MOE',O)')dnl
602 sc_draw(`m4Mdna',N,`define(`m4MSel',N)')dnl
603 sc_draw(`m4Mdna',S,`define(`m4MSel',S)')dnl
604 define(`m4Mn',`ifelse(`$1',,2,`ifelse(m4MOE,,`$1',incr(eval(`$1')))')')dnl
605 ifinstr(m4MSel,N, dnl Draw Sel pins
606 `ifelse(m4Mdna,,`lg_pin((0.5 between NW and NE),,Sel,n)',`for_(1,m4Mdna,1,`
607 lg_pin((m4x-0.5)/m4Mdna between NW and NE,,Sel`'eval(m4x-1),n)')')',
608 `ifelse(m4Mdna,,`lg_pin((0.5 between SW and SE),,Sel,s)',`for_(1,m4Mdna,1,`
609 lg_pin((m4x-0.5)/m4Mdna between SW and SE,,Sel`'eval(m4x-1),s)')')')
610 for_(1,m4Mn,1,`lg_pin( dnl Draw In pins
611 svec_(0,m4ML`'m4Mht*(0.5+(0.5-m4x)/m4Mn)),
612 define(`m4MNO',`ifelse(m4x,m4Mn,ifelse(m4MOE,,,N))')dnl
613 ifelse(m4MNO,,
614 `ifelse(m4MN,X,,
615 m4MN,B,`define(`m4dg',`ifelse(eval(`$1'>8),1,4,eval(`$1'>4),1,3,
616 eval(`$1'>2),1,2,1)') binary_(decr(m4x),m4dg)',
617 m4MN,H,`define(`m4dg',`ifelse(eval(`$1'>16),1,2,1)')dnl
618 hexadecimal_(decr(m4x),m4dg)',
619 decr(m4x))',
620 `ifelse(m4MN,X,,`ifelse(m4MOE,N,lg_bartxt(OE),OE)')'),
621 ifelse(m4MNO,,In`'decr(m4x),ifelse(m4MOE,N,N)OE),
622 w`'ifelse(m4MNO,,,ifelse(m4MOE,N,N)))')
623 `$7']')
624
625 `Demux(no, label,
626 [L][B|H|X][N[n]|S[n]][[N]OE],
627 wid, ht, attributes)
628 no = number of outputs (default 2)
629 The output pins are lines named
630 Out0, Out1, ... Out<ni-1>
631 chars:
632 L reverses pin numbering,
633 B displays binary pin numbers,
634 H displays hexadecimal pin numbers,
635 X disable pin labels
636 N[n] puts Sel or Sel0 .. Seln at the top
637 (with respect to the drawing direction)
638 S[n] puts the Sel inputs at the
639 bottom (default)
640 OE adds the OE input, NOE negated '
641 define(`Demux',`[
642 m4Dwid = ifelse(`$4',,Mux_wid,((`$4')/(L_unit)))
643 m4Dht = ifelse(`$5',,Mux_ht,((`$5')/(L_unit)))
644 W: (0,0)
645 C: svec_(m4Dwid/2,0)
646 E: svec_(m4Dwid,0)
647 NW: svec_(0,m4Dht/4)
648 SW: svec_(0,-m4Dht/4)
649 NE: svec_(m4Dwid,m4Dht/2)
650 SE: svec_(m4Dwid,-m4Dht/2)
651 Line: line from W to NW then to NE then to SE then to SW then to W \
652 Lg_body `$6'
653 ifelse(`$2',,,`"ifsvg(`svg_small($2,75)',`\scriptsize $2')" at C')
654 lg_pin(W,,In,w)
655 define(`m4Ddna',`$3')define(`m4DOE')dnl
656 sc_draw(`m4Ddna',L,`define(`m4DL',-)',`define(`m4DL')')dnl
657 define(`m4DN')define(`m4DSel',S)dnl
658 sc_draw(`m4Ddna',B,`define(`m4DN',B)')dnl
659 sc_draw(`m4Ddna',H,`define(`m4DN',H)')dnl
660 sc_draw(`m4Ddna',X,`define(`m4DN',X)')dnl
661 sc_draw(`m4Ddna',NOE,`define(`m4DOE',N)')dnl
662 sc_draw(`m4Ddna',OE,`define(`m4DOE',O)')dnl
663 sc_draw(`m4Ddna',N,`define(`m4DSel',N)')dnl
664 sc_draw(`m4Ddna',S,`define(`m4DSel',S)')dnl
665 define(`m4Dn',`ifelse(`$1',,2,`$1')')dnl
666 ifinstr(m4DSel,N,
667 `ifelse(m4Ddna,,`lg_pin((0.5 between NW and NE),,Sel,n)',`for_(1,m4Ddna,1,`
668 lg_pin((m4x-0.5)/m4Ddna between NW and NE,,Sel`'eval(m4x-1),n)')')',
669 `ifelse(m4Ddna,,`lg_pin((0.5 between SW and SE),,Sel,s)',`for_(1,m4Ddna,1,`
670 lg_pin((m4x-0.5)/m4Ddna between SW and SE,,Sel`'eval(m4x-1),s)')')')
671 for_(1,m4Dn,1,`lg_pin(
672 svec_(m4Dwid,m4DL`'m4Dht*(0.5+(0.5-m4x)/m4Dn)),
673 ifelse(m4DN,X,,
674 m4DN,B,`define(`m4dg',`ifelse(eval(`$1'>8),1,4,eval(`$1'>4),1,3,
675 eval(`$1'>2),1,2,1)') binary_(decr(m4x),m4dg)',
676 m4DN,H,`define(`m4dg',`ifelse(eval(`$1'>16),1,2,1)')dnl
677 hexadecimal_(decr(m4x),m4dg)',
678 decr(m4x)),
679 Out`'decr(m4x),
680 e)')
681 ifelse(m4DOE,,,`ifelse(m4DOE,N,
682 `lg_pin(svec_(0,m4Dht*(0.5+(1-m4Dn)/m4Dn)),lg_bartxt(OE),NOE,wN)',
683 `lg_pin(svec_(0,m4Dht*(0.5+(1-m4Dn)/m4Dn)),OE,OE,w)')')
684 `$7']')
685
686 ###########################################################################
687 AutoGate allowable functions (plus the ~ operator)
688 define(`And',`_AutoGate(AND,$@)')
689 define(`Or',`_AutoGate(OR,$@)')
690 define(`Not',`_AutoGate(NOT,$@)')
691 define(`NNot',`_AutoGate(NNOT,$@)')
692 define(`Buffer',`_AutoGate(BUFFER,$@)')
693 define(`Xor',`_AutoGate(XOR,$@)')
694 define(`Nand',`_AutoGate(NAND,$@)')
695 define(`Nor',`_AutoGate(NOR,$@)')
696 define(`Nxor',`_AutoGate(NXOR,$@)')
697 dnl Custom subcircuit template:
698 dnl define(`Mygate',`_AutoGate(My,$@)')
699 dnl define(`My_gate',`[ ...
700 dnl Out: ...
701 dnl In1: ...
702 dnl In2: ... ]')
703 dnl Not gate with negated input for NNOT above
704 define(`NNOT_gate',`BUFFER_gate(,N)')
705
706 Style parameters
707 define(`gatelineth',linethick) #define(`gatelineth',1.0) use absolute pt
708 define(`lineth',linethick) #define(`lineth',0.5)
709 define(`autoinputsep',`(BUF_ht*L_unit*5/4)') # distance between inputs
710 define(`autovsep',`L_unit') # vertical separation between input gates
711
712 ##########
713
714 Draw the gate with input sublayer Sg containing
715 gates G1, G2, ...
716 Example: `AutoGate(AND,$@)'
717 define(`AutoGate',`[
718 lu = L_unit define(`m4dirt',m4_dir_)
719 dnl Count the arguments (inputs) (could use $# )
720 pushdef(`m4nargs',0)dnl
721 Loopover_(`arg',`define(`m4nargs',incr(m4nargs))',shift($@))dnl
722 ifinstr(`$2',[,`define(`m4nargs',decr(m4nargs))dnl
723 define(`m4PN',substr(`$2',1,eval(len(`$2')-2)))')
724 `#' m4Delch(`$1') gate(m4nargs)
725 # Draw the gate
726 linethick = gatelineth
727 ifelse(m4Delch(`$1'),NOT,`Q: NOT_gate()',
728 m4Delch(`$1'),BUFFER,`Q: BUFFER_gate()',
729 `Q: m4Delch(`$1')_gate(ifinstr(`$2',[,m4PN,m4nargs))') `#' End Q
730 linethick = lineth
731 ifelse(substr(m4Delch(`$1'),0,1),N,
732 `Out: Q.Out',
733 `line thick lineth from Q.Out m4dirt N_diam*L_unit; Out: Here')
734 pushdef(`AutoOutNames',m4Path.Out)
735 T: ifelse(m4dirt,right,`Q.w-(2*lu,0)',`Q.e+(2*lu,0)')
736 # Sublayer Sg containing gates or vars G1, G2,...
737 # with output vertical median at the height of Q.c
738 Sg: [ pushdef(`m4_nct',0)dnl
739 pushdef(`m4Path',m4Path.Sg)dnl
740 Loopover_(`arg',
741 `define(`m4_nct',incr(m4_nct))dnl
742 dnl Variable or a sublayer gate
743 dnl Inputs are labelled In<var>_N or In<var>_X
744 dnl Remove initial white space; detect a name or gate
745 pushdef(`m4Path',m4Path.G`'m4_nct)dnl
746 m4ifboolvar_(arg,
747 `G`'m4_nct: [dnl
748 ifelse(substr(arg,0,1),~,
749 `define(`m4xg',substr(arg,1))dnl
750 pushdef(`m4InNames',m4Path.In`'m4xg`'_N)define(N_`'m4xg)dnl
751 In`'m4xg`'_N: Here',
752 `pushdef(`m4InNames',m4Path.In`'arg`'_X)define(X_`'arg)dnl
753 In`'arg`'_X: Here ')
754 Out: Here ] ht \
755 ifelse(m4nargs,1,`2*autovsep',`abs(Q.In1.y-Q.In2.y)-autovsep')',
756 `pushdef(`AutoOutNames',m4Path.Out)dnl
757 G`'m4_nct: m4Delch(arg)') ifelse(m4_nct,1,,`ifelse(m4dirt,right, \
758 `with .ne at last [].se',`with .nw at last [].sw')+(0,-autovsep)')
759 popdef(`m4Path') ',
760 ifinstr(`$2',[,`shift(shift($@))',`shift($@)'))
761 MidOut: 0.5 between G`'eval((m4_nct+1)/2).Out and G`'eval((m4_nct+2)/2).Out
762 popdef(`m4_nct')dnl
763 popdef(`m4Path')dnl
764 ] with .MidOut at T+(ifelse(m4dirt,right,-)m4nargs*lu,0) # end Sg
765 # Draw the connecting lines
766 define(`m4hhv',`(m4nargs-1)/2')dnl
767 ifelse(m4dirt,right,
768 `for_(1,m4nargs,1,`ifelse(m4x,1,,` ')line thick lineth from Q.In`'m4x \
769 left Q.In`'m4x.x-T.x+(m4hhv-abs(m4x-m4hhv-1))*lu \
770 then up Sg.G`'m4x.Out.y-Q.In`'m4x.y \
771 then to Sg.G`'m4x.Out')',
772 `for_(1,m4nargs,1,`line thick lineth from Q.In`'eval(m4nargs+1-m4x) \
773 right T.x-Q.In`'eval(m4nargs+1-m4x).x+(m4hhv-abs(m4x-m4hhv-1))*lu \
774 then up Sg.G`'m4x.Out.y-Q.In`'eval(m4nargs+1-m4x).y \
775 then to Sg.G`'m4x.Out')')
776 popdef(`m4nargs') m4xpand(m4dirt`'_) ]')
777
778 define(`m4ifboolvar_',
779 `ifelse(substr(`$1',0,1),_,`$3',`ifdef(`$1',`$3',`$2')')')
780
781 Empty a stack while nonblank, executing arg2
782 define(`m4stackdump',`ifdef(`$1',`ifelse($1,,`popdef(`$1')',
783 `$2`'popdef(`$1')m4stackdump(`$1',`$2')')')')dnl
784
785 define(`DrawIn',`
786 # Draw and label input $1
787 PrevInput: PrevInput-ifdef(`m4LI',`(0,autoinputsep)',
788 `(ifelse(m4_dir_,left,-)autoinputsep,0)')
789 In`'$1: PrevInput define(`m4dirt',m4_dir_)
790 ifinstr(`$2',N,
791 ` line thick lineth from PrevInput ifdef(`m4LI',m4_dir_`'_,down_) dimen_/2
792 linethick = gatelineth
793 NOT_gate
794 linethick = lineth
795 InNt`'$1: Here',
796 ` line thick lineth from PrevInput ifdef(`m4LI',m4_dir_,down) dimen_/4
797 Int`'$1: Here')
798 m4xpand(m4dirt`'_)
799 ')
800 define(`DrawInNotIn',`
801 # Draw and label input $1 inverted and uninverted.
802 PrevInput: PrevInput-ifdef(`m4LI',`(0,autoinputsep)',
803 `(ifelse(m4_dir_,left,-)autoinputsep*2,0)')
804 In`'$1: PrevInput define(`m4dirt',m4_dir_)
805 line thick lineth from PrevInput ifdef(`m4LI',m4_dir_,down) dimen_/4
806 ifdef(`m4LI',`PrevInput: PrevInput-(0,autoinputsep)')
807 Int`'$1: dot
808 line thick lineth ifdef(`m4LI',down,m4_dir_) autoinputsep \
809 then ifdef(`m4LI',m4_dir_`'_,down_) dimen_/4
810 linethick = gatelineth
811 NOT_gate
812 linethick = lineth
813 InNt`'$1: Here; m4xpand(m4dirt`'_)
814 ')
815
816 ifelse(0,1,`
817 dnl `Autologic(Boolean function, ****Obsolete
818 dnl [N[oconnect]][L[eftinputs]][R][V][;offset=val])'
819
820 ` Example: Autologic(`Nor(`And(x,y)',`And(~x,z)')')
821 Second example, (the same function):
822 Autologic(ZNor(ZAnd(x,y),ZAnd(~x,z)))
823 '
824 define(`Autologic',
825 `print `"*** Circuit_macros warning: Autologic is obsolete; use Autologix"'
826 Autologix($@)')
827 ')
828 ############################
829
830 `Autologix(FunctionSpec;FunctionSpec;... ,
831 [M[irror]]
832 [N[oconnect]]
833 [L[eftinputs]]
834 [R][V]
835 [;offset=val],
836 fill spec)
837 FunctionSpec =
838 Boolean-fn(args) [@ location attribute]'
839 ` e.g. HalfAdder: Autologix(Xor(x,y);And(x,y),LVR)
840
841 Drawing single gates is not enough; more general Boolean expressions
842 in function notation should be drawn automatically. This macro draws
843 one or more trees of gates with the output or outputs
844 (roots) to the right (on the left if the M[irror] option is used).
845 The predefined functions are And, Or, Not, Nand, Nor, Xor, Nxor,
846 and Buffer, and may be nested; e.g., Or(And(x,~y),And(~x,y)).
847 Function notation does not model internal connections such as
848 feedback. However, internal nodes are labelled for later access.
849
850 The exact appearance of a tree depends on the order in which
851 terms and variables appear in the expressions. Gates can be placed
852 relative to previously drawn objects using the @ location construct;
853 e.g. @with .nw at last [].sw+(0,-dimen_).
854
855 Autologix locates the distinct input variables (with variables preceded
856 by ~ given NOT gates) in a row or column and connects them to
857 the internal gates. If x1 (for example) appears as a variable,
858 then the corresponding input location is Inx1.
859
860 Inputs are placed in a row from right to left by default, in the
861 order in which the variables first appear in the expressions.
862 An R in the second argument reverses their order and a V reverses
863 the order in which variables are scanned in the expresssions. An
864 N in the second argument draws the expression tree or trees only,
865 suppresses drawing of the inputs, and defines the gate inputs as
866 In1, In2, ...
867
868 Placing inputs in a column on the left using the L option introduces
869 graphic complexity. Therefore, hand tuning may be required for
870 complex expressions; putting offset=value in arg2 shifts the array
871 of inputs. Time will tell whether this can be improved.
872
873 Arbitrary subcircuits with inputs In1, In2, ... on the left and at
874 least one output Out on the right can be included (see the example
875 containing SRff).
876
877 Internal blocks:
878 Fx contains all the FunctionSpec trees; the input connections
879 are drawn external to Fx
880 Each Txi contains an input variable or a function tree
881 Function tree i is drawn by the `AutoGate' macro.
882 '
883 define(`Autologix',
884 `[ `# Autologix'
885 dnl Split arg1 into FunctionSpecs
886 undefine(`m4BooleanR')stacksplit_(`m4BooleanR',`$1',;)dnl
887 dnl
888 ifinstr(`$2',L,`define(`m4LI')',`undefine(`m4LI')')dnl
889 undefine(`m4InNames')undefine(`AutoOutNames')define(`m4Path',)dnl
890 define(`m4nbf',0)undefine(`m4BooleanFn')undefine(`m4posattr')dnl
891 dnl Separate functions from position attributes
892 stackexec_(`m4BooleanR',,
893 `define(`m4nbf',incr(m4nbf))define(`m4xi',regexp(m4BooleanR,[a-zA-Z_~]))dnl
894 ifinstr(m4BooleanR,@,`define(`m4xc',index(m4BooleanR,@))dnl
895 pushdef(`m4BooleanFn',substr(m4BooleanR,m4xi,eval(m4xc-m4xi)))dnl
896 pushdef(`m4posattr',substr(m4BooleanR,incr(m4xc)))',
897 `pushdef(`m4BooleanFn',substr(m4BooleanR,m4xi))dnl
898 pushdef(`m4posattr')')dnl
899 ')dnl
900 pushdef(`Lg_body',`$3')
901 dnl Sublayer of functions, outputs Out1, Out2,..
902 pushdef(`m4Path',ifelse(m4Path,,,m4Path.)Fx)
903 Fx: [ define(`m4fno',0)dnl
904 ifinstr(`$2',M,`left_ define(`m4corner',e)',`right_ define(`m4corner',w)')
905 dnl
906 stackexec_(`m4BooleanFn',,
907 `define(`m4fno',incr(m4fno))dnl
908 pushdef(`m4Path',m4Path.Tx`'m4fno)dnl
909 dnl Simple variable or gate
910 m4ifboolvar_(m4BooleanFn,
911 `Tx`'m4fno: [ifelse(substr(m4BooleanFn,0,1),~,
912 `define(`m4xg',substr(m4BooleanFn,1))dnl
913 pushdef(`m4InNames',m4Path.In`'m4xg`'_N)define(N_`'m4xg)
914 In`'m4xg`'_N: Here',
915 `pushdef(`m4InNames',m4Path.In`'m4BooleanFn`'_X)dnl
916 define(X_`'m4BooleanFn)dnl
917 In`'m4BooleanFn`'_X: Here'); Out: Here ] ht 2*autovsep',
918 `Tx`'m4fno: m4Delch(m4BooleanFn)') ifelse(m4posattr,,
919 `ifelse(m4fno,1,,`with .n`'ifinstr(`$2',M,w,e) \
920 at Tx`'eval(m4fno-1).s`'ifinstr(`$2',M,w,e)+(0,-dimen_/4)')',
921 m4posattr) dnl
922 pushdef(`AutoOutNames',m4Path.Out)dnl
923 popdef(`m4posattr') `#' End Tx`'m4fno
924 popdef(`m4Path')dnl
925 dnl Functions drawn
926 dnl Out and Out1, Out2, ...
927 pushdef(`AutoOutNames',m4Path.Out`'m4fno)dnl
928 Out`'m4fno: Tx`'m4fno.Out
929 ifelse(m4nbf,1,`; Out: Tx1.Out')
930 ') ] `#' End Fx
931 popdef(`m4Path')dnl
932 stackreverse_(`m4InNames')dnl
933 define(`M4TopInput',m4InNames)dnl
934 dnl
935 dnl Delete blank names; mark top and bottom inputs
936 stackexec_(`m4InNames',,`ifelse(m4InNames,,,
937 `pushdef(`m4R',m4InNames)define(`m4bn',basename_(m4R))dnl
938 ifdef(Top`'m4bn,,`define(Top`'m4bn,m4R)')define(Bot`'m4bn,m4R)')')
939 dnl
940 dnl Check V option
941 ifinstr(`$2',V,
942 `stackexec_(`m4R',`m4TAL')stackexec_(`m4TAL',`m4InNames')',
943 `stackexec_(`m4R',`m4InNames')')
944 dnl
945 dnl Count and extract bare input names
946 undefine(`m4r')undefine(`m4f')define(`m4nargs',0)undefine(`m4N_')dnl
947 stackexec_(`m4InNames',`m4R',
948 `define(`m4nargs',incr(m4nargs))define(`m4bn',basename_(`m4InNames'))dnl
949 ifelse(substr(m4bn,decr(len(m4bn))),N,`define(`m4N_')')dnl
950 pushdef(`m4r',substr(m4bn,2,eval(len(m4bn)-4)))')dnl
951 stackexec_(`m4R',`m4InNames')stackexec_(`m4r',`m4f')
952 dnl
953 dnl Unique bare input names in forward order
954 stackexec_(`m4f',,`ifdef(D_`'m4f,,`pushdef(`m4r',m4f)define(D_`'m4f)')')
955 stackexec_(`m4r',`m4f',`undefine(D_`'m4r)')
956 dnl
957 dnl Optional reverse of bare name order
958 ifinstr(`$2',R,`stackreverse_(`m4f')')
959 dnl Get the offset=value if any
960 pushkey_($2,offset,0)dnl
961 dnl Place reference for row or column of inputs
962 PrevInput: ifdef(`m4LI',dnl
963 `ifinstr(`$2',M,
964 `(Fx.e.x+dimen_/2+m4nargs*2*L_unit`'ifdef(`m4N_',`+(BUF_wd+N_diam)*L_unit'),\
965 M4TopInput.y+autoinputsep+m4offset)',
966 `(Fx.w.x-dimen_/2-m4nargs*2*L_unit`'ifdef(`m4N_',`-(BUF_wd+N_diam)*L_unit'),\
967 M4TopInput.y+autoinputsep+m4offset)')',
968 `ifinstr(`$2',M,
969 `Fx.ne+(-(autoinputsep/2+dimen_/4+m4offset),dimen_`'ifdef(`m4N_',,/4))',
970 `Fx.nw+( autoinputsep/2+dimen_/4+m4offset, dimen_`'ifdef(`m4N_',,/4))')')
971 popdef(`m4offset') dnl
972 # Draw inputs right to left or top to bottom
973 stackexec_(`m4f',`m4r',`ifinstr(`$2',N,,`ifdef(X_`'m4f,
974 `ifdef(N_`'m4f,`DrawInNotIn(m4f)',`DrawIn(m4f)')',`DrawIn(m4f,N)')')')
975 stackexec_(`m4r',,`undefine(N_`'m4r)undefine(X_`'m4r)')
976 dnl
977 dnl Show the internal inputs in comment lines
978 dnl `# Internal input names' (m4nargs):dnl
979 dnl stackexec_(`m4InNames',`m4R',`
980 dnl `#' m4InNames')
981 dnl stackexec_(`m4R',`m4InNames')
982 dnl
983 # Promote the In and Out locations to the top level
984 `# Internal gate input labels' (m4nargs):dnl
985 define(`m4ix',0)dnl
986 stackexec_(`m4InNames',`m4R',`define(`m4ix',incr(m4ix))
987 In`'m4ix: m4InNames')
988 stackexec_(`m4R',`m4InNames')
989 `# Internal gate output labels (there may be duplicate locations)':dnl
990 define(`m4ix',0)define(`m4lastout')dnl
991 stackexec_(`AutoOutNames',,
992 `ifelse(AutoOutNames,m4lastout,,`define(`m4ix',incr(m4ix))
993 Out`'m4ix: AutoOutNames define(`m4lastout',AutoOutNames)')')
994 # Connect the gates to the inputs and clean up
995 ifinstr(`$2',N,,
996 `m4AutoConnex(Fx,`m4InNames',ifdef(`m4LI',`ifinstr(`$2',V,Top,Bot)',Bot))')
997 stackexec_(`m4TAL',,
998 `define(`m4bn',basename_(m4TAL))undefine(Bot`'m4bn)undefine(Top`'m4bn)')
999 popdef(`Lg_body')dnl
1000 # Define the outputs
1001 ifelse(m4nbf,1,`Out: Fx.Out')
1002 for_(1,m4nbf,1,`Out`'m4x: Fx.Out`'m4x')
1003 `$4' `# End Autologix'
1004 ] ')
1005
1006 define(`m4AutoConnex',`define(`m4cx',0)dnl
1007 define(`m4ltn',`substr(`$2',decr(len(`$2')))')dnl
1008 stackexec_(`$2',`m4TAL',
1009 `define(`m4ltn',substr($2,decr(len($2))))dnl
1010 ifelse(ifelse(m4ltn,N,T,m4ltn,X,T),T,
1011 `define(`m4bn',basename_($2))dnl
1012 define(`m4hn',substr(m4bn,2,eval(len(m4bn)-4)))dnl
1013 define(`m4cx',incr(m4cx))dnl
1014 ifdef(`m4LI',
1015 `Vx: ($1.w.x-(m4nargs-m4cx)*L_unit*2,$2.y)
1016 Tx: In`'ifelse(m4ltn,N,N)t`'m4hn
1017 line thick lineth from $2 to Vx \
1018 then to (Vx,Tx) ifelse($2,m4xpand($3`'m4bn),` then to Tx',`
1019 dot') ',
1020 `Tx: In`'ifelse(m4ltn,N,N)t`'m4hn
1021 line thick lineth from $2 \
1022 to (Tx,$2) ifelse($2,m4xpand($3`'m4bn),` then to Tx',`
1023 dot')')
1024 ')dnl
1025 ')')
1026
1027 ############################
1028
1029 `FlipFlop( D|T|RS|JK, label, boxspec, pinlength )'
1030 define(`FlipFlop',`ifelse(
1031 `$1',D, `FlipFlopX(`$3',`$2',
1032 : D:PinD;E:CK:PinCK, , :Q:PinQ;:lg_bartxt(Q):PinNQ,,`$4',`$5')',
1033 `$1',T, `FlipFlopX(`$3',`$2',
1034 : T:PinT;E:CK:PinCK, , :Q:PinQ;:lg_bartxt(Q):PinNQ,,`$4',`$5')',
1035 `$1',RS, `FlipFlopX(`$3',`$2',
1036 : R:PinR;: S:PinS, , :Q:PinQ;:lg_bartxt(Q):PinNQ,,`$4',`$5')',
1037 `$1',SR, `FlipFlopX(`$3',`$2',
1038 : S:PinS;: R:PinR, , :Q:PinQ;:lg_bartxt(Q):PinNQ,,`$4',`$5')',
1039 `$1',JK, `FlipFlopX(`$3',`$2',
1040 : J:PinJ;NE:CK:PinCK;: K:PinK, N:CLR:PinCLR,
1041 :Q:PinQ;:lg_bartxt(Q):PinNQ, N:PR:PinPR,`$4',`$5')',
1042 `FlipFlopX(`$3',`$2', ::Pin;, , :Q:PinQ;:lg_bartxt(Q):PinNQ,,`$4',`$5')')')
1043
1044 `FlipFlopX( boxspec, center label,
1045 leftpins, toppins, rightpins, bottompins,
1046 pinlength)
1047 General flipflop. Arg1 modifies the
1048 box (labelled Chip) default specification.
1049 Each of args 3 to 6 is null or a string of
1050 pinspecs separated by semicolons (;).
1051 A pinspec is either empty (null) or of the form
1052 [pinopts]:[label[:Picname]]. The first colon (:)
1053 draws the pin. Pins are placed top to bottom
1054 or left to right along the box edges with null
1055 pinspecs counted for placement. Pins are named
1056 by side and number by default; eg W1, W2, ...,
1057 N1, N2, ..., E1, ..., S1, ... ; however, if
1058 :Picname is present in a pinspec then Picname
1059 replaces the default name. A pinspec label is text
1060 placed at the pin base. Semicolons are not
1061 allowed in labels; use eg \char59{} instead.
1062 To put a bar over a label, use lg_bartxt(label).
1063 The pinopts are [L|M|I|O][N][E]
1064 L=active low out; M=active low in;
1065 I=inward arrow; O=outward arrow;
1066 N=pin with not circle; E=edge trigger
1067 Optional arg7 is the length of pins'
1068 define(`FlipFlopX',`[
1069 Chip: box wid_ FF_wid*L_unit ht_ FF_ht*L_unit Lg_body `$1'
1070 dnl Center label
1071 ifelse(`$2',,,"ifsvg(`svg_small($2,75)',`\scriptsize $2')" at Chip.c)
1072 ifelse(`$7',,,`pushdef(`lg_plen',(`$7')/L_unit)')dnl
1073 ifelse(`$3',,,
1074 `m4_ffside(`(m4x-0.5)/m4_pc between Chip.nw_ and Chip.sw_',`$3',W,w)')
1075 ifelse(`$4',,,
1076 `m4_ffside(`(m4x-0.5)/m4_pc between Chip.nw_ and Chip.ne_',`$4',N,n)')
1077 ifelse(`$5',,,
1078 `m4_ffside(`(m4x-0.5)/m4_pc between Chip.ne_ and Chip.se_',`$5',E,e)')
1079 ifelse(`$6',,,
1080 `m4_ffside(`(m4x-0.5)/m4_pc between Chip.sw_ and Chip.se_',`$6',S,s)')
1081 ifelse(`$7',,,`popdef(`lg_plen')')dnl
1082 `$8' ]')
1083 dnl `Stack, count, and draw the pins on the given side'
1084 define(`m4_ffside',
1085 `stacksplit_(`m4pr',`$2',;)define(`m4_pc',0)dnl
1086 stackexec_(`m4pr',`m4pspec',`define(`m4_pc',incr(m4_pc))')dnl
1087 for_(1,m4_pc,1,
1088 `ifinstr(m4pspec,:,
1089 `m4drawpin(`$1',`$3'm4x,`$4'm4_pattocomma(m4pspec,:))')dnl
1090 popdef(`m4pspec')')')dnl
1091 define(`m4drawpin',`lg_pin(`$1',`$4',ifelse(`$5',,`$2',`$5'),`$3')')
1092
1093 ###########################################################################
1094
1095 divert(0)dnl