-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmemory.py
203 lines (178 loc) · 5.15 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
##########################################################################
# This file is part of d00ks.
#
# d00ks is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# d00ks is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with d00ks. If not, see <http://www.gnu.org/licenses/>.
##########################################################################
"""
This file contains classes for dealing with memory
in the ARM simulator.
"""
from ctypes import *
import struct
class MemoryError(Exception):
pass
class Store(object):
def __repr__(self):
return str(self)
class DCB(Store):
"""Represents a DCB operation"""
def __init__(self, dcb_list):
super(DCB, self).__init__()
self.bytes = ""
for item in dcb_list:
if type(item) == str:
for char in item:
self.bytes += char
elif type(item) == int:
char = c_ubyte(item)
char = chr(char.value)
self.bytes += char
def size(self):
return len(self.bytes)
def store(self, mem, addr):
for byte in self.bytes:
byte = ord(byte)
mem.strb(addr, byte)
addr += 1
def __str__(self):
return "DCB " + repr(self.bytes)
class SPACE(Store):
"""Represents a SPACE operation"""
def __init__(self, length):
super(SPACE, self).__init__()
self.length = length
def __str__(self):
return "SPACE " + hex(self.length)
def size(self):
return self.length
def store(self, mem, addr):
pass
class Memory(object):
"""
Represents the RAM of a program.
Memory is stored using a byte buffer created
by the python ctypes module.
"""
def __init__(self, size=4096):
super(Memory, self).__init__()
self.size = size
self.buffer = create_string_buffer(size)
self.startaddr = 0xA1000000
self.debugmode = False
self.debug_clean()
def set_debugmode(self, boolean):
self.debugmode = boolean
def debug_clean(self):
self.readaccesses = []
self.writeaccesses = []
def debug(self):
"""
Prints the contents of memory.
"""
buf = ""
for i in range(self.startaddr, self.startaddr + self.size):
if i % 32 == 0:
buf += "\n0x%X:"%i
if i % 4 == 0:
buf += " "
addr = (i - (i%4)) + (4 - (i % 4) - 1)
buf += "%02X"%self.ldrb(addr)
print buf
def debug_char(self):
"""
Prints the contents of memory.
"""
buf = ""
for i in range(self.startaddr, self.startaddr + self.size):
if i % 32 == 0:
buf += "\n0x%X:"%i
if i % 4 == 0:
buf += " "
addr = i #(i - (i%4)) + (4 - (i % 4) - 1)
byte = self.ldrb(addr)
buf += "%s"%(chr(byte)+' ' if byte in range(65, 128) else ' .')
print buf
def realaddr(self, addr):
"""
Calculates the offset in the byte buffer
from a virtual address.
"""
real = addr - self.startaddr
if real >= self.size:
raise MemoryError("Out of bounds access at 0x%X"%addr)
return real
def strb(self, addr, byte):
"""Store byte"""
byte = c_uint(byte)
byte = byte.value & 0xFF
addr = self.realaddr(addr)
struct.pack_into("B", self.buffer, addr, byte)
if self.debugmode:
self.writeaccesses.append(addr)
def strh(self, addr, hw):
"""Store halfword"""
if addr & 0x1:
raise MemoryError("Halfword stores must be halfword-aligned!")
hw = c_ushort(hw)
hw = hw.value & 0xFFFF
addr = self.realaddr(addr)
struct.pack_into("H", self.buffer, addr, hw)
if self.debugmode:
self.writeaccesses.append(addr)
self.writeaccesses.append(addr+1)
def strw(self, addr, word):
"""Store word"""
if addr & 0x3:
raise MemoryError("Word stores must be word-aligned!")
word = c_uint(word)
word = word.value & 0xFFFFFFFF
addr = self.realaddr(addr)
struct.pack_into("I", self.buffer, addr, word)
if self.debugmode:
self.writeaccesses.append(addr)
self.writeaccesses.append(addr+1)
self.writeaccesses.append(addr+2)
self.writeaccesses.append(addr+3)
def ldrb(self, addr):
"""Load byte"""
addr = self.realaddr(addr)
(val,) = struct.unpack_from("B", self.buffer, addr)
if self.debugmode:
self.readaccesses.append(addr)
return val
def ldrh(self, addr):
"""Load halfword"""
if addr & 0x1:
raise MemoryError("Halfword reads must be halfword-aligned!")
addr = self.realaddr(addr)
(val,) = struct.unpack_from("H", self.buffer, addr)
if self.debugmode:
self.readaccesses.append(addr)
self.readaccesses.append(addr+1)
return val
def ldrw(self, addr):
"""Load word"""
if addr & 0x3:
raise MemoryError("Word stores must be word-aligned!")
addr = self.realaddr(addr)
(val,) = struct.unpack_from("I", self.buffer, addr)
if self.debugmode:
self.readaccesses.append(addr)
self.readaccesses.append(addr+1)
self.readaccesses.append(addr+2)
self.readaccesses.append(addr+3)
return val
def range_to_list(self, addr, length):
"""Return a range of memory as a list of bytes"""
return [self.ldrb(x) for x in range(addr, addr+length)]