Use l7801 extension for µPD7801 compiler.

This commit is contained in:
mooz 2018-11-21 21:21:07 +01:00
parent f2ab52c0ae
commit 52a5777949
9 changed files with 95 additions and 94 deletions

View File

@ -9,6 +9,7 @@ script:
- cmake . -DCMAKE_BUILD_TYPE=Release
- make
- cd samples; for f in *.l65; do echo $f; ../l65 $f || break; done; cd ..
- cd samples; for f in *.l7801; do echo $f; ../l7801 $f || break; done; cd ..
- cp l65 l65-$TRAVIS_TAG-$TRAVIS_OS_NAME
deploy:
provider: releases

View File

@ -127,7 +127,7 @@ set(L7801_HEADERS
${L65_HEADERS}
)
set(L7801_FILES ${L65_SOURCE_DIR}/scv.l65)
set(L7801_FILES ${L65_SOURCE_DIR}/scv.l7801)
set(L7801_SCRIPTS
${L65_SOURCE_DIR}/asm.lua

View File

@ -140,12 +140,12 @@ static int luaopen_l7801(lua_State *L)
}
#define SRC_LUA(name) { #name, 0, script_ ## name ## _lua, sizeof(script_ ## name ## _lua) }
#define SRC_L65(name) { #name, 1, script_ ## name ## _l65, sizeof(script_ ## name ## _l65) }
#define SRC_L7801(name) { #name, 1, script_ ## name ## _l7801, sizeof(script_ ## name ## _l7801) }
static struct script { const char *name; int t; const char *data; size_t sz; } embedded[] = {
SRC_LUA(dkjson),
SRC_LUA(l65cfg),
SRC_LUA(re),
SRC_L65(scv),
SRC_L7801(scv),
};
#undef SRC_LUA
#undef SRC_L7801

114
l7801.lua
View File

@ -2131,7 +2131,7 @@ local function ParseLua(src, src_name)
end
local function Format65(ast)
local function Format7801(ast)
local function splitLines(str)
if str:match("\n") then
local lines = {}
@ -2538,34 +2538,34 @@ local function Format65(ast)
end
local dirsep = package.config:sub(1,1)
local dirl65 = (string.match(arg[0], "(.*[\\/]).*") or ''):gsub('/',dirsep)
local searchl65 = ''
if #dirl65 > 0 then
searchl65 = string.format(";%s?;%s?.l65", dirl65, dirl65)
package.path = package.path .. string.format(";%s?.lua", dirl65)
local dirl7801 = (string.match(arg[0], "(.*[\\/]).*") or ''):gsub('/',dirsep)
local searchl7801 = ''
if #dirl7801 > 0 then
searchl7801 = string.format(";%s?;%s?.l7801", dirl7801, dirl7801)
package.path = package.path .. string.format(";%s?.lua", dirl7801)
end
l65_def = {
l7801_def = {
parse = ParseLua,
format = Format65,
format = Format7801,
searcher_index = 2,
search_path = string.format(".%s?;.%s?.l65%s", dirsep, dirsep, searchl65),
search_path = string.format(".%s?;.%s?.l7801%s", dirsep, dirsep, searchl7801),
load_org = load,
loadfile_org = loadfile,
dofile_org = dofile,
}
if not l65 then l65 = l65_def else for k,v in pairs(l65_def) do l65[k]=v end end
l65.report = function(success, ...)
if not l7801 then l7801 = l7801_def else for k,v in pairs(l7801_def) do l7801[k]=v end end
l7801.report = function(success, ...)
if success then return success,... end
local message=... io.stderr:write(tostring(message)..'\n')
os.exit(-1)
end
l65.msghandler = function(msg)
l7801.msghandler = function(msg)
local o = function(s) io.stderr:write(s) end
msg = tostring(msg)
msg = msg:gsub('%[string "(.-%.l65)"%]', '%1') -- [string "xxx.l65"] -> xxx.l65
msg = msg:gsub('%[string "(.-%.l7801)"%]', '%1') -- [string "xxx.l7801"] -> xxx.l7801
local trace_cur = debug.traceback(nil, 2)
trace_cur = trace_cur:gsub('%[string "(.-%.l65)"%]', '%1') -- [string "xxx.l65"] -> xxx.l65
trace_cur = trace_cur:gsub('%[string "(.-%.l7801)"%]', '%1') -- [string "xxx.l7801"] -> xxx.l7801
trace_cur = trace_cur:gsub('stack traceback:', '')
local i=2
@ -2574,7 +2574,7 @@ l65.msghandler = function(msg)
while true do
local n,v = debug.getlocal(i, j)
if not n then break end
if n == 'l65dbg' then
if n == 'l7801dbg' then
o(string.format("%s\n", msg))
if trace_cur:find("in local 'late'") then
local lines = {}
@ -2585,7 +2585,7 @@ l65.msghandler = function(msg)
o(table.concat(lines,'\n'))
end
local trace = v.trace:match(".-\n(.*)\n.-'xpcall'")
trace = trace:gsub('%[string "(.-%.l65)"%]', '%1')
trace = trace:gsub('%[string "(.-%.l7801)"%]', '%1')
trace = trace:gsub('stack traceback:', '')
o(trace .. '\n')
os.exit(-2)
@ -2601,32 +2601,32 @@ l65.msghandler = function(msg)
end
do
local getembedded = type(arg[-1]) == 'function' and arg[-1]
l65.load_embedded = function(name)
l7801.load_embedded = function(name)
if not getembedded then return end
local src,isl65 = getembedded(name)
local src,isl7801 = getembedded(name)
if not src then return end
if isl65 then
name = name .. '.l65'
local st, ast = l65.report(l65.parse(src, name))
src = l65.format(ast)
if isl7801 then
name = name .. '.l7801'
local st, ast = l7801.report(l7801.parse(src, name))
src = l7801.format(ast)
else
name = name .. '.lua'
end
local bc = assert(l65.load_org(src, name))
local bc = assert(l7801.load_org(src, name))
return bc, name
end
end
l65.searcher = function(name)
local filename,err = package.searchpath(name, l65.search_path, '.', '.')
l7801.searcher = function(name)
local filename,err = package.searchpath(name, l7801.search_path, '.', '.')
if not filename then return err end
local file = assert(io.open(filename, 'rb'))
local src = file:read('*a')
file:close()
local st, ast = l65.report(l65.parse(src, filename))
local bc = assert(l65.load_org(l65.format(ast), filename))
local st, ast = l7801.report(l7801.parse(src, filename))
local bc = assert(l7801.load_org(l7801.format(ast), filename))
return bc, filename
end
l65.load = function(chunk, chunkname, mode, ...)
l7801.load = function(chunk, chunkname, mode, ...)
local chunk_t,s = type(chunk)
if chunk_t == 'string' then s = chunk
elseif chunk_t == 'function' then
@ -2635,12 +2635,12 @@ l65.load = function(chunk, chunkname, mode, ...)
else return nil, string.format("invalid type for chunk %s: %s", chunkname or "=(load)", chunk_t)
end
if s:sub(1,4) == "\x1bLua" then -- a binary file
return l65.load_org(s, chunkname, mode, ...)
return l7801.load_org(s, chunkname, mode, ...)
end
local st, ast = l65.report(l65.parse(s, chunkname or "=(load)"))
return l65.load_org(l65.format(ast), chunkname, 't', ...)
local st, ast = l7801.report(l7801.parse(s, chunkname or "=(load)"))
return l7801.load_org(l7801.format(ast), chunkname, 't', ...)
end
l65.loadfile = function(filename, mode, ...)
l7801.loadfile = function(filename, mode, ...)
local s
if not filename then s = io.read('*a')
else
@ -2649,34 +2649,34 @@ l65.loadfile = function(filename, mode, ...)
s = file:read('*a')
file:close()
end
return l65.load(s, filename, mode, ...)
return l7801.load(s, filename, mode, ...)
end
l65.dofile = function(filename)
local f = l65.report(l65.loadfile(filename))
l7801.dofile = function(filename)
local f = l7801.report(l7801.loadfile(filename))
return f()
end
l65.installhooks = function()
if package.searchers[l65.searcher_index] ~= l65.searcher then
table.insert(package.searchers, l65.searcher_index, l65.searcher)
l7801.installhooks = function()
if package.searchers[l7801.searcher_index] ~= l7801.searcher then
table.insert(package.searchers, l7801.searcher_index, l7801.searcher)
end
if not l65.hooks_installed then l65.hooks_installed = true
load = l65.load
loadfile = l65.loadfile
dofile = l65.dofile
if not l7801.hooks_installed then l7801.hooks_installed = true
load = l7801.load
loadfile = l7801.loadfile
dofile = l7801.dofile
end
end
l65.uninstallhooks = function()
l7801.uninstallhooks = function()
for k,v in ipairs(package.searchers) do
if v == l65.searcher then table.remove(package.searchers, k) break end
if v == l7801.searcher then table.remove(package.searchers, k) break end
end
if l65.hooks_installed then l65.hooks_installed = nil
load = l65.load_org
loadfile = l65.loadfile_org
dofile = l65.dofile_org
if l7801.hooks_installed then l7801.hooks_installed = nil
load = l7801.load_org
loadfile = l7801.loadfile_org
dofile = l7801.dofile_org
end
end
table.insert(package.searchers, l65.searcher_index, l65.load_embedded)
l65.installhooks()
table.insert(package.searchers, l7801.searcher_index, l7801.load_embedded)
l7801.installhooks()
function getopt(optstring, ...)
local opts = { }
@ -2712,16 +2712,16 @@ function getopt(optstring, ...)
end)
end
local cfg=require"l65cfg" l65.cfg=cfg
local cfg=require"l65cfg" l7801.cfg=cfg
local version = function()
print(string.format("l65 %s", cfg.version))
print(string.format("l7801 %s", cfg.version))
end
local usage = function(f)
if not f then f = io.stdout end
f:write(string.format([[
Usage: %s [options] file [args]
Options:
-d <file> Dump the Lua code after l65 parsing into file
-d <file> Dump the Lua code after l7801 parsing into file
-h Display this information
-v Display the release version
]], arg[0]))
@ -2740,12 +2740,12 @@ for opt,arg,i in getopt("d:hv", ...) do
if opt == false then inf=arg optix=i+1 break end
end
if not inf then return invalid_usage() end
if dump then l65.format = function(ast)
local s=Format65(ast) l65.format = Format65
if dump then l7801.format = function(ast)
local s=Format7801(ast) l7801.format = Format7801
local f = assert(io.open(dump, 'wb')) f:write(s) f:close()
return s
end end
local fn='' for i=#inf,1,-1 do local c=inf:sub(i,i) if c==dirsep or c=='/' then break end fn=c..fn if c=='.' then fn='' end end filename=fn
local f = l65.report(l65.loadfile(inf))
return xpcall(f, l65.msghandler, select(optix, ...))
local f = l7801.report(l7801.loadfile(inf))
return xpcall(f, l7801.msghandler, select(optix, ...))

View File

View File

@ -111,9 +111,9 @@ local opregxx ={
} M.opregxx = opregxx
for k,v in pairs(opregxx) do
M[k] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 2 end
local bin = function() local l65dbg=l65dbg return { v.opc, M.op_eval_byte(late,early) } end
local bin = function() local l7801dbg=l7801dbg return { v.opc, M.op_eval_byte(late,early) } end
table.insert(M.section_current.instructions, { size=size, cycles=v.cycles, bin=bin })
end
end
@ -138,9 +138,9 @@ local opaxx ={
} M.opaxx = opaxx
for k,v in pairs(opaxx) do
M[k .. 'a'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 2 end
local bin = function() local l65dbg=l65dbg return { v.opc, M.op_eval_byte(late,early) } end
local bin = function() local l7801dbg=l7801dbg return { v.opc, M.op_eval_byte(late,early) } end
table.insert(M.section_current.instructions, { size=size, cycles=v.cycles, bin=bin })
end
end
@ -165,9 +165,9 @@ local opw = {
} M.opw = opw
for k,v in pairs(opw) do
M[k .. 'imm'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 3 end
local bin = function() local l65dbg=l65dbg
local bin = function() local l7801dbg=l7801dbg
local x = M.op_eval_word(late,early)
return { v.opc, x&0xff, x>>8 }
end
@ -183,9 +183,9 @@ local opr16w = {
} M.opr16w = opr16w
for k,v in pairs(opr16w) do
M[k] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 3 end
local bin = function() local l65dbg=l65dbg
local bin = function() local l7801dbg=l7801dbg
local x = M.op_eval_word(late,early)
return { v.opc, x&0xff, x>>8 }
end
@ -195,11 +195,11 @@ end
M['calt' .. 'imm'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local op = { cycles=19 }
op.size = function() late,early = M.size_op(late,early) return 1 end
op.bin = function()
local l65dbg=l65dbg
local l7801dbg=l7801dbg
local x = M.op_eval_byte(late,early)
if (x%2 == 1) then error("offset should be even : " .. x) end
if x < 0x80 or x > 0xfe then error("offset out of range : " .. x) end
@ -210,10 +210,10 @@ M['calt' .. 'imm'] = function(late, early)
end
M['calf' .. 'imm'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local op = { cycles=16 }
op.size = function() late,early = M.size_op(late,early) return 2 end
op.bin = function() local l65dbg=l65dbg
op.bin = function() local l7801dbg=l7801dbg
local x = 0 + M.op_eval_word(late,early)
if x < 0x0800 or x > 0xffff then error("subroutine address out of range [0x0800-0xffff]: " .. x) end
x = x - 0x0800;
@ -223,7 +223,7 @@ M['calf' .. 'imm'] = function(late, early)
end
M.jr = function(label)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local parent,offset = M.label_current
local section,rorg = M.section_current,M.location_current.rorg
local op = { cycles=13 }
@ -233,7 +233,7 @@ M.jr = function(label)
return 1
end
op.bin = function()
local l65dbg=l65dbg
local l7801dbg=l7801dbg
local x,l = label,label
if type(x) == 'function' then x=x() end
if type(x) == 'string' then
@ -254,7 +254,7 @@ M.jr = function(label)
end
M.jre = function(label)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local parent,offset = M.label_current
local section,rorg = M.section_current,M.location_current.rorg
local op = { cycles=17 }
@ -264,7 +264,7 @@ M.jre = function(label)
return 2
end
op.bin = function()
local l65dbg=l65dbg
local l7801dbg=l7801dbg
local x,l = label,label
if type(x) == 'function' then x=x() end
if type(x) == 'string' then
@ -296,9 +296,9 @@ local opwa = {
} M.opwa = opwa
for k,v in pairs(opwa) do
M[k .. 'wa'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 2 end
local bin = function() local l65dbg=l65dbg return { v.opc, M.op_eval_byte(late,early) } end
local bin = function() local l7801dbg=l7801dbg return { v.opc, M.op_eval_byte(late,early) } end
table.insert(M.section_current.instructions, { size=size, cycles=v.cycles, bin=bin })
end
end
@ -309,14 +309,14 @@ local opwaxx = {
} M.opwaxx = opwaxx
for k,v in pairs(opwaxx) do
M[k .. 'waxx'] = function(late_offset, late_data, early_offset, early_data)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function()
late_offset,early_offset = M.size_op(late_offset,early_offset)
late_data,early_data = M.size_op(late_data,early_data)
return 3
end
local bin = function()
local l65dbg=l65dbg
local l7801dbg=l7801dbg
return { v.opc, M.op_eval_byte(late_offset,early_offset), M.op_eval_byte(late_data,early_data) }
end
table.insert(M.section_current.instructions, { size=size, cycles=v.cycles, bin=bin })
@ -401,10 +401,10 @@ local opinout={
} M.opinout = op4inout
for k,v in pairs(opinout) do
M[k .. 'imm'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local op = { cycles=v.cycles }
op.size = function() late,early = M.size_op(late,early) return 2 end
op.bin = function() local l65dbg=l65dbg
op.bin = function() local l7801dbg=l7801dbg
local x = 0x00 + M.op_eval_byte(late,early)
return { v.opc, 0x00, x }
end
@ -492,10 +492,10 @@ for i,o in ipairs(op64names) do
if not M[name] then
local l = k
M[name] = function(late,early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local op = { cycles=11 }
op.size = function() late,early = M.size_op(late,early) return 3 end
op.bin = function() local l65dbg=l65dbg
op.bin = function() local l7801dbg=l7801dbg
local x = 0x00 + l;
local y = 0x00 + M.op_eval_byte(late,early)
return { 0x64, x, y }
@ -515,10 +515,10 @@ for i,o in ipairs(op64names) do
if not M[name] then
local l = k
M[name] = function(late,early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local op = { cycles=11 }
op.size = function() late,early = M.size_op(late,early) return 3 end
op.bin = function() local l65dbg=l65dbg
op.bin = function() local l7801dbg=l7801dbg
local x = 0x00 + l;
local y = 0x00 + M.op_eval_byte(late,early)
return { 0x64, x, y }
@ -550,9 +550,9 @@ local op74wa = {
} M.op74wa = op74wa
for k,v in pairs(op74wa) do
M[k .. 'wa'] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 3 end
local bin = function() local l65dbg=l65dbg return { 0x74, v.opc, M.op_eval_byte(late,early) } end
local bin = function() local l7801dbg=l7801dbg return { 0x74, v.opc, M.op_eval_byte(late,early) } end
table.insert(M.section_current.instructions, { size=size, cycles=v.cycles, bin=bin })
end
end
@ -569,9 +569,9 @@ local op70ind = {
} M.op70ind = op70ind
for k,v in pairs(op70ind) do
M[k] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 4 end
local bin = function() local l65dbg=l65dbg
local bin = function() local l7801dbg=l7801dbg
local x = M.op_eval_word(late,early)
return { 0x70, v.opc, x&0xff, x>>8 }
end
@ -591,9 +591,9 @@ local op70indr8 = {
} M.op70indr8 = op70indr8
for k,v in pairs(op70indr8) do
M[k] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 4 end
local bin = function() local l65dbg=l65dbg
local bin = function() local l7801dbg=l7801dbg
local x = M.op_eval_word(late,early)
return { 0x70, v.opc, x&0xff, x>>8 }
end
@ -613,9 +613,9 @@ local op70r8ind = {
} M.op70r8ind = op70r8ind
for k,v in pairs(op70r8ind) do
M[k] = function(late, early)
local l65dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local l7801dbg = { info=debug.getinfo(2, 'Sl'), trace=debug.traceback(nil, 1) }
local size = function() late,early = M.size_op(late,early) return 4 end
local bin = function() local l65dbg=l65dbg
local bin = function() local l7801dbg=l7801dbg
local x = M.op_eval_word(late,early)
return { 0x70, v.opc, x&0xff, x>>8 }
end