#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA774_SU1E_AU1E_AU2E
#ifdef MEDIA772_SU1E
#ifndef NOCOPYPROTECTION
void F1899_Pre_F1901_CopyProtectionRoutine_CPSX(
)
{
#include "FAKE2.C"
}

int16_t F1901_CopyProtectionRoutine_CPSX(
)
{
        register int16_t L5597_;
        char* L5598_;
        long L5747_l_Unreferenced;
        char* L5599_;


        L5598_ = (char*)F1886_Memory(14, C14_ALLOCATE, 576L);
        L5599_ = &G3601_l_VerticalBlankCount;
        L5597_ = 0;
        do {
                asm {
                        @Tloc_60EA:
                                movem.l d4/a2,-(sp)
                                move.w  #1,-(sp)
                                clr.l   -(sp)
                                move.w  #6,-(sp)
                                clr.w   -(sp)
                                clr.l   -(sp)
                                move.l  L5598_(a6),-(sp)
                                move.w  #8,-(sp)
                                trap    #0xE            /* Call XBIOS */
                                lea     0x14(sp),sp
                                clr.l   -(sp)
                                move.w  #0x20,-(sp)
                                trap    #1
                                move.l  d0,2(sp)
                                tst.w   (0x43E).w
                                bne.w   @Tloc_6256
                                move.w  #1,(0x43E).w
                                bsr.w   @Tloc_6184
                                bsr.w   @Tloc_6228
                                bsr.w   @Tloc_6140
                                bsr.w   @Tloc_6184
                                bsr.w   @Tloc_6228
                                bsr.w   @Tloc_6140
                                bra.w   @Tloc_6256
                        @Tloc_6140:
                                movea.l L5598_(a6),a0
                                lea     0x14(a0),a0
                                lea     0x1EC(a0),a1
                                move.w  #0x1E9,d0
                                moveq   #0x10,d4
                                moveq   #0,d7
                        @Tloc_6154:
                                moveq   #0,d1
                                moveq   #0,d2
                        @Tloc_6158:
                                move.b  (a0)+,d3
                                bpl.s   @Tloc_6162
                                bset    d1,d2
                                andi.b  #0x7F,d3
                        @Tloc_6162:
                                cmpi.b  #0x68,d3
                                beq.s   @Tloc_616C
                                moveq   #0,d7
                                rts
                        @Tloc_616C:
                                addq.w  #1,d1
                                subq.w  #1,d0
                                beq.s   @Tloc_6176
                                cmp.w   d4,d1
                                bne.s   @Tloc_6158
                        @Tloc_6176:
                                cmp.w   (a1),d2
                                beq.s   @Tloc_617C
                                addq.w  #1,d7
                        @Tloc_617C:
                                move.w  d2,(a1)+
                                tst.w   d0
                                bne.s   @Tloc_6154
                                rts
                        @Tloc_6184:
                                movea.l #0xFFFF8800,a1
                                move.w  #0xF8,d2
                                move    sr,d0
                                ori     #0x700,sr
                                move.b  #0xE,(a1)
                                and.b   (a1),d2
                                ori.b   #5,d2
                                move.b  d2,2(a1)
                                move    d0,sr
                                movea.l L5599_(a6),a2
                                move.l  (a2),d3
                                addi.l  #0x103,d3
                                movea.w #0x8606,a1
                                move.w  #0x80,(a1)
                                moveq   #7,d1
                                bsr.s   @Tloc_621A
                        @Tloc_61BC:
                                move.w  #0x80,(a1)
                                bsr.s   @Tloc_6220
                                moveq   #2,d1
                                and.w   -2(a1),d1
                                bsr.s   @Tloc_6220
                                tst.w   d1
                                beq.s   @Tloc_61BC
                                move.w  #0x80,(a1)
                                moveq   #0,d1
                                bsr.s   @Tloc_621A
                                move.w  #0x80,(a1)
                        @Tloc_61DA:
                                moveq   #0x2C,d0
                        @Tloc_61DC:
                                subq.w  #1,d0
                                bne.s   @Tloc_61DC
                                moveq   #1,d0
                                and.w   -2(a1),d0
                                bne.s   @Tloc_61DA
                                move.w  #0x84,(a1)
                                moveq   #8,d1
                                bsr.s   @Tloc_621A
                                move.b  L5598_+3(a6),(0xFFFF860D).w
                                move.b  L5598_+2(a6),(0xFFFF860B).w
                                move.b  L5598_+1(a6),(0xFFFF8609).w
                                move.w  #0x90,(a1)
                                move.w  #0x190,(a1)
                                move.w  #0x90,(a1)
                                move.w  #1,-2(a1)
                                move.w  #0x80,d1
                                move.w  d1,(a1)
                        @Tloc_621A:
                                bsr.s   @Tloc_6220
                                move.w  d1,-2(a1)
                        @Tloc_6220:
                                moveq   #0x2E,d2
                        @Tloc_6222:
                                subq.w  #1,d2
                                bne.s   @Tloc_6222
                                rts
                        @Tloc_6228:
                                cmp.l   (a2),d3
                                bmi.s   @Tloc_6254
                                move.l  (a2),d2
                                btst    #5,(0xFFFFFA01).w
                                bne.s   @Tloc_6228
                                move.w  #0x90,(a1)
                                moveq   #1,d0
                                and.w   (a1),d0
                                beq.s   @Tloc_6254
                                move.w  #0x80,(a1)
                                moveq   #0x2C,d0
                        @Tloc_6246:
                                subq.w  #1,d0
                                bne.s   @Tloc_6246
                                moveq   #0x10,d0
                                and.w   -2(a1),d0
                                bne.s   @Tloc_6254
                                rts
                        @Tloc_6254:
                                addq.l  #4,sp
                        @Tloc_6256:
                                clr.w   (0x43E).w
                                trap    #1
                                addq.l  #6,sp
                                movem.l (sp)+,d4/a2
                }
        } while (!L5597_);
        F1886_Memory(14, C20_FREE, L5598_);
        return 1;
}

void F1902_Post_F1901_CopyProtectionRoutine_CPSX(
)
{
#include "FAKE2.C"
}
#endif
#endif

void F1904_ApplyPaletteChangesToScreenArea(int16_t* P5728_pi_XYZ, unsigned char* P5729_puc_PaletteChanges) {
        register int16_t L5601_i_Width;
        register int16_t L5602_i_Height;
        unsigned char* L5603_puc_Bitmap;


        L5601_i_Width = (unsigned int16_t)((P5728_pi_XYZ[1] + 15) * 16) >> 3;
        L5602_i_Height = P5728_pi_XYZ[3] - P5728_pi_XYZ[2];
#ifdef MEDIA772_SU1E
        L5603_puc_Bitmap = (unsigned char*)F1886_Memory(14, C14_ALLOCATE, (long)(L5601_i_Width * L5602_i_Height));
#endif
#ifdef MEDIA763_AU1E_AU2E
        L5603_puc_Bitmap = (unsigned char*)F1886_Memory(6, C14_ALLOCATE, (long)(L5601_i_Width * L5602_i_Height));
#endif
        F1947_BlitBitmapToOrFromScreen(P5728_pi_XYZ, L5603_puc_Bitmap, C0_SCREEN_TO_BITMAP);
#ifdef MEDIA772_SU1E
        F1372_SetVerticalBlankInterrupt(3);
#endif
        F0129_VIDEO_BlitShrinkWithPaletteChanges(L5603_puc_Bitmap, L5603_puc_Bitmap, P5728_pi_XYZ[1] - P5728_pi_XYZ[0], L5602_i_Height, P5728_pi_XYZ[1] - P5728_pi_XYZ[0], L5602_i_Height, P5729_puc_PaletteChanges);
        F1947_BlitBitmapToOrFromScreen(P5728_pi_XYZ, L5603_puc_Bitmap, C1_BITMAP_TO_SCREEN);
#ifdef MEDIA772_SU1E
        F1886_Memory(14, C20_FREE, L5603_puc_Bitmap);
#endif
#ifdef MEDIA763_AU1E_AU2E
        F1886_Memory(6, C20_FREE, L5603_puc_Bitmap);
#endif
}

void F1905_(
)
{
        F1771_(1, 0x01060001, 0x010600FF, F1933_);
}

void F1906_(
)
{
        F1771_(1, 0x01060001, 0x010600FF, F1933_);
}
#endif
