320. Find sprite zero hit position

Detect the first sprite 0/background opaque-pixel overlap.

Lesson 320 of 356 · tests/chapter_11_sprite_zero_hit/test_320_find_sprite_zero_hit_position.py

File to create

emulator/rendering/sprite_zero_hit.py

Why this step exists

Step 319 established when sprite 0 hit is cleared. Before setting PPUSTATUS bit 6, we need a pure helper that answers:

Does a non-transparent sprite 0 pixel overlap a non-transparent background pixel?
If so, where is the first overlap?

Returning a position instead of only True/False gives the next timing step enough information to decide when the PPU should set sprite 0 hit.

Definitions

Sprite pixel is opaque:
    its decoded CHR color index is 1, 2, or 3

Background pixel is opaque:
    background_opaque_mask[y * screen_width + x] is True

Sprite 0 overlap:
    both conditions are true at the same visible screen coordinate

Suggested implementation example

from emulator.ppu.chr_decoder import decode_chr_tile
from emulator.rendering.framebuffer import NES_SCREEN_HEIGHT, NES_SCREEN_WIDTH
from emulator.rendering.nametable_renderer import BackgroundOpaqueMask
from emulator.rendering.sprite_renderer import (
    SpriteEntry,
    decode_sprite_attributes,
)


SpriteZeroHitPosition = tuple[int, int]


def find_sprite_zero_hit_position(
    sprite_zero: SpriteEntry,
    pattern_table: bytes,
    background_opaque_mask: BackgroundOpaqueMask,
    screen_width: int = NES_SCREEN_WIDTH,
    screen_height: int = NES_SCREEN_HEIGHT,
) -> SpriteZeroHitPosition | None:
    if len(background_opaque_mask) != screen_width * screen_height:
        raise ValueError(
            "Background opaque mask size must be equal to screen width * height"
        )

    tile_start = sprite_zero.tile_index * 16
    tile_end = tile_start + 16

    if tile_end > len(pattern_table):
        raise ValueError("Pattern table does not contain sprite 0 tile bytes")

    attributes = decode_sprite_attributes(sprite_zero.attributes)
    color_indexes = decode_chr_tile(pattern_table[tile_start:tile_end])

    for tile_y in range(8):
        for tile_x in range(8):
            source_x = 7 - tile_x if attributes.flip_horizontal else tile_x
            source_y = 7 - tile_y if attributes.flip_vertical else tile_y

            sprite_color_index = color_indexes[source_y][source_x]

            if sprite_color_index == 0:
                continue

            screen_x = sprite_zero.x + tile_x
            screen_y = sprite_zero.y + tile_y

            if not (0 <= screen_x < screen_width):
                continue
            if not (0 <= screen_y < screen_height):
                continue

            mask_index = screen_y * screen_width + screen_x

            if background_opaque_mask[mask_index]:
                return screen_x, screen_y

    return None

Important coordinate simplification

Real NES OAM stores the sprite top Y coordinate minus one, so the first sprite row normally appears at OAM Y + 1. The existing tutorial sprite renderer currently uses OAM Y directly. This helper intentionally matches that existing renderer so visible sprite pixels and overlap coordinates remain consistent. A later focused accuracy step should update both together.

Important rules

  • sprite color index 0 never contributes to a hit
  • background mask False never contributes to a hit
  • sprite priority bit 5 does not prevent sprite 0 hit detection
  • clipping must happen before indexing the background mask
  • this helper must not mutate PPUSTATUS

Out of scope

  • setting or clearing PPUSTATUS
  • scheduling the hit by scanline/cycle
  • exact OAM Y + 1 behavior
  • x=255 hardware exception
  • PPUMASK rendering-enable rules
  • 8x16 sprites
  • Super Mario Bros. validation

Run this lesson

uv run pytest tests/chapter_11_sprite_zero_hit/test_320_find_sprite_zero_hit_position.py -v