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 3 4 5 6 7 8
Joined: 2/14/2012
Posts: 73
FractalFusion wrote:
This run is a playaround but I think the branch should be changed. To me, "Total Control Hack" is misleading, but I can't think of another name for it at the moment. "Hacking" is fine, but not "Hack".
Executes arbitrary code?
Joined: 6/26/2011
Posts: 13
It just occurred to me that Totaka's Song would've been somewhat fitting. That's hardly cause for a No, though!
Skilled player (1742)
Joined: 9/17/2009
Posts: 4984
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
hezekiah wrote:
It just occurred to me that Totaka's Song would've been somewhat fitting. That's hardly cause for a No, though!
But it won't be something unexpected and surprising if its Nintendo related. :P
Joined: 5/2/2009
Posts: 656
jlun2 wrote:
hezekiah wrote:
It just occurred to me that Totaka's Song would've been somewhat fitting. That's hardly cause for a No, though!
But it won't be something unexpected and surprising if its Nintendo related. :P
so, logically it must be green hill zone
My first language is not English, so please excuse myself if I write something wrong. I'll do my best do write as cleary as I can, so cope with me here =) (ノಥ益ಥ)ノ
Skilled player (1742)
Joined: 9/17/2009
Posts: 4984
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
Joined: 5/2/2009
Posts: 656
jlun2 wrote:
OmegaWatcher wrote:
so, logically it must be green hill zone
I was thinking more of something that'll leave no brain cells alive.
oh wow. that's unwatchable.
My first language is not English, so please excuse myself if I write something wrong. I'll do my best do write as cleary as I can, so cope with me here =) (ノಥ益ಥ)ノ
Skilled player (1742)
Joined: 9/17/2009
Posts: 4984
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
OmegaWatcher wrote:
jlun2 wrote:
OmegaWatcher wrote:
so, logically it must be green hill zone
I was thinking more of something that'll leave no brain cells alive.
oh wow. that's unwatchable.
I know right? I think my IQ just dropped by points of several. I am thinking I need to take a Iq test. i hope its not 2 late for me. brb of too take iq test
Joined: 5/2/2006
Posts: 1020
Location: Boulder, CO
This is the first tas in years that has recreated the wonder hat I felt the first time I watched a tas. The impossible seems possible. This deserves more than a star. In my mind there should be some even more elite category: runs that change what can be achieved in the medium. I can only think of a few things that did this: 1. the original mario 3 run 2. the first use of the robot that shows some tases are reproducible on the console 3. this run 4. bisqbot? -- not sure this was the first, but writing a bot to play a game rather than just using re-records to play perfectly was a game changer.
Has never colored a dinosaur.
Skilled player (1742)
Joined: 9/17/2009
Posts: 4984
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
Twelvepack wrote:
4. bisqbot? -- not sure this was the first, but writing a bot to play a game rather than just using re-records to play perfectly was a game changer.
You mean lunar pool?
Joined: 2/20/2010
Posts: 209
Location: I'm in space
jlun2 wrote:
Twelvepack wrote:
4. bisqbot? -- not sure this was the first, but writing a bot to play a game rather than just using re-records to play perfectly was a game changer.
You mean lunar pool?
I think he means the frictionless run in particular, because it's impossible to beat without the bot.
Oh, play it cool. Play it cool. Here come the space cops.
Former player
Joined: 3/27/2010
Posts: 270
I rolled my eyes thinking erm? Waiting for something to happen the entire time. Once the hex showed up on the screen I was like "is that it?!, how could this be accepted!" .. When the balloons came I was like "hmm???!" ... My little pony theme came on and I instantly started laughing :p needless to say I enjoyed it; and also needless to say I didn't read the submission text first cause I wanted to be surprised since I seen people talking about this!
Mitjitsu
He/Him
Banned User
Joined: 4/24/2006
Posts: 2997
I'd be curious to know if this will play back correctly on console. First we had time savers;then we had design exploits;then we had glitches;then we had glitches that could skip vast sections of a game;then we had glitches that could rewrite the variables within the game;then we had glitches that could inexplicably warp a player to the end of the game and now we have glitches that allows someone to write their own programs within the game. No doubt we'll see a more advanced program like Pong be written within the games code a couple of years down the line.
Joined: 5/2/2006
Posts: 1020
Location: Boulder, CO
I really, really do wish that there were another game you could write, then beat in the usual tas fashion. Semi unrelated question: wasn't the procedure to "jailbreak" a wii kind of like this? Didn't the usual method involve changing the horse's name so that it overwrote a function pointer to point to an area of memory that you had control over?
Has never colored a dinosaur.
Skilled player (1742)
Joined: 9/17/2009
Posts: 4984
Location: ̶C̶a̶n̶a̶d̶a̶ "Kanatah"
Twelvepack wrote:
I really, really do wish that there were another game you could write, then beat in the usual tas fashion. Semi unrelated question: wasn't the procedure to "jailbreak" a wii kind of like this? Didn't the usual method involve changing the horse's name so that it overwrote a function pointer to point to an area of memory that you had control over?
It's one of the many ways to "unlock" a wii. :P
Skilled player (1417)
Joined: 10/27/2004
Posts: 1978
Location: Making an escape
I have to admit, the word "hack" in the submission title threw me off. Made me think it was just another hack. I haven't watched it because I don't feel like watching thirteen minutes of setup. What this run achieves, though, is simply monumental. Good job.
A hundred years from now, they will gaze upon my work and marvel at my skills but never know my name. And that will be good enough for me.
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3574)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
Wow! Consider me thoroughly impressed with this. More impressed than I have been about a TAS in quite awhile.
It's hard to look this good. My TAS projects
RachelB
She/Her
Player (129)
Joined: 12/3/2011
Posts: 1579
Twelvepack wrote:
I really, really do wish that there were another game you could write, then beat in the usual tas fashion. Semi unrelated question: wasn't the procedure to "jailbreak" a wii kind of like this? Didn't the usual method involve changing the horse's name so that it overwrote a function pointer to point to an area of memory that you had control over?
http://www.youtube.com/watch?v=Zif-0RxXNhM It can't be done with just the game and a controller though. The save file needs to be edited.
Banned User
Joined: 3/10/2004
Posts: 7698
Location: Finland
Ferret Warlord wrote:
I have to admit, the word "hack" in the submission title threw me off. Made me think it was just another hack.
Curiously, I'd say that in this case the usage of the word "hack" is more accurate than normally. Those modified games would be more correctly named "mods".
Editor, Emulator Coder, Site Developer
Joined: 5/11/2011
Posts: 1108
Location: Murka
vba-rr v23.5 svn421
  File: Pokemon - Yellow Version (UE) [C][!].gbc
CRC-32: 7d527d62
   MD4: 629cbcc8cbbcef8826345d61a7e00341
   MD5: d9290db87b1f0a23b89f99ee4469e34b
 SHA-1: cc7d03262ebfaf2f06772c1a480c7d9d5f4a38e1
adelikat
He/Him
Emulator Coder, Site Developer, Site Owner, Expert player (3574)
Joined: 11/3/2004
Posts: 4754
Location: Tennessee
Let's please come up with a better category name before publication. I agree that "Total control hack" undersells this movie significantly
It's hard to look this good. My TAS projects
Joined: 8/1/2006
Posts: 428
Just "Total Control" then?
Trying 127.0.0.1... telnet: connect to address 127.0.0.1: Connection refused telnet: Unable to connect to remote host
Joined: 11/9/2012
Posts: 23
GBC Pokemon Yellow "In Game Reprogramming" Meh.
Skilled player (1417)
Joined: 10/27/2004
Posts: 1978
Location: Making an escape
I just realized that this is kinda like that one scene in Haruhi Suzumiya or whatever where that alien robot chick rewrote a space combat game while it was running.
A hundred years from now, they will gaze upon my work and marvel at my skills but never know my name. And that will be good enough for me.
Former player
Joined: 3/27/2010
Posts: 270
adelikat wrote:
Let's please come up with a better category name before publication. I agree that "Total control hack" undersells this movie significantly
Internal program redesign ?
sack_bot
He/Him
Player (112)
Joined: 11/27/2011
Posts: 394
Location: Massachusetts
JSmith wrote:
Just "Total Control" then?
second
Message me here for my discord. Current Project: Psycho Waluigi Project on wait list: None?
1 2 3 4 5 6 7 8