1 package net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic;
3 import java.util.ArrayList;
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;
11 public class BitVectorSplittingAtomicHighLevelStateHandler implements AtomicHighLevelStateHandler
13 private SubmodelComponent component;
14 private final List<String> vectorPartTargets;
15 private final List<Integer> vectorPartLengthes;
17 public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context)
22 public BitVectorSplittingAtomicHighLevelStateHandler(HighLevelStateHandlerContext context,
23 BitVectorSplittingAtomicHighLevelStateHandlerParams params)
25 this.component = context.component;
26 this.vectorPartTargets = new ArrayList<>();
27 this.vectorPartLengthes = new ArrayList<>();
29 setVectorParts(params.vectorPartTargets, params.vectorPartLengthes);
32 public void set(List<String> targets, List<Integer> lengthes)
34 setVectorParts(targets, lengthes);
37 public void addVectorPart(String target, int length)
39 vectorPartTargets.add(target);
40 vectorPartLengthes.add(length);
43 public void clearVectorParts()
45 vectorPartTargets.clear();
46 vectorPartLengthes.clear();
49 private void setVectorParts(List<String> targets, List<Integer> lengthes)
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);
59 public Object getHighLevelState()
61 BitVector result = BitVector.of();
62 for (int partIndex = 0; partIndex < vectorPartTargets.size(); partIndex++)
64 Object subStateUncasted = component.getHighLevelState(vectorPartTargets.get(partIndex));
66 if (subStateUncasted instanceof Bit)
67 vectorPart = BitVector.of((Bit) subStateUncasted);
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?
79 public void setHighLevelState(Object newState)
81 BitVector newStateCasted = (BitVector) newState;
82 for (int partIndex = 0, bitIndex = 0; partIndex < vectorPartTargets.size(); partIndex++)
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;
91 public static class BitVectorSplittingAtomicHighLevelStateHandlerParams
93 public List<String> vectorPartTargets;
94 public List<Integer> vectorPartLengthes;