Am2900Machine now has registers
authorDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 30 Sep 2019 21:51:43 +0000 (23:51 +0200)
committerDaniel Kirschten <daniel.kirschten@gmx.de>
Mon, 30 Sep 2019 21:51:43 +0000 (23:51 +0200)
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900Machine.java
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900MachineDefinition.java
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/Am2900MicroInstructionDefinition.java
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/NumberedRegister.java [new file with mode: 0644]
plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/QRegister.java [new file with mode: 0644]
plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java [new file with mode: 0644]

index f705e74..759fda9 100644 (file)
@@ -6,6 +6,8 @@ import java.util.Set;
 import net.mograsim.logic.core.components.CoreClock;
 import net.mograsim.logic.core.timeline.Timeline;
 import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.logic.model.am2900.machine.registers.NumberedRegister;
+import net.mograsim.logic.model.am2900.machine.registers.QRegister;
 import net.mograsim.logic.model.model.LogicModel;
 import net.mograsim.logic.model.model.LogicModelModifiable;
 import net.mograsim.logic.model.model.components.ModelComponent;
@@ -81,7 +83,7 @@ public class Am2900Machine implements Machine
                MicroInstructionParameter[] defaultParams = muiDef.createDefaultInstruction().getParameters();
                defaultParams[19] = paramClassifications[19].parse("JZ");
                MicroInstruction jzMI = MicroInstruction.create(defaultParams);
-               logicModel.getComponentByName("Am2900").setHighLevelState("muir_2.q", jzMI.toBitVector());
+               am2900.setHighLevelState("muir_2.q", jzMI.toBitVector());
        }
 
        @Override
@@ -110,15 +112,37 @@ public class Am2900Machine implements Machine
        @Override
        public BitVector getRegister(Register r)
        {
-               // TODO Auto-generated method stub
-               return null;
+               String am2901CellSuffix;
+               if (r instanceof QRegister)
+                       am2901CellSuffix = "qreg.q";
+               else if (r instanceof NumberedRegister)
+                       am2901CellSuffix = "regs.c" + ((NumberedRegister) r).getIndexAsBitstring() + ".q";
+               else
+                       throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
+               BitVector result = BitVector.of();
+               for (int i = 0; i < 16; i += 4)
+               {
+                       String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
+                       result = result.concat((BitVector) am2900.getHighLevelState(hlsID));
+               }
+               return result;
        }
 
        @Override
        public void setRegister(Register r, BitVector value)
        {
-               // TODO Auto-generated method stub
-
+               String am2901CellSuffix;
+               if (r instanceof QRegister)
+                       am2901CellSuffix = "qreg.q";
+               else if (r instanceof NumberedRegister)
+                       am2901CellSuffix = "regs.c" + ((NumberedRegister) r).getIndexAsBitstring() + ".q";
+               else
+                       throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
+               for (int i = 0; i < 16; i += 4)
+               {
+                       String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
+                       am2900.setHighLevelState(hlsID, value.subVector(i, i + 4));
+               }
        }
 
        @Override
index 1ed0372..325f63f 100644 (file)
@@ -1,7 +1,11 @@
 package net.mograsim.logic.model.am2900.machine;
 
+import java.util.Collections;
+import java.util.HashSet;
 import java.util.Set;
 
+import net.mograsim.logic.model.am2900.machine.registers.NumberedRegister;
+import net.mograsim.logic.model.am2900.machine.registers.QRegister;
 import net.mograsim.logic.model.model.LogicModelModifiable;
 import net.mograsim.machine.ISASchema;
 import net.mograsim.machine.MachineDefinition;
@@ -13,6 +17,16 @@ public class Am2900MachineDefinition implements MachineDefinition
 {
        public static final String AM2900_MACHINE_ID = "Am2900";
 
+       public static final Set<Register> allRegisters;
+
+       static
+       {
+               Set<Register> allRegistersModifiable = new HashSet<>();
+               allRegistersModifiable.add(QRegister.instance);
+               allRegistersModifiable.addAll(NumberedRegister.instancesCorrectOrder);
+               allRegisters = Collections.unmodifiableSet(allRegistersModifiable);
+       }
+
        @Override
        public String getId()
        {
@@ -40,8 +54,7 @@ public class Am2900MachineDefinition implements MachineDefinition
        @Override
        public Set<Register> getRegisters()
        {
-               // TODO Auto-generated method stub
-               return null;
+               return allRegisters;
        }
 
        @Override
index 280b788..0b6a26f 100644 (file)
@@ -1,5 +1,6 @@
 package net.mograsim.logic.model.am2900.machine;
 
+import java.util.Arrays;
 import java.util.Optional;
 
 import net.mograsim.machine.mi.MicroInstructionDefinition;
@@ -89,7 +90,7 @@ public class Am2900MicroInstructionDefinition implements MicroInstructionDefinit
        @Override
        public ParameterClassification[] getParameterClassifications()
        {
-               return classes;
+               return Arrays.copyOf(classes, classes.length);
        }
 
        @Override
diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/NumberedRegister.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/NumberedRegister.java
new file mode 100644 (file)
index 0000000..7ca3d38
--- /dev/null
@@ -0,0 +1,49 @@
+package net.mograsim.logic.model.am2900.machine.registers;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+
+import net.mograsim.machine.StandardRegister;
+
+public class NumberedRegister extends StandardRegister
+{
+       public static final List<NumberedRegister> instancesCorrectOrder;
+
+       static
+       {
+               List<NumberedRegister> instancesCorrectOrderModifiable = new ArrayList<>();
+               for (int i = 0; i < 16; i++)
+                       instancesCorrectOrderModifiable.add(new NumberedRegister(i));
+               instancesCorrectOrder = Collections.unmodifiableList(instancesCorrectOrderModifiable);
+       }
+
+       private final int index;
+       private final String indexBitstring;
+
+       private NumberedRegister(int i)
+       {
+               super("R" + i, new HashSet<>(Arrays.asList(new String[] { "R" + i, "Register #" + i, "Register " + i })), 16, new HashMap<>());
+               this.index = i;
+
+               StringBuilder sb = new StringBuilder();
+               sb.append((index & 0b1000) != 0 ? '1' : '0');
+               sb.append((index & 0b0100) != 0 ? '1' : '0');
+               sb.append((index & 0b0010) != 0 ? '1' : '0');
+               sb.append((index & 0b0001) != 0 ? '1' : '0');
+               this.indexBitstring = sb.toString();
+       }
+
+       public int getIndex()
+       {
+               return index;
+       }
+
+       public String getIndexAsBitstring()
+       {
+               return indexBitstring;
+       }
+}
\ No newline at end of file
diff --git a/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/QRegister.java b/plugins/net.mograsim.logic.model.am2900/src/net/mograsim/logic/model/am2900/machine/registers/QRegister.java
new file mode 100644 (file)
index 0000000..b5d3540
--- /dev/null
@@ -0,0 +1,17 @@
+package net.mograsim.logic.model.am2900.machine.registers;
+
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+
+import net.mograsim.machine.StandardRegister;
+
+public class QRegister extends StandardRegister
+{
+       public static final QRegister instance = new QRegister();
+
+       private QRegister()
+       {
+               super("qreg", new HashSet<>(Arrays.asList(new String[] { "qreg", "Q register" })), 16, new HashMap<>());
+       }
+}
\ No newline at end of file
diff --git a/plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java b/plugins/net.mograsim.machine/src/net/mograsim/machine/StandardRegister.java
new file mode 100644 (file)
index 0000000..6fea551
--- /dev/null
@@ -0,0 +1,47 @@
+package net.mograsim.machine;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+public abstract class StandardRegister implements Register
+{
+       private final String id;
+       private final Set<String> names;
+       private final int width;
+       private final Map<String, Integer> widthForAliases;
+
+       public StandardRegister(String id, Set<String> names, int width, Map<String, Integer> widthForAliases)
+       {
+               this.id = id;
+               this.names = Collections.unmodifiableSet(new HashSet<>(names));
+               this.width = width;
+               this.widthForAliases = Collections.unmodifiableMap(new HashMap<>(widthForAliases));
+       }
+
+       @Override
+       public String id()
+       {
+               return id;
+       }
+
+       @Override
+       public Set<String> names()
+       {
+               return names;
+       }
+
+       @Override
+       public int getWidth()
+       {
+               return width;
+       }
+
+       @Override
+       public int getWidth(String name)
+       {
+               return widthForAliases.getOrDefault(name, width);
+       }
+}
\ No newline at end of file