MicroInstructions can now be converted to bits
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mi / MicroInstructionDefinition.java
1 package net.mograsim.machine.mi;
2
3 import java.math.BigInteger;
4 import java.util.Arrays;
5 import java.util.Optional;
6
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;
14
15 public interface MicroInstructionDefinition
16 {
17         /**
18          * @return The {@link ParameterClassification}s of which a MicroInstruction is composed.
19          */
20         public ParameterClassification[] getParameterClassifications();
21         
22         /**
23          * @throws IndexOutOfBoundsException
24          */
25         public ParameterClassification getParameterClassification(int index);
26         
27         
28         /**
29          * @return The amount of {@link MicroInstructionParameter}s in a {@link MicroInstruction} that follows this definition.
30          */
31         public default int size()
32         {
33                 return getParameterClassifications().length;
34         }
35         
36         /**
37          * @return The amount of {@link Bit}s in a {@link MicroInstruction} that follows this definition.
38          */
39         public default int sizeInBits()
40         {
41                 return Arrays.stream(getParameterClassifications()).mapToInt(e -> e.getExpectedBits()).reduce(0, (a, b) -> a + b);
42         }
43         
44         public default MicroInstruction createDefaultInstruction()
45         {
46                 int size = size();
47                 MicroInstructionParameter[] params = new MicroInstructionParameter[size];
48                 ParameterClassification[] classes = getParameterClassifications();
49                 for(int i = 0; i < size; i++)
50                 {
51                         MicroInstructionParameter newParam;
52                         ParameterClassification classification = classes[i];
53                         ParameterType type = classification.getExpectedType();
54                         switch(type)
55                         {
56                         case BOOLEAN_IMMEDIATE:
57                         case MNEMONIC:
58                                 newParam = ((MnemonicFamily) classification).get(0);
59                                 break;
60                         case INTEGER_IMMEDIATE:
61                                 newParam = new IntegerImmediate(BigInteger.valueOf(0), ((IntegerClassification) classification).getExpectedBits());
62                                 break;
63                         default:
64                                 throw new IllegalStateException("Unknown ParameterType " + type);
65                         }
66                         params[i] = newParam;
67                 }
68                 return new StandardMicroInstruction(params);
69         }
70
71         public Optional<String> getParameterDescription(int index);
72 }