#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA774_SU1E_AU1E_AU2E
#ifdef MEDIA772_SU1E
#undef Floprd
#define Floprd(a, b, c, d, e, f, g) (long)R0056_TOS_CallXBIOS(0x08, a, b, c, d, e, f, g)
#endif
#ifdef MEDIA762_AU1E
unsigned int16_t G3751_aui_Palette[16] = { 0x000, 0x777, 0x999, 0x730, 0x0DD, 0x950, 0x090, 0x0D0, 0xF00, 0xFD0, 0xD97, 0xFF0, 0x555, 0xBBB, 0x00F, 0xFFF };
long G3868_l_Unreferenced;
BOOLEAN G0567_B_GameAndSaveDiskUseSameDrive;
unsigned char G3441_[sizeof(struct InfoData) + 4]; /* Space to store a long aligned structure */
char G3870_ac_Unreferenced[224];
int16_t G0573_i_GameDiskFloppyDriveIndex;
int16_t G0574_i_SaveDiskFloppyDriveIndex = 1;
#if (EXETYPE == C07_HINT)
#include "CEDT101.C"
#endif
#endif


#ifdef MEDIA772_SU1E
void F0924_SetCriticalErrorHandler(
)
{
        register int16_t L5003_i_FloppyDriveCount;
        register long L5002_l_SupervisorStack;


        L5002_l_SupervisorStack = Super(0L);
        L5003_i_FloppyDriveCount = *((int16_t*)0x4A6); /* _nflops (Number of floppy drives) */
        asm {
                        lea     @Tloc_31A,a0
                        move.l  a0,(0x404).w    /* etv_critic : Critical Error Handoff Vector (GEMDOS) */
                        bra.s   @Tloc_31E
                @Tloc_31A:
                        moveq   #0xFFFFFFFF,d0
                        rts
                @Tloc_31E:
        }
        Super(L5002_l_SupervisorStack);
        if (L5003_i_FloppyDriveCount == 2) {
                G0574_i_SaveDiskFloppyDriveIndex = 1;
        } else {
                G3806_ = 1;
                G3774_pc_SaveFloppyDriveName[6] = 'A';
                G3797_pc_DungeonMasterSavedGameFileName[0] = 'A';
                G3798_pc_DungeonMasterBackupSavedGameFileName[0] = 'A';
                G3799_pc_ChaosStrikesBackSavedGameFileName[0] = 'A';
                G3800_pc_ChaosStrikesBackBackupSavedGameFileName[0] = 'A';
                G3802_pc_SaveFloppyDriveRootPath[0] = 'A';
        }
}

void F1928_ForceMediaChangeDetection(int16_t P5771_) {
        int16_t L5776_i_FloppyDriveIndex;
        register long L5642_l_SupervisorStack;
        register long L5777_;
        char* L5643_;
        char* L5734_;
        char* L5735_;


        if (P5771_ == 0) {
                L5776_i_FloppyDriveIndex = G0573_i_GameDiskFloppyDriveIndex;
        } else {
                L5776_i_FloppyDriveIndex = G0574_i_SaveDiskFloppyDriveIndex;
        }
        G3716_ac_TestFileName[0] = 'A' + L5776_i_FloppyDriveIndex;
        L5642_l_SupervisorStack = Super(0L);
        asm {
                @Tloc_75C8:
                        lea     @TA6Backup,a0
                        move.l  a6,(a0)
                        move.l  (0x472).w,L5643_(a6)
                        move.l  (0x47E).w,L5734_(a6)
                        move.l  (0x476).w,L5735_(a6)
                        lea     @Tloc_7636,a0
                        move.l  a0,(0x472).w
                        move.l  a0,d6
                        lea     @Tloc_7660,a0
                        move.l  a0,(0x47E).w
                        lea     @Tloc_767E,a0
                        move.l  a0,(0x476).w
                        move.l  a2,-(sp)
                        clr.w   -(sp)
                        pea     G3716_ac_TestFileName(a5)
                        move.w  #0x3D,-(sp)     /* Fopen */
                        trap    #1
                        addq.l  #8,sp
                        movea.l (sp)+,a2
                        cmp.l   (0x472).w,d6
                        bne.s   @Tloc_85E
                        move.l  L5643_(a6),(0x472).w
                        move.l  L5734_(a6),(0x47E).w
                        move.l  L5735_(a6),(0x476).w
                        bra.s   @Tloc_85E
                @TA6Backup:
                        dc.l 0
                @Tloc_7636:
                        move.l  a6,-(sp)
                        movea.l @TA6Backup,a6
                        move.w  L5776_i_FloppyDriveIndex(a6),d0
                        cmp.w   8(sp),d0
                        bne.s   @Tloc_7658
                        move.l  L5643_(a6),(0x472).w
                        move.l  L5734_(a6),(0x47E).w
                        move.l  L5735_(a6),(0x476).w
                @Tloc_7658:
                        movea.l L5643_(a6),a0
                        movea.l (sp)+,a6
                        jmp     (a0)
                @Tloc_7660:
                        move.l  a6,-(sp)
                        movea.l @TA6Backup,a6
                        move.w  L5776_i_FloppyDriveIndex(a6),d0
                        cmp.w   8(sp),d0
                        bne.s   @Tloc_7676
                        moveq   #2,d0
                        movea.l (sp)+,a6
                        rts
                @Tloc_7676:
                        movea.l L5734_(a6),a0
                        movea.l (sp)+,a6
                        jmp     (a0)
                @Tloc_767E:
                        move.l  a6,-(sp)
                        movea.l @TA6Backup,a6
                        move.w  L5776_i_FloppyDriveIndex(a6),d0
                        cmp.w   0x12(sp),d0
                        bne.s   @Tloc_7694
                        moveq   #0xFFFFFFF2,d0
                        movea.l (sp)+,a6
                        rts
                @Tloc_7694:
                        movea.l L5735_(a6),a0
                        movea.l (sp)+,a6
                        jmp     (a0)
                @Tloc_85E:
        }
        Super(L5642_l_SupervisorStack);
}
#endif

#if (defined(MEDIA762_AU1E) && (EXETYPE == C06_CEDT))
int16_t F2100_GetChildFiles(char* P6028_pc_DirectoryName, char (*P6029_pac_FileNames)[32]) {
        register struct InfoData* L6157_ps_InfoData;
        register struct FileLock* L6158_ps_FileLock;
        register int16_t L6159_;


        L6159_ = 0;
        L6157_ps_InfoData = (struct InfoData*)(((long)G3441_ + 3) & 0xFFFFFFFC); /* structure must be long aligned */
        if ((L6158_ps_FileLock = Lock(P6028_pc_DirectoryName, ACCESS_READ)) == NULL) {
                return L6159_;
        }
        if (Examine(L6158_ps_FileLock, L6157_ps_InfoData)) {
                do {
                        if (L6157_ps_InfoData->id_UnitNumber < 0) {
                                F0088_strcpy(P6029_pac_FileNames[L6159_++], &L6157_ps_InfoData->id_DiskState);
                        }
                } while (ExNext(L6158_ps_FileLock, L6157_ps_InfoData) && (L6159_ < 200));
        }
        UnLock(L6158_ps_FileLock);
        return L6159_;
}
#endif

#ifdef MEDIA762_AU1E
void F2007_SetFloppyDriveIndex(
)
{
        register int16_t L5872_i_DiskDriveCount;
        register int16_t L5873_i_SaveDiskFloppyDriveIndex;
        register char L5874_c_SavedGameFloppyDriveNumber;
#if (EXETYPE == C07_HINT)
        char* L5875_;
#endif


        L5872_i_DiskDriveCount = 1;
        for (L5873_i_SaveDiskFloppyDriveIndex = 1; L5873_i_SaveDiskFloppyDriveIndex < 4; L5873_i_SaveDiskFloppyDriveIndex++) {
                if (F1106_IsTrackdiskDeviceOpened(L5873_i_SaveDiskFloppyDriveIndex)) {
                        L5872_i_DiskDriveCount++;
                        break;
                }
        }
#if (EXETYPE == C06_CEDT)
        L5874_c_SavedGameFloppyDriveNumber = '0' + L5873_i_SaveDiskFloppyDriveIndex;
#endif
        if (L5872_i_DiskDriveCount > 1) {
#if (EXETYPE == C07_HINT)
                L5874_c_SavedGameFloppyDriveNumber = '0' + L5873_i_SaveDiskFloppyDriveIndex;
#endif
                G0567_B_GameAndSaveDiskUseSameDrive = C0_FALSE;
                G0573_i_GameDiskFloppyDriveIndex = 0;
                G0574_i_SaveDiskFloppyDriveIndex = L5873_i_SaveDiskFloppyDriveIndex;
                G3774_pc_SaveFloppyDriveName[8] = L5874_c_SavedGameFloppyDriveNumber;
                G3797_pc_DungeonMasterSavedGameFileName[2] = L5874_c_SavedGameFloppyDriveNumber;
                G3798_pc_DungeonMasterBackupSavedGameFileName[2] = L5874_c_SavedGameFloppyDriveNumber;
                G3799_pc_ChaosStrikesBackSavedGameFileName[2] = L5874_c_SavedGameFloppyDriveNumber;
                G3800_pc_ChaosStrikesBackBackupSavedGameFileName[2] = L5874_c_SavedGameFloppyDriveNumber;
                G3802_pc_SaveFloppyDriveRootPath[2] = L5874_c_SavedGameFloppyDriveNumber;
        } else {
#if (EXETYPE == C07_HINT)
                L5874_c_SavedGameFloppyDriveNumber = '0';
#endif
                G0567_B_GameAndSaveDiskUseSameDrive = C1_TRUE;
                G0573_i_GameDiskFloppyDriveIndex = 0;
                G0574_i_SaveDiskFloppyDriveIndex = 0;
                G3774_pc_SaveFloppyDriveName[8] = '0';
                G3797_pc_DungeonMasterSavedGameFileName[2] = '0';
                G3798_pc_DungeonMasterBackupSavedGameFileName[2] = '0';
                G3799_pc_ChaosStrikesBackSavedGameFileName[2] = '0';
                G3800_pc_ChaosStrikesBackBackupSavedGameFileName[2] = '0';
                G3802_pc_SaveFloppyDriveRootPath[2] = '0';
        }
#if (EXETYPE == C07_HINT)
        L5875_ = G3578_ps_InsertDiskInDriveBThenPressLoad->_pc_0x02_String;
        L5875_[94] = L5874_c_SavedGameFloppyDriveNumber;
        L5875_ = G3580_ps_InsertDiskInDriveB->_pc_0x02_String;
        L5875_[43] = L5874_c_SavedGameFloppyDriveNumber;
#endif
}
#endif

#ifdef MEDIA773_SU1E_AU1E
BOOLEAN F0530_FLOPPY_DirectoryExists(
register char* P0962_pc_Name,
int16_t P5903_)
{
#ifdef MEDIA772_SU1E
        register char* L5755_pc_PathWithoutTrailingAntislash;
        register long L5756_l_Result;
        char L5757_ac_Path[30];
        char L5758_ac_CurrentPathBackup[64];


        F1928_ForceMediaChangeDetection(P5903_);
        L5755_pc_PathWithoutTrailingAntislash = L5757_ac_Path;
        while (*L5755_pc_PathWithoutTrailingAntislash++ = *P0962_pc_Name++);
        L5755_pc_PathWithoutTrailingAntislash[-2] = '\0';       /* Replace trailing \ in path */
        Dgetpath(L5758_ac_CurrentPathBackup, 0);                /* Backup current directory of current drive */
        L5756_l_Result = Dsetpath(L5757_ac_Path);               /* Try to change directory */
        Dsetpath(L5758_ac_CurrentPathBackup);                   /* Restore current directory */
        return (L5756_l_Result >= 0);                           /* True if the directory exists */
#endif
#ifdef MEDIA762_AU1E
        struct FileLock* L1726_ps_Lock;


        L1726_ps_Lock = NULL;
        if (L1726_ps_Lock = Lock(P0962_pc_Name, ACCESS_READ)) {
                UnLock(L1726_ps_Lock);
        }
        return (L1726_ps_Lock != NULL);
#endif
}
#endif

#if (EXETYPE == C06_CEDT)
#ifdef MEDIA772_SU1E
int16_t F1991_(int16_t P5904_i_FloppyDriveIndex)
#endif
#ifdef MEDIA762_AU1E
int16_t F1991_(register int16_t P5904_i_FloppyDriveIndex)
#endif
{
#ifdef MEDIA772_SU1E
        char L5759_[600];


        F1928_ForceMediaChangeDetection(P5904_i_FloppyDriveIndex);
        if (Floprd(L5759_, 0L, (P5904_i_FloppyDriveIndex == 0) ? (unsigned int16_t)G0573_i_GameDiskFloppyDriveIndex : G0574_i_SaveDiskFloppyDriveIndex, 7, 0, 0, 1)) {
                return -1;
        }
        if ((F0087_strcmp(L5759_, "copyright (c) 1987, Software Heaven, Inc.") == 0) && (F0087_strcmp(&L5759_[128], "Chaos Strikes Back") == 0)) {
                return 1;
        } else {
                return 0;
        }
#endif
#ifdef MEDIA762_AU1E
        register struct InfoData* L6160_ps_InfoData;
        register struct FileLock* L6161_ps_Lock;
        register int16_t L6162_;
        register int16_t L6163_;
        static char* G3934_ = "DFx:";


        L6161_ps_Lock = NULL;
        L6162_ = -1;
        L6163_ = 0;
        P5904_i_FloppyDriveIndex = (P5904_i_FloppyDriveIndex == 0) ? (unsigned int16_t)G0573_i_GameDiskFloppyDriveIndex : G0574_i_SaveDiskFloppyDriveIndex;
        L6160_ps_InfoData = (struct InfoData*)(((long)G3441_ + 3) & 0xFFFFFFFC); /* structure must be long aligned */
        G3934_[2] = P5904_i_FloppyDriveIndex + '0';
        while (!F1109_GetDiskState(P5904_i_FloppyDriveIndex) && L6163_++ < 6) {
                Delay(12L);
        }
        L6163_ = 0;
        while (!(L6161_ps_Lock = Lock(G3934_, ACCESS_READ)) && L6163_++ < 7) {
                Delay(10L);
        }
        if (!L6161_ps_Lock) {
                return L6162_;
        }
        if (Examine(L6161_ps_Lock, L6160_ps_InfoData)) {
                L6162_ = (F0087_strcmp("FTL_CSB_Utility", &L6160_ps_InfoData->id_DiskState) == 0);
        }
        UnLock(L6161_ps_Lock);
        return L6162_;
#endif
}

#ifdef MEDIA772_SU1E
#include "CEDTINCJ.C"
#endif

BOOLEAN F0453_FLOPPY_IsFormatDiskSuccessful(
)
{
        register int16_t L1457_ui_TrackIndex;
        register BOOLEAN L1458_B_IsFormatDiskSuccessful;
        register long L1459_l_ByteCount;
        register unsigned char* L1460_puc_Buffer;


        L1458_B_IsFormatDiskSuccessful = C1_TRUE;
        do {
                L1457_ui_TrackIndex = F1991_(1);
                if (L1457_ui_TrackIndex > 0) {
#ifdef MEDIA772_SU1E
                        F7072_GetDialogChoiceOrdinal(G3764_THAT_S_THE_CHAOS_STRIKES_BACK_UTILITY_DISK, C08_COLOR_RED | MASK0x8000_INVERT_BOX, G7060_OK, &G2263_s_Input_Dialog1Choice);
#endif
#ifdef MEDIA762_AU1E
                        F7074_DrawDialogErrorMessage(G3764_THAT_S_THE_CHAOS_STRIKES_BACK_UTILITY_DISK, C08_COLOR_RED | MASK0x8000_UNKNOWN);
#endif
                        if (F7072_GetDialogChoiceOrdinal(G3786_THIS_WILL_DESTROY_ALL_INFORMATION_ON_THE_DISK_IN, C15_COLOR_WHITE | MASK0x8000_INVERT_BOX, G7061_OK_CANCEL, &G2264_s_Input_Dialog2Choices) == 2) {
#ifdef MEDIA772_SU1E
                                return;
#endif
#ifdef MEDIA762_AU1E
                                return C1_TRUE;
#endif
                        }
                } else {
                        if (L1457_ui_TrackIndex == 0) {
                                if (F7072_GetDialogChoiceOrdinal(G3786_THIS_WILL_DESTROY_ALL_INFORMATION_ON_THE_DISK_IN, C11_COLOR_YELLOW | MASK0x8000_INVERT_BOX, G3795_FORMAT_CANCEL, &G2264_s_Input_Dialog2Choices) == 2) {
#ifdef MEDIA772_SU1E
                                        return;
#endif
#ifdef MEDIA762_AU1E
                                        return C1_TRUE;
#endif
                                }
                        }
                }
        } while (L1457_ui_TrackIndex > 0);
        F7072_GetDialogChoiceOrdinal(G3787_PLEASE_WAIT_FORMATTING_DISK, C15_COLOR_WHITE, NULL, NULL);
#ifdef MEDIA772_SU1E
        L1460_puc_Buffer = F7023_Alloc(10000L);
        for (L1457_ui_TrackIndex = 0; L1457_ui_TrackIndex < 80; L1457_ui_TrackIndex++) {
                if (Flopfmt(L1460_puc_Buffer, C0L_FILLER, G0574_i_SaveDiskFloppyDriveIndex, C9_SECTORS_PER_TRACK, L1457_ui_TrackIndex, C0_SIDE_0, C1_INTERLEAVE, C0x87654321_MAGIC, (L1457_ui_TrackIndex <= 1) ? 0 : 0xE5E5)) {
                        goto T0453006;
                }
        }
        Protobt(L1460_puc_Buffer, C0x01000001_SERIAL_RANDOM, C2_80_TRACKS_SINGLE_SIDED, C0_DISK_NOT_EXECUTABLE);
        if (Flopwr(L1460_puc_Buffer, C0L_FILLER, G0574_i_SaveDiskFloppyDriveIndex, C001_SECTOR_1, C0_TRACK_0, C0_SIDE_0, C1_ONE_SECTOR)) {
                T0453006:
                L1458_B_IsFormatDiskSuccessful = C0_FALSE;
                F7072_GetDialogChoiceOrdinal(G3791_ERROR_WHILE_FORMATTING_DISK, C08_COLOR_RED | MASK0x8000_INVERT_BOX, G7060_OK, &G2263_s_Input_Dialog1Choice);
        }
        F7024_Free(L1460_puc_Buffer);
#endif
#ifdef MEDIA762_AU1E
        if (L1458_B_IsFormatDiskSuccessful = !F0519_FLOPPY_GetFormatResult(G0574_i_SaveDiskFloppyDriveIndex, "DungeonSave")) {
                return L1458_B_IsFormatDiskSuccessful;
        }
        L1458_B_IsFormatDiskSuccessful = C0_FALSE;
        F7074_DrawDialogErrorMessage(G3791_ERROR_WHILE_FORMATTING_DISK, C08_COLOR_RED | MASK0x8000_UNKNOWN);
#endif
        return L1458_B_IsFormatDiskSuccessful;
}
#endif

#if ((defined(MEDIA762_AU1E) && (EXETYPE == C06_CEDT)) || (EXETYPE == C07_HINT))
void F7323_WaitVerticalBlank(
)
{
        WaitTOF();
}
#endif

#ifdef MEDIA762_AU1E
#if (EXETYPE == C06_CEDT)
void F2102_(
)
{
        int16_t L6164_ai_ColorPalette[16];
        int16_t L6165_i_ColorIndex;


        F2042_InitializeScreen();
        F0575_VerticalBlank_Initialize();
        F1111_CPSX();
        for (L6165_i_ColorIndex = 0; L6165_i_ColorIndex < 16; L6165_i_ColorIndex++) {
                L6164_ai_ColorPalette[L6165_i_ColorIndex] = 0;
        }
        F2045_FadeToPalette(L6164_ai_ColorPalette);
        F7021_InitAllocatedMemoryBlocks();
        G3935_ = F7023_Alloc(10608L, MASK0x0008_MEMREQ);
        F2007_SetFloppyDriveIndex();
        F0077_MOUSE_EnableScreenUpdate_CPSE();
        F0008_MAIN_ClearBytes(G0348_Bitmap_Screen, 32000L);
        F0078_MOUSE_DisableScreenUpdate();
}

void F2103_(
)
{
        F1992_RequestUtilityDiskInDrive();
        F7024_Free(G3935_);
        F7022_FreeAllAllocatedBlocks();
        F0518_FLOPPY_Deinitialize_CPSX();
        F0576_VerticalBlank_Deinitialize();
        F2043_UninitializeScreen();
}
#endif

char F2008_IsLeftMouseButtonDown(
)
{
        return F1128_IsLeftMouseButtonDown();
}

int16_t F2009_GetMouseX(
)
{
        return F2047_GetMouseX();
}

int16_t F2010_GetMouseY(
)
{
        return F2048_GetMouseY();
}
#endif
#endif
