1 package net.mograsim.logic.model.am2900.machine;
3 import java.util.HashMap;
4 import java.util.HashSet;
7 import java.util.function.Consumer;
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;
33 public class Am2900Machine implements Machine
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;
44 private final Set<ActiveMicroInstructionChangedListener> amicListeners;
45 private final Map<Register, Map<Consumer<BitVector>, Consumer<Object>>> modelListenersPerRegisterListenerPerRegister;
47 public Am2900Machine(LogicModelModifiable model, Am2900MachineDefinition am2900MachineDefinition)
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<>();
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 ->
70 long oldAddress = activeInstructionAddress;
71 activeInstructionAddress = getCurrentMicroInstructionAddress();
72 notifyActiveMicroInstructionChangedListeners(oldAddress, activeInstructionAddress);
78 public MachineDefinition getDefinition()
80 return machineDefinition;
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());
95 public LogicModel getModel()
100 public ModelComponent getAm2900()
106 public Timeline getTimeline()
112 public CoreClock getClock()
117 // TODO too much code duplication
120 public BitVector getRegister(Register r)
122 String am2901CellSuffix = getAm2901CellSuffix(r);
123 BitVector result = BitVector.of();
124 for (int i = 0; i < 16; i += 4)
126 String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
127 result = result.concat((BitVector) am2900.getHighLevelState(hlsID));
133 public void setRegister(Register r, BitVector value)
135 String am2901CellSuffix = getAm2901CellSuffix(r);
136 for (int i = 0; i < 16; i += 4)
138 String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
139 am2900.setHighLevelState(hlsID, value.subVector(i, i + 4));
144 public void addRegisterListener(Register r, Consumer<BitVector> listener)
146 Map<Consumer<BitVector>, Consumer<Object>> modelListenersPerRegisterListener = modelListenersPerRegisterListenerPerRegister
147 .computeIfAbsent(r, k -> new HashMap<>());
148 if (modelListenersPerRegisterListener.containsKey(listener))
151 Consumer<Object> modelListener = v -> listener.accept(getRegister(r));
152 String am2901CellSuffix = getAm2901CellSuffix(r);
153 for (int i = 0; i < 16; i += 4)
155 String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
156 am2900.addHighLevelStateListener(hlsID, modelListener);
161 public void removeRegisterListener(Register r, Consumer<BitVector> listener)
163 Map<Consumer<BitVector>, Consumer<Object>> modelListenersPerRegisterListener = modelListenersPerRegisterListenerPerRegister.get(r);
164 if (modelListenersPerRegisterListener == null)
167 Consumer<Object> modelListener = modelListenersPerRegisterListener.get(listener);
168 if (modelListener == null)
171 String am2901CellSuffix = getAm2901CellSuffix(r);
172 for (int i = 0; i < 16; i += 4)
174 String hlsID = String.format("am2901_%d-%d.%s", (i + 3), i, am2901CellSuffix);
175 am2900.removeHighLevelStateListener(hlsID, modelListener);
179 private static String getAm2901CellSuffix(Register r)
181 if (r instanceof QRegister)
183 if (r instanceof NumberedRegister)
184 return "regs.c" + ((NumberedRegister) r).getIndexAsBitstring() + ".q";
185 throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
189 public AssignableMainMemory getMainMemory()
195 public AssignableMicroInstructionMemory getMicroInstructionMemory()
201 public long getActiveMicroInstructionAddress()
203 return activeInstructionAddress;
206 private long getCurrentMicroInstructionAddress()
208 BitVector vector = (BitVector) am2900.getHighLevelState("mpm_address");
209 return vector.isBinary() ? vector.getUnsignedValueLong() : -1;
213 public void addActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
215 amicListeners.add(listener);
219 public void removeActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
221 amicListeners.remove(listener);
224 private void notifyActiveMicroInstructionChangedListeners(long oldAddress, long newAddress)
226 amicListeners.forEach(l -> l.instructionChanged(oldAddress, newAddress));