#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA433_A20E_A20F_A20G_A21E_A22E_A22G
#ifndef NOCOPYPROTECTION
/* Software interrupt handler invoked whenever a disk insertion or removal occurs */

struct IOExtTD* G1108_ps_IOExtTD_CPSX = NULL; /* Game disk drive */
struct IOExtTD* G1111_ps_IOExtTD_CPSX = NULL; /* Save disk drive */
struct MsgPort* G1109_ps_MsgPort_CPSX = NULL; /* Game disk drive */
struct MsgPort* G1112_ps_MsgPort_CPSX = NULL; /* Save disk drive */


int16_t F0571_COPYPROB_InitializeDiskChangeHandler_CPSX(
)
{
        G1108_ps_IOExtTD_CPSX = AllocMem((long)sizeof(struct IOExtTD), MEMF_PUBLIC);
        if (G1108_ps_IOExtTD_CPSX == NULL) {
                Alert(0x80444D81, 0L);
        }
        G1109_ps_MsgPort_CPSX = CreatePort(NULL, 0L);
        if (G1109_ps_MsgPort_CPSX == NULL) {
                Alert(0x80444D82, 0L);
        }
        G1108_ps_IOExtTD_CPSX->iotd_Req.io_Message.mn_ReplyPort = G1109_ps_MsgPort_CPSX;
        G1110_ps_Interrupt_CPSX = AllocMem((long)sizeof(struct Interrupt), MEMF_PUBLIC);
        if (G1110_ps_Interrupt_CPSX == NULL) {
                Alert(0x80444D83, 0L);
        }
        G1110_ps_Interrupt_CPSX->is_Node.ln_Type = NT_INTERRUPT;
        G1110_ps_Interrupt_CPSX->is_Node.ln_Name = NULL;
        G1110_ps_Interrupt_CPSX->is_Node.ln_Pri = 0;
        G1110_ps_Interrupt_CPSX->is_Data = (APTR)&G1084_l_GameDiskChangeVerticalBlankCount;
        G1110_ps_Interrupt_CPSX->is_Code = S0574_COPYPROB_DiskChangeStub_CPSX;
        if (OpenDevice("trackdisk.device", 0L, G1108_ps_IOExtTD_CPSX, 0L)) { /* First 0L is index of game disk drive */
                Alert(0x80444D84, 0L);
        }
        G1108_ps_IOExtTD_CPSX->iotd_Req.io_Command = TD_ADDCHANGEINT;
        G1108_ps_IOExtTD_CPSX->iotd_Req.io_Length = sizeof(struct Interrupt);
        G1108_ps_IOExtTD_CPSX->iotd_Req.io_Flags = 0;
        G1108_ps_IOExtTD_CPSX->iotd_Req.io_Data = (APTR)G1110_ps_Interrupt_CPSX;
        SendIO(G1108_ps_IOExtTD_CPSX);
        if (G0567_B_GameAndSaveDiskUseSameDrive) {
                return 0;
        }
        G1111_ps_IOExtTD_CPSX = AllocMem((long)sizeof(struct IOExtTD), MEMF_PUBLIC);
        if (G1111_ps_IOExtTD_CPSX == NULL) {
                Alert(0x80444D81, 0L);
        }
        G1112_ps_MsgPort_CPSX = CreatePort(NULL, 0L);
        if (G1112_ps_MsgPort_CPSX == NULL) {
                Alert(0x80444D82, 0L);
        }
        G1111_ps_IOExtTD_CPSX->iotd_Req.io_Message.mn_ReplyPort = G1112_ps_MsgPort_CPSX;
        G1113_ps_Interrupt = AllocMem((long)sizeof(struct Interrupt), MEMF_PUBLIC);
        if (G1113_ps_Interrupt == NULL) {
                Alert(0x80444D83, 0L);
        }
        G1113_ps_Interrupt->is_Node.ln_Type = NT_INTERRUPT;
        G1113_ps_Interrupt->is_Node.ln_Name = NULL;
        G1113_ps_Interrupt->is_Node.ln_Pri = 0;
        G1113_ps_Interrupt->is_Data = (APTR)&G1085_l_SaveDiskChangeVerticalBlankCount;
        G1113_ps_Interrupt->is_Code = S0574_COPYPROB_DiskChangeStub_CPSX;
        if (OpenDevice("trackdisk.device", (long)G0574_i_SaveDiskFloppyDriveIndex, G1111_ps_IOExtTD_CPSX, 0L)) {
                Alert(0x80444D85, 0L);
        }
        G1111_ps_IOExtTD_CPSX->iotd_Req.io_Command = TD_ADDCHANGEINT;
        G1111_ps_IOExtTD_CPSX->iotd_Req.io_Length = sizeof(struct Interrupt);
        G1111_ps_IOExtTD_CPSX->iotd_Req.io_Flags = 0;
        G1111_ps_IOExtTD_CPSX->iotd_Req.io_Data = (APTR)G1113_ps_Interrupt;
        SendIO(G1111_ps_IOExtTD_CPSX);
}

void F0572_COPYPROB_ReleaseDiskChangeHandler_CPSX(
)
{
        if (G1110_ps_Interrupt_CPSX) {
                FreeMem(G1110_ps_Interrupt_CPSX, (long)sizeof(struct Interrupt));
        }
        if (G1113_ps_Interrupt) {
                FreeMem(G1113_ps_Interrupt, (long)sizeof(struct Interrupt));
        }
}

void F0573_COPYPROB_DiskChangeHandler_CPSX(
P0989_puc_, P0990_pul_InterruptData)
unsigned char* P0989_puc_              SEPARATOR
unsigned long* P0990_pul_InterruptData FINAL_SEPARATOR
{
        int_start();
        *P0990_pul_InterruptData = G1086_VerticalBlankCount;
        int_end();
}

#asm
_S0574_COPYPROB_DiskChangeStub_C:
        movem.l a0-a1,-(sp)
        bsr.s _F0573_COPYPROB_DiskChangeHandle
        addq.l  #8,sp
        rts
#endasm
#endif
#endif
