#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA174_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_SU1E
char* G0518_pc_SystemCallReturnAddress;


#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
overlay "main"

asm {
#if EXETYPE == C03_GAME
        R0055_TOS_CallBIOS:
                move.l  A2,D3                                                   /* Backup A2 register in D3 */
                move.l  (A7)+,G0518_pc_SystemCallReturnAddress(A4)              /* Backup return address and remove it from the stack */
                trap    #13                                                     /* Call BIOS */
                movea.l G0518_pc_SystemCallReturnAddress(A4),A0                 /* Restore return address */
                movea.l D3,A2                                                   /* Restore A2 from D3 */
                jmp     (A0)                                                    /* Jump to return address */
        R0056_TOS_CallXBIOS:
                move.l  A2,D3                                                   /* Backup A2 register in D3 */
                move.l  (A7)+,G0518_pc_SystemCallReturnAddress(A4)              /* Backup return address and remove it from the stack */
                trap    #14                                                     /* Call XBIOS */
                movea.l G0518_pc_SystemCallReturnAddress(A4),A0                 /* Restore return address */
                movea.l D3,A2                                                   /* Restore A2 from D3 */
                jmp     (A0)                                                    /* Jump to return address */
#endif
        R0057_TOS_CallGEMDOS:
                move.l  A2,D3                                                   /* Backup A2 register in D3 */
                move.l  (A7)+,G0518_pc_SystemCallReturnAddress(A4)              /* Backup return address and remove it from the stack */
                trap    #1                                                      /* Call GEMDOS */
                movea.l G0518_pc_SystemCallReturnAddress(A4),A0                 /* Restore return address */
                movea.l D3,A2                                                   /* Restore A2 from D3 */
                jmp     (A0)                                                    /* Jump to return address */
#if EXETYPE == C03_GAME
        S0058_TOS_Exception257Handler_CriticalError:
                moveq   #-1,D0                                                  /* This handler simply returns -1 */
                rts
        S0059_TOS_SetException257Vector_CriticalError:
                lea     S0058_TOS_Exception257Handler_CriticalError(PC),A0
                move.l  A0,C0x0404_ADDRESS_SYSTEM_VARIABLE_etv_critic           /* Replace the default critical error handler by the custom handler */
                rts                                                             /* Comment required to avoid compilation bug */
#endif
}
#endif
#ifdef MEDIA772_SU1E
#if EXETYPE == C02_LAUNCHER
asm {
        gemdos:                 ; This label overrides the gemdos routine from SYSLIB
        R0057_TOS_CallGEMDOS:
                move.l          A2,D3
                move.l          (A7)+,G0518_pc_SystemCallReturnAddress(A4)
                trap            #1
                bra.s           T0923001
        R0055_TOS_CallBIOS:
                move.l          A2,D3
                move.l          (A7)+,G0518_pc_SystemCallReturnAddress(A4)
                trap            #13
                bra.s           T0923001
        R0056_TOS_CallXBIOS:
                move.l          A2,D3
                move.l          (A7)+,G0518_pc_SystemCallReturnAddress(A4)
                trap            #14
        T0923001:
                movea.l         G0518_pc_SystemCallReturnAddress(A4),A0
                movea.l         D3,A2
                jmp             (A0)
}
#endif
#if EXETYPE != C02_LAUNCHER
unsigned char* G3520_A2RegisterBackup;


long R0055_TOS_CallBIOS(
)
{
        asm {
                move.l  a2,G3520_A2RegisterBackup(a5)
                move.l  (sp)+,G0518_pc_SystemCallReturnAddress(a5)
                trap    #0xD
                movea.l G0518_pc_SystemCallReturnAddress(a5),a0
                movea.l G3520_A2RegisterBackup(a5),a2
                jmp     (a0)
        }
}

long R0056_TOS_CallXBIOS(
)
{
        asm {
                move.l  a2,G3520_A2RegisterBackup(a5)
                move.l  (sp)+,G0518_pc_SystemCallReturnAddress(a5)
                trap    #0xE
                movea.l G0518_pc_SystemCallReturnAddress(a5),a0
                movea.l G3520_A2RegisterBackup(a5),a2
                jmp     (a0)
        }
}

long R0057_TOS_CallGEMDOS(
)
{
        asm {
                move.l  a2,G3520_A2RegisterBackup(a5)
                move.l  (sp)+,G0518_pc_SystemCallReturnAddress(a5)
                trap    #1
                movea.l G0518_pc_SystemCallReturnAddress(a5),a0
                movea.l G3520_A2RegisterBackup(a5),a2
                jmp     (a0)
        }
}

void F1603_TerminateTOSProgram(int16_t P5077_i_ReturnCode) {
        Pterm(P5077_i_ReturnCode);
}
#endif
#endif
#endif
