#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
unsigned char G3978_auc_FlippedPixels[256]; /* For each byte value as index, contains the same value with nibbles swapped. For example, at index 0x37 you get value 0x73 */
unsigned char G3983_aauc_[16][256];
#ifdef MEDIA350_G14ED_G20E
unsigned char G3984_aauc_[16][256];
#endif
unsigned char* G3980_apuc_[16];
#ifdef MEDIA350_G14ED_G20E
unsigned char* G3981_apuc_[16];
#endif

segment "gsthings"

void F2164_PatchAddresses(
)
{ /* Patch offsets in F2172_ and F2171_ */
        unsigned char* L6360_puc_CodeToPatch;
        unsigned char* L6361_puc_Bitmap;
        unsigned char* L6362_puc_ScreenMemoryOffset;


        asm {
                        CLC
                        LDA        #F2172_
                        ADC        #0x23
                        STA        L6360_puc_CodeToPatch
                        LDA        #^F2172_
                        ADC        #0x00
                        STA        L6360_puc_CodeToPatch+2
                        LDA        G0296_puc_Bitmap_Viewport
                        STA        L6361_puc_Bitmap
                        LDA        G0296_puc_Bitmap_Viewport + 2
                        STA        L6361_puc_Bitmap+2

                        LDA        #0x34a0      /* e134a0: Address of line 33 (Viewport) on screen. Address should reference the screen address 0xe12000 with an offset */
                        STA        L6362_puc_ScreenMemoryOffset
                        LDY        #0x02
                        LDX        #0x38        /* Loop 56 times for the 56 word copy operations to patch in F2172_ */
                L0814AE:
                        LDA        L6361_puc_Bitmap
                        STA        [L6360_puc_CodeToPatch]
                        LDA        L6361_puc_Bitmap+2
                        SEP        #0x20
                        STA        [L6360_puc_CodeToPatch],Y
                        REP        #0x20
                        CLC                                             /* -+     L6360_puc_CodeToPatch += 4 */
                        LDA        L6360_puc_CodeToPatch                /*  | */
                        ADC        #0x04                                /*  | */
                        STA        L6360_puc_CodeToPatch                /*  | */
                        BCC        L0814C6                              /*  | */
                        INC        L6360_puc_CodeToPatch+2              /* -+ */
                L0814C6:
                        LDA        L6362_puc_ScreenMemoryOffset
                        STA        [L6360_puc_CodeToPatch]
                        CLC                                             /* -+     L6360_puc_CodeToPatch += 3 */
                        LDA        L6360_puc_CodeToPatch                /*  | */
                        ADC        #0x03                                /*  | */
                        STA        L6360_puc_CodeToPatch                /*  | */
                        BCC        L0814D6                              /*  | */
                        INC        L6360_puc_CodeToPatch+2              /* -+ */
                L0814D6:
                        CLC                                             /* -+     L6361_puc_Bitmap += 2 */
                        LDA        L6361_puc_Bitmap                     /*  | */
                        ADC        #0x02                                /*  | */
                        STA        L6361_puc_Bitmap                     /*  | */
                        BCC        L0814E2                              /*  | */
                        INC        L6361_puc_Bitmap+2                   /* -+ */
                L0814E2:
                        CLC                                             /* -+     L6362_puc_ScreenMemoryOffset += 2 */
                        LDA        L6362_puc_ScreenMemoryOffset         /*  | */
                        ADC        #0x02                                /*  | */
                        STA        L6362_puc_ScreenMemoryOffset         /*  | */
                        BCC        L0814EE                              /*  | */
                        INC        L6362_puc_ScreenMemoryOffset+2       /* -+ */
                L0814EE:
                        DEX
                        BNE        L0814AE
                        CLC
                        LDA        #F2171_
                        ADC        #0x12
                        STA        L6360_puc_CodeToPatch
                        LDA        #^F2171_
                        ADC        #0x00
                        STA        L6360_puc_CodeToPatch+2
                        LDA        #0x8c20
                        STA        L6361_puc_Bitmap
                        LDA        #0xe1
                        STA        L6361_puc_Bitmap+2     /* e18c20: Address of the second line of the Message Area (bottom of the screen). Address should reference the screen address 0xe12000 with an offset */
                        STA        L6362_puc_ScreenMemoryOffset+2
                        LDA        #0x8b80      /* e18b80: Address of the first line of the Message Area. Address should reference the screen address 0xe12000 with an offset */
                        STA        L6362_puc_ScreenMemoryOffset
                        LDY        #0x02
                        LDX        #0x50        /* Loop 80 times for the 80 word copy operations to patch in F2171_ */
                L081519:
                        LDA        L6361_puc_Bitmap
                        STA        [L6360_puc_CodeToPatch]
                        LDA        L6361_puc_Bitmap+2
                        SEP        #0x20
                        STA        [L6360_puc_CodeToPatch],Y
                        REP        #0x20
                        CLC                                             /* -+     L6360_puc_CodeToPatch += 4 */
                        LDA        L6360_puc_CodeToPatch                /*  | */
                        ADC        #0x04                                /*  | */
                        STA        L6360_puc_CodeToPatch                /*  | */
                        BCC        L081531                              /*  | */
                        INC        L6360_puc_CodeToPatch+2              /* -+ */
                L081531:
                        LDA        L6362_puc_ScreenMemoryOffset
                        STA        [L6360_puc_CodeToPatch]
                        LDA        L6362_puc_ScreenMemoryOffset+2
                        SEP        #0x20
                        STA        [L6360_puc_CodeToPatch],Y
                        REP        #0x20
                        CLC                                             /* -+     L6360_puc_CodeToPatch += 4 */
                        LDA        L6360_puc_CodeToPatch                /*  | */
                        ADC        #0x04                                /*  | */
                        STA        L6360_puc_CodeToPatch                /*  | */
                        BCC        L081549                              /*  | */
                        INC        L6360_puc_CodeToPatch+2              /* -+ */
                L081549:
                        CLC                                             /* -+     L6361_puc_Bitmap += 2 */
                        LDA        L6361_puc_Bitmap                     /*  | */
                        ADC        #0x02                                /*  | */
                        STA        L6361_puc_Bitmap                     /*  | */
                        BCC        L081555                              /*  | */
                        INC        L6361_puc_Bitmap+2                   /* -+ */
                L081555:
                        CLC                                             /* -+     L6362_puc_ScreenMemoryOffset += 2 */
                        LDA        L6362_puc_ScreenMemoryOffset         /*  | */
                        ADC        #0x02                                /*  | */
                        STA        L6362_puc_ScreenMemoryOffset         /*  | */
                        BCC        L081561                              /*  | */
                        INC        L6362_puc_ScreenMemoryOffset+2       /* -+ */
                L081561:
                        DEX
                        BNE        L081519
        }
}

void F2179_InitializeGraphicData(
)
{
        char L6365_c_Index;
        char L6366_;
        char L6367_;
        char L6368_;
        char L6369_;
        char L6370_;


        L6365_c_Index = 0;
        do {
                L6366_ = L6367_ = L6365_c_Index;
                G3978_auc_FlippedPixels[L6365_c_Index++] = (L6366_ << 4) | (L6367_ >> 4);
        } while (L6365_c_Index != 0);
        L6368_ = 0;
        do {
                L6369_ = L6368_ & 0xF0;
                L6370_ = L6368_ & 0x0F;
                G3980_apuc_[L6370_] = G3983_aauc_[L6370_];
#ifdef MEDIA350_G14ED_G20E
                G3981_apuc_[L6370_] = G3984_aauc_[L6370_];
#endif
                L6365_c_Index = 0;
                do {
#ifdef MEDIA350_G14ED_G20E
                        L6366_ = 0;
#endif
#ifdef MEDIA409_G21E
                        L6366_ = 0xFF;
#endif
                        if ((L6365_c_Index & 0xF0) == L6369_) {
#ifdef MEDIA350_G14ED_G20E
                                L6366_ = 0xF0;
#endif
#ifdef MEDIA409_G21E
                                L6366_ = 0x0F;
#endif
                        }
                        if ((L6365_c_Index & 0x0F) == L6370_) {
#ifdef MEDIA350_G14ED_G20E
                                L6366_ |= 0x0F;
#endif
#ifdef MEDIA409_G21E
                                L6366_ &= 0xF0;
#endif
                        }
#ifdef MEDIA350_G14ED_G20E
                        G3983_aauc_[L6370_][L6365_c_Index] = L6366_;
                        G3984_aauc_[L6370_][L6365_c_Index++] = L6365_c_Index & (L6366_ ^ 0xFFFF);
#endif
#ifdef MEDIA409_G21E
                        G3983_aauc_[L6370_][L6365_c_Index++] = L6366_;
#endif
                } while (L6365_c_Index != 0);
                L6368_ += 17;
        } while (L6368_ != 16);
}

void F2165_PrintErrorMessage(P6044_i_ErrorNumber, P6045_pc_Message)
int16_t P6044_i_ErrorNumber;
char* P6045_pc_Message;
{
#ifdef MEDIA349_G14ED
        asm {   /* Disable Super HiRes Graphics */
                SEP     #0x30
                LDA     0xc029  /* NEWVIDEO */
                AND     #0x1f
                ORA     #0x40
                STA     0xc029  /* NEWVIDEO */
                REP     #0x30
        }
#endif
#ifdef MEDIA388_G20E_G21E
        F2148_DisableSuperHiResGraphics();
#endif
        c2pstr(P6045_pc_Message);
        SysFailMgr(P6044_i_ErrorNumber, P6045_pc_Message);
        for (;;); /*_Infinite loop_*/
}

#ifdef MEDIA388_G20E_G21E
void F2162_ShutDown(
)
{
        OSShutDownRecGS L6371_;


        *G3952_puc_ = G3953_i_SoundVolumeLevel;
        F2174_();
        QDShutDown();
        SANEShutDown();
        EMShutDown();
        SchShutDown();
        MTShutDown();
        HUnlockAll(_ownerid);
        DisposeAll(_ownerid);
        MMShutDown(_ownerid);
        TLShutDown();
        L6371_.pCount = 1;
        L6371_.shutdownFlag = 0;
        OSShutDownGS(&L6371_);
}
#endif
#endif
