Log In  


As we've been messing around with doing various things in PICO-8, we've realized that there are a fair few operations involving moving blocks of bytes around in base RAM - animating sprites by switching sprites with each other on the spritesheet, storing the current draw state or random number seed during an operation so it can be restored afterwards, etc. and so on - where hardcoding memory addresses for temporary storage could lead to one operation clobbering data for another.

We don't know a lot about computer science, but it feels like the proper way to handle this is either using a peek to grab the data as a variable in Lua RAM and poke()ing it back afterwards or with a call stack somewhere in the user data RAM that functions can push data onto and pull data from. If a call stack actually is a good tool to have for this, it seems like a good function to implement as a library routine.

When we were thinking about it a while ago, we came up with:

-- stack handling in 55 tokens
-- incl. 25 tokens of overflow/
-- underflow handling

do
	local pointer=0x5e00
	-- i.e. just after user data

	function push(addr,len)
		pointer -= len
		if pointer < 0x4300 then
			stop("call stack overflow from "..addr)
		end
		memcpy(pointer,addr,len)
	end
	function pop(addr,len)
		if pointer+len > 0x5e00 then
			stop("call stack underflow to "..tostr(addr,true))
		end
		memcpy(addr,pointer,len)
		pointer += len
	end
end

...but I wanna kick it out onto the forums for people better at optimizing PICO-8 code to review and comment on.



2

PICO8 lua gives you 2MB - much easier to work with than 0x4300 zone tbh.



[Please log in to post a comment]