Debian Codegraph @

texmf-dist/doc/latex/circuit-macros/examples/dpv/CanLogicDPV.m4

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