Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/examples/CanLogic.m4

raw
1 .PS
2 # CanLogic
3 log_init
4 divert(-1)
5 ###########################################################################
6 Macros for automatically drawing 2-layer
7 Boolean functions
8
9 Style parameters
10 define(`dotrad_',(0.01*scale))
11 define(`gatelineth',1)
12 define(`lineth',0.5)
13 define(`inputsep',`2*jog')
14 define(`jog',`(AND_ht/2)*L_unit')
15
16 `CanLogic(layer gate type,[N],
17 output gate type,[N],
18 term,term,...)
19 This is the driver macro. Terms are
20 strings of variables or of variables
21 preceded by the ~ character. Each
22 variable is one letter e.g.
23 CanLogic(NAND,,OR,N,abcd,a~b,c,~ad)'
24 define(`CanLogic',
25 `define(`terms',`shift(shift(shift(shift($@))))')
26 # Determine required input variables and negated
27 # variables
28 Loopover_(`t_',`CLvarloop(`v_',`define(X`'v_)',`define(XN`'v_)',t_)',terms)
29 # Draw the inputs with NOT gates as necessary
30 PrevInput: Here-(inputsep,0)
31 Loopover_(`t_',`CLvarloop(`v_',
32 `ifdef(D`'v_,,`ifdef(XN`'v_,`CLDrawInNotIn(v_)',`CLDrawIn(v_)')')',
33 `ifdef(D`'v_,,`ifdef(X`'v_,`CLDrawInNotIn(v_)',`CLDrawNotIn(v_)')')', t_)',
34 terms)
35 # Draw 2nd-layer gates
36 right_
37 LastGateSE: PrevInput+(5*jog,-(AND_wd*L_unit*1.5)) dnl
38 Loopover_(`t_',
39 `define(`termcount',m4Lx)CLDrawLayerGate(G`'termcount,$1,$2,t_)',
40 terms)
41 # Draw output gate
42 linethick = gatelineth
43 OP: m4xpand(`$3'_gate(termcount,$4)) with .Out at \
44 0.5<G1.Out,G`'termcount.Out> + (jog/2*(termcount+3)+AND_wd*L_unit,0)
45 Out: Here
46 linethick = lineth
47 # Connect 2nd-layer gates to the output gate
48 CLVectorConnect(G,termcount,OP)
49 # Connect the inputs and negated inputs to
50 # 2nd-layer gates
51 Loopover_(`t_',`CLConnectInputs(G`'m4Lx,t_)',terms)
52 # Clean up
53 Loopover_(`t_',`CLDeleteLogDefs(t_)',terms)
54 ')
55 `CLVectorConnect(number of 2nd layer gates,
56 common 2nd layer gate name,
57 output gate name)
58 Connect the 2nd-layer gate outputs to the
59 output gate inputs'
60 define(`CLVectorConnect',
61 `for_(1,`$2',1,
62 `line from `$1'm4x.Out right `$3'.In1.x-`$1'm4x.Out.x \
63 - jog/2*(`$2'+1-abs(2*m4x-`$2'-1)) \
64 then down `$1'm4x.Out.y - `$3'.In`'m4x.y then to `$3'.In`'m4x ')')
65
66 Draw and label a non-inverted input
67 define(`CLDrawIn',
68 `PrevInput: PrevInput+(inputsep,0)
69 In`'$1: PrevInput
70 "$$1$" ljust at PrevInput # Maybe labels should be done externally
71 Int`'$1: PrevInput
72 define(D`'$1)')
73
74 Draw and label an inverted input
75 define(`CLDrawNotIn',
76 `PrevInput: PrevInput+(inputsep,0)
77 In`'$1: PrevInput
78 "$$1$" ljust at PrevInput # Maybe labels should be done externally
79 line down_ 2*jog from PrevInput
80 linethick = gatelineth
81 NOT_gate
82 InNt`'$1: Here
83 linethick = lineth
84 define(D`'$1)')
85 Draw and label an input that is required both
86 inverted and uninverted.
87 define(`CLDrawInNotIn',
88 `PrevInput: PrevInput+(inputsep,0)
89 In`'$1: PrevInput
90 "$$1$" ljust at PrevInput # Maybe labels should be done externally
91 line from PrevInput down jog
92 Int`'$1: dot
93 PrevInput: PrevInput+(inputsep,0)
94 line to (PrevInput,Here) then down_ jog
95 linethick = gatelineth
96 NOT_gate
97 linethick = lineth
98 InNt`'$1: Here
99 define(D`'$1)')
100 `CLvarloop(`var',ifnotnegated,ifnegated,term)
101 Loop over term variables performing actions'
102 define(`CLvarloop',`ifelse(`$4',,,substr(`$4',0,1),~,
103 `define(`$1',substr($4,1,1)) $3
104 CLvarloop(`$1',`$2',`$3',substr($4,2))',
105 `define(`$1',substr($4,0,1)) $2
106 CLvarloop(`$1',`$2',`$3',substr($4,1))')')')
107
108 Count gate inputs and mark last appearance
109 define(`CLCountinputs',`CLvarloop(`v_',
110 `define(`incount',incr(incount)) define(Last`'v_,`$1')',
111 `define(`incount',incr(incount)) define(LastN`'v_,`$1')',$2)')
112
113 Draw a 2nd layer gate
114 define(`CLDrawLayerGate',
115 `define(`incount',0) dnl
116 CLCountinputs($1,$4) dnl
117 ifelse(incount,1,
118 `LastGateSE: LastGateSE-(0,jog)
119 $1: [ In1:Here; line right AND_wd*L_unit; Out: Here] \
120 with .Out at LastGateSE',
121 `LastGateSE: LastGateSE-(0,jog+AND_ht*L_unit)
122 linethick = gatelineth
123 $1: m4xpand(`$2'_gate(incount,$3)) with .se at LastGateSE
124 linethick = lineth ')')
125
126 Connect this gate to its input lines
127 define(`CLConnectInputs',`define(`innum',0) CLvarloop(`v_',
128 `define(`innum',incr(innum))
129 line from `$1'.In`'innum to (In`'v_,`$1'.In`'innum)dnl
130 ifelse(`$1',m4xpand(Last`'v_),`then to In`'v_',`; dot')',
131 `define(`innum',incr(innum))
132 line from `$1'.In`'innum to (InNt`'v_,`$1'.In`'innum)dnl
133 ifelse(`$1',m4xpand(LastN`'v_), `then to InNt`'v_', `; dot')',$2)')
134
135 Delete definitions to allow more than one
136 circuit per diagram
137 define(`CLDeleteLogDefs',`CLvarloop(`v_',
138 `undefine(Last`'v_) undefine(D`'v_) undefine(X`'v_)',
139 `undefine(LastN`'v_) undefine(D`'v_) undefine(XN`'v_)',$1)')
140
141 Thanks to Alexander Ray for suggesting the
142 need for something like these macros
143 ###########################################################################
144 divert(0)dnl
145
146 linethick = lineth
147
148 [ CanLogic(AND,,OR,,abcd,~ba,c,d~a) #; line right jog from Out "$f$" above
149 ]
150 {`"\tt CanLogic(AND,,OR,,abcd,\char126{}ba,c,d\char126{}a)"' \
151 at last [].s -(0,11bp__)}
152
153 [ CanLogic(OR,N,NAND,,ab~c,a~bc,ac,~d) #; line right jog from Out "$f$" above
154 ] with .sw at last [].se+(0.5,0)
155 {`"\tt CanLogic(OR,N,NAND,,ab\char126{}c,a\char126{}bc,ac,\char126{}d)"' \
156 at last [].s - (0,11bp__)}
157
158 .PE