-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmuri.lua
237 lines (195 loc) · 4.68 KB
/
muri.lua
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
local class = require('middleclass')
local ops = require('ops')
local utils = require('utils')
local OP2CODE = {}
-- full op name
for op, code in pairs(ops.op2code) do
OP2CODE[op] = code
end
for shortname, op in pairs(ops.shortname2op) do
OP2CODE[shortname] = ops.op2code[op]
end
local Muri = class('Muri')
function Muri:initialize(arg)
local conf = {}
conf.image_size = assert(arg.image_size)
conf.addr_start = assert(arg.addr_start)
conf.op2code = arg.op2code or OP2CODE
self.conf = conf
local init_target = arg.init_target
if init_target == nil then
init_target = true
end
self.state = {}
self:reset(init_target)
end
function Muri:init_target()
local start = self.conf.addr_start
local end_ = self.conf.image_size + start - 1
for i=start, end_ do
self.state.target[i] = 0
end
end
function Muri:reset(init_target)
if init_target == nil then
init_target = true
end
local state = self.state
state.target = {}
state.labels = {}
state.here = self.conf.addr_start
self.state = state
if init_target then
self:init_target()
end
end
function Muri:fatal_error(msg)
error("Fatal error: " .. msg)
end
function Muri:add_label(name, slice)
if self.state.labels[name] then
self:fatal_error(name .. " already defined")
end
self.state.labels[name] = slice
end
function Muri:lookup_label(name)
return self.state.labels[name]
end
function Muri:code_for(op)
return self.conf.op2code[op] or 0
end
function Muri:write_cell(cell)
local state = self.state
state.target[state.here] = cell
state.here = state.here + 1
end
-- load literate muri file
function Muri:load_file(path)
local f = io.open(path)
local in_block = false
local src_t = {}
for line in f:lines() do
if line == '~~~' then
if in_block then
in_block = false
else
in_block = true
end
else
if in_block and line ~= '' then
table.insert(src_t, line)
end
end
end
f:close()
return src_t
end
-- parse retro's muri source to ast. src is a table of source lines
function Muri:parse_source(src)
local translation_table = {
i = 'instruction',
d = 'number',
r = 'ref',
s = 'string',
[':'] = 'label',
}
local ast = {}
for _, line in ipairs(src) do
local entry = utils.ssplit(line)
local directive = translation_table[entry[1]]
if not directive then
self:fatal_error("illegal directive " .. directive)
end
entry[1] = directive
table.insert(ast, entry)
end
return ast
end
function Muri:_pass(ast, directives)
self.state.here = self.conf.addr_start
-- ast in {directive data...}
for _, entry in ipairs(ast) do
local directive = entry[1]
local processor = directives[directive]
if not processor then
self:fatal_error("illegal directive " .. directive)
end
processor(self, entry)
end
end
function Muri:pass1(ast)
local ds = self.class.directives.pass1
self:_pass(ast, ds)
end
function Muri:pass2(ast)
local ds = self.class.directives.pass2
self:_pass(ast, ds)
end
Muri.static.directives = {}
local _pass1 = {}
Muri.static.directives.pass1 = _pass1
function _pass1.instruction(self, entry)
local ops = entry[2]
local opcodes = {}
local pack_num = 4
for i=1, pack_num do
local op = ops:sub(2 * i - 1, 2 * i)
opcodes[i] = self:code_for(op)
end
local packed_opcode = utils.pack_opcodes(opcodes)
self:write_cell(packed_opcode)
end
function _pass1.instruction_alt(self, entry)
local opcodes = {}
local pack_num = 4
for i=1, pack_num do
local op = entry[i + 1] or 'nop'
opcodes[i] = self:code_for(op)
end
local packed_opcode = utils.pack_opcodes(opcodes)
self:write_cell(packed_opcode)
end
function _pass1.number(self, entry)
local num = tonumber(entry[2])
self:write_cell(num)
end
function _pass1.string(self, entry)
local s = entry[2]
for i=1, #s do
local char = string.byte(s, i)
self:write_cell(char)
end
self:write_cell(0)
end
function _pass1.label(self, entry)
local label = entry[2]
self:add_label(label, self.state.here)
end
function _pass1.ref(self, entry)
self:write_cell(-1)
end
local _pass2 = {}
Muri.static.directives.pass2 = _pass2
local function _move_on(self, entry)
self.state.here = self.state.here + 1
end
_pass2.instruction = _move_on
_pass2.instruction_alt = _move_on
_pass2.number = _move_on
function _pass2.label(self, entry)
-- do nothing
return
end
function _pass2.string(self, entry)
local s = entry[2]
self.state.here = self.state.here + #s + 1
end
function _pass2.ref(self, entry)
local label = entry[2]
local addr = self:lookup_label(label)
if not addr then
self:fatal_error("label look up failed: " .. label)
end
self:write_cell(addr)
end
return Muri