Compare commits

..

No commits in common. "39ab363afafd1dba7ee9f7666ae03526694e4a75" and "40968f0990257a5b55c223d3d5c613e6a3a66d2f" have entirely different histories.

9 changed files with 53 additions and 69 deletions

View File

@ -5,7 +5,6 @@ const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
const sext = @import("../../util.zig").sext;
const rotr = @import("../../util.zig").rotr;
pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I: bool, comptime W: bool, comptime L: bool) InstrFn {
return struct {
@ -39,7 +38,7 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
0b01 => {
// LDRH
const value = bus.read16(address & 0xFFFF_FFFE);
result = rotr(u32, value, 8 * (address & 1));
result = std.math.rotr(u32, value, 8 * (address & 1));
},
0b10 => {
// LDRSB
@ -53,7 +52,7 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
break :blk sext(16, bus.read16(address));
};
result = rotr(u32, value, 8 * (address & 1));
result = std.math.rotr(u32, value, 8 * (address & 1));
},
0b00 => unreachable, // SWP
}

View File

@ -7,8 +7,6 @@ const PSR = @import("../../cpu.zig").PSR;
const log = std.log.scoped(.PsrTransfer);
const rotr = @import("../../util.zig").rotr;
pub fn psrTransfer(comptime I: bool, comptime R: bool, comptime kind: u2) InstrFn {
return struct {
fn inner(cpu: *Arm7tdmi, _: *Bus, opcode: u32) void {
@ -24,7 +22,7 @@ pub fn psrTransfer(comptime I: bool, comptime R: bool, comptime kind: u2) InstrF
// MSR
const field_mask = @truncate(u4, opcode >> 16 & 0xF);
const rm_idx = opcode & 0xF;
const right = if (I) rotr(u32, opcode & 0xFF, (opcode >> 8 & 0xF) << 1) else cpu.r[rm_idx];
const right = if (I) std.math.rotr(u32, opcode & 0xFF, (opcode >> 8 & 0xF) << 1) else cpu.r[rm_idx];
if (R and !cpu.hasSPSR()) log.warn("Tried to write to SPSR in User/System Mode", .{});

View File

@ -4,8 +4,6 @@ const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
const rotr = @import("../../util.zig").rotr;
pub fn singleDataSwap(comptime B: bool) InstrFn {
return struct {
fn inner(cpu: *Arm7tdmi, bus: *Bus, opcode: u32) void {
@ -22,7 +20,7 @@ pub fn singleDataSwap(comptime B: bool) InstrFn {
cpu.r[rd] = value;
} else {
// SWP
const value = rotr(u32, bus.read32(address & 0xFFFF_FFFC), 8 * (address & 0x3));
const value = std.math.rotr(u32, bus.read32(address & 0xFFFF_FFFC), 8 * (address & 0x3));
bus.write32(address & 0xFFFF_FFFC, cpu.r[rm]);
cpu.r[rd] = value;
}

View File

@ -6,8 +6,6 @@ const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ArmInstrFn;
const rotr = @import("../../util.zig").rotr;
pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool, comptime B: bool, comptime W: bool, comptime L: bool) InstrFn {
return struct {
fn inner(cpu: *Arm7tdmi, bus: *Bus, opcode: u32) void {
@ -35,7 +33,7 @@ pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool,
} else {
// LDR
const value = bus.read32(address & 0xFFFF_FFFC);
result = rotr(u32, value, 8 * (address & 0x3));
result = std.math.rotr(u32, value, 8 * (address & 0x3));
}
} else {
if (B) {

View File

@ -3,8 +3,6 @@ const std = @import("std");
const Arm7tdmi = @import("../cpu.zig").Arm7tdmi;
const CPSR = @import("../cpu.zig").PSR;
const rotr = @import("../util.zig").rotr;
pub fn execute(comptime S: bool, cpu: *Arm7tdmi, opcode: u32) u32 {
var result: u32 = undefined;
if (opcode >> 4 & 1 == 1) {
@ -143,7 +141,7 @@ pub fn arithmeticRight(comptime S: bool, cpsr: *CPSR, rm: u32, total_amount: u8)
}
pub fn rotateRight(comptime S: bool, cpsr: *CPSR, rm: u32, total_amount: u8) u32 {
const result = rotr(u32, rm, total_amount);
const result = std.math.rotr(u32, rm, total_amount);
if (S and total_amount != 0) {
cpsr.c.write(result >> 31 & 1 == 1);

View File

@ -4,8 +4,6 @@ const Bus = @import("../../Bus.zig");
const Arm7tdmi = @import("../../cpu.zig").Arm7tdmi;
const InstrFn = @import("../../cpu.zig").ThumbInstrFn;
const rotr = @import("../../util.zig").rotr;
pub fn format6(comptime rd: u3) InstrFn {
return struct {
fn inner(cpu: *Arm7tdmi, bus: *Bus, opcode: u16) void {
@ -41,7 +39,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
0b10 => {
// LDRH
const value = bus.read16(address & 0xFFFF_FFFE);
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));
},
0b11 => {
// LDRSH
@ -51,7 +49,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
break :blk sext(16, bus.read16(address));
};
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));
},
}
} else {
@ -68,7 +66,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
0b10 => {
// LDR
const value = bus.read32(address & 0xFFFF_FFFC);
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 0x3));
},
0b11 => {
// LDRB
@ -95,7 +93,7 @@ pub fn format9(comptime B: bool, comptime L: bool, comptime offset: u5) InstrFn
// LDR
const address = cpu.r[rb] + (@as(u32, offset) << 2);
const value = bus.read32(address & 0xFFFF_FFFC);
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 0x3));
}
} else {
if (B) {
@ -123,7 +121,7 @@ pub fn format10(comptime L: bool, comptime offset: u5) InstrFn {
if (L) {
// LDRH
const value = bus.read16(address & 0xFFFF_FFFE);
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));
} else {
// STRH
bus.write16(address & 0xFFFF_FFFE, @truncate(u16, cpu.r[rd]));
@ -141,7 +139,7 @@ pub fn format11(comptime L: bool, comptime rd: u3) InstrFn {
if (L) {
// LDR
const value = bus.read32(address & 0xFFFF_FFFC);
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 0x3));
} else {
// STR
bus.write32(address & 0xFFFF_FFFC, cpu.r[rd]);

View File

@ -3,7 +3,6 @@ const std = @import("std");
const Bus = @import("Bus.zig");
const Scheduler = @import("scheduler.zig").Scheduler;
const Arm7tdmi = @import("cpu.zig").Arm7tdmi;
const FpsAverage = @import("util.zig").FpsAverage;
const Timer = std.time.Timer;
const Thread = std.Thread;
@ -25,13 +24,13 @@ const RunKind = enum {
LimitedBusy,
};
pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn run(kind: RunKind, quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
switch (kind) {
.Unlimited => runUnSync(quit, sched, cpu, bus),
.Limited => runSync(quit, sched, cpu, bus),
.UnlimitedFPS => runUnSyncFps(quit, fps, sched, cpu, bus),
.LimitedFPS => runSyncFps(quit, fps, sched, cpu, bus),
.LimitedBusy => runBusyLoop(quit, sched, cpu, bus),
.Unlimited => runUnSync(quit, pause, sched, cpu, bus),
.Limited => runSync(quit, pause, sched, cpu, bus),
.UnlimitedFPS => runUnSyncFps(quit, pause, fps, sched, cpu, bus),
.LimitedFPS => runSyncFps(quit, pause, fps, sched, cpu, bus),
.LimitedBusy => runBusyLoop(quit, pause, sched, cpu, bus),
}
}
@ -47,17 +46,17 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
}
}
pub fn runUnSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runUnSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Unsynchronized EmuThread has begun", .{});
while (!quit.load(.Unordered)) runFrame(sched, cpu, bus);
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) runFrame(sched, cpu, bus);
}
pub fn runSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Synchronized EmuThread has begun", .{});
var timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period;
while (!quit.load(.Unordered)) {
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
// Put the Thread to Sleep + Backup Spin Loop
@ -66,26 +65,26 @@ pub fn runSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus
// Update to the new wake time
wake_time += frame_period;
}
};
}
pub fn runUnSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runUnSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Unsynchronized EmuThread with FPS Tracking has begun", .{});
var fps_timer = Timer.start() catch unreachable;
while (!quit.load(.Unordered)) {
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
fps.add(emuFps(fps_timer.lap()));
}
fps.store(emuFps(fps_timer.lap()), .Unordered);
};
}
pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Synchronized EmuThread has begun", .{});
var timer = Timer.start() catch unreachable;
var fps_timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period;
while (!quit.load(.Unordered)) {
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
// Put the Thread to Sleep + Backup Spin Loop
@ -93,29 +92,29 @@ pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu:
sleep(&timer, &wake_time);
// Determine FPS
fps.add(emuFps(fps_timer.lap()));
fps.store(emuFps(fps_timer.lap()), .Unordered);
// Update to the new wake time
wake_time += frame_period;
}
};
}
pub fn runBusyLoop(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
pub fn runBusyLoop(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
log.info("Run EmuThread with spin-loop sync", .{});
var timer = Timer.start() catch unreachable;
var wake_time: u64 = frame_period;
while (!quit.load(.Unordered)) {
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) {
runFrame(sched, cpu, bus);
spinLoop(&timer, wake_time);
// Update to the new wake time
wake_time += frame_period;
}
};
}
fn sleep(timer: *Timer, wake_time: *u64) void {
// const step = std.time.ns_per_ms * 10; // 10ms
const step = std.time.ns_per_ms * 10;
const timestamp = timer.read();
// ns_late is non zero if we are late.
@ -131,15 +130,13 @@ fn sleep(timer: *Timer, wake_time: *u64) void {
const sleep_for = frame_period - ns_late;
// // Employ several sleep calls in periods of 10ms
// // By doing this the behaviour should average out to be
// // more consistent
// const loop_count = sleep_for / step; // How many groups of 10ms
// Employ several sleep calls in periods of 10ms
// By doing this the behaviour should average out to be
// more consistent
const loop_count = sleep_for / step; // How many groups of 10ms
// var i: usize = 0;
// while (i < loop_count) : (i += 1) std.time.sleep(step);
std.time.sleep(sleep_for);
var i: usize = 0;
while (i < loop_count) : (i += 1) std.time.sleep(step);
// Spin to make up the difference if there is a need
// Make sure that we're using the old wake time and not the onne we recalculated

View File

@ -84,10 +84,11 @@ pub fn main() anyerror!void {
// Init Atomics
var quit = Atomic(bool).init(false);
var emu_fps = FpsAverage.init();
var pause = Atomic(bool).init(false);
var emu_fps = Atomic(u64).init(0);
// Create Emulator Thread
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .UnlimitedFPS, &quit, &emu_fps, &scheduler, &cpu, &bus });
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &pause, &emu_fps, &scheduler, &cpu, &bus });
defer emu_thread.join();
// Initialize SDL
@ -118,6 +119,8 @@ pub fn main() anyerror!void {
// Init FPS Timer
var dyn_title_buf: [0x100]u8 = [_]u8{0x00} ** 0x100;
var fps_avg = FpsAverage.init();
emu_loop: while (true) {
var event: SDL.SDL_Event = undefined;
if (SDL.SDL_PollEvent(&event) != 0) {
@ -164,13 +167,17 @@ pub fn main() anyerror!void {
}
// 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);
_ = SDL.SDL_RenderCopy(renderer, texture, null, null);
SDL.SDL_RenderPresent(renderer);
pause.store(false, .Unordered);
const avg = emu_fps.calc();
const dyn_title = std.fmt.bufPrint(&dyn_title_buf, "{s} [Emu: {d:0>3}fps, {d:0>3}%] ", .{ title, avg, (avg * 100 / 59) }) catch unreachable;
fps_avg.add(emu_fps.load(.Unordered));
const avg = fps_avg.calc();
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);
}

View File

@ -1,21 +1,12 @@
const std = @import("std");
const Log2Int = std.math.Log2Int;
pub inline fn sext(comptime bits: comptime_int, value: u32) u32 {
pub fn sext(comptime bits: comptime_int, value: u32) u32 {
comptime std.debug.assert(bits <= 32);
const amount = 32 - bits;
return @bitCast(u32, @bitCast(i32, value << amount) >> amount);
}
/// See https://godbolt.org/z/W3en9Eche
pub inline fn rotr(comptime T: type, value: T, r: anytype) T {
comptime std.debug.assert(@typeInfo(T).Int.signedness == .unsigned);
const ar = @truncate(Log2Int(T), r);
return value >> ar | value << @truncate(Log2Int(T), @typeInfo(T).Int.bits - @as(T, ar));
}
pub const FpsAverage = struct {
const Self = @This();