Exemplo n.º 1
0
  private void drawInstance(InstancePainter painter, boolean isGhost) {
    Graphics2D g = (Graphics2D) painter.getGraphics().create();
    Location loc = painter.getLocation();
    g.translate(loc.getX(), loc.getY());

    Direction from = painter.getAttributeValue(StdAttr.FACING);
    int degrees = Direction.EAST.toDegrees() - from.toDegrees();
    double radians = Math.toRadians((degrees + 360) % 360);
    g.rotate(radians);

    GraphicsUtil.switchToWidth(g, Wire.WIDTH);
    if (!isGhost && painter.getShowState()) {
      g.setColor(painter.getPort(0).getColor());
    }
    g.drawLine(0, 0, 5, 0);

    GraphicsUtil.switchToWidth(g, 1);
    if (!isGhost && painter.shouldDrawColor()) {
      BitWidth width = painter.getAttributeValue(StdAttr.WIDTH);
      g.setColor(Value.repeat(Value.FALSE, width.getWidth()).getColor());
    }
    g.drawLine(6, -8, 6, 8);
    g.drawLine(9, -5, 9, 5);
    g.drawLine(12, -2, 12, 2);

    g.dispose();
  }
Exemplo n.º 2
0
  //
  // graphics methods
  //
  @Override
  public void paintInstance(InstancePainter painter) {
    Graphics g = painter.getGraphics();
    FontMetrics fm = g.getFontMetrics();
    int asc = fm.getAscent();

    painter.drawBounds();

    String s0;
    String type = getType(painter.getAttributeSet());
    if (type.equals("zero")) s0 = Strings.get("extenderZeroLabel");
    else if (type.equals("one")) s0 = Strings.get("extenderOneLabel");
    else if (type.equals("sign")) s0 = Strings.get("extenderSignLabel");
    else if (type.equals("input")) s0 = Strings.get("extenderInputLabel");
    else s0 = "???"; // should never happen
    String s1 = Strings.get("extenderMainLabel");
    Bounds bds = painter.getBounds();
    int x = bds.getX() + bds.getWidth() / 2;
    int y0 = bds.getY() + (bds.getHeight() / 2 + asc) / 2;
    int y1 = bds.getY() + (3 * bds.getHeight() / 2 + asc) / 2;
    GraphicsUtil.drawText(g, s0, x, y0, GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);
    GraphicsUtil.drawText(g, s1, x, y1, GraphicsUtil.H_CENTER, GraphicsUtil.V_BASELINE);

    BitWidth w0 = painter.getAttributeValue(ATTR_OUT_WIDTH);
    BitWidth w1 = painter.getAttributeValue(ATTR_IN_WIDTH);
    painter.drawPort(0, "" + w0.getWidth(), Direction.WEST);
    painter.drawPort(1, "" + w1.getWidth(), Direction.EAST);
    if (type.equals("input")) painter.drawPort(2);
  }
Exemplo n.º 3
0
 public BitExtender() {
   super("Bit Extender", Strings.getter("extenderComponent"));
   setIconName("extender.gif");
   setAttributes(
       new Attribute[] {ATTR_IN_WIDTH, ATTR_OUT_WIDTH, ATTR_TYPE},
       new Object[] {BitWidth.create(8), BitWidth.create(16), ATTR_TYPE.parse("sign")});
   setFacingAttribute(StdAttr.FACING);
   setKeyConfigurator(
       JoinedConfigurator.create(
           new BitWidthConfigurator(ATTR_OUT_WIDTH),
           new BitWidthConfigurator(ATTR_IN_WIDTH, 1, Value.MAX_WIDTH, 0)));
   setOffsetBounds(Bounds.create(-40, -20, 40, 40));
 }
Exemplo n.º 4
0
  @Override
  public void propagate(InstanceState state) {
    // get attributes
    BitWidth dataWidth = state.getAttributeValue(StdAttr.WIDTH);

    // compute outputs
    Value in = state.getPort(IN);
    Value out;
    if (in.isFullyDefined()) {
      out = Value.createKnown(in.getBitWidth(), -in.toIntValue());
    } else {
      Value[] bits = in.getAll();
      Value fill = Value.FALSE;
      int pos = 0;
      while (pos < bits.length) {
        if (bits[pos] == Value.FALSE) {
          bits[pos] = fill;
        } else if (bits[pos] == Value.TRUE) {
          if (fill != Value.FALSE) {
            bits[pos] = fill;
          }

          pos++;
          break;
        } else if (bits[pos] == Value.ERROR) {
          fill = Value.ERROR;
        } else {
          if (fill == Value.FALSE) {
            fill = bits[pos];
          } else {
            bits[pos] = fill;
          }
        }
        pos++;
      }
      while (pos < bits.length) {
        if (bits[pos] == Value.TRUE) {
          bits[pos] = Value.FALSE;
        } else if (bits[pos] == Value.FALSE) {
          bits[pos] = Value.TRUE;
        }
        pos++;
      }
      out = Value.create(bits);
    }

    // propagate them
    int delay = (dataWidth.getWidth() + 2) * Adder.PER_DELAY;
    state.setPort(OUT, out, delay);
  }
Exemplo n.º 5
0
 @Override
 public <V> void setValue(Attribute<V> attr, V value) {
   if (attr == StdAttr.WIDTH) {
     width = (BitWidth) value;
     int bits = width.getWidth();
     int mask = bits >= 32 ? -1 : ((1 << inputs) - 1);
     negated &= mask;
   } else if (attr == StdAttr.FACING) {
     facing = (Direction) value;
   } else if (attr == StdAttr.LABEL) {
     label = (String) value;
   } else if (attr == StdAttr.LABEL_FONT) {
     labelFont = (Font) value;
   } else if (attr == ATTR_SIZE) {
     size = (AttributeOption) value;
   } else if (attr == ATTR_INPUTS) {
     inputs = ((Integer) value).intValue();
     fireAttributeListChanged();
   } else if (attr == ATTR_XOR) {
     xorBehave = (AttributeOption) value;
   } else if (attr == ATTR_OUTPUT) {
     out = (AttributeOption) value;
   } else if (attr instanceof NegateAttribute) {
     int index = ((NegateAttribute) attr).index;
     if (((Boolean) value).booleanValue()) {
       negated |= 1 << index;
     } else {
       negated &= ~(1 << index);
     }
   } else {
     throw new IllegalArgumentException("unrecognized argument");
   }
   fireAttributeValueChanged(attr, value);
 }
Exemplo n.º 6
0
  @Override
  public void propagate(InstanceState state) {
    Value in = state.getPortValue(1);
    BitWidth wout = state.getAttributeValue(ATTR_OUT_WIDTH);
    String type = getType(state.getAttributeSet());
    Value extend;
    if (type.equals("one")) {
      extend = Value.TRUE;
    } else if (type.equals("sign")) {
      int win = in.getWidth();
      extend = win > 0 ? in.get(win - 1) : Value.ERROR;
    } else if (type.equals("input")) {
      extend = state.getPortValue(2);
      if (extend.getWidth() != 1) extend = Value.ERROR;
    } else {
      extend = Value.FALSE;
    }

    Value out = in.extendWidth(wout.getWidth(), extend);
    state.setPort(0, out, 1);
  }
Exemplo n.º 7
0
  public Negator() {
    super("Negator", __("negatorComponent"));
    setAttributes(new Attribute[] {StdAttr.WIDTH}, new Object[] {BitWidth.create(8)});
    setKeyConfigurator(new BitWidthConfigurator(StdAttr.WIDTH));
    setOffsetBounds(Bounds.create(-40, -20, 40, 40));
    setIconName("negator.svg");

    Port[] ps = new Port[2];
    ps[IN] = new Port(-40, 0, Port.INPUT, StdAttr.WIDTH);
    ps[OUT] = new Port(0, 0, Port.OUTPUT, StdAttr.WIDTH);
    ps[IN].setToolTip(__("negatorInputTip"));
    ps[OUT].setToolTip(__("negatorOutputTip"));
    setPorts(ps);
  }
Exemplo n.º 8
0
public class ALU extends ManagedComponent {
  public static final ComponentFactory factory = new Factory();

  private static class Factory extends AbstractComponentFactory {
    private Factory() {}

    public String getName() {
      return "MIPS ALU";
    }

    public String getDisplayName() {
      return "ALU";
    }

    public Component createComponent(Location loc, AttributeSet attrs) {
      return new ALU(loc, attrs);
    }

    public Bounds getOffsetBounds(AttributeSet attrs) {
      return Bounds.create(-30, -50, 60, 100);
    }

    public void paintIcon(ComponentDrawContext context, int x, int y, AttributeSet attrs) {
      drawALUIcon(context, x, y);
    }
  }

  public ALU(Location loc, AttributeSet attrs) {
    super(loc, attrs, 5);
    setEnd(0, loc.translate(-30, -30), BITWIDTH_32, 1);
    setEnd(1, loc.translate(-30, 30), BITWIDTH_32, 1);
    setEnd(2, loc.translate(-10, 40), BITWIDTH_4, 1);
    setEnd(3, loc.translate(30, -20), BITWIDTH_1, 2);
    setEnd(4, loc.translate(30, 0), BITWIDTH_32, 2);
    setEnd(5, loc.translate(30, 20), BITWIDTH_1, 2);
  }

  public ComponentFactory getFactory() {
    return factory;
  }

  public void propagate(CircuitState state) {
    int A = state.getValue(getEndLocation(0)).toIntValue();
    int B = state.getValue(getEndLocation(1)).toIntValue();
    int op = state.getValue(getEndLocation(2)).toIntValue();
    int ans = 0;
    int overflow = 0;
    int zero = 0;
    switch (op) {
      case 0x0:
        // and
        ans = A & B;
        break;
      case 0x1:
        // or
        ans = A | B;
        break;
      case 0x2:
        // add
        ans = A + B;
        if ((A >= 0 && B >= 0 && ans < 0) || (A < 0 && B < 0 && ans >= 0)) {
          overflow = 1;
        }
        break;
      case 0x6:
        // sub
        ans = A - B;
        if ((A >= 0 && B < 0 && ans < 0) || (A < 0 && B >= 0 && ans >= 0)) {
          overflow = 1;
        }
        break;
      case 0x7:
        // slt
        ans = (A < B) ? 0x1 : 0x0;
        break;
      case 0xC:
        // nor
        ans = ~(A | B);
        break;
      case 0x8:
        // sll
        ans = B << A;
        break;
      case 0x9:
        // srl
        ans = B >>> A;
        break;
      case 0xA:
        // sra
        ans = B >> A;
        break;
    }
    if (ans == 0) {
      zero = 1;
    }
    Value out = Value.createKnown(BITWIDTH_32, ans);
    Value outZero = Value.createKnown(BITWIDTH_1, zero);
    Value outOverflow = Value.createKnown(BITWIDTH_1, overflow);
    state.setValue(getEndLocation(3), outZero, this, 4);
    state.setValue(getEndLocation(4), out, this, 5);
    state.setValue(getEndLocation(5), outOverflow, this, 4);
  }

  static void drawALUIcon(ComponentDrawContext context, int x, int y) {
    Graphics g = context.getGraphics();
    g.setColor(Color.BLACK);
    int xp[] = {x, x + 15, x + 15, x, x, x + 3, x};
    int yp[] = {y, y + 5, y + 10, y + 15, y + 10, y + 8, y + 6};
    g.drawPolygon(xp, yp, 7);
  }

  static void drawALU(Graphics g, Bounds bds) {
    int wid = bds.getWidth();
    int ht = bds.getHeight();
    int x0 = bds.getX();
    int x1 = x0 + wid;
    int y0 = bds.getY();
    int y1 = y0 + ht;
    int xp[] = {x0, x1, x1, x0, x0, x0 + 20, x0};
    int yp[] = {y0, y0 + 30, y1 - 30, y1, y1 - 40, y1 - 50, y1 - 60};
    GraphicsUtil.switchToWidth(g, 2);
    g.drawPolygon(xp, yp, 7);
  }

  public void draw(ComponentDrawContext context) {
    drawALU(context.getGraphics(), getBounds());
    context.drawPin(this, 0, "A", Direction.EAST);
    context.drawPin(this, 1, "B", Direction.EAST);
    context.drawPin(this, 3, "Zero", Direction.WEST);
    context.drawPin(this, 4, "Res", Direction.WEST);
    context.drawPin(this, 5, "  OF ", Direction.WEST);
    context.getGraphics().setColor(Color.GRAY);
    context.drawPin(this, 2, "Op", Direction.SOUTH);
    context.getGraphics().setColor(Color.BLACK);
  }

  private static final BitWidth BITWIDTH_32 = BitWidth.create(32);
  private static final BitWidth BITWIDTH_4 = BitWidth.create(4);
  private static final BitWidth BITWIDTH_1 = BitWidth.create(1);
}
Exemplo n.º 9
0
 @Override
 public void propagate(InstanceState state) {
   BitWidth width = state.getAttributeValue(StdAttr.WIDTH);
   state.setPort(0, Value.repeat(Value.FALSE, width.getWidth()), 1);
 }
Exemplo n.º 10
0
 public Port(int dx, int dy, String type, int bits, String exclude) {
   this(dx, dy, type, BitWidth.create(bits), exclude);
 }
Exemplo n.º 11
0
 public Port(int dx, int dy, String type, int bits) {
   this(dx, dy, type, BitWidth.create(bits), defaultExclusive(type));
 }