Pokemon Yellow Total Control Hack. Reprogramming the game from the inside!

Game objectives

  • Emulator used: vba-rerecording 23.5
  • Reprogram the Game from the inside

Comments

I've included a detailed writeup here: http://aurellem.org/vba-clojure/html/total-control.html
The following are the highlights:

Introduction

Think of pokemon yellow as creating a little universe with certain rules. Inside that universe, you can buy items, defeat rival trainers, and raise your pokemon. But within that universe, you are bound by the rules of pokemon. You can't build new buildings, or change the music, or change your clothes.. There are some games (like chess), where it is not possible to alter the rules of the game from within the game. No matter what moves you make in chess, you can never change the rules of the game so that it becomes checkers or basketball. The point of this run is to show that you CAN change the rules in pokemon yellow. There is a certain sequence of valid actions (like walking from one place to another or buying items) that will allow you to transform pokemon yellow into Pacman, or Tetris, or Pong, or a MIDI player, or anything else you can imagine.

Background

The speedrun (#2913: p4wn3r's GBC Pokémon: Yellow Version "save glitch" in 01:36.95) by Felipe Lopes de Freitas (p4wn3r), beats pokemon yellow in only 1 minute and 36 seconds. It does it by corrupting the in-game item list so that he can advance the list past its normal limit of 20 items. The memory immediately after the item list includes the warp points for the current map, and by treating that data as items and switching and dropping them, he can make the door from his house take him directly to the end of the game.
When I first saw that speedrun, I was amazed at how fast pokemon yellow could be beaten, and that it was possible to manipulate the game from the inside, using only the item list. I wondered how far I could extend the techniques found in p4wn3r's run.
The gameboy is an 8 bit computer. That means that ultimately, anything that happens in pokemon is a result of the gameboy's CPU reading a stream of 8 bit numbers and doing whatever those numbers mean. For example, in the gameboy, the numbers:
62 16 37 224 47 240 37 230 15 55
mean to check which buttons are currently pressed and copy that result into the "A" register. With enough numbers, you can spell out an interactive program that reads input from the buttons and allows you to write any program you want to the gameboy. Once you have assembled such a program and forced the game to run it, you have won, since you can use that program to write any other program (like Tetris or Pacman) over pokemon yellow's code. I call a program that allows you to write any other program a "bootstrapping program". So, the goal is to somehow get a bootstrapping program into pokemon yellow and then force yellow to run that program instead of its own.
How can we spell out such a program? Everything in the game is ultimately numbers, including all items, pokemon, levels, etc. In particular, the item list looks like:
item-one-id         (0-255)
item-one-quantity   (0-255)
item-two-id         (0-255)
item-two-quantity   (0-255)
.
.
.

Let's consider the button measuring program [37 62 16 37 224 37 240 37 230 15 55] from before. Interpreted as items and item quantities, it is
lemonade     x16
guard spec.  x224
leaf stone   x240
guard spec.  x230
parlyz heal  x55
So, if we can get the right items in the right quantities, we can spell out a bootstrapping program. Likewise, when writing the bootstrapping program, we must be careful to only use numbers that are also valid items and quantities. This is hard because there aren't many different items to work with, and many machine instructions actually take 2 or even 3 numbers in a row, which severely restricts the types of items you can use. I ended up needing about 92 numbers to implement a bootstrap program. Half of those numbers were elaborate ways of doing nothing and were just there so that the entire program was also a valid item list.
The final part of the hack is getting pokemon yellow to execute the new program after it has been assembled with items. Fortunately, pokemon keeps a number called a function pointer within easy reach of the corrupted item list. This function pointer is the starting point (address) of a program which the game runs every so often to check for poison and do general maintenance. By shifting an item over this function pointer, I can rewrite that address to point to the bootstrapping program, and make the game execute it. Without this function pointer, it would not be possible to take over the game.

The Run

Pallet

I start off and name my rival Lp/k. These characters will eventually be treated as items and shifted over the function pointer, causing it to execute the bootstrapping program that will soon be constructed. I start the run the same as p4wn3r's and restart the game while saving, so that the pokemon list is corrupted. By switching the 8th and 10th pokemon, I corrupt the item list and can now scroll down past the 20th item. I shift items around to increase the text speed to maximum and rewrite the warp point of my house to Celadon Dept. Store. (p4wn3r used this to go directly to the hall of fame and win the game in his run.) I deposit many 0x00 glitch items into the PC from my corrupted inventory for later use. Then, I withdraw the potion from the PC. This repairs my item list by overflowing the item counter from 0xFF back to 0x00, though the potion is obliterated in the process. I then take 255 glitch items with ID 0x00 from the computer into my personal items.

Celadon Dept. Store

Leaving my house takes me directly to Celadon Dept. store, where I sell two 0x00 items for 414925 each, giving myself essentially max money. I hit every floor of the department store, gathering the following items:
 +-------------------+----------+
 |##| Item           | Quantity |
 +--+----------------+----------+
 |1 | TM02           |  98      |
 |2 | TM37           |  71      |
 |3 | TM05           |   1      |
 |4 | TM09           |   1      |
 |5 | burn-heal      |  12      |
 |6 | ice-heal       |  55      |
 |7 | parlyz-heal    |  99      |
 |8 | parlyz-heal    |  55      |
 |9 | TM18           |   1      |
 |10| fire-stone     |  23      |
 |11| water-stone    |  29      |
 |12| x-accuracy     |  58      |
 |13| guard-spec     |  99      |
 |14| guard-spec     |  24      |
 |15| lemonade       |  16      |
 |16| TM13           |   1      |
 +--+----------------+----------+
After gathering these items, I deposit them in the appropriate order into the item PC to spell out my bootstrapping program. Writing a full bootstrap program in one go using only items turned out to be too hard, so I split the process up into three parts. The program that I actually construct using items is very limited. It reads only from the A, B, start, and select buttons, and writes 4 bits each frame starting at a fixed point in memory. After it writes 200 or so bytes, it jumps directly to what it just wrote. In my run, I use this program to write another bootstrapping program that can write any number of bytes to any location in memory, and then jump to any location in memory. This new program can also write 8 bits per frame by using all the buttons. Using this new bootstrap program, I write a final bootstrapping program that does everything the previous bootstrapping program does except it also displays the bytes it is writing to memory on the screen.

Finale

After completing this bootstrapping program, I go to the Celadon mansion, because I find the metaness of that building to be sufficiently high to serve as an exit point for the pokemon universe. I corrupt my item list again by switching corrupted pokemon, scroll down to my rival's name and discard until it is equal to the address of my bootstrapping program, and then swap it with the function pointer. Once the menu is closed, the bootstrapping program takes over, and I write the payload....

Other comments

The entire video was played by the computer using bots. I used functional programming to write search programs over different possible game states to find the most efficient way of performing general actions. Some interesting things I developed but didn't use were pretty printing functions to display the game's internal data structures, and an "improbability drive" that forces improbable events to happen automatically using search.
Here are a few example scripts:
 (defn-memo viridian-store->oaks-lab
   ([] (viridian-store->oaks-lab
        (get-oaks-parcel) ) )
   ([ script \]
      (->> script
           (walk [↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
                  ← ← ← ← ← ← ← ← ← 
                  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
                  ← ←
                  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
                  ↓ ↓ ↓ ↓ ↓ ↓ ↓
                  → → → → → → → →
                  ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓
                  ← ← ← ← ←
                  ↓ ↓ ↓ ↓
                  ])
           (walk-thru-grass
            [↓ ↓ ↓ ↓ ↓ ↓ ↓])
           (walk [↓ ↓ ← ↓ ↓ ↓ ←
                  ↓ ↓ ↓ ↓ ↓ ↓
                  → → → ↑])
                 
           (do-nothing 1) ) ) )
This script walks from the Viridian City pokemon store to Oak's Lab in the most efficient way possible. The walk-thru-grass function guarantees that no wild battles will happen by manipulating the game's random number generator.
 (defn-memo hacking-10
   ([] (hacking-10 (hacking-9) ) )
   ([ script \]
      (->> script
           begin-deposit
           (deposit-held-item 17 230)
           (deposit-held-item-named :parlyz-heal 55)
           (deposit-held-item 14 178)
           (deposit-held-item-named :water-stone 29)
           (deposit-held-item 14 32)
           (deposit-held-item-named :TM18 1)
           (deposit-held-item 13 1)
           (deposit-held-item 13 191)
           (deposit-held-item-named :TM02 98)
           (deposit-held-item-named :TM09 1)
           close-menu) ) ) 
 
This script calculates the fastest sequence of key presses to deposit the requested items into a PC, assuming that the character starts out in front of a computer.

Other Comments

The final payload program is multiple programs. I created a reduced form of MIDI and implemented it in gameboy machine language. Then I translated a midi file from [dead link removed] into this reduced MIDI language. The payload program contains both the music data and the MIDI interpreter to play that data. The picture works in a similar way. There is code to translate a png file into a form that can be displayed on a gameboy, and other code to actually display that image. Both the image and the display code are also written by the final bootstrapping program. Even though my final payload is rather simple, you can write any program at all as the payload. The source for the sound and image displaying code is at http://hg.bortreb.com/vba-clojure
This entire project is open source and I encourage anyone who wants to take the code and play around!

Suggested Screenshots

Or whatever you all think would be best.
I encoded the video with/without button visualization here:

FractalFusion: Replaced movie file to fix time (note that the parser works in such a way so that the time listed for a VBM can easily be faked).
FractalFusion: Response is well-received for the new concept. Accepting for publication.
natt: Processing...
FractalFusion: Changed branch to "Executes Arbitrary Code".


1 2
6 7 8
Post subject: Movie published
TASVideoAgent
They/Them
Moderator
Joined: 8/3/2004
Posts: 15577
Location: 127.0.0.1
This movie has been published. The posts before this message apply to the submission, and posts after this message apply to the published movie. ---- [2187] GBC Pokémon: Yellow Version "arbitrary code execution" by bortreb in 12:51.87
Post subject: Re: Movie published
Spikestuff
They/Them
Editor, Publisher, Expert player (2642)
Joined: 10/12/2011
Posts: 6438
Location: The land down under.
TASVideoAgent wrote:

This movie has been published.
The posts before this message apply to the submission, and posts after this message apply to the published movie.
"Trade complete" Congrats bortreb for completely braking the game... I wonder what you'll do next
WebNations/Sabih wrote:
+fsvgm777 never censoring anything.
Disables Comments and Ratings for the YouTube account. Something better for yourself and also others.
Site Admin, Skilled player (1254)
Joined: 4/17/2010
Posts: 11475
Location: Lake Char­gogg­a­gogg­man­chaugg­a­gogg­chau­bun­a­gung­a­maugg
Program tetris to play inside pokemon.
Warning: When making decisions, I try to collect as much data as possible before actually deciding. I try to abstract away and see the principles behind real world events and people's opinions. I try to generalize them and turn into something clear and reusable. I hate depending on unpredictable and having to make lottery guesses. Any problem can be solved by systems thinking and acting.
NitroGenesis
He/Him
Editor, Experienced player (556)
Joined: 12/24/2009
Posts: 1873
-Star -6.3 average rating LOL, this shouldn't have a star, sure it's cool, but it is boring as fuck and everyone knows this.
YoungJ1997lol wrote:
Normally i would say Yes, but thennI thought "its not the same hack" so ill stick with meh.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
NitroGenesis wrote:
LOL, this shouldn't have a star, sure it's cool, but it is boring as fuck and everyone knows this.
Moons used to denote notable publications that were nevertheless not star-worthy (at least not at the time of publication.) Have we completely lost this kind of symbol?
Spikestuff
They/Them
Editor, Publisher, Expert player (2642)
Joined: 10/12/2011
Posts: 6438
Location: The land down under.
NitroGenesis wrote:
-Star -6.3 average rating LOL, this shouldn't have a star, sure it's cool, but it is boring as fuck and everyone knows this.
First time I watched it I was like... I'M SO FUCKING ENTERTAINED (so is usually an 8 on my entertainment) technical should be a straight 10
WebNations/Sabih wrote:
+fsvgm777 never censoring anything.
Disables Comments and Ratings for the YouTube account. Something better for yourself and also others.
WST
She/Her
Active player (489)
Joined: 10/6/2011
Posts: 1697
Location: RU · ID · AM
This is an example of what makes TASing be art… Strongest yes vote ever, even if it’s already published.
S3&A [Amy amy%] improvement (with Evil_3D & kaan55) — currently in SPZ2 my TAS channel · If I ever come into your dream, I’ll be riding an eggship :)
ALAKTORN
He/Him
Former player
Joined: 10/19/2009
Posts: 2527
Location: Italy
NitroGenesis wrote:
-Star -6.3 average rating LOL, this shouldn't have a star, sure it's cool, but it is boring as fuck and everyone knows this.
agreed, the best course of action would’ve been to optimize the movie in order to reduce that boring period of time, then it might’ve been publishable as a star but nobody gives a shit about optimization on this site, right?
Skilled player (1741)
Joined: 9/17/2009
Posts: 4981
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
NitroGenesis wrote:
-Star -6.3 average rating LOL, this shouldn't have a star, sure it's cool, but it is boring as fuck and everyone knows this.
Same goes for this movie. Except it's a moon, but people hate it to the point of wanting it removed.
ALAKTORN
He/Him
Former player
Joined: 10/19/2009
Posts: 2527
Location: Italy
^what’s with the broken obsolation in that publication?
Site Admin, Skilled player (1254)
Joined: 4/17/2010
Posts: 11475
Location: Lake Char­gogg­a­gogg­man­chaugg­a­gogg­chau­bun­a­gung­a­maugg
Stars are a showcase. But we don't only showcase FRAME achievemnts. Not only we showcase ENTERTAINMENT achievements. We ideally showcase everything innovative and jaw-dropping. This thing must have an innovatve TAS of 2012 award. Because it's a COMPLETELY new concept. Surely it doesn't shine with ALL its sides, but neither did Morimoto's smb3 judging it now. It will be improved I believe, frame wise and program wise.
Warning: When making decisions, I try to collect as much data as possible before actually deciding. I try to abstract away and see the principles behind real world events and people's opinions. I try to generalize them and turn into something clear and reusable. I hate depending on unpredictable and having to make lottery guesses. Any problem can be solved by systems thinking and acting.
Player (56)
Joined: 10/16/2012
Posts: 233
Location: Milwaukee, WI
Didn't see Tetris. Didn't see Pacman. Didn't see Pong. One day, this will work, right?
If I could have a tool-assisted real life, I'd... Being a novice, I'd probably load the wrong state, have the IRS AI bankrupt me, and eventually make me want to kill myself and redo 11 years of hard work.
BigBoct
He/Him
Editor, Former player
Joined: 8/9/2007
Posts: 1692
Location: Tiffin/Republic, OH
ALAKTORN wrote:
^what’s with the broken obsolation in that publication?
I think the idea is that it's outdated and the goal is out of step with the site's goals now, so it obsoletes itself to remove it from the list of currently published movies.
Previous Name: boct1584
Skilled player (1741)
Joined: 9/17/2009
Posts: 4981
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
JWinslow23 wrote:
Didn't see Tetris. Didn't see Pacman. Didn't see Pong. One day, this will work, right?
Pong? Seriously? ......... Er, yea...why isn't there PONG? It clearly is the most suitable game for a Tool Assisted Superplay, with it's simple graphics (the ball doesn't even look like a ball), and brilliant addicting gameplay. Oh god please tell me he isn't serious.
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3573)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
He was saying he would like to see pong as the game hacked into the pokemon game, not the game itself to be tased.
It's hard to look this good. My TAS projects
Skilled player (1741)
Joined: 9/17/2009
Posts: 4981
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
adelikat wrote:
He was saying he would like to see pong as the game hacked into the pokemon game, not the game itself to be tased.
Sorry, thought he was responding to feos's post. Anyway, in that case, I think that'll lead to an even longer wall of text, so I don't think it'll be worth it.
Former player
Joined: 9/3/2012
Posts: 40
Location: boston
Thank you everyone, for all of your comments on my run, and thank you for welcoming me into this great community. The technical competence and understanding of this TAS community has continued to impress me, and I'm glad to now be part of it. Your enjoyment makes the many months of work worth it. As others have said, it's meant to be a technical showcase of a neat vulnerability of Pokemon yellow. I'd like to remind everyone that the source code to everything is available at http://hg.bortreb.com/vba-clojure/, and is completely free software. If you don't like ponies, take it and make it into anything you want! I would be very happy to see anyone build on this run to create something even more amazing! I'd be happy to help; just email me (rlm at mit dot edu) or pm me. When I'm less busy, I'll revisit this and try to apply the many things I've learned from talking with all of you. sincerely, --Robert McIntyre
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3573)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
boct1584 wrote:
ALAKTORN wrote:
^what’s with the broken obsolation in that publication?
I think the idea is that it's outdated and the goal is out of step with the site's goals now, so it obsoletes itself to remove it from the list of currently published movies.
Well, that is exactly the thinking, it is obsolete without an obsoletor. So I was screwing around to see what the site would do if I set it to obsolete itself. Then I forgot to set it back <_< As to why it is a moon, it is because it does not fit the Vault. But then again, it doesn't fit Moons either. It just doesn't fit in with the site rules of today, but there isn't a better designation than moon.
It's hard to look this good. My TAS projects
Player (13)
Joined: 6/17/2006
Posts: 508
Holy smokes, how did I miss this submission? I nominate this to be TAS of the year! :D The holy grail of arbitrary code execution: Transform a Famicom Disk System version of Super Mario Bros. into Zelda no Densetsu through input alone... and then back to Mario again. :D Thanks bortreb for also providing a detailed explanation, it was an interesting read! I also approve the idea of having sub-frame input for all input, not just resets.
Spikestuff
They/Them
Editor, Publisher, Expert player (2642)
Joined: 10/12/2011
Posts: 6438
Location: The land down under.
SmashManiac wrote:
Holy smokes, how did I miss this submission? I nominate this to be TAS of the year! :D The holy grail of arbitrary code execution: Transform a Famicom Disk System version of Super Mario Bros. into Zelda no Densetsu through input alone... and then back to Mario again. :D Thanks bortreb for also providing a detailed explanation, it was an interesting read! I also approve the idea of having sub-frame input for all input, not just resets.
Glitchy tas of the year for me... don't know about tas of the year... we have to wait to the end of the year to figure that out.
WebNations/Sabih wrote:
+fsvgm777 never censoring anything.
Disables Comments and Ratings for the YouTube account. Something better for yourself and also others.
Site Admin, Skilled player (1254)
Joined: 4/17/2010
Posts: 11475
Location: Lake Char­gogg­a­gogg­man­chaugg­a­gogg­chau­bun­a­gung­a­maugg
Revive the INNOVATIVE award for this one!
Warning: When making decisions, I try to collect as much data as possible before actually deciding. I try to abstract away and see the principles behind real world events and people's opinions. I try to generalize them and turn into something clear and reusable. I hate depending on unpredictable and having to make lottery guesses. Any problem can be solved by systems thinking and acting.
ALAKTORN
He/Him
Former player
Joined: 10/19/2009
Posts: 2527
Location: Italy
adelikat wrote:
As to why it is a moon, it is because it does not fit the Vault. But then again, it doesn't fit Moons either. It just doesn't fit in with the site rules of today, but there isn't a better designation than moon.
can’t you just delete it from the site?
Emulator Coder, Skilled player (1113)
Joined: 5/1/2010
Posts: 1217
ALAKTORN wrote:
can’t you just delete it from the site?
It has been made abundantly clear that that isn't an option.
Warepire
He/Him
Editor
Joined: 3/2/2010
Posts: 2178
Location: A little to the left of nowhere (Sweden)
feos wrote:
Revive the INNOVATIVE award for this one!
There would be no better reason, ever, to revive that award than for this TAS. DO IT!
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Is there any GameBoy game that could fit in the RAM of the device?
1 2
6 7 8