1 package net.mograsim.logic.model.am2900.machine;
3 import java.util.HashSet;
6 import java.util.function.Consumer;
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;
37 public class Am2900Machine implements Machine
39 private AbstractAm2900MachineDefinition machineDefinition;
40 private LogicModelModifiable logicModel;
41 private ModelComponent am2900;
42 private Timeline timeline;
43 private CoreModelParameters params;
44 private AssignableMainMemory mainMemory;
45 private AssignableMicroInstructionMemory instMemory;
46 private AssignableMPROM mprom;
47 private CoreClock clock;
48 private long activeInstructionAddress;
50 private final Set<ActiveMicroInstructionChangedListener> amicListeners;
52 public Am2900Machine(LogicModelModifiable model, AbstractAm2900MachineDefinition am2900MachineDefinition)
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<>();
60 CoreModelParametersBuilder paramsBuilder = new CoreModelParametersBuilder();
61 paramsBuilder.gateProcessTime = 50;
62 paramsBuilder.hardcodedComponentProcessTime = paramsBuilder.gateProcessTime * 5;
63 paramsBuilder.wireTravelTime = 10;
64 params = paramsBuilder.build();
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 ->
79 long oldAddress = activeInstructionAddress;
80 activeInstructionAddress = getCurrentMicroInstructionAddress();
81 notifyActiveMicroInstructionChangedListeners(oldAddress, activeInstructionAddress);
87 public MachineDefinition getDefinition()
89 return machineDefinition;
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?
107 private void setRegistersTo(List<Register> registers, Bit value)
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()));
114 private void setRegisterGroupTo(List<RegisterGroup> registerGroups, Bit value)
116 for (RegisterGroup rg : registerGroups)
118 setRegistersTo(rg.getRegisters(), value);
119 setRegisterGroupTo(rg.getSubGroups(), value);
124 public LogicModel getModel()
129 public ModelComponent getAm2900()
135 public Timeline getTimeline()
141 public CoreModelParameters getCoreModelParameters()
147 public CoreClock getClock()
153 public BitVector getRegister(Register r)
155 return castAm2900Register(r).read(am2900);
159 public void setRegister(Register r, BitVector value)
161 castAm2900Register(r).write(am2900, value);
165 public void addRegisterListener(Register r, Consumer<BitVector> listener)
167 castAm2900Register(r).addListener(am2900, listener);
171 public void removeRegisterListener(Register r, Consumer<BitVector> listener)
173 castAm2900Register(r).removeListener(am2900, listener);
176 private static Am2900Register castAm2900Register(Register r)
178 if (r instanceof Am2900Register)
179 return (Am2900Register) r;
180 throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
184 public AssignableMainMemory getMainMemory()
190 public AssignableMicroInstructionMemory getMicroInstructionMemory()
196 public AssignableMPROM getMPROM()
202 public long getActiveMicroInstructionAddress()
204 return activeInstructionAddress;
207 private long getCurrentMicroInstructionAddress()
209 BitVector vector = (BitVector) am2900.getHighLevelState("mpm_address");
210 return vector.isBinary() ? vector.getUnsignedValueLong() : -1;
214 public void addActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
216 amicListeners.add(listener);
220 public void removeActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
222 amicListeners.remove(listener);
225 private void notifyActiveMicroInstructionChangedListeners(long oldAddress, long newAddress)
227 amicListeners.forEach(l -> l.instructionChanged(oldAddress, newAddress));