Added MnemonicFamilyBuilder and set most Mnemonics to 'X' by default
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / MicroInstructionDefinition.java
1 package net.mograsim.machine.mi;
2
3 import java.util.Arrays;
4 import java.util.Optional;
5
6 import net.mograsim.logic.core.types.Bit;
7 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
8 import net.mograsim.machine.mi.parameters.ParameterClassification;
9
10 public interface MicroInstructionDefinition
11 {
12         /**
13          * @return The {@link ParameterClassification}s of which a MicroInstruction is composed.
14          */
15         public ParameterClassification[] getParameterClassifications();
16
17         /**
18          * @throws IndexOutOfBoundsException
19          */
20         public ParameterClassification getParameterClassification(int index);
21
22         /**
23          * @return The amount of {@link MicroInstructionParameter}s in a {@link MicroInstruction} that follows this definition.
24          */
25         public default int size()
26         {
27                 return getParameterClassifications().length;
28         }
29
30         /**
31          * @return The amount of {@link Bit}s in a {@link MicroInstruction} that follows this definition.
32          */
33         public default int sizeInBits()
34         {
35                 return Arrays.stream(getParameterClassifications()).mapToInt(e -> e.getExpectedBits()).reduce(0, (a, b) -> a + b);
36         }
37
38         public default MicroInstruction createDefaultInstruction()
39         {
40                 int size = size();
41                 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
42                 ParameterClassification[] classes = getParameterClassifications();
43                 for (int i = 0; i < size; i++)
44                 {
45                         ParameterClassification classification = classes[i];
46                         params[i] = classification.getDefault();
47                 }
48                 return new StandardMicroInstruction(params);
49         }
50
51         public Optional<String> getParameterDescription(int index);
52 }