feat: minor performance improvements

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-03-18 09:41:06 -03:00
parent 3d61c0dba4
commit 1901a471e4
4 changed files with 39 additions and 41 deletions

View File

@ -107,8 +107,15 @@ fn DmaController(comptime id: u2) type {
self.writeCntHigh(@truncate(u16, word >> 16));
}
pub fn step(self: *Self, bus: *Bus) bool {
if (!self.enabled or !self.cnt.enabled.read()) return false;
pub inline fn check(self: *Self, bus: *Bus) bool {
if (!self.enabled) return false; // FIXME: Check CNT register?
self.step(bus);
return true;
}
pub fn step(self: *Self, bus: *Bus) void {
@setCold(true);
const sad_adj = std.meta.intToEnum(Adjustment, self.cnt.sad_adj.read()) catch unreachable;
const dad_adj = std.meta.intToEnum(Adjustment, self.cnt.dad_adj.read()) catch unreachable;
@ -160,8 +167,6 @@ fn DmaController(comptime id: u2) type {
// timing window
self.enabled = false;
}
return true;
}
pub fn isBlocking(self: *const Self) bool {

View File

@ -296,24 +296,22 @@ pub const Arm7tdmi = struct {
}
fn handleDMATransfers(self: *Self) bool {
if (self.bus.dma._0.step(self.bus)) return self.bus.dma._0.isBlocking();
if (self.bus.dma._1.step(self.bus)) return self.bus.dma._1.isBlocking();
if (self.bus.dma._2.step(self.bus)) return self.bus.dma._2.isBlocking();
if (self.bus.dma._3.step(self.bus)) return self.bus.dma._3.isBlocking();
if (self.bus.dma._0.check(self.bus)) return self.bus.dma._0.isBlocking();
if (self.bus.dma._1.check(self.bus)) return self.bus.dma._1.isBlocking();
if (self.bus.dma._2.check(self.bus)) return self.bus.dma._2.isBlocking();
if (self.bus.dma._3.check(self.bus)) return self.bus.dma._3.isBlocking();
return false;
}
fn thumbFetch(self: *Self) u16 {
const halfword = self.bus.read16(self.r[15]);
self.r[15] += 2;
return halfword;
defer self.r[15] += 2;
return self.bus.read16(self.r[15]);
}
fn fetch(self: *Self) u32 {
const word = self.bus.read32(self.r[15]);
self.r[15] += 4;
return word;
defer self.r[15] += 4;
return self.bus.read32(self.r[15]);
}
pub fn fakePC(self: *const Self) u32 {

View File

@ -92,21 +92,19 @@ pub const Ppu = struct {
// Attributes in OAM are 6 bytes long, with 2 bytes of padding
// Grab Attributes from OAM
const attr0 = @bitCast(Attr0, self.oam.get16(i));
const attr1 = @bitCast(Attr1, self.oam.get16(i + 2));
const attr2 = @bitCast(Attr2, self.oam.get16(i + 4));
const sprite = Sprite.init(attr0, attr1, attr2);
// Only consider enabled sprites
if (sprite.isDisabled()) continue;
// Only consider enabled Sprites
if (!attr0.disabled.read()) {
const attr1 = @bitCast(Attr1, self.oam.get16(i + 2));
// When fetching sprites we only care about ones that could be rendered
// on this scanline
const iy = @bitCast(i8, y);
const start = sprite.y();
const start = attr0.y.read();
const istart = @bitCast(i8, start);
const end = start +% sprite.height;
const end = start +% spriteDimensions(attr0.shape.read(), attr1.size.read())[1];
const iend = @bitCast(i8, end);
// Sprites are expected to be able to wraparound, we perform the same check
@ -114,7 +112,7 @@ pub const Ppu = struct {
if ((start <= y and y < end) or (istart <= iy and iy < iend)) {
for (self.scanline_sprites) |*maybe_sprite| {
if (maybe_sprite.* == null) {
maybe_sprite.* = sprite;
maybe_sprite.* = Sprite.init(attr0, attr1, @bitCast(Attr2, self.oam.get16(i + 4)));
continue :search;
}
}
@ -124,6 +122,7 @@ pub const Ppu = struct {
}
}
}
}
/// Draw all relevant sprites on a scanline
fn drawSprites(self: *Self, prio: u2) void {
@ -631,10 +630,6 @@ const Sprite = struct {
inline fn priority(self: *const Self) u2 {
return self.attr2.rel_prio.read();
}
inline fn isDisabled(self: *const Self) bool {
return self.attr0.disabled.read();
}
};
const Attr0 = extern union {

View File

@ -23,7 +23,7 @@ pub const FpsAverage = struct {
sample_count: u64,
pub fn init() Self {
return .{ .total = 0, .sample_count = 0 };
return .{ .total = 0, .sample_count = 1 };
}
pub fn add(self: *Self, sample: u64) void {