Gameboy Development Forum

Discussion about software development for the old-school Gameboys, ranging from the "Gray brick" to Gameboy Color
(Launched in 2008)

You are not logged in.

Ads

#1 2024-01-23 08:53:08

Sarasaworks
New member
Registered: 2024-01-23
Posts: 3

Left and Right Directional Keys Causing Havoc

I made a Key Tester using the stdio header included with GBDK. It works... Until it doesn't. All the keys register and print to the screen, except the Left(5) and Right(4) Directional Keys. Those keys throw the program into a weird state.

Pressing Left shows me this line in the Debugger (BGB):

HRAM: FFAD D3                             undefined code                                     ;0 26

Pressing Right shows me this:

HRAM: FF00 10 10                          <corrupted stop>                                ;1 2
HRAM: FF82 B7                              or      a                                               ;0 26

And this line in the bottom right:

WRA1:DFAB 00A7

I'm not at a point where I understand the debugger. In fact. this code I've written is my first dive into understanding how the Gameboy responds to software.

Here is my code so far. I used the assembly in Tetris as a base:

Code:

#include "C:\GBDK\include\stdio.h"
#include "C:\GBDK\include\gb\gb.h"

#define JOYPAD_REGISTER *(volatile unsigned char *)0xFF00
#define BUTTON_DOWN_REGISTER *(volatile unsigned char *)0xFF80
#define BUTTON_HIT_REGISTER *(volatile unsigned char *)0xFF81

void ReadJoypad() {
    unsigned char currentButtonState = 0;
    unsigned char previousButtonState, buttonHitState;

    JOYPAD_REGISTER = 1 << 5;
    for (int i = 0; i < 4; i++) {
        currentButtonState = JOYPAD_REGISTER;
    }
    currentButtonState = (~currentButtonState & 0x0F) << 4;

    JOYPAD_REGISTER = 1 << 4;
    for (int i = 0; i < 10; i++) {
        currentButtonState |= ~JOYPAD_REGISTER & 0x0F;
    }

    previousButtonState = BUTTON_DOWN_REGISTER;
    buttonHitState = (previousButtonState ^ currentButtonState) & currentButtonState;
    BUTTON_HIT_REGISTER = buttonHitState;

    BUTTON_DOWN_REGISTER = currentButtonState;

    JOYPAD_REGISTER = 1 << 5 | 1 << 4;
}

void main() {
    while (1) {
        ReadJoypad();
        unsigned char buttonHitState = BUTTON_HIT_REGISTER;

        if (buttonHitState != 0) {
            for (int i = 0; i < 8; i++) {
                if (buttonHitState & (1 << i)) {
                    printf("Button %d is being pressed\n", i);
                }
            }
        }
        delay(100);
    }
}

Last edited by Sarasaworks (2024-01-23 14:12:47)

Offline

 

#2 2024-01-25 14:08:04

Sarasaworks
New member
Registered: 2024-01-23
Posts: 3

Re: Left and Right Directional Keys Causing Havoc

The issue was that the default address set for GBDK's OAM refresh function was 0xFF80 which conflicted with code.

Luckily, GBDK offers a solution via LCC during the linker phase:

lcc -Wl-g_refresh_OAM=0xFEFF -o  test.gb main.o
This allowed me to change the address used by refresh_OAM to 0xFEFF. Which is commonly used for OAM manipulation in commercial titles.

Now, everything is in working order.

Last edited by Sarasaworks (2024-01-25 14:08:34)

Offline

 

#3 2024-01-25 15:52:22

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: Left and Right Directional Keys Causing Havoc

WHY?!

Code:

#include <gbdk/platform.h>
#include <stdio.h>
uint8_t joy = 0, old_joy = 0; 
void main(void) {
    while (TRUE) {
       joy = joypad();
       if (old_joy != joy) {
           for (uint8_t i = 0, j = 1; i!= 8; i++, j <<= 1)
              if (joy & j) printf("%hx ", j);
           if (joy) printf("\n");
           old_joy = joy; 
       }
       wait_vbl_done();
    }
}

then

Code:

lcc -o test.gb test.c

Offline

 

#4 2024-01-25 15:55:38

Sarasaworks
New member
Registered: 2024-01-23
Posts: 3

Re: Left and Right Directional Keys Causing Havoc

Because I take joy in taking things apart...

Offline

 

#5 2024-01-25 17:19:18

toxa
Member
Registered: 2020-02-13
Posts: 305

Re: Left and Right Directional Keys Causing Havoc

¯\_(ツ)_/¯

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson