emuforge

DMA and HDMA

DMA copies memory while the main processor is stopped. It is how games fill the picture's memories. HDMA sends a few bytes in each line, before the next one is drawn, and so changes colours, scrolls or windows from line to line. Both use the same eight channels and the same registers. They are in crates/snesemu-core/src/dma.rs.

What is here rests on the Super Famicom Development Wiki's "DMA and HDMA" and "Timing" (anomie's documents), the SNESdev Wiki's "DMA registers" and fullsnes (credits). What these do not say is a guess, and listed below.

Two buses

A byte goes from one bus to the other, and is on both at the same time:

  • The A-bus has all 24 bits of address: the cartridge, main memory and the processor's side.
  • The B-bus has 8 bits, for $2100 to $21FF: the picture, the ports to the sound side, and main memory through $2180.

DMA cannot copy from main memory to main memory. Memory would be on both buses. If $2180 is at one end and main memory at the other, nothing is written to $2180. In the other direction, memory gets what was on the bus. The address of $2180 does not move in either case.

The A-bus does not reach everything. In banks $00-$3F and $80-$BF, it does not reach $2100-$21FF, $4000-$420F and $4300-$437F. Documents differ on the processor's registers. This is what fullsnes measured for reads, and here it is the same for writes. Such a read gives open bus, and such a write is lost.

The registers of a channel

Channel n has $43n0 to $43nF. All can be read and written. When the console is turned on, all of their bits are 1. A reset leaves them as they are.

AddressNameFor DMAFor HDMA
$43n0DMAPhow it copies (below)the same
$43n1BBADthe address on the B-bus. It wraps from $21FF to $2100the same
$43n2, $43n3A1Tthe address on the A-bus, which moves with each bytewhere the table starts. It stays
$43n4A1Bits bank, which never changesthe bank of the table
$43n5, $43n6DAShow many bytes are left. 0 is 65,536with an indirect table, the address of the data
$43n7DASBwith an indirect table, the bank of the data
$43n8, $43n9A2Athe next byte of the table
$43nANTRLthe line counter
$43nB, $43nFone byte that is the same at both. Nothing uses it

$43nC to $43nE are open bus.

DMAP:

Bits
7the direction: 0 is from the A-bus to the B-bus, and 1 the other way
6HDMA only: the table is indirect
5can be written and read, and does nothing
4-3DMA only: 0 moves the A-bus address up, 2 moves it down, and 1 and 3 leave it
2-0the pattern on the B-bus

A unit is as long as its pattern, which says what is added to BBAD for each byte:

PatternBytes in a unitAdded to BBAD
010
120, 1
2, 620, 0
3, 740, 0, 1, 1
440, 1, 2, 3
540, 1, 0, 1

For example, pattern 1 with BBAD $18 writes words to the picture's memory, through $2118 and $2119. DMA repeats the pattern until no byte is left, which can be in the middle of it.

DMA

A write to $420B starts the channels whose bits are set, and they run one after the other, from 0 to 7:

  1. The processor does one more cycle after the write. This is normally the read of the next opcode.
  2. It stops, and the DMA waits for the next clock that is a multiple of 8, counted from when the console was turned on. This takes 1 to 8 clocks, and is never 0: where it is one already, it waits 8. (Everything in the console takes an even number of clocks, so it is 2, 4, 6 or 8.)
  3. 8 clocks for the whole DMA.
  4. For each channel, 8 clocks, and then 8 for each byte, whatever is at both ends. Slow memory takes no longer here.
  5. It waits for the processor's rhythm. The time since the processor stopped has to become a multiple of its next cycle (6, 8 or 12 clocks). This wait is never 0 either.

Afterwards, DAS is 0, A1T has moved by the number of bytes (unless it stays), and A1B is what it was.

For example, one channel copies 3 bytes, and the processor's next cycle takes 6 clocks. The documents have these four cases, and a test has them too:

Wait in step 2Steps 3 and 4So farWait in step 5All of it
28 + 8 + 24 = 40426, as 42 is a multiple of 648
44044448
64046248
84048654

Before each byte, refresh and HDMA can come in between. They add their own time. If HDMA uses the same channel, that channel's DMA ends, and DAS is then not 0. The others go on. NMIs and IRQs wait, as the processor is stopped.

HDMA

A write to $420C (HDMAEN) turns channels on. They stay on until it is written again. Each one has a table, which it reads from the start in every frame. Its entries begin with a byte:

First byteWhat it means
$00the end, until the next frame
$01-$80send one unit, and then wait. The next entry comes after that many lines
$81-$FFsend a unit in each line, for this number minus $80 lines

In a direct table, the data comes after this byte: one unit, or as many units as there are lines. In an indirect table (bit 6 of DMAP), a 16-bit address comes after it, and the data is there, in the bank of DASB. Addresses only go up, and stay in their bank. Bits 4 and 3 of DMAP do not matter here. Bit 7 does: with it, HDMA reads from the B-bus, and writes to where its data would have been.

At clock 24 of line 0, every channel that is on begins its table: A2A becomes A1T, and the first byte of the first entry goes to NTRL. If the table is indirect, its address goes to DAS. Each channel is then set to send.

At clock 1,112 of each line from 0 to the last one of the picture, the processor's cycle ends. Then this happens for the channels that are on and not at their end:

  1. 18 clocks for all of them together.
  2. Each channel that is set to send sends a unit, from channel 0 to 7. Each byte takes 8 clocks.
  3. Then each channel goes on in its table, again from 0 to 7. NTRL goes down by 1. Its new bit 7 says if the channel sends in the next line. If its lower 7 bits are now 0, the next entry's first byte goes to NTRL, and the channel is set to send. If that byte is $00, it is at its end. This takes 8 clocks. With an indirect table, the new address takes 16 more. If the lower 7 bits are not 0, the channel reads the table all the same, for 8 clocks, and does not use it.
Clocks
The start of the tables, if any channel is on18, and 8 for each channel, or 24 with an indirect table
A line, if any channel is not at its end18, and for each of these channels 8, 8 for each byte that it sends, and 16 for a new indirect address
The most in one line, with eight indirect channels of 4 bytes18 + 8 × (8 + 32 + 16) = 466

When the last of the active channels comes to the $00 of an indirect table, it reads only one byte of the address. That byte becomes the upper one, and the lower one is 0. This takes 8 clocks less.

Line 0 is not shown, but HDMA sends there too. So what is sent in one line is for the next one. There is no HDMA in V-blank. The game can turn it off with HDMAEN in any line, and a channel that is turned off and on again goes on where it was. One that is at its end stays there until the next frame.

For example: a window that changes, step by step

A game wants a window that is wide at the top and then gets narrower. Window 1's left and right edges are $2126 and $2127. It uses channel 3 with a direct table at $7E:2000:

AddressBytes
$7E:200003 40 C0send once, and then wait for 3 lines. Left is $40, and right $C0
$7E:200382 48 B8 50 B0send in each of 2 lines
$7E:200800the end

In V-blank, it writes $01 to $4330 (pattern 1, direct), $26 to $4331, $2000 to $4332 and $4333, $7E to $4334, and then $08 to $420C. No other channel is on.

The table begins, in line 0. A cycle of the processor that began before clock 24 ends, for example at clock 28. Then:

  1. 18 clocks pass, until clock 46.
  2. A2A becomes $2000. At clock 54, the byte $03 there goes to NTRL, and A2A is $2001. Channel 3 is set to send.
  3. Line 0 goes on at clock 54, after 26 clocks, with the processor's next cycle.

The first line, which is line 0. The processor's cycle ends at clock 1,118, for example. Then:

  1. 18 clocks pass, until clock 1,136.
  2. At clock 1,144, the byte $40 goes from $7E:2001 to $2126.
  3. At clock 1,152, the byte $C0 goes from $7E:2002 to $2127. A2A is $2003.
  4. NTRL goes from $03 to $02. Its bit 7 is 0, so nothing is sent in the next line. Its lower bits are not 0, so this takes 8 clocks more.
  5. The processor goes on at clock 1,160. HDMA took 42 clocks.

Line 0 is not shown. The last dot of any line begins at clock 1,108, so these writes change no dot that has begun. They are there for line 1, whose first dot comes out at clock 88. When HDMA changes $2126, the picture first catches up, so that all earlier lines are drawn with the old window.

All lines:

LineIt sendsNTRL before and afterA2A afterClocksWhat the next line shows
0$40, $C0$03 → $02$200318 + 16 + 8 = 42line 1: from $40 to $C0
1$02 → $01$200318 + 8 = 26line 2: the same
2$01 → $00, so the next entry is read: $82$200426line 3: the same
3$48, $B8$82 → $81$200642line 4: from $48 to $B8
4$50, $B0$81 → $80, so the next entry is read: $00$200942line 5: from $50 to $B0
5 and laterstays $00$20090the same. Nothing changes it any more

In line 2, the entry is read that line 3 sends. In line 4, the lower bits of $80 are 0, so the next byte is read too: that is the end. In the next frame, it all starts again at $7E:2000.

What is a guess

  • HDMA takes 18 clocks besides its channels and bytes. The documents say "about 18". That is likely because it lines itself up as a DMA does. Here it does not, so it is always 18.
  • In a line, all channels send first, and then all go on in their tables. The documents do not say this.
  • A channel that is turned on in the middle of a frame sends nothing until its table says so. Documents differ on this. Its table does not begin by itself, so the game has to write A2A and NTRL.
  • HDMA or refresh in the middle of a DMA adds its own clocks, and nothing else.
  • Open bus on the A-bus is the last byte that was on the bus.
  • That writes reach the same registers on the A-bus as reads do. Only the reads were measured.

No fault of any version of the chip is emulated.

Tests with these times and tables are in crates/snesemu-core/src/bus/tests.rs.

From commit 019fda0af379. Edit this page