-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwrite.py
107 lines (94 loc) · 3.52 KB
/
readwrite.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
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
import re
class iotools:
@staticmethod
def read_instructions(filename):
library = {}
with open(filename, 'r') as file:
for line in file:
op, code = line.strip().lower().split(':')
op = op.strip()
code = code.strip()
library[op] = code
return library
@staticmethod
def write_to_binary(filename, bin_arr):
filename = filename + '.txt'
with open(filename, 'w') as file:
for line in bin_arr:
file.write(line + '\n')
@staticmethod
def write_to_hex(filename, bin_arr, comment_arr):
filename = filename + '.hex'
hex_arr = conversion.bin_to_hex_arr(bin_arr)
with open(filename, 'w') as file:
for i in range(len(hex_arr)):
file.write(hex_arr[i] + ';' + comment_arr[i] + '\n')
@staticmethod
def read_compile(filename, library = dict):
bit_arr = []
comment_arr = []
with open(filename, 'r') as file:
for line in file:
if line.strip().startswith('%'):
bit_arr.append('0000000000000')
else:
try:
opcode, dest, data, comment = re.split(r'\s{1,15}', line.lower().strip())
if comment.startswith('%'):
comment = comment[1:]
else:
raise ValueError('Error due to earlier errors')
except:
try:
opcode, dest, data = re.split(r'\s{1,15}', line.lower().strip())
comment = ''
except:
opcode, data = re.split(r'\s{1,15}', line.lower().strip())
dest = 'n'
comment = ''
opcode_bin = library[opcode]
dest_bin = library[dest]
if data.startswith('$'):
data = data[1:]
else:
data_bin = conversion.decimal_to_binary(data)
bit_arr.append(opcode_bin + dest_bin + data_bin)
comment_arr.append(comment)
return [bit_arr, comment_arr]
class conversion:
@staticmethod
def decimal_to_binary(num_str):
num = int(num_str)
binary_str = bin(num)[2:]
if len(binary_str) < 8:
binary_str = '0' * (8 - len(binary_str)) + binary_str
elif len(binary_str) > 8:
binary_str = binary_str[-8:]
return binary_str
@staticmethod
def binary_to_hex(bin_str):
num = int(bin_str, 2)
hex_str = hex(num)[2:]
return hex_str.zfill(4)
@staticmethod
def bin_to_hex_arr(bin_arr):
hex_arr = []
for i in range (len(bin_arr)):
hex_arr.append(conversion.binary_to_hex(bin_arr[i]))
return hex_arr
@staticmethod
def fill_array(bin_arr):
if len(bin_arr) < 64:
remaining = 64 - len(bin_arr)
bin_arr.extend(['0000000000000'] * remaining)
elif len(bin_arr) > 64:
bin_arr = bin_arr[:64]
return bin_arr
@staticmethod
def fill_str_arr(str_arr):
if len(str_arr) < 64:
remaining = 64 - len(str_arr)
str_arr.extend([''] * remaining)
elif len(str_arr) > 64:
str_arr = str_arr[:64]
return str_arr