Fleshed out Memory and Microprogramming interfaces
[Mograsim.git] / net.mograsim.machine / src / net / mograsim / machine / mi / StandardMicroprogramMemory.java
1 package net.mograsim.machine.mi;
2
3 import java.util.HashSet;
4
5 import net.mograsim.machine.MemoryDefinition;
6 import net.mograsim.machine.MemoryObserver;
7 import net.mograsim.machine.standard.memory.MemoryException;
8
9 class StandardMicroprogramMemory implements MicroprogramMemory
10 {
11         private MicroInstruction[] data;
12         private MemoryDefinition definition;
13         private HashSet<MemoryObserver> observers;
14         
15         StandardMicroprogramMemory(MemoryDefinition definition)
16         {
17                 if(definition.size() > Integer.MAX_VALUE)
18                         throw new MemoryException("Size of MicroprogramMemory must be an int, not a long");
19                 this.definition = definition;
20                 data = new MicroInstruction[(int) definition.size()];
21         }
22         
23         private int translate(long address)
24         {
25                 return (int) (address - definition.getMinimalAddress());
26         }
27         
28         @Override
29         public MicroInstruction getCell(long address)
30         {
31                 return data[translate(address)];
32         }
33
34         @Override
35         public void setCell(long address, MicroInstruction data)
36         {
37                 this.data[translate(address)] = data;
38         }
39
40         @Override
41         public void registerObserver(MemoryObserver ob)
42         {
43                 observers.add(ob);
44         }
45
46         @Override
47         public void deregisterObserver(MemoryObserver ob)
48         {
49                 observers.remove(ob);
50         }
51
52         @Override
53         public void notifyObservers(long address)
54         {
55                 observers.forEach(ob -> ob.update(address));
56         }
57
58         @Override
59         public MemoryDefinition getDefinition()
60         {
61                 return definition;
62         }
63
64 }