1 package net.mograsim.logic.core.components.memory;
3 import java.util.Arrays;
4 import java.util.HashMap;
6 import net.mograsim.logic.core.types.Bit;
7 import net.mograsim.logic.core.types.BitVector;
9 public class WordAddressableMemory
11 private final int cellWidth;
12 private final long minimalAddress, maximalAddress;
13 private final int pageSize = 64;
15 private HashMap<Long, Page> pages;
17 public WordAddressableMemory(int cellWidth, long minimalAddress, long maximalAddress)
20 this.cellWidth = cellWidth;
21 this.minimalAddress = minimalAddress;
22 this.maximalAddress = maximalAddress;
23 this.pages = new HashMap<>();
26 public void setCell(long address, BitVector b)
28 if (address < minimalAddress || address > maximalAddress)
29 throw new IndexOutOfBoundsException(String.format("Memory address out of bounds! Minimum: %d Maximum: %d Actual: %d",
30 minimalAddress, maximalAddress, address));
31 long page = address / pageSize;
32 int offset = (int) (address % pageSize);
33 Page p = pages.get(Long.valueOf(page));
35 pages.put(page, p = new Page());
39 public BitVector getCell(long address)
41 long page = address / pageSize;
42 int offset = (int) (address % pageSize);
43 Page p = pages.get(Long.valueOf(page));
45 return BitVector.of(Bit.U, cellWidth);
46 return p.getCell(offset);
51 private BitVector[] memory;
55 memory = new BitVector[pageSize];
58 public BitVector getCell(int index)
60 BitVector b = memory[index];
62 return BitVector.of(Bit.U, cellWidth);
66 public void setCell(int index, BitVector bits)
68 if (bits.length() != cellWidth)
69 throw new IllegalArgumentException(String.format(
70 "BitVector to be saved in memory cell has unexpected length. Expected: %d Actual: %d", cellWidth, bits.length()));
75 public String toString()
77 return Arrays.deepToString(memory);