55namespace Gb \Ppu ;
66
77use Gb \Bus \DeviceInterface ;
8- use Gb \Dma \HdmaController ;
98use Gb \Interrupts \InterruptController ;
109use Gb \Interrupts \InterruptType ;
1110use Gb \Memory \Vram ;
@@ -98,19 +97,12 @@ final class Ppu implements DeviceInterface
9897 /** @var array<int, int> */
9998 private array $ bgColorBuffer = [];
10099
101- // Background priority buffer for CGB priority rules (stores BG-to-OAM priority bit)
102- /** @var array<int, bool> */
103- private array $ bgPriorityBuffer = [];
104-
105100 /** @var ColorPalette Color palette system (CGB) */
106101 private readonly ColorPalette $ colorPalette ;
107102
108103 /** @var bool CGB mode enabled */
109104 private bool $ cgbMode = false ;
110105
111- /** @var HdmaController|null HDMA controller for H-Blank DMA transfers (CGB) */
112- private ?HdmaController $ hdmaController = null ;
113-
114106 public function __construct (
115107 private readonly Vram $ vram ,
116108 private readonly Oam $ oam ,
@@ -217,11 +209,6 @@ private function setMode(PpuMode $mode): void
217209 // Update STAT register mode bits
218210 $ this ->stat = ($ this ->stat & ~self ::STAT_MODE_MASK ) | $ mode ->getStatBits ();
219211
220- // Trigger HDMA H-Blank transfer if entering H-Blank mode
221- if ($ mode === PpuMode::HBlank && $ this ->hdmaController !== null ) {
222- $ this ->hdmaController ->onHBlank ();
223- }
224-
225212 // Trigger STAT interrupt if enabled for this mode
226213 $ statInterrupt = match ($ mode ) {
227214 PpuMode::HBlank => ($ this ->stat & self ::STAT_MODE0_INT ) !== 0 ,
@@ -254,7 +241,6 @@ private function renderScanline(): void
254241 // Initialize scanline buffer and BG color buffer
255242 $ this ->scanlineBuffer = array_fill (0 , ArrayFramebuffer::WIDTH , Color::fromDmgShade (0 ));
256243 $ this ->bgColorBuffer = array_fill (0 , ArrayFramebuffer::WIDTH , 0 );
257- $ this ->bgPriorityBuffer = array_fill (0 , ArrayFramebuffer::WIDTH , false );
258244
259245 // Render layers
260246 if (($ this ->lcdc & self ::LCDC_BG_WINDOW_ENABLE ) !== 0 ) {
@@ -301,7 +287,6 @@ private function renderBackground(): void
301287 $ vramBank = ($ attributes & 0x08 ) !== 0 ? 1 : 0 ; // Bit 3: VRAM bank
302288 $ xFlip = ($ attributes & 0x20 ) !== 0 ; // Bit 5: horizontal flip
303289 $ yFlip = ($ attributes & 0x40 ) !== 0 ; // Bit 6: vertical flip
304- $ bgPriority = ($ attributes & 0x80 ) !== 0 ; // Bit 7: BG-to-OAM priority
305290
306291 // Apply flips
307292 $ finalTileY = $ yFlip ? (7 - $ tileY ) : $ tileY ;
@@ -314,9 +299,8 @@ private function renderBackground(): void
314299 // Get pixel color
315300 $ color = $ this ->getTilePixel ($ vramData , $ tileDataAddr , $ finalTileX , $ finalTileY );
316301
317- // Store raw color and priority for sprite priority checking
302+ // Store raw color for sprite priority checking
318303 $ this ->bgColorBuffer [$ x ] = $ color ;
319- $ this ->bgPriorityBuffer [$ x ] = $ bgPriority ;
320304
321305 // Apply palette
322306 if ($ this ->cgbMode ) {
@@ -362,7 +346,6 @@ private function renderWindow(): void
362346 $ vramBank = ($ attributes & 0x08 ) !== 0 ? 1 : 0 ; // Bit 3: VRAM bank
363347 $ xFlip = ($ attributes & 0x20 ) !== 0 ; // Bit 5: horizontal flip
364348 $ yFlip = ($ attributes & 0x40 ) !== 0 ; // Bit 6: vertical flip
365- $ bgPriority = ($ attributes & 0x80 ) !== 0 ; // Bit 7: BG-to-OAM priority
366349
367350 // Apply flips
368351 $ finalTileY = $ yFlip ? (7 - $ tileY ) : $ tileY ;
@@ -374,9 +357,8 @@ private function renderWindow(): void
374357
375358 $ color = $ this ->getTilePixel ($ vramData , $ tileDataAddr , $ finalTileX , $ finalTileY );
376359
377- // Store raw color and priority for sprite priority checking
360+ // Store raw color for sprite priority checking
378361 $ this ->bgColorBuffer [$ x ] = $ color ;
379- $ this ->bgPriorityBuffer [$ x ] = $ bgPriority ;
380362
381363 // Apply palette
382364 if ($ this ->cgbMode ) {
@@ -484,17 +466,9 @@ private function renderSprite(array $sprite, int $spriteHeight, array $vramData)
484466 }
485467
486468 // Check priority (behind BG)
487- $ bgColor = $ this ->bgColorBuffer [$ pixelX ];
488- if ($ bgColor !== 0 ) {
489- // In CGB mode, check BG priority bit first
490- if ($ this ->cgbMode && $ this ->bgPriorityBuffer [$ pixelX ]) {
491- // BG priority bit is set - BG always wins
492- continue ;
493- }
494- // Otherwise, use normal sprite priority (behindBg flag)
495- if ($ behindBg ) {
496- continue ;
497- }
469+ if ($ behindBg && $ this ->bgColorBuffer [$ pixelX ] !== 0 ) {
470+ // If BG pixel is not color 0, sprite is hidden behind BG
471+ continue ;
498472 }
499473
500474 if ($ this ->cgbMode ) {
@@ -562,14 +536,6 @@ public function isCgbMode(): bool
562536 return $ this ->cgbMode ;
563537 }
564538
565- /**
566- * Set the HDMA controller for H-Blank DMA transfers.
567- */
568- public function setHdmaController (HdmaController $ hdmaController ): void
569- {
570- $ this ->hdmaController = $ hdmaController ;
571- }
572-
573539 // DeviceInterface implementation for I/O registers
574540 public function readByte (int $ address ): int
575541 {
@@ -585,11 +551,10 @@ public function readByte(int $address): int
585551 self ::OBP1 => $ this ->obp1 ,
586552 self ::WY => $ this ->wy ,
587553 self ::WX => $ this ->wx ,
588- // CGB color palette registers - only accessible in CGB mode
589- self ::BCPS => $ this ->cgbMode ? $ this ->colorPalette ->readBgIndex () : 0xFF ,
590- self ::BCPD => $ this ->cgbMode ? $ this ->colorPalette ->readBgData () : 0xFF ,
591- self ::OCPS => $ this ->cgbMode ? $ this ->colorPalette ->readObjIndex () : 0xFF ,
592- self ::OCPD => $ this ->cgbMode ? $ this ->colorPalette ->readObjData () : 0xFF ,
554+ self ::BCPS => $ this ->colorPalette ->readBgIndex (),
555+ self ::BCPD => $ this ->colorPalette ->readBgData (),
556+ self ::OCPS => $ this ->colorPalette ->readObjIndex (),
557+ self ::OCPD => $ this ->colorPalette ->readObjData (),
593558 default => 0xFF ,
594559 };
595560 }
@@ -608,11 +573,10 @@ public function writeByte(int $address, int $value): void
608573 self ::OBP1 => $ this ->obp1 = $ value ,
609574 self ::WY => $ this ->wy = $ value ,
610575 self ::WX => $ this ->wx = $ value ,
611- // CGB color palette registers - only writable in CGB mode
612- self ::BCPS => $ this ->cgbMode ? $ this ->colorPalette ->writeBgIndex ($ value ) : null ,
613- self ::BCPD => $ this ->cgbMode ? $ this ->colorPalette ->writeBgData ($ value ) : null ,
614- self ::OCPS => $ this ->cgbMode ? $ this ->colorPalette ->writeObjIndex ($ value ) : null ,
615- self ::OCPD => $ this ->cgbMode ? $ this ->colorPalette ->writeObjData ($ value ) : null ,
576+ self ::BCPS => $ this ->colorPalette ->writeBgIndex ($ value ),
577+ self ::BCPD => $ this ->colorPalette ->writeBgData ($ value ),
578+ self ::OCPS => $ this ->colorPalette ->writeObjIndex ($ value ),
579+ self ::OCPD => $ this ->colorPalette ->writeObjData ($ value ),
616580 default => null ,
617581 };
618582
0 commit comments