An error check in the 3 line handshake

SGDK only sub forum

Moderator: Stef

Post Reply
Chilly Willy
Very interested
Posts: 2603
Joined: Fri Aug 17, 2007 9:33 pm

An error check in the 3 line handshake

Post by Chilly Willy » Mon Nov 05, 2018 2:29 pm

An issue came up in the Plutiedev discord with the mouse. He took out the mouse support, but accidentally left the mouse support activated. When you activate the mouse or the tap and there isn't one plugged in, the current code has to timeout before it returns, which wastes a LOT of time. The very first nibble of the header is made while the device is deactivated, so a quick check should be made for the first value of the mouse and tap as an early out to get around the timeout. Something like this

Code: Select all

    /* make sure port is deselected to start at first phase */
    pb = (vu8 *)0xa10003 + port*2;
    *pb = 0x60;
    asm volatile ("nop");
    asm volatile ("nop");

    /* quick check for at least initial mouse/tap value to cut the amount
     * of time spent if a mouse/tap isn't really plugged in, but the game
     * still activates the support
     */
    i = *pb & 0x0F;
    if ((i != 0) && (i != 3))
        return -1;

    hdr[0] = THREELINE_HANDSHAKE(pb, 0x60);
That should return almost instantly if the mouse or tap isn't plugged in. If keyboard support is ever added, it should also check for 1, the first nibble of the keyboard header.

User avatar
Stef
Very interested
Posts: 2781
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: An error check in the 3 line handshake

Post by Stef » Tue Nov 06, 2018 4:08 pm

Is it possible to just add that part of the code directly in the SGDK mouse support ?

Chilly Willy
Very interested
Posts: 2603
Joined: Fri Aug 17, 2007 9:33 pm

Re: An error check in the 3 line handshake

Post by Chilly Willy » Tue Nov 06, 2018 4:13 pm

Hmm - I guess probably the best way to handle it would be in the code where you set the support flags. If you set the support flag for the mouse, check if there's a mouse. If so, allow the flag to be set. If not, don't set the flag. That routine really should check where it's possible, like with regular or 6-button pads, mice, etc. If you CAN verify a device associated with the support, do so. That really only works for things the SEGA ID routine can actually ID uniquely.

That was the problem in his code: setting the mouse flag when no mouse was there.

User avatar
Sik
Very interested
Posts: 715
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: An error check in the 3 line handshake

Post by Sik » Thu Nov 08, 2018 3:46 am

Doing the check only when you set the support flags means it won't work with hotplugging (I know, it's a bad idea to hotplug in the first place, but it's doable and people will keep doing it…). You really want to do the check every time it goes look at the peripheral (or at least, whenever SGDK believes there's going to be a mouse there).

But yeah, checking the initial nibble (the one that shows while TH is still set) before starting the packet proper is a very fast way to bail out. And yes, it really should be in the SGDK code itself, since it's SGDK the one that's doing the reads.
Sik is pronounced as "seek", not as "sick".

Chilly Willy
Very interested
Posts: 2603
Joined: Fri Aug 17, 2007 9:33 pm

Re: An error check in the 3 line handshake

Post by Chilly Willy » Fri Nov 09, 2018 11:57 pm

Yeah, didn't think about hot plugging... they'd have to try to set the support any time they looked for hot plugged controllers.

So, early out it is! Well, unless you make an official interface for handling hot plugging. Maybe a callback the user registers for when controllers change.

User avatar
Miquel
Very interested
Posts: 341
Joined: Sat Jul 30, 2016 12:33 am

Re: An error check in the 3 line handshake

Post by Miquel » Sat Nov 10, 2018 12:56 pm

Sik wrote:
Thu Nov 08, 2018 3:46 am
(I know, it's a bad idea to hotplug in the first place, but it's doable and people will keep doing it…). You really want to do the check every time it goes look at the peripheral (or at least, whenever SGDK believes there's going to be a mouse there).
Why is a bad idea to hotplug? You can hurt the hardware or something?

You can employ a counter and check the available devices once every second or so to not harm cpu speed.

Awaiting answer, this site is like music on Mars: never stops surprising you for good!
Yeah, I see you have to hive hope for the “believers” and the “non- believers”. Don’t worry I know.
“How much you have yet to learn”, is usually what them tell me when I fail to comprehend.

User avatar
Sik
Very interested
Posts: 715
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: An error check in the 3 line handshake

Post by Sik » Sat Nov 10, 2018 2:39 pm

Yes, the risk of harming the hardware is the issue. Some plugs are made so that GND gets connected first but the Mega Drive's plug is not one of those. Worse yet, the shape of the plug makes the pins wear out easily if you're constantly plugging and unplugging (probably a bigger problem, even!).

Mind you, in practice I never had an issue so you probably shouldn't worry about it if you ever need to do it (and there's probably some sort of protection going on because it's perfectly possible for a pin to be set to the wrong direction), but it's definitely something you don't want to encourage. That said, supporting it when somebody does it anyway is still a nice thing to have if you can afford it (maybe the controller unplugged by accident, maybe they forgot to swap peripherals before turning it on, etc.).
Sik is pronounced as "seek", not as "sick".

User avatar
Miquel
Very interested
Posts: 341
Joined: Sat Jul 30, 2016 12:33 am

Re: An error check in the 3 line handshake

Post by Miquel » Mon Nov 12, 2018 12:57 pm

I’m not an expert on electronics but:
A circuit doesn’t work until there is a circle between ground and current/positive, that’s why is called a circuit. If one of them isn’t connected electrons don’t race in the circuit and there is no power. Only possible failure is elements that make ground with air, but that’s not the case.

I don’t fully understand this: “pin to be set to the wrong direction”, do you remember that the connector is shaped to enforce only one way to plug it?
Yeah, I see you have to hive hope for the “believers” and the “non- believers”. Don’t worry I know.
“How much you have yet to learn”, is usually what them tell me when I fail to comprehend.

User avatar
TmEE co.(TM)
Very interested
Posts: 2319
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Re: An error check in the 3 line handshake

Post by TmEE co.(TM) » Mon Nov 12, 2018 3:24 pm

Pins on the controller port can be both inputs and outputs and some devices expect the direction to be set different from what a normal controller configuration uses. If two outputs get connected together you will have one or both sides getting damaged sooner or later.
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

User avatar
Miquel
Very interested
Posts: 341
Joined: Sat Jul 30, 2016 12:33 am

Re: An error check in the 3 line handshake

Post by Miquel » Mon Nov 12, 2018 4:00 pm

Sik, check the jar experiment its basically how electricity began to be understood.

TmEE, you will get undesired results, sure; but damaged, why? probably that's implemented as a tristate.
- If both ends get the same value, no problem.
- If one is high impedance, no problem.
- If one is ground and the other is high you have a short circuit with very low amperage, probably nothing happens

Lots of electronics are designed to tolerant in such situations, check buses and ISA, PCI and similar things.

As I said I'm no expert, so happy to be wrong and learn things.
Yeah, I see you have to hive hope for the “believers” and the “non- believers”. Don’t worry I know.
“How much you have yet to learn”, is usually what them tell me when I fail to comprehend.

User avatar
Sik
Very interested
Posts: 715
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: An error check in the 3 line handshake

Post by Sik » Mon Nov 12, 2018 4:06 pm

TmEE co.(TM) wrote:
Mon Nov 12, 2018 3:24 pm
Pins on the controller port can be both inputs and outputs and some devices expect the direction to be set different from what a normal controller configuration uses. If two outputs get connected together you will have one or both sides getting damaged sooner or later.
Reminds me, apparently the 3-button controller has no protection at all there (it wires the outputs as-is without any way to account for when the pin is configured the wrong way, which is common for TR on games supporting other devices). Does the console provide anything? Because I think Jorge said it doesn't, in which case WTF Sega (・_・)

My first thought was to add some sort of escape route (sorta like a pull-down resistor but working backwards, in that it would suck in the current to GND if two outputs happened to collide), but Jorge also scoffed at the idea.
Sik is pronounced as "seek", not as "sick".

User avatar
TmEE co.(TM)
Very interested
Posts: 2319
Joined: Tue Dec 05, 2006 1:37 pm
Location: Estonia, Rapla City
Contact:

Re: An error check in the 3 line handshake

Post by TmEE co.(TM) » Mon Nov 12, 2018 4:26 pm

There are 330ohm series resistors on TH and TL or TR lines which will help in cases using those pins out of the normal way. 330 ohms isn't a huge load to fight.
Mida sa loed ? Nagunii aru ei saa ;)
http://www.tmeeco.eu
Files of all broken links and images of mine are found here : http://www.tmeeco.eu/FileDen

User avatar
Sik
Very interested
Posts: 715
Joined: Thu Apr 10, 2008 3:03 pm
Contact:

Re: An error check in the 3 line handshake

Post by Sik » Tue Nov 13, 2018 5:09 am

Gonna continue that discussion here to avoid more derailing:
viewtopic.php?f=13&t=2936
Sik is pronounced as "seek", not as "sick".

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests