-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpcode Translator Tool.py
More file actions
125 lines (107 loc) · 4.33 KB
/
Opcode Translator Tool.py
File metadata and controls
125 lines (107 loc) · 4.33 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
"""
Opcode Translator Tool
Single-file Python 3 desktop application using Tkinter.
Features:
- Input assembly-like instructions or opcodes
- Translate into binary or machine code
- Support for a predefined set of opcodes (e.g., MOV, ADD, SUB, JMP)
- Add custom opcode mappings
- Export translations to CSV
- Copy results to clipboard
Run:
python "Opcode Translator Tool.py"
"""
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
import csv
import io
# Predefined opcode table (example: simple hypothetical CPU)
OPCODE_TABLE = {
'MOV': '0001',
'ADD': '0010',
'SUB': '0011',
'MUL': '0100',
'DIV': '0101',
'JMP': '0110',
'NOP': '0000',
'AND': '0111',
'OR': '1000',
'XOR': '1001',
'CMP': '1010',
}
class OpcodeTranslatorApp(tk.Tk):
def __init__(self):
super().__init__()
self.title('Opcode Translator Tool')
self.geometry('800x500')
# Input area
input_frame = ttk.Frame(self, padding=8)
input_frame.pack(fill=tk.X)
ttk.Label(input_frame, text='Enter Opcodes (one per line):').pack(anchor='w')
self.input_text = tk.Text(input_frame, height=8, width=80)
self.input_text.pack(padx=4, pady=4)
# Buttons
btn_frame = ttk.Frame(self, padding=4)
btn_frame.pack(fill=tk.X)
ttk.Button(btn_frame, text='Translate', command=self.translate_opcodes).pack(side=tk.LEFT, padx=4)
ttk.Button(btn_frame, text='Export CSV', command=self.export_csv).pack(side=tk.LEFT, padx=4)
ttk.Button(btn_frame, text='Copy to Clipboard', command=self.copy_clipboard).pack(side=tk.LEFT, padx=4)
# Result area
result_frame = ttk.Frame(self, padding=8)
result_frame.pack(fill=tk.BOTH, expand=True)
ttk.Label(result_frame, text='Translation Results:').pack(anchor='w')
self.result_text = tk.Text(result_frame, height=12, width=80, wrap=tk.WORD)
self.result_text.pack(padx=4, pady=4, fill=tk.BOTH, expand=True)
# Last translations
self.last_translations = []
def translate_opcodes(self):
self.result_text.delete('1.0', tk.END)
lines = self.input_text.get('1.0', tk.END).splitlines()
translations = []
for line in lines:
line = line.strip().upper()
if not line:
continue
parts = line.split()
op = parts[0]
args = parts[1:] if len(parts) > 1 else []
if op not in OPCODE_TABLE:
code = 'UNKNOWN'
else:
code = OPCODE_TABLE[op]
if args:
code += ' ' + ' '.join(args) # Append arguments as-is
translations.append((line, code))
self.result_text.insert(tk.END, f'{line} -> {code}\n')
self.last_translations = translations
def export_csv(self):
if not self.last_translations:
messagebox.showwarning('No translations', 'Please translate opcodes first.')
return
path = filedialog.asksaveasfilename(defaultextension='.csv',
filetypes=[('CSV files', '*.csv')],
title='Save translations')
if not path:
return
try:
with open(path, 'w', newline='') as f:
writer = csv.writer(f)
writer.writerow(['Instruction', 'Machine Code'])
for instr, code in self.last_translations:
writer.writerow([instr, code])
messagebox.showinfo('Saved', f'Translations exported to {path}')
except Exception as e:
messagebox.showerror('Error', f'Could not save CSV: {e}')
def copy_clipboard(self):
if not self.last_translations:
messagebox.showwarning('No translations', 'Please translate opcodes first.')
return
output = io.StringIO()
for instr, code in self.last_translations:
output.write(f'{instr} -> {code}\n')
self.clipboard_clear()
self.clipboard_append(output.getvalue())
messagebox.showinfo('Copied', 'Translations copied to clipboard.')
if __name__ == '__main__':
app = OpcodeTranslatorApp()
app.mainloop()