import java.util.Arrays;
import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import net.mograsim.logic.core.types.Bit;
import net.mograsim.logic.core.types.BitVector;
import net.mograsim.logic.model.model.ViewModelModifiable;
import net.mograsim.logic.model.model.components.atomic.SimpleRectangularHardcodedGUIComponent;
import net.mograsim.logic.model.model.wires.Pin;
+import net.mograsim.logic.model.model.wires.PinUsage;
import net.mograsim.logic.model.serializing.IndirectGUIComponentCreator;
import net.mograsim.logic.model.snippets.symbolrenderers.PinNamesSymbolRenderer.PinNamesParams.Position;
{
super(model, name, "RAM\n5 x 12 Bit");
setSize(40, 40);
- addPin(new Pin(this, "A", 3, 10, 0), Usage.INPUT, Position.BOTTOM);
- addPin(new Pin(this, "B", 3, 30, 0), Usage.INPUT, Position.BOTTOM);
- addPin(new Pin(this, "WE", 1, 0, 5), Usage.INPUT, Position.RIGHT);
- addPin(new Pin(this, "C", 1, 0, 15), Usage.INPUT, Position.RIGHT);
- addPin(new Pin(this, "Y", 12, 0, 30), Usage.OUTPUT, Position.RIGHT);
- addPin(new Pin(this, "D", 12, 20, 40), Usage.INPUT, Position.TOP);
+ addPin(new Pin(this, "A", 3, PinUsage.INPUT, 10, 0), Position.BOTTOM);
+ addPin(new Pin(this, "B", 3, PinUsage.INPUT, 30, 0), Position.BOTTOM);
+ addPin(new Pin(this, "WE", 1, PinUsage.INPUT, 0, 5), Position.RIGHT);
+ addPin(new Pin(this, "C", 1, PinUsage.INPUT, 0, 15), Position.RIGHT);
+ addPin(new Pin(this, "Y", 12, PinUsage.OUTPUT, 0, 30), Position.RIGHT);
+ addPin(new Pin(this, "D", 12, PinUsage.INPUT, 20, 40), Position.TOP);
}
@Override
- protected Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
+ public Object recalculate(Object lastState, Map<String, ReadEnd> readEnds, Map<String, ReadWriteEnd> readWriteEnds)
{
BitVector[] memC = (BitVector[]) lastState;
if (memC == null)
for (int i = 0; i < 3; i++)
if (bits[i] == Z)
return -1;
- // TODO maybe this is the wrong way around
return (bits[0] == ONE ? 4 : 0) + (bits[1] == ONE ? 2 : 0) + (bits[2] == ONE ? 1 : 0);
}
+ Pattern stateIDPattern = Pattern.compile("c(0[10][10]|100)");
+
+ @Override
+ protected Object getHighLevelState(Object state, String stateID)
+ {
+ Matcher m = stateIDPattern.matcher(stateID);
+ if (m.matches())
+ return ((BitVector[]) state)[Integer.parseInt(m.group(1), 2)];
+ return super.getHighLevelState(state, stateID);
+ }
+
+ @Override
+ protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState)
+ {
+ Matcher m = stateIDPattern.matcher(stateID);
+ if (m.matches())
+ {
+ int addr = Integer.parseInt(m.group(1), 2);
+ BitVector newHighLevelStateCasted = (BitVector) newHighLevelState;
+ if (newHighLevelStateCasted.length() != 12)
+ throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length());
+ BitVector[] memC = (BitVector[]) lastState;
+ memC[addr] = newHighLevelStateCasted;
+ return memC;
+ }
+ return super.setHighLevelState(lastState, stateID, newHighLevelState);
+ }
+
static
{
IndirectGUIComponentCreator.setComponentSupplier(GUIram5_12.class.getCanonicalName(), (m, p, n) -> new GUIram5_12(m, n));