Compare commits

...

2 Commits

4 changed files with 46 additions and 33 deletions

View File

@ -49,28 +49,42 @@ pub fn runEmuThread(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64)
// ns_late is non zero if we are late. // ns_late is non zero if we are late.
var ns_late = timestamp -| wake_time; var ns_late = timestamp -| wake_time;
// Calculate the new Thread wake time // log.info("timestamp: {} | late: {}", .{ timestamp, ns_late });
wake_time += frame_period;
// If we're more than a frame late, skip the rest of this loop // If we're more than a frame late, skip the rest of this loop
if (ns_late > frame_period) continue; // Recalculate what our new wake time should be so that we can
// get "back on track"
if (ns_late > frame_period) {
wake_time = timestamp + frame_period;
continue;
}
if (sync_to_video) std.time.sleep(frame_period - ns_late); if (sync_to_video) {
// Employ several sleep calls in periods of 10ms
// By doing this the behaviour should average out to be
// more consistent
// Backup Busy Loop Routine const sleep_for = frame_period - ns_late;
// if (sync_to_video) spinLoop(&timer, wake_time); const loop_count = sleep_for / (std.time.ns_per_ms * 10); // How many groups of 10ms
var i: usize = 0;
while (i < loop_count) : (i += 1) {
std.time.sleep(std.time.ns_per_ms * 10);
}
// Spin to make up the difference if there is a need
// Make sure that we're using the old wake time and not the onne we recalcualted
spinLoop(&timer, wake_time);
}
// Update to the new wake time
wake_time += frame_period;
} }
} }
} }
fn spinLoop(timer: *Timer, wake_time: u64) void { fn spinLoop(timer: *Timer, wake_time: u64) void {
while (true) { while (true) if (timer.read() > wake_time) break;
const timestamp = timer.read();
if (timestamp >= wake_time) {
break;
}
}
} }
fn emuFps(left: u64) u64 { fn emuFps(left: u64) u64 {

View File

@ -177,7 +177,7 @@ pub fn main() anyerror!void {
fps_avg.add(emu_fps.load(.Unordered)); fps_avg.add(emu_fps.load(.Unordered));
const avg = fps_avg.calc(); const avg = fps_avg.calc();
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3}fps, {d:0>3}%] ", .{ title, avg, (avg * 100 / 59) }) catch unreachable; const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3}fps, {d:0>3}%] ", .{ title, avg, (avg * 100 / 60) }) catch unreachable;
SDL.SDL_SetWindowTitle(window, dyn_title.ptr); SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
} }

View File

@ -121,37 +121,36 @@ pub const Ppu = struct {
/// Draw all relevant sprites on a scanline /// Draw all relevant sprites on a scanline
fn drawSprites(self: *Self, prio: u2) void { fn drawSprites(self: *Self, prio: u2) void {
// Object VRAM 3rd and 4th (0-indexed) charblocks
const char_base = 0x4000 * 4; const char_base = 0x4000 * 4;
const y = @bitCast(i8, self.vcount.scanline.read()); const y = @bitCast(i8, self.vcount.scanline.read());
var i: u9 = 0; // Loop over every fetched sprite
while (i < width) : (i += 1) { sprite_loop: for (self.scanline_sprites) |maybe_sprites| {
// Exit early if a pixel is already here if (maybe_sprites) |sprite| {
if (self.scanline_buf[i] != null) continue; // Move on to the next sprite If its of a different priority
if (sprite.priority() != prio) continue :sprite_loop;
const x = i;
for (self.scanline_sprites) |maybe_sprite| {
if (maybe_sprite) |sprite| {
if (sprite.priority() != prio) continue;
var i: u9 = 0;
px_loop: while (i < sprite.width) : (i += 1) {
const x = (sprite.x() +% i) % 240;
const ix = @bitCast(i9, x); const ix = @bitCast(i9, x);
// If We've already rendered a pixel here don't overwrite it
if (self.scanline_buf[x] != null) continue :px_loop;
const start = sprite.x(); const start = sprite.x();
const istart = @bitCast(i9, start); const istart = @bitCast(i9, start);
const end = start +% sprite.width; const end = start +% sprite.width;
const iend = @bitCast(i9, end); const iend = @bitCast(i9, end);
// Sprites are expected to wrap, by performing the same check on both // By comparing with both signed and unsigned values we ensure that sprites
// signed and unsigned values we ensure that sprites are properly displayed // are displayed in all valid (AFAIK) configuration
// in all valid scenarios
if ((start <= x and x < end) or (istart <= ix and ix < iend)) { if ((start <= x and x < end) or (istart <= ix and ix < iend)) {
self.drawSpritePixel(char_base, sprite, ix, y); self.drawSpritePixel(char_base, sprite, ix, y);
} }
} else break; }
} } else break;
} }
} }
@ -275,12 +274,12 @@ pub const Ppu = struct {
0x0 => { 0x0 => {
const start = framebuf_pitch * @as(usize, scanline); const start = framebuf_pitch * @as(usize, scanline);
self.fetchSprites(); if (obj_enable) self.fetchSprites();
var i: usize = 0; var i: usize = 0;
while (i < 4) : (i += 1) { while (i < 4) : (i += 1) {
// Draw Sprites Here // Draw Sprites Here
if (obj_enable) self.drawSprites(@truncate(u2, i)); self.drawSprites(@truncate(u2, i));
if (i == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackround(0); if (i == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackround(0);
if (i == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackround(1); if (i == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackround(1);
if (i == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawBackround(2); if (i == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawBackround(2);

View File

@ -18,7 +18,7 @@ pub const FpsAverage = struct {
} }
pub fn add(self: *Self, sample: u64) void { pub fn add(self: *Self, sample: u64) void {
if (self.sample_count == 1000) return self.reset(sample); if (self.sample_count == 600) return self.reset(sample);
self.total += sample; self.total += sample;
self.sample_count += 1; self.sample_count += 1;