Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Input [Code documentation]

LapisRaider edited this page May 27, 2021 · 6 revisions

input.asm

input.asm contains functions and variables that handles inputs.

Sections in input.asm:


References and more info:


  • Contains variables that help keep track of the current input.
  • Located in WRAM, so it can be read and write.
  • The input is stored as bit flags in the variables.
  • first 4 bits: down (7), up (6), Left (5), right (4)
  • last 4 bits: start (3), select (2), B (1), A (0)
  • Variables:
    • wCurrentInputKeys, the input currently initialised, stores all current inputs regardless if it is previously pressed or not
    • wNewlyInputKeys, inputs that was not pressed previously, but pressed this time
; How to use the variables
   ld a, [wCurrentInputKeys/wNewlyInputKeys] ; get current/new inputs
   bit n, a ; check bit n of register a, this will set the z flag in the register f. 
            ; if it is pressed, the z flag will be 0
            ; bit 0, a means checked the 0 bit of a. If the 0 bit of a is 1, which means button A is press, z flag will be 0

  • Read the latest input, store and update the input variables created.
  • Located in ROM0
  • Function:

Things to note

  • We did not use the joypad interrupt to update and handle our input.
  • The joypad interrupt is not really used for collecting input, as the interrupt is not called when there is both d-pad and a/b/start/select input at the same time.
  • Thus we use our own function to collect input instead.

UpdateInput

  • Gets the current input, perform the instruction to handle debouncing and storing the correct input in the variables.
  • To be called in the main update loop.
  • Reason for debouncing: Sometimes the button states will keep switching when it is initially read, so we need to load the button state a few times to check and make sure it's stable. We want the final button state as it's the most stable.
  • To be able to get d-pad input, we write the value $20 to the register at the address $FF00 (reg for reading button info). To get the button inputs (A, B, start, select), we write the value $10 to the register instead.
  • To read the input information, we get the value in address $FF00. The last 4 bits of this register are the flags for the inputs. If you wrote value $20 to it, the last 4 bits are flags for the d-pad, if you wrote $10 to it, the flags are for the button.
  • Please note: We need to wait for a few cycles in between writing the value to the register, and reading the input information. This is so we can wait for the system to 'stabilise' in a sense for the Gameboy to provide the correct input.
; Input handler logic
UpdateInput:
      ; Write $10 to $FF00, wait a few cycles, read bits 3-0 to get the button inputs
      ; Write $20 to $FF00, wait a few cycles, read bits 3-0 to get the d-pad inputs
      ; Write $30 to P1 to release the key matrix
      ; Combine the data read into 1 byte
      ; Do some logic to check which buttons were newly pressed (so was not pressed in previous frames, the button isn't held down)
      ; Store the data in the corresponding variables
   ret