scotws wrote:
So now that I have lots of tools, I'm thinking about, amazingly enough, actually writing code again. One thing I'm not sure about with the 65816 is how to organize the Direct Page (the equivalent of Zero Page on the 6502) so programs don't end up messing stuff up if there is more than one of them in memory.
My usual tack if the program is running in bank $00 (which is the case on POC V1, since there is no RAM above bank $00) is to set direct page to a BSS area after the program, since direct page effectively "grows up," unlike the stack, which grows down. The BSS has only to be as large as the maximum DP address you are going to use, which the assembler should be able to figure out for you. The DP located in BSS would be used by the mainline routine. Subroutines can be given ephemeral direct pages on the stack, which is a painless thing to do—some simple math and fiddling with the stack pointer.
If the code is outside of bank $00 you may have to have a supervisor that would keep track of where direct pages and stacks are located, and have each program request a direct page and stack range from the supervisor at startup. The supervisor can return the DP starting address in
.C and the top-of-stack address in
.X for convenience. You'd have to make sure that the running program doesn't violate the bounds of DP and/or the stack, or else you'd have a major mess on your hands.
Keep in mind that if the base address for DP is not on an even page boundary a one cycle penalty will apply to DP accesses. Hence
DP should be set to
$xx00 if performance is a concern (where
xx is any page).
The fact that DP and stack references are always to bank $00 is something that I eventually hope to mitigate in programmable logic.