Amiga Asm: Detecting Key Press To Control Program Flow

When a key is pressed it is stored in the $bfec01 register. To decode the register and find the value we have to execute a not.b and ror.b #1 to expose the raw key code. After this we can then do our checks to find out what key has been pressed.

To access the register and collect the necessary data we can use the following code

move.b  $bfec01,d0      ; Keypress
not.b d0
ror.b #1,d0 ; d0 now contains the raw key

From the above code the data register D0 will now contain the raw key code. A raw key code is a hex representation of the key that has been selected. The image below (taken from show the Amiga keyboard along with the RAW key codes for each of the keys

Raw Key Codes
US Keyboard Layout

We can then start checking what keys are pressed and branch to our desired sub-routine.

cmp.b   #$50,d0            ;Check for $50 = F1
beq F1KeyPress
cmp.b #$51,d0 ;Check for $51 = F2
beq F2KeyPress

Lets test this out with our ‘Hello World’ code allowing using to check for the escape key to quit the program by exiting the busy loop and the ‘s’ key to start another loop.

ExecBase = 4                 
OpenLib = -552
OpenLibVersion = 34
CloseLib = -414
PutString = -948
MOVE.L #OpenLibVersion,D0
LEA DosName,A1
MOVE.L ExecBase,A6
JSR OpenLib(A6)
BEQ.B NoLibError
MOVE.L #DisplayString,D1
JSR PutString(A6)
MOVE.B $bfec01,d0 ; Keypress
NOT.b d0
ROR.b #1,d0 ; d0 now contains the raw key
CMP.b #$45,d0 ;Check for esc
BEQ quit
CMP.b #$21,d0 ;Check for 'S'
BEQ start
BNE title
MOVE.L #DisplayStartString,D1
JSR PutString(A6)
BTST #6,$bfe001
BNE main
MOVE.L #DisplayQuitString,D1
JSR PutString(A6)
MOVE.L ExecBase,A6
JSR CloseLib(A6)
NoLibError: CLR.L D0
DosName: DC.B "dos.library",0
DisplayString: DC.B "Hello World!",10,0
DisplayStartString: DC.B "Start",10,0
DisplayQuitString: DC.B "Quit",10,0

Again assembling the code and jumping over to UAE to run the program from the shell gives the usual hello world output but this time instead of clicking the left mouse button hit the escape key. You will see a control character be added to the cli but the minute you delete this from the window control is given back to the cli showing that the program has indeed exited. The same if the ‘s’ key has been pressed, we will branch to the ‘start’ section and loop until the mouse button has been pressed.

We have now created a very basic title loop and main loop. In other words title page and main game page.

Be the first to comment

Leave a Reply

Your email address will not be published.