#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA692_X31J
extern FTL_LIBRARY* F9008_OpenPRIM();

extern void F1681_INT1_06_();
extern void F1680_INT1_05_UninstallInterruptHandler();
extern int16_t F1679_INT1_04_InstallInterruptHandler();
extern void F1683_INT1_03_Expunge();

extern void F1667_ExecuteVerticalBlankHandlers();
extern void F1670_ExecuteTimerDHandlers();
extern FTL_LIBRARY* F1682_();

extern FTL_LIBRARY* G9015_INT1base;

struct {
        void* Functions[6];
        INT1_FTL_LIBRARY Data;
} G3430_s_INT1Library = {
        { (void*)F1681_INT1_06_,        /* 06 -$18 */
          (void*)F1680_INT1_05_UninstallInterruptHandler,        /* 05 -$14 */
          (void*)F1679_INT1_04_InstallInterruptHandler,        /* 04 -$10 */
          (void*)F1683_INT1_03_Expunge, /* 03 -$0C */
          NULL,                         /* 02 -$08 */
          NULL                          /* 01 -$04 */
} };

/*FTL_LIBRARY* G9004_PRIMbase;*/            /* PRIM data here! */
extern INT1_HANDLER* G3433_aps_VerticalBlankHandlers[8];
extern INT1_HANDLER* G3434_aps_TimerDHandlers[6];
extern int16_t G3435_i_VerticalBlankHandlerCount;
extern int16_t G3436_i_TimerDHandlerCount;
extern int16_t G3437_i_SampleHandlerCount;
extern BOOLEAN G3438_B_VerticalBlankHandlerManagerInstalled;
extern BOOLEAN G3439_B_TimerDHandlerManagerInstalled;
extern BOOLEAN G3440_B_SampleHandlerManagerInstalled;


void main(P5007_l_)
long P5007_l_;
{
        G3430_s_INT1Library.Data.FTLLibrary.FTLNode.Name = FTL_INT1;
        G3430_s_INT1Library.Data.FTLLibrary.Version = 3;
        G3430_s_INT1Library.Data.FTLLibrary.Unused1 = 0;
        F1682_(P5007_l_, &G3430_s_INT1Library.Data.FTLLibrary);
}

void F1665_InstallVerticalBlankHandlerManager(
)
{
        if (G3438_B_VerticalBlankHandlerManagerInstalled) {
                return;
        }
        G3438_B_VerticalBlankHandlerManagerInstalled = C1_TRUE;
        G3435_i_VerticalBlankHandlerCount = 0;
        asm {
                        lea     @Tdword_2A0,a0
                        move.l  a5,(a0)
                        lea     @Tloc_28E,a1
                        moveq   #1,d1
                        moveq   #0x6C,d0        /* VDISPST vertical synchronous interrupt setting */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        tst.l   d0
                        beq.s   @Tlocret_2A4
                        movea.l #0,a1
                        moveq   #1,d1
                        moveq   #0x6C,d0        /* VDISPST vertical synchronous interrupt setting */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        lea     @Tloc_28E,a1
                        moveq   #1,d1
                        moveq   #0x6C,d0        /* VDISPST vertical synchronous interrupt setting */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        bra.s   @Tlocret_2A4
                @Tloc_28E:
                        movem.l d0-d7/a0-a6,-(sp)
                        movea.l @Tdword_2A0,a5
                        jsr     F1667_ExecuteVerticalBlankHandlers
                        movem.l (sp)+,d0-d7/a0-a6
                        rte
                @Tdword_2A0:
                        dc.l 0
                @Tlocret_2A4:
        }
}

void F1666_UninstallVerticalBlankHandlerManager(
)
{
        if (!G3438_B_VerticalBlankHandlerManagerInstalled) {
                return;
        }
        asm {
                movea.l #0,a1
                moveq   #1,d1
                moveq   #0x6C,d0        /* VDISPST vertical synchronous interrupt setting */
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
        }
        G3435_i_VerticalBlankHandlerCount = 0;
        G3438_B_VerticalBlankHandlerManagerInstalled = C0_FALSE;
}

void F1667_ExecuteVerticalBlankHandlers(
)
{
        asm {
                        move.w  G3435_i_VerticalBlankHandlerCount(a5),d0
                        beq.s   @Tlocret_2E4
                        lea     G3433_aps_VerticalBlankHandlers(a5),a0
                        bra.s   @Tloc_2E0
                @Tloc_2CE:
                        movea.l (a0)+,a1
                        subq.l  #1,0xC(a1)      /* CurrentInterruptCount-- */
                        bne.s   @Tloc_2E0
                        move.l  8(a1),0xC(a1)   /* RunEveryInterruptCount = CurrentInterruptCount */
                        movea.l (a1),a1         /* Code */
                        jsr     (a1)            /* Call handler */
                @Tloc_2E0:
                        dbf     d0,@Tloc_2CE
                @Tlocret_2E4:
        }
}

void F1668_InstallTimerDHandlerManager(
)
{
        if (G3439_B_TimerDHandlerManagerInstalled) {
                return;
        }
        G3439_B_TimerDHandlerManagerInstalled = C1_TRUE;
        G3436_i_TimerDHandlerCount = 0;
        asm {
                        lea     @TA5Backup,a0
                        move.l  a5,(a0)
                        lea     @Tloc_324,a1
                        move.w  #0x714,d1
                        moveq   #0x6B,d0        /* TIMERDST Timer-D interrupt setting */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        tst.l   d0
                        beq.s   @Tlocret_33A
                        movea.l #0,a1
                        moveq   #0x6B,d0        /* TIMERDST Timer-D interrupt setting */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        lea     @Tloc_324,a1
                        move.w  #0x714,d1
                        moveq   #0x6B,d0        /* TIMERDST Timer-D interrupt setting */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        bra.s   @Tlocret_33A
                @Tloc_324:
                        movem.l d0-d7/a0-a6,-(sp)
                        movea.l @TA5Backup,a5
                        jsr     F1670_ExecuteTimerDHandlers
                        movem.l (sp)+,d0-d7/a0-a6
                        rte
                @TA5Backup:
                        dc.l 0
                @Tlocret_33A:
        }
}

void F1669_UninstallTimerDHandlerManager(
)
{
        if (!G3439_B_TimerDHandlerManagerInstalled) {
                return;
        }
        asm {
                movea.l #0,a1
                moveq   #0x6B,d0        /* VDISPST vertical synchronous interrupt setting */
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
        }
        G3436_i_TimerDHandlerCount = 0;
        G3439_B_TimerDHandlerManagerInstalled = C0_FALSE;
}

void F1670_ExecuteTimerDHandlers(
)
{
        int16_t L5065_;


        if (G3436_i_TimerDHandlerCount <= 0) {
                return;
        }
        for (L5065_ = 0; L5065_ < G3436_i_TimerDHandlerCount; L5065_++) {
                if (--G3434_aps_TimerDHandlers[L5065_]->RunEveryInterruptCount > 0) {
                        continue;
                }
                G3434_aps_TimerDHandlers[L5065_]->RunEveryInterruptCount = G3434_aps_TimerDHandlers[L5065_]->CurrentInterruptCount;
                G3434_aps_TimerDHandlers[L5065_]->Code();
        }
}

void F1671_InstallSampleHandlerManager(
)
{
        if (G3440_B_SampleHandlerManagerInstalled) {
                return;
        }
        G3440_B_SampleHandlerManagerInstalled = C1_TRUE;
        G3437_i_SampleHandlerCount = 0;
}

void F1672_UninstallSampleHandlerManager(
)
{
        if (!G3440_B_SampleHandlerManagerInstalled) {
                return;
        }
        G3437_i_SampleHandlerCount = 0;
        G3440_B_SampleHandlerManagerInstalled = C0_FALSE;
}

void F1673_ExecuteSampleHandlers_Unreferenced(
)
{
        int16_t L5066_Unreferenced;


        asm{
                tst.w   -0xC(a5)
        }
}

int16_t F1674_AddInterruptHandler(P5013_ps_Handler, P5014_i_InterruptHandlerCount, P5015_i_MaximumHandlerCount, P5016_pps_Handlers)
INT1_HANDLER* P5013_ps_Handler;
int16_t* P5014_i_InterruptHandlerCount;
int16_t P5015_i_MaximumHandlerCount;
INT1_HANDLER** P5016_pps_Handlers;
{
        long L5070_l_SupervisorStack;
        int16_t L5071_;
        int16_t L5072_;


        L5072_ = 1;
        if (*P5014_i_InterruptHandlerCount < P5015_i_MaximumHandlerCount) {
                asm {
                        clr.l   -(sp)
                        dc.w    0xFF20  /* DOS CALL SUPER */
                        addq.l  #4,sp
                        move.l  d0,L5070_l_SupervisorStack(a6)
                        move    #0x2700,sr
                }
                if (P5013_ps_Handler->CurrentInterruptCount < 1) {
                        P5013_ps_Handler->CurrentInterruptCount = 1;
                }
                P5013_ps_Handler->RunEveryInterruptCount = P5013_ps_Handler->CurrentInterruptCount;
                for (L5071_ = 0; L5071_ < *P5014_i_InterruptHandlerCount; L5071_++) {
                        if (P5016_pps_Handlers[L5071_] == P5013_ps_Handler) {
                                if (P5016_pps_Handlers[L5071_]->Code == P5013_ps_Handler->Code) {
                                        L5072_ = 3;
                                        goto loc_4B4;
                                }
                        }
                }
                P5016_pps_Handlers[*P5014_i_InterruptHandlerCount] = P5013_ps_Handler;
                (*P5014_i_InterruptHandlerCount)++;
                L5072_ = 0;
                loc_4B4:
                asm {
                        move    #0x2000,sr
                        move.l  L5070_l_SupervisorStack(a6),-(sp)
                        dc.w    0xFF20  /* DOS CALL SUPER */
                        addq.l  #4,sp
                }
        }
        return L5072_;
}

int16_t F1675_InstallInterruptHandler(P5012_ps_Handler)
INT1_HANDLER* P5012_ps_Handler;
{
        int16_t L5069_;


        switch (P5012_ps_Handler->Type) {
                case 0:
                        F1665_InstallVerticalBlankHandlerManager();
                        L5069_ = F1674_AddInterruptHandler(P5012_ps_Handler, &G3435_i_VerticalBlankHandlerCount, 8, G3433_aps_VerticalBlankHandlers);
                        break;
                case 1:
                        F1668_InstallTimerDHandlerManager();
                        L5069_ = F1674_AddInterruptHandler(P5012_ps_Handler, &G3436_i_TimerDHandlerCount, 4, G3434_aps_TimerDHandlers);
                        break;
                case 2:
                        F1671_InstallSampleHandlerManager();
                        L5069_ = 2;
        }
        return L5069_;
}

int16_t F1676_UninstallInterruptHandler(P5017_ps_Handler, P5018_pi_HandlerCount, P5019_pps_Handlers)
INT1_HANDLER* P5017_ps_Handler;
int16_t* P5018_pi_HandlerCount;
INT1_HANDLER** P5019_pps_Handlers;
{
        int16_t L5073_;
        int16_t L5074_;
        long L5075_l_SupervisorStack;


        L5074_ = 0;
        if (*P5018_pi_HandlerCount > 0) {
                asm {
                        clr.l   -(sp)
                        dc.w    0xFF20  /* DOS CALL SUPER */
                        addq.l  #4,sp
                        move.l  d0,L5075_l_SupervisorStack(a6)
                        move    #0x2700,sr
                }
                for (L5073_ = 0; L5073_ < *P5018_pi_HandlerCount; L5073_++) {
                        if (P5019_pps_Handlers[L5073_] == P5017_ps_Handler) {
                                break;
                        }
                }
                if (P5019_pps_Handlers[L5073_] == P5017_ps_Handler) {
                        P5019_pps_Handlers[L5073_] = NULL;
                        (*P5018_pi_HandlerCount)--;
                        P5019_pps_Handlers[L5073_] = P5019_pps_Handlers[*P5018_pi_HandlerCount];
                        L5074_ = 1;
                }
                asm {
                        move    #0x2000,sr
                        move.l  L5075_l_SupervisorStack(a6),-(sp)
                        dc.w    0xFF20  /* DOS CALL SUPER */
                        addq.l  #4,sp
                }
        }
        return L5074_;
}

int16_t F1677_UninstallInterruptHandler(P5011_ps_Handler)
INT1_HANDLER* P5011_ps_Handler;
{
        int16_t L5068_;


        switch (P5011_ps_Handler->Type) {
                case 0:
                        L5068_ = F1676_UninstallInterruptHandler(P5011_ps_Handler, &G3435_i_VerticalBlankHandlerCount, G3433_aps_VerticalBlankHandlers);
                        break;
                case 1:
                        L5068_ = F1676_UninstallInterruptHandler(P5011_ps_Handler, &G3436_i_TimerDHandlerCount, G3434_aps_TimerDHandlers);
                        break;
                case 2:
                        L5068_ = 0;
        }
        return L5068_;
}

void F1678_UninstallAllHandlerManagers(
)
{
        F1666_UninstallVerticalBlankHandlerManager();
        F1669_UninstallTimerDHandlerManager();
        F1672_UninstallSampleHandlerManager();
}

int16_t F1679_INT1_04_InstallInterruptHandler(P5008_ps_Handler)
INT1_HANDLER* P5008_ps_Handler;
{
        int16_t L5067_;


        ASM_STUB_PREFIX
        L5067_ = F1675_InstallInterruptHandler(P5008_ps_Handler);
        ASM_STUB_SUFFIX
        return L5067_;
}

void F1680_INT1_05_UninstallInterruptHandler(P5020_ps_Handler)
INT1_HANDLER* P5020_ps_Handler;
{
        ASM_STUB_PREFIX
        F1677_UninstallInterruptHandler(P5020_ps_Handler);
        ASM_STUB_SUFFIX
}

void F1681_INT1_06_(P5021_ps_Handler)
INT1_HANDLER* P5021_ps_Handler;
{
        ASM_STUB_PREFIX
        ASM_STUB_SUFFIX
}

FTL_LIBRARY* F1682_(P5009_, P5010_)
long P5009_;
FTL_LIBRARY* P5010_;
{
        if (F9008_OpenPRIM(P5009_, 2) == NULL) {
                return NULL;
        }
        G9015_INT1base = P5010_;
        G3440_B_SampleHandlerManagerInstalled = C0_FALSE;
        G3439_B_TimerDHandlerManagerInstalled = C0_FALSE;
        G3438_B_VerticalBlankHandlerManagerInstalled = C0_FALSE;
        return P5010_;
}

void F1683_INT1_03_Expunge(
)
{
        ASM_STUB_PREFIX
        F1678_UninstallAllHandlerManagers();
        F9009_ClosePRIM();
        ASM_STUB_SUFFIX
}
#endif
