1 package net.mograsim.machine.mi;
3 import java.util.HashSet;
5 import net.mograsim.machine.MemoryObserver;
6 import net.mograsim.machine.standard.memory.MemoryException;
8 public class StandardMicroInstructionMemory implements MicroInstructionMemory
10 private MicroInstruction[] data;
11 private MicroInstructionMemoryDefinition definition;
12 private HashSet<MemoryObserver> observers = new HashSet<>();
14 public StandardMicroInstructionMemory(MicroInstructionMemoryDefinition definition)
16 if(definition.size() > Integer.MAX_VALUE)
17 throw new MemoryException("Size of MicroInstructionMemory must be an int, not a long");
18 this.definition = definition;
19 data = new MicroInstruction[(int) definition.size()];
22 private int translate(long address)
24 return (int) (address - definition.getMinimalAddress());
28 public MicroInstruction getCell(long address)
30 int translatedAddress = translate(address);
31 MicroInstruction actual = data[translatedAddress];
33 actual = data[translatedAddress] = definition.getMicroInstructionDefinition().createDefaultInstruction();
38 public void setCell(long address, MicroInstruction data)
40 this.data[translate(address)] = data;
41 notifyObservers(address);
45 public void registerObserver(MemoryObserver ob)
51 public void deregisterObserver(MemoryObserver ob)
57 public void notifyObservers(long address)
59 observers.forEach(ob -> ob.update(address));
63 public MicroInstructionMemoryDefinition getDefinition()