chore: improve init/deinit methods
This commit is contained in:
parent
aa52bb5917
commit
5f8c6833f4
10
src/Gui.zig
10
src/Gui.zig
|
@ -136,12 +136,13 @@ pub fn initAudio(self: *Self, apu: *Apu) void {
|
||||||
self.audio.?.play();
|
self.audio.?.play();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.audio) |aud| aud.deinit();
|
if (self.audio) |*aud| aud.deinit();
|
||||||
SDL.SDL_DestroyTexture(self.texture);
|
SDL.SDL_DestroyTexture(self.texture);
|
||||||
SDL.SDL_DestroyRenderer(self.renderer);
|
SDL.SDL_DestroyRenderer(self.renderer);
|
||||||
SDL.SDL_DestroyWindow(self.window);
|
SDL.SDL_DestroyWindow(self.window);
|
||||||
SDL.SDL_Quit();
|
SDL.SDL_Quit();
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Audio = struct {
|
const Audio = struct {
|
||||||
|
@ -168,8 +169,9 @@ const Audio = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(this: This) void {
|
fn deinit(self: *This) void {
|
||||||
SDL.SDL_CloseAudioDevice(this.device);
|
SDL.SDL_CloseAudioDevice(self.device);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn play(this: *This) void {
|
pub fn play(this: *This) void {
|
||||||
|
|
|
@ -49,8 +49,8 @@ io: Io,
|
||||||
cpu: ?*Arm7tdmi,
|
cpu: ?*Arm7tdmi,
|
||||||
sched: *Scheduler,
|
sched: *Scheduler,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, sched: *Scheduler, paths: FilePaths) !Self {
|
pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void {
|
||||||
return Self{
|
self.* = .{
|
||||||
.pak = try GamePak.init(alloc, paths.rom, paths.save),
|
.pak = try GamePak.init(alloc, paths.rom, paths.save),
|
||||||
.bios = try Bios.init(alloc, paths.bios),
|
.bios = try Bios.init(alloc, paths.bios),
|
||||||
.ppu = try Ppu.init(alloc, sched),
|
.ppu = try Ppu.init(alloc, sched),
|
||||||
|
@ -60,21 +60,34 @@ pub fn init(alloc: Allocator, sched: *Scheduler, paths: FilePaths) !Self {
|
||||||
.dma = createDmaTuple(),
|
.dma = createDmaTuple(),
|
||||||
.tim = createTimerTuple(sched),
|
.tim = createTimerTuple(sched),
|
||||||
.io = Io.init(),
|
.io = Io.init(),
|
||||||
.cpu = null,
|
.cpu = cpu,
|
||||||
.sched = sched,
|
.sched = sched,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
// 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.iwram.deinit();
|
||||||
self.ewram.deinit();
|
self.ewram.deinit();
|
||||||
self.pak.deinit();
|
self.pak.deinit();
|
||||||
self.bios.deinit();
|
self.bios.deinit();
|
||||||
self.ppu.deinit();
|
self.ppu.deinit();
|
||||||
}
|
self.* = undefined;
|
||||||
|
|
||||||
pub fn attach(self: *Self, cpu: *Arm7tdmi) void {
|
|
||||||
self.cpu = cpu;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn dbgRead(self: *const Self, comptime T: type, address: u32) T {
|
pub fn dbgRead(self: *const Self, comptime T: type, address: u32) T {
|
||||||
|
|
|
@ -27,8 +27,9 @@ pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
if (self.buf) |buf| self.alloc.free(buf);
|
if (self.buf) |buf| self.alloc.free(buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *Self, comptime T: type, r15: u32, addr: u32) T {
|
pub fn read(self: *Self, comptime T: type, r15: u32, addr: u32) T {
|
||||||
|
|
|
@ -17,8 +17,9 @@ pub fn init(alloc: Allocator) !Self {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||||
|
|
|
@ -58,9 +58,10 @@ inline fn isLarge(self: *const Self) bool {
|
||||||
return self.buf.len > 0x100_0000;
|
return self.buf.len > 0x100_0000;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.alloc.free(self.buf);
|
|
||||||
self.backup.deinit();
|
self.backup.deinit();
|
||||||
|
self.alloc.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *Self, comptime T: type, address: u32) T {
|
pub fn read(self: *Self, comptime T: type, address: u32) T {
|
||||||
|
|
|
@ -17,8 +17,9 @@ pub fn init(alloc: Allocator) !Self {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||||
|
|
|
@ -74,9 +74,10 @@ pub const Backup = struct {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
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});
|
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);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn loadSaveFromDisk(self: *Self, allocator: Allocator, path: []const u8) !void {
|
fn loadSaveFromDisk(self: *Self, allocator: Allocator, path: []const u8) !void {
|
||||||
|
|
|
@ -250,7 +250,7 @@ pub const Arm7tdmi = struct {
|
||||||
|
|
||||||
logger: ?Logger,
|
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{
|
return Self{
|
||||||
.r = [_]u32{0x00} ** 16,
|
.r = [_]u32{0x00} ** 16,
|
||||||
.sched = sched,
|
.sched = sched,
|
||||||
|
@ -260,14 +260,10 @@ 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,
|
||||||
.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 {
|
inline fn bankedIdx(mode: Mode, kind: BankedKind) usize {
|
||||||
const idx: usize = switch (mode) {
|
const idx: usize = switch (mode) {
|
||||||
.User, .System => 0,
|
.User, .System => 0,
|
||||||
|
|
|
@ -46,23 +46,17 @@ pub const Ppu = struct {
|
||||||
scanline_sprites: [128]?Sprite,
|
scanline_sprites: [128]?Sprite,
|
||||||
scanline: Scanline,
|
scanline: Scanline,
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, sched: *Scheduler) !Self {
|
pub fn init(allocator: Allocator, sched: *Scheduler) !Self {
|
||||||
// Queue first Hblank
|
// Queue first Hblank
|
||||||
sched.push(.Draw, 240 * 4);
|
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);
|
|
||||||
|
|
||||||
return Self{
|
return Self{
|
||||||
.vram = try Vram.init(alloc),
|
.vram = try Vram.init(allocator),
|
||||||
.palette = try Palette.init(alloc),
|
.palette = try Palette.init(allocator),
|
||||||
.oam = try Oam.init(alloc),
|
.oam = try Oam.init(allocator),
|
||||||
.sched = sched,
|
.sched = sched,
|
||||||
.framebuf = FrameBuffer.init(framebufs),
|
.framebuf = try FrameBuffer.init(allocator),
|
||||||
.alloc = alloc,
|
.alloc = allocator,
|
||||||
|
|
||||||
// Registers
|
// Registers
|
||||||
.win = Window.init(),
|
.win = Window.init(),
|
||||||
|
@ -75,17 +69,18 @@ pub const Ppu = struct {
|
||||||
.bldalpha = .{ .raw = 0x0000 },
|
.bldalpha = .{ .raw = 0x0000 },
|
||||||
.bldy = .{ .raw = 0x0000 },
|
.bldy = .{ .raw = 0x0000 },
|
||||||
|
|
||||||
.scanline = Scanline.init(scanline_buf),
|
.scanline = try Scanline.init(allocator),
|
||||||
.scanline_sprites = [_]?Sprite{null} ** 128,
|
.scanline_sprites = [_]?Sprite{null} ** 128,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.framebuf.deinit(self.alloc);
|
self.framebuf.deinit();
|
||||||
self.scanline.deinit(self.alloc);
|
self.scanline.deinit();
|
||||||
self.vram.deinit();
|
self.vram.deinit();
|
||||||
self.palette.deinit();
|
self.palette.deinit();
|
||||||
self.oam.deinit();
|
self.oam.deinit();
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn setBgOffsets(self: *Self, comptime n: u2, word: u32) void {
|
pub fn setBgOffsets(self: *Self, comptime n: u2, word: u32) void {
|
||||||
|
@ -641,8 +636,9 @@ const Palette = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(self: Self) void {
|
fn deinit(self: *Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||||
|
@ -689,8 +685,9 @@ const Vram = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(self: Self) void {
|
fn deinit(self: *Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||||
|
@ -749,8 +746,9 @@ const Oam = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(self: Self) void {
|
fn deinit(self: *Self) void {
|
||||||
self.alloc.free(self.buf);
|
self.alloc.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
pub fn read(self: *const Self, comptime T: type, address: usize) T {
|
||||||
|
@ -1213,35 +1211,38 @@ fn copyToSpriteBuffer(bldcnt: io.BldCnt, scanline: *Scanline, x: u9, bgr555: u16
|
||||||
const Scanline = struct {
|
const Scanline = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
buf: [2][]?u16,
|
layers: [2][]?u16,
|
||||||
original: []?u16,
|
buf: []?u16,
|
||||||
|
|
||||||
fn init(buf: []?u16) Self {
|
allocator: Allocator,
|
||||||
std.debug.assert(buf.len == width * 2);
|
|
||||||
|
|
||||||
const top_slice = buf[0..][0..width];
|
fn init(allocator: Allocator) !Self {
|
||||||
const btm_slice = buf[width..][0..width];
|
const buf = try allocator.alloc(?u16, width * 2); // Top & Bottom Scanline
|
||||||
|
std.mem.set(?u16, buf, null);
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.buf = [_][]?u16{ top_slice, btm_slice },
|
// Top & Bototm Layers
|
||||||
.original = buf,
|
.layers = [_][]?u16{ buf[0..][0..width], buf[width..][0..width] },
|
||||||
|
.buf = buf,
|
||||||
|
.allocator = allocator,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn reset(self: *Self) void {
|
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 {
|
fn deinit(self: *Self) void {
|
||||||
alloc.free(self.original);
|
self.allocator.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
fn top(self: *Self) []?u16 {
|
fn top(self: *Self) []?u16 {
|
||||||
return self.buf[0];
|
return self.layers[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
fn btm(self: *Self) []?u16 {
|
fn btm(self: *Self) []?u16 {
|
||||||
return self.buf[1];
|
return self.layers[1];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1249,31 +1250,36 @@ const Scanline = struct {
|
||||||
const FrameBuffer = struct {
|
const FrameBuffer = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
buf: [2][]u8,
|
layers: [2][]u8,
|
||||||
original: []u8,
|
buf: []u8,
|
||||||
current: u1,
|
current: u1,
|
||||||
|
|
||||||
|
allocator: Allocator,
|
||||||
|
|
||||||
// TODO: Rename
|
// TODO: Rename
|
||||||
const Device = enum {
|
const Device = enum {
|
||||||
Emulator,
|
Emulator,
|
||||||
Renderer,
|
Renderer,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn init(bufs: []u8) Self {
|
pub fn init(allocator: Allocator) !Self {
|
||||||
std.debug.assert(bufs.len == framebuf_pitch * height * 2);
|
const framebuf_len = framebuf_pitch * height;
|
||||||
|
const buf = try allocator.alloc(u8, framebuf_len * 2);
|
||||||
const front = bufs[0 .. framebuf_pitch * height];
|
std.mem.set(u8, buf, 0);
|
||||||
const back = bufs[framebuf_pitch * height ..];
|
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.buf = [2][]u8{ front, back },
|
// Front and Back Framebuffers
|
||||||
.original = bufs,
|
.layers = [_][]u8{ buf[0..][0..framebuf_len], buf[framebuf_len..][0..framebuf_len] },
|
||||||
|
.buf = buf,
|
||||||
.current = 0,
|
.current = 0,
|
||||||
|
|
||||||
|
.allocator = allocator,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
fn deinit(self: Self, alloc: Allocator) void {
|
fn deinit(self: *Self) void {
|
||||||
alloc.free(self.original);
|
self.allocator.free(self.buf);
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn swap(self: *Self) void {
|
pub fn swap(self: *Self) void {
|
||||||
|
@ -1281,6 +1287,6 @@ const FrameBuffer = struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get(self: *Self, comptime dev: Device) []u8 {
|
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];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
@ -21,8 +21,9 @@ pub const Scheduler = struct {
|
||||||
return sched;
|
return sched;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn deinit(self: Self) void {
|
pub fn deinit(self: *Self) void {
|
||||||
self.queue.deinit();
|
self.queue.deinit();
|
||||||
|
self.* = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
pub inline fn now(self: *const Self) u64 {
|
pub inline fn now(self: *const Self) u64 {
|
||||||
|
|
18
src/main.zig
18
src/main.zig
|
@ -40,22 +40,20 @@ pub fn main() anyerror!void {
|
||||||
const paths = try handleArguments(allocator, &result);
|
const paths = try handleArguments(allocator, &result);
|
||||||
defer if (paths.save) |path| allocator.free(path);
|
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;
|
||||||
|
defer if (log_file) |file| file.close();
|
||||||
|
|
||||||
// TODO: Take Emulator Init Code out of main.zig
|
// TODO: Take Emulator Init Code out of main.zig
|
||||||
var scheduler = Scheduler.init(allocator);
|
var scheduler = Scheduler.init(allocator);
|
||||||
defer scheduler.deinit();
|
defer scheduler.deinit();
|
||||||
|
|
||||||
var bus = try Bus.init(allocator, &scheduler, paths);
|
var bus: Bus = undefined;
|
||||||
defer bus.deinit();
|
var arm7tdmi = Arm7tdmi.init(&scheduler, &bus, log_file);
|
||||||
|
|
||||||
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();
|
if (paths.bios == null) arm7tdmi.fastBoot();
|
||||||
|
|
||||||
|
try bus.init(allocator, &scheduler, &arm7tdmi, paths);
|
||||||
|
defer bus.deinit();
|
||||||
|
|
||||||
var gui = Gui.init(bus.pak.title, width, height);
|
var gui = Gui.init(bus.pak.title, width, height);
|
||||||
gui.initAudio(&bus.apu);
|
gui.initAudio(&bus.apu);
|
||||||
defer gui.deinit();
|
defer gui.deinit();
|
||||||
|
|
Loading…
Reference in New Issue