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 2afd6dbf9e
7 changed files with 219 additions and 123 deletions

View File

@ -14,8 +14,8 @@
.hash = "12202fa30d679d821292bcd953458b9e76097a5d16999489125a206db63a53392833", .hash = "12202fa30d679d821292bcd953458b9e76097a5d16999489125a206db63a53392833",
}, },
.@"zba-gdbstub" = .{ .@"zba-gdbstub" = .{
.url = "https://git.musuka.dev/paoda/zba-gdbstub/archive/8d2c76e410357166f092008ff5c7f7f097a043a7.tar.gz", .url = "https://git.musuka.dev/paoda/zba-gdbstub/archive/7ae72ed5a892d2fc6cc3f5511e2b96134d928b59.tar.gz",
.hash = "1220a171c7e14769386e3f4aab6aed53b5e176d654bdf1428a15b972effed750bf30", .hash = "1220823e961f369e22b62edc1b4da3742af0a7cb420ae9a52ec33216ff5a8ef270c8",
}, },
.arm32 = .{ .arm32 = .{
.url = "https://git.musuka.dev/paoda/arm32/archive/6f0e27136072610e6dba97ff8aaf5e2ec86e2c09.tar.gz", .url = "https://git.musuka.dev/paoda/arm32/archive/6f0e27136072610e6dba97ff8aaf5e2ec86e2c09.tar.gz",

View File

@ -2,6 +2,7 @@ const std = @import("std");
const Header = @import("cartridge.zig").Header; const Header = @import("cartridge.zig").Header;
const Scheduler = @import("Scheduler.zig"); const Scheduler = @import("Scheduler.zig");
const Ui = @import("../platform.zig").Ui;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -404,6 +405,16 @@ pub fn fastBoot(system: System) void {
} }
} }
pub const Sync = struct {
const Atomic = std.atomic.Value;
should_quit: Atomic(bool) = Atomic(bool).init(false),
pub fn init(self: *Sync) void {
self.* = .{};
}
};
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;
@ -457,6 +468,8 @@ pub const debug = struct {
system: System, system: System,
scheduler: *Scheduler, scheduler: *Scheduler,
tick: u64 = 0,
pub fn init(system: System, scheduler: *Scheduler) @This() { pub fn init(system: System, scheduler: *Scheduler) @This() {
return .{ .system = system, .scheduler = scheduler }; return .{ .system = system, .scheduler = scheduler };
} }
@ -516,12 +529,20 @@ pub const debug = struct {
inline else => |halt| { inline else => |halt| {
if (!dma9.step(system.arm946es) and comptime halt != .arm9) { if (!dma9.step(system.arm946es) and comptime halt != .arm9) {
system.arm946es.step(); system.arm946es.step();
system.arm946es.step();
switch (proc) {
.nds9 => did_step = true,
.nds7 => system.arm946es.step(),
}
} }
if (!dma7.step(system.arm7tdmi) and comptime halt != .arm7) { if (!dma7.step(system.arm7tdmi) and comptime halt != .arm7) {
system.arm7tdmi.step(); if (proc == .nds7 or self.tick % 2 == 0) system.arm7tdmi.step();
if (proc == .nds7) {
did_step = true; did_step = true;
self.tick += 1;
}
} }
}, },
} }
@ -535,8 +556,8 @@ pub const debug = struct {
}; };
} }
pub fn run(allocator: Allocator, system: System, scheduler: *Scheduler, should_quit: *AtomicBool) !void { pub fn run(allocator: Allocator, ui: *Ui, scheduler: *Scheduler, system: System, sync: *Sync) !void {
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 +565,9 @@ 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 }); const thread = try std.Thread.spawn(.{}, Server.run, .{ &server, allocator, &sync.should_quit });
defer thread.join(); defer thread.join();
try ui.debug_run(scheduler, system, sync);
} }
}; };

View File

@ -71,7 +71,7 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T
} }
return switch (aligned_addr) { return switch (aligned_addr) {
0x0000_0000...0x01FF_FFFF => self.bios.read(T, address), 0x0000_0000...0x01FF_FFFF => self.bios.read(T, aligned_addr),
0x0200_0000...0x02FF_FFFF => std.mem.readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], .little), 0x0200_0000...0x02FF_FFFF => std.mem.readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], .little),
0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) { 0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) {
0b00 => std.mem.readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], .little), 0b00 => std.mem.readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], .little),
@ -105,7 +105,7 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v
} }
switch (aligned_addr) { switch (aligned_addr) {
0x0000_0000...0x01FF_FFFF => self.bios.write(T, address, value), 0x0000_0000...0x01FF_FFFF => self.bios.write(T, aligned_addr, value),
0x0200_0000...0x02FF_FFFF => std.mem.writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], value, .little), 0x0200_0000...0x02FF_FFFF => std.mem.writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], value, .little),
0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) { 0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) {
0b00 => std.mem.writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value, .little), 0b00 => std.mem.writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value, .little),

View File

@ -81,7 +81,7 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T
0x0500_0000...0x05FF_FFFF => std.mem.readInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], .little), 0x0500_0000...0x05FF_FFFF => std.mem.readInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], .little),
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(T, .nds9, aligned_addr), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(T, .nds9, aligned_addr),
0x0700_0000...0x07FF_FFFF => std.mem.readInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..byte_count], .little), 0x0700_0000...0x07FF_FFFF => std.mem.readInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..byte_count], .little),
0xFFFF_0000...0xFFFF_FFFF => self.bios.read(T, address), 0xFFFF_0000...0xFFFF_FFFF => self.bios.read(T, aligned_addr),
else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }), else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
}; };
} }
@ -111,7 +111,7 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v
0x0500_0000...0x05FF_FFFF => std.mem.writeInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little), 0x0500_0000...0x05FF_FFFF => std.mem.writeInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little),
0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(T, .nds9, aligned_addr, value), 0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(T, .nds9, aligned_addr, value),
0x0700_0000...0x07FF_FFFF => std.mem.writeInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little), 0x0700_0000...0x07FF_FFFF => std.mem.writeInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little),
0xFFFF_0000...0xFFFF_FFFF => self.bios.write(T, address, value), 0xFFFF_0000...0xFFFF_FFFF => self.bios.write(T, aligned_addr, value),
else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }), else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
} }
} }

View File

@ -6,6 +6,7 @@ const emu = @import("core/emu.zig");
const Ui = @import("platform.zig").Ui; const Ui = @import("platform.zig").Ui;
const SharedCtx = @import("core/emu.zig").SharedCtx; const SharedCtx = @import("core/emu.zig").SharedCtx;
const System = @import("core/emu.zig").System; const System = @import("core/emu.zig").System;
const Sync = @import("core/emu.zig").Sync;
const Scheduler = @import("core/Scheduler.zig"); const Scheduler = @import("core/Scheduler.zig");
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
@ -68,16 +69,20 @@ 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(Sync);
defer allocator.destroy(sync);
sync.init();
if (result.args.gdb == 0) {
try ui.run(&scheduler, system, sync);
} else {
try emu.debug.run(allocator, &ui, &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,105 @@ 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 setTitle(self: *@This(), title: [12]u8) void {
// TODO: Sort this out please self.state.title = title ++ [_:0]u8{};
}
const vao_id = opengl_impl.vao(); pub fn run(self: *Self, scheduler: *Scheduler, system: System, sync: *Sync) !void {
defer gl.deleteVertexArrays(1, &[_]GLuint{vao_id}); const id = try opengl_impl.runInit(&system.bus9.ppu.fb);
defer id.deinit();
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 => {
@ -178,42 +244,6 @@ pub const Ui = struct {
else => {}, else => {},
} }
} }
{
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 {
self.state.title = title ++ [_:0]u8{};
}
}; };
fn panic() noreturn { fn panic() noreturn {
@ -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;
} }