Talk:Controller reading code

From NESdev Wiki
Jump to navigationJump to search

Reorganization needed with Standard controller

See: Talk:Standard_controller#Reorganization_needed_with_Controller_Reading

Removed last-frame compromise

This code appeared in the DPCM Safety using Repeated Reads section. I have removed it, because I think it hurts comprehension for new readers who may not understand what they are trading away to save a few cycles they may not need. There's probably a place for this code somewhere, but I don't think it belongs in the middle of the article, and certainly not in lieu of a simpler and more standard example.

I left a description of the technique, but not the code. - Rainwarrior (talk) 13:14, 16 April 2019 (MDT)

Code:

last_frame_buttons1 = $00
last_frame_buttons2 = $01
first_read_buttons1 = $02
first_read_buttons2 = $03

readjoy_safe:
    lda buttons2
    sta last_frame_buttons2
    lda buttons1
    sta last_frame_buttons1

    ; Read the controllers once and stash the result
    jsr readjoy
    lda buttons2
    sta first_read_buttons2
    lda buttons1
    sta first_read_buttons1

    ; Read the controllers again and compare
    jsr readjoy
    ldx #1
cleanup_loop:
    ; Ignore read values if a bit deletion occurred
    lda buttons1,x
    cmp first_read_buttons1,x
    beq not_glitched
        lda last_frame_buttons,x
        sta buttons1,x
    not_glitched:

    dex
    bpl cleanup_loop

    rts