1 package net.mograsim.machine.standard.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;
8 import net.mograsim.machine.MainMemory;
9 import net.mograsim.machine.MainMemoryDefinition;
11 public class WordAddressableMemory implements MainMemory
13 private final int cellWidth;
14 private final long minimalAddress, maximalAddress;
15 private final MainMemoryDefinition definition;
16 private final int pageSize = 64;
18 private HashMap<Long, Page> pages;
20 public WordAddressableMemory(MainMemoryDefinition definition)
23 this.cellWidth = definition.getCellWidth();
24 this.minimalAddress = definition.getMinimalAddress();
25 this.maximalAddress = definition.getMaximalAddress();
26 this.definition = definition;
27 this.pages = new HashMap<>();
31 public void setCell(long address, BitVector b)
33 if (address < minimalAddress || address > maximalAddress)
34 throw new IndexOutOfBoundsException(String.format("Memory address out of bounds! Minimum: %d Maximum: %d Actual: %d",
35 minimalAddress, maximalAddress, address));
36 long page = address / pageSize;
37 int offset = (int) (address % pageSize);
38 Page p = pages.get(Long.valueOf(page));
40 pages.put(page, p = new Page());
45 public BitVector getCell(long address)
47 long page = address / pageSize;
48 int offset = (int) (address % pageSize);
49 Page p = pages.get(Long.valueOf(page));
51 return BitVector.of(Bit.U, cellWidth);
52 return p.getCell(offset);
57 private BitVector[] memory;
61 memory = new BitVector[pageSize];
64 public BitVector getCell(int index)
66 BitVector b = memory[index];
68 return BitVector.of(Bit.U, cellWidth);
72 public void setCell(int index, BitVector bits)
74 if (bits.length() != cellWidth)
75 throw new IllegalArgumentException(String.format(
76 "BitVector to be saved in memory cell has unexpected length. Expected: %d Actual: %d", cellWidth, bits.length()));
81 public String toString()
83 return Arrays.deepToString(memory);
88 public MainMemoryDefinition getDefinition()