/******************************************************************************* * This file is part of logisim-evolution. * * logisim-evolution is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * logisim-evolution is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with logisim-evolution. If not, see <http://www.gnu.org/licenses/>. * * Original code by Carl Burch (http://www.cburch.com), 2011. * Subsequent modifications by : * + Haute École Spécialisée Bernoise * http://www.bfh.ch * + Haute École du paysage, d'ingénierie et d'architecture de Genève * http://hepia.hesge.ch/ * + Haute École d'Ingénierie et de Gestion du Canton de Vaud * http://www.heig-vd.ch/ * The project is currently maintained by : * + REDS Institute - HEIG-VD * Yverdon-les-Bains, Switzerland * http://reds.heig-vd.ch *******************************************************************************/ package com.cburch.logisim.std.arith; import java.awt.Color; import java.awt.Graphics; import com.bfh.logisim.designrulecheck.CorrectLabel; import com.cburch.logisim.data.Attribute; import com.cburch.logisim.data.AttributeSet; import com.cburch.logisim.data.BitWidth; import com.cburch.logisim.data.Bounds; import com.cburch.logisim.data.Direction; import com.cburch.logisim.data.Location; import com.cburch.logisim.data.Value; import com.cburch.logisim.instance.InstanceFactory; import com.cburch.logisim.instance.InstancePainter; import com.cburch.logisim.instance.InstanceState; import com.cburch.logisim.instance.Port; import com.cburch.logisim.instance.StdAttr; import com.cburch.logisim.tools.key.BitWidthConfigurator; import com.cburch.logisim.util.GraphicsUtil; public class Subtractor extends InstanceFactory { private static final int IN0 = 0; private static final int IN1 = 1; private static final int OUT = 2; private static final int B_IN = 3; private static final int B_OUT = 4; public Subtractor() { super("Subtractor", Strings.getter("subtractorComponent")); setAttributes(new Attribute[] { StdAttr.WIDTH }, new Object[] { BitWidth.create(8) }); setKeyConfigurator(new BitWidthConfigurator(StdAttr.WIDTH)); setOffsetBounds(Bounds.create(-40, -20, 40, 40)); setIconName("subtractor.gif"); Port[] ps = new Port[5]; ps[IN0] = new Port(-40, -10, Port.INPUT, StdAttr.WIDTH); ps[IN1] = new Port(-40, 10, Port.INPUT, StdAttr.WIDTH); ps[OUT] = new Port(0, 0, Port.OUTPUT, StdAttr.WIDTH); ps[B_IN] = new Port(-20, -20, Port.INPUT, 1); ps[B_OUT] = new Port(-20, 20, Port.OUTPUT, 1); ps[IN0].setToolTip(Strings.getter("subtractorMinuendTip")); ps[IN1].setToolTip(Strings.getter("subtractorSubtrahendTip")); ps[OUT].setToolTip(Strings.getter("subtractorOutputTip")); ps[B_IN].setToolTip(Strings.getter("subtractorBorrowInTip")); ps[B_OUT].setToolTip(Strings.getter("subtractorBorrowOutTip")); setPorts(ps); } @Override public String getHDLName(AttributeSet attrs) { StringBuffer CompleteName = new StringBuffer(); if (attrs.getValue(StdAttr.WIDTH).getWidth() == 1) CompleteName.append("FullSubtractor"); else CompleteName.append(CorrectLabel.getCorrectLabel(this.getName())); return CompleteName.toString(); } @Override public boolean HDLSupportedComponent(String HDLIdentifier, AttributeSet attrs) { if (MyHDLGenerator == null) MyHDLGenerator = new SubtractorHDLGeneratorFactory(); return MyHDLGenerator.HDLTargetSupported(HDLIdentifier, attrs); } @Override public void paintInstance(InstancePainter painter) { Graphics g = painter.getGraphics(); painter.drawBounds(); g.setColor(Color.GRAY); painter.drawPort(IN0); painter.drawPort(IN1); painter.drawPort(OUT); painter.drawPort(B_IN, "b in", Direction.NORTH); painter.drawPort(B_OUT, "b out", Direction.SOUTH); Location loc = painter.getLocation(); int x = loc.getX(); int y = loc.getY(); GraphicsUtil.switchToWidth(g, 2); g.setColor(Color.BLACK); g.drawLine(x - 15, y, x - 5, y); GraphicsUtil.switchToWidth(g, 1); } @Override public void propagate(InstanceState state) { // get attributes BitWidth data = state.getAttributeValue(StdAttr.WIDTH); // compute outputs Value a = state.getPortValue(IN0); Value b = state.getPortValue(IN1); Value b_in = state.getPortValue(B_IN); if (b_in == Value.UNKNOWN || b_in == Value.NIL) b_in = Value.FALSE; Value[] outs = Adder.computeSum(data, a, b.not(), b_in.not()); // propagate them int delay = (data.getWidth() + 4) * Adder.PER_DELAY; state.setPort(OUT, outs[0], delay); state.setPort(B_OUT, outs[1].not(), delay); } }