Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mGBA debug enable register not properly restored #1

Open
Vulcalien opened this issue May 23, 2024 · 4 comments
Open

mGBA debug enable register not properly restored #1

Vulcalien opened this issue May 23, 2024 · 4 comments

Comments

@Vulcalien
Copy link

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
Copy link
Owner

tolik518 commented May 23, 2024

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
Copy link
Owner

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

@Vulcalien
Copy link
Author

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:

  • Debug was Enabled (original value = 0x1DEA)
  • identify_mgba()
  • Debug should be Enabled

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.

@tolik518
Copy link
Owner

tolik518 commented Jan 7, 2025

It took me a while to understand where you're coming from (not the whole year, no :D), but it apears to me that this is not the case and the correct value is restored.

So if mgba was in debug mode, it stays in debug mode, but if it wasnt, then it gets enabled and disabled in the next instruction.
I'll test it though to be sure :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants