Glitch-free controller reads with DMC?

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Glitch-free controller reads with DMC?

Post by thefox »

Rahsennor wrote:Slapped together the simplest test I could come up with, and to my surprise, it works perfectly in Nestopia... but on odd cycles instead of even. None of the other emulators I tested emulate DMC conflicts at all.
Same result in Nintendulator. Definitely should test on real hardware though (I don't have mine hooked up right now).

Just so this is clear to everybody:
- You have to have a standard controller plugged in, and shouldn't press Right.
- If screen stays black => no corruption
- If screen goes white => was corrupted
- (You might want to press Right in the end to force white to make sure that the ROM was properly loaded.)
Last edited by thefox on Mon May 30, 2016 12:14 pm, edited 1 time in total.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Glitch-free controller reads with DMC?

Post by Quietust »

Just ran it on my NTSC CopyNES, and the "even" test appears to pass (screen only goes white when I press Right on either controller), while the "odd" test fails (goes white immediately).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Glitch-free controller reads with DMC?

Post by tepples »

I saw the same results on my NTSC NES + PowerPak: odd started white, and even started black but turned white when I pressed Right.

I will report this finding to the wiki as a way that the mouse (and other controllers that can't just be reread) can possibly be used with DPCM.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Glitch-free controller reads with DMC?

Post by lidnariq »

I just tested on a bare flashcart, and saw the same results as both of the above.
User avatar
pubby
Posts: 583
Joined: Thu Mar 31, 2016 11:15 am

Re: Glitch-free controller reads with DMC?

Post by pubby »

Source code for the ROM that works please. :)
It should be posted on the wiki.
thefox wrote:
Rahsennor wrote:Slapped together the simplest test I could come up with, and to my surprise, it works perfectly in Nestopia... but on odd cycles instead of even. None of the other emulators I tested emulate DMC conflicts at all.
Same result in Nintendulator.
So if I understand correctly, Nestopia and Nintendulator are handling this behavior incorrectly?
User avatar
FrankenGraphics
Formerly WheelInventor
Posts: 2064
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: Glitch-free controller reads with DMC?

Post by FrankenGraphics »

thefox wrote:
Just so this is clear to everybody:
- You have to have a standard controller plugged in, and shouldn't press Right.
- If screen stays black => no corruption
- If screen goes white => was corrupted
- (You might want to press Right in the end to force white to make sure that the ROM was properly loaded.)
oh! in that case, both were black until i pressed right on PAL through powerpak.
tepples wrote: I will report this finding to the wiki as a way that the mouse (and other controllers that can't just be reread) can possibly be used with DPCM.
Would it be sound to suggest it as an alternative in general, with a list of known benefits/applications following? As to not suggest it is for this only
Last edited by FrankenGraphics on Mon May 30, 2016 1:27 pm, edited 1 time in total.
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Glitch-free controller reads with DMC?

Post by lidnariq »

The 2A07 CPU in the PAL NES is known to have had the relevant bug fixed, so unfortunately tests on PAL will give you the results you found.
User avatar
rainwarrior
Posts: 8732
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Glitch-free controller reads with DMC?

Post by rainwarrior »

Ah, this finally explains how I once accidentally made a DPCM-using ROM that failed to compensate for the glitch, but somehow never had the problem!

I had never understood why the problem never showed up, and it always bothered me; I kept looking through my input logic to find something that would filter out a spurious right press, but couldn't find anything that would explain it. I never thought about OAM DMA vs DPCM even-cycle alignment.
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Glitch-free controller reads with DMC?

Post by tokumaru »

I have to say, this is a very ingenious solution to the dreaded DPCM controller glitch. Definitely better than reading the controller's twice, plus an occasional third time.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Glitch-free controller reads with DMC?

Post by tepples »

pubby wrote:Source code for the ROM that works please. :)
It should be posted on the wiki.
Is my description any good?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: Glitch-free controller reads with DMC?

Post by lidnariq »

The implementation in the NES files is so simple that you can just throw the NES file into da65 to get something correct out.
Rahsennor
Posts: 479
Joined: Thu Aug 20, 2015 3:09 am

Re: Glitch-free controller reads with DMC?

Post by Rahsennor »

Okay, here's an updated test with source! Please check that it still works, in case I've done something stupid like post the odd ROM instead of the even one.

The ROM should be run with a standard NES controller in both ports. The expected output is that the screen should remain black until right is pressed on either controller. If it fails, it should do so almost instantly, producing a white screen. PAL NES does not have DPCM conflicts so this test is not meaningful on PAL.

For quick reference, here's the important code:

Code: Select all

	lda #OAM
	sta $4014          ; ------ DMA ------
	ldx #1             ; even odd          <- strobe code must take an odd number of cycles total
	stx controller1    ; even odd even
	stx $4016          ; odd even odd even
	dex                ; odd even
	stx $4016          ; odd even odd even
 read_loop:
	lda $4017          ; odd even odd EVEN <- loop code must take an even number of cycles total
	and #3             ; odd even
	cmp #1             ; odd even
	rol controller2, x ; odd even odd even odd even (X = 0; waste 1 cycle and 0 bytes for alignment)
	lda $4016          ; odd even odd EVEN
	and #3             ; odd even
	cmp #1             ; odd even
	rol controller1    ; odd even odd even odd
	bcc read_loop      ; even odd [even]
Yes, it really is that simple. (Do note that I read the controller bits in big-endian order; most other implementations I've seen use little-endian. Conversion between the two should be a simple exercise.)
Attachments
dma_sync_test_v2.zip
(1.2 KiB) Downloaded 1592 times
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Glitch-free controller reads with DMC?

Post by thefox »

pubby wrote:
thefox wrote:
Rahsennor wrote:Slapped together the simplest test I could come up with, and to my surprise, it works perfectly in Nestopia... but on odd cycles instead of even. None of the other emulators I tested emulate DMC conflicts at all.
Same result in Nintendulator.
So if I understand correctly, Nestopia and Nintendulator are handling this behavior incorrectly?
Yes.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Quietust
Posts: 1920
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Glitch-free controller reads with DMC?

Post by Quietust »

Previously, Nintendulator didn't implement proper DMC cycle-stealing (it was hardcoded to 4 cycles and didn't care about alignment). It tries to do so now, though I'm pretty sure it isn't 100% correct (specifically for the case of DMC fetch happening one cycle after a $2004 write).
Quietust, QMT Productions
P.S. If you don't get this note, let me know and I'll write you another.
Drag
Posts: 1615
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: Glitch-free controller reads with DMC?

Post by Drag »

The only caveat is that this gives you less time for PPU updates since sprite DMA should ideally be done first thing, followed by all the PPU bus accessing code. On the other hand, wow, you don't need to reread and compare the controller bytes! That's really spiffy, especially if you need to read more than 8 bits from each port. Nice finding!
Post Reply