Skip to content

Commit

Permalink
in-game hooks: more time efficient Auto Joypad Read snooping
Browse files Browse the repository at this point in the history
This is a bit of a fire-and-forget strategy. After NMI/IRQ read the AJR
result register as fast as possible to catch the readout from last
frame.
In some occasions AJR has already JUST started (its starting point is
variable) in which case the readout is #$0000. For IRQ hooks this might
lead to unusable readouts depending on the time of the IRQ.
Also don't compare controller button mappings when no buttons are
pressed.
  • Loading branch information
mrehkopf committed Feb 1, 2015
1 parent b1e44e0 commit 692d033
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions snes/nmihook.a65
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
#include "memmap.i65"

; *=$2BF0
; *=$2BB0
nmihook:
jsr !BRAM_PAYLOAD
php
rep #$30 : .al : .xl
pha
lda @$004218
sta @NMI_PAD
jsr BRAM_PAYLOAD
pla
plp
jmp $ffff
; *=2BC4
irqhook:
jsr !BRAM_PAYLOAD
php
rep #$30 : .al : .xl
pha
lda @$004218
sta @NMI_PAD
jsr BRAM_PAYLOAD
pla
plp
jmp $ffff

; *=$2A04
nmi_payload:
php
rep #$30 : .al : .xl
phb
phk
plb
pha
lda #$7548
sta !BRAM_UNLOCK
lda #$7a72
sta !BRAM_UNLOCK+2
phx
phy
phd
lda #$2b00
tcd
lda #$7548
sta <BRAM_UNLOCK
lda #$7a72
sta <BRAM_UNLOCK+2
; backup vectors
sep #$20 : .as
lda #$01
Expand All @@ -36,16 +48,16 @@ nmi_payload:
stx <IRQ_VECT_ORIG
stz <NMI_VECT_DISABLE
; only read controller every couple of frames
clc
lda <NMI_RUNMASK
cmp #$80
rol
sta <NMI_RUNMASK
bcc nmi_patches
; clc
; lda <NMI_RUNMASK
; cmp #$80
; rol
; sta <NMI_RUNMASK
; bcc nmi_patches
; read controller
lda <MIRROR_4200
lsr
; auto read is active, so we wait for it to finish and use the result
; auto read is active, so we reuse the early snapshot taken
bcs nmi_autoread
; no auto read; read the controller manually
stz $4016
Expand All @@ -65,21 +77,19 @@ nmi_payload:
rol <NMI_PAD+1
dex
bne -
bra nmi_pad_shortcut
nmi_autoread:
ldx #$0000
nmi_pad_shortcut:
ldy <NMI_PAD
beq nmi_patches
; check button combinations
; L+R+Start+Select : $3030
; L+R+Select+X : $2070
; L+R+Start+A : $10b0
; L+R+Start+B : $9030
; L+R+Start+Y : $5030
; L+R+Start+X : $1070
bra nmi_pad_loop
nmi_autoread:
- lda $4212
lsr
bcs -
ldy $4218
sty <NMI_PAD
ldx #$0000
nmi_pad_loop:
ldy <NMI_PAD_BUTTONS, x
cpy <NMI_PAD
Expand Down Expand Up @@ -126,14 +136,12 @@ nmi_patches:
bne -
nmi_exit:
; clean up
lda $ffea ; get another 72 cycles of BRAM visibility
stz <BRAM_UNLOCK ; disable BRAM visibility for game
pld
lda $ffea ; get another 48 cycles of BRAM visibility
stz !BRAM_UNLOCK ; disable BRAM visibility for game
rep #$30 : .al : .xl
ply
plx
pla
plb
plp
rts
.byt "bram_end"

0 comments on commit 692d033

Please sign in to comment.