This repository has been archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Translate_I8080_to_M0Plus.py
176 lines (149 loc) · 5.4 KB
/
Translate_I8080_to_M0Plus.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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from maps.I8080_M0Plus_Mappings import combined_mapping
unrecognized = []
def translate_8080_to_m0plus(given_instruction, next_instruction=None):
instruction = ''
value_a = ''
value_b = ''
value_c = ''
# Split instruction into components
components = given_instruction.split()
print("8080 Components:", components)
# Instruction mapping dictionary
mapping = combined_mapping
try:
try:
if components[0]:
instruction = components[0]
except Exception as exception:
# print(exception)
...
try:
if components[1]:
value_a = components[1]
except Exception as exception:
# print(exception)
...
try:
if components[2]:
value_b = components[2]
except Exception as exception:
# print(exception)
...
try:
if components[3]:
value_c = components[3]
except Exception as exception:
# print(exception)
...
# Translate instruction
if instruction in mapping:
mnemonic = mapping[instruction]
translation = ""
if instruction == "MOV":
if len(components) == 3 and ',' in value_a:
register = value_a.split(',')
register = register[0]
address = value_b.strip("[]")
translation = f"{mnemonic} {register}, {address}"
return translation
else:
print("\t\tTranslation:", translation)
return "Invalid MOV instruction format"
elif instruction == "XCHG":
if len(components) == 3 and ',' in value_a:
return f"{mnemonic} A, B"
else:
return "Invalid XCHG instruction format"
elif instruction == "RLC":
if len(components) == 1:
return f"{mnemonic} A"
else:
return "Invalid RLC instruction format"
elif instruction == "SET":
if len(components) == 3 and value_a.isdigit():
bit = int(value_a)
return f"{mnemonic} {value_b}, {value_b}, #{1 << bit}"
else:
return "Invalid SET instruction format"
elif instruction == "MVI":
if len(components) == 3:
register, value = value_a, value_b
return f"{mnemonic} {register}, #{value}"
else:
return "Invalid MVI instruction format"
elif instruction == "MUL":
if len(components) == 2:
return f"{mnemonic} A, {value_a}"
else:
return "Invalid MUL instruction format"
elif instruction in ["ADD", "SUB", "INC", "DEC", "AND", "OR", "XOR", "CMP"]:
if len(components) == 3:
value_a = value_a.strip(",")
return f"{mnemonic} {value_a}, {value_b}"
else:
return f"Invalid {instruction} instruction format"
else:
return f"\tTranslation {instruction} not implemented"
else:
if instruction != '':
if instruction not in unrecognized:
unrecognized.append(instruction)
return f"\t\tInstruction {instruction} not recognized"
else:
...
except Exception as exception:
print(exception)
finally:
if len(unrecognized) > 0:
unrecognized_instruction_print = f'"Unrecognized:", {unrecognized}'
print(unrecognized_instruction_print)
def test_translate_8080_to_m0_plus():
print("test_translate_8080_to_M0Plus:")
instructions_8080 = [
# MOV test
"MOV A, @DPTR",
"MOV A, [HL]",
"MOV A, [2345H]",
"MOV A", # Malformed instruction
"MOV", # Malformed instruction
# XCHG test
"XCHG A, B",
"XCHG", # Malformed instruction
"XCHG A, B, C", # Malformed instruction
# RLC test
"RLC",
"RLC A, B", # Malformed instruction
# SET test
"SET 3, C",
"SET C, 3", # Malformed instruction
"SET 3", # Malformed instruction
# MVI test
"MVI B, 10H",
"MVI C, 20H",
"MVI B", # Malformed instruction
# MUL test
"MUL B",
"MUL C",
"MUL B, C", # Malformed instruction
# Additional tests
"ADD A, B",
"ADD A", # Malformed instruction
"SUB A, B",
"SUB A", # Malformed instruction
"INC D",
"INC", # Malformed instruction
"DEC A",
"DEC", # Malformed instruction
"OR A, B",
"OR A", # Malformed instruction
"XOR A, B",
"XOR B", # Malformed instruction
"CMP A, B",
"CMP A", # Malformed instruction
]
for i, inst in enumerate(instructions_8080):
lookahead_instruction = instructions_8080[i + 1] if i + 1 < len(instructions_8080) else None
print("Instruction to translate:\n\t", inst)
print("\t", translate_8080_to_m0plus(inst, lookahead_instruction), "\n")
if __name__ == "__main__":
test_translate_8080_to_m0_plus()