LDIR takes two step over commands to go to next instruction?

By fcoury

Rookie (18)

Аватар пользователя fcoury

09-04-2023, 03:35

I am writing an MSX1 emulator and as part of my debugging routine I run my emulator and openMSX side by side, stepping over instructions simultaneously (using openMSX's socket interface).

Turns out when openMSX is at an LDIR (0xED 0xB0) instruction, it takes two "debug step" commands or two clicks on the step over command if you're running openMSX Debugger.

Is this behavior by design? Should I expect the same behavior from other extended opcodes like 0xCB (RLC, etc.)?

Thank you!

Для того, чтобы оставить комментарий, необходимо регистрация или !login

By wouter_

Hero (535)

Аватар пользователя wouter_

09-04-2023, 20:36


The Z80 executes the LDIR instruction as: execute LDI, then if BC is not equal to zero repeat the same instruction. With 'repeat' as in "do not increment PC". This way the Z80 will re-fetch the same LDIR instruction from memory and re-execute it. One important aspect of this implementation is that it allows to serve interrupt request during execution of a (long) LDIR instruction.

OpenMSX emulates the LDIR instruction in this way. That means that "debug step" on an LDIR instruction will only advance past this instruction if BC has become zero.

Note that openMSX offers two step commands: step_in and step_over ("debug step" implements "step_in"). The former exhibits the above behavior of the real Z80 (so execute an LDIR as multiple instructions). The latter executes an LDIR instruction in a single step (and in addition it also executes CALL instructions, including the actual subroutine plus return, in a single step).

By fcoury

Rookie (18)

Аватар пользователя fcoury

09-04-2023, 20:52

That makes a whole lot of sense, thank you so much for the thorough explanation!