counter.html 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <html>
  2. <head>
  3. <title>Gray Code Counter</title>
  4. </head>
  5. <body bgcolor="FFFFFF">
  6. <h1>Gray Code Counter</h1>
  7. <p>This orientation to the Logisim libraries concludes with a
  8. fairly sophisticated Gray code counter that allows the user to alter its
  9. current value using the Poke Tool and to place a label on the component using
  10. the Text Tool. It also customizes the icon that appears in the explorer,
  11. associated with the tool.</p>
  12. <h2>GrayCounter</h2>
  13. <pre>
  14. package com.cburch.gray;
  15. import java.net.URL;
  16. import javax.swing.ImageIcon;
  17. import com.cburch.logisim.data.Attribute;
  18. import com.cburch.logisim.data.BitWidth;
  19. import com.cburch.logisim.data.Bounds;
  20. import com.cburch.logisim.data.Direction;
  21. import com.cburch.logisim.instance.Instance;
  22. import com.cburch.logisim.instance.InstanceFactory;
  23. import com.cburch.logisim.instance.InstancePainter;
  24. import com.cburch.logisim.instance.InstanceState;
  25. import com.cburch.logisim.instance.Port;
  26. import com.cburch.logisim.instance.StdAttr;
  27. import com.cburch.logisim.util.GraphicsUtil;
  28. import com.cburch.logisim.util.StringUtil;
  29. /** Manufactures a counter that iterates over Gray codes. This demonstrates
  30. * several additional features beyond the SimpleGrayCounter class. */
  31. class GrayCounter extends InstanceFactory {
  32. public GrayCounter() {
  33. super("Gray Counter");
  34. setOffsetBounds(Bounds.create(-30, -15, 30, 30));
  35. setPorts(new Port[] {
  36. new Port(-30, 0, Port.INPUT, 1),
  37. new Port( 0, 0, Port.OUTPUT, StdAttr.WIDTH),
  38. });
  39. // We'll have width, label, and label font attributes. The latter two
  40. // attributes allow us to associate a label with the component (though
  41. // we'll also need configureNewInstance to configure the label's
  42. // location).
  43. setAttributes(
  44. new Attribute[] { StdAttr.WIDTH, StdAttr.LABEL, StdAttr.LABEL_FONT },
  45. new Object[] { BitWidth.create(4), "", StdAttr.DEFAULT_LABEL_FONT });
  46. // The following method invocation sets things up so that the instance's
  47. // state can be manipulated using the Poke Tool.
  48. setInstancePoker(CounterPoker.class);
  49. // These next two lines set it up so that the explorer window shows a
  50. // customized icon representing the component type. This should be a
  51. // 16x16 image.
  52. URL url = getClass().getClassLoader().getResource("com/cburch/gray/counter.gif");
  53. if(url != null) setIcon(new ImageIcon(url));
  54. }
  55. /** The configureNewInstance method is invoked every time a new instance
  56. * is created. In the superclass, the method doesn't do anything, since
  57. * the new instance is pretty thoroughly configured already by default. But
  58. * sometimes you need to do something particular to each instance, so you
  59. * would override the method. In this case, we need to set up the location
  60. * for its label. */
  61. protected void configureNewInstance(Instance instance) {
  62. Bounds bds = instance.getBounds();
  63. instance.setTextField(StdAttr.LABEL, StdAttr.LABEL_FONT,
  64. bds.getX() + bds.getWidth() / 2, bds.getY() - 3,
  65. GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);
  66. }
  67. public void propagate(InstanceState state) {
  68. // This is the same as with SimpleGrayCounter, except that we use the
  69. // StdAttr.WIDTH attribute to determine the bit width to work with.
  70. BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
  71. CounterData cur = CounterData.get(state, width);
  72. boolean trigger = cur.updateClock(state.getPort(0));
  73. if(trigger) cur.setValue(GrayIncrementer.nextGray(cur.getValue()));
  74. state.setPort(1, cur.getValue(), 9);
  75. }
  76. public void paintInstance(InstancePainter painter) {
  77. // This is essentially the same as with SimpleGrayCounter, except for
  78. // the invocation of painter.drawLabel to make the label be drawn.
  79. painter.drawBounds();
  80. painter.drawClock(0, Direction.EAST);
  81. painter.drawPort(1);
  82. painter.drawLabel();
  83. if(painter.getShowState()) {
  84. BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
  85. CounterData state = CounterData.get(painter, width);
  86. Bounds bds = painter.getBounds();
  87. GraphicsUtil.drawCenteredText(painter.getGraphics(),
  88. StringUtil.toHexString(width.getWidth(), state.getValue().toIntValue()),
  89. bds.getX() + bds.getWidth() / 2,
  90. bds.getY() + bds.getHeight() / 2);
  91. }
  92. }
  93. }
  94. </pre>
  95. <h2>CounterPoker</h2>
  96. <pre>
  97. package com.cburch.gray;
  98. import java.awt.Color;
  99. import java.awt.Graphics;
  100. import java.awt.event.KeyEvent;
  101. import java.awt.event.MouseEvent;
  102. import com.cburch.logisim.data.BitWidth;
  103. import com.cburch.logisim.data.Bounds;
  104. import com.cburch.logisim.data.Value;
  105. import com.cburch.logisim.instance.InstancePainter;
  106. import com.cburch.logisim.instance.InstancePoker;
  107. import com.cburch.logisim.instance.InstanceState;
  108. import com.cburch.logisim.instance.StdAttr;
  109. /** When the user clicks a counter using the Poke Tool, a CounterPoker object
  110. * is created, and that object will handle all user events. Note that
  111. * CounterPoker is a class specific to GrayCounter, and that it must be a
  112. * subclass of InstancePoker in the com.cburch.logisim.instance package. */
  113. public class CounterPoker extends InstancePoker {
  114. public CounterPoker() { }
  115. /** Determines whether the location the mouse was pressed should result
  116. * in initiating a poke.
  117. */
  118. public boolean init(InstanceState state, MouseEvent e) {
  119. return state.getInstance().getBounds().contains(e.getX(), e.getY());
  120. // Anywhere in the main rectangle initiates the poke. The user might
  121. // have clicked within a label, but that will be outside the bounds.
  122. }
  123. /** Draws an indicator that the caret is being selected. Here, we'll draw
  124. * a red rectangle around the value. */
  125. public void paint(InstancePainter painter) {
  126. Bounds bds = painter.getBounds();
  127. BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
  128. int len = (width.getWidth() + 3) / 4;
  129. Graphics g = painter.getGraphics();
  130. g.setColor(Color.RED);
  131. int wid = 7 * len + 2; // width of caret rectangle
  132. int ht = 16; // height of caret rectangle
  133. g.drawRect(bds.getX() + (bds.getWidth() - wid) / 2,
  134. bds.getY() + (bds.getHeight() - ht) / 2, wid, ht);
  135. g.setColor(Color.BLACK);
  136. }
  137. /** Processes a key by just adding it onto the end of the current value. */
  138. public void keyTyped(InstanceState state, KeyEvent e) {
  139. // convert it to a hex digit; if it isn't a hex digit, abort.
  140. int val = Character.digit(e.getKeyChar(), 16);
  141. BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
  142. if(val < 0 || (val & width.getMask()) != val) return;
  143. // compute the next value
  144. CounterData cur = CounterData.get(state, width);
  145. int newVal = (cur.getValue().toIntValue() * 16 + val) & width.getMask();
  146. Value newValue = Value.createKnown(width, newVal);
  147. cur.setValue(newValue);
  148. state.fireInvalidated();
  149. // You might be tempted to propagate the value immediately here, using
  150. // state.setPort. However, the circuit may currently be propagating in
  151. // another thread, and invoking setPort directly could interfere with
  152. // that. Using fireInvalidated notifies the propagation thread to
  153. // invoke propagate on the counter at its next opportunity.
  154. }
  155. }
  156. </pre>
  157. <p><strong>Next:</strong> <a href="guide.html">Guidelines</a>.</p>
  158. </body>
  159. </html>