Compare commits

..

4 Commits

4 changed files with 79 additions and 54 deletions

View File

@ -96,7 +96,7 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) ?T {
0x0400_0128 => util.io.read.todo(log, "Read {} from SIOCNT", .{T}), 0x0400_0128 => util.io.read.todo(log, "Read {} from SIOCNT", .{T}),
// Keypad Input // Keypad Input
0x0400_0130 => bus.io.keyinput.load(.Monotonic).raw, 0x0400_0130 => bus.io.keyinput.load(.Monotonic),
// Serial Communication 2 // Serial Communication 2
0x0400_0134 => util.io.read.todo(log, "Read {} from RCNT", .{T}), 0x0400_0134 => util.io.read.todo(log, "Read {} from RCNT", .{T}),
@ -366,7 +366,7 @@ const InterruptEnable = extern union {
/// Read Only /// Read Only
/// 0 = Pressed, 1 = Released /// 0 = Pressed, 1 = Released
const KeyInput = extern union { pub const KeyInput = extern union {
a: Bit(u16, 0), a: Bit(u16, 0),
b: Bit(u16, 1), b: Bit(u16, 1),
select: Bit(u16, 2), select: Bit(u16, 2),
@ -390,18 +390,19 @@ const AtomicKeyInput = struct {
return .{ .inner = value }; return .{ .inner = value };
} }
pub inline fn load(self: *const Self, comptime ordering: Ordering) KeyInput { pub inline fn load(self: *const Self, comptime ordering: Ordering) u16 {
return .{ .raw = switch (ordering) { return switch (ordering) {
.AcqRel, .Release => @compileError("not supported for atomic loads"), .AcqRel, .Release => @compileError("not supported for atomic loads"),
else => @atomicLoad(u16, &self.inner.raw, ordering), else => @atomicLoad(u16, &self.inner.raw, ordering),
} }; };
} }
pub inline fn store(self: *Self, value: u16, comptime ordering: Ordering) void { pub inline fn fetchOr(self: *Self, value: u16, comptime ordering: Ordering) void {
switch (ordering) { _ = @atomicRmw(u16, &self.inner.raw, .Or, value, ordering);
.AcqRel, .Acquire => @compileError("not supported for atomic stores"),
else => @atomicStore(u16, &self.inner.raw, value, ordering),
} }
pub inline fn fetchAnd(self: *Self, value: u16, comptime ordering: Ordering) void {
_ = @atomicRmw(u16, &self.inner.raw, .And, value, ordering);
} }
}; };

View File

@ -140,6 +140,10 @@ fn audioSync(audio_sync: bool, stream: *SDL.SDL_AudioStream, is_buffer_full: *bo
// If Busy is false, there's no need to sync here // If Busy is false, there's no need to sync here
if (!still_full) return; if (!still_full) return;
// TODO: Refactor!!!!
// while (SDL.SDL_AudioStreamAvailable(stream) > sample_size * max_buf_size >> 1)
// std.atomic.spinLoopHint();
while (true) { while (true) {
still_full = SDL.SDL_AudioStreamAvailable(stream) > sample_size * max_buf_size >> 1; still_full = SDL.SDL_AudioStreamAvailable(stream) > sample_size * max_buf_size >> 1;
if (!audio_sync or !still_full) break; if (!audio_sync or !still_full) break;
@ -184,7 +188,8 @@ fn sleep(timer: *Timer, wake_time: u64) ?u64 {
} }
fn spinLoop(timer: *Timer, wake_time: u64) void { fn spinLoop(timer: *Timer, wake_time: u64) void {
while (true) if (timer.read() > wake_time) break; while (timer.read() < wake_time)
std.atomic.spinLoopHint();
} }
pub const EmuThing = struct { pub const EmuThing = struct {

View File

@ -12,6 +12,7 @@ const emu = @import("core/emu.zig");
const Gui = @import("platform.zig").Gui; const Gui = @import("platform.zig").Gui;
const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi; const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi;
const RingBuffer = @import("zba-util").RingBuffer; const RingBuffer = @import("zba-util").RingBuffer;
const Dimensions = @import("platform.zig").Dimensions;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const GLuint = gl.GLuint; const GLuint = gl.GLuint;
@ -66,8 +67,10 @@ pub const State = struct {
} }
}; };
pub fn draw(state: *State, tex_id: GLuint, cpu: *Arm7tdmi) void { pub fn draw(state: *State, win_dim: Dimensions, tex_id: GLuint, cpu: *Arm7tdmi) bool {
const win_scale = config.config().host.win_scale; const scn_scale = config.config().host.win_scale;
zgui.backend.newFrame(@intToFloat(f32, win_dim.width), @intToFloat(f32, win_dim.height));
{ {
_ = zgui.beginMainMenuBar(); _ = zgui.beginMainMenuBar();
@ -142,8 +145,8 @@ pub fn draw(state: *State, tex_id: GLuint, cpu: *Arm7tdmi) void {
} }
{ {
const w = @intToFloat(f32, gba_width * win_scale); const w = @intToFloat(f32, gba_width * scn_scale);
const h = @intToFloat(f32, gba_height * win_scale); const h = @intToFloat(f32, gba_height * scn_scale);
const window_title = std.mem.sliceTo(&state.title, 0); const window_title = std.mem.sliceTo(&state.title, 0);
_ = zgui.begin(window_title, .{ .flags = .{ .no_resize = true, .always_auto_resize = true } }); _ = zgui.begin(window_title, .{ .flags = .{ .no_resize = true, .always_auto_resize = true } });
@ -318,6 +321,8 @@ pub fn draw(state: *State, tex_id: GLuint, cpu: *Arm7tdmi) void {
{ {
zgui.showDemoWindow(null); zgui.showDemoWindow(null);
} }
return true; // request redraw
} }
const widgets = struct { const widgets = struct {

View File

@ -12,6 +12,7 @@ const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi;
const Scheduler = @import("core/scheduler.zig").Scheduler; const Scheduler = @import("core/scheduler.zig").Scheduler;
const FpsTracker = @import("util.zig").FpsTracker; const FpsTracker = @import("util.zig").FpsTracker;
const TwoWayChannel = @import("zba-util").TwoWayChannel; const TwoWayChannel = @import("zba-util").TwoWayChannel;
const KeyInput = @import("core/bus/io.zig").KeyInput;
const gba_width = @import("core/ppu.zig").width; const gba_width = @import("core/ppu.zig").width;
const gba_height = @import("core/ppu.zig").height; const gba_height = @import("core/ppu.zig").height;
@ -21,7 +22,7 @@ const GLsizei = gl.GLsizei;
const SDL_GLContext = *anyopaque; const SDL_GLContext = *anyopaque;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const Dimensions = struct { width: u32, height: u32 }; pub const Dimensions = struct { width: u32, height: u32 };
const default_dim: Dimensions = .{ .width = 1280, .height = 720 }; const default_dim: Dimensions = .{ .width = 1280, .height = 720 };
pub const sample_rate = 1 << 15; pub const sample_rate = 1 << 15;
@ -134,28 +135,9 @@ pub const Gui = struct {
switch (event.type) { switch (event.type) {
SDL.SDL_QUIT => break :emu_loop, SDL.SDL_QUIT => break :emu_loop,
SDL.SDL_KEYDOWN => { SDL.SDL_KEYDOWN => {
// TODO: Make use of compare_and_xor?
const key_code = event.key.keysym.sym; const key_code = event.key.keysym.sym;
var keyinput = cpu.bus.io.keyinput.load(.Monotonic); var keyinput: KeyInput = .{ .raw = 0x0000 };
switch (key_code) {
SDL.SDLK_UP => keyinput.up.unset(),
SDL.SDLK_DOWN => keyinput.down.unset(),
SDL.SDLK_LEFT => keyinput.left.unset(),
SDL.SDLK_RIGHT => keyinput.right.unset(),
SDL.SDLK_x => keyinput.a.unset(),
SDL.SDLK_z => keyinput.b.unset(),
SDL.SDLK_a => keyinput.shoulder_l.unset(),
SDL.SDLK_s => keyinput.shoulder_r.unset(),
SDL.SDLK_RETURN => keyinput.start.unset(),
SDL.SDLK_RSHIFT => keyinput.select.unset(),
else => {},
}
cpu.bus.io.keyinput.store(keyinput.raw, .Monotonic);
},
SDL.SDL_KEYUP => {
const key_code = event.key.keysym.sym;
var keyinput = cpu.bus.io.keyinput.load(.Monotonic);
switch (key_code) { switch (key_code) {
SDL.SDLK_UP => keyinput.up.set(), SDL.SDLK_UP => keyinput.up.set(),
@ -171,7 +153,28 @@ pub const Gui = struct {
else => {}, else => {},
} }
cpu.bus.io.keyinput.store(keyinput.raw, .Monotonic); cpu.bus.io.keyinput.fetchAnd(~keyinput.raw, .Monotonic);
},
SDL.SDL_KEYUP => {
// TODO: Make use of compare_and_xor?
const key_code = event.key.keysym.sym;
var keyinput: KeyInput = .{ .raw = 0x0000 };
switch (key_code) {
SDL.SDLK_UP => keyinput.up.set(),
SDL.SDLK_DOWN => keyinput.down.set(),
SDL.SDLK_LEFT => keyinput.left.set(),
SDL.SDLK_RIGHT => keyinput.right.set(),
SDL.SDLK_x => keyinput.a.set(),
SDL.SDLK_z => keyinput.b.set(),
SDL.SDLK_a => keyinput.shoulder_l.set(),
SDL.SDLK_s => keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => keyinput.start.set(),
SDL.SDLK_RSHIFT => keyinput.select.set(),
else => {},
}
cpu.bus.io.keyinput.fetchOr(keyinput.raw, .Monotonic);
}, },
SDL.SDL_WINDOWEVENT => { SDL.SDL_WINDOWEVENT => {
if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) { if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) {
@ -185,7 +188,7 @@ pub const Gui = struct {
} }
} }
zgui.backend.newFrame(@intToFloat(f32, win_dim.width), @intToFloat(f32, win_dim.height)); var zgui_redraw: bool = false;
switch (self.state.emulation) { switch (self.state.emulation) {
.Transition => |inner| switch (inner) { .Transition => |inner| switch (inner) {
@ -204,19 +207,28 @@ pub const Gui = struct {
self.state.emulation = .Inactive; self.state.emulation = .Inactive;
}, },
}, },
.Active => { .Active => skip_draw: {
const is_std = mode == .Standard; const is_std = mode == .Standard;
if (is_std) channel.emu.push(.Pause); if (is_std) channel.emu.push(.Pause);
defer if (is_std) channel.emu.push(.Resume); defer if (is_std) channel.emu.push(.Resume);
switch (mode) { switch (mode) {
.Standard => { .Standard => blk: {
// TODO: add timeout const limit = 15; // TODO: What should this be?
while (true) switch (channel.gui.pop() orelse continue) {
.Paused => break, // TODO: learn more about std.atomic.spinLoopHint();
.Quit => unreachable, // only signaled in debug mode for (0..limit) |_| {
}; const message = channel.gui.pop() orelse continue;
switch (message) {
.Paused => break :blk,
.Quit => unreachable,
}
}
log.info("timed out waiting for emu thread to pause (limit: {})", .{limit});
break :skip_draw;
}, },
.Debug => blk: { .Debug => blk: {
switch (channel.gui.pop() orelse break :blk) { switch (channel.gui.pop() orelse break :blk) {
@ -239,11 +251,12 @@ pub const Gui = struct {
opengl_impl.drawScreenTexture(emu_tex, prog_id, objects, buf); opengl_impl.drawScreenTexture(emu_tex, prog_id, objects, buf);
} }
imgui.draw(&self.state, out_tex, cpu); zgui_redraw = imgui.draw(&self.state, win_dim, out_tex, cpu);
}, },
.Inactive => imgui.draw(&self.state, out_tex, cpu), .Inactive => zgui_redraw = imgui.draw(&self.state, win_dim, out_tex, cpu),
} }
if (zgui_redraw) {
// Background Colour // Background Colour
const size = zgui.io.getDisplaySize(); const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @floatToInt(GLsizei, size[0]), @floatToInt(GLsizei, size[1])); gl.viewport(0, 0, @floatToInt(GLsizei, size[0]), @floatToInt(GLsizei, size[1]));
@ -251,6 +264,7 @@ pub const Gui = struct {
gl.clear(gl.COLOR_BUFFER_BIT); gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw(); zgui.backend.draw();
}
SDL.SDL_GL_SwapWindow(self.window); SDL.SDL_GL_SwapWindow(self.window);
} }