From 7918ef55b9c393711c66233dac00c2e0d3951bfb Mon Sep 17 00:00:00 2001 From: FeralChild64 Date: Sat, 28 Nov 2020 23:33:29 +0100 Subject: [PATCH] more work on MEGA65 keyboard scanner --- src/aliases/,aliases_hardware_m65.s | 1 + src/aliases/,aliases_ram_lowmem_m65.s | 5 +- src/kernal/keyboard/scnkey.s | 2 +- .../keyboard_m65/mega65_kb_matrix_bucky.s | 15 ++ src/kernal/keyboard_m65/mega65_scnkey.s | 169 ++++++++++-------- 5 files changed, 108 insertions(+), 84 deletions(-) create mode 100644 src/kernal/keyboard_m65/mega65_kb_matrix_bucky.s diff --git a/src/aliases/,aliases_hardware_m65.s b/src/aliases/,aliases_hardware_m65.s index 9ca89e57..c85b88f1 100644 --- a/src/aliases/,aliases_hardware_m65.s +++ b/src/aliases/,aliases_hardware_m65.s @@ -32,6 +32,7 @@ !addr C65_EXTKEYS_PR = $D607 !addr C65_EXTKEYS_DDR = $D608 + !addr KBSCN_KEYCODE = $D610 !addr KBSCN_BUCKY = $D611 !addr KBSCN_PEEK = $D613 !addr KBSCN_SELECT = $D614 diff --git a/src/aliases/,aliases_ram_lowmem_m65.s b/src/aliases/,aliases_ram_lowmem_m65.s index 8d208c7e..e7bc2ce1 100644 --- a/src/aliases/,aliases_ram_lowmem_m65.s +++ b/src/aliases/,aliases_ram_lowmem_m65.s @@ -53,10 +53,7 @@ !addr M65_TXTWIN_Y1 = $591 ; text window - bottom-right Y coordinate + 1 !addr M65_TXTROW_OFF = $592 ; $592-$593 offset to the current text row from the viewport start !addr M65_SCRCOLMAX = $594 ; maximum allowed column - !addr M65_KB_BUCKY = $595 ; result of bucky keys scan - !addr M65_KB_COLSCAN = $596 ; $596-$59E results of keyboard scan, columns 0-8 - - ; $59F-$5EB -- UNUSED -- + ; $595-$5EB -- UNUSED -- !addr M65_DMAGIC_LIST = $5F1 ; $5EF-$5FF reserved for DMAgic list, 15 bytes (modulo bytes are not needed) !addr M65_RS232_INBUF = $600 ; $600-$6FF -- reserved for RS-232 input buffer -- !addr M65_RS232_OUTBUF = $700 ; $700-$7FF -- reserved for RS-232 output buffer -- diff --git a/src/kernal/keyboard/scnkey.s b/src/kernal/keyboard/scnkey.s index 01c8ad26..74196ef0 100644 --- a/src/kernal/keyboard/scnkey.s +++ b/src/kernal/keyboard/scnkey.s @@ -317,7 +317,7 @@ scnkey_output_key: lda NDX cmp XMAX - bcs scnkey_early_repeat ; no space in buffer + bcs scnkey_early_repeat ; branch if no free space in buffer ; Reinitialize secondary counter diff --git a/src/kernal/keyboard_m65/mega65_kb_matrix_bucky.s b/src/kernal/keyboard_m65/mega65_kb_matrix_bucky.s new file mode 100644 index 00000000..b7ea685e --- /dev/null +++ b/src/kernal/keyboard_m65/mega65_kb_matrix_bucky.s @@ -0,0 +1,15 @@ +;; #LAYOUT# M65 KERNAL_0 #TAKE +;; #LAYOUT# * * #IGNORE + +; Keyboard matrix for bucky keys decoding on MEGA65 + + +m65_kb_matrix_bucky: + + !byte KEY_FLAG_SHIFT + !byte KEY_FLAG_SHIFT + !byte KEY_FLAG_VENDOR + !byte KEY_FLAG_CTRL + !byte KEY_FLAG_ALT + !byte KEY_FLAG_NO_SCRL + !byte KEY_FLAG_CAPSL diff --git a/src/kernal/keyboard_m65/mega65_scnkey.s b/src/kernal/keyboard_m65/mega65_scnkey.s index 4d15f97c..6e2d495c 100644 --- a/src/kernal/keyboard_m65/mega65_scnkey.s +++ b/src/kernal/keyboard_m65/mega65_scnkey.s @@ -4,109 +4,120 @@ m65_scnkey: - ; First scan the whole keyboard + ; Read the key from automatic scanner lda KBSCN_BUCKY - sta M65_KB_BUCKY + ldy KBSCN_KEYCODE + sty KBSCN_KEYCODE - ldx #$08 -@1: - stx KBSCN_SELECT - lda KBSCN_PEEK - eor #$FF ; let bit 1 mean 'pressed', not the otherwise - sta M65_KB_COLSCAN, x + ; Update the SHFLAG in a way that preserves .Y + + and #%01111111 + + ldx SHFLAG + stx LSTSHF ; needed for SHIFT+VENDOR support + + ldx #$00 + stx SHFLAG dex - bpl @1 - - ; Update the SHFLAG - - lda SHFLAG - sta LSTSHF ; needed for SHIFT+VENDOR support - - ldy #$00 ; the new SHFLAG will be constructed in .Y - ldx M65_KB_BUCKY - beq m65_scnkey_SHFLAG_ready ; FALLTROUGH -m65_scnkey_detect_SHIFT: +m65_scnkey_SHFLAG_loop: - txa - and #%00000011 - beq m65_scnkey_detect_VENDOR + inx + asr + bcc m65_scnkey_SHFLAG_next + + ; Bucky key pressed + + pha + lda m65_kb_matrix_bucky, x + ora SHFLAG + sta SHFLAG + pla + + ; FALLTROUGH + +m65_scnkey_SHFLAG_next: + + bne m65_scnkey_SHFLAG_loop + + ; We have the key code in .Y - check if same as the last one + + cpy LSTX + beq m65_scnkey_try_repeat + + ; Not the same key - store the key code, reset delay counter + + sty LSTX +!ifndef CONFIG_RS232_UP9600 { + lda #$16 +} else { + lda #$18 +} + sta DELAY + + ; FALLTROUGH + +m65_scnkey_output_key: + + ; Check if we have free space in the keyboard buffer + + lda NDX + cmp XMAX + +bcs scnkey_early_repeat ; branch if no free space in buffer + + ; Reinitialize secondary counter + + lda #$03 + sta KOUNT + + ; Output PETSCII code to the keyboard buffer + +m65_scnkey_got_petscii: + + ; XXX if no key pressed, try joysticks tya - ora #KEY_FLAG_SHIFT - tay + beq m65_scnkey_done ; branch if no key was pressed + ldy NDX + sta KEYD, y + inc NDX ; FALLTROUGH -m65_scnkey_detect_VENDOR: - - txa - and #%00001000 - beq m65_scnkey_detect_CTRL +m65_scnkey_done: - tya - ora #KEY_FLAG_VENDOR - tay + rts - ; FALLTROUGH +m65_scnkey_try_repeat: -m65_scnkey_detect_CTRL: - - txa - and #%00000100 - beq m65_scnkey_detect_ALT +!ifndef CONFIG_KEY_REPEAT_ALWAYS { - tya - ora #KEY_FLAG_CTRL - tay + ; Check whether we should repeat keys - first the flag, afterwards hardcoded list - ; FALLTROUGH + lda RPTFLG + bmi m65_scnkey_handle_repeat ; branch if we should repeat always -m65_scnkey_detect_ALT: - - txa - and #%00010000 - beq m65_scnkey_detect_CAPS_LOCK + ; XXX if key to be repeated - go to m65_scnkey_handle_repeat, otherwise to m65_scnkey_done - tya - ora #KEY_FLAG_ALT - tay +m65_scnkey_handle_repeat: - ; FALLTROUGH +} ; no CONFIG_KEY_REPEAT_ALWAYS -m65_scnkey_detect_CAPS_LOCK: - - txa - and #%01000000 - beq m65_scnkey_detect_NO_SCRL + ; Countdown before first repeat - tya - ora #KEY_FLAG_NO_SCRL - tay + lda DELAY + beq @10 + dec DELAY - ; FALLTROUGH + rts +@10: + ; Countdown before subsequent repeats -m65_scnkey_detect_NO_SCRL: - - txa - and #%00100000 - beq m65_scnkey_SHFLAG_ready - - tya - ora #KEY_FLAG_CAPSL - tay - - ; FALLTROUGH - -m65_scnkey_SHFLAG_ready: - - sty SHFLAG - - - ; XXX - + lda KOUNT + beq m65_scnkey_output_key ; if second counter is also 0, we can repeat the key + dec KOUNT rts