2020-11-18 22:03:45 +01:00
|
|
|
import os
|
|
|
|
import struct
|
|
|
|
|
2020-12-02 21:18:58 +01:00
|
|
|
|
2020-12-17 14:21:35 +01:00
|
|
|
class Memory(list):
|
2020-12-25 05:30:14 +01:00
|
|
|
def __init__(self, source, initial, size):
|
2020-12-17 14:21:35 +01:00
|
|
|
m = [0] * size
|
|
|
|
self.extend(m)
|
2020-12-25 05:30:14 +01:00
|
|
|
if len(initial) == 0:
|
|
|
|
cells = int(os.path.getsize(source) / 4)
|
|
|
|
f = open(source, "rb")
|
|
|
|
i = 0
|
|
|
|
for cell in list(struct.unpack(cells * "i", f.read())):
|
|
|
|
self[i] = cell
|
|
|
|
i = i + 1
|
|
|
|
f.close()
|
|
|
|
else:
|
|
|
|
i = 0
|
|
|
|
for cell in initial:
|
|
|
|
self[i] = cell
|
|
|
|
i = i + 1
|
2020-12-02 21:18:58 +01:00
|
|
|
|
2020-12-28 15:04:30 +01:00
|
|
|
def load_image(self, name):
|
|
|
|
cells = int(os.path.getsize(name) / 4)
|
|
|
|
f = open(name, "rb")
|
|
|
|
i = 0
|
|
|
|
for cell in list(struct.unpack(cells * "i", f.read())):
|
|
|
|
self[i] = cell
|
|
|
|
i = i + 1
|
|
|
|
f.close()
|
|
|
|
|
2020-11-19 00:57:17 +01:00
|
|
|
def size(self):
|
2020-12-17 14:21:35 +01:00
|
|
|
return len(self)
|