From: Daniel Kirschten Date: Mon, 7 Oct 2019 00:05:49 +0000 (+0200) Subject: Fixed the same bug in some Am2900-specific ModelComponents X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=ee699a26797fe00e442e41d980d501807a7e092f;p=Mograsim.git Fixed the same bug in some Am2900-specific ModelComponents --- diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modeldff12.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modeldff12.java index 14500d5c..4abc5d56 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modeldff12.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modeldff12.java @@ -34,12 +34,7 @@ public class Modeldff12 extends SimpleRectangularHardcodedModelComponent @Override public Object recalculate(Object lastState, Map readEnds, Map readWriteEnds) { - Bit[] QC = (Bit[]) lastState; - if (QC == null) - { - QC = new Bit[13]; - Arrays.fill(QC, U); - } + Bit[] QC = castAndInitState(lastState); Bit CVal = readEnds.get("C").getValue(); @@ -55,29 +50,44 @@ public class Modeldff12 extends SimpleRectangularHardcodedModelComponent @Override protected Object getHighLevelState(Object state, String stateID) { + Bit[] QC = castAndInitState(state); + switch (stateID) { case "q": - return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12)); + return BitVector.of(Arrays.copyOfRange(QC, 0, 12)); default: - return super.getHighLevelState(state, stateID); + return super.getHighLevelState(QC, stateID); } } @Override protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState) { + Bit[] QC = castAndInitState(lastState); + switch (stateID) { case "q": BitVector newHighLevelStateCasted = (BitVector) newHighLevelState; if (newHighLevelStateCasted.length() != 12) throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length()); - System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 0, 12); - return lastState; + System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12); + return QC; default: - return super.setHighLevelState(lastState, stateID, newHighLevelState); + return super.setHighLevelState(QC, stateID, newHighLevelState); + } + } + + private static Bit[] castAndInitState(Object state) + { + Bit[] QC = (Bit[]) state; + if (QC == null) + { + QC = new Bit[13]; + Arrays.fill(QC, U); } + return QC; } static diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modelram5_12.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modelram5_12.java index 16c20776..36562414 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modelram5_12.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/Modelram5_12.java @@ -41,13 +41,8 @@ public class Modelram5_12 extends SimpleRectangularHardcodedModelComponent @Override public Object recalculate(Object lastState, Map readEnds, Map readWriteEnds) { - BitVector[] memC = (BitVector[]) lastState; - if (memC == null) - { - memC = new BitVector[6]; - Arrays.fill(memC, 0, 5, BitVector.of(U, 12)); - memC[5] = BitVector.of(U); - } + BitVector[] memC = castAndInitState(lastState); + BitVector CVal = readEnds.get("C").getValues(); BitVector oldC = memC[5]; // TODO is the timing right? @@ -91,15 +86,19 @@ public class Modelram5_12 extends SimpleRectangularHardcodedModelComponent @Override protected Object getHighLevelState(Object state, String stateID) { + BitVector[] memC = castAndInitState(state); + Matcher m = stateIDPattern.matcher(stateID); if (m.matches()) - return ((BitVector[]) state)[Integer.parseInt(m.group(1), 2)]; - return super.getHighLevelState(state, stateID); + return memC[Integer.parseInt(m.group(1), 2)]; + return super.getHighLevelState(memC, stateID); } @Override protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState) { + BitVector[] memC = castAndInitState(lastState); + Matcher m = stateIDPattern.matcher(stateID); if (m.matches()) { @@ -107,11 +106,22 @@ public class Modelram5_12 extends SimpleRectangularHardcodedModelComponent 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); + return super.setHighLevelState(memC, stateID, newHighLevelState); + } + + private static BitVector[] castAndInitState(Object state) + { + BitVector[] memC = (BitVector[]) state; + if (memC == null) + { + memC = new BitVector[6]; + Arrays.fill(memC, 0, 5, BitVector.of(U, 12)); + memC[5] = BitVector.of(U); + } + return memC; } static diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910RegCntr.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910RegCntr.java index 0c101bb4..7bdc703f 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910RegCntr.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910RegCntr.java @@ -37,12 +37,7 @@ public class ModelAm2910RegCntr extends SimpleRectangularHardcodedModelComponent @Override public Object recalculate(Object lastState, Map readEnds, Map readWriteEnds) { - Bit[] QC = (Bit[]) lastState; - if (QC == null) - { - QC = new Bit[13]; - Arrays.fill(QC, U); - } + Bit[] QC = castAndInitState(lastState); ReadEnd D = readEnds.get("D"); ReadEnd _RLD = readEnds.get("_RLD"); @@ -80,29 +75,44 @@ public class ModelAm2910RegCntr extends SimpleRectangularHardcodedModelComponent @Override protected Object getHighLevelState(Object state, String stateID) { + Bit[] QC = castAndInitState(state); + switch (stateID) { case "q": - return BitVector.of(Arrays.copyOfRange((Bit[]) state, 0, 12)); + return BitVector.of(Arrays.copyOfRange(QC, 0, 12)); default: - return super.getHighLevelState(state, stateID); + return super.getHighLevelState(QC, stateID); } } @Override protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState) { + Bit[] QC = castAndInitState(lastState); + switch (stateID) { case "q": BitVector newHighLevelStateCasted = (BitVector) newHighLevelState; if (newHighLevelStateCasted.length() != 12) throw new IllegalArgumentException("Expected BitVector of length 12, not " + newHighLevelStateCasted.length()); - System.arraycopy(newHighLevelStateCasted.getBits(), 0, lastState, 0, 12); - return lastState; + System.arraycopy(newHighLevelStateCasted.getBits(), 0, QC, 0, 12); + return QC; default: - return super.setHighLevelState(lastState, stateID, newHighLevelState); + return super.setHighLevelState(QC, stateID, newHighLevelState); + } + } + + private static Bit[] castAndInitState(Object lastState) + { + Bit[] QC = (Bit[]) lastState; + if (QC == null) + { + QC = new Bit[13]; + Arrays.fill(QC, U); } + return QC; } static diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910SP.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910SP.java index a99fe0d5..d9d1a60e 100644 --- a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910SP.java +++ b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/components/am2910/ModelAm2910SP.java @@ -37,13 +37,7 @@ public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent @Override public Object recalculate(Object lastState, Map readEnds, Map readWriteEnds) { - BitAndInt SPC = (BitAndInt) lastState; - if (SPC == null) - { - SPC = new BitAndInt(); - SPC.bit = U; - SPC.i = -2; - } + BitAndInt SPC = castAndInitState(lastState); int SP = SPC.i; Bit STKI0Val = readEnds.get("STKI0").getValue(); @@ -77,18 +71,22 @@ public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent @Override protected Object getHighLevelState(Object state, String stateID) { + BitAndInt SPC = castAndInitState(state); + switch (stateID) { case "q": - return getAsBitVector(((BitAndInt) state).i); + return getAsBitVector(SPC.i); default: - return super.getHighLevelState(state, stateID); + return super.getHighLevelState(SPC, stateID); } } @Override protected Object setHighLevelState(Object lastState, String stateID, Object newHighLevelState) { + BitAndInt SPC = castAndInitState(lastState); + switch (stateID) { case "q": @@ -102,11 +100,23 @@ public class ModelAm2910SP extends SimpleRectangularHardcodedModelComponent i = -1;// this makes setting to U impossible if (i > 5) throw new IllegalArgumentException("Given value not in range (0-5 incl.): " + i); - ((BitAndInt) lastState).i = i; - return lastState; + SPC.i = i; + return SPC; default: - return super.setHighLevelState(lastState, stateID, newHighLevelState); + return super.setHighLevelState(SPC, stateID, newHighLevelState); + } + } + + private static BitAndInt castAndInitState(Object lastState) + { + BitAndInt SPC = (BitAndInt) lastState; + if (SPC == null) + { + SPC = new BitAndInt(); + SPC.bit = U; + SPC.i = -2; } + return SPC; } private static class BitAndInt