Slashdot Mirror


A JavaScript Gameboy Emulator, Detailed In 8 Parts

Two9A writes "JavaScript has shed its image of being a limited language, tied to DOM manipulation in a browser; in recent years, new engines and frameworks have given JS a reputation as a language capable of bigger things. Mix this in with the new elements of HTML5, and you have the capacity to emulate a game console or other system, with full graphical output. This series of articles looks in detail at how an emulator is written in JavaScript, using the example of the Gameboy handheld: starting at the CPU, and (as of part 8) running a copy of Tetris."

23 of 62 comments (clear)

  1. There are already tons of Game Boy emulators... by Anonymous Coward · · Score: 5, Funny

    ...but whenever I use one, I can't help but think "I sure wish this was written in Javascript, so there wouldn't be any way to save my game. Saved games are for pussies. And sure, it wouldn't support sound, but who needs that when you've got the beautiful noise of your computer fans running on full blast, thanks to its excessive CPU usage!"

    1. Re:There are already tons of Game Boy emulators... by Anonymous Coward · · Score: 3, Informative

      And sure, it wouldn't support sound, but who needs that when you've got the beautiful noise of your computer fans running on full blast, thanks to its excessive CPU usage!

      This JavaScript NES emulator supports sound: http://benfirshman.com/projects/jsnes/

      Here are some other JavaScript audio demos:

      http://hacks.mozilla.org/2010/04/beyond-html5-experiments-with-interactive-audio/
      http://hacks.mozilla.org/2010/10/audio-data-api-audio-generation-demo/

    2. Re:There are already tons of Game Boy emulators... by AmberBlackCat · · Score: 2, Informative

      While I was playing Super Mario Brothers, the emulator in the first link was too slow in Firefox but worked fairly well in Opera. It's the first time in history that javascript performance actually mattered.

  2. Your jokes are out of date. by pavon · · Score: 4, Informative

    Both sound and client-side data storage are features of HTML5.

  3. Javascript? by Tr3vin · · Score: 3, Funny

    Real men write their GB emulators in Minecraft.

    1. Re:Javascript? by Anonymous Coward · · Score: 2, Funny

      No, *real* men write their emulators by arranging stones in the desert.

    2. Re:Javascript? by Anonymous Coward · · Score: 2, Funny

      No, *real* men write their emulators by arranging grains of sand into brainfuck on the beach.

      Excuse me, but real men use butterflies.
      They open their hands and let the delicate wings flap once.
      The disturbance ripples outward, changing the flow of the eddy currents in the upper atmosphere.
      These cause momentary pockets of higher-pressure air to form, which act as lenses that deflect incoming cosmic rays, focusing them to strike the drive platter and flip the desired bit...

      ...or something like that

    3. Re:Javascript? by RevWaldo · · Score: 2, Funny

      Well, damn, now what am I gonna do with this infinitely long spool of magnetic tape?

  4. Sound? by Mr.+Sketch · · Score: 5, Interesting

    I'm curious how he plans on handling dynamically generated sound from the GB ROM. Doing CPU and Graphics are usually the easy parts of emulating, but getting smooth dynamic sound without much latency is the challenge I've had to deal with when doing web-based emulators. Most web-based systems are designed to load a static set of sounds from a server, not dynamically generate them in the code.

    Flash 10 provides some dynamic sound capability, but it has a rather large latency (~250ms). I blogged about this while writing my NES emulator in flash.

    I read through these articles hoping for some insight on dynamically generated sound, but it doesn't look like he's gotten that far.

    1. Re:Sound? by BZ · · Score: 3, Informative

      You might be interested in https://wiki.mozilla.org/Audio_Data_API#Writing_Audio and https://github.com/corbanbrook/dsp.js

      Not sure what the latency is, but if it's too high for uses like this, please let the people involved know? They want this to actually be useful for exactly the sort of things you're talking about.

    2. Re:Sound? by Mr.+Sketch · · Score: 2, Interesting

      That Mozilla link is along the lines of what I was thinking of for dynamic audio. Too bad it's not supported by all browsers, but it would be a start. With some proper architecture, it should be easy enough to add support for other browsers when they support a similar feature.

      Thanks!

      I should probably start looking at what it would take to port my Flash NES emulator to JavaScript. I wrote it in Haxe with the goal of doing a JS version at some point. However, at the time, only Chrome could even come close to running the JS fast enough, but now most modern browsers should be fast enough (I'll have to do some performance tests). It should just require minor tweaking to replace the flash calls with JS/HTML5 calls without having to re-do and re-work the bulk of the logic.

  5. ... why? by Mongoose+Disciple · · Score: 4, Insightful

    I've said it before, and I'll say it again:

    You can write damn near anything in JavaScript if you really want to, but the better question is if you should.

    And yes, that includes half (but only half!) of the stuff you'll find done in JavaScript in web apps.

    1. Re:... why? by AstrumPreliator · · Score: 2, Insightful

      The same reason someone would write an interpreter or compiler for an esoteric programming language such as Brainfuck or Piet. Or make a homebrew computer. It may not be the most practical thing to do with your time, but it's fun and interesting. The only reason a geek needs to do something is, "I want to."

  6. Synthesis in real time? by tepples · · Score: 2, Informative

    Both sound and client-side data storage are features of HTML5.

    I've heard of the HTML5 <audio> element. But as I understand it, it's designed for playing pre-recorded audio, not audio synthesized in real time as would be necessary for an emulator. The only reference that the page linked from "sound" makes to synthesis is in the context of text-to-speech for a textual description track. Even if you generate .wav into a data: URI, I know of no way within this spec to link multiple data: URIs to play gaplessly.

    1. Re:Synthesis in real time? by BZ · · Score: 3, Informative

      Indeed. https://wiki.mozilla.org/Audio_Data_API#Writing_Audio and https://github.com/corbanbrook/dsp.js are closer to what's needed for realtime synthesis. Hopefully it will make its way to other browsers too.

  7. Permission denied by tepples · · Score: 2, Interesting

    You can write damn near anything in JavaScript if you really want to, but the better question is if you should.

    If your users have access to a web browser but lack the privileges to install a native client on machines that they use, then you have no choice but to write your application in a language that runs inside the web browser. This is often the case in break rooms, public libraries, and university computer labs, where the user isn't the owner, or with locked-down appliances, where even the owner of the device isn't an administrator.

    1. Re:Permission denied by evilviper · · Score: 2, Insightful

      If your users have access to a web browser but lack the privileges to install a native client on machines that they use, then you have no choice but to write your application in a language that runs inside the web browser.

      If your users have access to emacs, but lack a web browser... ...never mind.

      Web browsers are everywhere, because they are useful applications. They weren't written in Flash, or Javascript, or anything like that. That's part of the reason they are useful to begin with.

      I agree that there need to be web interfaces for many things, because, in a pinch, it might be necessary to work that way. But that's a long way from saying you should need an septuple-core CPU, a browser that hasn't been released yet, and a super-high-speed internet connection, to check your e-mail... Google maps is popular, but that's not to say it does anything that the old plain HTML Mapquest didn't do just as well before it came along (and on 100MHz CPUs over dial-up without trouble).

      That's the question, today. Do you need some massive web interface made of hacked up javascript, or do you just need to have a little app on the server that outputs plain old HTML? Yeah, if I can't navigate your site because you use drop-down boxes for all navigation, and omitted a "Go" button in favor of script because you don't like how the single extra button clashes with the design of the site, I say screw you. Unless I've got absolutely no other choice, NoScript and I are going elsewhere. When Slashdot eliminates the "Classic interface" option, I'll be leaving here, too. I use the web for the content, not to experiment with every random idiot's ideas about interface usability. Hell, anyone who has been on the web long enough will tell you what a nightmare it was when every site was laid out differently, before the world collectively standardized on the format everyone is familiar with today.

      Games? Developers are just be stupid by not putting it in a single EXE that can be run directly. Even a seriously locked down computer will let users download and run a file. And if you're on a ridiculously locked down system which doesn't allow even that... maybe you'll have to live without playing that one game on this computer you're obviously not supposed to be playing games on...

      --
      Slashdot gets worse every day... Pipedot: News for nerds, without the corporate slant
    2. Re:Permission denied by tepples · · Score: 3, Insightful

      Web browsers are everywhere, because they are useful applications. They weren't written in Flash, or Javascript, or anything like that. That's part of the reason they are useful to begin with.

      Unlike your application, the web browser has already been approved by the appliance maker or the PC administrator.

      Do you need some massive web interface made of hacked up javascript, or do you just need to have a little app on the server that outputs plain old HTML?

      On machines with an intermittent connection to the Internet, a massive JavaScript interface using a cache manifest and localStorage can be helpful.

      Games? Developers are just be stupid by not putting it in a single EXE that can be run directly.

      This would need the code and data combined into one file, much like a self-extracting archive. That could suffice for Windows, but Mac OS X and Linux can't run Windows EXEs without CrossOver Games or some other Wine variant. JavaScript runs on any platform with a modern browser.

      Even a seriously locked down computer will let users download and run a file.

      Not always. The administrator of a PC used by the public can set Software Restriction Policies to block execution of, for example, executables located inside %USERPROFILE% (the user's home directory).

      And if you're on a ridiculously locked down system which doesn't allow even that... maybe you'll have to live without playing that one game on this computer you're obviously not supposed to be playing games on...

      Video game consoles and other set-top appliances typically can't run native binaries that are not digitally signed by the manufacturer of the appliance, but some can run web pages using JavaScript and/or SWFs using ActionScript. Yet the majority of home users routinely buy consoles instead of home theater PCs.

  8. Ode to JavaScript by CobaltBlueDW · · Score: 4, Insightful

    JavaScript is truly a horrifyingly discussing,
    intrinsically retched,
    soul darkening, succubus from the abysmal depths of conceivable depravity.

    To know its stench
    is to know the crippling limitations of our future.
    To recognize its sloven decadence
    and remain indifferent
    is to burn the righteous.

    No faith,
    however moving and spectacular,
    could light a path of its continuation.
    No argument,
    however complex and equivocated,
    could elevate such a encumbering and wearisome burden.

    There is no failure so inadequate,
    or stagnation so bereft of utility
    as that of JavaScript.

    Thankyou.

  9. Vic20 in Javascript by Anonymous Coward · · Score: 4, Informative

    And here's a Vic20 emulator in Javascript HTML5
    http://www.mdawson.net/vic20beta/vic20.php

  10. Compile the JavaScript by Anonymous Coward · · Score: 4, Informative

    Saying that you shouldn't write this in JavaScript because of the limitations of JavaScript in web browsers is like saying people shouldn't write Apps in Java because Applets are limited. It's not JavaScript that is the problem per se, but the limitations of the Interpreter. If you want to write something like this in JavaScript, you should consider compiling the JavaScript instead.

    Using the Unity Framework ( http://unity3d.com ), you could write the emulator in JavaScript, compile it, and have a cross platform emulator that would work on Mac, PC, Wii, Iphone, Android, and PS3 with hardware accelerated 3d, support for sound, etc. Something like 20-25 of the top 100 Iphone Apps were made using Unity, and it is a pretty good platform for making 3d games.

  11. -1 Troll by rubypossum · · Score: 2

    I kind of like Javascript, I have written and maintained HUGE applications in many languages. To be honest, the worst was Java, not Javascript. Javascript is light, easy to understand, highly extensible (see JQuery or mootools) and once something is written to the standards it usually runs EVERYWHERE there is a reasonably modern web browser. Something which is not true of Flash or Java. Just look at how many people use it and write in it! Talk about a successful language! I'm fluent in Ruby, Python, Perl, Java and UNIX/Linux standard C. I've used C++, C#, AS3 and even Scheme on various professional jobs before. And I think I like Ruby best, but not by much. Javascript has a certain simple elegance that even Ruby doesn't have. It's what Java could have been. If Gosling hadn't been so strict type happy.

    --
    I have a theory that the truth is never told during the nine-to-five hours. - Hunter S. Thompson
  12. Re:... Impressive. by Sycraft-fu · · Score: 2, Insightful

    That actually is the case. If you look in to it, you find that fully functional Gameboy emulators, with sound and everything, ran well on 486-Pentium class computers. They were written in C/C++ of course, often with some assembly thrown in. The Gameboy is not challenging to emulate since it is low resolution, gray scale (with very few steps), has a small memory, slow simple CPU, and so on.

    All this really demonstrates is that even if your language sucks and runs on a slow platform (webbrowser's interpreters don't count as fast) that eventually there's enough CPU power to throw at it. It isn't really a testament to Javascript, it is a testament to modern CPUs.