diff --git a/include/x16/joystk.a02 b/include/x16/joystk.a02 index 3babf20..5cb1e84 100644 --- a/include/x16/joystk.a02 +++ b/include/x16/joystk.a02 @@ -1,33 +1,128 @@ -;Joystick Constants and Functions -;for Commander X16 Computer +;Joystick Constants and Functions for Commander X16 Computer + + SUBR _JOYSTK JYSTKS EQU 2 ;Number of Joysticks -.JYDATA EQU $02BC ;Joystick 0 Data - .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 $80 ;Button +JOYB0 EQU $C0 ;Button -;Read Joystick -JOYSTK: CMP #JYSTKS ;If Invalid Joystick Number - BCS JOYSTE ; Return ERROR - STA TEMP0 ;Save Joystick Number - JSR $FF06 ;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 $02BE,X ;If Controller Not Present - BNE JOYSTE ; Return ERROR - LDA $02BC,X ;Read Controller Status - ;EOR #$FF ;Invert Bits - AND #$CF ;and Mask off Select/Start +;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 -JOYSTE: LDA #255 ;Return Error Code + +;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 diff --git a/include/x16/joystk.h02 b/include/x16/joystk.h02 index a689489..0d61cdb 100644 --- a/include/x16/joystk.h02 +++ b/include/x16/joystk.h02 @@ -1,24 +1,43 @@ -/* Joystick Functions for * - * Commander X-16 Computer */ +/* Joystick Functions for Commander X-16 Computer * + * Supports Keyboard, NES, and SNES Controllers * + * A and B on Keyboard and SNES Controller and * + * A, B, X, Y on SNES Controller are all * + * mapped to the joystick button JOYB0 * + * Individual button status and Controller Type * + * are returned as extended results */ + +/* Keyboard to NES Controller Mappings * + * Key: Ctrl Alt Space Enter Up Down Left Right * + * NES: A B Select Start Up Down Left Right */ #define JYSTKS 2 //Number of Joysticks +//Joystick Result Bitmasks #define JOYUP $08 //Up #define JOYDN $04 //Down #define JOYLF $02 //Left #define JOYRT $01 //Right -#define JOYB0 $80 //Button +#define JOYB0 $C0 //Button -char jydata[]; //Joystick Data +//Extended Result Bitmasks +#define JOYBA $80 //A Button +#define JOYBB $40 //B Button +#define JOYSL $20 //Select +#define JOYST $10 //Start +#define JOYBL $08 //Left Button +#define JOYBR $04 //Right Button +#define JOYBX $02 //X Button +#define JOYBY $01 //Y Button -/* Poll Joysticks` * - * Sets: j = Joystick Number * - * Returns: $FF = Error * - * No Joysticks */ -void getjoy(); +//Controller Types +#define JOYKBD $00 //Keyboard (NES Emulation) +#define JOYNES $01 //NES Controller +#define JOYSNS $02 //SNS Controller -/* Read Joystick State * - * Args: j = Joystick Number * - * Returns: $FF = Error * - * No Joysticks */ +/* Read Joystick Status * + * Args: j = Joystick Number * + * Returns: char j = Joystick Status * + * $FF = Not Present * + * char b = Extended Status * + * char c = Controller Type */ char joystk();