#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA042_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
#ifdef MEDIA448_A21E_A22E
struct IOStdReq* G1144_s_IOStdReq = NULL;
struct MsgPort* G1145_ps_MsgPort = NULL;
char G1146_ac_KeyboardMatrix[13];
#endif
FILE_HANDLE G1023_ps_GameFileHandle; /* Dungeon file or saved game file */
long G0522_l_Unreferenced; /* BUG0_00 Useless code */
BOOLEAN G0523_B_RestartGameRequested;
BOOLEAN G0524_B_RestartGameAllowed;
long G0525_l_GameID;
unsigned int16_t G0526_ui_DungeonID;
int16_t G0527_i_Platform;
int16_t G0528_i_Format;
int16_t G0529_i_Useless; /* BUG0_00 Useless code */
char G0534_ac_SaveHeaderAdditionalData[134];
BOOLEAN G0530_B_LoadingCompressedDungeon;
unsigned char* G0531_puc_DecompressedDungeonCurrentPosition;
long G0532_l_DecompressedDungeonRemainingByteCount;
#ifndef NOCOPYPROTECTION
int16_t G0533_ai_CodePatch2_CPSE[34];
#endif
#endif


#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
overlay "start"
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
segment "main"
#endif

#ifdef MEDIA313_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J /* CHANGE7_30_IMPROVEMENT */
void F0455_FLOPPY_DecompressDungeon(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0863_puc_CompressedDungeonBuffer, P0864_puc_DecompressedDungeonBuffer, P0865_l_DecompressedByteCount)
#endif
#ifdef MEDIA312_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_F31E_F31J_X31J_P31J
REGISTER unsigned char* P0863_puc_CompressedDungeonBuffer   SEPARATOR
REGISTER unsigned char* P0864_puc_DecompressedDungeonBuffer SEPARATOR
REGISTER long           P0865_l_DecompressedByteCount       FINAL_SEPARATOR
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
unsigned char*          P0863_puc_CompressedDungeonBuffer   SEPARATOR
unsigned char*          P0864_puc_DecompressedDungeonBuffer SEPARATOR
long                    P0865_l_DecompressedByteCount       FINAL_SEPARATOR
#endif
{
#ifdef MEDIA330_S20E_S21E_X30J_X31J
        asm {
                        movea.l P0863_puc_CompressedDungeonBuffer,A0                                     /* Address of 2 tables containing: 4 most common bytes / 16 less common bytes */
                        lea     20(P0863_puc_CompressedDungeonBuffer),P0863_puc_CompressedDungeonBuffer
                        moveq   #0,D1
                @T0455001:
                        move.w  (P0863_puc_CompressedDungeonBuffer)+,D1                                  /* Get first word of compressed data */
                        moveq   #16,D0                                                                  /* Number of compressed data bits left to process in D1 */
                @T0455002_Loop:
                        btst    #15,D1
                        beq.s   @T0455004                                                                /* Branch if bit 15 is 0 */
                        moveq   #2,D2                                                                   /* Request 2 bits from compressed data. Can be either 10 or 11 */
                        bsr.s   @T0455006_Subroutine
                        subq.w  #2,D1                                                                   /* 2 less bits in D1 */
                        bne.s   @T0455003
                        swap    D1
                        moveq   #4,D2                                                                   /* Request 4 bits from compressed data */
                        bsr.s   @T0455006_Subroutine
                        move.b  4(A0,D1.w),(P0864_puc_DecompressedDungeonBuffer)+                        /* Output 1 byte from the table of 16 less common bytes */
                        bra.s   @T0455005
                @T0455003:
                        swap    D1
                        moveq   #8,D2                                                                   /* Request 8 bits from compressed data (a byte to output directly) */
                        bsr.s   @T0455006_Subroutine
                        move.b  D1,(P0864_puc_DecompressedDungeonBuffer)+                                /* Output 1 byte directly from compressed data */
                        bra.s   @T0455005
                @T0455004:
                        moveq   #3,D2                                                                   /* Request 3 bits from compressed data (first bit is 0, last two bits are an index in table of most common bytes) */
                        bsr.s   @T0455006_Subroutine
                        move.b  0(A0,D1.w),(P0864_puc_DecompressedDungeonBuffer)+                        /* Output 1 byte from the table of 4 most common bytes */
                @T0455005:
                        subq.l  #1,P0865_l_DecompressedByteCount                                         /* One less byte of decompressed data to output */
                        beq.s   @T0455008_Return                                                         /* Branch if there is no more data to output */
                        clr.w   D1
                        swap    D1
                        tst.w   D0
                        bne.s   @T0455002_Loop
                        bra.s   @T0455001
                @T0455006_Subroutine:
                        cmp.w   D2,D0
                        bpl.s   @T0455007                                                                /* Branch if D0 (Number of compressed data bits left to process in D1) >= D2 (Requested bit count) */
                        sub.w   D0,D2
                        lsl.l   D0,D1                                                                   /* Make room in D1 */
                        move.w  (P0863_puc_CompressedDungeonBuffer)+,D1                                  /* Get next word of compressed data */
                        moveq   #16,D0                                                                  /* Number of compressed data bits left to process in D1 */
                @T0455007:
                        sub.w   D2,D0                                                                   /* Decrease the number of bits left to process */
                        lsl.l   D2,D1                                                                   /* Remove bits left to process from D1 */
                        swap    D1
                        rts
                @T0455008_Return:
        }
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M /* Looks like the machine code is copied directly from the Amiga 2.x version generated by Aztec C */
        asm {
                        movem.l d3-d7/a2-a6,-(sp)
                        movea.l P0863_puc_CompressedDungeonBuffer(A6),A2
                        movea.l P0864_puc_DecompressedDungeonBuffer(A6),A3
                        move.l  P0865_l_DecompressedByteCount(A6),D4
                        movea.l A2,A0                                     /* Address of 2 tables containing: 4 most common bytes / 16 less common bytes */
                        lea     20(A2),A2
                        moveq   #0,D1
                @T0455001:
                        move.w  (A2)+,D1                                  /* Get first word of compressed data */
                        moveq   #16,D0                                                                  /* Number of compressed data bits left to process in D1 */
                @T0455002_Loop:
                        btst    #15,D1
                        beq.s   @T0455004                                                                /* Branch if bit 15 is 0 */
                        moveq   #2,D2                                                                   /* Request 2 bits from compressed data. Can be either 10 or 11 */
                        bsr.s   @T0455006_Subroutine
                        subq.w  #2,D1                                                                   /* 2 less bits in D1 */
                        bne.s   @T0455003
                        swap    D1
                        moveq   #4,D2                                                                   /* Request 4 bits from compressed data */
                        bsr.s   @T0455006_Subroutine
                        move.b  4(A0,D1.w),(A3)+                        /* Output 1 byte from the table of 16 less common bytes */
                        bra.s   @T0455005
                @T0455003:
                        swap    D1
                        moveq   #8,D2                                                                   /* Request 8 bits from compressed data (a byte to output directly) */
                        bsr.s   @T0455006_Subroutine
                        move.b  D1,(A3)+                                /* Output 1 byte directly from compressed data */
                        bra.s   @T0455005
                @T0455004:
                        moveq   #3,D2                                                                   /* Request 3 bits from compressed data (first bit is 0, last two bits are an index in table of most common bytes) */
                        bsr.s   @T0455006_Subroutine
                        move.b  0(A0,D1.w),(A3)+                        /* Output 1 byte from the table of 4 most common bytes */
                @T0455005:
                        subq.l  #1,D4                                         /* One less byte of decompressed data to output */
                        beq.s   @T0455008_Return                                                         /* Branch if there is no more data to output */
                        clr.w   D1
                        swap    D1
                        tst.w   D0
                        bne.s   @T0455002_Loop
                        bra.s   @T0455001
                @T0455006_Subroutine:
                        cmp.w   D2,D0
                        bpl.s   @T0455007                                                                /* Branch if D0 (Number of compressed data bits left to process in D1) >= D2 (Requested bit count) */
                        sub.w   D0,D2
                        lsl.l   D0,D1                                                                   /* Make room in D1 */
                        move.w  (A2)+,D1                                  /* Get next word of compressed data */
                        moveq   #16,D0                                                                  /* Number of compressed data bits left to process in D1 */
                @T0455007:
                        sub.w   D2,D0                                                                   /* Decrease the number of bits left to process */
                        lsl.l   D2,D1                                                                   /* Remove bits left to process from D1 */
                        swap    D1
                        rts
                @T0455008_Return:
                        movem.l        (sp)+,d3-d7/a2-a6
        }
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        AztecCDirective_asm
                        movea.l A2,A0                   ; P0863_puc_CompressedDungeonBuffer
                        lea     20(A2),A2               ; P0863_puc_CompressedDungeonBuffer
                        moveq   #0,D1
                T0455001:
                        move.w  (A2)+,D1                ; P0863_puc_CompressedDungeonBuffer
                        moveq   #16,D0
                T0455002_Loop:
                        btst    #15,D1
                        beq.s   T0455004
                        moveq   #2,D2
                        bsr.s   T0455006_Subroutine
                        subq.w  #2,D1
                        bne.s   T0455003
                        swap    D1
                        moveq   #4,D2
                        bsr.s   T0455006_Subroutine
                        move.b  4(A0,D1.w),(A3)+        ; P0864_puc_DecompressedDungeonBuffer
                        bra.s   T0455005
                T0455003:
                        swap    D1
                        moveq   #8,D2
                        bsr.s   T0455006_Subroutine
                        move.b  D1,(A3)+                ; P0864_puc_DecompressedDungeonBuffer
                        bra.s   T0455005
                T0455004:
                        moveq   #3,D2
                        bsr.s   T0455006_Subroutine
                        move.b  0(A0,D1.w),(A3)+        ; P0864_puc_DecompressedDungeonBuffer
                T0455005:
                        subq.l  #1,D4                   ; P0865_l_DecompressedByteCount
                        beq.s   T0455008_Return
                        clr.w   D1
                        swap    D1
                        tst.w   D0
                        bne.s   T0455002_Loop
                        bra.s   T0455001
                T0455006_Subroutine:
                        cmp.w   D2,D0
                        bpl.s   T0455007
                        sub.w   D0,D2
                        lsl.l   D0,D1
                        move.w  (A2)+,D1                ; P0863_puc_CompressedDungeonBuffer
                        moveq   #16,D0
                T0455007:
                        sub.w   D2,D0
                        lsl.l   D2,D1
                        swap    D1
                        rts
                T0455008_Return:
        AztecCDirective_endasm
#endif
#ifdef MEDIA481_P20JB_I34E_I34M_F31E_F31J_P31J
        unsigned long L1877_ul_ByteCount;
        unsigned long L1878_ul_BitBuffer;
        int16_t L1879_i_BitsInBufferCount;
        unsigned char* L1880_puc_MostCommonBytes; /* 4 bytes */
        unsigned char* L1881_puc_LessCommonBytes; /* 16 bytes */


        L1877_ul_ByteCount = 0;
        L1878_ul_BitBuffer = 0;
        L1879_i_BitsInBufferCount = 0;
        L1880_puc_MostCommonBytes = P0863_puc_CompressedDungeonBuffer;
        L1881_puc_LessCommonBytes = P0863_puc_CompressedDungeonBuffer + 4;
        P0863_puc_CompressedDungeonBuffer += 20; /* Address of the first compressed byte */
        do {
                while (L1879_i_BitsInBufferCount <= 24) {
#ifdef MEDIA480_P20JB_I34E_I34M_P31J
                        L1878_ul_BitBuffer = L1878_ul_BitBuffer << 8 | *P0863_puc_CompressedDungeonBuffer;
                        (unsigned char HUGE*)P0863_puc_CompressedDungeonBuffer += 1;
#endif
#ifdef MEDIA670_F31E_F31J
                        L1878_ul_BitBuffer = L1878_ul_BitBuffer << 8 | *P0863_puc_CompressedDungeonBuffer++;
#endif
                        L1879_i_BitsInBufferCount += 8;
                }
                switch ((L1878_ul_BitBuffer >> L1879_i_BitsInBufferCount - 2) & 3) {
                        case 0:
                        case 1:
                                P0864_puc_DecompressedDungeonBuffer[L1877_ul_ByteCount++] = L1880_puc_MostCommonBytes[(L1878_ul_BitBuffer >> (L1879_i_BitsInBufferCount -= 3)) & 3];
                                break;
                        case 2:
                                P0864_puc_DecompressedDungeonBuffer[L1877_ul_ByteCount++] = L1881_puc_LessCommonBytes[(L1878_ul_BitBuffer >> (L1879_i_BitsInBufferCount -= 6)) & 15];
                                break;
                        case 3:
                                P0864_puc_DecompressedDungeonBuffer[L1877_ul_ByteCount++] = (L1878_ul_BitBuffer >> (L1879_i_BitsInBufferCount -= 10)) & 255;
                }
        } while (L1877_ul_ByteCount < P0865_l_DecompressedByteCount);
#endif
}
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
asm(F0455_FLOPPY_DecompressDungeon) {
                PHD
                LDA        >G3958_
                TCD
                PLA
                STA        0xf
                PLA
                STA        0xc
                SEP        #0x20
                PLA
                STA        0xe
                REP        #0x20
                PLA
                STA        0x0
                PLA
                STA        0x2
                PLA
                STA        0x4
                PLA
                STA        0x6
                PLA
                STA        0x8
                PLA
                STA        0xa
                CLC
                LDA        0x0
                STA        0x11
                ADC        #0x14
                STA        0x0
                LDA        0x2
                STA        0x13
                ADC        #0x00
                STA        0x2
                JSR        |L0200E7_Subroutine
        L020054:
                LDA        #0x01
                JSR        |L0200B5_Subroutine
                CMP        #0x00
                BEQ        L02007E
                LDA        #0x01
                JSR        |L0200B5_Subroutine
                CMP        #0x00
                BNE        L020076
                LDA        #0x04
                JSR        |L0200B5_Subroutine
                CLC
                ADC        #0x04
                BRA        L020084
        L020076:
                LDA        #0x08
                JSR        |L0200B5_Subroutine
                BRA        L020087
        L02007E:
                LDA        #0x02
                JSR        |L0200B5_Subroutine
        L020084:
                TAY
                LDA        [0x11],Y
        L020087:
                SEP        #0x20
                STA        [0x4]
                REP        #0x20
                INC        0x4
                BNE        L020093
                INC        0x6
        L020093:
                LDA        0x8
                BNE        L020099
                DEC        0xa
        L020099:
                DEC        0x8
                BNE        L020054
                LDA        0xa
                BNE        L020054
                TSC
                SEC
                SBC        #0x0c
                TCS
                SEP        #0x20
                LDA        0xe
                PHA
                REP        #0x20
                LDA        0xc
                PHA
                LDA        0xf
                TCD
                RTL
}
asm(L0200B5_Subroutine) {
                STA        0x17
                STZ        0x1b
                LDA        0x19
                BEQ        L0200D2
                CMP        0x17
                BCS        L0200D5
                BEQ        L0200D5
                SEC
                LDA        0x17
                SBC        0x19
                STA        0x17
        L0200CA:
                ASL        0x15
                ROL        0x1b
                DEC        0x19
                BNE        L0200CA
        L0200D2:
                JSR        |L0200E7_Subroutine
        L0200D5:
                SEC
                LDA        0x19
                SBC        0x17
                STA        0x19
        L0200DC:
                ASL        0x15
                ROL        0x1b
                DEC        0x17
                BNE        L0200DC
                LDA        0x1b
                RTS
}
asm(L0200E7_Subroutine) {
                LDA        [0x0]
                XBA
                STA        0x15
                CLC
                LDA        0x0
                ADC        #0x02
                STA        0x0
                BCC        L0200F8
                INC        0x2
        L0200F8:
                LDA        #0x10
                STA        0x19
                RTS
}
#endif
#endif
