feat: parse cartridge header
This commit is contained in:
parent
271f42cf0e
commit
c3ff2ed6c1
|
@ -3,18 +3,48 @@ const std = @import("std");
|
||||||
const Allocator = std.mem.Allocator;
|
const Allocator = std.mem.Allocator;
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
|
||||||
|
title: [12]u8,
|
||||||
buf: []u8,
|
buf: []u8,
|
||||||
alloc: Allocator,
|
alloc: Allocator,
|
||||||
|
|
||||||
|
const log = std.log.scoped(.GamePak);
|
||||||
|
|
||||||
pub fn init(alloc: Allocator, path: []const u8) !Self {
|
pub fn init(alloc: Allocator, path: []const u8) !Self {
|
||||||
const file = try std.fs.cwd().openFile(path, .{});
|
const file = try std.fs.cwd().openFile(path, .{});
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
const len = try file.getEndPos();
|
const len = try file.getEndPos();
|
||||||
|
const buf = try file.readToEndAlloc(alloc, len);
|
||||||
|
const title = parseTitle(buf);
|
||||||
|
|
||||||
return Self{
|
const pak = Self{ .buf = buf, .alloc = alloc, .title = title };
|
||||||
.buf = try file.readToEndAlloc(alloc, len),
|
pak.parseHeader();
|
||||||
.alloc = alloc,
|
|
||||||
|
return pak;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseHeader(self: *const Self) void {
|
||||||
|
const title = parseTitle(self.buf);
|
||||||
|
const code = self.buf[0xAC..0xB0];
|
||||||
|
const maker = self.buf[0xB0..0xB2];
|
||||||
|
const version = self.buf[0xBC];
|
||||||
|
|
||||||
|
log.info("Title: {s}", .{title});
|
||||||
|
if (version != 0) log.info("Version: {}", .{version});
|
||||||
|
log.info("Game Code: {s}", .{code});
|
||||||
|
if (lookupMaker(maker)) |c| log.info("Maker Code: {s}", .{c}) else log.info("Maker: {s}", .{maker});
|
||||||
|
}
|
||||||
|
|
||||||
|
fn parseTitle(buf: []u8) [12]u8 {
|
||||||
|
return buf[0xA0..0xAC].*;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn lookupMaker(slice: *const [2]u8) ?[]const u8 {
|
||||||
|
const num = @as(u16, slice[1]) << 8 | @as(u16, slice[0]);
|
||||||
|
|
||||||
|
return switch (num) {
|
||||||
|
0x3130 => "Nintendo",
|
||||||
|
else => null,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
11
src/main.zig
11
src/main.zig
|
@ -94,8 +94,11 @@ pub fn main() anyerror!void {
|
||||||
if (status < 0) sdlPanic();
|
if (status < 0) sdlPanic();
|
||||||
defer SDL.SDL_Quit();
|
defer SDL.SDL_Quit();
|
||||||
|
|
||||||
|
var title_buf: [0x20]u8 = [_]u8{0x00} ** 0x20;
|
||||||
|
const title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{bus.pak.title});
|
||||||
|
|
||||||
var window = SDL.SDL_CreateWindow(
|
var window = SDL.SDL_CreateWindow(
|
||||||
"ZBA",
|
title.ptr,
|
||||||
SDL.SDL_WINDOWPOS_CENTERED,
|
SDL.SDL_WINDOWPOS_CENTERED,
|
||||||
SDL.SDL_WINDOWPOS_CENTERED,
|
SDL.SDL_WINDOWPOS_CENTERED,
|
||||||
gba_width * window_scale,
|
gba_width * window_scale,
|
||||||
|
@ -111,8 +114,8 @@ pub fn main() anyerror!void {
|
||||||
defer SDL.SDL_DestroyTexture(texture);
|
defer SDL.SDL_DestroyTexture(texture);
|
||||||
|
|
||||||
// Init FPS Timer
|
// Init FPS Timer
|
||||||
|
// var fps_buf: [0x100]u8 = [_]u8{0x00} ** 0x100;
|
||||||
// var timer = Timer.start() catch unreachable;
|
// var timer = Timer.start() catch unreachable;
|
||||||
// var title_buf: [0x30]u8 = [_]u8{0x00} ** 0x30;
|
|
||||||
|
|
||||||
emu_loop: while (true) {
|
emu_loop: while (true) {
|
||||||
var event: SDL.SDL_Event = undefined;
|
var event: SDL.SDL_Event = undefined;
|
||||||
|
@ -167,8 +170,8 @@ pub fn main() anyerror!void {
|
||||||
SDL.SDL_RenderPresent(renderer);
|
SDL.SDL_RenderPresent(renderer);
|
||||||
|
|
||||||
// const fps = std.time.ns_per_s / timer.lap();
|
// const fps = std.time.ns_per_s / timer.lap();
|
||||||
// const title = std.fmt.bufPrint(&title_buf, "ZBA FPS: {d}", .{fps}) catch unreachable;
|
// const fps_title = std.fmt.bufPrint(&fps_buf, "{s} [FPS: {d}]", .{ title, fps }) catch unreachable;
|
||||||
// SDL.SDL_SetWindowTitle(window, title.ptr);
|
// SDL.SDL_SetWindowTitle(window, fps_title.ptr);
|
||||||
|
|
||||||
pause.store(false, .Unordered);
|
pause.store(false, .Unordered);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue