Sunday, August 06, 2006

More R7 Optimisations

It's been a while since my last post, but I've still been hard at work with various optimisations for Daedalus R7.

Although my main focus is on improving the dynamic recompiler, I've been looking at optimising a couple of other areas that I noticed were fairly expensive. The texture cache is one of the areas that I spent time tuning this week. This cache is used to avoid converting textures from the native n64 formats to psp formats every frame. I made a couple of fixes to improve the hashing function which gives much faster lookups in certain situations (such as tiled backdrops). I also provided an option to change the frequency at which the texture cache checks for updates to the textures. Many roms look fine when this check is entirely disabled, and this can give quite a nice speed boost.

My main focus has continued to be on the dynamic recompiler. I've made a couple more bugfixes in this area. One bugfix involved detecting when roms were using self-modifying code. The fix involved dumping the contents of the dynarec cache so that the code is correctly regenerated for the updated instructions. This fix solves a couple of issues I was seeing with Quest64, and I'm sure it will help improve compatibility with a number of other roms too.

The other dynarec issue I fixed was related to the way I was handling certain types of branch instructions. The MIPS processor has a set of 'branch likely' instructions which work slightly differently to regular branches and so I handle them separately in the dynamic recompiler. It turned out that I had forgotten to link together code fragments when they exited through a branch likely instruction. This fix gives a nice little speedup.

The biggest bit of new development I've been doing on the dynarec is on optimising for various situations where I can determine the contents of a given register at the time I'm compiling the code. As an example, many roms use the following sequence to load an integer value from memory at a specific address:


LUI $t0, 0x8033 // Load Upper Immediate - i.e. load t0 with 0x80330000
LW $t0, 0x1234($t0) // Load Word - i.e. load t0 with the value at 0x80331234


Previously I'd generate code for both of these instructions on the PSP. The LUI instruction is easy (if t0 is cached on the PSP then this is just one instruction). The LW is a lot more tricky. I have to call a function to convert the address on the n64 (0x80331234 in this case) to the address in the emulated memory on the PSP. Then I have to read from that address, or trigger an exception in the emulator if the memory address is invalid.

With the changes I've just made, when I encounter the LUI instruction (or other instructions involving loading constant values into registers) I keep track of the fact that I've loaded t0 with 0x80330000. When I come to process the LW instruction, I can now determine that the desired address is 0x80331234. I can then map that address directly to the required location on the PSP, avoiding a function call in the generated code. By avoiding the function call I no longer need to flush cached registers back out to memory. Also, because I can tell in advance that the address lies in RAM (and isn't referencing a hardware register for instance) then I can also omit the code testing for an exception. Finally, in situations like the example above, I can don't need to generate any code for the initial LUI (as the register is immediately overwritten with the loaded value.)

In summary this is a very nice optimisation - it generates fewer instructions (reducing the size of the dynarec code), it avoids unnecessarily flushing out cached registers, it avoids generating exception handling code, and it can eliminate redundant instructions (the initial LUI). In the best case, for 2 source instructions it will generate just 3 output instructions, compared to 12-13 for the unoptimised case.

Unfortunately this approach only works with load and store instructions where the address can be determined in advance, but from the roms I've examined so far around 10-15% of the load/store instructions can be optimised in this way, which is enough to give a measurable benefit.

I'm going to spend the rest of this week seeing which other parts of the dynarec engine can benefit from similar approaches. I have a couple of other features to implement (configurable controllers etc), if that all goes to plan I'll try and prepare R7 for a release next weekend.

-StrmnNrmn

76 comments:

tsurumaru said...

Great to hear from you Strmnnrmn, hopefully it will help calm down those that thought you had disappeared into the ether (when the more sensible knew that you were just hard at work). Look at the last thread's posts for example!

Since I know you are against it and I did try to warn those posting them there, there are a number of Rom links in the Captain Morgan's Compatibility thread that you may wish to remove!

Good luck and keep up the great work! :)

Kramer said...

sounds great StrmnNrmn cant wait for next weekend
hopefully the comments section doesnt get spammed up again.

wally*won_kenobie said...

well i have news for everyone,

Im going to wait until R7 before continuing my compat list.. (probably need to start from scratch)

Its shocking

Strmnnmrn please fix the missing_mux and screenshot problem :P Its getting a bit overboard!

P.S That BBQ went for a looong time :D

Mario Kart God said...

About how much faster have you gotten it since last time? Percentage wise.

Urkel said...

yes, you forgot to tell us the zelda speed and you must tell us how much faster it is now.

wally*won_kenobie said...

who knows zelda could be full speed next week :P

Mario Kart God said...

I just wonder how fast Mario Karts going now!

xiringu said...

I've got a question about all that dynarec stuff.

what's the ratio, between difficulty and the bennefits we could achieve in other emulators, like gameboy advance.

for example, too difficult/too much developing time for the speed gained?

does dynarec adapt better to one kind of emulation (machine), or is it the same for all, say Commodore 64?

any comments on this? thanx :)

infernomax said...

Thankyou for the update man, Well done on the recompiler. Lookin forward to R7 ur a champ

BigMace said...

thanks for the update. I am really looking forward to next weekend.

Imtiaz said...

Congratulations on your accomplishments Strmnnrmn! If you reach full speed, may I request that you work on sound input? That is, if it's what you want to do...

kersplatty said...

sounds great hope you do everything you need to, its my uncles wedding next week as well :D playing mario in the church :D

PSDroideka said...

Good to hear!!

That bugfix you made sounds like it'll help with the problem in Buck Bumble in which, well, you cant see anything. Great Great news
: )

Laxer3A said...

Hi !

Self Modifying code on a MIPS ?!?!

I believed that it was limited to old cpu without any cache or pipeline.

Normally self-modifying isnt possible on RISC CPU.

Do you have any example ?
Keep up the good work.

Kemp said...

It can be implemented on any processor where either:

1) You can flush the cache/pipeline, either by a special instruction or by utilising something like the x86 far jump (which admittedly only flushes the pipeline [ignoring prediction abilities], but you get the point)

2) You can be sure the code you're modifying isn't currently in the cache or pipeline (this would be somewhere between hard and impossible)

In other cases you can use self-modifying code, but your changes will be ignored on a regular basis, at least until that particular piece of code has left the pipeline and cache.

Urkel said...

dude I hope strmnnrmn releases this early firday or thursday, cause Im goin out of town and I dont want to miss it.

Mario Kart God said...

Awesome news! Can't wait till the weekend!

GmDude66 said...

Great news, can't wait for the release of R7..

Morgan said...

Guys I'm leaving this Thursday for my house in Bahamas and I won't be back till the 18th. So just so you guys know I won't be here.

Morgan said...
This comment has been removed by a blog administrator.
StrmnNrmn said...

Sorry for my quiet spell last week - I was so busy I didn't check the site all week and was quite surprised by all the comments I found!

tsurumaru: Thanks for the heads up on the rom links - I'll go through and remove them now.

kramer: I think I'll try and post brief updates a couple of times through the week. I can appreciate that people get a bit frustrated when they don't hear anything for a few days, and new posts help to keep everything a bit fresher.

wally*won_kenobie: The compatibility does seem to have improved somewhat so it may well be worth waiting until R7 (not least because the mux/screenshot situation will be a bit less painfile for you :)

mario kart god: I've lost track where I'm at now. The problem is some roms are seeing much bigger speedups than others. For instance bits of Super Mario are running at 20fps now (in the castle for instance) whereas the opening sequence of Zelda is still stubbornly running at 4fps. I think you'll be quite pleased anyway :) MarioKart is around 12fps now - I'm not sure what it was before, but it's starting to feel quite playable.

urkel: As I mention above the opening sequence in Zelda is still just 4fps. In game is a little faster now (5-6fps rather than 4), but it's still not running as quickly as I'd like.

xiringu: Good question :) Dynarec should benefit all emulators. Obviously if the emulator runs at full speed through an interpretor then there's no point in going for the added complexity of implementing a dynarec engine too.
The principles of the dynarec are pretty similar for all machines, but some are easier to handle than others (the N64 is fairly easy because it's based on a RISC chip - I imagine writing a dynarec engine for an x86 emulator would be a lot more difficult for instance).

StrmnNrmn said...

imtiaz: It's definitely looking a lot better, but I think sound is still a handful of releases away (maybe R10 or R11??)

kersplatty: Just don't get caught playing games while you should be paying attention :)

laxer3a: Hello! I haven't traced through what the roms are doing - in the Quest64 case it looked like a decompression routine or something like that. It's maybe not 'self-modifying' in the true sense of the word - I think they're actually streaming code in dynamically from the rom at runtime to save on RAM.
I'm actually just trapping their calls to invalidate the cache (i.e. the mips CACHE instruction) and dumping the dynarec contents. It's not perfect, but it seems to work well enough for now.

kemp: You're exactly right - the roms in this case are using 1) to flush the instruction cache.
What's quite interesting is that in emulating the n64 on the psp, my dynarec is itself doing something similar on another MIPS processor (I have to regularly flush the instruction cache to avoid nasty hard-to-trace crashes).
I wonder if in a few years someone will be emulating Mario 64 in Daedalus PSP on a PSP emulator on a new nintendo handheld? :D

morgan: I'm insanely jealous - I wish I had a house in the Bahamas!!

StevIEmw said...

hey strmnnrmn, any ideas if goldeneye is compatable yet??

Disturbd1 said...

steviemw: Its still prolly too big for the emulator to run

Mario Kart God said...

Thanks for the update StrmnNrmn! Are you saying Mario Kart gets about 12 FPS now, when playing 150cc?

infernomax said...

Mario Kart was 20 FPS in Time Trial in R6 im guessing its about 30FPS now?

skater9269 said...

if mario runs at 15 fps for instance is that 1/4 of full speed or higher.because i believe that the n64 runs between 50-60 fps am i correct.

wally*won_kenobie said...

There are a mix between 30 and 60fps games and i think some 15 (this is unknown)

Mario kart is a 30fps game AFAIK, there is no real list out there on the net telling us what has what in it. Im going to be roughly estimating speeds in the next compat list

xlokichaosx said...

All of this is great news, keep up the good work... can't wait until the weekend.

Is R7 going to possibly be able to run Resident Evil 2? PsX Is difficult to emulate from what I've heard, and RE2 was originally for psx, So I just figure that's why its impossible to run it. I don't know anything though, so that's why I'm askin =P

Minotaur said...

Right, I too was at the moment thinking of RE2.

xlokichaosx said...

Yeah dude, RE would totally Rock, that way nobody would have to wait for the SonyPSOne Emulator to get their RE Fix on the go...Nonetheless pay for it. ;)

stee said...

Good work keep it up.

Morgan said...

Yeah StrmnNrmn I love it, it's Andross Island like 65 miles south west of Nassau (the main big island, I think the capital too but I forget). Mangrove Cay is the city, it's about 2 miles big the island. You walk 20 feet out the back yard and your in clear warm Bahama water. It's beautiful I can't wait till I go Thursday, but umm StrmnNrmn I sent you and email. Your gmail to be exact, please respond to it when you can, thank you and R7 really looks promising, great work!

RyanMWolfe said...

Resident evil 2 will probably not work yet, because it is 128MB and that is a lot of Space, For such a new and not improved Emulator.


PS StrnRmn does the Dynerac engine still freez mario Kart, or any other games

wally*won_kenobie said...

resident evil is 64mb to be even more precise.

Should work

wally*won_kenobie said...

Oh i should add

Strmnnrmn did clearly state that games shouldnt freeze like they used to. So games with dynarec off should work with dynarec on

Mario Kart God said...

I can't wait for this!!! I wish it would come out friday though.

Sroon said...

Hey Norm!! will there be any increase of savestates? i mean as in not having to press select after you save for the save to work!
Thanks bro! your the greatest!
Oh by the way i have been following your Blog since it started and I have learned alot from you (Teachings)
so I just want to say thank you for cracking the impostable code bro!
YOU ARE MY HERO!!!

Sroon said...

oh and almost forgot!!!
Has the Expansion pack been fixed so that Donkeykong 64 works or best of all Zelda Majoras mask works!
I thought id throw that in your face too!
great job bro nice to hear from you!!

Sroon(O)(O)tits

Morgan said...

Well first Daedalus doesn't use savestate and also what are you talking about hitting select in order to save? Just save liek you would in the normal game, the only game I've saved in is Super Mario 64 so.

Mario Kart God said...

Is it gonna be possible to reach 30 fps while playing Mario Kart in time mode in R7??? Because StrmnNrmn said that while playing Mrio Kart in 150cc now he got an average 12 fps, while we used to get about 5-6 fps. Now if I am correct that is about a 100% increase. And in R6 we used to get about 14-17 fps in time mode, times that by 100% equals about 30 fps!!! So time mode should be around 28-30 fps in R7 if i'm right.

tsurumaru said...

xlokichaosx

From what I've gathered the PSone would be relatively simple to emulate (remember Sony themselves are bringing out a PSone emulator for the PSP). It is however a bit of a legal minefield and as such noone has yet come forward to code one.

Mario Kart God, calm down! ;)

We'll see when the next build is released. You've got to remember that depending on what effects are used the frame rate will differ.

shallowlife said...

Nice improvement you did there Strmnnrmn , I cant wait for the emulator.

And Morgan you are not the only one going on holidays , Iam about to fly to the usa next week so I hope that I`ll be able to play Marion in the airplane :D

Potato said...

Wow. I am just amazed at what people can do with time and effort. I can barely do a Hello World in C++!

Potato said...
This comment has been removed by a blog administrator.
Morgan said...

Well I'm not going for "holidays" but I'm saying I'm going because I go once a year because I got a house down in the Bahamas. But yeah it's great, I'm actually planning on bringing one of my Bahamian friends back with me, but we'll see how that goes.

xlokichaosx said...

well tsurumaru,
I get where ur coming from, but i wouldnt go so far as to say "PSone Emulation would be relatively simple."
SNES was a long work in Progress with sound and great FPS, but like... RE2??
From what I've gathered, you need like ... 5x the system specs of the system you're trying to emulate, and they havn't even dabbled into trying to do sound yet for n64. I think the only reason Sony can do it, is because they're sony... Not to say strmnnrmn isn't talented (He obviousely is, His hard coding is the only reason this is possible) but I'd still say Sony is definately more expierenced... Homebrewers would be concerned with FPS, Sound, Ect., Sony would be more concerned with making menu's more user friendly, and finding ways to make more the emulator more Profitable. If StrmnNrmn was getting paid a salary for this, I bet with his talent that the n64 emu would already be done.

GREAT WORK StrmnNrmn YOU PWN

tsurumaru said...

xlokichaosx

Actually a lot of the emulator Devs have stated that they think it would be fairly easy due to the fact that the PSOne runs on a MIPS processor (just like the PSP and N64) its requirements would be lower than the N64's and its structural functionality would be fairly similar to the PSP. (Both ZX-81 (PSPVBA) and Laxer3a (SNESTYL) have stated they think it would be easier to implement than their own projects). Don't get me wrong I'm not saying it wouldn't require effort and problem solving, I'm just saying that it would in some ways be simpler IMHO. ;)

I wonder what StrmnNrmn thinks on this matter?

Mario Kart God said...

Ahhhhhh, i can't wait (sorry for being impatient) I just had a moment of wanting to play Mario Kart 64 so bad.

xlokichaosx said...

Yeah, I really don't know the first thing about coding, or anything like that. All I know is what I've heard, and alot of people say RE2 won't be playable, but you know what you're talking about. Lets hope StrmnNrmn will have something to say! :D

wally*won_kenobie said...

Im restarting my compat list, its going to be a lot different with this release

Morgan said...

StrmnNrmn just so you know when I leave for the Bahamas I won't have access to a pc for a week, so please take that into consideration. (this is regarding my email)

Mario Kart God said...

Just a couple more days!

Sroon said...

What i meant was having better save compatability and add savestates!
yup a couple of dayz!

Morgan said...

Well I leave early tomorrow morning for Bahamas so I won't get to see R7, and I won't have pc access for a week so he'll be working on R8 when I come back.

Mario Kart God said...

Well, at least you get to go to the bahamas! But i can't wait to play Mario Kart!

Morgan said...

StrmnNrmn I hope you have checked your mail and can give me a response BY tonight or EARLY (3am EST) tomorrow. I sent it to your gmail, hope you answer it! Keep up the great work and don't bother sending me a response if it's not BY those times above because I won't be able to even see a computer for a week.

Mario Kart God said...

Morgan it would be awesome if we could get the emu on Friday, but if you release it saturday thats fine to.

Morgan said...

Wouldn't matter I need the emu to be released 4:30am Tomorrow EST. If it comes out before then I would be good, but as you can see that's not going to happen!

skater9269 said...

I doubt strmn would update us on any progress made between when he said it will be released this weekend. wouldnt it be awesome if he made more great speed(or compatibillity) improvements since then and was gonna surprise us like we run mario and its like 30fps < yea right but still it would be sweet i cant wait.

Morgan said...

Please StrmnNrmn answer my email before I leave at 4:30am EST, thanks!

Mario Kart God said...

WHOOOOOOO, can't wait till the big day!!!

Urkel said...

same here MKG, same here :)

wally*won_kenobie said...

morgan,

he has more important things to do other than attend to your matters :)


Surely you could find an internet cafe somewhere when you go!!

Morgan said...

Surely I can't Wally and I'd appreciate you staying out of my business. With all do respect you don't have anything to do with the matter at hand, it's between StrmnNrmn and I. And no there isn't a cafe anywhere near the island I will be on. The island MY HOUSE is on is only 2 miles long and it's a poor island the people there have homes as big as my garage. They don't have any computers on the island and the closest one is miles and miles away and not in any way of contact. I think since I have a house down there that I would know if I had computer access. I do have Direct Tv down there but that's about it. Once again Wally with all do respect, I'm just a little stressed right now.

Morgan said...

And yes I did bring the matter here by posting about my question to StrmnNrmn. But it is very important that StrmnNrmn at leasts reads my email by the time I leave. I just hope this thing gets sorted out so I can get to lounging in the sun smoking a blunt in the Bahamas!

infernomax said...

Morgan WTF is your prob... wally was just pointing something out... morgan Dude dont be such a d1ck about it

Mario Kart God said...

Hey guys just stop arguing. Morgan have a great time in the bahamas!

xlokichaosx said...

ANYWAY. Anyone know if it's supposed to be released on Saturday, or is it Sunday? (assuming Friday is out of the question)

Morgan said...

Like I said I "was" stressed out and I told him with all do respect, Wally no offense but those comments were said when I was in the wrong mood. Feel me, and MKG don't worry I will! I bought a box of 50 Dutch Masters so I'm ready to roll up!

Sroon said...

I still havent tried drug...... herbs yet! is it cool?

Mario Kart God said...

Well I guess Morgan has left already. And lets stay on topic.

Sroon said...

YAY Morgans gone!
lol!
hopefully there will be a better gui and savestatrs added!
(O)(O)

Mario Kart God said...

Ahhhh, it seems like the never will get here! The days are going so slow now!

davey_crockett said...

I feel the same Mario Kart, wow this is gonna be so exciting when it's out

wally*won_kenobie said...

All I can say is What The..


Roving Enterprises TM