Skip to content

Instantly share code, notes, and snippets.

@nurpax
Last active August 10, 2019 20:39
Show Gist options
  • Save nurpax/5aeaba58e359c6d040a9e0f87fa68ab9 to your computer and use it in GitHub Desktop.
Save nurpax/5aeaba58e359c6d040a9e0f87fa68ab9 to your computer and use it in GitHub Desktop.
zero page allocation context example for c64jasm
module.exports = {
create: ({}, initial) => {
const stack = [initial];
return {
push: (elt) => {
stack.push(elt)
},
pop: () => stack.pop(),
top: () => {
return stack[stack.length-1];
}
}
}
}
!use "context" as zp
!let defaultZp = {
tmp0: $20,
sprite_idx: $22
}
; Zeropage allocation context for macro expansion
!let zpctx = zp.create(defaultZp)
; Macro expansion will look at the 'current' zp allocation
; by getting the top of the zp allocation stack.
!macro test() {
!let zp = zpctx.top()
lda #13
sta zp.tmp0
ldx zp.sprite_idx
}
func: {
+test()
rts
}
;; IRQ routine -- overload default ZP slots
;; used by above macro. Overloading uses
;; a stack to save/restore whatever is currently
;; assigned as the ZP allocation.
irq_func: {
!! zpctx.push({
tmp0: $40, ;; $40 instead of $20 here
sprite_idx: $42 ;; $42 instead of $22 here
})
+test()
!!zpctx.pop()
rti
}
;; This should use defaults again
func2: {
+test()
rts
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment