Skip to content

Commit

Permalink
[ppu] oam: Respect per-scanline cycle budget (fixes #98)
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jun 1, 2020
1 parent 233a886 commit edacae8
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions source/emulator/core/hw/ppu/render/oam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Refer to the included LICENSE file.
*/

#include <common/log.hpp>

#include "../ppu.hpp"

namespace nba::core {
Expand Down Expand Up @@ -49,6 +51,9 @@ void PPU::RenderLayerOAM(bool bitmap_mode) {

line_contains_alpha_obj = false;

int cycles = 0;
int cycle_limit = mmio.dispcnt.hblank_oam_access ? 954 : 1210;

/* TODO: check if there is a faster way to initialize the array. */
for (int x = 0; x < 240; x++) {
buffer_obj[x].priority = 4;
Expand Down Expand Up @@ -102,6 +107,8 @@ void PPU::RenderLayerOAM(bool bitmap_mode) {
x += half_width;
y += half_height;

int cycles_per_pixel = 1;

/* Load transform matrix. */
if (affine) {
int group = ((attr1 >> 9) & 0x1F) << 5;
Expand All @@ -121,6 +128,8 @@ void PPU::RenderLayerOAM(bool bitmap_mode) {
half_width *= 2;
half_height *= 2;
}

cycles_per_pixel = 2;
} else {
/* Set transform to identity:
* [ 1 0 ]
Expand All @@ -139,6 +148,12 @@ void PPU::RenderLayerOAM(bool bitmap_mode) {
continue;
}

if (affine) {
cycles += 10 + 2 * rect_width;
} else {
cycles += width;
}

int local_y = line - y;
int number = attr2 & 0x3FF;
int palette = (attr2 >> 12) + 16;
Expand All @@ -162,11 +177,21 @@ void PPU::RenderLayerOAM(bool bitmap_mode) {
local_y -= mmio.mosaic.obj._counter_y;
}

if (affine) {
cycles += 10;
}

/* Render OBJ scanline. */
for (int local_x = -half_width; local_x <= half_width; local_x++) {
int _local_x = local_x - mosaic_x;
int global_x = local_x + x;

if (cycles > cycle_limit) {
return;
}

cycles += cycles_per_pixel;

if (mosaic && (++mosaic_x == mmio.mosaic.obj.size_x)) {
mosaic_x = 0;
}
Expand Down

0 comments on commit edacae8

Please sign in to comment.