Skip to content

Commit

Permalink
[KERNAL] API to remap joy0 PS/2 keys (X16Community#307)
Browse files Browse the repository at this point in the history
  • Loading branch information
mooinglemur authored Mar 18, 2024
1 parent a08e5d3 commit a408981
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 9 deletions.
80 changes: 74 additions & 6 deletions kernal/drivers/x16/joystick.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
.include "banks.inc"
.include "io.inc"
.include "keycode.inc"
.include "regs.inc"

; KERNAL API
.export joystick_scan
.export joystick_get
; called by ps2 keyboard driver
.export joystick_from_ps2_init, joystick_from_ps2
.export joystick_ps2_keycodes

nes_data = d1pra
nes_ddr = d1ddra
Expand All @@ -23,6 +25,10 @@ bit_data3 = $20 ; PA5 DATA (controller #3)
bit_data2 = $40 ; PA6 DATA (controller #2)
bit_data1 = $80 ; PA7 DATA (controller #1)

mintab0_len = 9
mintab1_len = 5
mintabs_len = mintab0_len+mintab1_len

.segment "KVARSB0"

j0tmp: .res 1 ; keyboard joystick temp
Expand All @@ -32,6 +38,14 @@ joy2: .res 3 ; joystick 2 status
joy3: .res 3 ; joystick 3 status
joy4: .res 3 ; joystick 4 status

; Keep these allocations adjacent for code in joystick_ps2_keycodes

mintabs:
mintab0:
.res mintab0_len ; in-memory table for keycode -> joystick button (low controller bits)
mintab1:
.res mintab1_len ; in-memory table for keycode -> joystick button (high controller bits)

.segment "JOYSTICK"

;---------------------------------------------------------------
Expand Down Expand Up @@ -238,13 +252,65 @@ joystick_from_ps2_init:
lda #$ff
sta joy0
sta joy0+1
sta joy0+2 ; joy0 bot present
sta joy0+2 ; joy0 not present

; populate the default keyboard joystick mapping

ldx #intab0_len
: lda intab0-1,x
sta mintab0-1,x
dex
bne :-

ldx #intab1_len
: lda intab1-1,x
sta mintab1-1,x
dex
bne :-

rts

;----------------------------------------------------------------------
; joystick_ps2_keycodes:
;
; Get or set the keyboard mapping for joystick 0
; If carry is set, return the existing keycodes in r0-r6
; If carry is clear, set the keycodes from the values in r0-r6
;
;
; | r0L | r0H | r1L | r1H | r2L | r2H | r3L | r3H | r4L |
; | Right | Left | Down | Up | Start | Select | Y | B | B (alternate) |
;
; | r4H | r5L | r5H | r6L | r6H |
; | R (shoulder) | L (shoulder) | X | A | A (alternate) |
;----------------------------------------------------------------------


joystick_ps2_keycodes:
KVARS_START

ldx #mintabs_len
bcs @get
@set:
lda r0-1,x
sta mintabs-1,x
dex
bne @set
bra @end
@get:
lda mintabs-1,x
sta r0-1,x
dex
bne @get
@end:

KVARS_END
rts

;----------------------------------------------------------------------
; joystick_from_ps2:
;
; convert PS/2 scancode into SNES joystick state (internal)
; convert PS/2 keycode into SNES joystick state (internal)
;
; Note: This is called from the ps2kbd driver while bank 0 is active,
; no bank switching is performed.
Expand All @@ -256,15 +322,15 @@ joystick_from_ps2:
and #$7f

; Search key code table 0
ldx #intab0_len
: cmp intab0-1,x
ldx #mintab0_len
: cmp mintab0-1,x
beq @match0
dex
bne :-

; Search key code table 1
ldx #intab1_len
: cmp intab1-1,x
ldx #mintab1_len
: cmp mintab1-1,x
beq @match1
dex
bne :-
Expand Down Expand Up @@ -339,8 +405,10 @@ intab0:
.byte KEYCODE_ENTER, KEYCODE_LSHIFT, KEYCODE_A, KEYCODE_Z
.byte KEYCODE_LALT
intab0_len = *-intab0
.assert intab0_len = mintab0_len, error, "memory allocation for the PS/2 keycode lookup table mintab0 doesn't match the default table's length"

intab1:
.byte KEYCODE_C, KEYCODE_D, KEYCODE_S, KEYCODE_X
.byte KEYCODE_LCTRL
intab1_len = *-intab1
.assert intab1_len = mintab1_len, error, "memory allocation for the PS/2 keycode lookup table mintab1 doesn't match the default table's length"
8 changes: 5 additions & 3 deletions kernal/x16/extapi.s
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.import clear_status
.import extapi_getlfs
.import mouse_sprite_offset
.import joystick_ps2_keycodes

.export extapi

Expand Down Expand Up @@ -32,6 +33,7 @@ secrts:

apitbl:
.word secrts-1 ; slot 0 is reserved
.word clear_status-1 ; API 1
.word extapi_getlfs-1 ; API 2
.word mouse_sprite_offset-1 ; API 3
.word clear_status-1 ; API 1
.word extapi_getlfs-1 ; API 2
.word mouse_sprite_offset-1 ; API 3
.word joystick_ps2_keycodes-1 ; API 4

0 comments on commit a408981

Please sign in to comment.