Compare commits
10 Commits
59063fb2c4
...
8b018bdc49
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 8b018bdc49 | |
Rekai Nyangadzayi Musuka | 76f2ddfabe | |
Rekai Nyangadzayi Musuka | 3fb7f2f814 | |
Rekai Nyangadzayi Musuka | 59669ba3a5 | |
Rekai Nyangadzayi Musuka | 6a798d2c9d | |
Rekai Nyangadzayi Musuka | 5f8c6833f4 | |
Rekai Nyangadzayi Musuka | aa52bb5917 | |
Rekai Nyangadzayi Musuka | e57f918856 | |
Rekai Nyangadzayi Musuka | e5b7441740 | |
Rekai Nyangadzayi Musuka | 2ab8769b7a |
|
@ -1 +1 @@
|
|||
Subproject commit 401c50ff3d0b83ad4bd89caf580ce1bd90fb5618
|
||||
Subproject commit d66925011971fbe221fc2a7f7cb4cd8c181d9ba3
|
|
@ -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 {
|
||||
const self_bit: FieldType = (1 << shamt);
|
||||
|
||||
return struct {
|
||||
return extern struct {
|
||||
bits: Bitfield(FieldType, shamt, 1),
|
||||
|
||||
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);
|
||||
|
||||
return struct {
|
||||
return extern struct {
|
||||
dummy: FieldType,
|
||||
|
||||
fn field(self: anytype) PtrCastPreserveCV(@This(), @TypeOf(self), FieldType) {
|
||||
|
|
31
src/Gui.zig
31
src/Gui.zig
|
@ -11,7 +11,6 @@ const pitch = @import("core/ppu.zig").framebuf_pitch;
|
|||
const scale = @import("core/emu.zig").win_scale;
|
||||
|
||||
const emu = @import("core/emu.zig");
|
||||
const asString = @import("core/util.zig").asString;
|
||||
const log = std.log.scoped(.GUI);
|
||||
|
||||
const default_title: []const u8 = "ZBA";
|
||||
|
@ -54,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;
|
||||
|
@ -69,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) {
|
||||
|
@ -87,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) {
|
||||
|
@ -101,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();
|
||||
|
||||
|
@ -120,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);
|
||||
|
@ -137,12 +136,13 @@ pub fn initAudio(self: *Self, apu: *Apu) void {
|
|||
self.audio.?.play();
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
if (self.audio) |aud| aud.deinit();
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.audio) |*aud| aud.deinit();
|
||||
SDL.SDL_DestroyTexture(self.texture);
|
||||
SDL.SDL_DestroyRenderer(self.renderer);
|
||||
SDL.SDL_DestroyWindow(self.window);
|
||||
SDL.SDL_Quit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
const Audio = struct {
|
||||
|
@ -169,12 +169,13 @@ const Audio = struct {
|
|||
};
|
||||
}
|
||||
|
||||
fn deinit(this: This) void {
|
||||
SDL.SDL_CloseAudioDevice(this.device);
|
||||
fn deinit(self: *This) void {
|
||||
SDL.SDL_CloseAudioDevice(self.device);
|
||||
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 {
|
||||
|
|
|
@ -49,32 +49,29 @@ io: Io,
|
|||
cpu: ?*Arm7tdmi,
|
||||
sched: *Scheduler,
|
||||
|
||||
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),
|
||||
pub fn init(self: *Self, allocator: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void {
|
||||
self.* = .{
|
||||
.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(),
|
||||
.cpu = null,
|
||||
.cpu = cpu,
|
||||
.sched = sched,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.iwram.deinit();
|
||||
self.ewram.deinit();
|
||||
self.pak.deinit();
|
||||
self.bios.deinit();
|
||||
self.ppu.deinit();
|
||||
}
|
||||
|
||||
pub fn attach(self: *Self, cpu: *Arm7tdmi) void {
|
||||
self.cpu = cpu;
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn dbgRead(self: *const Self, comptime T: type, address: u32) T {
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -8,27 +8,28 @@ 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 {
|
||||
var buf: ?[]u8 = null;
|
||||
if (maybe_path) |path| {
|
||||
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();
|
||||
buf = 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);
|
||||
pub fn deinit(self: *Self) void {
|
||||
if (self.buf) |buf| self.allocator.free(buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *Self, comptime T: type, r15: u32, addr: u32) T {
|
||||
|
|
|
@ -5,20 +5,21 @@ 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);
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
const std = @import("std");
|
||||
|
||||
const Bit = @import("bitfield").Bit;
|
||||
const Bitfield = @import("bitfield").Bitfield;
|
||||
const Backup = @import("backup.zig").Backup;
|
||||
const Allocator = std.mem.Allocator;
|
||||
const log = std.log.scoped(.GamePak);
|
||||
|
@ -8,28 +10,48 @@ const Self = @This();
|
|||
|
||||
title: [12]u8,
|
||||
buf: []u8,
|
||||
alloc: Allocator,
|
||||
allocator: Allocator,
|
||||
backup: Backup,
|
||||
gpio: Gpio,
|
||||
|
||||
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{
|
||||
var 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),
|
||||
.gpio = Gpio.init(allocator, .Rtc),
|
||||
};
|
||||
pak.parseHeader();
|
||||
|
||||
return pak;
|
||||
}
|
||||
|
||||
/// Configures any GPIO Device that may be enabled
|
||||
///
|
||||
/// Fundamentally, this just passes a pointer to the initialized GPIO struct to whatever heap allocated GPIO Device struct
|
||||
/// we happen to be using
|
||||
///
|
||||
/// WARNIG: As far as I know, this method must be called in main() or else we'll have a dangling pointer issue
|
||||
/// Despite using the General Purpose Allocator, Zig doesn't prevent me from doing this :sadface:
|
||||
pub fn setupGpio(self: *Self) void {
|
||||
switch (self.gpio.device.kind) {
|
||||
.Rtc => {
|
||||
const ptr = self.gpio.device.ptr orelse @panic("RTC ptr is missing despite GPIO Device Kind");
|
||||
const clock = @ptrCast(*Clock, @alignCast(@alignOf(*Clock), ptr));
|
||||
Clock.init(clock, &self.gpio);
|
||||
},
|
||||
.None => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn parseHeader(self: *const Self) void {
|
||||
const title = parseTitle(self.buf);
|
||||
const code = self.buf[0xAC..0xB0];
|
||||
|
@ -58,9 +80,11 @@ inline fn isLarge(self: *const Self) bool {
|
|||
return self.buf.len > 0x100_0000;
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
self.alloc.free(self.buf);
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.backup.deinit();
|
||||
self.gpio.deinit(self.allocator);
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *Self, comptime T: type, address: u32) T {
|
||||
|
@ -82,6 +106,35 @@ pub fn read(self: *Self, comptime T: type, address: u32) T {
|
|||
}
|
||||
}
|
||||
|
||||
if (self.gpio.cnt == 1) {
|
||||
// GPIO Can be read from
|
||||
// We assume that this will only be true when a ROM actually does want something from GPIO
|
||||
|
||||
switch (T) {
|
||||
u32 => switch (address) {
|
||||
// TODO: Do I even need to implement these?
|
||||
0x0800_00C4 => std.debug.panic("Handle 32-bit GPIO Data/Direction Reads", .{}),
|
||||
0x0800_00C6 => std.debug.panic("Handle 32-bit GPIO Direction/Control Reads", .{}),
|
||||
0x0800_00C8 => std.debug.panic("Handle 32-bit GPIO Control Reads", .{}),
|
||||
else => {},
|
||||
},
|
||||
u16 => switch (address) {
|
||||
// FIXME: What do 16-bit GPIO Reads look like?
|
||||
0x0800_00C4 => return self.gpio.read(.Data),
|
||||
0x0800_00C6 => return self.gpio.read(.Direction),
|
||||
0x0800_00C8 => return self.gpio.read(.Control),
|
||||
else => {},
|
||||
},
|
||||
u8 => switch (address) {
|
||||
0x0800_00C4 => return self.gpio.read(.Data),
|
||||
0x0800_00C6 => return self.gpio.read(.Direction),
|
||||
0x0800_00C8 => return self.gpio.read(.Control),
|
||||
else => {},
|
||||
},
|
||||
else => @compileError("GamePak[GPIO]: Unsupported read width"),
|
||||
}
|
||||
}
|
||||
|
||||
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)),
|
||||
|
@ -140,17 +193,23 @@ pub fn write(self: *Self, comptime T: type, word_count: u16, address: u32, value
|
|||
|
||||
switch (T) {
|
||||
u32 => switch (address) {
|
||||
0x0800_00C4 => log.debug("Wrote {} 0x{X:} to I/O Port Data and Direction", .{ T, value }),
|
||||
0x0800_00C6 => log.debug("Wrote {} 0x{X:} to I/O Port Direction and Control", .{ T, value }),
|
||||
else => {},
|
||||
0x0800_00C4 => {
|
||||
self.gpio.write(.Data, @truncate(u4, value));
|
||||
self.gpio.write(.Direction, @truncate(u4, value >> 16));
|
||||
},
|
||||
0x0800_00C6 => {
|
||||
self.gpio.write(.Direction, @truncate(u4, value));
|
||||
self.gpio.write(.Control, @truncate(u1, value >> 16));
|
||||
},
|
||||
else => log.err("Wrote {} 0x{X:0>8} to 0x{X:0>8}, Unhandled", .{ T, value, address }),
|
||||
},
|
||||
u16 => switch (address) {
|
||||
0x0800_00C4 => log.debug("Wrote {} 0x{X:} to I/O Port Data", .{ T, value }),
|
||||
0x0800_00C6 => log.debug("Wrote {} 0x{X:} to I/O Port Direction", .{ T, value }),
|
||||
0x0800_00C8 => log.debug("Wrote {} 0x{X:} to I/O Port Control", .{ T, value }),
|
||||
else => {},
|
||||
0x0800_00C4 => self.gpio.write(.Data, @truncate(u4, value)),
|
||||
0x0800_00C6 => self.gpio.write(.Direction, @truncate(u4, value)),
|
||||
0x0800_00C8 => self.gpio.write(.Control, @truncate(u1, value)),
|
||||
else => log.err("Wrote {} 0x{X:0>4} to 0x{X:0>8}, Unhandled", .{ T, value, address }),
|
||||
},
|
||||
u8 => log.debug("Wrote {} 0x{X:} to 0x{X:0>8}, Ignored.", .{ T, value, address }),
|
||||
u8 => log.debug("Wrote {} 0x{X:0>2} to 0x{X:0>8}, Ignored.", .{ T, value, address }),
|
||||
else => @compileError("GamePak: Unsupported write width"),
|
||||
}
|
||||
}
|
||||
|
@ -182,3 +241,343 @@ test "OOB Access" {
|
|||
std.debug.assert(pak.get(4) == 0x02); // 0x0002
|
||||
std.debug.assert(pak.get(5) == 0x00);
|
||||
}
|
||||
|
||||
/// GPIO Register Implementation
|
||||
const Gpio = struct {
|
||||
const This = @This();
|
||||
|
||||
data: u4,
|
||||
direction: u4,
|
||||
cnt: u1,
|
||||
|
||||
device: Device,
|
||||
|
||||
const Device = struct {
|
||||
ptr: ?*anyopaque,
|
||||
// TODO: Maybe make this comptime known? Removes some if statements
|
||||
kind: Kind,
|
||||
|
||||
const Kind = enum {
|
||||
Rtc,
|
||||
None,
|
||||
};
|
||||
|
||||
fn step(self: *Device, value: u4) void {
|
||||
switch (self.kind) {
|
||||
.Rtc => {
|
||||
const ptr = self.ptr orelse @panic("Device.ptr should != null when Device.kind == .Rtc");
|
||||
const clock = @ptrCast(*Clock, @alignCast(@alignOf(*Clock), ptr));
|
||||
|
||||
clock.step(Clock.GpioData{ .raw = value });
|
||||
},
|
||||
.None => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn init(kind: Kind, ptr: ?*anyopaque) Device {
|
||||
return .{ .kind = kind, .ptr = ptr };
|
||||
}
|
||||
};
|
||||
|
||||
const Register = enum {
|
||||
Data,
|
||||
Direction,
|
||||
Control,
|
||||
};
|
||||
|
||||
fn init(allocator: Allocator, kind: Device.Kind) This {
|
||||
return .{
|
||||
.data = 0b0000,
|
||||
.direction = 0b1111, // TODO: What is GPIO Direction set to by default?
|
||||
.cnt = 0b0,
|
||||
|
||||
.device = switch (kind) {
|
||||
.Rtc => blk: {
|
||||
const ptr = allocator.create(Clock) catch @panic("Failed to allocate RTC struct on heap");
|
||||
break :blk Device{ .kind = kind, .ptr = ptr };
|
||||
},
|
||||
.None => Device{ .kind = kind, .ptr = null },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
fn deinit(self: This, allocator: Allocator) void {
|
||||
switch (self.device.kind) {
|
||||
.Rtc => {
|
||||
const ptr = self.device.ptr orelse @panic("Device.ptr should != null when Device.kind == .Rtc");
|
||||
allocator.destroy(@ptrCast(*Clock, @alignCast(@alignOf(*Clock), ptr)));
|
||||
},
|
||||
.None => {},
|
||||
}
|
||||
}
|
||||
|
||||
fn write(self: *This, comptime reg: Register, value: if (reg == .Control) u1 else u4) void {
|
||||
log.debug("RTC: Wrote 0b{b:0>4} to {}", .{ value, reg });
|
||||
|
||||
// if (reg == .Data)
|
||||
// log.err("original: 0b{b:0>4} masked: 0b{b:0>4} result: 0b{b:0>4}", .{ self.data, value & self.direction, self.data | (value & self.direction) });
|
||||
|
||||
switch (reg) {
|
||||
.Data => {
|
||||
const masked_value = value & self.direction;
|
||||
|
||||
self.device.step(masked_value);
|
||||
self.data = masked_value;
|
||||
},
|
||||
.Direction => self.direction = value,
|
||||
.Control => self.cnt = value,
|
||||
}
|
||||
}
|
||||
|
||||
fn read(self: *const This, comptime reg: Register) if (reg == .Control) u1 else u4 {
|
||||
if (self.cnt == 0) return 0;
|
||||
|
||||
return switch (reg) {
|
||||
.Data => self.data & ~self.direction,
|
||||
.Direction => self.direction,
|
||||
.Control => self.cnt,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// GBA Real Time Clock
|
||||
const Clock = struct {
|
||||
const This = @This();
|
||||
|
||||
cmd: Command,
|
||||
writer: Writer,
|
||||
state: State,
|
||||
cnt: Control,
|
||||
|
||||
year: u8,
|
||||
month: u5,
|
||||
day: u6,
|
||||
day_of_week: u3,
|
||||
hour: u6,
|
||||
minute: u7,
|
||||
second: u7,
|
||||
|
||||
gpio: *const Gpio,
|
||||
|
||||
const Register = enum {
|
||||
Control,
|
||||
DateTime,
|
||||
Time,
|
||||
};
|
||||
|
||||
const State = union(enum) {
|
||||
Idle,
|
||||
CommandInput,
|
||||
Write: Register,
|
||||
Read: Register,
|
||||
};
|
||||
|
||||
const Writer = struct {
|
||||
buf: u8,
|
||||
i: u4,
|
||||
|
||||
/// The Number of bytes written to since last reset
|
||||
count: u8,
|
||||
|
||||
fn push(self: *Writer, value: u1) void {
|
||||
const idx = @intCast(u3, self.i);
|
||||
self.buf = (self.buf & ~(@as(u8, 1) << idx)) | @as(u8, value) << idx;
|
||||
self.i += 1;
|
||||
}
|
||||
|
||||
fn lap(self: *Writer) void {
|
||||
self.buf = 0;
|
||||
self.i = 0;
|
||||
self.count += 1;
|
||||
}
|
||||
|
||||
fn reset(self: *Writer) void {
|
||||
self.buf = 0;
|
||||
self.i = 0;
|
||||
self.count = 0;
|
||||
}
|
||||
|
||||
fn isFinished(self: *const Writer) bool {
|
||||
return self.i >= 8;
|
||||
}
|
||||
|
||||
fn getCount(self: *const Writer) u8 {
|
||||
return self.count;
|
||||
}
|
||||
|
||||
fn getValue(self: *const Writer) u8 {
|
||||
return self.buf;
|
||||
}
|
||||
};
|
||||
|
||||
const Command = struct {
|
||||
buf: u8,
|
||||
i: u4,
|
||||
|
||||
fn push(self: *Command, value: u1) void {
|
||||
const idx = @intCast(u3, self.i);
|
||||
self.buf = (self.buf & ~(@as(u8, 1) << idx)) | @as(u8, value) << idx;
|
||||
self.i += 1;
|
||||
}
|
||||
|
||||
fn reset(self: *Command) void {
|
||||
self.buf = 0;
|
||||
self.i = 0;
|
||||
}
|
||||
|
||||
fn isFinished(self: *const Command) bool {
|
||||
return self.i >= 8;
|
||||
}
|
||||
|
||||
fn getCommand(self: *const Command) u8 {
|
||||
// If high Nybble does not contain 0x6, reverse the order of the nybbles.
|
||||
// For some reason RTC commands can be LSB or MSB which is funny
|
||||
return if (self.buf >> 4 & 0xF == 0x6) self.buf else (self.buf & 0xF) << 4 | (self.buf >> 4 & 0xF);
|
||||
}
|
||||
|
||||
fn handleCommand(self: *const Command, rtc: *Clock) State {
|
||||
log.info("RTC: Failed to handle Command 0b{b:0>8} aka 0x{X:0>2}", .{ self.buf, self.buf });
|
||||
const command = self.getCommand();
|
||||
|
||||
const is_write = command & 1 == 0;
|
||||
const rtc_register = @intCast(u3, command >> 1 & 0x7); // TODO: Make Truncate
|
||||
|
||||
if (is_write) {
|
||||
return switch (rtc_register) {
|
||||
0 => blk: {
|
||||
rtc.reset();
|
||||
break :blk .Idle;
|
||||
},
|
||||
1 => .{ .Write = .Control },
|
||||
2 => .{ .Write = .DateTime },
|
||||
3 => .{ .Write = .Time },
|
||||
6 => blk: {
|
||||
rtc.irq();
|
||||
break :blk .Idle;
|
||||
},
|
||||
4, 5, 7 => .Idle,
|
||||
};
|
||||
} else {
|
||||
return switch (rtc_register) {
|
||||
1 => .{ .Read = .Control },
|
||||
2 => .{ .Read = .DateTime },
|
||||
3 => .{ .Read = .Time },
|
||||
0, 4, 5, 6, 7 => .Idle, // Do Nothing
|
||||
};
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const GpioData = extern union {
|
||||
sck: Bit(u8, 0),
|
||||
sio: Bit(u8, 1),
|
||||
cs: Bit(u8, 2),
|
||||
raw: u8,
|
||||
};
|
||||
|
||||
const Control = extern union {
|
||||
/// Unknown, value should be preserved though
|
||||
unk: Bit(u8, 1),
|
||||
/// Per-minute IRQ
|
||||
/// If set, fire a Gamepak IRQ every 30s,
|
||||
irq: Bit(u8, 3),
|
||||
/// 12/24 Hour Bit
|
||||
/// If set, 12h mode
|
||||
/// If cleared, 24h mode
|
||||
mode: Bit(u8, 6),
|
||||
/// Read-Only, bit cleared on read
|
||||
/// If is set, means that there has been a failure / time has been lost
|
||||
off: Bit(u8, 7),
|
||||
raw: u8,
|
||||
};
|
||||
|
||||
fn init(ptr: *This, gpio: *const Gpio) void {
|
||||
ptr.* = .{
|
||||
.cmd = .{ .buf = 0, .i = 0 },
|
||||
.writer = .{ .buf = 0, .i = 0, .count = 0 },
|
||||
.state = .Idle,
|
||||
.cnt = .{ .raw = 0 },
|
||||
.year = 0,
|
||||
.month = 0,
|
||||
.day = 0,
|
||||
.day_of_week = 0,
|
||||
.hour = 0,
|
||||
.minute = 0,
|
||||
.second = 0,
|
||||
.gpio = gpio,
|
||||
};
|
||||
}
|
||||
|
||||
fn attachGpio(self: *This, gpio: *const Gpio) void {
|
||||
self.gpio = gpio;
|
||||
}
|
||||
|
||||
fn step(self: *This, value: GpioData) void {
|
||||
const cache: GpioData = .{ .raw = self.gpio.data };
|
||||
|
||||
switch (self.state) {
|
||||
.Idle => {
|
||||
// If SCK is high and CS rises, then prepare for Command
|
||||
// FIXME: Maybe check incoming value to see if SCK is also high?
|
||||
if (cache.sck.read()) {
|
||||
if (!cache.cs.read() and value.cs.read()) {
|
||||
log.err("RTC: Entering Command Mode", .{});
|
||||
self.state = .CommandInput;
|
||||
self.cmd.reset();
|
||||
}
|
||||
}
|
||||
},
|
||||
.CommandInput => {
|
||||
if (!value.cs.read()) log.err("RTC: Expected CS to be set during {}, however CS was cleared", .{self.state});
|
||||
|
||||
if (!cache.sck.read() and value.sck.read()) {
|
||||
// If SCK rises, sample SIO
|
||||
log.debug("RTC: Sampled 0b{b:0>1} from SIO", .{@boolToInt(value.sio.read())});
|
||||
self.cmd.push(@boolToInt(value.sio.read()));
|
||||
|
||||
if (self.cmd.isFinished()) {
|
||||
self.state = self.cmd.handleCommand(self);
|
||||
}
|
||||
}
|
||||
},
|
||||
State{ .Write = .Control } => {
|
||||
if (!value.cs.read()) log.err("RTC: Expected CS to be set during {}, however CS was cleared", .{self.state});
|
||||
|
||||
if (!cache.sck.read() and value.sck.read()) {
|
||||
// If SCK rises, sample SIO
|
||||
|
||||
log.debug("RTC: Sampled 0b{b:0>1} from SIO", .{@boolToInt(value.sio.read())});
|
||||
self.writer.push(@boolToInt(value.sio.read()));
|
||||
|
||||
if (self.writer.isFinished()) {
|
||||
self.writer.lap();
|
||||
self.cnt.raw = self.writer.getValue();
|
||||
|
||||
// FIXME: Move this to a constant or something
|
||||
if (self.writer.getCount() == 1) {
|
||||
self.writer.reset();
|
||||
self.state = .Idle;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
else => {
|
||||
// TODO: Implement Read/Writes for Date/Time and Time and Control
|
||||
log.err("RTC: Ignored request to handle {} command", .{self.state});
|
||||
self.state = .Idle;
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn reset(self: *This) void {
|
||||
// mGBA and NBA only zero the control register
|
||||
// we'll do the same
|
||||
self.cnt.raw = 0;
|
||||
log.info("RTC: Reset executed (control register was zeroed)", .{});
|
||||
}
|
||||
|
||||
fn irq(_: *const This) void {
|
||||
// TODO: Force GamePak IRQ
|
||||
log.err("RTC: TODO: Force GamePak IRQ", .{});
|
||||
}
|
||||
};
|
||||
|
|
|
@ -5,20 +5,21 @@ 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);
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||
|
|
|
@ -3,7 +3,7 @@ const Allocator = std.mem.Allocator;
|
|||
const log = std.log.scoped(.Backup);
|
||||
|
||||
const escape = @import("../util.zig").escape;
|
||||
const asString = @import("../util.zig").asString;
|
||||
const asStringSlice = @import("../util.zig").asStringSlice;
|
||||
|
||||
const backup_kinds = [5]Needle{
|
||||
.{ .str = "EEPROM_V", .kind = .Eeprom },
|
||||
|
@ -17,8 +17,8 @@ pub const Backup = struct {
|
|||
const Self = @This();
|
||||
|
||||
buf: []u8,
|
||||
alloc: Allocator,
|
||||
kind: BackupKind,
|
||||
allocator: Allocator,
|
||||
kind: Kind,
|
||||
|
||||
title: [12]u8,
|
||||
save_path: ?[]const u8,
|
||||
|
@ -26,7 +26,15 @@ pub const Backup = struct {
|
|||
flash: Flash,
|
||||
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});
|
||||
|
||||
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
|
||||
};
|
||||
|
||||
const buf = try alloc.alloc(u8, buf_size);
|
||||
const buf = try allocator.alloc(u8, buf_size);
|
||||
std.mem.set(u8, buf, 0xFF);
|
||||
|
||||
var backup = Self{
|
||||
.buf = buf,
|
||||
.alloc = alloc,
|
||||
.allocator = allocator,
|
||||
.kind = kind,
|
||||
.title = title,
|
||||
.save_path = path,
|
||||
.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;
|
||||
}
|
||||
|
||||
pub fn guessKind(rom: []const u8) ?BackupKind {
|
||||
pub fn guessKind(rom: []const u8) ?Kind {
|
||||
for (backup_kinds) |needle| {
|
||||
const needle_len = needle.str.len;
|
||||
|
||||
|
@ -66,14 +74,15 @@ pub const Backup = struct {
|
|||
return null;
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
if (self.save_path) |path| self.writeSaveToDisk(path) catch |e| log.err("Failed to write save: {}", .{e});
|
||||
self.alloc.free(self.buf);
|
||||
pub fn deinit(self: *Self) void {
|
||||
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;
|
||||
}
|
||||
|
||||
fn loadSaveFromDisk(self: *Self, path: []const u8) !void {
|
||||
const file_path = try self.getSaveFilePath(path);
|
||||
defer self.alloc.free(file_path);
|
||||
fn loadSaveFromDisk(self: *Self, allocator: Allocator, path: []const u8) !void {
|
||||
const file_path = try self.getSaveFilePath(allocator, path);
|
||||
defer allocator.free(file_path);
|
||||
|
||||
// FIXME: Don't rely on this lol
|
||||
if (std.mem.eql(u8, file_path[file_path.len - 12 .. file_path.len], "untitled.sav")) {
|
||||
|
@ -81,8 +90,8 @@ pub const Backup = struct {
|
|||
}
|
||||
|
||||
const file: std.fs.File = try std.fs.openFileAbsolute(file_path, .{});
|
||||
const file_buf = try file.readToEndAlloc(self.alloc, try file.getEndPos());
|
||||
defer self.alloc.free(file_buf);
|
||||
const file_buf = try file.readToEndAlloc(allocator, try file.getEndPos());
|
||||
defer allocator.free(file_buf);
|
||||
|
||||
switch (self.kind) {
|
||||
.Sram, .Flash, .Flash1M => {
|
||||
|
@ -97,7 +106,7 @@ pub const Backup = struct {
|
|||
if (file_buf.len == 0x200 or file_buf.len == 0x2000) {
|
||||
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);
|
||||
return log.info("Loaded Save from {s}", .{file_path});
|
||||
}
|
||||
|
@ -111,23 +120,23 @@ pub const Backup = struct {
|
|||
}
|
||||
}
|
||||
|
||||
fn getSaveFilePath(self: *const Self, path: []const u8) ![]const u8 {
|
||||
const filename = try self.getSaveFilename();
|
||||
defer self.alloc.free(filename);
|
||||
fn getSaveFilePath(self: *const Self, allocator: Allocator, path: []const u8) ![]const u8 {
|
||||
const filename = try self.getSaveFilename(allocator);
|
||||
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 {
|
||||
const title = asString(escape(self.title));
|
||||
const name = if (title.len != 0) title else "untitled";
|
||||
fn getSaveFilename(self: *const Self, allocator: Allocator) ![]const u8 {
|
||||
const title_str = asStringSlice(&escape(self.title));
|
||||
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 {
|
||||
const file_path = try self.getSaveFilePath(path);
|
||||
defer self.alloc.free(file_path);
|
||||
fn writeSaveToDisk(self: Self, allocator: Allocator, path: []const u8) !void {
|
||||
const file_path = try self.getSaveFilePath(allocator, path);
|
||||
defer allocator.free(file_path);
|
||||
|
||||
switch (self.kind) {
|
||||
.Sram, .Flash, .Flash1M, .Eeprom => {
|
||||
|
@ -201,21 +210,13 @@ pub const Backup = struct {
|
|||
}
|
||||
};
|
||||
|
||||
const BackupKind = enum {
|
||||
Eeprom,
|
||||
Sram,
|
||||
Flash,
|
||||
Flash1M,
|
||||
None,
|
||||
};
|
||||
|
||||
const Needle = struct {
|
||||
const Self = @This();
|
||||
|
||||
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 .{
|
||||
.str = str,
|
||||
.kind = kind,
|
||||
|
@ -230,7 +231,7 @@ const SaveError = error{
|
|||
const Flash = struct {
|
||||
const Self = @This();
|
||||
|
||||
state: FlashState,
|
||||
state: State,
|
||||
|
||||
id_mode: bool,
|
||||
set_bank: bool,
|
||||
|
@ -239,6 +240,12 @@ const Flash = struct {
|
|||
|
||||
bank: u1,
|
||||
|
||||
const State = enum {
|
||||
Ready,
|
||||
Set,
|
||||
Command,
|
||||
};
|
||||
|
||||
fn init() Self {
|
||||
return .{
|
||||
.state = .Ready,
|
||||
|
@ -293,12 +300,6 @@ const Flash = struct {
|
|||
}
|
||||
};
|
||||
|
||||
const FlashState = enum {
|
||||
Ready,
|
||||
Set,
|
||||
Command,
|
||||
};
|
||||
|
||||
const Eeprom = struct {
|
||||
const Self = @This();
|
||||
|
||||
|
@ -309,7 +310,7 @@ const Eeprom = struct {
|
|||
writer: Writer,
|
||||
reader: Reader,
|
||||
|
||||
alloc: Allocator,
|
||||
allocator: Allocator,
|
||||
|
||||
const Kind = enum {
|
||||
Unknown,
|
||||
|
@ -325,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,
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -360,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});
|
||||
};
|
||||
|
|
|
@ -12,7 +12,7 @@ const File = std.fs.File;
|
|||
|
||||
// ARM Instructions
|
||||
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 processing = @import("cpu/arm/data_processing.zig").dataProcessing;
|
||||
|
@ -110,7 +110,7 @@ pub const arm = struct {
|
|||
|
||||
// THUMB Instructions
|
||||
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 processing = @import("cpu/thumb/data_processing.zig");
|
||||
|
@ -250,7 +250,7 @@ pub const Arm7tdmi = struct {
|
|||
|
||||
logger: ?Logger,
|
||||
|
||||
pub fn init(sched: *Scheduler, bus: *Bus) Self {
|
||||
pub fn init(sched: *Scheduler, bus: *Bus, log_file: ?std.fs.File) Self {
|
||||
return Self{
|
||||
.r = [_]u32{0x00} ** 16,
|
||||
.sched = sched,
|
||||
|
@ -260,14 +260,10 @@ pub const Arm7tdmi = struct {
|
|||
.banked_fiq = [_]u32{0x00} ** 10,
|
||||
.banked_r = [_]u32{0x00} ** 12,
|
||||
.banked_spsr = [_]PSR{.{ .raw = 0x0000_0000 }} ** 5,
|
||||
.logger = null,
|
||||
.logger = if (log_file) |file| Logger.init(file) else null,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn attach(self: *Self, log_file: std.fs.File) void {
|
||||
self.logger = Logger.init(log_file);
|
||||
}
|
||||
|
||||
inline fn bankedIdx(mode: Mode, kind: BankedKind) usize {
|
||||
const idx: usize = switch (mode) {
|
||||
.User, .System => 0,
|
||||
|
|
138
src/core/ppu.zig
138
src/core/ppu.zig
|
@ -41,28 +41,25 @@ pub const Ppu = struct {
|
|||
oam: Oam,
|
||||
sched: *Scheduler,
|
||||
framebuf: FrameBuffer,
|
||||
alloc: Allocator,
|
||||
allocator: Allocator,
|
||||
|
||||
scanline_sprites: [128]?Sprite,
|
||||
scanline_sprites: *[128]?Sprite,
|
||||
scanline: Scanline,
|
||||
|
||||
pub fn init(alloc: Allocator, sched: *Scheduler) !Self {
|
||||
pub fn init(allocator: Allocator, sched: *Scheduler) !Self {
|
||||
// Queue first Hblank
|
||||
sched.push(.Draw, 240 * 4);
|
||||
|
||||
const framebufs = try alloc.alloc(u8, (framebuf_pitch * height) * 2);
|
||||
std.mem.set(u8, framebufs, 0);
|
||||
|
||||
const scanline_buf = try alloc.alloc(?u16, width * 2);
|
||||
std.mem.set(?u16, scanline_buf, null);
|
||||
const sprites = try allocator.create([128]?Sprite);
|
||||
sprites.* = [_]?Sprite{null} ** 128;
|
||||
|
||||
return Self{
|
||||
.vram = try Vram.init(alloc),
|
||||
.palette = try Palette.init(alloc),
|
||||
.oam = try Oam.init(alloc),
|
||||
.vram = try Vram.init(allocator),
|
||||
.palette = try Palette.init(allocator),
|
||||
.oam = try Oam.init(allocator),
|
||||
.sched = sched,
|
||||
.framebuf = FrameBuffer.init(framebufs),
|
||||
.alloc = alloc,
|
||||
.framebuf = try FrameBuffer.init(allocator),
|
||||
.allocator = allocator,
|
||||
|
||||
// Registers
|
||||
.win = Window.init(),
|
||||
|
@ -75,17 +72,19 @@ pub const Ppu = struct {
|
|||
.bldalpha = .{ .raw = 0x0000 },
|
||||
.bldy = .{ .raw = 0x0000 },
|
||||
|
||||
.scanline = Scanline.init(scanline_buf),
|
||||
.scanline_sprites = [_]?Sprite{null} ** 128,
|
||||
.scanline = try Scanline.init(allocator),
|
||||
.scanline_sprites = sprites,
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
self.framebuf.deinit(self.alloc);
|
||||
self.scanline.deinit(self.alloc);
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.allocator.destroy(self.scanline_sprites);
|
||||
self.framebuf.deinit();
|
||||
self.scanline.deinit();
|
||||
self.vram.deinit();
|
||||
self.palette.deinit();
|
||||
self.oam.deinit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn setBgOffsets(self: *Self, comptime n: u2, word: u32) void {
|
||||
|
@ -399,7 +398,7 @@ pub const Ppu = struct {
|
|||
// Reset Current Scanline Pixel Buffer and list of fetched sprites
|
||||
// in prep for next scanline
|
||||
self.scanline.reset();
|
||||
std.mem.set(?Sprite, &self.scanline_sprites, null);
|
||||
std.mem.set(?Sprite, self.scanline_sprites, null);
|
||||
},
|
||||
0x1 => {
|
||||
const fb_base = framebuf_pitch * @as(usize, scanline);
|
||||
|
@ -426,7 +425,7 @@ pub const Ppu = struct {
|
|||
// Reset Current Scanline Pixel Buffer and list of fetched sprites
|
||||
// in prep for next scanline
|
||||
self.scanline.reset();
|
||||
std.mem.set(?Sprite, &self.scanline_sprites, null);
|
||||
std.mem.set(?Sprite, self.scanline_sprites, null);
|
||||
},
|
||||
0x2 => {
|
||||
const fb_base = framebuf_pitch * @as(usize, scanline);
|
||||
|
@ -452,7 +451,7 @@ pub const Ppu = struct {
|
|||
// Reset Current Scanline Pixel Buffer and list of fetched sprites
|
||||
// in prep for next scanline
|
||||
self.scanline.reset();
|
||||
std.mem.set(?Sprite, &self.scanline_sprites, null);
|
||||
std.mem.set(?Sprite, self.scanline_sprites, null);
|
||||
},
|
||||
0x3 => {
|
||||
const vram_base = width * @sizeOf(u16) * @as(usize, scanline);
|
||||
|
@ -629,20 +628,21 @@ 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);
|
||||
fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||
|
@ -677,20 +677,21 @@ 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);
|
||||
fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||
|
@ -737,20 +738,21 @@ 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);
|
||||
fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||
|
@ -1213,35 +1215,38 @@ fn copyToSpriteBuffer(bldcnt: io.BldCnt, scanline: *Scanline, x: u9, bgr555: u16
|
|||
const Scanline = struct {
|
||||
const Self = @This();
|
||||
|
||||
buf: [2][]?u16,
|
||||
original: []?u16,
|
||||
layers: [2][]?u16,
|
||||
buf: []?u16,
|
||||
|
||||
fn init(buf: []?u16) Self {
|
||||
std.debug.assert(buf.len == width * 2);
|
||||
allocator: Allocator,
|
||||
|
||||
const top_slice = buf[0..][0..width];
|
||||
const btm_slice = buf[width..][0..width];
|
||||
fn init(allocator: Allocator) !Self {
|
||||
const buf = try allocator.alloc(?u16, width * 2); // Top & Bottom Scanline
|
||||
std.mem.set(?u16, buf, null);
|
||||
|
||||
return .{
|
||||
.buf = [_][]?u16{ top_slice, btm_slice },
|
||||
.original = buf,
|
||||
// Top & Bototm Layers
|
||||
.layers = [_][]?u16{ buf[0..][0..width], buf[width..][0..width] },
|
||||
.buf = buf,
|
||||
.allocator = allocator,
|
||||
};
|
||||
}
|
||||
|
||||
fn reset(self: *Self) void {
|
||||
std.mem.set(?u16, self.original, null);
|
||||
std.mem.set(?u16, self.buf, null);
|
||||
}
|
||||
|
||||
fn deinit(self: Self, alloc: Allocator) void {
|
||||
alloc.free(self.original);
|
||||
fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
fn top(self: *Self) []?u16 {
|
||||
return self.buf[0];
|
||||
return self.layers[0];
|
||||
}
|
||||
|
||||
fn btm(self: *Self) []?u16 {
|
||||
return self.buf[1];
|
||||
return self.layers[1];
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -1249,31 +1254,36 @@ const Scanline = struct {
|
|||
const FrameBuffer = struct {
|
||||
const Self = @This();
|
||||
|
||||
buf: [2][]u8,
|
||||
original: []u8,
|
||||
layers: [2][]u8,
|
||||
buf: []u8,
|
||||
current: u1,
|
||||
|
||||
allocator: Allocator,
|
||||
|
||||
// TODO: Rename
|
||||
const Device = enum {
|
||||
Emulator,
|
||||
Renderer,
|
||||
};
|
||||
|
||||
pub fn init(bufs: []u8) Self {
|
||||
std.debug.assert(bufs.len == framebuf_pitch * height * 2);
|
||||
|
||||
const front = bufs[0 .. framebuf_pitch * height];
|
||||
const back = bufs[framebuf_pitch * height ..];
|
||||
pub fn init(allocator: Allocator) !Self {
|
||||
const framebuf_len = framebuf_pitch * height;
|
||||
const buf = try allocator.alloc(u8, framebuf_len * 2);
|
||||
std.mem.set(u8, buf, 0);
|
||||
|
||||
return .{
|
||||
.buf = [2][]u8{ front, back },
|
||||
.original = bufs,
|
||||
// Front and Back Framebuffers
|
||||
.layers = [_][]u8{ buf[0..][0..framebuf_len], buf[framebuf_len..][0..framebuf_len] },
|
||||
.buf = buf,
|
||||
.current = 0,
|
||||
|
||||
.allocator = allocator,
|
||||
};
|
||||
}
|
||||
|
||||
fn deinit(self: Self, alloc: Allocator) void {
|
||||
alloc.free(self.original);
|
||||
fn deinit(self: *Self) void {
|
||||
self.allocator.free(self.buf);
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub fn swap(self: *Self) void {
|
||||
|
@ -1281,6 +1291,6 @@ const FrameBuffer = struct {
|
|||
}
|
||||
|
||||
pub fn get(self: *Self, comptime dev: Device) []u8 {
|
||||
return self.buf[if (dev == .Emulator) self.current else ~self.current];
|
||||
return self.layers[if (dev == .Emulator) self.current else ~self.current];
|
||||
}
|
||||
};
|
||||
|
|
|
@ -14,15 +14,16 @@ 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;
|
||||
}
|
||||
|
||||
pub fn deinit(self: Self) void {
|
||||
pub fn deinit(self: *Self) void {
|
||||
self.queue.deinit();
|
||||
self.* = undefined;
|
||||
}
|
||||
|
||||
pub inline fn now(self: *const Self) u64 {
|
||||
|
|
|
@ -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
|
||||
/// `\0`
|
||||
pub fn asString(title: [12]u8) []const u8 {
|
||||
pub fn asStringSlice(title: *const [12]u8) []const u8 {
|
||||
var len = title.len;
|
||||
for (title) |char, i| {
|
||||
if (char == 0) {
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
|
23
src/main.zig
23
src/main.zig
|
@ -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,27 +40,26 @@ 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 (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
|
||||
var scheduler = Scheduler.init(allocator);
|
||||
defer scheduler.deinit();
|
||||
|
||||
var bus = try Bus.init(allocator, &scheduler, paths);
|
||||
var bus: Bus = undefined;
|
||||
var cpu = Arm7tdmi.init(&scheduler, &bus, log_file);
|
||||
if (paths.bios == null) cpu.fastBoot();
|
||||
|
||||
try bus.init(allocator, &scheduler, &cpu, paths);
|
||||
bus.pak.setupGpio(); // FIXME: Can I not call this in main()?
|
||||
defer bus.deinit();
|
||||
|
||||
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?)
|
||||
if (paths.bios == null) arm7tdmi.fastBoot();
|
||||
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue