-
Notifications
You must be signed in to change notification settings - Fork 11
/
bfsim.rb
executable file
·188 lines (162 loc) · 3.63 KB
/
bfsim.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
#!/usr/bin/env ruby
require './common'
class BFSim
def initialize
@a = 0
@b = 0
@c = 0
@d = 0
@pc = 0
@bp = 0
@sp = 0
@cond = false
@mem = [0] * (UINT_MAX+1)
end
def set(r, v)
case r
when :a
@a = v
when :b
@b = v
when :c
@c = v
when :d
@d = v
when :bp
@bp = v
when :sp
@sp = v
when :pc
@pc = v
else
raise "invalid dest reg #{r}"
end
end
def src(o)
case o
when :a
@a
when :b
@b
when :c
@c
when :d
@d
when :bp
@bp
when :sp
@sp
when :pc
@pc
else
o
end
end
def run(code, data)
if !$quiet
STDERR.puts "code size: #{code.size}"
STDERR.puts "data size: #{data.size}"
end
if code.size > UINT_MAX
STDERR.puts "too much code size: #{code.size}"
end
data.each do |d, i|
@mem[i] = d
end
running = true
while running
npc = nil
code[@pc].each do |op, *args, lineno|
hp = @mem[256]
if @sp != 0 && @sp <= hp
STDERR.puts "stack overflow!!! #{@sp} vs #{hp}"
end
if $verbose
STDERR.puts "PC=#@pc A=#@a B=#@b C=#@c D=#@d BP=#@bp SP=#@sp"
STDERR.print "STK:"
32.times{|i|
STDERR.print " #{@mem[-32+i]}"
}
STDERR.puts
STDERR.puts "#{op} #{args} at #{lineno}"
end
if !op
raise "out of bound pc=#{@pc}"
end
npc = @pc + 1
case op
when :mov
set(args[0], src(args[1]))
when :add
set(args[0], (src(args[0]) + src(args[1])) & UINT_MAX)
when :sub
set(args[0], (src(args[0]) - src(args[1])) & UINT_MAX)
when :load
s = src(args[1])
v = @mem[s]
if s < 256
STDERR.puts "zero-page load: addr=#{s} (#{v}) @#{lineno}"
end
set(args[0], @mem[src(args[1])])
when :store
v = src(args[0])
d = src(args[1])
if d < 256
STDERR.puts "zero-page store: addr=#{d} (#{v}) @#{lineno}"
end
@mem[d] = v
when :jmp, :jeq, :jne, :jlt, :jgt, :jle, :jge
ok = true
case op
when :jeq
ok = src(args[1]) == src(args[2])
when :jne
ok = src(args[1]) != src(args[2])
when :jlt
ok = src(args[1]) < src(args[2])
when :jgt
ok = src(args[1]) > src(args[2])
when :jle
ok = src(args[1]) <= src(args[2])
when :jge
ok = src(args[1]) >= src(args[2])
end
if ok
npc = src(args[0])
end
when :eq
set(args[0], src(args[0]) == src(args[1]) ? 1 : 0)
when :ne
set(args[0], src(args[0]) != src(args[1]) ? 1 : 0)
when :lt
set(args[0], src(args[0]) < src(args[1]) ? 1 : 0)
when :gt
set(args[0], src(args[0]) > src(args[1]) ? 1 : 0)
when :le
set(args[0], src(args[0]) <= src(args[1]) ? 1 : 0)
when :ge
set(args[0], src(args[0]) >= src(args[1]) ? 1 : 0)
when :putc
putc src(args[0])
when :getc
c = STDIN.read(1)
set(args[0], c ? c.ord : 0)
when :exit
running = false
break
end
end
@pc = npc
end
if !$quiet
STDERR.puts "last heap: #{@mem[256]}"
end
end
end
if $0 == __FILE__
require './bfasm'
asm = BFAsm.new
code, data = asm.parse(File.read(ARGV[0]))
sim = BFSim.new
sim.run(code, data)
end