fix: create and draw ui even when running in gdb mode

This commit is contained in:
Rekai Nyangadzayi Musuka 2024-02-18 01:12:42 -06:00
parent 422e0d00b8
commit db0cb073e7
4 changed files with 194 additions and 116 deletions

View File

@ -404,6 +404,12 @@ pub fn fastBoot(system: System) void {
} }
} }
pub const Sync = struct {
const Atomic = std.atomic.Value;
should_quit: Atomic(bool) = Atomic(bool).init(false),
};
pub const debug = struct { pub const debug = struct {
const Interface = @import("gdbstub").Emulator; const Interface = @import("gdbstub").Emulator;
const Server = @import("gdbstub").Server; const Server = @import("gdbstub").Server;
@ -535,8 +541,8 @@ pub const debug = struct {
}; };
} }
pub fn run(allocator: Allocator, system: System, scheduler: *Scheduler, should_quit: *AtomicBool) !void { pub fn run(allocator: Allocator, scheduler: *Scheduler, system: System, sync: *Sync) !std.Thread {
var wrapper = Wrapper(.nds7).init(system, scheduler); var wrapper = Wrapper(.nds9).init(system, scheduler);
var emu_interface = wrapper.interface(allocator); var emu_interface = wrapper.interface(allocator);
defer emu_interface.deinit(); defer emu_interface.deinit();
@ -544,7 +550,6 @@ pub const debug = struct {
var server = try Server.init(emu_interface, .{ .target = nds7.target, .memory_map = nds7.memory_map }); var server = try Server.init(emu_interface, .{ .target = nds7.target, .memory_map = nds7.memory_map });
defer server.deinit(allocator); defer server.deinit(allocator);
const thread = try std.Thread.spawn(.{}, Server.run, .{ &server, allocator, should_quit }); return try std.Thread.spawn(.{}, Server.run, .{ &server, allocator, &sync.should_quit });
defer thread.join();
} }
}; };

View File

@ -68,16 +68,21 @@ pub fn main() !void {
emu.fastBoot(system); emu.fastBoot(system);
if (result.args.gdb == 0) {
var ui = try Ui.init(allocator); var ui = try Ui.init(allocator);
defer ui.deinit(allocator); defer ui.deinit(allocator);
ui.setTitle(rom_title); ui.setTitle(rom_title);
try ui.run(&scheduler, system);
} else {
var should_quit: std.atomic.Value(bool) = std.atomic.Value(bool).init(false);
try emu.debug.run(allocator, system, &scheduler, &should_quit); const sync = try allocator.create(emu.Sync);
defer allocator.destroy(sync);
if (result.args.gdb == 0) {
try ui.run(&scheduler, system, sync);
} else {
const thread = try emu.debug.run(allocator, &scheduler, system, sync);
defer thread.join();
try ui.debug_run(&scheduler, system, sync);
} }
} }

View File

@ -8,9 +8,11 @@ const imgui = @import("ui/imgui.zig");
const emu = @import("core/emu.zig"); const emu = @import("core/emu.zig");
const System = @import("core/emu.zig").System; const System = @import("core/emu.zig").System;
const Sync = @import("core/emu.zig").Sync;
const KeyInput = @import("core/io.zig").KeyInput; const KeyInput = @import("core/io.zig").KeyInput;
const ExtKeyIn = @import("core/io.zig").ExtKeyIn; const ExtKeyIn = @import("core/io.zig").ExtKeyIn;
const Scheduler = @import("core/Scheduler.zig"); const Scheduler = @import("core/Scheduler.zig");
const FrameBuffer = @import("core/ppu.zig").FrameBuffer;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -86,41 +88,101 @@ pub const Ui = struct {
return SDL.SDL_GL_GetProcAddress(proc.ptr); return SDL.SDL_GL_GetProcAddress(proc.ptr);
} }
pub fn run(self: *Self, scheduler: *Scheduler, system: System) !void { pub fn run(self: *Self, scheduler: *Scheduler, system: System, sync: *Sync) !void {
// TODO: Sort this out please const id = try opengl_impl.runInit(&system.bus9.ppu.fb);
defer id.deinit();
const vao_id = opengl_impl.vao();
defer gl.deleteVertexArrays(1, &[_]GLuint{vao_id});
const top_tex = opengl_impl.screenTex(system.bus9.ppu.fb.top(.front));
const btm_tex = opengl_impl.screenTex(system.bus9.ppu.fb.btm(.front));
const top_out_tex = opengl_impl.outTex();
const btm_out_tex = opengl_impl.outTex();
defer gl.deleteTextures(4, &[_]GLuint{ top_tex, top_out_tex, btm_tex, btm_out_tex });
const top_fbo = try opengl_impl.frameBuffer(top_out_tex);
const btm_fbo = try opengl_impl.frameBuffer(btm_out_tex);
defer gl.deleteFramebuffers(2, &[_]GLuint{ top_fbo, btm_fbo });
const prog_id = try opengl_impl.program();
defer gl.deleteProgram(prog_id);
var event: SDL.SDL_Event = undefined; var event: SDL.SDL_Event = undefined;
emu_loop: while (true) { while (!sync.should_quit.load(.Monotonic)) {
emu.runFrame(scheduler, system); emu.runFrame(scheduler, system); // TODO: run emu in separate thread
while (SDL.SDL_PollEvent(&event) != 0) { while (SDL.SDL_PollEvent(&event) != 0) {
_ = zgui.backend.processEvent(&event); _ = zgui.backend.processEvent(&event);
handleInput(&event, system, &self.state, sync);
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(id.top_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.top(.front));
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, id.btm_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(id.btm_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.btm(.front));
}
imgui.draw(&self.state, id.top_out_tex, id.btm_out_tex, system);
// Background Colour
const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw();
SDL.SDL_GL_SwapWindow(self.window);
}
}
pub fn debug_run(self: *Self, _: *Scheduler, system: System, sync: *Sync) !void {
const id = try opengl_impl.runInit(&system.bus9.ppu.fb);
defer id.deinit();
var event: SDL.SDL_Event = undefined;
while (!sync.should_quit.load(.Monotonic)) {
while (SDL.SDL_PollEvent(&event) != 0) {
_ = zgui.backend.processEvent(&event);
handleInput(&event, system, &self.state, sync);
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(id.top_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.top(.front));
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, id.btm_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(id.btm_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.btm(.front));
}
imgui.draw(&self.state, id.top_out_tex, id.btm_out_tex, system);
// Background Colour
const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw();
SDL.SDL_GL_SwapWindow(self.window);
}
}
fn handleInput(event: *SDL.SDL_Event, system: System, state: *imgui.State, sync: *Sync) void {
switch (event.type) { switch (event.type) {
SDL.SDL_QUIT => break :emu_loop, SDL.SDL_QUIT => sync.should_quit.store(true, .Monotonic),
SDL.SDL_WINDOWEVENT => { SDL.SDL_WINDOWEVENT => {
if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) { if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) {
std.log.debug("window resized to: {}x{}", .{ event.window.data1, event.window.data2 }); std.log.debug("window resized to: {}x{}", .{ event.window.data1, event.window.data2 });
self.state.dim.width = @intCast(event.window.data1); state.dim.width = @intCast(event.window.data1);
self.state.dim.height = @intCast(event.window.data2); state.dim.height = @intCast(event.window.data2);
zgui.io.setDisplaySize(@floatFromInt(event.window.data1), @floatFromInt(event.window.data2));
} }
}, },
SDL.SDL_KEYDOWN => { SDL.SDL_KEYDOWN => {
@ -179,38 +241,6 @@ pub const Ui = struct {
} }
} }
{
gl.bindFramebuffer(gl.FRAMEBUFFER, top_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(top_tex, prog_id, vao_id, system.bus9.ppu.fb.top(.front));
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, btm_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(btm_tex, prog_id, vao_id, system.bus9.ppu.fb.btm(.front));
}
const zgui_redraw = imgui.draw(&self.state, top_out_tex, btm_out_tex, system);
if (zgui_redraw) {
// Background Colour
const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw();
}
SDL.SDL_GL_SwapWindow(self.window);
}
}
pub fn setTitle(self: *@This(), title: [12]u8) void { pub fn setTitle(self: *@This(), title: [12]u8) void {
self.state.title = title ++ [_:0]u8{}; self.state.title = title ++ [_:0]u8{};
} }
@ -222,6 +252,46 @@ fn panic() noreturn {
} }
const opengl_impl = struct { const opengl_impl = struct {
const Ids = struct {
vao_id: GLuint,
top_tex: GLuint,
btm_tex: GLuint,
top_out_tex: GLuint,
btm_out_tex: GLuint,
top_fbo: GLuint,
btm_fbo: GLuint,
prog_id: GLuint,
fn deinit(self: Ids) void {
gl.deleteProgram(self.prog_id);
gl.deleteFramebuffers(2, &[_]GLuint{ self.top_fbo, self.btm_fbo });
gl.deleteTextures(4, &[_]GLuint{ self.top_tex, self.top_out_tex, self.btm_tex, self.btm_out_tex });
gl.deleteVertexArrays(1, &[_]GLuint{self.vao_id});
}
};
fn runInit(fb: *const FrameBuffer) !Ids {
const top_out_tex = opengl_impl.outTex();
const btm_out_tex = opengl_impl.outTex();
return .{
.vao_id = opengl_impl.vao(),
.top_tex = opengl_impl.screenTex(fb.top(.front)),
.btm_tex = opengl_impl.screenTex(fb.btm(.front)),
.top_out_tex = top_out_tex,
.btm_out_tex = btm_out_tex,
.top_fbo = try opengl_impl.frameBuffer(top_out_tex),
.btm_fbo = try opengl_impl.frameBuffer(btm_out_tex),
.prog_id = try opengl_impl.program(),
};
}
fn drawScreen(tex_id: GLuint, prog_id: GLuint, vao_id: GLuint, buf: []const u8) void { fn drawScreen(tex_id: GLuint, prog_id: GLuint, vao_id: GLuint, buf: []const u8) void {
gl.bindTexture(gl.TEXTURE_2D, tex_id); gl.bindTexture(gl.TEXTURE_2D, tex_id);
defer gl.bindTexture(gl.TEXTURE_2D, 0); defer gl.bindTexture(gl.TEXTURE_2D, 0);

View File

@ -18,7 +18,7 @@ pub const State = struct {
dim: Dimensions = .{ .width = 1600, .height = 900 }, dim: Dimensions = .{ .width = 1600, .height = 900 },
}; };
pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: System) bool { pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: System) void {
_ = system; _ = system;
zgui.backend.newFrame(@floatFromInt(state.dim.width), @floatFromInt(state.dim.height)); zgui.backend.newFrame(@floatFromInt(state.dim.width), @floatFromInt(state.dim.height));
@ -36,6 +36,4 @@ pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: Syste
zgui.image(@ptrFromInt(top_tex), .{ .w = w, .h = h }); zgui.image(@ptrFromInt(top_tex), .{ .w = w, .h = h });
zgui.image(@ptrFromInt(btm_tex), .{ .w = w, .h = h }); zgui.image(@ptrFromInt(btm_tex), .{ .w = w, .h = h });
} }
return true;
} }