From b62b8639a660cfa2b426b5d22a9c41b656fd40d6 Mon Sep 17 00:00:00 2001 From: Dean Netherton Date: Thu, 24 Apr 2025 12:31:58 +1000 Subject: [PATCH] ch376-native: updated keyboard driver 1. fix handling of caps-lock 2. removed support for returning scancode/bitfields for hbios call --- Source/HBIOS/ch376-native/ez80-firmware.asm | 24 +- .../keyboard/class_hid_keyboard.c.s | 176 ++++++------- .../HBIOS/ch376-native/keyboard/kyb-init.c.s | 40 ++- .../ch376-native/keyboard/kyb_driver.c.s | 248 ++++++++---------- .../source-doc/keyboard/class_hid_keyboard.c | 14 +- .../source-doc/keyboard/class_hid_keyboard.h | 3 +- .../source-doc/keyboard/kyb_driver.c | 49 ++-- .../source-doc/keyboard/kyb_driver.h | 2 +- Source/HBIOS/ch376kyb.asm | 20 +- Source/HBIOS/ez80instr.inc | 7 +- 10 files changed, 277 insertions(+), 306 deletions(-) diff --git a/Source/HBIOS/ch376-native/ez80-firmware.asm b/Source/HBIOS/ch376-native/ez80-firmware.asm index 2553e9d1..2cce1a71 100644 --- a/Source/HBIOS/ch376-native/ez80-firmware.asm +++ b/Source/HBIOS/ch376-native/ez80-firmware.asm @@ -108,12 +108,30 @@ _usb_ufi_get_cap: EZ80_CPY_UHL_TO_EHL RET -_usb_kyb_flush: -_usb_kyb_report: -_usb_kyb_buf_get_next: +; extern void usb_kyb_init(const uint8_t dev_index) __sdcccall(1); _usb_kyb_init: + LD C, A + EZ80_EX_USB_KYB_INIT RET +; extern uint8_t usb_kyb_flush() __sdcccall(1); +_usb_kyb_flush: + EZ80_EX_USB_KYB_FLUSH + RET + +; extern uint8_t usb_kyb_status() __sdcccall(1); +_usb_kyb_status: + EZ80_EX_USB_KYB_STATUS + RET + +; extern uint16_t usb_kyb_read(); +; H = 0/1 set if char, L=>code +_usb_kyb_read: + EZ80_EX_USB_KYB_READ + LD H, A + RET + + ;usb_device_type usb_get_device_type(const uint16_t dev_index) _usb_get_device_type: LD IY, 0 diff --git a/Source/HBIOS/ch376-native/keyboard/class_hid_keyboard.c.s b/Source/HBIOS/ch376-native/keyboard/class_hid_keyboard.c.s index 0c16292f..4e690852 100644 --- a/Source/HBIOS/ch376-native/keyboard/class_hid_keyboard.c.s +++ b/Source/HBIOS/ch376-native/keyboard/class_hid_keyboard.c.s @@ -30,8 +30,6 @@ ; .area _INITIALIZED removed by z88dk -_caps_lock_engaged: - DEFS 1 _scancodes_shift_table: DEFS 128 _scancodes_table: @@ -51,153 +49,137 @@ _scancodes_table: ;-------------------------------------------------------- ; code ;-------------------------------------------------------- -;source-doc/keyboard/class_hid_keyboard.c:337: }; +;source-doc/keyboard/class_hid_keyboard.c:334: }; ; --------------------------------- ; Function char_with_caps_lock ; --------------------------------- _char_with_caps_lock: - ld c, a -;source-doc/keyboard/class_hid_keyboard.c:338: - ld hl,_caps_lock_engaged - bit 0, (hl) - jr NZ,l_char_with_caps_lock_00102 -;source-doc/keyboard/class_hid_keyboard.c:339: char char_with_caps_lock(const char c) __sdcccall(1) { - ld a, c - jr l_char_with_caps_lock_00109 -l_char_with_caps_lock_00102: -;source-doc/keyboard/class_hid_keyboard.c:341: return c; - ld a, c - sub 0x41 - jr C,l_char_with_caps_lock_00104 - ld a,0x5a - sub c +;source-doc/keyboard/class_hid_keyboard.c:335: + bit 0, l +;source-doc/keyboard/class_hid_keyboard.c:336: static char char_with_caps_lock(const char c, const bool caps_lock_engaged) __sdcccall(1) { + jr Z,l_char_with_caps_lock_00109 +;source-doc/keyboard/class_hid_keyboard.c:338: return c; + cp 0x41 jr C,l_char_with_caps_lock_00104 -;source-doc/keyboard/class_hid_keyboard.c:342: - ld a, c + cp 0x5b + jr NC,l_char_with_caps_lock_00104 +;source-doc/keyboard/class_hid_keyboard.c:339: add a,0x20 jr l_char_with_caps_lock_00109 l_char_with_caps_lock_00104: -;source-doc/keyboard/class_hid_keyboard.c:344: return c - 'A' + 'a'; - ld a, c - sub 0x61 - jr C,l_char_with_caps_lock_00107 - ld a,0x7a - sub c - jr C,l_char_with_caps_lock_00107 -;source-doc/keyboard/class_hid_keyboard.c:345: - ld a, c +;source-doc/keyboard/class_hid_keyboard.c:341: return c - 'A' + 'a'; + cp 0x61 + ret C + cp 0x7b + ret NC +;source-doc/keyboard/class_hid_keyboard.c:342: add a,0xe0 - jr l_char_with_caps_lock_00109 -l_char_with_caps_lock_00107: -;source-doc/keyboard/class_hid_keyboard.c:347: return c - 'a' + 'A'; - ld a, c +;source-doc/keyboard/class_hid_keyboard.c:344: return c - 'a' + 'A'; l_char_with_caps_lock_00109: -;source-doc/keyboard/class_hid_keyboard.c:348: +;source-doc/keyboard/class_hid_keyboard.c:345: ret -;source-doc/keyboard/class_hid_keyboard.c:350: } +;source-doc/keyboard/class_hid_keyboard.c:347: } ; --------------------------------- ; Function scancode_to_char ; --------------------------------- _scancode_to_char: - ld c, a -;source-doc/keyboard/class_hid_keyboard.c:351: - ld a,l + push ix + ld ix,0 + add ix,sp +;source-doc/keyboard/class_hid_keyboard.c:348: + ld c,a ld e,l - sub 0x80 - jr C,l_scancode_to_char_00102 -;source-doc/keyboard/class_hid_keyboard.c:352: char scancode_to_char(const uint8_t modifier_keys, const uint8_t code) __sdcccall(1) { - xor a - jr l_scancode_to_char_00123 -l_scancode_to_char_00102: -;source-doc/keyboard/class_hid_keyboard.c:354: return 0; - ld a, c and 0x11 - jr Z,l_scancode_to_char_00120 -;source-doc/keyboard/class_hid_keyboard.c:355: + jr Z,l_scancode_to_char_00118 +;source-doc/keyboard/class_hid_keyboard.c:349: char scancode_to_char(const uint8_t modifier_keys, const uint8_t code, const bool caps_lock_engaged) __sdcccall(1) { ld a, e sub 0x04 - jr C,l_scancode_to_char_00104 + jr C,l_scancode_to_char_00102 ld a,0x1d sub e - jr C,l_scancode_to_char_00104 -;source-doc/keyboard/class_hid_keyboard.c:356: if ((modifier_keys & (KEY_MOD_LCTRL | KEY_MOD_RCTRL))) { + jr C,l_scancode_to_char_00102 +;source-doc/keyboard/class_hid_keyboard.c:350: if ((modifier_keys & (KEY_MOD_LCTRL | KEY_MOD_RCTRL))) { ld a, e add a,0xfd - jr l_scancode_to_char_00123 -l_scancode_to_char_00104: -;source-doc/keyboard/class_hid_keyboard.c:358: return code - 3; + jr l_scancode_to_char_00121 +l_scancode_to_char_00102: +;source-doc/keyboard/class_hid_keyboard.c:352: return code - 3; ld a,e cp 0x1f - jr Z,l_scancode_to_char_00106 + jr Z,l_scancode_to_char_00104 sub 0x2c - jr NZ,l_scancode_to_char_00107 -l_scancode_to_char_00106: -;source-doc/keyboard/class_hid_keyboard.c:359: + jr NZ,l_scancode_to_char_00105 +l_scancode_to_char_00104: +;source-doc/keyboard/class_hid_keyboard.c:353: xor a - jr l_scancode_to_char_00123 -l_scancode_to_char_00107: -;source-doc/keyboard/class_hid_keyboard.c:361: return 0; + jr l_scancode_to_char_00121 +l_scancode_to_char_00105: +;source-doc/keyboard/class_hid_keyboard.c:355: return 0; ld a, e sub 0x2f - jr NZ,l_scancode_to_char_00110 -;source-doc/keyboard/class_hid_keyboard.c:362: + jr NZ,l_scancode_to_char_00108 +;source-doc/keyboard/class_hid_keyboard.c:356: ld a,0x1b - jr l_scancode_to_char_00123 -l_scancode_to_char_00110: -;source-doc/keyboard/class_hid_keyboard.c:364: return 27; + jr l_scancode_to_char_00121 +l_scancode_to_char_00108: +;source-doc/keyboard/class_hid_keyboard.c:358: return 27; ld a, e sub 0x31 - jr NZ,l_scancode_to_char_00112 -;source-doc/keyboard/class_hid_keyboard.c:365: + jr NZ,l_scancode_to_char_00110 +;source-doc/keyboard/class_hid_keyboard.c:359: ld a,0x1c - jr l_scancode_to_char_00123 -l_scancode_to_char_00112: -;source-doc/keyboard/class_hid_keyboard.c:367: return 28; + jr l_scancode_to_char_00121 +l_scancode_to_char_00110: +;source-doc/keyboard/class_hid_keyboard.c:361: return 28; ld a, e sub 0x30 - jr NZ,l_scancode_to_char_00114 -;source-doc/keyboard/class_hid_keyboard.c:368: + jr NZ,l_scancode_to_char_00112 +;source-doc/keyboard/class_hid_keyboard.c:362: ld a,0x1d - jr l_scancode_to_char_00123 -l_scancode_to_char_00114: -;source-doc/keyboard/class_hid_keyboard.c:370: return 29; + jr l_scancode_to_char_00121 +l_scancode_to_char_00112: +;source-doc/keyboard/class_hid_keyboard.c:364: return 29; ld a, e sub 0x23 - jr NZ,l_scancode_to_char_00116 -;source-doc/keyboard/class_hid_keyboard.c:371: + jr NZ,l_scancode_to_char_00114 +;source-doc/keyboard/class_hid_keyboard.c:365: ld a,0x1e - jr l_scancode_to_char_00123 -l_scancode_to_char_00116: -;source-doc/keyboard/class_hid_keyboard.c:373: return 30; + jr l_scancode_to_char_00121 +l_scancode_to_char_00114: +;source-doc/keyboard/class_hid_keyboard.c:367: return 30; ld a, e sub 0x2d - jr NZ,l_scancode_to_char_00120 -;source-doc/keyboard/class_hid_keyboard.c:374: + jr NZ,l_scancode_to_char_00118 +;source-doc/keyboard/class_hid_keyboard.c:368: ld a,0x1f - jr l_scancode_to_char_00123 -l_scancode_to_char_00120: -;source-doc/keyboard/class_hid_keyboard.c:377: } + jr l_scancode_to_char_00121 +l_scancode_to_char_00118: +;source-doc/keyboard/class_hid_keyboard.c:371: } ld a, c and 0x22 - jr Z,l_scancode_to_char_00122 -;source-doc/keyboard/class_hid_keyboard.c:378: + jr Z,l_scancode_to_char_00120 +;source-doc/keyboard/class_hid_keyboard.c:372: ld d,0x00 ld hl,_scancodes_shift_table add hl, de ld a, (hl) - jp _char_with_caps_lock -l_scancode_to_char_00122: -;source-doc/keyboard/class_hid_keyboard.c:380: return char_with_caps_lock(scancodes_shift_table[code]); + ld l,(ix+4) + call _char_with_caps_lock + jr l_scancode_to_char_00121 +l_scancode_to_char_00120: +;source-doc/keyboard/class_hid_keyboard.c:374: return char_with_caps_lock(scancodes_shift_table[code], caps_lock_engaged); ld d,0x00 ld hl,_scancodes_table add hl, de ld a, (hl) - jp _char_with_caps_lock -l_scancode_to_char_00123: -;source-doc/keyboard/class_hid_keyboard.c:381: - ret -_caps_lock_engaged: - DEFB +0x01 + ld l,(ix+4) + call _char_with_caps_lock +l_scancode_to_char_00121: +;source-doc/keyboard/class_hid_keyboard.c:375: + pop ix + pop hl + inc sp + jp (hl) _scancodes_shift_table: DEFB +0x00 DEFB +0x00 diff --git a/Source/HBIOS/ch376-native/keyboard/kyb-init.c.s b/Source/HBIOS/ch376-native/keyboard/kyb-init.c.s index df53d3e5..af030411 100644 --- a/Source/HBIOS/ch376-native/keyboard/kyb-init.c.s +++ b/Source/HBIOS/ch376-native/keyboard/kyb-init.c.s @@ -48,7 +48,7 @@ _USB_MODULE_LEDS .EQU 0xff8a ;-------------------------------------------------------- ; code ;-------------------------------------------------------- -;source-doc/keyboard/kyb-init.c:6: void keyboard_init(void) __sdcccall(1) { +;source-doc/keyboard/kyb-init.c:6: uint8_t keyboard_init(void) __sdcccall(1) { ; --------------------------------- ; Function keyboard_init ; --------------------------------- @@ -57,27 +57,32 @@ _keyboard_init: ld ix,0 add ix,sp dec sp +;source-doc/keyboard/kyb-init.c:7: uint8_t index = 1; ;source-doc/keyboard/kyb-init.c:9: do { - ld (ix-1),0x01 + ld c,0x01 + ld (ix-1),c l_keyboard_init_00103: ;source-doc/keyboard/kyb-init.c:10: usb_device_type t = usb_get_device_type(index); - ld l,(ix-1) - ld h,0x00 - push hl - push hl + ld e, c + ld d,0x00 + push bc + push de + push de call _usb_get_device_type pop af ld a, l - pop hl + pop de + pop bc ;source-doc/keyboard/kyb-init.c:12: if (t == USB_IS_KEYBOARD) { sub 0x04 jr NZ,l_keyboard_init_00104 ;source-doc/keyboard/kyb-init.c:13: print_string("\r\nUSB: KEYBOARD @ $"); - push hl + push de ld hl,kyb_init_str_0 call _print_string - pop hl + pop de ;source-doc/keyboard/kyb-init.c:14: print_uint16(index); + ex de, hl call _print_uint16 ;source-doc/keyboard/kyb-init.c:15: print_string(" $"); ld hl,kyb_init_str_1 @@ -85,16 +90,23 @@ l_keyboard_init_00103: ;source-doc/keyboard/kyb-init.c:17: usb_kyb_init(index); ld a,(ix-1) call _usb_kyb_init +;source-doc/keyboard/kyb-init.c:18: return 1; + ld a,0x01 + jr l_keyboard_init_00106 l_keyboard_init_00104: -;source-doc/keyboard/kyb-init.c:19: } while (++index != MAX_NUMBER_OF_DEVICES + 1); - inc (ix-1) - ld a,(ix-1) +;source-doc/keyboard/kyb-init.c:20: } while (++index != MAX_NUMBER_OF_DEVICES + 1); + inc c + ld (ix-1),c + ld a, c sub 0x07 jr NZ,l_keyboard_init_00103 -;source-doc/keyboard/kyb-init.c:21: print_string("\r\nUSB: KEYBOARD: NOT FOUND$"); +;source-doc/keyboard/kyb-init.c:22: print_string("\r\nUSB: KEYBOARD: NOT FOUND$"); ld hl,kyb_init_str_2 call _print_string -;source-doc/keyboard/kyb-init.c:22: } +;source-doc/keyboard/kyb-init.c:24: return 0; + xor a +l_keyboard_init_00106: +;source-doc/keyboard/kyb-init.c:25: } inc sp pop ix ret diff --git a/Source/HBIOS/ch376-native/keyboard/kyb_driver.c.s b/Source/HBIOS/ch376-native/keyboard/kyb_driver.c.s index 170f046e..964dbaeb 100644 --- a/Source/HBIOS/ch376-native/keyboard/kyb_driver.c.s +++ b/Source/HBIOS/ch376-native/keyboard/kyb_driver.c.s @@ -33,10 +33,12 @@ _USB_MODULE_LEDS .EQU 0xff8a ; .area _INITIALIZED removed by z88dk +_caps_lock_engaged: + DEFS 1 _keyboard_config: DEFS 2 _buffer: - DEFS 16 + DEFS 8 _write_index: DEFS 1 _read_index: @@ -102,197 +104,180 @@ _keyboard_buf_put: add hl, bc ;source-doc/keyboard/kyb_driver.c:38: static void keyboard_buf_put(const uint8_t indx) __sdcccall(1) { ld a,(hl) - ld c,a - cp 0x80 - jr NC,l_keyboard_buf_put_00111 + ld e,a + sub 0x80 + jr NC,l_keyboard_buf_put_00112 + ld a, e or a ;source-doc/keyboard/kyb_driver.c:39: const uint8_t key_code = report.keyCode[indx]; - jr Z,l_keyboard_buf_put_00111 -;source-doc/keyboard/kyb_driver.c:43: // if already reported, just skip it -;source-doc/keyboard/kyb_driver.c:44: uint8_t i = 6; - ld b,0x06 + jr Z,l_keyboard_buf_put_00112 +;source-doc/keyboard/kyb_driver.c:42: + ld b,0x00 ld hl,+(_previous + 2) -l_keyboard_buf_put_00106: -;source-doc/keyboard/kyb_driver.c:45: uint8_t *a = previous.keyCode; + add hl, bc ld a, (hl) - inc hl - sub c -;source-doc/keyboard/kyb_driver.c:46: do { + sub e +;source-doc/keyboard/kyb_driver.c:43: // if already reported, just skip it + jr Z,l_keyboard_buf_put_00112 +;source-doc/keyboard/kyb_driver.c:45: return; + ld a, e + sub 0x39 + jr NZ,l_keyboard_buf_put_00107 +;source-doc/keyboard/kyb_driver.c:46: + ld hl,_caps_lock_engaged + ld a, (hl) + xor 0x01 + ld (hl), a +;source-doc/keyboard/kyb_driver.c:47: if (key_code == KEY_CODE_CAPS_LOCK) { + jr l_keyboard_buf_put_00112 +l_keyboard_buf_put_00107: +;source-doc/keyboard/kyb_driver.c:50: } + ld a,(_report) + ld hl,_caps_lock_engaged + ld h, (hl) + push hl + inc sp + ld l, e + call _scancode_to_char + ld b, a +;source-doc/keyboard/kyb_driver.c:52: const unsigned char c = scancode_to_char(report.bModifierKeys, key_code, caps_lock_engaged); + or a +;source-doc/keyboard/kyb_driver.c:53: ret Z -;source-doc/keyboard/kyb_driver.c:47: if (*a++ == key_code) - djnz l_keyboard_buf_put_00106 -;source-doc/keyboard/kyb_driver.c:49: } while (--i != 0); +;source-doc/keyboard/kyb_driver.c:55: return; ld a, (_write_index) inc a and 0x07 - ld b, a -;source-doc/keyboard/kyb_driver.c:50: - ld a,(_read_index) - sub b + ld c, a +;source-doc/keyboard/kyb_driver.c:56: + ld hl,_read_index + ld a, (hl) + sub c ret Z -;source-doc/keyboard/kyb_driver.c:51: uint8_t next_write_index = (write_index + 1) & KEYBOARD_BUFFER_SIZE_MASK; - ld de,_buffer+0 - ld hl, (_write_index) +;source-doc/keyboard/kyb_driver.c:57: uint8_t next_write_index = (write_index + 1) & KEYBOARD_BUFFER_SIZE_MASK; + ld hl,(_write_index) ld h,0x00 - add hl, hl - add hl, de - ex de, hl - ld hl,(_report) - xor a - xor a - ld a, c - ld (de), a - inc de - ld a, l - ld (de), a -;source-doc/keyboard/kyb_driver.c:52: if (next_write_index != read_index) { // Check if buffer is not full + ld de,_buffer + add hl,de + ld a,b + ld (hl),a ld hl,_write_index - ld (hl), b -l_keyboard_buf_put_00111: -;source-doc/keyboard/kyb_driver.c:54: write_index = next_write_index; +;source-doc/keyboard/kyb_driver.c:58: if (next_write_index != read_index) { // Check if buffer is not full + ld (hl), c +l_keyboard_buf_put_00112: +;source-doc/keyboard/kyb_driver.c:60: write_index = next_write_index; ret -;source-doc/keyboard/kyb_driver.c:56: } +;source-doc/keyboard/kyb_driver.c:62: } ; --------------------------------- ; Function usb_kyb_status ; --------------------------------- _usb_kyb_status: -;source-doc/keyboard/kyb_driver.c:57: +;source-doc/keyboard/kyb_driver.c:63: DI -;source-doc/keyboard/kyb_driver.c:61: uint8_t size; +;source-doc/keyboard/kyb_driver.c:67: uint8_t size; ld a,(_write_index) ld hl,_read_index sub (hl) jr C,l_usb_kyb_status_00102 -;source-doc/keyboard/kyb_driver.c:62: +;source-doc/keyboard/kyb_driver.c:68: ld a,(_write_index) ld hl,_read_index sub (hl) jr l_usb_kyb_status_00103 l_usb_kyb_status_00102: -;source-doc/keyboard/kyb_driver.c:64: size = write_index - read_index; +;source-doc/keyboard/kyb_driver.c:70: size = write_index - read_index; ld hl, (_read_index) ld a,0x08 sub l ld hl, (_write_index) add a, l l_usb_kyb_status_00103: -;source-doc/keyboard/kyb_driver.c:66: size = KEYBOARD_BUFFER_SIZE - read_index + write_index; +;source-doc/keyboard/kyb_driver.c:72: size = KEYBOARD_BUFFER_SIZE - read_index + write_index; EI -;source-doc/keyboard/kyb_driver.c:67: -;source-doc/keyboard/kyb_driver.c:68: EI; +;source-doc/keyboard/kyb_driver.c:73: +;source-doc/keyboard/kyb_driver.c:74: EI; ret -;source-doc/keyboard/kyb_driver.c:70: } +;source-doc/keyboard/kyb_driver.c:76: } ; --------------------------------- ; Function usb_kyb_read ; --------------------------------- _usb_kyb_read: - push ix - ld ix,0 - add ix,sp - push af - push af -;source-doc/keyboard/kyb_driver.c:71: +;source-doc/keyboard/kyb_driver.c:77: ld a,(_write_index) ld hl,_read_index sub (hl) jr NZ,l_usb_kyb_read_00102 -;source-doc/keyboard/kyb_driver.c:72: uint32_t usb_kyb_read() { +;source-doc/keyboard/kyb_driver.c:78: uint16_t usb_kyb_read() { ld hl,0xff00 - ld e, l - ld d, l jr l_usb_kyb_read_00103 l_usb_kyb_read_00102: -;source-doc/keyboard/kyb_driver.c:74: return 0x0000FF00; // H = -1, D, E, L = 0 +;source-doc/keyboard/kyb_driver.c:80: return 0xFF00; // H = -1, L = 0 DI -;source-doc/keyboard/kyb_driver.c:75: - ld bc,_buffer+0 - ld hl, (_read_index) +;source-doc/keyboard/kyb_driver.c:81: + ld hl,(_read_index) ld h,0x00 - add hl, hl - add hl, bc - ld c, (hl) - inc hl - ld b, (hl) -;source-doc/keyboard/kyb_driver.c:76: DI; -;source-doc/keyboard/kyb_driver.c:77: const uint8_t modifier_key = buffer[read_index] >> 8; + ld bc,_buffer + add hl,bc + ld a,(hl) + ld c,l + ld b,h + ld hl,_read_index + ld l, a +;source-doc/keyboard/kyb_driver.c:82: DI; ld a, (_read_index) inc a and 0x07 - ld hl,_read_index - ld (hl), a -;source-doc/keyboard/kyb_driver.c:78: const uint8_t key_code = buffer[read_index] & 255; + ld (_read_index), a +;source-doc/keyboard/kyb_driver.c:83: const uint8_t c = buffer[read_index]; EI -;source-doc/keyboard/kyb_driver.c:84: // L: KeyCode aka scan code - push bc - ld l, c - ld a, b - call _scancode_to_char - ld e, a - pop bc -;source-doc/keyboard/kyb_driver.c:87: /* D = modifier, e-> char, H = 0, L=>code */ - xor a - ld (ix-1),b - xor a - ld (ix-4),a - ld (ix-3),a - ld (ix-2),a - xor a - ld d,(ix-1) - ld (ix-4),c - xor a - ld (ix-3),a - ld (ix-2),a - ld (ix-1),a - pop hl - push hl +;source-doc/keyboard/kyb_driver.c:86: + ld h,0x00 l_usb_kyb_read_00103: -;source-doc/keyboard/kyb_driver.c:88: - ld sp, ix - pop ix +;source-doc/keyboard/kyb_driver.c:87: /* H = 0, L = ascii char */ ret -;source-doc/keyboard/kyb_driver.c:90: } +;source-doc/keyboard/kyb_driver.c:89: } ; --------------------------------- ; Function usb_kyb_flush ; --------------------------------- _usb_kyb_flush: -;source-doc/keyboard/kyb_driver.c:91: +;source-doc/keyboard/kyb_driver.c:90: DI -;source-doc/keyboard/kyb_driver.c:92: uint8_t usb_kyb_flush() __sdcccall(1) { +;source-doc/keyboard/kyb_driver.c:91: uint8_t usb_kyb_flush() __sdcccall(1) { xor a ld (_read_index),a ld (_write_index),a -;source-doc/keyboard/kyb_driver.c:95: +;source-doc/keyboard/kyb_driver.c:94: ld de,_previous+0 -;source-doc/keyboard/kyb_driver.c:96: uint8_t i = sizeof(previous); -;source-doc/keyboard/kyb_driver.c:97: uint8_t *a = (uint8_t *)previous; +;source-doc/keyboard/kyb_driver.c:95: uint8_t i = sizeof(previous); +;source-doc/keyboard/kyb_driver.c:96: uint8_t *a = (uint8_t *)previous; ld b,0x08 ld hl,_report l_usb_kyb_flush_00101: -;source-doc/keyboard/kyb_driver.c:98: uint8_t *b = (uint8_t *)report; +;source-doc/keyboard/kyb_driver.c:97: uint8_t *b = (uint8_t *)report; xor a ld (de), a inc de -;source-doc/keyboard/kyb_driver.c:99: do { +;source-doc/keyboard/kyb_driver.c:98: do { ld (hl),0x00 inc hl -;source-doc/keyboard/kyb_driver.c:100: *a++ = 0; +;source-doc/keyboard/kyb_driver.c:99: *a++ = 0; djnz l_usb_kyb_flush_00101 -;source-doc/keyboard/kyb_driver.c:102: } while (--i != 0); +;source-doc/keyboard/kyb_driver.c:101: } while (--i != 0); EI -;source-doc/keyboard/kyb_driver.c:104: EI; +;source-doc/keyboard/kyb_driver.c:103: EI; xor a -;source-doc/keyboard/kyb_driver.c:105: +;source-doc/keyboard/kyb_driver.c:104: ret -;source-doc/keyboard/kyb_driver.c:107: } +;source-doc/keyboard/kyb_driver.c:106: } ; --------------------------------- ; Function usb_kyb_tick ; --------------------------------- _usb_kyb_tick: -;source-doc/keyboard/kyb_driver.c:108: +;source-doc/keyboard/kyb_driver.c:107: ld hl,_in_critical_usb_section ld a, (hl) or a -;source-doc/keyboard/kyb_driver.c:109: void usb_kyb_tick(void) { +;source-doc/keyboard/kyb_driver.c:108: void usb_kyb_tick(void) { jr NZ,l_usb_kyb_tick_00112 ;././source-doc/base-drv//ch376.h:111: ld l,0x0b @@ -305,7 +290,7 @@ _usb_kyb_tick: ld a,0x1f ld bc,_CH376_DATA_PORT out (c), a -;source-doc/keyboard/kyb_driver.c:112: +;source-doc/keyboard/kyb_driver.c:111: ld bc,_report+0 ld hl, (_keyboard_config) ld a,0x08 @@ -319,7 +304,6 @@ _usb_kyb_tick: inc sp ld a, l ld (_result), a -;././source-doc/base-drv//ch376.h:111: ld l,0x0b call _ch_command ;././source-doc/base-drv//ch376.h:112: #endif @@ -330,75 +314,69 @@ _usb_kyb_tick: ld a,0xdf ld bc,_CH376_DATA_PORT out (c), a -;source-doc/keyboard/kyb_driver.c:114: result = usbdev_dat_in_trnsfer_0((device_config *)keyboard_config, (uint8_t *)&report, 8); ld hl,_result ld a, (hl) or a jr NZ,l_usb_kyb_tick_00112 -;source-doc/keyboard/kyb_driver.c:115: ch_configure_nak_retry_3s(); +;source-doc/keyboard/kyb_driver.c:114: ch_configure_nak_retry_3s(); call _report_diff or a jr Z,l_usb_kyb_tick_00112 -;source-doc/keyboard/kyb_driver.c:117: if (report_diff()) { +;source-doc/keyboard/kyb_driver.c:116: if (report_diff()) { ld b,0x06 l_usb_kyb_tick_00103: -;source-doc/keyboard/kyb_driver.c:118: uint8_t i = 6; +;source-doc/keyboard/kyb_driver.c:117: uint8_t i = 6; ld a, b dec a push bc call _keyboard_buf_put pop bc -;source-doc/keyboard/kyb_driver.c:119: do { +;source-doc/keyboard/kyb_driver.c:118: do { djnz l_usb_kyb_tick_00103 -;source-doc/keyboard/kyb_driver.c:120: keyboard_buf_put(i - 1); +;source-doc/keyboard/kyb_driver.c:119: keyboard_buf_put(i - 1); ld de,_previous ld bc,0x0008 ld hl,_report ldir l_usb_kyb_tick_00112: -;source-doc/keyboard/kyb_driver.c:123: } +;source-doc/keyboard/kyb_driver.c:122: } ret -;source-doc/keyboard/kyb_driver.c:125: } +;source-doc/keyboard/kyb_driver.c:124: } ; --------------------------------- ; Function usb_kyb_init ; --------------------------------- _usb_kyb_init: -;source-doc/keyboard/kyb_driver.c:126: +;source-doc/keyboard/kyb_driver.c:125: call _get_usb_device_config ex de, hl ld (_keyboard_config), hl -;source-doc/keyboard/kyb_driver.c:128: keyboard_config = (device_config_keyboard *)get_usb_device_config(dev_index); +;source-doc/keyboard/kyb_driver.c:127: keyboard_config = (device_config_keyboard *)get_usb_device_config(dev_index); ld hl,_keyboard_config + 1 ld a, (hl) dec hl or (hl) -;source-doc/keyboard/kyb_driver.c:129: +;source-doc/keyboard/kyb_driver.c:128: ret Z -;source-doc/keyboard/kyb_driver.c:131: return; +;source-doc/keyboard/kyb_driver.c:130: return; ld a,0x01 push af inc sp ld hl, (_keyboard_config) call _hid_set_protocol -;source-doc/keyboard/kyb_driver.c:132: +;source-doc/keyboard/kyb_driver.c:131: ld a,0x80 push af inc sp ld hl, (_keyboard_config) call _hid_set_idle -;source-doc/keyboard/kyb_driver.c:133: hid_set_protocol(keyboard_config, 1); +;source-doc/keyboard/kyb_driver.c:132: hid_set_protocol(keyboard_config, 1); ret +_caps_lock_engaged: + DEFB +0x01 _keyboard_config: DEFW +0x0000 _buffer: - DEFW +0x0000 - DEFB 0x00 - DEFB 0x00 - DEFB 0x00 - DEFB 0x00 - DEFB 0x00 - DEFB 0x00 - DEFB 0x00 + DEFB +0x00 DEFB 0x00 DEFB 0x00 DEFB 0x00 diff --git a/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.c b/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.c index 94c1d2c3..a047f36a 100644 --- a/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.c +++ b/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.c @@ -1,9 +1,6 @@ #include "class_hid_keyboard.h" #define ESC 0x1B - -bool caps_lock_engaged = true; - /** * scan codes sourced from https://deskthority.net/wiki/Scancode * @@ -336,7 +333,7 @@ char scancodes_table[128] = { 0x00 /* MUTE */, }; -char char_with_caps_lock(const char c) __sdcccall(1) { +static char char_with_caps_lock(const char c, const bool caps_lock_engaged) __sdcccall(1) { if (!caps_lock_engaged) return c; @@ -349,10 +346,7 @@ char char_with_caps_lock(const char c) __sdcccall(1) { return c; } -char scancode_to_char(const uint8_t modifier_keys, const uint8_t code) __sdcccall(1) { - if (code >= 0x80) - return 0; - +char scancode_to_char(const uint8_t modifier_keys, const uint8_t code, const bool caps_lock_engaged) __sdcccall(1) { if ((modifier_keys & (KEY_MOD_LCTRL | KEY_MOD_RCTRL))) { if (code >= 4 && code <= 0x1d) return code - 3; @@ -377,7 +371,7 @@ char scancode_to_char(const uint8_t modifier_keys, const uint8_t code) __sdcccal } if (modifier_keys & (KEY_MOD_LSHIFT | KEY_MOD_RSHIFT)) - return char_with_caps_lock(scancodes_shift_table[code]); + return char_with_caps_lock(scancodes_shift_table[code], caps_lock_engaged); - return char_with_caps_lock(scancodes_table[code]); + return char_with_caps_lock(scancodes_table[code], caps_lock_engaged); } diff --git a/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.h b/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.h index 1edf527f..6243c437 100644 --- a/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.h +++ b/Source/HBIOS/ch376-native/source-doc/keyboard/class_hid_keyboard.h @@ -1,6 +1,7 @@ #ifndef __CLASS_HID_KEYBOARD_H__ #define __CLASS_HID_KEYBOARD_H__ +#include #include typedef struct { @@ -21,6 +22,6 @@ typedef struct { #define KEY_CODE_CAPS_LOCK 0x39 extern char scancodes_table[128]; -extern char scancode_to_char(const uint8_t modifier_keys, const uint8_t code) __sdcccall(1); +extern char scancode_to_char(const uint8_t modifier_keys, const uint8_t code, const bool caps_lock_engaged) __sdcccall(1); #endif diff --git a/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.c b/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.c index 68a84ae1..217d151d 100644 --- a/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.c +++ b/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.c @@ -8,13 +8,13 @@ #define KEYBOARD_BUFFER_SIZE 8 #define KEYBOARD_BUFFER_SIZE_MASK 7 -typedef uint16_t modifier_and_code_t; -static device_config_keyboard *keyboard_config = 0; +static bool caps_lock_engaged = true; +static device_config_keyboard *keyboard_config = 0; -static modifier_and_code_t buffer[KEYBOARD_BUFFER_SIZE] = {0}; -static uint8_t write_index = 0; -static uint8_t read_index = 0; +static uint8_t buffer[KEYBOARD_BUFFER_SIZE] = {0}; +static uint8_t write_index = 0; +static uint8_t read_index = 0; static keyboard_report_t report = {0}; static keyboard_report_t previous = {0}; @@ -41,16 +41,22 @@ static void keyboard_buf_put(const uint8_t indx) __sdcccall(1) { return; // ignore ??? // if already reported, just skip it - uint8_t i = 6; - uint8_t *a = previous.keyCode; - do { - if (*a++ == key_code) - return; - } while (--i != 0); + if (previous.keyCode[indx] == key_code) + return; + + if (key_code == KEY_CODE_CAPS_LOCK) { + caps_lock_engaged = !caps_lock_engaged; + return; + } + + const unsigned char c = scancode_to_char(report.bModifierKeys, key_code, caps_lock_engaged); + + if (c == 0) + return; uint8_t next_write_index = (write_index + 1) & KEYBOARD_BUFFER_SIZE_MASK; if (next_write_index != read_index) { // Check if buffer is not full - buffer[write_index] = (uint16_t)report.bModifierKeys << 8 | (uint16_t)key_code; + buffer[write_index] = c; write_index = next_write_index; } } @@ -69,24 +75,17 @@ uint8_t usb_kyb_status() __sdcccall(1) { return size; } -uint32_t usb_kyb_read() { +uint16_t usb_kyb_read() { if (write_index == read_index) // Check if buffer is empty - return 0x0000FF00; // H = -1, D, E, L = 0 + return 0xFF00; // H = -1, L = 0 DI; - const uint8_t modifier_key = buffer[read_index] >> 8; - const uint8_t key_code = buffer[read_index] & 255; - read_index = (read_index + 1) & KEYBOARD_BUFFER_SIZE_MASK; + const uint8_t c = buffer[read_index]; + read_index = (read_index + 1) & KEYBOARD_BUFFER_SIZE_MASK; EI; - // D: Modifier keys - aka Keystate - // E: ASCII Code - // H: 0 - // L: KeyCode aka scan code - - const unsigned char c = scancode_to_char(modifier_key, key_code); - /* D = modifier, e-> char, H = 0, L=>code */ - return (uint32_t)modifier_key << 24 | (uint32_t)c << 16 | key_code; + /* H = 0, L = ascii char */ + return c; } uint8_t usb_kyb_flush() __sdcccall(1) { diff --git a/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.h b/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.h index b4d10c14..c9a744f7 100644 --- a/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.h +++ b/Source/HBIOS/ch376-native/source-doc/keyboard/kyb_driver.h @@ -7,6 +7,6 @@ extern void usb_kyb_init(const uint8_t dev_index) __sdcccall(1); extern uint8_t usb_kyb_flush() __sdcccall(1); extern uint8_t usb_kyb_status() __sdcccall(1); -extern uint32_t usb_kyb_read(); +extern uint16_t usb_kyb_read(); #endif diff --git a/Source/HBIOS/ch376kyb.asm b/Source/HBIOS/ch376kyb.asm index 7d46cd77..ff76443b 100644 --- a/Source/HBIOS/ch376kyb.asm +++ b/Source/HBIOS/ch376kyb.asm @@ -123,30 +123,12 @@ UKY_FLUSH .EQU _usb_kyb_flush ; ; Outputs: ; A: Status -; C: Scancode -; D: Keystate ; E: Keycode ; ; Read the next key data from the keyboard. If a buffer is used, return the next key code in the buffer. ; If no key data is available, this function will wait indefinitely for a key press. The Status (A) is a ; standard HBIOS result code. ; -; The Scancode (C) value is the raw scan code from the keyboard for the key press. Scan codes are standard -; usb scan codes -; -; The Keystate (D) is a bitmap representing the value of all modifier keys and shift states as they -; existed at the time of the keystroke. The bitmap is defined as: -; -; Bit Key state Indication -; 7 Key pressed was from the num pad -; 6 Caps Lock was active -; 5 Num Lock was active -; 4 Scroll Lock was active -; 3 Windows key was held down -; 2 Alt key was held down -; 1 Control key was held down -; 0 Shift key was held down -; ; The Keycode (E) is generally returned as appropriate ASCII values, if possible. Special keys, like ; function keys and arrows, are returned as reserved codes. ; @@ -155,6 +137,6 @@ UKY_READ: LD A, H OR A JR NZ, UKY_READ - LD C, L + LD E, L XOR A RET diff --git a/Source/HBIOS/ez80instr.inc b/Source/HBIOS/ez80instr.inc index 873b6b27..de7e168a 100644 --- a/Source/HBIOS/ez80instr.inc +++ b/Source/HBIOS/ez80instr.inc @@ -60,7 +60,12 @@ #DEFINE EZ80_EX_USB_UFI_WRITE LD A, 6 \ LD B, 17 \ EZ80_FN #DEFINE EZ80_EX_USB_UFI_GET_CAP LD A, 6 \ LD B, 18 \ EZ80_FN ; #DEFINE EZ80_EX_USB_UFI_INIT LD A, 6 \ LD B, 31 \ EZ80_FN - + + #DEFINE EZ80_EX_USB_KYB_STATUS LD A, 6 \ LD B, 32 \ EZ80_FN + #DEFINE EZ80_EX_USB_KYB_READ LD A, 6 \ LD B, 33 \ EZ80_FN + #DEFINE EZ80_EX_USB_KYB_FLUSH LD A, 6 \ LD B, 34 \ EZ80_FN + #DEFINE EZ80_EX_USB_KYB_INIT LD A, 6 \ LD B, 47 \ EZ80_FN + #DEFINE EZ80_THROTTLE_START(p,store) \ #DEFCONT \ PUSH AF #DEFCONT \ PUSH BC