1 package net.mograsim.logic.model.am2900.components;
3 import static net.mograsim.logic.core.types.Bit.ONE;
4 import static net.mograsim.logic.core.types.Bit.U;
5 import static net.mograsim.logic.core.types.Bit.X;
6 import static net.mograsim.logic.core.types.Bit.Z;
7 import static net.mograsim.logic.core.types.Bit.ZERO;
9 import java.util.Arrays;
12 import net.mograsim.logic.core.types.Bit;
13 import net.mograsim.logic.core.types.BitVector;
14 import net.mograsim.logic.core.wires.Wire.ReadEnd;
15 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
16 import net.mograsim.logic.model.model.ViewModelModifiable;
17 import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
18 import net.mograsim.logic.model.model.wires.Pin;
19 import net.mograsim.logic.model.model.wires.PinUsage;
20 import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
21 import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
23 public class GUIram5_12 extends SimpleRectangularHardcodedGUIComponent
25 public GUIram5_12(ViewModelModifiable model, String name)
27 super(model, name, "RAM\n5 x 12 Bit");
29 addPin(new Pin(this, "A", 3, PinUsage.INPUT, 10, 0), Position.BOTTOM);
30 addPin(new Pin(this, "B", 3, PinUsage.INPUT, 30, 0), Position.BOTTOM);
31 addPin(new Pin(this, "WE", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
32 addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
33 addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 0, 30), Position.RIGHT);
34 addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 40), Position.TOP);
38 public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
40 BitVector[] memC = (BitVector[]) lastState;
43 memC = new BitVector[6];
44 Arrays.fill(memC, 0, 5, BitVector.of(U, 12));
45 memC[5] = BitVector.of(U);
47 BitVector CVal = readEnds.get("C").getValues();
48 BitVector oldC = memC[5];
49 // TODO is the timing right?
50 if (oldC.getLSBit(0) == ZERO && CVal.getLSBit(0) == ONE && readEnds.get("WE").getValue() == ONE)
52 int BInt = getAsInt(readEnds.get("B").getValues());
53 if (BInt == -1 || BInt > 4)
54 Arrays.fill(memC, BitVector.of(X, 12));
56 Arrays.fill(memC, BitVector.of(U, 12));
58 memC[BInt] = readEnds.get("D").getValues();
60 int AInt = getAsInt(readEnds.get("A").getValues());
61 BitVector YVal = AInt == -1 || AInt > 4 ? BitVector.of(X, 12) : AInt == -2 ? BitVector.of(U, 12) : memC[AInt];
62 readWriteEnds.get("Y").feedSignals(YVal);
68 * -1 means X, -2 means U
70 private static int getAsInt(BitVector vect)
72 Bit[] bits = vect.getBits();
73 for (int i = 0; i < 3; i++)
76 for (int i = 0; i < 3; i++)
79 for (int i = 0; i < 3; i++)
82 // TODO maybe this is the wrong way around
83 return (bits[0] == ONE ? 4 : 0) + (bits[1] == ONE ? 2 : 0) + (bits[2] == ONE ? 1 : 0);
88 IndirectGUIComponentCreator.setComponentSupplier(GUIram5_12.class.getCanonicalName(), (m, p, n) -> new GUIram5_12(m, n));