2021-12-29 21:09:00 +00:00
|
|
|
const std = @import("std");
|
2022-10-21 08:12:38 +00:00
|
|
|
const builtin = @import("builtin");
|
2022-10-21 08:11:50 +00:00
|
|
|
const SDL = @import("sdl2");
|
2022-10-21 08:12:13 +00:00
|
|
|
const clap = @import("clap");
|
2022-10-21 08:12:29 +00:00
|
|
|
const known_folders = @import("known_folders");
|
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:12:36 +00:00
|
|
|
const Apu = @import("apu.zig").Apu;
|
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;
|
2022-10-21 08:12:44 +00:00
|
|
|
const FpsTracker = @import("util.zig").FpsTracker;
|
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:12:38 +00:00
|
|
|
const window_scale = 4;
|
2022-10-21 08:11:54 +00:00
|
|
|
const gba_width = @import("ppu.zig").width;
|
|
|
|
const gba_height = @import("ppu.zig").height;
|
2022-10-21 08:12:15 +00:00
|
|
|
const framebuf_pitch = @import("ppu.zig").framebuf_pitch;
|
2022-10-21 08:12:29 +00:00
|
|
|
const expected_rate = @import("emu.zig").frame_rate;
|
2022-10-21 08:11:51 +00:00
|
|
|
|
2022-10-21 08:12:36 +00:00
|
|
|
const sample_rate = @import("apu.zig").host_sample_rate;
|
|
|
|
|
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:29 +00:00
|
|
|
const log = std.log.scoped(.GUI);
|
2022-10-21 08:12:38 +00:00
|
|
|
pub const log_level = if (builtin.mode != .Debug) .info else std.log.default_level;
|
2022-10-21 08:12:29 +00:00
|
|
|
|
2022-10-21 08:12:39 +00:00
|
|
|
const asString = @import("util.zig").asString;
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
// CLI Arguments + Help Text
|
|
|
|
const params = clap.parseParamsComptime(
|
|
|
|
\\-h, --help Display this help and exit.
|
|
|
|
\\-b, --bios <str> Optional path to a GBA BIOS ROM.
|
|
|
|
\\<str> Path to the GBA GamePak ROM
|
|
|
|
\\
|
|
|
|
);
|
|
|
|
|
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(.{}){};
|
2022-10-21 08:11:47 +00:00
|
|
|
defer std.debug.assert(!gpa.deinit());
|
2022-10-21 08:12:44 +00:00
|
|
|
const alloc = gpa.allocator();
|
2021-12-29 21:09:00 +00:00
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
// Setup CLI using zig-clap
|
2022-10-21 08:12:31 +00:00
|
|
|
var res = try clap.parse(clap.Help, ¶ms, clap.parsers.default, .{});
|
|
|
|
defer res.deinit();
|
2022-10-21 08:11:45 +00:00
|
|
|
|
2022-10-21 08:12:13 +00:00
|
|
|
const stderr = std.io.getStdErr();
|
|
|
|
defer stderr.close();
|
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
// Display Help, if requested
|
|
|
|
// Grab ROM and BIOS paths if provided
|
2022-10-21 08:12:31 +00:00
|
|
|
if (res.args.help) return clap.help(stderr.writer(), clap.Help, ¶ms, .{});
|
2022-10-21 08:12:44 +00:00
|
|
|
const rom_path = try getRomPath(res, stderr);
|
2022-10-21 08:12:31 +00:00
|
|
|
const bios_path: ?[]const u8 = if (res.args.bios) |p| p else null;
|
|
|
|
|
2022-10-21 08:12:29 +00:00
|
|
|
// Determine Save Directory
|
2022-10-21 08:12:44 +00:00
|
|
|
const save_dir = try getSavePath(alloc);
|
2022-10-21 08:12:37 +00:00
|
|
|
defer if (save_dir) |path| alloc.free(path);
|
|
|
|
log.info("Found save directory: {s}", .{save_dir});
|
2022-10-21 08:12:29 +00:00
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
// Initialize Scheduler and ARM7TDMI Emulator
|
|
|
|
// Provide GBA Bus (initialized with ARM7TDMI) with a valid ptr to ARM7TDMI
|
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:12:37 +00:00
|
|
|
const paths = .{ .bios = bios_path, .rom = rom_path, .save = save_dir };
|
2022-10-21 08:12:35 +00:00
|
|
|
var cpu = try Arm7tdmi.init(alloc, &scheduler, paths);
|
|
|
|
defer cpu.deinit();
|
2022-10-21 08:12:37 +00:00
|
|
|
cpu.bus.attach(&cpu);
|
2022-10-21 08:12:44 +00:00
|
|
|
// cpu.fastBoot(); // Uncomment to skip BIOS
|
|
|
|
|
|
|
|
// Copy ROM title while Emulator still belongs to this thread
|
|
|
|
const title = cpu.bus.pak.title;
|
|
|
|
|
|
|
|
// Initialize SDL2
|
|
|
|
initSdl2();
|
|
|
|
defer SDL.SDL_Quit();
|
2022-10-21 08:11:46 +00:00
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
const dev = initAudio(&cpu.bus.apu);
|
|
|
|
defer SDL.SDL_CloseAudioDevice(dev);
|
2022-10-21 08:12:36 +00:00
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
// TODO: Refactor or delete this Logging code
|
|
|
|
// I probably still need logging in some form though (e.g. Golden Sun IIRC)
|
2022-10-21 08:12:29 +00:00
|
|
|
const log_file: ?File = if (enable_logging) blk: {
|
|
|
|
const file = try std.fs.cwd().createFile(if (is_binary) "zba.bin" else "zba.log", .{});
|
2022-10-21 08:12:03 +00:00
|
|
|
cpu.useLogger(&file, is_binary);
|
2022-10-21 08:12:29 +00:00
|
|
|
break :blk file;
|
|
|
|
} else null;
|
2022-10-21 08:12:04 +00:00
|
|
|
defer if (log_file) |file| file.close();
|
2022-10-21 08:12:03 +00:00
|
|
|
|
|
|
|
var quit = Atomic(bool).init(false);
|
2022-10-21 08:12:44 +00:00
|
|
|
var emu_rate = FpsTracker.init();
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
// Run Emulator in it's separate thread
|
|
|
|
// From this point on, interacting with Arm7tdmi or Scheduler
|
|
|
|
// be justified, as it will require to be thread-afe
|
2022-10-21 08:12:42 +00:00
|
|
|
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &emu_rate, &scheduler, &cpu });
|
2022-10-21 08:12:03 +00:00
|
|
|
defer emu_thread.join();
|
|
|
|
|
2022-10-21 08:12:26 +00:00
|
|
|
var title_buf: [0x20]u8 = std.mem.zeroes([0x20]u8);
|
2022-10-21 08:12:44 +00:00
|
|
|
const window_title = try std.fmt.bufPrint(&title_buf, "ZBA | {s}", .{asString(title)});
|
2022-10-21 08:12:13 +00:00
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
const window = createWindow(window_title, gba_width, gba_height);
|
2022-10-21 08:11:51 +00:00
|
|
|
defer SDL.SDL_DestroyWindow(window);
|
2022-10-21 08:11:50 +00:00
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
const renderer = createRenderer(window);
|
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:12:35 +00:00
|
|
|
const texture = createTexture(renderer, gba_width, gba_height);
|
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:26 +00:00
|
|
|
var dyn_title_buf: [0x100]u8 = [_]u8{0x00} ** 0x100;
|
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;
|
2022-10-21 08:12:29 +00:00
|
|
|
while (SDL.SDL_PollEvent(&event) != 0) {
|
2022-10-21 08:12:08 +00:00
|
|
|
switch (event.type) {
|
|
|
|
SDL.SDL_QUIT => break :emu_loop,
|
|
|
|
SDL.SDL_KEYDOWN => {
|
2022-10-21 08:12:35 +00:00
|
|
|
const io = &cpu.bus.io;
|
2022-10-21 08:12:08 +00:00
|
|
|
const key_code = event.key.keysym.sym;
|
|
|
|
|
|
|
|
switch (key_code) {
|
2022-10-21 08:12:35 +00:00
|
|
|
SDL.SDLK_UP => io.keyinput.up.unset(),
|
|
|
|
SDL.SDLK_DOWN => io.keyinput.down.unset(),
|
|
|
|
SDL.SDLK_LEFT => io.keyinput.left.unset(),
|
|
|
|
SDL.SDLK_RIGHT => io.keyinput.right.unset(),
|
|
|
|
SDL.SDLK_x => io.keyinput.a.unset(),
|
|
|
|
SDL.SDLK_z => io.keyinput.b.unset(),
|
|
|
|
SDL.SDLK_a => io.keyinput.shoulder_l.unset(),
|
|
|
|
SDL.SDLK_s => io.keyinput.shoulder_r.unset(),
|
|
|
|
SDL.SDLK_RETURN => io.keyinput.start.unset(),
|
|
|
|
SDL.SDLK_RSHIFT => io.keyinput.select.unset(),
|
2022-10-21 08:12:08 +00:00
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
SDL.SDL_KEYUP => {
|
2022-10-21 08:12:35 +00:00
|
|
|
const io = &cpu.bus.io;
|
2022-10-21 08:12:08 +00:00
|
|
|
const key_code = event.key.keysym.sym;
|
|
|
|
|
|
|
|
switch (key_code) {
|
2022-10-21 08:12:35 +00:00
|
|
|
SDL.SDLK_UP => io.keyinput.up.set(),
|
|
|
|
SDL.SDLK_DOWN => io.keyinput.down.set(),
|
|
|
|
SDL.SDLK_LEFT => io.keyinput.left.set(),
|
|
|
|
SDL.SDLK_RIGHT => io.keyinput.right.set(),
|
|
|
|
SDL.SDLK_x => io.keyinput.a.set(),
|
|
|
|
SDL.SDLK_z => io.keyinput.b.set(),
|
|
|
|
SDL.SDLK_a => io.keyinput.shoulder_l.set(),
|
|
|
|
SDL.SDLK_s => io.keyinput.shoulder_r.set(),
|
|
|
|
SDL.SDLK_RETURN => io.keyinput.start.set(),
|
|
|
|
SDL.SDLK_RSHIFT => io.keyinput.select.set(),
|
2022-10-21 08:12:45 +00:00
|
|
|
SDL.SDLK_i => log.err("Sample Count: {}", .{@intCast(u32, SDL.SDL_AudioStreamAvailable(cpu.bus.apu.stream)) / (2 * @sizeOf(u16))}),
|
|
|
|
SDL.SDLK_j => log.err("Scheduler Capacity: {} | Scheduler Event Count: {}", .{ scheduler.queue.capacity(), scheduler.queue.count() }),
|
2022-10-21 08:12:08 +00:00
|
|
|
else => {},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
else => {},
|
|
|
|
}
|
2022-10-21 08:11:50 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:42 +00:00
|
|
|
// Emulator has an internal Double Buffer
|
2022-10-21 08:12:42 +00:00
|
|
|
const buf_ptr = cpu.bus.ppu.framebuf.get(.Renderer).ptr;
|
2022-10-21 08:12:15 +00:00
|
|
|
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, framebuf_pitch);
|
2022-10-21 08:11:50 +00:00
|
|
|
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
|
|
|
|
SDL.SDL_RenderPresent(renderer);
|
2022-10-21 08:12:08 +00:00
|
|
|
|
2022-10-21 08:12:42 +00:00
|
|
|
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {}fps] ", .{ window_title, emu_rate.value() }) catch unreachable;
|
2022-10-21 08:12:27 +00:00
|
|
|
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
2022-10-21 08:12:03 +00:00
|
|
|
|
2022-10-21 08:12:42 +00:00
|
|
|
quit.store(true, .SeqCst); // Terminate Emulator Thread
|
2021-12-29 21:09:00 +00:00
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:13 +00:00
|
|
|
const CliError = error{
|
|
|
|
InsufficientOptions,
|
|
|
|
UnneededOptions,
|
|
|
|
};
|
2022-10-21 08:12:26 +00:00
|
|
|
|
2022-10-21 08:12:35 +00:00
|
|
|
fn sdlPanic() noreturn {
|
|
|
|
const str = @as(?[*:0]const u8, SDL.SDL_GetError()) orelse "unknown error";
|
|
|
|
@panic(std.mem.sliceTo(str, 0));
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:44 +00:00
|
|
|
fn initSdl2() void {
|
2022-10-21 08:12:35 +00:00
|
|
|
const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO | SDL.SDL_INIT_GAMECONTROLLER);
|
|
|
|
if (status < 0) sdlPanic();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn createWindow(title: []u8, width: c_int, height: c_int) *SDL.SDL_Window {
|
|
|
|
return SDL.SDL_CreateWindow(
|
|
|
|
title.ptr,
|
|
|
|
SDL.SDL_WINDOWPOS_CENTERED,
|
|
|
|
SDL.SDL_WINDOWPOS_CENTERED,
|
|
|
|
width * window_scale,
|
|
|
|
height * window_scale,
|
|
|
|
SDL.SDL_WINDOW_SHOWN,
|
|
|
|
) orelse sdlPanic();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn createRenderer(window: *SDL.SDL_Window) *SDL.SDL_Renderer {
|
|
|
|
return SDL.SDL_CreateRenderer(window, -1, SDL.SDL_RENDERER_ACCELERATED | SDL.SDL_RENDERER_PRESENTVSYNC) orelse sdlPanic();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn createTexture(renderer: *SDL.SDL_Renderer, width: c_int, height: c_int) *SDL.SDL_Texture {
|
|
|
|
return SDL.SDL_CreateTexture(
|
|
|
|
renderer,
|
|
|
|
SDL.SDL_PIXELFORMAT_RGBA8888,
|
|
|
|
SDL.SDL_TEXTUREACCESS_STREAMING,
|
|
|
|
width,
|
|
|
|
height,
|
|
|
|
) orelse sdlPanic();
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:12:36 +00:00
|
|
|
fn initAudio(apu: *Apu) SDL.SDL_AudioDeviceID {
|
2022-10-21 08:12:35 +00:00
|
|
|
var have: SDL.SDL_AudioSpec = undefined;
|
2022-10-21 08:12:36 +00:00
|
|
|
var want: SDL.SDL_AudioSpec = .{
|
|
|
|
.freq = sample_rate,
|
2022-10-21 08:12:44 +00:00
|
|
|
.format = SDL.AUDIO_U16,
|
2022-10-21 08:12:36 +00:00
|
|
|
.channels = 2,
|
|
|
|
.samples = 0x100,
|
|
|
|
.callback = audioCallback,
|
|
|
|
.userdata = apu,
|
|
|
|
.silence = undefined,
|
|
|
|
.size = undefined,
|
|
|
|
.padding = undefined,
|
|
|
|
};
|
2022-10-21 08:12:35 +00:00
|
|
|
|
|
|
|
const dev = SDL.SDL_OpenAudioDevice(null, 0, &want, &have, 0);
|
|
|
|
if (dev == 0) sdlPanic();
|
|
|
|
|
2022-10-21 08:12:36 +00:00
|
|
|
// Start Playback on the Audio device
|
2022-10-21 08:12:35 +00:00
|
|
|
SDL.SDL_PauseAudioDevice(dev, 0);
|
|
|
|
return dev;
|
|
|
|
}
|
2022-10-21 08:12:36 +00:00
|
|
|
|
|
|
|
export fn audioCallback(userdata: ?*anyopaque, stream: [*c]u8, len: c_int) void {
|
|
|
|
const apu = @ptrCast(*Apu, @alignCast(8, userdata));
|
2022-10-21 08:12:45 +00:00
|
|
|
const written = SDL.SDL_AudioStreamGet(apu.stream, stream, len);
|
|
|
|
|
|
|
|
// If we don't write anything, play silence otherwise garbage will be played
|
|
|
|
// FIXME: I don't think this hack to remove DC Offset is acceptable :thinking:
|
|
|
|
if (written == 0) std.mem.set(u8, stream[0..@intCast(usize, len)], 0x80);
|
2022-10-21 08:12:36 +00:00
|
|
|
}
|
2022-10-21 08:12:44 +00:00
|
|
|
|
|
|
|
fn getSavePath(alloc: std.mem.Allocator) !?[]const u8 {
|
|
|
|
const save_subpath = "zba" ++ [_]u8{std.fs.path.sep} ++ "save";
|
|
|
|
|
|
|
|
const maybe_data_path = try known_folders.getPath(alloc, .data);
|
|
|
|
defer if (maybe_data_path) |path| alloc.free(path);
|
|
|
|
|
|
|
|
const save_path = if (maybe_data_path) |base| try std.fs.path.join(alloc, &[_][]const u8{ base, "zba", "save" }) else null;
|
|
|
|
|
|
|
|
if (save_path) |_| {
|
|
|
|
// If we've determined what our save path should be, ensure the prereq directories
|
|
|
|
// are present so that we can successfully write to the path when necessary
|
|
|
|
const maybe_data_dir = try known_folders.open(alloc, .data, .{});
|
|
|
|
if (maybe_data_dir) |data_dir| try data_dir.makePath(save_subpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
return save_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getRomPath(res: clap.Result(clap.Help, ¶ms, clap.parsers.default), stderr: std.fs.File) ![]const u8 {
|
|
|
|
return switch (res.positionals.len) {
|
|
|
|
1 => res.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;
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|