VBA-M Forums

Full Version: dynamic recompiler to transform THUMB instructions to ARM instructions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Would it be possible to write a dynamic recompiler that translates from THUMB instructions to ARM instructions, so that existing instructions from the ARM core can be reused by the THUMB core? Would that give any benefits beside less code?
Yes, if you're willing to invite all of the challenges associated with dynamic recompilers, like dealing with self-modifying code and branches.

When you just want a one to one correspondence you can use a direct mapped decode cache. This is what the author of Gemulator likes to do (see emulators.com). This makes it so you don't have to perform and block tracking and mapping, but you still have to invalidate cache lines on writes to accommodate self-modifying code.

In this case, it's actually just as well to just use a lookup table to convert any Thumb opcode to an ARM equivalent (or special undefined ARM code where there's no equivalent). The input is only 16 bits, and the output is nominally 32 bits. You'll likely need another bit of output, though, to determine how whether the PC offsets should be halfword aligned or not, because Thumb behavior regarding this is inconsistent. This method would probably be superior to any kind of cached conversion.

For your purposes, that is, only using interpreters, eliminating code is really the only benefit. Performance would be substantially worse because of the time needed to convert from Thumb to ARM, and also because ARM instructions are just a lot more expensive to emulate in general. In a usual GBA game over half the instructions executed will be Thumb, so you'd be hurting speed a ton across the board.

I went with this technique in the emulator I'm currently writing, but only because I was never planning on relying on the interpreter for speed (did a recompiler for that) and I wanted to have less points of failure. For recompilation, where the speed isn't that big of a deal, it's nice to have less code to work with, and having one instruction set instead of two means you don't have to write a bunch of analysis and optimization code twice. Another benefit is that it gives you room to perform propagation from Thumb to ARM, resulting in ultimately better code.

So in short, I think you shouldn't do it.
Thank you very much for your detailed opinion.

It requires a lot of time to understand a system and the emulation techniques, which I currently don't have, so your experience is very appreciated.

I guess I should think my plans over. Maybe it's better to steal / borrow good [open source] code from other emulators instead of rewriting it from scratch.
maybe we should work with what we have, and what other open source emulators have and see what they do differently / better.
Reference URL's