PDA

View Full Version : Daedalus blog update - 15 August 2007 - Interesting Dynarec Hack



blackrave
August 15th, 2007, 23:21
New blog post from StrmnNrmn's blog (http://strmnnrmn.blogspot.com/) tells us about how some dynarec optimizations make Daedalus faster.


I was playing around with the code generation a couple of evenings ago, and realised that if I made a certain assumption, I could drastically speed up specific types of memory accesses.

When I discussed load/store handling on Sunday, I presented the new code that is typically generated for handling a load such as 'lw $t0, 0x24($t1)' on the N64:


ADDIU a0 = s1 + 0x0024 # add offset to base register
SLT t0 = (a0 BEQ t0 != r0 --> _Trampoline_XYZ123 # branch to trampoline if invalid
ADDU a1 = a0 + s7 # add offset to emulated ram
LW s0 <- 0x0000(a1) # load data


(I'll ignore all the extra code which is generated, and just concentrate on the 5 instructions above which correspond to the expected path of execution.)

Of the 5 instructions that are generated, two - the SLT and BEQ - are just there for performing error handling in the case that the address is invalid, a hardware register (i.e. for memory-mapped I/O), or a virtual address. I'll call this error handling for short.

If we were generating code in an idealised environment where we didn't have to perform error handling, we could drop the SLT/BEQ instructions to get this:


ADDIU a0 = s1 + 0x0024 # add offset to base register
ADDU a1 = a0 + s7 # add offset to emulated ram
LW s0 <- 0x0000(a1) # load data


We could then optimise this even further and perform the offset calculation directly as a part of the LW instruction:


ADDU a1 = s1 + s7 # add offset to emulated ram
LW s0 <- 0x0024(a1) # load data


In this idealised situation we could reduce an emulated load to just two instructions, with no branches. That's a pretty good saving!

The problem is that the environment we're generating code from is not 'ideal', and it's hard to know in advance of time which memory accesses are going to directly access physical ram, and which are going to access hardware registers or require virtual address translation. For that reason, we have to place a guard around every memory access to make sure that it behaves correctly. At least, that was the way I was thinking until earlier in the week.

What I realised on Monday is that I can make an assumption that lets me remove the error handling code for certain types of load/stores. The assumption is that when the N64 accesses any memory through the stack pointer ($sp) register, the address is always going to be valid, physical memory.

The assumption relies on the fact that most roms don't do anything particularly clever with their stack pointers - it gets set up for each thread to point at a valid region of memory then the game just runs along, pushing and popping values from it as the code executes. Of course, if the assumption is wrong then the emulator will just crash and grind to a halt in a unpredictable manner :)

It was straightforward to add a hack to the code generation to exploit this kind of behaviour, and the results have been better than I expected - I'm seeing at least a 10% speed up, and the code expansion factor (the ratio of generated bytes of instructions to input bytes) has dropped from around 5.0x to 4.0x. Stability has been excellent too - I've run about 8 roms with the hack so far, and all of them have run perfectly.

I think one of the reasons the hack has such an impact is that a lot of the memory accesses in a typical C program are through the stack. Here's an example snippet from the entry to a function on the N64, where the compiler emitted code to store the return address and arguments:


SW ra -> 0x0014(sp)
SW a0 -> 0x0058(sp)
SW a1 -> 0x005c(sp)
SW a2 -> 0x0060(sp)


When I look through disassembly for the roms I'm working on, it's very common to see lots of sequential loads/stores relative to the stack pointer like this.

Previously Daedalus generated around 20 instructions (including 5 branches) for the above snippet. With the hack, the generated code now looks like this:


ADDU t1 = s1 + s7
SW s4 -> 0x0014(t1)
ADDU t1 = s1 + s7
SW s3 -> 0x0058(t1)
ADDU t1 = s1 + s7
SW s2 -> 0x005c(t1)
ADDU t1 = s1 + s7
SW s5 -> 0x0060(t1)


8 instructions, 0 branches. What's more, it looks like with a little work, I could eliminate 3 redundant address calculations:


ADDU t1 = s1 + s7
SW s4 -> 0x0014(t1)
SW s3 -> 0x0058(t1)
SW s2 -> 0x005c(t1)
SW s5 -> 0x0060(t1)


Now that would be efficient :)

I still want to do lots of testing with the hack. I want to find out if there are roms that don't work with the hack enabled, and how common a problem it is. It's such a significant optimisation though that I'm certain I'll be adding it as an option in Daedalus R13. The results of my testing will probably determine whether I default it to on or off though.

So far Daedalus R13 is shaping up to be significantly faster than R12. I'm still not sure when I'll be ready to release it, but you'll hear about it here first.

-StrmnNrmn

Give feedback to StrmnNrmn here or in the comments section in his post (http://strmnnrmn.blogspot.com/2007/08/interesting-dynarec-hack.html).

masamune
August 15th, 2007, 23:40
This sounds good... I'm glad to see that his blog updates are only days apart now, Bless his mac :)

hounddog
August 15th, 2007, 23:41
cant wait

SpacemanSpiff
August 15th, 2007, 23:46
Adding it as an option seems like a good idea, it sounds like it could potentially break compatability for a number of games. But adding to his other changes, this should give at least a 20-25% speed increase over R12. Awesome!

FlyingMojo
August 16th, 2007, 00:04
Stability has been excellent too - I've run about 8 roms with the hack so far, and all of them have run perfectly.

:eek: ...and i now need to change my underwear

PLZKLLME0080
August 16th, 2007, 00:07
:eek: ...and i now need to change my underwear

Same here. I can't wait for the release of this!

mike03$$$
August 16th, 2007, 00:13
cool a 25% speed up looking good strmnnrmn

On The Rise
August 16th, 2007, 00:17
This a great breakthrough for the emu. This is going to be the most improved release yet to date for this emu. StrmnNrmn is on a hot streak people he is in his prime. The way he is coding we could see hopefully more than a 30% speed increase. Nobody rush him with a realese date or anything let him do his thing. Thanks StrmnNrmn for updating your blog and coding this great emu. I wish I could donate.

Veskgar
August 16th, 2007, 00:19
Does the excitement ever stop? I love the PSP homebrew scene and especially great emulators such as this. Makes me feel like a kid again.

Needless to say, I, like all of you, cannot wait for the next release.

dj2005
August 16th, 2007, 00:24
It was straightforward to add a hack to the code generation to exploit this kind of behaviour, and the results have been better than I expected - I'm seeing at least a 10% speed up, and the code expansion factor (the ratio of generated bytes of instructions to input bytes) has dropped from around 5.0x to 4.0x. Stability has been excellent too - I've run about 8 roms with the hack so far, and all of them have run perfectly.


Amazing. :thumbup:

Buddy4point0
August 16th, 2007, 00:26
amazing, i love dualdalus and cant wait for the next realise

acn010
August 16th, 2007, 00:31
omg 25%
oh wow... so bsically sm64 runs full speed????

jedikevin20
August 16th, 2007, 00:36
Oh man. the boys in his prime. At the rate his going, He may state i got a 50% improvement by this weekend lol. Go strm go. Woat Woat. Don;t anounce any dates man. Just add to the blog like you've been doing and let us marvel on whats about to come w00000t. Exophase and strm. Best coders in psp homebrew. Kepe doing yall things. so let me see 8 roms he's tested. Guarantee orb got like mario kart and super smash in prob mario 64 in them test. Hmm. 30% improvement. Hmm. super smashruns at like 25-35fps give a little up or down. 30% umm. thats about a 9-10 fps swing w00t w00t. GO strm gooo. Oh snap just peed my pants. Gotta go change lol

Tetris999
August 16th, 2007, 00:49
ya i bet mario 64 will be full speed :p

maxipower90
August 16th, 2007, 01:01
OMG this guy is better than sliced bread, OMG this the flipping great

CaptainMorgan4
August 16th, 2007, 01:06
I'm in Bahamas posting from my psp with the only working wifi on the island, glad to hear this great news, I'll be back tomorrow.

tylerscool
August 16th, 2007, 01:08
Speed hack o man this means ooohhh Zelda and hopfully the inside of houses will be better but it would be a dream to hear

"DIDDDDY KONGGGGG RACINGGGG" in full speed and I bet most people agree with me that diddy kong should be a #1 concern

blackrave
August 16th, 2007, 01:13
I'm in Bahamas posting from my psp with the only working wifi on the island, glad to hear this great news, I'll be back tomorrow.

Haha, so you do get a wireless connection at that island. I remember you posting that you hoped for a new release soon as you would be totally cut off from the Internet. I also thought I'd be cut off from the Internet when I was on holiday at my summer place, but there were actually two unsecured networks there that went straight to the laptop inside the hut. :) Also, I was searching for wireless connections when another strong connection showed up, but that was secured with WPA2 minutes later. :p

Anyway, this will be a very interesting Daedalus release (note the name, please, Buddy4point0! Seen you post it wrong many times, which is annoying when the correct name is right in the topic. :mad: ). I bet (and hope) it will come sometime in September.

Gene
August 16th, 2007, 01:23
I'm going blind out of anticipation.

`The0n3
August 16th, 2007, 01:25
Omg, he hasnt even implemented ME engine yet and hes already made a good 20-25% speed up breakthrough, dang this is gonna be a good release. Wait until R16 = full speed hopefully and great compatability. Great emu.

xg917
August 16th, 2007, 03:38
i have a feeling he can get at least 30-35% more of a speed up for this release (not including media engine) . if not. then more :D


Of course, if the assumption is wrong then the emulator will just crash and grind to a halt in a unpredictable manner :)

Stability has been excellent too - I've run about 8 roms with the hack so far, and all of them have run perfectly.

omg does this mean that games that crashed upon loading would now load with out crashing? and also zelda wouldnt lock up randomly?? if so then OMFG I LOVE THIS MAN hahaha

pkmaximum
August 16th, 2007, 04:09
Great work, I look forward to the release

emuking
August 16th, 2007, 07:59
end of the month for sure, cant wait :D

.:}<3\/!}\{:.
August 16th, 2007, 09:06
omg, can't wait!, It sounds really good!

Axelius
August 16th, 2007, 10:00
Once again I understand less than half of what he says. But it's very interesting to see what such an excellent coder is thinking. And 25% speedup sonds nice too.

bah
August 16th, 2007, 10:06
omg does this mean that games that crashed upon loading would now load with out crashing? and also zelda wouldnt lock up randomly?? if so then OMFG I LOVE THIS MAN hahaha

I believe he was saying that stability was excellent in the regards to how the application of the new speedup he detailed just prior went.

ie the assumption he made, and the 'hack' he added to the code to test out how that assumption would effect speed/stability went well and didn't cause any new compatibility issues on the 8 games he tested.

He said a few posts back (on his blog) that this release is focusing on speeding up emulation of games that currently run rather than adding to the compatibility of the emu at this stage based on comments on forums. I'm really glad he chose to focus on speed for r13 personally, who knows, there could be some minor compatibility updates to. :)

Thanks for all the hard work StrmnNrmn.


EDIT: emuking: Did that moron Jack Thompson really say that? He should have added 'or the vice president'.

kharaboudjan
August 16th, 2007, 11:17
WOW! i mean we will have like 5 fps more in soem of the games now. that means they will probaly be fully playable for real in R13!!!

hope u understand that this is so fantastic for all of us psp users! the next release gonna blow us away :D

Sonicboy 101
August 16th, 2007, 11:21
This is very good news for PSP owners. I can't wait for R13!
Thanks for all the work StrmnNrmn!

goldeneyegod
August 16th, 2007, 13:12
this is amazing I carn't wait, just hope its released before the 1st of sept because im of to china then and playing goldeneye sure would make my 9 hour flight litteraly fly bye lol

Zaitmi
August 16th, 2007, 20:28
You know what, I can actually wait as long as it takes, because the more I wait, the better the emulator will be. Let's hope that's how it goes, lol. I really am hoping for the speed-ups to somehow help the sound in Super Mario 64. It's a long shot, but possible...

xg917
August 16th, 2007, 20:33
of course speed up will help the sound.

CaptainMorgan4
August 17th, 2007, 01:49
I'm back at home now and pretty settled, great to be back and I got a nice tan. Just glad to be back in cool weather and no bug bites, ahh and the internet is great too. I can't wait for this R13 release I'm glad StrmnNrmn is having such a success in speed, means we can get compatability going faster, which will end up making this emulator perfect and the best for PSP.

zheg_pagaza
August 17th, 2007, 01:58
when its the realese date?

DanTheManMS
August 17th, 2007, 03:03
when its the realese date?

Reading the article usually helps answer such questions.

jurkevicz
August 17th, 2007, 16:21
Now, with the implementation of ME would help sound as well! This emu will be great!

xg917
August 17th, 2007, 16:34
hes gona hold off on the ME for this release as well..

CaptainMorgan4
August 17th, 2007, 17:36
Well with speed getting alot faster his current sound solution shouldn't be too bad for this release, especially for games like Mario 64 and StarFox 64.

xg917
August 17th, 2007, 17:37
ive never noticed anything wrong with the sound..

CaptainMorgan4
August 17th, 2007, 17:41
Oh yeah I mean there's nothing wrong with the sound it was just the speed was low which made the sound quality horrible, but I was just saying with the new speed the sound should be alot better with alot of games. Simply just because we just gained a bunch of FPS so the sacrifice level of FPS for the sound will still be significantly faster than R12, I hope that made sense.

xg917
August 17th, 2007, 18:00
it made sence to me :p

i did some calculations with the 25% speed up.
-Mario 64 = 6.25 FPS increase
-super smash bros = 8.75 FPS increase
-zelda OoT = 2.5 FPS increase
-Mario Kart 64 = 4 FPS increase
-mario party 1&2 = 3.5 FPS increase

CaptainMorgan4
August 17th, 2007, 18:05
Nice thanks for that Xg it's nice to see how much they are improved, but I'm sure StrmnNrmn isn't done with the speed progress. I'm going to go get something to eat but yeah thanks for that Xg.

kharaboudjan
August 18th, 2007, 12:08
seems nice xg17!! hope Zelda will be faster though!

Zaitmi
August 18th, 2007, 20:06
hey xg, isn't a 20% overall? StrmnNrmn said that it was a 20% speed-up overall in his blog when he responded to some comments. Maybe I'm wrong though, but just check to make sure.

CaptainMorgan4
August 24th, 2007, 19:00
Well it's been 9 days since his last update, I'm very confident that with this Dynarec hack and further improvements he's made to the Dynarec engine that we will be getting a huge speed increase in R13. I just can't wait till he updates again so we can see what kind of improvements he has made in these 9 days, and as this month rolls down towards the bottom we should expect a release soon too.