#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA637_A31E_A31M_A35E_X31J
extern FTL_LIBRARY* F9008_OpenPRIM();
extern FTL_LIBRARY* F9006_OpenMEM1();
extern FTL_LIBRARY* F9000_OpenGRF1();
extern FTL_LIBRARY* F9018_OpenINT1();
extern unsigned char* MEM1_06_Allocate();
extern void F1659_();
extern void F1660_();
extern BOOLEAN F1661_();

FTL_LIBRARY* G9017_MEM1_Library;
GRF1_FTL_LIBRARY* G3135_GRF1_Library;
#ifdef MEDIA636_A31E_A31M_A35E
DRIA_Video* G9019_ps_MsgPort_FTL_DRIA_Video;
unsigned int16_t* G3249_pui_BlitterTemporaryBitplane;
#endif
#ifdef MEDIA692_X31J
FTL_LIBRARY* G3137_INT1_Library;
#endif
VDEO_FTL_LIBRARY* G9018_VDEObase2;
#ifdef MEDIA692_X31J
long* G3410_pl_VerticalBlankCount;
BOOLEAN G3409_B_X68000_24KHzVideoMode;
long G3414_;
long G3417_;
long G3418_;
long* G3412_;
long G3428_Unreferenced;
long* G3413_;
#endif
unsigned char* G9031_apuc_Bitmap_Screens[M787_SCREEN_COUNT];
#ifdef MEDIA692_X31J
unsigned char* G3411_;
#endif
char G9032_[32][4] = {
        {  0, 0, 0, 0 },
        {  1, 0, 0, 0 },
        {  2, 0, 0, 0 },
        {  3, 0, 0, 0 },
        {  4, 0, 0, 0 },
        {  5, 0, 0, 0 },
        {  6, 0, 0, 0 },
        {  7, 0, 0, 0 },
        {  8, 0, 0, 0 },
        {  9, 0, 0, 0 },
        { 10, 0, 0, 0 },
        { 11, 0, 0, 0 },
        { 12, 0, 0, 0 },
        { 13, 0, 0, 0 },
        { 14, 0, 0, 0 },
        { 15, 0, 0, 0 },
        { 16, 0, 0, 0 },
        { 17, 0, 0, 0 },
        { 18, 0, 0, 0 },
        { 19, 0, 0, 0 },
        { 20, 0, 0, 0 },
        { 21, 0, 0, 0 },
        { 22, 0, 0, 0 },
        { 23, 0, 0, 0 },
        { 24, 0, 0, 0 },
        { 25, 0, 0, 0 },
        { 26, 0, 0, 0 },
        { 27, 0, 0, 0 },
        { 28, 0, 0, 0 },
        { 29, 0, 0, 0 },
        { 30, 0, 0, 0 },
        { 31, 0, 0, 0 } };
#ifdef MEDIA692_X31J
int16_t G3415_[8] = { 58, 7, 10, 50, 468, 0, 20, 468 };
int16_t G3416_[9] = { 91, 9, 20, 77, 567, 5, 40, 552, 27 };
#endif


#ifdef MEDIA692_X31J
long F9043_VDEO_04_GetBitmapByteCount(
int16_t P2932_ui_Width,
int16_t P2933_i_Height)
{
        ASM_STUB_PREFIX
        ASM_STUB_SUFFIX
        return (long)(P2932_ui_Width << 1) * P2933_i_Height;
}
#endif

void F9045_VDEO_19_Operation(
#ifdef MEDIA636_A31E_A31M_A35E
DRIA_Video** P2934_)
#endif
#ifdef MEDIA692_X31J
int16_t P5005_i_Operation,
BOOLEAN* P5006_B_24KHzVideoMode)
#endif
{
        ASM_STUB_PREFIX
#ifdef MEDIA636_A31E_A31M_A35E
        *P2934_ = G9019_ps_MsgPort_FTL_DRIA_Video;
#endif
#ifdef MEDIA692_X31J
        asm {
                        lea     @Tbyte_724,a1
                        moveq   #0x21,d0        /* B_PRINT string display */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        bra.s   @Tloc_74E
                @Tbyte_724:                       /* String of 40 line feed (clear text screen) */
                        dc.b 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA
                        dc.b 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA
                        dc.b 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA
                        dc.b 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA, 0xA
                        dc.b 0xD, 0
                @Tloc_74E:
                        move.w  #0,d1
                        move.w  #0,d2
                        moveq   #0x23,d0        /* B_LOCATE cursor movement */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
        }
        switch (P5005_i_Operation) {
                case 1:
                        F1662_InstallVerticalBlankInterruptHandler();
                        break;
                case 2:
                        F1663_UninstallVerticalBlankInterruptHandler();
                        break;
                case 3:
                        *P5006_B_24KHzVideoMode = G3409_B_X68000_24KHzVideoMode;
                        break;
                default:
                        break;
        }
#endif
        ASM_STUB_SUFFIX
}

#ifdef MEDIA692_X31J
void F1658_IsShiftKeyPressed_Unrerefenced(
)
{
        asm {
                        moveq   #2,d0           /* B_SFTSNS shift key state */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        andi.w  #1,d0
                        beq.s   @Tloc_796
                        moveq   #1,d0
                        bra.s   @Tlocret_798
                @Tloc_796:
                        moveq   #0,d0
                @Tlocret_798:
        }
}
#endif

VDEO_FTL_LIBRARY* F9046_(
long P2853_,
register VDEO_FTL_LIBRARY* P2854_)
{
#ifdef MEDIA692_X31J
        long L5021_;
        char L5022_;


#endif
        if (F9008_OpenPRIM(P2853_, 4) == NULL) {
                return 0;
        }
        if ((G9017_MEM1_Library = F9006_OpenMEM1(1)) == NULL) {
                return 0;
        }
        if ((G3135_GRF1_Library = F9000_OpenGRF1(2)) == NULL) {
                return 0;
        }
#ifdef MEDIA692_X31J
        if ((G3137_INT1_Library = F9018_OpenINT1(2)) == NULL) {
                return 0;
        }
#endif
#ifdef MEDIA636_A31E_A31M_A35E
        F1088_OpenAmigaStuff();
#endif
        G9002_VDEObase = P2854_;
        G9018_VDEObase2 = P2854_;
#ifdef MEDIA636_A31E_A31M_A35E
        P2854_->_w_0x36_BaseLineStridePixelCount = G3135_GRF1_Library->_w_0x36_BaseLineStridePixelCount;
#endif
#ifdef MEDIA692_X31J
        P2854_->_w_0x36_BaseLineStridePixelCount = 1;
#endif
        P2854_->_w_0x38 = 0;
        P2854_->_w_0x3A = 1;
        P2854_->_w_0x3C_ScreenWidth = 320;
        P2854_->_w_0x3E_ScreenHeight = 200;
#ifdef MEDIA636_A31E_A31M_A35E
        P2854_->_l_0x40 = 16;
        P2854_->_w_0x44 = 40;
        P2854_->_l_0x46 = 32000;
        P2854_->_w_0x4A = 64;
        P2854_->_w_0x4C = 1;
#endif
#ifdef MEDIA692_X31J
        P2854_->_l_0x40 = 32;
        P2854_->_w_0x44 = 1024;
        P2854_->_l_0x46 = 0xFFFFF400;
        P2854_->_w_0x4A = 32;
        P2854_->_w_0x4C = 2;
#endif
        P2854_->_w_0x4E_PhysicalScreenOrdinal = 1;
        P2854_->_pc_0x50 = &G9031_apuc_Bitmap_Screens;
        P2854_->_b_0x54_ColorCount = 32;
        P2854_->_pc_0x56_ColorComponents = &G9032_;
#ifdef MEDIA636_A31E_A31M_A35E
        GRF1_15_GetBlitterTemporaryBitplane(&G3249_pui_BlitterTemporaryBitplane);
        F9071_();
        G9031_apuc_Bitmap_Screens[0] = G9019_ps_MsgPort_FTL_DRIA_Video->_puc_0x134_Buffer32320Bytes;
#endif
#ifdef MEDIA692_X31J
        P2854_->_l_0x5A_VerticalBlankCount = 0;
        G3410_pl_VerticalBlankCount = &P2854_->_l_0x5A_VerticalBlankCount;
        if (F1661_() == 0) {
                return 0;
        }
        G3409_B_X68000_24KHzVideoMode = C0_FALSE;
        if ((PRIM_17_File_Open("VIDEO.PRM", &L5021_) & 0xFF) == 0) {
                if ((PRIM_20_File_Read(L5021_, 1L, &L5022_) & 0xFF) == 0) {
                        if (L5022_ == '1') {
                                G3409_B_X68000_24KHzVideoMode = C1_TRUE;
                        }
                }
                PRIM_18_File_Close(L5021_);
        }
        if (G3409_B_X68000_24KHzVideoMode) {
                G9031_apuc_Bitmap_Screens[0] = (unsigned char*)0x00C00000;
                G3411_ = (unsigned char*)0x00C80000;
        } else {
                G9031_apuc_Bitmap_Screens[0] = (unsigned char*)0x00C07090;
                G3411_ = (unsigned char*)0x00C87090;
        }
        F1659_();
#endif
        F9028_();
        return P2854_;
}

void F9047_VDEO_03_Expunge(
)
{
        ASM_STUB_PREFIX
#ifdef MEDIA636_A31E_A31M_A35E
        if (G9019_ps_MsgPort_FTL_DRIA_Video) {
                VDEO_15_ApplyNormalColorPalette(G9002_VDEObase->_b_0x54_ColorCount, G9002_VDEObase->_pc_0x56_ColorComponents, 0);
        }
        F9072_();
        F1089_CloseAmigaStuff();
#endif
#ifdef MEDIA692_X31J
        if (G3412_) {
                MEM1_07_Free(G3412_);
        }
        if (G3413_) {
                MEM1_07_Free(G3413_);
        }
        F1660_();
        F1663_UninstallVerticalBlankInterruptHandler();
        F9019_CloseINT1(G3137_INT1_Library);
#endif
        F9001_CloseGRF1(G3135_GRF1_Library);
        F9007_CloseMEM1(G9017_MEM1_Library);
        F9009_ClosePRIM();
        ASM_STUB_SUFFIX
}

#ifdef MEDIA692_X31J
void F1659_(
)
{
        asm {
                moveq   #0,d1
                moveq   #0x7D,d0        /* SKEY_MOD */
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                moveq   #0xE,d0         /* TPALET2 */
                moveq   #1,d1
                moveq   #2,d2
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                move.l  d0,G3414_(a5)
                moveq   #0x10,d0        /* CRTMOD */
                moveq   #8,d1           /* Mode: 512x512 pixels, 16 text colors, 256 graphics colors, 31 KHz */
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                clr.l   -(sp)
                dc.w    0xFF20          /* DOS CALL SUPER */
                movea.l #0xE80000,a1
                cmpi.w  #1,G3409_B_X68000_24KHzVideoMode(a5)
                bne.s   @Tloc_9A8
                lea     G3415_(a5),a0
                moveq   #7,d1
@Tloc_988:
                move.w  (a0)+,(a1)+
                dbf     d1,@Tloc_988
                move.w  #0x110,(0xE80028).l
                move.w  #0xE,(0xEB080C).l
                move.w  #0x25,(0xEB080E).l
                bra.s   @Tloc_9BC
@Tloc_9A8:
                lea     G3416_(a5),a0
                moveq   #8,d1
@Tloc_9AE:
                move.w  (a0)+,(a1)+
                dbf     d1,@Tloc_9AE
                move.w  #0x111,(0xE80028).l
@Tloc_9BC:
                move.l  d0,-(sp)
                dc.w    0xFF20          /* DOS CALL SUPER */
                addq.l  #4,sp
                move.l  #0x90,d0        /* G_CLR_ON */
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                move.w  #3,-(sp)
                move.w  #0xE,-(sp)
                dc.w    0xFF23          /* DOS CALL CONCTRL */
                addq.l  #4,sp
                move.w  #0x12,-(sp)
                dc.w    0xFF23          /* DOS CALL CONCTRL */
                addq.l  #2,sp
        }
}

void F1660_(
)
{
        asm {
                moveq   #0x10,d0        /* CRTMOD */
                moveq   #0x10,d1        /* Mode: 768*512, 16 text colors, 16 graphics colors, 31 KHz*/
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                move.l  #0x90,d0        /* G_CLR_ON */
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
                move.w  #3,-(sp)
                move.w  #0xE,-(sp)
                dc.w    0xFF23          /* DOS CALL CONCTRL */
                addq.l  #4,sp
                move.w  #0x11,-(sp)
                dc.w    0xFF23          /* DOS CALL CONCTRL */
                addq.l  #2,sp
                moveq   #0xE,d0         /* TPALET2 */
                moveq   #1,d1
                move.l  G3414_(a5),d2
                trap    #0xF            /* TRAP 15 (IOCS CALL) */
        }
}

BOOLEAN F1661_(
)
{
        register unsigned char L5026_;
        register int16_t L5027_Unreferenced;
        register int16_t L5028_Unreferenced;
        register int16_t L5029_Unreferenced;
        register int16_t L5030_Unreferenced;
        char L5023_[32];
        long L5024_;
        long L5025_;


        GRF1_15_GetBlitterTemporaryBitplane(&L5025_);
        if ((G3417_ = L5025_) != 0) {
                if ((G3418_ = L5024_) != 0) {
                        if (G3412_ = (long*)MEM1_06_Allocate(1024L, MASK0x0400_MEMREQ)) {
                                L5026_ = 0;
                                do {
                                        G3412_[L5026_] = ((long)(L5026_ & 0xF0) << 12) | (long)(L5026_ & 0x0F);
                                } while (L5026_++ != 255);
                                return C1_TRUE;
                        }
                }
        }
        return C0_FALSE;
}
#endif
#endif
