Class: Rubyboy::Ppu

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyboy/ppu.rb

Constant Summary collapse

MODE =
{
  hblank: 0,
  vblank: 1,
  oam_scan: 2,
  drawing: 3
}.freeze
LCDC =
{
  bg_window_enable: 0,
  sprite_enable: 1,
  sprite_size: 2,
  bg_tile_map_area: 3,
  bg_window_tile_data_area: 4,
  window_enable: 5,
  window_tile_map_area: 6,
  lcd_ppu_enable: 7
}.freeze
STAT =
{
  ly_eq_lyc: 2,
  hblank: 3,
  vblank: 4,
  oam_scan: 5,
  lyc: 6
}.freeze
SPRITE_FLAGS =
{
  bank: 3,
  dmg_palette: 4,
  x_flip: 5,
  y_flip: 6,
  priority: 7
}.freeze
LCD_WIDTH =
160
LCD_HEIGHT =
144
OAM_SCAN_CYCLES =
80
DRAWING_CYCLES =
172
HBLANK_CYCLES =
204
ONE_LINE_CYCLES =
OAM_SCAN_CYCLES + DRAWING_CYCLES + HBLANK_CYCLES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(interrupt) ⇒ Ppu

Returns a new instance of Ppu.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rubyboy/ppu.rb', line 49

def initialize(interrupt)
  @mode = MODE[:oam_scan]
  @lcdc = 0x91
  @stat = 0x00
  @scy = 0x00
  @scx = 0x00
  @ly = 0x00
  @lyc = 0x00
  @obp0 = 0x00
  @obp1 = 0x00
  @wy = 0x00
  @wx = 0x00
  @bgp = 0x00
  @vram = Array.new(0x2000, 0x00)
  @oam = Array.new(0xa0, 0x00)
  @wly = 0x00
  @cycles = 0
  @interrupt = interrupt
  @buffer = Array.new(144 * 160, 0xffffffff)
  @bg_pixels = Array.new(LCD_WIDTH, 0x00)
  @tile_cache = Array.new(384) { Array.new(64, 0) }
  @tile_map_cache = Array.new(2048, 0)
  @bgp_cache = Array.new(4, 0xffffffff)
  @obp0_cache = Array.new(4, 0xffffffff)
  @obp1_cache = Array.new(4, 0xffffffff)
  @sprite_cache = Array.new(40) { { y: 0xff, x: 0xff, tile_index: 0, flags: 0 } }
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



5
6
7
# File 'lib/rubyboy/ppu.rb', line 5

def buffer
  @buffer
end

Instance Method Details

#read_byte(addr) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rubyboy/ppu.rb', line 77

def read_byte(addr)
  case addr
  when 0x8000..0x9fff
    @mode == MODE[:drawing] ? 0xff : @vram[addr - 0x8000]
  when 0xfe00..0xfe9f
    @mode == MODE[:oam_scan] || @mode == MODE[:drawing] ? 0xff : @oam[addr - 0xfe00]
  when 0xff40
    @lcdc
  when 0xff41
    @stat | 0x80 | @mode
  when 0xff42
    @scy
  when 0xff43
    @scx
  when 0xff44
    @ly
  when 0xff45
    @lyc
  when 0xff47
    @bgp
  when 0xff48
    @obp0
  when 0xff49
    @obp1
  when 0xff4a
    @wy
  when 0xff4b
    @wx
  end
end

#render_bgObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
# File 'lib/rubyboy/ppu.rb', line 219

def render_bg
  return if @lcdc[LCDC[:bg_window_enable]] == 0

  y = (@ly + @scy) & 0xff
  tile_map_addr = (y >> 3) << 5
  tile_map_addr += 1024 if @lcdc[LCDC[:bg_tile_map_area]] == 1
  tile_y = (y & 7) << 3
  buffer_start_index = @ly * LCD_WIDTH

  scx = @scx
  buffer = @buffer
  bg_pixels = @bg_pixels
  tile_cache = @tile_cache
  tile_map_cache = @tile_map_cache
  bgp_cache = @bgp_cache

  i = 0
  current_tile = scx >> 3
  x_offset = scx & 7

  if x_offset > 0
    tile = tile_cache[tile_map_cache[tile_map_addr + current_tile]]
    while (x_offset + i) < 8
      pixel = tile[tile_y + x_offset + i]
      buffer[buffer_start_index + i] = bgp_cache[pixel]
      bg_pixels[i] = pixel
      i += 1
    end
    current_tile += 1
  end

  while i < LCD_WIDTH - 7
    tile = tile_cache[tile_map_cache[tile_map_addr + (current_tile & 0x1f)]]
    idx = buffer_start_index + i

    # Unroll the 8-pixel loop
    pixel = tile[tile_y]
    buffer[idx] = bgp_cache[pixel]
    bg_pixels[i] = pixel

    pixel = tile[tile_y + 1]
    buffer[idx + 1] = bgp_cache[pixel]
    bg_pixels[i + 1] = pixel

    pixel = tile[tile_y + 2]
    buffer[idx + 2] = bgp_cache[pixel]
    bg_pixels[i + 2] = pixel

    pixel = tile[tile_y + 3]
    buffer[idx + 3] = bgp_cache[pixel]
    bg_pixels[i + 3] = pixel

    pixel = tile[tile_y + 4]
    buffer[idx + 4] = bgp_cache[pixel]
    bg_pixels[i + 4] = pixel

    pixel = tile[tile_y + 5]
    buffer[idx + 5] = bgp_cache[pixel]
    bg_pixels[i + 5] = pixel

    pixel = tile[tile_y + 6]
    buffer[idx + 6] = bgp_cache[pixel]
    bg_pixels[i + 6] = pixel

    pixel = tile[tile_y + 7]
    buffer[idx + 7] = bgp_cache[pixel]
    bg_pixels[i + 7] = pixel

    i += 8
    current_tile += 1
  end

  return unless i < LCD_WIDTH

  tile = tile_cache[tile_map_cache[tile_map_addr + (current_tile & 0x1f)]]
  x = 0
  while i < LCD_WIDTH
    pixel = tile[tile_y + x]
    buffer[buffer_start_index + i] = bgp_cache[pixel]
    bg_pixels[i] = pixel
    x += 1
    i += 1
  end
end

#render_spritesObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/rubyboy/ppu.rb', line 326

def render_sprites
  return if @lcdc[LCDC[:sprite_enable]] == 0

  sprite_height = @lcdc[LCDC[:sprite_size]] == 0 ? 8 : 16
  sprites = []
  cnt = 0

  @sprite_cache.each do |sprite|
    next if sprite[:y] > @ly || sprite[:y] + sprite_height <= @ly

    sprites << sprite
    cnt += 1
    break if cnt == 10
  end
  sprites.reverse!
  sprites.sort! { |a, b| b[:x] <=> a[:x] }

  sprites.each do |sprite|
    flags = sprite[:flags]
    pallet = flags[SPRITE_FLAGS[:dmg_palette]] == 0 ? @obp0_cache : @obp1_cache
    tile_index = sprite[:tile_index]
    tile_index &= 0xfe if sprite_height == 16
    y = (@ly - sprite[:y]) & 0xff
    y = sprite_height - y - 1 if flags[SPRITE_FLAGS[:y_flip]] == 1
    tile_index = (tile_index + 1) & 0xff if y >= 8
    tile_y = (y & 7) << 3
    buffer_start_index = @ly * LCD_WIDTH

    8.times do |x|
      x_flipped = flags[SPRITE_FLAGS[:x_flip]] == 1 ? 7 - x : x

      pixel = @tile_cache[tile_index][tile_y + x_flipped]
      i = (sprite[:x] + x) & 0xff

      next if pixel == 0 || i >= LCD_WIDTH
      next if flags[SPRITE_FLAGS[:priority]] == 1 && @bg_pixels[i] != 0

      @buffer[buffer_start_index + i] = pallet[pixel]
    end
  end
end

#render_windowObject



304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'lib/rubyboy/ppu.rb', line 304

def render_window
  return if @lcdc[LCDC[:bg_window_enable]] == 0 || @lcdc[LCDC[:window_enable]] == 0 || @ly < @wy

  rendered = false
  y = @wly
  tile_map_addr = (y >> 3) << 5
  tile_map_addr += 1024 if @lcdc[LCDC[:window_tile_map_area]] == 1
  tile_y = (y & 7) << 3
  buffer_start_index = @ly * LCD_WIDTH
  LCD_WIDTH.times do |i|
    next if i < @wx - 7

    rendered = true
    x = i - (@wx - 7)
    tile_index = @tile_map_cache[tile_map_addr + (x >> 3)]
    pixel = @tile_cache[tile_index][tile_y + (x & 7)]
    @buffer[buffer_start_index + i] = @bgp_cache[pixel]
    @bg_pixels[i] = pixel
  end
  @wly += 1 if rendered
end

#step(cycles) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/rubyboy/ppu.rb', line 163

def step(cycles)
  return false if @lcdc[LCDC[:lcd_ppu_enable]] == 0

  res = false
  @cycles += cycles

  case @mode
  when MODE[:oam_scan]
    if @cycles >= OAM_SCAN_CYCLES
      @cycles -= OAM_SCAN_CYCLES
      @mode = MODE[:drawing]
    end
  when MODE[:drawing]
    if @cycles >= DRAWING_CYCLES
      render_bg
      render_window
      render_sprites
      @cycles -= DRAWING_CYCLES
      @mode = MODE[:hblank]
      @interrupt.request(:lcd) if @stat[STAT[:hblank]] == 1
    end
  when MODE[:hblank]
    if @cycles >= HBLANK_CYCLES
      @cycles -= HBLANK_CYCLES
      @ly += 1
      handle_ly_eq_lyc

      if @ly == LCD_HEIGHT
        @mode = MODE[:vblank]
        @interrupt.request(:vblank)
        @interrupt.request(:lcd) if @stat[STAT[:vblank]] == 1
      else
        @mode = MODE[:oam_scan]
        @interrupt.request(:lcd) if @stat[STAT[:oam_scan]] == 1
      end
    end
  when MODE[:vblank]
    if @cycles >= ONE_LINE_CYCLES
      @cycles -= ONE_LINE_CYCLES
      @ly += 1
      handle_ly_eq_lyc

      if @ly == 154
        @ly = 0
        @wly = 0
        handle_ly_eq_lyc
        @mode = MODE[:oam_scan]
        @interrupt.request(:lcd) if @stat[STAT[:oam_scan]] == 1
        res = true
      end
    end
  end

  res
end

#write_byte(addr, value) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/rubyboy/ppu.rb', line 108

def write_byte(addr, value)
  case addr
  when 0x8000..0x9fff
    if @mode != MODE[:drawing]
      @vram[addr - 0x8000] = value
      if addr < 0x9800
        update_tile_cache(addr - 0x8000)
      else
        update_tile_map_cache(addr - 0x8000)
      end
    end
  when 0xfe00..0xfe9f
    if @mode != MODE[:oam_scan] && @mode != MODE[:drawing]
      @oam[addr - 0xfe00] = value
      sprite_index = (addr - 0xfe00) >> 2
      attribute = (addr - 0xfe00) & 3

      case attribute
      when 0 then @sprite_cache[sprite_index][:y] = (value - 16) & 0xff
      when 1 then @sprite_cache[sprite_index][:x] = (value - 8) & 0xff
      when 2 then @sprite_cache[sprite_index][:tile_index] = value
      when 3 then @sprite_cache[sprite_index][:flags] = value
      end
    end
  when 0xff40
    old_lcdc = @lcdc
    @lcdc = value

    refresh_tile_map_cache if old_lcdc[LCDC[:bg_window_tile_data_area]] != value[LCDC[:bg_window_tile_data_area]]
  when 0xff41
    @stat = value & 0x78
  when 0xff42
    @scy = value
  when 0xff43
    @scx = value
  when 0xff44
    # ly is read only
  when 0xff45
    @lyc = value
  when 0xff47
    @bgp = value
    refresh_palette_cache(@bgp_cache, value)
  when 0xff48
    @obp0 = value
    refresh_palette_cache(@obp0_cache, value)
  when 0xff49
    @obp1 = value
    refresh_palette_cache(@obp1_cache, value)
  when 0xff4a
    @wy = value
  when 0xff4b
    @wx = value
  end
end