;Joystick Constants and Functions for Commander X16 Computer SUBR _JOYSTK JYSTKS EQU 2 ;Number of Joysticks .GETJOY EQU $FF06 ;Kernal GETJOY Routine .JYDATA EQU $02BC ;Joystick 0 Data ;JOY1 JOY2 ;$02BC $02BF | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ; NES | A | B |SEL|STA|UP |DN |LT |RT | ; SNES | B | Y |SEL|STA|UP |DN |LT |RT | ; ;$02BD $20C0 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | ; NES | 0 | 0 | 0 | 0 | 0 | 0 | 0 |KBD| ; SNES | A | X | L | R | 1 | 1 | 1 | 1 | ; ;$02BE $02C1 $00 = joystick present ; $FF = joystick not present ;The Joystick Button is mapped to ;A,B on the NES Controller and ;A,B,X,Y on the SNES Controller ;Select and Start are Ignored as Bits 4 and 5 of the return value ;are always 0 because returning $FF means no controller found ;Joystick Bit Masks JOYUP EQU $08 ;Up JOYDN EQU $04 ;Down JOYLF EQU $02 ;Left JOYRT EQU $01 ;Right JOYB0 EQU $C0 ;Button ;Extended Bit Masks JOYBA EQU $80 ;A Button JOYBB EQU $40 ;B Button JOYSL EQU $20 ;Select JOYST EQU $10 ;Start JOYBL EQU $08 ;Left Button JOYBR EQU $04 ;Right Button JOYBX EQU $02 ;X Button JOYBY EQU $01 ;Y Button ;Controller Types JOYKBD EQU $00 ;Keyboard (NES Emulation) JOYNES EQU $01 ;NES Controller JOYSNS EQU $02 ;SNS Controller ;joytsk(j) - Read Joystick ;Args: A = Joystick Number (0 or 1) ;Sets: TEMP0 = Inverted Controller Status ; TEMP1 = Inverted Extended Status ; TEMP2 - Extended Result (Buttons) ;Destroys: TEMP3 ;Returns: A = Joystick Status (Joystick Bit Masks ; Y = Extended Status (Extended Bit Masks) ; X = Controller Type (0=Keyboard, 1=NES, 2=SNES) JOYSTK: CMP #JYSTKS ;If Invalid Joystick Number BCS .JOYSTE ; Return ERROR STA TEMP0 ;Save Joystick Number BYTE $FF JOYSTL: WAI ;Wait for Interrupt LDA $9F27 ; Check Vera Interrupt Register AND #$01 ; If Not Vera Interrupt BEQ JOYSTL ; Loop JSR .GETJOY ;Call Kernal GETJOY Routine LDA TEMP0 ;Retrieve Joystick Number ASL ;Multiply it by 3 ADC TEMP0 ;(Assumes Number<128) TAX ;and Copy to X-Register LDA .JYDATA+2,X ;If Controller Not Present BNE .JOYSTE ; Return ERROR LDA .JYDATA,X ;Get Base Status EOR #$FF ;Invert Bits JSR .JOYEXT ;Build Extended Results AND #$CF ;Mask off Select and Start .JOYSTX RTS ;Return Error .JOYSTE LDA #$FF RTS ;Build Extended Result .JOYEXT STA TEMP0 ;Save Controller Status STZ TEMP3 ;Clear Button Mask LDA .JYDATA+1,X ;Get Extended Status BIT #$0E ;Check Bits 1 - 3 BNE .JOYEXS ;If NES/Keyboard STA TEMP1 ; Save for Debugging EOR #$01 ; Invert Extended Bits TAX ; and Copy to X LDA TEMP0 ; Get Controller Status AND #$F0 ; Isolate Bits 4-7 TAY ; and Copy Back to Y BRA .JOYEXX ;Else .JOYEXS JSR .JOYSNS ; Process SNES Buttons .JOYEXX LDA TEMP0 ;Restore Controller Status ORA TEMP3 ;Combine Buttons RTS ;Process SNES Buttons .JOYSNS LDX #2 ;Set Controller Type to 2 EOR #$FF ;Invert Extended Bits STA TEMP1 ; and Save Result AND #$C0 ;Isolate SNES A,X Buttons STA TEMP3 ;and Save in Button Mask LDA TEMP1 ;Retrieve Extended Buttons AND #$30 ;Isolate L and R ASL ;and Shift to Bits 2 and 3 ASL BIT TEMP1 ;Get Ext Status High Bits BCC +2 ;If Extended Bit 7 was Set ORA #$80 ; Set Buttons Bit 7 (A) BVC +2 ;If Extended Bit 6 was Set ORA #$20 ; Set Buttons Bit 1 (X) STA TEMP2 ;Save Buttons A,X,L,R LDA TEMP0 ;Retrieve Controller Status AND #$30 ;Isolate Select and Start ORA TEMP2 ;Combine with A,X,L,R BIT TEMP2 ;Get Status High Bits BCC +2 ;If Status Bit 7 was Set ORA #$40 ; Set Buttons Bit 6 (B) BVC +2 ;If Status Bit 6 was Set ORA #$01 ; Set Buttons Nit 0 (Y) STA TEMP2 ;Save Buttons in TEMP2 TAY ; and Copy to Y RTS