make dasm only show code

This commit is contained in:
Redo 2022-11-10 20:35:52 -06:00
parent 0e6ec8460b
commit 1dc679b430

View File

@ -81,16 +81,17 @@ local function mnemFromLine(line, instrs, validWords)
return mnem, imms return mnem, imms
end end
local function addByte(state, val) local function addByte(state, val, code)
assert(val>=-128 and val<=255, "invalid byte "..val) assert(val>=-128 and val<=255, "invalid byte "..val)
assert(state.memory[state.curAddr]==nil, "overwriting memory at "..state.curAddr) assert(state.memory[state.curAddr]==nil, "overwriting memory at "..state.curAddr)
state.memory[state.curAddr] = val%256 state.memory[state.curAddr] = val%256
if code then state.codeMap[state.curAddr] = true end
state.curAddr = state.curAddr + 1 state.curAddr = state.curAddr + 1
end end
local function addWord(state, val) local function addWord(state, val, code)
assert(val>=0 and val<=65535, "invalid word "..val) assert(val>=0 and val<=65535, "invalid word "..val)
addByte(state, math.floor(val/256)) addByte(state, math.floor(val/256), code)
addByte(state, val%256) addByte(state, val%256, code)
end end
local function addSpace(state, len) local function addSpace(state, len)
for i = 1, len do for i = 1, len do
@ -104,17 +105,19 @@ local function assembleInstruction(line, state, instrs, validWords)
local opcode = instrs[mnem] or error("invalid instruction \""..line.."\" (mnem \""..mnem.."\")") local opcode = instrs[mnem] or error("invalid instruction \""..line.."\" (mnem \""..mnem.."\")")
local writeimms = true local writeimms = true
local padlen = 0 local padlen = 0
local isInstr
if type(opcode)=="function" then if type(opcode)=="function" then
padlen, writeimms = opcode(imms) padlen, writeimms = opcode(imms)
addSpace(state, padlen) addSpace(state, padlen)
elseif opcode>=0 then elseif opcode>=0 then
addByte(state, opcode) isInstr = true
addByte(state, opcode, isInstr)
end end
if writeimms then if writeimms then
for _, imm in ipairs(imms) do for _, imm in ipairs(imms) do
if imm.val then if imm.val then
if imm.len==1 then addByte(state, imm.val) if imm.len==1 then addByte(state, imm.val, isInstr)
elseif imm.len==2 then addWord(state, imm.val) elseif imm.len==2 then addWord(state, imm.val, isInstr)
else error("invalid imm len") end else error("invalid imm len") end
elseif imm.label then elseif imm.label then
table.insert(state.labelReplacements, { table.insert(state.labelReplacements, {
@ -122,6 +125,7 @@ local function assembleInstruction(line, state, instrs, validWords)
addr = state.curAddr, addr = state.curAddr,
len = imm.len, len = imm.len,
rel = imm.len==1, rel = imm.len==1,
isCode = isInstr,
}) })
state.curAddr = state.curAddr + imm.len state.curAddr = state.curAddr + imm.len
else error("invalid imm") end else error("invalid imm") end
@ -144,6 +148,7 @@ local function assembleCode(code, instrs)
fileName = "", fileName = "",
curAddr = 0, curAddr = 0,
memory = {}, memory = {},
codeMap = {},
labelReplacements = {}, labelReplacements = {},
labelAddrs = {}, labelAddrs = {},
} }
@ -167,12 +172,12 @@ local function assembleCode(code, instrs)
for _, rep in ipairs(state.labelReplacements) do for _, rep in ipairs(state.labelReplacements) do
local labelAddr = state.labelAddrs[rep.name] or error("no label named "..rep.name) local labelAddr = state.labelAddrs[rep.name] or error("no label named "..rep.name)
state.curAddr = rep.addr state.curAddr = rep.addr
if rep.len==1 then addByte(state, labelAddr-(rep.addr+1)) if rep.len==1 then addByte(state, labelAddr-(rep.addr+1), rep.isCode)
elseif rep.len==2 then addWord(state, labelAddr) elseif rep.len==2 then addWord(state, labelAddr , rep.isCode)
else error("invalid labelreplace len") end else error("invalid labelreplace len") end
end end
return state.memory return state.memory, state.codeMap
end end
local function readFile(fn) local function readFile(fn)
@ -436,8 +441,8 @@ local function assembleFile(fn, arch)
code = preprocessCode(code) code = preprocessCode(code)
code = fixCode(code) code = fixCode(code)
local instrs = instrsFromArch(arch) local instrs = instrsFromArch(arch)
local mem = assembleCode(code, instrs) local mem, code = assembleCode(code, instrs)
return mem return mem, code
end end
local function mnemsFromArch(arch) local function mnemsFromArch(arch)
@ -449,7 +454,7 @@ local function mnemsFromArch(arch)
end end
return mnems return mnems
end end
local function disassembleMemory(mem, arch) local function disassembleMemory(mem, code, arch)
print("Disassembly:") print("Disassembly:")
local mnems = mnemsFromArch(arch) local mnems = mnemsFromArch(arch)
local addr = 0 local addr = 0
@ -457,7 +462,7 @@ local function disassembleMemory(mem, arch)
local lastaddr = 0 local lastaddr = 0
while addr<=0xFFFF do while addr<=0xFFFF do
local opcode = nextByte() local opcode = nextByte()
if opcode then if opcode and ((not code) or code[addr-1]) then
local line = {} local line = {}
local mnem = mnems[opcode] or "???" local mnem = mnems[opcode] or "???"
table.insert(line, trim(mnem:gsub("imm[0-9]+", ""))) table.insert(line, trim(mnem:gsub("imm[0-9]+", "")))
@ -593,12 +598,12 @@ end
local function strtovec(str) local v = {}; for word in str:gmatch("[^ \t\r\n]+") do table.insert(v, tonumber(word)) end; return v; end local function strtovec(str) local v = {}; for word in str:gmatch("[^ \t\r\n]+") do table.insert(v, tonumber(word)) end; return v; end
function AssembleFile(fn, romsizes, offsets, lens) local offset = tonumber(offsets); local len = tonumber(lens); local romsize = strtovec(romsizes); function AssembleFile(fn, romsizes, offsets, lens) local offset = tonumber(offsets); local len = tonumber(lens); local romsize = strtovec(romsizes);
local arch = arch8608 local arch = arch8608
local mem = assembleFile(fn, arch) local mem, code = assembleFile(fn, arch)
print(""..fn:match("[^/\\]+$").."\n") print(""..fn:match("[^/\\]+$").."\n")
printMemory(mem) printMemory(mem)
assert(#romsize==3, "incorrect rom size") assert(#romsize==3, "incorrect rom size")
buildMemory(mem, romsize, offset, len) buildMemory(mem, romsize, offset, len)
disassembleMemory(mem, arch) disassembleMemory(mem, code, arch)
end end
ts.eval [[ ts.eval [[
function AssembleFile(%fn, %romsize, %offset, %len) { luacall("AssembleFile", strReplace(%fn, "$", "Add-ons/_misc/rom/8608programs/"), %romsize, %offset, %len); } function AssembleFile(%fn, %romsize, %offset, %len) { luacall("AssembleFile", strReplace(%fn, "$", "Add-ons/_misc/rom/8608programs/"), %romsize, %offset, %len); }