Skip to content

Commit

Permalink
[KERNAL] enable control and persisting of ps2kbd LED state, respond t…
Browse files Browse the repository at this point in the history
…o numlock (#353)

* [KERNAL] enable control and persisting of ps2kbd LED state, respond to numlock

* extapi setter must use .X, and for consistency change getter
  • Loading branch information
mooinglemur authored Aug 22, 2024
1 parent 5501419 commit d2841e8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 18 deletions.
9 changes: 8 additions & 1 deletion kernal/drivers/x16/ps2data.s
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ CMD_GET_PS2DATA_FAST = $43
PS2DATA_OLD_STYLE = $01
PS2DATA_NEW_STYLE = $02

LED_NUM_LOCK = 2

.import i2c_read_byte, i2c_read_first_byte, i2c_direct_read, i2c_read_next_byte, i2c_read_stop, i2c_write_byte
.import tpmflg
.import tpmflg, ledstate

.export ps2data_init, ps2data_fetch, ps2data_kbd, ps2data_kbd_count, ps2data_mouse, ps2data_mouse_count
.export ps2data_keyboard_and_mouse, ps2data_keyboard_only, ps2data_raw

Expand Down Expand Up @@ -46,6 +49,10 @@ ps2data_init:
; Clear keyboard set typematic rate/delay flag
stz tpmflg

; Set internal numlock state to defaults
lda #LED_NUM_LOCK
sta ledstate

; Compare SMC firmare version major
ldx #I2C_ADDR
ldy #CMD_GET_VER1
Expand Down
60 changes: 43 additions & 17 deletions kernal/drivers/x16/ps2kbd.s
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
.import memory_decompress_internal ; [lzsa]

.export kbd_config, kbd_scan, receive_scancode_resume, keymap, ps2kbd_typematic
.export MODIFIER_4080
.export tpmflg
.export kbd_leds
.export tpmflg, ledstate

.import extapi, fetch_typematic_from_nvram

Expand Down Expand Up @@ -69,13 +69,15 @@ kbtmp: .res 1 ; meant for exclusive use in kbd_scan
.segment "KVARSB0"

tpmflg: .res 1 ; Set typematic rate/delay flag
brkflg: .res 1 ; PS/2: was key-up event
ledstate:
.res 1
curkbd: .res 1 ; current keyboard layout index
dk_shift:
.res 1
dk_scan:
.res 1


.segment "KEYMAP"
keymap_data:
.res TABLE_COUNT*128
Expand Down Expand Up @@ -278,7 +280,7 @@ _kbd_scan:
@1: ldy tpmflg
bne @3
inc tpmflg

pha
phx
jsr fetch_typematic_from_nvram
Expand All @@ -303,13 +305,21 @@ _kbd_scan:

; Is it Caps Lock down?
cmp #KEYCODE_CAPSLOCK
bne is_reg_key
bne check_numlock
pla ; Restore key code from stack
bmi @5 ; Ignore key up
lda shflag
eor #MODIFIER_CAPS
sta shflag
jmp set_caps_led
and #MODIFIER_CAPS
beq @caps_off
lda #LED_CAPS_LOCK
tsb ledstate
jmp _set_kbd_leds
@caps_off:
lda #LED_CAPS_LOCK
trb ledstate
jmp _set_kbd_leds
@5: rts

is_mod_key:
Expand All @@ -331,15 +341,23 @@ mod_key_down:
and #((~MODIFIER_TOGGLE_MASK) & $ff)
jmp check_charset_switch

is_reg_key:
pla

check_numlock:
pla
; Ignore key up events
bpl :+
rts

: cmp #KEYCODE_NUMLOCK
bne is_reg_key

lda #LED_NUM_LOCK
eor ledstate
sta ledstate
jmp _set_kbd_leds

is_reg_key:
; Transfer key code to Y
: tay
tay

; Pause/break key?
cmp #KEYCODE_PAUSEBRK
Expand Down Expand Up @@ -560,18 +578,26 @@ receive_scancode_resume:
rts

;*****************************************
; SET CAPS LOCK LED
; GET/SET CAPS/NUM/SCROLL LOCK LED
;*****************************************
set_caps_led:
kbd_leds:
KVARS_START
bcc @1
ldx ledstate
bra @2
@1: txa
and #(LED_NUM_LOCK + LED_CAPS_LOCK + LED_SCROLL_LOCK)
sta ledstate
jsr _set_kbd_leds
@2: KVARS_END
rts

_set_kbd_leds:
ldx #I2C_ADDRESS
ldy #I2C_KBD_CMD2
lda #$ed
jsr i2c_write_first_byte
lda shflag
and #MODIFIER_CAPS ; Caps Lock is bit 2
lsr
lsr
ora #LED_NUM_LOCK ; Num Lock always on
lda ledstate
jsr i2c_write_next_byte
jmp i2c_write_stop

Expand Down
2 changes: 2 additions & 0 deletions kernal/x16/extapi.s
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
.import led_update
.import mouse_set_position
.import scnsiz
.import kbd_leds

.export extapi

Expand Down Expand Up @@ -55,3 +56,4 @@ apitbl:
.word led_update-1 ; API 11
.word mouse_set_position-1 ; API 12
.word scnsiz-1 ; API 13
.word kbd_leds-1 ; API 14

0 comments on commit d2841e8

Please sign in to comment.