Implemented some infrastructure for MPROMs
[Mograsim.git] / plugins / net.mograsim.machine / src / net / mograsim / machine / mi / StandardMPROM.java
1 package net.mograsim.machine.mi;
2
3 import java.math.BigInteger;
4 import java.util.HashSet;
5
6 import net.mograsim.logic.core.types.BitVector;
7 import net.mograsim.machine.standard.memory.MemoryException;
8
9 public class StandardMPROM implements MPROM
10 {
11         private BitVector[] data;
12         private MPROMDefinition definition;
13         private HashSet<MemoryCellModifiedListener> observers = new HashSet<>();
14
15         public StandardMPROM(MPROMDefinition definition)
16         {
17                 if (definition.size() > Integer.MAX_VALUE)
18                         throw new MemoryException("Size of MPROM must be an int, not a long");
19                 this.definition = definition;
20                 data = new BitVector[(int) definition.size()];
21         }
22
23         private int translate(long address)
24         {
25                 return (int) (address - definition.getMinimalAddress());
26         }
27
28         @Override
29         public BitVector getCell(long address)
30         {
31                 int translatedAddress = translate(address);
32                 BitVector cell = data[translatedAddress];
33                 if (cell == null)
34                         cell = data[translatedAddress] = BitVector.from(address * 16, definition.getMicroInstructionMemoryAddressBits());
35                 return cell;
36         }
37
38         @Override
39         public BigInteger getCellAsBigInteger(long address)
40         {
41                 return getCell(address).getUnsignedValue();
42         }
43
44         @Override
45         public void setCell(long address, BitVector data)
46         {
47                 this.data[translate(address)] = data;
48                 notifyMemoryChanged(address);
49         }
50
51         @Override
52         public void registerCellModifiedListener(MemoryCellModifiedListener ob)
53         {
54                 observers.add(ob);
55         }
56
57         @Override
58         public void deregisterCellModifiedListener(MemoryCellModifiedListener ob)
59         {
60                 observers.remove(ob);
61         }
62
63         private void notifyMemoryChanged(long address)
64         {
65                 observers.forEach(ob -> ob.update(address));
66         }
67
68         @Override
69         public MPROMDefinition getDefinition()
70         {
71                 return definition;
72         }
73 }