1 % Some setup for metapost
4 % Use metaobj for drawing objects
7 % Set some defaults for pretty pictures
8 setObjectDefaultOption("Box")("framewidth")(.75mm);
9 setObjectDefaultOption("Circle")("framewidth")(.75mm);
10 setObjectDefaultOption("Circle")("circmargin")(3mm);
11 setObjectDefaultOption("Mux")("framewidth")(.75mm);
15 setCurveDefaultOption("linewidth",.75mm);
16 % Add a nodesep that's equal to the linewidth, since the linewidth isn't
17 % included in the bounding box for line cutoff.
18 setCurveDefaultOption("nodesep",curve_linewidth_default);
22 % |v| is either a picture, a string or an object given by its number which
23 % will be contained inside the register. Supports the same options as Box,
25 % reflect Reflect the register vertically, so input is right and output
26 % is left. (default: false)
27 % labels Show labels for the input and output ports. (default: false)
28 vardef newReg@#(expr v) text options=
29 ExecuteOptions(@#)(options);
33 % We contain a box (that contains the passed object in turn)
34 SubObject(box,obj(newobjstring_));
35 newBox.obj(@#box)(v) options;
37 % Some points for ports
40 ObjCode StandardEquations,
41 % Align our bounding box with the enclosed box
43 "obj(@#box).se = @#se",
44 "obj(@#box).nw = @#nw",
45 % Put the clock, data and output ports at the right spots
46 if OptionValue@#("reflect"):
47 "xpart @#out = xpart @#w",
48 "xpart @#d = xpart @#ck = xpart @#e",
50 "xpart @#out = xpart @#e",
51 "xpart @#d = xpart @#ck = xpart @#w",
53 "ypart @#out = ypart midpoint(@#n, @#s)",
54 "ypart @#d = ypart (@#sw * .25 + @#nw * .75)",
55 "ypart @#ck = ypart (@#sw * .75 + @#nw * .25)";
61 def drawReg(suffix reg)=
64 drawObj(obj(reg.box));
66 % Fill refl with 1 or -1, to invert coordinates when reflected.
67 if OptionValue.reg("reflect"):
73 % Calculate (half of) the height and width of the triangle
74 clkwh = ypart((reg.n - reg.s)) * .1;
75 % Draw the "clock" triangle
76 draw ((0, clkwh) -- (refl * clkwh, 0) -- (0, -clkwh)) shifted reg.ck;
78 % Draw input and output labels
79 if OptionValue.reg("labels"):
80 if OptionValue.reg("reflect"):
81 label.lft(\sometxt{D}, reg.d);
82 label.rt(\sometxt{O}, reg.out);
84 label.rt(\sometxt{D}, reg.d);
85 label.lft(\sometxt{O}, reg.out);
90 % Bounding path for a register
91 def BpathReg(suffix n)=BpathObj(obj(n.box)) enddef;
94 define_global_boolean_option("reflect");
95 define_global_boolean_option("labels");
96 setObjectDefaultOption("Reg")("reflect")(false);
97 setObjectDefaultOption("Reg")("labels")(false);
99 % Define a Multiplexer.
100 vardef newMux@# text options=
101 ExecuteOptions(@#)(options);
105 ObjPoint inpa, inpb, out, sel;
107 ObjCode MinimumStandardEquations,
108 % Make it a parallel trapezium
109 "xpart(@#ne - @#nw) = xpart(@#se - @#sw)",
110 "ypart(@#ne - @#nw) = -1 * ypart(@#se - @#sw)",
111 % Use a 22.5 degree angle for the sides
112 "ypart(@#nw) - ypart(@#ne) = xpart(@#ne-@#nw) / 2",
113 % With a specific width and height
114 "@#e-@#w = (" & decimal OptionValue@#("width") & ", 0mm)",
115 "@#n-@#s = (0mm, " & decimal OptionValue@#("height") & ")",
116 % And space the input ports evenly
117 "@#inpa = midpoint(@#nw, @#w)",
118 "@#inpb = midpoint(@#w, @#sw)",
126 def drawMux(suffix mux)=
128 % Use our bounding path to draw
129 drawFramedOrFilledObject_(mux);
132 % Bounding path for a multiplexer
133 def BpathMux(suffix n)=StandardBpath(n) enddef;
136 define_local_numeric_option("width");
137 define_local_numeric_option("height");
138 setObjectDefaultOption("Mux")("width")(5mm);
139 setObjectDefaultOption("Mux")("height")(15mm);
140 setObjectDefaultOption("Mux")("framed")(true);
142 %setObjectDefaultOption("Mux")("framewidth")(.5bp);
143 setObjectDefaultOption("Mux")("framecolor")(black);
144 setObjectDefaultOption("Mux")("framestyle")("");
145 setObjectDefaultOption("Mux")("shadow")(false);
146 setObjectDefaultOption("Mux")("shadowcolor")(black);
147 setObjectDefaultOption("Mux")("filled")(false);
148 setObjectDefaultOption("Mux")("fillcolor")(black);
151 def midpoint(expr a, b) = ((a + b) / 2) enddef;
155 % Make \overrightarrow "unexpanded", to make it work within metapost.
156 % http://www.ntg.nl/pipermail/ntg-context/2009/043620.html. Will be fixed in
158 \let\normaloverrightarrow\overrightarrow
159 \unexpanded\def\overrightarrow{\normaloverrightarrow}
161 % vim: set sw=2 sts=2 expandtab: