CPCWiki forum

General Category => Programming => Topic started by: nicf82 on 14:56, 27 March 20

Title: KM READ KEY firmware call (&BB1B)
Post by: nicf82 on 14:56, 27 March 20
Hi


Im just trying to learn assembler on the Z80, enjoying it so far but have a question about the KM READ KEY firmware call. So i wrote a program to output the key pressed in hex.


Im making the call then checking the contents of the A register. If i press the W key (for example) then according to the diagram (attached) I have i should get 59 (hex &3B) but i am getting &77.


When I call KM TEST KEY it obeys the key codes in the diagram. I think i might be confused as to what constitutes a 'key code' when talking about the KM READ KEY firmware return value?


Cheers
Nic


Code below in case it helps



txt_output equ &bb5a
km_get_joystick equ &bb24
km_read_key equ &bb1b
km_test_key equ &bb1e
txt_set_cursor equ &bb75
txt_clear_window  equ &bb6c
org &4000
call txt_clear_window
call CenterCursor
ld a,0
jr ShowKey
MainLoop:
call CenterCursor
;Check if 'q' key pressed, and quit
ld a,67
call km_test_key
jp nz, Done
call km_read_key
jp nc, MainLoop ;Nothing pressed, dont update
;call km_get_joystick
ShowKey:
ld c,a
ld d,c
call PrintBinary
ld a,' '
call txt_output
ld d,c
call PrintHex
jr MainLoop
Done:
call NewLine
call NewLine
ld hl,str_done
call PrintString
ret
CenterCursor:
ld hl,&0101
call txt_set_cursor
ret
;Print d register in binary
PrintBinary:
ld b,8
ld hl,str_bin_buffer+9 ;Bit char rep destination
PrintNextBit:
ld a,d ;put digit in a
dec hl
and %00000001
add 48
ld (hl),a
sra d ;Shift digit to the right
djnz PrintNextBit
ld hl,str_bin_buffer
call PrintString
ret
;Print d register in hex
PrintHex:
ld b,2
ld hl,str_hex_buffer+3 ;Hex char rep destination
PrintNextHexChar:
ld a,d ;put digit in a
dec hl
and %00001111
cp 10
jr c,PrintNumericHexChar
add 7
PrintNumericHexChar:
add 48
ld (hl),a
sra d ;Shift digit to the right
sra d ;Shift digit to the right
sra d ;Shift digit to the right
sra d ;Shift digit to the right
djnz PrintNextHexChar
ld hl,str_hex_buffer
call PrintString
ret
PrintString:
ld a,(hl)
cp 255
ret z
inc hl
call txt_output
jr PrintString
NewLine:
ld a,13
call txt_output
ld a,10
call txt_output
ret
str_bin_buffer: db '%        ',255
str_hex_buffer: db '&  ',255
str_done: db 'Done',255


Title: Re: KM READ KEY firmware call (&BB1B)
Post by: Urusergi on 16:40, 27 March 20
Maybe you have to differentiate ASCII codes from the cpc scan codes

https://ascii.cl/ (https://ascii.cl/)
Title: Re: KM READ KEY firmware call (&BB1B)
Post by: nicf82 on 18:35, 27 March 20
Ah yeah, so KM READ KEY returns the ASCII code rather than the key code, is there a way to read the key code?



Title: Re: KM READ KEY firmware call (&BB1B)
Post by: Urusergi on 19:09, 27 March 20
Afaik there is no specific routine in the firmware. Keycodes are almost the result of direct-reading of the keyboard port, so you have to create the low-level reading routine, or create a program to convert ascii to keycode.

http://www.cpcwiki.eu/index.php/Programming:Keyboard_scanning (http://www.cpcwiki.eu/index.php/Programming:Keyboard_scanning)
Title: Re: KM READ KEY firmware call (&BB1B)
Post by: nicf82 on 19:32, 27 March 20
Thanks Urusergi, see what you mean - I will be able to get by with that as long as i understand the difference


Nic
Title: Re: KM READ KEY firmware call (&BB1B)
Post by: AMSDOS on 22:08, 27 March 20
In BASIC KM READ KEY (&BB1B) is INKEY$, though to check for Key Numbers KM TEST KEY (&BB1E) is used with A containing the Key Number and the NZ holding the state, it gets a little bit confusing in BASIC because KM TEST KEY becomes INKEY().
Powered by SMFPacks Menu Editor Mod