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