#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
overlay "start"

#ifdef MEDIA265_S20E_S21E /* CHANGE7_03_IMPROVEMENT Support for two floppy disk drives */
BOOLEAN G0567_B_GameAndSaveDiskUseSameDrive;
unsigned int16_t G0568_ui_DiskType;
char* G0569_pc_SavedGameFileName;
char* G0570_pc_SavedGameBackupFileName;
unsigned char* G0571_puc_GameDiskDriveName;
unsigned char* G0572_puc_SavedGameDiskDriveName;
int16_t G0573_i_GameDiskFloppyDriveIndex;
int16_t G0574_i_SaveDiskFloppyDriveIndex;
char G0575_ac_SavedGameFileName[] = "A:\\CSBGAME.DAT";
char G0576_ac_SavedGameBackupFileName[] = "A:\\CSBGAME.BAK";
#endif

#ifdef MEDIA200_S10EA_S10EB_S11E_S20E_S21E /* CHANGE3_20_OPTIMIZATION CHANGE7_03_IMPROVEMENT Support for two floppy disk drives */
BOOLEAN F0449_FLOPPY_IsDiskWriteProtected(
P0858_i_FloppyDriveIndex, P0859_pc_Buffer)
int16_t P0858_i_FloppyDriveIndex SEPARATOR
char*   P0859_pc_Buffer          FINAL_SEPARATOR
{
        Floprd(P0859_pc_Buffer, C0L_FILLER, P0858_i_FloppyDriveIndex, C001_SECTOR_1, C0_TRACK_0, C0_SIDE_0, C1_ONE_SECTOR);
        return (Flopwr(P0859_pc_Buffer, C0L_FILLER, P0858_i_FloppyDriveIndex, C001_SECTOR_1, C0_TRACK_0, C0_SIDE_0, C1_ONE_SECTOR) == CM13_ERROR_EWRPRO_MEDIUM_WRITE_PROTECTED);
}
#endif
#ifdef MEDIA236_S12E_S12G_S13FA_S13FB /* CHANGE3_20_OPTIMIZATION Removed support for two floppy disk drives in the function to detect if a floppy disk is write protected. This support is reintroduced in CSB for Atari ST 2.0 */
BOOLEAN F0449_FLOPPY_IsDiskWriteProtected(
P0859_pc_Buffer)
char* P0859_pc_Buffer FINAL_SEPARATOR
{
        Floprd(P0859_pc_Buffer, C0L_FILLER, C0_FLOPPY_DRIVE_A, C001_SECTOR_1, C0_TRACK_0, C0_SIDE_0, C1_ONE_SECTOR);
        return (Flopwr(P0859_pc_Buffer, C0L_FILLER, C0_FLOPPY_DRIVE_A, C001_SECTOR_1, C0_TRACK_0, C0_SIDE_0, C1_ONE_SECTOR) == CM13_ERROR_EWRPRO_MEDIUM_WRITE_PROTECTED);
}
#endif

#ifdef MEDIA006_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB /* CHANGE7_03_IMPROVEMENT */
void F0450_FLOPPY_ForceMediaChangeDetection(
)
{
        Rwabs(C0_READ_WITH_MEDIA_CHANGE, NULL, C2_TWO_SECTORS, C0_LOGICAL_SECTOR_0, C0_FLOPPY_DRIVE_A);
        Fsfirst("F", C0_FILE_ATTRIBUTES); /* Searches for a file named "F" in the current folder of the default drive */
}
#endif
#ifdef MEDIA265_S20E_S21E /* CHANGE7_03_IMPROVEMENT Support for two floppy disk drives. The code of this function is directly inspired by a routine described in the official Atari "Rainbow TOS Release Notes" (First edition Aug 7, 1989 or second edition Mar 5, 1991) in a section named "Forcing 'Media Change'". It will force a "Media Change" even if the floppy disk in the drive has not changed (causing GEMDOS to flush any cached information about the file system) */
void F0450_FLOPPY_ForceMediaChangeDetection(
P0860_i_DriveType)
int16_t P0860_i_DriveType FINAL_SEPARATOR
{
        REGISTER long L1445_l_SupervisorStack;
        REGISTER long L1446_l_ReplacementBPBVector;
        int16_t L1447_i_FloppyDriveIndex;
        char* L1448_pc_DefaultBPBVector;
        char* L1449_pc_DefaultMediaChangeVector;
        char* L1450_pc_DefaultRWBlocksVector;
        static char G0577_ac_TestFileName[5] = "A:\\F";



        if (P0860_i_DriveType != C0_DRIVE_TYPE_NONE) {
                if (P0860_i_DriveType == C1_DRIVE_TYPE_GAME_DISK) {;
                        L1447_i_FloppyDriveIndex = G0573_i_GameDiskFloppyDriveIndex;
                } else {
                        L1447_i_FloppyDriveIndex = G0574_i_SaveDiskFloppyDriveIndex;
                }
                G0577_ac_TestFileName[0] = 'A' + L1447_i_FloppyDriveIndex;
                L1445_l_SupervisorStack = Super(0L);
                asm {
                                lea     V0450000_A6RegisterBackup(PC),A0
                                move.l  A6,(A0)
                                move.l  C0x0472_ADDRESS_SYSTEM_VARIABLE_hdv_bpb,L1448_pc_DefaultBPBVector(A6)                 /* Exception vector to routine that establishes the BPB of a BIOS drive. The drive index is passed on the stack at 4(A7) */
                                move.l  C0x047E_ADDRESS_SYSTEM_VARIABLE_hdv_mediach,L1449_pc_DefaultMediaChangeVector(A6)     /* Vector to routine for establishing the media-change status of a BIOS drive. The drive index is passed on the stack at 4(A7) */
                                move.l  C0x0476_ADDRESS_SYSTEM_VARIABLE_hdv_rw,L1450_pc_DefaultRWBlocksVector(A6)             /* Exception vector to the routine for reading and writing of blocks to BIOS drives. The same parameters are passed on the stack as for Rwabs (starting with 4(A7); rwflag) */
                                lea     T0450003_ReplacementRoutineBPBVector(PC),A0
                                move.l  A0,C0x0472_ADDRESS_SYSTEM_VARIABLE_hdv_bpb                                            /* Replace original vector */
                                move.l  A0,L1446_l_ReplacementBPBVector
                                lea     T0450005_ReplacementRoutineMediaChangeVector(PC),A0
                                move.l  A0,C0x047E_ADDRESS_SYSTEM_VARIABLE_hdv_mediach                                        /* Replace original vector */
                                lea     T0450007_ReplacementRoutineRWBlocks(PC),A0
                                move.l  A0,C0x0476_ADDRESS_SYSTEM_VARIABLE_hdv_rw                                             /* Replace original vector */
                }
                Fopen(G0577_ac_TestFileName, C0_READ_ONLY);
                asm {
                        cmp.l   C0x0472_ADDRESS_SYSTEM_VARIABLE_hdv_bpb,L1446_l_ReplacementBPBVector
                        bne.s   T0450010                                                                                /* If the current vector current vector is not the replacement routine */
                        move.l  L1448_pc_DefaultBPBVector(A6),C0x0472_ADDRESS_SYSTEM_VARIABLE_hdv_bpb                   /* Restore original vector */
                        move.l  L1449_pc_DefaultMediaChangeVector(A6),C0x047E_ADDRESS_SYSTEM_VARIABLE_hdv_mediach       /* Restore original vector */
                        move.l  L1450_pc_DefaultRWBlocksVector(A6),C0x0476_ADDRESS_SYSTEM_VARIABLE_hdv_rw               /* Restore original vector */
                        bra.s   T0450010
                        T0450003_ReplacementRoutineBPBVector:
                                movea.l V0450000_A6RegisterBackup(PC),A0
                                move.w  -2(A0),D0                                                                       /* L1447_i_FloppyDriveIndex */
                                cmp.w   4(A7),D0                                                                        /* Drive index */
                                bne.s   T0450004                                                                        /* Branch if D0 != drive index */
                                move.l  -6(A0),C0x0472_ADDRESS_SYSTEM_VARIABLE_hdv_bpb                                  /* Restore original vector L1448_pc_DefaultBPBVector */
                                move.l  -10(A0),C0x047E_ADDRESS_SYSTEM_VARIABLE_hdv_mediach                             /* Restore original vector L1449_pc_DefaultMediaChangeVector */
                                move.l  -14(A0),C0x0476_ADDRESS_SYSTEM_VARIABLE_hdv_rw                                  /* Restore original vector L1450_pc_DefaultRWBlocksVector */
                        T0450004:
                                movea.l -6(A0),A0                                                                       /* L1448_pc_DefaultBPBVector */
                                jmp     (A0)                                                                            /* Call L1448_pc_DefaultBPBVector */
                        T0450005_ReplacementRoutineMediaChangeVector:
                                movea.l V0450000_A6RegisterBackup(PC),A0
                                move.w  -2(A0),D0                                                                       /* L1447_i_FloppyDriveIndex */
                                cmp.w   4(A7),D0                                                                        /* Drive index */
                                bne.s   T0450006                                                                        /* Branch if D0 != drive index */
                                moveq   #2,D0                                                                           /* Return value. 2 is the BIOS value MED_CHANGED: Media has changed (see Mediach() BIOS function) */
                                rts
                        T0450006:
                                movea.l -10(A0),A0                                                                      /* L1449_pc_DefaultMediaChangeVector */
                                jmp     (A0)                                                                            /* Call L1449_pc_DefaultMediaChangeVector */
                        T0450007_ReplacementRoutineRWBlocks:
                                movea.l V0450000_A6RegisterBackup(PC),A0
                                move.w  -2(A0),D0                                                                       /* L1447_i_FloppyDriveIndex */
                                cmp.w   14(A7),D0                                                                       /* Drive index as in Rwabs parameters, starting at 4(a7) */
                                bne.s   T0450008                                                                        /* Branch if D0 != drive index */
                                moveq   #-14,D0                                                                         /* Return value. -14 is the BIOS error E_CHNG: Media change detected */
                                rts
                        T0450008:
                                movea.l -14(A0),A0                                                                      /* L1450_pc_DefaultRWBlocksVector */
                                jmp     (A0)                                                                            /* Call L1450_pc_DefaultRWBlocksVector */
                        V0450000_A6RegisterBackup:
                                dc.l    0x00000000
                        T0450010:
                }
                Super(L1445_l_SupervisorStack);
        }
}
#endif
#endif
