Hardware/emulator quirks

Discussion in 'Discussion & Q&A' started by Clownacy, Sep 10, 2015.

  1. Natsumi

    Natsumi Phoenix egg Member

    Joined:
    Oct 7, 2011
    Messages:
    679
    Location:
    Long and dangerous river
    This is something relating to Clownacy's post earlier: As the VDP manual claims, DMA may fail if two conditions aren't met:
    • The destination address write must be a word write
    • The write must be from work RAM (no d0 move to VDP directly!)
    [​IMG]

    As Clownacy's post suggests, he found an occasional crash with the first condition not met. Well, it seems I have found a crash where the second condition was not met (the first wasn't initially either, but it got ruled out pretty quickly as not causing the bug). For some context: While working for a screen, we noticed something strange happening; random tiles appearing and the game eventually freezing, sometimes with the Z80 continuing to work, sometimes not. This screen used a lot of DMA's to update everything necessary. As it turns out, it occurs in a very specific routine, and after a lot of experimentation, loading the whole DMA command longword into RAM, and then loading it from RAM word by word fixed these issues. After further experimentation, only the last word was required to be written from RAM, as the manual suggests.

    What is strange about this, is that this bug only occured on my 88' (model series 98M) model 1 Japanese SEGA Mega Drive. My model 1 Genesis did not exhibit this behaviour. Similarly, changing the base game to write the entire command directly into VDP did also not trigger this bug at all. It looks as if this is an actual VDP bug at the very least with early revisions of Mega Drives, but it either needs very specific circumstances to trigger or it triggers only very occasionally.
     
    ProjectFM and maple_t like this.
  2. FireRat

    FireRat "The grand imitator..." Member

    Joined:
    Oct 31, 2009
    Messages:
    533
    That's... weird. If only works from RAM, and not CPU regs, does the crash still occur if you copy the final word from ROM? If copying from any "memory" IS what actually lets it work, it means doing so from SRAM/PRAM/WRAM/32xVRAM would also work...
     
  3. Natsumi

    Natsumi Phoenix egg Member

    Joined:
    Oct 7, 2011
    Messages:
    679
    Location:
    Long and dangerous river
    Given the value is dynamic and I didn't write the code, I wouldn't be able to test it at any rate. I haven my doubts about it being able to work, but I can test some theory with other DMA transfers to see if loading from hardware registers suddenly break the system.
     
    MarkeyJester and FireRat like this.
  4. Natsumi

    Natsumi Phoenix egg Member

    Joined:
    Oct 7, 2011
    Messages:
    679
    Location:
    Long and dangerous river
    Oh yay, double post! As it turns out, my Japanese model is even more broken! As we were testing Dual PCM FlexEd working on hardware, I re-found a bug that had us scratching our heads. Specifically, it looked like the vertical interrupt on Z80 was never, or very rarely, getting fired. It seemed to be rather random what would work even temporarily before it all crashed and burned. This is strange, because any other hardware we tested on, the bug was not present. We thought this was a special one-off bug in a very early version of the Mega Drive, but it could happen on other models, we are just not sure yet. Soon after, with pretty much random guess, I added a im 1 instruction to the code, and it... Worked! After many hours of playing about and thinking this would never work, that single instruction fixed it. Turns out, im 0 is different in some early Mega Drives. Even more funnily, I had set im to 1 earlier for testing... In the Z80 init code. It turns out, at least I think so, that doing a z80 reset actually resets im back to 0, the mode that is broken (or different?) in these early models. Who knew. So, the conclusion is, never ever forget to set im to 1 or 2 in your z80 sound drivers!
     
  5. GerbilSoft

    GerbilSoft RickRotate'd. Maintenance

    Joined:
    Jul 11, 2009
    Messages:
    16
    Z80 interrupt modes 0 and 2 are known to not be supported on Mega Drive (and I think Master System as well; they may have worked on SG-1000 and SC-3000).

    IM 1 is the simplest interrupt mode, and is not present on the 8080 that the Z80 was derived from. When in IM 1, and the Z80 receives an interrupt, it jumps to $0038. That's it. No special handling required, other than remembering to return from the ISR using the RETI instruction.

    IM 0 is the original interrupt mode found in the 8080. In IM 0, when an interrupt is generated by a device, the device must also place a one-byte instruction on the data bus. This instruction is usually an RST instruction, e.g. RST $38. Since the Mega Drive VDP doesn't do this, the Z80 ends up reading garbage data (68000 prefetch?) and executes a random instruction.

    IM 2, vectored interrupts, is another interrupt mode specific to the Z80. This mode is similar to IM 0 in that it requires the interrupting device to put a value on the data bus when interrupting the CPU. The data value here is the low 8 bits of the interrupt vector. The high 8 bits is located in the Z80's I register. Combine the two, and you get the address of the interrupt vector. The Z80 reads the 16-bit value at this address and jumps to that location. This is similar to the 68000's interrupt table, though it's relocatable. (68020 adds interrupt table relocation, but that isn't useful here.)

    Both IM 0 and IM 2 require the interrupting device to write a value to the data bus. That doesn't happen on Mega Drive, which is why they don't work properly.

    Reference: http://www.z80.info/1653.htm

    EDIT: Since you mentioned that later MDs worked fine with IM 0: It's entirely possible that a later version of the MD chipset places $FF (RST $38) on the Z80 data bus when an interrupt occurs. This would fix broken games, but only on newer systems. (This could also be the result of adding pull-up resistors to the Z80 data bus, maybe.)
     
    Last edited: Apr 16, 2019
  6. FireRat

    FireRat "The grand imitator..." Member

    Joined:
    Oct 31, 2009
    Messages:
    533
    Last edited: May 16, 2019
    ProjectFM and Tanman Tanner like this.