¡Ajipirijou! josé albornoz's blog

How I built my own Twitch-Plays-Pokémon: Part 4

I gave this talk at Baruco 2014.
I'm publishing it now as a series of blogposts. Make sure to check out the first part.
You can find the slides in my speakerdeck. You can also see the recording in YouTube.


Ruby Version: Irresponsible Ruby

In EuRuKo 2013, Steve Klabnik gave an amazing talk explaining the concept of "Irresponsible Ruby".

Steve explained us how following the same procedures and patterns we do in our daily work can reduce our freedom and therefore, our creativity.

Steve's Quote

By focusing on how easy it is to test a feature or its modularity,
we're closing doors that might be very valuable.

Irresponsible Ruby is:

Irresponsible Ruby is using Ruby as you would in a hackathon: as creative as possible.

why's Quote

I loved the concept and wanted to apply it to this project.

Jano was testing every CPU instruction with rspec, which besides being tedious, it actually stopped me from working the way I needed.

Ruby Version: Figuring out How to test the Emulator

I still had to test the emulator though, but a traditional rspec setup wouldn't do.
Unit tests would help even less.
What I needed was some sort of master test.

The GameBoy CPU has 6 main 8-bit registers, which are just tiny numbers that the CPU can interact directly with.
The emulator executes each instruction from the GameBoy ROM.
Those numbers typically change after each CPU instruction.

CPU Steps

I thought that I could run the working javascript version of the emulator and record those numbers after each cpu instruction.
If I do the same for my ruby implementation then I could compare both sets of registers and see exactly in which instruction my CPU is failing.

Luckily, the specification of how the original GameBoy works was feature-frozen before I was born, so the spec was not going to change.

The first step was modifying the finished javascript emulator from the tutorial so it runs on nodejs.
Then, make it export the memory of each step to a JSON file.
Lastly, I modified Mimey so it compares each step.

Running the "test suite" looked like this: Test Suite

I actually did TDD with this tool.
My working routine was:

  1. running the test suite.
  2. see which cpu instruction fails and which registers were different.
    in this example, the register A was 1 instead of 6.
  3. fix the implementation of the instruction.
  4. rinse and repeat.

Ruby Version: Rendering the first image

With this testing method I started working on the CPU and the memory manager.
The system was good enough so I could reproduce the first 50.000 cpu instructions, enough to start rendering the first image.

Some people say that men who grow a beard are full of patience, because of all the time they put into growing it.
If you think that's patience, try writing a G-A-M-E-B-O-Y emulator for a couple days only seeing hexadecimal addresses and you'll know what patience means.

Patience

After porting part of the GPU, it was time for the emulator to render it's first frame!
The only way I had to display information was the terminal output, so I came up with a very rudimentary way of testing whether my GPU was working.

The system was ready, I just had to press enter and I should see an image.
Crossed my fingers and hit the button...
The only thing I saw was this:

First Rendering

It took me a long time to figure out what was causing the bug,
because the pixels were being processed just like in the Javascript version.
The memory was the same!

What I didn't consider was that the original javascript version, besides being horribly written, had a GPU hardcoded to render using Canvas.
In practice that means that one every four pixels had useful information.
The rest was just nil.

Dividing some number by 4 would do the job.

Diff

Ok, here we go again. Just one enter away.
Crossed my fingers again...

OMG IT WORKED!

Second Rendering


<- Read Part 3 ... Read Part 5 ->