import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.LinkedList;
import java.util.Objects;
+import java.util.Queue;
import java.util.Set;
import java.util.TreeSet;
import net.mograsim.logic.core.types.Bit;
import net.mograsim.logic.core.types.BitVector;
import net.mograsim.logic.core.types.BitVector.BitVectorMutator;
-import net.mograsim.logic.ui.model.ModelVisitor;
import net.mograsim.logic.ui.model.ViewModel;
import net.mograsim.logic.ui.model.ViewModelModifiable;
import net.mograsim.logic.ui.model.components.GUIBitDisplay;
import net.mograsim.logic.ui.model.components.GUIComponent;
import net.mograsim.logic.ui.model.components.GUIManualSwitch;
-import net.mograsim.logic.ui.model.components.SimpleRectangularGUIGate;
-import net.mograsim.logic.ui.model.components.SimpleRectangularSubmodelComponent;
-import net.mograsim.logic.ui.model.components.SubmodelInterface;
+import net.mograsim.logic.ui.model.components.SubmodelComponent;
import net.mograsim.logic.ui.model.components.mi.nandbased.am2901.GUIAm2901;
import net.mograsim.logic.ui.model.wires.GUIWire;
-import net.mograsim.logic.ui.model.wires.WireCrossPoint;
import net.mograsim.logic.ui.modeladapter.LogicModelParameters;
import net.mograsim.logic.ui.modeladapter.ViewLogicModelAdapter;
private BitDisplay F_0, Cn_4, OVR, F3;
private BitDisplay ORAMn, ORAMn_3, OQn, OQn_3;
- private Set<GUIWire> allWires;
- private Set<GUIComponent> allComponents;
-
private Set<String> wireDebugChangeSet;
private boolean debugWires = false;
public int debugEventThreshold = 10_000;
timeline.executeNext();
if (!timeline.hasNext())
{
- System.out.println("run() took " + eventCounter + " events");
+// System.out.println("run() took " + eventCounter + " events");
return Result.SUCCESS;
}
}
for (String id : am2901.getOutputPinNames())
{
GUIBitDisplay bd = new GUIBitDisplay(viewModel);
+// bd.addRedrawListener(() -> System.out.println(id + " " + bd.getBitDisplay().getDisplayedValue()));
new GUIWire(viewModel, am2901.getPin(id), bd.getInputPin());
idDisplayMap.put(id, bd);
}
setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getManualSwitch());
for (var entry : idDisplayMap.entrySet())
setField(entry.getKey().replaceAll("\\+|=", "_"), entry.getValue().getBitDisplay());
- // Switch Clock off first
- C.switchOff();
// Debug code
- allWires = new HashSet<>();
- allComponents = new HashSet<>();
- ModelAccumulator accumulator = new ModelAccumulator();
- accumulator.visit(viewModel);
- allWires.forEach(w -> w.addRedrawListener(() ->
+ HashSet<GUIWire> wiresIncludingSubmodels = new HashSet<>();
+ Queue<ViewModel> modelsToIterate = new LinkedList<>();
+ modelsToIterate.add(viewModel);
+ while (modelsToIterate.size() > 0)
+ {
+ ViewModel model = modelsToIterate.poll();
+ wiresIncludingSubmodels.addAll(model.getWires());
+ for (GUIComponent comp : model.getComponents())
+ if (comp instanceof SubmodelComponent)
+ modelsToIterate.offer(((SubmodelComponent) comp).submodel);
+ }
+ wiresIncludingSubmodels.forEach(w -> w.addRedrawListener(() ->
{
if (debugWires)
{
- System.out.println(w);
wireDebugChangeSet.add(w.toString());
}
}));
@Override
public void setReg_A(String val_4_bit)
{
- var bits = BitVector.parse(val_4_bit);
- A3.setToValueOf(bits.getBit(0));
- A2.setToValueOf(bits.getBit(1));
- A1.setToValueOf(bits.getBit(2));
- A0.setToValueOf(bits.getBit(3));
+ // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+ var bits = BitVector.parse(new StringBuilder(val_4_bit).reverse().toString());
+ A3.setToValueOf(bits.getBit(3));
+ A2.setToValueOf(bits.getBit(2));
+ A1.setToValueOf(bits.getBit(1));
+ A0.setToValueOf(bits.getBit(0));
}
@Override
public void setReg_B(String val_4_bit)
{
- var bits = BitVector.parse(val_4_bit);
- B3.setToValueOf(bits.getBit(0));
- B2.setToValueOf(bits.getBit(1));
- B1.setToValueOf(bits.getBit(2));
- B0.setToValueOf(bits.getBit(3));
+ // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+ var bits = BitVector.parse(new StringBuilder(val_4_bit).reverse().toString());
+ B3.setToValueOf(bits.getBit(3));
+ B2.setToValueOf(bits.getBit(2));
+ B1.setToValueOf(bits.getBit(1));
+ B0.setToValueOf(bits.getBit(0));
}
@Override
@Override
public void setD(String val_4_bit)
{
- var bits = BitVector.parse(val_4_bit);
- D4.setToValueOf(bits.getBit(0));
- D3.setToValueOf(bits.getBit(1));
- D2.setToValueOf(bits.getBit(2));
- D1.setToValueOf(bits.getBit(3));
+ // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+ var bits = BitVector.parse(new StringBuilder(val_4_bit).reverse().toString());
+ D4.setToValueOf(bits.getBit(3));
+ D3.setToValueOf(bits.getBit(2));
+ D2.setToValueOf(bits.getBit(1));
+ D1.setToValueOf(bits.getBit(0));
}
@Override
}
@Override
- public void toogleClock()
+ public void clockOn(boolean isClockOn)
{
- C.toggle();
+ C.setState(isClockOn);
}
@Override
var y2 = Y3.getDisplayedValue();
var y1 = Y2.getDisplayedValue();
var y0 = Y1.getDisplayedValue();
- return y3.concat(y2).concat(y1).concat(y0).toString();
+ // reverse because BitVector.toString() returns the LSBit at the beginning and the MSBit at the end
+ return new StringBuilder(y0.concat(y1).concat(y2).concat(y3).toString()).reverse().toString();
}
private void setField(String name, Object value)
return mutator.toBitVector();
}
- class ModelAccumulator implements ModelVisitor
+ @Override
+ public void setDirectly(Register r, String val_4_bit)
{
- @Override
- public void visit(GUIWire w)
- {
- allWires.add(w);
- }
-
- @Override
- public void visit(SimpleRectangularGUIGate simpleRectangularGUIGate)
- {
- allComponents.add(simpleRectangularGUIGate);
- }
-
- @Override
- public void visit(SimpleRectangularSubmodelComponent simpleRectangularSubmodelComponent)
- {
- allComponents.add(simpleRectangularSubmodelComponent);
- simpleRectangularSubmodelComponent.getWires().forEach(w -> w.accept(this));
- simpleRectangularSubmodelComponent.getComponents().forEach(w -> w.accept(this));
- }
-
- @Override
- public void visit(WireCrossPoint wireCrossPoint)
- {
- // nothing
- }
-
- @Override
- public void visit(GUIBitDisplay guiBitDisplay)
- {
- allComponents.add(guiBitDisplay);
- }
-
- @Override
- public void visit(GUIManualSwitch guiManualSwitch)
- {
- allComponents.add(guiManualSwitch);
- }
+ // reverse because the BitVector.parse() expects the LSBit at the beginning and the MSBit at the end
+ am2901.setHighLevelState(regToStateID(r), BitVector.parse(new StringBuilder(val_4_bit).reverse().toString()));
+ }
- @Override
- public void visit(SubmodelInterface submodelInterface)
- {
- // nothing
- }
+ @Override
+ public String getDirectly(Register r)
+ {
+ // reverse because BitVector.toString() returns the LSBit at the beginning and the MSBit at the end
+ return new StringBuilder(am2901.getHighLevelState(regToStateID(r)).toString()).reverse().toString();
+ }
- @Override
- public void visit(ViewModel viewModel)
- {
- viewModel.getWires().forEach(w -> w.accept(this));
- viewModel.getComponents().forEach(w -> w.accept(this));
- }
+ private static String regToStateID(Register r)
+ {
+ if (r == Register.Q)
+ return "qreg.q";
+ return "regs.c" + r.toBitString() + ".q";
}
}