+  RHDN Forum Archive
|-+  Romhacking
| |-+  ROM Hacking Discussion
| | |-+  DMA/HDMA Clarification
Pages: [1]
Author Topic: DMA/HDMA Clarification  (Read 2 times)
Maxxus
Guest
« on: September 04, 2008, 12:49:48 pm »

My first post here, so first off I would like to than Anomie for the wonderful docs, they have been immensely helpful.  And I would like to thank byuu for all his work on bsnes, which is also very useful.  I just have a couple (hopefully easy) questions to clarify my understanding of the SNES's behavior regarding DMA and HDMA.

First, and I only ask because it's not explicitly stated anywhere, after a write to $420b, let's say 0x01, the CPU is halted and DMA takes place.  Once the DMA is finished does bit 0 of $420b get reset or does it remain set?  If it remains set do you need to reset it before you can start the DMA again or can you just write another 0x01?

Second, and I'm sure this is covered somewhere, but I just want to confirm my assumptions, if both DMA and HDMA are active, HDMA transfers data on each H-Blank until V-Blank then DMA transfers all data during V-Blank and at the start of the next update all DMAs and HDMAs are complete and all bits are reset in $420b.
MathOnNapkins
Guest
« Reply #1 on: September 04, 2008, 01:34:29 pm »

I don't know if the bit gets reset in $420B after the transfer is over but it's pretty irrelevant, I think, since the register is write only. (I guess a read returns open bus data but I didn't look it up or anything). You will need to write to the register again to initiate additional transfers.

HDMA doesn't transfer data immediately. There's some wait and setup time during each hblank where HDMA has to figure out what it's doing, and that will depend upon how many channels are being used, and whether direct or indirect mode is being used for each channel. Indirect mode requires more time to get setup. Your assumption that DMA only activates during Vblank is not necessarily true, though due to a hardware bug in revision 1 of the SNES, there can be a conflict between HDMA and DMA if both are being used concurrently. Naturally the conflict would occur outside of Vblank because HDMA doesn't occur during Vblank. Thus, it's probably best unless you absolutely have to to keep your DMA transfers inside VBlank when also using HDMA. Most things that DMA is good for should be done during Vblank anyway (updating graphics, palettes and OAM, mainly). The point is that a DMA transfer begins as soon as you write to $420B, whereas HDMA should be configured to activate ahead of time (do it during Vblank when HDMA isn't running) and consists of one or more short transfers occuring each HBlank.

During each Vblank, you have to reenable HDMA ($420C) for the channels you want to be active on the next frame. This is because HDMA is deactivated for all channels at the start of Vblank (think of it as $420C being set to 0x00).

Also, it's very possible to go past the end of Vblank with several large DMA transfers or time consuming code within an NMI routine. It's not like the system automatically makes everything all better when it comes out of Vblank, so keep that in mind. You only have so much time during Vblank, you have to make efficient use of that time.
« Last Edit: September 04, 2008, 01:47:29 pm by MathOnNapkins »
Maxxus
Guest
« Reply #2 on: September 04, 2008, 02:00:13 pm »

Thanks that helps out alot.  I forgot about the register being write only, so it doen't really matter what the bit is after DMA completion.

All the documentation I've read says that HDMA take priority over DMA, but if DMA starts immeadiately after a write to $420b and freeze the CPU how can a HDMA and a DMA occur at the same time.  Does the PPU countinue to update the screen when the CPU is frozen?
Disch
Guest
« Reply #3 on: September 04, 2008, 03:18:13 pm »

Quote from: Maxxus on September 04, 2008, 02:00:13 pm
All the documentation I've read says that HDMA take priority over DMA, but if DMA starts immeadiately after a write to $420b and freeze the CPU how can a HDMA and a DMA occur at the same time.  Does the PPU countinue to update the screen when the CPU is frozen?

Yes.  What's basically happening is that the CPU is busy performing the DMA so it doesn't have time to execute instructions.  Since the PPU operates independently of the CPU and DMA, it continues to move forward during this time.  As does everything else on the system.
creaothceann
Guest
« Reply #4 on: September 05, 2008, 07:41:07 am »

Quote from: Disch on September 04, 2008, 03:18:13 pm
What's basically happening is that the CPU is busy performing the DMA

So it's actually not really DMA. Cheesy
Maxxus
Guest
« Reply #5 on: September 05, 2008, 09:06:16 am »

Okay, so HDMA only transfers on H-Blank, so does it only freeze the CPU on H-Blank?  Can other instructions be executed not during H-Blank?  And if so can those instructions executed affect the HDMA by changing the registers?
Disch
Guest
« Reply #6 on: September 05, 2008, 09:18:50 am »

Quote from: creaothceann on September 05, 2008, 07:41:07 am
So it's actually not really DMA. Cheesy

Let me rephrase.  The CPU bus is busy performing the DMA, so the CPU cannot use it to fetch opcodes.  So yes it really is DMA, but it keeps the bus occupied so the CPU must wait for the bus to open up again before continuing.

Quote from: Maxxus on September 05, 2008, 09:06:16 am
Okay, so HDMA only transfers on H-Blank, so does it only freeze the CPU on H-Blank?

Yes.  DMA/HDMA only halts the CPU while it's transfering bytes.  When no bytes are being transferred, the CPU is free to run normally.  Since HDMA only transfers bytes during HBlank, the CPU runs just fine outside HBlank.

Quote
Can other instructions be executed not during H-Blank?  And if so can those instructions executed affect the HDMA by changing the registers?

Yes to both of those.
Maxxus
Guest
« Reply #7 on: September 05, 2008, 09:50:41 am »

Is there any easy way to tell how many instructions/cycles will be executed between each H-Blank?
byuu
Guest
« Reply #8 on: September 05, 2008, 10:01:49 am »

1364-1096=268 clock cycles. /24 [average opcode width] = ~11.17 instructions can execute during hblank.

At 8 clocks/cycle, triggering at H=1104, HDMA has enough time to transfer all the data it needs. Max is 8 channels with 4 bytes each, 32*8=256. It may eat more time after fetching line counters and/or indirect addresses.

Maxxus
Guest
« Reply #9 on: September 05, 2008, 10:10:19 am »

I was meaning not during H-Blank but in between them.  So if i schedule an HDMA how many instructions/cycles can I expect to execute per scanline before the H-Blank, so I know how much time I have to manipulate the HDMA registers, or whatever else I want to do.
Maxxus
Guest
« Reply #10 on: September 05, 2008, 10:33:04 am »

Okay.  Looking at Anomie's timing doc I see that there are 1364 cycles per scan line and from byuu's post that 1096 is where H-Blank begins.  Anomie's doc also talks about a 40 cycle pause that happens every ~536 cycles.  So if I'm getting this all right, I have 1096 - 40 = 1056 cycles to execute before each H-Blank.  Correct?
byuu
Guest
« Reply #11 on: September 05, 2008, 12:46:26 pm »

It depends on how much data you are transferring. HDMA doesn't consume 100% of Hblank. It can be very short, needing only ~40 cycles, or very long, needing ~400 cycles. However much it consumes sans 1324 is how much time you have before the next HDMA transfer.

Also, DRAM refresh occurs at H=530-538. It varies based on the CPU revision. But it only happens once per scanline.

Pages: [1]  


Powered by SMF 1.1.4 | SMF © 2006-2007, Simple Machines LLC