The Final Fantasy IV Door Stack Glitch

The Final Fantasy series is loaded with bugs throughout. A full recounting would be much more than a longpost’s worth, but here is a quick description of one specific example, from Final Fantasy IV (originally II in the US, but most people now will probably think of it by the Japanese numbering anyway).

The door to the pub is push-type, potentially causing Problems.
Image from mynockx’s guide on GameFAQs.

Some RPGs, instead of coding area transitions all as a sequence of doors and destinations, instead use a form of stack to record where the player was when they entered the door. “Stack” here is a term from computer science, a data structure consisting of a region of memory and a pointed within it. Data can be “pushed” onto the stack, which means putting some number of bytes onto it and advancing the stack pointed by that number. Stacks can “grow” either up or down, meaning when the pointer advances, it’ll go in that direction. When the data is needed again, it’s read off the top of the stack, then the pointer is pulled back to its original position.

So how the door stack works is, when a player enters a location, say enters a town from the overworld, their location before entering is “pushed” onto the stack and they are then moved into the town’s entrance. When they exit the town, their old location is “pulled” from the stack, leaving it empty. (Actually, the data is still there, but because the stack pointer has been decremented, it’ll be overwritten the next time the player enters an area.)

Why use a stack? Well mostly it’s a convenience thing for the programmers. A door’s location can either be “into” an area, or “out of” it. “In” doors have to know where they’re going, but “out” doors just have to know they’re going outside. But it helps in one particular instance; if a game has a spell or item like “Exit,” “Outside,” or “Warp,” it can work simply by pulling every location off the stack until it gets to the last one. This means the programmers don’t have to have every location “know” where a given area is on the World Map. Just rewind the door stack until you get to the last location on it, that must be it.

Well there’s a subtle bug in some locations in Final Fantasy IV where some transitions that push when they should pull. One such transition is the one to the pub in the Dwarven Castle. When you enter the pub, the way in is pushed onto the stack; when you exit, instead of pulling that location off, the way out is pushed onto the stack.

There’s only so much memory reserved in a stack, which for old games is usually implemented as a single page (256 bytes) of memory. The pointer into it is thus one byte long, and so if the stack fills up, it wraps around. If you find such a door, and go through it enough times, you can cause it to overflow on purpose, with unexpected results.

This can be taken advantage of in Final Fantasy IV by overflowing the stack, then going through a pull-door, which causes the game state to be read from unexpected memory. Speedrunners (you just knew they’d be involved) use this to flip rapidly to the end of the game. Most players will never notice this very subtle bug, since when you return to the world map the game knows enough to completely clear the stack.

Something I’ve noticed about the 8- and 16-bit Final Fantasy games is, if there is a potential for an obscure bug somewhere, there is almost certainly going to be an example of that bug somewhere in that code. A lot of these bugs are only visible to a player with obsessive observation or repetition. This results in spells with unexpected effects, stats with no function, features that don’t operate, and item duplication bugs. Truly, it was an age before unit testing.

Final Fantasy Wiki: 64 Door Hierarchy Glitch