Added machine plugin project to repository
authorChristian Femers <femers@in.tum.de>
Sun, 18 Aug 2019 12:33:12 +0000 (14:33 +0200)
committerChristian Femers <femers@in.tum.de>
Sun, 18 Aug 2019 12:43:17 +0000 (14:43 +0200)
29 files changed:
net.mograsim.machine/.classpath [new file with mode: 0644]
net.mograsim.machine/.gitignore [new file with mode: 0644]
net.mograsim.machine/.project [new file with mode: 0644]
net.mograsim.machine/.settings/org.eclipse.jdt.core.prefs [new file with mode: 0644]
net.mograsim.machine/META-INF/MANIFEST.MF [new file with mode: 0644]
net.mograsim.machine/build.properties [new file with mode: 0644]
net.mograsim.machine/plugin.xml [new file with mode: 0644]
net.mograsim.machine/schema/net.mograsim.machinedefinition.exsd [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/AddressingScema.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/ISA.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/ISASchema.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/Machine.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/MainMemory.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/MainMemoryDefinition.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/MicroprogramMemory.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/Register.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmElement.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmFloatOperand.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmInstruction.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmIntegerOperand.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmLabel.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperand.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperands.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperation.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/NumericOperand.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/types/AsmException.java [new file with mode: 0644]
net.mograsim.machine/src/net/mograsim/machine/isa/types/AsmNumberFormatException.java [new file with mode: 0644]

diff --git a/net.mograsim.machine/.classpath b/net.mograsim.machine/.classpath
new file mode 100644 (file)
index 0000000..ca3785c
--- /dev/null
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<classpath>\r
+       <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11"/>\r
+       <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/>\r
+       <classpathentry kind="src" path="src"/>\r
+       <classpathentry kind="output" path="bin"/>\r
+</classpath>\r
diff --git a/net.mograsim.machine/.gitignore b/net.mograsim.machine/.gitignore
new file mode 100644 (file)
index 0000000..ae3c172
--- /dev/null
@@ -0,0 +1 @@
+/bin/
diff --git a/net.mograsim.machine/.project b/net.mograsim.machine/.project
new file mode 100644 (file)
index 0000000..10f4f22
--- /dev/null
@@ -0,0 +1,28 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<projectDescription>\r
+       <name>net.mograsim.machine</name>\r
+       <comment></comment>\r
+       <projects>\r
+       </projects>\r
+       <buildSpec>\r
+               <buildCommand>\r
+                       <name>org.eclipse.jdt.core.javabuilder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+               <buildCommand>\r
+                       <name>org.eclipse.pde.ManifestBuilder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+               <buildCommand>\r
+                       <name>org.eclipse.pde.SchemaBuilder</name>\r
+                       <arguments>\r
+                       </arguments>\r
+               </buildCommand>\r
+       </buildSpec>\r
+       <natures>\r
+               <nature>org.eclipse.pde.PluginNature</nature>\r
+               <nature>org.eclipse.jdt.core.javanature</nature>\r
+       </natures>\r
+</projectDescription>\r
diff --git a/net.mograsim.machine/.settings/org.eclipse.jdt.core.prefs b/net.mograsim.machine/.settings/org.eclipse.jdt.core.prefs
new file mode 100644 (file)
index 0000000..dbacf24
--- /dev/null
@@ -0,0 +1,7 @@
+eclipse.preferences.version=1\r
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=11\r
+org.eclipse.jdt.core.compiler.compliance=11\r
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error\r
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error\r
+org.eclipse.jdt.core.compiler.release=enabled\r
+org.eclipse.jdt.core.compiler.source=11\r
diff --git a/net.mograsim.machine/META-INF/MANIFEST.MF b/net.mograsim.machine/META-INF/MANIFEST.MF
new file mode 100644 (file)
index 0000000..3be14f8
--- /dev/null
@@ -0,0 +1,11 @@
+Manifest-Version: 1.0
+Bundle-ManifestVersion: 2
+Bundle-Name: Mograsim Machine Definition
+Bundle-SymbolicName: net.mograsim.machine;singleton:=true
+Bundle-Version: 0.1.0.qualifier
+Bundle-Vendor: Mograsim Team
+Automatic-Module-Name: net.mograsim.machine
+Bundle-RequiredExecutionEnvironment: JavaSE-11
+Require-Bundle: net.mograsim.logic.core;bundle-version="0.1.0",
+ net.mograsim.logic.model;bundle-version="0.1.0"
+Export-Package: net.mograsim.machine.isa
diff --git a/net.mograsim.machine/build.properties b/net.mograsim.machine/build.properties
new file mode 100644 (file)
index 0000000..6f20375
--- /dev/null
@@ -0,0 +1,5 @@
+source.. = src/\r
+output.. = bin/\r
+bin.includes = META-INF/,\\r
+               .,\\r
+               plugin.xml\r
diff --git a/net.mograsim.machine/plugin.xml b/net.mograsim.machine/plugin.xml
new file mode 100644 (file)
index 0000000..452a4de
--- /dev/null
@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="UTF-8"?>\r
+<?eclipse version="3.4"?>\r
+<plugin>\r
+   <extension-point id="net.mograsim.machinedefinition" name="Mograsim Machine Definition" schema="schema/net.mograsim.machinedefinition.exsd"/>\r
+\r
+</plugin>\r
diff --git a/net.mograsim.machine/schema/net.mograsim.machinedefinition.exsd b/net.mograsim.machine/schema/net.mograsim.machinedefinition.exsd
new file mode 100644 (file)
index 0000000..93c1bf5
--- /dev/null
@@ -0,0 +1,109 @@
+<?xml version='1.0' encoding='UTF-8'?>\r
+<!-- Schema file written by PDE -->\r
+<schema targetNamespace="net.mograsim.machine" xmlns="http://www.w3.org/2001/XMLSchema">\r
+<annotation>\r
+      <appinfo>\r
+         <meta.schema plugin="net.mograsim.machine" id="net.mograsim.machinedefinition" name="Mograsim Machine Definition"/>\r
+      </appinfo>\r
+      <documentation>\r
+         Mograsim extension for defining a machine\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <element name="extension">\r
+      <annotation>\r
+         <appinfo>\r
+            <meta.element />\r
+         </appinfo>\r
+      </annotation>\r
+      <complexType>\r
+         <choice minOccurs="1" maxOccurs="unbounded">\r
+            <element ref="MachineDefinitionImplementation"/>\r
+         </choice>\r
+         <attribute name="point" type="string" use="required">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+            </annotation>\r
+         </attribute>\r
+         <attribute name="id" type="string">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+            </annotation>\r
+         </attribute>\r
+         <attribute name="name" type="string">\r
+            <annotation>\r
+               <documentation>\r
+                  \r
+               </documentation>\r
+               <appinfo>\r
+                  <meta.attribute translatable="true"/>\r
+               </appinfo>\r
+            </annotation>\r
+         </attribute>\r
+      </complexType>\r
+   </element>\r
+\r
+   <element name="MachineDefinitionImplementation">\r
+      <complexType>\r
+         <attribute name="name" type="string" use="required">\r
+            <annotation>\r
+               <documentation>\r
+                  The machines name\r
+               </documentation>\r
+            </annotation>\r
+         </attribute>\r
+         <attribute name="class" type="string" use="required">\r
+            <annotation>\r
+               <documentation>\r
+                  A Class that implements the Mograsim Machine Definition\r
+               </documentation>\r
+               <appinfo>\r
+                  <meta.attribute kind="java" basedOn=":net.mograsim.machine.MachineDefinition"/>\r
+               </appinfo>\r
+            </annotation>\r
+         </attribute>\r
+      </complexType>\r
+   </element>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="since"/>\r
+      </appinfo>\r
+      <documentation>\r
+         0.1.0\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="examples"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter extension point usage example here.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="apiinfo"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter API information here.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+   <annotation>\r
+      <appinfo>\r
+         <meta.section type="implementation"/>\r
+      </appinfo>\r
+      <documentation>\r
+         [Enter information about supplied implementation of this extension point.]\r
+      </documentation>\r
+   </annotation>\r
+\r
+\r
+</schema>\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/AddressingScema.java b/net.mograsim.machine/src/net/mograsim/machine/AddressingScema.java
new file mode 100644 (file)
index 0000000..a5480e8
--- /dev/null
@@ -0,0 +1,5 @@
+package net.mograsim.machine;\r
+\r
+public interface AddressingScema {\r
+       \r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/ISA.java b/net.mograsim.machine/src/net/mograsim/machine/ISA.java
new file mode 100644 (file)
index 0000000..4ac29bf
--- /dev/null
@@ -0,0 +1,4 @@
+package net.mograsim.machine;\r
+\r
+public interface ISA {\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/ISASchema.java b/net.mograsim.machine/src/net/mograsim/machine/ISASchema.java
new file mode 100644 (file)
index 0000000..c03f665
--- /dev/null
@@ -0,0 +1,11 @@
+package net.mograsim.machine;\r
+\r
+import java.util.Set;\r
+\r
+import net.mograsim.machine.isa.AsmOperand;\r
+\r
+public interface ISASchema {\r
+       Set<AsmOperand> getSupportedOperands();\r
+       \r
+       AddressingScema getAddressingSchema();\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/Machine.java b/net.mograsim.machine/src/net/mograsim/machine/Machine.java
new file mode 100644 (file)
index 0000000..d475f8b
--- /dev/null
@@ -0,0 +1,19 @@
+package net.mograsim.machine;\r
+\r
+import net.mograsim.logic.core.components.Clock;\r
+import net.mograsim.logic.core.types.BitVector;\r
+import net.mograsim.logic.model.model.ViewModel;\r
+\r
+public interface Machine {\r
+       MachineDefinition getDefinition();\r
+       \r
+       void reset();\r
+       \r
+       ViewModel getModel();\r
+       \r
+       Clock getClock();\r
+       \r
+       BitVector getRegister(Register r);\r
+       \r
+       void setRegister(Register r, BitVector value);\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java b/net.mograsim.machine/src/net/mograsim/machine/MachineDefinition.java
new file mode 100644 (file)
index 0000000..7e66975
--- /dev/null
@@ -0,0 +1,50 @@
+package net.mograsim.machine;\r
+\r
+import java.util.Set;\r
+\r
+public interface MachineDefinition {\r
+\r
+       /**\r
+        * Creates a new instance of the machine\r
+        * \r
+        * @return a new object of an {@link Machine} implementation.\r
+        * @author Christian Femers\r
+        */\r
+       Machine createNew();\r
+\r
+       /**\r
+        * Returns the schema that all possible ISAs (Instruction Set Architecture) must\r
+        * be based on.\r
+        * \r
+        * @return an {@link ISASchema} implementation fitting the machine\r
+        * @author Christian Femers\r
+        */\r
+       ISASchema getISASchema();\r
+\r
+       /**\r
+        * Returns a set of all {@link Register}s present in the machine.\r
+        * \r
+        * @return all registers present in the machine.\r
+        * @author Christian Femers\r
+        */\r
+       Set<Register> getRegisters();\r
+\r
+       /**\r
+        * The number of bits used by the machine to address main memory. Note that this\r
+        * should be the number of bits used in the CPU, not a possibly different one\r
+        * used by the bus system.\r
+        * \r
+        * @return the number of bits used by the CPU\r
+        * @see MainMemoryDefinition#getMemoryAddressBits()\r
+        * @author Christian Femers\r
+        */\r
+       int getAddressBits();\r
+\r
+       /**\r
+        * Returns the definition of the machines main memory.\r
+        * \r
+        * @return the {@link MainMemoryDefinition} that defines the main memory.\r
+        * @author Christian Femers\r
+        */\r
+       MainMemoryDefinition getMainMemoryDefinition();\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/MainMemory.java b/net.mograsim.machine/src/net/mograsim/machine/MainMemory.java
new file mode 100644 (file)
index 0000000..c055965
--- /dev/null
@@ -0,0 +1,5 @@
+package net.mograsim.machine;\r
+\r
+public interface MainMemory {\r
+       \r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/MainMemoryDefinition.java b/net.mograsim.machine/src/net/mograsim/machine/MainMemoryDefinition.java
new file mode 100644 (file)
index 0000000..a132d25
--- /dev/null
@@ -0,0 +1,45 @@
+package net.mograsim.machine;\r
+\r
+/**\r
+ * This interface provides a means to get information about the machines memory\r
+ * architecture. It is not bound to any actual memory.\r
+ *\r
+ * @author Christian Femers\r
+ *\r
+ */\r
+public interface MainMemoryDefinition {\r
+\r
+       /**\r
+        * The number of bits that the main memory uses to address cells. Note that this\r
+        * does not need to equal {@link MachineDefinition#getAddressBits()}.\r
+        * \r
+        * @return the number of bits used to address a memory cell\r
+        * @author Christian Femers\r
+        */\r
+       int getMemoryAddressBits();\r
+\r
+       /**\r
+        * The width in bits of an addressable memory cell/unit. This is often 8 (= one\r
+        * byte). The actual cells/lines of the memory may be a lot larger.\r
+        * \r
+        * @return the addressable unit width in bits\r
+        * @author Christian Femers\r
+        */\r
+       int getCellWidth();\r
+\r
+       /**\r
+        * The minimal address possible to address/use. This is usually 0.\r
+        * \r
+        * @return the minimal possible address.\r
+        * @author Christian Femers\r
+        */\r
+       long getMinimalAddress();\r
+\r
+       /**\r
+        * The maximal address possible to address/use.\r
+        * \r
+        * @return the maximal possible address as <b>unsigned long</b>\r
+        * @author Christian Femers\r
+        */\r
+       long getMaximalAddress();\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java b/net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java
new file mode 100644 (file)
index 0000000..47ead9b
--- /dev/null
@@ -0,0 +1,5 @@
+package net.mograsim.machine;\r
+\r
+public interface MicroInstruction {\r
+\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/MicroprogramMemory.java b/net.mograsim.machine/src/net/mograsim/machine/MicroprogramMemory.java
new file mode 100644 (file)
index 0000000..20048cd
--- /dev/null
@@ -0,0 +1,5 @@
+package net.mograsim.machine;\r
+\r
+public interface MicroprogramMemory {\r
+\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/Register.java b/net.mograsim.machine/src/net/mograsim/machine/Register.java
new file mode 100644 (file)
index 0000000..04e8c84
--- /dev/null
@@ -0,0 +1,58 @@
+package net.mograsim.machine;\r
+\r
+import java.util.Set;\r
+\r
+/**\r
+ * A register in a machine is defined by this interface. A hardware register may\r
+ * have {@link Register#names() named sub-registers}.\r
+ *\r
+ *\r
+ * @author Christian Femers\r
+ *\r
+ */\r
+public interface Register {\r
+       /**\r
+        * The unique identifier of the register. This does not have to be the display\r
+        * name or name in the assembly language.\r
+        * \r
+        * @return the registers id as case sensitive String\r
+        * @author Christian Femers\r
+        */\r
+       String id();\r
+\r
+       /**\r
+        * The name(s) of this register. This is the displayed name and the name used in\r
+        * the assembly language. All names of all registers must be case-insensitive\r
+        * unique. A register can have multiple names if these names address different\r
+        * regions (but still have a common hardware structure), e.g. <code>EAX</code>,\r
+        * <code>AX</code>, <code>AL</code>, <code>AH</code>.\r
+        * \r
+        * @return all the names of regions addressing this register, must be\r
+        *         case-insensitive.\r
+        * @author Christian Femers\r
+        */\r
+       Set<String> names();\r
+\r
+       /**\r
+        * Returns the complete width in bits of the underlying hardware structure the\r
+        * register and possible sub-registers are part of.\r
+        * \r
+        * @param name the name of the register\r
+        * @return the width of the (sub-)register in bits.\r
+        * \r
+        * @see #names()\r
+        * @author Christian Femers\r
+        */\r
+       int getWidth();\r
+\r
+       /**\r
+        * Returns the width in bits of the register or a named part of it.\r
+        * \r
+        * @param name the name of the register\r
+        * @return the width of the (sub-)register in bits.\r
+        * \r
+        * @see #names()\r
+        * @author Christian Femers\r
+        */\r
+       int getWidth(String name);\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmElement.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmElement.java
new file mode 100644 (file)
index 0000000..0d6d194
--- /dev/null
@@ -0,0 +1,6 @@
+package net.mograsim.machine.isa;\r
+\r
+public interface AsmElement\r
+{\r
+       // only marker at the moment\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmFloatOperand.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmFloatOperand.java
new file mode 100644 (file)
index 0000000..43e9cb4
--- /dev/null
@@ -0,0 +1,98 @@
+package net.mograsim.machine.isa;\r
+\r
+import java.math.BigDecimal;\r
+import java.math.BigInteger;\r
+\r
+import net.mograsim.logic.core.types.Bit;\r
+import net.mograsim.logic.core.types.BitVector;\r
+import net.mograsim.logic.core.types.BitVector.BitVectorMutator;\r
+import net.mograsim.machine.isa.types.AsmNumberFormatException;\r
+\r
+public class AsmFloatOperand implements AsmOperand\r
+{\r
+       private final int size;\r
+       private final int mantissa;\r
+       private final int exponent;\r
+\r
+       public AsmFloatOperand(int size)\r
+       {\r
+               this.size = size;\r
+               switch (size)\r
+               {\r
+               case 8:\r
+                       exponent = 4;\r
+                       mantissa = 3;\r
+                       break;\r
+               case 16:\r
+                       exponent = 5;\r
+                       mantissa = 10;\r
+                       break;\r
+               case 32:\r
+                       exponent = 8;\r
+                       mantissa = 23;\r
+                       break;\r
+               case 64:\r
+                       exponent = 11;\r
+                       mantissa = 52;\r
+                       break;\r
+               case 128:\r
+                       exponent = 15;\r
+                       mantissa = 112;\r
+                       break;\r
+               default:\r
+                       if (size > 128 && size % 32 == 0)\r
+                       {\r
+                               exponent = (int) Math.round(Math.log(size) / Math.log(2)) - 13;\r
+                               mantissa = size - exponent - 1;\r
+                       } else\r
+                       {\r
+                               throw new IllegalArgumentException("Illegal floating point size: " + size);\r
+                       }\r
+               }\r
+       }\r
+\r
+       public AsmFloatOperand(int exponent, int mantissa)\r
+       {\r
+               if (exponent < 1 || mantissa < 1)\r
+                       throw new IllegalArgumentException("illegal floating point specification: e=" + exponent + ", m=" + mantissa);\r
+               this.exponent = exponent;\r
+               this.mantissa = mantissa;\r
+               this.size = exponent + mantissa + 1;\r
+       }\r
+\r
+       @Override\r
+       public int getSize()\r
+       {\r
+               return size;\r
+       }\r
+\r
+       @Override\r
+       public BitVector parse(String s) throws AsmNumberFormatException\r
+       {\r
+               String cleaned = s.replace("_", "").toLowerCase();\r
+               int len = cleaned.length();\r
+               BigInteger res;\r
+               try\r
+               {\r
+                       return bigDecToBitVector(new BigDecimal(cleaned));\r
+               }\r
+               catch (NumberFormatException e)\r
+               {\r
+                       throw new AsmNumberFormatException(e, "Error parsing %s: no valid float format", s);\r
+               }\r
+       }\r
+\r
+       BitVector bigDecToBitVector(BigDecimal bi) throws AsmNumberFormatException\r
+       {\r
+               BigInteger raw = bi.unscaledValue();\r
+               int mantLen = raw.bitLength();\r
+//             bi.\r
+               int bitLength = raw.bitLength() - (bi.signum() - 1) / 2;\r
+               if (bitLength > size)\r
+                       throw new AsmNumberFormatException("Error parsing %s: bit count %d exceeds size %d", bi, bitLength, size);\r
+               BitVectorMutator bvm = BitVectorMutator.ofLength(size);\r
+               for (int i = 0; i < size; i++)\r
+                       bvm.setLSBit(i, Bit.of(raw.testBit(i)));\r
+               return bvm.toBitVector();\r
+       }\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmInstruction.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmInstruction.java
new file mode 100644 (file)
index 0000000..ea32e6c
--- /dev/null
@@ -0,0 +1,39 @@
+package net.mograsim.machine.isa;\r
+\r
+import java.util.Objects;\r
+\r
+public final class AsmInstruction implements AsmElement\r
+{\r
+       private final AsmOperation operation;\r
+       private final AsmOperands operands;\r
+\r
+       public AsmInstruction(AsmOperation operation, AsmOperands operands)\r
+       {\r
+               this.operation = Objects.requireNonNull(operation);\r
+               this.operands = Objects.requireNonNull(operands);\r
+       }\r
+\r
+       @Override\r
+       public int hashCode()\r
+       {\r
+               return Objects.hash(operands, operation);\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj)\r
+       {\r
+               if (this == obj)\r
+                       return true;\r
+               if (!(obj instanceof AsmInstruction))\r
+                       return false;\r
+               AsmInstruction other = (AsmInstruction) obj;\r
+               return Objects.equals(operands, other.operands) && Objects.equals(operation, other.operation);\r
+       }\r
+\r
+       @Override\r
+       public String toString()\r
+       {\r
+               return String.format("%s %s", operation, operands).trim();\r
+       }\r
+\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmIntegerOperand.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmIntegerOperand.java
new file mode 100644 (file)
index 0000000..cc9897c
--- /dev/null
@@ -0,0 +1,95 @@
+package net.mograsim.machine.isa;\r
+\r
+import java.math.BigInteger;\r
+\r
+import net.mograsim.logic.core.types.Bit;\r
+import net.mograsim.logic.core.types.BitVector;\r
+import net.mograsim.logic.core.types.BitVector.BitVectorMutator;\r
+import net.mograsim.machine.isa.types.AsmNumberFormatException;\r
+\r
+public class AsmIntegerOperand implements AsmOperand\r
+{\r
+       private final int size;\r
+\r
+       public AsmIntegerOperand(int size)\r
+       {\r
+               this.size = size;\r
+       }\r
+\r
+       @Override\r
+       public int getSize()\r
+       {\r
+               return size;\r
+       }\r
+\r
+       @Override\r
+       public BitVector parse(String s) throws AsmNumberFormatException\r
+       {\r
+               String cleaned = s.replace("_", "").toLowerCase();\r
+               int len = cleaned.length();\r
+               BigInteger res;\r
+               try\r
+               {\r
+                       if (cleaned.startsWith("0x"))\r
+                               res = new BigInteger(cleaned.substring(2), 16);\r
+                       else if (cleaned.endsWith("h"))\r
+                               res = new BigInteger(cleaned.substring(0, len - 1), 16);\r
+                       else if (cleaned.startsWith("0b"))\r
+                               res = new BigInteger(cleaned.substring(2), 2);\r
+                       else if (cleaned.endsWith("b"))\r
+                               res = new BigInteger(cleaned.substring(2), 2);\r
+                       else if (cleaned.endsWith("q"))\r
+                               res = new BigInteger(cleaned.substring(0, len - 1), 8);\r
+                       else\r
+                               res = new BigInteger(cleaned);\r
+               }\r
+               catch (NumberFormatException e)\r
+               {\r
+                       throw new AsmNumberFormatException(e, "Error parsing %s: no valid integer format", s);\r
+               }\r
+               return bigIntToBitVector(res);\r
+       }\r
+\r
+       BitVector bigIntToBitVector(BigInteger bi) throws AsmNumberFormatException\r
+       {\r
+               int bitLength = bi.bitLength() - (bi.signum() - 1) / 2;\r
+               if (bitLength > size)\r
+                       throw new AsmNumberFormatException("Error parsing %s: bit count %d exceeds size %d", bi, bitLength, size);\r
+               BitVectorMutator bvm = BitVectorMutator.ofLength(size);\r
+               for (int i = 0; i < size; i++)\r
+                       bvm.setLSBit(i, Bit.of(bi.testBit(i)));\r
+               return bvm.toBitVector();\r
+       }\r
+\r
+       public static void main(String[] args) throws AsmNumberFormatException\r
+       {\r
+               AsmIntegerOperand aio = new AsmIntegerOperand(4);\r
+               System.out.println(aio.parse("0"));\r
+               System.out.println(aio.parse("1"));\r
+               System.out.println(aio.parse("2"));\r
+               System.out.println(aio.parse("3"));\r
+               System.out.println(aio.parse("4"));\r
+               System.out.println(aio.parse("5"));\r
+               System.out.println(aio.parse("6"));\r
+               System.out.println(aio.parse("7"));\r
+               System.out.println(aio.parse("8"));\r
+               System.out.println(aio.parse("9"));\r
+               System.out.println(aio.parse("10"));\r
+               System.out.println(aio.parse("11"));\r
+               System.out.println(aio.parse("12"));\r
+               System.out.println(aio.parse("13"));\r
+               System.out.println(aio.parse("14"));\r
+               System.out.println(aio.parse("15"));\r
+//             System.out.println(aio.parse("16"));\r
+               System.out.println(aio.parse("-0"));\r
+               System.out.println(aio.parse("-1"));\r
+               System.out.println(aio.parse("-2"));\r
+               System.out.println(aio.parse("-3"));\r
+               System.out.println(aio.parse("-4"));\r
+               System.out.println(aio.parse("-5"));\r
+               System.out.println(aio.parse("-6"));\r
+               System.out.println(aio.parse("-7"));\r
+               System.out.println(aio.parse("-8"));\r
+//             System.out.println(aio.parse("-9"));\r
+       }\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmLabel.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmLabel.java
new file mode 100644 (file)
index 0000000..96db70b
--- /dev/null
@@ -0,0 +1,49 @@
+package net.mograsim.machine.isa;\r
+\r
+import java.util.Objects;\r
+\r
+public final class AsmLabel implements AsmElement\r
+{\r
+       private final String name;\r
+       private AsmInstruction inst;\r
+\r
+       public AsmLabel(String name)\r
+       {\r
+               this.name = Objects.requireNonNull(name);\r
+       }\r
+\r
+       public String getName()\r
+       {\r
+               return name;\r
+       }\r
+\r
+       public void setInst(AsmInstruction inst)\r
+       {\r
+               if (inst != null)\r
+                       throw new IllegalStateException("Instrution already set for " + name);\r
+               this.inst = inst;\r
+       }\r
+\r
+       @Override\r
+       public String toString()\r
+       {\r
+               return name + ":";\r
+       }\r
+\r
+       @Override\r
+       public int hashCode()\r
+       {\r
+               return Objects.hash(inst, name);\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj)\r
+       {\r
+               if (this == obj)\r
+                       return true;\r
+               if (!(obj instanceof AsmLabel))\r
+                       return false;\r
+               AsmLabel other = (AsmLabel) obj;\r
+               return Objects.equals(inst, other.inst) && Objects.equals(name, other.name);\r
+       }\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperand.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperand.java
new file mode 100644 (file)
index 0000000..eddc033
--- /dev/null
@@ -0,0 +1,11 @@
+package net.mograsim.machine.isa;\r
+\r
+import net.mograsim.logic.core.types.BitVector;\r
+import net.mograsim.machine.isa.types.AsmException;\r
+\r
+public interface AsmOperand\r
+{\r
+       int getSize();\r
+\r
+       BitVector parse(String s) throws AsmException;\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperands.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperands.java
new file mode 100644 (file)
index 0000000..6abf881
--- /dev/null
@@ -0,0 +1,43 @@
+package net.mograsim.machine.isa;\r
+\r
+import java.util.List;\r
+import java.util.Objects;\r
+import java.util.stream.Collectors;\r
+\r
+public final class AsmOperands\r
+{\r
+       private final List<AsmOperand> operands;\r
+\r
+       public AsmOperands(List<AsmOperand> operands)\r
+       {\r
+               this.operands = List.copyOf(Objects.requireNonNull(operands));\r
+       }\r
+\r
+       public List<AsmOperand> getOperands()\r
+       {\r
+               return operands;\r
+       }\r
+\r
+       @Override\r
+       public int hashCode()\r
+       {\r
+               return operands.hashCode();\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj)\r
+       {\r
+               if (this == obj)\r
+                       return true;\r
+               if (!(obj instanceof AsmOperands))\r
+                       return false;\r
+               AsmOperands other = (AsmOperands) obj;\r
+               return operands.equals(other.operands);\r
+       }\r
+\r
+       @Override\r
+       public String toString()\r
+       {\r
+               return operands.stream().map(AsmOperand::toString).collect(Collectors.joining(", "));\r
+       }\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperation.java b/net.mograsim.machine/src/net/mograsim/machine/isa/AsmOperation.java
new file mode 100644 (file)
index 0000000..7c9ab48
--- /dev/null
@@ -0,0 +1,41 @@
+package net.mograsim.machine.isa;\r
+\r
+import java.util.Objects;\r
+\r
+public final class AsmOperation\r
+{\r
+       private final String mnemonic;\r
+\r
+       public AsmOperation(String mnemonic)\r
+       {\r
+               this.mnemonic = Objects.requireNonNull(mnemonic.toLowerCase());\r
+       }\r
+\r
+       public String getMnemonic()\r
+       {\r
+               return mnemonic;\r
+       }\r
+\r
+       @Override\r
+       public String toString()\r
+       {\r
+               return getMnemonic();\r
+       }\r
+\r
+       @Override\r
+       public int hashCode()\r
+       {\r
+               return mnemonic.hashCode();\r
+       }\r
+\r
+       @Override\r
+       public boolean equals(Object obj)\r
+       {\r
+               if (this == obj)\r
+                       return true;\r
+               if (!(obj instanceof AsmOperation))\r
+                       return false;\r
+               AsmOperation other = (AsmOperation) obj;\r
+               return mnemonic.equals(other.mnemonic);\r
+       }\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/NumericOperand.java b/net.mograsim.machine/src/net/mograsim/machine/isa/NumericOperand.java
new file mode 100644 (file)
index 0000000..2bb94ea
--- /dev/null
@@ -0,0 +1,6 @@
+package net.mograsim.machine.isa;\r
+\r
+public class NumericOperand\r
+{\r
+\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/types/AsmException.java b/net.mograsim.machine/src/net/mograsim/machine/isa/types/AsmException.java
new file mode 100644 (file)
index 0000000..c884163
--- /dev/null
@@ -0,0 +1,32 @@
+package net.mograsim.machine.isa.types;\r
+\r
+public class AsmException extends Exception\r
+{\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       public AsmException()\r
+       {\r
+               super();\r
+       }\r
+\r
+       public AsmException(Throwable cause, String message, boolean enableSuppression, boolean writableStackTrace)\r
+       {\r
+               super(message, cause, enableSuppression, writableStackTrace);\r
+       }\r
+\r
+       public AsmException(Throwable cause, String formatString, Object... formatArgs)\r
+       {\r
+               super(String.format(formatString, formatArgs), cause);\r
+       }\r
+\r
+       public AsmException(String formatString, Object... formatArgs)\r
+       {\r
+               super(String.format(formatString, formatArgs));\r
+       }\r
+\r
+       public AsmException(Throwable cause)\r
+       {\r
+               super(cause);\r
+       }\r
+\r
+}\r
diff --git a/net.mograsim.machine/src/net/mograsim/machine/isa/types/AsmNumberFormatException.java b/net.mograsim.machine/src/net/mograsim/machine/isa/types/AsmNumberFormatException.java
new file mode 100644 (file)
index 0000000..8580af6
--- /dev/null
@@ -0,0 +1,16 @@
+package net.mograsim.machine.isa.types;\r
+\r
+public class AsmNumberFormatException extends AsmException\r
+{\r
+       private static final long serialVersionUID = 1L;\r
+\r
+       public AsmNumberFormatException(Exception cause, String message, Object... formatArgs)\r
+       {\r
+               super(cause, message, formatArgs);\r
+       }\r
+\r
+       public AsmNumberFormatException(String message, Object... formatArgs)\r
+       {\r
+               super(message, formatArgs);\r
+       }\r
+}\r