Hacker News new | past | comments | ask | show | jobs | submit login
NES Emulator in Javascript (benfirshman.com)
144 points by icefox on May 16, 2010 | hide | past | favorite | 41 comments



Tried it on different browsers, and wow, this is an alternative way to check the efficiency of the javascript-engines.

Opera and Chrome comes best out so far, with a steady 57-59 FPS. Safari says it comes with 30-33 FPS, but stopped rendering. It still emulated the game fairly well though. I heard I died from a mushroom @ the super mario bros emulator. Firefox had a measly 4-5 FPS.

I tried it with Internet Explorer as well, but it wouldn't start without the sound-engine made in flash. Obviously installing flash for IE isn't that easy, so I didn't bother.


It doesn't work at all in IE. It's not anything to do with flash, rather, IE's lack of support for canvas. It gives a JS error on page load (object doesn't support this property or method), on line 28 where it tries to call Canvas getContext.


If you were to convert the nes asm into javascript how much faster would it run?


Insanely, insanely fast. I created a fork of http://6502asm.com/ that dynarec'd 6502 machine code to Javascript and cached the functions; the speedup was on the order of 25-100x, depending on the code in question. I used to have it hosted, but it's since disappeared; let me go throw it on github.

Edit: It's up at http://github.com/daeken/Dyna6502Asm now. If someone could actually host it so that people can test it out, I'd greatly appreciate it. I'm writing up a small document on how it works, for those who are curious; it'll be in the repo in a few minutes. Oh, and I believe that this was the first dynarec'd emulator for Javascript; this was back in December 2008.

Edit #2: Strategy document is up, here: http://github.com/daeken/Dyna6502Asm/blob/master/strategy.md. The same exact mechanisms should work just fine with this emulator, if someone is so inclined. One thing you ought to do, though, that I never got around to, is adding a simple checksum to the code cache. When you attempt to execute a block, check to make sure that the code in memory is the same as the one in the code cache; if it's different, then the code has been modified and needs to be recompiled.

Edit #3: Thanks to icefox, I now know how to host on github! http://daeken.github.com/Dyna6502Asm/


That's AWESOME.

I think I remember seeing this last time JSNES had some exposure. I'm going write support for this right now...


Let me know how it goes. If you run into anything, shoot me an email or hit me up on IRC: Daeken on freenode.


on github simply name your branch gh-pages and then it will be 'hosted' on http://daeken.github.com/Dyna6502Asm/


It depends on how aggressively you optimize. In the worst case scenario (the compiler just copies the code that the interpreter would run) you end up with the same performance as the interpreter.


Found a paper http://www.jordansmind.com/files/Laughy-DBT.pdf last night titled "Implementation of a Dynamic Binary Translator for the Nintendo Entertainment System (NES) console" Converting had a ten time speedup. Given that the js engines we have today determining the best way to convert the nes asm to js seems like it would be a very interesting project.


I wonder, are all those NES which can be played there public domain?


No. They aren't "abandonware" either, as many of them can still be bought for the Wii's virtual console.

The Emulator is pretty impressive, though. Especially the sound emulation/output - iirc the JavaScript Audio API has no "direct" way to play arbitrary wave forms. I guess it uses the data URL scheme and a simple WAV header!?


It says on the site that it uses Flash for dynamic sound. I looked into this a while ago and dynamic playback in <audio> is being prototyped by at least a few people, so maybe someday soon it'll hit the mainstream.


Actually it uses a small Flash app to generate the sound (see the News section at the bottom of the page). I wonder if data URLs could be used like that though?


I tried that, but unfortunately data URLs are far too slow. Firefox 3.7 has some exciting audio APIs though:

https://wiki.mozilla.org/Audio_Data_API

I've been meaning to write a JSNES prototype for them.


Thanks, looks like a very useful API. In addition to synthesizing audio it also has the ability to do spectrum analysis: http://vimeo.com/8525101


This demo seems to do just that:

http://acko.net/files/audiosynth/index.html

Edit: It's not exactly as "dynamic" as I thought. The different samples are generated once and then played in a loop. So the audio data is not generated in real time, but it is generated.


Many games are essentially open at this point, in that Nintendo and other publishers haven't asked that the roms not be distributed.

The ones they really care about they have an ESA request on. See http://www.romnation.net/srv/page/esa.html (WARNING : horrible flashing ads). This includes all Mario games, Zelda, and so on.


Are they licensable? If not - in my world they become public domain.


You can buy many of those games on Nintendo's Virtual Console service, so yes, they are licensable.


Yay Bubble Bobble!

But seriously, what's with Firefox canvas being so much slower than Chrome?

It can't be javascript speed because Firefox is just as fast as Chrome for JIT JS performance.


Long story short: Firefox is just slow. It's tracing engine doesn't cope with heavily branching code like that in an emulator, and it just falls back to interpreting it. It's nothing to do with the canvas. (In fact, Chrome's canvas is far slower than Firefox's.)

Chrome is wildly fast because it compiles everything to machine code instead of JIT compilation.


This is awesome. Even Konami code works in Contra!


Hope Simucal sees this. Sounds like he was working on an NES emulator 1/2 yr ago. They can probably team up.


Cool. How do you do the sound in javascript?


I did not looked deep enough, but there's a swf file called jssound.swf in the codebase.

EDIT: Well, from the index.html:

"It has been in the pipeline for a while, but JSNES can now play sound! There is no way of playing dynamically generated sound in JavaScript, but I have created a tiny Flash application reads a buffer from JavaScript then writes it to the sound card."


ok cool. yes pure javascript sound has always been elusive.


so laggy, can't play it on my laptop.


On my 2 year old laptop on Vista + Chrome 5beta, it runs just as fast as it did when I played it sitting at a TV screen eons ago. It's really quite impressive.

... or, rather, it's really quite depressing once you realize this is 25-year-old technology finally making its appearance into (some!) browsers without requiring plugins.


Why is that depressing? I find it amazing and exiting.


Why do you find it amazing? I saw this first probably 10 years ago with java applets. Is it more amazing because applets are unfashionable?


No, it's because Applets, like Flash, are a narrow, specific, proprietary setting. Nobody has ever questioned that Java can handle something like that.

The interesting part here is that it is showing that browsers and JavaScript, with a very different approach than Flash or Java, can now handle this sort of computationally intensive, graphics intensive application without any OS-specific plugins.


> computationally intensive, graphics intensive application

Wake me up when that means something from only 5 years ago and not 20.


I suspect a pure Javascript Gameboy Advance emulator is not too far off :P


getting code to work is easy, getting the largest most influential tech companies to cooperate on building a platform for you to deliver applications that work on most devices in the world entirely for free with no real financial incentive.

thats a plain miracle.



applets don't work on [insert random browser on device]


yeah, but as people have noted this doesn't work on anything other than the latest builds of chrome and safari. Still impressive but this is not exactly a compatibility win.


But this is an implementation based on open web standards. It'll eventually work on mainstream Firefox, Safari, Chrome, Opera and maybe (if <canvas> is supported and planets align) IE9.


This is too slow to play on my Dell i7 720, which I think says more about Dell's screwed up architecture causing horrible CPU performance than it does about this code.


guess this kills my nacl port.


I tried to play it but it is not taking input from keyboard

I am using ubuntu 9.10 and the default firefox.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: