#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA215_S10EA_A31E_A31M_X31J_AU2E
#ifdef MEDIA000_S10EA
void
#endif
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
int16_t
#endif
F0913_DecompressPAK(
#ifdef MEDIA000_S10EA
P1002_pui_MostFrequentWordsAddress, P1003_pui_CompressedDataAddress, P1004_l_DecompressedDataWordCount, P1005_pui_DecompressedPRGAddress)
unsigned int16_t* P1002_pui_MostFrequentWordsAddress SEPARATOR /* Array of 1920 most used words in decompressed PRG. Two parts: the first 128 words are the most used words, followed by 1792 words less used words */
#endif
unsigned int16_t* P1003_pui_CompressedDataAddress    SEPARATOR
#ifdef MEDIA000_S10EA
long P1004_l_DecompressedDataWordCount               SEPARATOR
#endif
unsigned int16_t* P1005_pui_DecompressedPRGAddress   FINAL_SEPARATOR
{
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
        asm {
                move.l  A5,-(sp)
                movea.l A1,A5
        }
        if (P1003_pui_CompressedDataAddress[0] != 0x5223) {
                asm {
                        movea.l (sp)+,A5
                }
                return -1;
        }
#endif
        asm {
                        movem.l D4-D5/A2-A3,-(A7)
#ifdef MEDIA000_S10EA
                        movea.l P1002_pui_MostFrequentWordsAddress(A6),A3
#endif
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
                        movea.l P1003_pui_CompressedDataAddress(A6),A2
#endif
                        movea.l P1005_pui_DecompressedPRGAddress(A6),A1
#ifdef MEDIA000_S10EA
                        movea.l P1003_pui_CompressedDataAddress(A6),A0
                        move.l  P1004_l_DecompressedDataWordCount(A6),D4
#endif
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
                        lea     8(A2),A3                        /* Array of 1920 most used words in decompressed PRG. Two parts: the first 128 words are the most used words, followed by 1792 words less used words */
                        lea     3848(A2),A0
                        move.l  4(A2),D4                        /* Decompressed Data Word Count */
#endif
                        moveq   #0,D3
                        moveq   #15,D1
                        moveq   #8,D0
                @T91300_MainLoop:
                        subq.l  #1,D4                           /* D4 contains the number of decompressed words left to output */
#ifdef MEDIA000_S10EA
                        bmi     @T91307_RestoreRegistersAndExit
#endif
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
                        bmi.s   @T91307_RestoreRegistersAndExit
#endif
                        moveq   #1,D5                           /* Read one nibble (4 bit) from compressed data. This is a control code */
                        bsr.s   @T91303_ClearD2AndGetNibbles
                        cmp.w   D0,D2
                        bpl.s   @T91301_LessFrequentWord        /* Branch if control code >= 8 */
                        moveq   #1,D5                           /* Read one more nibble from compressed data and use the two nibbles as an index in the most frequent words array (first 128 words) */
                        bsr.s   @T91304_GetNibbles
                        add.w   D2,D2
                        move.w  0(A3,D2.w),(A1)+
                        bra.s   @T91300_MainLoop
                @T91301_LessFrequentWord:
                        cmp.w   D1,D2
                        beq.s   @T91302_DirectWord              /* Branch if control code = 15 */
                        moveq   #2,D5                           /* Read two more nibbles from compressed data and use the three nibbles as an index in the most frequent words array (last 1792 words) */
                        bsr.s   @T91304_GetNibbles
                        sub.l   #1920,D2
                        add.w   D2,D2
                        move.w  0(A3,D2.w),(A1)+
                        bra.s   @T91300_MainLoop
                @T91302_DirectWord:
                        moveq   #4,D5                           /* Read word (4 nibbles) directly from compressed data (do not use most frequent words array) */
                        bsr.s   @T91303_ClearD2AndGetNibbles
                        move.w  D2,(A1)+
                        bra.s   @T91300_MainLoop
                @T91303_ClearD2AndGetNibbles:
                        clr.w   D2
                @T91304_GetNibbles:
                        swap    D2
                @T91305_Loop:
                        subq.w  #1,D3                           /* D3 contains the number of nibbles in D2 that have not been used yet */
                        bpl.s   @T91306_NibbleInD2
                        moveq   #3,D3
                        move.w  (A0)+,D2                        /* Read 4 nibbles from compressed data */
                @T91306_NibbleInD2:
                        lsl.l   #4,D2
                        subq.w  #1,D5                           /* One less nibble to get. D5 contains the number of nibble to read */
                        bne.s   @T91305_Loop
                        swap    D2
                        rts
                @T91307_RestoreRegistersAndExit:
                        movem.l (A7)+,D4-D5/A2-A3
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
                        movea.l (sp)+,A5
#endif
        }
#ifdef MEDIA635_A31E_A31M_X31J_AU2E
        return 0;
#endif
}
#endif
