-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory_bus.py
35 lines (30 loc) · 1.33 KB
/
memory_bus.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
# MemoryBus handles reading and writing to memory along with initialising memory with data from file
class MemoryBus:
def __init__(self):
self.memory = {} # dictionary to simulate memory storage
def read(self, memory_address):
# read data from memory at specific address
if memory_address in self.memory:
return self.memory[memory_address]
else:
raise KeyError(f"Memory address {memory_address} not found.")
def write(self, memory_address, value):
# write data to memory at specific address
self.memory[memory_address] = value
def initialise_memory(self, filename):
try:
with open(filename, "r") as file:
for line in file:
address, value = map(int, line.strip().split(','))
self.memory[address] = value
except FileNotFoundError:
raise FileNotFoundError(f"The file '{filename}' was not found.")
except ValueError as e:
raise ValueError(f"Could not convert data from file. {e}")
def print_memory_bus(self):
if not self.memory:
print("Memory is empty.")
else:
print("Memory contents:")
for address, value in self.memory.items():
print(f"Address: {address}, Value: {value}")