#include "game_types.h"

/* Sub_0800C9AC @ 0x0800C9AC (real 158B) -- mode-switched dispatch with swap tick. Mirrors src/matched/Sub_0800C9AC.c. */
/* Seeds the 0x02000144 block (512/768 by byte 2080, 0x1801, zeroes), then
   dispatches modes 0-4 through the CAA4/CB18/D000/D0B8/D0D0 chain (jump
   table padded with dead-island trampolines). Mismatched words 0/2 swap
   through word 1 with the word-4 tick reset, else the tick increments. */
extern void Sub_0800CAA4(int *p);
extern void Sub_0800CB18(int *p);
extern void Sub_0800D000(int *p);
extern void Sub_0800D0B8(int *p);
extern void Sub_0800D0D0(int *p);

void Sub_0800C9AC(int *p)
{
    int *r1 = *(int **)0x02000144;
    int mode = p[0];
    *(unsigned short *)r1 =
        (unsigned short)(((unsigned char *)p)[2080] == 0 ? 512 : 768);
    *(unsigned short *)((char *)r1 + 16) = 0x1801;
    *(unsigned short *)((char *)r1 + 32) = 0;
    *(unsigned short *)((char *)r1 + 36) = 0;
    if ((unsigned)mode <= 4) {
        switch (mode) {
        case 0:
            Sub_0800CAA4(p);
            break;
        case 1:
            Sub_0800CB18(p);
            break;
        case 2:
            Sub_0800D000(p);
            break;
        case 3:
            Sub_0800D0B8(p);
            break;
        default:
            Sub_0800D0D0(p);
            break;
        }
    }
    if (p[0] != p[2]) {
        p[1] = p[0];
        p[0] = p[2];
        p[4] = 0;
    } else {
        p[4]++;
    }
}
