Compare commits

...

7 Commits

21 changed files with 562 additions and 184 deletions

5
.gitignore vendored
View File

@@ -2,4 +2,7 @@ zig-cache/
zig-out/
bin/
doc/
imgui.ini
imgui.ini
.build_config/
**/*.log

3
.gitmodules vendored
View File

@@ -4,6 +4,3 @@
[submodule "lib/zgui"]
path = lib/zgui
url = https://git.musuka.dev/paoda/zgui
[submodule "lib/arm32"]
path = lib/arm32
url = https://git.musuka.dev/paoda/arm32.git

View File

@@ -1,8 +1,7 @@
const std = @import("std");
const Sdk = @import("lib/SDL.zig/Sdk.zig");
const Sdk = @import("lib/SDL.zig/build.zig");
const zgui = @import("lib/zgui/build.zig");
const arm32 = @import("lib/arm32/build.zig");
// Although this function looks imperative, note that its job is to
// declaratively construct a build graph that will be executed by an external
@@ -28,21 +27,23 @@ pub fn build(b: *std.Build) void {
.optimize = optimize,
});
exe.addModule("arm32", arm32.module(b));
exe.addModule("zig-clap", b.dependency("zig-clap", .{}).module("clap"));
exe.root_module.addImport("arm32", b.dependency("arm32", .{}).module("arm32"));
exe.root_module.addImport("gdbstub", b.dependency("zba-gdbstub", .{}).module("gdbstub"));
exe.root_module.addImport("zig-clap", b.dependency("zig-clap", .{}).module("clap"));
exe.addAnonymousModule("bitfield", .{ .source_file = .{ .path = "lib/bitfield.zig" } }); // https://github.com/FlorenceOS/
exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } }); // https://github.com/MasterQ32/zig-opengl
exe.root_module.addAnonymousImport("bitfield", .{ .root_source_file = .{ .path = "lib/bitfield.zig" } }); // https://github.com/FlorenceOS/
exe.root_module.addAnonymousImport("gl", .{ .root_source_file = .{ .path = "lib/gl.zig" } }); // https://github.com/MasterQ32/zig-opengl
// https://github.com/MasterQ32/SDL.zig
const sdk = Sdk.init(b, null);
sdk.link(exe, .dynamic);
exe.addModule("sdl2", sdk.getNativeModule());
sdk.link(exe, .static);
exe.root_module.addImport("sdl2", sdk.getNativeModule());
// https://git.musuka.dev/paoda/zgui
// .shared option should stay in sync with SDL.zig call above where true == .dynamic, and false == .static
const zgui_pkg = zgui.package(b, target, optimize, .{ .options = .{ .backend = .sdl2_opengl3, .shared = true } });
const zgui_pkg = zgui.package(b, target, optimize, .{ .options = .{ .backend = .sdl2_opengl3 } });
zgui_pkg.link(exe);
sdk.link(zgui_pkg.zgui_c_cpp, .static);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default

View File

@@ -1,15 +1,25 @@
.{
.name = "turbo",
.version = "0.1.0",
.paths = .{
"lib/bitfield.zig",
"lib/gl.zig",
"src",
"build.zig",
"build.zig.zon",
},
.dependencies = .{
.@"zig-clap" = .{
.url = "https://github.com/Hejsil/zig-clap/archive/f49b94700e0761b7514abdca0e4f0e7f3f938a93.tar.gz",
.hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2",
.url = "https://github.com/Hejsil/zig-clap/archive/4267b0b60ef6f87cccf3ee6ed481e6d0759180c6.tar.gz",
.hash = "12202fa30d679d821292bcd953458b9e76097a5d16999489125a206db63a53392833",
},
.@"zba-util" = .{
// Necessary to use paoda/arm32 as a git submodule
.url = "https://git.musuka.dev/paoda/zba-util/archive/322c798e384a0d24cc84ffcfa2e4a3ca807798a0.tar.gz",
.hash = "12209ce0e729460b997706e47a53a32f1842672cd120189e612f4871731780a30ed0",
.@"zba-gdbstub" = .{
.url = "https://git.musuka.dev/paoda/zba-gdbstub/archive/7ae72ed5a892d2fc6cc3f5511e2b96134d928b59.tar.gz",
.hash = "1220823e961f369e22b62edc1b4da3742af0a7cb420ae9a52ec33216ff5a8ef270c8",
},
.arm32 = .{
.url = "https://git.musuka.dev/paoda/arm32/archive/6f0e27136072610e6dba97ff8aaf5e2ec86e2c09.tar.gz",
.hash = "122047d0affe12b9e9e9c655a7ba6d51b311f02d688e9f1c9a91394a03103f1c0cd5",
},
},
}

32
dl_sdl2.ps1 Normal file
View File

@@ -0,0 +1,32 @@
$SDL2Version = "2.30.0"
$ArchiveFile = ".\SDL2-devel-mingw.zip"
$Json = @"
{
"x86_64-windows-gnu": {
"include": "SDL2\\include",
"libs": "SDL2\\lib",
"bin": "SDL2\\bin"
}
}
"@
New-Item -Force -ItemType Directory -Path .\.build_config
Set-Location -Path .build_config -PassThru
if (!(Test-Path -PathType Leaf $ArchiveFile)) {
Invoke-WebRequest "https://github.com/libsdl-org/SDL/releases/download/release-$SDL2Version/SDL2-devel-$SDL2Version-mingw.zip" -OutFile $ArchiveFile
}
Expand-Archive $ArchiveFile
if (Test-Path -PathType Leaf .\SDL2) {
Remove-Item -Recurse .\SDL2
}
New-Item -Force -ItemType Directory -Path .\SDL2
Get-ChildItem -Path ".\SDL2-devel-mingw\SDL2-$SDL2Version\x86_64-w64-mingw32" | Move-Item -Destination .\SDL2
New-Item -Force .\sdl.json -Value $Json
Remove-Item -Recurse .\SDL2-devel-mingw
Set-Location -Path .. -PassThru

Submodule lib/arm32 deleted from 580e7baca9

View File

@@ -2,6 +2,7 @@ const std = @import("std");
const Header = @import("cartridge.zig").Header;
const Scheduler = @import("Scheduler.zig");
const Ui = @import("../platform.zig").Ui;
const Allocator = std.mem.Allocator;
@@ -403,3 +404,170 @@ pub fn fastBoot(system: System) void {
cpu.bank.spsr[Bank.spsrIdx(.Supervisor)] = .{ .raw = 0x0000_0000 };
}
}
pub const Sync = struct {
const Atomic = std.atomic.Value;
should_quit: Atomic(bool) = Atomic(bool).init(false),
pub fn init(self: *Sync) void {
self.* = .{};
}
};
pub const debug = struct {
const Interface = @import("gdbstub").Emulator;
const Server = @import("gdbstub").Server;
const AtomicBool = std.atomic.Value(bool);
const log = std.log.scoped(.gdbstub);
const nds7 = struct {
const target: []const u8 =
\\<target version="1.0">
\\ <architecture>armv4t</architecture>
\\ <feature name="org.gnu.gdb.arm.core">
\\ <reg name="r0" bitsize="32" type="uint32"/>
\\ <reg name="r1" bitsize="32" type="uint32"/>
\\ <reg name="r2" bitsize="32" type="uint32"/>
\\ <reg name="r3" bitsize="32" type="uint32"/>
\\ <reg name="r4" bitsize="32" type="uint32"/>
\\ <reg name="r5" bitsize="32" type="uint32"/>
\\ <reg name="r6" bitsize="32" type="uint32"/>
\\ <reg name="r7" bitsize="32" type="uint32"/>
\\ <reg name="r8" bitsize="32" type="uint32"/>
\\ <reg name="r9" bitsize="32" type="uint32"/>
\\ <reg name="r10" bitsize="32" type="uint32"/>
\\ <reg name="r11" bitsize="32" type="uint32"/>
\\ <reg name="r12" bitsize="32" type="uint32"/>
\\ <reg name="sp" bitsize="32" type="data_ptr"/>
\\ <reg name="lr" bitsize="32"/>
\\ <reg name="pc" bitsize="32" type="code_ptr"/>
\\
\\ <reg name="cpsr" bitsize="32" regnum="25"/>
\\ </feature>
\\</target>
;
// Remember that a lot of memory regions are mirrored
const memory_map: []const u8 =
\\ <memory-map version="1.0">
\\ <memory type="rom" start="0x00000000" length="0x00004000"/>
\\ <memory type="ram" start="0x02000000" length="0x01000000"/>
\\ <memory type="ram" start="0x03000000" length="0x00800000"/>
\\ <memory type="ram" start="0x03800000" length="0x00800000"/>
\\ <memory type="ram" start="0x04000000" length="0x00100010"/>
\\ <memory type="ram" start="0x06000000" length="0x01000000"/>
\\ <memory type="rom" start="0x08000000" length="0x02000000"/>
\\ <memory type="rom" start="0x0A000000" length="0x01000000"/>
\\ </memory-map>
;
};
pub fn Wrapper(comptime proc: System.Process) type {
return struct {
system: System,
scheduler: *Scheduler,
tick: u64 = 0,
pub fn init(system: System, scheduler: *Scheduler) @This() {
return .{ .system = system, .scheduler = scheduler };
}
pub fn interface(self: *@This(), allocator: Allocator) Interface {
return Interface.init(allocator, self);
}
pub fn read(self: *const @This(), addr: u32) u8 {
const arm = switch (proc) {
.nds7 => self.system.arm7tdmi,
.nds9 => self.system.arm946es,
};
return arm.dbgRead(u8, addr);
}
pub fn write(self: *@This(), addr: u32, value: u8) void {
const arm = switch (proc) {
.nds7 => self.system.arm7tdmi,
.nds9 => self.system.arm946es,
};
return arm.dbgWrite(u8, addr, value);
}
pub fn registers(self: *const @This()) *[16]u32 {
const arm = switch (proc) {
.nds7 => self.system.arm7tdmi,
.nds9 => self.system.arm946es,
};
return &arm.r;
}
pub fn cpsr(self: *const @This()) u32 {
const arm = switch (proc) {
.nds7 => self.system.arm7tdmi,
.nds9 => self.system.arm946es,
};
return arm.cpsr.raw;
}
pub fn step(self: *@This()) void {
const scheduler = self.scheduler;
const system = self.system;
var did_step: bool = false;
// TODO: keep in lockstep with runFrame
while (true) {
if (did_step) break;
switch (isHalted(system)) {
.both => scheduler.tick = scheduler.peekTimestamp(),
inline else => |halt| {
if (!dma9.step(system.arm946es) and comptime halt != .arm9) {
system.arm946es.step();
switch (proc) {
.nds9 => did_step = true,
.nds7 => system.arm946es.step(),
}
}
if (!dma7.step(system.arm7tdmi) and comptime halt != .arm7) {
if (proc == .nds7 or self.tick % 2 == 0) system.arm7tdmi.step();
if (proc == .nds7) {
did_step = true;
self.tick += 1;
}
}
},
}
if (scheduler.check()) |ev| {
const late = scheduler.tick - ev.tick;
scheduler.handle(system, ev, late);
}
}
}
};
}
pub fn run(allocator: Allocator, ui: *Ui, scheduler: *Scheduler, system: System, sync: *Sync) !void {
var wrapper = Wrapper(.nds9).init(system, scheduler);
var emu_interface = wrapper.interface(allocator);
defer emu_interface.deinit();
var server = try Server.init(emu_interface, .{ .target = nds7.target, .memory_map = nds7.memory_map });
defer server.deinit(allocator);
const thread = try std.Thread.spawn(.{}, Server.run, .{ &server, allocator, &sync.should_quit });
defer thread.join();
try ui.debug_run(scheduler, system, sync);
}
};

View File

@@ -14,7 +14,7 @@ pub const Io = struct {
wramcnt: WramCnt = .{ .raw = 0x00 },
// Read Only
keyinput: AtomicKeyInput = .{},
input: Input = .{},
};
fn warn(comptime format: []const u8, args: anytype) u0 {
@@ -413,24 +413,54 @@ pub const KeyInput = extern union {
raw: u16,
};
const AtomicKeyInput = struct {
const Self = @This();
const Ordering = std.atomic.Ordering;
pub const ExtKeyIn = extern union {
x: Bit(u16, 0),
y: Bit(u16, 1),
debug: Bit(u16, 3),
stylus: Bit(u16, 6),
hinge: Bit(u16, 7),
raw: u16,
};
inner: KeyInput = .{ .raw = 0x03FF },
const Input = struct {
const AtomicOrder = std.builtin.AtomicOrder;
const AtomicRmwOp = std.builtin.AtomicRmwOp;
pub inline fn load(self: *const Self, comptime ordering: Ordering) u16 {
return switch (ordering) {
.AcqRel, .Release => @compileError("not supported for atomic loads"),
else => @atomicLoad(u16, &self.inner.raw, ordering),
inner: u32 = 0x007F_03FF,
pub inline fn keyinput(self: *const Input) KeyInput {
const value = @atomicLoad(u32, &self.inner, .Monotonic);
return .{ .raw = @truncate(value) };
}
pub inline fn set_keyinput(self: *Input, comptime op: AtomicRmwOp, input: KeyInput) void {
const msked = switch (op) {
.And => 0xFFFF_FFFF & @as(u32, input.raw),
.Or => 0x0000_0000 | @as(u32, input.raw),
else => @compileError("not supported"),
};
_ = @atomicRmw(u32, &self.inner, op, msked, .Monotonic);
}
pub inline fn fetchOr(self: *Self, value: u16, comptime ordering: Ordering) void {
_ = @atomicRmw(u16, &self.inner.raw, .Or, value, ordering);
pub inline fn extkeyin(self: *const Input) ExtKeyIn {
const value = @atomicLoad(u32, &self.inner, .Monotonic);
const shifted: u16 = @truncate(value >> 16);
return .{ .raw = shifted | 0b00110100 }; // bits 2, 4, 5 are always set
}
pub inline fn fetchAnd(self: *Self, value: u16, comptime ordering: Ordering) void {
_ = @atomicRmw(u16, &self.inner.raw, .And, value, ordering);
pub inline fn set_extkeyin(self: *Input, comptime op: AtomicRmwOp, input: ExtKeyIn) void {
const msked = switch (op) {
.And => 0xFFFF_FFFF & (@as(u32, ~input.raw) << 16),
.Or => 0x0000_0000 | (@as(u32, input.raw) << 16),
else => @compileError("not supported"),
};
_ = @atomicRmw(u32, &self.inner, op, msked, .Monotonic);
}
pub inline fn set(self: *Input, comptime op: AtomicRmwOp, value: u32) void {
_ = @atomicRmw(u32, &self.inner, op, value, .Monotonic);
}
};

View File

@@ -29,7 +29,6 @@ pub fn deinit(self: @This(), allocator: Allocator) void {
// Note: Parts of 16MiB addrspace that aren't mapped to BIOS are typically undefined
pub fn read(self: *const @This(), comptime T: type, address: u32) T {
const readInt = std.mem.readIntLittle;
const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
// if (address >= len) return 0x0000_0000; // TODO: What is undefined actually?
@@ -39,7 +38,7 @@ pub fn read(self: *const @This(), comptime T: type, address: u32) T {
@panic("TODO: ability to load in NDS7 BIOS just-in-time");
};
return readInt(T, ptr[address & (len - 1) ..][0..byte_count]);
return std.mem.readInt(T, ptr[address & (len - 1) ..][0..byte_count], .little);
}
pub fn write(_: *const @This(), comptime T: type, address: u32, value: T) void {

View File

@@ -62,8 +62,6 @@ pub fn dbgRead(self: *@This(), comptime T: type, address: u32) T {
fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T {
const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
const readInt = std.mem.readIntLittle;
const aligned_addr = forceAlign(T, address);
switch (mode) {
@@ -73,13 +71,13 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T
}
return switch (aligned_addr) {
0x0000_0000...0x01FF_FFFF => self.bios.read(T, address),
0x0200_0000...0x02FF_FFFF => readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count]),
0x0000_0000...0x01FF_FFFF => self.bios.read(T, aligned_addr),
0x0200_0000...0x02FF_FFFF => std.mem.readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], .little),
0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) {
0b00 => readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count]),
0b00 => std.mem.readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], .little),
else => self.shr_wram.read(T, .nds7, aligned_addr),
},
0x0380_0000...0x03FF_FFFF => readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count]),
0x0380_0000...0x03FF_FFFF => std.mem.readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], .little),
0x0400_0000...0x04FF_FFFF => io.read(self, T, aligned_addr),
0x0600_0000...0x06FF_FFFF => self.vram.read(T, .nds7, aligned_addr),
@@ -97,7 +95,6 @@ pub fn dbgWrite(self: *@This(), comptime T: type, address: u32, value: T) void {
fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, value: T) void {
const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
const writeInt = std.mem.writeIntLittle;
const aligned_addr = forceAlign(T, address);
@@ -108,13 +105,13 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v
}
switch (aligned_addr) {
0x0000_0000...0x01FF_FFFF => self.bios.write(T, address, value),
0x0200_0000...0x02FF_FFFF => writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], value),
0x0000_0000...0x01FF_FFFF => self.bios.write(T, aligned_addr, value),
0x0200_0000...0x02FF_FFFF => std.mem.writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], value, .little),
0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) {
0b00 => writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value),
0b00 => std.mem.writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value, .little),
else => self.shr_wram.write(T, .nds7, aligned_addr, value),
},
0x0380_0000...0x03FF_FFFF => writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value),
0x0380_0000...0x03FF_FFFF => std.mem.writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value, .little),
0x0400_0000...0x04FF_FFFF => io.write(self, T, aligned_addr, value),
0x0600_0000...0x06FF_FFFF => self.vram.write(T, .nds7, aligned_addr, value),
else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),

View File

@@ -223,7 +223,34 @@ fn Controller(comptime id: u2) type {
else => log.err("TODO: Implement DMA({}) {s} mode", .{ id, @tagName(start_timing) }),
}
log.debug("configured {s} transfer from 0x{X:0>8} -> 0x{X:0>8} ({} words) for DMA{}", .{ @tagName(start_timing), self.sad_latch, self.dad_latch, self._word_count, id });
// Debug stuff
{
const sad_adj: Adjustment = @enumFromInt(new.sad_adj.read());
const dad_adj: Adjustment = @enumFromInt(new.dad_adj.read());
const byte_count = @as(u32, @sizeOf(u16)) << @intFromBool(new.transfer_type.read());
const sad_final = switch (sad_adj) {
.Increment, .IncrementReload => self.sad_latch +% self._word_count * byte_count,
.Decrement => self.sad_latch -% self._word_count * byte_count,
.Fixed => self.sad_latch,
};
const dad_final = switch (dad_adj) {
.Increment, .IncrementReload => self.dad_latch +% self._word_count * byte_count,
.Decrement => self.dad_latch -% self._word_count * byte_count,
.Fixed => self.dad_latch,
};
log.debug("configured {s} transfer from 0x{X:0>8} -> 0x{X:0>8} to 0x{X:0>8} -> 0x{X:0>8} ({} words) for DMA{}", .{
@tagName(start_timing),
self.sad_latch,
sad_final,
self.dad_latch,
dad_final,
self._word_count,
id,
});
}
}
self.cnt.raw = halfword;

View File

@@ -57,7 +57,7 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
0x0400_00B0...0x0400_00DC => dma.read(T, &bus.dma, address) orelse 0x000_0000,
// Timers
0x0400_0100...0x0400_010C => warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010C => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {}", .{ T, address, T }),
0x0400_0180 => bus.io.shr.ipc._nds7.sync.raw,
0x0400_0208 => @intFromBool(bus.io.ime),
@@ -73,9 +73,11 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
0x0400_00B0...0x0400_00DE => dma.read(T, &bus.dma, address) orelse 0x0000,
// Timers
0x0400_0100...0x0400_010E => warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010E => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {}", .{ T, address, T }),
0x0400_0130 => bus.io.shr.input.keyinput().raw,
0x0400_0136 => bus.io.shr.input.extkeyin().raw,
0x0400_0130 => bus.io.shr.keyinput.load(.Monotonic),
0x0400_0180 => @truncate(bus.io.shr.ipc._nds7.sync.raw),
0x0400_0184 => @truncate(bus.io.shr.ipc._nds7.cnt.raw),
else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
@@ -85,10 +87,10 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
0x0400_00B0...0x0400_00DF => dma.read(T, &bus.dma, address) orelse 0x00,
// Timers
0x0400_0100...0x0400_010F => warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010F => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {}", .{ T, address, T }),
// RTC
0x0400_0138 => warn("TODO: RTC read", .{}),
0x0400_0138 => warn("TODO(rtc): read(T: {}, addr: 0x{X:0>8}) {}", .{ T, address, T }),
0x0400_0240 => bus.vram.stat().raw,
0x0400_0241 => bus.io.shr.wramcnt.raw,
@@ -107,7 +109,7 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
0x0400_00B0...0x0400_00DC => dma.write(T, &bus.dma, address, value),
// Timers
0x0400_0100...0x0400_010C => log.warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010C => log.warn("TODO(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
0x0400_0180 => bus.io.shr.ipc.setIpcSync(.nds7, value),
0x0400_0208 => bus.io.ime = value & 1 == 1,
@@ -124,7 +126,7 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
0x0400_00B0...0x0400_00DE => dma.write(T, &bus.dma, address, value),
// Timers
0x0400_0100...0x0400_010E => log.warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010E => log.warn("TODO(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
0x0400_0180 => bus.io.shr.ipc.setIpcSync(.nds7, value),
0x0400_0184 => bus.io.shr.ipc.setIpcFifoCnt(.nds7, value),
@@ -137,10 +139,10 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
0x0400_00B0...0x0400_00DF => dma.write(T, &bus.dma, address, value),
// Timers
0x0400_0100...0x0400_010F => log.warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010F => log.warn("TODO(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
// RTC
0x0400_0138 => log.warn("TODO: RTC write", .{}),
0x0400_0138 => log.warn("TODO(rtc): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
0x0400_0208 => bus.io.ime = value & 1 == 1,

View File

@@ -31,7 +31,6 @@ pub fn deinit(self: @This(), allocator: Allocator) void {
// Note: Parts of 16MiB addrspace that aren't mapped to BIOS are typically undefined
pub fn read(self: *const @This(), comptime T: type, address: u32) T {
const readInt = std.mem.readIntLittle;
const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
// if (address >= len) return 0x0000_0000; // TODO: What is undefined actually?
@@ -41,7 +40,7 @@ pub fn read(self: *const @This(), comptime T: type, address: u32) T {
@panic("TODO: ability to load in NDS9 BIOS just-in-time");
};
return readInt(T, ptr[address & (len - 1) ..][0..byte_count]);
return std.mem.readInt(T, ptr[address & (len - 1) ..][0..byte_count], .little);
}
pub fn write(_: *const @This(), comptime T: type, address: u32, value: T) void {

View File

@@ -66,8 +66,6 @@ pub fn dbgRead(self: *@This(), comptime T: type, address: u32) T {
fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T {
const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
const readInt = std.mem.readIntLittle;
const aligned_addr = forceAlign(T, address);
switch (mode) {
@@ -77,13 +75,13 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T
}
return switch (aligned_addr) {
0x0200_0000...0x02FF_FFFF => readInt(T, self.main[aligned_addr & (4 * MiB - 1) ..][0..byte_count]),
0x0200_0000...0x02FF_FFFF => std.mem.readInt(T, self.main[aligned_addr & (4 * MiB - 1) ..][0..byte_count], .little),
0x0300_0000...0x03FF_FFFF => self.wram.read(T, .nds9, aligned_addr),
0x0400_0000...0x04FF_FFFF => io.read(self, T, aligned_addr),
0x0500_0000...0x05FF_FFFF => readInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)]),
0x0500_0000...0x05FF_FFFF => std.mem.readInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], .little),
0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(T, .nds9, aligned_addr),
0x0700_0000...0x07FF_FFFF => readInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..byte_count]),
0xFFFF_0000...0xFFFF_FFFF => self.bios.read(T, address),
0x0700_0000...0x07FF_FFFF => std.mem.readInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..byte_count], .little),
0xFFFF_0000...0xFFFF_FFFF => self.bios.read(T, aligned_addr),
else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
};
}
@@ -98,8 +96,6 @@ pub fn dbgWrite(self: *@This(), comptime T: type, address: u32, value: T) void {
fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, value: T) void {
const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
const writeInt = std.mem.writeIntLittle;
const aligned_addr = forceAlign(T, address);
switch (mode) {
@@ -109,13 +105,13 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v
}
switch (aligned_addr) {
0x0200_0000...0x02FF_FFFF => writeInt(T, self.main[aligned_addr & (4 * MiB - 1) ..][0..byte_count], value),
0x0200_0000...0x02FF_FFFF => std.mem.writeInt(T, self.main[aligned_addr & (4 * MiB - 1) ..][0..byte_count], value, .little),
0x0300_0000...0x03FF_FFFF => self.wram.write(T, .nds9, aligned_addr, value),
0x0400_0000...0x04FF_FFFF => io.write(self, T, aligned_addr, value),
0x0500_0000...0x05FF_FFFF => writeInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value),
0x0500_0000...0x05FF_FFFF => std.mem.writeInt(T, self.makeshift_palram[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little),
0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(T, .nds9, aligned_addr, value),
0x0700_0000...0x07FF_FFFF => writeInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value),
0xFFFF_0000...0xFFFF_FFFF => self.bios.write(T, address, value),
0x0700_0000...0x07FF_FFFF => std.mem.writeInt(T, self.ppu.oam.buf[aligned_addr & (2 * KiB - 1) ..][0..@sizeOf(T)], value, .little),
0xFFFF_0000...0xFFFF_FFFF => self.bios.write(T, aligned_addr, value),
else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
}
}

View File

@@ -223,7 +223,34 @@ fn Controller(comptime id: u2) type {
else => log.err("TODO: Implement DMA({}) {s} mode", .{ id, @tagName(start_timing) }),
}
log.debug("configured {s} transfer from 0x{X:0>8} -> 0x{X:0>8} ({} words) for DMA{}", .{ @tagName(start_timing), self.sad_latch, self.dad_latch, self._word_count, id });
// Debug stuff
{
const sad_adj: Adjustment = @enumFromInt(new.sad_adj.read());
const dad_adj: Adjustment = @enumFromInt(new.dad_adj.read());
const byte_count = @as(u32, @sizeOf(u16)) << @intFromBool(new.transfer_type.read());
const sad_final = switch (sad_adj) {
.Increment, .IncrementReload => self.sad_latch +% self._word_count * byte_count,
.Decrement => self.sad_latch -% self._word_count * byte_count,
.Fixed => self.sad_latch,
};
const dad_final = switch (dad_adj) {
.Increment, .IncrementReload => self.dad_latch +% self._word_count * byte_count,
.Decrement => self.dad_latch -% self._word_count * byte_count,
.Fixed => self.dad_latch,
};
log.debug("configured {s} transfer from 0x{X:0>8} -> 0x{X:0>8} to 0x{X:0>8} -> 0x{X:0>8} ({} words) for DMA{}", .{
@tagName(start_timing),
self.sad_latch,
sad_final,
self.dad_latch,
dad_final,
self._word_count,
id,
});
}
}
self.cnt.raw = halfword;
@@ -235,7 +262,7 @@ fn Controller(comptime id: u2) type {
}
pub fn step(self: *Self, cpu: *System.Arm946es) void {
const bus_ptr: *System.Bus7 = @ptrCast(@alignCast(cpu.bus.ptr));
const bus_ptr: *System.Bus9 = @ptrCast(@alignCast(cpu.bus.ptr));
const is_fifo = (id == 1 or id == 2) and self.cnt.start_timing.read() == 0b11;
const sad_adj: Adjustment = @enumFromInt(self.cnt.sad_adj.read());

View File

@@ -55,10 +55,10 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
u32 => switch (address) {
// DMA Transfers
0x0400_00B0...0x0400_00DC => dma.read(T, &bus.dma, address) orelse 0x0000_0000,
0x0400_00E0...0x0400_00EC => std.mem.readIntLittle(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)]),
0x0400_00E0...0x0400_00EC => std.mem.readInt(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], .little),
// Timers
0x0400_0100...0x0400_010C => warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010C => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
0x0400_0180 => bus.io.shr.ipc._nds9.sync.raw,
0x0400_0208 => @intFromBool(bus.io.ime),
@@ -89,13 +89,13 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
// DMA Transfers
0x0400_00B0...0x0400_00DE => dma.read(T, &bus.dma, address) orelse 0x0000,
0x0400_00E0...0x0400_00EE => std.mem.readIntLittle(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)]),
0x0400_00E0...0x0400_00EE => std.mem.readInt(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], .little),
// Timers
0x0400_0100...0x0400_010E => warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010E => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
0x0400_0004 => bus.ppu.io.nds9.dispstat.raw,
0x0400_0130 => bus.io.shr.keyinput.load(.Monotonic),
0x0400_0130 => bus.io.shr.input.keyinput().raw,
0x0400_0180 => @truncate(bus.io.shr.ipc._nds9.sync.raw),
0x0400_0184 => @truncate(bus.io.shr.ipc._nds9.cnt.raw),
@@ -122,10 +122,10 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
u8 => switch (address) {
// DMA Transfers
0x0400_00B0...0x0400_00DF => dma.read(T, &bus.dma, address) orelse 0x00,
0x0400_00E0...0x0400_00EF => std.mem.readIntLittle(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)]),
0x0400_00E0...0x0400_00EF => std.mem.readInt(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], .little),
// Timers
0x0400_0100...0x0400_010F => warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010F => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
0x0400_0208 => @intFromBool(bus.io.ime),
@@ -170,10 +170,10 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
// DMA Transfers
0x0400_00B0...0x0400_00DC => dma.write(T, &bus.dma, address, value),
0x0400_00E0...0x0400_00EC => std.mem.writeIntLittle(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], value),
0x0400_00E0...0x0400_00EC => std.mem.writeInt(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], value, .little),
// Timers
0x0400_0100...0x0400_010C => log.warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010C => log.warn("TODO(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
0x0400_0180 => bus.io.shr.ipc.setIpcSync(.nds9, value),
0x0400_0184 => bus.io.shr.ipc.setIpcFifoCnt(.nds9, value),
@@ -262,10 +262,10 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
// DMA Transfers
0x0400_00B0...0x0400_00DE => dma.write(T, &bus.dma, address, value),
0x0400_00E0...0x0400_00EE => std.mem.writeIntLittle(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], value),
0x0400_00E0...0x0400_00EE => std.mem.writeInt(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], value, .little),
// Timers
0x0400_0100...0x0400_010E => log.warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010E => log.warn("TODO(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
0x0400_0180 => bus.io.shr.ipc.setIpcSync(.nds9, value),
0x0400_0184 => bus.io.shr.ipc.setIpcFifoCnt(.nds9, value),
@@ -302,10 +302,10 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
u8 => switch (address) {
// DMA Transfers
0x0400_00B0...0x0400_00DF => dma.write(T, &bus.dma, address, value),
0x0400_00E0...0x0400_00EF => std.mem.writeIntLittle(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], value),
0x0400_00E0...0x0400_00EF => std.mem.writeInt(T, bus.io.dma_fill[address & 0xF ..][0..@sizeOf(T)], value, .little),
// Timers
0x0400_0100...0x0400_010F => log.warn("TODO: impl timer", .{}),
0x0400_0100...0x0400_010F => log.warn("TODO(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
0x0400_0208 => bus.io.ime = value & 1 == 1,

View File

@@ -6,6 +6,7 @@ const emu = @import("core/emu.zig");
const Ui = @import("platform.zig").Ui;
const SharedCtx = @import("core/emu.zig").SharedCtx;
const System = @import("core/emu.zig").System;
const Sync = @import("core/emu.zig").Sync;
const Scheduler = @import("core/Scheduler.zig");
const Allocator = std.mem.Allocator;
@@ -14,6 +15,7 @@ const ClapResult = clap.Result(clap.Help, &cli_params, clap.parsers.default);
const cli_params = clap.parseParamsComptime(
\\-h, --help Display this help and exit.
\\-f, --firm <str> Path to NDS Firmware Directory
\\--gdb Run Turbo in GDB Mode
\\<str> Path to the NDS ROM
\\
);
@@ -26,7 +28,7 @@ pub fn main() !void {
const allocator = gpa.allocator();
const result = try clap.parse(clap.Help, &cli_params, clap.parsers.default, .{});
const result = try clap.parse(clap.Help, &cli_params, clap.parsers.default, .{ .allocator = allocator });
defer result.deinit();
const rom_path = try handlePositional(result);
@@ -71,7 +73,17 @@ pub fn main() !void {
defer ui.deinit(allocator);
ui.setTitle(rom_title);
try ui.run(&scheduler, system);
const sync = try allocator.create(Sync);
defer allocator.destroy(sync);
sync.init();
if (result.args.gdb == 0) {
try ui.run(&scheduler, system, sync);
} else {
try emu.debug.run(allocator, &ui, &scheduler, system, sync);
}
}
fn handlePositional(result: ClapResult) ![]const u8 {

View File

@@ -8,8 +8,11 @@ const imgui = @import("ui/imgui.zig");
const emu = @import("core/emu.zig");
const System = @import("core/emu.zig").System;
const Sync = @import("core/emu.zig").Sync;
const KeyInput = @import("core/io.zig").KeyInput;
const ExtKeyIn = @import("core/io.zig").ExtKeyIn;
const Scheduler = @import("core/Scheduler.zig");
const FrameBuffer = @import("core/ppu.zig").FrameBuffer;
const Allocator = std.mem.Allocator;
@@ -33,7 +36,7 @@ pub const Ui = struct {
state: imgui.State,
pub fn init(allocator: Allocator) !Self {
var state = imgui.State{};
const state = imgui.State{};
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic();
@@ -85,123 +88,161 @@ pub const Ui = struct {
return SDL.SDL_GL_GetProcAddress(proc.ptr);
}
pub fn run(self: *Self, scheduler: *Scheduler, system: System) !void {
// TODO: Sort this out please
pub fn setTitle(self: *@This(), title: [12]u8) void {
self.state.title = title ++ [_:0]u8{};
}
const vao_id = opengl_impl.vao();
defer gl.deleteVertexArrays(1, &[_]GLuint{vao_id});
const top_tex = opengl_impl.screenTex(system.bus9.ppu.fb.top(.front));
const btm_tex = opengl_impl.screenTex(system.bus9.ppu.fb.btm(.front));
const top_out_tex = opengl_impl.outTex();
const btm_out_tex = opengl_impl.outTex();
defer gl.deleteTextures(4, &[_]GLuint{ top_tex, top_out_tex, btm_tex, btm_out_tex });
const top_fbo = try opengl_impl.frameBuffer(top_out_tex);
const btm_fbo = try opengl_impl.frameBuffer(btm_out_tex);
defer gl.deleteFramebuffers(2, &[_]GLuint{ top_fbo, btm_fbo });
const prog_id = try opengl_impl.program();
defer gl.deleteProgram(prog_id);
pub fn run(self: *Self, scheduler: *Scheduler, system: System, sync: *Sync) !void {
const id = try opengl_impl.runInit(&system.bus9.ppu.fb);
defer id.deinit();
var event: SDL.SDL_Event = undefined;
emu_loop: while (true) {
emu.runFrame(scheduler, system);
while (!sync.should_quit.load(.Monotonic)) {
emu.runFrame(scheduler, system); // TODO: run emu in separate thread
while (SDL.SDL_PollEvent(&event) != 0) {
_ = zgui.backend.processEvent(&event);
switch (event.type) {
SDL.SDL_QUIT => break :emu_loop,
SDL.SDL_WINDOWEVENT => {
if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) {
std.log.debug("window resized to: {}x{}", .{ event.window.data1, event.window.data2 });
self.state.dim.width = @intCast(event.window.data1);
self.state.dim.height = @intCast(event.window.data2);
}
},
SDL.SDL_KEYDOWN => {
// TODO: Make use of compare_and_xor?
const key_code = event.key.keysym.sym;
var keyinput: KeyInput = .{ .raw = 0x0000 };
switch (key_code) {
SDL.SDLK_UP => keyinput.up.set(),
SDL.SDLK_DOWN => keyinput.down.set(),
SDL.SDLK_LEFT => keyinput.left.set(),
SDL.SDLK_RIGHT => keyinput.right.set(),
SDL.SDLK_x => keyinput.a.set(),
SDL.SDLK_z => keyinput.b.set(),
SDL.SDLK_a => keyinput.shoulder_l.set(),
SDL.SDLK_s => keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => keyinput.start.set(),
SDL.SDLK_RSHIFT => keyinput.select.set(),
else => {},
}
system.bus9.io.shr.keyinput.fetchAnd(~keyinput.raw, .Monotonic);
},
SDL.SDL_KEYUP => {
// TODO: Make use of compare_and_xor?
const key_code = event.key.keysym.sym;
var keyinput: KeyInput = .{ .raw = 0x0000 };
switch (key_code) {
SDL.SDLK_UP => keyinput.up.set(),
SDL.SDLK_DOWN => keyinput.down.set(),
SDL.SDLK_LEFT => keyinput.left.set(),
SDL.SDLK_RIGHT => keyinput.right.set(),
SDL.SDLK_x => keyinput.a.set(),
SDL.SDLK_z => keyinput.b.set(),
SDL.SDLK_a => keyinput.shoulder_l.set(),
SDL.SDLK_s => keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => keyinput.start.set(),
SDL.SDLK_RSHIFT => keyinput.select.set(),
else => {},
}
system.bus9.io.shr.keyinput.fetchOr(keyinput.raw, .Monotonic);
},
else => {},
}
handleInput(&event, system, &self.state, sync);
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, top_fbo);
gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(top_tex, prog_id, vao_id, system.bus9.ppu.fb.top(.front));
opengl_impl.drawScreen(id.top_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.top(.front));
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, btm_fbo);
gl.bindFramebuffer(gl.FRAMEBUFFER, id.btm_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(btm_tex, prog_id, vao_id, system.bus9.ppu.fb.btm(.front));
opengl_impl.drawScreen(id.btm_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.btm(.front));
}
const zgui_redraw = imgui.draw(&self.state, top_out_tex, btm_out_tex, system);
imgui.draw(&self.state, id.top_out_tex, id.btm_out_tex, system);
if (zgui_redraw) {
// Background Colour
const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw();
}
// Background Colour
const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw();
SDL.SDL_GL_SwapWindow(self.window);
}
}
pub fn setTitle(self: *@This(), title: [12]u8) void {
self.state.title = title ++ [_:0]u8{};
pub fn debug_run(self: *Self, _: *Scheduler, system: System, sync: *Sync) !void {
const id = try opengl_impl.runInit(&system.bus9.ppu.fb);
defer id.deinit();
var event: SDL.SDL_Event = undefined;
while (!sync.should_quit.load(.Monotonic)) {
while (SDL.SDL_PollEvent(&event) != 0) {
_ = zgui.backend.processEvent(&event);
handleInput(&event, system, &self.state, sync);
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(id.top_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.top(.front));
}
{
gl.bindFramebuffer(gl.FRAMEBUFFER, id.btm_fbo);
defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
gl.viewport(0, 0, nds_width, nds_height);
opengl_impl.drawScreen(id.btm_tex, id.prog_id, id.vao_id, system.bus9.ppu.fb.btm(.front));
}
imgui.draw(&self.state, id.top_out_tex, id.btm_out_tex, system);
// Background Colour
const size = zgui.io.getDisplaySize();
gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
gl.clearColor(0, 0, 0, 1.0);
gl.clear(gl.COLOR_BUFFER_BIT);
zgui.backend.draw();
SDL.SDL_GL_SwapWindow(self.window);
}
}
fn handleInput(event: *SDL.SDL_Event, system: System, state: *imgui.State, sync: *Sync) void {
switch (event.type) {
SDL.SDL_QUIT => sync.should_quit.store(true, .Monotonic),
SDL.SDL_WINDOWEVENT => {
if (event.window.event == SDL.SDL_WINDOWEVENT_RESIZED) {
std.log.debug("window resized to: {}x{}", .{ event.window.data1, event.window.data2 });
state.dim.width = @intCast(event.window.data1);
state.dim.height = @intCast(event.window.data2);
zgui.io.setDisplaySize(@floatFromInt(event.window.data1), @floatFromInt(event.window.data2));
}
},
SDL.SDL_KEYDOWN => {
// TODO: Make use of compare_and_xor?
const key_code = event.key.keysym.sym;
var keyinput: KeyInput = .{ .raw = 0x0000 };
var extkeyin: ExtKeyIn = .{ .raw = 0x0000 };
switch (key_code) {
SDL.SDLK_UP => keyinput.up.set(),
SDL.SDLK_DOWN => keyinput.down.set(),
SDL.SDLK_LEFT => keyinput.left.set(),
SDL.SDLK_RIGHT => keyinput.right.set(),
SDL.SDLK_c => keyinput.a.set(),
SDL.SDLK_x => keyinput.b.set(),
SDL.SDLK_d => extkeyin.x.set(),
SDL.SDLK_s => extkeyin.y.set(),
SDL.SDLK_a => keyinput.shoulder_l.set(),
SDL.SDLK_f => keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => keyinput.start.set(),
SDL.SDLK_RSHIFT => keyinput.select.set(),
else => {},
}
const input = (@as(u32, extkeyin.raw) << 16) | keyinput.raw;
system.bus9.io.shr.input.set(.And, ~input);
},
SDL.SDL_KEYUP => {
// TODO: Make use of compare_and_xor?
const key_code = event.key.keysym.sym;
var keyinput: KeyInput = .{ .raw = 0x0000 };
var extkeyin: ExtKeyIn = .{ .raw = 0x0000 };
switch (key_code) {
SDL.SDLK_UP => keyinput.up.set(),
SDL.SDLK_DOWN => keyinput.down.set(),
SDL.SDLK_LEFT => keyinput.left.set(),
SDL.SDLK_RIGHT => keyinput.right.set(),
SDL.SDLK_c => keyinput.a.set(),
SDL.SDLK_x => keyinput.b.set(),
SDL.SDLK_d => extkeyin.x.set(),
SDL.SDLK_s => extkeyin.y.set(),
SDL.SDLK_a => keyinput.shoulder_l.set(),
SDL.SDLK_f => keyinput.shoulder_r.set(),
SDL.SDLK_RETURN => keyinput.start.set(),
SDL.SDLK_RSHIFT => keyinput.select.set(),
else => {},
}
const input = (@as(u32, extkeyin.raw) << 16) | keyinput.raw;
system.bus9.io.shr.input.set(.Or, input);
},
else => {},
}
}
};
@@ -211,6 +252,46 @@ fn panic() noreturn {
}
const opengl_impl = struct {
const Ids = struct {
vao_id: GLuint,
top_tex: GLuint,
btm_tex: GLuint,
top_out_tex: GLuint,
btm_out_tex: GLuint,
top_fbo: GLuint,
btm_fbo: GLuint,
prog_id: GLuint,
fn deinit(self: Ids) void {
gl.deleteProgram(self.prog_id);
gl.deleteFramebuffers(2, &[_]GLuint{ self.top_fbo, self.btm_fbo });
gl.deleteTextures(4, &[_]GLuint{ self.top_tex, self.top_out_tex, self.btm_tex, self.btm_out_tex });
gl.deleteVertexArrays(1, &[_]GLuint{self.vao_id});
}
};
fn runInit(fb: *const FrameBuffer) !Ids {
const top_out_tex = opengl_impl.outTex();
const btm_out_tex = opengl_impl.outTex();
return .{
.vao_id = opengl_impl.vao(),
.top_tex = opengl_impl.screenTex(fb.top(.front)),
.btm_tex = opengl_impl.screenTex(fb.btm(.front)),
.top_out_tex = top_out_tex,
.btm_out_tex = btm_out_tex,
.top_fbo = try opengl_impl.frameBuffer(top_out_tex),
.btm_fbo = try opengl_impl.frameBuffer(btm_out_tex),
.prog_id = try opengl_impl.program(),
};
}
fn drawScreen(tex_id: GLuint, prog_id: GLuint, vao_id: GLuint, buf: []const u8) void {
gl.bindTexture(gl.TEXTURE_2D, tex_id);
defer gl.bindTexture(gl.TEXTURE_2D, 0);

View File

@@ -18,7 +18,7 @@ pub const State = struct {
dim: Dimensions = .{ .width = 1600, .height = 900 },
};
pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: System) bool {
pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: System) void {
_ = system;
zgui.backend.newFrame(@floatFromInt(state.dim.width), @floatFromInt(state.dim.height));
@@ -36,6 +36,4 @@ pub fn draw(state: *const State, top_tex: GLuint, btm_tex: GLuint, system: Syste
zgui.image(@ptrFromInt(top_tex), .{ .w = w, .h = h });
zgui.image(@ptrFromInt(btm_tex), .{ .w = w, .h = h });
}
return true;
}