Skip to content

video: mask palette indices to the palette's size (fixes dark SMS sprites) - #31

Merged
finger563 merged 1 commit into
mainfrom
fix/palette-index-mask
Nov 21, 2026
Merged

finger563 merged 1 commit into
mainfrom
fix/palette-index-mask

Conversation

@finger563

Copy link
Copy Markdown
Contributor

Summary

Fixes near-black sprites in Master System and Game Gear games. In Sonic the Hedgehog, Sonic was nearly black and the rings dark grey.

  • Tab5Emu::convert_tile() used each 8-bit pixel as a palette index without masking it.
  • smsplus keeps flags in the upper bits of its pixels (0x40 marks a sprite pixel, 0x20 a high-priority one) and hands over a 32-color palette. So every sprite pixel (0x50-0x5F) read 64 entries past the end of the port's palette array.
  • The colors came from whatever the linker put after that array: other variables, then the core's own palette in the other byte order. That changes with the build's layout and with those variables' contents, which is why the bug came and went between builds.
  • The box masks with palette_size - 1 in its video task; the Tab5 now does the same in every palette lookup (12). With 256-color palettes (Jedi Knight, Dark Forces, NES) the mask is 0xFF, so nothing changes for them.
  • PICO-8 also passes a 32-color palette and is covered too.

Testing

  • On the Tab5: Sonic the Hedgehog (SMS) shows correct sprite colors, confirmed on screen.
  • emulink screenshots can't show this bug: the SMS port masks when it converts its frame for them.

🤖 Generated with Claude Code

https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY

convert_tile() looked 8-bit pixels up in the core's palette unmasked.
smsplus keeps flags in the upper bits of its pixels (0x40 marks a sprite
pixel, 0x20 a high-priority one) and hands over a 32-color palette, so
every sprite pixel (0x50-0x5F) read 64 entries past its end: the colors
came from whatever the linker put after the port's palette array (seen as
a near-black Sonic and dark rings in Sonic the Hedgehog; it depends on
the build's layout and on those variables' contents). The box masks with
palette_size - 1 in its video task; the Tab5 now does the same in every
lookup (a no-op for 256-color palettes).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01SFCMzomCZ5wRHJN4sxwkvY
@finger563
finger563 merged commit 8989b1f into main Nov 21, 2026
1 check passed
@finger563
finger563 deleted the fix/palette-index-mask branch November 21, 2026 04:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant