-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom_dictionaries.py
48 lines (38 loc) · 1.51 KB
/
custom_dictionaries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from collections import UserDict
class RegisterDict(UserDict):
def __init__(self, state, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)
self.state = state
def __setitem__(self, key: str, value: str):
value = f"0x{int(value, 16):02x}"
if key == "M":
mem_addr = self.state.get_mem_addr_register_pair("H")
self.state.memory[mem_addr] = value
super().__setitem__(key, value)
def __getitem__(self, key: str):
if key == "M":
return self.state.get_register_pair_value("H")
return super().__getitem__(key)
def update(self, *args, **kwargs):
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
other = dict(*args, **kwargs)
for key in other:
self[key] = other[key]
class MemoryDict(UserDict):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.update(*args, **kwargs)
def __setitem__(self, key: str, value: str):
key = f"0x{int(key, 16):04x}"
value = f"0x{int(value, 16):02x}"
super().__setitem__(key, value)
def __getitem__(self, key: str):
return super().__getitem__(key)
def update(self, *args, **kwargs):
if len(args) > 1:
raise TypeError("update expected at most 1 arguments, got %d" % len(args))
other = dict(*args, **kwargs)
for key in other:
self[key] = other[key]