It is currently Sat Dec 16, 2017 11:25 pm

All times are UTC - 7 hours





Post new topic Reply to topic  [ 11 posts ] 
Author Message
PostPosted: Sun Feb 26, 2017 7:41 am 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1869
Location: DIGDUG
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


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 7:46 am 
Offline
User avatar

Joined: Mon Jan 03, 2005 10:36 am
Posts: 2983
Location: Tampere, Finland
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: kkfos.aspekt.fi


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 8:11 am 
Offline
Formerly ~J-@D!~
User avatar

Joined: Sun Mar 12, 2006 12:36 am
Posts: 445
Location: Rive nord de Montréal
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.


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 9:30 am 
Offline
User avatar

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


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 12:41 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1869
Location: DIGDUG
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 1083 times ]

_________________
nesdoug.com -- blog/tutorial on programming for the NES


Last edited by dougeff on Wed Mar 01, 2017 1:20 am, edited 1 time in total.
Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 12:50 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1869
Location: DIGDUG
This is the gist of the code, btw...

Code:
   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


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 3:33 pm 
Offline
User avatar

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


Top
 Profile  
 
PostPosted: Sun Feb 26, 2017 4:05 pm 
Offline
User avatar

Joined: Fri May 08, 2015 7:17 pm
Posts: 1869
Location: DIGDUG
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


Top
 Profile  
 
PostPosted: Mon Feb 27, 2017 2:27 am 
Offline
Site Admin
User avatar

Joined: Mon Sep 20, 2004 6:04 am
Posts: 3488
Location: Indianapolis
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.


Top
 Profile  
 
PostPosted: Mon Feb 27, 2017 11:02 am 
Offline
User avatar

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


Top
 Profile  
 
PostPosted: Mon Feb 27, 2017 12:06 pm 
Offline
User avatar

Joined: Fri Nov 19, 2004 7:35 pm
Posts: 3969
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 11 posts ] 

All times are UTC - 7 hours


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group