oh yeah... the old vtrace speedup ;)
First, without a very good interp core, dynarec is next to impossible. When I do a core, the first thing I do is have the dynarec put in calls to the interp core for every instruction. Obviously, this should work perfectly (and it never does, so you get the basic compiler bugs out of the way). Then (using #ifdefs) I work on sections at a time... starting with the easiest possible instructions. This way you can get, all the logic instructions, then memory, then conditionals etc running little bit by little bit.
Another easy approach is just to decode all of the instructions and build an array of functions pointers. This can give you a pretty big speed improvement over the interp core.... but yields big arrays and is still a good bit slower than a full dynarec engine.
To rehash... building a very solid interp core, with a good debugger is a critical first step. Try to think about how you would do the dynarec core so that you can best reuse your interp core when you get ready. Write the interp core in C and don't worry about speed. You will never gain much speed by optimizing or writting in ASM. The sheer number of cycles needed to decode instructions, jump off to a routine, execute the code, update registers and flags and then get the next instruction is too big 'tighten up'. Think of the ratio of native instructions to target instructions... it gets huge. With dynarec, you see ratios on the low end of 1:1 to 12:1 (memory accesses). You don't get anywhere near that with an optimized interp core.
The real key to dynarec is memory instructions. Determining what memory is being accessed is key. You'd like to get basic memory accesses down to a 1:1 or 3:1 ratio. Memory mapped IO (MMIO) is of course a little more difficult. The trick is to have the dynarec core understand memory loads from a register. My later cores would compile a 'sniffer' routine for memory accesses. The first time the sniffer runs, it looks at the address and determines if it could recompile the recompiled code better or if it is a MMIO access or such. In the MIPS cores, you often saw this...
LUI a0,0x4000
ADI a0,0x3ff0
STW a0,b0
which is the long way of saying
- Code:
*(DWORD *)0x40003ff0=b0;
if that address (0x40003ff0) is a 'safe' address, then this is just one x86 instructions. If 0x40003ff0 is a MMIO address, then the proper MMIO routine call is compiled in.
I also did some more advanced stuff that basically took the target ASM, upcompiled to a 'C' like langauge (I called it 'B') and then compiled that to x86 ASM. This is the way the real dynarec engines out there work and it yields amazing results. If you don't have to worry about virtual memory (MMU) then you can get an average of 1.5 native instructions per target instructions. This was, of course, with a RISC target, so it might not do as good when going CISC to CISC. ie; several RISC instructions are sometimes required for one 'C' statement that can often compile into one CICS instruction... thus a big savings that helps cover up the memory instructions poor ratio.
Anyway... as you correctly see, looking for branches back an instruction or two is a really good idea. The simple case is one instruction back and a test. Often this test is of a memory location, which is usually MMIO. Thus call a mmioSkip(DWORD address) function and let that function determine what should be done. You've already picked out VBLANK, other might be the results of DMA or and ISR. The usually approach is just to see if you have set an INT and jump to the ISR if so, or inc the cycles and trigger VBLANK and jump to the ISR.
The basic premise is that if the CPU is looping and NO computational instructions are in the loop, then it is waiting on something and (with an interp core...) you best have your core figure out what it is and trigger it right then and there.
Damn, I really wish I had time to work on this with you. Maybe when you get the interp core 100% I might have some time to help on the dynarec. If you want some source code to my old dynarec engines, PM me and I'll send it on your way.
Cheers,
rcp