feat: create emulator thread
This commit is contained in:
parent
8e7766e694
commit
de1c84914c
|
@ -29,7 +29,7 @@ pub const ThumbInstrFn = fn (*Arm7tdmi, *Bus, u16) void;
|
||||||
const arm_lut: [0x1000]ArmInstrFn = armPopulate();
|
const arm_lut: [0x1000]ArmInstrFn = armPopulate();
|
||||||
const thumb_lut: [0x400]ThumbInstrFn = thumbPopulate();
|
const thumb_lut: [0x400]ThumbInstrFn = thumbPopulate();
|
||||||
|
|
||||||
const logging_enabled: bool = false;
|
const enable_logging = @import("main.zig").enable_logging;
|
||||||
|
|
||||||
pub const Arm7tdmi = struct {
|
pub const Arm7tdmi = struct {
|
||||||
const Self = @This();
|
const Self = @This();
|
||||||
|
@ -183,12 +183,12 @@ pub const Arm7tdmi = struct {
|
||||||
pub fn step(self: *Self) u64 {
|
pub fn step(self: *Self) u64 {
|
||||||
if (self.cpsr.t.read()) {
|
if (self.cpsr.t.read()) {
|
||||||
const opcode = self.thumbFetch();
|
const opcode = self.thumbFetch();
|
||||||
if (logging_enabled) if (self.log_file) |file| self.log(file, @as(u32, opcode));
|
if (enable_logging) if (self.log_file) |file| self.log(file, @as(u32, opcode));
|
||||||
|
|
||||||
thumb_lut[thumbIdx(opcode)](self, self.bus, opcode);
|
thumb_lut[thumbIdx(opcode)](self, self.bus, opcode);
|
||||||
} else {
|
} else {
|
||||||
const opcode = self.fetch();
|
const opcode = self.fetch();
|
||||||
if (logging_enabled) if (self.log_file) |file| self.log(file, @as(u32, opcode));
|
if (enable_logging) if (self.log_file) |file| self.log(file, opcode);
|
||||||
|
|
||||||
if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) {
|
if (checkCond(self.cpsr, @truncate(u4, opcode >> 28))) {
|
||||||
arm_lut[armIdx(opcode)](self, self.bus, opcode);
|
arm_lut[armIdx(opcode)](self, self.bus, opcode);
|
||||||
|
|
|
@ -1,7 +1,10 @@
|
||||||
|
const std = @import("std");
|
||||||
|
|
||||||
const Bus = @import("Bus.zig");
|
const Bus = @import("Bus.zig");
|
||||||
const Scheduler = @import("scheduler.zig").Scheduler;
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
||||||
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
||||||
|
|
||||||
|
const Atomic = std.atomic.Atomic;
|
||||||
const cycles_per_frame: u64 = 160 * (308 * 4);
|
const cycles_per_frame: u64 = 160 * (308 * 4);
|
||||||
|
|
||||||
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||||
|
@ -15,3 +18,9 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn runEmuThread(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||||
|
while (!quit.load(.Unordered)) {
|
||||||
|
runFrame(sched, cpu, bus);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
34
src/main.zig
34
src/main.zig
|
@ -7,14 +7,18 @@ const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
|
||||||
const Scheduler = @import("scheduler.zig").Scheduler;
|
const Scheduler = @import("scheduler.zig").Scheduler;
|
||||||
|
|
||||||
const Timer = std.time.Timer;
|
const Timer = std.time.Timer;
|
||||||
|
const Thread = std.Thread;
|
||||||
|
const Atomic = std.atomic.Atomic;
|
||||||
|
|
||||||
const window_scale = 3;
|
const window_scale = 3;
|
||||||
const gba_width = @import("ppu.zig").width;
|
const gba_width = @import("ppu.zig").width;
|
||||||
const gba_height = @import("ppu.zig").height;
|
const gba_height = @import("ppu.zig").height;
|
||||||
const buf_pitch = @import("ppu.zig").buf_pitch;
|
const buf_pitch = @import("ppu.zig").buf_pitch;
|
||||||
|
|
||||||
|
pub const enable_logging: bool = false;
|
||||||
|
|
||||||
pub fn main() anyerror!void {
|
pub fn main() anyerror!void {
|
||||||
// Allocator for Emulator + CLI Aruments
|
// Allocator for Emulator + CLI
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const alloc = gpa.allocator();
|
const alloc = gpa.allocator();
|
||||||
defer std.debug.assert(!gpa.deinit());
|
defer std.debug.assert(!gpa.deinit());
|
||||||
|
@ -40,13 +44,25 @@ pub fn main() anyerror!void {
|
||||||
var bus = try Bus.init(alloc, &scheduler, zba_args[0]);
|
var bus = try Bus.init(alloc, &scheduler, zba_args[0]);
|
||||||
defer bus.deinit();
|
defer bus.deinit();
|
||||||
|
|
||||||
// Support for Sky's Logs
|
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
||||||
const file = try std.fs.cwd().createFile("zba.bin", .{ .read = true });
|
cpu.fastBoot();
|
||||||
|
|
||||||
|
if (enable_logging) {
|
||||||
|
const is_binary: bool = true;
|
||||||
|
const file_name = if (is_binary) "zba.bin" else "zba.log";
|
||||||
|
|
||||||
|
const file = try std.fs.cwd().createFile(file_name, .{ .read = true });
|
||||||
defer file.close();
|
defer file.close();
|
||||||
|
|
||||||
var cpu = Arm7tdmi.init(&scheduler, &bus);
|
cpu.useLogger(&file, is_binary);
|
||||||
cpu.useLogger(&file, true);
|
}
|
||||||
cpu.fastBoot();
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
// Initialize SDL
|
// Initialize SDL
|
||||||
const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO);
|
const status = SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO);
|
||||||
|
@ -74,8 +90,6 @@ pub fn main() anyerror!void {
|
||||||
var title_buf: [0x30]u8 = [_]u8{0x00} ** 0x30;
|
var title_buf: [0x30]u8 = [_]u8{0x00} ** 0x30;
|
||||||
|
|
||||||
emu_loop: while (true) {
|
emu_loop: while (true) {
|
||||||
emu.runFrame(&scheduler, &cpu, &bus);
|
|
||||||
|
|
||||||
var event: SDL.SDL_Event = undefined;
|
var event: SDL.SDL_Event = undefined;
|
||||||
_ = SDL.SDL_PollEvent(&event);
|
_ = SDL.SDL_PollEvent(&event);
|
||||||
|
|
||||||
|
@ -84,7 +98,9 @@ pub fn main() anyerror!void {
|
||||||
else => {},
|
else => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Make this Thread Safe
|
||||||
const buf_ptr = bus.ppu.frame_buf.ptr;
|
const buf_ptr = bus.ppu.frame_buf.ptr;
|
||||||
|
|
||||||
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, buf_pitch);
|
_ = SDL.SDL_UpdateTexture(texture, null, buf_ptr, buf_pitch);
|
||||||
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
|
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
|
||||||
SDL.SDL_RenderPresent(renderer);
|
SDL.SDL_RenderPresent(renderer);
|
||||||
|
@ -93,6 +109,8 @@ pub fn main() anyerror!void {
|
||||||
const title = std.fmt.bufPrint(&title_buf, "Gameboy Advance Emulator FPS: {d}", .{fps}) catch unreachable;
|
const title = std.fmt.bufPrint(&title_buf, "Gameboy Advance Emulator FPS: {d}", .{fps}) catch unreachable;
|
||||||
SDL.SDL_SetWindowTitle(window, title.ptr);
|
SDL.SDL_SetWindowTitle(window, title.ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
quit.store(true, .Unordered); // Terminate Emulator Thread
|
||||||
}
|
}
|
||||||
|
|
||||||
fn sdlPanic() noreturn {
|
fn sdlPanic() noreturn {
|
||||||
|
|
Loading…
Reference in New Issue