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.LogicCoreAdapter;
20 import net.mograsim.logic.model.serializing.IndirectModelComponentCreator;
21 import net.mograsim.machine.AssignableMainMemory;
22 import net.mograsim.machine.Machine;
23 import net.mograsim.machine.MachineDefinition;
24 import net.mograsim.machine.StandardMainMemory;
25 import net.mograsim.machine.mi.AssignableMPROM;
26 import net.mograsim.machine.mi.AssignableMicroInstructionMemory;
27 import net.mograsim.machine.mi.MicroInstruction;
28 import net.mograsim.machine.mi.MicroInstructionDefinition;
29 import net.mograsim.machine.mi.StandardMPROM;
30 import net.mograsim.machine.mi.StandardMicroInstructionMemory;
31 import net.mograsim.machine.mi.parameters.MicroInstructionParameter;
32 import net.mograsim.machine.mi.parameters.ParameterClassification;
33 import net.mograsim.machine.registers.Register;
34 import net.mograsim.machine.registers.RegisterGroup;
36 public class Am2900Machine implements Machine
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;
48 private final Set<ActiveMicroInstructionChangedListener> amicListeners;
50 public Am2900Machine(LogicModelModifiable model, AbstractAm2900MachineDefinition am2900MachineDefinition)
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<>();
58 CoreModelParameters params = new CoreModelParameters();
59 params.gateProcessTime = 50;
60 params.hardcodedComponentProcessTime = params.gateProcessTime * 5;
61 params.wireTravelTime = 10;
62 mainMemory = new AssignableMainMemory(new StandardMainMemory(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 ->
75 long oldAddress = activeInstructionAddress;
76 activeInstructionAddress = getCurrentMicroInstructionAddress();
77 notifyActiveMicroInstructionChangedListeners(oldAddress, activeInstructionAddress);
83 public MachineDefinition getDefinition()
85 return machineDefinition;
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?
103 private void setRegistersTo(List<Register> registers, Bit value)
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()));
110 private void setRegisterGroupTo(List<RegisterGroup> registerGroups, Bit value)
112 for (RegisterGroup rg : registerGroups)
114 setRegistersTo(rg.getRegisters(), value);
115 setRegisterGroupTo(rg.getSubGroups(), value);
120 public LogicModel getModel()
125 public ModelComponent getAm2900()
131 public Timeline getTimeline()
137 public CoreClock getClock()
143 public BitVector getRegister(Register r)
145 return castAm2900Register(r).read(am2900);
149 public void setRegister(Register r, BitVector value)
151 castAm2900Register(r).write(am2900, value);
155 public void addRegisterListener(Register r, Consumer<BitVector> listener)
157 castAm2900Register(r).addListener(am2900, listener);
161 public void removeRegisterListener(Register r, Consumer<BitVector> listener)
163 castAm2900Register(r).removeListener(am2900, listener);
166 private static Am2900Register castAm2900Register(Register r)
168 if (r instanceof Am2900Register)
169 return (Am2900Register) r;
170 throw new IllegalArgumentException("Not a register of an Am2900Machine: " + r);
174 public AssignableMainMemory getMainMemory()
180 public AssignableMicroInstructionMemory getMicroInstructionMemory()
186 public AssignableMPROM getMPROM()
192 public long getActiveMicroInstructionAddress()
194 return activeInstructionAddress;
197 private long getCurrentMicroInstructionAddress()
199 BitVector vector = (BitVector) am2900.getHighLevelState("mpm_address");
200 return vector.isBinary() ? vector.getUnsignedValueLong() : -1;
204 public void addActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
206 amicListeners.add(listener);
210 public void removeActiveMicroInstructionChangedListener(ActiveMicroInstructionChangedListener listener)
212 amicListeners.remove(listener);
215 private void notifyActiveMicroInstructionChangedListeners(long oldAddress, long newAddress)
217 amicListeners.forEach(l -> l.instructionChanged(oldAddress, newAddress));