Skip to content

Instantly share code, notes, and snippets.

@slashinfty
Created May 26, 2020 23:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slashinfty/b7ea0c9814ee82d9c02f49ddba1485c7 to your computer and use it in GitHub Desktop.
Save slashinfty/b7ea0c9814ee82d9c02f49ddba1485c7 to your computer and use it in GitHub Desktop.
local i = 0
local cx = 0
local cy = 0
local p1_cx = 0
local p1_cy = 0
local p2_cx = 0
local p2_cy = 0
local drawn = 0
function drawTile(x, y)
local px = bit.band((x * 0x10) - p2_cx + 0xB0, 0xFF)
local py = bit.band((y * 0x10) - p2_cy + 0xA8, 0xFF)
if (px > -16 and px < 160 and py > -16 and py < 136) then
drawn = drawn + 1
memory.usememorydomain("System Bus")
local index = 0xB000 + x + 0x100 * y
while index > 0xFFFF do index = index - 0x10000 end
local tile = memory.readbyte(index)
if (tile >= 0x04 and tile < 0x08) or (tile >= 0x10 and tile < 0x38) then
-- solid
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFFFFFF)
elseif tile >= 0x38 and tile < 0x48 then
-- platform
gui.drawRectangle(px, py, 16, 3, 0xFFFFFFFF, 0xAAFFFFFF)
elseif tile == 0x48 or tile == 0x49 then
-- coin
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFFFF00)
elseif tile == 0x4A then
-- level clear
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFF0000)
gui.drawText(px + 4, py, "Y", 0xFF000000)
elseif tile == 0x4B then
-- secret clear
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFF0000)
gui.drawText(px + 4, py, "N", 0xFF000000)
elseif tile == 0x50 or tile == 0x51 then
-- spikes
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFFFFFF)
gui.drawLine(px, py + 0, px + 3, py + 15, 0xFF000000)
gui.drawLine(px + 4, py + 15, px + 7, py + 0, 0xFF000000)
gui.drawLine(px + 8, py + 0, px + 11, py + 15, 0xFF000000)
gui.drawLine(px + 12, py + 15, px + 15, py + 0, 0xFF000000)
elseif tile >= 0x4C and tile < 0x58 then
-- water
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAA0000FF)
elseif tile == 0x00 or tile == 0x04 then
-- breakable block
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFFFFFF)
gui.drawLine(px + 3, py + 7, px + 4, py + 6, 0xFF000000)
gui.drawLine(px + 5, py + 6, px + 6, py + 7, 0xFF000000)
gui.drawLine(px + 9, py + 7, px + 10, py + 6, 0xFF000000)
gui.drawLine(px + 11, py + 6, px + 12, py + 7, 0xFF000000)
elseif tile == 0x01 or tile == 0x02 or tile == 0x7E then
-- ? block
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFFFFFF)
gui.drawText(px + 4, py, "?", 0xFF000000)
elseif tile >= 0x08 and tile < 0x10 then
-- pipe
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF, 0xAAFFFFFF)
if tile == 0x08 then
gui.drawRectangle(px+4, py, 12, 4, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x09 then
gui.drawRectangle(px, py, 12, 4, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x0A then
gui.drawRectangle(px+4, py + 12, 12, 4, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x0B then
gui.drawRectangle(px, py + 12, 12, 4, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x0C then
gui.drawRectangle(px, py+4, 4, 12, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x0D then
gui.drawRectangle(px, py, 4, 12, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x0E then
gui.drawRectangle(px+12, py+4, 4, 12, 0xFFFFFFFF, 0xAA00FF00)
elseif tile == 0x0F then
gui.drawRectangle(px+12, py, 4, 12, 0xFFFFFFFF, 0xAA00FF00)
end
else
-- empty
gui.drawRectangle(px, py, 16, 16, 0xFFFFFFFF)
end
if index == 0xA2D5 then
gui.drawText(px, py, "!!", 0xFFFF0000)
end
memory.usememorydomain("CartRAM")
end
end
-- Start Main --
console.writeline("Starting...")
memory.usememorydomain("CartRAM")
while true do
--gui.drawText(10, 10, "cx: " .. cx)
--gui.drawText(10, 30, "cy: " .. cy)
p2_cy = p1_cy
p2_cx = p1_cx
p1_cy = cy
p1_cx = cx
cy = memory.read_u16_be(0xF0E)
cx = memory.read_u16_be(0xF0A)
drawn = 0
local sx = bit.rshift((cx - 176), 4)
local sy = bit.rshift((cy - 168), 4)
local m = 0
while m < 11 do
local n = 0
while n < 10 do
drawTile(bit.band(sx + m, 0xFF), bit.band(sy + n, 0xFF))
n = n + 1
end
m = m + 1
end
i = i + 1
--gui.drawText(10, 60, "sx: " .. sx)
--gui.drawText(10, 75, "sy: " .. sy)
--gui.drawText(10, 90, "drawn: " .. drawn)
emu.frameadvance()
end
console.writeline("Done.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment