Compare commits

..

No commits in common. "d7e3d347260696cafaad53dd683deafc251d0448" and "002e33b48b381fae150afd2dfc2f177eeddc2908" have entirely different histories.

18 changed files with 121 additions and 229 deletions

View File

@ -77,7 +77,7 @@ arm7wrestler GBA Fixed | [destoer](https://github.com/destoer)
## Compiling ## Compiling
Most recently built on Zig [0.11.0-dev.368+1829b6eab](https://github.com/ziglang/zig/tree/1829b6eab) Most recently built on Zig [v0.11.0-dev.144+892fb0fc8](https://github.com/ziglang/zig/tree/892fb0fc8)
### Dependencies ### Dependencies

View File

@ -1,15 +1,7 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin");
const Sdk = @import("lib/SDL.zig/Sdk.zig"); const Sdk = @import("lib/SDL.zig/Sdk.zig");
pub fn build(b: *std.build.Builder) void { pub fn build(b: *std.build.Builder) void {
// Minimum Zig Version
const min_ver = std.SemanticVersion.parse("0.11.0-dev.323+30eb2a175") catch return; // https://github.com/ziglang/zig/commit/30eb2a175
if (builtin.zig_version.order(min_ver).compare(.lt)) {
std.log.err("{s}", .{b.fmt("Zig v{} does not meet the minimum version requirement. (Zig v{})", .{ builtin.zig_version, min_ver })});
std.os.exit(1);
}
// Standard target options allows the person running `zig build` to choose // Standard target options allows the person running `zig build` to choose
// what target to build for. Here we do not override the defaults, which // what target to build for. Here we do not override the defaults, which
// means any target is allowed, and the default is native. Other options // means any target is allowed, and the default is native. Other options

View File

@ -108,121 +108,106 @@ pub fn read(comptime T: type, apu: *const Apu, addr: u32) ?T {
pub fn write(comptime T: type, apu: *Apu, addr: u32, value: T) void { pub fn write(comptime T: type, apu: *Apu, addr: u32, value: T) void {
const byte_addr = @truncate(u8, addr); const byte_addr = @truncate(u8, addr);
if (byte_addr <= 0x81 and !apu.cnt.apu_enable.read()) return;
switch (T) { switch (T) {
u32 => { u32 => switch (byte_addr) {
// 0x80 and 0x81 handled in setSoundCnt 0x60 => apu.ch1.setSound1Cnt(value),
if (byte_addr < 0x80 and !apu.cnt.apu_enable.read()) return; 0x64 => apu.ch1.setSound1CntX(&apu.fs, @truncate(u16, value)),
switch (byte_addr) { 0x68 => apu.ch2.setSound2CntL(@truncate(u16, value)),
0x60 => apu.ch1.setSound1Cnt(value), 0x6C => apu.ch2.setSound2CntH(&apu.fs, @truncate(u16, value)),
0x64 => apu.ch1.setSound1CntX(&apu.fs, @truncate(u16, value)),
0x68 => apu.ch2.setSound2CntL(@truncate(u16, value)), 0x70 => apu.ch3.setSound3Cnt(value),
0x6C => apu.ch2.setSound2CntH(&apu.fs, @truncate(u16, value)), 0x74 => apu.ch3.setSound3CntX(&apu.fs, @truncate(u16, value)),
0x70 => apu.ch3.setSound3Cnt(value), 0x78 => apu.ch4.setSound4CntL(@truncate(u16, value)),
0x74 => apu.ch3.setSound3CntX(&apu.fs, @truncate(u16, value)), 0x7C => apu.ch4.setSound4CntH(&apu.fs, @truncate(u16, value)),
0x78 => apu.ch4.setSound4CntL(@truncate(u16, value)), 0x80 => apu.setSoundCnt(value),
0x7C => apu.ch4.setSound4CntH(&apu.fs, @truncate(u16, value)), 0x84 => apu.setSoundCntX(value >> 7 & 1 == 1),
0x88 => apu.bias.raw = @truncate(u16, value),
0x8C => {},
0x80 => apu.setSoundCnt(value), 0x90, 0x94, 0x98, 0x9C => apu.ch3.wave_dev.write(T, apu.ch3.select, addr, value),
0x84 => apu.setSoundCntX(value >> 7 & 1 == 1), 0xA0 => apu.chA.push(value), // FIFO_A
0x88 => apu.bias.raw = @truncate(u16, value), 0xA4 => apu.chB.push(value), // FIFO_B
0x8C => {}, else => util.io.write.undef(log, "Tried to write 0x{X:0>8}{} to 0x{X:0>8}", .{ value, T, addr }),
0x90, 0x94, 0x98, 0x9C => apu.ch3.wave_dev.write(T, apu.ch3.select, addr, value),
0xA0 => apu.chA.push(value), // FIFO_A
0xA4 => apu.chB.push(value), // FIFO_B
else => util.io.write.undef(log, "Tried to write 0x{X:0>8}{} to 0x{X:0>8}", .{ value, T, addr }),
}
}, },
u16 => { u16 => switch (byte_addr) {
if (byte_addr <= 0x81 and !apu.cnt.apu_enable.read()) return; 0x60 => apu.ch1.setSound1CntL(@truncate(u8, value)), // SOUND1CNT_L
0x62 => apu.ch1.setSound1CntH(value),
0x64 => apu.ch1.setSound1CntX(&apu.fs, value),
0x66 => {},
switch (byte_addr) { 0x68 => apu.ch2.setSound2CntL(value),
0x60 => apu.ch1.setSound1CntL(@truncate(u8, value)), // SOUND1CNT_L 0x6A => {},
0x62 => apu.ch1.setSound1CntH(value), 0x6C => apu.ch2.setSound2CntH(&apu.fs, value),
0x64 => apu.ch1.setSound1CntX(&apu.fs, value), 0x6E => {},
0x66 => {},
0x68 => apu.ch2.setSound2CntL(value), 0x70 => apu.ch3.setSound3CntL(@truncate(u8, value)),
0x6A => {}, 0x72 => apu.ch3.setSound3CntH(value),
0x6C => apu.ch2.setSound2CntH(&apu.fs, value), 0x74 => apu.ch3.setSound3CntX(&apu.fs, value),
0x6E => {}, 0x76 => {},
0x70 => apu.ch3.setSound3CntL(@truncate(u8, value)), 0x78 => apu.ch4.setSound4CntL(value),
0x72 => apu.ch3.setSound3CntH(value), 0x7A => {},
0x74 => apu.ch3.setSound3CntX(&apu.fs, value), 0x7C => apu.ch4.setSound4CntH(&apu.fs, value),
0x76 => {}, 0x7E => {},
0x78 => apu.ch4.setSound4CntL(value), 0x80 => apu.setSoundCntL(value),
0x7A => {}, 0x82 => apu.setSoundCntH(value),
0x7C => apu.ch4.setSound4CntH(&apu.fs, value), 0x84 => apu.setSoundCntX(value >> 7 & 1 == 1),
0x7E => {}, 0x86 => {},
0x88 => apu.bias.raw = value, // SOUNDBIAS
0x8A, 0x8C, 0x8E => {},
0x80 => apu.setSoundCntL(value), 0x90, 0x92, 0x94, 0x96, 0x98, 0x9A, 0x9C, 0x9E => apu.ch3.wave_dev.write(T, apu.ch3.select, addr, value),
0x82 => apu.setSoundCntH(value), 0xA0, 0xA2 => log.err("Tried to write 0x{X:0>4}{} to FIFO_A", .{ value, T }),
0x84 => apu.setSoundCntX(value >> 7 & 1 == 1), 0xA4, 0xA6 => log.err("Tried to write 0x{X:0>4}{} to FIFO_B", .{ value, T }),
0x86 => {}, else => util.io.write.undef(log, "Tried to write 0x{X:0>4}{} to 0x{X:0>8}", .{ value, T, addr }),
0x88 => apu.bias.raw = value, // SOUNDBIAS
0x8A, 0x8C, 0x8E => {},
0x90, 0x92, 0x94, 0x96, 0x98, 0x9A, 0x9C, 0x9E => apu.ch3.wave_dev.write(T, apu.ch3.select, addr, value),
0xA0, 0xA2 => log.err("Tried to write 0x{X:0>4}{} to FIFO_A", .{ value, T }),
0xA4, 0xA6 => log.err("Tried to write 0x{X:0>4}{} to FIFO_B", .{ value, T }),
else => util.io.write.undef(log, "Tried to write 0x{X:0>4}{} to 0x{X:0>8}", .{ value, T, addr }),
}
}, },
u8 => { u8 => switch (byte_addr) {
if (byte_addr <= 0x81 and !apu.cnt.apu_enable.read()) return; 0x60 => apu.ch1.setSound1CntL(value),
0x61 => {},
0x62 => apu.ch1.setNr11(value),
0x63 => apu.ch1.setNr12(value),
0x64 => apu.ch1.setNr13(value),
0x65 => apu.ch1.setNr14(&apu.fs, value),
0x66, 0x67 => {},
switch (byte_addr) { 0x68 => apu.ch2.setNr21(value),
0x60 => apu.ch1.setSound1CntL(value), 0x69 => apu.ch2.setNr22(value),
0x61 => {}, 0x6A, 0x6B => {},
0x62 => apu.ch1.setNr11(value), 0x6C => apu.ch2.setNr23(value),
0x63 => apu.ch1.setNr12(value), 0x6D => apu.ch2.setNr24(&apu.fs, value),
0x64 => apu.ch1.setNr13(value), 0x6E, 0x6F => {},
0x65 => apu.ch1.setNr14(&apu.fs, value),
0x66, 0x67 => {},
0x68 => apu.ch2.setNr21(value), 0x70 => apu.ch3.setSound3CntL(value), // NR30
0x69 => apu.ch2.setNr22(value), 0x71 => {},
0x6A, 0x6B => {}, 0x72 => apu.ch3.setNr31(value),
0x6C => apu.ch2.setNr23(value), 0x73 => apu.ch3.vol.raw = value, // NR32
0x6D => apu.ch2.setNr24(&apu.fs, value), 0x74 => apu.ch3.setNr33(value),
0x6E, 0x6F => {}, 0x75 => apu.ch3.setNr34(&apu.fs, value),
0x76, 0x77 => {},
0x70 => apu.ch3.setSound3CntL(value), // NR30 0x78 => apu.ch4.setNr41(value),
0x71 => {}, 0x79 => apu.ch4.setNr42(value),
0x72 => apu.ch3.setNr31(value), 0x7A, 0x7B => {},
0x73 => apu.ch3.vol.raw = value, // NR32 0x7C => apu.ch4.poly.raw = value, // NR 43
0x74 => apu.ch3.setNr33(value), 0x7D => apu.ch4.setNr44(&apu.fs, value),
0x75 => apu.ch3.setNr34(&apu.fs, value), 0x7E, 0x7F => {},
0x76, 0x77 => {},
0x78 => apu.ch4.setNr41(value), 0x80, 0x81 => apu.setSoundCntL(setHalf(u16, apu.psg_cnt.raw, byte_addr, value)),
0x79 => apu.ch4.setNr42(value), 0x82, 0x83 => apu.setSoundCntH(setHalf(u16, apu.dma_cnt.raw, byte_addr, value)),
0x7A, 0x7B => {}, 0x84 => apu.setSoundCntX(value >> 7 & 1 == 1),
0x7C => apu.ch4.poly.raw = value, // NR 43 0x85 => {},
0x7D => apu.ch4.setNr44(&apu.fs, value), 0x86, 0x87 => {},
0x7E, 0x7F => {}, 0x88, 0x89 => apu.bias.raw = setHalf(u16, apu.bias.raw, byte_addr, value), // SOUNDBIAS
0x8A...0x8F => {},
0x80, 0x81 => apu.setSoundCntL(setHalf(u16, apu.psg_cnt.raw, byte_addr, value)), 0x90...0x9F => apu.ch3.wave_dev.write(T, apu.ch3.select, addr, value),
0x82, 0x83 => apu.setSoundCntH(setHalf(u16, apu.dma_cnt.raw, byte_addr, value)), 0xA0...0xA3 => log.err("Tried to write 0x{X:0>2}{} to FIFO_A", .{ value, T }),
0x84 => apu.setSoundCntX(value >> 7 & 1 == 1), 0xA4...0xA7 => log.err("Tried to write 0x{X:0>2}{} to FIFO_B", .{ value, T }),
0x85 => {}, else => util.io.write.undef(log, "Tried to write 0x{X:0>2}{} to 0x{X:0>8}", .{ value, T, addr }),
0x86, 0x87 => {},
0x88, 0x89 => apu.bias.raw = setHalf(u16, apu.bias.raw, byte_addr, value), // SOUNDBIAS
0x8A...0x8F => {},
0x90...0x9F => apu.ch3.wave_dev.write(T, apu.ch3.select, addr, value),
0xA0...0xA3 => log.err("Tried to write 0x{X:0>2}{} to FIFO_A", .{ value, T }),
0xA4...0xA7 => log.err("Tried to write 0x{X:0>2}{} to FIFO_B", .{ value, T }),
else => util.io.write.undef(log, "Tried to write 0x{X:0>2}{} to 0x{X:0>8}", .{ value, T, addr }),
}
}, },
else => @compileError("APU: Unsupported write width"), else => @compileError("APU: Unsupported write width"),
} }
@ -290,20 +275,15 @@ pub const Apu = struct {
} }
fn reset(self: *Self) void { fn reset(self: *Self) void {
// All PSG Registers between 0x0400_0060..0x0400_0081 are zeroed
// 0x0400_0082 and 0x0400_0088 retain their values
self.ch1.reset(); self.ch1.reset();
self.ch2.reset(); self.ch2.reset();
self.ch3.reset(); self.ch3.reset();
self.ch4.reset(); self.ch4.reset();
// GBATEK says 4000060h..4000081h I take this to mean inclusive
self.psg_cnt.raw = 0x0000;
} }
/// SOUNDCNT /// SOUNDCNT
fn setSoundCnt(self: *Self, value: u32) void { fn setSoundCnt(self: *Self, value: u32) void {
if (self.cnt.apu_enable.read()) self.setSoundCntL(@truncate(u16, value)); self.setSoundCntL(@truncate(u16, value));
self.setSoundCntH(@truncate(u16, value >> 16)); self.setSoundCntH(@truncate(u16, value >> 16));
} }
@ -342,14 +322,11 @@ pub const Apu = struct {
self.fs.step = 0; // Reset Frame Sequencer self.fs.step = 0; // Reset Frame Sequencer
// Reset Square Wave Offsets // Reset Square Wave Offsets
self.ch1.square.reset(); self.ch1.square.pos = 0;
self.ch2.square.reset(); self.ch2.square.pos = 0;
// Reset Wave // Reset Wave Device Offsets
self.ch3.wave_dev.reset(); self.ch3.wave_dev.offset = 0;
// Rest Noise
self.ch4.lfsr.reset();
} else { } else {
self.reset(); self.reset();
} }
@ -418,8 +395,8 @@ pub const Apu = struct {
right += if (self.dma_cnt.chB_right.read()) chB_sample else 0; right += if (self.dma_cnt.chB_right.read()) chB_sample else 0;
// Add SOUNDBIAS // Add SOUNDBIAS
// FIXME: SOUNDBIAS is 10-bit but The waveform is centered around 0 if I treat it as 11-bit // FIXME: Is SOUNDBIAS 9-bit or 10-bit?
const bias = @as(i16, self.bias.level.read()) << 2; const bias = @as(i16, self.bias.level.read()) << 1;
left += bias; left += bias;
right += bias; right += bias;

View File

@ -49,13 +49,10 @@ pub fn init(sched: *Scheduler) Self {
} }
pub fn reset(self: *Self) void { pub fn reset(self: *Self) void {
self.len = 0; // NR41 self.len = 0;
self.envelope.raw = 0; // NR42 self.envelope.raw = 0;
self.poly.raw = 0; // NR43 self.poly.raw = 0;
self.cnt.raw = 0; // NR44 self.cnt.raw = 0;
self.len_dev.reset();
self.env_dev.reset();
self.sample = 0; self.sample = 0;
self.enabled = false; self.enabled = false;

View File

@ -43,12 +43,9 @@ pub fn init(sched: *Scheduler) Self {
} }
pub fn reset(self: *Self) void { pub fn reset(self: *Self) void {
self.duty.raw = 0; // NR21 self.duty.raw = 0;
self.envelope.raw = 0; // NR22 self.envelope.raw = 0;
self.freq.raw = 0; // NR32, NR24 self.freq.raw = 0;
self.len_dev.reset();
self.env_dev.reset();
self.sample = 0; self.sample = 0;
self.enabled = false; self.enabled = false;

View File

@ -50,14 +50,12 @@ pub fn init(sched: *Scheduler) Self {
} }
pub fn reset(self: *Self) void { pub fn reset(self: *Self) void {
self.sweep.raw = 0; // NR10 self.sweep.raw = 0;
self.duty.raw = 0; // NR11 self.sweep_dev.calc_performed = false;
self.envelope.raw = 0; // NR12
self.freq.raw = 0; // NR13, NR14
self.len_dev.reset(); self.duty.raw = 0;
self.sweep_dev.reset(); self.envelope.raw = 0;
self.env_dev.reset(); self.freq.raw = 0;
self.sample = 0; self.sample = 0;
self.enabled = false; self.enabled = false;

View File

@ -42,13 +42,10 @@ pub fn init(sched: *Scheduler) Self {
} }
pub fn reset(self: *Self) void { pub fn reset(self: *Self) void {
self.select.raw = 0; // NR30 self.select.raw = 0;
self.length = 0; // NR31 self.length = 0;
self.vol.raw = 0; // NR32 self.vol.raw = 0;
self.freq.raw = 0; // NR33, NR34 self.freq.raw = 0;
self.len_dev.reset();
self.wave_dev.reset();
self.sample = 0; self.sample = 0;
self.enabled = false; self.enabled = false;

View File

@ -11,11 +11,6 @@ pub fn create() Self {
return .{ .timer = 0, .vol = 0 }; return .{ .timer = 0, .vol = 0 };
} }
pub fn reset(self: *Self) void {
self.timer = 0;
self.vol = 0;
}
pub fn tick(self: *Self, nrx2: io.Envelope) void { pub fn tick(self: *Self, nrx2: io.Envelope) void {
if (nrx2.period.read() != 0) { if (nrx2.period.read() != 0) {
if (self.timer != 0) self.timer -= 1; if (self.timer != 0) self.timer -= 1;

View File

@ -6,10 +6,6 @@ pub fn create() Self {
return .{ .timer = 0 }; return .{ .timer = 0 };
} }
pub fn reset(self: *Self) void {
self.timer = 0;
}
pub fn tick(self: *Self, enabled: bool, ch_enable: *bool) void { pub fn tick(self: *Self, enabled: bool, ch_enable: *bool) void {
if (enabled) { if (enabled) {
if (self.timer == 0) return; if (self.timer == 0) return;

View File

@ -18,13 +18,6 @@ pub fn create() Self {
}; };
} }
pub fn reset(self: *Self) void {
self.timer = 0;
self.enabled = false;
self.shadow = 0;
self.calc_performed = false;
}
pub fn tick(self: *Self, ch1: *ToneSweep) void { pub fn tick(self: *Self, ch1: *ToneSweep) void {
if (self.timer != 0) self.timer -= 1; if (self.timer != 0) self.timer -= 1;

View File

@ -19,11 +19,6 @@ pub fn create(sched: *Scheduler) Self {
}; };
} }
pub fn reset(self: *Self) void {
self.shift = 0;
self.timer = 0;
}
pub fn sample(self: *const Self) i8 { pub fn sample(self: *const Self) i8 {
return if ((~self.shift & 1) == 1) 1 else -1; return if ((~self.shift & 1) == 1) 1 else -1;
} }

View File

@ -20,11 +20,6 @@ pub fn init(sched: *Scheduler) Self {
}; };
} }
pub fn reset(self: *Self) void {
self.timer = 0;
self.pos = 0;
}
/// Scheduler Event Handler for Square Synth Timer Expire /// Scheduler Event Handler for Square Synth Timer Expire
pub fn onSquareTimerExpire(self: *Self, comptime T: type, nrx34: io.Frequency, late: u64) void { pub fn onSquareTimerExpire(self: *Self, comptime T: type, nrx34: io.Frequency, late: u64) void {
comptime std.debug.assert(T == ToneSweep or T == Tone); comptime std.debug.assert(T == ToneSweep or T == Tone);

View File

@ -38,13 +38,6 @@ pub fn init(sched: *Scheduler) Self {
}; };
} }
pub fn reset(self: *Self) void {
self.timer = 0;
self.offset = 0;
// sample buffer isn't reset because it's outside of the range of what NR52{7}'s effects
}
/// Reload internal Wave Timer /// Reload internal Wave Timer
pub fn reload(self: *Self, value: u11) void { pub fn reload(self: *Self, value: u11) void {
self.sched.removeScheduledEvent(.{ .ApuChannel = 2 }); self.sched.removeScheduledEvent(.{ .ApuChannel = 2 });

View File

@ -5,7 +5,7 @@ const DmaControl = @import("io.zig").DmaControl;
const Bus = @import("../Bus.zig"); const Bus = @import("../Bus.zig");
const Arm7tdmi = @import("../cpu.zig").Arm7tdmi; const Arm7tdmi = @import("../cpu.zig").Arm7tdmi;
pub const DmaTuple = struct { DmaController(0), DmaController(1), DmaController(2), DmaController(3) }; pub const DmaTuple = std.meta.Tuple(&[_]type{ DmaController(0), DmaController(1), DmaController(2), DmaController(3) });
const log = std.log.scoped(.DmaTransfer); const log = std.log.scoped(.DmaTransfer);
const getHalf = util.getHalf; const getHalf = util.getHalf;

View File

@ -5,7 +5,7 @@ const TimerControl = @import("io.zig").TimerControl;
const Scheduler = @import("../scheduler.zig").Scheduler; const Scheduler = @import("../scheduler.zig").Scheduler;
const Arm7tdmi = @import("../cpu.zig").Arm7tdmi; const Arm7tdmi = @import("../cpu.zig").Arm7tdmi;
pub const TimerTuple = struct { Timer(0), Timer(1), Timer(2), Timer(3) }; pub const TimerTuple = std.meta.Tuple(&[_]type{ Timer(0), Timer(1), Timer(2), Timer(3) });
const log = std.log.scoped(.Timer); const log = std.log.scoped(.Timer);
const getHalf = util.getHalf; const getHalf = util.getHalf;

View File

@ -87,7 +87,7 @@ pub fn main() void {
cpu.fastBoot(); cpu.fastBoot();
} }
var gui = Gui.init(&bus.pak.title, &bus.apu, width, height) catch |e| exitln("failed to init gui: {}", .{e}); var gui = Gui.init(&bus.pak.title, &bus.apu, width, height);
defer gui.deinit(); defer gui.deinit();
gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e});

View File

@ -15,7 +15,7 @@ const gba_height = @import("core/ppu.zig").height;
pub const sample_rate = 1 << 15; pub const sample_rate = 1 << 15;
pub const sample_format = SDL.AUDIO_U16; pub const sample_format = SDL.AUDIO_U16;
const default_title = "ZBA"; const default_title: []const u8 = "ZBA";
pub const Gui = struct { pub const Gui = struct {
const Self = @This(); const Self = @This();
@ -44,7 +44,7 @@ pub const Gui = struct {
program_id: gl.GLuint, program_id: gl.GLuint,
pub fn init(title: *const [12]u8, apu: *Apu, width: i32, height: i32) !Self { pub fn init(title: *const [12]u8, apu: *Apu, width: i32, height: i32) Self {
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic(); if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic();
@ -53,7 +53,7 @@ pub const Gui = struct {
const win_scale = @intCast(c_int, config.config().host.win_scale); const win_scale = @intCast(c_int, config.config().host.win_scale);
const window = SDL.SDL_CreateWindow( const window = SDL.SDL_CreateWindow(
default_title, default_title.ptr,
SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED,
SDL.SDL_WINDOWPOS_CENTERED, SDL.SDL_WINDOWPOS_CENTERED,
@as(c_int, width * win_scale), @as(c_int, width * win_scale),
@ -67,7 +67,7 @@ pub const Gui = struct {
gl.load(ctx, Self.glGetProcAddress) catch @panic("gl.load failed"); gl.load(ctx, Self.glGetProcAddress) catch @panic("gl.load failed");
if (SDL.SDL_GL_SetSwapInterval(@boolToInt(config.config().host.vsync)) < 0) panic(); if (SDL.SDL_GL_SetSwapInterval(@boolToInt(config.config().host.vsync)) < 0) panic();
const program_id = try compileShaders(); const program_id = compileShaders();
return Self{ return Self{
.window = window, .window = window,
@ -78,7 +78,7 @@ pub const Gui = struct {
}; };
} }
fn compileShaders() !gl.GLuint { fn compileShaders() gl.GLuint {
// TODO: Panic on Shader Compiler Failure + Error Message // TODO: Panic on Shader Compiler Failure + Error Message
const vert_shader = @embedFile("shader/pixelbuf.vert"); const vert_shader = @embedFile("shader/pixelbuf.vert");
const frag_shader = @embedFile("shader/pixelbuf.frag"); const frag_shader = @embedFile("shader/pixelbuf.frag");
@ -89,16 +89,12 @@ pub const Gui = struct {
gl.shaderSource(vs, 1, &[_][*c]const u8{vert_shader}, 0); gl.shaderSource(vs, 1, &[_][*c]const u8{vert_shader}, 0);
gl.compileShader(vs); gl.compileShader(vs);
if (!shader.didCompile(vs)) return error.VertexCompileError;
const fs = gl.createShader(gl.FRAGMENT_SHADER); const fs = gl.createShader(gl.FRAGMENT_SHADER);
defer gl.deleteShader(fs); defer gl.deleteShader(fs);
gl.shaderSource(fs, 1, &[_][*c]const u8{frag_shader}, 0); gl.shaderSource(fs, 1, &[_][*c]const u8{frag_shader}, 0);
gl.compileShader(fs); gl.compileShader(fs);
if (!shader.didCompile(fs)) return error.FragmentCompileError;
const program = gl.createProgram(); const program = gl.createProgram();
gl.attachShader(program, vs); gl.attachShader(program, vs);
gl.attachShader(program, fs); gl.attachShader(program, fs);
@ -108,7 +104,7 @@ pub const Gui = struct {
} }
// Returns the VAO ID since it's used in run() // Returns the VAO ID since it's used in run()
fn generateBuffers() struct { c_uint, c_uint, c_uint } { fn generateBuffers() [3]c_uint {
var vao_id: c_uint = undefined; var vao_id: c_uint = undefined;
var vbo_id: c_uint = undefined; var vbo_id: c_uint = undefined;
var ebo_id: c_uint = undefined; var ebo_id: c_uint = undefined;
@ -158,20 +154,13 @@ pub const Gui = struct {
var quit = std.atomic.Atomic(bool).init(false); var quit = std.atomic.Atomic(bool).init(false);
var tracker = FpsTracker.init(); var tracker = FpsTracker.init();
var buffer_ids = Self.generateBuffers();
defer {
gl.deleteBuffers(1, &buffer_ids[2]); // EBO
gl.deleteBuffers(1, &buffer_ids[1]); // VBO
gl.deleteVertexArrays(1, &buffer_ids[0]); // VAO
}
const vao_id = buffer_ids[0];
_ = Self.generateTexture(cpu.bus.ppu.framebuf.get(.Renderer));
const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, scheduler, cpu, &tracker }); const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, scheduler, cpu, &tracker });
defer thread.join(); defer thread.join();
var title_buf: [0x100]u8 = undefined; var title_buf: [0x100]u8 = [_]u8{0} ** 0x100;
const vao_id = Self.generateBuffers()[0];
_ = Self.generateTexture(cpu.bus.ppu.framebuf.get(.Renderer));
emu_loop: while (true) { emu_loop: while (true) {
var event: SDL.SDL_Event = undefined; var event: SDL.SDL_Event = undefined;
@ -241,7 +230,7 @@ pub const Gui = struct {
gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, null); gl.drawElements(gl.TRIANGLES, 6, gl.UNSIGNED_INT, null);
SDL.SDL_GL_SwapWindow(self.window); SDL.SDL_GL_SwapWindow(self.window);
const dyn_title = std.fmt.bufPrintZ(&title_buf, "ZBA | {s} [Emu: {}fps] ", .{ self.title, tracker.value() }) catch unreachable; const dyn_title = std.fmt.bufPrint(&title_buf, "ZBA | {s} [Emu: {}fps] ", .{ self.title, tracker.value() }) catch unreachable;
SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr);
} }
@ -307,28 +296,6 @@ const Audio = struct {
} }
}; };
const shader = struct {
const Kind = enum { vertex, fragment };
const log = std.log.scoped(.Shader);
fn didCompile(id: gl.GLuint) bool {
var success: gl.GLint = undefined;
gl.getShaderiv(id, gl.COMPILE_STATUS, &success);
if (success == 0) err(id);
return success == 1;
}
fn err(id: gl.GLuint) void {
const buf_len = 512;
var error_msg: [buf_len]u8 = undefined;
gl.getShaderInfoLog(id, buf_len, 0, &error_msg);
log.err("{s}", .{std.mem.sliceTo(&error_msg, 0)});
}
};
fn panic() noreturn { fn panic() noreturn {
const str = @as(?[*:0]const u8, SDL.SDL_GetError()) orelse "unknown error"; const str = @as(?[*:0]const u8, SDL.SDL_GetError()) orelse "unknown error";
@panic(std.mem.sliceTo(str, 0)); @panic(std.mem.sliceTo(str, 0));

View File

@ -181,7 +181,7 @@ pub const Logger = struct {
} }
}; };
const FmtArgTuple = struct { u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 }; const FmtArgTuple = std.meta.Tuple(&.{ u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 });
pub const audio = struct { pub const audio = struct {
const _io = @import("core/bus/io.zig"); const _io = @import("core/bus/io.zig");