Compare commits
	
		
			8 Commits
		
	
	
		
			april-fool
			...
			5f3be3f2f5
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 5f3be3f2f5 | |||
| 76b3b4f582 | |||
| 7e9a3efacc | |||
| b2ce00a40c | |||
| 26e36b2ea2 | |||
| 45d304f3cd | |||
| bd506b4fe3 | |||
| 197be61186 | 
							
								
								
									
										3
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							| @@ -13,3 +13,6 @@ | |||||||
| [submodule "lib/zig-toml"] | [submodule "lib/zig-toml"] | ||||||
| 	path = lib/zig-toml | 	path = lib/zig-toml | ||||||
| 	url = https://github.com/aeronavery/zig-toml | 	url = https://github.com/aeronavery/zig-toml | ||||||
|  | [submodule "lib/zba-gdbstub"] | ||||||
|  | 	path = lib/zba-gdbstub | ||||||
|  | 	url = https://git.musuka.dev/paoda/zba-gdbstub | ||||||
|   | |||||||
| @@ -1,6 +1,8 @@ | |||||||
| const std = @import("std"); | const std = @import("std"); | ||||||
| const builtin = @import("builtin"); | const builtin = @import("builtin"); | ||||||
|  |  | ||||||
| const Sdk = @import("lib/SDL.zig/Sdk.zig"); | const Sdk = @import("lib/SDL.zig/Sdk.zig"); | ||||||
|  | const Gdbstub = @import("lib/zba-gdbstub/build.zig"); | ||||||
|  |  | ||||||
| pub fn build(b: *std.build.Builder) void { | pub fn build(b: *std.build.Builder) void { | ||||||
|     // Minimum Zig Version |     // Minimum Zig Version | ||||||
| @@ -40,6 +42,9 @@ pub fn build(b: *std.build.Builder) void { | |||||||
|     // OpenGL 3.3 Bindings |     // OpenGL 3.3 Bindings | ||||||
|     exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } }); |     exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } }); | ||||||
|  |  | ||||||
|  |     // gdbstub | ||||||
|  |     Gdbstub.link(exe); | ||||||
|  |  | ||||||
|     // Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig |     // Zig SDL Bindings: https://github.com/MasterQ32/SDL.zig | ||||||
|     const sdk = Sdk.init(b, null); |     const sdk = Sdk.init(b, null); | ||||||
|     sdk.link(exe, .dynamic); |     sdk.link(exe, .dynamic); | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								lib/zba-gdbstub
									
									
									
									
									
										Submodule
									
								
							
							
								
								
								
								
								
							
						
						
									
										1
									
								
								lib/zba-gdbstub
									
									
									
									
									
										Submodule
									
								
							 Submodule lib/zba-gdbstub added at 6d6a109a08
									
								
							
							
								
								
									
										148
									
								
								src/core/Bus.zig
									
									
									
									
									
								
							
							
						
						
									
										148
									
								
								src/core/Bus.zig
									
									
									
									
									
								
							| @@ -198,55 +198,6 @@ fn fillReadTableExternal(self: *Self, addr: u32) ?*anyopaque { | |||||||
|     return &self.pak.buf[masked_addr]; |     return &self.pak.buf[masked_addr]; | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn dbgRead(self: *const Self, comptime T: type, unaligned_address: u32) T { |  | ||||||
|     const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; |  | ||||||
|     const page = unaligned_address >> bits; |  | ||||||
|     const offset = unaligned_address & (page_size - 1); |  | ||||||
|  |  | ||||||
|     // We're doing some serious out-of-bounds open-bus reads |  | ||||||
|     if (page >= table_len) return self.openBus(T, unaligned_address); |  | ||||||
|  |  | ||||||
|     if (self.read_table[page]) |some_ptr| { |  | ||||||
|         // We have a pointer to a page, cast the pointer to it's underlying type |  | ||||||
|         const Ptr = [*]const T; |  | ||||||
|         const ptr = @ptrCast(Ptr, @alignCast(@alignOf(std.meta.Child(Ptr)), some_ptr)); |  | ||||||
|  |  | ||||||
|         // Note: We don't check array length, since we force align the |  | ||||||
|         // lower bits of the address as the GBA would |  | ||||||
|         return ptr[forceAlign(T, offset) / @sizeOf(T)]; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     return self.dbgSlowRead(T, unaligned_address); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| fn dbgSlowRead(self: *const Self, comptime T: type, unaligned_address: u32) T { |  | ||||||
|     const page = @truncate(u8, unaligned_address >> 24); |  | ||||||
|     const address = forceAlign(T, unaligned_address); |  | ||||||
|  |  | ||||||
|     return switch (page) { |  | ||||||
|         // General Internal Memory |  | ||||||
|         0x00 => blk: { |  | ||||||
|             if (address < Bios.size) |  | ||||||
|                 break :blk self.bios.dbgRead(T, self.cpu.r[15], unaligned_address); |  | ||||||
|  |  | ||||||
|             break :blk self.openBus(T, address); |  | ||||||
|         }, |  | ||||||
|         0x02 => unreachable, // handled by fastmem |  | ||||||
|         0x03 => unreachable, // handled by fastmem |  | ||||||
|         0x04 => self.readIo(T, address), |  | ||||||
|  |  | ||||||
|         // Internal Display Memory |  | ||||||
|         0x05 => unreachable, // handled by fastmem |  | ||||||
|         0x06 => unreachable, // handled by fastmem |  | ||||||
|         0x07 => unreachable, // handled by fastmem |  | ||||||
|  |  | ||||||
|         // External Memory (Game Pak) |  | ||||||
|         0x08...0x0D => self.pak.dbgRead(T, address), |  | ||||||
|         0x0E...0x0F => self.readBackup(T, unaligned_address), |  | ||||||
|         else => self.openBus(T, address), |  | ||||||
|     }; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| fn readIo(self: *const Self, comptime T: type, address: u32) T { | fn readIo(self: *const Self, comptime T: type, address: u32) T { | ||||||
|     return io.read(self, T, address) orelse self.openBus(T, address); |     return io.read(self, T, address) orelse self.openBus(T, address); | ||||||
| } | } | ||||||
| @@ -336,6 +287,27 @@ pub fn read(self: *Self, comptime T: type, unaligned_address: u32) T { | |||||||
|     return self.slowRead(T, unaligned_address); |     return self.slowRead(T, unaligned_address); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | pub fn dbgRead(self: *const Self, comptime T: type, unaligned_address: u32) T { | ||||||
|  |     const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; | ||||||
|  |     const page = unaligned_address >> bits; | ||||||
|  |     const offset = unaligned_address & (page_size - 1); | ||||||
|  |  | ||||||
|  |     // We're doing some serious out-of-bounds open-bus reads | ||||||
|  |     if (page >= table_len) return self.openBus(T, unaligned_address); | ||||||
|  |  | ||||||
|  |     if (self.read_table[page]) |some_ptr| { | ||||||
|  |         // We have a pointer to a page, cast the pointer to it's underlying type | ||||||
|  |         const Ptr = [*]const T; | ||||||
|  |         const ptr = @ptrCast(Ptr, @alignCast(@alignOf(std.meta.Child(Ptr)), some_ptr)); | ||||||
|  |  | ||||||
|  |         // Note: We don't check array length, since we force align the | ||||||
|  |         // lower bits of the address as the GBA would | ||||||
|  |         return ptr[forceAlign(T, offset) / @sizeOf(T)]; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     return self.dbgSlowRead(T, unaligned_address); | ||||||
|  | } | ||||||
|  |  | ||||||
| fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T { | fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T { | ||||||
|     @setCold(true); |     @setCold(true); | ||||||
|  |  | ||||||
| @@ -366,6 +338,34 @@ fn slowRead(self: *Self, comptime T: type, unaligned_address: u32) T { | |||||||
|     }; |     }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | fn dbgSlowRead(self: *const Self, comptime T: type, unaligned_address: u32) T { | ||||||
|  |     const page = @truncate(u8, unaligned_address >> 24); | ||||||
|  |     const address = forceAlign(T, unaligned_address); | ||||||
|  |  | ||||||
|  |     return switch (page) { | ||||||
|  |         // General Internal Memory | ||||||
|  |         0x00 => blk: { | ||||||
|  |             if (address < Bios.size) | ||||||
|  |                 break :blk self.bios.dbgRead(T, self.cpu.r[15], unaligned_address); | ||||||
|  |  | ||||||
|  |             break :blk self.openBus(T, address); | ||||||
|  |         }, | ||||||
|  |         0x02 => unreachable, // handled by fastmem | ||||||
|  |         0x03 => unreachable, // handled by fastmem | ||||||
|  |         0x04 => self.readIo(T, address), | ||||||
|  |  | ||||||
|  |         // Internal Display Memory | ||||||
|  |         0x05 => unreachable, // handled by fastmem | ||||||
|  |         0x06 => unreachable, // handled by fastmem | ||||||
|  |         0x07 => unreachable, // handled by fastmem | ||||||
|  |  | ||||||
|  |         // External Memory (Game Pak) | ||||||
|  |         0x08...0x0D => self.pak.dbgRead(T, address), | ||||||
|  |         0x0E...0x0F => self.readBackup(T, unaligned_address), | ||||||
|  |         else => self.openBus(T, address), | ||||||
|  |     }; | ||||||
|  | } | ||||||
|  |  | ||||||
| fn readBackup(self: *const Self, comptime T: type, unaligned_address: u32) T { | fn readBackup(self: *const Self, comptime T: type, unaligned_address: u32) T { | ||||||
|     const value = self.pak.backup.read(unaligned_address); |     const value = self.pak.backup.read(unaligned_address); | ||||||
|  |  | ||||||
| @@ -406,6 +406,31 @@ pub fn write(self: *Self, comptime T: type, unaligned_address: u32, value: T) vo | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /// Mostly Identical to `Bus.write`, slowmeme is handled by `Bus.dbgSlowWrite` | ||||||
|  | pub fn dbgWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { | ||||||
|  |     const bits = @typeInfo(std.math.IntFittingRange(0, page_size - 1)).Int.bits; | ||||||
|  |     const page = unaligned_address >> bits; | ||||||
|  |     const offset = unaligned_address & (page_size - 1); | ||||||
|  |  | ||||||
|  |     // We're doing some serious out-of-bounds open-bus writes, they do nothing though | ||||||
|  |     if (page >= table_len) return; | ||||||
|  |  | ||||||
|  |     if (self.write_tables[@boolToInt(T == u8)][page]) |some_ptr| { | ||||||
|  |         // We have a pointer to a page, cast the pointer to it's underlying type | ||||||
|  |         const Ptr = [*]T; | ||||||
|  |         const ptr = @ptrCast(Ptr, @alignCast(@alignOf(std.meta.Child(Ptr)), some_ptr)); | ||||||
|  |  | ||||||
|  |         // Note: We don't check array length, since we force align the | ||||||
|  |         // lower bits of the address as the GBA would | ||||||
|  |         ptr[forceAlign(T, offset) / @sizeOf(T)] = value; | ||||||
|  |     } else { | ||||||
|  |         // we can return early if this is an 8-bit OAM write | ||||||
|  |         if (T == u8 and @truncate(u8, unaligned_address >> 24) == 0x07) return; | ||||||
|  |  | ||||||
|  |         self.dbgSlowWrite(T, unaligned_address, value); | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| fn slowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { | fn slowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { | ||||||
|     @setCold(true); |     @setCold(true); | ||||||
|  |  | ||||||
| @@ -431,6 +456,31 @@ fn slowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) vo | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | fn dbgSlowWrite(self: *Self, comptime T: type, unaligned_address: u32, value: T) void { | ||||||
|  |     @setCold(true); | ||||||
|  |  | ||||||
|  |     const page = @truncate(u8, unaligned_address >> 24); | ||||||
|  |     const address = forceAlign(T, unaligned_address); | ||||||
|  |  | ||||||
|  |     switch (page) { | ||||||
|  |         // General Internal Memory | ||||||
|  |         0x00 => self.bios.write(T, address, value), | ||||||
|  |         0x02 => unreachable, // completely handled by fastmem | ||||||
|  |         0x03 => unreachable, // completely handled by fastmem | ||||||
|  |         0x04 => return, // FIXME: Let debug writes mess with I/O | ||||||
|  |  | ||||||
|  |         // Internal Display Memory | ||||||
|  |         0x05 => self.ppu.palette.write(T, address, value), | ||||||
|  |         0x06 => self.ppu.vram.write(T, self.ppu.dispcnt, address, value), | ||||||
|  |         0x07 => unreachable, // completely handled by fastmem | ||||||
|  |  | ||||||
|  |         // External Memory (Game Pak) | ||||||
|  |         0x08...0x0D => return, // FIXME: Debug Write to Backup/GPIO w/out messing with state | ||||||
|  |         0x0E...0x0F => return, // FIXME: Debug Write to Backup w/out messing with state | ||||||
|  |         else => {}, | ||||||
|  |     } | ||||||
|  | } | ||||||
|  |  | ||||||
| inline fn rotateBy(comptime T: type, address: u32) u32 { | inline fn rotateBy(comptime T: type, address: u32) u32 { | ||||||
|     return switch (T) { |     return switch (T) { | ||||||
|         u32 => address & 3, |         u32 => address & 3, | ||||||
|   | |||||||
| @@ -163,3 +163,59 @@ 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 (true) if (timer.read() > wake_time) break; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | pub const EmuThing = struct { | ||||||
|  |     const Self = @This(); | ||||||
|  |     const Interface = @import("gdbstub").Emulator; | ||||||
|  |     const Allocator = std.mem.Allocator; | ||||||
|  |  | ||||||
|  |     cpu: *Arm7tdmi, | ||||||
|  |     scheduler: *Scheduler, | ||||||
|  |  | ||||||
|  |     pub fn init(cpu: *Arm7tdmi, scheduler: *Scheduler) Self { | ||||||
|  |         return .{ .cpu = cpu, .scheduler = scheduler }; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn interface(self: *Self, allocator: Allocator) Interface { | ||||||
|  |         return Interface.init(allocator, self); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn read(self: *const Self, addr: u32) u8 { | ||||||
|  |         return self.cpu.bus.dbgRead(u8, addr); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn write(self: *Self, addr: u32, value: u8) void { | ||||||
|  |         self.cpu.bus.dbgWrite(u8, addr, value); | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn registers(self: *const Self) *[16]u32 { | ||||||
|  |         return &self.cpu.r; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn cpsr(self: *const Self) u32 { | ||||||
|  |         return self.cpu.cpsr.raw; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     pub fn step(self: *Self) void { | ||||||
|  |         const cpu = self.cpu; | ||||||
|  |         const sched = self.scheduler; | ||||||
|  |  | ||||||
|  |         // Is true when we have executed one (1) instruction | ||||||
|  |         var did_step: bool = false; | ||||||
|  |  | ||||||
|  |         // TODO: How can I make it easier to keep this in lock-step with runFrame? | ||||||
|  |         while (!did_step) { | ||||||
|  |             if (!cpu.stepDmaTransfer()) { | ||||||
|  |                 if (cpu.isHalted()) { | ||||||
|  |                     // Fast-forward to next Event | ||||||
|  |                     sched.tick = sched.queue.peek().?.tick; | ||||||
|  |                 } else { | ||||||
|  |                     cpu.step(); | ||||||
|  |                     did_step = true; | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |  | ||||||
|  |             if (sched.tick >= sched.nextTimestamp()) sched.handleEvent(cpu); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }; | ||||||
|   | |||||||
| @@ -990,8 +990,7 @@ pub const Ppu = struct { | |||||||
|             cpu.handleInterrupt(); |             cpu.handleInterrupt(); | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         // See if HBlank DMA is present and not enabled |         // If we're not also in VBlank, attempt to run any pending DMA Reqs | ||||||
|  |  | ||||||
|         if (!self.dispstat.vblank.read()) |         if (!self.dispstat.vblank.read()) | ||||||
|             dma.onBlanking(cpu.bus, .HBlank); |             dma.onBlanking(cpu.bus, .HBlank); | ||||||
|  |  | ||||||
|   | |||||||
							
								
								
									
										44
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										44
									
								
								src/main.zig
									
									
									
									
									
								
							| @@ -4,14 +4,17 @@ const known_folders = @import("known_folders"); | |||||||
| const clap = @import("clap"); | const clap = @import("clap"); | ||||||
|  |  | ||||||
| const config = @import("config.zig"); | const config = @import("config.zig"); | ||||||
|  | const emu = @import("core/emu.zig"); | ||||||
|  |  | ||||||
| const Gui = @import("platform.zig").Gui; | const Gui = @import("platform.zig").Gui; | ||||||
| const Bus = @import("core/Bus.zig"); | const Bus = @import("core/Bus.zig"); | ||||||
| const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi; | const Arm7tdmi = @import("core/cpu.zig").Arm7tdmi; | ||||||
| const Scheduler = @import("core/scheduler.zig").Scheduler; | const Scheduler = @import("core/scheduler.zig").Scheduler; | ||||||
| const FilePaths = @import("util.zig").FilePaths; | const FilePaths = @import("util.zig").FilePaths; | ||||||
|  | const FpsTracker = @import("util.zig").FpsTracker; | ||||||
| const Allocator = std.mem.Allocator; | const Allocator = std.mem.Allocator; | ||||||
|  | const Atomic = std.atomic.Atomic; | ||||||
|  |  | ||||||
| const log = std.log.scoped(.Cli); | const log = std.log.scoped(.Cli); | ||||||
| const width = @import("core/ppu.zig").width; | const width = @import("core/ppu.zig").width; | ||||||
| const height = @import("core/ppu.zig").height; | const height = @import("core/ppu.zig").height; | ||||||
| @@ -22,6 +25,7 @@ const params = clap.parseParamsComptime( | |||||||
|     \\-h, --help            Display this help and exit. |     \\-h, --help            Display this help and exit. | ||||||
|     \\-s, --skip            Skip BIOS. |     \\-s, --skip            Skip BIOS. | ||||||
|     \\-b, --bios <str>      Optional path to a GBA BIOS ROM. |     \\-b, --bios <str>      Optional path to a GBA BIOS ROM. | ||||||
|  |     \\ --gdb                Run ZBA from the context of a GDB Server | ||||||
|     \\<str>                 Path to the GBA GamePak ROM. |     \\<str>                 Path to the GBA GamePak ROM. | ||||||
|     \\ |     \\ | ||||||
| ); | ); | ||||||
| @@ -87,10 +91,46 @@ pub fn main() void { | |||||||
|         cpu.fastBoot(); |         cpu.fastBoot(); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     var quit = Atomic(bool).init(false); | ||||||
|     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) catch |e| exitln("failed to init gui: {}", .{e}); | ||||||
|     defer gui.deinit(); |     defer gui.deinit(); | ||||||
|  |  | ||||||
|     gui.run(&cpu, &scheduler) catch |e| exitln("failed to run gui thread: {}", .{e}); |     if (result.args.gdb) { | ||||||
|  |         const Server = @import("gdbstub").Server; | ||||||
|  |         const EmuThing = @import("core/emu.zig").EmuThing; | ||||||
|  |  | ||||||
|  |         var wrapper = EmuThing.init(&cpu, &scheduler); | ||||||
|  |         var emulator = wrapper.interface(allocator); | ||||||
|  |         defer emulator.deinit(); | ||||||
|  |  | ||||||
|  |         log.info("Ready to connect", .{}); | ||||||
|  |  | ||||||
|  |         var server = Server.init(emulator) catch |e| exitln("failed to init gdb server: {}", .{e}); | ||||||
|  |         defer server.deinit(allocator); | ||||||
|  |  | ||||||
|  |         log.info("Starting GDB Server Thread", .{}); | ||||||
|  |  | ||||||
|  |         const thread = std.Thread.spawn(.{}, Server.run, .{ &server, allocator, &quit }) catch |e| exitln("gdb server thread crashed: {}", .{e}); | ||||||
|  |         defer thread.join(); | ||||||
|  |  | ||||||
|  |         gui.run(.{ | ||||||
|  |             .cpu = &cpu, | ||||||
|  |             .scheduler = &scheduler, | ||||||
|  |             .quit = &quit, | ||||||
|  |         }) catch |e| exitln("main thread panicked: {}", .{e}); | ||||||
|  |     } else { | ||||||
|  |         var tracker = FpsTracker.init(); | ||||||
|  |  | ||||||
|  |         const thread = std.Thread.spawn(.{}, emu.run, .{ &quit, &scheduler, &cpu, &tracker }) catch |e| exitln("emu thread panicked: {}", .{e}); | ||||||
|  |         defer thread.join(); | ||||||
|  |  | ||||||
|  |         gui.run(.{ | ||||||
|  |             .cpu = &cpu, | ||||||
|  |             .scheduler = &scheduler, | ||||||
|  |             .tracker = &tracker, | ||||||
|  |             .quit = &quit, | ||||||
|  |         }) catch |e| exitln("main thread panicked: {}", .{e}); | ||||||
|  |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !FilePaths { | pub fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !FilePaths { | ||||||
|   | |||||||
| @@ -154,9 +154,17 @@ pub const Gui = struct { | |||||||
|         return tex_id; |         return tex_id; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub fn run(self: *Self, cpu: *Arm7tdmi, scheduler: *Scheduler) !void { |     const RunOptions = struct { | ||||||
|         var quit = std.atomic.Atomic(bool).init(false); |         quit: *std.atomic.Atomic(bool), | ||||||
|         var tracker = FpsTracker.init(); |         tracker: ?*FpsTracker = null, | ||||||
|  |         cpu: *Arm7tdmi, | ||||||
|  |         scheduler: *Scheduler, | ||||||
|  |     }; | ||||||
|  |  | ||||||
|  |     pub fn run(self: *Self, opt: RunOptions) !void { | ||||||
|  |         const cpu = opt.cpu; | ||||||
|  |         const tracker = opt.tracker; | ||||||
|  |         const quit = opt.quit; | ||||||
|  |  | ||||||
|         var buffer_ids = Self.generateBuffers(); |         var buffer_ids = Self.generateBuffers(); | ||||||
|         defer { |         defer { | ||||||
| @@ -169,13 +177,15 @@ pub const Gui = struct { | |||||||
|         const tex_id = Self.generateTexture(cpu.bus.ppu.framebuf.get(.Renderer)); |         const tex_id = Self.generateTexture(cpu.bus.ppu.framebuf.get(.Renderer)); | ||||||
|         defer gl.deleteTextures(1, &tex_id); |         defer gl.deleteTextures(1, &tex_id); | ||||||
|  |  | ||||||
|         const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, scheduler, cpu, &tracker }); |  | ||||||
|         defer thread.join(); |  | ||||||
|  |  | ||||||
|         var title_buf: [0x100]u8 = undefined; |         var title_buf: [0x100]u8 = undefined; | ||||||
|  |  | ||||||
|         emu_loop: while (true) { |         emu_loop: while (true) { | ||||||
|             var event: SDL.SDL_Event = undefined; |             var event: SDL.SDL_Event = undefined; | ||||||
|  |  | ||||||
|  |             // This might be true if the emu is running via a gdbstub server | ||||||
|  |             // and the gdb stub exits first | ||||||
|  |             if (quit.load(.Monotonic)) break :emu_loop; | ||||||
|  |  | ||||||
|             while (SDL.SDL_PollEvent(&event) != 0) { |             while (SDL.SDL_PollEvent(&event) != 0) { | ||||||
|                 switch (event.type) { |                 switch (event.type) { | ||||||
|                     SDL.SDL_QUIT => break :emu_loop, |                     SDL.SDL_QUIT => break :emu_loop, | ||||||
| @@ -238,9 +248,11 @@ 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; |             if (tracker) |t| { | ||||||
|  |                 const dyn_title = std.fmt.bufPrintZ(&title_buf, "ZBA | {s} [Emu: {}fps] ", .{ self.title, t.value() }) catch unreachable; | ||||||
|                 SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); |                 SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); | ||||||
|             } |             } | ||||||
|  |         } | ||||||
|  |  | ||||||
|         quit.store(true, .Monotonic); // Terminate Emulator Thread |         quit.store(true, .Monotonic); // Terminate Emulator Thread | ||||||
|     } |     } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user