Compare commits

..

10 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka 6919322c8c fix: impl workaround for stage2 miscompilation 2022-08-26 19:23:50 -05:00
Rekai Nyangadzayi Musuka affd89c6d6 chore: instantly refill the pipeline on flush
I believe this to be necessary in order to get hardware interrupts
working.

thumb.gba test 108 fails but I'm committing anyways (despite the
regression) because this is kind of rebase/merge hell and I have
something that at least sort of works rn
2022-08-26 14:29:49 -05:00
Rekai Nyangadzayi Musuka 5c334057b5 fix: reimpl handleInterrupt code 2022-08-26 14:29:49 -05:00
Rekai Nyangadzayi Musuka cec0e7f072 feat: implement basic pipeline
passes arm.gba, thumb.gb and armwrestler, fails in actual games
TODO: run FuzzARM debug specific titles
2022-08-26 14:29:49 -05:00
Rekai Nyangadzayi Musuka 442e90e24d feat: resolve off-by-{word, halfword} errors when printing debug info 2022-08-26 14:29:49 -05:00
Rekai Nyangadzayi Musuka 960683a1f5 feat: reimplement cpu logging 2022-08-26 14:29:49 -05:00
Rekai Nyangadzayi Musuka aa52bb5917 chore: reorganize some code 2022-08-26 14:13:49 -05:00
Rekai Nyangadzayi Musuka e57f918856 chore: pass the allocator as an argument more often
As of right now, I think the only cases where I shouldn't explicitly pass an allocator
are in read/write functions and deinits
2022-08-26 13:54:38 -05:00
Rekai Nyangadzayi Musuka e5b7441740 fix: resolve use-afer-free in backup.zig
This worked fine on stage1, and works fine in debug in stage3.
However, stage3 ReleaseSafe would panic due to what I assume must
have been an undefined behaviour optimization.

While I'm happy that I was quickly made aware of the issue thanks to
the safety checks in ReleaseSafe I do wish that this issue showed itself
in Debug, since I *am* using the GPA
2022-08-26 13:04:09 -05:00
Rekai Nyangadzayi Musuka 2ab8769b7a feat: Get ZBA working on Zig's new stage2/stage3 compiler 2022-08-21 12:28:31 -05:00
7 changed files with 55 additions and 55 deletions

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

View File

@ -14,7 +14,7 @@ fn PtrCastPreserveCV(comptime T: type, comptime PtrToT: type, comptime NewT: typ
fn BitType(comptime FieldType: type, comptime ValueType: type, comptime shamt: usize) type { fn BitType(comptime FieldType: type, comptime ValueType: type, comptime shamt: usize) type {
const self_bit: FieldType = (1 << shamt); const self_bit: FieldType = (1 << shamt);
return struct { return extern struct {
bits: Bitfield(FieldType, shamt, 1), bits: Bitfield(FieldType, shamt, 1),
pub fn set(self: anytype) void { pub fn set(self: anytype) void {
@ -63,7 +63,7 @@ pub fn Bitfield(comptime FieldType: type, comptime shamt: usize, comptime num_bi
const ValueType = std.meta.Int(.unsigned, num_bits); const ValueType = std.meta.Int(.unsigned, num_bits);
return struct { return extern struct {
dummy: FieldType, dummy: FieldType,
fn field(self: anytype) PtrCastPreserveCV(@This(), @TypeOf(self), FieldType) { fn field(self: anytype) PtrCastPreserveCV(@This(), @TypeOf(self), FieldType) {

View File

@ -11,7 +11,6 @@ const pitch = @import("core/ppu.zig").framebuf_pitch;
const scale = @import("core/emu.zig").win_scale; 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 log = std.log.scoped(.GUI); const log = std.log.scoped(.GUI);
const default_title: []const u8 = "ZBA"; const default_title: []const u8 = "ZBA";

View File

@ -13,12 +13,12 @@ alloc: Allocator,
addr_latch: u32, addr_latch: u32,
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self { pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
var buf: ?[]u8 = null; const buf: ?[]u8 = if (maybe_path) |path| blk: {
if (maybe_path) |path| {
const file = try std.fs.cwd().openFile(path, .{}); const file = try std.fs.cwd().openFile(path, .{});
defer file.close(); defer file.close();
buf = try file.readToEndAlloc(alloc, try file.getEndPos());
} break :blk try file.readToEndAlloc(alloc, try file.getEndPos());
} else null;
return Self{ return Self{
.buf = buf, .buf = buf,

View File

@ -3,7 +3,7 @@ const Allocator = std.mem.Allocator;
const log = std.log.scoped(.Backup); const log = std.log.scoped(.Backup);
const escape = @import("../util.zig").escape; const escape = @import("../util.zig").escape;
const asString = @import("../util.zig").asString; const asStringSlice = @import("../util.zig").asStringSlice;
const backup_kinds = [5]Needle{ const backup_kinds = [5]Needle{
.{ .str = "EEPROM_V", .kind = .Eeprom }, .{ .str = "EEPROM_V", .kind = .Eeprom },
@ -18,7 +18,7 @@ pub const Backup = struct {
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
kind: BackupKind, kind: Kind,
title: [12]u8, title: [12]u8,
save_path: ?[]const u8, save_path: ?[]const u8,
@ -26,7 +26,15 @@ pub const Backup = struct {
flash: Flash, flash: Flash,
eeprom: Eeprom, eeprom: Eeprom,
pub fn init(alloc: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self { const Kind = enum {
Eeprom,
Sram,
Flash,
Flash1M,
None,
};
pub fn init(allocator: Allocator, kind: Kind, title: [12]u8, path: ?[]const u8) !Self {
log.info("Kind: {}", .{kind}); log.info("Kind: {}", .{kind});
const buf_size: usize = switch (kind) { const buf_size: usize = switch (kind) {
@ -36,24 +44,24 @@ pub const Backup = struct {
.None, .Eeprom => 0, // EEPROM is handled upon first Read Request to it .None, .Eeprom => 0, // EEPROM is handled upon first Read Request to it
}; };
const buf = try alloc.alloc(u8, buf_size); const buf = try allocator.alloc(u8, buf_size);
std.mem.set(u8, buf, 0xFF); std.mem.set(u8, buf, 0xFF);
var backup = Self{ var backup = Self{
.buf = buf, .buf = buf,
.alloc = alloc, .alloc = allocator,
.kind = kind, .kind = kind,
.title = title, .title = title,
.save_path = path, .save_path = path,
.flash = Flash.init(), .flash = Flash.init(),
.eeprom = Eeprom.init(alloc), .eeprom = Eeprom.init(allocator),
}; };
if (backup.save_path) |p| backup.loadSaveFromDisk(p) catch |e| log.err("Failed to load save: {}", .{e}); if (backup.save_path) |p| backup.loadSaveFromDisk(allocator, p) catch |e| log.err("Failed to load save: {}", .{e});
return backup; return backup;
} }
pub fn guessKind(rom: []const u8) ?BackupKind { pub fn guessKind(rom: []const u8) ?Kind {
for (backup_kinds) |needle| { for (backup_kinds) |needle| {
const needle_len = needle.str.len; const needle_len = needle.str.len;
@ -67,13 +75,13 @@ pub const Backup = struct {
} }
pub fn deinit(self: Self) void { pub fn deinit(self: Self) void {
if (self.save_path) |path| self.writeSaveToDisk(path) catch |e| log.err("Failed to write save: {}", .{e}); if (self.save_path) |path| self.writeSaveToDisk(self.alloc, path) catch |e| log.err("Failed to write save: {}", .{e});
self.alloc.free(self.buf); self.alloc.free(self.buf);
} }
fn loadSaveFromDisk(self: *Self, path: []const u8) !void { fn loadSaveFromDisk(self: *Self, allocator: Allocator, path: []const u8) !void {
const file_path = try self.getSaveFilePath(path); const file_path = try self.getSaveFilePath(allocator, path);
defer self.alloc.free(file_path); defer allocator.free(file_path);
// FIXME: Don't rely on this lol // FIXME: Don't rely on this lol
if (std.mem.eql(u8, file_path[file_path.len - 12 .. file_path.len], "untitled.sav")) { if (std.mem.eql(u8, file_path[file_path.len - 12 .. file_path.len], "untitled.sav")) {
@ -81,8 +89,8 @@ pub const Backup = struct {
} }
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{}); const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
const file_buf = try file.readToEndAlloc(self.alloc, try file.getEndPos()); const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos());
defer self.alloc.free(file_buf); defer allocator.free(file_buf);
switch (self.kind) { switch (self.kind) {
.Sram, .Flash, .Flash1M => { .Sram, .Flash, .Flash1M => {
@ -97,7 +105,7 @@ pub const Backup = struct {
if (file_buf.len == 0x200 or file_buf.len == 0x2000) { if (file_buf.len == 0x200 or file_buf.len == 0x2000) {
self.eeprom.kind = if (file_buf.len == 0x200) .Small else .Large; self.eeprom.kind = if (file_buf.len == 0x200) .Small else .Large;
self.buf = try self.alloc.alloc(u8, file_buf.len); self.buf = try allocator.alloc(u8, file_buf.len);
std.mem.copy(u8, self.buf, file_buf); std.mem.copy(u8, self.buf, file_buf);
return log.info("Loaded Save from {s}", .{file_path}); return log.info("Loaded Save from {s}", .{file_path});
} }
@ -111,23 +119,23 @@ pub const Backup = struct {
} }
} }
fn getSaveFilePath(self: *const Self, path: []const u8) ![]const u8 { fn getSaveFilePath(self: *const Self, allocator: Allocator, path: []const u8) ![]const u8 {
const filename = try self.getSaveFilename(); const filename = try self.getSaveFilename(allocator);
defer self.alloc.free(filename); defer allocator.free(filename);
return try std.fs.path.join(self.alloc, &[_][]const u8{ path, filename }); return try std.fs.path.join(allocator, &[_][]const u8{ path, filename });
} }
fn getSaveFilename(self: *const Self) ![]const u8 { fn getSaveFilename(self: *const Self, allocator: Allocator) ![]const u8 {
const title = asString(escape(self.title)); const title_str = asStringSlice(&escape(self.title));
const name = if (title.len != 0) title else "untitled"; const name = if (title_str.len != 0) title_str else "untitled";
return try std.mem.concat(self.alloc, u8, &[_][]const u8{ name, ".sav" }); return try std.mem.concat(allocator, u8, &[_][]const u8{ name, ".sav" });
} }
fn writeSaveToDisk(self: Self, path: []const u8) !void { fn writeSaveToDisk(self: Self, allocator: Allocator, path: []const u8) !void {
const file_path = try self.getSaveFilePath(path); const file_path = try self.getSaveFilePath(allocator, path);
defer self.alloc.free(file_path); defer allocator.free(file_path);
switch (self.kind) { switch (self.kind) {
.Sram, .Flash, .Flash1M, .Eeprom => { .Sram, .Flash, .Flash1M, .Eeprom => {
@ -201,21 +209,13 @@ pub const Backup = struct {
} }
}; };
const BackupKind = enum {
Eeprom,
Sram,
Flash,
Flash1M,
None,
};
const Needle = struct { const Needle = struct {
const Self = @This(); const Self = @This();
str: []const u8, str: []const u8,
kind: BackupKind, kind: Backup.Kind,
fn init(str: []const u8, kind: BackupKind) Self { fn init(str: []const u8, kind: Backup.Kind) Self {
return .{ return .{
.str = str, .str = str,
.kind = kind, .kind = kind,
@ -230,7 +230,7 @@ const SaveError = error{
const Flash = struct { const Flash = struct {
const Self = @This(); const Self = @This();
state: FlashState, state: State,
id_mode: bool, id_mode: bool,
set_bank: bool, set_bank: bool,
@ -239,6 +239,12 @@ const Flash = struct {
bank: u1, bank: u1,
const State = enum {
Ready,
Set,
Command,
};
fn init() Self { fn init() Self {
return .{ return .{
.state = .Ready, .state = .Ready,
@ -293,12 +299,6 @@ const Flash = struct {
} }
}; };
const FlashState = enum {
Ready,
Set,
Command,
};
const Eeprom = struct { const Eeprom = struct {
const Self = @This(); const Self = @This();

View File

@ -12,7 +12,7 @@ const File = std.fs.File;
// ARM Instructions // ARM Instructions
pub const arm = struct { pub const arm = struct {
pub const InstrFn = fn (*Arm7tdmi, *Bus, u32) void; pub const InstrFn = *const fn (*Arm7tdmi, *Bus, u32) void;
const lut: [0x1000]InstrFn = populate(); const lut: [0x1000]InstrFn = populate();
const processing = @import("cpu/arm/data_processing.zig").dataProcessing; const processing = @import("cpu/arm/data_processing.zig").dataProcessing;
@ -110,7 +110,7 @@ pub const arm = struct {
// THUMB Instructions // THUMB Instructions
pub const thumb = struct { pub const thumb = struct {
pub const InstrFn = fn (*Arm7tdmi, *Bus, u16) void; pub const InstrFn = *const fn (*Arm7tdmi, *Bus, u16) void;
const lut: [0x400]InstrFn = populate(); const lut: [0x400]InstrFn = populate();
const processing = @import("cpu/thumb/data_processing.zig"); const processing = @import("cpu/thumb/data_processing.zig");
@ -685,7 +685,8 @@ const Pipline = struct {
pub fn step(self: *Self, cpu: *Arm7tdmi, comptime T: type) ?u32 { pub fn step(self: *Self, cpu: *Arm7tdmi, comptime T: type) ?u32 {
comptime std.debug.assert(T == u32 or T == u16); comptime std.debug.assert(T == u32 or T == u16);
const opcode = self.stage[0]; // FIXME: https://github.com/ziglang/zig/issues/12642
const opcode = self.stage[0..1][0];
self.stage[0] = self.stage[1]; self.stage[0] = self.stage[1];
self.stage[1] = cpu.bus.read(T, cpu.r[15]); self.stage[1] = cpu.bus.read(T, cpu.r[15]);

View File

@ -69,7 +69,7 @@ pub fn intToBytes(comptime T: type, value: anytype) [@sizeOf(T)]u8 {
/// ///
/// This function returns a slice of everything just before the first /// This function returns a slice of everything just before the first
/// `\0` /// `\0`
pub fn asString(title: [12]u8) []const u8 { pub fn asStringSlice(title: *const [12]u8) []const u8 {
var len = title.len; var len = title.len;
for (title) |char, i| { for (title) |char, i| {
if (char == 0) { if (char == 0) {