From: Fabian Stemmler Date: Mon, 26 Aug 2019 17:09:04 +0000 (+0200) Subject: Refined MicroInstructionDefinition and associated classes X-Git-Url: https://mograsim.net/gitweb/?a=commitdiff_plain;h=5e4efdd581f04578be5f59e591f4bcf746df44d8;hp=071e64ab6f5eeb1110d505ac1f4e5f0b06f69f6b;p=Mograsim.git Refined MicroInstructionDefinition and associated classes --- diff --git a/net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java b/net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java index b054c667..9f16320a 100644 --- a/net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java +++ b/net.mograsim.machine/src/net/mograsim/machine/MicroInstruction.java @@ -1,10 +1,11 @@ package net.mograsim.machine; -import net.mograsim.machine.mnemonics.Mnemonic; +import net.mograsim.machine.mi.parameters.MicroInstructionParameter; +import net.mograsim.machine.mi.parameters.Mnemonic; public interface MicroInstruction { - public Mnemonic getValue(int index); + public MicroInstructionParameter getParameter(int index); /** * @return The amount of {@link Mnemonic}s, the instruction is composed of diff --git a/net.mograsim.machine/src/net/mograsim/machine/MicroInstructionDefinition.java b/net.mograsim.machine/src/net/mograsim/machine/MicroInstructionDefinition.java index 3676b2b4..33a970c5 100644 --- a/net.mograsim.machine/src/net/mograsim/machine/MicroInstructionDefinition.java +++ b/net.mograsim.machine/src/net/mograsim/machine/MicroInstructionDefinition.java @@ -1,17 +1,17 @@ package net.mograsim.machine; -import net.mograsim.machine.mnemonics.Mnemonic; -import net.mograsim.machine.mnemonics.MnemonicFamily; +import net.mograsim.machine.mi.parameters.MicroInstructionParameter; +import net.mograsim.machine.mi.parameters.ParameterClassification; public interface MicroInstructionDefinition { /** - * @return The {@link MnemonicFamily}s of which a MicroInstruction is composed. + * @return The {@link ParameterClassification}s of which a MicroInstruction is composed. */ - public MnemonicFamily[] getMnemonicFamilies(); + public ParameterClassification[] getParameterClassifications(); /** - * @return The amount of {@link Mnemonic}s a {@link MicroInstruction} that follows this definition consists of. + * @return The amount of {@link MicroInstructionParameter}s in a {@link MicroInstruction} that follows this definition. */ public int size(); diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/BooleanImmediate.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/BooleanImmediate.java new file mode 100644 index 00000000..7ed60fde --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/BooleanImmediate.java @@ -0,0 +1,51 @@ +package net.mograsim.machine.mi.parameters; + +import net.mograsim.logic.core.types.BitVector; + +public class BooleanImmediate implements MicroInstructionParameter +{ + private boolean value; + + public BooleanImmediate(boolean value) + { + this.value = value; + } + + @Override + public BitVector getValue() + { + return value ? BitVector.SINGLE_1 : BitVector.SINGLE_0; + } + + @Override + public ParameterType getType() + { + return ParameterType.BOOLEAN_IMMEDIATE; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + (value ? 1231 : 1237); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof BooleanImmediate)) + return false; + BooleanImmediate other = (BooleanImmediate) obj; + if (value != other.value) + return false; + return true; + } + + +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/IntegerImmediate.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/IntegerImmediate.java new file mode 100644 index 00000000..e6f5f762 --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/IntegerImmediate.java @@ -0,0 +1,60 @@ +package net.mograsim.machine.mi.parameters; + +import java.math.BigInteger; + +import net.mograsim.logic.core.types.BitVector; + +public final class IntegerImmediate implements MicroInstructionParameter +{ + private BitVector value; + + public IntegerImmediate(BigInteger value, int bits) + { + this.value = BitVector.from(value, bits); + } + + public IntegerImmediate(BitVector value) + { + this.value = value; + } + + @Override + public BitVector getValue() + { + return value; + } + + @Override + public ParameterType getType() + { + return ParameterType.INTEGER_IMMEDIATE; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + ((value == null) ? 0 : value.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof IntegerImmediate)) + return false; + IntegerImmediate other = (IntegerImmediate) obj; + if (value == null) + { + if (other.value != null) + return false; + } else if (!value.equals(other.value)) + return false; + return true; + } +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MicroInstructionParameter.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MicroInstructionParameter.java new file mode 100644 index 00000000..ecff9f0b --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MicroInstructionParameter.java @@ -0,0 +1,15 @@ +package net.mograsim.machine.mi.parameters; + +import net.mograsim.logic.core.types.BitVector; + +public interface MicroInstructionParameter +{ + public BitVector getValue(); + + public ParameterType getType(); + + public static enum ParameterType + { + INTEGER_IMMEDIATE, BOOLEAN_IMMEDIATE, MNEMONIC + } +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java new file mode 100644 index 00000000..feeb88e8 --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/Mnemonic.java @@ -0,0 +1,68 @@ +package net.mograsim.machine.mi.parameters; + +import net.mograsim.logic.core.types.BitVector; + +public final class Mnemonic implements MicroInstructionParameter +{ + private final String text; + private final BitVector vector; + + public Mnemonic(String text, BitVector vector) + { + super(); + this.text = text; + this.vector = vector; + } + + public String getText() + { + return text; + } + + @Override + public BitVector getValue() + { + return vector; + } + + @Override + public int hashCode() + { + final int prime = 31; + int result = 1; + result = prime * result + ((text == null) ? 0 : text.hashCode()); + result = prime * result + ((vector == null) ? 0 : vector.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) + { + if (this == obj) + return true; + if (obj == null) + return false; + if (!(obj instanceof Mnemonic)) + return false; + Mnemonic other = (Mnemonic) obj; + if (text == null) + { + if (other.text != null) + return false; + } else if (!text.equals(other.text)) + return false; + if (vector == null) + { + if (other.vector != null) + return false; + } else if (!vector.equals(other.vector)) + return false; + return true; + } + + @Override + public ParameterType getType() + { + return ParameterType.MNEMONIC; + } +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java new file mode 100644 index 00000000..aa07f808 --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/MnemonicFamily.java @@ -0,0 +1,69 @@ +package net.mograsim.machine.mi.parameters; + +import java.util.Arrays; +import java.util.Map; +import java.util.stream.Collectors; + +import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType; + +public class MnemonicFamily implements ParameterClassification +{ + private final Mnemonic[] values; + private final Map byText; + private final int vectorLenght; + + public MnemonicFamily(Mnemonic... values) + { + this.values = values; + if(values.length == 0) + vectorLenght = 0; + else + { + vectorLenght = values[0].getValue().width(); + for(int i = 1; i < values.length; i++) + if(values[i].getValue().width() != vectorLenght) + throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!"); + } + byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m)); + } + + public Mnemonic[] getValues() + { + return values.clone(); + } + + public Mnemonic get(String text) + { + return byText.get(text); + } + + public boolean contains(Mnemonic m) + { + if(m != null) + return m.equals(byText.get(m.getText())); + else + return false; + } + + public int size() + { + return values.length; + } + + public int getVectorLength() + { + return vectorLenght; + } + + @Override + public boolean conforms(MicroInstructionParameter param) + { + return param instanceof Mnemonic ? contains((Mnemonic) param) : false; + } + + @Override + public ParameterType getExpectedType() + { + return ParameterType.MNEMONIC; + } +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/ParameterClassification.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/ParameterClassification.java new file mode 100644 index 00000000..b9362a09 --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/ParameterClassification.java @@ -0,0 +1,17 @@ +package net.mograsim.machine.mi.parameters; + +import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType; + +public interface ParameterClassification +{ + /** + * Determines whether a {@link MicroInstructionParameter} is part of this class of parameters. + * @return true if the classification contains the Parameter, false otherwise + */ + public boolean conforms(MicroInstructionParameter param); + + /** + * @return The type of the parameters in this classification. + */ + public ParameterType getExpectedType(); +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/SimpleTypeClassification.java b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/SimpleTypeClassification.java new file mode 100644 index 00000000..c6687aed --- /dev/null +++ b/net.mograsim.machine/src/net/mograsim/machine/mi/parameters/SimpleTypeClassification.java @@ -0,0 +1,25 @@ +package net.mograsim.machine.mi.parameters; + +import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType; + +public class SimpleTypeClassification implements ParameterClassification +{ + private ParameterType expectedType; + + public SimpleTypeClassification(ParameterType expectedType) + { + this.expectedType = expectedType; + } + + @Override + public boolean conforms(MicroInstructionParameter param) + { + return expectedType.equals(param.getType()); + } + + @Override + public ParameterType getExpectedType() + { + return expectedType; + } +} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mnemonics/Mnemonic.java b/net.mograsim.machine/src/net/mograsim/machine/mnemonics/Mnemonic.java deleted file mode 100644 index 822d7ed1..00000000 --- a/net.mograsim.machine/src/net/mograsim/machine/mnemonics/Mnemonic.java +++ /dev/null @@ -1,26 +0,0 @@ -package net.mograsim.machine.mnemonics; - -import net.mograsim.logic.core.types.BitVector; - -public final class Mnemonic -{ - private final String text; - private final BitVector vector; - - public Mnemonic(String text, BitVector vector) - { - super(); - this.text = text; - this.vector = vector; - } - - public String getText() - { - return text; - } - - public BitVector getVector() - { - return vector; - } -} diff --git a/net.mograsim.machine/src/net/mograsim/machine/mnemonics/MnemonicFamily.java b/net.mograsim.machine/src/net/mograsim/machine/mnemonics/MnemonicFamily.java deleted file mode 100644 index a55175f7..00000000 --- a/net.mograsim.machine/src/net/mograsim/machine/mnemonics/MnemonicFamily.java +++ /dev/null @@ -1,55 +0,0 @@ -package net.mograsim.machine.mnemonics; - -import java.util.Arrays; -import java.util.Map; -import java.util.stream.Collectors; - -public class MnemonicFamily -{ - private final Mnemonic[] values; - private final Map byText; - private final int vectorLenght; - - public MnemonicFamily(Mnemonic... values) - { - this.values = values; - if(values.length == 0) - vectorLenght = 0; - else - { - vectorLenght = values[0].getVector().width(); - for(int i = 1; i < values.length; i++) - if(values[i].getVector().width() != vectorLenght) - throw new IllegalArgumentException("MnemonicFamily is not of uniform vector length!"); - } - byText = Arrays.stream(values).collect(Collectors.toMap(m -> m.getText(), m -> m)); - } - - public Mnemonic[] getValues() - { - return values.clone(); - } - - public Mnemonic get(String text) - { - return byText.get(text); - } - - public boolean contains(Mnemonic m) - { - if(m != null) - return m.equals(byText.get(m.getText())); - else - return false; - } - - public int size() - { - return values.length; - } - - public int getVectorLength() - { - return vectorLenght; - } -}