Advertisement
HertzDevil

building all the driver headers in 9 seconds

Jun 2nd, 2015
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.28 KB | None | 0 0
  1. local resolveLabel = function (str)
  2.   if str == "ft_vibrato_table" then return "VIBRATO" end
  3.   if str == "ft_note_table_vrc7_l" then return "SNDCHIP_VRC7" end
  4.  
  5.   local chip = string.upper(string.match(str, "ft_periods_(.*)"))
  6.   if chip == "SAWTOOTH" then chip = "VRC6"
  7.   elseif chip == "NTSC" then chip = "NONE"
  8.   elseif chip == "PAL" then chip = "2A07"
  9.   end
  10.   return "SNDCHIP_" .. chip
  11. end
  12.  
  13. local build = function (chip)
  14.   print("Building NSF driver for " .. chip .. "...")
  15.   os.execute([[ca65 ../driver.s -l out_]]..chip..[[.lst -D USE_]]..chip..[[ -D PACKAGE]])
  16.   os.execute([[ld65 -o c0_]]..chip..[[.bin ../driver.o -C c0.cfg]])
  17.   os.execute([[ld65 -o c1_]]..chip..[[.bin ../driver.o -C c1.cfg]])
  18.  
  19.   local adr = {}
  20.   local pos = {}
  21.   for str in io.lines("out_"..chip..".lst") do
  22.     local hex, used, ident, colon = string.match(str, "(......).....(..).*%f[_%w]([_%w]*)(:?).*;; Patch")
  23.     if hex then
  24.       hex = tonumber(hex, 16)
  25.       used = used == "A9"
  26.       colon = colon == ":"
  27.       ident = resolveLabel(ident)
  28.       if colon then -- ASM label; add to frequency table
  29.         pos[ident] = hex
  30.       elseif used then -- ASM jump; remove entry from reloc table
  31.         adr[hex] = ident
  32.         if ident == "SNDCHIP_VRC7" and chip == "VRC7" or chip == "ALL" then adr[-1] = ident end
  33.       end
  34.     end
  35.   end
  36.   os.remove("out_"..chip..".lst")
  37.  
  38.   local in1 = io.open("c1_"..chip..".bin", "rb")
  39.   local in2 = io.open("c0_"..chip..".bin", "rb")
  40.   local relStr = ""
  41.   local hedStr = ""
  42.   local tRel, tHed = {}, {}
  43.   while true do
  44.     local x, y = in1:read(1), in2:read(1)
  45.     if not x then break
  46.     else x, y = string.byte(x), string.byte(y) end
  47.     if x == y then
  48.       tHed[#tHed + 1] = string.format("0x%02X,", x)
  49.     else
  50.       tHed[#tHed + 1] = string.format("0x%02X,", x - 0xC1)
  51.       if not adr[in1:seek() - 6] then tRel[#tRel + 1] = string.format("%d,", in1:seek() - 2) end
  52.       if #tRel == 20 then
  53.         relStr = relStr .. "\t" .. table.concat(tRel, " ") .. "\n"
  54.         tRel = {}
  55.       end
  56.     end
  57.     if #tHed == 16 then
  58.       hedStr = hedStr .. "\t" .. table.concat(tHed, " ") .. "\n"
  59.       tHed = {}
  60.     end
  61.   end
  62.  
  63.   if #tRel > 0 then
  64.     relStr = relStr .. "\t" .. table.concat(tRel, " ") .. "\n"
  65.   end
  66.   if #tHed > 0 then
  67.     hedStr = hedStr .. "\t" .. table.concat(tHed, " ") .. "\n"
  68.   end
  69.  
  70.   local final = io.open("output/drv_" .. string.lower(chip) .. ".h", "w")
  71.   final:write("const unsigned char DRIVER_", chip, "[] = {\t\t// // //\n")
  72.   final:write(hedStr)
  73.   final:write("};\n\nconst int DRIVER_RELOC_WORD_", chip, "[] = {\n")
  74.   final:write(relStr)
  75.   final:write("};\n\nconst int DRIVER_FREQ_TABLE_", chip, "[] = {\n")
  76.   for k, v in pairs(adr) do
  77.     final:write("\t")
  78.     final:write(k == -1 and "    -1" or string.format("0x%04X", k))
  79.     final:write(string.format(", 0x%04X, %s,\n", pos[v], v))
  80.   end
  81.   final:write("};\n\nconst unsigned int VIBRATO_TABLE_LOCATION_", chip, string.format(" = 0x%X;", pos["VIBRATO"]))
  82.  
  83.   in1:close()
  84.   in2:close()
  85.   final:close()
  86.   os.remove("c0_"..chip..".bin")
  87.   os.remove("c1_"..chip..".bin")
  88. end
  89.  
  90. for _, v in pairs {"2A03", "VRC6", "VRC7", "MMC5", "FDS", "N163", "S5B", "ALL"} do
  91.   build(v)
  92. end
  93. print("All driver headers created in " .. tonumber(os.clock()) .. " seconds.")
  94. os.execute("pause")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement