Does DMC playback affect CPU
Moderator: Moderators
Does DMC playback affect CPU
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?
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
Re: Does DMC playback affect CPU
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.dougeff wrote:Am I understanding this correctly?
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi
- Jarhmander
- Formerly ~J-@D!~
- Posts: 569
- Joined: Sun Mar 12, 2006 12:36 am
- Location: Rive nord de Montréal
Re: Does DMC playback affect CPU
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)))
- rainwarrior
- Posts: 8732
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Does DMC playback affect CPU
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.
Re: Does DMC playback affect CPU
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).
(ROM removed, see official thread for link).
- Attachments
-
- Rock5.png (1.64 KiB) Viewed 3315 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
Re: Does DMC playback affect CPU
This is the gist of the code, btw...
There's a little more to it, but this is the basics.
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
nesdoug.com -- blog/tutorial on programming for the NES
Re: Does DMC playback affect CPU
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.
Re: Does DMC playback affect CPU
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
Re: Does DMC playback affect CPU
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.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
- rainwarrior
- Posts: 8732
- Joined: Sun Jan 22, 2012 12:03 pm
- Location: Canada
- Contact:
Re: Does DMC playback affect CPU
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.Memblers wrote:Knowing the sample rate, and when the sample begins/ends, should be enough to predict when the CPU will stall.
Re: Does DMC playback affect CPU
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!