-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvm.py
292 lines (279 loc) · 6.19 KB
/
vm.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import struct
import sys
memory = open('challenge.bin','rb').read()
eip = 0
reg = [0]*8
stack = []
inline = ''
debugprint = [False]*22
ops = ['halt','set','push','pop','eq','gt','jmp','jt','jf','add','mult','mod',
'and','or','not','rmem','wmem','call','ret','out','in','noop']
play = '''
take tablet
doorway
north
north
bridge
continue
down
east
take empty lantern
west
west
passage
ladder
west
south
north
take can
use can
use lantern
west
ladder
darkness
continue
west
west
west
west
north
take red coin
north
east
take concave coin
down
take corroded coin
up
west
west
take blue coin
up
take shiny coin
down
east
use blue coin
use red coin
use shiny coin
use concave coin
use corroded coin
north
take teleporter
use teleporter
take business card
take strange book
debug teleporter
use teleporter
north
north
north
north
north
north
north
north
north
take orb
north
east
east
north
west
south
east
east
west
north
north
east
vault
take mirror
use mirror
'''.strip().split('\n')
def printdebug(op,s):
if debugprint[op]:
print(s)
def debugcommand(s):
global debugprint
if 'debug' not in s:
return
for i in s.split()[1:]:
if i == 'teleporter':
reg[7] = 25734
# nop out ackermann
NOP = 21
for addr in range(0x1571, 0x157a):
writemem(addr, NOP)
elif i == 'regs':
print('Regs: {}'.format(reg))
elif i == 'stack':
print('Stack: {}'.format(stack))
elif 'toggle' in i:
for j in i.split('-')[1:]:
ind = ops.index(j)
debugprint[ind] = not debugprint[ind]
def parseopcode():
global eip
r = struct.unpack('<H',memory[2*eip:2*eip+2])[0]
eip += 1
return r
def parsearg():
global eip
r = struct.unpack('<H',memory[2*eip:2*eip+2])[0]
eip += 1
if r <= 32767:
return r
elif 32768 <= r <= 32775:
return reg[r-32768]
else:
print("Invalid arg {}!".format(r))
sys.exit(1)
def parsereg():
global eip
r = struct.unpack('<H',memory[2*eip:2*eip+2])[0]
eip += 1
return r-32768
def readmem(i):
return struct.unpack('<H',memory[2*i:2*i+2])[0]
def writemem(a,b):
global memory
w = struct.pack('<H',b)
memory = memory[:2*a] + w + memory[2*a+2:]
def opcode(n):
global eip, inline, play
if n == 0:
printdebug(0,'{}: halt'.format(eip))
print('Exiting...')
sys.exit(0)
elif n == 1:
a = parsereg()
b = parsearg()
printdebug(1,'{}: set {} {}'.format(eip,a,b))
reg[a] = b
elif n == 2:
a = parsearg()
printdebug(2,'{}: push {}'.format(eip,a))
stack.append(a)
elif n == 3:
a = parsereg()
printdebug(3,'{}: pop {}'.format(eip,a))
reg[a] = stack.pop()
elif n == 4:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(4,'{}: eq {} {} {}'.format(eip,a,b,c))
if b == c:
reg[a] = 1
else:
reg[a] = 0
elif n == 5:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(5,'{}: gt {} {} {}'.format(eip,a,b,c))
if b > c:
reg[a] = 1
else:
reg[a] = 0
elif n == 6:
a = parsearg()
printdebug(6,'{}: jmp {}'.format(eip,a))
eip = a
elif n == 7:
a = parsearg()
b = parsearg()
printdebug(7,'{}: jt {} {}'.format(eip,a,b))
if a != 0:
eip = b
elif n == 8:
a = parsearg()
b = parsearg()
printdebug(8,'{}: jf {} {}'.format(eip,a,b))
if a == 0:
eip = b
elif n == 9:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(9,'{}: add {} {} {}'.format(eip,a,b,c))
reg[a] = (b+c)%32768
elif n == 10:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(10,'{}: mult {} {} {}'.format(eip,a,b,c))
reg[a] = (b*c)%32768
elif n == 11:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(11,'{}: mod {} {} {}'.format(eip,a,b,c))
reg[a] = (b%c)%32768
elif n == 12:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(12,'{}: and {} {} {}'.format(eip,a,b,c))
reg[a] = b&c
elif n == 13:
a = parsereg()
b = parsearg()
c = parsearg()
printdebug(13,'{}: or {} {} {}'.format(eip,a,b,c))
reg[a] = b|c
elif n == 14:
a = parsereg()
b = parsearg()
printdebug(14,'{}: not {} {}'.format(eip,a,b))
reg[a] = (~b)%32768
elif n == 15:
a = parsereg()
b = parsearg()
printdebug(15,'{}: rmem {} {}'.format(eip,a,b))
reg[a] = readmem(b)
elif n == 16:
a = parsearg()
b = parsearg()
printdebug(16,'{}: wmem {} {}'.format(eip,a,b))
writemem(a,b)
elif n == 17:
a = parsearg()
printdebug(17,'{}: call {}'.format(eip,a))
stack.append(eip)
eip = a
elif n == 18:
printdebug(18,'{}: ret'.format(eip))
eip = stack.pop()
elif n == 19:
a = parsearg()
printdebug(19,'{}: out {}'.format(eip,a))
print(chr(a),end='')
elif n == 20:
if inline == '':
if len(play) > 0:
inline = play[0]+"\n"
play = play[1:]
print("> {}".format(inline))
if 'debug' in inline:
debugcommand(inline)
inline = play[0]+"\n"
play = play[1:]
else:
inline = 'debug'
while 'debug' in inline:
#print("value: {}".format(readmem(0xf70)))
#print("timer: {:015b}".format(readmem(0xf72)))
inline = input()+'\n'
debugcommand(inline)
a = parsereg()
printdebug(20,'{}: in {}'.format(eip,a))
reg[a] = ord(inline[0])
inline = inline[1:]
elif n == 21:
printdebug(21,'{}: noop'.format(eip))
pass
else:
print("Unknown opcode {}!".format(n))
sys.exit(1)
while True:
opcode(parseopcode())