Moved main memory to machine module
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / standard / memory / WordAddressableMemory.java
1 package net.mograsim.machine.standard.memory;
2
3 import java.util.Arrays;
4 import java.util.HashMap;
5
6 import net.mograsim.logic.core.types.Bit;
7 import net.mograsim.logic.core.types.BitVector;
8
9 public class WordAddressableMemory
10 {
11         private final int cellWidth;
12         private final long minimalAddress, maximalAddress;
13         private final int pageSize = 64;
14
15         private HashMap<Long, Page> pages;
16
17         public WordAddressableMemory(int cellWidth, long minimalAddress, long maximalAddress)
18         {
19                 super();
20                 this.cellWidth = cellWidth;
21                 this.minimalAddress = minimalAddress;
22                 this.maximalAddress = maximalAddress;
23                 this.pages = new HashMap<>();
24         }
25
26         public void setCell(long address, BitVector b)
27         {
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));
34                 if (p == null)
35                         pages.put(page, p = new Page());
36                 p.setCell(offset, b);
37         }
38
39         public BitVector getCell(long address)
40         {
41                 long page = address / pageSize;
42                 int offset = (int) (address % pageSize);
43                 Page p = pages.get(Long.valueOf(page));
44                 if (p == null)
45                         return BitVector.of(Bit.U, cellWidth);
46                 return p.getCell(offset);
47         }
48
49         private class Page
50         {
51                 private BitVector[] memory;
52
53                 public Page()
54                 {
55                         memory = new BitVector[pageSize];
56                 }
57
58                 public BitVector getCell(int index)
59                 {
60                         BitVector b = memory[index];
61                         if (b == null)
62                                 return BitVector.of(Bit.U, cellWidth);
63                         return memory[index];
64                 }
65
66                 public void setCell(int index, BitVector bits)
67                 {
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()));
71                         memory[index] = bits;
72                 }
73
74                 @Override
75                 public String toString()
76                 {
77                         return Arrays.deepToString(memory);
78                 }
79         }
80 }