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.
| Address | Name | For DMA | For HDMA |
$43n0 | DMAP | how it copies (below) | the same |
$43n1 | BBAD | the address on the B-bus. It wraps from $21FF to $2100 | the same |
$43n2, $43n3 | A1T | the address on the A-bus, which moves with each byte | where the table starts. It stays |
$43n4 | A1B | its bank, which never changes | the bank of the table |
$43n5, $43n6 | DAS | how many bytes are left. 0 is 65,536 | with an indirect table, the address of the data |
$43n7 | DASB | | with an indirect table, the bank of the data |
$43n8, $43n9 | A2A | | the next byte of the table |
$43nA | NTRL | | the line counter |
$43nB, $43nF | | one byte that is the same at both. Nothing uses it | |
$43nC to $43nE are open bus.
DMAP:
| Bits | |
| 7 | the direction: 0 is from the A-bus to the B-bus, and 1 the other way |
| 6 | HDMA only: the table is indirect |
| 5 | can be written and read, and does nothing |
| 4-3 | DMA only: 0 moves the A-bus address up, 2 moves it down, and 1 and 3 leave it |
| 2-0 | the pattern on the B-bus |
A unit is as long as its pattern, which says what is added to BBAD for each byte:
| Pattern | Bytes in a unit | Added to BBAD |
| 0 | 1 | 0 |
| 1 | 2 | 0, 1 |
| 2, 6 | 2 | 0, 0 |
| 3, 7 | 4 | 0, 0, 1, 1 |
| 4 | 4 | 0, 1, 2, 3 |
| 5 | 4 | 0, 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:
- The processor does one more cycle after the write. This is normally the read of the next opcode.
- 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.)
- 8 clocks for the whole DMA.
- For each channel, 8 clocks, and then 8 for each byte, whatever is at both ends. Slow memory takes
no longer here.
- 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 2 | Steps 3 and 4 | So far | Wait in step 5 | All of it |
| 2 | 8 + 8 + 24 = 40 | 42 | 6, as 42 is a multiple of 6 | 48 |
| 4 | 40 | 44 | 4 | 48 |
| 6 | 40 | 46 | 2 | 48 |
| 8 | 40 | 48 | 6 | 54 |
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 byte | What it means |
$00 | the end, until the next frame |
$01-$80 | send one unit, and then wait. The next entry comes after that many lines |
$81-$FF | send 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:
- 18 clocks for all of them together.
- Each channel that is set to send sends a unit, from channel 0 to 7. Each byte takes 8 clocks.
- 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 on | 18, and 8 for each channel, or 24 with an indirect table |
| A line, if any channel is not at its end | 18, 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 bytes | 18 + 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:
| Address | Bytes | |
$7E:2000 | 03 40 C0 | send once, and then wait for 3 lines. Left is $40, and right $C0 |
$7E:2003 | 82 48 B8 50 B0 | send in each of 2 lines |
$7E:2008 | 00 | the 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:
- 18 clocks pass, until clock 46.
- A2A becomes
$2000. At clock 54, the byte $03 there goes to NTRL, and A2A is $2001. Channel 3 is set
to send.
- 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:
- 18 clocks pass, until clock 1,136.
- At clock 1,144, the byte
$40 goes from $7E:2001 to $2126.
- At clock 1,152, the byte
$C0 goes from $7E:2002 to $2127. A2A is $2003.
- 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.
- 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:
| Line | It sends | NTRL before and after | A2A after | Clocks | What the next line shows |
| 0 | $40, $C0 | $03 → $02 | $2003 | 18 + 16 + 8 = 42 | line 1: from $40 to $C0 |
| 1 | | $02 → $01 | $2003 | 18 + 8 = 26 | line 2: the same |
| 2 | | $01 → $00, so the next entry is read: $82 | $2004 | 26 | line 3: the same |
| 3 | $48, $B8 | $82 → $81 | $2006 | 42 | line 4: from $48 to $B8 |
| 4 | $50, $B0 | $81 → $80, so the next entry is read: $00 | $2009 | 42 | line 5: from $50 to $B0 |
| 5 and later | | stays $00 | $2009 | 0 | the 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.