#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA081_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
overlay "show"
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
segment "show"
#endif

#ifdef MEDIA153_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A31E_A31M_A33M_A35E_A35M
void F0137_CPSEF_PatchFunctionWithHiddenCodeLauncher(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0227_pfv_FunctionToPatch, P0228_pfpuc_GetGraphicAddressFunction, P0229_i_GraphicIndex, P0230_pc_SectorBuffer, P0231_pi_HiddenCodeParameters, P0232_pps_CodePatches, P0233_i_CodePatchCount)
#endif
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
void F0137_CPSEF_PatchFunctionWithHiddenCodeLauncher(
P0227_pfv_FunctionToPatch, P0228_pfpuc_GetGraphicAddressFunction, P0229_i_GraphicIndex, P6042_i_, P0230_pc_SectorBuffer, P0231_pi_HiddenCodeParameters, P0232_pps_CodePatches, P0233_i_CodePatchCount)
#endif
REGISTER void  (*P0227_pfv_FunctionToPatch)()             SEPARATOR
unsigned char* (*P0228_pfpuc_GetGraphicAddressFunction)() SEPARATOR
int16_t          P0229_i_GraphicIndex                     SEPARATOR
#ifdef MEDIA351_G14ED_G20E_G21E
int16_t          P6042_i_                                 SEPARATOR
#endif
char*            P0230_pc_SectorBuffer                    SEPARATOR
int16_t*         P0231_pi_HiddenCodeParameters            SEPARATOR
CODE_PATCH**     P0232_pps_CodePatches                    SEPARATOR
int16_t          P0233_i_CodePatchCount                   FINAL_SEPARATOR
{
        REGISTER CODE_PATCH* L0233_ps_CodePatch;


        do {
                L0233_ps_CodePatch = P0232_pps_CodePatches[M002_RANDOM(P0233_i_CodePatchCount)];
                if (L0233_ps_CodePatch->Used); /* BUG0_00 Useless code */
        } while (L0233_ps_CodePatch->Used);
        L0233_ps_CodePatch->Used = C1_TRUE;
#ifdef MEDIA137_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        F0007_MAIN_CopyBytes(R0138_CPSEF_HiddenCodeLauncher, L0233_ps_CodePatch, 42);
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
        F0007_MAIN_CopyBytes(R0138_CPSEF_HiddenCodeLauncher, L0233_ps_CodePatch, 261);
#endif
#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
        F0007_MAIN_CopyBytes(F0211_CPSDF_GetSubroutineAbsoluteAddress(R0138_CPSEF_HiddenCodeLauncher), L0233_ps_CodePatch, 42);
#endif
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
        asm {
                T0137002:
                        cmpi.w  #0x4EF9,(P0227_pfv_FunctionToPatch)
                        bne.s   T0137003                                                /* If the first instruction of the function to patch is not a jmp ($4EF9). A jmp instruction would mean we are in the jumptable and not directly on the function */
                        movea.l 2(P0227_pfv_FunctionToPatch),P0227_pfv_FunctionToPatch    /* Use the address following the jmp instruction (this is the real address of the function) */
                        bra.s   T0137002
                T0137003:
        }
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        #asm
                T0137002:
                        cmpi.w  #$4EF9,(A2)     ; P0227_pfv_FunctionToPatch
                        bne.s   T0137003
                        movea.l 2(A2),A2        ; P0227_pfv_FunctionToPatch
                        bra.s   T0137002
                T0137003:
        #endasm
#endif
#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
        asm {
                @T0137002:
                        cmpi.w  #0x4EF9,(A4)    /* P0227_pfv_FunctionToPatch */
                        bne.s   @T0137003
                        movea.l 2(A4),A4        /* P0227_pfv_FunctionToPatch */
                        bra.s   @T0137002
                @T0137003:
        }
#endif
        F0007_MAIN_CopyBytes(P0227_pfv_FunctionToPatch, L0233_ps_CodePatch->FirstWords, sizeof(L0233_ps_CodePatch->FirstWords));
#ifdef MEDIA351_G14ED_G20E_G21E
        L0233_ps_CodePatch->DirectPage = G3958_;
#endif
        L0233_ps_CodePatch->PatchedFunction = P0227_pfv_FunctionToPatch;
        L0233_ps_CodePatch->GetGraphicAddressFunction = P0228_pfpuc_GetGraphicAddressFunction;
        L0233_ps_CodePatch->GraphicIndex = P0229_i_GraphicIndex;
#ifdef MEDIA351_G14ED_G20E_G21E
        L0233_ps_CodePatch->DirectPage2 = P6042_i_;
#endif
        L0233_ps_CodePatch->SectorBuffer = P0230_pc_SectorBuffer;
        L0233_ps_CodePatch->HiddenCodeParameters = P0231_pi_HiddenCodeParameters;
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
        asm {
                move.w  #0x4EF9,(P0227_pfv_FunctionToPatch)+             /* Write bytes for jmp instruction */
                move.l  L0233_ps_CodePatch,(P0227_pfv_FunctionToPatch)   /* Replace the following long with the buffer address containing the routine */
        }
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
        asm { /* Write 4 bytes for jmp instruction */
                SEP        #0x30
                LDA        #0x5c                           /* jmp instruction operand */
                STA        [P0227_pfv_FunctionToPatch]
                REP        #0x30
                LDY        #0x01                           /* Offset of first byte of target address */
                LDA        L0233_ps_CodePatch
                STA        [P0227_pfv_FunctionToPatch],Y   /* First and second bytes of target address */
                INY
                INY                                     /* Y now contains offset of third byte of target address */
                SEP        #0x30
                LDA        L0233_ps_CodePatch+2
                STA        [P0227_pfv_FunctionToPatch],Y   /* Third byte of target address */
                REP        #0x30
        }
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        #asm
                move.w  #$4EF9,(A2)+    ; P0227_pfv_FunctionToPatch
                move.l  A3,(A2)         ; L0233_ps_CodePatch, P0227_pfv_FunctionToPatch
        #endasm
#endif
#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
        asm {
                move.w  #0x4EF9,(A4)+   /* P0227_pfv_FunctionToPatch */
                move.l  A3,(A4)         /* L0233_ps_CodePatch, P0227_pfv_FunctionToPatch */
        }
#endif
}
#endif
#endif
