import net.mograsim.logic.core.LogicObserver;
import net.mograsim.logic.core.timeline.Timeline;
import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
import net.mograsim.logic.core.wires.Wire.ReadEnd;
import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
{
super(timeline);
observers = new ArrayList<>();
- if (output.length() != 1)
- throw new IllegalArgumentException("Switch output can be only a single wire");
this.output = output;
}
- public void switchOn()
+ public void switchFullOn()
{
- setState(true);
+ setState(BitVector.of(Bit.ONE, output.length()));
}
- public void switchOff()
+ public void switchFullOff()
{
- setState(false);
+ setState(BitVector.of(Bit.ZERO, output.length()));
}
public void toggle()
{
- setState(!isOn());
+ if (isFullOn())
+ switchFullOff();
+ else
+ switchFullOn();
}
- public void setState(boolean isOn)
+ public void setState(Bit bit)
{
- setToValueOf(isOn ? Bit.ONE : Bit.ZERO);
+ setState(BitVector.of(bit));
}
- public void setToValueOf(Bit bit)
+ public void setState(BitVector bits)
{
- if (!bit.isBinary())
- throw new IllegalArgumentException("Cannot set ManualSwitch to the value of Bit " + bit);
- if (bit == output.getInputValue())
+ if (bits.length() != output.length())
+ throw new IllegalArgumentException("Incorrect bit vector length");
+ if (bits.equals(output.getInputValues()))
return;
- output.feedSignals(bit);
+ output.feedSignals(bits);
notifyObservers();
}
- public boolean isOn()
+ public boolean isFullOn()
{
- return output.getInputValue() == Bit.ONE;
+ return BitVector.of(Bit.ONE, output.length()).equals(output.getInputValues());
}
- public Bit getValue()
+ public BitVector getValues()
{
- return output.getInputValue();
+ return output.getInputValues();
}
@Override
{
if (dim.getValue().contains(e.getPoint()))
{
- dim.getKey().switchOff();
+ dim.getKey().switchFullOff();
repaint();
}
}
{
if (dim.getValue().contains(e.getPoint()))
{
- dim.getKey().switchOn();
+ dim.getKey().switchFullOn();
repaint();
}
}
switchMap.put(ms, r);
}
- g.setColor(ms.isOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE);
+ g.setColor(ms.isFullOn() ? Color.getHSBColor(.3f, .5f, 1f) : Color.WHITE);
g.fillRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
setBlack(g);
g.drawRect(x1, y1, x2 - x1 + 1, y2 - y1 + 1);
for (int i = 0; i < inputPinNames.size(); i++)
{
String pinName = inputPinNames.get(i);
- if (comp.getPin(pinName).logicWidth == 1)
- {
- GUIManualSwitch sw = new GUIManualSwitch(model);
- sw.moveTo(0, 20 * i);
- new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
- }
+ GUIManualSwitch sw = new GUIManualSwitch(model, comp.getPin(pinName).logicWidth);
+ sw.moveTo(0, 20 * i);
+ new GUIWire(model, comp.getPin(pinName), sw.getOutputPin());
}
for (int i = 0; i < outputPinNames.size(); i++)
{
String pinName = outputPinNames.get(i);
- if (comp.getPin(pinName).logicWidth == 1)
- {
- GUIBitDisplay bd = new GUIBitDisplay(model);
- bd.moveTo(200, 20 * i);
- new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
- }
+ GUIBitDisplay bd = new GUIBitDisplay(model, comp.getPin(pinName).logicWidth);
+ bd.moveTo(200, 20 * i);
+ new GUIWire(model, comp.getPin(pinName), bd.getInputPin());
}
}
}
\ No newline at end of file
comp.moveTo(240, 0);
- GUIManualSwitch enable = new GUIManualSwitch(model);
+ GUIManualSwitch enable = new GUIManualSwitch(model, 1);
WireCrossPoint wcp0 = new WireCrossPoint(model, 1);
GUINotGate not1 = new GUINotGate(model, 1);
GUINotGate not2 = new GUINotGate(model, 1);
WireCrossPoint wcp = new WireCrossPoint(model, 1);
GUIComponent d_ff = IndirectGUIComponentCreator.createComponent(model, "GUIdff");
- GUIManualSwitch sw = new GUIManualSwitch(model);
+ GUIManualSwitch sw = new GUIManualSwitch(model, 1);
tool.connect(last, wcp);
tool.connect(wcp, d_ff, "C");
{
double x = 300 + 75 * (i % 2);
double y = 10 * i - 2.5;
- GUIBitDisplay bd = new GUIBitDisplay(model);
+ GUIBitDisplay bd = new GUIBitDisplay(model, 1);
bd.moveTo(x, y);
tool.connect(bd.getInputPin(), comp, outputPinNames.get(i));
HashMap<String, GUIManualSwitch> idSwitchMap = new HashMap<>();
for (String id : inputPinNames)
{
- GUIManualSwitch sw = new GUIManualSwitch(viewModel);
+ GUIManualSwitch sw = new GUIManualSwitch(viewModel, 1);
new GUIWire(viewModel, am2901.getPin(id), sw.getOutputPin());
idSwitchMap.put(id, sw);
}
HashMap<String, GUIBitDisplay> idDisplayMap = new HashMap<>();
for (String id : outputPinNames)
{
- GUIBitDisplay bd = new GUIBitDisplay(viewModel);
+ GUIBitDisplay bd = new GUIBitDisplay(viewModel, 1);
// bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
idDisplayMap.put(id, bd);
public void setDest(Am2901_Dest dest)
{
var bits = of(dest.ordinal(), 3);
- I8.setToValueOf(bits.getLSBit(2));
- I7.setToValueOf(bits.getLSBit(1));
- I6.setToValueOf(bits.getLSBit(0));
+ I8.setState(bits.getLSBit(2));
+ I7.setState(bits.getLSBit(1));
+ I6.setState(bits.getLSBit(0));
}
@Override
public void setFunc(Am2901_Func func)
{
var bits = of(func.ordinal(), 3);
- I5.setToValueOf(bits.getLSBit(2));
- I4.setToValueOf(bits.getLSBit(1));
- I3.setToValueOf(bits.getLSBit(0));
+ I5.setState(bits.getLSBit(2));
+ I4.setState(bits.getLSBit(1));
+ I3.setState(bits.getLSBit(0));
}
@Override
public void setSrc(Am2901_Src src)
{
var bits = of(src.ordinal(), 3);
- I2.setToValueOf(bits.getLSBit(2));
- I1.setToValueOf(bits.getLSBit(1));
- I0.setToValueOf(bits.getLSBit(0));
+ I2.setState(bits.getLSBit(2));
+ I1.setState(bits.getLSBit(1));
+ I0.setState(bits.getLSBit(0));
}
@Override
public void setReg_A(String val_4_bit)
{
var bits = BitVector.parse(val_4_bit);
- A3.setToValueOf(bits.getLSBit(3));
- A2.setToValueOf(bits.getLSBit(2));
- A1.setToValueOf(bits.getLSBit(1));
- A0.setToValueOf(bits.getLSBit(0));
+ A3.setState(bits.getLSBit(3));
+ A2.setState(bits.getLSBit(2));
+ A1.setState(bits.getLSBit(1));
+ A0.setState(bits.getLSBit(0));
}
@Override
public void setReg_B(String val_4_bit)
{
var bits = BitVector.parse(val_4_bit);
- B3.setToValueOf(bits.getLSBit(3));
- B2.setToValueOf(bits.getLSBit(2));
- B1.setToValueOf(bits.getLSBit(1));
- B0.setToValueOf(bits.getLSBit(0));
+ B3.setState(bits.getLSBit(3));
+ B2.setState(bits.getLSBit(2));
+ B1.setState(bits.getLSBit(1));
+ B0.setState(bits.getLSBit(0));
}
@Override
public void setCarryIn(String val_1_bit)
{
- Cn.setToValueOf(Bit.parse(val_1_bit));
+ Cn.setState(Bit.parse(val_1_bit));
}
@Override
public void setD(String val_4_bit)
{
var bits = BitVector.parse(val_4_bit);
- D4.setToValueOf(bits.getLSBit(3));
- D3.setToValueOf(bits.getLSBit(2));
- D2.setToValueOf(bits.getLSBit(1));
- D1.setToValueOf(bits.getLSBit(0));
+ D4.setState(bits.getLSBit(3));
+ D3.setState(bits.getLSBit(2));
+ D2.setState(bits.getLSBit(1));
+ D1.setState(bits.getLSBit(0));
}
@Override
public void setQ_0(String val_1_bit)
{
- IQn.setToValueOf(Bit.parse(val_1_bit));
+ IQn.setState(Bit.parse(val_1_bit));
}
@Override
public void setQ_3(String val_1_bit)
{
- IQn_3.setToValueOf(Bit.parse(val_1_bit));
+ IQn_3.setState(Bit.parse(val_1_bit));
}
@Override
public void setRAM_0(String val_1_bit)
{
- IRAMn.setToValueOf(Bit.parse(val_1_bit));
+ IRAMn.setState(Bit.parse(val_1_bit));
}
@Override
public void setRAM_3(String val_1_bit)
{
- IRAMn_3.setToValueOf(Bit.parse(val_1_bit));
+ IRAMn_3.setState(Bit.parse(val_1_bit));
}
@Override
public void clockOn(boolean isClockOn)
{
- C.setState(isClockOn);
+ C.setState(isClockOn ? Bit.ONE : Bit.ZERO);
}
@Override
setSubmodelScale(.4);
setOutputPins("O0");
- GUIManualSwitch sw = new GUIManualSwitch(submodelModifiable);
- GUIBitDisplay bd = new GUIBitDisplay(submodelModifiable);
+ GUIManualSwitch sw = new GUIManualSwitch(submodelModifiable, 1);
+ GUIBitDisplay bd = new GUIBitDisplay(submodelModifiable, 1);
sw.moveTo(10, 5);
bd.moveTo(50, 5);
@SuppressWarnings("unused") // for GUIWires being created
public static void createRSLatchExample(ViewModelModifiable model)
{
- GUIManualSwitch rIn = new GUIManualSwitch(model);
+ GUIManualSwitch rIn = new GUIManualSwitch(model, 1);
rIn.moveTo(100, 100);
- GUIManualSwitch sIn = new GUIManualSwitch(model);
+ GUIManualSwitch sIn = new GUIManualSwitch(model, 1);
sIn.moveTo(100, 200);
GUIOrGate or1 = new GUIOrGate(model, 1);
private final LogicObserver logicObs;
private BitDisplay bitDisplay;
- public GUIBitDisplay(ViewModelModifiable model)
+ public GUIBitDisplay(ViewModelModifiable model, int logicWidth)
{
- this(model, null);
+ this(model, logicWidth, null);
}
- public GUIBitDisplay(ViewModelModifiable model, String name)
+ public GUIBitDisplay(ViewModelModifiable model, int logicWidth, String name)
{
super(model, name);
logicObs = (i) -> model.requestRedraw();
setSize(width, height);
- addPin(this.inputPin = new Pin(this, "", 1, 0, height / 2));
+ addPin(this.inputPin = new Pin(this, "", logicWidth, 0, height / 2));
}
@Override
static
{
ViewLogicModelAdapter.addComponentAdapter(new BitDisplayAdapter());
- IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(), (m, p, n) -> new GUIBitDisplay(m, n));
+ IndirectGUIComponentCreator.setComponentSupplier(GUIBitDisplay.class.getCanonicalName(),
+ (m, p, n) -> new GUIBitDisplay(m, p.getAsInt(), n));
}
}
\ No newline at end of file
import net.mograsim.logic.core.LogicObservable;
import net.mograsim.logic.core.LogicObserver;
import net.mograsim.logic.core.components.ManualSwitch;
-import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
import net.mograsim.logic.core.types.BitVectorFormatter;
import net.mograsim.logic.core.wires.Wire.ReadEnd;
import net.mograsim.logic.model.model.ViewModelModifiable;
private ManualSwitch logicSwitch;
private ReadEnd end;
- public GUIManualSwitch(ViewModelModifiable model)
+ public GUIManualSwitch(ViewModelModifiable model, int logicWidth)
{
- this(model, null);
+ this(model, logicWidth, null);
}
- public GUIManualSwitch(ViewModelModifiable model, String name)
+ public GUIManualSwitch(ViewModelModifiable model, int logicWidth, String name)
{
super(model, name);
logicObs = (i) -> model.requestRedraw();
setSize(width, height);
- addPin(this.outputPin = new Pin(this, "", 1, width, height / 2));
+ addPin(this.outputPin = new Pin(this, "", logicWidth, width, height / 2));
}
@Override
{
case "out":
if (logicSwitch != null)
- return logicSwitch.getValue();
+ return logicSwitch.getValues();
return null;
default:
return super.getHighLevelState(stateID);
{
case "out":
if (logicSwitch != null)
- logicSwitch.setToValueOf((Bit) newState);
+ logicSwitch.setState((BitVector) newState);
break;
default:
super.setHighLevelState(stateID, newState);
static
{
ViewLogicModelAdapter.addComponentAdapter(new ManualSwitchAdapter());
- IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(), (m, p, n) -> new GUIManualSwitch(m, n));
+ IndirectGUIComponentCreator.setComponentSupplier(GUIManualSwitch.class.getName(),
+ (m, p, n) -> new GUIManualSwitch(m, p.getAsInt(), n));
}
}
\ No newline at end of file
params.gateProcessTime = 50;
params.wireTravelTime = 10;
- GUIManualSwitch rIn = new GUIManualSwitch(model);
+ GUIManualSwitch rIn = new GUIManualSwitch(model, 1);
rIn.moveTo(10, 10);
- GUIManualSwitch sIn = new GUIManualSwitch(model);
+ GUIManualSwitch sIn = new GUIManualSwitch(model, 1);
sIn.moveTo(10, 70);
GUIOrGate or1 = new GUIOrGate(model, 1);
comp.moveTo(100, 0);
for (int i = 0; i < inputPinNames.size(); i++)
{
- GUIManualSwitch sw = new GUIManualSwitch(model);
+ GUIManualSwitch sw = new GUIManualSwitch(model, 1);
sw.moveTo(0, 20 * i);
new GUIWire(model, comp.getPin(inputPinNames.get(i)), sw.getOutputPin());
}
for (int i = 0; i < outputPinNames.size(); i++)
{
- GUIBitDisplay bd = new GUIBitDisplay(model);
+ GUIBitDisplay bd = new GUIBitDisplay(model, 1);
bd.moveTo(200, 20 * i);
new GUIWire(model, comp.getPin(outputPinNames.get(i)), bd.getInputPin());
}