read: ldx #05h loop2: ldy #09h sty 4016h dey sty 4016h loop: txa ;read the controllers 3x each lsr a bcs + lda 4016h bcc ++ + lda 4017h ++ lsr a rol buttons,x dey bne loop dex bpl loop2 inx ;x = 0 inx ;x = 1 loop3: txa ;y = 0/1 for buttons+0 tay lda buttons+0,x ;check buttons to see if any pair matches cmp buttons+2,x beq + lda buttons+0,x cmp buttons+4,x beq + iny ;y = 2/3 for buttons+0 iny lda buttons+2,x cmp buttons+4,x bne ++ ;no match at all, skip updating this time + lda buttons+0,y ;get the buttons that match at least one set pha eor oldbuts,x and buttons+0,y sta pressed,x pla sta oldbuts,x ;save for next check loop ++ dex bpl loop3 rts RAM variables: ;first byte is controller 1, second byte is controller 2. buttons: .dsb 6 ;reserve 6 bytes for current state of the buttons realbut: .dsb 2 ;reserve oldbuts: .dsb 2 ;reserve 2 bytes for old state of the buttons pressed: .dsb 2 ;reserve 2 bytes for which buttons were just pressed