
Sauf précision, les routines proposées respecteront la syntaxe nAssembler/armasm.
Pour commencer, je vous propose ma routine WAITKEY qui attend l'appui d'une touche (clavier Touchpad)
Here's the place where everyone would bring/optimise routines written in ARM926EJ-S assembly
Unless noticed, the suggested sources will respect nAssembler/armasm syntax

Let's start with my routine WAITKEY which waits for any key to be pressed (Touchpad keypad)
Touchpad keypad map

- Code: Select all
WAITKEY ;OUTPUT: r12 = custom key-code (from [ret]=1 to [,]=88)
stmfd sp!,{r0-r4,lr} ; saves r0,...r4 & Link Register
adr r0,V900e0010 ; r0=memory area containing value 0x900e0010
ldr r0,[r0] ; r0=#0x900e0010
kpress bl kstate
bne kpress ; Loop until no key being pressed
wait4k bl kstate
beq wait4k ;Loop until one key being pressed
mov r2,#0 ; r2=0 ; (1st line of table)
mov r1,r2 ; r1's highest bytes must be cleared for later operations
which_k ldrh r1,[r0],#2 ; 16bit transfering : r1=[r0] ; then r0=r0+2
clz r3,r1 ; r3=nb of cleared bits in r1 until set bit (from bit31 to bit0)
rsbs r3,r3,#32 ; r3=32-r3 ; thus r3=0 when every bits are cleared
addeq r2,r2,#11 ;If so, r2=r2+11 ; (next line of table , 11 bits per line)
beq which_k ; and continue to check.
add r12,r3,r2 ; Otherwise , r12=r3+r2 ; (we found the key!)
ldmfd sp!,{r0-r4,pc} ; restores r0-r4 ; pc=lr to come back from this routine
;-----------------------------------------------------------------
kstate ldmia r0,{r1-r4} ; r1=[900e0010] ...r4=[900e001c]
orr r1,r1,r2 ; r1=r1 or r2
orr r2,r3,r4 ; r2=r3 or r4
orrs r1,r1,r2 ; r1=r1 or r2
mov pc,lr ; pc=lr to come back from this subroutine
;------------------------------------------------------------------
V900e0010 dcd 0x900e0010
Passons maintenant à la version pour modèles clickpad/ clavier Ti84+ que nous appellerons WAITKEY_classic.
Contrairement aux modèles Touchpad, le contrôleur clavier est par défaut en mode "single scan" (il va donc falloir momentanément basculer en mode "continuous scan") . De plus, lorsque l'on scrute l'état des touches, un bit à 1 siginifie que la touche s'y référant n'est PAS pressée.
Now let's talk about the version for clickpad/Ti84+ keyboard models we'll name WAITKEY_classic.
Contrary to touchpad models, the keypad controller is in "single scan" mode (so we'll have to set in "continous scan" mode for a while).
Moreover, when we read states of keys, a bit set to "1" means "key not pressed".
Clickpad keypad map

TI-84+ keypad map

- Code: Select all
WAITKEY_classic ;OUTPUT: r12 = custom key-code (from [ret]=1 to [=]=88)
stmfd sp!,{r0-r6,lr} ; saves r0,...r6 & Link Register
adr r0,V900e0010 ; r0=memory area containing value 0x900e0010
ldr r0,[r0] ; r0=#0x900e0010
sub r6,r0,#0x10 ; r6=#0x900e0000
ldr r5,[r6] ; r5=default keypad controller parameters
orr r1,r5,#1 ; as "single scan" is default mode on clickpad models,
str r1,[r6] ; setting bit0 to 1 will turn into "continuous scan" mode
kpress bl kstate
bne kpress ; Loop until no key being pressed
wait4k bl kstate
beq wait4k ;Loop until one key being pressed
mov r2,#0 ; r2=0 ; (1st line of table)
adr r4,V0b11111111111
ldr r4,[r4] ; r4 is a mask to keep bit0 to bit10 only
which_k ldrh r1,[r0],#2 ; 16bit transfering : r1=[r0] ; then r0=r0+2
mvn r1,r1 ;reverse the result ('cause on clickpad, bit cleared mean key pressed)
and r1,r1,r4 ; only bit0 to bit10 concerned
clz r3,r1 ; r3=nb of cleared bits in r1 until set bit (from bit31 to bit0)
rsbs r3,r3,#32 ; r3=32-r3 ; thus r3=0 when every bits are cleared
addeq r2,r2,#11 ;If so, r2=r2+11 ; (next line of table , 11 bits per line)
beq which_k ; and continue to check.
add r12,r3,r2 ; Otherwise , r12=r3+r2 ; (we found the key!)
str r5,[r6] ; restore "single scan" mode
ldmfd sp!,{r0-r6,pc} ; restores r0-r6; pc=lr to come back from this routine
;-----------------------------------------------------------------
kstate ldmia r0,{r1-r4} ; r1=[900e0010] ...r4=[900e001c]
eor r1,r1,r2 ; r1=r1 xor r2
eor r2,r3,r4 ; r2=r3 xor r4
orrs r1,r1,r2 ; r1=r1 or r2
mov pc,lr ; pc=lr to come back from this subroutine
;------------------------------------------------------------------
V900e0010 dcd 0x900e0010
V0b11111111111 dcd 0b11111111111