Glitch-free controller reads with DMC?

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

Moderator: Moderators

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

Re: Glitch-free controller reads with DMC?

Post by thefox » Mon May 30, 2016 9:54 am

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: 1500
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Glitch-free controller reads with DMC?

Post by Quietust » Mon May 30, 2016 10:26 am

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: 21809
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Glitch-free controller reads with DMC?

Post by tepples » Mon May 30, 2016 10:31 am

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: 8922
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Glitch-free controller reads with DMC?

Post by lidnariq » Mon May 30, 2016 11:18 am

I just tested on a bare flashcart, and saw the same results as both of the above.

User avatar
pubby
Posts: 549
Joined: Thu Mar 31, 2016 11:15 am

Re: Glitch-free controller reads with DMC?

Post by pubby » Mon May 30, 2016 1:02 pm

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: 2032
Joined: Thu Apr 14, 2016 2:55 am
Location: Gothenburg, Sweden
Contact:

Re: Glitch-free controller reads with DMC?

Post by FrankenGraphics » Mon May 30, 2016 1:14 pm

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.
http://www.frankengraphics.com - personal NES blog

lidnariq
Posts: 8922
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Glitch-free controller reads with DMC?

Post by lidnariq » Mon May 30, 2016 1:16 pm

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: 7703
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Glitch-free controller reads with DMC?

Post by rainwarrior » Mon May 30, 2016 1:21 pm

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: 11500
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Glitch-free controller reads with DMC?

Post by tokumaru » Mon May 30, 2016 1:32 pm

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: 21809
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Glitch-free controller reads with DMC?

Post by tepples » Mon May 30, 2016 1:54 pm

pubby wrote:Source code for the ROM that works please. :)
It should be posted on the wiki.
Is my description any good?

lidnariq
Posts: 8922
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Glitch-free controller reads with DMC?

Post by lidnariq » Mon May 30, 2016 2:05 pm

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: 475
Joined: Thu Aug 20, 2015 3:09 am

Re: Glitch-free controller reads with DMC?

Post by Rahsennor » Mon May 30, 2016 5:44 pm

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 1012 times

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

Re: Glitch-free controller reads with DMC?

Post by thefox » Mon May 30, 2016 8:27 pm

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: 1500
Joined: Sun Sep 19, 2004 10:59 pm
Contact:

Re: Glitch-free controller reads with DMC?

Post by Quietust » Tue May 31, 2016 8:08 am

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: 1290
Joined: Mon Sep 27, 2004 2:57 pm
Contact:

Re: Glitch-free controller reads with DMC?

Post by Drag » Tue May 31, 2016 12:00 pm

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