Compare commits

...

16 Commits
ppu ... main

Author SHA1 Message Date
Rekai Nyangadzayi Musuka 9a0d86b89d fix(ci): disable macOS builds 2024-09-09 04:13:16 -05:00
Rekai Nyangadzayi Musuka 98d6de640f fix(windows): "SDL.h" as opposed to "SDL2/SDL.h" 2024-09-09 03:34:00 -05:00
Rekai Nyangadzayi Musuka 26870542cb feat: Update to Zig v0.13.0 2024-09-09 02:43:04 -05:00
Rekai Nyangadzayi Musuka 34210d1068 chore: update SDL.zig
Windows CI should work again
2024-03-22 12:39:53 -05:00
Rekai Nyangadzayi Musuka b0e23c1e28 chore: update zgui 2024-03-22 11:14:45 -05:00
Rekai Nyangadzayi Musuka 9c998f7d66 fix: properly handle 16-bit misaligned reads 2024-03-11 11:07:01 -05:00
Rekai Nyangadzayi Musuka 0ac107dd28 chore: replace SDL.zig 2024-03-06 18:44:06 -06:00
Rekai Nyangadzayi Musuka d71a07733d feat(ci): update ci 2024-03-06 18:36:34 -06:00
Rekai Nyangadzayi Musuka 114eb69e35 fix: correctly point to SDL2 on windows builds 2024-03-06 15:32:37 -06:00
Rekai Nyangadzayi Musuka 2afd6dbf9e fix: create and draw ui even when running in gdb mode 2024-03-05 20:59:30 -06:00
Rekai Nyangadzayi Musuka 422e0d00b8 chore: improve debug messages 2024-02-14 15:35:38 -06:00
Rekai Nyangadzayi Musuka 32e54c67bf feat: revamp controls, add support for X and Y buttons 2024-02-14 13:25:24 -06:00
Rekai Nyangadzayi Musuka 0ad017cf43 chore: upgrade to Zig v2024.1.0-mach 2024-02-12 20:22:02 -06:00
Rekai Nyangadzayi Musuka 62db837442 chore: remove unnecesary gdb.zig file 2024-01-13 16:10:42 -06:00
Rekai Nyangadzayi Musuka 117a95d3a9 feat: implement ARM7 gdbstub 2023-12-27 21:35:59 -06:00
Rekai Nyangadzayi Musuka ed72427c71 tmp: progress towards gdb stub 2023-12-27 19:52:15 -06:00
22 changed files with 590 additions and 213 deletions

View File

@ -4,7 +4,8 @@ on:
push:
paths:
- "**.zig"
- "**.zig.zon"
- "dl_sdl2.ps1"
- "build.zig.zon"
branches:
- main
schedule:
@ -15,12 +16,17 @@ jobs:
build:
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, windows-latest] # TODO: Figure out Apple Silicon macOS
runs-on: ${{matrix.os}}
steps:
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0
version: 0.13.0
- run: |
git config --global core.autocrlf false
- uses: actions/checkout@v3
with:
submodules: recursive
- name: prepare-linux
if: runner.os == 'Linux'
run: |
@ -29,26 +35,19 @@ jobs:
- name: prepare-windows
if: runner.os == 'Windows'
run: |
vcpkg integrate install
vcpkg install sdl2:x64-windows
git config --global core.autocrlf false
.\dl_sdl2.ps1
- name: prepare-macos
if: runner.os == 'macOS'
run: |
brew install sdl2
- uses: actions/checkout@v3
with:
submodules: recursive
- name: build
run: zig build -Doptimize=ReleaseSafe -Dcpu=baseline
- name: prepare-executable
run: |
mv zig-out/lib/* zig-out/bin
- name: upload
uses: actions/upload-artifact@v3
with:
name: zba-${{matrix.os}}
path: zig-out/bin
path: zig-out
lint:
runs-on: ubuntu-latest
steps:
@ -57,6 +56,5 @@ jobs:
submodules: recursive
- uses: goto-bus-stop/setup-zig@v2
with:
version: 0.11.0-dev.3395+1e7dcaa3a
- run: zig fmt src/**/*.zig
version: 0.13.0
- run: zig fmt --check {src,lib}/**/*.zig build.zig build.zig.zon

5
.gitignore vendored
View File

@ -1,5 +1,8 @@
zig-cache/
.zig-cache/
zig-out/
bin/
doc/
imgui.ini
.build_config/
**/*.log

8
.gitmodules vendored
View File

@ -1,9 +1,3 @@
[submodule "lib/SDL.zig"]
path = lib/SDL.zig
url = https://github.com/MasterQ32/SDL.zig
[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
url = https://github.com/paoda/SDL.zig

View File

@ -1,8 +1,5 @@
const std = @import("std");
const Sdk = @import("lib/SDL.zig/Sdk.zig");
const zgui = @import("lib/zgui/build.zig");
const arm32 = @import("lib/arm32/build.zig");
const sdl = @import("lib/SDL.zig/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
@ -23,26 +20,27 @@ pub fn build(b: *std.Build) void {
.name = "turbo",
// In this case the main source file is merely a path, however, in more
// complicated build scripts, this could be a generated file.
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});
exe.addModule("arm32", arm32.module(b));
exe.addModule("zig-clap", b.dependency("zig-clap", .{}).module("clap"));
const sdk = sdl.init(b, null, null);
const zgui = b.dependency("zgui", .{ .shared = false, .with_implot = true, .backend = .sdl2_opengl3 });
const imgui = zgui.artifact("imgui");
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.addImport("arm32", b.dependency("arm32", .{}).module("arm32")); // https://git.musuka.dev/paoda/arm32
exe.root_module.addImport("gdbstub", b.dependency("zba-gdbstub", .{}).module("zba-gdbstub")); // https://git.musuka.dev/paoda/zba-gdbstub
exe.root_module.addImport("zig-clap", b.dependency("zig-clap", .{}).module("clap")); // https://github.com/Hejsil/zig-clap
exe.root_module.addImport("zgui", zgui.module("root")); // https://git.musuka.dev/paoda/zgui
exe.root_module.addImport("sdl2", sdk.getNativeModule()); // https://github.com/MasterQ32/SDL.zig
// https://github.com/MasterQ32/SDL.zig
const sdk = Sdk.init(b, null);
sdk.link(exe, .dynamic);
exe.addModule("sdl2", sdk.getNativeModule());
exe.root_module.addAnonymousImport("bitfield", .{ .root_source_file = b.path("lib/bitfield.zig") }); // https://github.com/FlorenceOS/
exe.root_module.addAnonymousImport("gl", .{ .root_source_file = b.path("lib/gl.zig") }); // https://github.com/MasterQ32/zig-opengl
// 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 } });
zgui_pkg.link(exe);
sdk.link(exe, .dynamic, .SDL2);
sdk.link(imgui, .dynamic, .SDL2);
exe.linkLibrary(imgui);
// This declares intent for the executable to be installed into the
// standard location when the user invokes the "install" step (the default
@ -75,7 +73,7 @@ pub fn build(b: *std.Build) void {
// Creates a step for unit testing. This only builds the test executable
// but does not run it.
const unit_tests = b.addTest(.{
.root_source_file = .{ .path = "src/main.zig" },
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

View File

@ -1,15 +1,29 @@
.{
.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 = "git+https://github.com/Hejsil/zig-clap#c0193e9247335a6c1688b946325060289405de2a",
.hash = "12207ee987ce045596cb992cfb15b0d6d9456e50d4721c3061c69dabc2962053644d",
},
.@"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 = "git+https://git.musuka.dev/paoda/zba-gdbstub#9a50607d5f48293f950a4e823344f2bc24582a5a",
.hash = "1220ac267744ed2a735f03c4620d7c6210fbd36d7bfb2b376ddc3436faebadee0f61",
},
.arm32 = .{
.url = "git+https://git.musuka.dev/paoda/arm32#814d081ea0983bc48841a6baad7158c157b17ad6",
.hash = "12203c3dacf3a7aa7aee5fc5763dd7b40399bd1c34d1483330b6bd5a76bffef22d82",
},
.zgui = .{
.url = "git+https://git.musuka.dev/paoda/zgui#7f8d05101e96c64314d7926c80ee157dcb89da4e",
.hash = "1220bd81a1c7734892b1d4233ed047710487787873c85dd5fc76d1764a331ed2ff43",
},
},
}

36
dl_sdl2.ps1 Normal file
View File

@ -0,0 +1,36 @@
$SDL2Version = "2.30.0"
$ArchiveFile = ".\SDL2-devel-mingw.zip"
$Json = @"
{
"x86_64-windows-gnu": {
"include": ".build_config\\SDL2\\include",
"libs": ".build_config\\SDL2\\lib",
"bin": ".build_config\\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 Container .\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
# #include <SDL.h>
Move-Item -Force -Path .\SDL2\include\SDL2\* -Destination .\SDL2\include
Remove-Item -Force .\SDL2\include\SDL2
New-Item -Force .\sdl.json -Value $Json
Remove-Item -Recurse .\SDL2-devel-mingw
Set-Location -Path .. -PassThru

@ -1 +1 @@
Subproject commit 80e7409e21ebbf0bd182b34b6e0206cc26e5ca05
Subproject commit fac81ec499cfd64da7b846de27f6db4a0d4943bf

@ -1 +0,0 @@
Subproject commit 580e7baca962dd73815bb4717db05b83d55fd58e

@ -1 +0,0 @@
Subproject commit ca27a472249a70dd1e94fb94f05f26dd931363fb

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;
}