Skip to content

Instantly share code, notes, and snippets.

@niconii
Last active November 16, 2016 05:41
Show Gist options
  • Select an option

  • Save niconii/54c4765a13f3df3f6c12 to your computer and use it in GitHub Desktop.

Select an option

Save niconii/54c4765a13f3df3f6c12 to your computer and use it in GitHub Desktop.
; Goal: to turn controller button samples, incoming on the x register each
; frame, into three variables:
; - cur, which represents the current state of the controller buttons
; - prs, which represents the buttons pressed this frame
; - rls, which represents the buttons released this frame
;
; Example:
; x cur prs rls
; ................ ................ ................ <- initial values
; ...........1.... ...........1.... ...........1.... ................
; ...1.......1.... ...1.......1.... ...1............ ................
; ...1............ ...1............ ................ ...........1....
; ...1.11......... ...1.11......... .....11......... ................
; ...1.11......... ...1.11......... ................ ................
; ...1.1.....1.... ...1.1.....1.... ...........1.... ......1.........
; .....1.....1.... .....1.....1.... ................ ...1............
; .........1.1.... .........1.1.... .........1...... .....1..........
; .........1.1.... .........1.1.... ................ ................
; .........1.1.... .........1.1.... ................ ................
; ....1....1.1.... ....1....1.1.... ....1........... ................
; ....1....1.1.... ....1....1.1.... ................ ................
; ....1......1.... ....1......1.... ................ .........1......
; ...11......1.... ...11......1.... ...1............ ................
; ...11......1.... ...11......1.... ................ ................
; ...........1.... ...........1.... ................ ...11...........
;
; prs == !old & cur == (old ^ cur) & cur
; rls == old & !cur == (old ^ cur) & old
;b c a x y cur prs rls
;------------------------------
; cur old
lda cur ;2 4 old cur old a = cur
stx cur ;2 4 old cur cur cur = x
stx prs ;2 4 old cur cur cur prs = x
sta rls ;2 4 old cur cur cur old rls = a
trb prs ;2 7 old cur cur prs old prs &= !a
txa ;1 2 cur cur cur prs old a = x
trb rls ;2 7 cur cur cur prs rls rls &= !a
; 13 bytes, 32 cycles
;b c a x y cur prs rls
;------------------------------
; cur old
txa ;1 2 cur cur old a = x
eor cur ;2 4 o^c cur old a ^= cur
tay ;1 2 o^c cur o^c old y = a
and cur ;2 4 rls cur o^c old a &= cur
sta rls ;2 4 rls cur o^c old rls rls = a
stx cur ;2 4 rls cur o^c cur rls cur = x
tya ;1 2 o^c cur o^c cur rls a = y
and cur ;2 4 prs cur o^c cur rls a &= cur
sta prs ;2 4 prs cur o^c cur prs rls prs = a
; 15 bytes, 30 cycles
;---------------------------------------------------------------------------
; Or without rls:
;b c a x y cur prs
;--------------------------
; cur old
lda cur ;2 4 old cur old a = cur
stx cur ;2 4 old cur cur cur = x
stx prs ;2 4 old cur cur cur prs = x
trb prs ;2 7 old cur cur prs prs &= !a
; 8 bytes, 19 cycles
;b c a x y cur prs
;--------------------------
; cur old
txa ;1 2 cur cur old a = x
eor cur ;2 4 o^c cur old a ^= cur
stx cur ;2 4 o^c cur cur cur = x
and cur ;2 4 prs cur cur a &= cur
sta prs ;2 4 prs cur cur prs prs = a
; 9 bytes, 18 cycles
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment