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);
14 setCurveDefaultOption("linewidth",.75mm);
15 % Add a nodesep that's equal to the linewidth, since the linewidth isn't
16 % included in the bounding box for line cutoff.
17 setCurveDefaultOption("nodesep",curve_linewidth_default);
21 % |v| is either a picture, a string or an object given by its number which
22 % will be contained inside the register. Supports the same options as Box,
24 % reflect Reflect the register vertically, so input is right and output
25 % is left. (default: false)
26 % labels Show labels for the input and output ports. (default: false)
27 vardef newReg@#(expr v) text options=
28 ExecuteOptions(@#)(options);
32 % We contain a box (that contains the passed object in turn)
33 SubObject(box,obj(newobjstring_));
34 newBox.obj(@#box)(v) options;
36 % Some points for ports
39 ObjCode StandardEquations,
40 % Align our bounding box with the enclosed box
42 "obj(@#box).se = @#se",
43 "obj(@#box).nw = @#nw",
44 % Put the clock, data and output ports at the right spots
45 if OptionValue@#("reflect"):
46 "xpart @#out = xpart @#w",
47 "xpart @#d = xpart @#ck = xpart @#e",
49 "xpart @#out = xpart @#e",
50 "xpart @#d = xpart @#ck = xpart @#w",
52 "ypart @#d = ypart @#out = ypart (@#sw * .25 + @#nw * .75)",
53 "ypart @#ck = ypart (@#sw * .75 + @#nw * .25)";
59 def drawReg(suffix reg)=
62 drawObj(obj(reg.box));
64 % Fill refl with 1 or -1, to invert coordinates when reflected.
65 if OptionValue.reg("reflect"):
71 % Calculate (half of) the height and width of the triangle
72 clkwh = ypart((reg.n - reg.s)) * .1;
73 % Draw the "clock" triangle
74 draw ((0, clkwh) -- (refl * clkwh, 0) -- (0, -clkwh)) shifted reg.ck;
76 % Draw input and output labels
77 if OptionValue.reg("labels"):
78 if OptionValue.reg("reflect"):
79 label.lft(\sometxt{D}, reg.d);
80 label.rt(\sometxt{O}, reg.out);
82 label.rt(\sometxt{D}, reg.d);
83 label.lft(\sometxt{O}, reg.out);
88 % Bounding path for a register
89 def BpathReg(suffix n)=BpathObj(obj(n.box)) enddef;
92 define_global_boolean_option("reflect");
93 define_global_boolean_option("labels");
94 setObjectDefaultOption("Reg")("reflect")(false);
95 setObjectDefaultOption("Reg")("labels")(false);
97 def midpoint(expr a, b) = ((a + b) / 2) enddef;
101 % Make \overrightarrow "unexpanded", to make it work within metapost.
102 % http://www.ntg.nl/pipermail/ntg-context/2009/043620.html. Will be fixed in
104 \let\normaloverrightarrow\overrightarrow
105 \unexpanded\def\overrightarrow{\normaloverrightarrow}