From 262cd83c6a62cc3ff4ad4b1b47a448637aba5ea9 Mon Sep 17 00:00:00 2001 From: Sour Date: Sun, 2 May 2021 23:36:19 -0400 Subject: [PATCH] NSF - Fixed issues with playback, fixed flickering in UI --- Core/NES/Mappers/NsfMapper.cpp | 53 +++++++++++++--------------- Core/NES/Mappers/NsfMapper.h | 6 ++-- Core/NES/NesConsole.cpp | 1 - Core/NES/NesCpu.cpp | 6 ++-- Core/NES/NesPpu.cpp | 1 + Core/Shared/Audio/AudioPlayerHud.cpp | 4 +-- 6 files changed, 33 insertions(+), 38 deletions(-) diff --git a/Core/NES/Mappers/NsfMapper.cpp b/Core/NES/Mappers/NsfMapper.cpp index bd44eeb1..1d0f3898 100644 --- a/Core/NES/Mappers/NsfMapper.cpp +++ b/Core/NES/Mappers/NsfMapper.cpp @@ -79,6 +79,15 @@ void NsfMapper::InitMapper(RomData& romData) if(_nsfHeader.SoundChips & NsfSoundChips::FDS) { AddRegisterRange(0x4040, 0x4092, MemoryOperation::Any); } + + //Reset/IRQ vector + AddRegisterRange(0xFFFC, 0xFFFF, MemoryOperation::Read); + + //Set PLAY/INIT addresses in NSF code + _nsfBios[0x02] = _nsfHeader.InitAddress & 0xFF; + _nsfBios[0x03] = (_nsfHeader.InitAddress >> 8) & 0xFF; + _nsfBios[0x14] = _nsfHeader.PlayAddress & 0xFF; + _nsfBios[0x15] = (_nsfHeader.PlayAddress >> 8) & 0xFF; } void NsfMapper::Reset(bool softReset) @@ -87,28 +96,12 @@ void NsfMapper::Reset(bool softReset) _songNumber = _nsfHeader.StartingSong - 1; } + //INIT logic NesMemoryManager* mm = _console->GetMemoryManager(); - //INIT logic - - //Set PLAY/INIT addresses in NSF code - _nsfBios[0x02] = _nsfHeader.InitAddress & 0xFF; - _nsfBios[0x03] = (_nsfHeader.InitAddress >> 8) & 0xFF; - - _nsfBios[0x05] = _nsfHeader.PlayAddress & 0xFF; - _nsfBios[0x06] = (_nsfHeader.PlayAddress >> 8) & 0xFF; - _nsfBios[0x14] = _nsfHeader.PlayAddress & 0xFF; - _nsfBios[0x15] = (_nsfHeader.PlayAddress >> 8) & 0xFF; - - //Clear internal RAM - for(uint16_t i = 0; i < 0x800; i++) { - mm->Write(i, 0, MemoryOperationType::Write); - } - - //Clear work ram - for(uint16_t i = 0x6000; i < 0x7FFF; i++) { - mm->Write(i, 0, MemoryOperationType::Write); - } + //Clear internal + work RAM + memset(mm->GetInternalRAM(), 0, NesMemoryManager::InternalRAMSize); + memset(GetWorkRam(), 0, GetWorkRamSize()); //Reset APU for(uint16_t i = 0x4000; i < 0x4013; i++) { @@ -153,7 +146,7 @@ void NsfMapper::Reset(bool softReset) state.Y = 0; _console->GetCpu()->SetIrqMask((uint8_t)IRQSource::External); - _irqCounter = GetIrqReloadValue(); + _irqCounter = 0; _allowSilenceDetection = false; _trackEndCounter = -1; @@ -168,9 +161,6 @@ void NsfMapper::Reset(bool softReset) _sunsoftAudio.reset(new Sunsoft5bAudio(_console)); InternalSelectTrack(_songNumber); - - //Reset/IRQ vector - AddRegisterRange(0xFFFC, 0xFFFF, MemoryOperation::Read); } void NsfMapper::GetMemoryRanges(MemoryRanges& ranges) @@ -285,10 +275,12 @@ void NsfMapper::ProcessCpuClock() } */ - _irqCounter--; - if(_irqCounter == 0) { - _irqCounter = GetIrqReloadValue(); - TriggerIrq(); + if(_irqCounter > 0) { + _irqCounter--; + if(_irqCounter == 0) { + _irqCounter = GetIrqReloadValue(); + TriggerIrq(); + } } ClockLengthAndFadeCounters(); @@ -354,7 +346,10 @@ void NsfMapper::WriteRegister(uint16_t addr, uint8_t value) _sunsoftAudio->WriteRegister(addr, value); } else { switch(addr) { - case 0x3F00: ClearIrq(); break; + case 0x3F00: + _irqCounter = GetIrqReloadValue(); + ClearIrq(); + break; //MMC5 multiplication case 0x5205: _mmc5MultiplierValues[0] = value; break; diff --git a/Core/NES/Mappers/NsfMapper.h b/Core/NES/Mappers/NsfMapper.h index a4ae35a9..7f01072f 100644 --- a/Core/NES/Mappers/NsfMapper.h +++ b/Core/NES/Mappers/NsfMapper.h @@ -51,8 +51,8 @@ private: .org $3F00 reset: CLI - JSR [INIT] ; set at load time - JSR [PLAY] ; set at load time + JSR [INIT] ;set at load time + STA $3F00 ;clear irq loop: JMP loop ;loop forever @@ -64,7 +64,7 @@ private: */ uint8_t _nsfBios[0x20] = { - 0x58,0x20,0x00,0x00,0x20,0x00,0x00,0x4C,0x07,0x3F,0x00,0x00,0x00,0x00,0x00,0x00, + 0x58,0x20,0x00,0x00,0x8D,0x00,0x3F,0x4C,0x07,0x3F,0x00,0x00,0x00,0x00,0x00,0x00, 0x8D,0x00,0x3F,0x20,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }; diff --git a/Core/NES/NesConsole.cpp b/Core/NES/NesConsole.cpp index 696a6292..de1bdb61 100644 --- a/Core/NES/NesConsole.cpp +++ b/Core/NES/NesConsole.cpp @@ -236,7 +236,6 @@ void NesConsole::RunFrame() RunVsSubConsole(); } } - _emu->ProcessEndOfFrame(); } void NesConsole::RunVsSubConsole() diff --git a/Core/NES/NesCpu.cpp b/Core/NES/NesCpu.cpp index 54669c10..c4021463 100644 --- a/Core/NES/NesCpu.cpp +++ b/Core/NES/NesCpu.cpp @@ -92,9 +92,6 @@ void NesCpu::Reset(bool softReset, ConsoleRegion region) _dmcDmaRunning = false; _lastCrashWarning = 0; - //Used by NSF code to disable Frame Counter & DMC interrupts - _irqMask = 0xFF; - //Use _memoryManager->Read() directly to prevent clocking the PPU/APU when setting PC at reset _state.PC = _memoryManager->Read(NesCpu::ResetVector) | _memoryManager->Read(NesCpu::ResetVector+1) << 8; @@ -102,6 +99,9 @@ void NesCpu::Reset(bool softReset, ConsoleRegion region) SetFlags(PSFlags::Interrupt); _state.SP -= 0x03; } else { + //Used by NSF code to disable Frame Counter & DMC interrupts + _irqMask = 0xFF; + _state.A = 0; _state.SP = 0xFD; _state.X = 0; diff --git a/Core/NES/NesPpu.cpp b/Core/NES/NesPpu.cpp index 11da6e91..99dcc144 100644 --- a/Core/NES/NesPpu.cpp +++ b/Core/NES/NesPpu.cpp @@ -1198,6 +1198,7 @@ void NesPpu::SendFrame() UpdateGrayscaleAndIntensifyBits(); if(_console->IsVsMainConsole()) { + _emu->ProcessEndOfFrame(); _emu->GetNotificationManager()->SendNotification(ConsoleNotificationType::PpuFrameDone, _currentOutputBuffer); } diff --git a/Core/Shared/Audio/AudioPlayerHud.cpp b/Core/Shared/Audio/AudioPlayerHud.cpp index eaa8ace5..29f6d870 100644 --- a/Core/Shared/Audio/AudioPlayerHud.cpp +++ b/Core/Shared/Audio/AudioPlayerHud.cpp @@ -56,10 +56,10 @@ void AudioPlayerHud::Draw() _hud->DrawString(15, 218, std::to_string(trackInfo.TrackNumber) + " / " + std::to_string(trackInfo.TrackCount), 0xFFFFFF, 0, 1); if(trackInfo.Length <= 0) { - _hud->DrawString(220, 208, position, 0xFFFFFF, 0, 1); + _hud->DrawString(215, 208, " " + position + " ", 0xFFFFFF, 0, 1); } else { position += " / " + FormatSeconds(trackInfo.Length); - _hud->DrawString(182, 208, position, 0xFFFFFF, 0, 1); + _hud->DrawString(177, 208, " " + position + " ", 0xFFFFFF, 0, 1); constexpr int barWidth = 222; _hud->DrawRectangle(15, 199, barWidth + 4, 6, 0xBBBBBB, false, 1);