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