fix: allocate framebuf on heap

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:11:51 -03:00
parent f28b963f9d
commit c33068d831
2 changed files with 18 additions and 9 deletions

View File

@ -6,6 +6,8 @@ const Bus = @import("Bus.zig");
const Arm7tdmi = @import("cpu.zig").Arm7tdmi; const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const buf_pitch = @import("ppu.zig").buf_pitch;
pub fn main() anyerror!void { pub fn main() anyerror!void {
// Allocator for Emulator + CLI Aruments // Allocator for Emulator + CLI Aruments
var gpa = std.heap.GeneralPurposeAllocator(.{}){}; var gpa = std.heap.GeneralPurposeAllocator(.{}){};
@ -57,14 +59,6 @@ pub fn main() anyerror!void {
const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_BGR555, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160) orelse sdlPanic(); const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_BGR555, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160) orelse sdlPanic();
defer SDL.SDL_DestroyTexture(texture); defer SDL.SDL_DestroyTexture(texture);
const buf_pitch = 240 * @sizeOf(u16);
const buf_len = buf_pitch * 160;
var white: [buf_len]u8 = [_]u8{ 0xFF, 0x7F } ** (buf_len / 2);
var white_heap = try alloc.alloc(u8, buf_len);
for (white) |b, i| white_heap[i] = b;
defer alloc.free(white_heap);
emu_loop: while (true) { emu_loop: while (true) {
emu.runFrame(&scheduler, &cpu, &bus); emu.runFrame(&scheduler, &cpu, &bus);
@ -76,7 +70,8 @@ pub fn main() anyerror!void {
else => {}, else => {},
} }
_ = SDL.SDL_UpdateTexture(texture, null, &white_heap, buf_pitch); const buf_ptr = bus.ppu.frame_buf.ptr;
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, buf_pitch);
_ = SDL.SDL_RenderCopy(renderer, texture, null, null); _ = SDL.SDL_RenderCopy(renderer, texture, null, null);
SDL.SDL_RenderPresent(renderer); SDL.SDL_RenderPresent(renderer);
} }

View File

@ -4,24 +4,38 @@ const EventKind = @import("scheduler.zig").EventKind;
const Scheduler = @import("scheduler.zig").Scheduler; const Scheduler = @import("scheduler.zig").Scheduler;
const Allocator = std.mem.Allocator; const Allocator = std.mem.Allocator;
const width = 240;
const height = 160;
pub const buf_pitch = width * @sizeOf(u16);
const buf_len = buf_pitch * height;
pub const Ppu = struct { pub const Ppu = struct {
vram: Vram, vram: Vram,
palette: Palette, palette: Palette,
sched: *Scheduler, sched: *Scheduler,
frame_buf: []u8,
alloc: Allocator,
pub fn init(alloc: Allocator, sched: *Scheduler) !@This() { pub fn init(alloc: Allocator, sched: *Scheduler) !@This() {
// Queue first Hblank // Queue first Hblank
sched.push(.{ .kind = .HBlank, .tick = sched.tick + 240 * 4 }); sched.push(.{ .kind = .HBlank, .tick = sched.tick + 240 * 4 });
// Initialize the Frame Buffer to white
const white_buf: [buf_len]u8 = [_]u8{ 0xFF, 0x7F } ** (buf_len / 2);
const frame_buf = try alloc.alloc(u8, buf_len);
std.mem.copy(u8, frame_buf, &white_buf);
return @This(){ return @This(){
.vram = try Vram.init(alloc), .vram = try Vram.init(alloc),
.palette = try Palette.init(alloc), .palette = try Palette.init(alloc),
.sched = sched, .sched = sched,
.frame_buf = frame_buf,
.alloc = alloc,
}; };
} }
pub fn deinit(self: @This()) void { pub fn deinit(self: @This()) void {
self.alloc.free(self.frame_buf);
self.vram.deinit(); self.vram.deinit();
self.palette.deinit(); self.palette.deinit();
} }