zba/src/main.zig

208 lines
7.4 KiB
Zig
Raw Normal View History

2021-12-29 21:09:00 +00:00
const std = @import("std");
2022-01-08 02:46:17 +00:00
const SDL = @import("sdl2");
2022-02-04 05:55:14 +00:00
const clap = @import("clap");
2021-12-29 21:09:00 +00:00
2022-01-08 02:46:17 +00:00
const emu = @import("emu.zig");
2022-01-07 23:49:58 +00:00
const Bus = @import("Bus.zig");
2022-01-02 03:08:36 +00:00
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
2022-01-08 00:00:42 +00:00
const Scheduler = @import("scheduler.zig").Scheduler;
const FpsAverage = @import("util.zig").FpsAverage;
2021-12-29 21:09:00 +00:00
2022-01-10 05:22:55 +00:00
const Timer = std.time.Timer;
2022-01-28 20:33:38 +00:00
const Thread = std.Thread;
const Atomic = std.atomic.Atomic;
2022-01-29 05:17:29 +00:00
const File = std.fs.File;
2022-01-10 05:22:55 +00:00
2022-01-12 04:46:20 +00:00
const window_scale = 3;
const gba_width = @import("ppu.zig").width;
const gba_height = @import("ppu.zig").height;
const framebuf_pitch = @import("ppu.zig").framebuf_pitch;
2022-01-09 00:30:57 +00:00
2022-01-28 20:33:38 +00:00
pub const enable_logging: bool = false;
2022-01-29 02:57:48 +00:00
const is_binary: bool = false;
2022-01-28 20:33:38 +00:00
2021-12-29 21:09:00 +00:00
pub fn main() anyerror!void {
2022-01-28 20:33:38 +00:00
// Allocator for Emulator + CLI
2021-12-29 21:09:00 +00:00
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gpa.allocator();
2022-01-04 02:08:55 +00:00
defer std.debug.assert(!gpa.deinit());
2021-12-29 21:09:00 +00:00
2022-02-04 05:55:14 +00:00
// Parse CLI Arguments
const params = comptime [_]clap.Param(clap.Help){
clap.parseParam("-h, --help Display this help and exit. ") catch unreachable,
clap.parseParam("-b, --bios <PATH> Optional Path to GBA BIOS ROM. ") catch unreachable,
clap.parseParam("<PATH> Path to GBA GamePak ROM ") catch unreachable,
};
2022-01-02 05:37:21 +00:00
2022-02-04 05:55:14 +00:00
var args = try clap.parse(clap.Help, &params, .{});
defer args.deinit();
2022-01-02 05:37:21 +00:00
2022-02-04 05:55:14 +00:00
if (args.flag("--help")) {
return clap.help(std.io.getStdErr().writer(), &params);
2022-01-02 05:37:21 +00:00
}
2022-02-04 05:55:14 +00:00
var maybe_bios: ?[]const u8 = null;
if (args.option("--bios")) |path| {
maybe_bios = path;
}
const positionals = args.positionals();
const stderr = std.io.getStdErr();
defer stderr.close();
const rom_path = switch (positionals.len) {
1 => positionals[0],
0 => {
try stderr.writeAll("ZBA requires a positional path to a GamePak ROM.\n");
return CliError.InsufficientOptions;
},
else => {
try stderr.writeAll("ZBA received too many arguments.\n");
return CliError.UnneededOptions;
},
};
2022-01-08 02:46:17 +00:00
// Initialize Emulator
var scheduler = Scheduler.init(alloc);
2022-01-04 02:08:55 +00:00
defer scheduler.deinit();
2022-02-04 05:55:14 +00:00
var bus = try Bus.init(alloc, &scheduler, rom_path, maybe_bios);
defer bus.deinit();
var cpu = Arm7tdmi.init(&scheduler, &bus);
2022-01-25 15:15:17 +00:00
cpu.fastBoot();
2022-01-02 20:58:39 +00:00
2022-01-29 05:17:29 +00:00
var log_file: ?File = undefined;
2022-01-28 20:33:38 +00:00
if (enable_logging) {
2022-01-29 05:17:29 +00:00
const file_name: []const u8 = if (is_binary) "zba.bin" else "zba.log";
2022-02-03 01:26:12 +00:00
const file = try std.fs.cwd().createFile(file_name, .{});
2022-01-28 20:33:38 +00:00
cpu.useLogger(&file, is_binary);
2022-01-29 05:17:29 +00:00
log_file = file;
2022-01-28 20:33:38 +00:00
}
2022-01-29 05:17:29 +00:00
defer if (log_file) |file| file.close();
2022-01-28 20:33:38 +00:00
// Init Atomics
var quit = Atomic(bool).init(false);
2022-02-01 20:11:59 +00:00
var pause = Atomic(bool).init(false);
2022-03-14 08:16:02 +00:00
var emu_fps = Atomic(u64).init(0);
2022-01-28 20:33:38 +00:00
// Create Emulator Thread
2022-03-14 08:16:02 +00:00
const emu_thread = try Thread.spawn(.{}, emu.runEmuThread, .{ &quit, &pause, &emu_fps, &scheduler, &cpu, &bus });
2022-01-28 20:33:38 +00:00
defer emu_thread.join();
2022-01-08 02:46:17 +00:00
// Initialize SDL
2022-02-01 20:11:59 +00:00
const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_GAMECONTROLLER);
2022-01-08 02:46:17 +00:00
if (status < 0) sdlPanic();
defer SDL.SDL_Quit();
2022-03-14 08:16:02 +00:00
var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8);
var title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{bus.pak.title});
correctTitleSlice(&title);
2022-02-04 09:54:06 +00:00
2022-01-08 02:46:17 +00:00
var window = SDL.SDL_CreateWindow(
2022-02-04 09:54:06 +00:00
title.ptr,
2022-01-08 02:46:17 +00:00
SDL.SDL_WINDOWPOS_CENTERED,
SDL.SDL_WINDOWPOS_CENTERED,
2022-01-12 04:46:20 +00:00
gba_width * window_scale,
gba_height * window_scale,
2022-01-08 02:46:17 +00:00
SDL.SDL_WINDOW_SHOWN,
) orelse sdlPanic();
defer SDL.SDL_DestroyWindow(window);
2022-01-08 02:46:17 +00:00
2022-03-14 08:16:02 +00:00
var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED | SDL.SDL_RENDERER_PRESENTVSYNC) orelse sdlPanic();
defer SDL.SDL_DestroyRenderer(renderer);
2022-01-08 02:46:17 +00:00
const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_BGR555, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160) orelse sdlPanic();
2022-01-08 02:46:17 +00:00
defer SDL.SDL_DestroyTexture(texture);
2022-01-10 05:22:55 +00:00
// Init FPS Timer
2022-03-14 08:16:02 +00:00
var dyn_title_buf: [0x100]u8 = [_]u8{0x00} ** 0x100;
2022-01-10 05:22:55 +00:00
var fps_avg = FpsAverage.init();
2022-01-08 02:46:17 +00:00
emu_loop: while (true) {
var event: SDL.SDL_Event = undefined;
2022-02-01 20:11:59 +00:00
if (SDL.SDL_PollEvent(&event) != 0) {
// Pause Emulation Thread during Input Writing
switch (event.type) {
SDL.SDL_QUIT => break :emu_loop,
SDL.SDL_KEYDOWN => {
const key_code = event.key.keysym.sym;
switch (key_code) {
SDL.SDLK_UP => bus.io.keyinput.up.unset(),
SDL.SDLK_DOWN => bus.io.keyinput.down.unset(),
SDL.SDLK_LEFT => bus.io.keyinput.left.unset(),
SDL.SDLK_RIGHT => bus.io.keyinput.right.unset(),
SDL.SDLK_x => bus.io.keyinput.a.unset(),
SDL.SDLK_z => bus.io.keyinput.b.unset(),
SDL.SDLK_a => bus.io.keyinput.shoulder_l.unset(),
SDL.SDLK_s => bus.io.keyinput.shoulder_r.unset(),
SDL.SDLK_RETURN => bus.io.keyinput.start.unset(),
SDL.SDLK_RSHIFT => bus.io.keyinput.select.unset(),
else => {},
}
},
SDL.SDL_KEYUP => {
const key_code = event.key.keysym.sym;
switch (key_code) {
SDL.SDLK_UP => bus.io.keyinput.up.set(),
SDL.SDLK_DOWN => bus.io.keyinput.down.set(),
SDL.SDLK_LEFT => bus.io.keyinput.left.set(),
SDL.SDLK_RIGHT => bus.io.keyinput.right.set(),
SDL.SDLK_x => bus.io.keyinput.a.set(),
SDL.SDLK_z => bus.io.keyinput.b.set(),
SDL.SDLK_a => bus.io.keyinput.shoulder_l.set(),
SDL.SDLK_s => bus.io.keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => bus.io.keyinput.start.set(),
SDL.SDLK_RSHIFT => bus.io.keyinput.select.set(),
else => {},
}
},
else => {},
}
2022-01-08 02:46:17 +00:00
}
2022-02-01 20:11:59 +00:00
// FIXME: Is it OK just to copy the Emulator's Frame Buffer to SDL?
pause.store(true, .Unordered);
const buf_ptr = bus.ppu.framebuf.ptr;
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, framebuf_pitch);
2022-01-08 02:46:17 +00:00
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
SDL.SDL_RenderPresent(renderer);
pause.store(false, .Unordered);
2022-01-10 05:22:55 +00:00
fps_avg.add(emu_fps.load(.Unordered));
const avg = fps_avg.calc();
2022-02-01 20:11:59 +00:00
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3}fps, {d:0>3}%] ", .{ title, avg, (avg * 100 / 60) }) catch unreachable;
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
2021-12-29 21:09:00 +00:00
}
2022-01-28 20:33:38 +00:00
quit.store(true, .Unordered); // Terminate Emulator Thread
2021-12-29 21:09:00 +00:00
}
2022-01-08 02:46:17 +00:00
fn sdlPanic() noreturn {
const str = @as(?[*:0]const u8, SDL.SDL_GetError()) orelse "unknown error";
@panic(std.mem.sliceTo(str, 0));
2021-12-29 21:09:00 +00:00
}
2022-02-04 05:55:14 +00:00
const CliError = error{
InsufficientOptions,
UnneededOptions,
};
2022-03-14 08:16:02 +00:00
/// The slice considers some null values to be a part of the string
/// so change the length of the slice so that isn't the case
// FIXME: This is awful and bad
fn correctTitleSlice(title: *[]u8) void {
for (title.*) |char, i| {
if (char == 0) {
title.len = i;
break;
}
}
}