Merge branch 'development' of
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / components / CoreMicroInstructionMemory.java
1 package net.mograsim.machine.mi.components;
2
3 import java.util.List;
4
5 import net.mograsim.logic.core.components.BasicCoreComponent;
6 import net.mograsim.logic.core.timeline.Timeline;
7 import net.mograsim.logic.core.timeline.TimelineEventHandler;
8 import net.mograsim.logic.core.types.Bit;
9 import net.mograsim.logic.core.types.BitVector;
10 import net.mograsim.logic.core.wires.CoreWire.ReadEnd;
11 import net.mograsim.logic.core.wires.CoreWire.ReadWriteEnd;
12 import net.mograsim.machine.mi.MicroInstructionMemory;
13
14 public class CoreMicroInstructionMemory extends BasicCoreComponent
15 {
16         private final ReadWriteEnd data;
17         private final ReadEnd address;
18         private final MicroInstructionMemory memory;
19
20         public CoreMicroInstructionMemory(Timeline timeline, int processTime, MicroInstructionMemory memory, ReadWriteEnd data, ReadEnd address)
21         {
22                 super(timeline, processTime);
23                 this.memory = memory;
24                 this.data = data;
25                 this.address = address;
26                 memory.registerObserver(a -> update());
27                 address.registerObserver(this);
28         }
29
30         public MicroInstructionMemory getMemory()
31         {
32                 return memory;
33         }
34
35         @Override
36         public List<ReadEnd> getAllInputs()
37         {
38                 return List.of(address);
39         }
40
41         @Override
42         public List<ReadWriteEnd> getAllOutputs()
43         {
44                 return List.of(data);
45         }
46
47         @Override
48         protected TimelineEventHandler compute()
49         {
50                 if (!address.getValues().isBinary())
51                 {
52                         return e -> data.feedSignals(Bit.U.toVector(data.width()));// TODO don't always feed U, but decide to feed X or U.
53                 }
54                 long addressed = address.getValues().getUnsignedValueLong();
55                 BitVector storedData = memory.getCell(addressed).toBitVector();
56                 return e -> data.feedSignals(storedData);
57         }
58 }