2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
2022-01-08 02:46:17 +00:00
|
|
|
const SDL = @import("sdl2");
|
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;
|
2021-12-29 21:09:00 +00:00
|
|
|
|
|
|
|
pub fn main() anyerror!void {
|
2022-01-08 02:46:17 +00:00
|
|
|
// Allocator for Emulator + CLI Aruments
|
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-01-08 02:46:17 +00:00
|
|
|
// Handle CLI Arguments
|
2022-01-02 05:37:21 +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-01-08 02:46:17 +00:00
|
|
|
// Initialize Emulator
|
2022-01-02 19:58:57 +00:00
|
|
|
var scheduler = Scheduler.init(alloc);
|
2022-01-04 02:08:55 +00:00
|
|
|
defer scheduler.deinit();
|
|
|
|
|
2022-01-05 22:34:42 +00:00
|
|
|
var bus = try Bus.init(alloc, &scheduler, zba_args[0]);
|
|
|
|
defer bus.deinit();
|
|
|
|
|
2022-01-02 19:58:57 +00:00
|
|
|
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
2022-01-02 20:58:39 +00:00
|
|
|
cpu.skipBios();
|
|
|
|
|
2022-01-08 02:46:17 +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(
|
|
|
|
"Gameboy Advance Emulator",
|
|
|
|
SDL.SDL_WINDOWPOS_CENTERED,
|
|
|
|
SDL.SDL_WINDOWPOS_CENTERED,
|
|
|
|
240 * 3,
|
|
|
|
160 * 3,
|
|
|
|
SDL.SDL_WINDOW_SHOWN,
|
|
|
|
) orelse sdlPanic();
|
|
|
|
defer _ = SDL.SDL_DestroyWindow(window);
|
|
|
|
|
|
|
|
var renderer = SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED) orelse sdlPanic();
|
|
|
|
defer _ = SDL.SDL_DestroyRenderer(renderer);
|
|
|
|
|
|
|
|
const texture = SDL.SDL_CreateTexture(renderer, SDL.SDL_PIXELFORMAT_RGBA8888, SDL.SDL_TEXTUREACCESS_STREAMING, 240, 160);
|
|
|
|
defer SDL.SDL_DestroyTexture(texture);
|
|
|
|
|
|
|
|
const buf_len = (240 * 4) * 160;
|
|
|
|
var white: [buf_len]u8 = [_]u8{0xFF} ** buf_len;
|
|
|
|
|
|
|
|
emu_loop: while (true) {
|
2021-12-29 21:09:00 +00:00
|
|
|
emu.runFrame(&scheduler, &cpu, &bus);
|
2022-01-08 02:46:17 +00:00
|
|
|
|
|
|
|
var event: SDL.SDL_Event = undefined;
|
|
|
|
_ = SDL.SDL_PollEvent(&event);
|
|
|
|
|
|
|
|
switch (event.type) {
|
|
|
|
SDL.SDL_QUIT => break :emu_loop,
|
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
|
|
|
|
_ = SDL.SDL_UpdateTexture(texture, null, &white, 240 * 4);
|
|
|
|
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
|
|
|
|
SDL.SDL_RenderPresent(renderer);
|
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
|
|
|
}
|