This page is a mirror of Tepples' nesdev forum mirror (URL TBD).
Last updated on Oct-18-2019 Download

Controller detection

Controller detection
by on (#54260)
On the NES, is there a reliable way to distinguish a controller on port 2 from an empty port 2?

My current project is a 2-player alternating card game. Because a lot of NES owners I've met (as well as PC emulator users) have only one working controller, I want my game to work whether one or two controllers are plugged in. I've noticed that Super Mario World for Super NES reads Luigi's keypresses from controller 1 if controller 2 is not present. I've also noticed that PS1 and PS2 games pause when the player unplugs a controller (e.g. "YOU ARE ROCKING OUT A BIT TOO HARD" in Guitar Hero III), and all three Super Smash Bros. games autodetect plug-ins and plug-outs (even when the N64 console's manual suggests not plugging except with the power off).

by on (#54267)
iirc, if no controller is plugged in, reads result in open bus. I guess the trick here would be to put something significant on the bus and see if it gets read back.

I'd imagine doing page boundary cross on the read would do the trick:

Code:
jsr strobe_joypad

loop_me_8_times:
  ldx #$80
  lda $4017-$80, x
  cmp #$3F   ; or maybe just check some of the low bits
  bne something_is_plugged_in


I bet a lot of older emus would choke on this, though. And I'm not sure whether or not it would actually work -- it's just a theory.

Worth a shot though. Easily testable with a powerpak.

EDIT: note that this could be "fooled" if the inserted controller returns an all '1' state. This can't happen on a standard controller though unless the player is pressing all buttons at once. Other types of input devices might be more prone to borks, though.

by on (#54277)
Disch wrote:
Worth a shot though. Easily testable with a powerpak.

That's the problem. PowerPak has pull-ups on CPU D0-D7, and a bunch of games that expect exactly $41 for a keypress needed patches. But you've found one way to copy-protect games, albeit easily worked around just like anything that doesn't rely on a remote server.

by on (#54288)
Why not just have a screen where you select how many controllers to use? It doesn't have to be an automatic detection. :P

by on (#54305)
Open-bus definitely wouldn't help -- the controller inputs are pulled high before the '368 buffers, so an absent controller really does look the same as one where no buttons are pressed.

by on (#54335)
Also I believe there is something different about the controllers that came with the PAL NES, but I forget what exactly.

It would be cool to do, but it seems there are all sorts of places where something can go wrong. Wouldn't hurt to put out a test ROM though, if anyone would actually report back about all the different hardware combinations out there.

by on (#54337)
I read somewhere it was possible to detect the presence of a controller by reading $4016/$4017 a certain number of times (more than 8). I don't know if this is true tough, since it was in an old doc.

by on (#54347)
At Bregalad's cue, I looked more closely -- it looks like the serial input pin on the CD4021 is tied to ground ... which means the 9th read should come back as a one if the controller is present. Otherwise it should be zero.

The wiki says "all subsequent reads will return D=1 on an authentic controller but may return D=0 on third party controllers."

by on (#54348)
That wiki entry is mine. I tested the 9th+ read for controllers, and some third party brands did return 0s instead of 1s. I couldn't find a reliable way to check for 1 vs 2 controllers. Only how to detect Four Score etc.

The PAL controllers have open collector outputs on clock and latch, so they need pull ups inside the controller to work. USA controllers don't have those pull ups meaning it is a crude region lock. That problem also hit the NEX wireless controllers which didn't have them either.

by on (#54349)
Read pal controllers a few times right after each other and they'll start to misbehave as well. I'm pretty sure I was the first one to find out about this weird phenonemon.

edit: http://nesdev.com/bbs/viewtopi ... highlight= <---the thread I discussed it in.

by on (#54356)
On the snes, it's easy because there's only 12 possible buttons, but it's a 16-bit shift register. The last 4 bits will let you know if something is plugged in or not, and even what it is (like a mouse). 100% reliable? no. but a 3rd party controller had better report that it was a controller.

Newer stuff has a more sophisticated serial protocol, so if nothing answers, it's obviously not there.

I know this doesn't help you that much.

If the 9th bit doesn't work, maybe do like some games do with a sort of battle interlude where it runs down a timer waiting for start button presses on the controllers that want to play. That, or just let the user decide by putting up a menu. Can never hurt to ask.

Secret of Mana allowed hot-plugging by requiring that select (or start?) be pressed. That way players could drop in or out.

by on (#54357)
whicker wrote:
Secret of Mana allowed hot-plugging by requiring that select (or start?) be pressed. That way players could drop in or out.

Controller 2 on the Japanese system has no start button, which is why LJ65 (a game where players can join in at will) uses A+B like Wii games. I guess I'll just require an A+B after the turn changes.