Class: Amaterasu::GameBoy::Ppu::Modes::Rendering::PixelFifo

Inherits:
Object
  • Object
show all
Defined in:
lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb

Overview

Models the PPU Pixel FIFO from the original Game Boy (DMG).

Constant Summary collapse

MAX_PIXELS =

or 8?

16

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePixelFifo

Returns a new instance of PixelFifo.



15
16
17
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 15

def initialize
  @pixel_buffer = Array.new
end

Instance Attribute Details

#pixelsObject (readonly)

Array of raw color indices (0 - 3).



13
14
15
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 13

def pixels
  @pixels
end

Instance Method Details

#clearObject

Removes all current elements of the FIFO.



57
58
59
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 57

def clear
  @pixel_buffer.clear
end

#empty?Boolean

Removes all current elements of the FIFO.

Returns:

  • (Boolean)


62
63
64
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 62

def empty?
  @pixel_buffer.empty?
end

#merge(sprite_pixels) ⇒ Object

This is specific for Sprites.



36
37
38
39
40
41
42
43
44
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 36

def merge(sprite_pixels)
  idx = 0

  while idx <= 7
    transparent = (@pixel_buffer[idx] & 0b11) == 0b00
    @pixel_buffer[idx] = sprite_pixels[idx] if transparent || @pixel_buffer[idx].nil?
    idx += 1
  end
end

#pop_pixelInteger

To follow the FIFO rules, elements need to be popped from left to right, that is why we need to use Array#shift.

Returns:

  • (Integer)

    2 bit number representing the color id of the pixel.



50
51
52
53
54
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 50

def pop_pixel
  return if @pixel_buffer.empty?

  @pixel_buffer.shift
end

#push?(pushed_pixels) ⇒ Boolean

The Pixel Fetcher is the pixel producer, it produces 8 color indices (1 for each pixel) and attempts to push all 8 at the same time until it succeeds.

It will only succeed if the FIFO is empty.

Returns:

  • (Boolean)

    If the push was successful or not.



26
27
28
29
30
31
32
# File 'lib/amaterasu/game_boy/ppu/modes/rendering/pixel_fifo.rb', line 26

def push?(pushed_pixels)
  return false unless @pixel_buffer.empty?

  pushed_pixels.each { |pp| @pixel_buffer << pp }

  true
end