#ifndef COMPILE_H
#include "COMPILE.H"
#endif
#ifdef MEDIA043_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_SU1E
#ifdef MEDIA772_SU1E
extern void F1785_SOUND_RestoreKeyClick();


#endif
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
BOOLEAN G0578_B_UseByteBoxCoordinates = C0_FALSE;
int16_t G0579_i_Unreferenced; /* BUG0_00 Useless code */
#endif
#ifdef MEDIA174_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_SU1E
unsigned int16_t G0580_ui_PSGModeRegisterBackup;
#endif
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
char* G0581_pc_contermSystemVariable = C0x0484_ADDRESS_SYSTEM_VARIABLE_conterm;
#endif
#ifdef MEDIA772_SU1E
char* G0581_pc_contermSystemVariable;
#endif
#ifdef MEDIA174_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_SU1E
char G0582_c_contermSystemVariableBackup;
#endif
#ifdef MEDIA167_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA_P20JB_I34E_I34M_X31J_P31J
int16_t G0583_i_PendingSoundIndex = -1;
#endif
#ifdef MEDIA166_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA_P20JB
unsigned int16_t G0584_PendingSoundVolume = 0;
#endif
#ifdef MEDIA714_I34E_I34M_X31J_P31J
int16_t G0584_PendingSoundVolume = 0;
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
SOUND_VOLUME G3077_s_EntranceSoundVolume = { 64, 64 };
SOUND_VOLUME G1026_s_PendingSound1Volume;
SOUND_VOLUME G1027_s_PendingSound2Volume;
#endif
#ifdef MEDIA488_F20E_F20J
unsigned int16_t G4080_aui_SoundVolume[M513_SOUND_COUNT];
#endif
#ifdef MEDIA670_F31E_F31J
int16_t G4080_aui_SoundVolume[M513_SOUND_COUNT];
#endif
#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
int16_t G0585_ai_CodePatch0_CPSE[34];
#endif
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
SOUND_DATA_AMIGA G1022_as_SoundData[M513_SOUND_COUNT];
#endif
#ifdef MEDIA371_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A36M_A31E_A31M_A33M_A35E_A35M
unsigned char G1028_aauc_DistanceToSoundVolume[25][25] = {
#endif
#ifdef MEDIA425_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A36M_A31E_A31M_A33M_A35E_A35M
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 4,  5,  5,  5,  5,  5,  5,  5, 5, 4, 4, 4, 4, 4 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 5,  6,  6,  6,  6,  5,  5,  5, 5, 5, 5, 4, 4, 4 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 5,  6,  6,  6,  6,  6,  6,  5, 5, 5, 5, 5, 4, 4 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 5,  7,  7,  7,  7,  6,  6,  6, 6, 5, 5, 5, 5, 4 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 5,  8,  8,  7,  7,  7,  7,  6, 6, 6, 5, 5, 5, 4 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 4, 6,  9,  9,  8,  8,  8,  7,  7, 6, 6, 6, 5, 5, 5 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 6, 10, 10, 10,  9,  8,  8,  7, 7, 6, 6, 5, 5, 5 },
        { 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 4, 7, 12, 12, 11, 10,  9,  9,  8, 7, 7, 6, 6, 5, 5 },
        { 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 7, 15, 14, 13, 12, 11,  9,  8, 8, 7, 6, 6, 5, 5 },
        { 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 8, 20, 19, 16, 14, 12, 10,  9, 8, 7, 7, 6, 6, 5 },
        { 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4, 8, 29, 26, 21, 16, 13, 11, 10, 8, 7, 7, 6, 6, 5 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 4, 8, 58, 41, 26, 19, 14, 12, 10, 9, 8, 7, 6, 6, 5 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 6, 64, 58, 29, 20, 15, 12, 10, 9, 8, 7, 6, 6, 5 },
        { 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 6, 41, 29, 19, 13, 10,  8,  7, 6, 6, 5, 5, 4, 4 },
        { 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 6, 21, 19, 15, 12, 10,  8,  7, 6, 5, 5, 4, 4, 4 },
        { 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 3, 6, 14, 13, 12, 10,  9,  7,  7, 6, 5, 5, 4, 4, 4 },
        { 0, 1, 1, 1, 1, 1, 1, 1, 2, 2, 3, 5, 11, 10, 10,  9,  8,  7,  6, 6, 5, 5, 4, 4, 4 },
        { 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 5,  9,  8,  8,  7,  7,  6,  6, 5, 5, 4, 4, 4, 4 },
        { 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 5,  7,  7,  7,  7,  6,  6,  5, 5, 5, 4, 4, 4, 4 },
        { 0, 1, 1, 1, 1, 1, 1, 2, 2, 1, 3, 4,  6,  6,  6,  6,  6,  5,  5, 5, 4, 4, 4, 4, 3 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4,  6,  6,  5,  5,  5,  5,  5, 4, 4, 4, 4, 3, 3 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 4,  5,  5,  5,  5,  5,  4,  4, 4, 4, 4, 3, 3, 3 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3,  5,  5,  4,  4,  4,  4,  4, 4, 4, 3, 3, 3, 3 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 3, 3,  4,  4,  4,  4,  4,  4,  4, 4, 3, 3, 3, 3, 3 },
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3,  4,  4,  4,  4,  4,  4,  4, 3, 3, 3, 3, 3, 3 } };
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
        { 3, 3, 3, 4, 4, 5, 6, 7, 8,  9, 12, 15,  20,  20,  19, 19, 19, 18, 18, 17, 16, 16, 15, 15, 14 },
        { 3, 3, 3, 4, 4, 5, 6, 7, 8, 10, 12, 17,  21,  21,  21, 21, 20, 19, 19, 18, 17, 17, 16, 15, 15 },
        { 3, 3, 3, 4, 4, 5, 6, 7, 8, 10, 13, 16,  23,  23,  23, 22, 22, 21, 20, 19, 18, 18, 17, 16, 15 },
        { 3, 3, 3, 4, 4, 5, 6, 7, 8, 10, 13, 18,  26,  26,  25, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16 },
        { 3, 3, 3, 4, 4, 5, 6, 7, 8, 12, 14, 20,  29,  29,  28, 27, 26, 25, 23, 22, 21, 20, 18, 17, 16 },
        { 3, 3, 3, 4, 4, 5, 5, 7, 8,  6, 14, 21,  33,  33,  32, 31, 29, 27, 25, 24, 22, 21, 19, 18, 17 },
        { 3, 3, 3, 4, 4, 5, 5, 6, 8, 11, 15, 23,  39,  38,  37, 35, 32, 30, 28, 25, 23, 22, 20, 19, 18 },
        { 3, 3, 3, 3, 4, 4, 5, 6, 8, 11, 16, 25,  46,  46,  43, 40, 36, 33, 30, 27, 25, 23, 21, 19, 18 },
        { 3, 3, 3, 3, 4, 4, 5, 6, 8, 11, 16, 27,  58,  56,  52, 46, 41, 36, 32, 29, 26, 24, 22, 20, 19 },
        { 3, 3, 3, 3, 4, 4, 5, 6, 7, 10, 16, 30,  77,  73,  64, 55, 46, 40, 35, 31, 27, 25, 22, 21, 19 },
        { 2, 3, 3, 3, 4, 4, 5, 6, 7, 10, 15, 32, 115, 103,  82, 64, 52, 43, 37, 32, 28, 25, 23, 21, 19 },
        { 2, 3, 3, 3, 3, 4, 4, 5, 7,  9, 14, 29, 230, 163, 103, 73, 56, 46, 38, 33, 29, 26, 23, 21, 20 },
        { 2, 3, 3, 3, 3, 4, 4, 5, 6,  8, 12, 23, 255, 230, 115, 77, 58, 46, 39, 33, 29, 26, 23, 21, 20 },
        { 2, 2, 2, 2, 3, 3, 3, 4, 5,  6, 10, 21, 161, 114,  72, 51, 39, 32, 27, 23, 20, 18, 16, 15, 14 },
        { 2, 2, 2, 2, 3, 3, 3, 4, 5,  7, 11, 22,  81,  72,  57, 45, 36, 30, 26, 23, 20, 18, 16, 15, 14 },
        { 2, 2, 2, 2, 3, 3, 4, 4, 5,  7, 12, 21,  54,  51,  45, 38, 33, 28, 24, 22, 19, 17, 16, 15, 13 },
        { 2, 2, 2, 3, 3, 3, 4, 4, 6,  8, 12, 19,  41,  39,  36, 33, 29, 26, 23, 20, 18, 17, 15, 14, 13 },
        { 2, 2, 2, 3, 3, 3, 4, 5, 6,  8, 11, 18,  33,  32,  30, 28, 26, 23, 21, 19, 18, 16, 15, 14, 13 },
        { 2, 2, 2, 3, 3, 3, 4, 5, 6,  8, 11, 17,  27,  27,  26, 24, 23, 21, 19, 18, 17, 15, 14, 13, 12 },
        { 2, 2, 2, 3, 3, 3, 4, 5, 6,  4, 10, 15,  23,  23,  23, 22, 20, 19, 18, 17, 16, 15, 14, 13, 12 },
        { 2, 2, 2, 3, 3, 3, 4, 5, 6,  8, 10, 14,  21,  20,  20, 19, 18, 18, 17, 16, 15, 14, 13, 12, 12 },
        { 2, 2, 2, 3, 3, 3, 4, 5, 6,  7,  9, 13,  18,  18,  18, 17, 17, 16, 15, 15, 14, 13, 12, 12, 11 },
        { 2, 2, 3, 3, 3, 4, 4, 5, 6,  7,  9, 12,  17,  16,  16, 16, 15, 15, 14, 14, 13, 12, 12, 11, 11 },
        { 2, 2, 3, 3, 3, 4, 4, 5, 6,  7,  9, 12,  15,  15,  15, 15, 14, 14, 13, 13, 12, 12, 11, 11, 10 },
        { 2, 2, 3, 3, 3, 4, 4, 5, 6,  7,  8, 11,  14,  14,  14, 13, 13, 13, 12, 12, 12, 11, 11, 10, 10 } };
#endif
#ifdef MEDIA620_A31E_A31M_A33M
SOUND_DATA_AMIGA G1022_as_SoundData[M513_SOUND_COUNT];
char G3238_AllocatedUnits;
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
int16_t G3239_PendingSound1Index;
int16_t G3240_PendingSound2Index;
struct Device* G3231_ps_AudioDevice;
struct IOAudio* G3227_ps_Sound1Control = NULL;
struct IOAudio* G3225_ps_Sound1LeftChannel = NULL;
struct IOAudio* G3226_ps_Sound1RightChannel = NULL;
struct IOAudio* G3230_ps_Sound2Control = NULL;
struct IOAudio* G3228_ps_Sound2LeftChannel = NULL;
struct IOAudio* G3229_ps_Sound2RightChannel = NULL;
struct MsgPort* G3234_ps_IOAudio3ReplyPort = NULL;
struct MsgPort* G3232_ps_IOAudio1ReplyPort = NULL;
struct MsgPort* G3233_ps_IOAudio2ReplyPort = NULL;
struct MsgPort* G3237_ps_IOAudio6ReplyPort = NULL;
struct MsgPort* G3235_ps_IOAudio4ReplyPort = NULL;
struct MsgPort* G3236_ps_IOAudio5ReplyPort = NULL;
#endif
#ifdef MEDIA425_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A36M_A31E_A31M_A33M_A35E_A35M
SOUND_VOLUME G1024_as_SoundVolume[2] = { {0, 0}, {0, 0} }; /* Replaces G0584_PendingSoundVolume */
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
UBYTE G0730_uc_AudioChannelsAllocation = 0x0F;
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
char G4033_c_ = 0;
long G4031_l_ = 0;
#endif
#ifdef MEDIA388_G20E_G21E
unsigned int16_t G4029_ui_PlayedSoundCount_CPSX;
BOOLEAN G4028_B_PreventSoundPlay_CPSX;
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
int16_t G3953_i_SoundVolumeLevel;
unsigned char* G3952_puc_;

char G4040_ac_[8] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
char G4039_ac_[256];
char G4034_ac_[M513_SOUND_COUNT];
int16_t G4035_ai_[M513_SOUND_COUNT];
char G4041_ac_[M513_SOUND_COUNT];
char G4036_ac_[M513_SOUND_COUNT];
unsigned int16_t* G4037_apui_[M513_SOUND_COUNT];
int16_t G4038_ai_[M513_SOUND_COUNT];
long G4030_al_[M513_SOUND_COUNT];
SOUND_VOLUME G4026_as_[M513_SOUND_COUNT];
#endif
#ifdef MEDIA137_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
#ifndef NOCOPYPROTECTION
int16_t G0585_ai_CodePatch0_CPSE[34];
#endif
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
#ifndef NOCOPYPROTECTION
char G0585_ai_CodePatch0_CPSE[289];
#endif
#endif


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

#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
void F0501_SOUND_Deinitialize(
)
{
        if (G0734_ps_AmigaData->AudioDevice) {
                CloseDevice(G0734_ps_AmigaData->Sound1Control);
        }
        if (G0734_ps_AmigaData->Sound1LeftChannel) {
                FreeMem(G0734_ps_AmigaData->Sound1LeftChannel, (long)sizeof(struct IOAudio));
        }
        if (G0734_ps_AmigaData->Sound1RightChannel) {
                FreeMem(G0734_ps_AmigaData->Sound1RightChannel, (long)sizeof(struct IOAudio));
        }
        if (G0734_ps_AmigaData->Sound1Control) {
                FreeMem(G0734_ps_AmigaData->Sound1Control, (long)sizeof(struct IOAudio));
        }
        if (G0734_ps_AmigaData->Sound2LeftChannel) {
                FreeMem(G0734_ps_AmigaData->Sound2LeftChannel, (long)sizeof(struct IOAudio));
        }
        if (G0734_ps_AmigaData->Sound2RightChannel) {
                FreeMem(G0734_ps_AmigaData->Sound2RightChannel, (long)sizeof(struct IOAudio));
        }
        if (G0734_ps_AmigaData->Sound2Control) {
                FreeMem(G0734_ps_AmigaData->Sound2Control, (long)sizeof(struct IOAudio));
        }
        if (G0734_ps_AmigaData->IOAudio1ReplyPort) {
                DeletePort(G0734_ps_AmigaData->IOAudio1ReplyPort);
        }
        if (G0734_ps_AmigaData->IOAudio2ReplyPort) {
                DeletePort(G0734_ps_AmigaData->IOAudio2ReplyPort);
        }
        if (G0734_ps_AmigaData->IOAudio3ReplyPort) {
                DeletePort(G0734_ps_AmigaData->IOAudio3ReplyPort);
        }
        if (G0734_ps_AmigaData->IOAudio4ReplyPort) {
                DeletePort(G0734_ps_AmigaData->IOAudio4ReplyPort);
        }
        if (G0734_ps_AmigaData->IOAudio5ReplyPort) {
                DeletePort(G0734_ps_AmigaData->IOAudio5ReplyPort);
        }
        if (G0734_ps_AmigaData->IOAudio6ReplyPort) {
                DeletePort(G0734_ps_AmigaData->IOAudio6ReplyPort);
        }
}
#endif

#ifdef MEDIA440_A20E_A20F_A20G_A21E_A22E_A22G_A36M_A31E_A31M_A33M_A35E_A35M
int16_t F0502_SOUND_CompareVolumes(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0934_ps_SoundVolume1, P0935_ps_SoundVolume2)
#endif
REGISTER SOUND_VOLUME* P0934_ps_SoundVolume1 SEPARATOR
REGISTER SOUND_VOLUME* P0935_ps_SoundVolume2 FINAL_SEPARATOR
{
        REGISTER int16_t L1669_i_;
        REGISTER int16_t L1670_i_;
        REGISTER int16_t L1671_i_TotalSoundVolume1;
        REGISTER int16_t L1672_i_TotalSoundVolume2;


        L1671_i_TotalSoundVolume1 = P0934_ps_SoundVolume1->Left + P0934_ps_SoundVolume1->Right;
        L1672_i_TotalSoundVolume2 = P0935_ps_SoundVolume2->Left + P0935_ps_SoundVolume2->Right;
        if (L1671_i_TotalSoundVolume1 > 25) {
                L1669_i_ = 2;
        } else {
                if (15 > L1671_i_TotalSoundVolume1) {
                        L1669_i_ = 0;
                } else {
                        L1669_i_ = 1;
                }
        }
        if (L1672_i_TotalSoundVolume2 > 25) {
                L1670_i_ = 2;
        } else {
                if (15 > L1672_i_TotalSoundVolume2) {
                        L1670_i_ = 0;
                } else {
                        L1670_i_ = 1;
                }
        }
        if (L1669_i_ > L1670_i_) {
                return 1;
        }
        if (L1669_i_ == L1670_i_) {
                return 0;
        } else {
                return -1;
        }
}
#endif

#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
void F1051_(
void
)
{
#ifdef MEDIA620_A31E_A31M_A33M
        register int16_t L3437_i_;
        register int16_t L3438_i_;
        register SOUND_DATA_AMIGA* L3439_ps_;
        register char* L3440_pc_;


        L3437_i_ = 0;
        L3439_ps_ = G1022_as_SoundData;
        while (L3437_i_ < M513_SOUND_COUNT) {
                L3438_i_ = G0060_as_Graphic562_Sounds[L3437_i_].GraphicIndex;
                L3439_ps_->ByteCount = F0494_MEMORY_GetGraphicDecompressedByteCount(L3438_i_) - 2;
                L3440_pc_ = F0468_MEMORY_Allocate(L3439_ps_->ByteCount + 2, C1_ALLOCATION_PERMANENT);
                L3439_ps_->FirstSample = L3440_pc_ + 2;
                G0060_as_Graphic562_Sounds[L3437_i_].Buffer = L3439_ps_->FirstSample;
                F0490_MEMORY_LoadDecompressAndExpandGraphic(L3438_i_ | MASK0x8000_NOT_EXPANDED | MASK0x4000_DO_NOT_COPY_DIMENSIONS, L3440_pc_);
                L3437_i_++;
                L3439_ps_++;
        }
#endif
}

void F0710_WaitSoundPlayComplete(
void
)
{
#ifdef MEDIA758_A36M_A35E_A35M
        G3227_ps_Sound1Control->ioa_Request.io_Command = ADCMD_FINISH;
        M008_SET(G3227_ps_Sound1Control->ioa_Request.io_Flags, ADIOF_SYNCCYCLE);
        G3230_ps_Sound2Control->ioa_Request.io_Command = ADCMD_FINISH;
        M008_SET(G3230_ps_Sound2Control->ioa_Request.io_Flags, ADIOF_SYNCCYCLE);
        BeginIO(G3227_ps_Sound1Control);
        BeginIO(G3230_ps_Sound2Control);
        WaitIO(G3225_ps_Sound1LeftChannel);
        WaitIO(G3226_ps_Sound1RightChannel);
        WaitIO(G3228_ps_Sound2LeftChannel);
        WaitIO(G3229_ps_Sound2RightChannel);
        M009_CLEAR(G3227_ps_Sound1Control->ioa_Request.io_Flags, ADIOF_SYNCCYCLE);
        M009_CLEAR(G3230_ps_Sound2Control->ioa_Request.io_Flags, ADIOF_SYNCCYCLE);
        G3227_ps_Sound1Control->ioa_Request.io_Command = CMD_STOP;
        G3230_ps_Sound2Control->ioa_Request.io_Command = CMD_STOP;
        DoIO(G3227_ps_Sound1Control);
        DoIO(G3230_ps_Sound2Control);
#endif
}

BOOLEAN F0707_ResetSoundBufferAddress(
int16_t P2391_i_ FINAL_SEPARATOR /* in IO.C for other versions */
{
#ifdef MEDIA758_A36M_A35E_A35M
        if ((P2391_i_ < 0) || (G0060_as_Graphic562_Sounds[P2391_i_].GraphicIndex < 0)) {
#endif
                return C0_FALSE;
#ifdef MEDIA758_A36M_A35E_A35M
        }
        G0060_as_Graphic562_Sounds[P2391_i_].Buffer = NULL;
        return C1_TRUE;
#endif
}

void F0709_StartSound( /* Similar to F0060_SOUND_Play_CPSX Amiga 2.x */
int16_t      P0921_i_SoundIndex  SEPARATOR
SOUND_VOLUME P0922_s_SoundVolume FINAL_SEPARATOR
{
        REGISTER int16_t L3441_i_Period;
#ifdef MEDIA620_A31E_A31M_A33M
        REGISTER SOUND_DATA_AMIGA* L1570_ps_SoundData;
#endif
        REGISTER struct IOAudio* L1573_ps_SoundControl;
#ifdef MEDIA758_A36M_A35E_A35M
        register unsigned char* L3442_puc_;
        register unsigned int16_t L3443_ui_;
#endif
        struct IOAudio* L1571_ps_SoundLeftChannel;
        struct IOAudio* L1572_ps_SoundRightChannel;


#ifdef MEDIA620_A31E_A31M_A33M
        L1570_ps_SoundData = &G1022_as_SoundData[P0921_i_SoundIndex];
#endif
#ifdef MEDIA758_A36M_A35E_A35M
        L3442_puc_ = G0060_as_Graphic562_Sounds[P0921_i_SoundIndex].Buffer;
#endif
        L3441_i_Period = G0060_as_Graphic562_Sounds[P0921_i_SoundIndex].Period;
#ifdef MEDIA758_A36M_A35E_A35M
        L3443_ui_ = F0494_MEMORY_GetGraphicDecompressedByteCount(G0060_as_Graphic562_Sounds[P0921_i_SoundIndex].GraphicIndex) - 2;
        L3442_puc_ += 2;
#endif
#ifdef MEDIA620_A31E_A31M_A33M
        if (!L1570_ps_SoundData->ByteCount) {
#endif
#ifdef MEDIA758_A36M_A35E_A35M
        if (!L3443_ui_) {
#endif
                return;
        }
        if (CheckIO(G3225_ps_Sound1LeftChannel)) {
                L1571_ps_SoundLeftChannel = G3225_ps_Sound1LeftChannel;
                L1572_ps_SoundRightChannel = G3226_ps_Sound1RightChannel;
                L1573_ps_SoundControl = G3227_ps_Sound1Control;
                G1024_as_SoundVolume[0].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[0].Right = P0922_s_SoundVolume.Right;
        } else {
                if (CheckIO(G3228_ps_Sound2LeftChannel)) {
                        L1571_ps_SoundLeftChannel = G3228_ps_Sound2LeftChannel;
                        L1572_ps_SoundRightChannel = G3229_ps_Sound2RightChannel;
                        L1573_ps_SoundControl = G3230_ps_Sound2Control;
                        G1024_as_SoundVolume[1].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[1].Right = P0922_s_SoundVolume.Right;
                } else {
                        if (F0502_SOUND_CompareVolumes(&G1024_as_SoundVolume[0], &G1024_as_SoundVolume[1]) == 1) {
                                        L1571_ps_SoundLeftChannel = G3228_ps_Sound2LeftChannel;
                                        L1572_ps_SoundRightChannel = G3229_ps_Sound2RightChannel;
                                        L1573_ps_SoundControl = G3230_ps_Sound2Control;
                                        G1024_as_SoundVolume[1].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[1].Right = P0922_s_SoundVolume.Right;
                                } else {
                                        L1571_ps_SoundLeftChannel = G3225_ps_Sound1LeftChannel;
                                        L1572_ps_SoundRightChannel = G3226_ps_Sound1RightChannel;
                                        L1573_ps_SoundControl = G3227_ps_Sound1Control;
                                        G1024_as_SoundVolume[0].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[0].Right = P0922_s_SoundVolume.Right;
                        }
                }
        }
        L1573_ps_SoundControl->ioa_Request.io_Command = ADCMD_FINISH;
        BeginIO(L1573_ps_SoundControl);
        WaitIO(L1571_ps_SoundLeftChannel);
        WaitIO(L1572_ps_SoundRightChannel);
        L1573_ps_SoundControl->ioa_Request.io_Command = CMD_STOP;
        DoIO(L1573_ps_SoundControl);
#ifdef MEDIA620_A31E_A31M_A33M
        L1571_ps_SoundLeftChannel->ioa_Length = L1570_ps_SoundData->ByteCount;
        L1572_ps_SoundRightChannel->ioa_Length = L1570_ps_SoundData->ByteCount;
        L1571_ps_SoundLeftChannel->ioa_Data = L1570_ps_SoundData->FirstSample;
        L1572_ps_SoundRightChannel->ioa_Data = L1570_ps_SoundData->FirstSample;
#endif
#ifdef MEDIA758_A36M_A35E_A35M
        L1571_ps_SoundLeftChannel->ioa_Length = L3443_ui_;
        L1572_ps_SoundRightChannel->ioa_Length = L3443_ui_;
        L1571_ps_SoundLeftChannel->ioa_Data = L3442_puc_;
        L1572_ps_SoundRightChannel->ioa_Data = L3442_puc_;
#endif
        L1571_ps_SoundLeftChannel->ioa_Period = 72800 / L3441_i_Period;
        L1572_ps_SoundRightChannel->ioa_Period = 72800 / L3441_i_Period;
        L1571_ps_SoundLeftChannel->ioa_Volume = P0922_s_SoundVolume.Left;
        L1572_ps_SoundRightChannel->ioa_Volume = P0922_s_SoundVolume.Right;
        BeginIO(L1571_ps_SoundLeftChannel);
        BeginIO(L1572_ps_SoundRightChannel);
        L1573_ps_SoundControl->ioa_Request.io_Command = CMD_START;
        DoIO(L1573_ps_SoundControl);
}

void F0945_InitAudioData(
void
)
{
        register long L1777_l_Error;
        register int16_t L1778_i_Unreferenced;


        G3225_ps_Sound1LeftChannel = AllocMem((long)sizeof(struct IOAudio), MEMF_CHIP | MEMF_CLEAR);
        G3226_ps_Sound1RightChannel = AllocMem((long)sizeof(struct IOAudio), MEMF_CHIP | MEMF_CLEAR);
        G3227_ps_Sound1Control = AllocMem((long)sizeof(struct IOAudio), MEMF_CHIP | MEMF_CLEAR);
        G3228_ps_Sound2LeftChannel = AllocMem((long)sizeof(struct IOAudio), MEMF_CHIP | MEMF_CLEAR);
        G3229_ps_Sound2RightChannel = AllocMem((long)sizeof(struct IOAudio), MEMF_CHIP | MEMF_CLEAR);
        G3230_ps_Sound2Control = AllocMem((long)sizeof(struct IOAudio), MEMF_CHIP | MEMF_CLEAR);
        if ((G3225_ps_Sound1LeftChannel == NULL) || (G3226_ps_Sound1RightChannel == NULL) || (G3227_ps_Sound1Control == NULL)) {
                F1050_AlertCSBSystemError(0x80FF001F);
        }
        if ((G3228_ps_Sound2LeftChannel == NULL) || (G3229_ps_Sound2RightChannel == NULL) || (G3230_ps_Sound2Control == NULL)) {
                F1050_AlertCSBSystemError(0x80FF0020);
        }
        if (OpenDevice("audio.device", 0L, G3227_ps_Sound1Control, 0L) != 0) {
                F1050_AlertCSBSystemError(0x80FF0021);
        }
        G3231_ps_AudioDevice = G3227_ps_Sound1Control->ioa_Request.io_Device;
        G3232_ps_IOAudio1ReplyPort = CreatePort(NULL, 0L);
        G3233_ps_IOAudio2ReplyPort = CreatePort(NULL, 0L);
        G3234_ps_IOAudio3ReplyPort = CreatePort(NULL, 0L);
        G3235_ps_IOAudio4ReplyPort = CreatePort(NULL, 0L);
        G3236_ps_IOAudio5ReplyPort = CreatePort(NULL, 0L);
        G3237_ps_IOAudio6ReplyPort = CreatePort(NULL, 0L);
        if ((G3232_ps_IOAudio1ReplyPort == NULL) || (G3233_ps_IOAudio2ReplyPort == NULL) || (G3234_ps_IOAudio3ReplyPort == NULL)) {
                F1050_AlertCSBSystemError(0x80FF0022);
        }
        if ((G3235_ps_IOAudio4ReplyPort == NULL) || (G3236_ps_IOAudio5ReplyPort == NULL) || (G3237_ps_IOAudio6ReplyPort == NULL)) {
                F1050_AlertCSBSystemError(0x80FF0023);
        }
        G3227_ps_Sound1Control->ioa_Request.io_Message.mn_ReplyPort = G3234_ps_IOAudio3ReplyPort;
        G3227_ps_Sound1Control->ioa_Request.io_Message.mn_Node.ln_Pri = ADALLOC_MAXPREC;
        G3227_ps_Sound1Control->ioa_Request.io_Command = ADCMD_ALLOCATE;
        G3227_ps_Sound1Control->ioa_Data = &G0730_uc_AudioChannelsAllocation; /* Value 0x0F = allocate all 4 channels (one bit per channel) */
        G3227_ps_Sound1Control->ioa_Length = sizeof(G0730_uc_AudioChannelsAllocation);
        if (DoIO(G3227_ps_Sound1Control) != 0) {
                F1050_AlertCSBSystemError(0x80FF0024);
        }
#ifdef MEDIA620_A31E_A31M_A33M
        G3238_AllocatedUnits = (long)G3227_ps_Sound1Control->ioa_Request.io_Unit & 255;
#endif
        *G3225_ps_Sound1LeftChannel = *G3227_ps_Sound1Control;
        G3227_ps_Sound1Control->ioa_Request.io_Command = ADCMD_FINISH;
        G3227_ps_Sound1Control->ioa_Request.io_Flags = IOF_QUICK;
        G3227_ps_Sound1Control->ioa_Request.io_Unit = (struct Unit*)(4 | 8); /* Channel 2 & 3 */
        G3225_ps_Sound1LeftChannel->ioa_Request.io_Command = CMD_WRITE;
        G3225_ps_Sound1LeftChannel->ioa_Request.io_Flags = IOF_QUICK | ADIOF_PERVOL;
        G3225_ps_Sound1LeftChannel->ioa_Volume = 64;
        G3225_ps_Sound1LeftChannel->ioa_Cycles = 1;
        G3225_ps_Sound1LeftChannel->ioa_Request.io_Unit = (struct Unit*)8; /* Channel 3 produce sound on left side */
        *G3226_ps_Sound1RightChannel = *G3225_ps_Sound1LeftChannel;
        G3226_ps_Sound1RightChannel->ioa_Request.io_Message.mn_ReplyPort = G3233_ps_IOAudio2ReplyPort;
        G3226_ps_Sound1RightChannel->ioa_Request.io_Unit = (struct Unit*)4; /* Channel 2 produce sound on right side */
        *G3230_ps_Sound2Control = *G3227_ps_Sound1Control;
        G3230_ps_Sound2Control->ioa_Request.io_Unit = (struct Unit*)(1 | 2); /* Channel 0 & 1 */
        *G3228_ps_Sound2LeftChannel = *G3225_ps_Sound1LeftChannel;
        G3228_ps_Sound2LeftChannel->ioa_Request.io_Message.mn_ReplyPort = G3235_ps_IOAudio4ReplyPort;
        G3228_ps_Sound2LeftChannel->ioa_Request.io_Unit = (struct Unit*)1; /* Channel 0 produce sound on left side */
        *G3229_ps_Sound2RightChannel = *G3226_ps_Sound1RightChannel;
        G3229_ps_Sound2RightChannel->ioa_Request.io_Message.mn_ReplyPort = G3236_ps_IOAudio5ReplyPort;
        G3229_ps_Sound2RightChannel->ioa_Request.io_Unit = (struct Unit*)2; /* Channel 1 produce sound on right side */
        G3239_PendingSound1Index = -1;
        G3240_PendingSound2Index = -1;
}

void F0946_ReleaseAudioData(
void
)
{
        if (G3231_ps_AudioDevice) {
                CloseDevice(G3227_ps_Sound1Control);
        }
        G3231_ps_AudioDevice = NULL;
        if (G3225_ps_Sound1LeftChannel) {
                FreeMem(G3225_ps_Sound1LeftChannel, (long)sizeof(struct IOAudio));
        }
        G3225_ps_Sound1LeftChannel = NULL;
        if (G3226_ps_Sound1RightChannel) {
                FreeMem(G3226_ps_Sound1RightChannel, (long)sizeof(struct IOAudio));
        }
        G3226_ps_Sound1RightChannel = NULL;
        if (G3227_ps_Sound1Control) {
                FreeMem(G3227_ps_Sound1Control, (long)sizeof(struct IOAudio));
        }
        G3227_ps_Sound1Control = NULL;
        if (G3228_ps_Sound2LeftChannel) {
                FreeMem(G3228_ps_Sound2LeftChannel, (long)sizeof(struct IOAudio));
        }
        G3228_ps_Sound2LeftChannel = NULL;
        if (G3229_ps_Sound2RightChannel) {
                FreeMem(G3229_ps_Sound2RightChannel, (long)sizeof(struct IOAudio));
        }
        G3229_ps_Sound2RightChannel = NULL;
        if (G3230_ps_Sound2Control) {
                FreeMem(G3230_ps_Sound2Control, (long)sizeof(struct IOAudio));
        }
        G3230_ps_Sound2Control = NULL;
        if (G3232_ps_IOAudio1ReplyPort) {
                DeletePort(G3232_ps_IOAudio1ReplyPort);
        }
        G3232_ps_IOAudio1ReplyPort = NULL;
        if (G3233_ps_IOAudio2ReplyPort) {
                DeletePort(G3233_ps_IOAudio2ReplyPort);
        }
        G3233_ps_IOAudio2ReplyPort = NULL;
        if (G3234_ps_IOAudio3ReplyPort) {
                DeletePort(G3234_ps_IOAudio3ReplyPort);
        }
        G3234_ps_IOAudio3ReplyPort = NULL;
        if (G3235_ps_IOAudio4ReplyPort) {
                DeletePort(G3235_ps_IOAudio4ReplyPort);
        }
        G3235_ps_IOAudio4ReplyPort = NULL;
        if (G3236_ps_IOAudio5ReplyPort) {
                DeletePort(G3236_ps_IOAudio5ReplyPort);
        }
        G3236_ps_IOAudio5ReplyPort = NULL;
        if (G3237_ps_IOAudio6ReplyPort) {
                DeletePort(G3237_ps_IOAudio6ReplyPort);
        }
        G3237_ps_IOAudio6ReplyPort = NULL;
}

#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
void F0504_SOUND_ReadSecondSector_CPSD(P0936_puc_) /* Corresponds to S0465_CPSD_ReadSector247 in Atari ST */
unsigned char* P0936_puc_;
{
        asm {
                        movem.l d3-d7/a2-a6,-(sp)
                @Tloc_3910:
                        bset    #0,G3171_c_ReadingCopyProtectionSector_CPSX(a5)
                        bne.s   @Tloc_3910
                @Tloc_3918:
                        movea.l G1053_ps_DiscResourceUnit_CPSX(a5),a1
                        movea.l G1054_ps_DiskResource_CPSX(a5),a6
                        jsr     -0x12(a6)       /* disk.resource/GetUnit (see DR_GETUNIT in disk.h) */
                        tst.l   d0
                        bne.s   @Tloc_3942
                        movea.l ExecBase(a5),a6
                        movea.l G1053_ps_DiscResourceUnit_CPSX(a5),a4
                        movea.l 0xE(a4),a0
                        jsr     -0x180(a6)
                        movea.l 0xE(a4),a0
                        jsr     -0x174(a6)
                        bra.s   @Tloc_3918
                @Tloc_3942:
                        movem.l (sp)+,d3-d7/a2-a6
        }
        ciab.ciaprb = CIAF_DSKSEL3 | CIAF_DSKSEL2 | CIAF_DSKSEL1 | CIAF_DSKSEL0 | CIAF_DSKDIREC | CIAF_DSKSTEP; /* 0x7B. MOTOR ON, do not select any drive, select head 0 */
        ciab.ciaprb = CIAF_DSKSEL3 | CIAF_DSKSEL2 | CIAF_DSKSEL1 | CIAF_DSKDIREC | CIAF_DSKSTEP; /* 0x73. Select drive 0 (internal drive) */
        custom.dskpt = (APTR)P0936_puc_; /* Address of disk DMA data */
        G0625_i_FloppyDriveDMATimeout_CPSDF = 1;
        G0314_i_SectorsReadRequested_CPSDF = MASK0x0004_CPS_SECOND_SECTOR;
}
#endif
#endif
#endif

#ifdef MEDIA351_G14ED_G20E_G21E
BOOLEAN F2184_(
P6078_)
int16_t P6078_ FINAL_SEPARATOR
{
        BOOLEAN L6441_;


        asm {
                SEP     #0x30
                LDX     P6078_
                LDA     >G4039_ac_,X
                STA     L6441_
                STZ     L6441_+1
                REP     #0x30
        }
        return L6441_;
}

void F2185_(
P6076_, P6077_)
int16_t P6076_ SEPARATOR
int16_t P6077_ FINAL_SEPARATOR
{
        asm {
                SEP     #0x30
                LDX     P6077_
                LDA     P6076_
                STA     >G4039_ac_,X
                REP     #0x30
        }
}

void F2186_(
P6079_)
int16_t P6079_ FINAL_SEPARATOR
{
        asm {
                SEP     #0x30
                LDX     P6079_
                LDA     #0xff
                STA     >G4039_ac_,X
                REP     #0x30
        }
}

void F2187_(
P6075_i_SoundIndex)
int16_t P6075_i_SoundIndex FINAL_SEPARATOR
{
        unsigned int16_t L6440_;


        if (G4035_ai_[P6075_i_SoundIndex] >= 0) {
                asm {
                                SEP     #0x30
                        L083159:
                                LDA     0xc03c          /* SOUNDCTL */
                                BMI     L083159
                                STZ     0xc03c          /* SOUNDCTL */
                                LDX     P6075_i_SoundIndex
                                CLC
                                LDA     #0xa0
                                ADC     >G4034_ac_,X
                                STA     0xc03e          /* SOUNDADRL */
                                LDA     #0x01
                                STA     0xc03d          /* SOUNDDATA */
                                STA     0xc03d          /* SOUNDDATA */
                                REP     #0x30
                }
                for (L6440_ = G4035_ai_[P6075_i_SoundIndex]; L6440_ < G4035_ai_[P6075_i_SoundIndex] + G4041_ac_[P6075_i_SoundIndex]; L6440_++) {
                        F2186_(L6440_);
                }
                G4035_ai_[P6075_i_SoundIndex] = -1;
        }
}

BOOLEAN F2188_(
P6074_i_SoundIndex)
int16_t P6074_i_SoundIndex FINAL_SEPARATOR
{
        int16_t L6433_;
        int16_t L6434_;
        unsigned int16_t L6435_;
        int16_t L6437_;
        unsigned int16_t L6436_;
        int16_t L6439_Unreferenced;
        unsigned int16_t L6432_;
        long L6438_;


        if (G4035_ai_[P6074_i_SoundIndex] >= 0) {
                return C0_FALSE;
        }
        L6432_ = G4038_ai_[P6074_i_SoundIndex];
        if (L6432_ <= 256) {
                L6433_ = 0;
        } else {
                if (L6432_ <= 512) {
                        L6433_ = 1;
                } else {
                        if (L6432_ <= 1024) {
                                L6433_ = 2;
                        } else {
                                if (L6432_ <= 2048) {
                                        L6433_ = 3;
                                } else {
                                        if (L6432_ <= 4096) {
                                                L6433_ = 4;
                                        } else {
                                                if (L6432_ <= 8192) {
                                                        L6433_ = 5;
                                                } else {
                                                        if (L6432_ <= 16384) {
                                                                L6433_ = 6;
                                                        } else {
                                                                L6433_ = 7;
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
        L6434_ = L6432_ >> 8;
        if (L6432_ & 0xFF) {
                L6434_++;
        }
        L6435_ = 0;
        for (;;) {
                while (L6435_ < 256) {
                        if (F2184_(L6435_) != 0xFF) {
                                L6435_ += G4040_ac_[L6433_];
                        } else {
                                L6436_ = 0;
                                for (L6437_ = L6435_; L6437_ < L6435_ + L6434_; L6437_++) {
                                        if (F2184_(L6437_) != 0xFF) {
                                                L6436_++;
                                                break;
                                        }
                                }
                                if (L6436_ == 0) {
                                        for (L6437_ = L6435_; L6437_ < L6435_ + L6434_; L6437_++) {
                                                F2185_(P6074_i_SoundIndex, L6437_);
                                        }
                                        G4041_ac_[P6074_i_SoundIndex] = L6434_;
                                        G4035_ai_[P6074_i_SoundIndex] = L6435_;
                                        G4036_ac_[P6074_i_SoundIndex] = L6433_ | (L6433_ << 3);
                                        return C1_TRUE;
                                } else {
                                        L6435_ += G4040_ac_[L6433_];
                                }
                        }
                }
                L6437_ = L6435_ = 0;
                L6438_ = G4030_al_[F2184_(0)];
                while (L6435_ < 256) {
                        if ((L6436_ = F2184_(L6435_)) != 0xFF) {
                                if (G4030_al_[L6436_] < L6438_) {
                                        L6437_ = L6435_;
                                        L6438_ = G4030_al_[F2184_(L6435_)];
                                }
                        } else {
                                L6437_ = L6435_;
                                break;
                        }
                        L6435_ += G4040_ac_[L6433_];
                }
                for (L6436_ = L6437_; L6436_ < L6437_ + L6434_; L6436_++) {
                        if (F2184_(L6436_) != 0xFF) {
                                F2187_(F2184_(L6436_));
                        }
                }
                L6435_ = L6437_;
        }
}

void F0062_SOUND_Initialize(
)
{
        unsigned int16_t L6428_ui_SoundIndex;
        unsigned int16_t L6429_;
        int16_t L6431_;
        unsigned int16_t* L6430_;


        for (L6428_ui_SoundIndex = 0; L6428_ui_SoundIndex < M513_SOUND_COUNT; L6428_ui_SoundIndex++) {
                G4035_ai_[L6428_ui_SoundIndex] = -1;
                L6429_ = G0060_as_Graphic562_Sounds[L6428_ui_SoundIndex].GraphicIndex;
                L6430_ = (unsigned int16_t*)F0468_MEMORY_Allocate((long)G0634_pui_GraphicCompressedByteCount[L6429_], C1_ALLOCATION_PERMANENT);
                F0490_MEMORY_LoadDecompressAndExpandGraphic(L6429_ | MASK0x8000_NOT_EXPANDED, L6430_, 0, 0);
                asm {
                        LDA     [L6430_]
                        XBA
                        CLC
                        ADC     #1
                        STA     L6431_
                }
                G4038_ai_[L6428_ui_SoundIndex] = L6431_;
                L6430_ += 1;
                G4037_apui_[L6428_ui_SoundIndex] = L6430_;
        }
        for (L6429_ = 0; L6429_ < 256; L6429_++) {
                G4039_ac_[L6429_] = -1;
        }
        asm {
                        SEP     #0x30
                L08365A:
                        LDA     0xc03c          /* SOUNDCTL */
                        BMI     L08365A
                        STZ     0xc03c          /* SOUNDCTL */
                        LDA     #0xe1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     #0xff
                        STA     0xc03d          /* SOUNDDATA */
                        REP     #0x30
        }
}

void F2189_(
P6073_i_SoundIndex)
int16_t P6073_i_SoundIndex FINAL_SEPARATOR
{
        int16_t L6426_i_Unreferenced;
        int16_t L6425_;
        char L6427_[6];
        unsigned int16_t* L6424_;


        L6424_ = G4037_apui_[P6073_i_SoundIndex];
        L6425_ = G4038_ai_[P6073_i_SoundIndex] - 1;
        if (F2188_(P6073_i_SoundIndex) == 0) {
                return;
        }
        asm {
                        SEP     #0x30
                L0836D6:
                        LDA     0xc03c          /* SOUNDCTL */
                        BMI     L0836D6
                        LDA     #0x60
                        STA     0xc03c          /* SOUNDCTL */
                        LDA     P6073_i_SoundIndex
                        ASL     A
                        TAX
                        LDA     >G4035_ai_,X
                        STA     0xc03f          /* SOUNDADRH */
                        STZ     0xc03e          /* SOUNDADRL */
                L0836EE:
                        LDA     [L6424_]
                        CLC
                        ADC     #0x80
                        BNE     L0836F6
                        INC     A
                L0836F6:
                        STA     0xc03d          /* SOUNDDATA */
                        REP     #0x30
                        INC     L6424_
                        BNE     L083701
                        INC     L6424_+2
                L083701:
                        DEC     L6425_
                        SEP     #0x30
                        BNE     L0836EE
                        STZ     0xc03d          /* SOUNDDATA */
                        REP     #0x30
        }
}
#endif

#ifdef MEDIA062_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_X30J
void F0060_SOUND_Play_CPSX(
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
P0084_puc_SoundData, P0085_i_Period, P0086_i_SoundVolume)
#endif
#ifdef MEDIA353_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0921_ui_SoundIndex, P0085_i_Period, P0922_s_SoundVolume)
#endif
#ifdef MEDIA164_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J
unsigned char*   P0084_puc_SoundData SEPARATOR
#endif
#ifdef MEDIA353_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
unsigned int16_t P0921_ui_SoundIndex SEPARATOR
#endif
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
REGISTER int16_t P0085_i_Period      SEPARATOR
#endif
#ifdef MEDIA577_X30J
unsigned int16_t P0085_i_Period      SEPARATOR
#endif
#ifdef MEDIA164_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J
int16_t          P0086_i_SoundVolume FINAL_SEPARATOR
#endif
#ifdef MEDIA353_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
SOUND_VOLUME     P0922_s_SoundVolume FINAL_SEPARATOR
#endif
{
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
        REGISTER long L0038_l_NextSoundSample; /* 'Virtual' address of the first sound sample: the quotient of this value divided by 2 is the address of the byte containing the sample, the remainder of the same division determines which 4 bit sample is addressed in the byte */
        REGISTER int16_t L0039_i_RemainingSoundSampleCount;
        int16_t L0040_i_PSGModeRegisterBackup;
        long L0041_l_SupervisorStack;


        L0038_l_NextSoundSample = (long)P0084_puc_SoundData;
        L0038_l_NextSoundSample = (L0038_l_NextSoundSample + 2) * 2; /* The first two bytes contain the sample count. There are two samples per byte of sound data */
        L0039_i_RemainingSoundSampleCount = ((unsigned char)P0084_puc_SoundData[0] << 8) | (unsigned char)P0084_puc_SoundData[1];
        Giaccess(0, MASK0x0080_WRITE_PSG_REGISTER | C11_REGISTER_PSG_WAVEFORM_FREQUENCY_LOW_BYTE);
        Giaccess(0, MASK0x0080_WRITE_PSG_REGISTER | C12_REGISTER_PSG_WAVEFORM_FREQUENCY_HIGH_BYTE);
        L0040_i_PSGModeRegisterBackup = Giaccess(C0_RESERVED, MASK0x0000_READ_PSG_REGISTER | C07_REGISTER_PSG_MODE);
        Giaccess(L0040_i_PSGModeRegisterBackup | C0x003F_MODE_ENABLE_TONE_AND_WHITE_NOISE_CHANNELS_A_B_C, MASK0x0080_WRITE_PSG_REGISTER | C07_REGISTER_PSG_MODE);
        if (P0085_i_Period > 10) {
                L0041_l_SupervisorStack = Super(0L);
                asm {
                        lea     0xFFFFFA1F,A0           /* Address of MFP-ST Timer A Data Register */
                        move.b  P0085_i_Period,(A0)      /* Write to Timer A Data Register
                                                                Value 145 (used for the Door sound on the main menu): 2.4576 MHz / (4 * 145) = 4237 Hz
                                                                Value 112 (used for all other sounds): 2.4576 MHz / (4 * 112) = 5486 Hz */
                }
                Super(L0041_l_SupervisorStack);
        }
        asm {
                lea     V0061002_NextSoundSample(PC),A0
                move.l  L0038_l_NextSoundSample,(A0)
                lea     V0061000_RemainingSoundSampleCount(PC),A0
                move.w  L0039_i_RemainingSoundSampleCount,(A0)
                lea     V0061001_SoundSampleRepeatCount(PC),A0
                clr.w   (A0)
        }
        if (P0086_i_SoundVolume == 0) {
                asm {
                        lea     V0061005_ChannelsAmplitudeTableSoft(PC),A0
                        lea     V0061003_CurrentChannelsAmplitudeTable(PC),A1
                        move.l  A0,(A1)                                                 /* Set current channels amplitude to table B (soft) */
                }
        } else {
                asm {
                        lea     V0061004_ChannelsAmplitudeTableLoud(PC),A0
                        lea     V0061003_CurrentChannelsAmplitudeTable(PC),A1
                        move.l  A0,(A1)                                                 /* Set current channels amplitude to table A (loud) */
                }
        }
        Jenabint(C13_INTERRUPT_TIMER_A);
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
        int16_t L6422_;
        unsigned char L6421_uc_VolumeLeft;
        unsigned char L6423_uc_VolumeRight;


#ifndef NOCOPYPROTECTION
        if (G4028_B_PreventSoundPlay_CPSX) {
                return;
        } else {
                if (++G4029_ui_PlayedSoundCount_CPSX >= 200) {
                        G4029_ui_PlayedSoundCount_CPSX = 0;
                        if ((F0413_CPSC_GetChecksumEor(F2207_CPSX, F2198_CPSX) != CHECKSUM_I) ||
                            (F0413_CPSC_GetChecksumEor(F0527_FLOPPY_ReadFuzzySector_CPSX, F2200_CPSX) != CHECKSUM_J) ||
                            !F2152_CPSX(F0527_FLOPPY_ReadFuzzySector_CPSX, F2201_CPSX) ||
                            !F2151_CPSX(F2207_CPSX, F2199_CPSX) ||
                            !F2151_CPSX(F0527_FLOPPY_ReadFuzzySector_CPSX, F2202_CPSX)) {
                                G4028_B_PreventSoundPlay_CPSX = C1_TRUE;
                        }
                }
        }
#endif
        F2189_(P0921_ui_SoundIndex);
        G4030_al_[P0921_ui_SoundIndex] = ++G4031_l_;
        L6421_uc_VolumeLeft = (P0922_s_SoundVolume.Left / 15) * G3953_i_SoundVolumeLevel;
        L6423_uc_VolumeRight = (P0922_s_SoundVolume.Right / 15) * G3953_i_SoundVolumeLevel;
        asm {
                        SEP     #0x30
                L08384E:
                        LDA     0xc03c          /* SOUNDCTL */
                        BMI     L08384E
                        LDA     #0x20
                        STA     0xc03c          /* SOUNDCTL */
                        LDA     >G4033_c_
                        TAX
                        INC     A
                        CMP     #0x0f
                        BCC     L083864
                        LDA     #0x00
                L083864:
                        STA     >G4033_c_
                        TXA
                        ASL     A
                        STA     L6422_+1
                        LDX     #0x00
                L08386E:
                        LDA     >G4034_ac_,X
                        CMP     L6422_+1
                        BNE     L08387C
                        LDA     #0xff
                        STA     >G4034_ac_,X
                L08387C:
                        INX
                        CPX     #M513_SOUND_COUNT
                        BCC     L08386E
                        LDX     P0921_ui_SoundIndex
                        LDA     L6422_+1
                        STA     >G4034_ac_,X
                        CLC
                        LDA     #0xa0
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     #0x01
                        STA     0xc03d          /* SOUNDDATA */
                        STA     0xc03d          /* SOUNDDATA */
                        CLC
                        LDA     #0x80
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        PHX
                        LDA     P0921_ui_SoundIndex
                        ASL     A
                        TAX
                        LDA     >G4035_ai_,X
                        STA     0xc03d          /* SOUNDDATA */
                        STA     0xc03d          /* SOUNDDATA */
                        PLX
                        CLC
                        LDA     #0xc0
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     >G4036_ac_,X
                        STA     0xc03d          /* SOUNDDATA */
                        STA     0xc03d          /* SOUNDDATA */
                        CLC
                        LDA     #0x40
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     L6421_uc_VolumeLeft
                        STA     0xc03d          /* SOUNDDATA */
                        LDA     L6423_uc_VolumeRight
                        STA     0xc03d          /* SOUNDDATA */
                        CLC
                        LDA     #0x00
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     P0085_i_Period
                        STA     0xc03d          /* SOUNDDATA */
                        STA     0xc03d          /* SOUNDDATA */
                        CLC
                        LDA     #0x20
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     P0085_i_Period+1
                        STA     0xc03d          /* SOUNDDATA */
                        STA     0xc03d          /* SOUNDDATA */
                        CLC
                        LDA     #0xa0
                        ADC     L6422_+1
                        STA     0xc03e          /* SOUNDADRL */
                        LDA     #0x02
                        STA     0xc03d          /* SOUNDDATA */
                        LDA     #0x12
                        STA     0xc03d          /* SOUNDDATA */
                        REP     #0x30
        }
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        REGISTER SOUND_DATA_AMIGA* L1570_ps_SoundData;
        REGISTER struct IOAudio* L1571_ps_SoundLeftChannel;
        REGISTER struct IOAudio* L1572_ps_SoundRightChannel;
        REGISTER struct IOAudio* L1573_ps_SoundControl;


        L1570_ps_SoundData = &G1022_as_SoundData[P0921_ui_SoundIndex];
        if (CheckIO(G0734_ps_AmigaData->Sound1LeftChannel)) {
                L1571_ps_SoundLeftChannel = G0734_ps_AmigaData->Sound1LeftChannel;
                L1572_ps_SoundRightChannel = G0734_ps_AmigaData->Sound1RightChannel;
                L1573_ps_SoundControl = G0734_ps_AmigaData->Sound1Control;
                G1024_as_SoundVolume[0].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[0].Right = P0922_s_SoundVolume.Right;
        } else {
                if (CheckIO(G0734_ps_AmigaData->Sound2LeftChannel)) {
                        L1571_ps_SoundLeftChannel = G0734_ps_AmigaData->Sound2LeftChannel;
                        L1572_ps_SoundRightChannel = G0734_ps_AmigaData->Sound2RightChannel;
                        L1573_ps_SoundControl = G0734_ps_AmigaData->Sound2Control;
                        G1024_as_SoundVolume[1].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[1].Right = P0922_s_SoundVolume.Right;
                } else {
#ifdef MEDIA433_A20E_A20F_A20G_A21E_A22E_A22G
                        if (F0502_SOUND_CompareVolumes(&G1024_as_SoundVolume[0], &G1024_as_SoundVolume[1]) == 1) {
#endif
#ifdef MEDIA410_A20ED
                        if ((P0922_s_SoundVolume.Left + P0922_s_SoundVolume.Right > G1024_as_SoundVolume[0].Left + G1024_as_SoundVolume[0].Right) || (P0922_s_SoundVolume.Left + P0922_s_SoundVolume.Right > G1024_as_SoundVolume[1].Left + G1024_as_SoundVolume[1].Right)) {
                                if (G1024_as_SoundVolume[0].Left + G1024_as_SoundVolume[0].Right > G1024_as_SoundVolume[1].Left + G1024_as_SoundVolume[1].Right) {
#endif
                                        L1571_ps_SoundLeftChannel = G0734_ps_AmigaData->Sound2LeftChannel;
                                        L1572_ps_SoundRightChannel = G0734_ps_AmigaData->Sound2RightChannel;
                                        L1573_ps_SoundControl = G0734_ps_AmigaData->Sound2Control;
                                        G1024_as_SoundVolume[1].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[1].Right = P0922_s_SoundVolume.Right;
                                } else {
                                        L1571_ps_SoundLeftChannel = G0734_ps_AmigaData->Sound1LeftChannel;
                                        L1572_ps_SoundRightChannel = G0734_ps_AmigaData->Sound1RightChannel;
                                        L1573_ps_SoundControl = G0734_ps_AmigaData->Sound1Control;
                                        G1024_as_SoundVolume[0].Left = P0922_s_SoundVolume.Left, G1024_as_SoundVolume[0].Right = P0922_s_SoundVolume.Right;
#ifdef MEDIA433_A20E_A20F_A20G_A21E_A22E_A22G
                        }
#endif
#ifdef MEDIA410_A20ED
                                }
                        } else {
                                return;
                        }
#endif
                }
        }
        L1573_ps_SoundControl->ioa_Request.io_Command = ADCMD_FINISH;
        BeginIO(L1573_ps_SoundControl);
        WaitIO(L1571_ps_SoundLeftChannel);
        WaitIO(L1572_ps_SoundRightChannel);
        L1573_ps_SoundControl->ioa_Request.io_Command = CMD_STOP;
        DoIO(L1573_ps_SoundControl);
        L1571_ps_SoundLeftChannel->ioa_Length = L1570_ps_SoundData->ByteCount;
        L1572_ps_SoundRightChannel->ioa_Length = L1570_ps_SoundData->ByteCount;
        L1571_ps_SoundLeftChannel->ioa_Data = L1570_ps_SoundData->FirstSample;
        L1572_ps_SoundRightChannel->ioa_Data = L1570_ps_SoundData->FirstSample;
        L1571_ps_SoundLeftChannel->ioa_Period = 72800 / P0085_i_Period;
        L1572_ps_SoundRightChannel->ioa_Period = 72800 / P0085_i_Period;
        L1571_ps_SoundLeftChannel->ioa_Volume = P0922_s_SoundVolume.Left;
        L1572_ps_SoundRightChannel->ioa_Volume = P0922_s_SoundVolume.Right;
        BeginIO(L1571_ps_SoundLeftChannel);
        BeginIO(L1572_ps_SoundRightChannel);
        L1573_ps_SoundControl->ioa_Request.io_Command = CMD_START;
        DoIO(L1573_ps_SoundControl);
#endif
#ifdef MEDIA577_X30J
        asm {
                        moveq   #0,d1
                        moveq   #0x67,d0        /* ADPCMMOD */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
                        movea.l P0084_puc_SoundData(a6),a1    /* Address of sound data */
                        moveq   #0,d2
                        move.w  (a1)+,d2        /* First word: 4 bit sample count */
                        addq.w  #1,d2
                        lsr.w   #1,d2           /* Byte count = sample count / 2 */
                        cmpi.w  #130,P0085_i_Period(a6)
                        bpl.s   @Tloc_1E986
                        move.w  #0x103,d1       /* Argument d1.w wait mode / frequency / output mode */
                                                /* bit 15: wait mode (0: Normal 1: no wait) */
                                                /* bit 10-8 sampling frequency (0: 3.9kHz, 1: 5.2kHz, 2: 7.8kHz, 3: 10.4kHz, 4: 15.6kHz) */
                                                /* bit 1-0 output mode (0: None, 1: left, 2: right, 3: left and right) */
                        bra.s   @Tloc_1E98A
                @Tloc_1E986:
                        move.w  #3,d1
                @Tloc_1E98A:
                        moveq   #0x60,d0        /* ADPCMOUT */
                        trap    #0xF            /* TRAP 15 (IOCS CALL) */
        }
#endif
}
#endif

#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
void F0061_SOUND_SetChannelAmplitudes(
P0087_i_AmplitudeIndex)
REGISTER int16_t P0087_i_AmplitudeIndex FINAL_SEPARATOR
{
        REGISTER long L0042_l_Multiple;
#define AL0042_l_SupervisorStack        L0042_l_Multiple
#define AL0042_l_SoundSampleRepeatCount L0042_l_Multiple


        AL0042_l_SupervisorStack = Super(0L);
        asm {
                and.w   #0x000F,P0087_i_AmplitudeIndex                                  /* This function is called only once with P0087_i_AmplitudeIndex = 8 */
                add.b   P0087_i_AmplitudeIndex,P0087_i_AmplitudeIndex
                add.b   P0087_i_AmplitudeIndex,P0087_i_AmplitudeIndex                   /* P0087_i_AmplitudeIndex is now 4 * AmplitudeIndex = 32 */
                movea.w #0x8800,A1                                                      /* Address of the PSG Register Select */
                lea     V0061004_ChannelsAmplitudeTableLoud(PC),A0
                move.l  -64(A0,P0087_i_AmplitudeIndex),(A1)                             /* Writing a long writes both a word for PSG Register Select and a word for PSG Write Data Channel A amplitude (0 for P0087_i_AmplitudeIndex = 32) */
                move.l  0(A0,P0087_i_AmplitudeIndex),(A1)                               /* Channel B amplitude (0 for P0087_i_AmplitudeIndex = 32) */
                move.l  64(A0,P0087_i_AmplitudeIndex),(A1)                              /* Channel C amplitude (0 for P0087_i_AmplitudeIndex = 32) */
                bra     T0061017
                E0061_SOUND_Exception77Handler_TimerA_SoundPlayer:
                        movem.l D7/A0-A1,-(A7)
                        lea     V0061000_RemainingSoundSampleCount(PC),A0
                        tst.w   (A0)
                        beq.s   T0061005                                                /* If there are no sound samples left to play */
                        subq.w  #1,(A0)+                                                /* There is now one less sample to play. Increment A0 which is now the address of V0061001_SoundSampleRepeatCount */
                        tst.w   (A0)+                                                   /* Test V0061001_SoundSampleRepeatCount and increment A0 which is now the address of V0061002_NextSoundSample */
                        beq.s   T0061002                                                /* Branch if V0061001_SoundSampleRepeatCount = 0 */
                        subq.w  #1,-(A0)                                                /* Decrement V0061001_SoundSampleRepeatCount */
                        bra.s   T0061003
                T0061002:
                        move.l  (A0),D7                                                 /* Address of the next sound sample to play */
                        addq.l  #1,(A0)+                                                /* Proceed to next sound sample to play. Increment A0 which is now the address of V0061003_CurrentChannelsAmplitudeTable */
                        lsr.l   #1,D7                                                   /* Address of the byte containing the next sound sample to play. Bits are shifted to the right, Bit 0 is removed and placed in C and X bits in the CCR. Bit 31 is set to 0 */
                        bcs.s   T0061004                                                /* Branch if bit 0 of the address was 1. The second 4 bits sound sample in the byte should be played (least significant bits) */
                        movea.l D7,A1                                                   /* Address of the byte containing the next sound sample to play */
                        move.b  (A1),D7                                                 /* Byte containing the sound sample to play */
                        lsr.b   #2,D7                                                   /* Sample value multiplied by 4 (each entry in the table is 4 bytes large) */
                        andi.w  #0x003C,D7                                              /* The 4 bits sound sample to play (most significant nibble) */
                        beq.s   T0061006                                                /* If sound sample = 0. If the previous sample should be repeated */
                        movea.w #0x8800,A1                                              /* Address of the PSG Register Select */
                        movea.l (A0),A0                                                 /* V0061003_CurrentChannelsAmplitudeTable */
                        move.l  -64(A0,D7.w),(A1)                                       /* Writing a long writes both a word for PSG Register Select and a word for PSG Write Data. Channel A amplitude */
                        move.l  0(A0,D7.w),(A1)                                         /* Channel B amplitude */
                        move.l  64(A0,D7.w),(A1)                                        /* Channel C amplitude */
                T0061003:
                        bclr    #0x05,0xFFFFFA0F                                        /* Clear bit 5 on MFP 'Interrupt In-Service Register A' to indicate that the Timer A interrupt processing is complete */
                        movem.l (A7)+,D7/A0-A1
                        rte
                T0061004:
                        movea.l D7,A1
                        move.b  (A1),D7
                        andi.w  #0x000F,D7                                              /* Get least significant bits of the sound data byte */
                        beq.s   T0061006                                                /* If the sound sample should be repeated */
                        add.b   D7,D7
                        add.b   D7,D7                                                   /* Sample value multiplied by 4 (each entry in the table is 4 bytes large) */
                        movea.w #0x8800,A1                                              /* Address of the PSG Register Select */
                        movea.l (A0),A0                                                 /* V0061003_CurrentChannelsAmplitudeTable */
                        move.l  -64(A0,D7.w),(A1)                                       /* Writing a long writes both a word for PSG Register Select and a word for PSG Write Data. Channel A amplitude */
                        move.l  0(A0,D7.w),(A1)                                         /* Channel B amplitude */
                        move.l  64(A0,D7.w),(A1)                                        /* Channel C amplitude */
                        bclr    #0x05,0xFFFFFA0F                                        /* Clear bit 5 on MFP 'Interrupt In-Service Register A' to indicate that the Timer A interrupt processing is complete */
                        movem.l (A7)+,D7/A0-A1
                        rte
                T0061005:
                        bclr    #5,0xFFFFFA07                                           /* Clear bit 5 on MFP 'Interrupt Enable Register A' to disable Timer A interrupts. This marks the end of processing for playing a sound */
                        bclr    #5,0xFFFFFA0F                                           /* Clear bit 5 on MFP 'Interrupt In-Service Register A' to indicate that the Timer A interrupt processing is complete */
                        movem.l (A7)+,D7/A0-A1
                        rte
                T0061006:
                        move.l  AL0042_l_SupervisorStack,-(A7)
                        clr.w   AL0042_l_SoundSampleRepeatCount
                T0061007:
                        lea     V0061002_NextSoundSample(PC),A0
                        move.l  (A0),D7
                        addq.l  #1,(A0)                                                 /* Proceed to the next sound sample */
                        lsr.l   #1,D7                                                   /* Address of the byte containing the next sound sample to play */
                        bcs.s   T0061008                                                /* Branch if bit 0 of the address was 1. The second 4 bit sound sample in the byte should be played */
                        movea.l D7,A1
                        move.b  (A1),D7
                        lsr.b   #4,D7                                                   /* Get 4 most significant bits */
                        bra.s   T0061009
                T0061008:
                        movea.l D7,A1
                        move.b  (A1),D7                                                 /* Get 4 least significant bits */
                T0061009:
                        btst    #3,D7
                        beq.s   T0061010                                                /* Branch if bit 3 of D7 = 0. If the repeat count value is complete */
                        and.w   #0x0007,D7                                              /* Keep only the 3 bits of the repeat count */
                        lsl.w   #3,AL0042_l_SoundSampleRepeatCount                      /* Make room in AL0042_l_SoundSampleRepeatCount for the next 3 bits */
                        add.w   D7,AL0042_l_SoundSampleRepeatCount                      /* Place the 3 bits in AL0042_l_SoundSampleRepeatCount */
                        bra.s   T0061007
                T0061010:
                        lsl.w   #3,AL0042_l_SoundSampleRepeatCount                      /* Make room in AL0042_l_SoundSampleRepeatCount for the last 3 bits */
                        and.w   #0x0007,D7
                        add.w   D7,AL0042_l_SoundSampleRepeatCount                      /* Place the last 3 bits in AL0042_l_SoundSampleRepeatCount */
                        addq.w  #2,AL0042_l_SoundSampleRepeatCount
                        lea     V0061001_SoundSampleRepeatCount(PC),A0
                        move.w  AL0042_l_SoundSampleRepeatCount,(A0)                    /* Set the repeat count value. The sound sample is played AL0042_l_SoundSampleRepeatCount + 3 times total (the first time + repeat count + 2) */
                        bclr    #5,0xFFFFFA0F                                           /* Clear bit 5 on MFP 'Interrupt In-Service Register A' to indicate that the Timer A interrupt processing is complete */
                        move.l  (A7)+,AL0042_l_SupervisorStack
                        movem.l (A7)+,D7/A0-A1
                        rte
                V0061000_RemainingSoundSampleCount:
                        dc.w    0x0000
                V0061001_SoundSampleRepeatCount:
                        dc.w    0x0000
                V0061002_NextSoundSample:
                        dc.l    0x00000000
                V0061003_CurrentChannelsAmplitudeTable:
                        dc.l    0x00000000
                        dc.w    0x0800,0x0000,0x0800,0x0800                             /* 192 bytes: 3x64 bytes for channel A (register 8), channel B (register 9) and channel C (register 10) */
                        dc.w    0x0800,0x0A00,0x0800,0x0B00                             /* Accessed using V0061004_ChannelsAmplitudeTableLoud with offsets -64, 0 and 64 */
                        dc.w    0x0800,0x0C00,0x0800,0x0D00                             /* First byte: Register number (for channel selection) */
                        dc.w    0x0800,0x0D00,0x0800,0x0D00                             /* Third byte: Channel A/B/C amplitude */
                        dc.w    0x0800,0x0E00,0x0800,0x0E00                             /* Second and fourth bytes are padding 0x00 as data is written as longs to write to both PSG addresses in one instruction */
                        dc.w    0x0800,0x0E00,0x0800,0x0E00
                        dc.w    0x0800,0x0F00,0x0800,0x0E00
                        dc.w    0x0800,0x0E00,0x0800,0x0E00
                V0061004_ChannelsAmplitudeTableLoud:
                        dc.w    0x0900,0x0000,0x0900,0x0500
                        dc.w    0x0900,0x0700,0x0900,0x0900
                        dc.w    0x0900,0x0900,0x0900,0x0500
                        dc.w    0x0900,0x0A00,0x0900,0x0C00
                        dc.w    0x0900,0x0800,0x0900,0x0A00
                        dc.w    0x0900,0x0C00,0x0900,0x0D00
                        dc.w    0x0900,0x0B00,0x0900,0x0E00
                        dc.w    0x0900,0x0E00,0x0900,0x0E00
                        dc.w    0x0A00,0x0000,0x0A00,0x0000
                        dc.w    0x0A00,0x0000,0x0A00,0x0000
                        dc.w    0x0A00,0x0000,0x0A00,0x0000
                        dc.w    0x0A00,0x0600,0x0A00,0x0000
                        dc.w    0x0A00,0x0000,0x0A00,0x0A00
                        dc.w    0x0A00,0x0A00,0x0A00,0x0A00
                        dc.w    0x0A00,0x0000,0x0A00,0x0B00
                        dc.w    0x0A00,0x0D00,0x0A00,0x0E00
                        dc.w    0x0800,0x0000,0x0800,0x0500                             /* 192 bytes: 3x64 bytes for Channel A (register 8), channel B (register 9) and channel C (register 10) */
                        dc.w    0x0800,0x0700,0x0800,0x0800                             /* Accessed using V0061005_ChannelsAmplitudeTableSoft with offsets -64, 0 and 64 */
                        dc.w    0x0800,0x0900,0x0800,0x0900                             /* First byte: Register number (for channel selection) */
                        dc.w    0x0800,0x0A00,0x0800,0x0A00                             /* Third byte: Channel A/B/C amplitude */
                        dc.w    0x0800,0x0B00,0x0800,0x0B00                             /* Second and fourth bytes are padding 0x00 as data is written as longs to write to both PSG addresses in one instruction */
                        dc.w    0x0800,0x0B00,0x0800,0x0B00
                        dc.w    0x0800,0x0C00,0x0800,0x0C00
                        dc.w    0x0800,0x0C00,0x0800,0x0C00
                V0061005_ChannelsAmplitudeTableSoft:
                        dc.w    0x0900,0x0000,0x0900,0x0000
                        dc.w    0x0900,0x0100,0x0900,0x0200
                        dc.w    0x0900,0x0200,0x0900,0x0500
                        dc.w    0x0900,0x0200,0x0900,0x0600
                        dc.w    0x0900,0x0300,0x0900,0x0600
                        dc.w    0x0900,0x0700,0x0900,0x0800
                        dc.w    0x0900,0x0400,0x0900,0x0600
                        dc.w    0x0900,0x0800,0x0900,0x0900
                        dc.w    0x0A00,0x0000,0x0A00,0x0000
                        dc.w    0x0A00,0x0000,0x0A00,0x0000
                        dc.w    0x0A00,0x0000,0x0A00,0x0200
                        dc.w    0x0A00,0x0200,0x0A00,0x0100
                        dc.w    0x0A00,0x0100,0x0A00,0x0200
                        dc.w    0x0A00,0x0400,0x0A00,0x0500
                        dc.w    0x0A00,0x0100,0x0A00,0x0200
                        dc.w    0x0A00,0x0000,0x0A00,0x0000
                T0061017:
        }
        Super(AL0042_l_SupervisorStack);
}

void F0062_SOUND_Initialize(
#endif
#ifdef MEDIA772_SU1E
int16_t F0062_SOUND_Initialize(
#endif
#ifdef MEDIA174_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_SU1E
)
{
        F0063_SOUND_KeyClickOff();
        Giaccess(0, MASK0x0080_WRITE_PSG_REGISTER | C11_REGISTER_PSG_WAVEFORM_FREQUENCY_LOW_BYTE);
        Giaccess(0, MASK0x0080_WRITE_PSG_REGISTER | C12_REGISTER_PSG_WAVEFORM_FREQUENCY_HIGH_BYTE);
        G0580_ui_PSGModeRegisterBackup = Giaccess(C0_RESERVED, MASK0x0000_READ_PSG_REGISTER | C07_REGISTER_PSG_MODE);
        Giaccess(G0580_ui_PSGModeRegisterBackup | C0x003F_MODE_ENABLE_TONE_AND_WHITE_NOISE_CHANNELS_A_B_C, MASK0x0080_WRITE_PSG_REGISTER | C07_REGISTER_PSG_MODE);
        F0061_SOUND_SetChannelAmplitudes(8);
        Jdisint(C13_INTERRUPT_TIMER_A);
        Xbtimer(C0_TIMER_A, C1_DELAY_MODE_4_PRESCALE, 112, (char*)E0061_SOUND_Exception77Handler_TimerA_SoundPlayer); /* Every 4 MFP input clock ticks, the timer A data (initial value 112) is decremented. An interrupt is triggered each time the data reaches 0. This means every 448 MFP input clock ticks, the actual frequency is then 2457600 Hz / 448 = 5486 Hz */
        Jdisint(C13_INTERRUPT_TIMER_A);
#ifdef MEDIA772_SU1E
        return 1;
#endif
}
#endif

#ifdef MEDIA772_SU1E
void F1783_(
)
{
        Jdisint(C13_INTERRUPT_TIMER_A);
        F0061_SOUND_SetChannelAmplitudes(0);
        Giaccess(G0580_ui_PSGModeRegisterBackup, MASK0x0080_WRITE_PSG_REGISTER | C07_REGISTER_PSG_MODE);
        F1785_SOUND_RestoreKeyClick();
}
#endif

#ifdef MEDIA174_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_SU1E
void F0063_SOUND_KeyClickOff(
)
{
        long L0043_l_SupervisorStack;


        L0043_l_SupervisorStack = Super(0L);
        G0582_c_contermSystemVariableBackup = *G0581_pc_contermSystemVariable;
        M009_CLEAR(*G0581_pc_contermSystemVariable, MASK0xFF01_KEY_CLICK);
        Super(L0043_l_SupervisorStack);
}
#endif

#ifdef MEDIA772_SU1E
void F1785_SOUND_RestoreKeyClick(
)
{
        long L5263_l_SupervisorStack;


        L5263_l_SupervisorStack = Super(0L);
        *G0581_pc_contermSystemVariable = G0582_c_contermSystemVariableBackup;
        Super(L5263_l_SupervisorStack);
}

void F1786_(
)
{
        G0581_pc_contermSystemVariable = (unsigned char*)0x00000484; /* conterm system variable */
        F0062_SOUND_Initialize();
}

void F1787_(
)
{
        F1783_();
}

int16_t F1788_PlaySound(unsigned char* P5522_) {
        F0060_SOUND_Play_CPSX(P5522_, 112, 1);
        return 1;
}

int16_t F1789_Unreferenced(int16_t P5602_) {
        return 0;
}

void F1790_Unreferenced(int16_t P5603_) {
}
#endif

#ifdef MEDIA728_I34E_I34M_A36M_A35E_A35M
void F0799_SOUND_DisableUnavailableSounds(
void
)
{
        REGISTER int16_t L3444_i_SoundIndex;


        if (!G2040_auc_MemoryFlags[C3_MEM_BASE_SOUNDS]) {
                for (L3444_i_SoundIndex = 0; L3444_i_SoundIndex < C19_SOUND_FIRST_ATTACK; L3444_i_SoundIndex++) {
                        G0060_as_Graphic562_Sounds[L3444_i_SoundIndex].GraphicIndex = CM1_SOUND_NONE;
                }
        }
        if (!G2040_auc_MemoryFlags[C4_MEM_ATTACK_SOUNDS]) {
                for (L3444_i_SoundIndex = 0; L3444_i_SoundIndex < C28_SOUND_FIRST_MOVEMENT - C19_SOUND_FIRST_ATTACK; L3444_i_SoundIndex++) {
                        G0060_as_Graphic562_Sounds[L3444_i_SoundIndex + C19_SOUND_FIRST_ATTACK].GraphicIndex = CM1_SOUND_NONE;
                }
        }
        if (!G2040_auc_MemoryFlags[C5_MEM_MOVEMENT_SOUNDS]) {
                for (L3444_i_SoundIndex = 0; L3444_i_SoundIndex < M513_SOUND_COUNT - C28_SOUND_FIRST_MOVEMENT; L3444_i_SoundIndex++) {
                        G0060_as_Graphic562_Sounds[L3444_i_SoundIndex + C28_SOUND_FIRST_MOVEMENT].GraphicIndex = CM1_SOUND_NONE;
                }
        }
}
#endif

#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
void F0503_SOUND_LoadAll(
)
{
        REGISTER SOUND_DATA_AMIGA* L1673_ps_SoundData;
        REGISTER unsigned char* L1674_puc_SoundFirstSample;
        REGISTER int16_t L1675_i_SoundIndex;
        REGISTER int16_t L1676_i_GraphicIndex;
        REGISTER int16_t L1677_i_Unreferenced;


        for (L1675_i_SoundIndex = 0, L1673_ps_SoundData = G1022_as_SoundData; L1675_i_SoundIndex < M513_SOUND_COUNT; L1675_i_SoundIndex++, L1673_ps_SoundData ++) {
                L1676_i_GraphicIndex = G0060_as_Graphic562_Sounds[L1675_i_SoundIndex].GraphicIndex;
                L1673_ps_SoundData->ByteCount = G0635_pui_GraphicDecompressedByteCount[L1676_i_GraphicIndex] - 2; /* cast to long */
                L1674_puc_SoundFirstSample = (unsigned char*)F0468_MEMORY_Allocate(L1673_ps_SoundData->ByteCount + 2, C1_ALLOCATION_PERMANENT);
                if ((long)L1674_puc_SoundFirstSample & 1) { /* If address is odd */
                        Alert(0x80444DF0, 0L);
                } else {
                        if (!((long)L1674_puc_SoundFirstSample & 3)) { /* If address is not a multiple of 4 */
                                L1674_puc_SoundFirstSample += 2;
                        }
                }
                L1673_ps_SoundData->FirstSample = L1674_puc_SoundFirstSample + 2; /* Bypass the first word of sound data (which is the sample count) */
                F0490_MEMORY_LoadDecompressAndExpandGraphic(MASK0x8000_NOT_EXPANDED | L1676_i_GraphicIndex, L1674_puc_SoundFirstSample, 0, 0);
        }
}

#ifndef NOCOPYPROTECTION
void F0504_SOUND_ReadSecondSector_CPSD( /* Corresponds to S0465_CPSD_ReadSector247 in Atari ST */
P0936_puc_)
unsigned char* P0936_puc_ FINAL_SEPARATOR
{
        AztecCDirective_asm
                        movea.l _G1053_ps_DiscResourceUnit_CPSX,a1
                        move.l  a6,-(sp)
                        movea.l _G1054_ps_DiskResource_CPSX,a6
                        jsr     -$12(a6)                                ; disk.resource/GetUnit (see DR_GETUNIT in disk.h)
                        movea.l (sp)+,a6
                public _G1053_ps_DiscResourceUnit_CPSX
                public _G1054_ps_DiskResource_CPSX
        AztecCDirective_endasm
        ciab.ciaprb = CIAF_DSKSEL3 | CIAF_DSKSEL2 | CIAF_DSKSEL1 | CIAF_DSKSEL0 | CIAF_DSKDIREC | CIAF_DSKSTEP; /* 0x7B. MOTOR ON, do not select any drive, select head 0 */
        ciab.ciaprb = CIAF_DSKSEL3 | CIAF_DSKSEL2 | CIAF_DSKSEL1 | CIAF_DSKDIREC | CIAF_DSKSTEP; /* 0x73. Select drive 0 (internal drive) */
        custom.dskpt = (APTR)P0936_puc_; /* Address of disk DMA data */
        G0625_i_FloppyDriveDMATimeout_CPSDF = 1;
        G0314_i_SectorsReadRequested_CPSDF = MASK0x0004_CPS_SECOND_SECTOR;
}
#endif

SOUND_VOLUME G1026_s_PendingSound1Volume;
SOUND_VOLUME G1027_s_PendingSound2Volume;
#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
void F0064_SOUND_RequestPlay_CPSD(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0088_SoundIndex, P0089_i_MapX, P0090_i_MapY, P0091_ui_Mode)
#endif
#ifdef MEDIA019_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_F20E_F20J_X30J_P20JA
unsigned int16_t P0088_SoundIndex SEPARATOR
#endif
#ifdef MEDIA485_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
int16_t          P0088_SoundIndex SEPARATOR
#endif
int16_t          P0089_i_MapX     SEPARATOR
int16_t          P0090_i_MapY     SEPARATOR
unsigned int16_t P0091_ui_Mode    FINAL_SEPARATOR
{
        REGISTER SOUND_DATA* L0044_ps_Sound;
#ifdef MEDIA605_X30J_F31E_F31J_X31J
        int16_t L0046_SoundVolume;
#endif
#ifdef MEDIA006_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB /* CHANGE7_08_OPTIMIZATION Variable definition changed */
        REGISTER int16_t L0046_SoundVolume;
#endif
#ifdef MEDIA744_A36M
        int16_t L0047_i_DistanceToParty;
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        SOUND_VOLUME L1574_s_RequestedSoundVolume;
#endif
        EVENT L0045_s_Event;
#ifdef MEDIA034_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_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
        REGISTER int16_t L0047_i_DistanceToParty;
#endif
#ifdef MEDIA326_S20E_S21E_P20JA_P20JB_I34E_I34M_P31J /* CHANGE7_08_OPTIMIZATION Variable definition changed */
        int16_t L0046_SoundVolume;
#endif
#ifdef MEDIA488_F20E_F20J
        unsigned int16_t L0046_SoundVolume;
#endif
#ifdef MEDIA353_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        SOUND_VOLUME L1574_s_RequestedSoundVolume;
#endif
#ifdef MEDIA153_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
        static int16_t G0586_i_ImmediateSoundCount_CPSD;
#endif
#endif


        if (
#ifdef MEDIA485_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
           (P0088_SoundIndex == CM1_SOUND_NONE) ||
#endif
           (P0091_ui_Mode && (G0272_i_CurrentMapIndex != G0309_i_PartyMapIndex))) {
                return;
        }
        L0044_ps_Sound = &G0060_as_Graphic562_Sounds[P0088_SoundIndex];
#ifdef MEDIA485_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
        if ((P0088_SoundIndex < 0) || (L0044_ps_Sound->GraphicIndex < 0)) {
                return;
        }
#endif
        if (P0091_ui_Mode > C01_MODE_PLAY_IF_PRIORITIZED) { /* Add an event in the timeline to play the sound (P0091_ui_Mode - 1) ticks later */
                M033_SET_MAP_AND_TIME(L0045_s_Event.Map_Time, G0272_i_CurrentMapIndex, G0313_ul_GameTime + P0091_ui_Mode - 1);
                L0045_s_Event.A.A.Type = C20_EVENT_PLAY_SOUND;
                L0045_s_Event.A.A.Priority = L0044_ps_Sound->Priority;
                L0045_s_Event.C.SoundIndex = P0088_SoundIndex;
                L0045_s_Event.B.Location.MapX = P0089_i_MapX;
                L0045_s_Event.B.Location.MapY = P0090_i_MapY;
                F0238_TIMELINE_AddEvent_GetEventIndex_CPSE(&L0045_s_Event);
                return;
        }
#ifdef MEDIA161_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_F31E_F31J_X31J_P31J
        L0047_i_DistanceToParty = (((L0047_i_DistanceToParty = (P0089_i_MapX - G0306_i_PartyMapX)) < 0) ? -L0047_i_DistanceToParty : L0047_i_DistanceToParty) + (((L0047_i_DistanceToParty = (P0090_i_MapY - G0307_i_PartyMapY)) < 0) ? -L0047_i_DistanceToParty : L0047_i_DistanceToParty);
#endif
#ifdef MEDIA168_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA_X31J
        if (L0047_i_DistanceToParty < L0044_ps_Sound->LoudDistance) {
                L0046_SoundVolume = 1;
        } else {
                if (L0047_i_DistanceToParty < L0044_ps_Sound->SoftDistance) {
                        L0046_SoundVolume = 0;
                } else {
                        return;
                }
        }
#endif
#ifdef MEDIA371_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A36M_A31E_A31M_A33M_A35E_A35M
        if (!F0505_SOUND_GetVolume(P0089_i_MapX, P0090_i_MapY, &L1574_s_RequestedSoundVolume)) {
                return;
        }
#endif
#ifdef MEDIA505_F20E_F20J_P20JB_I34E_I34M_F31E_F31J_P31J
        if (L0047_i_DistanceToParty > L0044_ps_Sound->SoftDistance) {
                return;
        }
        if (L0047_i_DistanceToParty < L0044_ps_Sound->LoudDistance) {
#endif
#ifdef MEDIA551_F20E_F20J_F31E_F31J
                L0046_SoundVolume = 127;
        } else {
                L0046_SoundVolume = (127 / (L0044_ps_Sound->SoftDistance + 2 - L0044_ps_Sound->LoudDistance)) * (L0044_ps_Sound->SoftDistance + 1 - L0047_i_DistanceToParty);
        }
#endif
#ifdef MEDIA480_P20JB_I34E_I34M_P31J
                L0046_SoundVolume = 3;
        } else {
                L0046_SoundVolume = 3 - (L0047_i_DistanceToParty - L0044_ps_Sound->LoudDistance);
        }
#endif
#ifdef MEDIA153_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
        G0586_i_ImmediateSoundCount_CPSD++;
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
        if (((unsigned int16_t)G0586_i_ImmediateSoundCount_CPSD >= 88) && !G0625_i_FloppyDriveDMATimeout_CPSDF) {
#endif
#ifdef MEDIA423_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A31E_A31M_A33M_A35E_A35M
        if (((unsigned int16_t)G0586_i_ImmediateSoundCount_CPSD >= 880) && !G0625_i_FloppyDriveDMATimeout_CPSDF) {
#endif
                G0586_i_ImmediateSoundCount_CPSD = 0;
                G0240_i_Graphic559_State_CPSD += 3;
                M008_SET(G0644_i_FloppyDiskReadBufferContentType_CPSDF, MASK0x0002_BUFFER_CONTAINS_CPS_SECOND_SECTOR);
#ifdef MEDIA007_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E
                S0465_CPSD_ReadSector247(G0643_puc_FloppyDiskReadBuffer_CPSDF + 512);
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
                F0504_SOUND_ReadSecondSector_CPSD(G0643_puc_FloppyDiskReadBuffer_CPSDF + 1026);
#endif
#ifdef MEDIA626_A31E_A31M_A33M_A35E_A35M
                F0504_SOUND_ReadSecondSector_CPSD(G0643_puc_FloppyDiskReadBuffer_CPSDF + 2260);
#endif
                G0329_i_Useless_CPSD &= (G0058_i_Graphic562_Useless_CPSD = G0240_i_Graphic559_State_CPSD << 2); /* BUG0_00 Useless code */
        }
#endif
#endif
        if (!P0091_ui_Mode) { /* Play the sound immediately */
#ifdef MEDIA160_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_F20E_F20J_X30J_P20JA
                F0060_SOUND_Play_CPSX(F0489_MEMORY_GetNativeBitmapOrGraphic(L0044_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED), L0044_ps_Sound->Period, L0046_SoundVolume);
#endif
#ifdef MEDIA485_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
                if (L0044_ps_Sound->Buffer == NULL) {
                        L0044_ps_Sound->Buffer = F0489_MEMORY_GetNativeBitmapOrGraphic(L0044_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED);
                }
#endif
#ifdef MEDIA482_P20JB_I34E_I34M_F31E_F31J_X31J_P31J
                F0709_StartSound(P0088_SoundIndex, L0046_SoundVolume);
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
                F0709_StartSound(P0088_SoundIndex, L1574_s_RequestedSoundVolume);
                return;
        }
#endif
#ifdef MEDIA551_F20E_F20J_F31E_F31J
        } else {
                if (L0046_SoundVolume > G4080_aui_SoundVolume[P0088_SoundIndex]) {
                        G4080_aui_SoundVolume[P0088_SoundIndex] = L0046_SoundVolume;
                }
        }
#endif
#ifdef MEDIA167_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA_P20JB_I34E_I34M_X31J_P31J
        } else { /* Set the requested sound as the pending sound if it is louder or has a higher priority than the current pending sound */
                if ((G0583_i_PendingSoundIndex == -1) || (L0046_SoundVolume > G0584_PendingSoundVolume) || ((L0046_SoundVolume == G0584_PendingSoundVolume) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G0583_i_PendingSoundIndex].Priority))) {
                        G0583_i_PendingSoundIndex = P0088_SoundIndex;
                        G0584_PendingSoundVolume = L0046_SoundVolume;
                }
        }
#endif
#ifdef MEDIA353_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
                F0060_SOUND_Play_CPSX(P0088_SoundIndex, L0044_ps_Sound->Period, L1574_s_RequestedSoundVolume);
                return;
        }
#endif
#ifdef MEDIA349_G14ED
        if ((L1574_s_RequestedSoundVolume.Left > G4026_as_[P0088_SoundIndex].Left) || (L1574_s_RequestedSoundVolume.Right > G4026_as_[P0088_SoundIndex].Right)) {
#endif
#ifdef MEDIA388_G20E_G21E
        if ((L1574_s_RequestedSoundVolume.Left + L1574_s_RequestedSoundVolume.Right) > (G4026_as_[P0088_SoundIndex].Left + G4026_as_[P0088_SoundIndex].Right)) {
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
                G4026_as_[P0088_SoundIndex].Left = L1574_s_RequestedSoundVolume.Left;
                G4026_as_[P0088_SoundIndex].Right = L1574_s_RequestedSoundVolume.Right;
        }
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        /* Set the requested sound as the pending sound if it is louder or has a higher priority than the current pending sound */
        if (G0734_ps_AmigaData->PendingSound1Index == -1) {
                G0734_ps_AmigaData->PendingSound1Index = P0088_SoundIndex;
                G1026_s_PendingSound1Volume.Left = L1574_s_RequestedSoundVolume.Left;
                G1026_s_PendingSound1Volume.Right = L1574_s_RequestedSoundVolume.Right;
                (int16_t)L1574_s_RequestedSoundVolume.Right;
        } else {
                if (G0734_ps_AmigaData->PendingSound2Index == -1) {
                        G0734_ps_AmigaData->PendingSound2Index = P0088_SoundIndex;
                        G1027_s_PendingSound2Volume.Left = L1574_s_RequestedSoundVolume.Left;
                        G1027_s_PendingSound2Volume.Right = L1574_s_RequestedSoundVolume.Right;
                        (int16_t)L1574_s_RequestedSoundVolume.Right;
                } else {
#ifdef MEDIA433_A20E_A20F_A20G_A21E_A22E_A22G
                        if ((F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1026_s_PendingSound1Volume) == 1) || (F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1027_s_PendingSound2Volume) == 1)) {
                                if (F0502_SOUND_CompareVolumes(&G1026_s_PendingSound1Volume, &G1027_s_PendingSound2Volume) == 1) {
#endif
#ifdef MEDIA410_A20ED
                        if ((L1574_s_RequestedSoundVolume.Left + L1574_s_RequestedSoundVolume.Right > G1026_s_PendingSound1Volume.Left + G1026_s_PendingSound1Volume.Right) || (L1574_s_RequestedSoundVolume.Left + L1574_s_RequestedSoundVolume.Right > G1027_s_PendingSound2Volume.Left + G1027_s_PendingSound2Volume.Right)) {
                                if (G1026_s_PendingSound1Volume.Left + G1026_s_PendingSound1Volume.Right > G1027_s_PendingSound2Volume.Left + G1027_s_PendingSound2Volume.Right) {
#endif
                                        G0734_ps_AmigaData->PendingSound2Index = P0088_SoundIndex;
                                        G1027_s_PendingSound2Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                        G1027_s_PendingSound2Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                        (int16_t)L1574_s_RequestedSoundVolume.Right;
                                } else {
                                        G0734_ps_AmigaData->PendingSound1Index = P0088_SoundIndex;
                                        G1026_s_PendingSound1Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                        G1026_s_PendingSound1Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                        (int16_t)L1574_s_RequestedSoundVolume.Right;
                                }
                        } else {
#ifdef MEDIA433_A20E_A20F_A20G_A21E_A22E_A22G
                                if ((F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1026_s_PendingSound1Volume) == 0) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G0734_ps_AmigaData->PendingSound1Index].Priority)) {
#endif
#ifdef MEDIA410_A20ED
                                if ((L1574_s_RequestedSoundVolume.Left == G1026_s_PendingSound1Volume.Left) && (L1574_s_RequestedSoundVolume.Right == G1026_s_PendingSound1Volume.Right) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G0734_ps_AmigaData->PendingSound1Index].Priority)) {
#endif
                                        G0734_ps_AmigaData->PendingSound1Index = P0088_SoundIndex;
                                        G1026_s_PendingSound1Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                        G1026_s_PendingSound1Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                        (int16_t)L1574_s_RequestedSoundVolume.Right;
                                } else {
#ifdef MEDIA433_A20E_A20F_A20G_A21E_A22E_A22G
                                        if ((F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1027_s_PendingSound2Volume) == 0) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G0734_ps_AmigaData->PendingSound2Index].Priority)) {
#endif
#ifdef MEDIA410_A20ED
                                        if ((L1574_s_RequestedSoundVolume.Left == G1027_s_PendingSound2Volume.Left) && (L1574_s_RequestedSoundVolume.Right == G1027_s_PendingSound2Volume.Right) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G0734_ps_AmigaData->PendingSound2Index].Priority)) {
#endif
                                                G0734_ps_AmigaData->PendingSound2Index = P0088_SoundIndex;
                                                G1027_s_PendingSound2Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                                G1027_s_PendingSound2Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                                (int16_t)L1574_s_RequestedSoundVolume.Right;
                                        }
                                }
                        }
                }
        }
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        /* Set the requested sound as the pending sound if it is louder or has a higher priority than the current pending sound */
        if (G3239_PendingSound1Index == -1) {
                G3239_PendingSound1Index = P0088_SoundIndex;
                G1026_s_PendingSound1Volume.Left = L1574_s_RequestedSoundVolume.Left;
                G1026_s_PendingSound1Volume.Right = L1574_s_RequestedSoundVolume.Right;
        } else {
                if (G3240_PendingSound2Index == -1) {
                        G3240_PendingSound2Index = P0088_SoundIndex;
                        G1027_s_PendingSound2Volume.Left = L1574_s_RequestedSoundVolume.Left;
                        G1027_s_PendingSound2Volume.Right = L1574_s_RequestedSoundVolume.Right;
                } else {
                        if ((F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1026_s_PendingSound1Volume) == 1) || (F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1027_s_PendingSound2Volume) == 1)) {
                                if (F0502_SOUND_CompareVolumes(&G1026_s_PendingSound1Volume, &G1027_s_PendingSound2Volume) == 1) {
                                        G3240_PendingSound2Index = P0088_SoundIndex;
                                        G1027_s_PendingSound2Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                        G1027_s_PendingSound2Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                } else {
                                        G3239_PendingSound1Index = P0088_SoundIndex;
                                        G1026_s_PendingSound1Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                        G1026_s_PendingSound1Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                }
                        } else {
                                if ((F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1026_s_PendingSound1Volume) == 0) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G3239_PendingSound1Index].Priority)) {
                                        G3239_PendingSound1Index = P0088_SoundIndex;
                                        G1026_s_PendingSound1Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                        G1026_s_PendingSound1Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                } else {
                                        if ((F0502_SOUND_CompareVolumes(&L1574_s_RequestedSoundVolume, &G1027_s_PendingSound2Volume) == 0) && (L0044_ps_Sound->Priority > G0060_as_Graphic562_Sounds[G3240_PendingSound2Index].Priority)) {
                                                G3240_PendingSound2Index = P0088_SoundIndex;
                                                G1027_s_PendingSound2Volume.Left = L1574_s_RequestedSoundVolume.Left;
                                                G1027_s_PendingSound2Volume.Right = L1574_s_RequestedSoundVolume.Right;
                                        }
                                }
                        }
                }
        }
#endif
}

void F0065_SOUND_PlayPendingSound_CPSD(
#ifdef MEDIA529_F20E_F20J_X30J_P20JA_P20JB_I34E_I34M_A36M_A31E_A31M_A33M_A35E_A35M_F31E_F31J_X31J_P31J
void
#endif
)
{
#ifdef MEDIA141_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_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
        REGISTER SOUND_DATA* L0048_ps_Sound;
#endif
#ifdef MEDIA373_G14ED_G20E_G21E_F20E_F20J
        unsigned int L6420_;
#endif
#ifdef MEDIA670_F31E_F31J
        unsigned int16_t L6420_;
#endif


#ifdef MEDIA551_F20E_F20J_F31E_F31J
        for (L6420_ = 0; L6420_ < M513_SOUND_COUNT; L6420_++) {
                if (G4080_aui_SoundVolume[L6420_]) {
#ifdef MEDIA488_F20E_F20J
                        L0048_ps_Sound = &G0060_as_Graphic562_Sounds[L6420_];
                        F0060_SOUND_Play_CPSX(F0489_MEMORY_GetNativeBitmapOrGraphic(L0048_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED), L0048_ps_Sound->Period, G4080_aui_SoundVolume[L6420_]);
                        G4080_aui_SoundVolume[L6420_] = 0;
#endif
#ifdef MEDIA670_F31E_F31J
                        L0048_ps_Sound = &G0060_as_Graphic562_Sounds[L6420_];
                        if (L0048_ps_Sound->Buffer == NULL) {
                                L0048_ps_Sound->Buffer = F0489_MEMORY_GetNativeBitmapOrGraphic(L0048_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED);
                        }
                        F0709_StartSound(L6420_, G4080_aui_SoundVolume[L6420_]);
                        G4080_aui_SoundVolume[L6420_] = 0;
#endif
                }
        }
#endif
#ifdef MEDIA351_G14ED_G20E_G21E
        for (L6420_ = 0; L6420_ < M513_SOUND_COUNT; L6420_++) {
                if (G4026_as_[L6420_].Left == 0) {
                        continue;
                }
                if (G4026_as_[L6420_].Right == 0) {
                        continue;
                }
                F0060_SOUND_Play_CPSX(L6420_, G0060_as_Graphic562_Sounds[L6420_].Period, G4026_as_[L6420_]);
                G4026_as_[L6420_].Left = G4026_as_[L6420_].Right = 0;
        }
#endif
#ifdef MEDIA167_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA_P20JB_I34E_I34M_X31J_P31J
        if (G0583_i_PendingSoundIndex != -1) {
                L0048_ps_Sound = &G0060_as_Graphic562_Sounds[G0583_i_PendingSoundIndex];
#endif
#ifdef MEDIA165_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA
                F0060_SOUND_Play_CPSX(F0489_MEMORY_GetNativeBitmapOrGraphic(L0048_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED), L0048_ps_Sound->Period, G0584_PendingSoundVolume);
#endif
#ifdef MEDIA413_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        if (G0734_ps_AmigaData->PendingSound1Index != -1) {
                L0048_ps_Sound = &G0060_as_Graphic562_Sounds[G0734_ps_AmigaData->PendingSound1Index];
                F0060_SOUND_Play_CPSX(G0734_ps_AmigaData->PendingSound1Index, L0048_ps_Sound->Period, G1026_s_PendingSound1Volume);
                G0734_ps_AmigaData->PendingSound1Index = -1;
        }
        if (G0734_ps_AmigaData->PendingSound2Index != -1) {
                L0048_ps_Sound = &G0060_as_Graphic562_Sounds[G0734_ps_AmigaData->PendingSound2Index];
                F0060_SOUND_Play_CPSX(G0734_ps_AmigaData->PendingSound2Index, L0048_ps_Sound->Period, G1027_s_PendingSound2Volume);
                G0734_ps_AmigaData->PendingSound2Index = -1;
        }
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        if (G3239_PendingSound1Index != -1) {
                L0048_ps_Sound = &G0060_as_Graphic562_Sounds[G3239_PendingSound1Index];
#ifdef MEDIA758_A36M_A35E_A35M
                if (L0048_ps_Sound->Buffer == NULL) {
                        L0048_ps_Sound->Buffer = F0489_MEMORY_GetNativeBitmapOrGraphic(L0048_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED);
                }
#endif
                F0709_StartSound(G3239_PendingSound1Index, G1026_s_PendingSound1Volume);
                G3239_PendingSound1Index = -1;
        }
        if (G3240_PendingSound2Index != -1) {
                L0048_ps_Sound = &G0060_as_Graphic562_Sounds[G3240_PendingSound2Index];
#ifdef MEDIA758_A36M_A35E_A35M
                if (L0048_ps_Sound->Buffer == NULL) {
                        L0048_ps_Sound->Buffer = F0489_MEMORY_GetNativeBitmapOrGraphic(L0048_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED);
                }
#endif
                F0709_StartSound(G3240_PendingSound2Index, G1027_s_PendingSound2Volume);
                G3240_PendingSound2Index = -1;
        }
#endif
#ifdef MEDIA483_P20JB_I34E_I34M_X31J_P31J
                if (L0048_ps_Sound->Buffer == NULL) {
                        L0048_ps_Sound->Buffer = F0489_MEMORY_GetNativeBitmapOrGraphic(L0048_ps_Sound->GraphicIndex | MASK0x8000_NOT_EXPANDED);
                }
                F0709_StartSound(G0583_i_PendingSoundIndex, G0584_PendingSoundVolume);
#endif
#ifdef MEDIA153_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A31E_A31M_A33M_A35E_A35M
#ifndef NOCOPYPROTECTION
                if ((G0240_i_Graphic559_State_CPSD & 0x0001) && !G0625_i_FloppyDriveDMATimeout_CPSDF) { /* If variable has an odd value, this means sector 247 was read */
                        if (M007_GET(G0316_i_SectorsReadPreviousFailure_CPSDF, MASK0x0004_CPS_SECOND_SECTOR)) {
                                M009_CLEAR(G0316_i_SectorsReadPreviousFailure_CPSDF, MASK0x0004_CPS_SECOND_SECTOR);
                                M009_CLEAR(G0315_i_SectorsReadFailure_CPSDF, MASK0x0004_CPS_SECOND_SECTOR);
                        }
                        G0240_i_Graphic559_State_CPSD++; /* Variable has an even value again */
                }
#endif
#endif
#ifdef MEDIA167_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_X30J_P20JA_P20JB_I34E_I34M_X31J_P31J
                G0583_i_PendingSoundIndex = -1;
                G0584_PendingSoundVolume = 0;
        }
#endif
}
#endif

#ifdef MEDIA371_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G_A36M_A31E_A31M_A33M_A35E_A35M
BOOLEAN F0505_SOUND_GetVolume(
#ifdef MEDIA009_S10EA_S10EB_S11E_S12E_S12G_S13FA_S13FB_S20E_S21E_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
P0937_i_SoundSourceMapX, P0938_i_SoundSourceMapY, P0939_ps_SoundVolume)
#endif
REGISTER int16_t       P0937_i_SoundSourceMapX SEPARATOR
REGISTER int16_t       P0938_i_SoundSourceMapY SEPARATOR
REGISTER SOUND_VOLUME* P0939_ps_SoundVolume    FINAL_SEPARATOR
{
        REGISTER int16_t L1678_i_RightVolumeColumnIndex;
        REGISTER int16_t L1679_i_LineIndex;
#ifdef MEDIA353_G14ED_G20E_G21E_A20ED_A20E_A20F_A20G_A21E_A22E_A22G
        int16_t L1680_i_LeftVolumeColumnIndex;
#endif
#ifdef MEDIA746_A36M_A31E_A31M_A33M_A35E_A35M
        register int16_t L1680_i_LeftVolumeColumnIndex;
#endif


        switch(G0308_i_PartyDirection) {
                case C0_DIRECTION_NORTH:
                        L1678_i_RightVolumeColumnIndex = P0937_i_SoundSourceMapX - G0306_i_PartyMapX;
                        L1679_i_LineIndex = P0938_i_SoundSourceMapY - G0307_i_PartyMapY;
                        break;
                case C1_DIRECTION_EAST:
                        L1678_i_RightVolumeColumnIndex = P0938_i_SoundSourceMapY - G0307_i_PartyMapY;
                        L1679_i_LineIndex = -(P0937_i_SoundSourceMapX - G0306_i_PartyMapX);
                        break;
                case C2_DIRECTION_SOUTH:
                        L1678_i_RightVolumeColumnIndex = -(P0937_i_SoundSourceMapX - G0306_i_PartyMapX);
                        L1679_i_LineIndex = -(P0938_i_SoundSourceMapY - G0307_i_PartyMapY);
                        break;
                case C3_DIRECTION_WEST:
                        L1678_i_RightVolumeColumnIndex = -(P0938_i_SoundSourceMapY - G0307_i_PartyMapY);
                        L1679_i_LineIndex = P0937_i_SoundSourceMapX - G0306_i_PartyMapX;
                        break;
        }
        if ((L1678_i_RightVolumeColumnIndex < -12) || (L1678_i_RightVolumeColumnIndex > 12)) { /* Sound is not audible if source is more than 12 squares away from the party */
                return C0_FALSE;
        }
        if ((L1679_i_LineIndex < -12) || (L1679_i_LineIndex > 12)) { /* Sound is not audible if source is more than 12 squares away from the party */
                return C0_FALSE;
        }
        L1680_i_LeftVolumeColumnIndex = -L1678_i_RightVolumeColumnIndex + 12;
        L1678_i_RightVolumeColumnIndex += 12;
        L1679_i_LineIndex += 12;
        P0939_ps_SoundVolume->Right = G1028_aauc_DistanceToSoundVolume[L1679_i_LineIndex][L1678_i_RightVolumeColumnIndex];
        P0939_ps_SoundVolume->Left = G1028_aauc_DistanceToSoundVolume[L1679_i_LineIndex][L1680_i_LeftVolumeColumnIndex];
        return C1_TRUE;
}
#endif
#endif
