Cause of Special Menu Select glitches (JP)
Posted by: Stackout
Date: 2017-09-01 21:19:45
I used luckytyphlosion's pokered-jp disassembly (a fork of pokered that aims to be a disasm of JP R/G) to help me.
This glitch is caused entirely by the HandleItemListSwapping function. As you can probably see by the helpful comments, the direct cause is the wrong conditional jump was used, and the jump back to the item menu loop happens when carry flag is set, instead of when zero flag is not.
The function can only handle item swapping, and special item lists are just lists of item indices, without quantities.
So, swapping items 1 and 2 cause items 1 and 2 to be swapped with 3 and 4, as items 1 and 2 are interpreted as item+quantity of item 1, and items 3 and 4 as item+quantity of item 2.
Going by the sets of swaps in the wiki article, the last swap swaps items 7 and 8, that is, bytes 14-15 and 16-17. These bytes are located after the FF array terminator. The buffer in WRAM where the item list is located is in fact only 16 bytes long (and the first byte is the list quantity). Directly after this buffer is a pointer to the item list itself, so this pointer is swapped with some undefined data beyond the list terminator but still within the buffer.
I have not yet determined why this causes corruptions to other parts of memory.