Sanc and Quit

Sanc and quit is dumb. It's a dumb thing with a dumb name, and it smells dumb. But why does it happen? And why is it so dumb?

Enter if you dare

There are a bunch of ways to load a room as an entrance. You can walk in from the overworld; you can spawn from file select; you can use this dumb thing called sanc and quit; you can die and choose to continue; or you could mirror while in a dungeon.

That last thing is the one we're interested in.

When you use the mirror, the screen gradually gets more pixelated using a hardware effect built into the SNES PPU called mosaic. There are 16 levels of mosaic, ranging from 0 (no mosaic) to 15 (very mosaic), and they can be applied only to, but any permutation of, the four background layers. This effect is controlled by a single register. The most significant 4 bits behave as a single value from $0 to $F. The lower 4 bits are a bitfield where each bit corresponds to a background on which mosaic is applied.

For example, a mosaic level of 6 can be applied to backgrounds 1 and 2 by writing $63 to the mosaic register.

The strength of the mosaic effect is generally handled by itself in MOSAICLEVEL ($7EC011)—(the mosaic effect during special overworld transitions writes $F7, but that's cleared after it's done). Actual mosaic control is handled by the WRAM queue MOSAICQ ($95). Sometimes, MOSAICLEVEL is used to interface with mosaic control, and other times, MOSAICQ is used directly.

Dumb

So where does this come into play with sanc and quit? Well, it's actually MOSAICLEVEL that's abused as a flag to identify mirror warps in the underworld. Three addresses are checked at a certain point in the file loading routine. Oh…

Did I forget to mention that mirroring uses the same module as file loading? Ya. They share a lot of code, and it makes mirroring do some redundant stuff, but it also overloads this routine with a lot of garbage that results in the garbage you're reading about.

So back on track: when you load a file, it checks MOSAICLEVEL, sees that it's set, and just continues. When it gets to loading your entrance, it loads the current spawn point you have set, which, after you've rescued Zelda, is going to be $01. It will briefly be $05 while you're escorting the old man, but he sets it back to $01 when he hands you the mirror (this is why rescuing the old man before Zelda always has you spawn in the sanctuary).

Even when you're given the choice of a spawn point, the value won't deviate from $01 if things are working as intended. Before your selection is finalized, the original value is cached. Your selection overwrites it when the spawn point is loaded, but after that, the cached value is restored.

In this way, the sanctuary is sort of a fixed point, but not really. There's nothing that says it has to always be that value, and post-Zelda save and quits work perfectly fine with any other value. It's just that this value is held to tightly, but if you found a way to change it outside of the spawn selection routine, you would spawn there instead of sanctuary when you sanc and quit.

Quit and Sanc

Remember how I said the file loading routine is overloaded to include mirror functionality? Well, it's also overloaded to include death functionality.

There's a special case of sanc and dumb that occurs exactly when you save and quit with a mosaic strength of 1MOSAICLEVEL will have a value of $10. Normally, loading a room from file select will reset MOSAICLEVEL to $00, but that's only if you spawn indoors. In the Dark World with Agahnim defeated, you will spawn outdoors on the pyramid (to be precise: it's a mastaba).

If you then take your festering mosaic strength to a dungeon and use the mirror, you'll be prompted with the spawn selection. What gives?

This is what happens when you try to shove everything in a single function.

When you use the mirror, it doesn't bother to initialize MOSAICLEVEL; it just blindly trusts that it will already be $00. It will then add $10 to it a total of 15 times to reach maximum mosaic. Under normal circumstances, the end result of this is a value of $F0, which the file loading routine subsequently identifies as you having used the mirror. But when we start with $10, the calculation overflows, and the end result is $00.

The check on MOSAICLEVEL fails, and the next check is used. Address $010A is read to determine whether or not you are respawning from death. Well, we didn't just die, so it's not set. And since it's not, it is assumed that you must have entered this routine from the file select menu.

And here's where it gets dumber… You really are choosing a spawn point, but your choice is subsequently discarded, because yet another flag gets in your way! Grrrr!!!

The variable RESPAWN ($04AA) is checked to determine if you should load a spawn point entrance or your current normal entrance. This flag is expected to be clear when you mirror, which means you end up not using spawn entrance; you use the entrance already set, which is the same one the mirror intended to return you to.

Summary

Sanc and quit is dumb.

Qirn found and solved Quit and Sanc.