Fixed a bug in Am2900; created dlatch8/80; relayouted some components
[Mograsim.git] / net.mograsim.logic.model.am2900 / src / net / mograsim / logic / model / examples / GenerateDff80HighLevelStateHandler.java
1 package net.mograsim.logic.model.examples;
2
3 import java.util.ArrayList;
4
5 import net.mograsim.logic.model.am2900.Am2900Loader;
6 import net.mograsim.logic.model.model.LogicModelModifiable;
7 import net.mograsim.logic.model.model.components.submodels.SubmodelComponent;
8 import net.mograsim.logic.model.serializing.DeserializedSubmodelComponent;
9 import net.mograsim.logic.model.serializing.IdentifyParams;
10 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
11 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.StandardHighLevelStateHandler;
12 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler;
13 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.BitVectorSplittingAtomicHighLevelStateHandler.BitVectorSplittingAtomicHighLevelStateHandlerParams;
14 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler;
15 import net.mograsim.logic.model.snippets.highlevelstatehandlers.standard.atomic.DelegatingAtomicHighLevelStateHandler.DelegatingAtomicHighLevelStateHandlerParams;
16
17 public class GenerateDff80HighLevelStateHandler
18 {
19         public static void main(String[] args)
20         {
21                 Am2900Loader.setup();
22                 LogicModelModifiable model = new LogicModelModifiable();
23                 DeserializedSubmodelComponent comp = new DeserializedSubmodelComponent(model, null, null, null);
24                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#0");// LSB
25                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#1");
26                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#2");
27                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#3");
28                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#4");
29                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#5");
30                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#6");
31                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#7");
32                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#8");
33                 IndirectModelComponentCreator.createComponent(comp.getSubmodelModifiable(), "dff8", "dff8#9");// MSB
34
35                 StandardHighLevelStateHandler hlsh = new StandardHighLevelStateHandler(comp);
36                 comp.setHighLevelStateHandler(hlsh);
37                 BitVectorSplittingAtomicHighLevelStateHandlerParams p = new BitVectorSplittingAtomicHighLevelStateHandlerParams();
38                 p.vectorPartLengthes = new ArrayList<>();
39                 p.vectorPartTargets = new ArrayList<>();
40                 for (int i = 0; i < 10; i++)
41                 {
42                         addHandlersForByte(comp, hlsh, i, p);
43                 }
44                 hlsh.addAtomicHighLevelState("q", new BitVectorSplittingAtomicHighLevelStateHandler(comp, p));
45
46                 System.out.println(comp.getHighLevelStateHandler().getParamsForSerializingJSON(new IdentifyParams()));
47         }
48
49         private static void addHandlersForByte(SubmodelComponent comp, StandardHighLevelStateHandler hlsh, int LSByteIndex,
50                         BitVectorSplittingAtomicHighLevelStateHandlerParams p2)
51         {
52                 // TODO remove the "+ 1" as soon as HighLevelStates count from 0
53                 // Also replace the 1 in "bitIndexInByte = 1" below with a 0
54                 int LSBitIndex = LSByteIndex * 8 + 1;
55                 int MSBitIndex = LSBitIndex + 7;
56                 String dffThisByte = "dff8#" + LSByteIndex;
57                 String thisByteHLSID = "q" + MSBitIndex + "-" + LSBitIndex;
58
59                 p2.vectorPartLengthes.add(0, 8);
60                 p2.vectorPartTargets.add(0, thisByteHLSID);
61
62                 DelegatingAtomicHighLevelStateHandlerParams p = new DelegatingAtomicHighLevelStateHandlerParams();
63                 p.delegateTarget = dffThisByte;
64                 p.subStateID = "q";
65                 hlsh.addAtomicHighLevelState(thisByteHLSID, new DelegatingAtomicHighLevelStateHandler(comp, p));
66                 for (int bitIndexOuter = LSBitIndex, bitIndexInByte = 1; bitIndexOuter <= MSBitIndex; bitIndexOuter++, bitIndexInByte++)
67                 {
68                         p = new DelegatingAtomicHighLevelStateHandlerParams();
69                         p.delegateTarget = dffThisByte;
70                         p.subStateID = "q" + bitIndexInByte;
71                         hlsh.addAtomicHighLevelState("q" + bitIndexOuter, new DelegatingAtomicHighLevelStateHandler(comp, p));
72                 }
73         }
74 }