#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA595_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_X31J_P31J
#ifdef MEDIA463_P20JA_P20JB_I34E_I34M_P31J
unsigned char* G0348_Bitmap_Screen = NULL;
#ifdef MEDIA480_P20JB_I34E_I34M_P31J
char* G2059_pc_Unused = NULL;
char* G2060_pc_Unused = NULL;
#endif
int16_t G2123_ = -1;
int16_t G2061_DungeonViewPaletteIndices[6] = { 0, 1, 2, 3, 4, 5 };

VIDEO_DRIVER* G2156_VideoDriver;
#ifdef MEDIA707_I34E_I34M
unsigned int16_t G2157_;
#endif
char G2208_Unreferenced[4];
unsigned char G2158_auc_Bitmap_PixelLine[160];
unsigned char* G2159_puc_Bitmap_Source;
unsigned char* G2160_puc_Bitmap_Destination;


#include "IMAGE3.C"
#include "FILLBOX.C"
#include "BLIT.C"
#include "EXPAND.C"
#endif

#ifdef MEDIA577_X30J
long F1027_Supervisor(long P2779_l_) {
        asm {
                move.l  P2779_l_(a6),-(sp)
                dc.w    0xFF20  /* DOS CALL SUPER */
                addq.l  #4,sp
        }
}
#endif

void F0693_WaitVerticalBlank(
)
{
#ifdef MEDIA463_P20JA_P20JB_I34E_I34M_P31J
        (*(G2156_VideoDriver->VIDRV_07_WaitVerticalBlank))();
#endif
#ifdef MEDIA607_X30J_X31J
        long L2771_l_;


        L2771_l_ = G0317_i_WaitForInputVerticalBlankCount;
        while (G0317_i_WaitForInputVerticalBlankCount == L2771_l_);
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        WaitTOF();
#endif
}

#ifdef MEDIA607_X30J_X31J
void F1028_FillX68000LargeScreenBlack(
)
{
#ifdef MEDIA577_X30J
        unsigned char* L2772_puc_;


        L2772_puc_ = G3090_X68000VideoMemoryAddress;
        G3090_X68000VideoMemoryAddress = (unsigned char*)0xC00000;
        F0732_FillScreenArea(G3042_ai_XYZ_X68000Screen, C00_COLOR_BLACK);
        G3090_X68000VideoMemoryAddress = L2772_puc_;
#endif
#ifdef MEDIA692_X31J
        register unsigned char** L2773_ppuc_;
        register unsigned char* L2774_puc_;


        if (!G3076_B_) {
                F0733_FillZoneByIndex(C002_ZONE_SCREEN, C00_COLOR_BLACK);
        } else {
                L2773_ppuc_ = G9002_VDEObase->_pc_0x50 + (G9002_VDEObase->_w_0x4E_PhysicalScreenOrdinal - 1);
                L2774_puc_ = *L2773_ppuc_;
                *L2773_ppuc_ = (unsigned char*)0xC00000;
                F0732_FillScreenArea(G3042_ai_XYZ_X68000Screen, C00_COLOR_BLACK);
                *L2773_ppuc_ = L2774_puc_;
        }
#endif
}

void F1010_LoadX68000BorderGraphics(
)
{
        register int16_t L2775_i_;
        register long L2776_l_;
#ifdef MEDIA692_X31J
        register unsigned char** L2777_ppuc_;
#endif
        register unsigned char* L2778_puc_;
        unsigned char* L2779_apuc_[4];


#ifdef MEDIA577_X30J
        L2778_puc_ = G3090_X68000VideoMemoryAddress;
#endif
#ifdef MEDIA692_X31J
        if (!G3076_B_) {
                return;
        }
#endif
        L2776_l_ = 0;
        for (L2775_i_ = 0; L2775_i_ < 4; L2775_i_++) {
                L2776_l_ += F0440_STARTEND_GetTemporarilyLoadedGraphicByteCount(M723_X68000_BORDER_TOP + L2775_i_, &L2779_apuc_[L2775_i_]);
        }
#ifdef MEDIA577_X30J
        G3090_X68000VideoMemoryAddress = (unsigned char*)0xC00000;
#endif
#ifdef MEDIA692_X31J
        L2777_ppuc_ = G9002_VDEObase->_pc_0x50 + (G9002_VDEObase->_w_0x4E_PhysicalScreenOrdinal - 1);
        L2778_puc_ = *L2777_ppuc_;
        *L2777_ppuc_ = (unsigned char*)0xC00000;
#endif
        for (L2775_i_ = 0; L2775_i_ < 4; L2775_i_++) {
#ifdef MEDIA577_X30J
                F0466_EXPAND_GraphicToBitmap(L2779_apuc_[L2775_i_], G0296_puc_Bitmap_Viewport, 0, 0);
#endif
#ifdef MEDIA692_X31J
                F0466_EXPAND_GraphicToBitmap(L2779_apuc_[L2775_i_], G0296_puc_Bitmap_Viewport);
#endif
                F0007_MAIN_CopyBytes(L2779_apuc_[L2775_i_], G0296_puc_Bitmap_Viewport - 4, (long)4);
                F0021_MAIN_BlitToScreen(G0296_puc_Bitmap_Viewport, C420_ZONE_X68000_BORDER + L2775_i_, CM1_COLOR_NO_TRANSPARENCY);
        }
#ifdef MEDIA577_X30J
        G3090_X68000VideoMemoryAddress = L2778_puc_;
#endif
#ifdef MEDIA692_X31J
        *L2777_ppuc_ = L2778_puc_;
#endif
        F0469_MEMORY_FreeAtHeapTop(L2776_l_);
}
#endif

#ifdef MEDIA463_P20JA_P20JB_I34E_I34M_P31J
void F0694_SetMultipleColorsInPalette(int16_t P2368_i_PaletteIndex)
{
        (*(G2156_VideoDriver->VIDRV_08_SetMultipleColorsInPalette))(P2368_i_PaletteIndex);
}

void F1012_PALETTE_SetCurtain(unsigned int16_t P2369_ui_)
{
        (*(G2156_VideoDriver->VIDRV_11_PALETTE_SetCurtain))((unsigned char)P2369_ui_);
}

void F0695_SetCreatureReplacementColors(int P2370_i_ReplacedColor, int P2371_i_ReplacementColorSetIndex)
{
        (*(G2156_VideoDriver->VIDRV_12_SetCreatureReplacementColors))(P2370_i_ReplacedColor, P2371_i_ReplacementColorSetIndex);
        G2123_ = -1;
}

#include "DRAWVIEW.C"
#include "DRAWMSGA.C"

void F0697_HatchScreenBox(int16_t* P2372_pi_XYZ, unsigned int16_t P2373_ui_Color)
{
#ifdef MEDIA458_P20JA_P20JB
        (*(G2156_VideoDriver->VIDRV_06_HatchScreenBox))(M704_ZONE_LEFT(P2372_pi_XYZ), M705_ZONE_RIGHT(P2372_pi_XYZ), M706_ZONE_TOP(P2372_pi_XYZ), M707_ZONE_BOTTOM(P2372_pi_XYZ));
#endif
#ifdef MEDIA709_I34E_I34M_P31J
        int16_t L2780_i_X1;
        int16_t L2781_i_X2;
        int16_t L2782_i_Y1;
        int16_t L2783_i_Y2;


        L2780_i_X1 = M704_ZONE_LEFT(P2372_pi_XYZ);
        L2781_i_X2 = M705_ZONE_RIGHT(P2372_pi_XYZ);
        L2782_i_Y1 = M706_ZONE_TOP(P2372_pi_XYZ);
        L2783_i_Y2 = M707_ZONE_BOTTOM(P2372_pi_XYZ);
        (*(G2156_VideoDriver->VIDRV_06_HatchScreenBox))(L2780_i_X1, L2781_i_X2, L2782_i_Y1, L2783_i_Y2);
#endif
}
#endif

#ifdef MEDIA579_X30J_P20JA_P20JB
void F0698_InvertBox(int16_t P2377_i_X1, int16_t P2378_i_X2, int16_t P2379_i_Y1, int16_t P2380_i_Y2)
{
#ifdef MEDIA458_P20JA_P20JB
        (*(G2156_VideoDriver->VIDRV_05_InvertBox))(P2377_i_X1, P2378_i_X2, P2379_i_Y1, P2380_i_Y2);
#endif
#ifdef MEDIA577_X30J
        long L2784_l_SupervisorStack;


        L2784_l_SupervisorStack = F1027_Supervisor(0L);
        asm {
                        move.w  d3,-(sp)
                        move.w  P2379_i_Y1(a6),d0
                        move.w  P2380_i_Y2(a6),d1
                        sub.w   d0,d1
                        movea.l G3090_X68000VideoMemoryAddress(a5),a0
                        move.w  G3089_(a5),d2
                        mulu.w  d2,d0
                        adda.l  d0,a0
                        move.w  P2377_i_X1(a6),d0
                        move.w  P2378_i_X2(a6),d3
                        sub.w   d0,d3
                        sub.w   d3,d2
                        sub.w   d3,d2
                        subq.w  #2,d2
                        movea.w d2,a1
                        adda.w  d0,a0
                        adda.w  d0,a0
                        moveq   #4,d2
                @Tloc_1CB0E:
                        move.w  d3,d0
                @Tloc_1CB10:
                        tst.w   (a0)
                        bne.s   @Tloc_1CB1C
                        move.w  d2,(a0)+
                        dbf     d0,@Tloc_1CB10
                        bra.s   @Tloc_1CB22
                @Tloc_1CB1C:
                        clr.w   (a0)+
                        dbf     d0,@Tloc_1CB10
                @Tloc_1CB22:
                        adda.w  a1,a0
                        dbf     d1,@Tloc_1CB0E
                        move.w  (sp)+,d3
        }
        F1027_Supervisor(L2784_l_SupervisorStack);
#endif
}
#endif
#ifdef MEDIA722_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_X31J_P31J
void F0698_InvertBox(int16_t* P2374_pi_XYZ)
{
#ifdef MEDIA692_X31J
        long L2784_l_SupervisorStack;


        asm {
                        clr.l   -(sp)
                        dc.w    0xFF20  /* DOS CALL SUPER */
                        addq.l  #4,sp
                        move.l  d0,L2784_l_SupervisorStack(a6)
                        move.w  d3,-(sp)
                        movea.l P2374_pi_XYZ(a6),a1
                        move.w  2(a1),d0
                        move.w  6(a1),d1
                        subq.w  #1,d1
                        movea.l G3090_X68000VideoMemoryAddress(a5),a0
                        move.w  G3089_(a5),d2
                        mulu.w  d2,d0
                        adda.l  d0,a0
                        move.w  (a1),d0
                        move.w  4(a1),d3
                        subq.w  #1,d3
                        sub.w   d3,d2
                        sub.w   d3,d2
                        subq.w  #2,d2
                        movea.w d2,a1
                        adda.w  d0,a0
                        adda.w  d0,a0
                        moveq   #4,d2
                @Tloc_1D92A:
                        move.w  d3,d0
                @Tloc_1D92C:
                        tst.w   (a0)
                        bne.s   @Tloc_1D938
                        move.w  d2,(a0)+
                        dbf     d0,@Tloc_1D92C
                        bra.s   @Tloc_1D93E
                @Tloc_1D938:
                        clr.w   (a0)+
                        dbf     d0,@Tloc_1D92C
                @Tloc_1D93E:
                        adda.w  a1,a0
                        dbf     d1,@Tloc_1D92A
                        move.w  (sp)+,d3
                        move.l  L2784_l_SupervisorStack(a6),-(sp)
                        dc.w    0xFF20  /* DOS CALL SUPER */
                        addq.l  #4,sp
        }
#endif
#ifdef MEDIA709_I34E_I34M_P31J
        int16_t L2785_i_X1;
        int16_t L2786_i_X2;
        int16_t L2787_i_Y1;
        int16_t L2788_i_Y2;


        L2785_i_X1 = M704_ZONE_LEFT(P2374_pi_XYZ);
        L2786_i_X2 = M705_ZONE_RIGHT(P2374_pi_XYZ);
        L2787_i_Y1 = M706_ZONE_TOP(P2374_pi_XYZ);
        L2788_i_Y2 = M707_ZONE_BOTTOM(P2374_pi_XYZ);
        (*(G2156_VideoDriver->VIDRV_05_InvertBox))(L2785_i_X1, L2786_i_X2, L2787_i_Y1, L2788_i_Y2);
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        int16_t L2789_i_X1;
        int16_t L2790_i_X2;
        int16_t L2791_i_Y1;
        int16_t L2792_i_Y2;


        L2789_i_X1 = M704_ZONE_LEFT(P2374_pi_XYZ);
        L2791_i_Y1 = M706_ZONE_TOP(P2374_pi_XYZ);
        L2790_i_X2 = L2789_i_X1 + M708_ZONE_WIDTH(P2374_pi_XYZ) - 1;
        L2792_i_Y2 = L2791_i_Y1 + M709_ZONE_HEIGHT(P2374_pi_XYZ) - 1;
        asm {
                        movem.l d3-d7,-(sp)
                        movea.l G0348_Bitmap_Screen(a5),a0
                        moveq   #0,d5
                        adda.l  #0x3E80,a0
                        move.w  L2791_i_Y1(a6),d5
                        move.w  L2792_i_Y2(a6),d0
                        sub.w   d5,d0
                        mulu.w  #0x28,d5
                        adda.w  d5,a0
                        move.w  L2789_i_X1(a6),d5
                        move.w  d5,d6
                        lsr.w   #3,d5
                        andi.w  #0xFE,d5
                        move.w  d5,d7
                        adda.w  d5,a0
                        andi.w  #0xF,d6
                        move.w  #0xFFFF,d3
                        move.w  d3,d4
                        lsr.w   d6,d3
                        move.w  L2790_i_X2(a6),d5
                        move.w  d5,d6
                        lsr.w   #3,d6
                        andi.w  #0xFE,d6
                        andi.w  #0xF,d5
                        neg.w   d5
                        addi.w  #0xF,d5
                        lsl.w   d5,d4
                        cmp.w   d6,d7
                        beq.s   @Tloc_19F2
                        move.w  d6,d1
                        sub.w   d7,d1
                        subq.w  #2,d1
                        lsr.w   #1,d1
                @Tloc_19B4:
                        lea     0x28(a0),a1
                        move.w  (a0),d5
                        move.w  d5,d6
                        not.w   d5
                        and.w   d3,d5
                        move.w  d3,d7
                        not.w   d7
                        and.w   d7,d6
                        or.w    d5,d6
                        move.w  d6,(a0)
                        addq.l  #2,a0
                        move.w  d1,d2
                        bra.s   @Tloc_19D4
                @Tloc_19D0:
                        not.w   (a0)
                        addq.l  #2,a0
                @Tloc_19D4:
                        dbf     d2,@Tloc_19D0
                        move.w  (a0),d5
                        move.w  d5,d6
                        not.w   d5
                        and.w   d4,d5
                        move.w  d4,d7
                        not.w   d7
                        and.w   d7,d6
                        or.w    d5,d6
                        move.w  d6,(a0)
                        movea.l a1,a0
                        dbf     d0,@Tloc_19B4
                        bra.s   @Tloc_1A0E
                @Tloc_19F2:
                        and.w   d4,d3
                @Tloc_19F4:
                        move.w  (a0),d5
                        move.w  d5,d6
                        not.w   d5
                        and.w   d3,d5
                        move.w  d3,d7
                        not.w   d7
                        and.w   d7,d6
                        or.w    d5,d6
                        move.w  d6,(a0)
                        lea     0x28(a0),a0
                        dbf     d0,@Tloc_19F4
                @Tloc_1A0E:
                        movem.l (sp)+,d3-d7
        }
#endif
}
#endif

#ifdef MEDIA463_P20JA_P20JB_I34E_I34M_P31J
void F0699_InitVideoInterrupt(
)
{
        G2156_VideoDriver = (VIDEO_DRIVER*)getvect(C255_DM_VIDEO_INTERRUPT);
#ifdef MEDIA480_P20JB_I34E_I34M_P31J
        (*(G2156_VideoDriver->VIDRV_13_InitializeUnusedGlobalVariables))(&G2059_pc_Unused, &G2060_pc_Unused);
#endif
}
#endif
#endif
