Prev: D16F Up: Map Next: D20C
D188: Set User-Defined Keys
Used by the routines at TitleScreen and Restart_SetUserDefinedKeys.
user-defined-keys
SetUserDefinedKeys D188 LD A,$00 Reset *Current_UserDefinedKey to 00.
D18A LD ($D20C),A
D18D LD A,$24 Write Keyboard Input (24) to *ControlMethod.
D18F LD ($5BEA),A
Don't clear the whole screen. Leave the header from the title screen.
D192 LD B,$12 Clear the bottom 12 lines using CL_LINE.
D194 CALL $0E44
UserDefinedKeys_Loop D197 LD DE,$DACB DE=Messaging_DefineKeys.
D19A CALL PrintString Call PrintString.
Pause to let the message sink in...
D19D LD B,$0A B=0A (pause loops).
UserDefinedKeys_PauseLoop D19F PUSH BC Stash BC on the stack.
D1A0 CALL SmallPause Call SmallPause.
D1A3 POP BC Restore BC from the stack.
D1A4 DJNZ UserDefinedKeys_PauseLoop Decrease counter by one and loop back to UserDefinedKeys_PauseLoop until counter is zero.
Display each (current) user-defined key, and the relevant messaging.
D1A6 LD A,$00 Call PrintUserDefinedKey using key position: 00.
D1A8 CALL PrintUserDefinedKey
D1AB LD A,$01 Call PrintUserDefinedKey using key position: 01.
D1AD CALL PrintUserDefinedKey
D1B0 LD A,$02 Call PrintUserDefinedKey using key position: 02.
D1B2 CALL PrintUserDefinedKey
D1B5 LD A,$03 Call PrintUserDefinedKey using key position: 03.
D1B7 CALL PrintUserDefinedKey
D1BA LD A,$04 Call PrintUserDefinedKey using key position: 04.
D1BC CALL PrintUserDefinedKey
Fetch the user input.
UserDefinedKeys_InputLoop D1BF CALL $028E Call KEY_SCAN.
D1C2 LD A,E Loop back to UserDefinedKeys_InputLoop until any key has been pressed.
D1C3 CP $FF
D1C5 JR Z,UserDefinedKeys_InputLoop
D1C7 CP $20 Jump to CheckForDuplicates if "SPACE" has been pressed (20).
D1C9 JP Z,CheckForDuplicates
D1CC PUSH AF Stash the keypress on the stack briefly.
Create an offset for the currently in-focus key position using DE.
D1CD LD A,($D20C) E=*Current_UserDefinedKey.
D1D0 LD E,A
D1D1 POP AF Restore the keypress from the stack.
D1D2 LD D,$00 D=00 (to finish creating the offset for the current key position using DE).
D1D4 LD HL,$5BEB HL=UserDefinedKeys_Left+DE.
D1D7 ADD HL,DE
D1D8 LD (HL),A Write the keypress code to the appropriate position in the user-defined keys table held by *HL.
A small pause loop to give the user a chance to release the key.
D1D9 LD B,$02 B=02 (pause loops).
UserDefinedKeys_Debounce D1DB PUSH BC Stash BC on the stack.
D1DC CALL SmallPause Call SmallPause.
D1DF POP BC Restore BC from the stack.
D1E0 DJNZ UserDefinedKeys_Debounce Decrease counter by one and loop back to UserDefinedKeys_Debounce until counter is zero.
Move onto the next key position.
D1E2 LD A,($D20C) Increment *Current_UserDefinedKey by one.
D1E5 INC A
D1E6 LD ($D20C),A
D1E9 CP $05 Loop back to UserDefinedKeys_Loop until all 05 keys have been defined.
D1EB JP NZ,UserDefinedKeys_Loop
Each value is checked against other values ahead of it. It doesn't need to check "behind" as e.g. consider the following table:
Cycle Using Positions
01 02 03 04
01 01 02 03 04 05
02 02 03 04 05 ---
03 03 04 05 --- ---
04 04 05 --- --- ---
On the 1st cycle; 01 is checked against 02 03 04 and 05.
So on the 2nd cycle, there's no need to check 02 against 01 as this already happened in the 1st cycle.
And so on...
CheckForDuplicates D1EE LD B,$04 Set a counter; there are 04 other keys to check at the beginning of the cycle.
D1F0 LD HL,$5BEB Set a pointer in HL to the beginning of the key storage: UserDefinedKeys_Left.
CheckForDuplicates_Loop D1F3 PUSH BC Stash the current key map counter on the stack (this is reduced by one on each cycle).
D1F4 LD A,(HL) Does nothing, this is immediately overwritten in the loop below.
Clone HL into DE to prepare for the checking loop.
D1F5 PUSH HL DE=HL (using the stack).
D1F6 POP DE
Process this cycle. *HL points to the current "checking" key map value, and DE is incremented on each cycle to check against it.
CheckForDuplicates_CheckLoop D1F7 INC DE *DE will contain the comparison key map value, so increment DE by one.
D1F8 LD A,(DE) If there's a match between *DE and *HL, jump to Restart_SetUserDefinedKeys and get the user to try again.
D1F9 CP (HL)
D1FA JP Z,Restart_SetUserDefinedKeys
D1FD DJNZ CheckForDuplicates_CheckLoop Decrease the key map counter by one and loop back to CheckForDuplicates_CheckLoop until all key map values have been checked.
This cycle is finished, so prepare for the next one.
D1FF POP BC Restore the key map counter from the stack.
D200 INC HL Move the value pointer to the next value.
D201 DEC B Decrease the key map counter by one, each cycle checks one less key value.
D202 JR NZ,CheckForDuplicates_Loop Jump to CheckForDuplicates_Loop until all key map values have been checked.
We are good! Clear the screen and move back to the title screen.
D204 LD B,$12 Clear the bottom 12 lines using CL_LINE.
D206 CALL $0E44
D209 JP Print_TitleScreen Jump to Print_TitleScreen.
Prev: D16F Up: Map Next: D20C