<?xml version='1.0' encoding='utf-8'?>
<CheatTable CheatEngineTableVersion="46">
  <CheatEntries>
      <CheatEntry>
        <ID>1</ID>
        <Description>"Auto-Attach and Version Check"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
-- Auto attach runs from the table script.
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end

{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>2</ID>
        <Description>"How to use - Katzy"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
showMessage([[Contra Anniversary Collection - Steam build 3875530. Table v4.41.
Table identification: Katzy.

Start gameplay, then enable its MAIN group and desired options.
Returning to the collection menu automatically turns the current MAIN off
and restores its changes. Start gameplay again before enabling MAIN.
Use one Cheat Engine instance. Turn the current MAIN off before changing
games, loading a saved state, or replacing the table.

Seven MAIN groups combine the supported regional versions of each game.
Shared options come first, followed by P1 and P2. P2 gameplay testing is
still deferred and its options remain marked as requiring testing.

WEAPONS
Hard Corps and its regional variants grant the dropdown choice once.
Normal weapon switching and pickups remain possible. The selected choice
returns after death. All other games keep the chosen weapon active,
including after pickups and death.

Turning a weapon option off equips the regular starting weapon without
Rapid Fire or upgrades. MAIN Off and normal reload/close do the same for
active selectors. This reset happens once; pickups work normally afterward.
Hard Corps bombs have separate toggles: they grant bombs and hold the stock
without selecting them or preventing normal weapon switching.

OTHER OPTIONS
Lives, bombs and continues restore their saved pre-cheat values on Off.
Invincibility has a visible effect and releases protection on Off; expired
native protection is not revived. Counters/icons may refresh on resuming.
Normal reload and close clean up active options. A forced exit cannot do so.

Japanese Hard Corps has Infinite Health. Its continues and Contra Spirits'
continues are already unlimited; the shared option leaves those counters alone.
Super Contra Arcade has upgraded weapons and overhead-stage Mega Shells.
US Hard Corps and Contra Spirits have no Turbo mode. European Original/Turbo
versions are handled within their corresponding game groups.

VERIFICATION
US Super C weapon locking, death persistence and Regular Off are confirmed.
US Contra Arcade continue without a coin and credit restoration are confirmed.
US Operation C natural continue/resume and code restoration are confirmed.
US and Japanese Super Contra Arcade continuing beyond the limit are confirmed.
US Contra III displays 04 correctly, resumes and restores on Off.
The final requirement and restoration audit is complete. Regional P1
reports, native-code checks and automated tests are indexed in the
contra-ct workfolder FINAL-AUDIT.md. P2 remains untested by agreement.
Not every difficulty or regional feature combination was played live.

A failed verified update stops cheats, attempts cleanup and shows the error.]])
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry><ID>45</ID><Description>"[MAIN] Contra (Arcade, US / Japan)"</Description><VariableType>Auto Assembler Script</VariableType><Options moHideChildren="1" moManualExpandCollapse="1" /><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_arc_enable()
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_arc",false)
{$asm}
</AssemblerScript><CheatEntries><CheatEntry><ID>48</ID><Description>"Infinite Credits / Continues"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_credits",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_credits",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>46</ID><Description>"Infinite Lives (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_lives1",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_lives1",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>47</ID><Description>"Invincibility (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_inv1",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_inv1",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>49</ID><Description>"Weapon (P1)"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Laser
4:Machine Gun
10:Spread
20:Flame</DropDownList></CheatEntry><CheatEntry><ID>51</ID><Description>"Infinite Lives (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_lives2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_lives2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>52</ID><Description>"Invincibility (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_inv2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("arc_inv2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>50</ID><Description>"Weapon (P2) - requires testing"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Laser
4:Machine Gun
10:Spread
20:Flame</DropDownList></CheatEntry></CheatEntries></CheatEntry><CheatEntry><ID>53</ID><Description>"[MAIN] Super Contra (Arcade, US / Japan)"</Description><VariableType>Auto Assembler Script</VariableType><Options moHideChildren="1" moManualExpandCollapse="1" /><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_sarc_enable()
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_sarc",false)
{$asm}
</AssemblerScript><CheatEntries><CheatEntry><ID>56</ID><Description>"Infinite Credits / Continues"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_credits",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_credits",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>54</ID><Description>"Infinite Lives (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_lives1",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_lives1",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>55</ID><Description>"Invincibility (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_inv1",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_inv1",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>74</ID><Description>"Infinite Mega Shells (P1) - overhead stages"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_shell1",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_shell1",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>57</ID><Description>"Weapon (P1)"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
1:Laser
129:Laser (upgraded)
2:Machine Gun
130:Machine Gun (upgraded)
3:Spread Gun
131:Spread Gun (upgraded)
4:Bomb
132:Bomb (upgraded)</DropDownList></CheatEntry><CheatEntry><ID>59</ID><Description>"Infinite Lives (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_lives2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_lives2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>60</ID><Description>"Invincibility (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_inv2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_inv2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>75</ID><Description>"Infinite Mega Shells (P2) - overhead stages / requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_shell2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("sarc_shell2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>58</ID><Description>"Weapon (P2) - requires testing"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
1:Laser
129:Laser (upgraded)
2:Machine Gun
130:Machine Gun (upgraded)
3:Spread Gun
131:Spread Gun (upgraded)
4:Bomb
132:Bomb (upgraded)</DropDownList></CheatEntry></CheatEntries></CheatEntry><CheatEntry>
        <ID>10</ID>
        <Description>"[MAIN] Contra (NES / Famicom)"</Description>
        <Options moHideChildren="1" moManualExpandCollapse="1" />
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_contra", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_contra", false)
{$asm}
</AssemblerScript>
        <CheatEntries>
      <CheatEntry>
        <ID>5</ID>
        <Description>"Infinite Continues"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_cont", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_cont", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>3</ID>
        <Description>"Infinite Lives (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_lives1", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_lives1", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>6</ID>
        <Description>"Invincibility (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_inv1", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_inv1", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>8</ID>
        <Description>"Weapon (P1)"</Description>
        <ShowAsHex>0</ShowAsHex>
        <VariableType>Byte</VariableType>
        <Address>00000000</Address>
        <DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1" DisplayValueAndDescription="0">0:Off / Regular weapon
2:Machine Gun (M)
3:Flame (F)
4:Spread (S)
5:Laser (L)
17:Regular (R) + Rapid Fire
18:Machine Gun (M) + Rapid Fire
19:Flame (F) + Rapid Fire
20:Spread (S) + Rapid Fire
21:Laser (L) + Rapid Fire</DropDownList>
      </CheatEntry>
      <CheatEntry>
        <ID>4</ID>
        <Description>"Infinite Lives (P2) - requires testing"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_lives2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_lives2", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>7</ID>
        <Description>"Invincibility (P2) - requires testing"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_inv2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c_inv2", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>9</ID>
        <Description>"Weapon (P2) - requires testing"</Description>
        <ShowAsHex>0</ShowAsHex>
        <VariableType>Byte</VariableType>
        <Address>00000000</Address>
        <DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1" DisplayValueAndDescription="0">0:Off / Regular weapon
2:Machine Gun (M)
3:Flame (F)
4:Spread (S)
5:Laser (L)
17:Regular (R) + Rapid Fire
18:Machine Gun (M) + Rapid Fire
19:Flame (F) + Rapid Fire
20:Spread (S) + Rapid Fire
21:Laser (L) + Rapid Fire</DropDownList>
      </CheatEntry>
        </CheatEntries>
      </CheatEntry>
      <CheatEntry>
        <ID>18</ID>
        <Description>"[MAIN] Super C / Super Contra (NES / Famicom)"</Description>
        <Options moHideChildren="1" moManualExpandCollapse="1" />
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_superc", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_superc", false)
{$asm}
</AssemblerScript>
        <CheatEntries>
      <CheatEntry>
        <ID>13</ID>
        <Description>"Infinite Continues"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_cont", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_cont", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>11</ID>
        <Description>"Infinite Lives (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_lives1", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_lives1", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>14</ID>
        <Description>"Invincibility (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_inv1", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_inv1", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>16</ID>
        <Description>"Weapon (P1)"</Description>
        <ShowAsHex>0</ShowAsHex>
        <VariableType>Byte</VariableType>
        <Address>00000000</Address>
        <DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1" DisplayValueAndDescription="0">0:Off / Regular weapon
2:Machine Gun (M)
3:Spray (S)
4:Laser (L)
5:Flame (F)
17:Regular (R) + Rapid Fire
18:Machine Gun (M) + Rapid Fire
19:Spray (S) + Rapid Fire
20:Laser (L) + Rapid Fire
21:Flame (F) + Rapid Fire</DropDownList>
      </CheatEntry>
      <CheatEntry>
        <ID>12</ID>
        <Description>"Infinite Lives (P2) - requires testing"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_lives2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_lives2", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>15</ID>
        <Description>"Invincibility (P2) - requires testing"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_inv2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("s_inv2", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>17</ID>
        <Description>"Weapon (P2) - requires testing"</Description>
        <ShowAsHex>0</ShowAsHex>
        <VariableType>Byte</VariableType>
        <Address>00000000</Address>
        <DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1" DisplayValueAndDescription="0">0:Off / Regular weapon
2:Machine Gun (M)
3:Spray (S)
4:Laser (L)
5:Flame (F)
17:Regular (R) + Rapid Fire
18:Machine Gun (M) + Rapid Fire
19:Spray (S) + Rapid Fire
20:Laser (L) + Rapid Fire
21:Flame (F) + Rapid Fire</DropDownList>
      </CheatEntry>
        </CheatEntries>
      </CheatEntry>
      <CheatEntry><ID>40</ID><Description>"[MAIN] Operation C / Contra (Game Boy)"</Description><VariableType>Auto Assembler Script</VariableType><Options moHideChildren="1" moManualExpandCollapse="1" /><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_gb_enable()
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_gb",false)
{$asm}
</AssemblerScript><CheatEntries><CheatEntry><ID>44</ID><Description>"Infinite Continues"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("gb_cont",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("gb_cont",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>41</ID><Description>"Infinite Lives"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("gb_lives",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("gb_lives",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>42</ID><Description>"Invincibility"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("gb_inv",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("gb_inv",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>43</ID><Description>"Weapon"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Spread
3:Spread (upgraded)
4:Homing
5:Flame</DropDownList></CheatEntry></CheatEntries></CheatEntry><CheatEntry>
        <ID>21</ID>
        <Description>"[MAIN] Contra III / Contra Spirits / Super Probotector (SNES)"</Description>
        <Options moHideChildren="1" moManualExpandCollapse="1" />
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_c3_enable()
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_c3", false)
{$asm}
</AssemblerScript>
        <CheatEntries>
      <CheatEntry><ID>36</ID><Description>"Infinite Continues"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_cont", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_cont", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry>
        <ID>19</ID>
        <Description>"Infinite Lives (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_lives", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_lives", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
      <CheatEntry>
        <ID>20</ID>
        <Description>"Invincibility (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_inv", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_inv", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
        <CheatEntry><ID>30</ID><Description>"Gun 1 (P1)"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Spread (S)
3:Crush (C)
4:Homing (H)
5:Flame (F)
6:Laser (L)</DropDownList></CheatEntry><CheatEntry><ID>31</ID><Description>"Gun 2 (P1)"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Spread (S)
3:Crush (C)
4:Homing (H)
5:Flame (F)
6:Laser (L)</DropDownList></CheatEntry><CheatEntry><ID>37</ID><Description>"Infinite Bombs (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_bombs1", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_bombs1", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>34</ID><Description>"Infinite Lives (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_lives2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_lives2", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>35</ID><Description>"Invincibility (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_inv2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_inv2", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>32</ID><Description>"Gun 1 (P2) - requires testing"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Spread (S)
3:Crush (C)
4:Homing (H)
5:Flame (F)
6:Laser (L)</DropDownList></CheatEntry><CheatEntry><ID>33</ID><Description>"Gun 2 (P2) - requires testing"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1">0:Off / Regular weapon
2:Spread (S)
3:Crush (C)
4:Homing (H)
5:Flame (F)
6:Laser (L)</DropDownList></CheatEntry><CheatEntry><ID>38</ID><Description>"Infinite Bombs (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_bombs2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("c3_bombs2", false)
{$asm}
</AssemblerScript></CheatEntry></CheatEntries>
      </CheatEntry>
      <CheatEntry>
        <ID>23</ID>
        <Description>"[MAIN] Hard Corps / Probotector (Mega Drive)"</Description>
        <Options moHideChildren="1" moManualExpandCollapse="1" />
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_hc_enable()
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("game_hc", false)
{$asm}
</AssemblerScript>
        <CheatEntries>
      <CheatEntry><ID>39</ID><Description>"Infinite Continues"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_cont", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_cont", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry>
        <ID>22</ID>
        <Description>"Infinite Lives (P1)"</Description>
        <VariableType>Auto Assembler Script</VariableType>
        <AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_lives", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_lives", false)
{$asm}
</AssemblerScript>
      </CheatEntry>
        <CheatEntry><ID>72</ID><Description>"Infinite Health (P1, Japan only)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
if not CACC_md_native_continues() then error("Infinite Health requires Japanese Hard Corps") end
CACC_set("hc_health1",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_health1",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>25</ID><Description>"Invincibility (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_inv", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_inv", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>26</ID><Description>"Weapon (P1)"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1" DisplayValueAndDescription="0">0:Off / Regular weapon
1:Weapon A
2:Weapon B
3:Weapon C
4:Weapon D</DropDownList></CheatEntry><CheatEntry><ID>28</ID><Description>"Infinite Bombs (P1)"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_bombs1", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_bombs1", false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>69</ID><Description>"Infinite Lives (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_lives2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_lives2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>73</ID><Description>"Infinite Health (P2, Japan only) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
if not CACC_md_native_continues() then error("Infinite Health requires Japanese Hard Corps") end
CACC_set("hc_health2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_health2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>70</ID><Description>"Invincibility (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_inv2",true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_inv2",false)
{$asm}
</AssemblerScript></CheatEntry><CheatEntry><ID>27</ID><Description>"Weapon (P2) - requires testing"</Description><VariableType>Byte</VariableType><Address>00000000</Address><DropDownList ReadOnly="1" DescriptionOnly="1" DisplayValueAsItem="1" DisplayValueAndDescription="0">0:Off / Regular weapon
1:Weapon A
2:Weapon B
3:Weapon C
4:Weapon D</DropDownList></CheatEntry><CheatEntry><ID>29</ID><Description>"Infinite Bombs (P2) - requires testing"</Description><VariableType>Auto Assembler Script</VariableType><AssemblerScript>[ENABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_bombs2", true)
{$asm}
[DISABLE]
{$lua}
if syntaxcheck then return end
CACC_set("hc_bombs2", false)
{$asm}
</AssemblerScript></CheatEntry></CheatEntries>
      </CheatEntry>
      </CheatEntries>
  <LuaScript>--[[
Table identification: Katzy
Contra Anniversary Collection | Steam build 3875530
]]
-- One state ledger owns every reversible change in this table.
if CACC_shutdown then CACC_shutdown() elseif CACC_gb_restore_all then
  if CACC and CACC.timer then CACC.timer.Enabled=false;CACC.timer.destroy() end
  CACC_gb_restore_all()
  if CACC.sel and CACC.pid==getOpenedProcessID() then deAlloc(CACC.sel) end
end
CACC={on={},saved={},sel_val={},EXPECTED_MODULE_SIZE=0x1C47000,
  ID_CONTRA="60C001C0B5C0",ID_SUPERC="67FBCAFA7FE0"}
function CACC_ge(a,b) return type(a)=="number" and math.max(a,b)==a end
function CACC_le(a,b) return type(a)=="number" and math.min(a,b)==a end
function CACC_ok(p) return CACC_ge(p,0x10000) and CACC_le(p,0x7FF00000) end
function CACC_hex(v) return v and string.format("%X",v) or "nil" end
function CACC_rbn(a,n)
  if not a then return nil end
  local ok,v=pcall(readBytes,a,n,true)
  if ok and v and #v==n then return v end
end
function CACC_rb(a) local v=CACC_rbn(a,1);return v and v[1] end
function CACC_ri(a)
  if not a then return nil end
  local ok,v=pcall(readInteger,a);if ok then return v end
end
function CACC_wpm(a,v)
  local values=type(v)=="table" and v or {v}
  local old=CACC_rbn(a,#values);if not old then error("Unreadable target "..CACC_hex(a)) end
  local same=true;for i,x in ipairs(values) do if old[i]~=x then same=false end end
  if same then return end
  local ok,result=pcall(writeBytes,a,values)
  local check=CACC_rbn(a,#values)
  if not ok or result==false or not check then error("Write failed at "..CACC_hex(a)) end
  for i,x in ipairs(values) do if check[i]~=x then error("Write verification failed at "..CACC_hex(a)) end end
end
function CACC_mask(value,mask)
  local result=0
  for bit=0,7 do local p=2^bit;if math.floor(mask/p)%2==1 and math.floor(value/p)%2==1 then result=result+p end end
  return result
end
function CACC_merge(old,value,mask) return old-CACC_mask(old,mask)+CACC_mask(value,mask) end
CACC.groups={
 game_contra={id=10,features={"c_lives1","c_lives2","c_cont","c_inv1","c_inv2"},selectors={1,2}},
 game_superc={id=18,features={"s_lives1","s_lives2","s_cont","s_inv1","s_inv2"},selectors={3,4}},
 game_c3={id=21,features={"c3_lives","c3_lives2","c3_cont","c3_inv","c3_inv2","c3_bombs1","c3_bombs2"},selectors={7,8,9,10}},
 game_hc={id=23,features={"hc_lives","hc_cont","hc_inv","hc_bombs1","hc_bombs2","hc_lives2","hc_inv2","hc_health1","hc_health2"},selectors={5,6}},
 game_sarc={id=53,features={"sarc_shell1","sarc_shell2","sarc_lives1","sarc_lives2","sarc_inv1","sarc_inv2","sarc_credits"},selectors={14,15}},
 game_arc={id=45,features={"arc_lives1","arc_lives2","arc_inv1","arc_inv2","arc_credits"},selectors={12,13}},
 game_gb={id=40,features={"gb_lives","gb_inv","gb_cont"},selectors={11}}
}
CACC.featureIds={sarc_shell1=74,sarc_shell2=75,hc_health1=72,hc_health2=73,hc_lives2=69,hc_inv2=70,sarc_lives1=54,sarc_inv1=55,sarc_credits=56,sarc_lives2=59,sarc_inv2=60,arc_lives1=46,arc_lives2=51,arc_inv1=47,arc_inv2=52,arc_credits=48,c_lives1=3,c_lives2=4,c_cont=5,c_inv1=6,c_inv2=7,s_lives1=11,s_lives2=12,s_cont=13,s_inv1=14,s_inv2=15,
 c3_lives=19,c3_inv=20,c3_lives2=34,c3_inv2=35,c3_cont=36,c3_bombs1=37,c3_bombs2=38,
 hc_lives=22,hc_inv=25,hc_bombs1=28,hc_bombs2=29,hc_cont=39,gb_lives=41,gb_inv=42,gb_cont=44}
CACC.selectorIds={8,9,16,17,26,27,30,31,32,33,43,49,50,57,58}
-- Build 3875530: read the collection's own Squirrel gameplay controller.
-- Creation/deletion was traced in play_standalone.nut; no script is executed.
CACC.sessions={}
CACC.sessionKeys={}
CACC.architectures={game_hc="md",game_contra="nes",game_superc="nes",
  game_gb="gameboy",game_arc="contra",game_sarc="scontra"}
function CACC_u32(bytes,offset)
  return bytes[offset+1]+256*bytes[offset+2]+65536*bytes[offset+3]+16777216*bytes[offset+4]
end
function CACC_sq_text(address,limit)
  if not CACC_ok(address) then return nil end
  local header=CACC_rbn(address,24)
  if not header or CACC_u32(header,0)~=CACC.mod+0x7408B8 then return nil end
  local size=CACC_u32(header,20)
  if size&lt;1 or size&gt;limit then return nil end
  local text=CACC_rbn(address+28,size)
  return text and string.char(table.unpack(text)) or nil
end
function CACC_sq_key(address,name)
  return CACC_sq_text(address,#name)==name
end
function CACC_sq_find(object,name)
  if not CACC_ok(object) then return nil end
  local header=CACC_rbn(object,44)
  if not header or CACC_u32(header,0)~=CACC.mod+0x74081C then return nil end
  local nodes,count,used=CACC_u32(header,32),CACC_u32(header,36),CACC_u32(header,40)
  if not CACC_ok(nodes) or count&lt;1 or count&gt;4096 or used&gt;count then return nil end
  local key=tostring(object)..":"..name
  local cached=CACC.sessionKeys[key]
  local function match(address,bytes,offset)
    if CACC_u32(bytes,offset+16)~=0x08000010 or not CACC_sq_key(CACC_u32(bytes,offset+24),name) then return nil end
    return {address=address,type=CACC_u32(bytes,offset),value=CACC_u32(bytes,offset+8)}
  end
  if cached and cached.nodes==nodes and cached.count==count then
    local bytes=CACC_rbn(cached.address,32)
    local found=bytes and match(cached.address,bytes,0)
    if found then return found end
  end
  local bytes=CACC_rbn(nodes,count*40)
  if not bytes then return nil end
  for index=0,count-1 do
    local offset=index*40
    local found=match(nodes+offset,bytes,offset)
    if found then
      CACC.sessionKeys[key]={nodes=nodes,count=count,address=found.address}
      return found
    end
  end
end
function CACC_session_read()
  if CACC.transactionDepth and CACC.sessionRead~=nil then return CACC.sessionRead or nil end
  local function read()
    if not CACC.mod or not CACC.pid or CACC.pid~=getOpenedProcessID() then return nil end
    local vm=CACC_ri(CACC.mod+0x78AB20)
    if not CACC_ok(vm) or CACC_ri(vm)~=CACC.mod+0x74058C then return nil end
    if CACC_ri(vm+0x38)~=0x0A000020 then return nil end
    local root=CACC_ri(vm+0x40)
    local task=CACC_sq_find(root,"g_emu_task")
    local title=CACC_sq_find(root,"s_current_title_dev_id")
    local architecture=CACC_sq_find(root,"s_init_emulator_arch_current")
    if not task or task.type~=0x0A008000 or not CACC_ok(task.value) or not title or title.type~=0x05000002
      or not architecture or architecture.type~=0x08000010 then return nil end
    local arch=CACC_sq_text(architecture.value,16)
    if not arch then return nil end
    local instance=CACC_rbn(task.value,44)
    if not instance or CACC_u32(instance,0)~=CACC.mod+0x740804 then return nil end
    local members,class,size=CACC_u32(instance,24),CACC_u32(instance,28),CACC_u32(instance,40)
    if not CACC_ok(class) or CACC_ri(class)~=CACC.mod+0x7407F0 or CACC_ri(class+24)~=members then return nil end
    local member=CACC_sq_find(members,"standalone_control")
    if not member or member.type~=0x05000002 or math.floor(member.value/0x1000000)~=2 then return nil end
    local offset=48+16*(member.value%0x1000000)
    if offset+16&gt;size or size&gt;0x10000 then return nil end
    local control=CACC_rbn(task.value+offset,16)
    if not control then return nil end
    local kind,pointer=CACC_u32(control,0),CACC_u32(control,8)
    if kind==0x01000001 then pointer=0
    elseif kind~=0x0A008000 or not CACC_ok(pointer) or CACC_ri(pointer)~=CACC.mod+0x740804 then return nil end
    return {pid=CACC.pid,vm=vm,root=root,task=task.value,title=title.value,control=pointer,arch=arch}
  end
  local result=read()
  if CACC.transactionDepth then CACC.sessionRead=result or false end
  return result
end
function CACC_session_matches(saved,current,cleanup)
  if not saved or not current or saved.control==0 then return false end
  for _,key in ipairs({"pid","vm","root","task","title","arch"}) do if saved[key]~=current[key] then return false end end
  return saved.control==current.control or (cleanup and current.control==0)
end
function CACC_session_allows(group,current)
  if not current then return false end
  if group=="game_c3" then return current.arch=="sfc" or current.arch=="sfc_cga" end
  return CACC.architectures[group]~=nil and current.arch==CACC.architectures[group]
end
function CACC_session_require(group)
  local current=CACC_session_read()
  if not current or current.control==0 then error("Start gameplay before enabling this MAIN group.") end
  if not CACC_session_allows(group,current) then error("This MAIN group does not match the game currently running.") end
  return current
end

function CACC_context(group,base,core)
  return {group=group,base=base,core=core or (group=="game_hc" and CACC.hc_core),pid=CACC.pid,rom=CACC.hc_rom,session=CACC.sessions[group]}
end
function CACC_nes_matches(group,id)
  if group=="game_contra" then return id==CACC.ID_CONTRA or id=="89F80DF8E1F8" end
  return group=="game_superc" and (id==CACC.ID_SUPERC or id=="2BFB8EFA7FE0")
end
function CACC_context_valid(ctx)
  if not CACC_session_allows(ctx.group,ctx.session) or not CACC_session_matches(ctx.session,CACC_session_read(),true) then return false end
  if not CACC_ok(ctx.base) then return false end
  if ctx.pid~=CACC.pid or ctx.pid~=getOpenedProcessID() then return false end
  if ctx.group=="game_contra" or ctx.group=="game_superc" then
    return CACC_nes_matches(ctx.group,CACC_nes_romid()) and CACC_chain("nes")==ctx.base
  elseif ctx.group=="game_c3" then return CACC_c3_valid(ctx.core) and ctx.base==ctx.core+0x1A0
  elseif ctx.group=="game_sarc" then return CACC_sarc_ram(ctx.core)==ctx.base
  elseif ctx.group=="game_arc" then return CACC_arc_ram(ctx.core)==ctx.base
  elseif ctx.group=="game_gb" then return CACC_gb_ram(ctx.core)==ctx.base
  elseif ctx.group=="game_hc" then
    local active=CACC_md_active()
    return active~=nil and ctx.core==active.core and ctx.rom==active.rom+0x150
      and (ctx.base==active.buffers[1] or ctx.base==active.buffers[2])
  end
  return false
end
-- MD-only lifecycle gate: defer grants until the player actor is ready.
function CACC_md_grant_ready(ctx,index,saved)
  if ctx.group~="game_hc" then return true end
  local actor=ctx.base+0xD700+(index-5)*0x80
  local bytes=CACC_rbn(actor,4)
  if not bytes or #bytes~=4 then error("Player lifecycle is unreadable") end
  local kind=bytes[1]+256*bytes[2]
  local state=bytes[3]+256*bytes[4]
  if kind==0 or state==0x22 then
    if saved then saved.respawnPending=true end
    return false
  end
  if saved and saved.respawnPending then
    saved.grantChoice=nil
    saved.respawnPending=nil
  end
  return true
end

function CACC_hold(ctx,key,spec,after,code)
  local token=key..":"..ctx.base
  local saved=CACC.saved[token]
  local weaponIndex=ctx.group=="game_hc" and tonumber(key:match("^weapon(%d+)$"))
  local grantChoice=weaponIndex and CACC_selread(weaponIndex)
  if weaponIndex and not CACC_md_grant_ready(ctx,weaponIndex,saved) then return false end
  if weaponIndex and saved and saved.grantChoice==grantChoice then return false end
  if not saved then
    local cells={}
    for i,item in ipairs(spec) do
      local a=ctx.base+item[1];local v=CACC_rb(a)
      if v==nil then error("Cannot save original value for "..key) end
      cells[i]={address=a,original=v,mask=item[3] or 255,last=v}
    end
    saved={ctx=ctx,key=key,cells=cells,after=after,code=code};CACC.saved[token]=saved
  end
  if code then
    local values={}
    for i,item in ipairs(spec) do
      local cell=saved.cells[i];local now=CACC_rb(cell.address)
      if now~=cell.last and now~=cell.original then error("Code changed outside this table") end
      cell.last=item[2];values[i]=item[2]
    end
    CACC_wpm(saved.cells[1].address,values);return
  end
  for i,item in ipairs(spec) do
    local cell=saved.cells[i];local old=CACC_rb(cell.address)
    if old==nil then error("Target became unreadable for "..key) end
    local v=CACC_merge(old,item[2],cell.mask)
    cell.last=v -- Keep rollback information even if a subsequent write fails.
    -- The transaction already stopped the game and read this byte. An
    -- unchanged value needs no second read; actual writes still verify.
    if v~=old then CACC_wpm(cell.address,v) end
  end
  if weaponIndex then saved.grantChoice=grantChoice end
  return true
end
function CACC_off_value(saved,cell)
  if saved.key=="gb_inv" and saved.ctx.group=="game_gb" and cell.address==saved.ctx.base+0x09 then return 1 end
  -- A completed native countdown cannot clear a resurrected protection flag.
  if saved.key=="gb_inv" and saved.ctx.group=="game_gb" and cell.address==saved.ctx.base+0x29 then
    local timer=CACC_rb(saved.ctx.base+0x18)
    if timer==nil then error("Protection countdown unreadable") end
    if timer==0 then return 0 end
  end
  if not saved.key:match("^weapon%d+$") then return cell.original end
  local group=saved.ctx.group;local offset=cell.address-saved.ctx.base
  if group=="game_gb" then
    return ({[0x881]=5,[0x882]=1,[0x883]=14,[0x884]=4,[0x885]=22})[offset] or cell.original
  elseif group=="game_hc" then
    if offset==0xFA0E or offset==0xFA2E then return CACC_merge(cell.original,1,3) end
    return 0
  elseif group=="game_contra" or group=="game_superc" or group=="game_c3" or group=="game_arc" or group=="game_sarc" then
    return 0
  end
  error("No regular weapon configuration for "..tostring(group))
end
function CACC_release(key,group)
  local tokens={};for token,s in pairs(CACC.saved) do if (not key or s.key==key or (key=="sarc_credits" and s.key:match("^sarc_creditgate[123]$"))) and (not group or s.ctx.group==group) then tokens[#tokens+1]=token end end
  local errors={}
  for _,token in ipairs(tokens) do
    local s=CACC.saved[token]
    if not CACC_context_valid(s.ctx) then
      CACC.saved[token]=nil -- Never restore into a replacement process or unrelated RAM.
    else
      local ok,err=pcall(function()
        if s.code then for _,cell in ipairs(s.cells) do local v=CACC_rb(cell.address);if v~=cell.last and v~=cell.original then error("Code changed outside this table; refusing to overwrite it") end end end
        if s.code then
          local values={};for i,cell in ipairs(s.cells) do values[i]=cell.original end
          CACC_wpm(s.cells[1].address,values)
        else for _,cell in ipairs(s.cells) do
          local current=CACC_rb(cell.address);if current==nil then error("Restoration target unreadable") end
          CACC_wpm(cell.address,CACC_merge(current,CACC_off_value(s,cell),cell.mask))
        end
        end
        if s.after then s.after(s.ctx) end
      end)
      if ok then CACC.saved[token]=nil else errors[#errors+1]=tostring(err) end
    end
  end
  if #errors~=0 then error(table.concat(errors,"\n")) end
end
function CACC_option(ctx,key,spec)
  if CACC.on[key] then CACC_hold(ctx,key,spec) else CACC_release(key,ctx.group) end
end
function CACC_selread(i)
  if not CACC.sel then return 0 end
  local value=CACC_rb(CACC.sel+i-1) or 0;CACC.sel_val[i]=value;return value
end
function CACC_bindsel()
  if CACC.sel then return true end
  CACC.sel=allocateMemory(0x40);if not CACC.sel then error("Could not allocate weapon selectors") end
  local list=getAddressList()
  for i,id in ipairs(CACC.selectorIds) do
    CACC_wpm(CACC.sel+i-1,0)
    local record=list.getMemoryRecordByID(id);if record then record.Address=string.format("%X",CACC.sel+i-1) end
  end
  return true
end
function CACC_uncheck(id)
  local r=getAddressList().getMemoryRecordByID(id);if r and r.Active then r.Active=false end
end
function CACC_disable_group(group)
  local config=CACC.groups[group];if not config then return end
  CACC.on[group]=false
  for _,key in ipairs(config.features) do CACC.on[key]=false end
  local ok,err=pcall(CACC_release,nil,group)
  if CACC.sel and CACC.pid==getOpenedProcessID() then for _,i in ipairs(config.selectors) do CACC_wpm(CACC.sel+i-1,0);CACC.sel_val[i]=0 end end
  CACC.syncing=true
  for _,key in ipairs(config.features) do CACC_uncheck(CACC.featureIds[key]) end
  CACC_uncheck(config.id)
  CACC.syncing=false
  if ok then CACC.sessions[group]=nil end
  if not ok then error(err) end
end
function CACC_disable_all()
  local errors={}
  for group in pairs(CACC.groups) do local ok,err=pcall(CACC_disable_group,group);if not ok then errors[#errors+1]=tostring(err) end end
  if #errors~=0 then error(table.concat(errors,"\n")) end
end
function CACC_set(key,value)
  local session=nil
  if CACC.syncing then return end
  if CACC.groups[key] then
    if not value then CACC_disable_group(key);return end
    if not CACC_attach() then error("Launch the supported game first") end
    session=CACC_session_require(key)
    if key=="game_contra" or key=="game_superc" then
      if not CACC_nes_matches(key,CACC_nes_romid()) or not CACC_chain("nes") then error("Load the selected NES game first") end
    end
  end
  if session then
    if not CACC_session_matches(session,CACC_session_read(),false) then error("The selected game changed while enabling cheats.") end
    CACC.sessions[key]=session
  end
  if value and CACC.timer then CACC.timer.Enabled=true end
  CACC.on[key]=value
  if not value then
    if key=="gb_inv" then CACC_release("gb_inv_code") end
    CACC_release(key)
  end
end
function CACC_attach()
  local pid=getProcessIDFromProcessName("game.exe")
  if not pid or pid==0 then CACC.pid=nil;CACC.mod=nil;CACC.sel=nil;CACC.on={};CACC.saved={};CACC.sessions={};CACC.sessionKeys={};return false end
  if CACC.pid~=pid or getOpenedProcessID()~=pid then
    -- An old PID cannot own this new process's addresses.
    CACC.on={};CACC.saved={};CACC.sessions={};CACC.sessionKeys={};CACC.sel=nil;CACC.mod=nil
    openProcess(pid);if getOpenedProcessID()~=pid then return false end
    CACC.pid=pid
  end
  if not CACC.mod then
    if getModuleSize("game.exe")~=CACC.EXPECTED_MODULE_SIZE then error("Unsupported game.exe build; cheats disabled") end
    CACC.mod=getAddress("game.exe")
  end
  return true
end
function CACC_chain(core,trace)
  if core~="nes" or not CACC.mod then return nil end
  local p=CACC_ri(CACC.mod+0x78AA70)
  if CACC_ok(p) then p=CACC_ri(p) end
  if CACC_ok(p) then p=CACC_ri(p+0x2C) end
  if CACC_ok(p) and CACC_rb(p+0x7FF) then return p end
end

function CACC_nes_romid()
  if CACC.mod == nil then return nil end
  local a = CACC_ri(CACC.mod + 0x78AA70)
  if CACC_ok(a) == false then return nil end
  local b = CACC_ri(a)
  if CACC_ok(b) == false then return nil end
  local page = CACC_ri(b + 0x2C + 31 * 4)
  if CACC_ok(page) == false then return nil end
  local t = CACC_rbn(page + 0x7FA, 6)
  if t == nil then return nil end
  local s = ""
  local i = 1
  while CACC_le(i, 6) do
    s = s .. string.format("%02X", t[i])
    i = i + 1
  end
  return s
end

CACC.snesVariants={
  {name="Contra III",title="CONTRA3 THE ALIEN WAR",header="CONTRA3 THE ALIEN WARS"},
  {name="Super Probotector",title="SUPER PROBOTECTOR    ",header="SUPER PROBOTECTOR    "},
  {name="Contra Spirits",title="CONTRA SPIRITS       ",header="CONTRA SPIRITS       ",nativeContinues=true}
}
function CACC_c3_variant(c)
  if not CACC_ok(c) then return nil end
  local title=CACC_rbn(c+0x154,21)
  if not title then return nil end
  title=string.char(table.unpack(title))
  local variant=nil
  for _,v in ipairs(CACC.snesVariants) do if title==v.title then variant=v;break end end
  if not variant then return nil end
  local rom=CACC_ri(c+0x16C);local bus=CACC_ri(c+0x144)
  if not CACC_ok(rom) or not CACC_ok(bus) then return nil end
  if CACC_ri(c+0x170)~=0x100000 or CACC_ri(bus)~=c or CACC_ri(bus+0x38)~=c+0x1A0 then return nil end
  local hdr=CACC_rbn(rom+0x7FC0,#variant.header)
  if hdr and string.char(table.unpack(hdr))==variant.header and CACC_rb(c+0x2019F)~=nil then return variant end
end
function CACC_c3_valid(c) return CACC_c3_variant(c)~=nil end
function CACC_c3_enable()
  CACC_disable_group("game_c3")
  CACC.on.game_c3=false;CACC.c3_core=nil
  if not CACC_attach() then error("Launch the supported game first.") end
  local candidates={};local seen={}
  for _,variant in ipairs(CACC.snesVariants) do
    local pattern={}
    for v in (variant.title..string.char(0)):gmatch(".") do table.insert(pattern,string.format("%02X",string.byte(v))) end
    local hits=AOBScan(table.concat(pattern," "),"+W")
    if hits then
      for i=0,hits.Count-1 do
        local c=tonumber(hits[i],16)-0x154
        if not seen[c] and CACC_c3_valid(c) then seen[c]=true;table.insert(candidates,c) end
      end
      hits.destroy()
    end
  end
  if #candidates~=1 then error("Load Contra III, Contra Spirits or Super Probotector gameplay, then enable this group again. A unique RAM structure was not found.") end
  CACC.c3_core=candidates[1]
  if CACC.timer then CACC.timer.Enabled=true end
  CACC.on.game_c3=true
end

function CACC_hc_valid(b)
  return CACC.mod ~= nil and CACC_ri(b-0x1C) == CACC.mod+0x73A330
    and CACC_ri(b-0x18) == 0x8000 and CACC_rb(b+0xFFFF) ~= nil
end
-- The engine owns a linked list of live CPUs at module+78AA70. The
-- M68000 byte-read handler indexes its bank table at CPU+1C4 and swaps
-- the low address bit. RAM bank FF and its reciprocal mirror are owned
-- by this CPU; unrelated title strings are not evidence of ownership.
function CACC_md_active()
  if not CACC.mod or CACC.pid~=getOpenedProcessID() then return nil end
  local node=CACC_ri(CACC.mod+0x78AA70)
  local seen,found={},nil
  local m68000,z80=0,0
  for count=1,32 do
    if node==0 then return found end
    if not CACC_ok(node) or seen[node] then return nil end
    seen[node]=true
    local cpu=CACC_ri(node)
    if not CACC_ok(cpu) then return nil end
    local kind=CACC_ri(cpu)
    if kind==CACC.mod+0x77FF30 then
      m68000=m68000+1;if m68000&gt;1 then return nil end
      if CACC_ri(cpu+0xD4)~=CACC.mod+0x38520 then return nil end
      local rom=CACC_ri(cpu+0x1C4)
      local ram=CACC_ri(cpu+0x5C0)
      if not CACC_ok(rom) or not CACC_ok(ram) then return nil end
      local raw=CACC_rbn(rom+0x100,0x100)
      if not raw then return nil end
      local chars={}
      for i=1,#raw,2 do chars[i]=string.char(raw[i+1]);chars[i+1]=string.char(raw[i]) end
      local header=table.concat(chars)
      local title=header:sub(0x51,0x80):gsub(" +$","")
      if header:sub(1,4)=="SEGA" and (title=="CONTRA HARDCORPS" or title=="PROBOTECTOR") then
        local mirror=CACC_ri(ram-0x10)
        if not CACC_hc_valid(ram) or not CACC_ok(mirror) or mirror==ram or not CACC_hc_valid(mirror)
          or CACC_ri(mirror-0x10)~=ram then return nil end
        if found then return nil end
        found={core=cpu,rom=rom,title=title,region=header:sub(0xF1,0x100),buffers={ram,mirror}}
      end
    elseif kind==CACC.mod+0x780004 then
      z80=z80+1;if z80&gt;1 then return nil end
    else
      -- A different console/arcade CPU still being installed or removed
      -- makes this a transition, not an isolated active Mega Drive.
      return nil
    end
    node=CACC_ri(node+4)
  end
  return nil
end

function CACC_md_native_continues()
  local active=CACC_md_active()
  return active~=nil and active.core==CACC.hc_core and active.rom+0x150==CACC.hc_rom
    and active.title=="CONTRA HARDCORPS" and active.region=="J               "
end
function CACC_hc_enable()
  CACC_disable_group("game_hc")
  CACC.on.game_hc=false;CACC.hc_buffers={};CACC.hc_core=nil;CACC.hc_rom=nil
  if not CACC_attach() then error("Launch the supported game first.") end
  local active=CACC_md_active()
  if not active then error("Load Hard Corps or Probotector gameplay first. A unique active CPU and paired RAM were not found.") end
  CACC.hc_core=active.core;CACC.hc_rom=active.rom+0x150;CACC.hc_buffers=active.buffers
  if CACC.timer then CACC.timer.Enabled=true end
  CACC.on.game_hc=true
end

-- CPU+18 is a timeslice checkpoint, not the currently executing instruction.
-- The verified interpreter updates address registers in memory before each
-- queue access. Check current and stack-saved cursors (interrupts can hide a
-- live cursor in a MOVEM save) while the host transaction holds the process.
function CACC_hc_queue_idle(b)
  local active=CACC_md_active()
  if not active or (b~=active.buffers[1] and b~=active.buffers[2]) then return false end
  local cpu=active.core
  if CACC_ri(cpu+0xDC4)~=0 then return false end -- accelerator not covered by this proof
  local function cursor(v)
    if type(v)~="number" then return nil end
    local address=v%0x1000000
    return address&gt;=0xFFA1F2 and address&lt;=0xFFA235
  end
  local a0,a6,sp=CACC_ri(cpu+0x48),CACC_ri(cpu+0x60),CACC_ri(cpu+0x64)
  if a0==nil or a6==nil or sp==nil or cursor(a0) or cursor(a6) then return false end
  local initial=CACC_rbn(active.rom,4)
  if not initial then return false end
  local top=((initial[1]+initial[2]*256)*65536+initial[3]+initial[4]*256)%0x1000000
  sp=sp%0x1000000
  if sp%2~=0 or top%2~=0 or sp&lt;0xFF0000 or top&gt;0xFFFFFF or sp&gt;top or top-sp&gt;4096 then return false end
  if sp==top then return true end
  local stack=CACC_rbn(active.buffers[1]+sp%65536,top-sp)
  if not stack then return false end
  for i=1,#stack-3,2 do
    local value=(stack[i]+stack[i+1]*256)*65536+stack[i+2]+stack[i+3]*256
    if cursor(value) then return false end
  end
  return true
end

-- Native MD HUD commands only write fixed tile areas. Retain the final
-- occurrence in its original order, then append our newest redraw.
function CACC_hc_queue(b)
  local bytes=CACC_rbn(b+0xA1F4,64)
  if not bytes then return nil end
  local queue={}
  for i=1,63,2 do
    local command=bytes[i+1]
    if command==0 then return queue end
    local kind=command%128
    if i==63 or kind&lt;1 or kind&gt;13 then return nil end
    queue[#queue+1]={value=bytes[i],command=command}
  end
end
function CACC_hc_hud(b, player, choice, value)
  local mode=CACC_rb(b+0xA087)
  if mode~=3 or not CACC_hc_queue_idle(b) then return false end
  local queue=CACC_hc_queue(b)
  if not queue then return false end
  local command=4+choice+(player-1)*0x80
  queue[#queue+1]={value=value or 1,command=command}
  local last={}
  for i,item in ipairs(queue) do
    -- Regular and A paint the same complete 2x2 icon for this player.
    local key=item.command
    if key%128==10 then key=key-5 end
    item.key=key;last[key]=i
  end
  local bytes={}
  for i,item in ipairs(queue) do
    if last[item.key]==i then
      bytes[#bytes+1]=item.value;bytes[#bytes+1]=item.command
    end
  end
  -- At most 12 distinct drawing commands per player, plus terminator.
  if #bytes&gt;60 then return false end
  bytes[#bytes+1]=0;bytes[#bytes+1]=0
  CACC_wpm(b+0xA1F4,bytes)
  return true
end

function CACC_hc_finish_hud()
  -- Modes 2/4 are dialogue setup/display; 5 reloads the HUD and then
  -- redraw both players from native state before returning to mode 3.
  -- Their old HUD queue is obsolete. Do not draw it over the dialogue or
  -- leave it occupying slots needed by the game's full redraw.
  for key,pending in pairs(CACC.hud or {}) do
    if not CACC_context_valid(pending.ctx) then CACC.hud[key]=nil
    else
      local mode=CACC_rb(pending.ctx.base+0xA087)
      if (mode==2 or mode==4 or mode==5) and CACC_hc_queue_idle(pending.ctx.base) then
        if not CACC_hc_queue(pending.ctx.base) then error("Hard Corps display queue could not be validated for cleanup") end
        CACC_wpm(pending.ctx.base+0xA1F4,{0,0})
        CACC.hud[key]=nil
      end
    end
  end
  CACC_hc_flush_hud()
  return next(CACC.hud or {})==nil
end

CACC.gbVariants={
 {title="OPERATION C",inv=0x1AE0,cont=0x3E03},
 {title="CONTRA",inv=0x1AAC,cont=0x3E09}
}
function CACC_gb_variant(c)
  if not CACC_ok(c) then return nil end
  for _,v in ipairs(CACC.gbVariants) do
    local title=CACC_rbn(c+0x138,#v.title+1)
    local header=CACC_rbn(c+0x2A8,#v.title+1)
    local expected=v.title..string.char(0)
    if title and header and string.char(table.unpack(title))==expected and string.char(table.unpack(header))==expected then return v end
  end
end
function CACC_gb_ram(c)
  if not CACC_ok(c) then return nil end
  if not CACC_gb_variant(c) then return nil end
  if CACC_ri(c+0x16C) ~= c then return nil end
  local bus=CACC_ri(c+0x160)
  if not CACC_ok(bus) or CACC_ri(bus) ~= c+0x15C then return nil end
  if CACC_ri(bus+0x38) ~= c+0x174 then return nil end
  local b=CACC_ri(bus+0x78)
  if not CACC_ok(b) or CACC_ri(bus+0x88) ~= b+0x1000 then return nil end
  if CACC_rb(b+0x1FFF) == nil then return nil end
  return b
end

function CACC_gb_enable()
  CACC_disable_group("game_gb")
  CACC.on.game_gb=false
  CACC.gb_core=nil
  if not CACC_attach() then error("Launch the supported game first.") end
  local candidates={};local seen={}
  for _,v in ipairs(CACC.gbVariants) do
    local pattern={}
    for ch in (v.title..string.char(0)):gmatch(".") do pattern[#pattern+1]=string.format("%02X",string.byte(ch)) end
    local hits=AOBScan(table.concat(pattern," "),"+W")
    if hits then
      for i=0,hits.Count-1 do
        local c=tonumber(hits[i],16)-0x138
        if not seen[c] and CACC_gb_ram(c) then seen[c]=true;candidates[#candidates+1]=c end
      end
      hits.destroy()
    end
  end
  if #candidates ~= 1 then error("Load Operation C or Japanese Contra GB gameplay, then enable this group again. A unique RAM mapping was not found.") end
  CACC.gb_core=candidates[1]
  if CACC.timer then CACC.timer.Enabled=true end
  CACC.on.game_gb=true
end

function CACC_decode(v)
  if CACC_ge(v,1) and CACC_le(v,5) then return v-1,false end
  if CACC_ge(v,17) and CACC_le(v,21) then return v-17,true end
end
function CACC_nes_tick(group,base)
  local contra=group=="game_contra"
  local prefix=contra and "c_" or "s_"
  local ctx=CACC_context(group,base)
  local map=contra and {0x32,0x33,0x3A,0xAE,0xAF,0xAA,0xAB} or {0x53,0x54,0x59,0xC4,0xC5,0xB8,0xB9}
  for i,key in ipairs({"lives1","lives2","cont","inv1","inv2"}) do CACC_option(ctx,prefix..key,{{map[i],CACC_le(i,3) and 9 or 0xF0}}) end
  for player=1,2 do
    local index=(contra and 0 or 2)+player
    local w,rapid=CACC_decode(CACC_selread(index))
    local key="weapon"..index
    if w then
      local spec={{map[5+player],w+(rapid and (contra and 0x10 or 0x80) or 0)}}
      if contra and player==1 then spec[#spec+1]={0x2F,({[0]=0,2,1,3,2})[w]} end
      CACC_hold(ctx,key,spec)
    else CACC_release(key,group) end
  end
end
function CACC_word(offset,value) return {{offset,value%256},{offset+1,math.floor(value/256)}} end
function CACC_snes_contra3(base)
  local ctx=CACC_context("game_c3",base,CACC.c3_core)
  local variant=CACC_c3_variant(CACC.c3_core)
  if variant and variant.nativeContinues then CACC_release("c3_cont",ctx.group)
  else CACC_option(ctx,"c3_cont",CACC_word(0x1E62,3)) end
  for player=1,2 do
    local shift=(player-1)*0x40
    local alive=(CACC_rb(base+0x1F8A+shift) or 0)~=0
    local keys=player==1 and {"c3_lives","c3_inv","c3_bombs1"} or {"c3_lives2","c3_inv2","c3_bombs2"}
    for i,off in ipairs({0x1F8A,0x1F88,0x1F8C}) do
      if alive and CACC.on[keys[i]] then CACC_hold(ctx,keys[i],CACC_word(off+shift,i==2 and 0x200 or 9))
      elseif not CACC.on[keys[i]] then CACC_release(keys[i],ctx.group) end
    end
    for slot=1,2 do
      local index=6+(player-1)*2+slot;local choice=CACC_selread(index);local key="weapon"..index
      if CACC_ge(choice,1) and CACC_le(choice,6) then
        if alive then CACC_hold(ctx,key,CACC_word(0x1F84+shift+(slot-1)*2,choice-1)) end
      else CACC_release(key,ctx.group) end
    end
  end
end
function CACC_hc_refresh(ctx,player,weapon,bombs)
  CACC.hud=CACC.hud or {};local key=ctx.base..":"..player
  local pending=CACC.hud[key] or {ctx=ctx,player=player};CACC.hud[key]=pending
  if weapon then
    local shift=(player-1)*0x20
    CACC_wpm(ctx.base+0xA5B1+(player-1)*0x80,0)
    CACC_wpm(ctx.base+0xD754+(player-1)*0x80,0)
    local owned=CACC_rb(ctx.base+0xFA0E+shift)
    pending.icons={}
    for choice=1,4 do pending.icons[choice]=math.floor(owned/2^choice)%2 end
    if CACC_mask(owned,1)==1 and CACC_mask(owned,2)==0 then pending.icons[1]=nil;pending.icons[6]=1 end
  end
  if bombs then pending.bombs=CACC_rb(ctx.base+0xFA10+(player-1)*0x20) end
end
function CACC_hc_flush_hud()
  for key,pending in pairs(CACC.hud or {}) do
    if not CACC_context_valid(pending.ctx) then CACC.hud[key]=nil
    else
      if pending.icons then
        for choice,value in pairs(pending.icons) do if CACC_hc_hud(pending.ctx.base,pending.player,choice,value) then pending.icons[choice]=nil end end
        if next(pending.icons)==nil then pending.icons=nil end
      end
      if pending.bombs and CACC_hc_hud(pending.ctx.base,pending.player,5,pending.bombs) then pending.bombs=nil end
      if pending.health and CACC_hc_hud(pending.ctx.base,pending.player,0,pending.health) then pending.health=nil end
      if not pending.icons and pending.bombs==nil and pending.health==nil then CACC.hud[key]=nil end
    end
  end
end
-- Japan-only optional health hold; uses the native max HP and command 4.
function CACC_hc_health_refresh(ctx,player)
  local shift=(player-1)*0x20
  local hp=CACC_rb(ctx.base+0xFA0C+shift)
  local maximum=CACC_rb(ctx.base+0xFA13+shift)
  if hp==nil or maximum==nil then error("Health display fields unreadable") end
  if hp&gt;=128 then hp=0 end
  CACC.hud=CACC.hud or {}
  local key=ctx.base..":"..player
  local pending=CACC.hud[key] or {ctx=ctx,player=player}
  pending.health=(maximum%16)*16+hp%16
  CACC.hud[key]=pending
end
function CACC_hc_health(ctx,player)
  local key="hc_health"..player
  if not CACC.on[key] then CACC_release(key,ctx.group);return end
  if not CACC_md_native_continues() then error("Infinite Health requires Japanese Hard Corps") end
  local shift=(player-1)*0x20
  local hp=CACC_rb(ctx.base+0xFA0C+shift)
  local maximum=CACC_rb(ctx.base+0xFA13+shift)
  local lives=CACC_rb(ctx.base+0xFA0D+shift)
  if hp==nil or maximum==nil or lives==nil then error("Health fields unreadable") end
  if lives==0 or hp==0 or hp&gt;=128 then return end
  if maximum&lt;1 or maximum&gt;15 then error("Native maximum health is invalid") end
  CACC_hold(ctx,key,{{0xFA0C+shift,maximum}},function(c) CACC_hc_health_refresh(c,player) end)
  if hp~=maximum then CACC_hc_health_refresh(ctx,player) end
end

function CACC_hc_tick()
  if not CACC.on.game_hc then return end
  for _,b in ipairs(CACC.hc_buffers or {}) do
    local ctx=CACC_context("game_hc",b)
    if not CACC_context_valid(ctx) then CACC_disable_group(ctx.group);return end
    if CACC_md_native_continues() then CACC_release("hc_cont",ctx.group)
    else CACC_option(ctx,"hc_cont",{{0xFA44,9}}) end
    for _,item in ipairs({{"hc_lives",0xFA0D,10},{"hc_inv",0xD759,0x47},{"hc_lives2",0xFA2D,10},{"hc_inv2",0xD7D9,0x47}}) do CACC_option(ctx,item[1],{{item[2],item[3]}}) end
    for player=1,2 do
      local shift=(player-1)*0x20;local alive=(CACC_rb(b+0xFA0D+shift) or 0)~=0
      CACC_hc_health(ctx,player)
      local bombkey="hc_bombs"..player
      if CACC.on[bombkey] and alive then
        local oldCount=CACC_rb(b+0xFA10+shift);local oldOwned=CACC_rb(b+0xFA0E+shift)
        CACC_hold(ctx,bombkey,{{0xFA10+shift,9},{0xFA0E+shift,32,32}},function(c) CACC_hc_refresh(c,player,false,true) end)
        if oldCount~=9 or CACC_mask(oldOwned,32)==0 then CACC_hc_refresh(ctx,player,false,true) end
      elseif not CACC.on[bombkey] then CACC_release(bombkey,ctx.group) end
      local index=4+player;local key="weapon"..index;local choice=CACC_selread(index)
      if CACC_ge(choice,1) and CACC_le(choice,4) then
        if alive then
          local owned=CACC_rb(b+0xFA0E+shift);local current=CACC_rb(b+0xFA0F+shift)
          local bit=2^choice;local target=CACC_merge(owned,bit,bit)
          if choice==1 then target=CACC_merge(target,0,1) end
          local applied=CACC_hold(ctx,key,{{0xFA0E+shift,target,31},{0xFA0F+shift,choice}},function(c) CACC_hc_refresh(c,player,true,false) end)
          if applied and (target~=owned or current~=choice) then CACC_hc_refresh(ctx,player,true,false) end
        end
      else CACC_release(key,ctx.group) end
    end
  end
end
function CACC_gb_tick()
  if not CACC.on.game_gb then return end
  local variant=CACC_gb_variant(CACC.gb_core)
  if not variant then CACC_disable_group("game_gb");return end
  local b=CACC_gb_ram(CACC.gb_core);local ctx=CACC_context("game_gb",b,CACC.gb_core)
  if not b or not CACC_context_valid(ctx) then CACC_disable_group(ctx.group);return end
  CACC_option(ctx,"gb_lives",{{0x888,9}})
  local choice=CACC_selread(11)
  if CACC_ge(choice,2) and CACC_le(choice,5) then
    local settings={{5,14,4,22},{6,16,11,26},{10,16,8,32},{6,16,8,56},{2,5,8,22}}
    local values=settings[choice]
    local token="weapon11:"..b
    if not CACC.saved[token] or CACC_rb(b+0x882)~=choice then
      CACC_hold(ctx,"weapon11",{{0x881,values[1]},{0x882,choice},{0x883,values[2]},{0x884,values[3]},{0x885,values[4]}})
    end
  else CACC_release("weapon11",ctx.group) end
  if CACC.on.gb_inv then
    local token="gb_inv_code:"..b
    local saved=CACC.saved[token]
    local bank=CACC_ri(CACC_ri(CACC.gb_core+0x160)+0x48)
    if not saved and not CACC_ok(bank) then error("Operation C code bank is unavailable") end
    local p=saved and saved.cells[1].address or bank+variant.inv
    if not saved then
      local bytes=CACC_rbn(p-5,8)
      if not bytes or string.char(table.unpack(bytes))~=string.char(0xCB,0xBE,0xCB,0x8E,0xAF,0xEA,0x29,0xC0) then error("Operation C protection routine did not match") end
    end
    CACC_hold(ctx,"gb_inv_code",{{p-b,0},{p-b+1,0},{p-b+2,0}},nil,true)
    CACC_hold(ctx,"gb_inv",{{0x29,1},{0x09,math.floor(CACC_rb(b+0x1EE2)/2)%2,1}})
  else CACC_release("gb_inv_code",ctx.group);CACC_release("gb_inv",ctx.group) end
  if CACC.on.gb_cont then
    local p=CACC.gb_core+0x174+variant.cont
    if not CACC.saved["gb_cont:"..b] then
      local bytes=CACC_rbn(p-3,7)
      if not bytes or string.char(table.unpack(bytes))~=string.char(0x21,0xDD,0xDE,0x35,0xC2,0x9B,0x08) then error("Operation C continue routine did not match") end
    end
    CACC_hold(ctx,"gb_cont",{{p-b,0},{p-b+1,0xC3}},nil,true)
  else CACC_release("gb_cont",ctx.group) end
end
CACC.sarcVariants={{name="US",rom=0x468360,fixed=0x470360},{name="Japan",rom=0x490360,fixed=0x498360}}
function CACC_sarc_variant(core)
  if not CACC_ok(core) or not CACC.mod then return nil end
  for _,v in ipairs(CACC.sarcVariants) do
    if CACC_ri(core+0x1970)==CACC.mod+v.rom then return v end
  end
end
function CACC_sarc_ram(core)
  if not CACC_ok(core) or not CACC.mod then return nil end
  local variant=CACC_sarc_variant(core);if not variant then return nil end
  local bus=CACC_ri(core+0x13C)
  if not CACC_ok(bus) or CACC_ri(bus)~=core then return nil end
  if CACC_ri(bus+0x38)~=CACC.mod+variant.fixed or CACC_ri(bus+0x60)~=0x1800 then return nil end
  local ram=CACC_ri(bus+0x58)
  if not CACC_ok(ram) or CACC_rb(ram+0x17FF)==nil then return nil end
  return ram
end
function CACC_sarc_enable()
  CACC_disable_group("game_sarc")
  if not CACC_attach() then error("Launch the supported game first") end
  local found={};local seen={}
  for _,variant in ipairs(CACC.sarcVariants) do
    local rom=CACC.mod+variant.rom
    local pattern=string.format("%02X %02X %02X %02X",rom%256,math.floor(rom/256)%256,math.floor(rom/65536)%256,math.floor(rom/16777216)%256)
    local hits=AOBScan(pattern,"+W")
    if hits then
      for i=0,hits.Count-1 do
        local core=tonumber(hits[i],16)-0x1970
        if not seen[core] and CACC_sarc_ram(core) then found[#found+1]=core;seen[core]=true end
      end
      hits.destroy()
    end
  end
  if #found~=1 then error("Start Super Contra (Arcade), US or Japanese version, then enable this group") end
  CACC.sarc_core=found[1];CACC.on.game_sarc=true;CACC.timer.Enabled=true
end
-- Native Mega Shell icon descriptors are identical in US and Japanese ROMs.
-- Caller runs under the table's transaction; no separate external writer.
function CACC_sarc_shell_icon(ctx,player,validateOnly)
  if not CACC_context_valid(ctx) then return end
  local base=ctx.base;local shift=(player-1)*0x100
  if (CACC_rb(base+0xC2) or 0)==0 then return end
  if CACC_rb(base+0xAA6+shift)~=1 then return end
  local hi=CACC_rb(base+0xABE+shift);local lo=CACC_rb(base+0xABF+shift)
  if hi==nil or lo==nil then error("Mega Shell icon unreadable") end
  local address=hi*256+lo
  if address&lt;0x3C00 or address&gt;0x3FF8 or address%8~=0 then error("Mega Shell sprite pointer invalid") end
  local video=CACC_ri(ctx.core+0x138)
  if not CACC_ok(video) then error("Mega Shell graphics unavailable") end
  local sprites=CACC_ri(video+4)
  if not CACC_ok(sprites) or CACC_rb(sprites+0x3FF)==nil then error("Mega Shell sprite memory unavailable") end
  local stock=CACC_rb(base+0xAB+(player-1)*0x10)
  if stock==nil then error("Mega Shell stock unreadable") end
  local tile=stock&gt;0 and 0x68 or 0x6C
  local sprite=sprites+address-0x3C00
  -- Native initialization sets these descriptor bytes; reject other objects.
  if CACC_rb(sprite+1)~=0x7A or CACC_mask(CACC_rb(sprite+3) or 0,15)~=0x0E then error("Mega Shell icon identity changed") end
  if validateOnly then return true end
  CACC_wpm(sprite+2,tile)
  CACC_wpm(base+0xAA7+shift,{0x7F,stock&gt;0 and 0x1E or 0x13,1})
end
function CACC_sarc_shell(ctx,player)
  local key="sarc_shell"..player
  if not CACC.on[key] then CACC_release(key,ctx.group);return end
  local alive=(CACC_rb(ctx.base+0xA0+(player-1)*0x10) or 0)&gt;0
  if not alive or (CACC_rb(ctx.base+0xC2) or 0)==0 then return end
  if CACC_rb(ctx.base+0xAA6+(player-1)*0x100)~=1 then return end
  if not CACC_sarc_shell_icon(ctx,player,true) then return end
  CACC_hold(ctx,key,{{0xAB+(player-1)*0x10,9}},function(savedCtx) CACC_sarc_shell_icon(savedCtx,player) end)
  CACC_sarc_shell_icon(ctx,player)
end

CACC.sarcContinueGates={
 {offset=0x8EC8,before={0x6A,0,0x67},after={0xAE,0xAE,0xAE}},
 {offset=0x9051,before={0x72,6},after={0x60,6}},
 {offset=0x942E,before={0x72,11},after={0x60,11}}
}
function CACC_sarc_continues(ctx)
  if not CACC.on.sarc_credits then CACC_release("sarc_credits",ctx.group);return end
  local variant=CACC_sarc_variant(ctx.core)
  if not variant then error("Super Contra continue ROM identity changed") end
  local rom=CACC.mod+variant.rom
  -- Validate all sites before changing any of them.
  for i,gate in ipairs(CACC.sarcContinueGates) do
    local saved=CACC.saved["sarc_creditgate"..i..":"..ctx.base]
    for j,v in ipairs(gate.before) do
      local now=CACC_rb(rom+gate.offset+j-1)
      if now~=v and (not saved or now~=gate.after[j]) then error("Super Contra continue code did not match") end
    end
    local prefix=CACC_rbn(rom+gate.offset-6,6)
    if not prefix or string.char(table.unpack(prefix))~=string.char(0x12,0xC4,0xCF,0x36,0xC4,0x91) then error("Super Contra continue comparison did not match") end
  end
  for i,gate in ipairs(CACC.sarcContinueGates) do
    local spec={}
    for j,v in ipairs(gate.after) do spec[j]={rom+gate.offset+j-1-ctx.base,v} end
    CACC_hold(ctx,"sarc_creditgate"..i,spec,nil,true)
  end
  CACC_hold(ctx,"sarc_credits",{{0x86,9}})
end

function CACC_sarc_tick()
  if not CACC.on.game_sarc then return end
  local ram=CACC_sarc_ram(CACC.sarc_core);local ctx=CACC_context("game_sarc",ram,CACC.sarc_core)
  if not ram then CACC_disable_group(ctx.group);return end
  CACC_sarc_continues(ctx)
  for player=1,2 do
    local shift=(player-1)*0x100;local small=(player-1)*0x10
    local alive=(CACC_rb(ram+0xA0+small) or 0)&gt;0
    local lifeKey="sarc_lives"..player;local invKey="sarc_inv"..player
    if CACC.on[lifeKey] and alive then CACC_hold(ctx,lifeKey,{{0xA0+small,9}})
    elseif not CACC.on[lifeKey] then CACC_release(lifeKey,ctx.group) end
    if CACC.on[invKey] and alive then
      local state=CACC_rb(ram+0xA00+shift)
      CACC_hold(ctx,invKey,{{0xA00+shift,CACC_mask(state,128),192},{0xA1B+shift,160},{0xA2D+shift,CACC_rb(ram+0xA2D+shift)}})
    elseif not CACC.on[invKey] then CACC_release(invKey,ctx.group) end
    CACC_sarc_shell(ctx,player)
    local index=13+player;local choice=CACC_selread(index);local key="weapon"..index
    if choice==1 or choice==2 or choice==3 or choice==4 or choice==129 or choice==130 or choice==131 or choice==132 then
      if alive then CACC_hold(ctx,key,{{0xA30+shift,choice},{0xA9+small,choice}}) end
    else CACC_release(key,ctx.group) end
  end
end

CACC.arcVariants={{name="US",rom=0x27FD60,fixed=0x2D80D0},{name="Japan",rom=0x2C00D0,fixed=0x2E00D0}}
function CACC_arc_variant(core)
  if not CACC_ok(core) or not CACC.mod then return nil end
  for _,v in ipairs(CACC.arcVariants) do
    if CACC_ri(core+0x154)==CACC.mod+v.rom then return v end
  end
end
function CACC_arc_ram(core)
  if not CACC_ok(core) or not CACC.mod then return nil end
  local variant=CACC_arc_variant(core);if not variant then return nil end
  local bus=CACC_ri(core+0x13C)
  if not CACC_ok(bus) or CACC_ri(bus)~=core then return nil end
  if CACC_ri(bus+0x38)~=CACC.mod+variant.fixed or CACC_ri(bus+0x60)~=0x1000 then return nil end
  local ram=CACC_ri(bus+0x58)
  if not CACC_ok(ram) or CACC_rb(ram+0xFFF)==nil then return nil end
  return ram
end
function CACC_arc_enable()
  CACC_disable_group("game_arc")
  if not CACC_attach() then error("Launch the supported game first") end
  local found={};local seen={}
  for _,variant in ipairs(CACC.arcVariants) do
    local rom=CACC.mod+variant.rom
    local signature=CACC_rbn(CACC.mod+variant.fixed,12)
    if signature and string.char(table.unpack(signature))==string.char(0x4F,0xB7,0,7,0x97,0xF0,0xB7,0x70,0,0x7E,0x61,0x11) then
      local pattern=string.format("%02X %02X %02X %02X",rom%256,math.floor(rom/256)%256,math.floor(rom/65536)%256,math.floor(rom/16777216)%256)
      local hits=AOBScan(pattern,"+W")
      if hits then
        for i=0,hits.Count-1 do
          local core=tonumber(hits[i],16)-0x154
          if not seen[core] and CACC_arc_ram(core) then found[#found+1]=core;seen[core]=true end
        end
        hits.destroy()
      end
    end
  end
  if #found~=1 then error("Start Contra (Arcade), US or Japanese version, then enable this group") end
  CACC.arc_core=found[1];CACC.on.game_arc=true;CACC.timer.Enabled=true
end
function CACC_arc_tick()
  if not CACC.on.game_arc then return end
  local ram=CACC_arc_ram(CACC.arc_core);local ctx=CACC_context("game_arc",ram,CACC.arc_core)
  if not ram then CACC_disable_group(ctx.group);return end
  CACC_option(ctx,"arc_credits",{{0x1A,9}})
  for player=1,2 do
    local shift=(player-1)*0xC0;local lives=player==1 and 0x81 or 0x8D
    local alive=(CACC_rb(ram+lives) or 0)~=0
    local lifeKey="arc_lives"..player;local invKey="arc_inv"..player
    if CACC.on[lifeKey] and alive then CACC_hold(ctx,lifeKey,{{lives,9}})
    elseif not CACC.on[lifeKey] then CACC_release(lifeKey,ctx.group) end
    if CACC.on[invKey] and alive then
      -- Palette fields are captured but remain controlled by the game's blink routine.
      local spec={{0x245+shift,1},{0x246+shift,240},{0x200+shift,0,64}}
      for _,off in ipairs({0x209,0x221,0x239,0x251}) do spec[#spec+1]={off+shift,CACC_rb(ram+off+shift),240} end
      CACC_hold(ctx,invKey,spec)
    elseif not CACC.on[invKey] then CACC_release(invKey,ctx.group) end
    local index=11+player;local choice=CACC_selread(index);local key="weapon"..index
    if choice==2 or choice==4 or choice==10 or choice==20 then
      if alive then CACC_hold(ctx,key,{{0x215+shift,choice,31}}) end
    else CACC_release(key,ctx.group) end
  end
end

function CACC_body()
  local session=nil
  for group in pairs(CACC.groups) do
    if CACC.on[group] then
      session=session or CACC_session_read()
      if not CACC_session_matches(CACC.sessions[group],session,false) then CACC_disable_group(group) end
    end
  end
  CACC_bindsel()
  for _,group in ipairs({"game_contra","game_superc"}) do if CACC.on[group] then
    local b=CACC_chain("nes");local ctx=CACC_context(group,b)
    if b and CACC_context_valid(ctx) then CACC_nes_tick(group,b) else CACC_disable_group(group) end
  end end
  if CACC.on.game_c3 then
    local ctx=CACC_context("game_c3",CACC.c3_core and CACC.c3_core+0x1A0,CACC.c3_core)
    if CACC_context_valid(ctx) then CACC_snes_contra3(ctx.base) else CACC_disable_group(ctx.group) end
  end
  CACC_hc_tick();CACC_gb_tick();CACC_arc_tick();CACC_sarc_tick();CACC_hc_flush_hud()
end
function CACC_tick()
  local ok,err=pcall(CACC_body)
  if not ok then
    local restored,cleanup=pcall(CACC_disable_all)
    CACC.last_error=tostring(err)..(restored and "" or "\nRestoration: "..tostring(cleanup))
    CACC.timer.Enabled=false
    showMessage("Cheats stopped: "..CACC.last_error)
  end
end
function CACC_shutdown()
  if CACC.timer then CACC.timer.Enabled=false end
  CACC_disable_all()
  local finished=false
  for attempt=1,21 do
    if CACC_hc_finish_hud() then finished=true;break end
    if isPaused() or attempt==21 then break end
    sleep(5) -- only while the target was already running; never undo a user pause
  end
  if not finished then
    if CACC.timer then CACC.timer.Enabled=true end
    error("Cheats are off, but Hard Corps display cleanup is still pending. Resume the process and retry closing or loading the table.")
  end
  if CACC.sel and CACC.pid==getOpenedProcessID() then deAlloc(CACC.sel) end
  CACC.sel=nil
  if CACC.timer then CACC.timer.destroy();CACC.timer=nil end
  if CACC.closeToken then getMainForm().unregisterCloseCallback(CACC.closeToken);CACC.closeToken=nil end
  local form=getMainForm()
  if form.OnCloseQuery==CACC.closeQuery then form.OnCloseQuery=CACC.previousCloseQuery end
  if onTableLoad==CACC.tableHook then onTableLoad=CACC.previousTableHook end
end
-- Keep the target stable while capturing, applying and checking mutable state.
function CACC_transaction(fn,...)
  if CACC.transactionDepth then return fn(...) end
  if not CACC.pid or getOpenedProcessID()~=CACC.pid then return fn(...) end
  local wasPaused=isPaused()
  if not wasPaused then
    local ok,err=pcall(pause)
    if not ok or not isPaused() then
      if isPaused() then pcall(unpause) end
      error("Could not pause target for a verified update: "..tostring(err))
    end
  end
  CACC.transactionDepth=true
  CACC.sessionRead=nil
  local result=table.pack(pcall(fn,...))
  CACC.transactionDepth=nil
  CACC.sessionRead=nil
  if not wasPaused then
    local ok,err=pcall(unpause)
    if not ok or isPaused() then error("Could not resume target after update: "..tostring(err)) end
  end
  if not result[1] then error(result[2]) end
  return table.unpack(result,2,result.n)
end
for _,name in ipairs({"CACC_hold","CACC_release","CACC_hc_hud","CACC_hc_refresh","CACC_hc_flush_hud","CACC_hc_finish_hud"}) do
  local operation=_G[name]
  _G[name]=function(...)
    if name=="CACC_release" and next(CACC.saved)==nil then return end
    if name=="CACC_hc_flush_hud" and next(CACC.hud or {})==nil then return end
    if name=="CACC_hc_finish_hud" and next(CACC.hud or {})==nil then return true end
    return CACC_transaction(operation,...)
  end
end
local activeBody=CACC_body
function CACC_body()
  if not CACC_attach() then return end
  local active=next(CACC.saved)~=nil
  for key,enabled in pairs(CACC.on) do if enabled and not CACC.groups[key] then active=true;break end end
  -- A newly selected weapon has no ledger yet. Inspect pending selectors
  -- before entering the body so paired buffers update in one transaction.
  if not active and CACC.sel then
    local choices=nil
    for group,config in pairs(CACC.groups) do
      if CACC.on[group] then
        choices=choices or CACC_rbn(CACC.sel,#CACC.selectorIds)
        if not choices then error("Weapon selectors became unreadable") end
        for _,index in ipairs(config.selectors) do
          if choices[index]~=0 then active=true;break end
        end
        if active then break end
      end
    end
  end
  if active then return CACC_transaction(activeBody) end
  return activeBody()
end
-- A resolver may take time; reject a collection-session change during it.
for name,group in pairs({CACC_c3_enable="game_c3",CACC_hc_enable="game_hc",
  CACC_gb_enable="game_gb",CACC_arc_enable="game_arc",CACC_sarc_enable="game_sarc"}) do
  local enable=_G[name]
  _G[name]=function(...)
    if not CACC_attach() then error("Launch the supported game first.") end
    local session=CACC_session_require(group)
    enable(...)
    if not CACC_session_matches(session,CACC_session_read(),false) then
      CACC_disable_group(group)
      error("The selected game changed while enabling cheats. Enable MAIN again in gameplay.")
    end
    CACC.sessions[group]=session
  end
end
CACC.previousTableHook=onTableLoad
local previousHook=onTableLoad
CACC.tableHook=function(before)
  if before then CACC_shutdown() end
  if previousHook then previousHook(before) end
end
onTableLoad=CACC.tableHook
CACC.previousCloseQuery=getMainForm().OnCloseQuery
local previousCloseQuery=CACC.previousCloseQuery
CACC.closeQuery=function(sender)
  if previousCloseQuery and previousCloseQuery(sender)==false then return false end
  local ok,err=pcall(CACC_shutdown)
  if not ok then showMessage(tostring(err));return false end
  return true
end
getMainForm().OnCloseQuery=CACC.closeQuery
CACC.closeToken=getMainForm().registerCloseCallback(function() CACC_shutdown() end)
CACC.timer=createTimer(nil,false)
CACC.timer.Interval=25;CACC.timer.OnTimer=CACC_tick;CACC.timer.Enabled=true
</LuaScript>
</CheatTable>