diff --git a/Core/NES/Mappers/Homebrew/Rainbow.cpp b/Core/NES/Mappers/Homebrew/Rainbow.cpp index 1c6e9daea2..733d09bedd 100644 --- a/Core/NES/Mappers/Homebrew/Rainbow.cpp +++ b/Core/NES/Mappers/Homebrew/Rainbow.cpp @@ -114,7 +114,7 @@ uint8_t Rainbow::ReadRam(uint16_t addr) if(_cpuIrqAckOn4011) { AckCpuIrq(); } - return (_audio->GetLastOutput() << 1); + return _audio->GetLastOutput4011(_console->GetMemoryManager()->GetOpenBus()); } return BaseMapper::InternalRead(addr); } @@ -264,7 +264,11 @@ void Rainbow::ProcessCpuClock() { BaseProcessCpuClock(); - _jitterCounter++; + if(_slIrqPending) { + _jitterCounter++; + } else { + _jitterCounter = 0; + } _parityCounter = !_parityCounter; _audio->Clock(); @@ -286,7 +290,6 @@ void Rainbow::ProcessCpuClock() _scanlineCounter = -1; _ntFetchCounter = 0; _ntReadCounter = 0; - _oamAddr = 0; } } } @@ -297,15 +300,15 @@ void Rainbow::UpdateInWindowFlag() bool xMatch; uint8_t scanline = _ntFetchCounter >= 49 ? _scanlineCounter + 1 : _scanlineCounter; - if(_windowY1 >= _windowY2) { - yMatch = scanline <= _windowY2 || scanline > _windowY1; + if(_windowY1 > _windowY2) { + yMatch = scanline <= _windowY2 || scanline >= _windowY1; } else { yMatch = scanline >= _windowY1 && scanline <= _windowY2; } - uint8_t column = (_ntFetchCounter + 1) % 50; - if(_windowX1 >= _windowX2) { - xMatch = column <= _windowX2 || column > _windowX1; + uint8_t column = ((_ntFetchCounter + 1) % 50) & 0x1F; + if(_windowX1 > _windowX2) { + xMatch = column <= _windowX2 || column >= _windowX1; } else { xMatch = column >= _windowX1 && column <= _windowX2; } @@ -448,18 +451,16 @@ uint8_t Rainbow::ReadChr(uint32_t fetchAddr, uint16_t ppuAddr) default: case 0: return _chrRomSize ? _chrRom[fetchAddr & (_chrRomSize - 1)] : 0; case 1: return _chrRamSize ? _chrRam[fetchAddr & (_chrRamSize - 1)] : 0; - case 2: return _mapperRam[fetchAddr & 0x1FFF]; + case 2: return _mapperRam[fetchAddr & 0x0FFF]; case 3: return GetNametable((ppuAddr & 0x400) >> 10)[ppuAddr & 0x3FF]; } } void Rainbow::UpdateIrqStatus() { - bool active = (_cpuIrqEnabled && _cpuIrqPending) || (_slIrqEnabled && _slIrqPending); + _wifiIrqPending = _wifiIrqEnabled && _dataReceived; + bool active = (_cpuIrqEnabled && _cpuIrqPending) || (_slIrqEnabled && _slIrqPending) || _wifiIrqPending; if(active) { - if(!_console->GetCpu()->HasIrqSource(IRQSource::External)) { - _jitterCounter = 0; - } _console->GetCpu()->SetIrqSource(IRQSource::External); } else { _console->GetCpu()->ClearIrqSource(IRQSource::External); @@ -479,8 +480,6 @@ void Rainbow::ProcessSpriteEval() int scanline = _scanlineCounter; int height = (_largeSprites ? 16 : 8); - memset(_oamMappings, 0, sizeof(_oamMappings)); - for(int i = 0; i < 64; i++) { uint8_t y = _oamPosY[i]; if(scanline >= y && scanline < y + height) { @@ -502,7 +501,8 @@ void Rainbow::DetectScanlineStart(uint16_t addr) _ntReadCounter++; if(_ntReadCounter >= 2) { - //After 3 identical NT reads, trigger IRQ when the following attribute byte is read + //The third consecutive read from the same NT address starts the scanline + //and counts as PPU read 1 for scanline IRQ timing if(!_inFrame) { _inFrame = true; _scanlineCounter = 0; @@ -513,7 +513,7 @@ void Rainbow::DetectScanlineStart(uint16_t addr) ProcessSpriteEval(); _ntFetchCounter = 0; - _ppuReadCounter = 0; + _ppuReadCounter = 1; _ntReadCounter = 0; _inHBlank = false; } @@ -531,14 +531,6 @@ uint8_t Rainbow::ReadRegister(uint16_t addr) case 0x4100: return _highMode | (_lowMode << 7); case 0x4120: return _chrMode | ((uint8_t)_windowEnabled << 4) | ((uint8_t)_spriteExtMode << 5) | (_chrSource << 6); - case 0x412A: - case 0x412B: - case 0x412C: - case 0x412D: - return _ntControl[addr - 0x412A].ToByte(); - - case 0x412F: return _windowControl.ToByte(); - case 0x4150: return (uint8_t)_scanlineCounter; case 0x4151: @@ -551,7 +543,8 @@ uint8_t Rainbow::ReadRegister(uint16_t addr) case 0x4154: return _jitterCounter; case 0x4157: return ((uint8_t)_parityCounter << 7); - case 0x415F: { + case 0x415F: + { uint8_t value = _mapperRam[_fpgaRamAddr]; _fpgaRamAddr = (_fpgaRamAddr + _fpgaRamInc) & 0x1FFF; return value; @@ -559,10 +552,10 @@ uint8_t Rainbow::ReadRegister(uint16_t addr) case 0x4160: return MAPPER_VERSION; case 0x4161: - return ( - (uint8_t)_wifiIrqPending | - ((uint8_t)_cpuIrqPending << 6) | - ((uint8_t)_slIrqPending << 7)); + return + uint8_t(_wifiIrqEnabled && _dataReceived) | + (uint8_t(_cpuIrqEnabled && _cpuIrqPending) << 6) | + (uint8_t(_slIrqEnabled && _slIrqPending) << 7); case 0x4280: GenerateOamSlowUpdate(); break; case 0x4282: GenerateExtUpdate(); break; @@ -631,6 +624,7 @@ void Rainbow::WriteRegister(uint16_t addr, uint8_t value) case 0x4120: _chrMode = value & 0x07; + if(_chrMode > 4) _chrMode = 4; _windowEnabled = value & 0x10; _spriteExtMode = value & 0x20; _chrSource = (value & 0xC0) >> 6; @@ -641,18 +635,21 @@ void Rainbow::WriteRegister(uint16_t addr, uint8_t value) case 0x4124: _fillModeTileIndex = value; break; case 0x4125: _fillModeAttrIndex = value & 0x03; break; - case 0x412E: _windowBank = value; break; + case 0x412E: _windowBank = value & 0x03; break; case 0x412F: - _windowControl.BgExtMode = value & 0x01; - _windowControl.AttrExtMode = value & 0x02; + _windowControl.AttrExtMode = value & 0x01; + _windowControl.BgExtMode = value & 0x02; _windowControl.FpgaRamSrc = (value & 0x0C) >> 2; _windowControl.FillMode = value & 0x20; - _windowControl.Source = (value & 0xC0) >> 6; //todo this is writeable/readable but ignored? + _windowControl.Source = 0b10; // Forced to FPGA-RAM break; case 0x4150: _slIrqScanline = value; break; - case 0x4151: _slIrqEnabled = true; break; + case 0x4151: + _slIrqEnabled = true; + UpdateIrqStatus(); + break; case 0x4152: _slIrqEnabled = false; @@ -707,11 +704,22 @@ void Rainbow::WriteRegister(uint16_t addr, uint8_t value) case 0x4172: _windowY1 = value; break; case 0x4173: _windowY2 = value; break; case 0x4174: _windowScrollX = value & 0x1F; break; - case 0x4175: _windowScrollY = value; break; + case 0x4175: + if(value < 240) _windowScrollY = value; + else _windowScrollY = 239; + break; case 0x4190: _espEnabled = value & 0x01; _wifiIrqEnabled = value & 0x02; + + if(!_espEnabled) { + _dataReceived = false; + _dataSent = true; + } + + UpdateIrqStatus(); + break; case 0x4191: _dataReceived = false; break; @@ -785,16 +793,16 @@ vector Rainbow::GetMapperStateEntries() case 2: source = "PRG-RAM"; break; case 3: source = "FPGA RAM"; break; } - entries.push_back(MapperStateEntry("$4116.6-7", "PRG-RAM Bank 0 Source", source)); + entries.push_back(MapperStateEntry("$4106.6-7", "PRG-RAM Bank 0 Source", source)); entries.push_back(MapperStateEntry("$4107/17", "PRG-RAM Bank 1", _lowBanks[1], MapperStateValueType::Number16)); - switch((_lowBanks[0] & 0xC000) >> 14) { + switch((_lowBanks[1] & 0xC000) >> 14) { case 0: case 1: source = "PRG-ROM"; break; case 2: source = "PRG-RAM"; break; case 3: source = "FPGA RAM"; break; } - entries.push_back(MapperStateEntry("$4117.6-7", "PRG-RAM Bank 1 Source", source)); + entries.push_back(MapperStateEntry("$4107.6-7", "PRG-RAM Bank 1 Source", source)); for(int i = 0; i < 8; i++) { entries.push_back(MapperStateEntry( @@ -804,7 +812,7 @@ vector Rainbow::GetMapperStateEntries() MapperStateValueType::Number16)); entries.push_back(MapperStateEntry( - HexUtilities::ToHex(0x4118 + i) + ".7", + HexUtilities::ToHex(0x4108 + i) + ".7", "ROM Bank " + std::to_string(i) + " Source", (string)((_highBanks[i] & 0x8000) ? "RAM" : "ROM"))); } @@ -901,7 +909,7 @@ vector Rainbow::GetMapperStateEntries() entries.push_back(MapperStateEntry("$4172", "Start Scanline", _windowY1, MapperStateValueType::Number8)); entries.push_back(MapperStateEntry("$4173", "End Scanline", _windowY2, MapperStateValueType::Number8)); entries.push_back(MapperStateEntry("$4174.0-4", "Scroll X", _windowScrollX, MapperStateValueType::Number8)); - entries.push_back(MapperStateEntry("$4175", "Scroll Y", _windowScrollX, MapperStateValueType::Number8)); + entries.push_back(MapperStateEntry("$4175", "Scroll Y", _windowScrollY, MapperStateValueType::Number8)); entries.push_back(MapperStateEntry("", "Wi-Fi")); entries.push_back(MapperStateEntry("$4190.0", "ESP Enabled", _espEnabled)); @@ -950,7 +958,7 @@ uint8_t Rainbow::DebugReadChr(ExtModeConfig& cfg, uint32_t addr) default: case 0: return _chrRomSize ? _chrRom[addr & (_chrRomSize - 1)] : 0; case 1: return _chrRamSize ? _chrRam[addr & (_chrRamSize - 1)] : 0; - case 2: return cfg.ExtRam[addr & 0x1FFF]; + case 2: return cfg.ExtRam[addr & 0x0FFF]; case 3: return GetNametable((addr & 0x400) >> 10)[addr & 0x3FF]; } } diff --git a/Core/NES/Mappers/Homebrew/Rainbow.h b/Core/NES/Mappers/Homebrew/Rainbow.h index 952e5b7e16..e28073f29f 100644 --- a/Core/NES/Mappers/Homebrew/Rainbow.h +++ b/Core/NES/Mappers/Homebrew/Rainbow.h @@ -116,7 +116,7 @@ class Rainbow : public BaseMapper, public IExtModeMapperDebug bool _espEnabled = false; bool _wifiIrqEnabled = false; bool _wifiIrqPending = false; - bool _dataSent = false; + bool _dataSent = true; bool _dataReceived = false; bool _dataReady = false; uint8_t _sendSrcAddr = 0; diff --git a/Core/NES/Mappers/Homebrew/RainbowAudio.h b/Core/NES/Mappers/Homebrew/RainbowAudio.h index f8dd4bba6f..a0c64be71c 100644 --- a/Core/NES/Mappers/Homebrew/RainbowAudio.h +++ b/Core/NES/Mappers/Homebrew/RainbowAudio.h @@ -68,6 +68,13 @@ class RainbowAudio final : public ISerializable return _lastOutput; } + uint8_t GetLastOutput4011(uint8_t openBus) + { + return _outputTo4011 + ? static_cast(_lastOutput << 1) + : openBus; + } + void WriteRegister(uint16_t addr, uint8_t value) { addr &= 0x0F;