gbadev.org forum archive

This is a read-only mirror of the content originally found on forum.gbadev.org (now offline), salvaged from Wayback machine copies. A new forum can be found here.

ASM > Checking If Some Button Was Pressed

#174649 - nathanpc - Mon Jul 05, 2010 12:09 am

Hello,
I want to know how could I check if the A Button was pressed, and if it was. Do a b change_color. If possible, first give the theory(but very well explained), then I will try to do some code and post the result here. By this way I will learn and try to practice my knowledge.

Remember to specify the length of the addresses and values, as I usually have problems if I use str/ldr(for 32-bits values), or strh/ldrh(for 16-bits values). Or try to teach me how to recognize which type to use. :)

Best Regards,
Nathan Paulino Campos
_________________
Reading Tonc and trying to remind my very old C knowledge.

Just bought a GameBoy Advance SP one of those cartridges to put the ROMs for onboard debug.

#174859 - Dirbaio - Mon Jul 26, 2010 4:03 pm

(i'm assuming you know how hexadecimal and binary numbers work)

First off, you go to this document:
http://nocash.emubase.de/gbatek.htm

It is very useful for GBA and NDS developing and hacking. it explains how the communications to the GBA's hardware works.

Generally, you talk to hardware using "registers". A register is a value that can be written or read, and can be used to retrieve or modify the state of the hardware. On the GBA (and the NDS) registers are in what is called the I/O area, which is in addresses 0x04xxxxxx

Look at GBA Keypad input: http://nocash.emubase.de/gbatek.htm#gbakeypadinput

Code:

4000130h - KEYINPUT - Key Status (R)

  Bit   Expl.
  0     Button A        (0=Pressed, 1=Released)
  1     Button B        (etc.)
  2     Select          (etc.)
  3     Start           (etc.)
  4     Right           (etc.)
  5     Left            (etc.)
  6     Up              (etc.)
  7     Down            (etc.)
  8     Button R        (etc.)
  9     Button L        (etc.)
  10-15 Not used


The first line is telling you that there is a register at 0x04000130 which tells you which keys are pressed. It's a 16-bit register.

So, we'll read the value of that register.

LDR R0, =0x04000130
LDRH R0, [R0]

The first instruction loads the value 0x04000130 onto R0, and the second one reads the 16-bit value that is being pointed by R0, which is the register's value.

Now we have the value of the keypad register in R0. Now we want to get the value of Bit 0 only, to see if A is pressed or not. We'll be using the bitwise AND operation.

If we have a binary number like this: 000...01011b and we want to know it's bit 0, we have to do 000...01011b AND 1b:
Code:

000...01011b
000...00001b
--------------- &
00000000001b

but

000...01010b
000...00001b
--------------- &
00000000000b


AND's in ASM are done with, well, the instruction AND :P

AND R0, R0, #1

The first R0 is the destination, the second one and the #1 are the operands. That instruction would read R0 = R0 AND 1
Note the # on #1. It's put there because that #1 is called an "immediate expression", which is data included in the instruction itself. You have to prefix it with a #

Then, after that instruction, you'll get in R0: 1 if not pressed, 0 if pressed.

Then:

CMP R0, #0 //Compare
BEQ doSomething //Branch if EQual

or

CMP R0, #0
BNE skipSomething //Branch if Not Equal
blah blah blah... //Code that will be run if R0 = 0
skipSomething:



Hope it helps :P

#175100 - nathanpc - Thu Sep 09, 2010 12:49 am

Thanks very much for your great and well explanated answer! :)
_________________
Reading Tonc and trying to remind my very old C knowledge.

Just bought a GameBoy Advance SP one of those cartridges to put the ROMs for onboard debug.

#175102 - headspin - Thu Sep 09, 2010 9:32 am

Also check out the "tst" instruction for checking input bits.
_________________
Warhawk DS | Manic Miner: The Lost Levels | The Detective Game

#177359 - dantheman - Sat May 05, 2012 8:48 am

God I wish GBAdev had a report function...

There's no way this poster is anything but a copy+paste spambot.

#177360 - Dwedit - Sat May 05, 2012 10:14 pm

You PM the mods, that's your report function.
_________________
"We are merely sprites that dance at the beck and call of our button pressing overlord."