Compare commits
2 Commits
40968f0990
...
39ab363afa
Author | SHA1 | Date |
---|---|---|
Rekai Nyangadzayi Musuka | 39ab363afa | |
Rekai Nyangadzayi Musuka | 1921218c7b |
|
@ -5,6 +5,7 @@ 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 {
|
||||
|
@ -38,7 +39,7 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
|
|||
0b01 => {
|
||||
// LDRH
|
||||
const value = bus.read16(address & 0xFFFF_FFFE);
|
||||
result = std.math.rotr(u32, value, 8 * (address & 1));
|
||||
result = rotr(u32, value, 8 * (address & 1));
|
||||
},
|
||||
0b10 => {
|
||||
// LDRSB
|
||||
|
@ -52,7 +53,7 @@ pub fn halfAndSignedDataTransfer(comptime P: bool, comptime U: bool, comptime I:
|
|||
break :blk sext(16, bus.read16(address));
|
||||
};
|
||||
|
||||
result = std.math.rotr(u32, value, 8 * (address & 1));
|
||||
result = rotr(u32, value, 8 * (address & 1));
|
||||
},
|
||||
0b00 => unreachable, // SWP
|
||||
}
|
||||
|
|
|
@ -7,6 +7,8 @@ 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 {
|
||||
|
@ -22,7 +24,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) std.math.rotr(u32, opcode & 0xFF, (opcode >> 8 & 0xF) << 1) else cpu.r[rm_idx];
|
||||
const right = if (I) 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", .{});
|
||||
|
||||
|
|
|
@ -4,6 +4,8 @@ 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 {
|
||||
|
@ -20,7 +22,7 @@ pub fn singleDataSwap(comptime B: bool) InstrFn {
|
|||
cpu.r[rd] = value;
|
||||
} else {
|
||||
// SWP
|
||||
const value = std.math.rotr(u32, bus.read32(address & 0xFFFF_FFFC), 8 * (address & 0x3));
|
||||
const value = rotr(u32, bus.read32(address & 0xFFFF_FFFC), 8 * (address & 0x3));
|
||||
bus.write32(address & 0xFFFF_FFFC, cpu.r[rm]);
|
||||
cpu.r[rd] = value;
|
||||
}
|
||||
|
|
|
@ -6,6 +6,8 @@ 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 {
|
||||
|
@ -33,7 +35,7 @@ pub fn singleDataTransfer(comptime I: bool, comptime P: bool, comptime U: bool,
|
|||
} else {
|
||||
// LDR
|
||||
const value = bus.read32(address & 0xFFFF_FFFC);
|
||||
result = std.math.rotr(u32, value, 8 * (address & 0x3));
|
||||
result = rotr(u32, value, 8 * (address & 0x3));
|
||||
}
|
||||
} else {
|
||||
if (B) {
|
||||
|
|
|
@ -3,6 +3,8 @@ 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) {
|
||||
|
@ -141,7 +143,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 = std.math.rotr(u32, rm, total_amount);
|
||||
const result = rotr(u32, rm, total_amount);
|
||||
|
||||
if (S and total_amount != 0) {
|
||||
cpsr.c.write(result >> 31 & 1 == 1);
|
||||
|
|
|
@ -4,6 +4,8 @@ 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 {
|
||||
|
@ -39,7 +41,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
|
|||
0b10 => {
|
||||
// LDRH
|
||||
const value = bus.read16(address & 0xFFFF_FFFE);
|
||||
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));
|
||||
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
|
||||
},
|
||||
0b11 => {
|
||||
// LDRSH
|
||||
|
@ -49,7 +51,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
|
|||
break :blk sext(16, bus.read16(address));
|
||||
};
|
||||
|
||||
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));
|
||||
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
|
||||
},
|
||||
}
|
||||
} else {
|
||||
|
@ -66,7 +68,7 @@ pub fn format78(comptime op: u2, comptime T: bool) InstrFn {
|
|||
0b10 => {
|
||||
// LDR
|
||||
const value = bus.read32(address & 0xFFFF_FFFC);
|
||||
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 0x3));
|
||||
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
|
||||
},
|
||||
0b11 => {
|
||||
// LDRB
|
||||
|
@ -93,7 +95,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] = std.math.rotr(u32, value, 8 * (address & 0x3));
|
||||
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
|
||||
}
|
||||
} else {
|
||||
if (B) {
|
||||
|
@ -121,7 +123,7 @@ pub fn format10(comptime L: bool, comptime offset: u5) InstrFn {
|
|||
if (L) {
|
||||
// LDRH
|
||||
const value = bus.read16(address & 0xFFFF_FFFE);
|
||||
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 1));
|
||||
cpu.r[rd] = rotr(u32, value, 8 * (address & 1));
|
||||
} else {
|
||||
// STRH
|
||||
bus.write16(address & 0xFFFF_FFFE, @truncate(u16, cpu.r[rd]));
|
||||
|
@ -139,7 +141,7 @@ pub fn format11(comptime L: bool, comptime rd: u3) InstrFn {
|
|||
if (L) {
|
||||
// LDR
|
||||
const value = bus.read32(address & 0xFFFF_FFFC);
|
||||
cpu.r[rd] = std.math.rotr(u32, value, 8 * (address & 0x3));
|
||||
cpu.r[rd] = rotr(u32, value, 8 * (address & 0x3));
|
||||
} else {
|
||||
// STR
|
||||
bus.write32(address & 0xFFFF_FFFC, cpu.r[rd]);
|
||||
|
|
61
src/emu.zig
61
src/emu.zig
|
@ -3,6 +3,7 @@ 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;
|
||||
|
@ -24,13 +25,13 @@ const RunKind = enum {
|
|||
LimitedBusy,
|
||||
};
|
||||
|
||||
pub fn run(kind: RunKind, quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
pub fn run(kind: RunKind, quit: *Atomic(bool), fps: *FpsAverage, sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
switch (kind) {
|
||||
.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),
|
||||
.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),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,17 +47,17 @@ pub fn runFrame(sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn runUnSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
pub fn runUnSync(quit: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
log.info("Unsynchronized EmuThread has begun", .{});
|
||||
while (!quit.load(.Unordered)) if (!pause.load(.Unordered)) runFrame(sched, cpu, bus);
|
||||
while (!quit.load(.Unordered)) runFrame(sched, cpu, bus);
|
||||
}
|
||||
|
||||
pub fn runSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
pub fn runSync(quit: *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)) if (!pause.load(.Unordered)) {
|
||||
while (!quit.load(.Unordered)) {
|
||||
runFrame(sched, cpu, bus);
|
||||
|
||||
// Put the Thread to Sleep + Backup Spin Loop
|
||||
|
@ -65,26 +66,26 @@ pub fn runSync(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu
|
|||
|
||||
// Update to the new wake time
|
||||
wake_time += frame_period;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runUnSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
pub fn runUnSyncFps(quit: *Atomic(bool), fps: *FpsAverage, 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)) if (!pause.load(.Unordered)) {
|
||||
while (!quit.load(.Unordered)) {
|
||||
runFrame(sched, cpu, bus);
|
||||
fps.store(emuFps(fps_timer.lap()), .Unordered);
|
||||
};
|
||||
fps.add(emuFps(fps_timer.lap()));
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
pub fn runSyncFps(quit: *Atomic(bool), fps: *FpsAverage, 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)) if (!pause.load(.Unordered)) {
|
||||
while (!quit.load(.Unordered)) {
|
||||
runFrame(sched, cpu, bus);
|
||||
|
||||
// Put the Thread to Sleep + Backup Spin Loop
|
||||
|
@ -92,29 +93,29 @@ pub fn runSyncFps(quit: *Atomic(bool), pause: *Atomic(bool), fps: *Atomic(u64),
|
|||
sleep(&timer, &wake_time);
|
||||
|
||||
// Determine FPS
|
||||
fps.store(emuFps(fps_timer.lap()), .Unordered);
|
||||
fps.add(emuFps(fps_timer.lap()));
|
||||
|
||||
// Update to the new wake time
|
||||
wake_time += frame_period;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runBusyLoop(quit: *Atomic(bool), pause: *Atomic(bool), sched: *Scheduler, cpu: *Arm7tdmi, bus: *Bus) void {
|
||||
pub fn runBusyLoop(quit: *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)) if (!pause.load(.Unordered)) {
|
||||
while (!quit.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;
|
||||
// const step = std.time.ns_per_ms * 10; // 10ms
|
||||
const timestamp = timer.read();
|
||||
|
||||
// ns_late is non zero if we are late.
|
||||
|
@ -130,13 +131,15 @@ 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);
|
||||
// var i: usize = 0;
|
||||
// while (i < loop_count) : (i += 1) std.time.sleep(step);
|
||||
|
||||
std.time.sleep(sleep_for);
|
||||
|
||||
// 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
|
||||
|
|
15
src/main.zig
15
src/main.zig
|
@ -84,11 +84,10 @@ pub fn main() anyerror!void {
|
|||
|
||||
// Init Atomics
|
||||
var quit = Atomic(bool).init(false);
|
||||
var pause = Atomic(bool).init(false);
|
||||
var emu_fps = Atomic(u64).init(0);
|
||||
var emu_fps = FpsAverage.init();
|
||||
|
||||
// Create Emulator Thread
|
||||
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .LimitedFPS, &quit, &pause, &emu_fps, &scheduler, &cpu, &bus });
|
||||
const emu_thread = try Thread.spawn(.{}, emu.run, .{ .UnlimitedFPS, &quit, &emu_fps, &scheduler, &cpu, &bus });
|
||||
defer emu_thread.join();
|
||||
|
||||
// Initialize SDL
|
||||
|
@ -119,8 +118,6 @@ 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) {
|
||||
|
@ -167,17 +164,13 @@ 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);
|
||||
|
||||
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;
|
||||
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;
|
||||
SDL.SDL_SetWindowTitle(window, dyn_title.ptr);
|
||||
}
|
||||
|
||||
|
|
11
src/util.zig
11
src/util.zig
|
@ -1,12 +1,21 @@
|
|||
const std = @import("std");
|
||||
const Log2Int = std.math.Log2Int;
|
||||
|
||||
pub fn sext(comptime bits: comptime_int, value: u32) u32 {
|
||||
pub inline 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();
|
||||
|
||||
|
|
Loading…
Reference in New Issue