JSNES: A Javascript NES emulator
benfirshman.com
benfirshman.com
A while back, http://6502asm.com/ popped up with a web-based 6502 (chip used in the NES, Apple ][, and many others) emulator. I stumbled upon it and in a matter of hours, I patched it to become the first (to my knowledge) dynarec emulator in JS. You can see a live version here: http://ironbabel.googlepages.com/6502.html
This same technology could easily be applied to JSNES for considerable speedups.
Assemblers don't compile, they assemble.
Neat hack though!
He is taking the assembly code you type in and translating it to machine code, then he's translating the machine code to JavaScript source, then executing that source directly.
Translating a program from one language to another is compilation, by definition. While the first translation step could also be considered assembling, the second cannot.
You have a hash table mapping an address to a block length, block function, and a hash/checksum. Your execution loop looks for the program counter in the hash table. If it exists, you take <block length> bytes from the program counter, hash/checksum it, and compare it against the hash table entry.
If the cache entry misses, you need to recompile the block. Starting from the program counter, read instructions and rather than executing the operation (as in an interpreter), you append the operation's code to a string. When you hit a branch and output the code, you break the loop. You now have the code of the block.
Wrap it in a function, eval it, and put the function in the cache.
There are a number of optimizations that can be done (see IronBabel for specifics), but the easiest is to group blocks into a state machine. Simply push all of the code into a big while(true) { switch(PC) { case 0xDEADBEEF: ... }} and break out if you hit a block that isn't in the state machine.
Next step, do like Cappuccino and "compile" the NES rom's back to native Javascript. At that point, I think my head might explode.
Agreed. Google Web Toolkit was a step in this direction. Once you can develop in a language of your choice (Java, Python, Ruby, etc.) and have the user interface code auto-magically compiled into equivalent functional HTML5 and JavaScript so that it can run on any browser, I think Flash will begin its fall and decline. However, I think Adobe probably has some skunkworks project to target ActionScript/Flash as pure HTML5+Javascript output (they'll sell the graphical development tool and give up on browser domination).
Adobe is working towads the holy grail of UI design tools. To build the tool that lets wireframes become functional. They are working on FXG. I imagine, internally, that there are major turf wars on continuing with Flash native vs HTML5.
http://opensource.adobe.com/wiki/display/flexsdk/FXG+1.0+Spe...
JavaScript is one of the few languages where you keep being surprised with what can be done with it. For the longest time I saw it as pretty limited, then one day somone shows me an apple II emulator in javascript.
Java on the other hand (as much as I hate java) can do high performance stuff. Here is a java applet NES emulator, with sound: http://www.nescafeweb.com
He also predicted speeds bordering on C within a few years.
[EDIT] Source: http://perseus.franklins.net/hanselminutes_0130.pdf
I can't believe what I just witnessed: a playable javascript NES emulator. I think this is a pretty significant step for the "web," actually.
See here for more info about Mac Chrome:
You can actually use the data uri scheme for <audio> src. So if you generate WAV data on the fly via JavaScript, and encode it as Base64, you can play it immediately.
Here's an example of on-the-fly WAV generation w/ <embed>, but I just tried it in Firefox with <audio> and that works as well:
http://sk89q.therisenrealm.com/2008/11/dynamically-generatin...