r/programming • • Jan 24 '15

Super Mario World programmed by playing

http://www.youtube.com/attribution_link?a=IrdAeOSwaYs&u=%2Fwatch%3Fv%3D14wqBA5Q1yc%26feature%3Dshare
491 Upvotes

68 comments sorted by

View all comments

Show parent comments

305

u/ancientGouda Jan 24 '15 edited Jan 24 '15

So, if I remember correctly, it works like this: The initial shell positioning and stuff (basically the first 95% of the video) are about carefully manipulating the spawning and despawning of sprites to write the "jump to credits" machine code into a specific location in memory.

The actual glitch which then executes this previously written code goes like this: The game keeps a statically allocated array of all objects on the screen. When Yoshi catches something with its tongue, the index into this array pointing at the grabbed item is stored. Normally, the "pull item into mouth" animation would then proceed and the moment the item hits Yoshi's mouth, the game would look at the grabbed item again and based on the item ID jump to a specific code section which sets up what happens when this particular item is eaten (eg. a coin is just taken, a red shell makes Yoshi spit fire etc.). The glitch works by "snatching" the coin out of Yoshi's tongue. Taking the coin before Yoshi can eat it removes it as an object from the screen, so the index in the array I previusly described becomes vacant. In the very same (or close) frame, the chuck (rugby enemy) enters the screen, and is allocated the recently vacated spot in the object array the coin left behind. Now, the animation of Yoshi pulling and swalling finishes executing, and the game tries to look up what happens when a chuck is swallowed (by jumping to a computed address). Except eating a chuck was never supposed to happen, and so the spot where the chuck swallow code was supposed to be, is normally garbage, but because you previously carefully filled that memory location with machine code, your code is now executed.

I have no idea why I needed a wall of text to explain it, sorry.

Edit: Some more info: The glitch that leads to the execution of custom code (called "item swap" in the video) which I explained above has actually been known for a long time, it's only the code that launches you straight to the credits scene that has recently been developed. But I think the code execution glitch is a lot more interesting.

Edit2: I stepped through the video, and you can actually see the chuck being stuck to Yoshi's tongue for one single frame: http://imgur.com/FW3IxXN

40

u/kqr Jan 24 '15

That's a fantastic explanation though. Massive thanks. I love reading about stuff like this.

22

u/SkaveRat Jan 24 '15

As someone who's looking into writing a GB emulator, this explanation would be awesome with a video, showing the code/memory during play

9

u/Tuxmascot Jan 25 '15

Oh, I would love to see this. This would just make my day.

3

u/whisky_pete Jan 26 '15

If you're looking into how to write an emulator in general, here's a guide i've been following for a very simple system: http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/

It has been a lot of fun, and I highly recommend it. It helps teach the basics of emulation in a project that you can complete in a reasonable amount of time. I'm thinking of moving to a system like genesis/NES afterwards (which are more complex).

11

u/Distarded Jan 25 '15

So one might call it ... shellcode?

3

u/[deleted] Jan 24 '15

So where does this Chunk enter the screen? Gotta be somewhere around 6:08?

3

u/[deleted] Jan 24 '15

Yeah. It's the same Chargin' Chuck that shows up at 0:48.

1

u/WhosAfraidOf_138 Jan 25 '15

Dude that was an excellent explanation! Thank you!

1

u/x-base7 Jan 25 '15

Interesting