Hi,
I implemented some hl stuff with ca65 and come across this thread. It's easy to have a stack in ca65.
Code:
.macro push stackname, value, sp ; stackname, value to assign, stackpointer
.ident( .sprintf("%s_%04X_",stackname,sp)) .set value
sp .set sp + 1
.endmacro
.macro pop stackname, var, sp ; stackname, identifier to assign value to, stackpointer
sp .set sp - 1
var .set .ident( .sprintf("%s_%04X_",stackname,sp))
.endmacro
These two macros give you as many stacks as you like, just identify which stack you are working with by name. I would like to write up better documentation, but there is some decent stuff here:
http://mynesdev.blogspot.ca/2012/10/ca6 ... again.htmlAlthough my goal is a NES project, this would work for any 6502 (just standard 6502 at this point since I am targeting the NES.)
Edit: Actually, I was thinking, it complicates these macros slightly, but makes for nicer code elsewhere - the macros should take care of the stack pointers too:
Code:
.macro push stackname, value
.ifndef ::.ident(.sprintf("_%s_POINTER", stackname))
::.ident(.sprintf("_%s_POINTER", stackname)) .set 0
.endif
::.ident( .sprintf("%s_%04X_",stackname,::.ident(.sprintf("_%s_POINTER", stackname)))) .set value
::.ident(.sprintf("_%s_POINTER", stackname)) .set ::.ident(.sprintf("_%s_POINTER", stackname)) + 1
.endmacro
.macro pop stackname, var ; puts a -1 in var if there is a problem
.ifndef ::.ident(.sprintf("_%s_POINTER", stackname)) ; unknown stack pointer
var .set -1
.exitmacro
.endif
::.ident(.sprintf("_%s_POINTER", stackname)) .set ::.ident(.sprintf("_%s_POINTER", stackname)) - 1
.if ::.ident(.sprintf("_%s_POINTER", stackname)) < 0 ; stackpointer is negative
var .set -1
.else
var .set ::.ident( .sprintf("%s_%04X_",stackname,::.ident(.sprintf("_%s_POINTER", stackname))))
.endif
.endmacro