From 17bb339fec8a0bc8c538eb28fed8e190f5344183 Mon Sep 17 00:00:00 2001 From: Sour Date: Sun, 17 Feb 2019 23:53:19 -0500 Subject: [PATCH] PPU: Very incomplete color math support --- Core/Ppu.cpp | 26 +++++++++++++++++++++++++- Core/Ppu.h | 8 ++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Core/Ppu.cpp b/Core/Ppu.cpp index 72ecd76..72aadbf 100644 --- a/Core/Ppu.cpp +++ b/Core/Ppu.cpp @@ -102,6 +102,8 @@ void Ppu::RenderTilemap(uint8_t layerIndex, uint8_t bpp) return; } + bool useColorMath = ((_colorMathEnabled >> layerIndex) & 0x01) != 0; + LayerConfig &config = _layerConfig[layerIndex]; uint16_t tilemapAddr = config.TilemapAddress; @@ -126,7 +128,15 @@ void Ppu::RenderTilemap(uint8_t layerIndex, uint8_t bpp) if(color > 0) { uint16_t paletteRamOffset = (palette * (1 << bpp) + color) * 2; uint16_t paletteColor = _cgram[paletteRamOffset] | (_cgram[paletteRamOffset + 1] << 8); - _currentBuffer[(y * 8 + i) * 256 + x * 8 + j] = paletteColor; + + uint16_t ¤tPixel = _currentBuffer[(y * 8 + i) * 256 + x * 8 + j]; + if(useColorMath) { + uint16_t r = std::min(((currentPixel & 0x001F) + (paletteColor & 0x001F)), 0x1F); + uint16_t g = std::min(((currentPixel & 0x03E0) + (paletteColor & 0x03E0)), 0x3E0); + uint16_t b = std::min(((currentPixel & 0x7C00) + (paletteColor & 0x7C00)), 0x7C00); + paletteColor = r | g | b; + } + currentPixel = paletteColor; } } } @@ -385,6 +395,20 @@ void Ppu::Write(uint32_t addr, uint8_t value) //TM - Main Screen Designation _mainScreenLayers = value & 0x1F; break; + + case 0x2130: + //CGWSEL - Color Addition Select + _colorMathClipMode = (value >> 6) & 0x03; + _colorMathPreventMode = (value >> 4) & 0x03; + _colorMathAddSubscreen = (value & 0x02) != 0; + _colorMathDirectColorMode = (value & 0x01) != 0; + break; + + case 0x2131: + _colorMathEnabled = value & 0x3F; + _colorMathSubstractMode = (value & 0x80) != 0; + _colorMathHalveResult = (value & 0x80) != 0; + break; default: MessageManager::DisplayMessage("Debug", "Unimplemented register write: " + HexUtilities::ToHex(addr)); diff --git a/Core/Ppu.h b/Core/Ppu.h index e491cff..547b717 100644 --- a/Core/Ppu.h +++ b/Core/Ppu.h @@ -59,6 +59,14 @@ private: uint16_t _internalOamAddress = 0; uint8_t _oamWriteBuffer = 0; + uint8_t _colorMathClipMode = 0; + uint8_t _colorMathPreventMode = 0; + bool _colorMathAddSubscreen = false; + bool _colorMathDirectColorMode = false; + uint8_t _colorMathEnabled = 0; + bool _colorMathSubstractMode = false; + bool _colorMathHalveResult = false; + void RenderTilemap(uint8_t layerIndex, uint8_t bpp); void DrawSprites();