3d3d6c395a62a1e67ea16d9345e97f33685f07fa
[Mograsim.git] /
1 package net.mograsim.logic.model.am2900.machine;
2
3 import java.util.HashSet;
4 import java.util.List;
5 import java.util.Set;
6 import java.util.function.Consumer;
7
8 import net.mograsim.logic.core.components.CoreClock;
9 import net.mograsim.logic.core.timeline.Timeline;
10 import net.mograsim.logic.core.types.Bit;
11 import net.mograsim.logic.core.types.BitVector;
12 import net.mograsim.logic.model.am2900.machine.registers.Am2900Register;
13 import net.mograsim.logic.model.am2900.machine.registers.muInstrRegister;
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.CoreModelParameters.CoreModelParametersBuilder;
20 import net.mograsim.logic.model.modeladapter.LogicCoreAdapter;
21 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
22 import net.mograsim.machine.AssignableMainMemory;
23 import net.mograsim.machine.Machine;
24 import net.mograsim.machine.MachineDefinition;
25 import net.mograsim.machine.StandardMainMemory;
26 import net.mograsim.machine.mi.AssignableMPROM;
27 import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
28 import net.mograsim.machine.mi.MicroInstruction;
29 import net.mograsim.machine.mi.MicroInstructionDefinition;
30 import net.mograsim.machine.mi.StandardMPROM;
31 import net.mograsim.machine.mi.StandardMicroInstructionMemory;
32 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
33 import net.mograsim.machine.mi.parameters.ParameterClassification;
34 import net.mograsim.machine.registers.Register;
35 import net.mograsim.machine.registers.RegisterGroup;
36
37 public class Am2900Machine implements Machine
38 {
39         private final AbstractAm2900MachineDefinition machineDefinition;
40         private final LogicModelModifiable logicModel;
41         private final ModelComponent am2900;
42         private final Timeline timeline;
43         private final CoreModelParameters params;
44         private final AssignableMainMemory mainMemory;
45         private final AssignableMicroInstructionMemory instMemory;
46         private final AssignableMPROM mprom;
47         private final CoreClock clock;
48         private long activeInstructionAddress;
49
50         private final Set<ActiveMicroInstructionChangedListener> amicListeners;
51
52         public Am2900Machine(LogicModelModifiable model, AbstractAm2900MachineDefinition am2900MachineDefinition)
53         {
54                 this.machineDefinition = am2900MachineDefinition;
55                 this.logicModel = model;
56                 this.am2900 = IndirectModelComponentCreator.createComponent(logicModel,
57                                 "resloader:Am2900Loader:jsonres:net/mograsim/logic/model/am2900/components/Am2900.json", "Am2900");
58                 this.amicListeners = new HashSet<>();
59
60                 CoreModelParametersBuilder paramsBuilder = new CoreModelParametersBuilder();
61                 paramsBuilder.gateProcessTime = 50;
62                 paramsBuilder.hardcodedComponentProcessTime = paramsBuilder.gateProcessTime * 5;
63                 paramsBuilder.wireTravelTime = 10;
64                 params = paramsBuilder.build();
65
66                 mainMemory = new AssignableMainMemory(new StandardMainMemory(am2900MachineDefinition.getMainMemoryDefinition()));
67                 instMemory = new AssignableMicroInstructionMemory(
68                                 new StandardMicroInstructionMemory(am2900MachineDefinition.getMicroInstructionMemoryDefinition()));
69                 mprom = new AssignableMPROM(new StandardMPROM(am2900MachineDefinition.getMPROMDefinition()));
70                 timeline = LogicCoreAdapter.convert(logicModel, params);
71                 am2900.setHighLevelState("ram.memory_binding", mainMemory);
72                 am2900.setHighLevelState("mpm.memory_binding", instMemory);
73                 am2900.setHighLevelState("mprom.memory_binding", mprom);
74                 clock = logicModel.getComponentBySubmodelPath("Am2900.Clock#0", ModelClock.class).getClock();
75                 clock.registerObserver(c ->
76                 {
77                         if (clock.isOn())
78                         {
79                                 long oldAddress = activeInstructionAddress;
80                                 activeInstructionAddress = getCurrentMicroInstructionAddress();
81                                 notifyActiveMicroInstructionChangedListeners(oldAddress, activeInstructionAddress);
82                         }
83                 });
84         }
85
86         @Override
87         public MachineDefinition getDefinition()
88         {
89                 return machineDefinition;
90         }
91
92         @Override
93         public void reset()
94         {
95                 MicroInstructionDefinition muiDef = getDefinition().getMicroInstructionMemoryDefinition().getMicroInstructionDefinition();
96                 ParameterClassification[] paramClassifications = muiDef.getParameterClassifications();
97                 MicroInstructionParameter[] defaultParams = muiDef.createDefaultInstruction().getParameters();
98                 defaultParams[19] = paramClassifications[19].parse("JZ");
99                 MicroInstruction jzMI = MicroInstruction.create(defaultParams);
100                 am2900.setHighLevelState("muir_2.q", jzMI.toBitVector());
101                 Bit regsValue = machineDefinition.expert ? Bit.U : Bit.ZERO;
102                 setRegistersTo(machineDefinition.getUnsortedRegisters(), regsValue);
103                 setRegisterGroupTo(machineDefinition.getRegisterGroups(), regsValue);
104                 // TODO reset latches?
105         }
106
107         private void setRegistersTo(List<Register> registers, Bit value)
108         {
109                 for (Register r : registers)
110                         if (r != muInstrRegister.instance)// don't reset; sometimes causes a glitch
111                                 setRegister(r, BitVector.of(value, r.getWidth()));
112         }
113
114         private void setRegisterGroupTo(List<RegisterGroup> registerGroups, Bit value)
115         {
116                 for (RegisterGroup rg : registerGroups)
117                 {
118                         setRegistersTo(rg.getRegisters(), value);
119                         setRegisterGroupTo(rg.getSubGroups(), value);
120                 }
121         }
122
123         @Override
124         public LogicModel getModel()
125         {
126                 return logicModel;
127         }
128
129         public ModelComponent getAm2900()
130         {
131                 return am2900;
132         }
133
134         @Override
135         public Timeline getTimeline()
136         {
137                 return timeline;
138         }
139
140         @Override
141         public CoreModelParameters getCoreModelParameters()
142         {
143                 return params;
144         }
145
146         @Override
147         public CoreClock getClock()
148         {
149                 return clock;
150         }
151
152         @Override
153         public BitVector getRegister(Register r)
154         {
155                 return castAm2900Register(r).read(am2900);
156         }
157
158         @Override
159         public void setRegister(Register r, BitVector value)
160         {
161                 castAm2900Register(r).write(am2900, value);
162         }
163
164         @Override
165         public void addRegisterListener(Register r, Consumer<BitVector> listener)
166         {
167                 castAm2900Register(r).addListener(am2900, listener);
168         }
169
170         @Override
171         public void removeRegisterListener(Register r, Consumer<BitVector> listener)
172         {
173                 castAm2900Register(r).removeListener(am2900, listener);
174         }
175
176         private static Am2900Register castAm2900Register(Register r)
177         {
178                 if (r instanceof Am2900Register)
179                         return (Am2900Register) r;
180                 throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
181         }
182
183         @Override
184         public AssignableMainMemory getMainMemory()
185         {
186                 return mainMemory;
187         }
188
189         @Override
190         public AssignableMicroInstructionMemory getMicroInstructionMemory()
191         {
192                 return instMemory;
193         }
194
195         @Override
196         public AssignableMPROM getMPROM()
197         {
198                 return mprom;
199         }
200
201         @Override
202         public long getActiveMicroInstructionAddress()
203         {
204                 return activeInstructionAddress;
205         }
206
207         private long getCurrentMicroInstructionAddress()
208         {
209                 BitVector vector = (BitVector) am2900.getHighLevelState("mpm_address");
210                 return vector.isBinary() ? vector.getUnsignedValueLong() : -1;
211         }
212
213         @Override
214         public void addActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
215         {
216                 amicListeners.add(listener);
217         }
218
219         @Override
220         public void removeActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
221         {
222                 amicListeners.remove(listener);
223         }
224
225         private void notifyActiveMicroInstructionChangedListeners(long oldAddress, long newAddress)
226         {
227                 amicListeners.forEach(l -> l.instructionChanged(oldAddress, newAddress));
228         }
229 }