Compare commits

..

No commits in common. "6a798d2c9d8dba15e58652155ae16459660c32ed" and "aa52bb5917cf88264e4153f91fa706a471236923" have entirely different histories.

11 changed files with 83 additions and 108 deletions

View File

@ -136,13 +136,12 @@ 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,9 +168,8 @@ const Audio = struct {
};
}
fn deinit(self: *This) void {
SDL.SDL_CloseAudioDevice(self.device);
self.* = undefined;
fn deinit(this: This) void {
SDL.SDL_CloseAudioDevice(this.device);
}
pub fn play(this: *This) void {

View File

@ -49,8 +49,8 @@ io: Io,
cpu: ?*Arm7tdmi,
sched: *Scheduler,
pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, paths: FilePaths) !void {
self.* = .{
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),
@ -60,34 +60,21 @@ pub fn init(self: *Self, alloc: Allocator, sched: *Scheduler, cpu: *Arm7tdmi, pa
.dma = createDmaTuple(),
.tim = createTimerTuple(sched),
.io = Io.init(),
.cpu = cpu,
.cpu = null,
.sched = sched,
};
}
// 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 {
pub fn deinit(self: Self) void {
self.iwram.deinit();
self.ewram.deinit();
self.pak.deinit();
self.bios.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 {

View File

@ -27,9 +27,8 @@ 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);
self.* = undefined;
}
pub fn read(self: *Self, comptime T: type, r15: u32, addr: u32) T {

View File

@ -17,9 +17,8 @@ pub fn init(alloc: Allocator) !Self {
};
}
pub fn deinit(self: *Self) void {
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
self.* = undefined;
}
pub fn read(self: *const Self, comptime T: type, address: usize) T {

View File

@ -58,10 +58,9 @@ inline fn isLarge(self: *const Self) bool {
return self.buf.len > 0x100_0000;
}
pub fn deinit(self: *Self) void {
self.backup.deinit();
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
self.* = undefined;
self.backup.deinit();
}
pub fn read(self: *Self, comptime T: type, address: u32) T {

View File

@ -17,9 +17,8 @@ pub fn init(alloc: Allocator) !Self {
};
}
pub fn deinit(self: *Self) void {
pub fn deinit(self: Self) void {
self.alloc.free(self.buf);
self.* = undefined;
}
pub fn read(self: *const Self, comptime T: type, address: usize) T {

View File

@ -74,10 +74,9 @@ pub const Backup = struct {
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});
self.alloc.free(self.buf);
self.* = undefined;
}
fn loadSaveFromDisk(self: *Self, allocator: Allocator, path: []const u8) !void {

View File

@ -250,7 +250,7 @@ pub const Arm7tdmi = struct {
logger: ?Logger,
pub fn init(sched: *Scheduler, bus: *Bus, log_file: ?std.fs.File) Self {
pub fn init(sched: *Scheduler, bus: *Bus) Self {
return Self{
.r = [_]u32{0x00} ** 16,
.sched = sched,
@ -260,10 +260,14 @@ pub const Arm7tdmi = struct {
.banked_fiq = [_]u32{0x00} ** 10,
.banked_r = [_]u32{0x00} ** 12,
.banked_spsr = [_]PSR{.{ .raw = 0x0000_0000 }} ** 5,
.logger = if (log_file) |file| Logger.init(file) else null,
.logger = 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,

View File

@ -43,23 +43,26 @@ pub const Ppu = struct {
framebuf: FrameBuffer,
alloc: Allocator,
scanline_sprites: *[128]?Sprite,
scanline_sprites: [128]?Sprite,
scanline: Scanline,
pub fn init(allocator: Allocator, sched: *Scheduler) !Self {
pub fn init(alloc: Allocator, sched: *Scheduler) !Self {
// Queue first Hblank
sched.push(.Draw, 240 * 4);
const sprites = try allocator.create([128]?Sprite);
sprites.* = [_]?Sprite{null} ** 128;
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{
.vram = try Vram.init(allocator),
.palette = try Palette.init(allocator),
.oam = try Oam.init(allocator),
.vram = try Vram.init(alloc),
.palette = try Palette.init(alloc),
.oam = try Oam.init(alloc),
.sched = sched,
.framebuf = try FrameBuffer.init(allocator),
.alloc = allocator,
.framebuf = FrameBuffer.init(framebufs),
.alloc = alloc,
// Registers
.win = Window.init(),
@ -72,19 +75,17 @@ pub const Ppu = struct {
.bldalpha = .{ .raw = 0x0000 },
.bldy = .{ .raw = 0x0000 },
.scanline = try Scanline.init(allocator),
.scanline_sprites = sprites,
.scanline = Scanline.init(scanline_buf),
.scanline_sprites = [_]?Sprite{null} ** 128,
};
}
pub fn deinit(self: *Self) void {
self.alloc.destroy(self.scanline_sprites);
self.framebuf.deinit();
self.scanline.deinit();
pub fn deinit(self: Self) void {
self.framebuf.deinit(self.alloc);
self.scanline.deinit(self.alloc);
self.vram.deinit();
self.palette.deinit();
self.oam.deinit();
self.* = undefined;
}
pub fn setBgOffsets(self: *Self, comptime n: u2, word: u32) void {
@ -398,7 +399,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);
@ -425,7 +426,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);
@ -451,7 +452,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);
@ -640,9 +641,8 @@ const Palette = struct {
};
}
fn deinit(self: *Self) void {
fn deinit(self: Self) void {
self.alloc.free(self.buf);
self.* = undefined;
}
pub fn read(self: *const Self, comptime T: type, address: usize) T {
@ -689,9 +689,8 @@ const Vram = struct {
};
}
fn deinit(self: *Self) void {
fn deinit(self: Self) void {
self.alloc.free(self.buf);
self.* = undefined;
}
pub fn read(self: *const Self, comptime T: type, address: usize) T {
@ -750,9 +749,8 @@ const Oam = struct {
};
}
fn deinit(self: *Self) void {
fn deinit(self: Self) void {
self.alloc.free(self.buf);
self.* = undefined;
}
pub fn read(self: *const Self, comptime T: type, address: usize) T {
@ -1215,38 +1213,35 @@ fn copyToSpriteBuffer(bldcnt: io.BldCnt, scanline: *Scanline, x: u9, bgr555: u16
const Scanline = struct {
const Self = @This();
layers: [2][]?u16,
buf: []?u16,
buf: [2][]?u16,
original: []?u16,
allocator: Allocator,
fn init(buf: []?u16) Self {
std.debug.assert(buf.len == width * 2);
fn init(allocator: Allocator) !Self {
const buf = try allocator.alloc(?u16, width * 2); // Top & Bottom Scanline
std.mem.set(?u16, buf, null);
const top_slice = buf[0..][0..width];
const btm_slice = buf[width..][0..width];
return .{
// Top & Bototm Layers
.layers = [_][]?u16{ buf[0..][0..width], buf[width..][0..width] },
.buf = buf,
.allocator = allocator,
.buf = [_][]?u16{ top_slice, btm_slice },
.original = buf,
};
}
fn reset(self: *Self) void {
std.mem.set(?u16, self.buf, null);
std.mem.set(?u16, self.original, null);
}
fn deinit(self: *Self) void {
self.allocator.free(self.buf);
self.* = undefined;
fn deinit(self: Self, alloc: Allocator) void {
alloc.free(self.original);
}
fn top(self: *Self) []?u16 {
return self.layers[0];
return self.buf[0];
}
fn btm(self: *Self) []?u16 {
return self.layers[1];
return self.buf[1];
}
};
@ -1254,36 +1249,31 @@ const Scanline = struct {
const FrameBuffer = struct {
const Self = @This();
layers: [2][]u8,
buf: []u8,
buf: [2][]u8,
original: []u8,
current: u1,
allocator: Allocator,
// TODO: Rename
const Device = enum {
Emulator,
Renderer,
};
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);
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 ..];
return .{
// Front and Back Framebuffers
.layers = [_][]u8{ buf[0..][0..framebuf_len], buf[framebuf_len..][0..framebuf_len] },
.buf = buf,
.buf = [2][]u8{ front, back },
.original = bufs,
.current = 0,
.allocator = allocator,
};
}
fn deinit(self: *Self) void {
self.allocator.free(self.buf);
self.* = undefined;
fn deinit(self: Self, alloc: Allocator) void {
alloc.free(self.original);
}
pub fn swap(self: *Self) void {
@ -1291,6 +1281,6 @@ const FrameBuffer = struct {
}
pub fn get(self: *Self, comptime dev: Device) []u8 {
return self.layers[if (dev == .Emulator) self.current else ~self.current];
return self.buf[if (dev == .Emulator) self.current else ~self.current];
}
};

View File

@ -21,9 +21,8 @@ pub const Scheduler = struct {
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 {

View File

@ -40,20 +40,22 @@ 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;
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: Bus = undefined;
var arm7tdmi = Arm7tdmi.init(&scheduler, &bus, log_file);
if (paths.bios == null) arm7tdmi.fastBoot();
try bus.init(allocator, &scheduler, &arm7tdmi, paths);
var bus = try Bus.init(allocator, &scheduler, paths);
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();