-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinterpreter.rb
412 lines (351 loc) · 12.8 KB
/
interpreter.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
require './cellnum'
require 'pp'
require './disassembler.rb'
class CoreThread
attr_accessor :player
attr_accessor :eip,:iret,:r1,:r2,:acc,:ptr
attr_accessor :interrupt_enable,:Imem,:Icza,:Iclk #interrupt flags
attr_accessor :Tmem,:Tcza,:Tclk #interrupt vector target addresses
attr_accessor :ImemPtr,:IczaVal, :IclkVal #interrupt values
attr_accessor :equal,:above,:smaller,:clk
attr_accessor :ILastAcc, :ILastMem
attr_accessor :lastExec
attr_reader :threadID
def to_s
"thread: #{@player.name}, @#{eip.to_i} acc,ptr,r1,r2=#{[@acc,@ptr,@r1,@r2].join ","}"
end
def initialize(eip,threadid,player)
@eip,@iret,@r1,@r2,@acc,@ptr,@player=Cellnum.new(eip),Cellnum.new(eip),Cellnum.new(0),Cellnum.new(0),Cellnum.new(0),Cellnum.new(0),player
@clk=Cellnum.new(0)
@interrupt_enable,@Imem,@Icza,@Iclk =false,false,false,false
@Tmem,@Tcza,@Tclk =nil,nil,nil
@ImemPtr,@IczaVal,@IclkVal =Cellnum.new(0),Cellnum.new(0),Cellnum.new(0)
@equal,@above,@smaller=false,false,false
@ILastAcc=@acc
@ILastMem=Cellnum.new(0)
@threadID=threadID
@lastExec=Cellnum.new(eip)
end
def tick(interpreter)
if @IclkVal!=0 and @clk!=0 and (@clk % @IclkVal)==0
@Iclk = true
end
@clk+=1
if (thismem=interpreter.getCellNum(@ImemPtr))!=@ILastMem
@Imem = true
@ILastMem=thismem
end
if @acc==@IczaVal && @acc!=@ILastAcc
@Icza =true
end
@ILastAcc=@acc
end
end
class Player
attr_accessor :threads, :playerID
attr_reader :threadcount, :threadindex
attr_accessor :name
attr_accessor :lastRead, :lastWritten
attr_accessor :bot_id
attr_accessor :wins
attr_accessor :ties
def initialize
@threads=[]
@threadindex=0
@threadcount=0
@lastRead=[]
@lastWritten=[]
end
#return the next thread to run
def choose
return nil if @threads.length==0
@threadindex+=1
@threadindex%[email protected]
return @threads[@threadindex]
end
def split(position)
t=CoreThread.new(position,@threadcount+=1,self)
@threads<<t
end
def kill(thread)
@threads-=[thread]
end
def alive?
@threads.length!=0
end
end
class Interpreter
attr_accessor :players, :arena, :quit, :next, :blacklist
attr_reader :cmds
def initialize(config)
@config=config
@arena=Array.new(config.arena_length){Cellnum.new(0)}
@quit,@next=false
@cmds={
:add => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)+value(p2,t)) },
:sub => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)-value(p2,t)) },
:mul => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)*value(p2,t)) },
:div => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)/value(p2,t)) },
:mod => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)%value(p2,t)) },
:and => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)&value(p2,t)) },
:or => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)|value(p2,t)) },
:xor => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p1,t)^value(p2,t)) },
:not => lambda{|p1,t| arithmetic_op( p1 , t, value(p1,t).not) },
:inc => lambda{|p1,t| arithmetic_op( p1 , t, value(p1,t)+1) },
:dec => lambda{|p1,t| arithmetic_op( p1 , t, value(p1,t)-1) },
:neg => lambda{|p1,t| arithmetic_op( p1 , t, value(p1,t)*-1) },
:ada => lambda{|p1,t| arithmetic_op( [:immediate,:acc,nil] , t, value([:immediate,:acc,nil],t)+value(p1,t)) },
:sba => lambda{|p1,t| arithmetic_op( [:immediate,:acc,nil] , t, value([:immediate,:acc,nil],t)-value(p1,t)) },
:ica => lambda{|t| arithmetic_op( [:immediate,:acc,nil] , t, value([:immediate,:acc,nil],t)+1) },
:dca => lambda{|t| arithmetic_op( [:immediate,:acc,nil] , t, value([:immediate,:acc,nil],t)-1) },
:mov => lambda{|p1,p2,t| arithmetic_op( p1 , t, value(p2,t)) },
:mvp => lambda{|p1,t| arithmetic_op( [:direct,:ptr,nil] , t, value(p1,t)) },
:mva => lambda{|p1,t| arithmetic_op( [:immediate,:acc,nil] , t, value(p1,t)) },
:jmp => lambda{|p1,t| jmp_op( t, value(p1,t),true) },
:jie => lambda{|p1,t| jmp_op( t, value(p1,t),t.equal) },
:jia => lambda{|p1,t| jmp_op( t, value(p1,t),t.above) },
:jis => lambda{|p1,t| jmp_op( t, value(p1,t),t.smaller) },
:jne => lambda{|p1,t| jmp_op( t, value(p1,t),!t.equal) },
:jna => lambda{|p1,t| jmp_op( t, value(p1,t),!t.above) },
:jns => lambda{|p1,t| jmp_op( t, value(p1,t),!t.smaller) },
:cmp => lambda{|p1,p2,t| cmp_op(t,value(p1,t),value(p2,t))},
:sti => lambda{|t| t.interrupt_enable=true; t.eip+=1;},
:cli => lambda{|t| t.interrupt_enable=false; t.eip+=1;},
:rti => lambda{|t| t.interrupt_enable=true; t.eip=t.iret;},
:clk => lambda{|p1,p2,t|
t.IclkVal=Cellnum.new([value(p1,t).to_i,4].max)
t.Tclk=value(p2,t);
if t.Tclk==0
t.Tclk=nil
else
t.Tclk+=t.eip
end
t.Iclk=false
t.eip+=1
t.clk=Cellnum.new(0)
},
#TODO set the lastMem and lastcza values
:mem => lambda{|p1,p2,t|
t.ImemPtr=t.eip+value(p1,t);
t.Tmem=value(p2,t);
if t.Tmem==0
t.Tmem=nil
else
t.Tmem+=t.eip
end
t.Imem=false
t.ILastMem=getCellNum(t.ImemPtr)
t.eip+=1
},
:cza => lambda{|p1,p2,t|
t.IczaVal=value(p1,t);
t.Tcza=value(p2,t);
if t.Tcza==0
t.Tcza=nil
else
t.Tcza+=t.eip
end
t.Icza=false
t.ILastAcc=t.acc
t.eip+=1
}
}
end
def cmp_op(thread,value1,value2)
thread.equal=(value1==value2)
thread.smaller=(value1<value2)
thread.above=(value1>value2)
thread.eip+=1
end
def arithmetic_op(param,thread,value)
store(param,thread,value)
thread.equal=(value==0)
thread.smaller=(value<0)
thread.above=(value>0)
thread.eip+=1
end
def jmp_op(thread,value, do_jump)
if do_jump
thread.eip+=value.to_i
else
thread.eip+=1
end
end
def do_instruction(code,player,thread)
cmd,param1,param2=code
raise "process die due to blacklisted cmd" if @blacklist and @blacklist.include? cmd
old_eip=thread.eip
thread.lastExec=thread.eip
if @cmds.include? cmd
exec=@cmds[cmd]
if exec.arity==3 && param1 && param2
exec.call(param1,param2,thread)
elsif exec.arity==2 && param1
exec.call(param1,thread)
elsif exec.arity==1
exec.call(thread)
else
raise "process died due to invalid parameters for #{cmd}"
end
elsif cmd==:splt
if param1
if player.threads.length < @config.max_player_threads
player.split(old_eip+value(param1,thread))
end
thread.eip+=1
else
raise "process died due to invalid parameters for #{cmd}"
end
else
raise "process died due to invalid opcode #{cmd}"
end
post_op(param1,thread,old_eip)
post_op(param2,thread,old_eip)
end
def value(param,thread)
type,val,op=param
#puts "val #{val.inspect}"
nval= case val
when :ptr then Cellnum.new(thread.ptr)
when :acc then Cellnum.new(thread.acc)
when :r1 then Cellnum.new(thread.r1)
when :r2 then Cellnum.new(thread.r2)
when Fixnum,Bignum then Cellnum.new(val)
end
#puts "nval #{nval.inspect}"
return nval if type==:immediate
thread.player.lastRead<<thread.eip+nval
nval = getCellNum(thread.eip+nval)
return nval if type==:direct
thread.player.lastRead<<thread.eip+nval
nval = getCellNum(thread.eip+nval)
return nval if type==:indirect
raise "Interpretation failed, invalid type #{param.inspect}"
end
def store(param,thread,value)
type,val,op=param
if type==:immediate
case val
when :ptr then thread.ptr=value ; return
when :acc then thread.acc=value ; return
when :r1 then thread.r1=value ; return
when :r2 then thread.r2=value ; return
else raise "ERROR in Store: Trying to change a constant instead of a register"
end
end
addr= thread.eip + case val
when :ptr then thread.ptr
when :acc then thread.acc
when :r1 then thread.r1
when :r2 then thread.r2
when Fixnum,Bignum then val
else raise "ERROR in Store: Wrong direct parameter"
end
addr=thread.eip+getCellNum(addr) if type==:indirect
setCellNum(addr,value,thread.player)
end
def getCellNum(addr)
@arena[addr.to_i % @arena.length]
end
def getCellCode(addr)
#Disassembler.bin_to_code(getCellNum(addr).to_bin)
getCellNum(addr).to_code
end
def getCellChar(addr)
begin
char=case getCellCode(addr)[0].to_s
when /^([mjc])/ then $1
when "die" then "!"
else "a"
end
return char
rescue Exception => e
return "#"
end
end
def setCellNum(addr,value,owner=nil)
@arena[addr.to_i % @arena.length]=Cellnum.new(value)
@arena[addr.to_i % @arena.length].owner=owner
owner.lastWritten<<addr if owner
end
def post_op(param,thread,eip)
#we need the old eip befor a jump
#TODO needs to be fixed in runcmd
#should be fixed now (testing)!
oldeip=thread.eip
thread.eip=eip
type,val,op=param
if op
cval=value(param,thread)
case op
when :inc then cval+=1
when :dec then cval-=1
end
store(param,thread,cval)
end
thread.eip=oldeip
end
def round_callback(round)
end
def end_callback(players)
end
def run_callback(players)
end
def run(maxrounds,*players)
players.each_index { |e| players[e].playerID=e}
@players=players # for use in the callbacks
finished=false
rounds=0
run_callback(players)
while not finished
#puts "round #{rounds}"
rounds+=1
alivecount=0
players.sort_by{rand}.each do |p|
p.lastWritten=[]
p.lastRead=[]
t=p.choose
next if not t
t.tick(self) #update interrupt values
#check for interrupt occurance
if t.interrupt_enable
if t.Imem && t.Tmem
t.Imem=false
t.iret=t.eip
t.eip=t.Tmem
elsif t.Icza && t.Tcza
t.Icza=false
t.iret=t.eip
t.eip=t.Tcza
elsif t.Iclk && t.Tclk
t.Iclk=false
t.iret=t.eip
t.eip=t.Tclk
end
end
#try to execute code or kill thread
begin
begin
code=getCellNum(t.eip).to_code
ensure
if $DEBUG
STDERR.puts "thread: #{t}"
STDERR.puts "executes: #{code.inspect}"
#STDERR.puts "in context #{@arena.map {|e| e.to_i}.inspect}"
end
end
do_instruction(code,p,t)
rescue
if $DEBUG
STDERR.puts "thread #{t} from player #{t.player.name} died at addr. #{t.eip} in round #{rounds} because of #{$!}\n #{$!.backtrace.join("\n")}"
end
p.kill(t)
end
alivecount+=1 if p.alive?
end
round_callback(rounds)
finished=true if @next or @quit or alivecount<=1 or rounds>=maxrounds
end
end_callback(players)
end
end