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

Z80 instructions brokenly reordered in decompilation #2257

Open
cm68 opened this issue Sep 9, 2020 · 5 comments
Open

Z80 instructions brokenly reordered in decompilation #2257

cm68 opened this issue Sep 9, 2020 · 5 comments
Assignees
Labels
Feature: Processor/Z80 Status: Triage Information is being gathered

Comments

@cm68
Copy link

cm68 commented Sep 9, 2020

the order of EI and DI instructions is important, and moving them exposes races that do not exist in the original code

the code:

                             **************************************************************
                             *                          FUNCTION                          *
                             **************************************************************
                             void _enable(void)
             void              <VOID>         <RETURN>
                             _enable                                    
                0                   ram:6b27 af              XOR        A
                0                   ram:6b28 32 36 6b        LD         (dicount),A
                0                   ram:6b2b ed 46           IM
                0                   ram:6b2d fb              EI
                0                   ram:6b2e c9              RET

decompiles to:

void _enable(void)
{
  setInterruptMode(0);
  enableMaskableInterrupts();
  dicount = 0;
  return;
}

the original code uses dicount as a lock, and moving the clearing of the allows an interrupt handler to see a non-zero value.

ghidra version 9.2-DEV

@agatti
Copy link
Contributor

agatti commented Sep 15, 2020

Maybe it is the same as #1208? Marking dicount as volatile might do the trick I guess...

@cm68
Copy link
Author

cm68 commented Sep 15, 2020 via email

@agatti
Copy link
Contributor

agatti commented Sep 16, 2020

No idea, really. Maybe you can create a region that's as big as that variable... Still, this may or may be not a bug in the decompiler, I guess the Ghidra folks could take a look at that.

@emteere
Copy link
Contributor

emteere commented Sep 16, 2020

You can create a db or other data type at dicount, and change the data settings for mutability to volatile.
That does indeed re-order the "dicount=0" to before the Interrupt enable pcode userops.

You really shouldn't need to do this, and the order preserved, but it does work.
I'll check into why.

@neuromancer
Copy link

Is there any updates on this issue? I was wondering if it will possible to add generic IO memory marked as volatile for different devices (e.g. ZX Spectrum, Amstrad CPC, etc), in order to workaround this issue and also to offer better decompilation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Feature: Processor/Z80 Status: Triage Information is being gathered
Projects
None yet
Development

No branches or pull requests

5 participants