-
Notifications
You must be signed in to change notification settings - Fork 0
/
process.py
57 lines (49 loc) · 2.04 KB
/
process.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
__author__ = 'marcus'
from base import Memory
class FileProcess(object):
IS_PC = 0
IS_MEM = 1
IS_REG = 2
mem = Memory()
def __init__(self, mem):
self.mem = mem
def processLine(self, line):
if line[0] is ('#'): # if starts with a pound symbol is a comment line and we ignore
return "#"
else:
return line[0].translate(None, "[]") # get the memory address on the first value of the input file
def getRealAddress(self, location):
try:
return (int(location, 16), self.IS_MEM) # see if it's a hex address
except:
if location == "PC":
return (0, self.IS_PC)
else:
return (self.mem.get_register_num(location), self.IS_REG)
def process(self, file):
for line in file:
location = self.processLine(line)
if location is "#":
continue
addr = self.getRealAddress(location)
iterLines = iter(line) # make a iterator over the array
next(iterLines) # skip the first element
counter = 0
for s in iterLines: # iterate over each string of the line
try:
address = addr[0] + counter
counter += 4
val = int(s, 16) # check if the value is a hex string
# print val
if addr[1] == self.IS_MEM:
self.mem.set_val_to_address(val, address)
# print "Addr: " + str(address)
# print "Addr Val: " + str(self.mem.mem[address >> 4])
elif addr[1] == self.IS_PC:
self.mem.get_registers().set_initial_pc(val)
elif addr[1] == self.IS_REG:
self.mem.get_registers().set_value_for_register(address, val)
else:
continue
except ValueError:
break # break the loop if the value is no longer a hex string