Skip to content

Commit

Permalink
GBC: Fixed issues with read/writing palette register
Browse files Browse the repository at this point in the history
Fixes "CRAM readable" and "CRAM access blocking" tests
  • Loading branch information
SourMesen committed May 13, 2023
1 parent d9ae147 commit ff032b3
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Core/Gameboy/Debugger/GbPpuTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,11 +266,11 @@ DebugPaletteInfo GbPpuTools::GetPaletteInfo(GetPaletteInfoOptions options)

for(int i = 0; i < 8 * 4; i++) {
info.RawPalette[i] = state.CgbBgPalettes[i];
info.RgbPalette[i] = SnesDefaultVideoFilter::ToArgb(state.CgbBgPalettes[i]);
info.RgbPalette[i] = SnesDefaultVideoFilter::ToArgb(state.CgbBgPalettes[i] & 0x7FFF);
}
for(int i = 0; i < 8 * 4; i++) {
info.RawPalette[i+32] = state.CgbObjPalettes[i];
info.RgbPalette[i+32] = SnesDefaultVideoFilter::ToArgb(state.CgbObjPalettes[i]);
info.RgbPalette[i+32] = SnesDefaultVideoFilter::ToArgb(state.CgbObjPalettes[i] & 0x7FFF);
}
} else {
info.RawFormat = RawPaletteFormat::Indexed;
Expand Down
18 changes: 13 additions & 5 deletions Core/Gameboy/GbPpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ void GbPpu::RunDrawCycle()
void GbPpu::WriteBgPixel(uint8_t colorIndex)
{
uint16_t outOffset = _state.Scanline * GbConstants::ScreenWidth + _drawnPixels;
_currentBuffer[outOffset] = _state.CgbBgPalettes[colorIndex];
_currentBuffer[outOffset] = _state.CgbBgPalettes[colorIndex] & 0x7FFF;
if(_gameboy->IsSgb()) {
_gameboy->GetSgb()->WriteLcdColor(_state.Scanline, (uint8_t)_drawnPixels, colorIndex & 0x03);
}
Expand All @@ -420,7 +420,7 @@ void GbPpu::WriteBgPixel(uint8_t colorIndex)
void GbPpu::WriteObjPixel(uint8_t colorIndex)
{
uint16_t outOffset = _state.Scanline * GbConstants::ScreenWidth + _drawnPixels;
_currentBuffer[outOffset] = _state.CgbObjPalettes[colorIndex];
_currentBuffer[outOffset] = _state.CgbObjPalettes[colorIndex] & 0x7FFF;
if(_gameboy->IsSgb()) {
_gameboy->GetSgb()->WriteLcdColor(_state.Scanline, (uint8_t)_drawnPixels, colorIndex & 0x03);
}
Expand Down Expand Up @@ -1026,9 +1026,9 @@ uint8_t GbPpu::ReadCgbRegister(uint16_t addr)
switch(addr) {
case 0xFF4F: return _state.CgbVramBank | 0xFE;
case 0xFF68: return _state.CgbBgPalPosition | (_state.CgbBgPalAutoInc ? 0x80 : 0) | 0x40;
case 0xFF69: return (_state.CgbBgPalettes[_state.CgbBgPalPosition >> 1] >> ((_state.CgbBgPalPosition & 0x01) ? 8 : 0) & 0xFF);
case 0xFF69: return ReadCgbPalette(_state.CgbBgPalPosition, _state.CgbBgPalettes);
case 0xFF6A: return _state.CgbObjPalPosition | (_state.CgbObjPalAutoInc ? 0x80 : 0) | 0x40;
case 0xFF6B: return (_state.CgbObjPalettes[_state.CgbObjPalPosition >> 1] >> ((_state.CgbObjPalPosition & 0x01) ? 8 : 0) & 0xFF);
case 0xFF6B: return ReadCgbPalette(_state.CgbObjPalPosition, _state.CgbObjPalettes);
}
LogDebug("[Debug] GBC - Missing read handler: $" + HexUtilities::ToHex(addr));
return 0xFF;
Expand Down Expand Up @@ -1073,11 +1073,19 @@ void GbPpu::WriteCgbRegister(uint16_t addr, uint8_t value)
}
}

uint8_t GbPpu::ReadCgbPalette(uint8_t& pos, uint16_t* pal)
{
if(_state.Mode <= PpuMode::OamEvaluation) {
return (pal[pos >> 1] >> ((pos & 0x01) ? 8 : 0)) & 0xFF;
}
return 0xFF;
}

void GbPpu::WriteCgbPalette(uint8_t& pos, uint16_t* pal, bool autoInc, uint8_t value)
{
if(_state.Mode <= PpuMode::OamEvaluation) {
if(pos & 0x01) {
pal[pos >> 1] = (pal[pos >> 1] & 0xFF) | ((value & 0x7F) << 8);
pal[pos >> 1] = (pal[pos >> 1] & 0xFF) | (value << 8);
} else {
pal[pos >> 1] = (pal[pos >> 1] & 0xFF00) | value;
}
Expand Down
1 change: 1 addition & 0 deletions Core/Gameboy/GbPpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class GbPpu : public ISerializable
void SendFrame();
void UpdatePalette();

uint8_t ReadCgbPalette(uint8_t& pos, uint16_t* pal);
void WriteCgbPalette(uint8_t& pos, uint16_t* pal, bool autoInc, uint8_t value);

public:
Expand Down
2 changes: 1 addition & 1 deletion UI/Debugger/Utilities/PaletteHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static DynamicTooltip GetPreviewPanel(UInt32[] rgbPalette, UInt32[] rawPa
entries.AddEntry("Value", "$" + rawPalette[index].ToString("X4"));
entries.AddEntry("R", "$" + (rawPalette[index] & 0x1F).ToString("X2"));
entries.AddEntry("G", "$" + ((rawPalette[index] >> 5) & 0x1F).ToString("X2"));
entries.AddEntry("B", "$" + (rawPalette[index] >> 10).ToString("X2"));
entries.AddEntry("B", "$" + ((rawPalette[index] >> 10) & 0x1F).ToString("X2"));
} else if(format == RawPaletteFormat.Rgb333) {
entries.AddEntry("Value", "$" + rawPalette[index].ToString("X3"));
entries.AddEntry("R", "$" + ((rawPalette[index] >> 3) & 0x07).ToString("X2"));
Expand Down

0 comments on commit ff032b3

Please sign in to comment.