X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2Fmemory%2FWordAddressableMemoryComponent.java;fp=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Fcomponents%2Fmemory%2FWordAddressableMemoryComponent.java;h=821b1f36ccffe9aefedae1d386474433658fe350;hb=1c7820abab8dc28939e35275f24a9c25e139add5;hp=0000000000000000000000000000000000000000;hpb=bffda9003a621f34f431230c265b76a79597a1ae;p=Mograsim.git diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java new file mode 100644 index 00000000..821b1f36 --- /dev/null +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/components/memory/WordAddressableMemoryComponent.java @@ -0,0 +1,77 @@ +package net.mograsim.logic.core.components.memory; + +import java.util.List; + +import net.mograsim.logic.core.components.BasicComponent; +import net.mograsim.logic.core.timeline.Timeline; +import net.mograsim.logic.core.types.Bit; +import net.mograsim.logic.core.types.BitVector; +import net.mograsim.logic.core.wires.Wire.ReadEnd; +import net.mograsim.logic.core.wires.Wire.ReadWriteEnd; + +/** + * A memory component that only allows access to words of a specific length + */ +public class WordAddressableMemoryComponent extends BasicComponent +{ + private final WordAddressableMemory memory; + private final static Bit read = Bit.ONE; + + private ReadWriteEnd data; + private ReadEnd rWBit, address; + + /** + * @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 + * a memory word + * @param rWBit The value of the 0th bit dictates the mode: 0: Write, 1: Read + * @param address The bits of this ReadEnd address the memory cell to read/write + */ + public WordAddressableMemoryComponent(Timeline timeline, int processTime, long minimalAddress, long maximalAddress, ReadWriteEnd data, + ReadEnd rWBit, ReadEnd address) + { + super(timeline, processTime); + this.data = data; + this.rWBit = rWBit; + this.address = address; + data.registerObserver(this); + rWBit.registerObserver(this); + address.registerObserver(this); + + memory = new WordAddressableMemory(data.length(), minimalAddress, maximalAddress); + } + + @Override + protected void compute() + { + if (!address.hasNumericValue()) + { + if (read.equals(rWBit.getValue())) + data.feedSignals(BitVector.of(Bit.U, data.length())); + else + data.clearSignals(); + return; + } + long addressed = address.getUnsignedValue(); + if (read.equals(rWBit.getValue())) + data.feedSignals(memory.getCell(addressed)); + else + { + data.clearSignals(); + System.out.println(memory); + memory.setCell(addressed, data.getValues()); + } + } + + @Override + public List getAllInputs() + { + return List.of(data, rWBit, address); + } + + @Override + public List getAllOutputs() + { + return List.of(data); + } + +} \ No newline at end of file