2016-04-26 15:28:58 -07:00
|
|
|
#!/usr/bin/env luajit
|
2016-04-02 02:06:46 -07:00
|
|
|
package.path = package.path..";./?/init.lua"
|
|
|
|
|
2016-04-26 15:28:58 -07:00
|
|
|
--require "test.strict"
|
2016-04-02 02:06:46 -07:00
|
|
|
local assemble = require "lips"
|
2016-04-10 07:00:10 -07:00
|
|
|
local cereal = require "serialize"
|
|
|
|
local argparse = require "argparse"
|
2016-04-02 02:06:46 -07:00
|
|
|
|
2016-04-21 13:04:59 -07:00
|
|
|
local function lament(...)
|
|
|
|
io.stdout:write(...)
|
|
|
|
io.stdout:write('\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
local function parsenum(s)
|
2016-04-26 15:28:58 -07:00
|
|
|
if type(s) == 'number' then
|
|
|
|
return s
|
|
|
|
end
|
2016-04-21 13:04:59 -07:00
|
|
|
if s:sub(1, 2) == '0x' then
|
|
|
|
return tonumber(s, 16)
|
|
|
|
elseif s:sub(1, 1) == '0' then
|
|
|
|
return tonumber(s, 8)
|
|
|
|
else
|
|
|
|
return tonumber(s)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-26 15:28:58 -07:00
|
|
|
local function make_verbose_writer()
|
|
|
|
local buff = {}
|
|
|
|
local max = -1
|
|
|
|
return function(pos, b)
|
|
|
|
if pos then
|
|
|
|
buff[pos] = b
|
|
|
|
if pos > max then
|
|
|
|
max = pos
|
|
|
|
end
|
|
|
|
elseif max >= 0 then
|
|
|
|
for i=0, max, 4 do
|
|
|
|
local a = buff[i+0] or nil
|
|
|
|
local b = buff[i+1] or nil
|
|
|
|
local c = buff[i+2] or nil
|
|
|
|
local d = buff[i+3] or nil
|
|
|
|
if a or b or c or d then
|
|
|
|
a = a and ("%02X"):format(a) or '--'
|
|
|
|
b = b and ("%02X"):format(b) or '--'
|
|
|
|
c = c and ("%02X"):format(c) or '--'
|
|
|
|
d = d and ("%02X"):format(d) or '--'
|
|
|
|
print(('%08X %s'):format(i, a..b..c..d))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-10 07:00:10 -07:00
|
|
|
local function inject(args)
|
2016-04-21 13:04:59 -07:00
|
|
|
local offset = args.offset and parsenum(args.offset) or 0
|
|
|
|
local origin = args.origin and parsenum(args.origin) or 0
|
|
|
|
local base = args.base and parsenum(args.base) or 0x80000000
|
2016-04-02 05:58:46 -07:00
|
|
|
|
2016-04-26 15:28:58 -07:00
|
|
|
local f
|
|
|
|
if args.output then
|
|
|
|
f = io.open(args.output, 'r+b')
|
|
|
|
if not f then
|
|
|
|
lament("file not found:", args.output)
|
|
|
|
return
|
|
|
|
end
|
2016-04-09 04:18:00 -07:00
|
|
|
end
|
2016-04-02 02:06:46 -07:00
|
|
|
|
2016-04-10 07:00:10 -07:00
|
|
|
local state = {}
|
|
|
|
for _, import in ipairs(args.import) do
|
|
|
|
local new_state = cereal.deserialize(import)
|
|
|
|
for k, v in pairs(new_state) do
|
|
|
|
state[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-10 08:56:42 -07:00
|
|
|
local function write(pos, b)
|
2016-04-26 15:28:58 -07:00
|
|
|
--if args.extra_rom and args.extra_ram and pos >= args.extra_ram then
|
|
|
|
-- pos = pos - args.extra_ram + args.extra_rom
|
|
|
|
--elseif pos >= offset then
|
|
|
|
if pos >= offset then
|
2016-04-21 13:04:59 -07:00
|
|
|
pos = pos - offset
|
2016-04-02 05:58:46 -07:00
|
|
|
end
|
|
|
|
if pos >= 1024*1024*1024 then
|
2016-04-26 15:28:58 -07:00
|
|
|
lament(("oops: %08X %02X"):format(pos, b))
|
2016-04-02 05:58:46 -07:00
|
|
|
return
|
|
|
|
end
|
2016-04-09 04:18:00 -07:00
|
|
|
|
2016-04-26 15:28:58 -07:00
|
|
|
if f then
|
|
|
|
f:seek('set', pos)
|
|
|
|
f:write(string.char(b))
|
|
|
|
else
|
|
|
|
print(("%08X %02X"):format(pos, b))
|
|
|
|
end
|
2016-04-02 02:06:46 -07:00
|
|
|
end
|
|
|
|
|
2016-04-21 13:04:59 -07:00
|
|
|
local options = {
|
|
|
|
unsafe = true,
|
|
|
|
labels = state,
|
|
|
|
debug_token = args.dump_token,
|
|
|
|
debug_pre = args.dump_pre,
|
2016-04-26 15:28:58 -07:00
|
|
|
debug_post = args.dump_post,
|
|
|
|
debug_asm = args.dump_asm,
|
2016-04-21 13:04:59 -07:00
|
|
|
}
|
|
|
|
if args.offset then
|
|
|
|
if args.origin or args.base then
|
|
|
|
error('--offset is mutually exclusive from --origin, --base')
|
|
|
|
end
|
|
|
|
options.offset = offset
|
|
|
|
else
|
|
|
|
options.origin = origin
|
2016-04-26 15:28:58 -07:00
|
|
|
options.base = base
|
2016-04-21 13:04:59 -07:00
|
|
|
end
|
|
|
|
|
2016-04-26 15:28:58 -07:00
|
|
|
if f then
|
|
|
|
assemble(args.input, write, options)
|
|
|
|
else
|
|
|
|
local vb = make_verbose_writer()
|
|
|
|
assemble(args.input, vb, options)
|
|
|
|
vb()
|
|
|
|
end
|
2016-04-10 07:00:10 -07:00
|
|
|
|
|
|
|
if args.export then
|
|
|
|
cereal.serialize(args.export, state)
|
|
|
|
end
|
2016-04-02 02:06:46 -07:00
|
|
|
|
2016-04-26 15:28:58 -07:00
|
|
|
if f then
|
|
|
|
f:close()
|
|
|
|
end
|
2016-04-02 02:06:46 -07:00
|
|
|
end
|
|
|
|
|
2016-04-10 07:00:10 -07:00
|
|
|
local ap = argparse("patch", "patch a binary file with assembly")
|
|
|
|
|
2016-04-15 11:43:38 -07:00
|
|
|
-- TODO: option to dump hex or gs codes when no output is given
|
2016-04-21 13:04:59 -07:00
|
|
|
ap:argument("input", "input assembly file")
|
2016-04-26 15:28:58 -07:00
|
|
|
ap:argument("output", "output binary file"):args('?')
|
2016-04-21 13:04:59 -07:00
|
|
|
ap:option("-o --offset", "(deprecated) offset to pass to lips", nil)
|
2016-04-26 15:28:58 -07:00
|
|
|
ap:option("-O --origin", "origin to pass to lips", nil)
|
|
|
|
ap:option("-b --base", "base to pass to lips", nil)
|
2016-04-21 13:04:59 -07:00
|
|
|
ap:option("-i --import", "import state file(s) containing labels"):count("*")
|
|
|
|
ap:option("-e --export", "export state file containing labels")
|
|
|
|
ap:flag("--dump-token", "(debug) dump statements to stdout after lexing")
|
|
|
|
ap:flag("--dump-pre", "(debug) dump statements to stdout after preprocessing")
|
2016-04-26 15:28:58 -07:00
|
|
|
ap:flag("--dump-post", "(debug) dump statements to stdout after expanding")
|
2016-04-21 13:04:59 -07:00
|
|
|
ap:flag("--dump-asm", "(debug) dump statements to stdout after assembling")
|
2016-04-10 07:00:10 -07:00
|
|
|
--ap:option("-s --state", "--import and --export to this file")
|
2016-04-26 15:28:58 -07:00
|
|
|
-- use -D defines instead
|
|
|
|
--ap:option("--extra-rom", "dumb stuff"):convert(parsenum)
|
|
|
|
--ap:option("--extra-ram", "dumb stuff"):convert(parsenum)
|
2016-04-10 07:00:10 -07:00
|
|
|
|
|
|
|
local inject_args = ap:parse()
|
|
|
|
|
|
|
|
inject(inject_args)
|