I use the first few bytes of RAM for general purpose stuff like this, and I redefine variables in that area all the time. It's labeled "Scratchpad", and I shamelessly use
.org to redefine temporary variables in that space over and over:
Code:
.segment "DUMMY" ;<- dummy segment where I can pile variables
.org Scratchpad
SomeVariable: .res 2
AnotherVariable: .res 4
.reloc
Sometimes I need to use new temporary variables without overwriting others that are still in use, such as when one subroutine calls another and I need the temporary variables of both to share the scratchpad space. In this case so I just reserve a large enough unnamed space in the second block of variables, or I end the first block with a label marking its end and use that label in the
.org statement of the second block, so it starts right after the variables it isn't supposed to overwrite.
If you don't like the idea of using
.org, you can always declare these variables in a more old school way:
Code:
SomeVariable := Scratchpad + 0
AnotherVariable := Scratchpad + 2
If you know which specific variables can share the same byte, as in your ObjectOffset/SpriteCount example, you can always give 2 names to the same memory location like this:
Code:
ObjectOffset:
SpriteCount: .res 1
That is, only reserve the space after the last name you'll be using to refer to that location. You cal obviously also create aliases like this:
SpriteCount := ObjectOffsetAnother option is to go the "virtual registers" way, where you give each byte/word generic numbered names, like in your "GeneralPurpose1" idea, and either use those as is or create aliases for them as needed:
ObjectOffset := GeneralPurpose4There are many options, but the one thing that all these methods have in common is that you have to be extra careful with how you use this shared memory. When you have multiple names for the same memory locations it's easy to get confused and overwrite values you shouldn't.