-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmk2asm.lua
More file actions
425 lines (356 loc) · 11.7 KB
/
mk2asm.lua
File metadata and controls
425 lines (356 loc) · 11.7 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
-- important global constants
WORD_SIZE = 3 -- word size is default to 3 bytes
-- utilities
local function clone(tbl)
local out = {}
for i = 1, #tbl do out[i] = tbl[i] end
return out
end
local function parse_hex(tok)
if not tok then return nil end
if tok:sub(1, 2) == "0x" or tok:sub(1, 2) == "0X" then
return tonumber(tok:sub(3), 16)
end
-- allow plain decimal
return tonumber(tok)
end
-- error handling stuff
local function perror_at(token, msg)
io.stderr:write(string.format("Error at '%s': %s\n", token, msg))
os.exit(1)
end
local function perror(msg)
io.stderr:write(string.format("Error: %s\n", msg))
os.exit(1)
end
-- tokenization stuff
local function read_source(path)
local f = io.open(path, "r")
if not f then perror(string.format("Could not open file %s", path)) end
local text = f:read("*a")
f:close()
return text
end
local function tokenize(source)
local tokens = {}
local in_comment = false
for tok in source:gmatch("%S+") do
if tok == "(" then in_comment = true
elseif tok == ")" then in_comment = false
elseif not in_comment then table.insert(tokens, tok)
end
end
return tokens
end
-- expanding macros
local function expand_macros(tokens, macros)
macros = macros or {}
local expanded = {} -- final output
local macro_expansion_counter = 0 -- for label mangling
local assembler_directives = {
pad = true,
offset = true,
padto = true,
}
local i = 1
while i <= #tokens do
local tok = tokens[i]
-- if we get a %def, save the token and continue
if tok == "%def" then
local name = tokens[i + 1]
if not name then
perror_at(tok, "macro defined without a name")
end
local body = {}
i = i + 2 -- skip %def and name
while i <= #tokens and tokens[i] ~= "%end" do
table.insert(body, tokens[i])
i = i + 1
end
if tokens[i] ~= "%end" then
perror("macro definition is not closed")
end
macros[name] = body
i = i + 1 -- skip %end
elseif tok:sub(1, 1) == "%" then -- we get a macro call, expand it
local name = tok:sub(2)
-- check if its an assembler directive- %pad, %padto, %offset
if assembler_directives[name] then
table.insert(expanded, tok)
-- also insert its arguments to be used later
if tokens[i + 1] then
i = i + 1
table.insert(expanded, tokens[i])
end
i = i + 1
else
-- otherwise just expand the macro
local body = macros[name]
if not body then
perror_at(tok, "macro definition not found")
end
-- for mangling
macro_expansion_counter = macro_expansion_counter + 1
local expansion_id = name .. "$" .. macro_expansion_counter
-- clone the macro body so we can safely modify it
local cloned_body = clone(body)
local local_labels = {} -- storing label declarations
for j = 1, #cloned_body do
local t = cloned_body[j]
if t:sub(1, 1) == "." then
local_labels[t:sub(2)] = true
cloned_body[j] = t .. "$" .. expansion_id
end
end
for j = 1, #cloned_body do -- manging label references when needed
local t = cloned_body[j]
if t:sub(1, 1) == "@" then -- label reference
local has_skip = t:sub(-1) == "?"
local label_name = has_skip and t:sub(2, -2) or t:sub(2)
if local_labels[label_name] then
-- mangle only local labels
cloned_body[j] = "@" .. label_name .. "$" .. expansion_id
if has_skip then
cloned_body[j] = cloned_body[j] .. "?"
end
end
-- otherwise, don't touch labels declared outside the macro
end
end
-- append macro body into expanded list
local reexpanded = expand_macros(cloned_body, macros)
for _, t in ipairs(reexpanded) do
table.insert(expanded, t)
end
i = i + 1 -- move past macro call
end
else
-- normal token
table.insert(expanded, tok)
i = i + 1
end
end
return expanded
end
local function handle_labels(tokens)
local labels = {}
local pc = 0
local i = 1
while i <= #tokens do -- first pass, store all label declarations with pc value
local tok = tokens[i]
-- %pad n macro inserts n NOP bytes to pad the ROM
if tok == "%pad" then
local n = parse_hex(tokens[i + 1])
if not n or n < 0 then perror_at(tok, "expected non-negative number after %pad") end
pc = pc + n
i = i + 2
goto continue
-- %padto addr inserts NOP bytes until the PC reaches a specific address
elseif tok == "%padto" then
local addr = parse_hex(tokens[i + 1])
if not addr then perror_at(tok, "expected address after %padto") end
if addr < pc then
perror_at(tok, string.format("cannot pad to 0x%X, pc already at 0x%X", addr, pc))
end
pc = addr
i = i + 2
goto continue
-- %offset addr sets the pc to that address
elseif tok == "%offset" then
local addr = parse_hex(tokens[i + 1])
if not addr then perror_at(tok, "expected address in after %offset") end
pc = addr
i = i + 2
goto continue
elseif tok:sub(1, 1) == "." then -- label declaration
local label_name = tok:sub(2)
labels[label_name] = pc
i = i + 1
elseif tok:sub(1, 1) == "@" then -- increment pc by size of word
pc = pc + 1 + WORD_SIZE -- plus extra 1 for a "LIT" opcode
i = i + 1
elseif tok:sub(1, 1) == "#" then -- hex literal
local has_skip = tok:sub(-1) == "?"
local digits = has_skip and tok:sub(2, -2) or tok:sub(2)
if (#digits ~= 2) and (#digits ~= 4) and (#digits ~= 6) and (#digits ~= 8) then
perror_at(tok, "invalid number of hex digits in literal (must be 2, 4, 6, or 8)")
end
-- 1 byte for the LIT opcode, plus n more for immediate value
pc = pc + 1 + (#digits // 2)
i = i + 1
else
pc = pc + 1 -- normal opcode
i = i + 1
end
::continue::
end
-- second pass, convert all labels into lit instructions
-- keep everything else (%offset, %padto, %pad included- handled later)
local expanded = {}
local j = 1
while j <= #tokens do
local tok = tokens[j]
if tok:sub(1, 1) == "@" then
local has_skip = tok:sub(-1) == "?"
local name = has_skip and tok:sub(2, -2) or tok:sub(2)
local pc_value = labels[name]
if not pc_value then
perror_at(tok, "label declaration not found")
end
local pc_hex = string.format("%0" .. (WORD_SIZE * 2) .. "x", pc_value)
local literal = "#" .. pc_hex .. (has_skip and "?" or "")
table.insert(expanded, literal)
elseif tok:sub(1, 1) ~= "." then -- don't insert label declarations!
table.insert(expanded, tok)
end
j = j + 1
end
return expanded
end
local function assemble(opcodes)
local binary = {}
local pc = 0 -- tracking for %pad, %padto, and %offset
local opmap = {
ADD = 0x00, AND = 0x01, XOR = 0x02, SHF = 0x03,
SWP = 0x04, CMP = 0x05, STR = 0x06, LOD = 0x07,
DUP = 0x08, DRP = 0x09, PSH = 0x0a, POP = 0x0b,
JMP = 0x0c, LIT = 0x0d, FET = 0x0e, NOP = 0x0f,
}
local function emit_op(op, a, skip)
local instr = 0x80
if skip then instr = instr | 0x40 end
instr = instr | (a & 0x03 << 4)
instr = instr | (op & 0x0f)
table.insert(binary, instr)
pc = pc + 1
end
local i = 1
while i <= #opcodes do
local tok = opcodes[i]
-- %pad n directive
if tok == "%pad" then
local n = parse_hex(opcodes[i + 1])
if not n or n < 0 then
perror_at(tok, "expected non-negative number after %pad")
end
-- emit n NOPs (1 byte each)
for _ = 1, n do
-- NOP with default a and skip=false
emit_op(opmap.NOP, (WORD_SIZE - 1) & 0x03, false)
end
i = i + 2
goto continue
-- %padto addr directive
elseif tok == "%padto" then
local addr = parse_hex(opcodes[i + 1])
if not addr then perror_at(tok, "expected address after %padto") end
if addr < pc then
perror_at(tok, string.format("cannot pad to 0x%X, pc already at 0x%X", addr, pc))
end
while pc < addr do
emit_op(opmap.NOP, (WORD_SIZE - 1) & 0x03, false)
end
i = i + 2
goto continue
-- %offset addr directive
elseif tok == "%offset" then
local addr = parse_hex(opcodes[i + 1])
if not addr then perror_at(tok, "expected address after %offset") end
pc = addr -- move pc without emitting bytes
i = i + 2
goto continue
end
if tok == "HLT" then
table.insert(binary, 15) -- (0b00001111) this way it doesn't get confused for an EOF char
pc = pc + 1
elseif tok:sub(1, 1) == "#" then -- LIT instruction
local has_skip = tok:sub(-1) == "?"
local digits = has_skip and tok:sub(2, -2) or tok:sub(2)
-- we shouldn't need to check this again, but i will just in case
if (#digits ~= 2) and (#digits ~= 4) and (#digits ~= 6) and (#digits ~= 8) then
perror_at(tok, "invalid number of hex digits in literal (must be 2, 4, 6, or 8)")
end
local a = (#digits // 2) - 1
local instruction = 0x8d -- emitting LIT opcode
if has_skip then instruction = instruction | 0x40 end
instruction = instruction | (a << 4)
table.insert(binary, instruction)
pc = pc + 1
-- insert every 2 hex digits as a byte, big-endian order
for j = 1, #digits, 2 do
local byte_value = tonumber(digits:sub(j, j + 1), 16)
if not byte_value then
perror_at(tok, "invalid hex literal digits.")
end
table.insert(binary, byte_value)
pc = pc + 1
end
else -- standard opcode
-- pattern matching can grab a lot of this stuff
local op, num_suffix, skip = tok:match("^([A-Z][A-Z][A-Z])([1-4]?)(%??)$")
if not op then
perror_at(tok, "Invalid opcode")
end
local opcode = opmap[op]
if not opcode then
perror_at(tok, "Invalid opcode")
end
local has_skip = skip == "?" -- skip flag
local a = WORD_SIZE - 1 -- default 'a' value
if num_suffix ~= "" then
local num = tonumber(num_suffix)
a = num - 1
end
emit_op(opcode, a, has_skip)
end
i = i + 1
::continue::
end
return binary
end
-- writing bytes to a file
local function write_binary(filename, bytes)
local f, err = io.open(filename, "wb")
if not f then error("Failed to open " .. filename) end
local data = {}
for i = 1, #bytes do
data[i] = string.char(bytes[i] & 0xff) -- masking just in case
end
f:write(table.concat(data))
f:close()
end
-- "main", the assembler loop
local input = arg[1]
local output = arg[2] or "out.okb"
local function print_usage()
io.stdout:write("Usage: lua mk2asm.lua input.mk2 [output.okb]\n")
end
if not input then
print_usage()
os.exit(1)
end
local source = read_source(input)
local tokens = tokenize(source)
local expanded_macros = expand_macros(tokens)
-- for testing macro expansions
-- local i = 1
-- while i <= #expanded_macros do
-- print("EXPANDED: " .. expanded_macros[i])
-- i = i + 1
-- end
local handled_labels = handle_labels(expanded_macros)
-- for testing label handling
-- local i = 1
-- while i <= #handled_labels do
-- print("LBL HANDLING: " .. handled_labels[i])
-- i = i + 1
-- end
local binary = assemble(handled_labels)
-- for testing the binary output
-- local i = 1
-- while i <= #binary do
-- print("BINARY: " .. string.format("%2x", binary[i]))
-- i = i + 1
-- end
write_binary(output, binary)
-- TODO more testing