-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdcpu.py
175 lines (134 loc) · 4.23 KB
/
dcpu.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
import subprocess
import tempfile
import os
import re
import time
import threading
def link(files):
out_fd, out_fname = tempfile.mkstemp()
os.fdopen(out_fd).close()
process_flags = ["dtld", "-o", out_fname, "-k", "none"]
filenames = []
for file in files:
fd, fname = tempfile.mkstemp()
f = os.fdopen(fd, 'wb')
f.write(file)
f.close()
filenames.append(fname)
process_flags.append(fname)
proc = subprocess.Popen(process_flags, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
res, err = proc.communicate()
for file in filenames:
os.remove(file)
final = open(out_fname)
res = final.read()
final.close()
return (res, err)
def assemble_file(code, binary=False):
code = '\n'.join(code.split('/'))
code = '\n'.join(code.split('\\'))
process_flags = ["dtasm", "-o", "-", "-"]
if binary:
process_flags.append("--binary")
process = subprocess.Popen(process_flags, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
results = process.communicate(code)
res = results[0]
err = results[1]
return (res, err)
def assemble_binary(code):
code = '///'.join(code.split('\\\\\\'))
files = code.split('///')
if len(files) > 1:
file_binaries = []
err = ""
for file in files:
assembled = assemble_file(file)
if assembled[0]:
file_binaries.append(assembled[0])
if assembled[1]:
err += assembled[1]
res, link_err = link(file_binaries)
err += link_err
return (res, err)
else:
return assemble_file(code, True)
def assemble(code):
res, err = assemble_binary(code)
words = []
i = 0
while i < len(res) / 2:
byte_one = ord(res[2*i])
byte_two = ord(res[2*i+1])
print "Bytes:", byte_one, byte_two
word = "0x%04x" % ((byte_one << 8) + byte_two)
words.append(word)
i += 1
print "Assembly attempted"
return (words, err)
def timeout(p):
if p.poll() == None:
p.kill()
hex_re = re.compile("^0x[0-9a-fA-F]+")
disasm_re = re.compile("0x[0-9a-fA-F]{4} \(0x[0-9a-fA-F]{4}\): *(>>>)? *(.+)\r?\n?")
null_re = re.compile("<null>")
def disassemble(binary_str):
byte_strings = binary_str.split(",")
print byte_strings
fd, filename = tempfile.mkstemp()
file = os.fdopen(fd, 'wb')
for byte in byte_strings:
byte = byte.strip()
if hex_re.match(byte):
byte = int(byte, 16)
else:
byte = int(byte)
file.write(chr(byte >> 8))
file.write(chr(byte & 0xff))
file.close()
length = hex(len(byte_strings))
print length
args = 'dtdb -c "disasm 0x0 ' + length + '" ' + filename
print args
proc = subprocess.Popen(args, stderr=subprocess.PIPE, shell=True)
proc.wait()
res = proc.stderr.read()
matches = disasm_re.findall(res)
res = []
for match in matches:
if not null_re.match(match[1]):
res.append(match[1].replace('\r', ''))
response = ' / '.join(res)
os.remove(filename)
return response
register_re = re.compile(r"([A-Z]{1,2}):\s*0x([\dA-F]+)")
def execute(code):
binary, err = assemble_binary(code)
if err and not ("warning" in err):
return ("", err)
num_words = len(binary) / 2
fd, filename = tempfile.mkstemp()
file = os.fdopen(fd, 'wb')
file.write(binary)
file.close()
start = time.time()
proc = subprocess.Popen(['dtemu', '-t', '-h', filename], stderr=subprocess.PIPE)
t = threading.Timer(5, timeout, [proc])
while proc.poll() == None:
if time.time() - start > 5:
proc.kill()
final = time.time() - start
err = proc.stderr.read()
err_lines = err.split("\n")
for i in range(11):
err_lines.pop()
errors = "\n".join(err_lines)
os.remove(filename)
register_matches = register_re.findall(err)
changed_registers = []
for match in register_matches:
if match[1] != "0000":
changed_registers.append(match[0] + ":0x" + match[1])
registers = ', '.join(changed_registers)
ms = final * 1000
response = "[" + str(num_words) + " words][" + registers + "][%dms]" % round(ms)
return (response, errors)