-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmagicmachine.rb
304 lines (239 loc) · 4.97 KB
/
magicmachine.rb
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
293
294
295
296
297
298
299
300
301
302
require 'pry'
class MagicMachine
class Halt < StandardError; end
OPCODES = %w{
halt set push pop eq gt jmp jt jf add mult mod and or not
rmem wmem call ret out in noop
}
ADMIN_COMMANDS = [
'quit', 'registers', 'set register', 'fix teleporter', 'stack', 'memdump'
]
attr_reader :memory, :registers, :stack
def initialize()
@address = 0
@memory = Array.new(2**15){ 0 }
@registers = Array.new(8){ 0 }
@stack = []
@input_buffer = []
@autoinput = []
end
def inspect
to_s
end
def load_bin(filename)
next_address = 0
File.open(filename) do |file|
until file.eof?
next_vals = file.read(2).bytes.map{ |b| b.ord }
@memory[next_address] = next_vals[0] + (next_vals[1] * 2**8)
next_address += 1
end
end
next_address
end
def load_autoinput(filename)
@autoinput = File.open(filename).readlines
end
def run(start_address = 0)
@address = start_address
while @address < @memory.length
opaddr = @address
opnum = @memory[opaddr]
opname = OPCODES[opnum]
operation = method("op_#{opname}")
opargs = []
operation.arity.times do
@address += 1
opargs << @memory[@address]
end
begin
ret = operation.call(*opargs)
@address += 1 if ret != 'jmp'
rescue Halt
break
end
end
end
def admin_memdump
n = 0
while true
filename = "memdumps/memdump_%04d.txt" % n
break unless File.exist?(filename)
n += 1
end
File.open(filename, 'w') do |file|
address = 0
while address < self.memory.length
opaddr = address
opnum = @memory[address]
opname = OPCODES[opnum] || opnum
operation = method("op_#{opname}") rescue nil
opargs = []
if operation
operation.arity.times do
address += 1
opargs << @memory[address]
end
else
if opnum < 256
start_string = address
end_string = address
while @memory[end_string] < 256
end_string += 1
address += 1
end
opname = "<string>"
opargs = @memory[start_string...end_string]
end
end
if ['out', '<string>'].include?(opname)
opargs.map!{ |a| (a >= 32 && a <= 126) ? a.chr : a }
opargs = opargs.join('')
else
opargs.map!{ |a| a >= (2**15) ? "reg%d" % (a % 2**15) : a }
opargs = opargs.join(' ')
end
file.write("%05d #{opname} #{opargs}\n" % opaddr)
address += 1
end
end
end
def to_register(value)
value % (2**15)
end
def resolve(value)
if value >= (2**15)
@registers[to_register(value)]
else
value
end
end
def admin_quit
op_halt
end
def admin_registers
puts @registers.inspect
end
def admin_set_register(regnum, val)
op_set(regnum.to_i, val.to_i)
end
def admin_stack
puts @stack.inspect
end
def admin_fix_teleporter
@registers[7] = 5
@memory[6027...6030] = [1, 32769, 32775]
@memory[6030...6034] = [9, 32768, 32769, 1]
end
def op_halt
raise Halt
end
def op_set(a, b)
@registers[to_register(a)] = resolve(b)
end
def op_push(a)
@stack << resolve(a)
end
def op_pop(a)
op_set(a, @stack.pop)
end
def op_eq(a, b, c)
val = resolve(b) == resolve(c) ? 1 : 0
op_set(a, val)
end
def op_gt(a, b, c)
val = resolve(b) > resolve(c) ? 1 : 0
op_set(a, val)
end
def op_jmp(a)
@address = resolve(a)
'jmp'
end
def op_jt(a, b)
op_jmp(b) if resolve(a) != 0
end
def op_jf(a, b)
op_jmp(b) if resolve(a) == 0
end
def op_add(a, b, c)
val = (resolve(b) + resolve(c)) % (2**15)
op_set(a, val)
end
def op_mult(a, b, c)
val = (resolve(b) * resolve(c)) % (2**15)
op_set(a, val)
end
def op_mod(a, b, c)
val = resolve(b) % resolve(c)
op_set(a, val)
end
def op_and(a, b, c)
val = resolve(b) & resolve(c)
op_set(a, val)
end
def op_or(a, b, c)
val = resolve(b) | resolve(c)
op_set(a, val)
end
def op_not(a, b)
val = (-resolve(b) - 1) % (2**15)
op_set(a, val)
end
def op_rmem(a, b)
val = @memory[resolve(b)]
op_set(a, val)
end
def op_wmem(a, b)
@memory[resolve(a)] = resolve(b)
end
def op_call(a)
@stack << @address + 1
op_jmp(a)
end
def op_ret
op_halt if @stack.empty?
op_jmp(@stack.pop)
end
def op_out(a)
print resolve(a).chr
end
def op_in(a)
prompt = "> "
if @input_buffer.empty?
command = nil
while command.nil?
if @autoinput.empty? || @autoinput[0] == "---\n"
print prompt
command = gets
else
command = @autoinput.shift
if command.start_with?('#')
command = nil
next
end
print "(auto) #{prompt}#{command}"
end
ADMIN_COMMANDS.each do |admin_command|
if command.start_with?(admin_command)
method_name = "admin_#{admin_command.gsub(' ', '_')}"
args = command[admin_command.length+1..-1].split
method(method_name).call(*args)
command = nil
break
end
end
end
@input_buffer = command.split('')
end
char = @input_buffer.shift
op_set(a, char.ord)
end
def op_noop
end
end
if __FILE__ == $0
mm = MagicMachine.new
mm.load_bin('challenge.bin')
mm.load_autoinput('autoinput.txt')
mm.run
end