diff --git a/src/Bus.zig b/src/Bus.zig index a6effec..420658b 100644 --- a/src/Bus.zig +++ b/src/Bus.zig @@ -40,20 +40,20 @@ pub fn deinit(self: Self) void { pub fn read32(self: *const Self, addr: u32) u32 { return switch (addr) { // General Internal Memory - 0x0000_0000...0x0000_3FFF => self.bios.get32(@as(usize, addr)), + 0x0000_0000...0x0000_3FFF => self.bios.get32(addr), 0x0200_0000...0x0203_FFFF => self.iwram.get32(addr - 0x0200_0000), 0x0300_0000...0x0300_7FFF => self.ewram.get32(addr - 0x0300_0000), 0x0400_0000...0x0400_03FE => self.io.read32(addr), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.get32(@as(usize, addr - 0x0500_0000)), - 0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(@as(usize, addr - 0x0600_0000)), + 0x0500_0000...0x0500_03FF => self.ppu.palette.get32(addr - 0x0500_0000), + 0x0600_0000...0x0601_7FFF => self.ppu.vram.get32(addr - 0x0600_0000), 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:32] read from 0x{X:} in OAM", .{addr}), // External Memory (Game Pak) - 0x0800_0000...0x09FF_FFFF => self.pak.get32(@as(usize, addr - 0x0800_0000)), - 0x0A00_0000...0x0BFF_FFFF => self.pak.get32(@as(usize, addr - 0x0A00_0000)), - 0x0C00_0000...0x0DFF_FFFF => self.pak.get32(@as(usize, addr - 0x0C00_0000)), + 0x0800_0000...0x09FF_FFFF => self.pak.get32(addr - 0x0800_0000), + 0x0A00_0000...0x0BFF_FFFF => self.pak.get32(addr - 0x0A00_0000), + 0x0C00_0000...0x0DFF_FFFF => self.pak.get32(addr - 0x0C00_0000), else => { std.log.warn("[Bus:32] ZBA tried to read from 0x{X:}", .{addr}); @@ -72,8 +72,8 @@ pub fn write32(self: *Self, addr: u32, word: u32) void { 0x0400_0000...0x0400_03FE => self.io.write32(addr, word), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.set32(@as(usize, addr - 0x0500_0000), word), - 0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(@as(usize, addr - 0x0600_0000), word), + 0x0500_0000...0x0500_03FF => self.ppu.palette.set32(addr - 0x0500_0000, word), + 0x0600_0000...0x0601_7FFF => self.ppu.vram.set32(addr - 0x0600_0000, word), 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:32] wrote 0x{X:} to 0x{X:} in OAM", .{ word, addr }), else => std.log.warn("[Bus:32] ZBA tried to write 0x{X:} to 0x{X:}", .{ word, addr }), @@ -83,20 +83,20 @@ pub fn write32(self: *Self, addr: u32, word: u32) void { pub fn read16(self: *const Self, addr: u32) u16 { return switch (addr) { // General Internal Memory - 0x0000_0000...0x0000_3FFF => self.bios.get16(@as(usize, addr)), + 0x0000_0000...0x0000_3FFF => self.bios.get16(addr), 0x0200_0000...0x0203_FFFF => self.iwram.get16(addr - 0x0200_0000), 0x0300_0000...0x0300_7FFF => self.ewram.get16(addr - 0x0300_0000), 0x0400_0000...0x0400_03FE => self.io.read16(addr), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.get16(@as(usize, addr - 0x0500_0000)), - 0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(@as(usize, addr - 0x0600_0000)), + 0x0500_0000...0x0500_03FF => self.ppu.palette.get16(addr - 0x0500_0000), + 0x0600_0000...0x0601_7FFF => self.ppu.vram.get16(addr - 0x0600_0000), 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:16] read from 0x{X:} in OAM", .{addr}), // External Memory (Game Pak) - 0x0800_0000...0x09FF_FFFF => self.pak.get16(@as(usize, addr - 0x0800_0000)), - 0x0A00_0000...0x0BFF_FFFF => self.pak.get16(@as(usize, addr - 0x0A00_0000)), - 0x0C00_0000...0x0DFF_FFFF => self.pak.get16(@as(usize, addr - 0x0C00_0000)), + 0x0800_0000...0x09FF_FFFF => self.pak.get16(addr - 0x0800_0000), + 0x0A00_0000...0x0BFF_FFFF => self.pak.get16(addr - 0x0A00_0000), + 0x0C00_0000...0x0DFF_FFFF => self.pak.get16(addr - 0x0C00_0000), else => { std.log.warn("[Bus:16] ZBA tried to read from 0x{X:}", .{addr}); @@ -114,8 +114,8 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void { 0x0400_0000...0x0400_03FE => self.io.write16(addr, halfword), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.set16(@as(usize, addr - 0x0500_0000), halfword), - 0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(@as(usize, addr - 0x0600_0000), halfword), + 0x0500_0000...0x0500_03FF => self.ppu.palette.set16(addr - 0x0500_0000, halfword), + 0x0600_0000...0x0601_7FFF => self.ppu.vram.set16(addr - 0x0600_0000, halfword), 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:16] write 0x{X:} to 0x{X:} in OAM", .{ halfword, addr }), else => std.log.warn("[Bus:16] ZBA tried to write 0x{X:} to 0x{X:}", .{ halfword, addr }), @@ -125,20 +125,20 @@ pub fn write16(self: *Self, addr: u32, halfword: u16) void { pub fn read8(self: *const Self, addr: u32) u8 { return switch (addr) { // General Internal Memory - 0x0000_0000...0x0000_3FFF => self.bios.get8(@as(usize, addr)), + 0x0000_0000...0x0000_3FFF => self.bios.get8(addr), 0x0200_0000...0x0203_FFFF => self.iwram.get8(addr - 0x0200_0000), 0x0300_0000...0x0300_7FFF => self.ewram.get8(addr - 0x0300_0000), 0x0400_0000...0x0400_03FE => self.io.read8(addr), // Internal Display Memory - 0x0500_0000...0x0500_03FF => self.ppu.palette.get8(@as(usize, addr - 0x0500_0000)), - 0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(@as(usize, addr - 0x0600_0000)), + 0x0500_0000...0x0500_03FF => self.ppu.palette.get8(addr - 0x0500_0000), + 0x0600_0000...0x0601_7FFF => self.ppu.vram.get8(addr - 0x0600_0000), 0x0700_0000...0x0700_03FF => std.debug.panic("[Bus:8] read from 0x{X:} in OAM", .{addr}), // External Memory (Game Pak) - 0x0800_0000...0x09FF_FFFF => self.pak.get8(@as(usize, addr - 0x0800_0000)), - 0x0A00_0000...0x0BFF_FFFF => self.pak.get8(@as(usize, addr - 0x0A00_0000)), - 0x0C00_0000...0x0DFF_FFFF => self.pak.get8(@as(usize, addr - 0x0C00_0000)), + 0x0800_0000...0x09FF_FFFF => self.pak.get8(addr - 0x0800_0000), + 0x0A00_0000...0x0BFF_FFFF => self.pak.get8(addr - 0x0A00_0000), + 0x0C00_0000...0x0DFF_FFFF => self.pak.get8(addr - 0x0C00_0000), 0x0E00_0000...0x0E00_FFFF => std.debug.panic("[Bus:8] read from 0x{X:} in Game Pak SRAM", .{addr}), else => { diff --git a/src/bus/GamePak.zig b/src/bus/GamePak.zig index f7dd515..4ab9bf2 100644 --- a/src/bus/GamePak.zig +++ b/src/bus/GamePak.zig @@ -40,9 +40,7 @@ fn parseTitle(buf: []u8) [12]u8 { } fn lookupMaker(slice: *const [2]u8) ?[]const u8 { - const num = @as(u16, slice[1]) << 8 | @as(u16, slice[0]); - - return switch (num) { + return switch (std.mem.bytesToValue(u16, slice)) { 0x3130 => "Nintendo", else => null, }; diff --git a/src/bus/io.zig b/src/bus/io.zig index 08c529b..64a1884 100644 --- a/src/bus/io.zig +++ b/src/bus/io.zig @@ -27,10 +27,10 @@ pub const Io = struct { pub fn read32(self: *const Self, addr: u32) u32 { return switch (addr) { - 0x0400_0000 => @as(u32, self.dispcnt.raw), - 0x0400_0004 => @as(u32, self.dispstat.raw), - 0x0400_0006 => @as(u32, self.vcount.raw), - 0x0400_0200 => @as(u32, self.ie.raw), + 0x0400_0000 => self.dispcnt.raw, + 0x0400_0004 => self.dispstat.raw, + 0x0400_0006 => self.vcount.raw, + 0x0400_0200 => self.ie.raw, 0x0400_0208 => @boolToInt(self.ime), else => std.debug.panic("[I/O:32] tried to read from {X:}", .{addr}), }; diff --git a/src/cpu.zig b/src/cpu.zig index e017cc7..7249d7a 100644 --- a/src/cpu.zig +++ b/src/cpu.zig @@ -250,7 +250,7 @@ pub const Arm7tdmi = struct { pub fn step(self: *Self) u64 { if (self.cpsr.t.read()) { const opcode = self.thumbFetch(); - if (enable_logging) if (self.log_file) |file| self.log(file, @as(u32, opcode)); + if (enable_logging) if (self.log_file) |file| self.log(file, opcode); thumb_lut[thumbIdx(opcode)](self, self.bus, opcode); } else { diff --git a/src/cpu/arm/half_signed_data_transfer.zig b/src/cpu/arm/half_signed_data_transfer.zig index 3edb9b4..ba97d62 100644 --- a/src/cpu/arm/half_signed_data_transfer.zig +++ b/src/cpu/arm/half_signed_data_transfer.zig @@ -37,15 +37,15 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: 0b01 => { // LDRH const value = bus.read16(address & 0xFFFF_FFFE); - result = std.math.rotr(u32, @as(u32, value), 8 * (address & 1)); + result = std.math.rotr(u32, value, 8 * (address & 1)); }, 0b10 => { // LDRSB - result = util.u32SignExtend(8, @as(u32, bus.read8(address))); + result = util.u32SignExtend(8, bus.read8(address)); }, 0b11 => { // LDRSH - result = util.u32SignExtend(16, @as(u32, bus.read16(address & 0xFFFF_FFFE))); + result = util.u32SignExtend(16, bus.read16(address & 0xFFFF_FFFE)); }, 0b00 => unreachable, // SWP } diff --git a/src/cpu/thumb/block_data_transfer.zig b/src/cpu/thumb/block_data_transfer.zig index 42c47ec..7d9ae2a 100644 --- a/src/cpu/thumb/block_data_transfer.zig +++ b/src/cpu/thumb/block_data_transfer.zig @@ -10,9 +10,9 @@ pub fn format14(comptime L: bool, comptime R: bool) InstrFn { // POP address = cpu.r[13]; - var i: usize = 0; + var i: u4 = 0; while (i < 8) : (i += 1) { - if ((opcode >> @truncate(u3, i)) & 1 == 1) { + if ((opcode >> i) & 1 == 1) { cpu.r[i] = bus.read32(address); address += 4; } @@ -31,11 +31,11 @@ pub fn format14(comptime L: bool, comptime R: bool) InstrFn { address -= 4; } - var i: usize = 8; + var i: u4 = 8; while (i > 0) : (i -= 1) { const j = i - 1; - if ((opcode >> @truncate(u3, j)) & 1 == 1) { + if ((opcode >> j) & 1 == 1) { bus.write32(address, cpu.r[j]); address -= 4; } diff --git a/src/cpu/thumb/branch.zig b/src/cpu/thumb/branch.zig index ddcbd05..58cbaff 100644 --- a/src/cpu/thumb/branch.zig +++ b/src/cpu/thumb/branch.zig @@ -47,7 +47,7 @@ pub fn format19(comptime is_low: bool) InstrFn { cpu.r[14] = old_pc | 1; } else { // Instruction 1 - cpu.r[14] = (cpu.r[15] + 2) +% (u32SignExtend(11, @as(u32, offset)) << 12); + cpu.r[14] = (cpu.r[15] + 2) +% (u32SignExtend(11, offset) << 12); } } }.inner; diff --git a/src/cpu/thumb/data_processing.zig b/src/cpu/thumb/data_processing.zig index caf59bd..d8c0b72 100644 --- a/src/cpu/thumb/data_processing.zig +++ b/src/cpu/thumb/data_processing.zig @@ -60,14 +60,14 @@ pub fn format2(comptime I: bool, is_sub: bool, rn: u3) InstrFn { if (is_sub) { // SUB cpu.r[rd] = if (I) blk: { - break :blk sub(true, cpu, cpu.r[rs], @as(u32, rn)); + break :blk sub(true, cpu, cpu.r[rs], rn); } else blk: { break :blk sub(true, cpu, cpu.r[rs], cpu.r[rn]); }; } else { // ADD cpu.r[rd] = if (I) blk: { - break :blk add(true, cpu, cpu.r[rs], @as(u32, rn)); + break :blk add(true, cpu, cpu.r[rs], rn); } else blk: { break :blk add(true, cpu, cpu.r[rs], cpu.r[rn]); }; diff --git a/src/cpu/thumb/data_transfer.zig b/src/cpu/thumb/data_transfer.zig index fb5240c..786dd67 100644 --- a/src/cpu/thumb/data_transfer.zig +++ b/src/cpu/thumb/data_transfer.zig @@ -36,12 +36,12 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn { }, 0b01 => { // LDSB - cpu.r[rd] = u32SignExtend(8, @as(u32, bus.read8(address))); + cpu.r[rd] = u32SignExtend(8, bus.read8(address)); }, 0b10 => { // LDRH const value = bus.read16(address & 0xFFFF_FFFE); - cpu.r[rd] = std.math.rotr(u32, @as(u32, value), 8 * (address & 1)); + cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1)); }, 0b11 => { // LDRSH @@ -123,7 +123,7 @@ pub fn format10(comptime L: bool, comptime offset: u5) InstrFn { if (L) { // LDRH const value = bus.read16(address & 0xFFFF_FFFE); - cpu.r[rd] = std.math.rotr(u32, @as(u32, value), 8 * (address & 1)); + cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1)); } else { // STRH bus.write16(address & 0xFFFF_FFFE, @truncate(u16, cpu.r[rd]));