DMC-fortified controller read routine

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

tepples
Posts: 22052
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Post by tepples » Sat May 24, 2008 9:19 pm

blargg wrote:I guess I'll try writing and analyzing a version that reads until two consecutive reads give the same value. The main problem is that this takes a varying amount of time, and would hang if someone fed the controller a very rapid turbo signal.
As far as I can tell, the turbo controllers that carry the Official Nintendo Seal use a timer that turns on and off at up to 30 Hz or so, not a flip-flop that inverts after every strobe. At least my SN ProPad's box said it used a timer, and the NES Advantage uses adjustable timers. Has anyone reverse-engineered other licensed turbo controllers such as the NES Max?

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg » Sat May 24, 2008 10:50 pm

I wrote a version that keeps reading until it gets two in a row that match. I worked out the worst-case to be 5 reads, which can occur in the following two cases:

1: Correct
Controller changes here
2: Correct
3: Corrupted by DMC DMA
4: Correct
5: Correct

1: Correct
2: Corrupted by DMC DMA
3: Correct
Controller changes here
4: Correct
5: Correct

Due to the timing of the reads, DMC DMA cannot corrupt more than one read in 5 consecutive reads, even though 5 reads takes longer than 432 clocks. So the only thing that would hang this algorithm is a controller which kept changing more than once every 800 clocks or so.

I guess I could make this multi-read algorithm always read 5 times, then analyze the results, so as to reduce the timing variation.
tepples wrote:As far as I can tell, the turbo controllers that carry the Official Nintendo Seal use a timer that turns on and off at up to 30 Hz or so, not a flip-flop that inverts after every strobe.
The NES Max also used an oscillator for turbo. I doubt any controllers clock turbo from strobe.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg » Sun May 25, 2008 12:45 am

OK, probably the final version of the algorithm. This reads four times, then returns two matching readings, taking right around 660 clocks. DMC DMA can only corrupt one, and assuming only one controller change at most between the first and last read, at least two readings will always match. This one uses three zero-page temporary bytes temp, temp2, temp3.

read_joy3.zip
Last edited by blargg on Sun Jul 25, 2010 12:03 pm, edited 1 time in total.

User avatar
Dwedit
Posts: 4352
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit » Sun May 25, 2008 4:26 am

Has anyone thought of building a list of games potentially affected by the bug?
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
Bregalad
Posts: 7951
Joined: Fri Nov 12, 2004 2:49 pm
Location: Chexbres, VD, Switzerland

Post by Bregalad » Mon Sep 01, 2008 1:34 pm

Running count_errors.nes from the ReadJoy3 package on my PAL NES outputs me Conflicts : 0/1000 on all tries.

Should we conclude that the PAL NES is exempt from this DMC bug ?
Useless, lumbering half-wits don't scare us.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Post by blargg » Mon Sep 01, 2008 9:24 pm

I'm pretty sure the test program is tuned for NTSC DMC rates. I'll have to write a PAL version sometime soon.

EDIT: nevermind, that test doesn't do any synchronization. That suggests it doesn't occur on the PAL NES, though a thorough test would be good.

User avatar
Dwedit
Posts: 4352
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Post by Dwedit » Tue May 11, 2010 2:04 pm

The file is gone, anyone care to rehost it?

I also read the companion thread, and realized that pocketnes has a buggy joystick reader (does not require strobe to be stopped before reading bits).
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Post by thefox » Thu May 13, 2010 4:53 am

Dwedit wrote:The file is gone, anyone care to rehost it?

I also read the companion thread, and realized that pocketnes has a buggy joystick reader (does not require strobe to be stopped before reading bits).
read_joy3.zip
(26.95 KiB) Downloaded 273 times
Last edited by thefox on Wed Mar 04, 2015 12:57 pm, edited 1 time in total.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

User avatar
Jeroen
Posts: 998
Joined: Tue Jul 03, 2007 1:49 pm

Post by Jeroen » Wed Mar 07, 2012 6:52 pm

Anyone got a backup of these? The links appear down (at least for me)

edit: i'm retarded and didnt see the page 2 link >_> sorry for the necrobump

edit2: that being said...any chance of a asm6 version?

User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow » Thu Mar 08, 2012 11:21 am

Jeroen wrote:Anyone got a backup of these? The links appear down (at least for me)

edit: i'm retarded and didnt see the page 2 link >_> sorry for the necrobump

edit2: that being said...any chance of a asm6 version?
here

edit: i'm retarded and didn't read your second sentence but decided to submit my post anyway since my answer to your question is valid.

User avatar
Jeroen
Posts: 998
Joined: Tue Jul 03, 2007 1:49 pm

Post by Jeroen » Thu Mar 08, 2012 11:33 am

Awesome. Also I don't see why that would make YOU retarded....the thread's not magicly gonna be unnecrobumped. (And some extra mirrors are never gonna hurt)

User avatar
cpow
NESICIDE developer
Posts: 1097
Joined: Mon Oct 13, 2008 7:55 pm
Location: Minneapolis, MN
Contact:

Post by cpow » Thu Mar 08, 2012 12:20 pm

Jeroen wrote:Awesome. Also I don't see why that would make YOU retarded....the thread's not magicly gonna be unnecrobumped. (And some extra mirrors are never gonna hurt)
It's funny. Every time I see a thread like this pop back up my first reaction is "blargg's baaaaaaaack?!!".

User avatar
Zepper
Formerly Fx3
Posts: 3218
Joined: Fri Nov 12, 2004 4:59 pm
Location: Brazil
Contact:

Post by Zepper » Thu Mar 08, 2012 2:48 pm

Just for double checking the things: if no buttons are pressed, it should give a PASS; otherwise, ERROR. Is this ok?

3gengames
Formerly 65024U
Posts: 2277
Joined: Sat Mar 27, 2010 12:57 pm

Post by 3gengames » Thu Mar 08, 2012 4:33 pm

Yeah, assuming it's a official controller as the 9th read would be a high.

Post Reply