#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA674_F31E_F31J_X31J_P31J
#ifdef MEDIA692_X31J
extern char* G3095_pc_CopyProtectionIdentifier_CPSX;
extern unsigned char F2087_();
extern int16_t G3885_[3];
extern void F2094_Call_DOS_FFLUSH();
#endif

int16_t F7308_Return0(int16_t P4098_i_)
{
#ifdef MEDIA692_X31J
        int16_t L5913_;


        L5913_ = 0;
        return L5913_;
#endif
#ifdef MEDIA671_F31E_F31J_P31J
        return 0;
#endif
}

int16_t F7309_(FILE_DESCRIPTOR* P4099_ps_, FILE_DESCRIPTOR** P4100_pps_)
{
        REGISTER int16_t L4626_i_;
        REGISTER FILE_DESCRIPTOR* L4625_ps_;


#ifdef MEDIA692_X31J
        L4626_i_ = 0;
#endif
        L4625_ps_ = P4099_ps_;
        *P4100_pps_ = L4625_ps_;
#ifdef MEDIA692_X31J
        if (L4625_ps_->Signature != 0x52A3) {
#endif
#ifdef MEDIA671_F31E_F31J_P31J
        if (L4625_ps_->Signature != 0x5223) {
#endif
                L4626_i_ = -1;
#ifdef MEDIA671_F31E_F31J_P31J
        } else {
                L4626_i_ = 0;
#endif
        }
        return L4626_i_;
}

#ifdef MEDIA671_F31E_F31J_P31J
int16_t F7310_Unreferenced(long P4101_l_)
{
        unsigned int16_t L4628_i_;
        unsigned int16_t L4627_ui_;


        if (P4101_l_ >= 0) {
                L4628_i_ = 0;
        } else {
                L4627_ui_ = 0;
                for (;;) { /*_Infinite loop_*/
                        if (G2299_[L4627_ui_].l_0 == 1) {
                                L4628_i_ = G2299_[L4627_ui_].i_4;
                                break;
                        }
                        if (G2299_[L4627_ui_].l_0 == P4101_l_) {
                                L4628_i_ = G2299_[L4627_ui_].i_4;
                                break;
                        }
                        L4627_ui_++;
                };

        }
        return L4628_i_;
}

int16_t F7311_(unsigned char* P4102_puc_)
{
        unsigned int16_t L4629_ui_;
        int16_t L4630_i_;
        int16_t L4631_i_;
        unsigned char L4632_uc_;
        int16_t L4633_i_;
        int16_t L4634_i_;


        for (L4629_ui_ = 0; L4629_ui_ < 80; L4629_ui_++) {
                L4632_uc_ = P4102_puc_[L4629_ui_];
                if (L4632_uc_ == '\0') {
                        break;
                }
                if (((L4632_uc_ >= 'A') && (L4632_uc_ <= 'Z')) || ((L4632_uc_ >= 'a') && (L4632_uc_ <= 'z'))) {
                        P4102_puc_[L4629_ui_] = ((L4632_uc_ < 'a') || (L4632_uc_ > 'z')) ? L4632_uc_ : L4632_uc_ + 'A' - 'a';
                }
        }
        if ((L4629_ui_ < 40) || (L4629_ui_ >= 80)) {
                L4630_i_ = -1;
        } else {
                L4633_i_ = F7097_strstr("COPYRIGHT (C)", M772_CAST_PC(P4102_puc_));
                L4634_i_ = F7097_strstr("SOFTWARE HEAVEN, INC.", M772_CAST_PC(P4102_puc_));
                if ((L4633_i_ < 0) || (L4634_i_ < 0)) {
                        L4630_i_ = -2;
                } else {
                        if (L4633_i_ >= L4634_i_) {
                                L4630_i_ = -3;
                        } else {
                                for (L4629_ui_ = F0090_strlen("COPYRIGHT (C)") + L4633_i_; L4629_ui_ < L4634_i_; L4629_ui_++) {
                                        if (((P4102_puc_[L4629_ui_] >= 'A') && (P4102_puc_[L4629_ui_] <= 'Z')) || ((P4102_puc_[L4629_ui_] >= 'a') && (P4102_puc_[L4629_ui_] <= 'z'))) {
                                                L4630_i_ = -4;
                                                goto loc_1FC5A;
                                        }
                                }
                                L4629_ui_ = 0;
                                for (;;) { /*_Infinite loop_*/
                                        if (G2298_[L4629_ui_].i_0 == 0) {
                                                L4631_i_ = 2;
                                                break;
                                        } else {
                                                if (F7097_strstr(G2298_[L4629_ui_].pc_4, M772_CAST_PC(P4102_puc_)) >= 0) {
                                                        L4631_i_ = G2298_[L4629_ui_].i_0;
                                                        break;
                                                }
                                        }
                                        L4629_ui_++;
                                };
                                L4630_i_ = 0;
                        }
                }
        }
        loc_1FC5A:
        if (L4630_i_ < 0) {
                L4631_i_ = 1;
        }
        return L4631_i_;
}

int16_t F7312_(int16_t P4103_i_)
{
        return P4103_i_ + 5;
}
#endif

#ifdef MEDIA692_X31J
int16_t F7313_GetDriveIndex(FILE_DESCRIPTOR* P4104_ps_, int16_t* P5956_) {
#endif
#ifdef MEDIA671_F31E_F31J_P31J
int16_t F7313_GetDriveIndex(FILE_DESCRIPTOR* P4104_ps_) {
#endif
        REGISTER unsigned char L4635_uc_;
        REGISTER int16_t L4637_i_;
        FILE_DESCRIPTOR* L4636_ps_;


        if (F7309_(P4104_ps_, &L4636_ps_) < 0) {
#ifdef MEDIA692_X31J
                return 3;
#endif
        }
        L4635_uc_ = L4636_ps_->FileName[0];
        if (((L4635_uc_ >= 'A') && (L4635_uc_ <= 'Z')) || ((L4635_uc_ >= 'a') && (L4635_uc_ <= 'z'))) {
#ifdef MEDIA692_X31J
                if (L4635_uc_ >= 'a') {
                        L4637_i_ = L4635_uc_ - 'a';
                } else {
                        L4637_i_ = L4635_uc_ - 'A';
                }
#endif
#ifdef MEDIA671_F31E_F31J_P31J
                L4635_uc_ = ((L4635_uc_ < 'a') || (L4635_uc_ > 'z')) ? L4635_uc_ : L4635_uc_ + 'A' - 'a';
                L4637_i_ = L4635_uc_ - 'A';
                if ((L4637_i_ == 1) && (G4194_.ui_0_DataDriveOrdinal == 1)) {
                        F7112_FallbackToLastProgramState(C1_TRUE, 21, 0L);
                }
#endif
        } else {
                L4637_i_ = M001_ORDINAL_TO_INDEX(G4194_.ui_0_DataDriveOrdinal);
        }
#ifdef MEDIA692_X31J
        *P5956_ = L4637_i_;
        return 0;
#endif
#ifdef MEDIA671_F31E_F31J_P31J
        return L4637_i_;
#endif
}

#ifdef MEDIA692_X31J
int16_t F2084_CheckX68000OriginalDisk_CPSX(
int16_t P5957_i_DriveIndex)
{
#ifndef NOCOPYPROTECTION
        register char L5917_;
#endif
        UDSTRUCT1* L5918_ps_SectorBuffer;
        int16_t L5919_;
        int16_t L5920_i_DriveAddress;
        static char* G3095_pc_CopyProtectionIdentifier_CPSX = "HPR-0007";


        L5918_ps_SectorBuffer = G2281_->UDS8_pc_6(&G2281_);
        L5920_i_DriveAddress = F2091_GetDriveAddress(P5957_i_DriveIndex);
        asm {
#ifndef NOCOPYPROTECTION
                        bsr.s   @TReadSec9
                        move.b  d0,d7
                        bsr.s   @TReadSec9
                        cmp.b   d7,d0
                        beq.s   @Tloc_EE06
#else /* Crack, not tested. Probably breaks checksum check in Prim loader */
                        cmpi.b  #0x91,L5920_i_DriveAddress(a6)    /* If specified drive is the save disk drive */
                        beq.w   @Tloc_1FD8E
#endif
                        moveq   #1,d0           /* Game disk */
                        bra.w   @Tloc_EEA4
                @Tloc_EE06:
                        jsr     F1031_IsOperationSuccessful
                        beq.w   @Tloc_EEA2
                        moveq   #2,d0           /* Save disk */
                        bra.w   @Tloc_EEA4

#ifndef NOCOPYPROTECTION
                @TReadSec9:
                        moveq   #0x46,d0        /* B_READ */
                        move.w  L5920_i_DriveAddress(a6),d1
                        addi.w  #0x50,d1        /* 0x50: Mode (01010000 Bit 6 = 1: MFM, Bit 5 = 0: Do not retry, Bit 4 = 1: Seek and then execute) */
                        move.l  #0x01010109,d2  /* 4 bytes: Sector length 1 (0: 128, 1: 256, 2: 512, 3: 1024), Track 1 (0..79), Side 1 (0..1), Sector 9 (1..n) */
                        move.l  #256,d3         /* Number of bytes to read */
                        movea.l L5918_ps_SectorBuffer(a6),a1
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        btst    #0x1E,d0
                        beq.s   @Tloc_EE6E
                        btst    #0x15,d0
                        beq.s   @Tloc_EE6E
                        btst    #0xD,d0
                        beq.s   @Tloc_EE6E
                        moveq   #8,d0
                        movea.l L5918_ps_SectorBuffer(a6),a0
                        movea.l G3095_pc_CopyProtectionIdentifier_CPSX(a5),a1
                @Tloc_EE4C:
                        cmpm.b  (a0)+,(a1)+
                        bne.s   @Tloc_EE06
                        subq.b  #1,d0
                        bne.s   @Tloc_EE4C
                        move.l  a0,-(sp)
                        movea.l L5918_ps_SectorBuffer(a6),a0
                        move.b  8(a0),d0
                        add.b   9(a0),d0
                        add.b   0xA(a0),d0
                        add.b   0xB(a0),d0
                        movea.l (sp)+,a0
                        rts
#endif

                @Tloc_EE6E:
                        clr.w   G3091_i_ErrorCount(a5)
                        moveq   #0x46,d0        /* B_READ */
                        move.w  L5920_i_DriveAddress(a6),d1
                        addi.w  #0x50,d1        /* 0x50: Mode (01010000 Bit 6 = 1: MFM, Bit 5 = 0: Do not retry, Bit 4 = 1: Seek and then execute) */
                        move.l  #0x03010106,d2  /* 4 bytes: Sector length 3 (0: 128, 1: 256, 2: 512, 3: 1024), Track 1 (0..79), Side 1 (0..1), Sector 6 (1..n) */
                        move.l  #1024,d3        /* Number of bytes to read */
                        movea.l L5918_ps_SectorBuffer(a6),a1
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        btst    #0x1E,d0
                        bne.s   @Tloc_EEA2
                        btst    #0x15,d0
                        bne.s   @Tloc_EEA2
                        btst    #0xD,d0
                        beq.w   @Tloc_EE06
                @Tloc_EEA2:
                        moveq   #0,d0           /* Unformatted disk */
                @Tloc_EEA4:
                        move.w  d0,L5919_(a6)
        }
        G2281_->UDS8_pc_A(&G2281_);
        return L5919_;
}

void F2085_EjectFloppyDisk(int16_t P5958_i_DriveIndex) {
        int16_t L5921_i_DriveAddress;


        L5921_i_DriveAddress = F2091_GetDriveAddress(P5958_i_DriveIndex);
        asm {
                moveq   #0x4F,d0        /* B_EJECT */
                move.w  L5921_i_DriveAddress(a6),d1
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
        }
        F2094_Call_DOS_FFLUSH();
}

int16_t F2086_(int16_t P5959_i_DriveIndex) {
        unsigned char L5922_;
        int16_t L5923_i_DriveAddress;
        int16_t L5924_;


        L5923_i_DriveAddress = F2091_GetDriveAddress(P5959_i_DriveIndex);
        L5922_ = F2087_(L5923_i_DriveAddress, 0);
        if (L5922_ & 0x04) {
                L5924_ = 2;
        } else {
                if (L5922_ & 0x08) {
                        L5924_ = 3;
                } else {
                        L5924_ = 1;
                }
        }
        return L5924_;
}

unsigned char F2087_(int16_t P5993_, int16_t P5994_) {
        unsigned char L5925_;


        G3091_i_ErrorCount = 0;
        asm {
                moveq   #0x4E,d0        /* B_DRVCHK */
                move.w  P5993_(a6),d1
                move.w  P5994_(a6),d2
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                move.b  d0,L5925_(a6)
        }
        if (G3091_i_ErrorCount) {
                G3091_i_ErrorCount = 0;
        }
        return L5925_;
}

int16_t F2088_(unsigned int16_t P5960_i_DriveIndex, int16_t P5961_) {
        register int16_t L5926_;
        int16_t L5927_i_DriveAddress;
        unsigned char L5928_;
        int16_t L5930_;
        int16_t L5929_;


        L5926_ = 1;
        L5929_ = G3885_[P5960_i_DriveIndex] + 1;
        if (L5929_ < 2) {
                F2094_Call_DOS_FFLUSH();
                L5927_i_DriveAddress = F2091_GetDriveAddress(P5960_i_DriveIndex);
                L5928_ = F2087_(L5927_i_DriveAddress, 6);
                if ((L5928_ & 0x04) && P5961_) {
                        L5926_ = 2;
                        L5929_--;
                } else {
                        L5926_ = 1;
                }
        }
        G3885_[P5960_i_DriveIndex] = L5929_;
        return L5926_;
}

int16_t F2089_(unsigned int16_t P5962_ui_DriveIndex) {
        register int16_t L5931_;
        int16_t L5932_i_DriveAddress;
        unsigned char L5933_;
        int16_t L5935_;
        int16_t L5934_;


        L5931_ = 1;
        L5934_ = G3885_[P5962_ui_DriveIndex] - 1;
        if (L5934_ < 0) {
                L5934_ = 0;
                L5931_ = 2;
        } else {
                if (L5934_ < 1) {
                        L5932_i_DriveAddress = F2091_GetDriveAddress(P5962_ui_DriveIndex);
                        L5933_ = F2087_(L5932_i_DriveAddress, 7);
                        if (L5933_ & 0x04) {
                                L5931_ = 2;
                        } else {
                                L5931_ = 1;
                        }
                }
        }
        G3885_[P5962_ui_DriveIndex] = L5934_;
        return L5931_;
}

int16_t F2090_(unsigned int16_t P5963_ui_DriveIndex) {
        register int16_t L5936_;
        int16_t L5937_i_DriveAddress;
        unsigned char L5938_;
        int16_t L6067_i_Unreferenced;


        L5937_i_DriveAddress = F2091_GetDriveAddress(P5963_ui_DriveIndex);
        L5938_ = F2087_(L5937_i_DriveAddress, 7);
        if (L5938_ & 0x04) {
                L5936_ = 2;
        } else {
                L5936_ = 1;
                G3885_[P5963_ui_DriveIndex] = 0;
        }
        return L5936_;
}

int16_t F2091_GetDriveAddress(int16_t P5964_i_DriveIndex) {
        int16_t L5939_i_DriveAddress;


        L5939_i_DriveAddress = (P5964_i_DriveIndex + 0x90) << 8; /* 0x90 for Drive 0, 0x91 for Drive 1 */
        return L5939_i_DriveAddress;
}

void F2094_Call_DOS_FFLUSH(
)
{
        asm {
                dc.w    0xFF0D  /* DOS CALL FFLUSH */
        }
}
#endif
#endif
