-
Notifications
You must be signed in to change notification settings - Fork 11
/
bfcore.rb
executable file
·186 lines (151 loc) · 3.96 KB
/
bfcore.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
#!/usr/bin/env ruby
require './common'
class BFCore
def initialize(g)
@g = g
end
def gen_prologue(data)
g = @g
g.comment('interpreter check')
# Test for cell wrap != 256
g.emit '>[-]<[-]++++++++[>++++++++<-]>[<++++>-]<[>>'
# Print message 'Sorry this program needs an 8bit interpreter\n'
g.emit '>++++[<++++>-]<+[>++++++>+++++++>++>+++>+++++<<<<<-]>>>>>--.<<<-'
g.emit '-------.+++..+++++++.>--.<-----.<++.+.>-.>.<---.++.---.<--.>+++.'
g.emit '<------.>-----.>.<+.<++++..-.>+++++.>.<<---.>-----.>.>+++++.<<<+'
g.emit '.>-----.+++++++++++.>.<<+++++++.+++++.>.<---------.>--.--.++.<.>'
g.emit '++.<.>--.<<++++++++++.'
# endif
g.emit '<<[-]]'
# Test for cell wrap != 256 and flip condition
g.emit '>[-]<[-]++++++++[>++++++++<-]>[<++++>-]+<[>-<[-]]>[-<'
g.comment('init data')
data.each do |d, i|
raise if i > 65535
hi = i / 256
lo = i % 256
ptr = MEM + MEM_BLK_LEN * hi + MEM_CTL_LEN + lo * (BITS / 8)
g.add_word(ptr, d)
end
g.comment('prologue')
g.add(RUNNING, 1)
g.emit '['
#g.emit '@'
end
def gen_core
g = @g
gen_mem_load
gen_mem_store
g.move_word(NPC, PC)
end
def gen_mem_load
g = @g
g.comment 'memory (load)'
g.decloop(LOAD_REQ) {
g.move_ptr(MEM)
g.set_ptr(0)
if $bfs24
g.decloop(MEM_A-1) {
g.move_word(MEM_A, MEM_A + MEM_BLK_LEN*256)
g.move_ptr(MEM_A + MEM_BLK_LEN*256)
g.set_ptr(MEM_A)
g.add(MEM_USE+1, 1)
}
end
g.decloop(MEM_A) {
g.move_word(MEM_A, MEM_A + MEM_BLK_LEN)
g.move_ptr(MEM_A + MEM_BLK_LEN)
g.set_ptr(MEM_A)
g.add(MEM_USE, 1)
}
256.times{|al|
g.move_ptr(MEM_A + 1)
g.ifzero(1) do
g.copy_word(MEM_CTL_LEN + al * (BITS / 8), MEM_V, MEM_WRK + 2)
end
g.add(MEM_A + 1, -1)
}
g.clear(MEM_A + 1)
g.decloop(MEM_USE) {
g.move_word(MEM_V, MEM_V - MEM_BLK_LEN)
g.move_ptr(MEM_V - MEM_BLK_LEN)
g.set_ptr(MEM_V)
}
if $bfs24
g.decloop(MEM_USE+1) {
g.move_word(MEM_V, MEM_V - MEM_BLK_LEN*256)
g.move_ptr(MEM_V - MEM_BLK_LEN*256)
g.set_ptr(MEM_V)
}
end
g.move_ptr(0)
g.set_ptr(MEM)
g.clear_word(A)
g.move_word(MEM + MEM_V, A)
}
end
def gen_mem_store
g = @g
g.comment 'memory (store)'
g.decloop(STORE_REQ) {
g.move_ptr(MEM)
g.set_ptr(0)
if $bfs24
g.decloop(MEM_A-1) {
g.move_word(MEM_V, MEM_V + MEM_BLK_LEN*256)
g.move_word(MEM_A, MEM_A + MEM_BLK_LEN*256)
g.move_ptr(MEM_A + MEM_BLK_LEN*256)
g.set_ptr(MEM_A)
g.add(MEM_USE+1, 1)
}
end
g.decloop(MEM_A) {
g.move_word(MEM_V, MEM_V + MEM_BLK_LEN)
g.move_word(MEM_A, MEM_A + MEM_BLK_LEN)
g.move_ptr(MEM_A + MEM_BLK_LEN)
g.set_ptr(MEM_A)
g.add(MEM_USE, 1)
}
# VH VL 0 AL 1
256.times{|al|
g.move_ptr(MEM_A + 1)
g.ifzero(1) do
g.clear_word(MEM_CTL_LEN + al * (BITS / 8))
g.move_word(MEM_V, MEM_CTL_LEN + al * (BITS / 8))
end
g.add(MEM_A + 1, -1)
}
g.clear(MEM_A + 1)
g.move_ptr(MEM_USE)
g.emit '[-' + '<' * MEM_BLK_LEN + ']'
if $bfs24
g.move_ptr(MEM_USE+1)
g.emit '[-' + '<' * (MEM_BLK_LEN*256) + ']'
end
g.move_ptr(0)
g.set_ptr(MEM)
}
end
def gen_epilogue
g = @g
g.comment('epilogue')
g.move_ptr(RUNNING)
g.emit ']'
# endif for cell size check. (should be [-]] but we already have a zero)
g.emit ']'
# EOL at EOF
g.comment('[...THE END...]')
end
end
if __FILE__ == $0
require './bfasm'
require './bfgen'
bfa = BFAsm.new
code, data = bfa.parse(File.read(ARGV[0]))
g = BFGen.new
bfc = BFCore.new(g)
bfc.gen_prologue(data)
bfa.emit(g)
bfc.gen_core
bfc.gen_epilogue
end