-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemory.py
More file actions
135 lines (120 loc) · 5.61 KB
/
Copy pathMemory.py
File metadata and controls
135 lines (120 loc) · 5.61 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
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
from Helper import Helper
from OpCodeLookup import *
class Memory:
FULL_BYTE_MASK = 0xFF
NEGATIVE_BIT_MASK = 1 << 7
OVERFLOW_BIT_MASK = 1 << 6
DUMMY_BIT_MASK = 1 << 5
BREAK_BIT_MASK = 1 << 4
DECIMAL_BIT_MASK = 1 << 3
INTERRUPT_BIT_MASK = 1 << 2
ZERO_BIT_MASK = 1 << 1
CARRY_BIT_MASK = 1 << 0
def __init__(self):
self.registers = dict()
self.mainMemory = bytearray(2 ** 16)
self.opCode = 0
self.operand1 = None
self.operand2 = None
self.initialize_registers()
@staticmethod
def display_register_header():
"""print the formatted header which labels the content from the
registers"""
print(" PC OPC INS AMOD OPRND AC XR YR SP NV-BDIZC")
def display_registers(self, past_pc):
""" prints the contents of the special registers in the format specified by
the project writeup"""
constructed_string = ""
pc_display = Helper.get_hex_string_from_decimal_number(past_pc)
while len(pc_display) < 4:
pc_display = " " + pc_display
constructed_string += pc_display
constructed_string += " "
constructed_string += Helper.get_hex_string_from_decimal_number(self.opCode)
constructed_string += " "
constructed_string += str(OpCodeLookup.lookupTable[self.opCode][0].name)
constructed_string += " "
address_spacing = str(OpCodeLookup.lookupTable[self.opCode][1].value[1])
while len(address_spacing) < 5:
address_spacing = " " + address_spacing
constructed_string += address_spacing
constructed_string += " "
if self.operand1 is None:
constructed_string += "--"
else:
constructed_string += Helper.get_hex_string_from_decimal_number(self.operand1)
constructed_string += " "
if self.operand2 is None:
constructed_string += "--"
else:
constructed_string += Helper.get_hex_string_from_decimal_number(self.operand2)
constructed_string += " "
constructed_string += Helper.get_hex_string_from_decimal_number(self.registers["AC"])
constructed_string += " "
constructed_string += Helper.get_hex_string_from_decimal_number(self.registers["X"])
constructed_string += " "
constructed_string += Helper.get_hex_string_from_decimal_number(self.registers["Y"])
constructed_string += " "
constructed_string += Helper.get_hex_string_from_decimal_number(self.registers["SP"])
constructed_string += " "
constructed_string += Helper.get_string_from_signed_byte(self.registers["SR"])
print(constructed_string)
def initialize_registers(self):
"""resets all special registers to default values"""
self.registers["PC"] = 0
self.registers["AC"] = 0
self.registers["X"] = 0
self.registers["Y"] = 0
self.registers["SR"] = Memory.DUMMY_BIT_MASK
self.registers["SP"] = 0xFF
def save_values_to_memory(self, starting_memory_location, values):
"""takes in a starting location and a list of memory values, then
stores those values into memory starting at the starting location"""
for value in values:
self.mainMemory[starting_memory_location] = value
starting_memory_location += 1
def display_data_from_range(self, starting_address, end_address):
"""display the data in memory from the eight floored starting
value to the ending value. Format with 8 values per line and the memory location as a tag
before the eight values"""
starting_address -= starting_address % 8
for i in range(starting_address, end_address + 1):
if i % 8 == 0:
print(Helper.get_hex_string_from_decimal_number(i, 3), end=" ")
print(Helper.get_hex_string_from_decimal_number(self.mainMemory[i]), end=' ')
if i % 8 == 7 or i == end_address:
print()
def display_single_data(self, location):
"""display the data within memory at the given location"""
print(Helper.get_hex_string_from_decimal_number(self.mainMemory[location]))
def parse_intel_hex(self, object_file_name):
""" loads a given file, interprets it as an intex hex file, and then passes
the parsed values to the memory setting function """
values = []
with open(object_file_name, 'r') as obj_file:
line = obj_file.readline()
while line:
start_code = line[0:1]
byte_count = line[1:3]
address = line[3:7]
record_type = line[7:9]
num_bytes = Helper.get_decimal_number_from_hex_string(byte_count)
curr_start = 9
for i in range(0, num_bytes):
temp = line[curr_start:curr_start+2]
curr_start += 2
values.append(Helper.get_decimal_number_from_hex_string(temp))
self.save_values_to_memory(Helper.get_decimal_number_from_hex_string(address), values)
line = obj_file.readline()
def push_to_stack(self, val):
"""push a value to the stack and decrement the stack pointer"""
stack_pointer = self.registers["SP"]
self.mainMemory[Helper.get_decimal_number_from_hex_string("100") + stack_pointer] = val
self.registers["SP"] -= 1
def pop_from_stack(self):
"""pop a value off the stack and increment the stack pointer"""
self.registers["SP"] += 1
stack_pointer = self.registers["SP"]
val = self.mainMemory[Helper.get_decimal_number_from_hex_string("100") + stack_pointer]
return val