add braces

This commit is contained in:
Redo 2022-10-26 22:01:00 -06:00
parent ded021ee99
commit 870bb34454
2 changed files with 21 additions and 11 deletions

View File

@ -45,7 +45,7 @@ local function mnemFromLine(line, instrs, validWords)
mnem = mnem:gsub(" %-?0x[0-9a-fA-F]+ " , function(n) return addNum (n) end) mnem = mnem:gsub(" %-?0x[0-9a-fA-F]+ " , function(n) return addNum (n) end)
mnem = mnem:gsub(" %-?[0-9a-fA-F]+h " , function(n) if not validWords[trim(n)] then return addNum (n) end end) mnem = mnem:gsub(" %-?[0-9a-fA-F]+h " , function(n) if not validWords[trim(n)] then return addNum (n) end end)
mnem = mnem:gsub(" %-?[0-9]+ " , function(n) if not validWords[trim(n)] then return addNum (n) end end) mnem = mnem:gsub(" %-?[0-9]+ " , function(n) if not validWords[trim(n)] then return addNum (n) end end)
mnem = mnem:gsub(" [a-zA-Z0-9_]+ " , function(n) if not validWords[trim(n)] then return addLabel(n) end end) mnem = mnem:gsub(" [a-zA-Z_][a-zA-Z0-9_%.]* ", function(n) if not validWords[trim(n)] then return addLabel(n) end end)
mnem = trim(mnem):gsub(" +", " ") mnem = trim(mnem):gsub(" +", " ")
return mnem, imms return mnem, imms
@ -185,11 +185,19 @@ end
local function fixCode(code) local function fixCode(code)
code = code:gsub(",", " ") code = code:gsub(",", " ")
code = code:gsub(":([^\\/])", ":\n%1") code = code:gsub(":([^\\/])", ":\n%1")
code = code:gsub("[ \t]+:", ":")
code = code:gsub("%]", " %] ") code = code:gsub("%]", " %] ")
code = code:gsub("%[", " %[ ") code = code:gsub("%[", " %[ ")
code = code:gsub("%*", " %* ") code = code:gsub("%*", " %* ")
code = code:gsub("\n[ \t\r\n]*", "\n") code = code:gsub("\n[ \t\r\n]*", "\n")
code = code:gsub(" +", " ") code = code:gsub(" +", " ")
local curScope = ""
code = code:gsub("([a-zA-Z_%.][a-zA-Z0-9_%.]*)(:?)", function(name, colon)
if name:sub(1, 1)=="." and not directiveFunctions[name:sub(2, #name)] then name = curScope..name end
if colon==":" then curScope = name:match("^[^%.]+") end
return name..colon
end)
return code return code
end end
local stringEscapes = { ["\\"] = "\\", ["n"] = "\n", ["r"] = "\r", ["t"] = "\t", ["0"] = "\0", ["\""] = "\"", ["\'"] = "\'", } local stringEscapes = { ["\\"] = "\\", ["n"] = "\n", ["r"] = "\r", ["t"] = "\t", ["0"] = "\0", ["\""] = "\"", ["\'"] = "\'", }
@ -249,6 +257,7 @@ local function prefixCode(code, fn) -- fix strings, add line numbers
out(stringEscapes[c] or error("invalid escape "..c)); state = "string"; out(stringEscapes[c] or error("invalid escape "..c)); state = "string";
end end
end end
assert(#bracestack==0, "unclosed brace")
local code2 = table.concat(outt) local code2 = table.concat(outt)
return code2 return code2
end end
@ -281,6 +290,7 @@ local function instrsFromArch(arch)
local mnem = instr.mnem local mnem = instr.mnem
mnem = mnem:gsub("%*([^ ])", "%* %1") mnem = mnem:gsub("%*([^ ])", "%* %1")
instrs[mnem] = instr.opcode instrs[mnem] = instr.opcode
if mnem:find("%*") then instrs[mnem:gsub("%*", "%[").." ]"] = instr.opcode end
end end
end end
return instrs return instrs
@ -329,7 +339,7 @@ local function disassembleMemory(mem, arch)
end end
local lb = table.concat(lineb, " ") local lb = table.concat(lineb, " ")
if lastaddr~=addr-tlen then print("...") end if lastaddr~=addr-tlen then print("...") end
print(string.format("%04X", addr-tlen).." | "..(" "):rep(9-#lb)..lb.." | "..table.concat(line, " ")) print(string.format("%04X", addr-tlen).." | "..(" "):rep(8-#lb)..lb.." | "..table.concat(line, " "))
lastaddr = addr lastaddr = addr
end end
end end

View File

@ -186,10 +186,10 @@ instructions = {
{ mnem="jpr imm8" , opcode=0x31, ncycles=2, {"loadImmed","memSaveT","instrSub1"}, {"jmpRelT"}, desc="I+=imm8" }, { mnem="jpr imm8" , opcode=0x31, ncycles=2, {"loadImmed","memSaveT","instrSub1"}, {"jmpRelT"}, desc="I+=imm8" },
{ mnem="jnz imm8" , opcode=0x30, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0NZ" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if !Zero" }, { mnem="jnz imm8" , opcode=0x30, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0NZ" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if !Zero" },
{ mnem="jpz imm8" , opcode=0x32, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0Z" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if Zero" }, { mnem="jpz imm8" , opcode=0x32, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0Z" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if Zero" },
{ mnem="jge imm8" , opcode=0x33, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0NC" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if Carry" }, { mnem="jlt imm8" , opcode=0x33, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0NC" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if Carry" },
{ mnem="jlt imm8" , opcode=0x34, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0C" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if !Carry" }, { mnem="jge imm8" , opcode=0x34, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0C" }, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if !Carry" },
{ mnem="jgt imm8" , opcode=0x35, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0C","instrNext0Z"}, {}, {"jmpRelT"}, {"instrNext"}, desc="I+=imm8 if !Zero&!Carry" }, { mnem="jle imm8" , opcode=0x35, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0C","instrNext0Z"}, {}, {"jmpRelT"}, {"instrNext"}, desc="I+=imm8 if !Zero&!Carry" },
{ mnem="jle imm8" , opcode=0x36, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0C","instrNext0Z"}, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if Zero|Carry" }, { mnem="jgt imm8" , opcode=0x36, ncycles=2, {"loadImmed","memSaveT","instrSub23Cond","instrNext0C","instrNext0Z"}, {}, {"instrNext"}, {"jmpRelT"}, desc="I+=imm8 if Zero|Carry" },
{ category = "Stack", catlet="S" }, { category = "Stack", catlet="S" },
{ mnem="pha" , opcode=0x40, {"pushReg","alurA","instrSub1"}, {"instrNext"}, desc="*(S++)=A" }, { mnem="pha" , opcode=0x40, {"pushReg","alurA","instrSub1"}, {"instrNext"}, desc="*(S++)=A" },