1 package net.mograsim.machine.mi;
3 import java.util.HashSet;
5 import net.mograsim.machine.standard.memory.MemoryException;
7 public class StandardMicroInstructionMemory implements MicroInstructionMemory
9 private MicroInstruction[] data;
10 private MicroInstructionMemoryDefinition definition;
11 private HashSet<MemoryCellModifiedListener> observers = new HashSet<>();
13 public StandardMicroInstructionMemory(MicroInstructionMemoryDefinition definition)
15 if (definition.size() > Integer.MAX_VALUE)
16 throw new MemoryException("Size of MicroInstructionMemory must be an int, not a long");
17 this.definition = definition;
18 data = new MicroInstruction[(int) definition.size()];
21 private int translate(long address)
23 return (int) (address - definition.getMinimalAddress());
27 public MicroInstruction getCell(long address)
29 int translatedAddress = translate(address);
30 MicroInstruction actual = data[translatedAddress];
32 actual = data[translatedAddress] = definition.getMicroInstructionDefinition().createDefaultInstruction();
37 public void setCell(long address, MicroInstruction data)
39 this.data[translate(address)] = data;
40 notifyMemoryChanged(address);
44 public void registerCellModifiedListener(MemoryCellModifiedListener ob)
50 public void deregisterCellModifiedListener(MemoryCellModifiedListener ob)
55 private void notifyMemoryChanged(long address)
57 observers.forEach(ob -> ob.update(address));
61 public MicroInstructionMemoryDefinition getDefinition()