-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemory.py
68 lines (49 loc) · 2.19 KB
/
memory.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
pyxt.memory - Memory devices for PyXT.
"""
# Standard library imports
import array
# Six imports
import six
# PyXT imports
from pyxt.bus import Device
# Classes
# These 2 classes are memory only devices and do no implement I/O port operations.
class RAM(Device): # pylint:disable=abstract-method
""" A device emulating a RAM storage device. """
def __init__(self, size, **kwargs):
super(RAM, self).__init__(**kwargs)
self.contents = array.array("B", (0,) * size)
# Inline these calls directly to the array object for speed.
self.mem_read_byte = self.contents.__getitem__
self.mem_write_byte = self.contents.__setitem__
def __repr__(self):
return "<%s(size=0x%x)>" % (self.__class__.__name__, len(self.contents))
def get_memory_size(self):
return len(self.contents)
# def mem_read_byte(self, offset):
# return self.contents[offset]
def mem_read_word(self, offset):
return self.contents[offset + 1] << 8 | self.contents[offset]
# def mem_write_byte(self, offset, value):
# self.contents[offset] = value
def mem_write_word(self, offset, value):
self.contents[offset], self.contents[offset + 1] = (value & 0x00FF), ((value & 0xFF00) >> 8)
class ROM(RAM): # pylint:disable=abstract-method
""" A device emulating a ROM storage device. """
def __init__(self, size, init_file = None, **kwargs):
super(ROM, self).__init__(size, **kwargs)
if init_file is not None:
self.load_from_file(init_file)
# Ensure this points at a version that doesn't allow setting.
self.mem_write_byte = self.local_mem_write_byte
def load_from_file(self, filename, offset = 0):
""" Load this ROM with the contents of a file. """
with open(filename, "rb") as fileptr:
data = fileptr.read()
for index, byte in enumerate(six.iterbytes(data), start = offset):
self.contents[index] = byte
def local_mem_write_byte(self, offset, value):
pass
def mem_write_word(self, offset, value):
pass