// fff1f8 sram__validate_slot ok 35 ms, 82 lines, 2369 chars

/* Validate save slot $0479 (0-2): signature $E41B at $30:7FF8+2s and checksum $30:7FF0+2s ==
   sram__slot_checksum. Result in DP $00: $0000 valid, $8080 no signature (empty), $4040 bad
   checksum. */

void sram__validate_slot(void)

{
  undefined1 uVar1;
  ushort uVar2;
  undefined1 uVar3;
  undefined2 in_X;
  ushort uVar4;
  ushort in_DP;
  BADSPACEBASE *in_SP;
  undefined1 *puVar5;
  undefined1 in_DBR;
  byte bVar6;
  char in_NF;
  char in_VF;
  char in_DF;
  char in_IF;
  char in_ZF;
  byte in_CF;
  uint in_register_00000040;
  undefined1 uStack_6;
  undefined1 uStack_5;
  undefined1 uStack_4;
  undefined1 uStack_3;
  undefined1 uStack_2;
  undefined1 uStack_1;
  
  *(char *)ZEXT23(&stack0x0000) = (char)((ushort)in_X >> 8);
  *(char *)ZEXT23(&uStack_1) = (char)in_X;
  *(undefined1 *)ZEXT23(&uStack_2) = in_DBR;
  *(byte *)ZEXT23(&uStack_3) =
       in_NF << 7 | in_VF << 6 | ((byte)in_register_00000040 & 1) << 5 |
       ((byte)(in_register_00000040 >> 1) & 1) << 4 | in_DF << 3 | in_IF << 2 | in_ZF << 1 | in_CF;
  *(undefined1 *)ZEXT23(&uStack_4) = 0x30;
  *(undefined1 *)ZEXT23(&uStack_5) = 0x30;
  bVar6 = *(byte *)ZEXT23(&uStack_4);
  *(undefined1 *)ZEXT23(&uStack_4) = 0xf2;
  *(undefined1 *)ZEXT23(&uStack_5) = 4;
  puVar5 = &uStack_6;
  sram__slot_base();
  if (((byte)(in_register_00000040 >> 3) & 1) == 1 && (char)in_DP == '\0') {
    uVar4 = in_DP | 0x79;
  }
  else {
    uVar4 = in_DP + 0x79;
  }
  uVar4 = (*(byte *)(uint3)uVar4 & 3) << 1;
  if (CONCAT11(*(undefined1 *)((uint3)bVar6 * 0x10000 + (uint3)uVar4 + 0x7ff9),
               *(undefined1 *)((uint3)bVar6 * 0x10000 + (uint3)uVar4 + 0x7ff8)) == -0x1be5) {
    *(undefined1 *)ZEXT23(puVar5) = 0xf2;
    *(undefined1 *)ZEXT23(puVar5 + -1) = 0x16;
    uVar2 = sram__slot_checksum();
    if (uVar2 == CONCAT11(*(undefined1 *)((uint3)bVar6 * 0x10000 + (uint3)uVar4 + 0x7ff1),
                          *(undefined1 *)((uint3)bVar6 * 0x10000 + (uint3)uVar4 + 0x7ff0))) {
      uVar1 = 0;
// c18a9d btl__timer_06_null ok 1 ms, 10 lines, 197 chars

/* WARNING: Unknown calling convention */
/* Battle timer channel 6 (table $C1B92D): per-unit countdown; bit $0040 of $B186 marks it pending.
    */

void btl__timer_06_null(void)

{
  return;
}
// c18461 btl_ai__decide ok 155 ms, 701 lines, 30231 chars

/* WARNING: Removing unreachable block (bus,0xc1866a) */
/* WARNING: Removing unreachable block (bus,0xc1858b) */
/* WARNING: Removing unreachable block (bus,0xc1846b) */
/* WARNING: Removing unreachable block (bus,0xc18558) */
/* WARNING: Removing unreachable block (bus,0xc18643) */
/* WARNING: Removing unreachable block (bus,0xc18702) */
/* WARNING: Unknown calling convention */
/* Enemy AI decision: timer channel 12. For the enemy whose turn came up ($B252/$AEC8) walks its
   script in bank CC (pointer $B1D2): blocks of <=2 four-byte conditions, FE, actions, FE; section
   ends FF. Conditions dispatch through the condition table; the first block whose conditions leave
   $AF24=0 is chosen and its current action is prepared (btl_ai__prep_action). Skipped blocks are
   stepped over by scanning for FE/FE. */

void btl_ai__decide(void)

{
  undefined1 uVar1;
  uint3 uVar2;
  byte bVar3;
  uint uVar4;
  int iVar5;
  char cVar6;
  char cVar7;
  byte bVar8;
  undefined1 uVar9;
  ushort uVar10;
  undefined1 uVar12;
  ushort uVar13;
  ushort in_DP;
  BADSPACEBASE *in_SP;
  undefined1 *puVar14;
  undefined1 *puVar15;
  undefined1 *puVar16;
  undefined1 *puVar17;
  undefined1 *puVar18;
  undefined1 *puVar19;
  undefined1 *puVar20;
  byte in_DBR;
  char in_DF;
  uint in_register_00000040;
  undefined1 uStack_3;
  undefined1 uStack_2;
  undefined1 uStack_1;
  ushort uVar11;
  
  cVar7 = (char)in_DP;
  (&UNK_00b2c0)[(uint3)in_DBR * 0x10000] = cVar7;
  (&UNK_00b3b9)[(uint3)in_DBR * 0x10000] = cVar7;
  uVar10 = in_DP;
  if ((*(byte *)((uint3)in_DBR * 0x10000 + 0x27) & 1) != 0) {
    do {
      (&UNK_00aeb2)[(uint3)in_DBR * 0x10000 + (uint3)uVar10] = cVar7;
      uVar10 = uVar10 + 1;
    } while ((int)(short)uVar10 + -9 < 0);
    uVar9 = (undefined1)(in_DP >> 8);
    cVar6 = (&UNK_00b185)[(uint3)in_DBR * 0x10000];
    if (cVar6 == '\0') {
      cVar6 = (&UNK_00aec7)[(uint3)in_DBR * 0x10000];
    }
// c1aed3 dead__btl_ai__activate_reserve_enemy ok 5 ms, 35 lines, 1177 chars

/* Unreferenced (no pointer, no flow, 0 executions in the MAME session). Activates the reserve enemy
   whose formation id ($AF0D,X) equals $0E: $AF02,X=id, clears $AF15,X bit 7; none found -> $AF24=1.
   Shape of an AI action EXEC handler; no action op points here (JP actions 08/0E are RTS stubs). */

void dead__btl_ai__activate_reserve_enemy(void)

{
  ushort uVar1;
  char cVar2;
  ushort in_DP;
  byte in_DBR;
  uint in_register_00000040;
  
  (&UNK_00af24)[(uint3)in_DBR * 0x10000] = (char)in_DP;
  if (((byte)(in_register_00000040 >> 3) & 1) == 1 && (char)in_DP == '\0') {
    uVar1 = in_DP | 0xe;
  }
  else {
    uVar1 = in_DP + 0xe;
  }
  cVar2 = *(char *)(uint3)uVar1;
  while ((cVar2 != (&UNK_00af0d)[(uint3)in_DBR * 0x10000 + (uint3)in_DP] ||
         (cVar2 == (&UNK_00af02)[(uint3)in_DBR * 0x10000 + (uint3)in_DP]))) {
    in_DP = in_DP + 1;
    if (-1 < (int)(short)in_DP + -8) {
      (&UNK_00af24)[(uint3)in_DBR * 0x10000] = 1;
      return;
    }
  }
  (&UNK_00af02)[(uint3)in_DBR * 0x10000 + (uint3)in_DP] = cVar2;
  (&UNK_00af15)[(uint3)in_DBR * 0x10000 + (uint3)in_DP] =
       (&UNK_00af15)[(uint3)in_DBR * 0x10000 + (uint3)in_DP] & 0x7f;
  return;
}
