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.MicroInstructionParameter.ParameterType;
12 import net.mograsim.machine.mi.parameters.MnemonicFamily;
13 import net.mograsim.machine.mi.parameters.ParameterClassification;
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);
28 * @return The amount of {@link MicroInstructionParameter}s in a {@link MicroInstruction} that follows this definition.
30 public default int size()
32 return getParameterClassifications().length;
36 * @return The amount of {@link Bit}s in a {@link MicroInstruction} that follows this definition.
38 public default int sizeInBits()
40 return Arrays.stream(getParameterClassifications()).mapToInt(e -> e.getExpectedBits()).reduce(0, (a, b) -> a + b);
43 public default MicroInstruction createDefaultInstruction(Runnable updateCallback)
46 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
47 ParameterClassification[] classes = getParameterClassifications();
48 for (int i = 0; i < size; i++)
50 MicroInstructionParameter newParam;
51 ParameterClassification classification = classes[i];
52 ParameterType type = classification.getExpectedType();
55 case BOOLEAN_IMMEDIATE:
57 newParam = ((MnemonicFamily) classification).get(0);
59 case INTEGER_IMMEDIATE:
60 newParam = new IntegerImmediate(BigInteger.valueOf(0), ((IntegerClassification) classification).getExpectedBits());
63 throw new IllegalStateException("Unknown ParameterType " + type);
67 return new StandardMicroInstruction(updateCallback, params);
70 public Optional<String> getParameterDescription(int index);