tolik518 / gba_env

Library for detecting the environment for GameBoy Advance ROMs (GBA/NDS/mGBA/VBA/gpSP/etc)
5 stars 0 forks source link

mGBA debug enable register not properly restored #1

Open Vulcalien opened 4 months ago

Vulcalien commented 4 months ago
pub fn identify_mgba() -> bool {
    const REG_MGBA_ENABLE: *mut u16 = 0x04FFF780 as *mut u16;
    let original_value = unsafe { read_volatile(REG_MGBA_ENABLE) };

    unsafe {
        write_volatile(REG_MGBA_ENABLE, 0xC0DE);
        let result = read_volatile(REG_MGBA_ENABLE) == 0x1DEA;
        write_volatile(REG_MGBA_ENABLE, original_value); // Restore original value
        result
    }
}

I haven't tested the code, but by looking at it I noticed you're reading the register's value and then writing the same value again. But the debug enable register, when read, does not return '0xc0de', but instead '0x1dea' or any other value.

So when restoring the register, it should look something like this:

if original_value == 0xidea:
    write 0xc0de to REG_MGBA_ENABLE // or do nothing, since the register has just been enabled during the test
else
    write 0 to REG_MGBA_ENABLE
tolik518 commented 4 months ago

Hey @Vulcalien, I'm saving the original value from REG_MGBA_ENABLE (which is usually 0x8011 as it seems), then I write 0xC0DE to it and then I restore the original value, which is 0x8011 in some cases and 0x0 in other cases. So the original value can never be 0x1DEA or 0xC0DE. Except if it was enabled before, but then it stays enabled with the current implementation.

Tested current behavior in different emulators: Screenshot 2024-05-23 100531

Maybe I'm wildly misunderstanding something? (which is very possible because I have a light fever)

tolik518 commented 2 months ago

Hey @Vulcalien could you please update me on the issue?

Vulcalien commented 2 months ago

I still believe the stored value read from the Enable register should not be restored, since it's not a real read/write register. But I don't have time to test this right now.

I would test this scenario:

I suspect that after identify_mgba(), Debug will be disable instead. If that's not the case, then I was mistaken and this issue should be closed.