Sunday, May 07, 2006

Dynamic Recompilation

I'm in the proces of rewriting the dynamic recompilation engine for Daedalus. The previous version just about did the job on the PC, but the approach I originally used had a number of problems when it came to the PSP.

Firstly it was a huge memory hog. As memory is relatively cheap on the PC, the recompilation engine is fairly enthusiastic about recompiling blocks of code almost as soon as it comes across them. I'm not so sure this is such a good strategy now, as thousands of ops are recompiled never to be executed again. On the PSP I just can't afford this memory overhead.

Another big problem with the PC dynarec is that there's currently no way for me to easily flush old fragments of code from the cache to save memory. Again this isn't much of a problem on the PC, but on the PSP I can't afford to have old fragments of code lying around, chewing up valuable memory.

Finally the PC version of the dynarec is sufficiently old, poorly documented and overly complex that it's almost impossible for me to improve and properly debug. In general I'd rather refactor systems than rewrite them from scratch, but I feel there are so many drawbacks with maintaining the old system that it makes sense to look for an alternative solution.

Earlier in the week I started doing a bit of background reading on dynamic recompilation. In the past I've always struggled to find any useful papers on the subject (pehaps because 'dynamic recompilation' seems to be emulator-scene terminology). Googling 'dynamic code generation' turned up this paper:

Dynamo: A Transparent Dynamic Optimization System

Although this is talking about native recompilation (i.e. interpreting and recompiling a binary on the same platform it was developed for), it addresses the issues I raised above. What I find incredibly impressive is that they can improve on the speed of native execution, even with the overhead that they're generating by initially interpreting the code. I feel that if I can use their approach for a new version of Daedalus's dynamic recompiler I'll be able to obtain significant improvements in speed and memory usage over the old version.



LaMa said...

Very interesting,
I'll keep an eye out on this blog.
Looking forward to reading more updates on the dynarec design.

Please do keep us informed on your progress. I personally very much appreciate it.

Pere Felices said...

Yes, another code-blog! please keep us informed :)

Linkzie said...

this is good (:

kekpsp said...

Is it possible to emulate a system like the N64 with the system limitations that the PSP poses, I think that the efforts of StrmnNrmn and Psmonkey are trully amazing, and if they pull it off, will signify a new landmark in Emulation.
Thank you StrmnNrmn you are a pioneer on a new system, I wish you the best of luck.

kekpsp said...

If you came up with a static recompiler that would run Mario at full speed with sound I think you would reach God like status in the homebrew scene, Good luck and thanks for making this possible

narles said...

This blog is a great idea. I'm so fascinated by the whole emulation process... and getting updates on what is probably THE most anticipated emulator is a nice plus too! Keep up the great work StrmnNrmn!

_Psycho said...

Yeah it's nice to see blog and not forums about new stuff coming up. As long as you keep it updated ;)

Dynamic recompilation is not an easy task. I hope you will bring up something that work quite well !

Good coding !

StrmnNrmn said...

Thanks for your comments guys.

I've been thinking about starting a technical blog on and off for a couple of years, but never really felt I had enough of interest to talk about.

Although it's quite a niche subject, I think (or rather hope :) there are plenty of people who are interested in emulators and how they're put together.

pookie said...

Amazing work you are doing for the 64, i knew someday it would happen thanks for your effort. 64 is king.

The Melancholy Prole said...

Im no expert but would it be possible to rather than buffer an on the fly dynamic recompiler, seperately recompile an entire rom into say a Daedalus format through a seperate program. For example dynamicaly recompile or port your roms through a program on your computer before uploading and emulating.

The Melancholy Prole said...

I believe what Im talking about is called processor retargetability. Here's a short paper.
Im no programming expert though I should be, ehh. If there's anything I can do to help your efforts lemme know as I have been obsessed with emulation for too long.

Shalted said...

Nice work, although it looks like you have alot of work ahead of you. Don't you find it annoying that you're writing a Dynamic Mips R4300i recompiler for a Mips R4000 (R4400?)processor, I actually think it may be possible to just use some opcodes instead of recompiling them, although a few would have to be recompiled, and jumps would have to be routed to a function that loads more code, and frees the old block, system calls would also have to be patched. Also some adresses would have to be patched but it would probably do the job.

kekpsp said...

I just had a thought, could textures and lighting be switched off, that would increase the FPS considerably, sure the graphics would look very basic but the game would become playable