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