;============================================================ ;_getkey loop routine by Leif Astrand (leif.astrand@multi.fi) ;22 bytes ;version 1.0 ; ;in: de = pointer to keyset ;out: hl = address jumped to (could be used in some morphic code...) ;action: jumped to somewhwere, depending on the pressed key ;destroyed: af,b,de,hl ;============================================================ ; ;You're free to use this code in any way, but please give me ; a credit in the readme file ; ;You save 2 bytes for every key compared to cp x and jp z,y ;You save 1 byte for every key compared to cp x and jr z,y ;You lose 1 byte for every keyset (due to the lenght byte) ; ;It's up to you to count bytes and see if your program gets ; smaller using this routine ; ;The keyset starts with a lenght byte (number of keys) ; then each key is 3 bytes: ; first the _getkey code for the key ; then two bytes address to jump to if the key is pressed ; ;Example program: ; ;#include "ti86asm.inc" ; .org _asm_exec_ram ;keyloop: ; ld de,keyset ;load the keyset ; jp smallkey ;jump to the keyloop (can also be called) ; ;enter: ;get here if enter is pressed ; call _putc ;do something to confirm that the keypress was detected ; jr keyloop ;exit: ;and this is the code executed after pressing exit ; ret ; ;keyset: ;the keyset ; .db 2 ;two entries in this one ; ; .db kEnter ;if enter pressed... ; .dw enter ;...then jump to this label ; ; .db kExit ;cp kExit ; .dw exit ;jp z,exit smallkey: push de ;destroyed by _getkey call _getkey ;get keycode to a pop de ;get ponter back ld h,d ;ld hl,de, use hl as pointer, de is saved as inital pointer ld l,e ld b,(hl) ;get the first byte as loop counter smallkey_loop: inc hl ;now hl = keycode for current entry cp (hl) ;compare with the pressed key inc hl ;now hl = address to jump to if keycodes match jr z,smallkey_pressed ;do the jump if the keycodes match inc hl ;now hl = 1 byte before next keycode djnz smallkey_loop ;go to next entry jr smallkey ;go for a new keypress if no keycode matched, de is still the original pointer smallkey_pressed: ;jump to address at address in hl call _ldhlind ;aka ld hl,(hl) -> hl is now the address to jump to jp (hl) ;jump to the address stored in the hl registry .end