CPCWiki forum

General Category => Programming => Topic started by: Kitsune Mifune on 14:46, 18 October 19

Title: Noob Key reader problems
Post by: Kitsune Mifune on 14:46, 18 October 19
Hi folks,

I've hit that point now where I have my little sprite on screen and changing the value of a variable changes the animation on a re-compile. This is great, but I want to start attaching these to input in the form of key reads so I can do it in "real time" as it were (push a key and it changes the value of the variable, thereby changing the animation etc.), but it's proving harder than I expected.

I've watched a few tutorials on reading the keyboard, and I get the theory, but a lot of it is going over my head in terms of how to practically apply it in code. I looked at the firmware guide as a temporary easy solution and it has a couple of instructions on reading keys, but again; it tells you what they do, but doesn't give an example of how to use it in regard to where it's placed in your code/how to call what key etc.

I downloaded the below code from the fantastic Chibakumas site as it seems to be exactly what I need, but unfortunately I've no idea what to do with it, where to put it, or how to apply it/call it when needed (eg if I wanted to push "s" to do something). Also, the compiler throws up two missing definitions within the keymap (KeyChar_Enter, and KeyChar_Backspace) and I can't seem to find them anywhere (I had a trawl through the various branches, but I was starting to get really lost that way).

I have the code in a separate.asm file which is read outside the main loop and attached to a label (READ_KEY:), but as I say I just don't know what to do with it.

I realise this must be easy beans for hardcore coders, but any help/explanations for a noob dummy would be great, thanks! :)



; --------------------------------------------------------------------------------------------
;***************************************************************************************************


; Keyboard Reader


;***************************************************************************************************
;--------------------------------------------------------------------------------------------


KeyboardScanner_LineCount EQU 10
KeyboardScanner_LineWidth EQU 8
KeyboardScanner_KeyPresses  EQU 0


; Don't ask me how this works - someone smarter than me wrote it!


;KeyboardScanner_Flush equ &BB03


;map with 10*8 = 80 key status bits (bit=0 key is pressed)


Read_Keyboard:


KeyboardScanner_Read:


;Key input on the CPC goes through the 8255 PPI an AY soundchip, we need to use the PPI to talk to the AY,
;and tell it to read input through reg 14


;Port F6C0 = Select Regsiter
;Port F600 = Inactive
;Port F6xx = Recieve Data
;Port F4xx = Send Data to selected Register


ld bc,#f782     ;Select PPI port direction... A out /C out
ld hl,KeyboardScanner_KeyPresses    ;Destination Mem for the Keypresses
xor a
di             
out (c),c       


ld bc,#f40e     ; Select Ay reg 14 on ppi port A
ld e,b         
out (c),c       
ld bc,#f6c0     ;This value is an AY index (R14)
ld d,b         
out (c),c       
out (c),a    ; F600 - set to inactive (needed)
ld bc,#f792     ; Set PPI port direction A in/C out
out (c),c       
ld a,#40        ;First line is at &40, last is at &4A
ld c,#4a     
 
KeyboardScanner_Loop
ld b,d    ;d=#f6     
out (c),a       ;select line &F640-F64A
ld b,e    ;e=#f4   
ini             ;read in the bits to HL keymap
inc a           
cp c            ;Have we got to the end?
jp c,KeyboardScanner_Loop     
ld bc,#f782     
out (c),c       ; reet PPI port direction - PPI port A out / C out
ei


KeyboardScanner_AllowJoysticks:             
ret


; If we just need joypad UDLR-F type controls we're done...
; ...but if we want to read in text from the keyboard, we need to be able to convert to key


ifdef UseHardwareKeyMap


UseHardwareKeyMap:
db "u","r","d","9","6","3",KeyChar_Enter,"."
db "l","c","7","8","5","1","2","F"
db "c","[",13 ,"]","4","s","\","c"
db "^","-","@","P",";",":","/","."
db "0","9","O","I","L","K","M",","
db "8","7","U","Y","H","J","N"," "
db "6","5","R","T","G","F","B","V"
db "4","3","E","W","S","D","C","X"
db "1","2","e","Q","t","A","c","Z"
db "u","d","l","r","f","g","h",KeyChar_Backspace
endif


;Line 7 6 5 4 3 2 1 0
;&40 F Dot ENTER F3 F6 F9 CUR D CUR R CUR U
;&41 F0 F2 F1 F5 F8 F7 COPY CUR L
;&42 CTRL \ SHIFT F4 ] RETN [ CLR
;&43 . / : ; P @ - ^
;&44 , M K L I O 9 0
;&45 SPACE N J H Y U 7 8
;&46 V B F G/J2 F1 T/ J2 R R/ J2 L 5/ J2 D 6/ J2 U
;&47 X C D S W E 3 4
;&48 Z CAPSLK A TAB Q ESC 2 1
;&49 DEL J1 F3 J1 F2 J1 F 1 J1 R J1 L J1 D J1 U




KeyboardScanner_ScanForOne:
call KeyboardScanner_Read


ld b,10
ld c,0
ld hl,KeyboardScanner_KeyPresses


KeyboardScanner_WaitForKey_Check:
ld a,(hl)
cp 255
ret nz
inc hl
inc c
djnz KeyboardScanner_WaitForKey_Check
RET


KeyboardScanner_WaitForKey:
;call KeyboardScanner_WaitForKey2

;KeyboardScanner_WaitForKey2:
call KeyboardScanner_ScanForOne
cp 255
jr z,KeyboardScanner_WaitForKey
RET


;; Read Controls ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


Player_ReadControlsClassic: ;if either player presses anything act on it - used for menus etc


; ixl = Keypress bitmap Player 1
; ixh = Keypress bitmap Player 2
; HL Direct pointer to the keymap


call KeyboardScanner_Read
; call Player_ReadControls2
ld ixh,a
jr Player_ReadControls


; ret






;Player_ReadControls2:
; ld hl,KeyMap2
; ld bc,&F991
; jr Player_ReadControlsB


Player_ReadControls:
; returns
; ixl = Keypress bitmap Player
; HL Direct pointer to the keymap
ld hl,UseHardwareKeyMap
ld bc,&F990


;Player_ReadControlsB:
; ld a,0 :multiplaysupport_Plus2;in a,(c) ;
; cpl
push af
ld c,0
ld b,8
; We compare the keypresses to the 8 key map,
; and store the result as a single byte
; when 2player support is done, we will do this twice one for
; each controller


Player_Handler_ReadNext:


push bc


ld d,(hl) ;Bitmap
inc hl
ld a,(hl) ;line num
inc hl


push hl
ld hl,KeyboardScanner_KeyPresses
;push de
; ld d,0
; add hl,de
add l
ld l,a
; pop de
ld a,(hl)
or d
inc a ; see if A is 255!
ld e,0
jr nz,Player_Handler_notPressed
inc e
Player_Handler_notPressed:
pop hl
pop bc
ld a,c
rlca
add e
ld c,a
djnz Player_Handler_ReadNext
pop af
and c




ld ixl,a
ld hl,KeyboardScanner_KeyPresses ; This is the location of the real memory to do with as you wish!
RET
Title: Re: Noob Key reader problems
Post by: geebus on 16:08, 19 October 19
Hi, looks like i'm in a similar position to you in the learning of ASM. I just started making something yesterday and found that if I use the following I can "read" a keypress and then activate a routine.
org &4000

Printchar equ &bb5a ;firmware for printing the ascii character to screen
Waitchar equ &bb06 ;firmware routine to check if a key is being pressed

keypress:
call WaitChar
cp a,114 ; pressing r  ;if 'r' (ascii code 114) is being pressed
jr z,RandomDate        ;if last command resulted in a true value then jump relative to RandomDate routine
cp a,101 ; pressing e ;if 'e' (ascii code 101) is being pressed
jr z,End                         ;if last command resulted in a true value then jump relative to End routine
;cp a,240 ; pressing up
;jp z,IncreaseDate
;cp a,241 ; pressing down
;jp z,DecreaseDate
;cp a,242 ; pressing left
;jp z,MoveLeft
;cp a,243 ; pressing right
;jp z,MoveRightjr nz,Keypress             ;if none of the above is true then return to the start of the keypress routine.

RandomDate:
ld hl,RandomDateText
call Print
jr Keypress
End:
ret
Print:
ld a,(hl)
cp 255
ret z
inc hl
call PrintChar
jr Print

This is using a firmware routine to do the check for keypresses etc then run routines based on what key is being pressed.]As I said, My first day of writing my own program in ASM (and I've gotten stuck with how to change the background and pen colours..) So there might very well be better ways to do this.
You can see that i've commented out my up, down, left and right keypresses at the moment as I haven't made their routines yet.
I think what I have will work fine for my current program because it's fairly simple and nothing happens unless a key is pressed (so the keypress routine is continually going unless a key IS pressed). Whereas if you're making a game, i'm sure there will be tons of routines going and you could press a key at any moment. So now that i've written all this out and thought about it, i'm realising that my example might very well be useless to you...
Hopefully it is, but if not... I've now thought of future problems for future things I want to make..
Title: Re: Noob Key reader problems
Post by: Kitsune Mifune on 12:14, 20 October 19
Thank you very much geebus, I'll try this out when I get in tonight. I managed to get the joystick firmware code to work for me, but this will come in handy as well. Using the firmware is fine for me just now as getting it working is more important that how efficient it works.

Yeah it's tough being a noob. I feel really silly every time I have to ask a question, like I'm being a burden to people who are discussing more advanced stuff in the forum.

Maybe there should be a "Beginners Programming" category where it's ok to ask really stupid, nooby questions and get seasoned volunteers to proof read bits of code. lol
Title: Re: Noob Key reader problems
Post by: geebus on 11:03, 21 October 19
I'm now looking into the keyboard routine that you posted. Assuming its from https://www.chibiakumas.com/z80/platform.php#LessonP6 (https://www.chibiakumas.com/z80/platform.php#LessonP6) ?
I think I might impliment that as I don't have things working the way I would like them to in the way I posted before.


I'll let you know if I make any breakthroughs.


Looks like you're in Scotland too? if so, where abouts?
Title: Re: Noob Key reader problems
Post by: Kitsune Mifune on 11:35, 21 October 19
I'm in Glasgow mate. Just south of the Clyde close to the Squinty bridge.
Title: Re: Noob Key reader problems
Post by: geebus on 11:56, 21 October 19
Tradeston?
I used to live in Wallace st.
Small world.
I work in Glasgow. Here at the moment.


I created an Amstrad Scotland Facebook page as I was getting a bit annoyed when I kept seeing things I wanted for sale, collection only, and it was in England.
Not done anything with it yet. Possibly should.
Title: Re: Noob Key reader problems
Post by: Kitsune Mifune on 12:18, 21 October 19
Cessnock, so just down the road from Tradeston. I'm originally from further West though. Kilmacolm to be precise.

I'll check out your Facebook group for sure. I was on another one but I left as most of the members were really childish and petty in regard to other retro systems like the C64 and the Speccy - and they were pretty serious too. Can't be bothered with that playground tier rivalry bollocks, especially from people in their 30s and 40s.
Title: Re: Noob Key reader problems
Post by: geebus on 12:28, 21 October 19
Are you kidding me!?
I Live in Kilmacolm...
HAHAHA whats the chances.


The facebook group has nothing in it yet mate. Feel free to add yourself though.
Title: Re: Noob Key reader problems
Post by: Kitsune Mifune on 12:41, 21 October 19
Wow, what are the chances indeed! I've probably bumped into you in The Pullman at some point in the past when I'm down seeing the family.

I can't seem to fine the Facebook group in a search so fire us a PM with the link in it.
Title: Re: Noob Key reader problems
Post by: geebus on 13:18, 21 October 19

Probably because I just made it the other day and there's only me in it haha!
https://www.facebook.com/groups/912729385765311 (https://www.facebook.com/groups/912729385765311)


The pullman is no more. It's now the coach house and isn't open yet.

To go back to the original reason for this post!
I've been looking at the keyboard scanning routine and I have no idea how to implement this either.
I'll keep digging.
Title: Re: Noob Key reader problems
Post by: SRS on 20:55, 26 October 19
I bought my first FRONT242 CD at Glasgow :)
Scotts ... I just say  ... "ELEVEN"  !


Title: Re: Noob Key reader problems
Post by: ComSoft6128 on 06:07, 27 October 19

Off-topic.

Headhunter and Masterhit!


Back in the nineties, in Glasgow,  Masterhit was one ( + various NIN, Killing Joke, Sisters of Mercy vids etc ) of the music videos I put thru the ROMBO Vidi looking for an interesting screen grab.

Small CPC world indeed.
Powered by SMFPacks Menu Editor Mod