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 2021-11-19 20:10:37

bcafuk
New member
From: Zagreb, Croatia
Registered: 2021-11-19
Posts: 2

Joypad timing?

Hello Game Boy developers!

I have joypad input working, but I was wondering what exactly the required timing for reading the joypad register is.
I've looked at several sources and they all say that after writing to memory address $FF00, one must wait a little before the data stabilizes.
From what I've gathered, most games implement the little delay by reading the register multiple times consecutively.
My question is, how big does that little delay even have to be?


Looking at the dissasembly, it seems as if the Tetris ROM I got does 4 reads for the buttons, followed by 10 reads for the D-pad.
With the reads using the ldh a, [$FF00] instruction, each read takes 3 cycles.
This puts it at 12 cycles to read the first set of four inputs, and 30 cycles to read the second set of four inputs.

I also looked at a ROM of Pokemon Red, which used 6 read instructions for the buttons, then 10 read instructions for the D-pad.
This means that it takes 18 cycles for the first one, and 30 cycles for the second one.

The BGB emulator seems to work fine even with a single nop of delay, followed by ld a, [$FF00 + c] to load the value from memory.
Together, this makes a working read take 3 cycles.


So, my questions are:
How short of a delay can I get away with?
How much should I use to be safe if I decide to run my game on actual hardware?
Has anyone tested this on actual hardware?

I figure I can just copy what others do, but I find it odd that I have not yet stumbled upon any documentation mentioning actual values.


I'd like to give big thanks to the community. The tools, docs, and forum threads have been very helpful.

- Borna

Offline

 

#2 2021-11-20 14:06:27

tmk
Member
Registered: 2017-05-01
Posts: 63
Website

Re: Joypad timing?

Hi there!

Well, I don't quite understand what you mean but I'll try to answer the best I can. wink

You have the theory described in the manual - pages 23 and 24.

An example code is here.

Short answer would be: you have routine to read joypad state, you call it every frame and process results accordingly.
If you choose to call it every second, third, etc. frame then input lag will be more noticeable.
The mandatory delay after writing to rP1 is constant, you need exactly 2 reads for P14 and 6 reads for P15 (see example) to get valid state.

HTH!

Offline

 

#3 2021-11-20 14:30:44

bcafuk
New member
From: Zagreb, Croatia
Registered: 2021-11-19
Posts: 2

Re: Joypad timing?

Thanks for the reply, that's exactly what I was asking!

tmk wrote:

You have the theory described in the manual - pages 23 and 24.

Sorry, I didn't even know an official manual is publicly available. I'll make sure to use it now that I know about it.
The comments in the example code in the manual say exactly what I wanted to know, which was this:

tmk wrote:

The mandatory delay after writing to rP1 is constant, you need exactly 2 reads for P14 and 6 reads for P15 (see example) to get valid state.

However, now I am wondering why both Tetris and Pokemon seem to read more times than is specified in the manual.
For example, Tetris reads 4 times for P14, and 10 times for P15.
Do you perhaps know the answer to this?
These are the only two ROMs I looked at, so I have no idea what other licensed games did.


As a side-note, it seems that the manual has a mistake in the example code on page 24: It says LD A, ($10) instead of LD A, $10.

-Borna

Last edited by bcafuk (2021-11-20 14:31:16)

Offline

 

#4 2021-11-20 15:48:41

tmk
Member
Registered: 2017-05-01
Posts: 63
Website

Re: Joypad timing?

Yes, the manual has some bugs but they're easy to spot. There's some unofficial documentation like Pan docs, etc. Choose whatever you feel like.

As for Tetris, no idea. Japanese (V1) has proper routine, World (V1.1) just like you say. Routine from DX version is also like the manual says.

Offline

 

Board footer

Powered by PunBB
© Copyright 2002–2005 Rickard Andersson