1 package net.mograsim.machine.standard.memory;
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.MainMemory;
13 import net.mograsim.machine.MainMemoryDefinition;
16 * A memory component that only allows access to words of a specific width
18 public class CoreWordAddressableMemory extends BasicCoreComponent
20 private final MainMemory memory;
21 private final static Bit read = Bit.ONE;
23 private ReadWriteEnd data;
24 private ReadEnd rWBit, address;
27 * @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
29 * @param rWBit The value of the 0th bit dictates the mode: 0: Write, 1: Read
30 * @param address The bits of this ReadEnd address the memory cell to read/write
32 public CoreWordAddressableMemory(Timeline timeline, int processTime, MainMemory memory, ReadWriteEnd data,
33 ReadEnd rWBit, ReadEnd address)
35 super(timeline, processTime);
36 MainMemoryDefinition definition = memory.getDefinition();
37 if(data.width() != definition.getCellWidth())
38 throw new IllegalArgumentException(String.format("Bit width of data wire does not match main memory definition. Expected: %d Actual: %d", definition.getCellWidth(), data.width()));
39 if(rWBit.width() != 1)
40 throw new IllegalArgumentException(String.format("Bit width of read/write mode select wire is unexpected. Expected: 1 Actual: %d", rWBit.width()));
41 if(address.width() != definition.getMemoryAddressBits())
42 throw new IllegalArgumentException(String.format("Bit width of address wire does not match main memory definition. Expected: %d Actual: %d", definition.getMemoryAddressBits(), address.width()));
45 this.address = address;
46 data.registerObserver(this);
47 rWBit.registerObserver(this);
48 address.registerObserver(this);
54 protected TimelineEventHandler compute()
56 if (!address.hasNumericValue())
58 if (read.equals(rWBit.getValue()))
59 return e -> data.feedSignals(Bit.U.toVector(data.width()));//TODO don't always feed U, but decide to feed X or U.
60 return e -> data.clearSignals();
62 long addressed = address.getUnsignedValue();
63 if (read.equals(rWBit.getValue()))
65 BitVector storedData = memory.getCell(addressed);
66 return e -> data.feedSignals(storedData);
70 BitVector transData = data.getValues();
74 memory.setCell(addressed, transData);
80 public List<ReadEnd> getAllInputs()
82 return List.of(data, rWBit, address);
86 public List<ReadWriteEnd> getAllOutputs()