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 @#d = ypart @#out = ypart (@#sw * .25 + @#nw * .75)",
54 "ypart @#ck = ypart (@#sw * .75 + @#nw * .25)";
60 def drawReg(suffix reg)=
63 drawObj(obj(reg.box));
65 % Fill refl with 1 or -1, to invert coordinates when reflected.
66 if OptionValue.reg("reflect"):
72 % Calculate (half of) the height and width of the triangle
73 clkwh = ypart((reg.n - reg.s)) * .1;
74 % Draw the "clock" triangle
75 draw ((0, clkwh) -- (refl * clkwh, 0) -- (0, -clkwh)) shifted reg.ck;
77 % Draw input and output labels
78 if OptionValue.reg("labels"):
79 if OptionValue.reg("reflect"):
80 label.lft(\sometxt{D}, reg.d);
81 label.rt(\sometxt{O}, reg.out);
83 label.rt(\sometxt{D}, reg.d);
84 label.lft(\sometxt{O}, reg.out);
89 % Bounding path for a register
90 def BpathReg(suffix n)=BpathObj(obj(n.box)) enddef;
93 define_global_boolean_option("reflect");
94 define_global_boolean_option("labels");
95 setObjectDefaultOption("Reg")("reflect")(false);
96 setObjectDefaultOption("Reg")("labels")(false);
98 % Define a Multiplexer.
99 vardef newMux@# text options=
100 ExecuteOptions(@#)(options);
104 ObjPoint inpa, inpb, out;
106 ObjCode MinimumStandardEquations,
107 % Make it a parallel trapezium
108 "xpart(@#ne - @#nw) = xpart(@#se - @#sw)",
109 "ypart(@#ne - @#nw) = -1 * ypart(@#se - @#sw)",
110 % Use a 22.5 degree angle for the sides
111 "ypart(@#nw) - ypart(@#ne) = xpart(@#ne-@#nw) / 2",
112 % With a specific width and height
113 "@#e-@#w = (" & decimal OptionValue@#("width") & ", 0mm)",
114 "@#n-@#s = (0mm, " & decimal OptionValue@#("height") & ")",
115 % And space the input ports evenly
116 "@#inpa = midpoint(@#nw, @#w)",
117 "@#inpb = midpoint(@#w, @#sw)",
124 def drawMux(suffix mux)=
126 % Use our bounding path to draw
127 drawFramedOrFilledObject_(mux);
130 % Bounding path for a multiplexer
131 def BpathMux(suffix n)=StandardBpath(n) enddef;
134 define_local_numeric_option("width");
135 define_local_numeric_option("height");
136 setObjectDefaultOption("Mux")("width")(5mm);
137 setObjectDefaultOption("Mux")("height")(20mm);
138 setObjectDefaultOption("Mux")("framed")(true);
140 %setObjectDefaultOption("Mux")("framewidth")(.5bp);
141 setObjectDefaultOption("Mux")("framecolor")(black);
142 setObjectDefaultOption("Mux")("framestyle")("");
143 setObjectDefaultOption("Mux")("shadow")(false);
144 setObjectDefaultOption("Mux")("shadowcolor")(black);
145 setObjectDefaultOption("Mux")("filled")(false);
146 setObjectDefaultOption("Mux")("fillcolor")(black);
149 def midpoint(expr a, b) = ((a + b) / 2) enddef;
153 % Make \overrightarrow "unexpanded", to make it work within metapost.
154 % http://www.ntg.nl/pipermail/ntg-context/2009/043620.html. Will be fixed in
156 \let\normaloverrightarrow\overrightarrow
157 \unexpanded\def\overrightarrow{\normaloverrightarrow}