Moved main memory to machine module
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / standard / memory / WordAddressableMemoryComponent.java
1 package net.mograsim.machine.standard.memory;
2
3 import java.util.List;
4
5 import net.mograsim.logic.core.components.BasicComponent;
6 import net.mograsim.logic.core.timeline.Timeline;
7 import net.mograsim.logic.core.types.Bit;
8 import net.mograsim.logic.core.types.BitVector;
9 import net.mograsim.logic.core.wires.Wire.ReadEnd;
10 import net.mograsim.logic.core.wires.Wire.ReadWriteEnd;
11
12 /**
13  * A memory component that only allows access to words of a specific length
14  */
15 public class WordAddressableMemoryComponent extends BasicComponent
16 {
17         private final WordAddressableMemory memory;
18         private final static Bit read = Bit.ONE;
19
20         private ReadWriteEnd data;
21         private ReadEnd rWBit, address;
22
23         /**
24          * @param data    The bits of this ReadEnd are the value that is written to/read from memory; The bit width of this wire is the width of
25          *                a memory word
26          * @param rWBit   The value of the 0th bit dictates the mode: 0: Write, 1: Read
27          * @param address The bits of this ReadEnd address the memory cell to read/write
28          */
29         public WordAddressableMemoryComponent(Timeline timeline, int processTime, long minimalAddress, long maximalAddress, ReadWriteEnd data,
30                         ReadEnd rWBit, ReadEnd address)
31         {
32                 super(timeline, processTime);
33                 this.data = data;
34                 this.rWBit = rWBit;
35                 this.address = address;
36                 data.registerObserver(this);
37                 rWBit.registerObserver(this);
38                 address.registerObserver(this);
39
40                 memory = new WordAddressableMemory(data.length(), minimalAddress, maximalAddress);
41         }
42
43         @Override
44         protected void compute()
45         {
46                 if (!address.hasNumericValue())
47                 {
48                         if (read.equals(rWBit.getValue()))
49                                 data.feedSignals(BitVector.of(Bit.U, data.length()));
50                         else
51                                 data.clearSignals();
52                         return;
53                 }
54                 long addressed = address.getUnsignedValue();
55                 if (read.equals(rWBit.getValue()))
56                         data.feedSignals(memory.getCell(addressed));
57                 else
58                 {
59                         data.clearSignals();
60                         memory.setCell(addressed, data.getValues());
61                 }
62         }
63
64         @Override
65         public List<ReadEnd> getAllInputs()
66         {
67                 return List.of(data, rWBit, address);
68         }
69
70         @Override
71         public List<ReadWriteEnd> getAllOutputs()
72         {
73                 return List.of(data);
74         }
75 }