Define a custom "Register" MetaObj object.
authorMatthijs Kooijman <matthijs@stdin.nl>
Wed, 23 Sep 2009 13:40:58 +0000 (15:40 +0200)
committerMatthijs Kooijman <matthijs@stdin.nl>
Wed, 23 Sep 2009 13:40:58 +0000 (15:40 +0200)
Utils/Metapost.tex

index cac69ddbd68bac88ae4bbf2b8591e862376ea5f8..6d00ae47ca901d393b16de523de9d376f8d95833 100644 (file)
@@ -16,4 +16,82 @@ setCurveDefaultOption("linewidth",.75mm);
 % included in the bounding box for line cutoff.
 setCurveDefaultOption("nodesep",curve_linewidth_default);
 
 % included in the bounding box for line cutoff.
 setCurveDefaultOption("nodesep",curve_linewidth_default);
 
+
+% Define a register.
+% |v| is either a picture, a string or an object given by its number which
+% will be contained inside the register. Supports the same options as Box,
+% plus:
+%   reflect    Reflect the register vertically, so input is right and output
+%              is left. (default: false)
+%   labels     Show labels for the input and output ports. (default: false)
+vardef newReg@#(expr v) text options=
+  ExecuteOptions(@#)(options);
+  assignObj(@#,"Reg");
+  StandardInterface;
+  
+  % We contain a box (that contains the passed object in turn)
+  SubObject(box,obj(newobjstring_));
+  newBox.obj(@#box)(v) options;
+  % Some points for ports
+  ObjPoint ck,d,out;
+
+  ObjCode StandardEquations,
+    % Align our bounding box with the enclosed box
+    "obj(@#box).c = @#c",
+    "obj(@#box).se = @#se",
+    "obj(@#box).nw = @#nw",
+    % Put the clock, data and output ports at the right spots
+    if OptionValue@#("reflect"):
+      "xpart @#out = xpart @#w",
+      "xpart @#d = xpart @#ck = xpart @#e",
+    else:
+      "xpart @#out = xpart @#e",
+      "xpart @#d = xpart @#ck = xpart @#w",
+    fi
+    "ypart @#d = ypart @#out = ypart (@#sw * .25 + @#nw * .75)",
+    "ypart @#ck = ypart (@#sw * .75 + @#nw * .25)";
+
+  StandardTies;
+enddef;
+
+% Draw a register
+def drawReg(suffix reg)=
+  save clkwh, refl;
+  % Draw the box
+  drawObj(obj(reg.box));
+
+  % Fill refl with 1 or -1, to invert coordinates when reflected.
+  if OptionValue.reg("reflect"):
+    refl = -1;
+  else:
+    refl = 1;
+  fi;
+
+  % Calculate (half of) the height and width of the triangle
+  clkwh = ypart((reg.n - reg.s)) * .1;
+  % Draw the "clock" triangle
+  draw ((0, clkwh) -- (refl * clkwh, 0) -- (0, -clkwh)) shifted reg.ck;
+
+  % Draw input and output labels
+  if OptionValue.reg("labels"):
+    if OptionValue.reg("reflect"):
+      label.lft(\sometxt{D}, reg.d);
+      label.rt(\sometxt{O}, reg.out);
+    else:
+      label.rt(\sometxt{D}, reg.d);
+      label.lft(\sometxt{O}, reg.out);
+    fi;
+  fi
+enddef;
+
+% Bounding path for a register
+def BpathReg(suffix n)=BpathObj(obj(n.box)) enddef;
+
+% Define options
+define_global_boolean_option("reflect");
+define_global_boolean_option("labels");
+setObjectDefaultOption("Reg")("reflect")(false);
+setObjectDefaultOption("Reg")("labels")(false);
+
 \stopMPinclusions
 \stopMPinclusions