Talk:Controller reading code

From NESdev Wiki
Revision as of 00:27, 11 January 2024 by Rainwarrior (talk | contribs) (→‎Removed last-frame compromise: follow-up, it was determined unsafe against DPCM in 2023)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
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
In november 2023 it was demonstrated that this is still subject to DPCM errors because the readjoy loop does not complete fast enough (allowing 2 matching corruptions of the same controller). So, aside from the compromise of 1 frame of lag, it wasn't completely DPCM error free either. Note that tepples has since replaced this code with the OAMDMA sync solution in his NROM template (gitub: pinobatch/nrom-template changes). So, perhaps this code may just be retired here for eternity. - Rainwarrior (talk) 00:27, 11 January 2024 (UTC)