2

My last question wasn't specific enough so I'll rephrase it. I've been having trouble with programming game consoles/computers like Commodore 64 that have active-low joysticks, where the bits are all packed into one or more bytes that is read from the joystick port. (I'm used to the NES where you have to strobe the keys and read it active-high). I'd like to convert the routine below to active-high, by flipping the bits of joypad1, removing the CMP #$FF, and changing each BNE to BEQ. I thought that would have been enough, but this ends up making none of the controls work. (Each direction is set up to move a hardware sprite around the screen.) How do I correctly alter the code below to read the input active-high?

JOYSTICK_FIRE   equ #%00010000
JOYSTICK_RIGHT  equ #%00001000
JOYSTICK_LEFT   equ #%00000100
JOYSTICK_DOWN   equ #%00000010
JOYSTICK_UP     equ #%00000001

handle_input:
    lda joypad1
    cmp #$FF                    ; if all bits are 1, no buttons pressed.
    bne continueReadingInput    
        jmp doneReadingInput    ;don't bother checking if no keys pressed
continueReadingInput:
    lda joypad1
    and #JOYSTICK_FIRE
    bne JoyNotFire
    ;YOUR CODE FOR WHAT HAPPENS WHEN YOU PRESS FIRE GOES HERE.

JoyNotFire:
    lda joypad1
    and #JOYSTICK_RIGHT
    bne JoyNotRight

    ;YOUR CODE FOR WHAT HAPPENS WHEN YOU PRESS LEFT GOES HERE.
JoyNotRight:
    lda joypad1
    and #JOYSTICK_LEFT
    bne JoyNotLeft

    ;YOUR CODE FOR WHAT HAPPENS WHEN YOU PRESS LEFT GOES HERE.

JoyNotLeft:
    lda joypad1
    and #JOYSTICK_DOWN
    bne JoyNotDown

    ;YOUR CODE FOR WHAT HAPPENS WHEN YOU PRESS DOWN GOES HERE.

JoyNotDown:
    lda joypad1_held
    and #JOYSTICK_UP
    bne doneReadingInput
    ;YOUR CODE FOR WHAT HAPPENS WHEN YOU PRESS UP GOES HERE.
    
doneReadingInput:
    rts
5
  • 6
    Could you not just throw in an appropriate EOR #$ff?
    – Tommy
    Sep 15 at 3:21
  • I tried that but it either led to a CPU jam or no input response. I seem to have the same problem regardless of which hardware I'm programming for, even the Game Boy Advance has the same issues. I'll try it again tonight, doing EOR #$FF after each LDA joypad1 and see what happens. I'd rather just do it once when the joypad1 variable is created but it causes problems. Sep 15 at 15:17
  • The EOR #$FF method after each instance of LDA JOYPAD1 works, unless I try comparing anything other than JOYPAD1 (such as JOYPAD1_HELD, JOYPAD1_PRESSED etc.) Flipping the bits read from the joystick and saving them to the variable crashes the game but only when pressing Down or Left. Very strange Sep 16 at 0:20
  • Couldn't you trace or single-step your code in an Emulator like VICE to see where it goes wrong?
    – TeaRex
    Sep 16 at 18:56
  • 1
    I find the debugger a little bit difficult to use. I'll give it a shot. I tried starting over and I'm not having the same issues so it must have been an error somewhere else. Sep 16 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.