From 7dc95a0103af08c95c9093b6efa6c77af77a2538 Mon Sep 17 00:00:00 2001 From: aaaaaa123456789 Date: Sun, 13 Sep 2020 04:22:50 -0300 Subject: Undo PokeCodec's PRs This commit undoes most of PokeCodec's PRs after the debate in chat. Some harmless or completely superseded PRs have been left alone, as there is not much benefit in attempting to undo them. Reverts #1104, #1108, #1115, #1118, #1119, #1124, #1126, #1127, #1132, #1136, #1137, #1139, #1140, #1144, #1148, #1149, #1150, #1153, #1155, #1177, #1179, #1180, #1181, #1182 and #1183. --- src/m4a.c | 111 ++++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 75 insertions(+), 36 deletions(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index b8f9e21c1..11a4525ea 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -22,7 +22,8 @@ u8 gMPlayMemAccArea[0x10]; u32 MidiKeyToFreq(struct WaveData *wav, u8 key, u8 fineAdjust) { - u32 val1, val2; + u32 val1; + u32 val2; u32 fineAdjustShifted = fineAdjust << 24; if (key > 178) @@ -314,7 +315,7 @@ void MPlayExtender(struct CgbChannel *cgbChans) void MusicPlayerJumpTableCopy(void) { - asm("svc 0x2A"); + asm("swi 0x2A"); } void ClearChain(void *x) @@ -351,25 +352,25 @@ void SoundInit(struct SoundInfo *soundInfo) | SOUND_ALL_MIX_FULL; REG_SOUNDBIAS_H = (REG_SOUNDBIAS_H & 0x3F) | 0x40; - REG_DMA1SAD = (u32)soundInfo->pcmBuffer; - REG_DMA1DAD = (u32)®_FIFO_A; - REG_DMA2SAD = (u32)soundInfo->pcmBuffer + PCM_DMA_BUF_SIZE; - REG_DMA2DAD = (u32)®_FIFO_B; + REG_DMA1SAD = (s32)soundInfo->pcmBuffer; + REG_DMA1DAD = (s32)®_FIFO_A; + REG_DMA2SAD = (s32)soundInfo->pcmBuffer + PCM_DMA_BUF_SIZE; + REG_DMA2DAD = (s32)®_FIFO_B; SOUND_INFO_PTR = soundInfo; CpuFill32(0, soundInfo, sizeof(struct SoundInfo)); soundInfo->maxChans = 8; soundInfo->masterVolume = 15; - soundInfo->plynote = ply_note; + soundInfo->plynote = (u32)ply_note; soundInfo->CgbSound = DummyFunc; soundInfo->CgbOscOff = (void (*)(u8))DummyFunc; soundInfo->MidiKeyToCgbFreq = (u32 (*)(u8, u8, u8))DummyFunc; - soundInfo->ExtVolPit = (void (*)(struct MusicPlayerInfo *, struct MusicPlayerTrack *))DummyFunc; + soundInfo->ExtVolPit = (u32)DummyFunc; MPlayJumpTableCopy(gMPlayJumpTable); - soundInfo->MPlayJumpTable = (void (**)())gMPlayJumpTable; + soundInfo->MPlayJumpTable = (u32)gMPlayJumpTable; SampleFreqSet(SOUND_MODE_FREQ_13379); @@ -380,7 +381,7 @@ void SampleFreqSet(u32 freq) { struct SoundInfo *soundInfo = SOUND_INFO_PTR; - freq = (freq & SOUND_MODE_FREQ) >> SOUND_MODE_FREQ_SHIFT; + freq = (freq & 0xF0000) >> 16; soundInfo->freq = freq; soundInfo->pcmSamplesPerVBlank = gPcmSamplesPerVBlankTable[freq - 1]; soundInfo->pcmDmaPeriod = PCM_DMA_BUF_SIZE / soundInfo->pcmSamplesPerVBlank; @@ -431,9 +432,14 @@ void m4aSoundMode(u32 mode) soundInfo->maxChans = temp >> SOUND_MODE_MAXCHN_SHIFT; - for (temp = MAX_DIRECTSOUND_CHANNELS, chan = soundInfo->chans; temp != 0; temp--,chan++) + temp = MAX_DIRECTSOUND_CHANNELS; + chan = &soundInfo->chans[0]; + + while (temp != 0) { chan->status = 0; + temp--; + chan++; } } @@ -463,28 +469,37 @@ void m4aSoundMode(u32 mode) void SoundClear(void) { - s32 i; struct SoundInfo *soundInfo = SOUND_INFO_PTR; - struct SoundChannel *chan; + s32 i; + void *chan; if (soundInfo->ident != ID_NUMBER) return; soundInfo->ident++; - for (i = MAX_DIRECTSOUND_CHANNELS, chan = soundInfo->chans; i > 0; i--,chan++) + i = MAX_DIRECTSOUND_CHANNELS; + chan = &soundInfo->chans[0]; + + while (i > 0) { - chan->status = 0; + ((struct SoundChannel *)chan)->status = 0; + i--; + chan = (void *)((s32)chan + sizeof(struct SoundChannel)); } - chan = (struct SoundChannel *)soundInfo->cgbChans; + chan = soundInfo->cgbChans; if (chan) { - for (i=1; i<=4; i++,chan++) + i = 1; + + while (i <= 4) { soundInfo->CgbOscOff(i); - chan->status = 0; + ((struct CgbChannel *)chan)->sf = 0; + i++; + chan = (void *)((s32)chan + sizeof(struct CgbChannel)); } } @@ -495,20 +510,21 @@ void m4aSoundVSyncOff(void) { struct SoundInfo *soundInfo = SOUND_INFO_PTR; - if (soundInfo->ident < ID_NUMBER || soundInfo->ident > ID_NUMBER + 1) - return; - soundInfo->ident += 10; + if (soundInfo->ident >= ID_NUMBER && soundInfo->ident <= ID_NUMBER + 1) + { + soundInfo->ident += 10; - if (REG_DMA1CNT & (DMA_REPEAT << 16)) - REG_DMA1CNT = ((DMA_ENABLE | DMA_START_NOW | DMA_32BIT | DMA_SRC_INC | DMA_DEST_FIXED) << 16) | 4; + if (REG_DMA1CNT & (DMA_REPEAT << 16)) + REG_DMA1CNT = ((DMA_ENABLE | DMA_START_NOW | DMA_32BIT | DMA_SRC_INC | DMA_DEST_FIXED) << 16) | 4; - if (REG_DMA2CNT & (DMA_REPEAT << 16)) - REG_DMA2CNT = ((DMA_ENABLE | DMA_START_NOW | DMA_32BIT | DMA_SRC_INC | DMA_DEST_FIXED) << 16) | 4; + if (REG_DMA2CNT & (DMA_REPEAT << 16)) + REG_DMA2CNT = ((DMA_ENABLE | DMA_START_NOW | DMA_32BIT | DMA_SRC_INC | DMA_DEST_FIXED) << 16) | 4; - REG_DMA1CNT_H = DMA_32BIT; - REG_DMA2CNT_H = DMA_32BIT; + REG_DMA1CNT_H = DMA_32BIT; + REG_DMA2CNT_H = DMA_32BIT; - CpuFill32(0, soundInfo->pcmBuffer, sizeof(soundInfo->pcmBuffer)); + CpuFill32(0, soundInfo->pcmBuffer, sizeof(soundInfo->pcmBuffer)); + } } void m4aSoundVSyncOn(void) @@ -564,7 +580,7 @@ void MPlayOpen(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track } soundInfo->intp = (u32)mplayInfo; - soundInfo->func = MPlayMain; + soundInfo->func = (u32)MPlayMain; soundInfo->ident = ID_NUMBER; mplayInfo->ident = ID_NUMBER; } @@ -598,21 +614,28 @@ void MPlayStart(struct MusicPlayerInfo *mplayInfo, struct SongHeader *songHeader mplayInfo->tempoC = 0; mplayInfo->fadeOI = 0; - for (i = 0, track = mplayInfo->tracks; i < songHeader->trackCount && i < mplayInfo->trackCount; i++, track++) + i = 0; + track = mplayInfo->tracks; + + while (i < songHeader->trackCount && i < mplayInfo->trackCount) { TrackStop(mplayInfo, track); track->flags = MPT_FLG_EXIST | MPT_FLG_START; track->chan = 0; track->cmdPtr = songHeader->part[i]; + i++; + track++; } - for (; i < mplayInfo->trackCount; i++, track++) + while (i < mplayInfo->trackCount) { TrackStop(mplayInfo, track); track->flags = 0; + i++; + track++; } - if (songHeader->reverb & SOUND_MODE_REVERB_SET) + if (songHeader->reverb & 0x80) m4aSoundMode(songHeader->reverb); mplayInfo->ident = ID_NUMBER; @@ -630,9 +653,14 @@ void m4aMPlayStop(struct MusicPlayerInfo *mplayInfo) mplayInfo->ident++; mplayInfo->status |= MUSICPLAYER_STATUS_PAUSE; - for (i = mplayInfo->trackCount, track = mplayInfo->tracks; i > 0; i--, track++) + i = mplayInfo->trackCount; + track = mplayInfo->tracks; + + while (i > 0) { TrackStop(mplayInfo, track); + i--; + track++; } mplayInfo->ident = ID_NUMBER; @@ -676,8 +704,10 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) if ((s16)(mplayInfo->fadeOV & mask) <= 0) { + i = mplayInfo->trackCount; + track = mplayInfo->tracks; - for (i = mplayInfo->trackCount, track = mplayInfo->tracks; i > 0; i--, track++) + while (i > 0) { u32 val; @@ -689,6 +719,9 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) if (!val) track->flags = 0; + + i--; + track++; } if (mplayInfo->fadeOV & TEMPORARY_FADE) @@ -701,7 +734,10 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) } } - for (i = mplayInfo->trackCount, track = mplayInfo->tracks; i > 0; i--, track++) + i = mplayInfo->trackCount; + track = mplayInfo->tracks; + + while (i > 0) { if (track->flags & MPT_FLG_EXIST) { @@ -710,6 +746,9 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) track->volX = (fadeOV >> FADE_VOL_SHIFT); track->flags |= MPT_FLG_VOLCHG; } + + i--; + track++; } } @@ -717,7 +756,7 @@ void TrkVolPitSet(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *tr { if (track->flags & MPT_FLG_VOLSET) { - u32 x; + s32 x; s32 y; x = (u32)(track->vol * track->volX) >> 5; -- cgit v1.2.3 From c31c2d93d73bd6c6d328df3ac07ab5d160a21008 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Fri, 16 Oct 2020 16:55:29 -0400 Subject: [LEAK INFORMED] match FadeOutBody --- src/m4a.c | 21 ++++----------------- 1 file changed, 4 insertions(+), 17 deletions(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index b8f9e21c1..a417466cf 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -643,40 +643,27 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) s32 i; struct MusicPlayerTrack *track; u16 fadeOV; -#ifdef NONMATCHING - u16 mask; -#else - register u16 mask asm("r2"); -#endif // NONMATCHING if (mplayInfo->fadeOI == 0) return; - - mplayInfo->fadeOC--; - mask = 0xFFFF; - - if (mplayInfo->fadeOC != 0) + if (--mplayInfo->fadeOC != 0) return; mplayInfo->fadeOC = mplayInfo->fadeOI; if (mplayInfo->fadeOV & FADE_IN) { - mplayInfo->fadeOV += (4 << FADE_VOL_SHIFT); - - if ((u16)(mplayInfo->fadeOV & mask) >= (64 << FADE_VOL_SHIFT)) + if ((u16)(mplayInfo->fadeOV += (4 << FADE_VOL_SHIFT)) >= (64 << FADE_VOL_SHIFT)) { mplayInfo->fadeOV = (64 << FADE_VOL_SHIFT); mplayInfo->fadeOI = 0; } } + else { - mplayInfo->fadeOV -= (4 << FADE_VOL_SHIFT); - - if ((s16)(mplayInfo->fadeOV & mask) <= 0) + if ((s16)(mplayInfo->fadeOV-=(4 << FADE_VOL_SHIFT)) <= 0) { - for (i = mplayInfo->trackCount, track = mplayInfo->tracks; i > 0; i--, track++) { u32 val; -- cgit v1.2.3 From bf03f1154f4e25b8d03035074bd4f77fe421aa3b Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Fri, 16 Oct 2020 16:57:41 -0400 Subject: formatting --- src/m4a.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index a417466cf..fa81a09c3 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -662,7 +662,7 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) else { - if ((s16)(mplayInfo->fadeOV-=(4 << FADE_VOL_SHIFT)) <= 0) + if ((s16)(mplayInfo->fadeOV -= (4 << FADE_VOL_SHIFT)) <= 0) { for (i = mplayInfo->trackCount, track = mplayInfo->tracks; i > 0; i--, track++) { -- cgit v1.2.3 From aedfe27f8d609e4a6cef15b977ae8322f190d094 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Fri, 16 Oct 2020 17:16:25 -0400 Subject: Remove newline --- src/m4a.c | 1 - 1 file changed, 1 deletion(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index fa81a09c3..2280fc74c 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -659,7 +659,6 @@ void FadeOutBody(struct MusicPlayerInfo *mplayInfo) mplayInfo->fadeOI = 0; } } - else { if ((s16)(mplayInfo->fadeOV -= (4 << FADE_VOL_SHIFT)) <= 0) -- cgit v1.2.3 From 8bf401c2272b7c4182f54cb1285f6067fb5247f9 Mon Sep 17 00:00:00 2001 From: Kurausukun Date: Mon, 2 Nov 2020 18:43:11 -0500 Subject: address comments --- src/m4a.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index 11a4525ea..50a9a54be 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -635,7 +635,7 @@ void MPlayStart(struct MusicPlayerInfo *mplayInfo, struct SongHeader *songHeader track++; } - if (songHeader->reverb & 0x80) + if (songHeader->reverb & SOUND_MODE_REVERB_SET) m4aSoundMode(songHeader->reverb); mplayInfo->ident = ID_NUMBER; -- cgit v1.2.3 From 15ff55eee58bd246d60f97c7ade919519f645654 Mon Sep 17 00:00:00 2001 From: Michael Panzlaff Date: Fri, 11 Dec 2020 13:58:00 +0100 Subject: use better m4a variable/constant names --- src/m4a.c | 226 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 114 insertions(+), 112 deletions(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index f061ba9b9..3d564a5a1 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -10,7 +10,7 @@ BSS_CODE ALIGNED(4) char SoundMainRAM_Buffer[0x800] = {0}; struct SoundInfo gSoundInfo; struct PokemonCrySong gPokemonCrySongs[MAX_POKEMON_CRIES]; struct MusicPlayerInfo gPokemonCryMusicPlayers[MAX_POKEMON_CRIES]; -void *gMPlayJumpTable[36]; +MPlayFunc gMPlayJumpTable[36]; struct CgbChannel gCgbChans[4]; struct MusicPlayerTrack gPokemonCryTracks[MAX_POKEMON_CRIES * 2]; struct PokemonCrySong gPokemonCrySong; @@ -293,7 +293,7 @@ void MPlayExtender(struct CgbChannel *cgbChans) gMPlayJumpTable[32] = FadeOutBody; gMPlayJumpTable[33] = TrkVolPitSet; - soundInfo->cgbChans = (struct CgbChannel *)cgbChans; + soundInfo->cgbChans = cgbChans; soundInfo->CgbSound = CgbSound; soundInfo->CgbOscOff = CgbOscOff; soundInfo->MidiKeyToCgbFreq = MidiKeyToCgbFreq; @@ -301,13 +301,13 @@ void MPlayExtender(struct CgbChannel *cgbChans) CpuFill32(0, cgbChans, sizeof(struct CgbChannel) * 4); - cgbChans[0].ty = 1; + cgbChans[0].type = 1; cgbChans[0].panMask = 0x11; - cgbChans[1].ty = 2; + cgbChans[1].type = 2; cgbChans[1].panMask = 0x22; - cgbChans[2].ty = 3; + cgbChans[2].type = 3; cgbChans[2].panMask = 0x44; - cgbChans[3].ty = 4; + cgbChans[3].type = 4; cgbChans[3].panMask = 0x88; soundInfo->ident = ident; @@ -362,15 +362,15 @@ void SoundInit(struct SoundInfo *soundInfo) soundInfo->maxChans = 8; soundInfo->masterVolume = 15; - soundInfo->plynote = (u32)ply_note; + soundInfo->plynote = ply_note; soundInfo->CgbSound = DummyFunc; - soundInfo->CgbOscOff = (void (*)(u8))DummyFunc; - soundInfo->MidiKeyToCgbFreq = (u32 (*)(u8, u8, u8))DummyFunc; - soundInfo->ExtVolPit = (u32)DummyFunc; + soundInfo->CgbOscOff = (CgbOscOffFunc)DummyFunc; + soundInfo->MidiKeyToCgbFreq = (MidiKeyToCgbFreqFunc)DummyFunc; + soundInfo->ExtVolPit = (ExtVolPitFunc)DummyFunc; MPlayJumpTableCopy(gMPlayJumpTable); - soundInfo->MPlayJumpTable = (u32)gMPlayJumpTable; + soundInfo->MPlayJumpTable = gMPlayJumpTable; SampleFreqSet(SOUND_MODE_FREQ_13379); @@ -437,7 +437,7 @@ void m4aSoundMode(u32 mode) while (temp != 0) { - chan->status = 0; + chan->statusFlags = 0; temp--; chan++; } @@ -483,7 +483,7 @@ void SoundClear(void) while (i > 0) { - ((struct SoundChannel *)chan)->status = 0; + ((struct SoundChannel *)chan)->statusFlags = 0; i--; chan = (void *)((s32)chan + sizeof(struct SoundChannel)); } @@ -497,7 +497,7 @@ void SoundClear(void) while (i <= 4) { soundInfo->CgbOscOff(i); - ((struct CgbChannel *)chan)->sf = 0; + ((struct CgbChannel *)chan)->statusFlags = 0; i++; chan = (void *)((s32)chan + sizeof(struct CgbChannel)); } @@ -572,15 +572,18 @@ void MPlayOpen(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track tracks++; } - if (soundInfo->func != 0) + // append music player and MPlayMain to linked list + + if (soundInfo->MPlayMainHead != NULL) { - mplayInfo->func = soundInfo->func; - mplayInfo->intp = soundInfo->intp; - soundInfo->func = 0; + mplayInfo->MPlayMainNext = soundInfo->MPlayMainHead; + mplayInfo->musicPlayerNext = soundInfo->musicPlayerHead; + // NULL assignment semantically useless, but required for match + soundInfo->MPlayMainHead = NULL; } - soundInfo->intp = (u32)mplayInfo; - soundInfo->func = (u32)MPlayMain; + soundInfo->musicPlayerHead = mplayInfo; + soundInfo->MPlayMainHead = MPlayMain; soundInfo->ident = ID_NUMBER; mplayInfo->ident = ID_NUMBER; } @@ -884,7 +887,7 @@ void CgbModVol(struct CgbChannel *chan) if ((soundInfo->mode & 1) || !CgbPan(chan)) { chan->pan = 0xFF; - chan->eg = (u32)(chan->rightVolume + chan->leftVolume) >> 4; + chan->envelopeGoal = (u32)(chan->rightVolume + chan->leftVolume) >> 4; } else { @@ -895,12 +898,12 @@ void CgbModVol(struct CgbChannel *chan) asm("" : : : "memory"); #endif - chan->eg = (u32)(chan->rightVolume + chan->leftVolume) >> 4; - if (chan->eg > 15) - chan->eg = 15; + chan->envelopeGoal = (u32)(chan->rightVolume + chan->leftVolume) >> 4; + if (chan->envelopeGoal > 15) + chan->envelopeGoal = 15; } - chan->sg = (chan->eg * chan->su + 15) >> 4; + chan->sustainGoal = (chan->envelopeGoal * chan->sustain + 15) >> 4; chan->pan &= chan->panMask; } @@ -927,7 +930,7 @@ void CgbSound(void) for (ch = 1, channels = soundInfo->cgbChans; ch <= 4; ch++, channels++) { - if (!(channels->sf & 0xc7)) + if (!(channels->statusFlags & SOUND_CHANNEL_SF_ON)) continue; switch (ch) @@ -965,53 +968,53 @@ void CgbSound(void) prevC15 = soundInfo->c15; evAdd = *nrx2ptr; - if (channels->sf & 0x80) + if (channels->statusFlags & SOUND_CHANNEL_SF_START) { - if (!(channels->sf & 0x40)) + if (!(channels->statusFlags & SOUND_CHANNEL_SF_STOP)) { - channels->sf = 3; - channels->mo = 3; + channels->statusFlags = SOUND_CHANNEL_SF_ENV_ATTACK; + channels->modify = CGB_CHANNEL_MO_PIT | CGB_CHANNEL_MO_VOL; CgbModVol(channels); switch (ch) { case 1: - *nrx0ptr = channels->sw; + *nrx0ptr = channels->sweep; // fallthrough case 2: - *nrx1ptr = ((u32)channels->wp << 6) + channels->le; + *nrx1ptr = ((u32)channels->wavePointer << 6) + channels->length; goto loc_82E0E30; case 3: - if ((u32)channels->wp != channels->cp) + if (channels->wavePointer != channels->currentPointer) { *nrx0ptr = 0x40; - REG_WAVE_RAM0 = channels->wp[0]; - REG_WAVE_RAM1 = channels->wp[1]; - REG_WAVE_RAM2 = channels->wp[2]; - REG_WAVE_RAM3 = channels->wp[3]; - channels->cp = (u32)channels->wp; + REG_WAVE_RAM0 = channels->wavePointer[0]; + REG_WAVE_RAM1 = channels->wavePointer[1]; + REG_WAVE_RAM2 = channels->wavePointer[2]; + REG_WAVE_RAM3 = channels->wavePointer[3]; + channels->currentPointer = channels->wavePointer; } *nrx0ptr = 0; - *nrx1ptr = channels->le; - if (channels->le) - channels->n4 = -64; + *nrx1ptr = channels->length; + if (channels->length) + channels->n4 = 0xC0; else - channels->n4 = -128; + channels->n4 = 0x80; break; default: - *nrx1ptr = channels->le; - *nrx3ptr = (u32)channels->wp << 3; + *nrx1ptr = channels->length; + *nrx3ptr = (u32)channels->wavePointer << 3; loc_82E0E30: - evAdd = channels->at + 8; - if (channels->le) - channels->n4 = 64; + evAdd = channels->attack + 8; + if (channels->length) + channels->n4 = 0x40; else - channels->n4 = 0; + channels->n4 = 0x00; break; } - channels->ec = channels->at; - if ((s8)(channels->at & mask)) + channels->envelopeCounter = channels->attack; + if ((s8)(channels->attack & mask)) { - channels->ev = 0; + channels->envelopeVolume = 0; goto EC_MINUS; } else @@ -1024,28 +1027,28 @@ void CgbSound(void) goto loc_82E0E82; } } - else if (channels->sf & 0x04) + else if (channels->statusFlags & SOUND_CHANNEL_SF_IEC) { - channels->echoLength--; - if ((s8)(channels->echoLength & mask) <= 0) + channels->pseudoEchoLength--; + if ((s8)(channels->pseudoEchoLength & mask) <= 0) { loc_82E0E82: CgbOscOff(ch); - channels->sf = 0; + channels->statusFlags = 0; goto LAST_LABEL; } goto loc_82E0FD6; } - else if ((channels->sf & 0x40) && (channels->sf & 0x03)) + else if ((channels->statusFlags & SOUND_CHANNEL_SF_STOP) && (channels->statusFlags & SOUND_CHANNEL_SF_ENV)) { - channels->sf &= 0xfc; - channels->ec = channels->re; - if ((s8)(channels->re & mask)) + channels->statusFlags &= ~SOUND_CHANNEL_SF_ENV; + channels->envelopeCounter = channels->release; + if ((s8)(channels->release & mask)) { - channels->mo |= 1; + channels->modify |= CGB_CHANNEL_MO_VOL; if (ch != 3) { - evAdd = channels->re; + evAdd = channels->release; } goto EC_MINUS; } @@ -1057,24 +1060,24 @@ void CgbSound(void) else { loc_82E0ED0: - if (channels->ec == 0) + if (channels->envelopeCounter == 0) { if (ch == 3) { - channels->mo |= 1; + channels->modify |= CGB_CHANNEL_MO_VOL; } CgbModVol(channels); - if ((channels->sf & 0x3) == 0) + if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == 0) { - channels->ev--; - if ((s8)(channels->ev & mask) <= 0) + channels->envelopeVolume--; + if ((s8)(channels->envelopeVolume & mask) <= 0) { loc_82E0F02: - channels->ev = ((channels->eg * channels->echoVolume) + 0xFF) >> 8; - if (channels->ev) + channels->envelopeVolume = ((channels->envelopeGoal * channels->pseudoEchoVolume) + 0xFF) >> 8; + if (channels->envelopeVolume) { - channels->sf |= 0x4; - channels->mo |= 1; + channels->statusFlags |= SOUND_CHANNEL_SF_IEC; + channels->modify |= CGB_CHANNEL_MO_VOL; if (ch != 3) { evAdd = 8; @@ -1088,34 +1091,34 @@ void CgbSound(void) } else { - channels->ec = channels->re; + channels->envelopeCounter = channels->release; } } - else if ((channels->sf & 0x3) == 1) + else if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == 1) { loc_82E0F3A: - channels->ev = channels->sg; - channels->ec = 7; + channels->envelopeVolume = channels->sustainGoal; + channels->envelopeCounter = 7; } - else if ((channels->sf & 0x3) == 2) + else if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == 2) { - int ev, sg; + int envelopeVolume, sustainGoal; - channels->ev--; - ev = (s8)(channels->ev & mask); - sg = (s8)(channels->sg); - if (ev <= sg) + channels->envelopeVolume--; + envelopeVolume = (s8)(channels->envelopeVolume & mask); + sustainGoal = (s8)(channels->sustainGoal); + if (envelopeVolume <= sustainGoal) { loc_82E0F5A: - if (channels->su == 0) + if (channels->sustain == 0) { - channels->sf &= 0xfc; + channels->statusFlags &= ~SOUND_CHANNEL_SF_ENV; goto loc_82E0F02; } else { - channels->sf--; - channels->mo |= 1; + channels->statusFlags--; + channels->modify |= CGB_CHANNEL_MO_VOL; if (ch != 3) { evAdd = 8; @@ -1125,24 +1128,24 @@ void CgbSound(void) } else { - channels->ec = channels->de; + channels->envelopeCounter = channels->decay; } } else { - channels->ev++; - if ((u8)(channels->ev & mask) >= channels->eg) + channels->envelopeVolume++; + if ((u8)(channels->envelopeVolume & mask) >= channels->envelopeGoal) { loc_82E0F96: - channels->sf--; - channels->ec = channels->de; - if ((u8)(channels->ec & mask)) + channels->statusFlags--; + channels->envelopeCounter = channels->decay; + if ((u8)(channels->envelopeCounter & mask)) { - channels->mo |= 1; - channels->ev = channels->eg; + channels->modify |= CGB_CHANNEL_MO_VOL; + channels->envelopeVolume = channels->envelopeGoal; if (ch != 3) { - evAdd = channels->de; + evAdd = channels->decay; } } else @@ -1152,14 +1155,14 @@ void CgbSound(void) } else { - channels->ec = channels->at; + channels->envelopeCounter = channels->attack; } } } } EC_MINUS: - channels->ec--; + channels->envelopeCounter--; if (prevC15 == 0) { prevC15--; @@ -1167,39 +1170,39 @@ void CgbSound(void) } loc_82E0FD6: - if (channels->mo & 0x2) + if (channels->modify & CGB_CHANNEL_MO_PIT) { - if (ch < 4 && (channels->ty & 0x08)) + if (ch < 4 && (channels->type & TONEDATA_TYPE_FIX)) { int biasH = REG_SOUNDBIAS_H; if (biasH < 64) { - channels->fr = (channels->fr + 2) & 0x7fc; + channels->frequency = (channels->frequency + 2) & 0x7fc; } else if (biasH < 128) { - channels->fr = (channels->fr + 1) & 0x7fe; + channels->frequency = (channels->frequency + 1) & 0x7fe; } } if (ch != 4) { - *nrx3ptr = channels->fr; + *nrx3ptr = channels->frequency; } else { - *nrx3ptr = (*nrx3ptr & 0x08) | channels->fr; + *nrx3ptr = (*nrx3ptr & 0x08) | channels->frequency; } - channels->n4 = (channels->n4 & 0xC0) + (*((u8*)(&channels->fr) + 1)); + channels->n4 = (channels->n4 & 0xC0) + (*((u8*)(&channels->frequency) + 1)); *nrx4ptr = (s8)(channels->n4 & mask); } - if (channels->mo & 1) + if (channels->modify & 1) { REG_NR51 = (REG_NR51 & ~channels->panMask) | channels->pan; if (ch == 3) { - *nrx2ptr = gCgb3Vol[channels->ev]; + *nrx2ptr = gCgb3Vol[channels->envelopeVolume]; if (channels->n4 & 0x80) { *nrx0ptr = 0x80; @@ -1210,7 +1213,7 @@ void CgbSound(void) else { evAdd &= 0xf; - *nrx2ptr = (channels->ev << 4) + evAdd; + *nrx2ptr = (channels->envelopeVolume << 4) + evAdd; *nrx4ptr = channels->n4 | 0x80; if (ch == 1 && !(*nrx0ptr & 0x08)) { @@ -1220,7 +1223,7 @@ void CgbSound(void) } LAST_LABEL: - channels->mo = 0; + channels->modify = 0; } } @@ -1504,8 +1507,8 @@ void ply_memacc(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *trac cond_true: { - void (*func)(struct MusicPlayerInfo *, struct MusicPlayerTrack *) = *(&gMPlayJumpTable[1]); - func(mplayInfo, track); + // *& is required for matching + (*&gMPlayJumpTable[1])(mplayInfo, track); return; } @@ -1523,8 +1526,7 @@ void ply_xcmd(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track) void ply_xxx(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track) { - void (*func)(struct MusicPlayerInfo *, struct MusicPlayerTrack *) = *(&gMPlayJumpTable[0]); - func(mplayInfo, track); + gMPlayJumpTable[0](mplayInfo, track); } #define READ_XCMD_BYTE(var, n) \ @@ -1580,13 +1582,13 @@ void ply_xrele(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track void ply_xiecv(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track) { - track->echoVolume = *track->cmdPtr; + track->pseudoEchoVolume = *track->cmdPtr; track->cmdPtr++; } void ply_xiecl(struct MusicPlayerInfo *mplayInfo, struct MusicPlayerTrack *track) { - track->echoLength = *track->cmdPtr; + track->pseudoEchoLength = *track->cmdPtr; track->cmdPtr++; } -- cgit v1.2.3 From f177c736fc722a2168a105b595888a0e6a6d5c02 Mon Sep 17 00:00:00 2001 From: Michael Panzlaff Date: Fri, 11 Dec 2020 16:06:18 +0100 Subject: more CgbSound decipher work --- src/m4a.c | 109 ++++++++++++++++++++++++++++---------------------------------- 1 file changed, 49 insertions(+), 60 deletions(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index 3d564a5a1..99feeadd7 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -911,7 +911,7 @@ void CgbSound(void) { s32 ch; struct CgbChannel *channels; - s32 evAdd; + s32 envelopeStepTimeAndDir; s32 prevC15; struct SoundInfo *soundInfo = SOUND_INFO_PTR; vu8 *nrx0ptr; @@ -933,6 +933,7 @@ void CgbSound(void) if (!(channels->statusFlags & SOUND_CHANNEL_SF_ON)) continue; + /* 1. determine hardware channel registers */ switch (ch) { case 1: @@ -966,8 +967,9 @@ void CgbSound(void) } prevC15 = soundInfo->c15; - evAdd = *nrx2ptr; + envelopeStepTimeAndDir = *nrx2ptr; + /* 2. calculate envelope volume */ if (channels->statusFlags & SOUND_CHANNEL_SF_START) { if (!(channels->statusFlags & SOUND_CHANNEL_SF_STOP)) @@ -982,7 +984,7 @@ void CgbSound(void) // fallthrough case 2: *nrx1ptr = ((u32)channels->wavePointer << 6) + channels->length; - goto loc_82E0E30; + goto init_env_step_time_dir; case 3: if (channels->wavePointer != channels->currentPointer) { @@ -1003,8 +1005,8 @@ void CgbSound(void) default: *nrx1ptr = channels->length; *nrx3ptr = (u32)channels->wavePointer << 3; - loc_82E0E30: - evAdd = channels->attack + 8; + init_env_step_time_dir: + envelopeStepTimeAndDir = channels->attack + CGB_NRx2_ENV_DIR_INC; if (channels->length) channels->n4 = 0x40; else @@ -1015,16 +1017,17 @@ void CgbSound(void) if ((s8)(channels->attack & mask)) { channels->envelopeVolume = 0; - goto EC_MINUS; + goto envelope_step_complete; } else { - goto loc_82E0F96; + // skip attack phase if attack is instantaneous (=0) + goto envelope_decay_start; } } else { - goto loc_82E0E82; + goto oscillator_off; } } else if (channels->statusFlags & SOUND_CHANNEL_SF_IEC) @@ -1032,12 +1035,12 @@ void CgbSound(void) channels->pseudoEchoLength--; if ((s8)(channels->pseudoEchoLength & mask) <= 0) { - loc_82E0E82: + oscillator_off: CgbOscOff(ch); channels->statusFlags = 0; - goto LAST_LABEL; + goto channel_complete; } - goto loc_82E0FD6; + goto envelope_complete; } else if ((channels->statusFlags & SOUND_CHANNEL_SF_STOP) && (channels->statusFlags & SOUND_CHANNEL_SF_ENV)) { @@ -1047,46 +1050,41 @@ void CgbSound(void) { channels->modify |= CGB_CHANNEL_MO_VOL; if (ch != 3) - { - evAdd = channels->release; - } - goto EC_MINUS; + envelopeStepTimeAndDir = channels->release | CGB_NRx2_ENV_DIR_DEC; + goto envelope_step_complete; } else { - goto loc_82E0F02; + goto envelope_pseudoecho_start; } } else { - loc_82E0ED0: + envelope_step_repeat: if (channels->envelopeCounter == 0) { if (ch == 3) - { channels->modify |= CGB_CHANNEL_MO_VOL; - } + CgbModVol(channels); - if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == 0) + if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == SOUND_CHANNEL_SF_ENV_RELEASE) { channels->envelopeVolume--; if ((s8)(channels->envelopeVolume & mask) <= 0) { - loc_82E0F02: + envelope_pseudoecho_start: channels->envelopeVolume = ((channels->envelopeGoal * channels->pseudoEchoVolume) + 0xFF) >> 8; if (channels->envelopeVolume) { channels->statusFlags |= SOUND_CHANNEL_SF_IEC; channels->modify |= CGB_CHANNEL_MO_VOL; if (ch != 3) - { - evAdd = 8; - } - goto loc_82E0FD6; + envelopeStepTimeAndDir = 0 | CGB_NRx2_ENV_DIR_INC; + goto envelope_complete; } else { - goto loc_82E0E82; + goto oscillator_off; } } else @@ -1094,13 +1092,13 @@ void CgbSound(void) channels->envelopeCounter = channels->release; } } - else if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == 1) + else if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == SOUND_CHANNEL_SF_ENV_SUSTAIN) { - loc_82E0F3A: + envelope_sustain: channels->envelopeVolume = channels->sustainGoal; channels->envelopeCounter = 7; } - else if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == 2) + else if ((channels->statusFlags & SOUND_CHANNEL_SF_ENV) == SOUND_CHANNEL_SF_ENV_DECAY) { int envelopeVolume, sustainGoal; @@ -1109,21 +1107,19 @@ void CgbSound(void) sustainGoal = (s8)(channels->sustainGoal); if (envelopeVolume <= sustainGoal) { - loc_82E0F5A: + envelope_sustain_start: if (channels->sustain == 0) { channels->statusFlags &= ~SOUND_CHANNEL_SF_ENV; - goto loc_82E0F02; + goto envelope_pseudoecho_start; } else { channels->statusFlags--; channels->modify |= CGB_CHANNEL_MO_VOL; if (ch != 3) - { - evAdd = 8; - } - goto loc_82E0F3A; + envelopeStepTimeAndDir = 0 | CGB_NRx2_ENV_DIR_INC; + goto envelope_sustain; } } else @@ -1136,7 +1132,7 @@ void CgbSound(void) channels->envelopeVolume++; if ((u8)(channels->envelopeVolume & mask) >= channels->envelopeGoal) { - loc_82E0F96: + envelope_decay_start: channels->statusFlags--; channels->envelopeCounter = channels->decay; if ((u8)(channels->envelopeCounter & mask)) @@ -1144,13 +1140,11 @@ void CgbSound(void) channels->modify |= CGB_CHANNEL_MO_VOL; channels->envelopeVolume = channels->envelopeGoal; if (ch != 3) - { - evAdd = channels->decay; - } + envelopeStepTimeAndDir = channels->decay | CGB_NRx2_ENV_DIR_DEC; } else { - goto loc_82E0F5A; + goto envelope_sustain_start; } } else @@ -1161,43 +1155,40 @@ void CgbSound(void) } } - EC_MINUS: + envelope_step_complete: + // every 15 frames, envelope calculation has to be done twice + // to keep up with the hardware envelope rate (1/64 s) channels->envelopeCounter--; if (prevC15 == 0) { prevC15--; - goto loc_82E0ED0; + goto envelope_step_repeat; } - loc_82E0FD6: + envelope_complete: + /* 3. apply pitch to HW registers */ if (channels->modify & CGB_CHANNEL_MO_PIT) { if (ch < 4 && (channels->type & TONEDATA_TYPE_FIX)) { - int biasH = REG_SOUNDBIAS_H; + int dac_pwm_rate = REG_SOUNDBIAS_H; - if (biasH < 64) - { + if (dac_pwm_rate < 0x40) // if PWM rate = 32768 Hz channels->frequency = (channels->frequency + 2) & 0x7fc; - } - else if (biasH < 128) - { + else if (dac_pwm_rate < 0x80) // if PWM rate = 65536 Hz channels->frequency = (channels->frequency + 1) & 0x7fe; - } } + if (ch != 4) - { *nrx3ptr = channels->frequency; - } else - { *nrx3ptr = (*nrx3ptr & 0x08) | channels->frequency; - } channels->n4 = (channels->n4 & 0xC0) + (*((u8*)(&channels->frequency) + 1)); *nrx4ptr = (s8)(channels->n4 & mask); } - if (channels->modify & 1) + /* 4. apply envelope & volume to HW registers */ + if (channels->modify & CGB_CHANNEL_MO_VOL ) { REG_NR51 = (REG_NR51 & ~channels->panMask) | channels->pan; if (ch == 3) @@ -1212,17 +1203,15 @@ void CgbSound(void) } else { - evAdd &= 0xf; - *nrx2ptr = (channels->envelopeVolume << 4) + evAdd; + envelopeStepTimeAndDir &= 0xf; + *nrx2ptr = (channels->envelopeVolume << 4) + envelopeStepTimeAndDir; *nrx4ptr = channels->n4 | 0x80; if (ch == 1 && !(*nrx0ptr & 0x08)) - { *nrx4ptr = channels->n4 | 0x80; - } } } - LAST_LABEL: + channel_complete: channels->modify = 0; } } -- cgit v1.2.3 From eaff64e223d88e20134392a1058faa4edc74f200 Mon Sep 17 00:00:00 2001 From: Michael Panzlaff Date: Sat, 12 Dec 2020 13:19:08 +0100 Subject: remove trailing spaces --- src/m4a.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/m4a.c') diff --git a/src/m4a.c b/src/m4a.c index 99feeadd7..e079ba4f6 100644 --- a/src/m4a.c +++ b/src/m4a.c @@ -1188,7 +1188,7 @@ void CgbSound(void) } /* 4. apply envelope & volume to HW registers */ - if (channels->modify & CGB_CHANNEL_MO_VOL ) + if (channels->modify & CGB_CHANNEL_MO_VOL) { REG_NR51 = (REG_NR51 & ~channels->panMask) | channels->pan; if (ch == 3) -- cgit v1.2.3