Wishful Coding

Didn't you ever wish your computer understood you?

How I learned to program in 10 years

Inspired by a blog post from Julia Evans with an identical title, here is how I learned to program.

Unlike Julia, I learned most of my programming from my own hacks and projects. I feel like the jobs I had where plateaus in my learning curve rather than spikes.

I must have stared somewhere mid-’00, before which I was raised without much technology in my life. I did not watch TV, smartphones where not invented yet, and I did not have my own computer.

I’m really fuzzy about the timeline, but several different things happened, seemingly independently of each other. I did not consider myself a programmer during any of this.

My brother likes to take apart computers, so we kind of grew into the system administrators of our tiny school, recombining and configuring donated hardware into usable computers.

Back when Game Maker was still owned by Mark Overmars, I made a lot of silly games with friends. Later we also used an RPG thing called Elysium, which ceased to exist.

This got me in touch with a guy who wanted to make a clone in Java. This lead me to read Head First Java, to date the only programming book I read extensively, including all the exercises.

I always played with LEGO, but at some point I got a LEGO RCX for my birthday, and built a lot of robots with it. Like Game Maker, it was programmed by dragging blocks around.

A guy at my school taught me and a friend some Basic. Nothing much came of this.

I did a course in web development. I learned HTML and a tiny bit of CSS, but not how to use classes and ID’s, so for a while, I would just style tag names, and use frames for layout.

Later a woman at my school who was a web designer taught me and the same friend proper HTML and CSS and introduced us to PHP.

So at this point in time I know some Java and PHP. What follows is a period where I teach myself a lot of things, do a lot of crazy hacks, and get more serious about programming.

I learned JS, Python, and Clojure by reading things on the web, and just building random stuff with it.

I used to have a folder of projects and ideas. But this was before I knew version control, and before Github. At some point I tried moving the whole thing into SVN, but I messed it up. This whole folder and its ~50 projects is lost in time.

It contained crazy things like a site that was only text. The background was a PHP-generated text image, the borders where text images, the text was text. They where only distinguished by color. I also had a memo app when local storage was new. And a HTML editor with a circular context menu.

The only thing that remains form this era is my first blog. This is your only chance to see code I wrote in 2009.

I did an internship with Oblivion, where I did some CSS and HTML prototypes. Then I did an internship with Eight media where I did Django projects.

I also started doing my first paid jobs for this designer at the school. Followed by Wordpress websites for other people.

Now we are getting to “recent history” where I know dates and stuff is on Github. By this time I was doing freelance webdev an knew several programming languages quite well.

So far, I did not have many peers. All of my friends that where interested in technology moved on to television, photography, theatre, and other things.

This changed radically in 2012, when I went to Hacker School, where I learned a ton of things and met a ton of people who where interested in the same kind of things as me. Amongst many other things, I learned C and Haskell.

After Hacker School I started living on my own and tried having a job at Silk where I did some Haskell and JS things. This did not work out very well, so I went back to freelancing.

Around this time I got a LEGO NXT. This time I programmed it using Not Exactly C. I also learned how to make NXT sensors using PICAXE. Later, when Arduino became a thing, I moved my hardware hacking to that platform.

In 2013 I did some freelance work for Freenom, who then hired me to do Python stuff. I worked there for over a year.

This brings me to today. I quit Freenom to see some more of the world. Visit far places, take on odd projects, find weird jobs. Help in this area is highly appreciated.

Pepijn de Vos

Catch 'em All

Isn’t it frustrating that some of the Pokemon in the first generation Game Boy games are exclusive to one of the games? There is no way to get Oddish in Blue and no way to get Bellsprout in Red.

The intended solution is to trade Pokemon with friends to get the missing ones. There are even Pokemon like Graveler that only evolve when traded.

Sadly, not a whole lot of people own a Game Boy, a Pokemon game, and a link cable. So I decided to get creative and trade Pokemon with my Arduino.

Needed parts:

  • 1 x Game Boy (Color and Advanced work too)
  • 2 x Pokemon cartridge (Red and Blue)
  • 1 x Arduino board
  • 1 x Link connector

The Game Boy communicates over what is essentially a 5v SPI bus that can act both as a master and a slave. At 8KHz it is slow enough to bit-bang, so it works on any 3 GPIO pins.

I salvaged a connector from a GBA wireless adapter, and hooked it up to 3 Arduino pins with 1KΩ series resistors to be sure. Because both ends can in theory drive the clock line, I don’t want to short them out.

|  6  4  2  |
 \_5__3__1_/   (at cable) 

Connect to the Arduino as follows:

  1. DNC
  2. Serial Out
  3. Serial In
  4. DNC
  5. Serial clock
  6. Ground

I read through the Pokemon disassembly in my previous post, so I won’t go into too much detail about the data transferred. Here is the data structure that contains the Pokemon, if you are interested.

Long story short, I emulated the protocol on the Arduino, acting as a slave. So go Check out the code, and finally catch ‘em all.

The Arduino has one Pokemon stored in its EEPROM, which it will trade with you. So even if you have one cartridge, you can trade Graveler with the Arduino and then trade it back to get a Golem.

If you have two cartridges, you can trade a Pokemon with the Arduino, swap the cartridge, and trade it back.

No Pokemon where harmed in the making of this program. Or, not that many anyway. One Weedle turned into Slowpoke and then into Missingno. Several other Pokemon are now nicknamed Pidgey, but are otherwise doing fine.

I think it’s now complete, allowing multiple trades in one session, including canceling trades. Don’t forget to reset the Arduino when you reset the Game Boy, or bad things will happen.

Pepijn de Vos

Grep your way into Pokemon Red

I’m trying to figure out how the serial communication between two first generation Pokemon games works.

Armed with a CPU manual and opcode summary, I’m grepping through a disassembled ROM.

Reading the section in the CPU manual about serial communication, I found that it uses two registers at addresses ff01 and ff02. Grepping the source reveals constants/hardware_constants.asm that defines rSB and rSC.

Grepping those leads me to home/serial.asm and engine/overworld/cable_club_npc.asm. I’m not sure which does what, but in the former I find a promising label called Serial_ExchangeBytes. Grepping for that leads me to engine/cable_club.asm.

The function is called 3 times in this file. Looking at the comments and at my opcode reference, it seems to be using ld to load 3 16 bit registers for the arguments. hl stores the data to send, de the data to receive, and bc the number of bytes to send.

Grepping for wSerialRandomNumberListBlock leads me to wram.asm where all 3 are defined. Looking in the manual, WRAM refers to the 8 4Kb RAM banks that can be mapped into certain address spaces. I’m assuming ds n means a data section of n bytes, but I’m not sure.

So the first command sends 17 bytes of random data, while wSerialRandomNumberListBlock is only 7, but followed by 10 bytes of wLinkBattleRandomNumberList. I could grep further, but it’s random data, so whatever. On to wSerialPlayerDataBlock

This block contains all the good stuff. We know we are looking for 424 bytes of data, but here we see them clearly labeled.

First there is a preamble, like with the random block. No idea what it’s for. Then there is space for the player name, followed by the number of Pokemon you cary and their ID. PARTY_LENGTH is just defined as 6.

Then there are 6 party_struct that contain more info about your Pokemon. I know because I found this page earlier.

I’m grepping for those bytes in the preamble. They seem to be used for a bunch of other things, completely unrelated to the player data.

I’m just randomly trying to read bit and pieces of the code and comparing with bytes coming out of the Game Boy. In the data I see a lot of FD, and in the code I found SERIAL_PREAMBLE_BYTE which so happens to be defined as FD. These seem to be for synchronisation only.

That’s it for now. There is plenty of stuff I don’t understand, but at least I know the structure of the data that is sent over the link cable.

Pepijn de Vos