Added Am2900 MainMemory and MicroInstructionMemory Core/Model Components
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mi / components / CoreMicroInstructionMemory.java
diff --git a/net.mograsim.machine/src/net/mograsim/machine/mi/components/CoreMicroInstructionMemory.java b/net.mograsim.machine/src/net/mograsim/machine/mi/components/CoreMicroInstructionMemory.java
new file mode 100644 (file)
index 0000000..29793c5
--- /dev/null
@@ -0,0 +1,61 @@
+package net.mograsim.machine.mi.components;
+
+import java.util.List;
+
+import net.mograsim.logic.core.components.BasicCoreComponent;
+import net.mograsim.logic.core.timeline.Timeline;
+import net.mograsim.logic.core.timeline.TimelineEventHandler;
+import net.mograsim.logic.core.types.Bit;
+import net.mograsim.logic.core.types.BitVector;
+import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
+import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
+import net.mograsim.machine.mi.MicroInstructionMemory;
+
+public class CoreMicroInstructionMemory extends BasicCoreComponent
+{
+       private final ReadWriteEnd data;
+       private final ReadEnd address, clock;
+       private final MicroInstructionMemory memory;
+       
+       
+       public CoreMicroInstructionMemory(Timeline timeline, int processTime, MicroInstructionMemory memory, ReadWriteEnd data, ReadEnd address, ReadEnd clock)
+       {
+               super(timeline, processTime);
+               this.memory = memory;
+               this.data = data;
+               this.address = address;
+               this.clock = clock;
+       }
+
+       public MicroInstructionMemory getMemory()
+       {
+               return memory;
+       }
+       
+       @Override
+       public List<ReadEnd> getAllInputs()
+       {
+               return List.of(address, clock);
+       }
+
+       @Override
+       public List<ReadWriteEnd> getAllOutputs()
+       {
+               return List.of(data);
+       }
+
+       @Override
+       protected TimelineEventHandler compute()
+       {
+               if(clock.getValue() != Bit.ONE)
+                       return null;
+               
+               if (!address.hasNumericValue())
+               {
+                       return e -> data.feedSignals(Bit.U.toVector(data.width()));
+               }
+               long addressed = address.getUnsignedValue();
+               BitVector storedData = memory.getCell(addressed).toBitVector();
+               return e -> data.feedSignals(storedData);
+       }
+}