Compare commits
6 Commits
dd52b8f6c1
...
891087dd0b
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 891087dd0b | |
Rekai Nyangadzayi Musuka | 9dc8b52d13 | |
Rekai Nyangadzayi Musuka | 5eb12e9765 | |
Rekai Nyangadzayi Musuka | 84f814f661 | |
Rekai Nyangadzayi Musuka | 65cfc97f28 | |
Rekai Nyangadzayi Musuka | fa862f095a |
|
@ -180,11 +180,11 @@ const Audio = struct {
|
|||
|
||||
export fn callback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void {
|
||||
const apu = @ptrCast(*Apu, @alignCast(@alignOf(*Apu), userdata));
|
||||
const written = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
|
||||
_ = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
|
||||
|
||||
// If we don't write anything, play silence otherwise garbage will be played
|
||||
// FIXME: I don't think this hack to remove DC Offset is acceptable :thinking:
|
||||
if (written == 0) std.mem.set(u8, stream[0..@intCast(usize, len)], 0x40);
|
||||
// if (written == 0) std.mem.set(u8, stream[0..@intCast(usize, len)], 0x40);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -163,6 +163,8 @@ pub const Apu = struct {
|
|||
fs: FrameSequencer,
|
||||
capacitor: f32,
|
||||
|
||||
is_buffer_full: bool,
|
||||
|
||||
pub fn init(sched: *Scheduler) Self {
|
||||
const apu: Self = .{
|
||||
.ch1 = ToneSweep.init(sched),
|
||||
|
@ -178,11 +180,12 @@ pub const Apu = struct {
|
|||
.bias = .{ .raw = 0x0200 },
|
||||
|
||||
.sampling_cycle = 0b00,
|
||||
.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, 1 << 15, SDL.AUDIO_U16, 2, host_sample_rate) orelse unreachable,
|
||||
.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, 1 << 15, SDL.AUDIO_U16, 2, host_sample_rate).?,
|
||||
.sched = sched,
|
||||
|
||||
.capacitor = 0,
|
||||
.fs = FrameSequencer.init(),
|
||||
.is_buffer_full = false,
|
||||
};
|
||||
|
||||
sched.push(.SampleAudio, apu.sampleTicks());
|
||||
|
@ -277,6 +280,13 @@ pub const Apu = struct {
|
|||
}
|
||||
|
||||
pub fn sampleAudio(self: *Self, late: u64) void {
|
||||
self.sched.push(.SampleAudio, self.sampleTicks() -| late);
|
||||
|
||||
// Whether the APU is busy or not is determined by the main loop in emu.zig
|
||||
// This should only ever be true (because this side of the emu is single threaded)
|
||||
// When audio sync is disaabled
|
||||
if (self.is_buffer_full) return;
|
||||
|
||||
var left: i16 = 0;
|
||||
var right: i16 = 0;
|
||||
|
||||
|
@ -325,28 +335,30 @@ pub const Apu = struct {
|
|||
left += bias;
|
||||
right += bias;
|
||||
|
||||
const tmp_left = std.math.clamp(@bitCast(u16, left), std.math.minInt(u11), std.math.maxInt(u11));
|
||||
const tmp_right = std.math.clamp(@bitCast(u16, right), std.math.minInt(u11), std.math.maxInt(u11));
|
||||
const clamped_left = std.math.clamp(@bitCast(u16, left), std.math.minInt(u11), std.math.maxInt(u11));
|
||||
const clamped_right = std.math.clamp(@bitCast(u16, right), std.math.minInt(u11), std.math.maxInt(u11));
|
||||
|
||||
// Extend to 16-bit signed audio samples
|
||||
const final_left = (tmp_left << 5) | (tmp_left >> 6);
|
||||
const final_right = (tmp_right << 5) | (tmp_right >> 6);
|
||||
const ext_left = (clamped_left << 5) | (clamped_left >> 6);
|
||||
const ext_right = (clamped_right << 5) | (clamped_right >> 6);
|
||||
|
||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) {
|
||||
const new_sample_rate = Self.sampleRate(self.bias.sampling_cycle.read());
|
||||
log.info("Sample Rate changed from {}Hz to {}Hz", .{ Self.sampleRate(self.sampling_cycle), new_sample_rate });
|
||||
// FIXME: This rarely happens
|
||||
if (self.sampling_cycle != self.bias.sampling_cycle.read()) self.replaceSDLResampler();
|
||||
|
||||
// Sample Rate Changed, Create a new Resampler since i can't figure out how to change
|
||||
// the parameters of the old one
|
||||
const old = self.stream;
|
||||
defer SDL.SDL_FreeAudioStream(old);
|
||||
|
||||
self.sampling_cycle = self.bias.sampling_cycle.read();
|
||||
self.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, new_sample_rate), SDL.AUDIO_U16, 2, host_sample_rate) orelse unreachable;
|
||||
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ ext_left, ext_right }, 2 * @sizeOf(u16));
|
||||
}
|
||||
|
||||
_ = SDL.SDL_AudioStreamPut(self.stream, &[2]u16{ final_left, final_right }, 2 * @sizeOf(u16));
|
||||
self.sched.push(.SampleAudio, self.sampleTicks() -| late);
|
||||
fn replaceSDLResampler(self: *Self) void {
|
||||
const sample_rate = Self.sampleRate(self.bias.sampling_cycle.read());
|
||||
log.info("Sample Rate changed from {}Hz to {}Hz", .{ Self.sampleRate(self.sampling_cycle), sample_rate });
|
||||
|
||||
// Sampling Cycle (Sample Rate) changed, Craete a new SDL Audio Resampler
|
||||
// FIXME: Replace SDL's Audio Resampler with either a custom or more reliable one
|
||||
const old_stream = self.stream;
|
||||
defer SDL.SDL_FreeAudioStream(old_stream);
|
||||
|
||||
self.sampling_cycle = self.bias.sampling_cycle.read();
|
||||
self.stream = SDL.SDL_NewAudioStream(SDL.AUDIO_U16, 2, @intCast(c_int, sample_rate), SDL.AUDIO_U16, 2, host_sample_rate).?;
|
||||
}
|
||||
|
||||
fn sampleTicks(self: *const Self) u64 {
|
||||
|
|
|
@ -302,6 +302,14 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
|
|||
0x0400_0009 => bus.ppu.bg[0].cnt.raw = (@as(u16, value) << 8) | (bus.ppu.bg[0].cnt.raw & 0xFF),
|
||||
0x0400_000A => bus.ppu.bg[1].cnt.raw = (bus.ppu.bg[1].cnt.raw & 0xFF00) | value,
|
||||
0x0400_000B => bus.ppu.bg[1].cnt.raw = (@as(u16, value) << 8) | (bus.ppu.bg[1].cnt.raw & 0xFF),
|
||||
0x0400_0040 => bus.ppu.win.h[0].set(.Lo, value),
|
||||
0x0400_0041 => bus.ppu.win.h[0].set(.Hi, value),
|
||||
0x0400_0042 => bus.ppu.win.h[1].set(.Lo, value),
|
||||
0x0400_0043 => bus.ppu.win.h[1].set(.Hi, value),
|
||||
0x0400_0044 => bus.ppu.win.v[0].set(.Lo, value),
|
||||
0x0400_0045 => bus.ppu.win.v[0].set(.Hi, value),
|
||||
0x0400_0046 => bus.ppu.win.v[1].set(.Lo, value),
|
||||
0x0400_0047 => bus.ppu.win.v[1].set(.Hi, value),
|
||||
0x0400_0048 => bus.ppu.win.setInL(value),
|
||||
0x0400_0049 => bus.ppu.win.setInH(value),
|
||||
0x0400_004A => bus.ppu.win.setOutL(value),
|
||||
|
@ -464,37 +472,57 @@ pub const BldY = extern union {
|
|||
raw: u16,
|
||||
};
|
||||
|
||||
const u8WriteKind = enum { Hi, Lo };
|
||||
|
||||
/// Write-only
|
||||
pub const WinH = extern union {
|
||||
const Self = @This();
|
||||
|
||||
x2: Bitfield(u16, 0, 8),
|
||||
x1: Bitfield(u16, 8, 8),
|
||||
raw: u16,
|
||||
|
||||
pub fn set(self: *Self, comptime K: u8WriteKind, value: u8) void {
|
||||
self.raw = switch (K) {
|
||||
.Hi => (@as(u16, value) << 8) | self.raw & 0xFF,
|
||||
.Lo => (self.raw & 0xFF00) | value,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// Write-only
|
||||
pub const WinV = extern union {
|
||||
const Self = @This();
|
||||
|
||||
y2: Bitfield(u16, 0, 8),
|
||||
y1: Bitfield(u16, 8, 8),
|
||||
raw: u16,
|
||||
|
||||
pub fn set(self: *Self, comptime K: u8WriteKind, value: u8) void {
|
||||
self.raw = switch (K) {
|
||||
.Hi => (@as(u16, value) << 8) | self.raw & 0xFF,
|
||||
.Lo => (self.raw & 0xFF00) | value,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
pub const WinIn = extern union {
|
||||
w0_bg: Bitfield(u16, 0, 4),
|
||||
w0_obj: Bit(u16, 4),
|
||||
w0_colour: Bit(u16, 5),
|
||||
w0_bld: Bit(u16, 5),
|
||||
w1_bg: Bitfield(u16, 8, 4),
|
||||
w1_obj: Bit(u16, 12),
|
||||
w1_colour: Bit(u16, 13),
|
||||
w1_bld: Bit(u16, 13),
|
||||
raw: u16,
|
||||
};
|
||||
|
||||
pub const WinOut = extern union {
|
||||
out_bg: Bitfield(u16, 0, 4),
|
||||
out_obj: Bit(u16, 4),
|
||||
out_colour: Bit(u16, 5),
|
||||
out_bld: Bit(u16, 5),
|
||||
obj_bg: Bitfield(u16, 8, 4),
|
||||
obj_obj: Bit(u16, 12),
|
||||
obj_colour: Bit(u16, 13),
|
||||
obj_bld: Bit(u16, 13),
|
||||
raw: u16,
|
||||
};
|
||||
|
||||
|
|
|
@ -28,9 +28,14 @@ pub const arm = struct {
|
|||
const multiply = @import("cpu/arm/multiply.zig").multiply;
|
||||
const multiplyLong = @import("cpu/arm/multiply.zig").multiplyLong;
|
||||
|
||||
/// Determine index into ARM InstrFn LUT
|
||||
fn idx(opcode: u32) u12 {
|
||||
return @truncate(u12, opcode >> 20 & 0xFF) << 4 | @truncate(u12, opcode >> 4 & 0xF);
|
||||
}
|
||||
|
||||
// Undefined ARM Instruction handler
|
||||
fn und(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
|
||||
const id = armIdx(opcode);
|
||||
const id = idx(opcode);
|
||||
cpu.panic("[CPU/Decode] ID: 0x{X:0>3} 0x{X:0>8} is an illegal opcode", .{ id, opcode });
|
||||
}
|
||||
|
||||
|
@ -120,9 +125,14 @@ pub const thumb = struct {
|
|||
const swi = @import("cpu/thumb/software_interrupt.zig").fmt17;
|
||||
const branch = @import("cpu/thumb/branch.zig");
|
||||
|
||||
/// Determine index into THUMB InstrFn LUT
|
||||
fn idx(opcode: u16) u10 {
|
||||
return @truncate(u10, opcode >> 6);
|
||||
}
|
||||
|
||||
/// Undefined THUMB Instruction Handler
|
||||
fn und(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
|
||||
const id = thumbIdx(opcode);
|
||||
const id = idx(opcode);
|
||||
cpu.panic("[CPU/Decode] ID: 0b{b:0>10} 0x{X:0>2} is an illegal opcode", .{ id, opcode });
|
||||
}
|
||||
|
||||
|
@ -420,13 +430,13 @@ pub const Arm7tdmi = struct {
|
|||
const opcode = self.fetch(u16);
|
||||
if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
|
||||
|
||||
thumb.lut[thumbIdx(opcode)](self, self.bus, opcode);
|
||||
thumb.lut[thumb.idx(opcode)](self, self.bus, opcode);
|
||||
} else {
|
||||
const opcode = self.fetch(u32);
|
||||
if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
|
||||
|
||||
if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) {
|
||||
arm.lut[armIdx(opcode)](self, self.bus, opcode);
|
||||
arm.lut[arm.idx(opcode)](self, self.bus, opcode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -517,11 +527,11 @@ pub const Arm7tdmi = struct {
|
|||
|
||||
if (self.cpsr.t.read()) {
|
||||
const opcode = self.bus.dbgRead(u16, self.r[15] - 4);
|
||||
const id = thumbIdx(opcode);
|
||||
const id = thumb.idx(opcode);
|
||||
std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode });
|
||||
} else {
|
||||
const opcode = self.bus.dbgRead(u32, self.r[15] - 4);
|
||||
const id = armIdx(opcode);
|
||||
const id = arm.idx(opcode);
|
||||
std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode });
|
||||
}
|
||||
|
||||
|
@ -601,14 +611,6 @@ pub const Arm7tdmi = struct {
|
|||
}
|
||||
};
|
||||
|
||||
inline fn armIdx(opcode: u32) u12 {
|
||||
return @truncate(u12, opcode >> 20 & 0xFF) << 4 | @truncate(u12, opcode >> 4 & 0xF);
|
||||
}
|
||||
|
||||
inline fn thumbIdx(opcode: u16) u10 {
|
||||
return @truncate(u10, opcode >> 6);
|
||||
}
|
||||
|
||||
pub fn checkCond(cpsr: PSR, cond: u4) bool {
|
||||
return switch (cond) {
|
||||
0x0 => cpsr.z.read(), // EQ - Equal
|
||||
|
|
|
@ -70,12 +70,21 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi) void {
|
|||
}
|
||||
}
|
||||
|
||||
fn syncToAudio(cpu: *const Arm7tdmi) void {
|
||||
const stream = cpu.bus.apu.stream;
|
||||
const min_sample_count = 0x800;
|
||||
fn syncToAudio(stream: *SDL.SDL_AudioStream, is_buffer_full: *bool) void {
|
||||
const sample_size = 2 * @sizeOf(u16);
|
||||
const max_buf_size: c_int = 0x400;
|
||||
|
||||
// Busy Loop while we wait for the Audio system to catch up
|
||||
while (SDL.SDL_AudioStreamAvailable(stream) > (@sizeOf(u16) * 2) * min_sample_count) {}
|
||||
// Determine whether the APU is busy right at this moment
|
||||
var still_full: bool = SDL.SDL_AudioStreamAvailable(stream) > sample_size * if (is_buffer_full.*) max_buf_size >> 1 else max_buf_size;
|
||||
defer is_buffer_full.* = still_full; // Update APU Busy status right before exiting scope
|
||||
|
||||
// If Busy is false, there's no need to sync here
|
||||
if (!still_full) return;
|
||||
|
||||
while (true) {
|
||||
still_full = SDL.SDL_AudioStreamAvailable(stream) > sample_size * max_buf_size >> 1;
|
||||
if (!sync_audio or !still_full) break;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
|
||||
|
@ -86,21 +95,21 @@ pub fn runUnsynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi,
|
|||
|
||||
while (!quit.load(.SeqCst)) {
|
||||
runFrame(sched, cpu);
|
||||
if (sync_audio) syncToAudio(cpu);
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
|
||||
tracker.tick();
|
||||
}
|
||||
} else {
|
||||
while (!quit.load(.SeqCst)) {
|
||||
runFrame(sched, cpu);
|
||||
if (sync_audio) syncToAudio(cpu);
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, fps: ?*FpsTracker) void {
|
||||
log.info("Emulation thread w/ video sync", .{});
|
||||
var timer = Timer.start() catch unreachable;
|
||||
var timer = Timer.start() catch std.debug.panic("Failed to initialize std.timer.Timer", .{});
|
||||
var wake_time: u64 = frame_period;
|
||||
|
||||
if (fps) |tracker| {
|
||||
|
@ -108,13 +117,14 @@ pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, f
|
|||
|
||||
while (!quit.load(.SeqCst)) {
|
||||
runFrame(sched, cpu);
|
||||
const new_wake_time = syncToVideo(&timer, wake_time);
|
||||
const new_wake_time = blockOnVideo(&timer, wake_time);
|
||||
|
||||
// Spin to make up the difference of OS scheduler innacuracies
|
||||
// If we happen to also be syncing to audio, we choose to spin on
|
||||
// the amount of time needed for audio to catch up rather than
|
||||
// our expected wake-up time
|
||||
if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
if (!sync_audio) spinLoop(&timer, wake_time);
|
||||
wake_time = new_wake_time;
|
||||
|
||||
tracker.tick();
|
||||
|
@ -122,16 +132,17 @@ pub fn runSynchronized(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, f
|
|||
} else {
|
||||
while (!quit.load(.SeqCst)) {
|
||||
runFrame(sched, cpu);
|
||||
const new_wake_time = syncToVideo(&timer, wake_time);
|
||||
// see above comment
|
||||
if (sync_audio) syncToAudio(cpu) else spinLoop(&timer, wake_time);
|
||||
const new_wake_time = blockOnVideo(&timer, wake_time);
|
||||
|
||||
// see above comment
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
if (!sync_audio) spinLoop(&timer, wake_time);
|
||||
wake_time = new_wake_time;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inline fn syncToVideo(timer: *Timer, wake_time: u64) u64 {
|
||||
inline fn blockOnVideo(timer: *Timer, wake_time: u64) u64 {
|
||||
// Use the OS scheduler to put the emulation thread to sleep
|
||||
const maybe_recalc_wake_time = sleep(timer, wake_time);
|
||||
|
||||
|
@ -149,6 +160,8 @@ pub fn runBusyLoop(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi) void
|
|||
runFrame(sched, cpu);
|
||||
spinLoop(&timer, wake_time);
|
||||
|
||||
syncToAudio(cpu.bus.apu.stream, &cpu.bus.apu.is_buffer_full);
|
||||
|
||||
// Update to the new wake time
|
||||
wake_time += frame_period;
|
||||
}
|
||||
|
|
186
src/core/ppu.zig
186
src/core/ppu.zig
|
@ -277,16 +277,17 @@ pub const Ppu = struct {
|
|||
aff_x += self.aff_bg[n - 2].pa;
|
||||
aff_y += self.aff_bg[n - 2].pc;
|
||||
|
||||
if (!shouldDrawBackground(n, self.bldcnt, &self.scanline, i)) continue;
|
||||
const x = @bitCast(u32, ix);
|
||||
const y = @bitCast(u32, iy);
|
||||
|
||||
const win_bounds = self.windowBounds(@truncate(u9, x), @truncate(u8, y));
|
||||
if (!shouldDrawBackground(self, n, win_bounds, i)) continue;
|
||||
|
||||
if (self.bg[n].cnt.display_overflow.read()) {
|
||||
ix = if (ix > px_width) @rem(ix, px_width) else if (ix < 0) px_width + @rem(ix, px_width) else ix;
|
||||
iy = if (iy > px_height) @rem(iy, px_height) else if (iy < 0) px_height + @rem(iy, px_height) else iy;
|
||||
} else if (ix > px_width or iy > px_height or ix < 0 or iy < 0) continue;
|
||||
|
||||
const x = @bitCast(u32, ix);
|
||||
const y = @bitCast(u32, iy);
|
||||
|
||||
const tile_id: u32 = self.vram.read(u8, screen_base + ((y / 8) * @bitCast(u32, tile_width) + (x / 8)));
|
||||
const row = y & 7;
|
||||
const col = x & 7;
|
||||
|
@ -296,7 +297,7 @@ pub const Ppu = struct {
|
|||
|
||||
if (pal_id != 0) {
|
||||
const bgr555 = self.palette.read(u16, pal_id * 2);
|
||||
copyToBackgroundBuffer(n, self.bldcnt, &self.scanline, i, bgr555);
|
||||
self.copyToBackgroundBuffer(n, win_bounds, i, bgr555);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -305,7 +306,7 @@ pub const Ppu = struct {
|
|||
self.aff_bg[n - 2].y_latch.? += self.aff_bg[n - 2].pd; // PD is added to BGxY
|
||||
}
|
||||
|
||||
fn drawBackround(self: *Self, comptime n: u2) void {
|
||||
fn drawBackground(self: *Self, comptime n: u2) void {
|
||||
// A Tile in a charblock is a byte, while a Screen Entry is a halfword
|
||||
|
||||
const char_base = 0x4000 * @as(u32, self.bg[n].cnt.char_base.read());
|
||||
|
@ -325,10 +326,11 @@ pub const Ppu = struct {
|
|||
|
||||
var i: u32 = 0;
|
||||
while (i < width) : (i += 1) {
|
||||
if (!shouldDrawBackground(n, self.bldcnt, &self.scanline, i)) continue;
|
||||
|
||||
const x = hofs + i;
|
||||
|
||||
const win_bounds = self.windowBounds(@truncate(u9, x), @truncate(u8, y));
|
||||
if (!shouldDrawBackground(self, n, win_bounds, i)) continue;
|
||||
|
||||
// Grab the Screen Entry from VRAM
|
||||
const entry_addr = screen_base + tilemapOffset(size, x, y);
|
||||
const entry = @bitCast(ScreenEntry, self.vram.read(u16, entry_addr));
|
||||
|
@ -353,7 +355,7 @@ pub const Ppu = struct {
|
|||
|
||||
if (pal_id != 0) {
|
||||
const bgr555 = self.palette.read(u16, pal_id * 2);
|
||||
copyToBackgroundBuffer(n, self.bldcnt, &self.scanline, i, bgr555);
|
||||
self.copyToBackgroundBuffer(n, win_bounds, i, bgr555);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -379,10 +381,10 @@ pub const Ppu = struct {
|
|||
var layer: usize = 0;
|
||||
while (layer < 4) : (layer += 1) {
|
||||
self.drawSprites(@truncate(u2, layer));
|
||||
if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackround(0);
|
||||
if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackround(1);
|
||||
if (layer == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawBackround(2);
|
||||
if (layer == self.bg[3].cnt.priority.read() and bg_enable >> 3 & 1 == 1) self.drawBackround(3);
|
||||
if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackground(0);
|
||||
if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackground(1);
|
||||
if (layer == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawBackground(2);
|
||||
if (layer == self.bg[3].cnt.priority.read() and bg_enable >> 3 & 1 == 1) self.drawBackground(3);
|
||||
}
|
||||
|
||||
// Copy Drawn Scanline to Frame Buffer
|
||||
|
@ -407,8 +409,8 @@ pub const Ppu = struct {
|
|||
var layer: usize = 0;
|
||||
while (layer < 4) : (layer += 1) {
|
||||
self.drawSprites(@truncate(u2, layer));
|
||||
if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackround(0);
|
||||
if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackround(1);
|
||||
if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackground(0);
|
||||
if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackground(1);
|
||||
if (layer == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawAffineBackground(2);
|
||||
}
|
||||
|
||||
|
@ -533,6 +535,93 @@ pub const Ppu = struct {
|
|||
return self.palette.getBackdrop();
|
||||
}
|
||||
|
||||
fn copyToBackgroundBuffer(self: *Self, comptime n: u2, bounds: ?WindowBounds, i: usize, bgr555: u16) void {
|
||||
if (self.bldcnt.mode.read() != 0b00) {
|
||||
// Standard Alpha Blending
|
||||
const a_layers = self.bldcnt.layer_a.read();
|
||||
const is_blend_enabled = (a_layers >> n) & 1 == 1;
|
||||
|
||||
// If Alpha Blending is enabled and we've found an eligible layer for
|
||||
// Pixel A, store the pixel in the bottom pixel buffer
|
||||
|
||||
const win_part = if (bounds) |win| blk: {
|
||||
// Window Enabled
|
||||
break :blk switch (win) {
|
||||
.win0 => self.win.in.w0_bld.read(),
|
||||
.win1 => self.win.in.w1_bld.read(),
|
||||
.out => self.win.out.out_bld.read(),
|
||||
};
|
||||
} else true;
|
||||
|
||||
if (win_part and is_blend_enabled) {
|
||||
self.scanline.btm()[i] = bgr555;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
self.scanline.top()[i] = bgr555;
|
||||
}
|
||||
|
||||
const WindowBounds = enum { win0, win1, out };
|
||||
|
||||
fn windowBounds(self: *Self, x: u9, y: u8) ?WindowBounds {
|
||||
const win0 = self.dispcnt.win_enable.read() & 1 == 1;
|
||||
const win1 = (self.dispcnt.win_enable.read() >> 1) & 1 == 1;
|
||||
const winObj = self.dispcnt.obj_win_enable.read();
|
||||
|
||||
if (!(win0 or win1 or winObj)) return null;
|
||||
|
||||
if (win0 and self.win.inRange(0, x, y)) return .win0;
|
||||
if (win1 and self.win.inRange(1, x, y)) return .win1;
|
||||
|
||||
return .out;
|
||||
}
|
||||
|
||||
fn shouldDrawBackground(self: *Self, comptime n: u2, bounds: ?WindowBounds, i: usize) bool {
|
||||
// If a pixel has been drawn on the top layer, it's because:
|
||||
// 1. The pixel is to be blended with a pixel on the bottom layer
|
||||
// 2. The pixel is not to be blended at all
|
||||
// Also, if we find a pixel on the top layer we don't need to bother with this I think?
|
||||
if (self.scanline.top()[i] != null) return false;
|
||||
|
||||
if (bounds) |win| {
|
||||
switch (win) {
|
||||
.win0 => if ((self.win.in.w0_bg.read() >> n) & 1 == 0) return false,
|
||||
.win1 => if ((self.win.in.w1_bg.read() >> n) & 1 == 0) return false,
|
||||
.out => if ((self.win.out.out_bg.read() >> n) & 1 == 0) return false,
|
||||
}
|
||||
}
|
||||
|
||||
if (self.scanline.btm()[i] != null) {
|
||||
// The pixel found in the bottom layer is:
|
||||
// 1. From a higher priority background
|
||||
// 2. From a background that is marked for blending (Pixel A)
|
||||
|
||||
// If Alpha Blending isn't enabled, then we've already found a higher prio
|
||||
// pixel, we can return early
|
||||
if (self.bldcnt.mode.read() != 0b01) return false;
|
||||
|
||||
const b_layers = self.bldcnt.layer_b.read();
|
||||
|
||||
const win_part = if (bounds) |win| blk: {
|
||||
// Window Enabled
|
||||
break :blk switch (win) {
|
||||
.win0 => self.win.in.w0_bld.read(),
|
||||
.win1 => self.win.in.w1_bld.read(),
|
||||
.out => self.win.out.out_bld.read(),
|
||||
};
|
||||
} else true;
|
||||
|
||||
// If the Background is not marked for blending, we've already found
|
||||
// a higher priority pixel, move on.
|
||||
|
||||
const is_blend_enabled = win_part and ((b_layers >> n) & 1 == 1);
|
||||
if (!is_blend_enabled) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO: Comment this + get a better understanding
|
||||
fn tilemapOffset(size: u2, x: u32, y: u32) u32 {
|
||||
// Current Row: (y % PIXEL_COUNT) / 8
|
||||
|
@ -794,6 +883,25 @@ const Window = struct {
|
|||
};
|
||||
}
|
||||
|
||||
fn inRange(self: *const Self, comptime id: u1, x: u9, y: u8) bool {
|
||||
const h = self.h[id];
|
||||
const v = self.v[id];
|
||||
|
||||
const y1 = v.y1.read();
|
||||
const y2 = if (y1 > v.y2.read()) 160 else std.math.min(160, v.y2.read());
|
||||
|
||||
if (y1 <= y and y < y2) {
|
||||
// Within Y bounds
|
||||
const x1 = h.x1.read();
|
||||
const x2 = if (x1 > h.x2.read()) 240 else std.math.min(240, h.x2.read());
|
||||
|
||||
// Within X Bounds
|
||||
return x1 <= x and x < x2;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
pub fn setH(self: *Self, value: u32) void {
|
||||
self.h[0].raw = @truncate(u16, value);
|
||||
self.h[1].raw = @truncate(u16, value >> 16);
|
||||
|
@ -1135,37 +1243,6 @@ fn alphaBlend(top: u16, btm: u16, bldalpha: io.BldAlpha) u16 {
|
|||
return (bld_b << 10) | (bld_g << 5) | bld_r;
|
||||
}
|
||||
|
||||
fn shouldDrawBackground(comptime n: u2, bldcnt: io.BldCnt, scanline: *Scanline, i: usize) bool {
|
||||
// If a pixel has been drawn on the top layer, it's because
|
||||
// Either the pixel is to be blended with a pixel on the bottom layer
|
||||
// or the pixel is not to be blended at all
|
||||
// Consequentially, if we find a pixel on the top layer, there's no need
|
||||
// to render anything I think?
|
||||
if (scanline.top()[i] != null) return false;
|
||||
|
||||
if (scanline.btm()[i] != null) {
|
||||
// The Pixel found in the Bottom layer is
|
||||
// 1. From a higher priority
|
||||
// 2. From a Backround that is marked for Blending (Pixel A)
|
||||
//
|
||||
// We now have to confirm whether this current Background can be used
|
||||
// as Pixel B or not.
|
||||
|
||||
// If Alpha Blending isn't enabled, we've aready found a higher
|
||||
// priority pixel to render. Move on
|
||||
if (bldcnt.mode.read() != 0b01) return false;
|
||||
|
||||
const b_layers = bldcnt.layer_b.read();
|
||||
const is_blend_enabled = (b_layers >> n) & 1 == 1;
|
||||
|
||||
// If the Background is not marked for blending, we've already found
|
||||
// a higher priority pixel, move on.
|
||||
if (!is_blend_enabled) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
fn shouldDrawSprite(bldcnt: io.BldCnt, scanline: *Scanline, x: u9) bool {
|
||||
if (scanline.top()[x] != null) return false;
|
||||
|
||||
|
@ -1180,23 +1257,6 @@ fn shouldDrawSprite(bldcnt: io.BldCnt, scanline: *Scanline, x: u9) bool {
|
|||
return true;
|
||||
}
|
||||
|
||||
fn copyToBackgroundBuffer(comptime n: u2, bldcnt: io.BldCnt, scanline: *Scanline, i: usize, bgr555: u16) void {
|
||||
if (bldcnt.mode.read() != 0b00) {
|
||||
// Standard Alpha Blending
|
||||
const a_layers = bldcnt.layer_a.read();
|
||||
const is_blend_enabled = (a_layers >> n) & 1 == 1;
|
||||
|
||||
// If Alpha Blending is enabled and we've found an eligible layer for
|
||||
// Pixel A, store the pixel in the bottom pixel buffer
|
||||
if (is_blend_enabled) {
|
||||
scanline.btm()[i] = bgr555;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
scanline.top()[i] = bgr555;
|
||||
}
|
||||
|
||||
fn copyToSpriteBuffer(bldcnt: io.BldCnt, scanline: *Scanline, x: u9, bgr555: u16) void {
|
||||
if (bldcnt.mode.read() != 0b00) {
|
||||
// Alpha Blending
|
||||
|
|
Loading…
Reference in New Issue