Compare commits
	
		
			15 Commits
		
	
	
		
			5f3be3f2f5
			...
			window
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 3b13102abb | |||
| 7234ecab37 | |||
| ddf4599162 | |||
| 01f5410180 | |||
| 49706842af | |||
| 2798a90d83 | |||
| 518b868249 | |||
| 755115660b | |||
| 6709f8c551 | |||
| 1f3cdd9513 | |||
| 65af6aa499 | |||
| 024151a5c1 | |||
| e380af7056 | |||
| e654abfd1d | |||
| 3510a6cff8 | 
							
								
								
									
										9
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										9
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
								
							| @@ -14,7 +14,8 @@ jobs: | |||||||
|   build: |   build: | ||||||
|     strategy: |     strategy: | ||||||
|       matrix: |       matrix: | ||||||
|         os: [ubuntu-latest, windows-latest, macos-latest] |         # os: [ubuntu-latest, windows-latest, macos-latest] | ||||||
|  |         os: [ubuntu-latest, windows-latest] | ||||||
|     runs-on: ${{matrix.os}} |     runs-on: ${{matrix.os}} | ||||||
|     steps: |     steps: | ||||||
|       - uses: goto-bus-stop/setup-zig@v2 |       - uses: goto-bus-stop/setup-zig@v2 | ||||||
| @@ -37,9 +38,9 @@ jobs: | |||||||
|             brew install sdl2 |             brew install sdl2 | ||||||
|       - uses: actions/checkout@v3 |       - uses: actions/checkout@v3 | ||||||
|         with: |         with: | ||||||
|           submodules: true |           submodules: recursive | ||||||
|       - name: build  |       - name: build  | ||||||
|         run: zig build -Doptimize=ReleaseSafe |         run: zig build -Doptimize=ReleaseSafe -Dcpu=baseline | ||||||
|       - name: upload |       - name: upload | ||||||
|         uses: actions/upload-artifact@v3 |         uses: actions/upload-artifact@v3 | ||||||
|         with: |         with: | ||||||
| @@ -50,7 +51,7 @@ jobs: | |||||||
|     steps:  |     steps:  | ||||||
|       - uses: actions/checkout@v3 |       - uses: actions/checkout@v3 | ||||||
|         with: |         with: | ||||||
|           submodules: true |           submodules: recursive | ||||||
|       - uses: goto-bus-stop/setup-zig@v2 |       - uses: goto-bus-stop/setup-zig@v2 | ||||||
|         with: |         with: | ||||||
|           version: master |           version: master | ||||||
|   | |||||||
							
								
								
									
										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 | ||||||
|   | |||||||
| @@ -96,7 +96,7 @@ Use `git submodule update --init` from the project root to pull the git relevant | |||||||
| Be sure to provide SDL2 using: | Be sure to provide SDL2 using: | ||||||
|  |  | ||||||
| - Linux: Your distro's package manager | - Linux: Your distro's package manager | ||||||
| - MacOS: `brew` (install [this formula](https://formulae.brew.sh/formula/sdl2)) | - macOS: ¯\\\_(ツ)_/¯ (try [this formula](https://formulae.brew.sh/formula/sdl2)?) | ||||||
| - Windows: [`vcpkg`](https://github.com/Microsoft/vcpkg) (install `sdl2:x64-windows`) | - Windows: [`vcpkg`](https://github.com/Microsoft/vcpkg) (install `sdl2:x64-windows`) | ||||||
|  |  | ||||||
| `SDL.zig` will provide a helpful compile error if the zig compiler is unable to find SDL2. | `SDL.zig` will provide a helpful compile error if the zig compiler is unable to find SDL2. | ||||||
|   | |||||||
| @@ -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 acb59994fc
									
								
							 Submodule lib/zig-clap updated: 272d8e2088...861de651f3
									
								
							 Submodule lib/zig-datetime updated: 932d284521...bf0ae0c27c
									
								
							
							
								
								
									
										152
									
								
								src/core/Bus.zig
									
									
									
									
									
								
							
							
						
						
									
										152
									
								
								src/core/Bus.zig
									
									
									
									
									
								
							| @@ -105,7 +105,7 @@ pub fn deinit(self: *Self) void { | |||||||
| fn fillReadTable(self: *Self, table: *[table_len]?*const anyopaque) void { | fn fillReadTable(self: *Self, table: *[table_len]?*const anyopaque) void { | ||||||
|     const vramMirror = @import("ppu/Vram.zig").mirror; |     const vramMirror = @import("ppu/Vram.zig").mirror; | ||||||
|  |  | ||||||
|     for (table) |*ptr, i| { |     for (table, 0..) |*ptr, i| { | ||||||
|         const addr = @intCast(u32, page_size * i); |         const addr = @intCast(u32, page_size * i); | ||||||
|  |  | ||||||
|         ptr.* = switch (addr) { |         ptr.* = switch (addr) { | ||||||
| @@ -132,7 +132,7 @@ fn fillWriteTable(self: *Self, comptime T: type, table: *[table_len]?*const anyo | |||||||
|     comptime std.debug.assert(T == u32 or T == u16 or T == u8); |     comptime std.debug.assert(T == u32 or T == u16 or T == u8); | ||||||
|     const vramMirror = @import("ppu/Vram.zig").mirror; |     const vramMirror = @import("ppu/Vram.zig").mirror; | ||||||
|  |  | ||||||
|     for (table) |*ptr, i| { |     for (table, 0..) |*ptr, i| { | ||||||
|         const addr = @intCast(u32, page_size * i); |         const addr = @intCast(u32, page_size * i); | ||||||
|  |  | ||||||
|         ptr.* = switch (addr) { |         ptr.* = switch (addr) { | ||||||
| @@ -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, | ||||||
|   | |||||||
| @@ -213,6 +213,7 @@ fn guessDevice(buf: []const u8) Gpio.Device.Kind { | |||||||
|     // Try to Guess if ROM uses RTC |     // Try to Guess if ROM uses RTC | ||||||
|     const needle = "RTC_V"; // I was told SIIRTC_V, though Pokemen Firered (USA) is a false negative |     const needle = "RTC_V"; // I was told SIIRTC_V, though Pokemen Firered (USA) is a false negative | ||||||
|  |  | ||||||
|  |     // TODO: Use new for loop syntax? | ||||||
|     var i: usize = 0; |     var i: usize = 0; | ||||||
|     while ((i + needle.len) < buf.len) : (i += 1) { |     while ((i + needle.len) < buf.len) : (i += 1) { | ||||||
|         if (std.mem.eql(u8, needle, buf[i..(i + needle.len)])) return .Rtc; |         if (std.mem.eql(u8, needle, buf[i..(i + needle.len)])) return .Rtc; | ||||||
|   | |||||||
| @@ -137,6 +137,7 @@ pub const Backup = struct { | |||||||
|         for (backup_kinds) |needle| { |         for (backup_kinds) |needle| { | ||||||
|             const needle_len = needle.str.len; |             const needle_len = needle.str.len; | ||||||
|  |  | ||||||
|  |             // TODO: Use new for loop syntax? | ||||||
|             var i: usize = 0; |             var i: usize = 0; | ||||||
|             while ((i + needle_len) < rom.len) : (i += 1) { |             while ((i + needle_len) < rom.len) : (i += 1) { | ||||||
|                 if (std.mem.eql(u8, needle.str, rom[i..][0..needle_len])) return needle.kind; |                 if (std.mem.eql(u8, needle.str, rom[i..][0..needle_len])) return needle.kind; | ||||||
|   | |||||||
| @@ -339,10 +339,7 @@ fn DmaController(comptime id: u2) type { | |||||||
| } | } | ||||||
|  |  | ||||||
| pub fn onBlanking(bus: *Bus, comptime kind: DmaKind) void { | pub fn onBlanking(bus: *Bus, comptime kind: DmaKind) void { | ||||||
|     comptime var i: usize = 0; |     inline for (0..4) |i| bus.dma[i].poll(kind); | ||||||
|     inline while (i < 4) : (i += 1) { |  | ||||||
|         bus.dma[i].poll(kind); |  | ||||||
|     } |  | ||||||
| } | } | ||||||
|  |  | ||||||
| const Adjustment = enum(u2) { | const Adjustment = enum(u2) { | ||||||
|   | |||||||
| @@ -293,13 +293,13 @@ pub const Clock = struct { | |||||||
|         self.cpu.sched.push(.RealTimeClock, (1 << 24) -| late); // Reschedule |         self.cpu.sched.push(.RealTimeClock, (1 << 24) -| late); // Reschedule | ||||||
|  |  | ||||||
|         const now = DateTime.now(); |         const now = DateTime.now(); | ||||||
|         self.year = bcd(u8, @intCast(u8, now.date.year - 2000)); |         self.year = bcd(@intCast(u8, now.date.year - 2000)); | ||||||
|         self.month = bcd(u5, now.date.month); |         self.month = @truncate(u5, bcd(now.date.month)); | ||||||
|         self.day = bcd(u6, now.date.day); |         self.day = @truncate(u6, bcd(now.date.day)); | ||||||
|         self.weekday = bcd(u3, (now.date.weekday() + 1) % 7); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6 |         self.weekday = @truncate(u3, bcd((now.date.weekday() + 1) % 7)); // API is Monday = 0, Sunday = 6. We want Sunday = 0, Saturday = 6 | ||||||
|         self.hour = bcd(u6, now.time.hour); |         self.hour = @truncate(u6, bcd(now.time.hour)); | ||||||
|         self.minute = bcd(u7, now.time.minute); |         self.minute = @truncate(u7, bcd(now.time.minute)); | ||||||
|         self.second = bcd(u7, now.time.second); |         self.second = @truncate(u7, bcd(now.time.second)); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn step(self: *Self, value: Data) u4 { |     fn step(self: *Self, value: Data) u4 { | ||||||
| @@ -449,16 +449,8 @@ pub const Clock = struct { | |||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| fn bcd(comptime T: type, value: u8) T { | /// Converts an 8-bit unsigned integer to its BCD representation. | ||||||
|     var input = value; | /// Note: Algorithm only works for values between 0 and 99 inclusive. | ||||||
|     var ret: u8 = 0; | fn bcd(value: u8) u8 { | ||||||
|     var shift: u3 = 0; |     return ((value / 10) << 4) + (value % 10); | ||||||
|  |  | ||||||
|     while (input > 0) { |  | ||||||
|         ret |= (input % 10) << (shift << 2); |  | ||||||
|         shift += 1; |  | ||||||
|         input /= 10; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     return @truncate(T, ret); |  | ||||||
| } | } | ||||||
|   | |||||||
| @@ -39,13 +39,12 @@ pub const arm = struct { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn populate() [0x1000]InstrFn { |     fn populate() [0x1000]InstrFn { | ||||||
|         return comptime { |         comptime { | ||||||
|             @setEvalBranchQuota(0xE000); |             @setEvalBranchQuota(0xE000); | ||||||
|             var ret = [_]InstrFn{und} ** 0x1000; |             var table = [_]InstrFn{und} ** 0x1000; | ||||||
|  |  | ||||||
|             var i: usize = 0; |             for (&table, 0..) |*handler, i| { | ||||||
|             while (i < ret.len) : (i += 1) { |                 handler.* = switch (@as(u2, i >> 10)) { | ||||||
|                 ret[i] = switch (@as(u2, i >> 10)) { |  | ||||||
|                     0b00 => if (i == 0x121) blk: { |                     0b00 => if (i == 0x121) blk: { | ||||||
|                         break :blk branchExchange; |                         break :blk branchExchange; | ||||||
|                     } else if (i & 0xFCF == 0x009) blk: { |                     } else if (i & 0xFCF == 0x009) blk: { | ||||||
| @@ -107,8 +106,8 @@ pub const arm = struct { | |||||||
|                 }; |                 }; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return ret; |             return table; | ||||||
|         }; |         } | ||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -136,13 +135,12 @@ pub const thumb = struct { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     fn populate() [0x400]InstrFn { |     fn populate() [0x400]InstrFn { | ||||||
|         return comptime { |         comptime { | ||||||
|             @setEvalBranchQuota(5025); // This is exact |             @setEvalBranchQuota(5025); // This is exact | ||||||
|             var ret = [_]InstrFn{und} ** 0x400; |             var table = [_]InstrFn{und} ** 0x400; | ||||||
|  |  | ||||||
|             var i: usize = 0; |             for (&table, 0..) |*handler, i| { | ||||||
|             while (i < ret.len) : (i += 1) { |                 handler.* = switch (@as(u3, i >> 7 & 0x7)) { | ||||||
|                 ret[i] = switch (@as(u3, i >> 7 & 0x7)) { |  | ||||||
|                     0b000 => if (i >> 5 & 0x3 == 0b11) blk: { |                     0b000 => if (i >> 5 & 0x3 == 0b11) blk: { | ||||||
|                         const I = i >> 4 & 1 == 1; |                         const I = i >> 4 & 1 == 1; | ||||||
|                         const is_sub = i >> 3 & 1 == 1; |                         const is_sub = i >> 3 & 1 == 1; | ||||||
| @@ -230,8 +228,8 @@ pub const thumb = struct { | |||||||
|                 }; |                 }; | ||||||
|             } |             } | ||||||
|  |  | ||||||
|             return ret; |             return table; | ||||||
|         }; |         } | ||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -385,8 +383,7 @@ pub const Arm7tdmi = struct { | |||||||
|         const now = getModeChecked(self, self.cpsr.mode.read()); |         const now = getModeChecked(self, self.cpsr.mode.read()); | ||||||
|  |  | ||||||
|         // Bank R8 -> r12 |         // Bank R8 -> r12 | ||||||
|         var i: usize = 0; |         for (0..5) |i| { | ||||||
|         while (i < 5) : (i += 1) { |  | ||||||
|             self.bank.fiq[Bank.fiqIdx(i, now)] = self.r[8 + i]; |             self.bank.fiq[Bank.fiqIdx(i, now)] = self.r[8 + i]; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -404,8 +401,7 @@ pub const Arm7tdmi = struct { | |||||||
|         } |         } | ||||||
|  |  | ||||||
|         // Grab R8 -> R12 |         // Grab R8 -> R12 | ||||||
|         i = 0; |         for (0..5) |i| { | ||||||
|         while (i < 5) : (i += 1) { |  | ||||||
|             self.r[8 + i] = self.bank.fiq[Bank.fiqIdx(i, next)]; |             self.r[8 + i] = self.bank.fiq[Bank.fiqIdx(i, next)]; | ||||||
|         } |         } | ||||||
|  |  | ||||||
| @@ -470,8 +466,7 @@ pub const Arm7tdmi = struct { | |||||||
|     } |     } | ||||||
|  |  | ||||||
|     pub fn stepDmaTransfer(self: *Self) bool { |     pub fn stepDmaTransfer(self: *Self) bool { | ||||||
|         comptime var i: usize = 0; |         inline for (0..4) |i| { | ||||||
|         inline while (i < 4) : (i += 1) { |  | ||||||
|             if (self.bus.dma[i].in_progress) { |             if (self.bus.dma[i].in_progress) { | ||||||
|                 self.bus.dma[i].step(self); |                 self.bus.dma[i].step(self); | ||||||
|                 return true; |                 return true; | ||||||
|   | |||||||
| @@ -92,8 +92,7 @@ pub fn fmt15(comptime L: bool, comptime rb: u3) InstrFn { | |||||||
| inline fn countRlist(opcode: u16) u32 { | inline fn countRlist(opcode: u16) u32 { | ||||||
|     var count: u32 = 0; |     var count: u32 = 0; | ||||||
|  |  | ||||||
|     comptime var i: u4 = 0; |     inline for (0..8) |i| { | ||||||
|     inline while (i < 8) : (i += 1) { |  | ||||||
|         if (opcode >> (7 - i) & 1 == 1) count += 1; |         if (opcode >> (7 - i) & 1 == 1) count += 1; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -146,9 +146,8 @@ fn sleep(timer: *Timer, wake_time: u64) ?u64 { | |||||||
|  |  | ||||||
|     const step = 2 * std.time.ns_per_ms; // Granularity of 2ms |     const step = 2 * std.time.ns_per_ms; // Granularity of 2ms | ||||||
|     const times = sleep_for / step; |     const times = sleep_for / step; | ||||||
|     var i: usize = 0; |  | ||||||
|  |  | ||||||
|     while (i < times) : (i += 1) { |     for (0..times) |_| { | ||||||
|         std.time.sleep(step); |         std.time.sleep(step); | ||||||
|  |  | ||||||
|         // Upon wakeup, check to see if this particular sleep was longer than expected |         // Upon wakeup, check to see if this particular sleep was longer than expected | ||||||
| @@ -163,3 +162,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); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  | }; | ||||||
|   | |||||||
| @@ -625,8 +625,7 @@ pub const Ppu = struct { | |||||||
|                 const framebuf_base = width * @as(usize, scanline); |                 const framebuf_base = width * @as(usize, scanline); | ||||||
|                 if (obj_enable) self.fetchSprites(); |                 if (obj_enable) self.fetchSprites(); | ||||||
|  |  | ||||||
|                 var layer: usize = 0; |                 for (0..4) |layer| { | ||||||
|                 while (layer < 4) : (layer += 1) { |  | ||||||
|                     self.drawSprites(@truncate(u2, layer)); |                     self.drawSprites(@truncate(u2, layer)); | ||||||
|                     if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackground(0); |                     if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackground(0); | ||||||
|                     if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackground(1); |                     if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackground(1); | ||||||
| @@ -640,8 +639,7 @@ pub const Ppu = struct { | |||||||
|                 const framebuf_base = width * @as(usize, scanline); |                 const framebuf_base = width * @as(usize, scanline); | ||||||
|                 if (obj_enable) self.fetchSprites(); |                 if (obj_enable) self.fetchSprites(); | ||||||
|  |  | ||||||
|                 var layer: usize = 0; |                 for (0..4) |layer| { | ||||||
|                 while (layer < 4) : (layer += 1) { |  | ||||||
|                     self.drawSprites(@truncate(u2, layer)); |                     self.drawSprites(@truncate(u2, layer)); | ||||||
|                     if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackground(0); |                     if (layer == self.bg[0].cnt.priority.read() and bg_enable & 1 == 1) self.drawBackground(0); | ||||||
|                     if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackground(1); |                     if (layer == self.bg[1].cnt.priority.read() and bg_enable >> 1 & 1 == 1) self.drawBackground(1); | ||||||
| @@ -654,8 +652,7 @@ pub const Ppu = struct { | |||||||
|                 const framebuf_base = width * @as(usize, scanline); |                 const framebuf_base = width * @as(usize, scanline); | ||||||
|                 if (obj_enable) self.fetchSprites(); |                 if (obj_enable) self.fetchSprites(); | ||||||
|  |  | ||||||
|                 var layer: usize = 0; |                 for (0..4) |layer| { | ||||||
|                 while (layer < 4) : (layer += 1) { |  | ||||||
|                     self.drawSprites(@truncate(u2, layer)); |                     self.drawSprites(@truncate(u2, layer)); | ||||||
|                     if (layer == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawAffineBackground(2); |                     if (layer == self.bg[2].cnt.priority.read() and bg_enable >> 2 & 1 == 1) self.drawAffineBackground(2); | ||||||
|                     if (layer == self.bg[3].cnt.priority.read() and bg_enable >> 3 & 1 == 1) self.drawAffineBackground(3); |                     if (layer == self.bg[3].cnt.priority.read() and bg_enable >> 3 & 1 == 1) self.drawAffineBackground(3); | ||||||
| @@ -671,7 +668,7 @@ pub const Ppu = struct { | |||||||
|                 const vram_buf = @ptrCast([*]const u16, @alignCast(@alignOf(u16), self.vram.buf)); |                 const vram_buf = @ptrCast([*]const u16, @alignCast(@alignOf(u16), self.vram.buf)); | ||||||
|                 const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); |                 const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); | ||||||
|  |  | ||||||
|                 for (vram_buf[vram_base .. vram_base + width]) |bgr555, i| { |                 for (vram_buf[vram_base .. vram_base + width], 0..) |bgr555, i| { | ||||||
|                     framebuf[framebuf_base + i] = rgba888(bgr555); |                     framebuf[framebuf_base + i] = rgba888(bgr555); | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
| @@ -685,7 +682,7 @@ pub const Ppu = struct { | |||||||
|                 const pal_buf = @ptrCast([*]const u16, @alignCast(@alignOf(u16), self.palette.buf)); |                 const pal_buf = @ptrCast([*]const u16, @alignCast(@alignOf(u16), self.palette.buf)); | ||||||
|                 const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); |                 const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); | ||||||
|  |  | ||||||
|                 for (self.vram.buf[vram_base .. vram_base + width]) |pal_id, i| { |                 for (self.vram.buf[vram_base .. vram_base + width], 0..) |pal_id, i| { | ||||||
|                     framebuf[framebuf_base + i] = rgba888(pal_buf[pal_id]); |                     framebuf[framebuf_base + i] = rgba888(pal_buf[pal_id]); | ||||||
|                 } |                 } | ||||||
|             }, |             }, | ||||||
| @@ -701,8 +698,7 @@ pub const Ppu = struct { | |||||||
|                 const vram_buf = @ptrCast([*]const u16, @alignCast(@alignOf(u16), self.vram.buf)); |                 const vram_buf = @ptrCast([*]const u16, @alignCast(@alignOf(u16), self.vram.buf)); | ||||||
|                 const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); |                 const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); | ||||||
|  |  | ||||||
|                 var i: usize = 0; |                 for (0..width) |i| { | ||||||
|                 while (i < width) : (i += 1) { |  | ||||||
|                     const bgr555 = if (scanline < m5_height and i < m5_width) vram_buf[vram_base + i] else self.palette.backdrop(); |                     const bgr555 = if (scanline < m5_height and i < m5_width) vram_buf[vram_base + i] else self.palette.backdrop(); | ||||||
|                     framebuf[framebuf_base + i] = rgba888(bgr555); |                     framebuf[framebuf_base + i] = rgba888(bgr555); | ||||||
|                 } |                 } | ||||||
| @@ -718,7 +714,7 @@ pub const Ppu = struct { | |||||||
|         // FIXME: @ptrCast between slices changing the length isn't implemented yet |         // FIXME: @ptrCast between slices changing the length isn't implemented yet | ||||||
|         const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); |         const framebuf = @ptrCast([*]u32, @alignCast(@alignOf(u32), self.framebuf.get(.Emulator))); | ||||||
|  |  | ||||||
|         for (self.scanline.top()) |maybe_top, i| { |         for (self.scanline.top(), 0..) |maybe_top, i| { | ||||||
|             const maybe_btm = self.scanline.btm()[i]; |             const maybe_btm = self.scanline.btm()[i]; | ||||||
|  |  | ||||||
|             const bgr555 = self.getBgr555(maybe_top, maybe_btm); |             const bgr555 = self.getBgr555(maybe_top, maybe_btm); | ||||||
| @@ -990,8 +986,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); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -73,7 +73,7 @@ pub const Scheduler = struct { | |||||||
|  |  | ||||||
|     /// Removes the **first** scheduled event of type `needle` |     /// Removes the **first** scheduled event of type `needle` | ||||||
|     pub fn removeScheduledEvent(self: *Self, needle: EventKind) void { |     pub fn removeScheduledEvent(self: *Self, needle: EventKind) void { | ||||||
|         for (self.queue.items) |event, i| { |         for (self.queue.items, 0..) |event, i| { | ||||||
|             if (std.meta.eql(event.kind, needle)) { |             if (std.meta.eql(event.kind, needle)) { | ||||||
|  |  | ||||||
|                 // invalidates the slice we're iterating over |                 // invalidates the slice we're iterating over | ||||||
|   | |||||||
							
								
								
									
										46
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										46
									
								
								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,13 +91,49 @@ 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 { | fn handleArguments(allocator: Allocator, data_path: []const u8, result: *const clap.Result(clap.Help, ¶ms, clap.parsers.default)) !FilePaths { | ||||||
|     const rom_path = romPath(result); |     const rom_path = romPath(result); | ||||||
|     log.info("ROM path: {s}", .{rom_path}); |     log.info("ROM path: {s}", .{rom_path}); | ||||||
|  |  | ||||||
|   | |||||||
| @@ -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,8 +248,10 @@ 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| { | ||||||
|             SDL.SDL_SetWindowTitle(self.window, dyn_title.ptr); |                 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); | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         quit.store(true, .Monotonic); // Terminate Emulator Thread |         quit.store(true, .Monotonic); // Terminate Emulator Thread | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user