2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
2022-10-21 08:11:50 +00:00
|
|
|
const SDL = @import("sdl2");
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
const emu = @import("emu.zig");
|
2022-10-21 08:11:50 +00:00
|
|
|
const Bus = @import("Bus.zig");
|
2022-10-21 08:11:44 +00:00
|
|
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
2022-10-21 08:11:50 +00:00
|
|
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
const Timer = std.time.Timer;
|
2022-10-21 08:12:03 +00:00
|
|
|
const Thread = std.Thread;
|
|
|
|
const Atomic = std.atomic.Atomic;
|
2022-10-21 08:12:04 +00:00
|
|
|
const File = std.fs.File;
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:11:54 +00:00
|
|
|
const window_scale = 3;
|
|
|
|
const gba_width = @import("ppu.zig").width;
|
|
|
|
const gba_height = @import("ppu.zig").height;
|
2022-10-21 08:11:51 +00:00
|
|
|
const buf_pitch = @import("ppu.zig").buf_pitch;
|
|
|
|
|
2022-10-21 08:12:03 +00:00
|
|
|
pub const enable_logging: bool = false;
|
2022-10-21 08:12:03 +00:00
|
|
|
const is_binary: bool = false;
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2021-12-29 21:09:00 +00:00
|
|
|
pub fn main() anyerror!void {
|
2022-10-21 08:12:03 +00:00
|
|
|
// Allocator for Emulator + CLI
|
2021-12-29 21:09:00 +00:00
|
|
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
|
|
|
const alloc = gpa.allocator();
|
2022-10-21 08:11:47 +00:00
|
|
|
defer std.debug.assert(!gpa.deinit());
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
// Handle CLI Arguments
|
2022-10-21 08:11:45 +00:00
|
|
|
const args = try std.process.argsAlloc(alloc);
|
|
|
|
defer std.process.argsFree(alloc, args);
|
|
|
|
|
|
|
|
const zba_args: []const []const u8 = args[1..];
|
|
|
|
|
|
|
|
if (zba_args.len == 0) {
|
|
|
|
std.log.err("Expected PATH to Gameboy Advance ROM as a CLI argument", .{});
|
|
|
|
return;
|
|
|
|
} else if (zba_args.len > 1) {
|
|
|
|
std.log.err("Too many CLI arguments were provided", .{});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
// Initialize Emulator
|
2022-10-21 08:11:45 +00:00
|
|
|
var scheduler = Scheduler.init(alloc);
|
2022-10-21 08:11:47 +00:00
|
|
|
defer scheduler.deinit();
|
|
|
|
|
2022-10-21 08:11:49 +00:00
|
|
|
var bus = try Bus.init(alloc, &scheduler, zba_args[0]);
|
|
|
|
defer bus.deinit();
|
|
|
|
|
2022-10-21 08:11:45 +00:00
|
|
|
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
2022-10-21 08:12:02 +00:00
|
|
|
cpu.fastBoot();
|
2022-10-21 08:11:46 +00:00
|
|
|
|
2022-10-21 08:12:04 +00:00
|
|
|
var log_file: ?File = undefined;
|
2022-10-21 08:12:03 +00:00
|
|
|
if (enable_logging) {
|
2022-10-21 08:12:04 +00:00
|
|
|
const file_name: []const u8 = if (is_binary) "zba.bin" else "zba.log";
|
2022-10-21 08:12:03 +00:00
|
|
|
const file = try std.fs.cwd().createFile(file_name, .{ .read = true });
|
|
|
|
cpu.useLogger(&file, is_binary);
|
2022-10-21 08:12:04 +00:00
|
|
|
|
|
|
|
log_file = file;
|
2022-10-21 08:12:03 +00:00
|
|
|
}
|
2022-10-21 08:12:04 +00:00
|
|
|
defer if (log_file) |file| file.close();
|
2022-10-21 08:12:03 +00:00
|
|
|
|
|
|
|
// Init Atomics
|
|
|
|
var quit = Atomic(bool).init(false);
|
|
|
|
|
|
|
|
// Create Emulator Thread
|
|
|
|
const emu_thread = try Thread.spawn(.{}, emu.runEmuThread, .{ &quit, &scheduler, &cpu, &bus });
|
|
|
|
defer emu_thread.join();
|
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
// Initialize SDL
|
|
|
|
const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO);
|
|
|
|
if (status < 0) sdlPanic();
|
|
|
|
defer SDL.SDL_Quit();
|
|
|
|
|
|
|
|
var window = SDL.SDL_CreateWindow(
|
2022-10-21 08:12:04 +00:00
|
|
|
"ZBA",
|
2022-10-21 08:11:50 +00:00
|
|
|
SDL.SDL_WINDOWPOS_CENTERED,
|
|
|
|
SDL.SDL_WINDOWPOS_CENTERED,
|
2022-10-21 08:11:54 +00:00
|
|
|
gba_width * window_scale,
|
|
|
|
gba_height * window_scale,
|
2022-10-21 08:11:50 +00:00
|
|
|
SDL.SDL_WINDOW_SHOWN,
|
|
|
|
) orelse sdlPanic();
|
2022-10-21 08:11:51 +00:00
|
|
|
defer SDL.SDL_DestroyWindow(window);
|
2022-10-21 08:11:50 +00:00
|
|
|
|
|
|
|
var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED) orelse sdlPanic();
|
2022-10-21 08:11:51 +00:00
|
|
|
defer SDL.SDL_DestroyRenderer(renderer);
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_BGR555, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160) orelse sdlPanic();
|
2022-10-21 08:11:50 +00:00
|
|
|
defer SDL.SDL_DestroyTexture(texture);
|
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
// Init FPS Timer
|
2022-10-21 08:12:08 +00:00
|
|
|
// var timer = Timer.start() catch unreachable;
|
|
|
|
// var title_buf: [0x30]u8 = [_]u8{0x00} ** 0x30;
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:11:50 +00:00
|
|
|
emu_loop: while (true) {
|
|
|
|
var event: SDL.SDL_Event = undefined;
|
|
|
|
_ = SDL.SDL_PollEvent(&event);
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
SDL.SDL_QUIT => break :emu_loop,
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:03 +00:00
|
|
|
// TODO: Make this Thread Safe
|
2022-10-21 08:11:51 +00:00
|
|
|
const buf_ptr = bus.ppu.frame_buf.ptr;
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:11:51 +00:00
|
|
|
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, buf_pitch);
|
2022-10-21 08:11:50 +00:00
|
|
|
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
|
|
|
|
SDL.SDL_RenderPresent(renderer);
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:12:08 +00:00
|
|
|
// const fps = std.time.ns_per_s / timer.lap();
|
|
|
|
// const title = std.fmt.bufPrint(&title_buf, "ZBA FPS: {d}", .{fps}) catch unreachable;
|
|
|
|
// SDL.SDL_SetWindowTitle(window, title.ptr);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
2022-10-21 08:12:03 +00:00
|
|
|
|
|
|
|
quit.store(true, .Unordered); // Terminate Emulator Thread
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:11:50 +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
|
|
|
}
|