Compare commits

...

2 Commits

Author SHA1 Message Date
Rekai Nyangadzayi Musuka 3fb7f2f814 chore: better conform to zig idioms 2022-09-03 18:30:48 -03:00
Rekai Nyangadzayi Musuka 59669ba3a5 chore: rename arm7tdmi variables to just cpu
Less verbose, specifying arm7tdmi doesn't really do much when there's
no other CPU in the system
2022-09-03 17:56:37 -03:00
12 changed files with 106 additions and 122 deletions

View File

@ -53,11 +53,11 @@ pub fn init(title: [12]u8, width: i32, height: i32) Self {
};
}
pub fn run(self: *Self, arm7tdmi: *Arm7tdmi, scheduler: *Scheduler) !void {
pub fn run(self: *Self, cpu: *Arm7tdmi, scheduler: *Scheduler) !void {
var quit = std.atomic.Atomic(bool).init(false);
var frame_rate = FpsTracker.init();
const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, &frame_rate, scheduler, arm7tdmi });
const thread = try std.Thread.spawn(.{}, emu.run, .{ &quit, &frame_rate, scheduler, cpu });
defer thread.join();
var title_buf: [0x100]u8 = [_]u8{0} ** 0x100;
@ -68,7 +68,7 @@ pub fn run(self: *Self, arm7tdmi: *Arm7tdmi, scheduler: *Scheduler) !void {
switch (event.type) {
SDL.SDL_QUIT => break :emu_loop,
SDL.SDL_KEYDOWN => {
const io = &arm7tdmi.bus.io;
const io = &cpu.bus.io;
const key_code = event.key.keysym.sym;
switch (key_code) {
@ -86,7 +86,7 @@ pub fn run(self: *Self, arm7tdmi: *Arm7tdmi, scheduler: *Scheduler) !void {
}
},
SDL.SDL_KEYUP => {
const io = &arm7tdmi.bus.io;
const io = &cpu.bus.io;
const key_code = event.key.keysym.sym;
switch (key_code) {
@ -100,12 +100,12 @@ pub fn run(self: *Self, arm7tdmi: *Arm7tdmi, scheduler: *Scheduler) !void {
SDL.SDLK_s => io.keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => io.keyinput.start.set(),
SDL.SDLK_RSHIFT => io.keyinput.select.set(),
SDL.SDLK_i => log.err("Sample Count: {}", .{@intCast(u32, SDL.SDL_AudioStreamAvailable(arm7tdmi.bus.apu.stream)) / (2 * @sizeOf(u16))}),
SDL.SDLK_i => log.err("Sample Count: {}", .{@intCast(u32, SDL.SDL_AudioStreamAvailable(cpu.bus.apu.stream)) / (2 * @sizeOf(u16))}),
SDL.SDLK_j => log.err("Scheduler Capacity: {} | Scheduler Event Count: {}", .{ scheduler.queue.capacity(), scheduler.queue.count() }),
SDL.SDLK_k => {
// Dump IWRAM to file
log.info("PC: 0x{X:0>8}", .{arm7tdmi.r[15]});
log.info("LR: 0x{X:0>8}", .{arm7tdmi.r[14]});
log.info("PC: 0x{X:0>8}", .{cpu.r[15]});
log.info("LR: 0x{X:0>8}", .{cpu.r[14]});
// const iwram_file = try std.fs.cwd().createFile("iwram.bin", .{});
// defer iwram_file.close();
@ -119,7 +119,7 @@ pub fn run(self: *Self, arm7tdmi: *Arm7tdmi, scheduler: *Scheduler) !void {
}
// Emulator has an internal Double Buffer
const framebuf = arm7tdmi.bus.ppu.framebuf.get(.Renderer);
const framebuf = cpu.bus.ppu.framebuf.get(.Renderer);
_ = SDL.SDL_UpdateTexture(self.texture, null, framebuf.ptr, pitch);
_ = SDL.SDL_RenderCopy(self.renderer, self.texture, null, null);
SDL.SDL_RenderPresent(self.renderer);
@ -174,8 +174,8 @@ const Audio = struct {
self.* = undefined;
}
pub fn play(this: *This) void {
SDL.SDL_PauseAudioDevice(this.device, 0);
pub fn play(self: *This) void {
SDL.SDL_PauseAudioDevice(self.device, 0);
}
export fn callback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void {

View File

@ -49,14 +49,14 @@ io: Io,
cpu: ?*Arm7tdmi,
sched: *Scheduler,
pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void {
pub fn init(self: *Self, allocator: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void {
self.* = .{
.pak = try GamePak.init(alloc, paths.rom, paths.save),
.bios = try Bios.init(alloc, paths.bios),
.ppu = try Ppu.init(alloc, sched),
.pak = try GamePak.init(allocator, paths.rom, paths.save),
.bios = try Bios.init(allocator, paths.bios),
.ppu = try Ppu.init(allocator, sched),
.apu = Apu.init(sched),
.iwram = try Iwram.init(alloc),
.ewram = try Ewram.init(alloc),
.iwram = try Iwram.init(allocator),
.ewram = try Ewram.init(allocator),
.dma = createDmaTuple(),
.tim = createTimerTuple(sched),
.io = Io.init(),
@ -65,22 +65,6 @@ pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, pa
};
}
// pub fn init(alloc: Allocator, sched: *Scheduler, paths: FilePaths) !Self {
// return Self{
// .pak = try GamePak.init(alloc, paths.rom, paths.save),
// .bios = try Bios.init(alloc, paths.bios),
// .ppu = try Ppu.init(alloc, sched),
// .apu = Apu.init(sched),
// .iwram = try Iwram.init(alloc),
// .ewram = try Ewram.init(alloc),
// .dma = createDmaTuple(),
// .tim = createTimerTuple(sched),
// .io = Io.init(),
// .cpu = null,
// .sched = sched,
// };
// }
pub fn deinit(self: *Self) void {
self.iwram.deinit();
self.ewram.deinit();

View File

@ -444,29 +444,29 @@ const ToneSweep = struct {
};
}
pub fn tick(this: *This, ch1: *Self) void {
if (this.timer != 0) this.timer -= 1;
pub fn tick(self: *This, ch1: *Self) void {
if (self.timer != 0) self.timer -= 1;
if (this.timer == 0) {
if (self.timer == 0) {
const period = ch1.sweep.period.read();
this.timer = if (period == 0) 8 else period;
if (!this.calc_performed) this.calc_performed = true;
self.timer = if (period == 0) 8 else period;
if (!self.calc_performed) self.calc_performed = true;
if (this.enabled and period != 0) {
const new_freq = this.calcFrequency(ch1);
if (self.enabled and period != 0) {
const new_freq = self.calcFrequency(ch1);
if (new_freq <= 0x7FF and ch1.sweep.shift.read() != 0) {
ch1.freq.frequency.write(@truncate(u11, new_freq));
this.shadow = @truncate(u11, new_freq);
self.shadow = @truncate(u11, new_freq);
_ = this.calcFrequency(ch1);
_ = self.calcFrequency(ch1);
}
}
}
}
fn calcFrequency(this: *This, ch1: *Self) u12 {
const shadow = @as(u12, this.shadow);
fn calcFrequency(self: *This, ch1: *Self) u12 {
const shadow = @as(u12, self.shadow);
const shadow_shifted = shadow >> ch1.sweep.shift.read();
const decrease = ch1.sweep.direction.read();

View File

@ -8,27 +8,27 @@ pub const size = 0x4000;
const Self = @This();
buf: ?[]u8,
alloc: Allocator,
allocator: Allocator,
addr_latch: u32,
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
pub fn init(allocator: Allocator, maybe_path: ?[]const u8) !Self {
const buf: ?[]u8 = if (maybe_path) |path| blk: {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
break :blk try file.readToEndAlloc(alloc, try file.getEndPos());
break :blk try file.readToEndAlloc(allocator, try file.getEndPos());
} else null;
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
.addr_latch = 0,
};
}
pub fn deinit(self: *Self) void {
if (self.buf) |buf| self.alloc.free(buf);
if (self.buf) |buf| self.allocator.free(buf);
self.* = undefined;
}

View File

@ -5,20 +5,20 @@ const ewram_size = 0x40000;
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, ewram_size);
pub fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, ewram_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@ -8,22 +8,22 @@ const Self = @This();
title: [12]u8,
buf: []u8,
alloc: Allocator,
allocator: Allocator,
backup: Backup,
pub fn init(alloc: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self {
pub fn init(allocator: Allocator, rom_path: []const u8, save_path: ?[]const u8) !Self {
const file = try std.fs.cwd().openFile(rom_path, .{});
defer file.close();
const file_buf = try file.readToEndAlloc(alloc, try file.getEndPos());
const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos());
const title = parseTitle(file_buf);
const kind = Backup.guessKind(file_buf) orelse .None;
const pak = Self{
.buf = file_buf,
.alloc = alloc,
.allocator = allocator,
.title = title,
.backup = try Backup.init(alloc, kind, title, save_path),
.backup = try Backup.init(allocator, kind, title, save_path),
};
pak.parseHeader();
@ -60,7 +60,7 @@ inline fn isLarge(self: *const Self) bool {
pub fn deinit(self: *Self) void {
self.backup.deinit();
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@ -5,20 +5,20 @@ const iwram_size = 0x8000;
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
pub fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, iwram_size);
pub fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, iwram_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
pub fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@ -17,7 +17,7 @@ pub const Backup = struct {
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
kind: Kind,
title: [12]u8,
@ -49,7 +49,7 @@ pub const Backup = struct {
var backup = Self{
.buf = buf,
.alloc = allocator,
.allocator = allocator,
.kind = kind,
.title = title,
.save_path = path,
@ -75,8 +75,8 @@ pub const Backup = struct {
}
pub fn deinit(self: *Self) void {
if (self.save_path) |path| self.writeSaveToDisk(self.alloc, path) catch |e| log.err("Failed to write save: {}", .{e});
self.alloc.free(self.buf);
if (self.save_path) |path| self.writeSaveToDisk(self.allocator, path) catch |e| log.err("Failed to write save: {}", .{e});
self.allocator.free(self.buf);
self.* = undefined;
}
@ -310,7 +310,7 @@ const Eeprom = struct {
writer: Writer,
reader: Reader,
alloc: Allocator,
allocator: Allocator,
const Kind = enum {
Unknown,
@ -326,14 +326,14 @@ const Eeprom = struct {
RequestEnd,
};
fn init(alloc: Allocator) Self {
fn init(allocator: Allocator) Self {
return .{
.kind = .Unknown,
.state = .Ready,
.writer = Writer.init(),
.reader = Reader.init(),
.addr = 0,
.alloc = alloc,
.allocator = allocator,
};
}
@ -361,7 +361,7 @@ const Eeprom = struct {
else => unreachable,
};
buf.* = self.alloc.alloc(u8, len) catch |e| {
buf.* = self.allocator.alloc(u8, len) catch |e| {
log.err("Failed to resize EEPROM buf to {} bytes", .{len});
std.debug.panic("EEPROM entered irrecoverable state {}", .{e});
};

View File

@ -41,7 +41,7 @@ pub const Ppu = struct {
oam: Oam,
sched: *Scheduler,
framebuf: FrameBuffer,
alloc: Allocator,
allocator: Allocator,
scanline_sprites: *[128]?Sprite,
scanline: Scanline,
@ -59,7 +59,7 @@ pub const Ppu = struct {
.oam = try Oam.init(allocator),
.sched = sched,
.framebuf = try FrameBuffer.init(allocator),
.alloc = allocator,
.allocator = allocator,
// Registers
.win = Window.init(),
@ -78,7 +78,7 @@ pub const Ppu = struct {
}
pub fn deinit(self: *Self) void {
self.alloc.destroy(self.scanline_sprites);
self.allocator.destroy(self.scanline_sprites);
self.framebuf.deinit();
self.scanline.deinit();
self.vram.deinit();
@ -628,20 +628,20 @@ const Palette = struct {
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, palram_size);
fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, palram_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}
@ -677,20 +677,20 @@ const Vram = struct {
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, vram_size);
fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, vram_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}
@ -738,20 +738,20 @@ const Oam = struct {
const Self = @This();
buf: []u8,
alloc: Allocator,
allocator: Allocator,
fn init(alloc: Allocator) !Self {
const buf = try alloc.alloc(u8, oam_size);
fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(u8, oam_size);
std.mem.set(u8, buf, 0);
return Self{
.buf = buf,
.alloc = alloc,
.allocator = allocator,
};
}
fn deinit(self: *Self) void {
self.alloc.free(self.buf);
self.allocator.free(self.buf);
self.* = undefined;
}

View File

@ -14,8 +14,8 @@ pub const Scheduler = struct {
tick: u64,
queue: PriorityQueue(Event, void, lessThan),
pub fn init(alloc: Allocator) Self {
var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(alloc, {}) };
pub fn init(allocator: Allocator) Self {
var sched = Self{ .tick = 0, .queue = PriorityQueue(Event, void, lessThan).init(allocator, {}) };
sched.queue.add(.{ .kind = .HeatDeath, .tick = std.math.maxInt(u64) }) catch unreachable;
return sched;

View File

@ -129,45 +129,45 @@ pub const Logger = struct {
try self.buf.writer().print(format, args);
}
pub fn mgbaLog(self: *Self, arm7tdmi: *const Arm7tdmi, opcode: u32) void {
pub fn mgbaLog(self: *Self, cpu: *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 (cpu.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 low = cpu.bus.dbgRead(u16, cpu.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");
self.print(arm_fmt, Self.fmtArgs(cpu, 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");
self.print(thumb_fmt, Self.fmtArgs(cpu, 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");
self.print(arm_fmt, Self.fmtArgs(cpu, opcode)) catch @panic("failed to write to log file");
}
}
fn fmtArgs(arm7tdmi: *const Arm7tdmi, opcode: u32) FmtArgTuple {
fn fmtArgs(cpu: *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],
arm7tdmi.cpsr.raw,
cpu.r[0],
cpu.r[1],
cpu.r[2],
cpu.r[3],
cpu.r[4],
cpu.r[5],
cpu.r[6],
cpu.r[7],
cpu.r[8],
cpu.r[9],
cpu.r[10],
cpu.r[11],
cpu.r[12],
cpu.r[13],
cpu.r[14],
cpu.r[15],
cpu.cpsr.raw,
opcode,
};
}

View File

@ -14,7 +14,7 @@ const Allocator = std.mem.Allocator;
const log = std.log.scoped(.CLI);
const width = @import("core/ppu.zig").width;
const height = @import("core/ppu.zig").height;
const arm7tdmi_logging = @import("core/emu.zig").cpu_logging;
const cpu_logging = @import("core/emu.zig").cpu_logging;
pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level;
// TODO: Reimpl Logging
@ -40,7 +40,7 @@ pub fn main() anyerror!void {
const paths = try handleArguments(allocator, &result);
defer if (paths.save) |path| allocator.free(path);
const log_file: ?std.fs.File = if (arm7tdmi_logging) try std.fs.cwd().createFile("zba.log", .{}) else null;
const log_file: ?std.fs.File = if (cpu_logging) try std.fs.cwd().createFile("zba.log", .{}) else null;
defer if (log_file) |file| file.close();
// TODO: Take Emulator Init Code out of main.zig
@ -48,17 +48,17 @@ pub fn main() anyerror!void {
defer scheduler.deinit();
var bus: Bus = undefined;
var arm7tdmi = Arm7tdmi.init(&scheduler, &bus, log_file);
if (paths.bios == null) arm7tdmi.fastBoot();
var cpu = Arm7tdmi.init(&scheduler, &bus, log_file);
if (paths.bios == null) cpu.fastBoot();
try bus.init(allocator, &scheduler, &arm7tdmi, paths);
try bus.init(allocator, &scheduler, &cpu, paths);
defer bus.deinit();
var gui = Gui.init(bus.pak.title, width, height);
gui.initAudio(&bus.apu);
defer gui.deinit();
try gui.run(&arm7tdmi, &scheduler);
try gui.run(&cpu, &scheduler);
}
fn getSavePath(allocator: Allocator) !?[]const u8 {