1 package net.mograsim.machine.mi;
3 import java.math.BigInteger;
4 import java.util.Arrays;
5 import java.util.Optional;
7 import net.mograsim.logic.core.types.Bit;
8 import net.mograsim.machine.mi.parameters.IntegerClassification;
9 import net.mograsim.machine.mi.parameters.IntegerImmediate;
10 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
11 import net.mograsim.machine.mi.parameters.MnemonicFamily;
12 import net.mograsim.machine.mi.parameters.ParameterClassification;
13 import net.mograsim.machine.mi.parameters.MicroInstructionParameter.ParameterType;
15 public interface MicroInstructionDefinition
18 * @return The {@link ParameterClassification}s of which a MicroInstruction is composed.
20 public ParameterClassification[] getParameterClassifications();
23 * @throws IndexOutOfBoundsException
25 public ParameterClassification getParameterClassification(int index);
29 * @return The amount of {@link MicroInstructionParameter}s in a {@link MicroInstruction} that follows this definition.
31 public default int size()
33 return getParameterClassifications().length;
37 * @return The amount of {@link Bit}s in a {@link MicroInstruction} that follows this definition.
39 public default int sizeInBits()
41 return Arrays.stream(getParameterClassifications()).mapToInt(e -> e.getExpectedBits()).reduce(0, (a, b) -> a + b);
44 public default MicroInstruction createDefaultInstruction()
47 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
48 ParameterClassification[] classes = getParameterClassifications();
49 for(int i = 0; i < size; i++)
51 MicroInstructionParameter newParam;
52 ParameterClassification classification = classes[i];
53 ParameterType type = classification.getExpectedType();
56 case BOOLEAN_IMMEDIATE:
58 newParam = ((MnemonicFamily) classification).get(0);
60 case INTEGER_IMMEDIATE:
61 newParam = new IntegerImmediate(BigInteger.valueOf(0), ((IntegerClassification) classification).getExpectedBits());
64 throw new IllegalStateException("Unknown ParameterType " + type);
68 return new StandardMicroInstruction(params);
71 public Optional<String> getParameterDescription(int index);