Emulating random numbers on the web


Patrick's Pocket Challenge is a procedural puzzle game for the Nintendo Game Boy. Until now, you had to download the Game Boy ROM to your computer and play it locally in an emulator, but it's now playable in your browser!

If you're interested, here's some background on why I had to find the right web emulator to achieve this. The game doesn't seem very technologically advanced, right?

Random Number Generator

OK, calling the game procedural is a bit of a stretch; I just wanted to use a buzzword. It consists of randomized puzzles, which work like this: There are 28 tiles in each puzzle, and 7 different kinds of objects that are randomly placed on these tiles. There's Patrick (you), and 6 special tiles that remove other tiles when stepped on.

The random number generator I'm using in this assembly game relies on one simple fact: On the Game Boy, the uninitialized RAM is filled with "random" undefined values after boot. I use three bytes of RAM as a seed for an RNG algorithm I found on the classic Game Boy Dev'rs website (which I then map from values between 0–255 to 0–27). It works great; the code is here.

That is, it works great on a real Game Boy, and on good Game Boy emulators that fill the RAM with random values on boot. I quickly found out that many emulators just initialize the RAM with zeroes. There are several good and hardware-accurate desktop emulators that emulate random uninitialized RAM, but sadly I wasn't able to find any such accurate emulators written in JavaScript.

That meant that every level looked like this when played in the browser (the numbers on the top are the "random numbers" + 1; all the special objects are put on the first tile, tile #0):

Not very challenging. So I dropped the idea of having the game be playable in the browser, recommended people installed an accurate emulator like BGB, and shipped it. That seemed to be what most other Game Boy homebrew developers did too, at least those who write games that rely on accurate emulation.

Gambatte + archjs

But then I found Dungeon Ball, a Game Boy game that's playable in the browser. Of course, that's not amazing in itself, since it could've been using one of the inaccurate JavaScript emulators. But no, it's using Gambatte, a very accurate emulator (it's even open source, unlike BGB). And Gambatte emulates random uninitialized RAM!

As it turns out, Gambatte has a libretro implementation called gambatte-libretro. Libretro is a cross-platform API powering RetroArch, a nifty project to make games for old consoles playable on a multitude of platforms. Including web browsers, using the Archjs frontend.

(Interestingly, Gambatte was ported to libretro by a fellow Norwegian, Sindre Aamås. The assembler I'm using, RGBDS, was ported to Linux by another Norwegian, Vegard Nossum. Pretty crazy that I've been indirectly helped by two other Norwegians to bring this game to you. Made in Norway!)

So now you can finally play this Game Boy game right in your web browser! Are you ready for the… challenge?

Note: It sadly seems that persistent save data isn't retained between sessions in the web emulator. It also doesn't have on-screen controls. The perfect web emulator is still a pipe dream.

Get Patrick's Pocket Challenge

Download NowName your own price

Leave a comment

Log in with itch.io to leave a comment.