diff --git a/c64/,,config_generic.s b/c64/,,config_generic.s index 74605b8a..8965f1dd 100644 --- a/c64/,,config_generic.s +++ b/c64/,,config_generic.s @@ -52,10 +52,10 @@ // Keyboard settings // #define CONFIG_LEGACY_SCNKEY -#define CONFIG_KEYBOARD_C128 +#define CONFIG_KEYBOARD_C128 // incomplete #define CONFIG_KEYBOARD_C128_CAPS_LOCK // #define CONFIG_KEY_REPEAT_DEFAULT -#define CONFIG_KEY_REPEAT_ALWAYS +// #define CONFIG_KEY_REPEAT_ALWAYS #define CONFIG_KEY_FAST_SCAN #define CONFIG_JOY1_CURSOR #define CONFIG_JOY2_CURSOR diff --git a/c64/,,config_mega65.s b/c64/,,config_mega65.s index b4d2fa59..65169f8d 100644 --- a/c64/,,config_mega65.s +++ b/c64/,,config_mega65.s @@ -54,10 +54,10 @@ // Keyboard settings // #define CONFIG_LEGACY_SCNKEY -// #define CONFIG_KEYBOARD_C128 +// #define CONFIG_KEYBOARD_C128 // incomplete // #define CONFIG_KEYBOARD_C128_CAPS_LOCK // #define CONFIG_KEY_REPEAT_DEFAULT -#define CONFIG_KEY_REPEAT_ALWAYS +// #define CONFIG_KEY_REPEAT_ALWAYS #define CONFIG_KEY_FAST_SCAN #define CONFIG_JOY1_CURSOR #define CONFIG_JOY2_CURSOR diff --git a/c64/,,config_testing.s b/c64/,,config_testing.s index b6c84487..877a5005 100644 --- a/c64/,,config_testing.s +++ b/c64/,,config_testing.s @@ -53,7 +53,7 @@ // Keyboard settings #define CONFIG_LEGACY_SCNKEY -// #define CONFIG_KEYBOARD_C128 +// #define CONFIG_KEYBOARD_C128 // incomplete // #define CONFIG_KEYBOARD_C128_CAPS_LOCK // #define CONFIG_KEY_REPEAT_DEFAULT // #define CONFIG_KEY_REPEAT_ALWAYS diff --git a/c64/,,config_ultimate64.s b/c64/,,config_ultimate64.s index 4b99e84e..1eda8501 100644 --- a/c64/,,config_ultimate64.s +++ b/c64/,,config_ultimate64.s @@ -52,7 +52,7 @@ // Keyboard settings // #define CONFIG_LEGACY_SCNKEY -// #define CONFIG_KEYBOARD_C128 +// #define CONFIG_KEYBOARD_C128 // incomplete // #define CONFIG_KEYBOARD_C128_CAPS_LOCK // #define CONFIG_KEY_REPEAT_DEFAULT // #define CONFIG_KEY_REPEAT_ALWAYS diff --git a/c64/kernal/interrupts/udtim.s b/c64/kernal/interrupts/udtim.s index f9d4fa99..769ab0e4 100644 --- a/c64/kernal/interrupts/udtim.s +++ b/c64/kernal/interrupts/udtim.s @@ -3,7 +3,7 @@ // Official Kernal routine, described in: // // - [RG64] C64 Programmer's Reference Guide - page 303 -// - [CM64] Compute's Mapping the Commodore 64 - page 232 +// - [CM64] Compute's Mapping the Commodore 64 - pages 27, 232 // // CPU registers that has to be preserved (see [RG64]): .Y // @@ -23,18 +23,33 @@ UDTIM: udtim_update_stkey: // entry to be used when interrupts are disabled - // Another action we have to perform is to copy the last row of keyboard to RAM, - // so that various routines can detect the STOP key press + // According to [CM64], page 27, another action we have to perform is to copy + // the last row of keyboard to RAM, so that various routines can detect the STOP + // key press. But something as simple as this would have created warm restart + // with M+N+SPACE+RESTORE due to ghosting - so the original ROM has to provide + // some protection here. I do not know what the mechanism there exactly does, + // but I hope the mechanism below will be compatible enough. + lda #$80 + sta CIA1_PRA // select all the rows except the last one #if CONFIG_KEYBOARD_C128 - lda #$FF - sta VIC_XSCAN // disconnect the extra C128 keys + ldx #$00 + stx VIC_XSCAN // connect all the extra C128 keys #endif + lda CIA1_PRB // read the keys + eor #$FF + sta STKEY // store reversed state - to filter out anything vulnerable to ghosting + +#if CONFIG_KEYBOARD_C128 + dex // puts $FF + stx VIC_XSCAN // disconnect the extra C128 keys +#endif lda #$7F sta CIA1_PRA // select the last row (bit to 0) lda CIA1_PRB // read the row + ora STKEY // filter out what might be the reason of ghosting sta STKEY // Leave the CIA configured this way. It would be far better to disconnect diff --git a/c64/kernal/keyboard/scnkey.s b/c64/kernal/keyboard/scnkey.s index 821abfa1..b1ed3a5f 100644 --- a/c64/kernal/keyboard/scnkey.s +++ b/c64/kernal/keyboard/scnkey.s @@ -37,10 +37,11 @@ SCNKEY: lda #$00 sta SHFLAG - // Check for any activity - #if !CONFIG_JOY2_CURSOR && CONFIG_KEY_FAST_SCAN + // Check for any activity - speed optimization, but cannot be done + // that early if we are to scan the joystick #2 + ldx #$00 stx CIA1_PRA // connect all the rows #if CONFIG_KEYBOARD_C128 @@ -57,13 +58,10 @@ SCNKEY: #if CONFIG_KEYBOARD_C128_CAPS_LOCK // First check if this is really a C128 - to avoid false positive - lda #$00 - sta VIC_XSCAN - lda #$FF -#if !CONFIG_KEYBOARD_C128 - tax // preserve the $FF, will be needed later -#endif - cmp VIC_XSCAN + ldx #$00 + stx VIC_XSCAN + dex // puts $FF + cpx VIC_XSCAN beq !+ // branch if C64, C128 will keep some bits cleared lda CPU_R6510 @@ -90,7 +88,7 @@ scnkey_bucky_loop: #endif lda kb_matrix_bucky_testmask, y and CIA1_PRB - bne !+ // not pressed + bne !+ // branch if not pressed lda SHFLAG ora kb_matrix_bucky_shflag, y sta SHFLAG @@ -165,7 +163,7 @@ scnkey_matrix_loop: cmp #$FF beq scnkey_matrix_loop_next // skip if no key pressed from this row cpy #$FF - bne scnkey_no_keys // clash, more than one key pressed + bne scnkey_no_keys // jam, more than one key pressed // We have at least one key pressed in this row, we need to find which one exactly ldy #$07 scnkey_matrix_loop_inner: diff --git a/c64/kernal/keyboard/scnkey_128.s b/c64/kernal/keyboard/scnkey_128.s index 44f810d4..7fc8ddf5 100644 --- a/c64/kernal/keyboard/scnkey_128.s +++ b/c64/kernal/keyboard/scnkey_128.s @@ -28,7 +28,7 @@ scnkey_matrix_128_loop: cmp #$FF beq scnkey_matrix_128_loop_next // skip if no key pressed from this row cpy #$FF - bne scnkey_128_no_keys // clash, more than one key pressed + bne scnkey_128_no_keys // jam, more than one key pressed // We have at least one key pressed in this row, we need to find which one exactly ldy #$07 scnkey_matrix_128_loop_inner: