BitVectorSplittingAtomicHighLevelStateHandler now supports Bits
[Mograsim.git] / net.mograsim.logic.model / src / net / mograsim / logic / model / snippets / highlevelstatehandlers / standard / atomic / BitVectorSplittingAtomicHighLevelStateHandler.java
1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import net.mograsim.logic.core.types.Bit;
7 import net.mograsim.logic.core.types.BitVector;
8 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
9 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.HighLevelStateHandlerContext;
10
11 public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler
12 {
13         private SubmodelComponent component;
14         private final List<String> vectorPartTargets;
15         private final List<Integer> vectorPartLengthes;
16
17         public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context)
18         {
19                 this(context, null);
20         }
21
22         public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context,
23                         BitVectorSplittingAtomicHighLevelStateHandlerParams params)
24         {
25                 this.component = context.component;
26                 this.vectorPartTargets = new ArrayList<>();
27                 this.vectorPartLengthes = new ArrayList<>();
28                 if (params != null)
29                         setVectorParts(params.vectorPartTargets, params.vectorPartLengthes);
30         }
31
32         public void set(List<String> targets, List<Integer> lengthes)
33         {
34                 setVectorParts(targets, lengthes);
35         }
36
37         public void addVectorPart(String target, int length)
38         {
39                 vectorPartTargets.add(target);
40                 vectorPartLengthes.add(length);
41         }
42
43         public void clearVectorParts()
44         {
45                 vectorPartTargets.clear();
46                 vectorPartLengthes.clear();
47         }
48
49         private void setVectorParts(List<String> targets, List<Integer> lengthes)
50         {
51                 clearVectorParts();
52                 if (targets.size() != lengthes.size())
53                         throw new IllegalArgumentException("Targets list and lenghtes list have different sizes");
54                 vectorPartTargets.addAll(targets);
55                 vectorPartLengthes.addAll(lengthes);
56         }
57
58         @Override
59         public Object getHighLevelState()
60         {
61                 BitVector result = BitVector.of();
62                 for (int partIndex = 0; partIndex < vectorPartTargets.size(); partIndex++)
63                 {
64                         Object subStateUncasted = component.getHighLevelState(vectorPartTargets.get(partIndex));
65                         BitVector vectorPart;
66                         if (subStateUncasted instanceof Bit)
67                                 vectorPart = BitVector.of((Bit) subStateUncasted);
68                         else
69                                 vectorPart = (BitVector) subStateUncasted;
70                         if (vectorPart.length() != vectorPartLengthes.get(partIndex))
71                                 throw new IllegalArgumentException(
72                                                 "Illegal vector part length: " + vectorPart.length() + "; expected " + vectorPartLengthes.get(partIndex));
73                         result = result.concat(vectorPart);// TODO is the bit order correct?
74                 }
75                 return result;
76         }
77
78         @Override
79         public void setHighLevelState(Object newState)
80         {
81                 BitVector newStateCasted = (BitVector) newState;
82                 for (int partIndex = 0, bitIndex = 0; partIndex < vectorPartTargets.size(); partIndex++)
83                 {
84                         int vectorPartLength = vectorPartLengthes.get(partIndex);
85                         BitVector vectorPart = newStateCasted.subVector(bitIndex, vectorPartLength);// TODO is the bit order correct?
86                         component.setHighLevelState(vectorPartTargets.get(partIndex), vectorPart);
87                         bitIndex += vectorPartLength;
88                 }
89         }
90
91         public static class BitVectorSplittingAtomicHighLevelStateHandlerParams
92         {
93                 public List<String> vectorPartTargets;
94                 public List<Integer> vectorPartLengthes;
95         }
96 }