Popo Shadow

This is something that has bugged me for a long time. The answer is simpler than I expected, but I consider this so important that I must document it for future generations.

Gamering

Link charges into a tentacle monster; to his left is an unexplained shadow.
You will never unsee this.

Pay close attention the next time you watch someone run NMG. In the room after the elegantly named Popo Genocide, focus your gaze a little to the left of the torch. For a split second—literally 1 frame—a shadow pops up in the middle of the screen. The first time I noticed this, I was confused, intrigued, and probably hungry (I can't say for certain).

Look closely at the right popoPopo is a funny word.. The popo is missing its shadow. It seems the popo may be the owner of the mysterious shadow. Why is the popo's shadow way over there?

Don't treat me like an object

To unravel the mystery of the popo shadow, we need to venture into one of the scariest parts of the game engine: its object allocation system.

For clarity: "object" is the official technical nomenclature for what you might call a "hardware sprite", and that will be the nomenclature used throughout this explication. We also need to talk a lot about the enemy kind of sprite, but it abets confusion to use the same word for both.

The SNES is limited to 128 objects, and each object is defined by 4 bytes4.25 bytes, actually. Object have an additional 2 bits in a table at the end of OAM where 4 objects are packed together in a single byte. We only need to talk about the main table here though. Furthermore, ALTTP's buffer gives each object its own byte for those 2 bits and packs them together at the end of the frame.. To effectively manage an arbitrary number of objects, ALTTP allocates space in its buffer by placing objects into one of six regions.

Region Start End
A $0830 $0970
B $09D0 $0A00
C $0800 $0830
D $0830 $08C0
E $0920 $0940
F $0940 $09D0

And here's a pretty table to visualize it:

Region A
Region B
Region C
Region D
Region E
Region F

It looks like regions D, E, and F overlap with region A, but these actually represent two different allocation modes. In the overworld and most underworld rooms, the flag LAYERING at $0FB3 is set to 0—off—telling the shared allocation system to exclusively use region A. In rooms where sprites can potentially pass over each other on different layers, such as the eastern wing of Hyrule Castle, this flag will be set to 1—on—telling the system to split allocation by layer. Upper layer sprites' objects are sent to region D and lower layer sprites' objects to region F. Higher slots, and thus sprites on the lower layer, are drawn first, giving them a lower priorityThe hardware can be configured to cycle which slot is considered first every frame, but ALTTP does not use that..

Region C is special. It's used for small, high priority objects like fairy dust and beamos eyeballs.

Regions B and E are also special. These are both used as a sort of VIP space for sprites and ancillae you really don't want to get mangled, such as bombs or debirando pits. Region B sees much more usage than region E, which is used exclusively by ancilla in a shared routine that allocates based on LAYERING, using B when it's off and E when it's on. But also the ice rod, which, for some reason, allocates to A when LAYERING is off or uses its layer to pick between D and E when it's on.

Man, the next section is going to write itself...

That's not how this works

At its core, the framework itself is not bad, and it provides a robust allocation system that does a pretty decent job at making sure every object gets memory to draw. It even has a flicker mechanism like NES titles did as a last resort when everything is used up! All of the problems stem from its consumers who abuse, strain, and ignore it. These consumers are why tracking object allocation is a nightmare.

Too much too early

Before a sprite even knows what it is, it will perform a laundry list of errands including caching its coordinates, allocating objects, and even seeing if it happens to be Agahnim so he can threaten you. We want to focus on the allocation, because doing it here is a bad idea.

This allocation always occurs. Even if the sprite is off-screen or in a state that uses a shared draw routine. It also allocates the requested number of objects plus one extra—likely to account for shadows. This means even an invisible sprite that lacks a draw routine entirely and requests nothing takes precious memory away from real, god-fearing objects that actually need it. Furthermore, some sprites will completely reallocate memory themselves in their draw routines. These sprites generally request 0 objects and hand an exact number of bytes to the system, but that's still an object that could have been used by someone else, because the shared sprite allocator always adds one to the request.

I know better than you

The next most evil thing is the sheer number of hardcoded slots. It's mostly bosses that do this. Mothula is guilty of it, Lanmolas is guilty of it, and pots are guilty of it.

Bosses are questionable. There's not much going on in their rooms, and they choose slots that are relatively safe. Mothula, for example, draws herself near the end of region A. It would take a lot of objects61 to overwrite hers, but, if they did, they would win. Using hardcoded slots means the allocation system is unaware that this memory is being usedThe .NET garbage collector is probably giving me the stink eye right now..

Pots cannot be forgiven. Nor can bushes. Or rocks, for that matter. These items only use hardcoded slots when LAYERING is active and they're not on the upper layer, but they'll choose one of the of four slots at the end of region D. This might even be an oversight, as there are exactly 4 safe slots immediately after region D that they could have used instead.

Link also uses hardcoded slots, but he's allowed to, because he's the star of the show. Link chooses slots based on LAYERING, and he makes safe choices: either just after region A when it's off or just after region D (with a gap for pots!) when it's on. Although, if he's falling ($5B=$03), he'll always use the very first slot of the entire buffer.

Too soon

Neither of the previous foibles is responsible for the popo shadow; rather, it was an earnest attempt to use the system as intended, just slightly misplaced.

When most sprites die, they will explode with a tiny cloud drawn over them by a shared routine. To prepare for this cloud, with its last gasp of breath, a dying sprite will increase their requested object allocation by 4. This is a good thing, but it's done too soon. And too late. The puff isn't being drawn yet, and allocation for this frame has already occurred. Our dying popo was given eight bytes in the buffer, and that is what it can rightfully claim as its own.

This would be fine, if not for the shadow.

A shadow is a shadow and should thus appear below what it shadows. To guarantee this, shadows take a shortcut: they always pick the last slot that has been allocated, and they do this by rereading the object allocation count. Our popo, who was given 2 objects, has a shadow requesting object #6.

This results in a memory collision. As it is not tracked, later sprites may reuse this slot, but that still doesn't fully explain the shadow. To reveal why it was shifted left and not simply overwritten, we must look at exactly how objects are drawn.

Finally, the truth

Objects in memory have their 4 bytes in this exact order: X screen coordinate, Y screen coordinate, Tile ID, Properties. All of ALTTP's routines handle the data in this order too. It's important to note here that objects have no means of being disabled; to get rid of something, you hide it off screen. At the beginning of every frame, all 128 objects are hidden by setting their Y coordinates to 240, well below the bottom of the viewport. Other draw routines preserve this behavior by testing their Y screen coordinates and defaulting to 240 if it's determined the object is off screen on that axis.

But not all of them. In particular, SpriteDraw_SingleSmall, SpriteDraw_SingleLarge, and SpriteDraw_Shadow all take a shortcut by immediately exiting when they are off-screen on the Y axis without forcing their Y screen coordinate to 240.

This is the exact problem. Popos are a single 16-by-16 sprite with no complexity beyond their wiggle, which is handled entirely by changing graphic characters. They rely on SpriteDraw_SingleLarge to do the leg work of drawing as well as conditionally placing their shadow.

There are three issues here:

Knowing this, we can look at the last piece of the puzzle: there are two more popos in this section. They're far enough above the screen that we can't see them but not far enough to become inactive, which would short circuit their draw routine entirely.

Summary

Here's what happened:

At the beginning of its code, the popo in slot 7 allocated 2 objects for itself. It processed its pending damage, dying in the process. To prepare for its death puff, it increased its allocation request by 4 objects. When it drew its shadow, it picked an object slot based on that larger allocation and not the one that was already processed. That shadow was drawn to a slot that has not been accounted for. Later, the popo in slot 2 requested 2 objects and was allocated the untracked memory where popo 7 drew its shadow. Popo 2 was too far above the screen to be visible; after its X coordinate was written, its draw routine stopped short, leaving the rest of memory in tact. Thus, popo 7's shadow X coordinate took on the value for popo 2 but nothing else, teleporting it to the left.