Page 1 of 1

I wrote a zero page allocator for Kick Assembler.

Posted: Mon Sep 20, 2021 10:01 pm
by geon
https://github.com/geon/zpallocator/blo ... xample.asm

The idea is to be able to use the zero page for all variables. You allocate a zp address when you need it, and when you are done with it (ie. at the end of the macro/function scope), you deallocate it.

Code: Select all

	// BackgroundColor will contain a free zp address.
	.var backgroundColor = allocateZpByte()

	// Do stuff with backgroundColor
	lda #BROWN
	sta backgroundColor
	// ...
	deallocateZpByte(backgroundColor)
This way, I can write macros and pass arguments by reference without worrying about collisions.

I could use some help to define a couple of ranges of reserved zp addresses. I don't care about basic, so I can use $02-$70 or so without problem, but I might want to use some kernal routines like screen io and the jiffy clock. I'm not sure what other areas of the zp are important.

Re: I wrote a zero page allocator for Kick Assembler.

Posted: Tue Sep 21, 2021 6:06 am
by fachat
geon wrote:
https://github.com/geon/zpallocator/blob/master/zpallocator.example.asm

The idea is to be able to use the zero page for all variables. You allocate a zp address when you need it, and when you are done with it (ie. at the end of the macro/function scope), you deallocate it.
Not sure I understand. It looks like you're allocating at build time, right? How do you handle run time conflicts like one routine reusing the location reserved by another routine but calling each other?

André

Re: I wrote a zero page allocator for Kick Assembler.

Posted: Tue Sep 21, 2021 10:47 am
by geon
fachat wrote:
It looks like you're allocating at build time, right?
Yes.
fachat wrote:
How do you handle run time conflicts like one routine reusing the location reserved by another routine but calling each other?
For a function, you would need to allocate the address permanently. It works better with macros.

Re: I wrote a zero page allocator for Kick Assembler.

Posted: Tue Sep 21, 2021 2:16 pm
by fachat
Then don't you use macros in functions?

Did you try to 'simulate' it e.g. with a paper walk through?

Writing up how it works for a few real world test cases helps a great deal in understanding if it really works or where the pitfalls are.

Re: I wrote a zero page allocator for Kick Assembler.

Posted: Tue Sep 21, 2021 4:07 pm
by geon
fachat wrote:
Then don't you use macros in functions?
Actually no. Or rather, I haven't used functions at all so far, just macros.
fachat wrote:
Writing up how it works for a few real world test cases helps a great deal in understanding if it really works or where the pitfalls are.
Yes, I do TDD. I'll have to look at functions.

Re: I wrote a zero page allocator for Kick Assembler.

Posted: Tue Sep 21, 2021 8:11 pm
by BigDumbDinosaur
This sort of thing is easily done at run-time with the 65C816. Functions (subroutines) save the current value of DP (direct page pointer) on the stack, reserve stack space as needed and then point DP to that space. when the function exits it releases the stack space and restores DP to its entry value.

Re: I wrote a zero page allocator for Kick Assembler.

Posted: Wed Sep 22, 2021 8:08 am
by BigEd
Well done geon, thanks for sharing your creation!