forked from milindkulkarni/RiscSim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.py
More file actions
57 lines (46 loc) · 2.14 KB
/
memory.py
File metadata and controls
57 lines (46 loc) · 2.14 KB
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
class Memory(dict) :
#initialize memory by making clear what the valid segments are: .globals, .stack, .heap, .text, .strings
#IMPORTANT: memory is not byte addressable -- can only be addressed at word granularity -- means we do not have to actually manage byte mapping
# and we don't have to worry about endianness
#IMPORTANT: we do not actually allow reading/writing to the text space, but Memory keeps track of the address range since it's part of the memory configuration
def __init__(self, globs = (0x20000000, 0x30000000),
stack = (0x30000000, 0x40000000),
heap = (0x40000000, 0x80000000),
text = (0x00000000, 0x10000000),
strings = (0x1000000, 0x20000000)) :
super().__init__()
self.globs = globs
self.stack = stack
self.heap = heap
self.text = text
self.strings = strings
self.r_count = 0
self.w_count = 0
def __getitem__(self, key) :
self.__validateAddress(key)
self.r_count += 1
return super().__getitem__(key)
def __setitem__(self, key, value) :
self.__validateAddress(key)
self.w_count += 1
super().__setitem__(key, value)
def __missing__(self, key) :
assert False, "Reading from uninitialized memory location: " + hex(key)
def __validateAddress(self, key) :
#key needs to be an integer
assert(type(key) == int), "Can only address memory with integers"
#key needs to be a multiple of 4
assert(key % 0x4 == 0), "Memory must be addressed at byte granularity"
#key needs to be in a segment
valid = False
for s in [self.globs, self.stack, self.heap, self.strings] :
if (key >= s[0] and key < s[1]) :
valid = True
assert valid == True, "Address not in a mapped segment"
def getAccessCounts(self):
return (self.r_count, self.w_count, self.r_count + self.w_count)
if __name__ == '__main__' :
memory = Memory()
memory[0x20100000] = 80
print(memory[0x20100000])
print(memory[0x10100004]) #should fail