3d6db31c4b2146c2a5ac616d254b4d67204c3cf8
[Mograsim.git] /
1 package net.mograsim.logic.model.am2900.machine;
2
3 import java.util.HashMap;
4 import java.util.HashSet;
5 import java.util.Map;
6 import java.util.Set;
7 import java.util.function.Consumer;
8
9 import net.mograsim.logic.core.components.CoreClock;
10 import net.mograsim.logic.core.timeline.Timeline;
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.logic.model.am2900.machine.registers.NumberedRegister;
13 import net.mograsim.logic.model.am2900.machine.registers.QRegister;
14 import net.mograsim.logic.model.model.LogicModel;
15 import net.mograsim.logic.model.model.LogicModelModifiable;
16 import net.mograsim.logic.model.model.components.ModelComponent;
17 import net.mograsim.logic.model.model.components.atomic.ModelClock;
18 import net.mograsim.logic.model.modeladapter.CoreModelParameters;
19 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
20 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
21 import net.mograsim.machine.Machine;
22 import net.mograsim.machine.MachineDefinition;
23 import net.mograsim.machine.Register;
24 import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
25 import net.mograsim.machine.mi.MicroInstruction;
26 import net.mograsim.machine.mi.MicroInstructionDefinition;
27 import net.mograsim.machine.mi.StandardMicroInstructionMemory;
28 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
29 import net.mograsim.machine.mi.parameters.ParameterClassification;
30 import net.mograsim.machine.standard.memory.AssignableMainMemory;
31 import net.mograsim.machine.standard.memory.WordAddressableMemory;
32
33 public class Am2900Machine implements Machine
34 {
35         private Am2900MachineDefinition machineDefinition;
36         private LogicModelModifiable logicModel;
37         private ModelComponent am2900;
38         private Timeline timeline;
39         private AssignableMainMemory mainMemory;
40         private AssignableMicroInstructionMemory instMemory;
41         private CoreClock clock;
42         private long activeInstructionAddress;
43
44         private final Set<ActiveMicroInstructionChangedListener> amicListeners;
45         private final Map<Register, Map<Consumer<BitVector>, Consumer<Object>>> modelListenersPerRegisterListenerPerRegister;
46
47         public Am2900Machine(LogicModelModifiable model, Am2900MachineDefinition am2900MachineDefinition)
48         {
49                 this.machineDefinition = am2900MachineDefinition;
50                 this.logicModel = model;
51                 this.am2900 = IndirectModelComponentCreator.createComponent(logicModel,
52                                 "resloader:Am2900Loader:jsonres:net/mograsim/logic/model/am2900/components/Am2900.json", "Am2900");
53                 this.amicListeners = new HashSet<>();
54                 this.modelListenersPerRegisterListenerPerRegister = new HashMap<>();
55
56                 CoreModelParameters params = new CoreModelParameters();
57                 params.gateProcessTime = 50;
58                 params.wireTravelTime = 10;
59                 mainMemory = new AssignableMainMemory(new WordAddressableMemory(am2900MachineDefinition.getMainMemoryDefinition()));
60                 instMemory = new AssignableMicroInstructionMemory(
61                                 new StandardMicroInstructionMemory(am2900MachineDefinition.getMicroInstructionMemoryDefinition()));
62                 timeline = LogicCoreAdapter.convert(logicModel, params);
63                 am2900.setHighLevelState("ram.memory_binding", mainMemory);
64                 am2900.setHighLevelState("mpm.memory_binding", instMemory);
65                 clock = logicModel.getComponentBySubmodelPath("Am2900.Clock#0", ModelClock.class).getClock();
66                 clock.registerObserver(c ->
67                 {
68                         if (clock.isOn())
69                         {
70                                 long oldAddress = activeInstructionAddress;
71                                 activeInstructionAddress = getCurrentMicroInstructionAddress();
72                                 notifyActiveMicroInstructionChangedListeners(oldAddress, activeInstructionAddress);
73                         }
74                 });
75         }
76
77         @Override
78         public MachineDefinition getDefinition()
79         {
80                 return machineDefinition;
81         }
82
83         @Override
84         public void reset()
85         {
86                 MicroInstructionDefinition muiDef = getDefinition().getMicroInstructionMemoryDefinition().getMicroInstructionDefinition();
87                 ParameterClassification[] paramClassifications = muiDef.getParameterClassifications();
88                 MicroInstructionParameter[] defaultParams = muiDef.createDefaultInstruction().getParameters();
89                 defaultParams[19] = paramClassifications[19].parse("JZ");
90                 MicroInstruction jzMI = MicroInstruction.create(defaultParams);
91                 am2900.setHighLevelState("muir_2.q", jzMI.toBitVector());
92         }
93
94         @Override
95         public LogicModel getModel()
96         {
97                 return logicModel;
98         }
99
100         public ModelComponent getAm2900()
101         {
102                 return am2900;
103         }
104
105         @Override
106         public Timeline getTimeline()
107         {
108                 return timeline;
109         }
110
111         @Override
112         public CoreClock getClock()
113         {
114                 return clock;
115         }
116
117         // TODO too much code duplication
118
119         @Override
120         public BitVector getRegister(Register r)
121         {
122                 String am2901CellSuffix = getAm2901CellSuffix(r);
123                 BitVector result = BitVector.of();
124                 for (int i = 0; i < 16; i += 4)
125                 {
126                         String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
127                         result = result.concat((BitVector) am2900.getHighLevelState(hlsID));
128                 }
129                 return result;
130         }
131
132         @Override
133         public void setRegister(Register r, BitVector value)
134         {
135                 String am2901CellSuffix = getAm2901CellSuffix(r);
136                 for (int i = 0; i < 16; i += 4)
137                 {
138                         String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
139                         am2900.setHighLevelState(hlsID, value.subVector(i, i + 4));
140                 }
141         }
142
143         @Override
144         public void addRegisterListener(Register r, Consumer<BitVector> listener)
145         {
146                 Map<Consumer<BitVector>, Consumer<Object>> modelListenersPerRegisterListener = modelListenersPerRegisterListenerPerRegister
147                                 .computeIfAbsent(r, k -> new HashMap<>());
148                 if (modelListenersPerRegisterListener.containsKey(listener))
149                         return;
150
151                 Consumer<Object> modelListener = v -> listener.accept(getRegister(r));
152                 String am2901CellSuffix = getAm2901CellSuffix(r);
153                 for (int i = 0; i < 16; i += 4)
154                 {
155                         String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
156                         am2900.addHighLevelStateListener(hlsID, modelListener);
157                 }
158         }
159
160         @Override
161         public void removeRegisterListener(Register r, Consumer<BitVector> listener)
162         {
163                 Map<Consumer<BitVector>, Consumer<Object>> modelListenersPerRegisterListener = modelListenersPerRegisterListenerPerRegister.get(r);
164                 if (modelListenersPerRegisterListener == null)
165                         return;
166
167                 Consumer<Object> modelListener = modelListenersPerRegisterListener.get(listener);
168                 if (modelListener == null)
169                         return;
170
171                 String am2901CellSuffix = getAm2901CellSuffix(r);
172                 for (int i = 0; i < 16; i += 4)
173                 {
174                         String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
175                         am2900.removeHighLevelStateListener(hlsID, modelListener);
176                 }
177         }
178
179         private static String getAm2901CellSuffix(Register r)
180         {
181                 if (r instanceof QRegister)
182                         return "qreg.q";
183                 if (r instanceof NumberedRegister)
184                         return "regs.c" + ((NumberedRegister) r).getIndexAsBitstring() + ".q";
185                 throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
186         }
187
188         @Override
189         public AssignableMainMemory getMainMemory()
190         {
191                 return mainMemory;
192         }
193
194         @Override
195         public AssignableMicroInstructionMemory getMicroInstructionMemory()
196         {
197                 return instMemory;
198         }
199
200         @Override
201         public long getActiveMicroInstructionAddress()
202         {
203                 return activeInstructionAddress;
204         }
205
206         private long getCurrentMicroInstructionAddress()
207         {
208                 BitVector vector = (BitVector) am2900.getHighLevelState("mpm_address");
209                 return vector.isBinary() ? vector.getUnsignedValueLong() : -1;
210         }
211
212         @Override
213         public void addActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
214         {
215                 amicListeners.add(listener);
216         }
217
218         @Override
219         public void removeActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
220         {
221                 amicListeners.remove(listener);
222         }
223
224         private void notifyActiveMicroInstructionChangedListeners(long oldAddress, long newAddress)
225         {
226                 amicListeners.forEach(l -> l.instructionChanged(oldAddress, newAddress));
227         }
228 }