Does DMC playback affect CPU

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

Post Reply
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Does DMC playback affect CPU

Post by dougeff »

I was playing around with some timed code, that is acting erratically... which didn't make any sense, until I turned off music updates...fixed it..

Then it occurred to me that maybe DMC playback is the issue, because the screen also shakes (I'm doing midscreen PPU writes) exactly when the DMC sample plays.

Then I looked at the wiki, which says (for DMC)...

"The CPU is stalled for up to 4 CPU cycles" when its buffer is empty.

Am I understanding this correctly? Or am I barking up the wrong tree?
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
thefox
Posts: 3134
Joined: Mon Jan 03, 2005 10:36 am
Location: 🇫🇮
Contact:

Re: Does DMC playback affect CPU

Post by thefox »

dougeff wrote:Am I understanding this correctly?
Yes, the DMC unit will stall the CPU every now and then to fetch a new sample byte. How often, depends on the DMC rate (frequency) of the sample that is being played.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
User avatar
Jarhmander
Formerly ~J-@D!~
Posts: 568
Joined: Sun Mar 12, 2006 12:36 am
Location: Rive nord de Montréal

Re: Does DMC playback affect CPU

Post by Jarhmander »

DMC DMA can definitely affect the timing of cycle-counted code, because it "steals" cycles from the CPU. You'll either have to use timer or scanline IRQs, or not use DMC at all.
((λ (x) (x x)) (λ (x) (x x)))
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Does DMC playback affect CPU

Post by rainwarrior »

Just for completeness, a list of all ways it can affect the CPU:
  • 1. Whenever the DMC reads a new byte the CPU halts for a few cycles.
  • 2. If a DMC byte fetch coincides with a read from $4016/4017/2007 (controllers or PPU), that read will be corrupted.
  • 3. The DMC can produce an IRQ.
  • 4. The DMC bit in $4015 can be polled to find out when a sample stops.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Does DMC playback affect CPU

Post by dougeff »

OK, I'm trying to make this game a little better / more interesting...added cool special effect. Let me know what you think. Thanks.

(ROM removed, see official thread for link).
Attachments
Rock5.png
Rock5.png (1.64 KiB) Viewed 3310 times
Last edited by dougeff on Wed Mar 01, 2017 1:20 am, edited 1 time in total.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Does DMC playback affect CPU

Post by dougeff »

This is the gist of the code, btw...

Code: Select all

	ldy scanlineToRepeat
	ldx scanlineToRepeat
	...
-
	lda #0
	sta $2006
	sty $2005 ;y
	sta $2005 ;x = 0
	tya
	and #$f8
	asl a
	asl a
	sta $2006
	jsr Timed100 ;waits a bit
	dex ;2
	bne -

	...
	dec scanlineToRepeat
There's a little more to it, but this is the basics.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
tokumaru
Posts: 12427
Joined: Sat Feb 12, 2005 9:43 pm
Location: Rio de Janeiro - Brazil

Re: Does DMC playback affect CPU

Post by tokumaru »

Even if there's nothing wrong with the code itself, as long as you're playing DMC samples, the CPU will occasionally pause in order to fetch sample bytes from ROM, screwing up the timing of any timed code you might be running. AFAIK, there's no way to compensate for that manually, so you need more reliable ways to keep track of time (e.g. mapper IRQs). I would just think of other effects that don't need such timing precision.
User avatar
dougeff
Posts: 3078
Joined: Fri May 08, 2015 7:17 pm

Re: Does DMC playback affect CPU

Post by dougeff »

Oh, I gutted the DMC off that song, and won't have any on the final song.
nesdoug.com -- blog/tutorial on programming for the NES
User avatar
Memblers
Site Admin
Posts: 4044
Joined: Mon Sep 20, 2004 6:04 am
Location: Indianapolis
Contact:

Re: Does DMC playback affect CPU

Post by Memblers »

tokumaru wrote:Even if there's nothing wrong with the code itself, as long as you're playing DMC samples, the CPU will occasionally pause in order to fetch sample bytes from ROM, screwing up the timing of any timed code you might be running. AFAIK, there's no way to compensate for that manually
Knowing the sample rate, and when the sample begins/ends, should be enough to predict when the CPU will stall. You would need different timing for every sample rate (and for once we can be grateful that there's only 16). I don't know how accurate it could be, but surely it's enough to stop the timing error from accumulating over the course of the frame. I think the Years Behind demo might have done something like this.
User avatar
rainwarrior
Posts: 8731
Joined: Sun Jan 22, 2012 12:03 pm
Location: Canada
Contact:

Re: Does DMC playback affect CPU

Post by rainwarrior »

Memblers wrote:Knowing the sample rate, and when the sample begins/ends, should be enough to predict when the CPU will stall.
It was my understanding that the stall is a variable number of cycles depending on where it falls, as per Blargg's post about it. I think there's also a lot of difficulty getting it to start on a specific cycle too.
User avatar
Dwedit
Posts: 4921
Joined: Fri Nov 19, 2004 7:35 pm
Contact:

Re: Does DMC playback affect CPU

Post by Dwedit »

As far as I know, the DMC channel doesn't even have a consistent startup state for its timers, so it can even vary between power ons. Try playing some Blades of Steel and watching the demo.
Here come the fortune cookies! Here come the fortune cookies! They're wearing paper hats!
Post Reply