Some DMA questions, Exodus emulator hangs

For anything related to VDP (plane, color, sprite, tiles)

Moderators: BigEvilCorporation, Mask of Destiny

Post Reply
Sjoerd
Newbie
Posts: 2
Joined: Tue Jan 31, 2023 9:45 pm

Some DMA questions, Exodus emulator hangs

Post by Sjoerd » Fri Feb 17, 2023 12:53 pm

I'm making a game for the Sega Mega Drive and I have some questions about the DMA functionalities.

When I try the game on the Exodus emulator, it now and then hangs. Sometimes it takes a couple of minutes, sometimes it doesn't happen at all. And always on the 'add d6, d2' instruction, the one just after the start of the DMA command (see below).

This is how the routine looks:

I build the screen in RAM from address screenbuf + 256, and copy it row by row to plane A, which starts at address C000. I don't write 94 (high byte length) since it is 0 from the last DMA. I don't rewrite the RAM source address after the first time because the registers already contain the right values.

Code: Select all

  move.l #9700h | >>(screenbuf >> 1) & 127 :: 8f02, (a4)        ; these values are leftover from another DMA, so written before that DMA

  move.l #93h : 38 :: 4000h | 0c000h & 3fffh, d2                ; length 38 en low part dest
  move.w #80h | (0c000h & 0c000h) >> 14, (a6)                   ; high part destination in (a6)

  move.l #95009600h | (<((screenbuf + 256) >> 1) :: 0) | >((screenbuf + 256) >> 1), (a4)      ; low and mid source

  move.l d2, (a4)     ; length and low part address      \   repeat 29 times
  move.w d4, (a5)     ; z80 pause
  move.w d5, (a5)     ; z80 resume
  move.w (a6), (a4)   ; high part address and DMA command
  add.w d6, d2        ; next row (d6 = 128)              /

  move.l d2, (a4)     ; line 30
  move.w d4, (a5)
  move.w d5, (a5)
  move.w (a6), (a4)
The questions I have (for now) are:
- Is the problem I see just something that is related to the Exodus emulator? I didn't encounter it yet on my Mega Drive model 2, or with other emulators. But maybe it does happen on other mega drive or genesis models and Exodus is just a better emulator than others?
- The sega manual says I have to disable DMA in VDP register 1 when it's finished. Is there any software that does this?
- Is there a order in which the source registers should be written? Probably not because I've seen 95, 96, 97 and 96, 95, 97. Never 97 first though.
- Is it a problem I don't write the high byte of the length?
- Is it a problem I don't write the highest byte of the source registers since those don't change? This byte does contain part of the DMA command after all.

All examples I could find just write all length and source registers, but this doesn't seem to be necessary apart from the problem with Exodus.

Mask of Destiny
Very interested
Posts: 615
Joined: Thu Nov 30, 2006 6:30 am

Re: Some DMA questions, Exodus emulator hangs

Post by Mask of Destiny » Sun Feb 19, 2023 9:37 pm

Sjoerd wrote:
Fri Feb 17, 2023 12:53 pm
- Is the problem I see just something that is related to the Exodus emulator? I didn't encounter it yet on my Mega Drive model 2, or with other emulators. But maybe it does happen on other mega drive or genesis models and Exodus is just a better emulator than others?
In general, if something doesn't work on an emulator and does work on hardware it's probably an emulator bug. The main exception tends to be when you accidentally forget to initialize something. When people test on hardware, it's often on a flash cart with some kind of menu that does its own hardware initialization. This can get you into trouble if you accidentally depend on that initialization.

There are some model specific quirks, but the code you posted doesn't seem like it would run into any of them.
Sjoerd wrote:
Fri Feb 17, 2023 12:53 pm
- The sega manual says I have to disable DMA in VDP register 1 when it's finished. Is there any software that does this?
Some commercial games do this, but definitely not all of them. Galaxy Force II is dependent on the DMA disable bit working, but only because it has buggy code and will accidentally trigger DMA without it.
Sjoerd wrote:
Fri Feb 17, 2023 12:53 pm
- Is there a order in which the source registers should be written? Probably not because I've seen 95, 96, 97 and 96, 95, 97. Never 97 first though.
Register 97 (or $17 I guess since the MSB is really just indicating a register write) is special, but I've seen no indication that order is important for any of the others. If you write to a value to 97 that indicates a M68K->VDP transfer and then DON'T perform a DMA and instead write a VDP command word for a normal access you will lock up the machine. The best theory on what's happening here is that the write to register 97 "primes" the 68K bus request for DMA so that it can occur immediately on the command word write and since the DMA doesn't actually get triggered the bus is never released.

That said, you still don't have to write reg 97 last, just need to make sure you follow through on DMA when you do write it.
Sjoerd wrote:
Fri Feb 17, 2023 12:53 pm
- Is it a problem I don't write the high byte of the length?
I'm not sure if it has a well-defined power-on state or not, so you should probably ensure it's written at least once. Shouldn't be a problem on subsequent DMA transfers though as long as you're expecting this to be 0 after a DMA is complete.
Sjoerd wrote:
Fri Feb 17, 2023 12:53 pm
- Is it a problem I don't write the highest byte of the source registers since those don't change? This byte does contain part of the DMA command after all.
You can do repeated DMA without changing any of the source registers. This is one of the complications of the leading theory for the weird lock-up behavior of register 97. If the write to register 97 actually does something useful, why does DMA seem to work fine without writing this register again?

Anyway, I have some code that does repeated DMA transfers to CRAM during blanking and it doesn't change any of the DMA source registers outside of vblank unless it needs to cross a 128KB boundary. It just updates the length registers between hblanks. This code works fine on hardware and has been tested on multiple models.

Sjoerd
Newbie
Posts: 2
Joined: Tue Jan 31, 2023 9:45 pm

Re: Some DMA questions, Exodus emulator hangs

Post by Sjoerd » Tue Feb 21, 2023 10:19 am

Thank you for the detailed answers. I'll just go on and keep this code unchanged.

Post Reply