Compare commits

...

8 Commits

24 changed files with 407 additions and 227 deletions

@ -1 +1 @@
Subproject commit f3d7620f85929bca8b7ce2be2fa8a938e164f720 Subproject commit 401c50ff3d0b83ad4bd89caf580ce1bd90fb5618

@ -1 +1 @@
Subproject commit 9db1b99219c767d5e24994b1525273fe4031e464 Subproject commit 24845b0103e611c108d6bc334231c464e699742c

@ -1 +1 @@
Subproject commit 996821a3e1f186c9e5cdfd971d742c9815ea590e Subproject commit 1c09e0dc31918dd716b1032ad3e1d1c080cbbff1

View File

@ -8,12 +8,12 @@ const Scheduler = @import("core/scheduler.zig").Scheduler;
const FpsTracker = @import("core/util.zig").FpsTracker; const FpsTracker = @import("core/util.zig").FpsTracker;
const pitch = @import("core/ppu.zig").framebuf_pitch; const pitch = @import("core/ppu.zig").framebuf_pitch;
const scale = @import("core/emu.zig").win_scale;
const emu = @import("core/emu.zig"); const emu = @import("core/emu.zig");
const asString = @import("core/util.zig").asString; const asString = @import("core/util.zig").asString;
const log = std.log.scoped(.GUI); const log = std.log.scoped(.GUI);
const scale = 4;
const default_title: []const u8 = "ZBA"; const default_title: []const u8 = "ZBA";
window: *SDL.SDL_Window, window: *SDL.SDL_Window,

View File

@ -77,70 +77,29 @@ pub fn attach(self: *Self, cpu: *Arm7tdmi) void {
self.cpu = cpu; self.cpu = cpu;
} }
pub fn debugRead(self: *const Self, comptime T: type, address: u32) T { pub fn dbgRead(self: *const Self, comptime T: type, address: u32) T {
const cached = self.sched.tick;
defer self.sched.tick = cached;
// FIXME: This is bad but it's a debug read so I don't care that much?
const this = @intToPtr(*Self, @ptrToInt(self));
return this.read(T, address);
}
fn readOpenBus(self: *const Self, comptime T: type, address: u32) T {
const r15 = self.cpu.?.r[15];
const word = if (self.cpu.?.cpsr.t.read()) blk: {
const page = @truncate(u8, r15 >> 24);
switch (page) {
// EWRAM, PALRAM, VRAM, and Game ROM (16-bit)
0x02, 0x05, 0x06, 0x08...0x0D => {
const halfword = self.debugRead(u16, r15 + 2);
break :blk @as(u32, halfword) << 16 | halfword;
},
// BIOS or OAM (32-bit)
0x00, 0x07 => {
const offset: u32 = if (address & 3 == 0b00) 2 else 0;
break :blk @as(u32, self.debugRead(u16, (r15 + 2) + offset)) << 16 | self.debugRead(u16, r15 + offset);
},
// IWRAM (16-bit but special)
0x03 => {
const offset: u32 = if (address & 3 == 0b00) 2 else 0;
break :blk @as(u32, self.debugRead(u16, (r15 + 2) - offset)) << 16 | self.debugRead(u16, r15 + offset);
},
else => unreachable,
}
} else self.debugRead(u32, r15 + 4);
return @truncate(T, rotr(u32, word, 8 * (address & 3)));
}
fn readBios(self: *Self, comptime T: type, address: u32) T {
if (address < Bios.size) return self.bios.checkedRead(T, self.cpu.?.r[15], alignAddress(T, address));
return self.readOpenBus(T, address);
}
pub fn read(self: *Self, comptime T: type, address: u32) T {
const page = @truncate(u8, address >> 24); const page = @truncate(u8, address >> 24);
const align_addr = alignAddress(T, address); const aligned_addr = forceAlign(T, address);
defer self.sched.tick += timings[@boolToInt(T == u32)][@truncate(u4, page)];
return switch (page) { return switch (page) {
// General Internal Memory // General Internal Memory
0x00 => self.readBios(T, address), 0x00 => blk: {
0x02 => self.ewram.read(T, align_addr), if (address < Bios.size)
0x03 => self.iwram.read(T, align_addr), break :blk self.bios.dbgRead(T, self.cpu.?.r[15], aligned_addr);
0x04 => io.read(self, T, align_addr),
break :blk self.readOpenBus(T, address);
},
0x02 => self.ewram.read(T, aligned_addr),
0x03 => self.iwram.read(T, aligned_addr),
0x04 => io.read(self, T, aligned_addr),
// Internal Display Memory // Internal Display Memory
0x05 => self.ppu.palette.read(T, align_addr), 0x05 => self.ppu.palette.read(T, aligned_addr),
0x06 => self.ppu.vram.read(T, align_addr), 0x06 => self.ppu.vram.read(T, aligned_addr),
0x07 => self.ppu.oam.read(T, align_addr), 0x07 => self.ppu.oam.read(T, aligned_addr),
// External Memory (Game Pak) // External Memory (Game Pak)
0x08...0x0D => self.pak.read(T, align_addr), 0x08...0x0D => self.pak.dbgRead(T, aligned_addr),
0x0E...0x0F => blk: { 0x0E...0x0F => blk: {
const value = self.pak.backup.read(address); const value = self.pak.backup.read(address);
@ -153,29 +112,100 @@ pub fn read(self: *Self, comptime T: type, address: u32) T {
break :blk @as(T, value) * multiplier; break :blk @as(T, value) * multiplier;
}, },
else => readOpenBus(self, T, address), else => self.readOpenBus(T, address),
};
}
fn readOpenBus(self: *const Self, comptime T: type, address: u32) T {
const r15 = self.cpu.?.r[15];
const word = if (self.cpu.?.cpsr.t.read()) blk: {
const page = @truncate(u8, r15 >> 24);
switch (page) {
// EWRAM, PALRAM, VRAM, and Game ROM (16-bit)
0x02, 0x05, 0x06, 0x08...0x0D => {
const halfword = self.dbgRead(u16, r15 + 2);
break :blk @as(u32, halfword) << 16 | halfword;
},
// BIOS or OAM (32-bit)
0x00, 0x07 => {
const offset: u32 = if (address & 3 == 0b00) 2 else 0;
break :blk @as(u32, self.dbgRead(u16, (r15 + 2) + offset)) << 16 | self.dbgRead(u16, r15 + offset);
},
// IWRAM (16-bit but special)
0x03 => {
const offset: u32 = if (address & 3 == 0b00) 2 else 0;
break :blk @as(u32, self.dbgRead(u16, (r15 + 2) - offset)) << 16 | self.dbgRead(u16, r15 + offset);
},
else => unreachable,
}
} else self.dbgRead(u32, r15 + 4);
return @truncate(T, rotr(u32, word, 8 * (address & 3)));
}
pub fn read(self: *Self, comptime T: type, address: u32) T {
const page = @truncate(u8, address >> 24);
const aligned_addr = forceAlign(T, address);
self.sched.tick += timings[@boolToInt(T == u32)][@truncate(u4, page)];
return switch (page) {
// General Internal Memory
0x00 => blk: {
if (address < Bios.size)
break :blk self.bios.read(T, self.cpu.?.r[15], aligned_addr);
break :blk self.readOpenBus(T, address);
},
0x02 => self.ewram.read(T, aligned_addr),
0x03 => self.iwram.read(T, aligned_addr),
0x04 => io.read(self, T, aligned_addr),
// Internal Display Memory
0x05 => self.ppu.palette.read(T, aligned_addr),
0x06 => self.ppu.vram.read(T, aligned_addr),
0x07 => self.ppu.oam.read(T, aligned_addr),
// External Memory (Game Pak)
0x08...0x0D => self.pak.read(T, aligned_addr),
0x0E...0x0F => blk: {
const value = self.pak.backup.read(address);
const multiplier = switch (T) {
u32 => 0x01010101,
u16 => 0x0101,
u8 => 1,
else => @compileError("Backup: Unsupported read width"),
};
break :blk @as(T, value) * multiplier;
},
else => self.readOpenBus(T, address),
}; };
} }
pub fn write(self: *Self, comptime T: type, address: u32, value: T) void { pub fn write(self: *Self, comptime T: type, address: u32, value: T) void {
const page = @truncate(u8, address >> 24); const page = @truncate(u8, address >> 24);
const align_addr = alignAddress(T, address); const aligned_addr = forceAlign(T, address);
defer self.sched.tick += timings[@boolToInt(T == u32)][@truncate(u4, page)];
self.sched.tick += timings[@boolToInt(T == u32)][@truncate(u4, page)];
switch (page) { switch (page) {
// General Internal Memory // General Internal Memory
0x00 => self.bios.write(T, align_addr, value), 0x00 => self.bios.write(T, aligned_addr, value),
0x02 => self.ewram.write(T, align_addr, value), 0x02 => self.ewram.write(T, aligned_addr, value),
0x03 => self.iwram.write(T, align_addr, value), 0x03 => self.iwram.write(T, aligned_addr, value),
0x04 => io.write(self, T, align_addr, value), 0x04 => io.write(self, T, aligned_addr, value),
// Internal Display Memory // Internal Display Memory
0x05 => self.ppu.palette.write(T, align_addr, value), 0x05 => self.ppu.palette.write(T, aligned_addr, value),
0x06 => self.ppu.vram.write(T, self.ppu.dispcnt, align_addr, value), 0x06 => self.ppu.vram.write(T, self.ppu.dispcnt, aligned_addr, value),
0x07 => self.ppu.oam.write(T, align_addr, value), 0x07 => self.ppu.oam.write(T, aligned_addr, value),
// External Memory (Game Pak) // External Memory (Game Pak)
0x08...0x0D => self.pak.write(T, self.dma[3].word_count, align_addr, value), 0x08...0x0D => self.pak.write(T, self.dma[3].word_count, aligned_addr, value),
0x0E...0x0F => { 0x0E...0x0F => {
const rotate_by = switch (T) { const rotate_by = switch (T) {
u32 => address & 3, u32 => address & 3,
@ -190,7 +220,7 @@ pub fn write(self: *Self, comptime T: type, address: u32, value: T) void {
} }
} }
fn alignAddress(comptime T: type, address: u32) u32 { fn forceAlign(comptime T: type, address: u32) u32 {
return switch (T) { return switch (T) {
u32 => address & 0xFFFF_FFFC, u32 => address & 0xFFFF_FFFC,
u16 => address & 0xFFFF_FFFE, u16 => address & 0xFFFF_FFFE,

View File

@ -31,17 +31,22 @@ pub fn deinit(self: Self) void {
if (self.buf) |buf| self.alloc.free(buf); if (self.buf) |buf| self.alloc.free(buf);
} }
pub fn checkedRead(self: *Self, comptime T: type, r15: u32, addr: u32) T { pub fn read(self: *Self, comptime T: type, r15: u32, addr: u32) T {
if (r15 < Self.size) { if (r15 < Self.size) {
self.addr_latch = addr; self.addr_latch = addr;
return self.read(T, addr); return self.uncheckedRead(T, addr);
} }
log.debug("Rejected read since r15=0x{X:0>8}", .{r15}); log.debug("Rejected read since r15=0x{X:0>8}", .{r15});
return @truncate(T, self.read(T, self.addr_latch + 8)); return @truncate(T, self.uncheckedRead(T, self.addr_latch + 8));
} }
fn read(self: *const Self, comptime T: type, addr: u32) T { pub fn dbgRead(self: *const Self, comptime T: type, r15: u32, addr: u32) T {
if (r15 < Self.size) return self.uncheckedRead(T, addr);
return @truncate(T, self.uncheckedRead(T, self.addr_latch + 8));
}
fn uncheckedRead(self: *const Self, comptime T: type, addr: u32) T {
if (self.buf) |buf| { if (self.buf) |buf| {
return switch (T) { return switch (T) {
u32, u16, u8 => std.mem.readIntSliceLittle(T, buf[addr..][0..@sizeOf(T)]), u32, u16, u8 => std.mem.readIntSliceLittle(T, buf[addr..][0..@sizeOf(T)]),

View File

@ -90,6 +90,33 @@ pub fn read(self: *Self, comptime T: type, address: u32) T {
}; };
} }
pub fn dbgRead(self: *const Self, comptime T: type, address: u32) T {
const addr = address & 0x1FF_FFFF;
if (self.backup.kind == .Eeprom) {
if (self.isLarge()) {
// Addresses 0x1FF_FF00 to 0x1FF_FFFF are reserved from EEPROM accesses if
// * Backup type is EEPROM
// * Large ROM (Size is greater than 16MB)
if (addr > 0x1FF_FEFF)
return self.backup.eeprom.dbgRead();
} else {
// Addresses 0x0D00_0000 to 0x0DFF_FFFF are reserved for EEPROM accesses if
// * Backup type is EEPROM
// * Small ROM (less than 16MB)
if (@truncate(u8, address >> 24) == 0x0D)
return self.backup.eeprom.dbgRead();
}
}
return switch (T) {
u32 => (@as(T, self.get(addr + 3)) << 24) | (@as(T, self.get(addr + 2)) << 16) | (@as(T, self.get(addr + 1)) << 8) | (@as(T, self.get(addr))),
u16 => (@as(T, self.get(addr + 1)) << 8) | @as(T, self.get(addr)),
u8 => self.get(addr),
else => @compileError("GamePak: Unsupported read width"),
};
}
pub fn write(self: *Self, comptime T: type, word_count: u16, address: u32, value: T) void { pub fn write(self: *Self, comptime T: type, word_count: u16, address: u32, value: T) void {
const addr = address & 0x1FF_FFFF; const addr = address & 0x1FF_FFFF;

View File

@ -340,6 +340,10 @@ const Eeprom = struct {
return self.reader.read(); return self.reader.read();
} }
pub fn dbgRead(self: *const Self) u1 {
return self.reader.dbgRead();
}
pub fn write(self: *Self, word_count: u16, buf: *[]u8, bit: u1) void { pub fn write(self: *Self, word_count: u16, buf: *[]u8, bit: u1) void {
if (self.guessKind(word_count)) |found| { if (self.guessKind(word_count)) |found| {
log.info("EEPROM Kind: {}", .{found}); log.info("EEPROM Kind: {}", .{found});
@ -492,6 +496,19 @@ const Eeprom = struct {
return bit; return bit;
} }
fn dbgRead(self: *const This) u1 {
if (!self.enabled) return 1;
const bit = if (self.i < 4) blk: {
break :blk 0;
} else blk: {
const idx = @intCast(u6, 63 - (self.i - 4));
break :blk @truncate(u1, self.data >> idx);
};
return bit;
}
}; };
const Writer = struct { const Writer = struct {

View File

@ -6,6 +6,7 @@ const Bit = @import("bitfield").Bit;
const Bitfield = @import("bitfield").Bitfield; const Bitfield = @import("bitfield").Bitfield;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const FilePaths = @import("util.zig").FilePaths; const FilePaths = @import("util.zig").FilePaths;
const Logger = @import("util.zig").Logger;
const File = std.fs.File; const File = std.fs.File;
@ -225,13 +226,14 @@ pub const thumb = struct {
} }
}; };
const enable_logging = false; const cpu_logging = @import("emu.zig").cpu_logging;
const log = std.log.scoped(.Arm7Tdmi); const log = std.log.scoped(.Arm7Tdmi);
pub const Arm7tdmi = struct { pub const Arm7tdmi = struct {
const Self = @This(); const Self = @This();
r: [16]u32, r: [16]u32,
pipe: Pipline,
sched: *Scheduler, sched: *Scheduler,
bus: *Bus, bus: *Bus,
cpsr: PSR, cpsr: PSR,
@ -247,13 +249,12 @@ pub const Arm7tdmi = struct {
banked_spsr: [5]PSR, banked_spsr: [5]PSR,
log_file: ?*const File, logger: ?Logger,
log_buf: [0x100]u8,
binary_log: bool,
pub fn init(sched: *Scheduler, bus: *Bus) Self { pub fn init(sched: *Scheduler, bus: *Bus) Self {
return Self{ return Self{
.r = [_]u32{0x00} ** 16, .r = [_]u32{0x00} ** 16,
.pipe = Pipline.init(),
.sched = sched, .sched = sched,
.bus = bus, .bus = bus,
.cpsr = .{ .raw = 0x0000_001F }, .cpsr = .{ .raw = 0x0000_001F },
@ -261,15 +262,12 @@ pub const Arm7tdmi = struct {
.banked_fiq = [_]u32{0x00} ** 10, .banked_fiq = [_]u32{0x00} ** 10,
.banked_r = [_]u32{0x00} ** 12, .banked_r = [_]u32{0x00} ** 12,
.banked_spsr = [_]PSR{.{ .raw = 0x0000_0000 }} ** 5, .banked_spsr = [_]PSR{.{ .raw = 0x0000_0000 }} ** 5,
.log_file = null, .logger = null,
.log_buf = undefined,
.binary_log = false,
}; };
} }
pub fn useLogger(self: *Self, file: *const File, is_binary: bool) void { pub fn attach(self: *Self, log_file: std.fs.File) void {
self.log_file = file; self.logger = Logger.init(log_file);
self.binary_log = is_binary;
} }
inline fn bankedIdx(mode: Mode, kind: BankedKind) usize { inline fn bankedIdx(mode: Mode, kind: BankedKind) usize {
@ -320,8 +318,21 @@ pub const Arm7tdmi = struct {
return self.bus.io.haltcnt == .Halt; return self.bus.io.haltcnt == .Halt;
} }
pub fn setCpsrNoFlush(self: *Self, value: u32) void {
if (value & 0x1F != self.cpsr.raw & 0x1F) self.changeModeFromIdx(@truncate(u5, value & 0x1F));
self.cpsr.raw = value;
}
pub fn setCpsr(self: *Self, value: u32) void { pub fn setCpsr(self: *Self, value: u32) void {
if (value & 0x1F != self.cpsr.raw & 0x1F) self.changeModeFromIdx(@truncate(u5, value & 0x1F)); if (value & 0x1F != self.cpsr.raw & 0x1F) self.changeModeFromIdx(@truncate(u5, value & 0x1F));
const new: PSR = .{ .raw = value };
if (self.cpsr.t.read() != new.t.read()) {
// If THUMB to ARM or ARM to THUMB, flush pipeline
self.r[15] &= if (new.t.read()) ~@as(u32, 1) else ~@as(u32, 3);
self.pipe.flush();
}
self.cpsr.raw = value; self.cpsr.raw = value;
} }
@ -424,19 +435,22 @@ pub const Arm7tdmi = struct {
} }
pub fn step(self: *Self) void { pub fn step(self: *Self) void {
if (self.cpsr.t.read()) { if (self.cpsr.t.read()) blk: {
const opcode = self.fetch(u16); const opcode = @truncate(u16, self.pipe.step(self, u16) orelse break :blk);
if (enable_logging) if (self.log_file) |file| self.debug_log(file, opcode); if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
thumb.lut[thumbIdx(opcode)](self, self.bus, opcode); thumb.lut[thumbIdx(opcode)](self, self.bus, opcode);
} else { } else blk: {
const opcode = self.fetch(u32); const opcode = self.pipe.step(self, u32) orelse break :blk;
if (enable_logging) if (self.log_file) |file| self.debug_log(file, opcode); if (cpu_logging) self.logger.?.mgbaLog(self, opcode);
if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) { if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) {
arm.lut[armIdx(opcode)](self, self.bus, opcode); arm.lut[armIdx(opcode)](self, self.bus, opcode);
} }
} }
if (!self.pipe.flushed) self.r[15] += if (self.cpsr.t.read()) 2 else @as(u32, 4);
self.pipe.flushed = false;
} }
pub fn stepDmaTransfer(self: *Self) bool { pub fn stepDmaTransfer(self: *Self) bool {
@ -471,27 +485,26 @@ pub const Arm7tdmi = struct {
pub fn handleInterrupt(self: *Self) void { pub fn handleInterrupt(self: *Self) void {
const should_handle = self.bus.io.ie.raw & self.bus.io.irq.raw; const should_handle = self.bus.io.ie.raw & self.bus.io.irq.raw;
if (should_handle != 0) { // Return if IME is disabled, CPSR I is set or there is nothing to handle
self.bus.io.haltcnt = .Execute; if (!self.bus.io.ime or self.cpsr.i.read() or should_handle == 0) return;
// log.debug("An Interrupt was Fired!", .{});
// Either IME is not true or I in CPSR is true // If pipeline isn't full, return but reschedule the handling of the event
// Don't handle interrupts if (!self.pipe.isFull()) return;
if (!self.bus.io.ime or self.cpsr.i.read()) return;
// log.debug("An interrupt was Handled!", .{});
// retAddr.gba says r15 on it's own is off by -04h in both ARM and THUMB mode // log.debug("Handling Interrupt!", .{});
const r15 = self.r[15] + 4; self.bus.io.haltcnt = .Execute;
const cpsr = self.cpsr.raw;
self.changeMode(.Irq); const ret_addr = self.r[15] - if (self.cpsr.t.read()) 2 else @as(u32, 4);
self.cpsr.t.write(false); const new_spsr = self.cpsr.raw;
self.cpsr.i.write(true);
self.r[14] = r15; self.changeMode(.Irq);
self.spsr.raw = cpsr; self.cpsr.t.write(false);
self.r[15] = 0x000_0018; self.cpsr.i.write(true);
}
self.r[14] = ret_addr;
self.spsr.raw = new_spsr;
self.r[15] = 0x0000_0018;
self.pipe.flush();
} }
inline fn fetch(self: *Self, comptime T: type) T { inline fn fetch(self: *Self, comptime T: type) T {
@ -505,10 +518,6 @@ pub const Arm7tdmi = struct {
return self.bus.read(T, self.r[15]); return self.bus.read(T, self.r[15]);
} }
pub fn fakePC(self: *const Self) u32 {
return self.r[15] + 4;
}
fn debug_log(self: *const Self, file: *const File, opcode: u32) void { fn debug_log(self: *const Self, file: *const File, opcode: u32) void {
if (self.binary_log) { if (self.binary_log) {
self.skyLog(file) catch unreachable; self.skyLog(file) catch unreachable;
@ -532,11 +541,11 @@ pub const Arm7tdmi = struct {
prettyPrintPsr(&self.spsr); prettyPrintPsr(&self.spsr);
if (self.cpsr.t.read()) { if (self.cpsr.t.read()) {
const opcode = self.bus.debugRead(u16, self.r[15] - 4); const opcode = self.bus.dbgRead(u16, self.r[15] - 4);
const id = thumbIdx(opcode); const id = thumbIdx(opcode);
std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode }); std.debug.print("opcode: ID: 0x{b:0>10} 0x{X:0>4}\n", .{ id, opcode });
} else { } else {
const opcode = self.bus.debugRead(u32, self.r[15] - 4); const opcode = self.bus.dbgRead(u32, self.r[15] - 4);
const id = armIdx(opcode); const id = armIdx(opcode);
std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode }); std.debug.print("opcode: ID: 0x{X:0>3} 0x{X:0>8}\n", .{ id, opcode });
} }
@ -574,25 +583,6 @@ pub const Arm7tdmi = struct {
}; };
} }
fn skyLog(self: *const Self, file: *const File) !void {
var buf: [18 * @sizeOf(u32)]u8 = undefined;
// Write Registers
var i: usize = 0;
while (i < 0x10) : (i += 1) {
skyWrite(&buf, i, self.r[i]);
}
skyWrite(&buf, 0x10, self.cpsr.raw);
skyWrite(&buf, 0x11, if (self.hasSPSR()) self.spsr.raw else self.cpsr.raw);
_ = try file.writeAll(&buf);
}
fn skyWrite(buf: []u8, i: usize, num: u32) void {
const j = @sizeOf(u32) * i;
std.mem.writeIntSliceNative(u32, buf[j..(j + @sizeOf(u32))], num);
}
fn mgbaLog(self: *const Self, file: *const File, opcode: u32) !void { fn mgbaLog(self: *const Self, file: *const File, opcode: u32) !void {
const thumb_fmt = "{X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} cpsr: {X:0>8} | {X:0>4}:\n"; const thumb_fmt = "{X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} cpsr: {X:0>8} | {X:0>4}:\n";
const arm_fmt = "{X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} cpsr: {X:0>8} | {X:0>8}:\n"; const arm_fmt = "{X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} cpsr: {X:0>8} | {X:0>8}:\n";
@ -613,7 +603,7 @@ pub const Arm7tdmi = struct {
const r12 = self.r[12]; const r12 = self.r[12];
const r13 = self.r[13]; const r13 = self.r[13];
const r14 = self.r[14]; const r14 = self.r[14];
const r15 = self.r[15]; const r15 = self.r[15] -| if (self.cpsr.t.read()) 2 else @as(u32, 4);
const c_psr = self.cpsr.raw; const c_psr = self.cpsr.raw;
@ -621,7 +611,7 @@ pub const Arm7tdmi = struct {
if (self.cpsr.t.read()) { if (self.cpsr.t.read()) {
if (opcode >> 11 == 0x1E) { if (opcode >> 11 == 0x1E) {
// Instruction 1 of a BL Opcode, print in ARM mode // Instruction 1 of a BL Opcode, print in ARM mode
const other_half = self.bus.debugRead(u16, self.r[15]); const other_half = self.bus.debugRead(u16, self.r[15] - 2);
const bl_opcode = @as(u32, opcode) << 16 | other_half; const bl_opcode = @as(u32, opcode) << 16 | other_half;
log_str = try std.fmt.bufPrint(&buf, arm_fmt, .{ r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, c_psr, bl_opcode }); log_str = try std.fmt.bufPrint(&buf, arm_fmt, .{ r0, r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, c_psr, bl_opcode });
@ -665,6 +655,48 @@ pub fn checkCond(cpsr: PSR, cond: u4) bool {
}; };
} }
const Pipline = struct {
const Self = @This();
stage: [2]?u32,
flushed: bool,
fn init() Self {
return .{
.stage = [_]?u32{null} ** 2,
.flushed = false,
};
}
pub fn flush(self: *Self) void {
for (self.stage) |*opcode| opcode.* = null;
self.flushed = true;
}
pub fn isFull(self: *const Self) bool {
return self.stage[0] != null and self.stage[1] != null;
}
pub fn step(self: *Self, cpu: *Arm7tdmi, comptime T: type) ?u32 {
comptime std.debug.assert(T == u32 or T == u16);
const opcode = self.stage[0];
self.stage[0] = self.stage[1];
self.stage[1] = cpu.bus.read(T, cpu.r[15]);
return opcode;
}
fn reload(self: *Self, cpu: *Arm7tdmi, comptime T: type) void {
comptime std.debug.assert(T == u32 or T == u16);
const inc = if (T == u32) 4 else 2;
self.stage[0] = cpu.bus.read(T, cpu.r[15]);
self.stage[1] = cpu.bus.read(T, cpu.r[15] + inc);
cpu.r[15] += inc * 2;
}
};
pub const PSR = extern union { pub const PSR = extern union {
mode: Bitfield(u32, 0, 5), mode: Bitfield(u32, 0, 5),
t: Bit(u32, 5), t: Bit(u32, 5),

View File

@ -55,8 +55,10 @@ pub fn blockDataTransfer(comptime P: bool, comptime U: bool, comptime S: bool, c
if (L) { if (L) {
cpu.r[15] = bus.read(u32, und_addr); cpu.r[15] = bus.read(u32, und_addr);
cpu.pipe.flush();
} else { } else {
bus.write(u32, und_addr, cpu.r[15] + 8); // FIXME: Should r15 on write be +12 ahead?
bus.write(u32, und_addr, cpu.r[15] + 4);
} }
cpu.r[rn] = if (U) cpu.r[rn] + 0x40 else cpu.r[rn] - 0x40; cpu.r[rn] = if (U) cpu.r[rn] + 0x40 else cpu.r[rn] - 0x40;
@ -86,17 +88,23 @@ pub fn blockDataTransfer(comptime P: bool, comptime U: bool, comptime S: bool, c
cpu.setUserModeRegister(i, bus.read(u32, address)); cpu.setUserModeRegister(i, bus.read(u32, address));
} else { } else {
const value = bus.read(u32, address); const value = bus.read(u32, address);
cpu.r[i] = if (i == 0xF) value & 0xFFFF_FFFC else value;
if (S and i == 0xF) cpu.setCpsr(cpu.spsr.raw); cpu.r[i] = value;
if (i == 0xF) {
cpu.r[i] &= ~@as(u32, 3); // Align r15
cpu.pipe.flush();
if (S) cpu.setCpsr(cpu.spsr.raw);
}
} }
} else { } else {
if (S) { if (S) {
// Always Transfer User mode Registers // Always Transfer User mode Registers
// This happens regardless if r15 is in the list // This happens regardless if r15 is in the list
const value = cpu.getUserModeRegister(i); const value = cpu.getUserModeRegister(i);
bus.write(u32, address, value + if (i == 0xF) 8 else @as(u32, 0)); // PC is already 4 ahead to make 12 bus.write(u32, address, value + if (i == 0xF) 4 else @as(u32, 0)); // PC is already 8 ahead to make 12
} else { } else {
bus.write(u32, address, cpu.r[i] + if (i == 0xF) 8 else @as(u32, 0)); bus.write(u32, address, cpu.r[i] + if (i == 0xF) 4 else @as(u32, 0));
} }
} }
} }

View File

@ -9,14 +9,19 @@ const sext = @import("../../util.zig").sext;
pub fn branch(comptime L: bool) InstrFn { pub fn branch(comptime L: bool) InstrFn {
return struct { return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void { fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
if (L) cpu.r[14] = cpu.r[15]; if (L) cpu.r[14] = cpu.r[15] - 4;
cpu.r[15] = cpu.fakePC() +% (sext(u32, u24, opcode) << 2);
cpu.r[15] +%= sext(u32, u24, opcode) << 2;
cpu.pipe.flush();
} }
}.inner; }.inner;
} }
pub fn branchAndExchange(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void { pub fn branchAndExchange(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
const rn = opcode & 0xF; const rn = opcode & 0xF;
cpu.cpsr.t.write(cpu.r[rn] & 1 == 1);
cpu.r[15] = cpu.r[rn] & 0xFFFF_FFFE; const thumb = cpu.r[rn] & 1 == 1;
cpu.r[15] = cpu.r[rn] & if (thumb) ~@as(u32, 1) else ~@as(u32, 3);
cpu.cpsr.t.write(thumb);
cpu.pipe.flush();
} }

View File

@ -13,17 +13,12 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
const old_carry = @boolToInt(cpu.cpsr.c.read()); const old_carry = @boolToInt(cpu.cpsr.c.read());
// If certain conditions are met, PC is 12 ahead instead of 8 // If certain conditions are met, PC is 12 ahead instead of 8
// TODO: What are these conditions? I can't remember
if (!I and opcode >> 4 & 1 == 1) cpu.r[15] += 4; if (!I and opcode >> 4 & 1 == 1) cpu.r[15] += 4;
const op1 = cpu.r[rn];
const op1 = if (rn == 0xF) cpu.fakePC() else cpu.r[rn]; const amount = @truncate(u8, (opcode >> 8 & 0xF) << 1);
const op2 = if (I) rotateRight(S, &cpu.cpsr, opcode & 0xFF, amount) else execute(S, cpu, opcode);
var op2: u32 = undefined;
if (I) {
const amount = @truncate(u8, (opcode >> 8 & 0xF) << 1);
op2 = rotateRight(S, &cpu.cpsr, opcode & 0xFF, amount);
} else {
op2 = execute(S, cpu, opcode);
}
// Undo special condition from above // Undo special condition from above
if (!I and opcode >> 4 & 1 == 1) cpu.r[15] -= 4; if (!I and opcode >> 4 & 1 == 1) cpu.r[15] -= 4;
@ -67,39 +62,31 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
}, },
0x8 => { 0x8 => {
// TST // TST
if (rd == 0xF) { if (rd == 0xF)
undefinedTestBehaviour(cpu); return undefinedTestBehaviour(cpu);
return;
}
const result = op1 & op2; const result = op1 & op2;
setTestOpFlags(S, cpu, opcode, result); setTestOpFlags(S, cpu, opcode, result);
}, },
0x9 => { 0x9 => {
// TEQ // TEQ
if (rd == 0xF) { if (rd == 0xF)
undefinedTestBehaviour(cpu); return undefinedTestBehaviour(cpu);
return;
}
const result = op1 ^ op2; const result = op1 ^ op2;
setTestOpFlags(S, cpu, opcode, result); setTestOpFlags(S, cpu, opcode, result);
}, },
0xA => { 0xA => {
// CMP // CMP
if (rd == 0xF) { if (rd == 0xF)
undefinedTestBehaviour(cpu); return undefinedTestBehaviour(cpu);
return;
}
cmp(cpu, op1, op2); cmp(cpu, op1, op2);
}, },
0xB => { 0xB => {
// CMN // CMN
if (rd == 0xF) { if (rd == 0xF)
undefinedTestBehaviour(cpu); return undefinedTestBehaviour(cpu);
return;
}
cmn(cpu, op1, op2); cmn(cpu, op1, op2);
}, },
@ -127,6 +114,8 @@ pub fn dataProcessing(comptime I: bool, comptime S: bool, comptime instrKind: u4
setArmLogicOpFlags(S, cpu, rd, result); setArmLogicOpFlags(S, cpu, rd, result);
}, },
} }
if (rd == 0xF) cpu.pipe.flush();
} }
}.inner; }.inner;
} }
@ -280,5 +269,5 @@ fn setTestOpFlags(comptime S: bool, cpu: *Arm7tdmi, opcode: u32, result: u32) vo
fn undefinedTestBehaviour(cpu: *Arm7tdmi) void { fn undefinedTestBehaviour(cpu: *Arm7tdmi) void {
@setCold(true); @setCold(true);
cpu.setCpsr(cpu.spsr.raw); cpu.setCpsrNoFlush(cpu.spsr.raw);
} }

View File

@ -15,20 +15,8 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
const rm = opcode & 0xF; const rm = opcode & 0xF;
const imm_offset_high = opcode >> 8 & 0xF; const imm_offset_high = opcode >> 8 & 0xF;
var base: u32 = undefined; const base = cpu.r[rn] + if (!L and rn == 0xF) 4 else @as(u32, 0);
if (rn == 0xF) { const offset = if (I) imm_offset_high << 4 | rm else cpu.r[rm];
base = cpu.fakePC();
if (!L) base += 4;
} else {
base = cpu.r[rn];
}
var offset: u32 = undefined;
if (I) {
offset = imm_offset_high << 4 | rm;
} else {
offset = cpu.r[rm];
}
const modified_base = if (U) base +% offset else base -% offset; const modified_base = if (U) base +% offset else base -% offset;
var address = if (P) modified_base else base; var address = if (P) modified_base else base;

View File

@ -14,13 +14,8 @@ pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool,
const rn = opcode >> 16 & 0xF; const rn = opcode >> 16 & 0xF;
const rd = opcode >> 12 & 0xF; const rd = opcode >> 12 & 0xF;
var base: u32 = undefined; // rn is r15 and L is not set, the PC is 12 ahead
if (rn == 0xF) { const base = cpu.r[rn] + if (!L and rn == 0xF) 4 else @as(u32, 0);
base = cpu.fakePC();
if (!L) base += 4; // Offset of 12
} else {
base = cpu.r[rn];
}
const offset = if (I) shifter.immShift(false, cpu, opcode) else opcode & 0xFFF; const offset = if (I) shifter.immShift(false, cpu, opcode) else opcode & 0xFFF;
@ -40,18 +35,26 @@ pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool,
} else { } else {
if (B) { if (B) {
// STRB // STRB
const value = if (rd == 0xF) cpu.r[rd] + 8 else cpu.r[rd]; const value = cpu.r[rd] + if (rd == 0xF) 4 else @as(u32, 0); // PC is 12 ahead
bus.write(u8, address, @truncate(u8, value)); bus.write(u8, address, @truncate(u8, value));
} else { } else {
// STR // STR
const value = if (rd == 0xF) cpu.r[rd] + 8 else cpu.r[rd]; const value = cpu.r[rd] + if (rd == 0xF) 4 else @as(u32, 0);
bus.write(u32, address, value); bus.write(u32, address, value);
} }
} }
address = modified_base; address = modified_base;
if (W and P or !P) cpu.r[rn] = address; if (W and P or !P) {
if (L) cpu.r[rd] = result; // This emulates the LDR rd == rn behaviour cpu.r[rn] = address;
if (rn == 0xF) cpu.pipe.flush();
}
if (L) {
// This emulates the LDR rd == rn behaviour
cpu.r[rd] = result;
if (rd == 0xF) cpu.pipe.flush();
}
} }
}.inner; }.inner;
} }

View File

@ -6,7 +6,7 @@ pub fn armSoftwareInterrupt() InstrFn {
return struct { return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, _: u32) void { fn inner(cpu: *Arm7tdmi, _: *Bus, _: u32) void {
// Copy Values from Current Mode // Copy Values from Current Mode
const r15 = cpu.r[15]; const ret_addr = cpu.r[15] - 4;
const cpsr = cpu.cpsr.raw; const cpsr = cpu.cpsr.raw;
// Switch Mode // Switch Mode
@ -14,9 +14,10 @@ pub fn armSoftwareInterrupt() InstrFn {
cpu.cpsr.t.write(false); // Force ARM Mode cpu.cpsr.t.write(false); // Force ARM Mode
cpu.cpsr.i.write(true); // Disable normal interrupts cpu.cpsr.i.write(true); // Disable normal interrupts
cpu.r[14] = r15; // Resume Execution cpu.r[14] = ret_addr; // Resume Execution
cpu.spsr.raw = cpsr; // Previous mode CPSR cpu.spsr.raw = cpsr; // Previous mode CPSR
cpu.r[15] = 0x0000_0008; cpu.r[15] = 0x0000_0008;
cpu.pipe.flush();
} }
}.inner; }.inner;
} }

View File

@ -18,11 +18,9 @@ pub fn execute(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
fn registerShift(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 { fn registerShift(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
const rs_idx = opcode >> 8 & 0xF; const rs_idx = opcode >> 8 & 0xF;
const rm = cpu.r[opcode & 0xF];
const rs = @truncate(u8, cpu.r[rs_idx]); const rs = @truncate(u8, cpu.r[rs_idx]);
const rm_idx = opcode & 0xF;
const rm = if (rm_idx == 0xF) cpu.fakePC() else cpu.r[rm_idx];
return switch (@truncate(u2, opcode >> 5)) { return switch (@truncate(u2, opcode >> 5)) {
0b00 => logicalLeft(S, &cpu.cpsr, rm, rs), 0b00 => logicalLeft(S, &cpu.cpsr, rm, rs),
0b01 => logicalRight(S, &cpu.cpsr, rm, rs), 0b01 => logicalRight(S, &cpu.cpsr, rm, rs),
@ -33,9 +31,7 @@ fn registerShift(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
pub fn immShift(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 { pub fn immShift(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
const amount = @truncate(u8, opcode >> 7 & 0x1F); const amount = @truncate(u8, opcode >> 7 & 0x1F);
const rm = cpu.r[opcode & 0xF];
const rm_idx = opcode & 0xF;
const rm = if (rm_idx == 0xF) cpu.fakePC() else cpu.r[rm_idx];
var result: u32 = undefined; var result: u32 = undefined;
if (amount == 0) { if (amount == 0) {

View File

@ -33,7 +33,8 @@ pub fn fmt14(comptime L: bool, comptime R: bool) InstrFn {
if (R) { if (R) {
if (L) { if (L) {
const value = bus.read(u32, address); const value = bus.read(u32, address);
cpu.r[15] = value & 0xFFFF_FFFE; cpu.r[15] = value & ~@as(u32, 1);
cpu.pipe.flush();
} else { } else {
bus.write(u32, address, cpu.r[14]); bus.write(u32, address, cpu.r[14]);
} }
@ -52,7 +53,13 @@ pub fn fmt15(comptime L: bool, comptime rb: u3) InstrFn {
const end_address = cpu.r[rb] + 4 * countRlist(opcode); const end_address = cpu.r[rb] + 4 * countRlist(opcode);
if (opcode & 0xFF == 0) { if (opcode & 0xFF == 0) {
if (L) cpu.r[15] = bus.read(u32, address) else bus.write(u32, address, cpu.r[15] + 4); if (L) {
cpu.r[15] = bus.read(u32, address);
cpu.pipe.flush();
} else {
bus.write(u32, address, cpu.r[15] + 2);
}
cpu.r[rb] += 0x40; cpu.r[rb] += 0x40;
return; return;
} }

View File

@ -9,16 +9,13 @@ pub fn fmt16(comptime cond: u4) InstrFn {
return struct { return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void { fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
// B // B
const offset = sext(u32, u8, opcode & 0xFF) << 1; if (cond == 0xE or cond == 0xF)
cpu.panic("[CPU/THUMB.16] Undefined conditional branch with condition {}", .{cond});
const should_execute = switch (cond) { if (!checkCond(cpu.cpsr, cond)) return;
0xE, 0xF => cpu.panic("[CPU/THUMB.16] Undefined conditional branch with condition {}", .{cond}),
else => checkCond(cpu.cpsr, cond),
};
if (should_execute) { cpu.r[15] +%= sext(u32, u8, opcode & 0xFF) << 1;
cpu.r[15] = (cpu.r[15] + 2) +% offset; cpu.pipe.flush();
}
} }
}.inner; }.inner;
} }
@ -27,8 +24,8 @@ pub fn fmt18() InstrFn {
return struct { return struct {
// B but conditional // B but conditional
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void { fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
const offset = sext(u32, u11, opcode & 0x7FF) << 1; cpu.r[15] +%= sext(u32, u11, opcode & 0x7FF) << 1;
cpu.r[15] = (cpu.r[15] + 2) +% offset; cpu.pipe.flush();
} }
}.inner; }.inner;
} }
@ -41,13 +38,16 @@ pub fn fmt19(comptime is_low: bool) InstrFn {
if (is_low) { if (is_low) {
// Instruction 2 // Instruction 2
const old_pc = cpu.r[15]; const next_opcode = cpu.r[15] - 2;
cpu.r[15] = cpu.r[14] +% (offset << 1); cpu.r[15] = cpu.r[14] +% (offset << 1);
cpu.r[14] = old_pc | 1; cpu.r[14] = next_opcode | 1;
cpu.pipe.flush();
} else { } else {
// Instruction 1 // Instruction 1
cpu.r[14] = (cpu.r[15] + 2) +% (sext(u32, u11, offset) << 12); const lr_offset = sext(u32, u11, offset) << 12;
cpu.r[14] = (cpu.r[15] +% lr_offset) & ~@as(u32, 1);
} }
} }
}.inner; }.inner;

View File

@ -133,10 +133,9 @@ pub fn fmt12(comptime isSP: bool, comptime rd: u3) InstrFn {
return struct { return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void { fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u16) void {
// ADD // ADD
const left = if (isSP) cpu.r[13] else (cpu.r[15] + 2) & 0xFFFF_FFFD; const left = if (isSP) cpu.r[13] else cpu.r[15] & ~@as(u32, 2);
const right = (opcode & 0xFF) << 2; const right = (opcode & 0xFF) << 2;
const result = left + right; cpu.r[rd] = left + right;
cpu.r[rd] = result;
} }
}.inner; }.inner;
} }

View File

@ -11,7 +11,9 @@ pub fn fmt6(comptime rd: u3) InstrFn {
fn inner(cpu: *Arm7tdmi, bus: *Bus, opcode: u16) void { fn inner(cpu: *Arm7tdmi, bus: *Bus, opcode: u16) void {
// LDR // LDR
const offset = (opcode & 0xFF) << 2; const offset = (opcode & 0xFF) << 2;
cpu.r[rd] = bus.read(u32, (cpu.r[15] + 2 & 0xFFFF_FFFD) + offset);
// Bit 1 of the PC intentionally ignored
cpu.r[rd] = bus.read(u32, (cpu.r[15] & ~@as(u32, 2)) + offset);
} }
}.inner; }.inner;
} }

View File

@ -6,7 +6,7 @@ pub fn fmt17() InstrFn {
return struct { return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, _: u16) void { fn inner(cpu: *Arm7tdmi, _: *Bus, _: u16) void {
// Copy Values from Current Mode // Copy Values from Current Mode
const r15 = cpu.r[15]; const ret_addr = cpu.r[15] - 2;
const cpsr = cpu.cpsr.raw; const cpsr = cpu.cpsr.raw;
// Switch Mode // Switch Mode
@ -14,9 +14,10 @@ pub fn fmt17() InstrFn {
cpu.cpsr.t.write(false); // Force ARM Mode cpu.cpsr.t.write(false); // Force ARM Mode
cpu.cpsr.i.write(true); // Disable normal interrupts cpu.cpsr.i.write(true); // Disable normal interrupts
cpu.r[14] = r15; // Resume Execution cpu.r[14] = ret_addr; // Resume Execution
cpu.spsr.raw = cpsr; // Previous mode CPSR cpu.spsr.raw = cpsr; // Previous mode CPSR
cpu.r[15] = 0x0000_0008; cpu.r[15] = 0x0000_0008;
cpu.pipe.flush();
} }
}.inner; }.inner;
} }

View File

@ -12,8 +12,11 @@ const Thread = std.Thread;
const Atomic = std.atomic.Atomic; const Atomic = std.atomic.Atomic;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const sync_audio = false; // TODO: Move these to a TOML File
const sync_video: RunKind = .UnlimitedFPS; const sync_audio = true; // Enable Audio Sync
const sync_video: RunKind = .LimitedFPS; // Configure Video Sync
pub const win_scale = 3; // 1x, 2x, 3x, etc. Window Scaling
pub const cpu_logging = false; // Enable detailed CPU logging
// 228 Lines which consist of 308 dots (which are 4 cycles long) // 228 Lines which consist of 308 dots (which are 4 cycles long)
const cycles_per_frame: u64 = 228 * (308 * 4); //280896 const cycles_per_frame: u64 = 228 * (308 * 4); //280896

View File

@ -1,6 +1,7 @@
const std = @import("std"); const std = @import("std");
const builtin = @import("builtin"); const builtin = @import("builtin");
const Log2Int = std.math.Log2Int; const Log2Int = std.math.Log2Int;
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
// Sign-Extend value of type `T` to type `U` // Sign-Extend value of type `T` to type `U`
pub fn sext(comptime T: type, comptime U: type, value: T) T { pub fn sext(comptime T: type, comptime U: type, value: T) T {
@ -112,3 +113,64 @@ pub fn writeUndefined(log: anytype, comptime format: []const u8, args: anytype)
log.warn(format, args); log.warn(format, args);
if (builtin.mode == .Debug) std.debug.panic("TODO: Implement I/O Register", .{}); if (builtin.mode == .Debug) std.debug.panic("TODO: Implement I/O Register", .{});
} }
pub const Logger = struct {
const Self = @This();
buf: std.io.BufferedWriter(4096 << 2, std.fs.File.Writer),
pub fn init(file: std.fs.File) Self {
return .{
.buf = .{ .unbuffered_writer = file.writer() },
};
}
pub fn print(self: *Self, comptime format: []const u8, args: anytype) !void {
try self.buf.writer().print(format, args);
}
pub fn mgbaLog(self: *Self, arm7tdmi: *const Arm7tdmi, opcode: u32) void {
const fmt_base = "{X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} {X:0>8} cpsr: {X:0>8} | ";
const thumb_fmt = fmt_base ++ "{X:0>4}:\n";
const arm_fmt = fmt_base ++ "{X:0>8}:\n";
if (arm7tdmi.cpsr.t.read()) {
if (opcode >> 11 == 0x1E) {
// Instruction 1 of a BL Opcode, print in ARM mode
const low = arm7tdmi.bus.dbgRead(u16, arm7tdmi.r[15]);
const bl_opcode = @as(u32, opcode) << 16 | low;
self.print(arm_fmt, Self.fmtArgs(arm7tdmi, bl_opcode)) catch @panic("failed to write to log file");
} else {
self.print(thumb_fmt, Self.fmtArgs(arm7tdmi, opcode)) catch @panic("failed to write to log file");
}
} else {
self.print(arm_fmt, Self.fmtArgs(arm7tdmi, opcode)) catch @panic("failed to write to log file");
}
}
fn fmtArgs(arm7tdmi: *const Arm7tdmi, opcode: u32) FmtArgTuple {
return .{
arm7tdmi.r[0],
arm7tdmi.r[1],
arm7tdmi.r[2],
arm7tdmi.r[3],
arm7tdmi.r[4],
arm7tdmi.r[5],
arm7tdmi.r[6],
arm7tdmi.r[7],
arm7tdmi.r[8],
arm7tdmi.r[9],
arm7tdmi.r[10],
arm7tdmi.r[11],
arm7tdmi.r[12],
arm7tdmi.r[13],
arm7tdmi.r[14],
arm7tdmi.r[15] - if (arm7tdmi.cpsr.t.read()) 2 else @as(u32, 4),
arm7tdmi.cpsr.raw,
opcode,
};
}
};
const FmtArgTuple = std.meta.Tuple(&.{ u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 });

View File

@ -14,6 +14,7 @@ const Allocator = std.mem.Allocator;
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;
const arm7tdmi_logging = @import("core/emu.zig").cpu_logging;
pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level; pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level;
// TODO: Reimpl Logging // TODO: Reimpl Logging
@ -48,6 +49,10 @@ pub fn main() anyerror!void {
var arm7tdmi = Arm7tdmi.init(&scheduler, &bus); var arm7tdmi = Arm7tdmi.init(&scheduler, &bus);
const log_file: ?std.fs.File = if (arm7tdmi_logging) try std.fs.cwd().createFile("zba.log", .{}) else null;
defer if (log_file) |file| file.close();
if (log_file) |file| arm7tdmi.attach(file);
bus.attach(&arm7tdmi); // TODO: Shrink Surface (only CPSR and r15?) bus.attach(&arm7tdmi); // TODO: Shrink Surface (only CPSR and r15?)
if (paths.bios == null) arm7tdmi.fastBoot(); if (paths.bios == null) arm7tdmi.fastBoot();