Larry’s Personal & Tech ramblings

Just another WordPress.com weblog

Optimizing the 68K

I started writing an emulator for the 68000 several years ago for a Capcom CPS1 project.  At the time I was targeting the desktop PC, so writing it in C wasn’t a problem.  Lately I’ve had to create an ARM version for portable and embedded systems and speed is definitely an issue.  My first pass at a purely ARM 68k emulator didn’t take too long to get working, but was not terribly fast.  The complexity of the 68k kept me from taking too many chances with some of my optimization ideas.  I’ve spent much time since then thinking about ways of speeding it up without coming up without anything useful.  Starting last week I took another look at the code and I found myself with 5 new ideas for further optimization.  I’ve implemented 4 of them so far and I’ve sped up the code a good amount and reduced the size by 15%.  The last idea will end up growing the code size, but at this point that won’t have much effect on the speed since it doesn’t fit in the code cache anyway.  Without revealing too many details of what I did, here are my ideas which worked:

1) Delayed flag calculation - processors which affect the flags on almost every instruction waste lots of time manipulating them.

2) When possible, use the native ARM flags to calculate things such as OVERFLOW.

3) For Read-Modify-Write functions, try to re-use the effective address efficiently.

4) Use better register management and instruction ordering to reduce pipe stalls and memory accesses.

5) Work with the statistics of the opcodes and focus energy on the most used instructions.

The worst part of embarking on a project like this is that you start with working code and end up breaking while trying to improve it.  Much time is lost figuring out what broke, but in the end it’s all worth it :).

March 22, 2007 Posted by bitbank | arm, asm, assembly language, emulation, optimization, performance, tech | | 3 Comments