1
0
mirror of https://github.com/mnaberez/py65.git synced 2024-11-07 21:05:41 +00:00

Use defaultdict for storing subscriber lists

This commit is contained in:
Mike Naberezny 2012-01-08 20:12:32 -08:00
parent d28ef8f658
commit 6180eddb8a

View File

@ -1,20 +1,22 @@
from collections import defaultdict
class ObservableMemory:
def __init__(self, subject=None, addrWidth=16):
self.physMask = 0xffff
if addrWidth > 16:
# even with 32-bit address space, model only 256k memory
self.physMask = 0x3ffff
if subject is None:
subject = (self.physMask+1) * [0x00]
self._subject = subject
self._read_subscribers = {}
self._write_subscribers = {}
self._read_subscribers = defaultdict(list)
self._write_subscribers = defaultdict(list)
def __setitem__(self, address, value):
address &= self.physMask
callbacks = self._write_subscribers.get(address, [])
callbacks = self._write_subscribers[address]
for callback in callbacks:
result = callback(address, value)
@ -25,7 +27,7 @@ class ObservableMemory:
def __getitem__(self, address):
address &= self.physMask
callbacks = self._read_subscribers.get(address, [])
callbacks = self._read_subscribers[address]
final_result = None
for callback in callbacks: