From 8f9dc9d33cf2e2fb6b3b4d772615afe50f86c320 Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Sun, 18 Sep 2022 07:41:37 -0300 Subject: [PATCH] chore: move FrameBuffer struct to util.zig --- src/core/ppu.zig | 48 ++--------------------------------------------- src/core/util.zig | 44 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/src/core/ppu.zig b/src/core/ppu.zig index 2ea45f0..32a8d09 100644 --- a/src/core/ppu.zig +++ b/src/core/ppu.zig @@ -10,6 +10,7 @@ const Vram = @import("ppu/Vram.zig"); const EventKind = @import("scheduler.zig").EventKind; const Scheduler = @import("scheduler.zig").Scheduler; const Arm7tdmi = @import("cpu.zig").Arm7tdmi; +const FrameBuffer = @import("util.zig").FrameBuffer; const Allocator = std.mem.Allocator; const log = std.log.scoped(.Ppu); @@ -60,7 +61,7 @@ pub const Ppu = struct { .palette = try Palette.init(allocator), .oam = try Oam.init(allocator), .sched = sched, - .framebuf = try FrameBuffer.init(allocator), + .framebuf = try FrameBuffer.init(allocator, framebuf_pitch * height), .allocator = allocator, // Registers @@ -1159,48 +1160,3 @@ const Scanline = struct { return self.layers[1]; } }; - -// Double Buffering Implementation -const FrameBuffer = struct { - const Self = @This(); - - layers: [2][]u8, - buf: []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); - - return .{ - // 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) void { - self.allocator.free(self.buf); - self.* = undefined; - } - - pub fn swap(self: *Self) void { - self.current = ~self.current; - } - - pub fn get(self: *Self, comptime dev: Device) []u8 { - return self.layers[if (dev == .Emulator) self.current else ~self.current]; - } -}; diff --git a/src/core/util.zig b/src/core/util.zig index 51d2760..92db973 100644 --- a/src/core/util.zig +++ b/src/core/util.zig @@ -3,6 +3,8 @@ const builtin = @import("builtin"); const Log2Int = std.math.Log2Int; const Arm7tdmi = @import("cpu.zig").Arm7tdmi; +const Allocator = std.mem.Allocator; + const allow_unhandled_io = @import("emu.zig").allow_unhandled_io; // Sign-Extend value of type `T` to type `U` @@ -172,6 +174,7 @@ pub fn writeUndefined(log: anytype, comptime format: []const u8, args: anytype) pub const Logger = struct { const Self = @This(); + const FmtArgTuple = std.meta.Tuple(&.{ u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 }); buf: std.io.BufferedWriter(4096 << 2, std.fs.File.Writer), @@ -229,4 +232,43 @@ pub const Logger = struct { } }; -const FmtArgTuple = std.meta.Tuple(&.{ u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32, u32 }); +// Double Buffering Implementation +pub const FrameBuffer = struct { + const Self = @This(); + + layers: [2][]u8, + buf: []u8, + current: u1, + + allocator: Allocator, + + // TODO: Rename + const Device = enum { Emulator, Renderer }; + + pub fn init(allocator: Allocator, comptime len: comptime_int) !Self { + const buf = try allocator.alloc(u8, len * 2); + std.mem.set(u8, buf, 0); + + return .{ + // Front and Back Framebuffers + .layers = [_][]u8{ buf[0..][0..len], buf[len..][0..len] }, + .buf = buf, + .current = 0, + + .allocator = allocator, + }; + } + + pub fn deinit(self: *Self) void { + self.allocator.free(self.buf); + self.* = undefined; + } + + pub fn swap(self: *Self) void { + self.current = ~self.current; + } + + pub fn get(self: *Self, comptime dev: Device) []u8 { + return self.layers[if (dev == .Emulator) self.current else ~self.current]; + } +};