Compare commits
	
		
			41 Commits
		
	
	
		
			0765682035
			...
			main
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 9a0d86b89d | |||
| 98d6de640f | |||
| 26870542cb | |||
| 34210d1068 | |||
| b0e23c1e28 | |||
| 9c998f7d66 | |||
| 0ac107dd28 | |||
| d71a07733d | |||
| 114eb69e35 | |||
| 2afd6dbf9e | |||
| 422e0d00b8 | |||
| 32e54c67bf | |||
| 0ad017cf43 | |||
| 62db837442 | |||
| 117a95d3a9 | |||
| ed72427c71 | |||
| 7f98f4cc26 | |||
| 51076597e8 | |||
| 14f5682095 | |||
| 334cd432d4 | |||
| b903f2c4a8 | |||
| f52e1ab6b9 | |||
| ea63b04056 | |||
| e0171e5b65 | |||
| 3a1ebfb6e6 | |||
| 16233f3cd8 | |||
| 64b1bdbe19 | |||
| 37aff56c22 | |||
| bf08e93508 | |||
| 83f94f6d9d | |||
| 16b3325fe3 | |||
| 13f5497808 | |||
| ac3927333b | |||
| 81c17b9965 | |||
| 1a56a1a285 | |||
| 95dfeceb00 | |||
| 7f4dad485f | |||
| 825b6e95ac | |||
| c72e36ef6b | |||
| ea3b6ba8fd | |||
| 7e2ab0fa6d | 
							
								
								
									
										30
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										30
									
								
								.github/workflows/main.yml
									
									
									
									
										vendored
									
									
								
							@@ -4,7 +4,8 @@ on:
 | 
				
			|||||||
  push:
 | 
					  push:
 | 
				
			||||||
    paths:
 | 
					    paths:
 | 
				
			||||||
      - "**.zig"
 | 
					      - "**.zig"
 | 
				
			||||||
      - "**.zig.zon"
 | 
					      - "dl_sdl2.ps1"
 | 
				
			||||||
 | 
					      - "build.zig.zon"
 | 
				
			||||||
    branches:
 | 
					    branches:
 | 
				
			||||||
      - main
 | 
					      - main
 | 
				
			||||||
  schedule:
 | 
					  schedule:
 | 
				
			||||||
@@ -15,12 +16,17 @@ jobs:
 | 
				
			|||||||
  build:
 | 
					  build:
 | 
				
			||||||
    strategy:
 | 
					    strategy:
 | 
				
			||||||
      matrix:
 | 
					      matrix:
 | 
				
			||||||
        os: [ubuntu-latest, windows-latest, macos-latest]
 | 
					        os: [ubuntu-latest, windows-latest] # TODO: Figure out Apple Silicon macOS
 | 
				
			||||||
    runs-on: ${{matrix.os}}
 | 
					    runs-on: ${{matrix.os}}
 | 
				
			||||||
    steps:
 | 
					    steps:
 | 
				
			||||||
      - uses: goto-bus-stop/setup-zig@v2
 | 
					      - uses: goto-bus-stop/setup-zig@v2
 | 
				
			||||||
        with:
 | 
					        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
 | 
					      - name: prepare-linux
 | 
				
			||||||
        if: runner.os == 'Linux'
 | 
					        if: runner.os == 'Linux'
 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
@@ -29,26 +35,19 @@ jobs:
 | 
				
			|||||||
      - name: prepare-windows
 | 
					      - name: prepare-windows
 | 
				
			||||||
        if: runner.os == 'Windows'
 | 
					        if: runner.os == 'Windows'
 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
            vcpkg integrate install
 | 
					            .\dl_sdl2.ps1
 | 
				
			||||||
            vcpkg install sdl2:x64-windows
 | 
					 | 
				
			||||||
            git config --global core.autocrlf false
 | 
					 | 
				
			||||||
      - name: prepare-macos
 | 
					      - name: prepare-macos
 | 
				
			||||||
        if: runner.os == 'macOS'
 | 
					        if: runner.os == 'macOS'
 | 
				
			||||||
        run: |
 | 
					        run: |
 | 
				
			||||||
            brew install sdl2
 | 
					            brew install sdl2
 | 
				
			||||||
      - uses: actions/checkout@v3
 | 
					 | 
				
			||||||
        with:
 | 
					 | 
				
			||||||
          submodules: recursive
 | 
					 | 
				
			||||||
      - name: build 
 | 
					      - name: build 
 | 
				
			||||||
        run: zig build -Doptimize=ReleaseSafe -Dcpu=baseline
 | 
					        run: zig build -Doptimize=ReleaseSafe -Dcpu=baseline
 | 
				
			||||||
      - name: prepare-executable
 | 
					 | 
				
			||||||
        run: |
 | 
					 | 
				
			||||||
            mv zig-out/lib/* zig-out/bin
 | 
					 | 
				
			||||||
      - name: upload
 | 
					      - name: upload
 | 
				
			||||||
        uses: actions/upload-artifact@v3
 | 
					        uses: actions/upload-artifact@v3
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          name: zba-${{matrix.os}}
 | 
					          name: zba-${{matrix.os}}
 | 
				
			||||||
          path: zig-out/bin
 | 
					          path: zig-out
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  lint:
 | 
					  lint:
 | 
				
			||||||
    runs-on: ubuntu-latest
 | 
					    runs-on: ubuntu-latest
 | 
				
			||||||
    steps: 
 | 
					    steps: 
 | 
				
			||||||
@@ -57,6 +56,5 @@ jobs:
 | 
				
			|||||||
          submodules: recursive
 | 
					          submodules: recursive
 | 
				
			||||||
      - uses: goto-bus-stop/setup-zig@v2
 | 
					      - uses: goto-bus-stop/setup-zig@v2
 | 
				
			||||||
        with:
 | 
					        with:
 | 
				
			||||||
          version: 0.11.0-dev.3395+1e7dcaa3a
 | 
					          version: 0.13.0
 | 
				
			||||||
      - run: zig fmt src/**/*.zig
 | 
					      - run: zig fmt --check {src,lib}/**/*.zig build.zig build.zig.zon
 | 
				
			||||||
  
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										5
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -1,5 +1,8 @@
 | 
				
			|||||||
zig-cache/
 | 
					.zig-cache/
 | 
				
			||||||
zig-out/
 | 
					zig-out/
 | 
				
			||||||
bin/
 | 
					bin/
 | 
				
			||||||
doc/
 | 
					doc/
 | 
				
			||||||
imgui.ini
 | 
					imgui.ini
 | 
				
			||||||
 | 
					.build_config/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					**/*.log
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										8
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										8
									
								
								.gitmodules
									
									
									
									
										vendored
									
									
								
							@@ -1,9 +1,3 @@
 | 
				
			|||||||
[submodule "lib/SDL.zig"]
 | 
					[submodule "lib/SDL.zig"]
 | 
				
			||||||
	path = lib/SDL.zig
 | 
						path = lib/SDL.zig
 | 
				
			||||||
	url = https://github.com/MasterQ32/SDL.zig
 | 
						url = https://github.com/paoda/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
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										34
									
								
								build.zig
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								build.zig
									
									
									
									
									
								
							@@ -1,8 +1,5 @@
 | 
				
			|||||||
const std = @import("std");
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					const sdl = @import("lib/SDL.zig/build.zig");
 | 
				
			||||||
const Sdk = @import("lib/SDL.zig/Sdk.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
 | 
					// Although this function looks imperative, note that its job is to
 | 
				
			||||||
// declaratively construct a build graph that will be executed by an external
 | 
					// 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",
 | 
					        .name = "turbo",
 | 
				
			||||||
        // In this case the main source file is merely a path, however, in more
 | 
					        // In this case the main source file is merely a path, however, in more
 | 
				
			||||||
        // complicated build scripts, this could be a generated file.
 | 
					        // 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,
 | 
					        .target = target,
 | 
				
			||||||
        .optimize = optimize,
 | 
					        .optimize = optimize,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    exe.addModule("arm32", arm32.module(b));
 | 
					    const sdk = sdl.init(b, null, null);
 | 
				
			||||||
    exe.addModule("zig-clap", b.dependency("zig-clap", .{}).module("clap"));
 | 
					    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.root_module.addImport("arm32", b.dependency("arm32", .{}).module("arm32")); // https://git.musuka.dev/paoda/arm32
 | 
				
			||||||
    exe.addAnonymousModule("gl", .{ .source_file = .{ .path = "lib/gl.zig" } }); // https://github.com/MasterQ32/zig-opengl
 | 
					    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
 | 
					    exe.root_module.addAnonymousImport("bitfield", .{ .root_source_file = b.path("lib/bitfield.zig") }); // https://github.com/FlorenceOS/
 | 
				
			||||||
    const sdk = Sdk.init(b, null);
 | 
					    exe.root_module.addAnonymousImport("gl", .{ .root_source_file = b.path("lib/gl.zig") }); // https://github.com/MasterQ32/zig-opengl
 | 
				
			||||||
    sdk.link(exe, .dynamic);
 | 
					 | 
				
			||||||
    exe.addModule("sdl2", sdk.getNativeModule());
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // https://git.musuka.dev/paoda/zgui
 | 
					    sdk.link(exe, .dynamic, .SDL2);
 | 
				
			||||||
    // .shared option should stay in sync with SDL.zig call above where true == .dynamic, and false == .static
 | 
					    sdk.link(imgui, .dynamic, .SDL2);
 | 
				
			||||||
    const zgui_pkg = zgui.package(b, target, optimize, .{ .options = .{ .backend = .sdl2_opengl3, .shared = true } });
 | 
					    exe.linkLibrary(imgui);
 | 
				
			||||||
    zgui_pkg.link(exe);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // This declares intent for the executable to be installed into the
 | 
					    // This declares intent for the executable to be installed into the
 | 
				
			||||||
    // standard location when the user invokes the "install" step (the default
 | 
					    // 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
 | 
					    // Creates a step for unit testing. This only builds the test executable
 | 
				
			||||||
    // but does not run it.
 | 
					    // but does not run it.
 | 
				
			||||||
    const unit_tests = b.addTest(.{
 | 
					    const unit_tests = b.addTest(.{
 | 
				
			||||||
        .root_source_file = .{ .path = "src/main.zig" },
 | 
					        .root_source_file = b.path("src/main.zig"),
 | 
				
			||||||
        .target = target,
 | 
					        .target = target,
 | 
				
			||||||
        .optimize = optimize,
 | 
					        .optimize = optimize,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,15 +1,29 @@
 | 
				
			|||||||
.{
 | 
					.{
 | 
				
			||||||
    .name = "turbo",
 | 
					    .name = "turbo",
 | 
				
			||||||
    .version = "0.1.0",
 | 
					    .version = "0.1.0",
 | 
				
			||||||
 | 
					    .paths = .{
 | 
				
			||||||
 | 
					        "lib/bitfield.zig",
 | 
				
			||||||
 | 
					        "lib/gl.zig",
 | 
				
			||||||
 | 
					        "src",
 | 
				
			||||||
 | 
					        "build.zig",
 | 
				
			||||||
 | 
					        "build.zig.zon",
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
    .dependencies = .{
 | 
					    .dependencies = .{
 | 
				
			||||||
        .@"zig-clap" = .{
 | 
					        .@"zig-clap" = .{
 | 
				
			||||||
            .url = "https://github.com/Hejsil/zig-clap/archive/f49b94700e0761b7514abdca0e4f0e7f3f938a93.tar.gz",
 | 
					            .url = "git+https://github.com/Hejsil/zig-clap#c0193e9247335a6c1688b946325060289405de2a",
 | 
				
			||||||
            .hash = "1220f48518ce22882e102255ed3bcdb7aeeb4891f50b2cdd3bd74b5b2e24d3149ba2",
 | 
					            .hash = "12207ee987ce045596cb992cfb15b0d6d9456e50d4721c3061c69dabc2962053644d",
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        .@"zba-util" = .{
 | 
					        .@"zba-gdbstub" = .{
 | 
				
			||||||
            // Necessary to use paoda/arm32 as a git submodule
 | 
					            .url = "git+https://git.musuka.dev/paoda/zba-gdbstub#9a50607d5f48293f950a4e823344f2bc24582a5a",
 | 
				
			||||||
            .url = "https://git.musuka.dev/paoda/zba-util/archive/322c798e384a0d24cc84ffcfa2e4a3ca807798a0.tar.gz",
 | 
					            .hash = "1220ac267744ed2a735f03c4620d7c6210fbd36d7bfb2b376ddc3436faebadee0f61",
 | 
				
			||||||
            .hash = "12209ce0e729460b997706e47a53a32f1842672cd120189e612f4871731780a30ed0",
 | 
					        },
 | 
				
			||||||
 | 
					        .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
									
								
							
							
						
						
									
										36
									
								
								dl_sdl2.ps1
									
									
									
									
									
										Normal 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
 | 
				
			||||||
 Submodule lib/SDL.zig updated: 602aeb7f1d...fac81ec499
									
								
							 Submodule lib/arm32 deleted from 8d2a3f1b67
									
								
							
							
								
								
									
										1
									
								
								lib/zgui
									
									
									
									
									
								
							
							
								
								
								
								
								
							
						
						
									
										1
									
								
								lib/zgui
									
									
									
									
									
								
							 Submodule lib/zgui deleted from 2e4f8d1409
									
								
							@@ -1,7 +1,6 @@
 | 
				
			|||||||
const std = @import("std");
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Bus9 = @import("nds9/Bus.zig");
 | 
					const System = @import("emu.zig").System;
 | 
				
			||||||
const Bus7 = @import("nds7/Bus.zig");
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
const PriorityQueue = std.PriorityQueue(Event, void, Event.lessThan);
 | 
					const PriorityQueue = std.PriorityQueue(Event, void, Event.lessThan);
 | 
				
			||||||
const Allocator = std.mem.Allocator;
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
@@ -41,6 +40,10 @@ pub fn reset(self: *@This()) void {
 | 
				
			|||||||
    self.tick = 0;
 | 
					    self.tick = 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub inline fn peekTimestamp(self: *const @This()) u64 {
 | 
				
			||||||
 | 
					    return self.queue.items[0].tick;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub inline fn check(self: *@This()) ?Event {
 | 
					pub inline fn check(self: *@This()) ?Event {
 | 
				
			||||||
    @setRuntimeSafety(false);
 | 
					    @setRuntimeSafety(false);
 | 
				
			||||||
    if (self.tick < self.queue.items[0].tick) return null;
 | 
					    if (self.tick < self.queue.items[0].tick) return null;
 | 
				
			||||||
@@ -48,25 +51,25 @@ pub inline fn check(self: *@This()) ?Event {
 | 
				
			|||||||
    return self.queue.remove();
 | 
					    return self.queue.remove();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn handle(self: *@This(), bus_ptr: ?*anyopaque, event: Event, late: u64) void {
 | 
					pub fn handle(self: *@This(), system: System, event: Event, late: u64) void {
 | 
				
			||||||
    switch (event.kind) {
 | 
					    switch (event.kind) {
 | 
				
			||||||
        .heat_death => unreachable,
 | 
					        .heat_death => unreachable,
 | 
				
			||||||
        .nds7 => |ev| {
 | 
					        .nds7 => |ev| {
 | 
				
			||||||
            const bus: *Bus7 = @ptrCast(@alignCast(bus_ptr));
 | 
					            const bus = system.bus7;
 | 
				
			||||||
            _ = bus;
 | 
					            _ = bus;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            switch (ev) {}
 | 
					            switch (ev) {}
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        .nds9 => |ev| {
 | 
					        .nds9 => |ev| {
 | 
				
			||||||
            const bus: *Bus9 = @ptrCast(@alignCast(bus_ptr));
 | 
					            const bus = system.bus9;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            switch (ev) {
 | 
					            switch (ev) {
 | 
				
			||||||
                .draw => {
 | 
					                .draw => {
 | 
				
			||||||
                    bus.ppu.drawScanline(bus);
 | 
					                    bus.ppu.drawScanline(bus);
 | 
				
			||||||
                    bus.ppu.onHdrawEnd(self, late);
 | 
					                    bus.ppu.onHdrawEnd(system, self, late);
 | 
				
			||||||
                },
 | 
					                },
 | 
				
			||||||
                .hblank => bus.ppu.onHblankEnd(self, late),
 | 
					                .hblank => bus.ppu.onHblankEnd(system, self, late),
 | 
				
			||||||
                .vblank => bus.ppu.onHblankEnd(self, late),
 | 
					                .vblank => bus.ppu.onVblankEnd(system, self, late),
 | 
				
			||||||
                .sqrt => bus.io.sqrt.onSqrtCalc(),
 | 
					                .sqrt => bus.io.sqrt.onSqrtCalc(),
 | 
				
			||||||
                .div => bus.io.div.onDivCalc(),
 | 
					                .div => bus.io.div.onDivCalc(),
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										360
									
								
								src/core/emu.zig
									
									
									
									
									
								
							
							
						
						
									
										360
									
								
								src/core/emu.zig
									
									
									
									
									
								
							@@ -1,21 +1,27 @@
 | 
				
			|||||||
const std = @import("std");
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Header = @import("cartridge.zig").Header;
 | 
					const Header = @import("cartridge.zig").Header;
 | 
				
			||||||
const SharedIo = @import("io.zig").Io;
 | 
					 | 
				
			||||||
const Scheduler = @import("Scheduler.zig");
 | 
					const Scheduler = @import("Scheduler.zig");
 | 
				
			||||||
 | 
					const Ui = @import("../platform.zig").Ui;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Allocator = std.mem.Allocator;
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const dma7 = @import("nds7/dma.zig");
 | 
				
			||||||
 | 
					const dma9 = @import("nds9/dma.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Load a NDS Cartridge
 | 
					/// Load a NDS Cartridge
 | 
				
			||||||
///
 | 
					///
 | 
				
			||||||
/// intended to be used immediately after Emulator initialization
 | 
					/// intended to be used immediately after Emulator initialization
 | 
				
			||||||
pub fn load(allocator: Allocator, system: System, rom_file: std.fs.File) ![12]u8 {
 | 
					pub fn load(allocator: Allocator, system: System, rom_path: []const u8) ![12]u8 {
 | 
				
			||||||
    const log = std.log.scoped(.load_rom);
 | 
					    const log = std.log.scoped(.load_rom);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const rom_buf = try rom_file.readToEndAlloc(allocator, try rom_file.getEndPos());
 | 
					    const file = try std.fs.cwd().openFile(rom_path, .{});
 | 
				
			||||||
    defer allocator.free(rom_buf);
 | 
					    defer file.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var stream = std.io.fixedBufferStream(rom_buf);
 | 
					    const buf = try file.readToEndAlloc(allocator, try file.getEndPos());
 | 
				
			||||||
 | 
					    defer allocator.free(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    var stream = std.io.fixedBufferStream(buf);
 | 
				
			||||||
    const header = try stream.reader().readStruct(Header);
 | 
					    const header = try stream.reader().readStruct(Header);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    log.info("Title: \"{s}\"", .{std.mem.sliceTo(&header.title, 0)});
 | 
					    log.info("Title: \"{s}\"", .{std.mem.sliceTo(&header.title, 0)});
 | 
				
			||||||
@@ -30,7 +36,7 @@ pub fn load(allocator: Allocator, system: System, rom_file: std.fs.File) ![12]u8
 | 
				
			|||||||
        log.debug("ARM9 Size: 0x{X:0>8}", .{header.arm9_size});
 | 
					        log.debug("ARM9 Size: 0x{X:0>8}", .{header.arm9_size});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Copy ARM9 Code into Main Memory
 | 
					        // Copy ARM9 Code into Main Memory
 | 
				
			||||||
        for (rom_buf[header.arm9_rom_offset..][0..header.arm9_size], 0..) |value, i| {
 | 
					        for (buf[header.arm9_rom_offset..][0..header.arm9_size], 0..) |value, i| {
 | 
				
			||||||
            const address = header.arm9_ram_address + @as(u32, @intCast(i));
 | 
					            const address = header.arm9_ram_address + @as(u32, @intCast(i));
 | 
				
			||||||
            system.bus9.dbgWrite(u8, address, value);
 | 
					            system.bus9.dbgWrite(u8, address, value);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -46,7 +52,7 @@ pub fn load(allocator: Allocator, system: System, rom_file: std.fs.File) ![12]u8
 | 
				
			|||||||
        log.debug("ARM7 Size: 0x{X:0>8}", .{header.arm7_size});
 | 
					        log.debug("ARM7 Size: 0x{X:0>8}", .{header.arm7_size});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // Copy ARM7 Code into Main Memory
 | 
					        // Copy ARM7 Code into Main Memory
 | 
				
			||||||
        for (rom_buf[header.arm7_rom_offset..][0..header.arm7_size], 0..) |value, i| {
 | 
					        for (buf[header.arm7_rom_offset..][0..header.arm7_size], 0..) |value, i| {
 | 
				
			||||||
            const address = header.arm7_ram_address + @as(u32, @intCast(i));
 | 
					            const address = header.arm7_ram_address + @as(u32, @intCast(i));
 | 
				
			||||||
            system.bus7.dbgWrite(u8, address, value);
 | 
					            system.bus7.dbgWrite(u8, address, value);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -57,6 +63,41 @@ pub fn load(allocator: Allocator, system: System, rom_file: std.fs.File) ![12]u8
 | 
				
			|||||||
    return header.title;
 | 
					    return header.title;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Load NDS Firmware
 | 
				
			||||||
 | 
					pub fn loadFirm(allocator: Allocator, system: System, firm_path: []const u8) !void {
 | 
				
			||||||
 | 
					    const log = std.log.scoped(.load_firm);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    { // NDS7 BIOS
 | 
				
			||||||
 | 
					        const path = try std.mem.join(allocator, "/", &.{ firm_path, "bios7.bin" });
 | 
				
			||||||
 | 
					        defer allocator.free(path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        log.debug("bios7 path: {s}", .{path});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const file = try std.fs.cwd().openFile(path, .{});
 | 
				
			||||||
 | 
					        defer file.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const buf = try file.readToEndAlloc(allocator, try file.getEndPos());
 | 
				
			||||||
 | 
					        defer allocator.free(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try system.bus7.bios.load(allocator, buf);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    { // NDS9 BIOS
 | 
				
			||||||
 | 
					        const path = try std.mem.join(allocator, "/", &.{ firm_path, "bios9.bin" });
 | 
				
			||||||
 | 
					        defer allocator.free(path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        log.debug("bios9 path: {s}", .{path});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const file = try std.fs.cwd().openFile(path, .{});
 | 
				
			||||||
 | 
					        defer file.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const buf = try file.readToEndAlloc(allocator, try file.getEndPos());
 | 
				
			||||||
 | 
					        defer allocator.free(buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        try system.bus9.bios.load(allocator, buf);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const bus_clock = 33513982; // 33.513982 Hz
 | 
					const bus_clock = 33513982; // 33.513982 Hz
 | 
				
			||||||
const dot_clock = 5585664; //   5.585664 Hz
 | 
					const dot_clock = 5585664; //   5.585664 Hz
 | 
				
			||||||
const arm7_clock = bus_clock;
 | 
					const arm7_clock = bus_clock;
 | 
				
			||||||
@@ -70,33 +111,48 @@ pub fn runFrame(scheduler: *Scheduler, system: System) void {
 | 
				
			|||||||
    const frame_end = scheduler.tick + cycles_per_frame;
 | 
					    const frame_end = scheduler.tick + cycles_per_frame;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    while (scheduler.tick < frame_end) {
 | 
					    while (scheduler.tick < frame_end) {
 | 
				
			||||||
 | 
					        switch (isHalted(system)) {
 | 
				
			||||||
 | 
					            .both => scheduler.tick = scheduler.peekTimestamp(),
 | 
				
			||||||
 | 
					            inline else => |halt| {
 | 
				
			||||||
 | 
					                if (!dma9.step(system.arm946es) and comptime halt != .arm9) {
 | 
				
			||||||
 | 
					                    system.arm946es.step();
 | 
				
			||||||
 | 
					                    system.arm946es.step();
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (!dma7.step(system.arm7tdmi) and comptime halt != .arm7) {
 | 
				
			||||||
                    system.arm7tdmi.step();
 | 
					                    system.arm7tdmi.step();
 | 
				
			||||||
        system.arm946es.step();
 | 
					                }
 | 
				
			||||||
        system.arm946es.step();
 | 
					            },
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (scheduler.check()) |ev| {
 | 
					        if (scheduler.check()) |ev| {
 | 
				
			||||||
            const late = scheduler.tick - ev.tick;
 | 
					            const late = scheduler.tick - ev.tick;
 | 
				
			||||||
 | 
					            scheduler.handle(system, ev, late);
 | 
				
			||||||
            // this is kinda really jank lol
 | 
					 | 
				
			||||||
            const bus_ptr: ?*anyopaque = switch (ev.kind) {
 | 
					 | 
				
			||||||
                .heat_death => null,
 | 
					 | 
				
			||||||
                .nds7 => system.bus7,
 | 
					 | 
				
			||||||
                .nds9 => system.bus9,
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            scheduler.handle(bus_ptr, ev, late);
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Halted = enum { arm7, arm9, both, none };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					inline fn isHalted(system: System) Halted {
 | 
				
			||||||
 | 
					    const ret = [_]Halted{ .none, .arm7, .arm9, .both };
 | 
				
			||||||
 | 
					    const nds7_bus: *System.Bus7 = @ptrCast(@alignCast(system.arm7tdmi.bus.ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const nds9_halt: u2 = @intFromBool(system.cp15.wait_for_interrupt);
 | 
				
			||||||
 | 
					    const nds7_halt: u2 = @intFromBool(nds7_bus.io.haltcnt == .halt);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return ret[(nds9_halt << 1) | nds7_halt];
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FIXME: Perf win to allocating on the stack instead?
 | 
					// FIXME: Perf win to allocating on the stack instead?
 | 
				
			||||||
pub const SharedContext = struct {
 | 
					pub const SharedCtx = struct {
 | 
				
			||||||
    const MiB = 0x100000;
 | 
					    const MiB = 0x100000;
 | 
				
			||||||
    const KiB = 0x400;
 | 
					    const KiB = 0x400;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const Vram = @import("ppu.zig").Vram;
 | 
					    pub const Io = @import("io.zig").Io;
 | 
				
			||||||
 | 
					    const Vram = @import("ppu/Vram.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    io: *SharedIo,
 | 
					    io: *Io,
 | 
				
			||||||
    main: *[4 * MiB]u8,
 | 
					    main: *[4 * MiB]u8,
 | 
				
			||||||
    wram: *Wram,
 | 
					    wram: *Wram,
 | 
				
			||||||
    vram: *Vram,
 | 
					    vram: *Vram,
 | 
				
			||||||
@@ -112,7 +168,7 @@ pub const SharedContext = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const ctx = .{
 | 
					        const ctx = .{
 | 
				
			||||||
            .io = blk: {
 | 
					            .io = blk: {
 | 
				
			||||||
                const io = try allocator.create(SharedIo);
 | 
					                const io = try allocator.create(Io);
 | 
				
			||||||
                io.* = .{};
 | 
					                io.* = .{};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                break :blk io;
 | 
					                break :blk io;
 | 
				
			||||||
@@ -210,15 +266,12 @@ pub const Wram = struct {
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: Rename
 | 
					    pub fn read(self: @This(), comptime T: type, comptime proc: System.Process, address: u32) T {
 | 
				
			||||||
    const Device = enum { nds9, nds7 };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn read(self: @This(), comptime T: type, comptime dev: Device, address: u32) T {
 | 
					 | 
				
			||||||
        const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
					        const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
				
			||||||
        const masked_addr = address & (addr_space_size - 1);
 | 
					        const masked_addr = address & (addr_space_size - 1);
 | 
				
			||||||
        const page = masked_addr >> bits;
 | 
					        const page = masked_addr >> bits;
 | 
				
			||||||
        const offset = masked_addr & (page_size - 1);
 | 
					        const offset = masked_addr & (page_size - 1);
 | 
				
			||||||
        const table = if (dev == .nds9) self.nds9_table else self.nds7_table;
 | 
					        const table = if (proc == .nds9) self.nds9_table else self.nds7_table;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (table[page]) |some_ptr| {
 | 
					        if (table[page]) |some_ptr| {
 | 
				
			||||||
            const ptr: [*]const T = @ptrCast(@alignCast(some_ptr));
 | 
					            const ptr: [*]const T = @ptrCast(@alignCast(some_ptr));
 | 
				
			||||||
@@ -226,16 +279,16 @@ pub const Wram = struct {
 | 
				
			|||||||
            return ptr[offset / @sizeOf(T)];
 | 
					            return ptr[offset / @sizeOf(T)];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        log.err("{s}: read(T: {}, addr: 0x{X:0>8}) was in un-mapped WRAM space", .{ @tagName(dev), T, 0x0300_0000 + address });
 | 
					        log.err("{s}: read(T: {}, addr: 0x{X:0>8}) was in un-mapped WRAM space", .{ @tagName(proc), T, 0x0300_0000 + address });
 | 
				
			||||||
        return 0x00;
 | 
					        return 0x00;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn write(self: *@This(), comptime T: type, comptime dev: Device, address: u32, value: T) void {
 | 
					    pub fn write(self: *@This(), comptime T: type, comptime proc: System.Process, address: u32, value: T) void {
 | 
				
			||||||
        const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
					        const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
				
			||||||
        const masked_addr = address & (addr_space_size - 1);
 | 
					        const masked_addr = address & (addr_space_size - 1);
 | 
				
			||||||
        const page = masked_addr >> bits;
 | 
					        const page = masked_addr >> bits;
 | 
				
			||||||
        const offset = masked_addr & (page_size - 1);
 | 
					        const offset = masked_addr & (page_size - 1);
 | 
				
			||||||
        const table = if (dev == .nds9) self.nds9_table else self.nds7_table;
 | 
					        const table = if (proc == .nds9) self.nds9_table else self.nds7_table;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (table[page]) |some_ptr| {
 | 
					        if (table[page]) |some_ptr| {
 | 
				
			||||||
            const ptr: [*]T = @ptrCast(@alignCast(some_ptr));
 | 
					            const ptr: [*]T = @ptrCast(@alignCast(some_ptr));
 | 
				
			||||||
@@ -244,7 +297,7 @@ pub const Wram = struct {
 | 
				
			|||||||
            return;
 | 
					            return;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        log.err("{s}: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8}) was in un-mapped WRAM space", .{ @tagName(dev), T, 0x0300_0000 + address, value });
 | 
					        log.err("{s}: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8}) was in un-mapped WRAM space", .{ @tagName(proc), T, 0x0300_0000 + address, value });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -260,6 +313,8 @@ pub const System = struct {
 | 
				
			|||||||
    pub const Arm7tdmi = @import("arm32").Arm7tdmi;
 | 
					    pub const Arm7tdmi = @import("arm32").Arm7tdmi;
 | 
				
			||||||
    pub const Arm946es = @import("arm32").Arm946es;
 | 
					    pub const Arm946es = @import("arm32").Arm946es;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub const Process = enum { nds7, nds9 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    arm7tdmi: *Arm7tdmi,
 | 
					    arm7tdmi: *Arm7tdmi,
 | 
				
			||||||
    arm946es: *Arm946es,
 | 
					    arm946es: *Arm946es,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -272,4 +327,247 @@ pub const System = struct {
 | 
				
			|||||||
        self.bus7.deinit(allocator);
 | 
					        self.bus7.deinit(allocator);
 | 
				
			||||||
        self.bus9.deinit(allocator);
 | 
					        self.bus9.deinit(allocator);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn Cpu(comptime proc: Process) type {
 | 
				
			||||||
 | 
					        return switch (proc) {
 | 
				
			||||||
 | 
					            .nds7 => Arm7tdmi,
 | 
				
			||||||
 | 
					            .nds9 => Arm946es,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn Bus(comptime proc: Process) type {
 | 
				
			||||||
 | 
					        return switch (proc) {
 | 
				
			||||||
 | 
					            .nds7 => Bus7,
 | 
				
			||||||
 | 
					            .nds9 => Bus9,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn handleInterrupt(comptime proc: System.Process, cpu: *System.Cpu(proc)) void {
 | 
				
			||||||
 | 
					    const bus_ptr: *System.Bus(proc) = @ptrCast(@alignCast(cpu.bus.ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!bus_ptr.io.ime or cpu.cpsr.i.read()) return; // ensure irqs are enabled
 | 
				
			||||||
 | 
					    if ((bus_ptr.io.ie.raw & bus_ptr.io.irq.raw) == 0) return; // ensure there is an irq to handle
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch (proc) {
 | 
				
			||||||
 | 
					        .nds9 => {
 | 
				
			||||||
 | 
					            const cp15: *System.Cp15 = @ptrCast(@alignCast(cpu.cp15.ptr));
 | 
				
			||||||
 | 
					            cp15.wait_for_interrupt = false;
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .nds7 => bus_ptr.io.haltcnt = .execute,
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const ret_addr = cpu.r[15] - if (cpu.cpsr.t.read()) 0 else @as(u32, 4);
 | 
				
			||||||
 | 
					    const spsr = cpu.cpsr;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cpu.changeMode(.Irq);
 | 
				
			||||||
 | 
					    cpu.cpsr.t.unset();
 | 
				
			||||||
 | 
					    cpu.cpsr.i.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    cpu.r[14] = ret_addr;
 | 
				
			||||||
 | 
					    cpu.spsr.raw = spsr.raw;
 | 
				
			||||||
 | 
					    cpu.r[15] = if (proc == .nds9) 0xFFFF_0018 else 0x0000_0018;
 | 
				
			||||||
 | 
					    cpu.pipe.reload(cpu);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn fastBoot(system: System) void {
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        const Bank = System.Arm946es.Bank;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const cpu = system.arm946es;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // from advanDS
 | 
				
			||||||
 | 
					        cpu.spsr = .{ .raw = 0x0000_000DF };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @memset(cpu.r[0..12], 0x0000_0000); // r0 -> r11 are zeroed
 | 
				
			||||||
 | 
					        // TODO: r12, r14, and r15 are set to the entrypoint?
 | 
				
			||||||
 | 
					        cpu.r[13] = 0x0300_2F7C; // FIXME: Why is there (!) in GBATEK?
 | 
				
			||||||
 | 
					        cpu.bank.r[Bank.regIdx(.Irq, .R13)] = 0x0300_3F80;
 | 
				
			||||||
 | 
					        cpu.bank.r[Bank.regIdx(.Supervisor, .R13)] = 0x0300_3FC0;
 | 
				
			||||||
 | 
					        cpu.bank.spsr[Bank.spsrIdx(.Irq)] = .{ .raw = 0x0000_0000 };
 | 
				
			||||||
 | 
					        cpu.bank.spsr[Bank.spsrIdx(.Supervisor)] = .{ .raw = 0x0000_0000 };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        const Bank = System.Arm7tdmi.Bank;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const cpu = system.arm7tdmi;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // from advanDS
 | 
				
			||||||
 | 
					        cpu.spsr = .{ .raw = 0x0000_000D3 };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        @memset(cpu.r[0..12], 0x0000_0000); // r0 -> r11 are zeroed
 | 
				
			||||||
 | 
					        // TODO: r12, r14, and r15 are set to the entrypoint?
 | 
				
			||||||
 | 
					        cpu.r[13] = 0x0380_FD80;
 | 
				
			||||||
 | 
					        cpu.bank.r[Bank.regIdx(.Irq, .R13)] = 0x0380_FF80;
 | 
				
			||||||
 | 
					        cpu.bank.r[Bank.regIdx(.Supervisor, .R13)] = 0x0380_FFC0;
 | 
				
			||||||
 | 
					        cpu.bank.spsr[Bank.spsrIdx(.Irq)] = .{ .raw = 0x0000_0000 };
 | 
				
			||||||
 | 
					        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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										209
									
								
								src/core/io.zig
									
									
									
									
									
								
							
							
						
						
									
										209
									
								
								src/core/io.zig
									
									
									
									
									
								
							@@ -2,20 +2,19 @@ const std = @import("std");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const Bitfield = @import("bitfield").Bitfield;
 | 
					const Bitfield = @import("bitfield").Bitfield;
 | 
				
			||||||
const Bit = @import("bitfield").Bit;
 | 
					const Bit = @import("bitfield").Bit;
 | 
				
			||||||
 | 
					const System = @import("emu.zig").System;
 | 
				
			||||||
 | 
					const handleInterrupt = @import("emu.zig").handleInterrupt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const log = std.log.scoped(.shared_io);
 | 
					const log = std.log.scoped(.shared_io);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// FIXME: This whole thing is bad bad bad bad bad
 | 
					 | 
				
			||||||
// I think only the IPC stuff needs to be here, since they talk to each other.
 | 
					 | 
				
			||||||
// every other "shared I/O register" is just duplicated on both CPUs. So they shouldn't be here
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub const Io = struct {
 | 
					pub const Io = struct {
 | 
				
			||||||
    /// Inter Process Communication FIFO
 | 
					    /// Inter Process Communication FIFO
 | 
				
			||||||
    ipc_fifo: IpcFifo = .{},
 | 
					    ipc: Ipc = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    wramcnt: WramCnt = .{ .raw = 0x00 },
 | 
					    wramcnt: WramCnt = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: DS Cartridge I/O Ports
 | 
					    // Read Only
 | 
				
			||||||
 | 
					    input: Input = .{},
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
fn warn(comptime format: []const u8, args: anytype) u0 {
 | 
					fn warn(comptime format: []const u8, args: anytype) u0 {
 | 
				
			||||||
@@ -23,14 +22,19 @@ fn warn(comptime format: []const u8, args: anytype) u0 {
 | 
				
			|||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const IpcFifo = struct {
 | 
					/// Inter-Process Communication
 | 
				
			||||||
 | 
					const Ipc = struct {
 | 
				
			||||||
    const Sync = IpcSync;
 | 
					    const Sync = IpcSync;
 | 
				
			||||||
    const Control = IpcFifoCnt;
 | 
					    const Control = IpcFifoCnt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    _nds7: Impl = .{},
 | 
					    _nds7: Impl = .{},
 | 
				
			||||||
    _nds9: Impl = .{},
 | 
					    _nds9: Impl = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const Source = enum { nds7, nds9 };
 | 
					    // we need access to the CPUs to handle IPC IRQs
 | 
				
			||||||
 | 
					    arm7tdmi: ?*System.Arm7tdmi = null,
 | 
				
			||||||
 | 
					    arm946es: ?*System.Arm946es = null,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // TODO: DS Cartridge I/O Ports
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const Impl = struct {
 | 
					    const Impl = struct {
 | 
				
			||||||
        /// IPC Synchronize
 | 
					        /// IPC Synchronize
 | 
				
			||||||
@@ -47,36 +51,55 @@ const IpcFifo = struct {
 | 
				
			|||||||
        last_read: ?u32 = null,
 | 
					        last_read: ?u32 = null,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn configure(self: *@This(), system: System) void {
 | 
				
			||||||
 | 
					        self.arm7tdmi = system.arm7tdmi;
 | 
				
			||||||
 | 
					        self.arm946es = system.arm946es;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// IPCSYNC
 | 
					    /// IPCSYNC
 | 
				
			||||||
    /// Read/Write
 | 
					    /// Read/Write
 | 
				
			||||||
    pub fn setIpcSync(self: *@This(), comptime src: Source, value: anytype) void {
 | 
					    pub fn setIpcSync(self: *@This(), comptime proc: System.Process, value: anytype) void {
 | 
				
			||||||
        switch (src) {
 | 
					        switch (proc) {
 | 
				
			||||||
            .nds7 => {
 | 
					            .nds7 => {
 | 
				
			||||||
                self._nds7.sync.raw = masks.ipcFifoSync(self._nds7.sync.raw, value);
 | 
					                self._nds7.sync.raw = masks.ipcFifoSync(self._nds7.sync.raw, value);
 | 
				
			||||||
                self._nds9.sync.raw = masks.mask(self._nds9.sync.raw, (self._nds7.sync.raw >> 8) & 0xF, 0xF);
 | 
					                self._nds9.sync.raw = masks.mask(self._nds9.sync.raw, (self._nds7.sync.raw >> 8) & 0xF, 0xF);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (value >> 13 & 1 == 1 and self._nds9.sync.recv_irq.read()) {
 | 
				
			||||||
 | 
					                    const bus: *System.Bus9 = @ptrCast(@alignCast(self.arm946es.?.bus.ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    bus.io.irq.ipcsync.set();
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds9, self.arm946es.?);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (value >> 3 & 1 == 1) {
 | 
					                if (value >> 3 & 1 == 1) {
 | 
				
			||||||
                    self._nds7.fifo.reset();
 | 
					                    self._nds7.fifo.reset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    self._nds7.cnt.send_fifo_empty.write(true);
 | 
					                    self._nds7.cnt.send_fifo_empty.set();
 | 
				
			||||||
                    self._nds9.cnt.recv_fifo_empty.write(true);
 | 
					                    self._nds9.cnt.recv_fifo_empty.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    self._nds7.cnt.send_fifo_full.write(false);
 | 
					                    self._nds7.cnt.send_fifo_full.unset();
 | 
				
			||||||
                    self._nds9.cnt.recv_fifo_full.write(false);
 | 
					                    self._nds9.cnt.recv_fifo_full.unset();
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            .nds9 => {
 | 
					            .nds9 => {
 | 
				
			||||||
                self._nds9.sync.raw = masks.ipcFifoSync(self._nds9.sync.raw, value);
 | 
					                self._nds9.sync.raw = masks.ipcFifoSync(self._nds9.sync.raw, value);
 | 
				
			||||||
                self._nds7.sync.raw = masks.mask(self._nds7.sync.raw, (self._nds9.sync.raw >> 8) & 0xF, 0xF);
 | 
					                self._nds7.sync.raw = masks.mask(self._nds7.sync.raw, (self._nds9.sync.raw >> 8) & 0xF, 0xF);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (value >> 13 & 1 == 1 and self._nds7.sync.recv_irq.read()) {
 | 
				
			||||||
 | 
					                    const bus: *System.Bus7 = @ptrCast(@alignCast(self.arm7tdmi.?.bus.ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    bus.io.irq.ipcsync.set();
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds7, self.arm7tdmi.?);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (value >> 3 & 1 == 1) {
 | 
					                if (value >> 3 & 1 == 1) {
 | 
				
			||||||
                    self._nds9.fifo.reset();
 | 
					                    self._nds9.fifo.reset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    self._nds9.cnt.send_fifo_empty.write(true);
 | 
					                    self._nds9.cnt.send_fifo_empty.set();
 | 
				
			||||||
                    self._nds7.cnt.recv_fifo_empty.write(true);
 | 
					                    self._nds7.cnt.recv_fifo_empty.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    self._nds9.cnt.send_fifo_full.write(false);
 | 
					                    self._nds9.cnt.send_fifo_full.unset();
 | 
				
			||||||
                    self._nds7.cnt.recv_fifo_full.write(false);
 | 
					                    self._nds7.cnt.recv_fifo_full.unset();
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -84,8 +107,8 @@ const IpcFifo = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /// IPCFIFOCNT
 | 
					    /// IPCFIFOCNT
 | 
				
			||||||
    /// Read/Write
 | 
					    /// Read/Write
 | 
				
			||||||
    pub fn setIpcFifoCnt(self: *@This(), comptime src: Source, value: anytype) void {
 | 
					    pub fn setIpcFifoCnt(self: *@This(), comptime proc: System.Process, value: anytype) void {
 | 
				
			||||||
        switch (src) {
 | 
					        switch (proc) {
 | 
				
			||||||
            .nds7 => self._nds7.cnt.raw = masks.ipcFifoCnt(self._nds7.cnt.raw, value),
 | 
					            .nds7 => self._nds7.cnt.raw = masks.ipcFifoCnt(self._nds7.cnt.raw, value),
 | 
				
			||||||
            .nds9 => self._nds9.cnt.raw = masks.ipcFifoCnt(self._nds9.cnt.raw, value),
 | 
					            .nds9 => self._nds9.cnt.raw = masks.ipcFifoCnt(self._nds9.cnt.raw, value),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -93,11 +116,13 @@ const IpcFifo = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    /// IPC Send FIFO
 | 
					    /// IPC Send FIFO
 | 
				
			||||||
    /// Write-Only
 | 
					    /// Write-Only
 | 
				
			||||||
    pub fn send(self: *@This(), comptime src: Source, value: u32) !void {
 | 
					    pub fn send(self: *@This(), comptime proc: System.Process, value: u32) void {
 | 
				
			||||||
        switch (src) {
 | 
					        switch (proc) {
 | 
				
			||||||
            .nds7 => {
 | 
					            .nds7 => {
 | 
				
			||||||
                if (!self._nds7.cnt.enable_fifos.read()) return;
 | 
					                if (!self._nds7.cnt.enable_fifos.read()) return;
 | 
				
			||||||
                try self._nds7.fifo.push(value);
 | 
					                self._nds7.fifo.push(value) catch unreachable; // see early return above
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const not_empty_cache = !self._nds9.cnt.recv_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // update status bits
 | 
					                // update status bits
 | 
				
			||||||
                self._nds7.cnt.send_fifo_empty.write(self._nds7.fifo._len() == 0);
 | 
					                self._nds7.cnt.send_fifo_empty.write(self._nds7.fifo._len() == 0);
 | 
				
			||||||
@@ -105,10 +130,23 @@ const IpcFifo = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                self._nds7.cnt.send_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
					                self._nds7.cnt.send_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
				
			||||||
                self._nds9.cnt.recv_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
					                self._nds9.cnt.recv_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const not_empty = !self._nds9.cnt.recv_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (self._nds9.cnt.recv_fifo_irq_enable.read() and !not_empty_cache and not_empty) {
 | 
				
			||||||
 | 
					                    // NDS7 Send | NDS9 RECV (Handling Not Empty)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    const bus: *System.Bus9 = @ptrCast(@alignCast(self.arm946es.?.bus.ptr));
 | 
				
			||||||
 | 
					                    bus.io.irq.ipc_recv_not_empty.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds9, self.arm946es.?);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            .nds9 => {
 | 
					            .nds9 => {
 | 
				
			||||||
                if (!self._nds9.cnt.enable_fifos.read()) return;
 | 
					                if (!self._nds9.cnt.enable_fifos.read()) return;
 | 
				
			||||||
                try self._nds9.fifo.push(value);
 | 
					                self._nds9.fifo.push(value) catch unreachable; // see early return above
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const not_empty_cache = !self._nds7.cnt.recv_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // update status bits
 | 
					                // update status bits
 | 
				
			||||||
                self._nds9.cnt.send_fifo_empty.write(self._nds9.fifo._len() == 0);
 | 
					                self._nds9.cnt.send_fifo_empty.write(self._nds9.fifo._len() == 0);
 | 
				
			||||||
@@ -116,14 +154,25 @@ const IpcFifo = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                self._nds9.cnt.send_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
					                self._nds9.cnt.send_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
				
			||||||
                self._nds7.cnt.recv_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
					                self._nds7.cnt.recv_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const not_empty = !self._nds7.cnt.recv_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (self._nds7.cnt.recv_fifo_irq_enable.read() and !not_empty_cache and not_empty) {
 | 
				
			||||||
 | 
					                    // NDS9 Send | NDS7 RECV (Handling Not Empty)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    const bus: *System.Bus7 = @ptrCast(@alignCast(self.arm7tdmi.?.bus.ptr));
 | 
				
			||||||
 | 
					                    bus.io.irq.ipc_recv_not_empty.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds7, self.arm7tdmi.?);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// IPC Receive FIFO
 | 
					    /// IPC Receive FIFO
 | 
				
			||||||
    /// Read-Only
 | 
					    /// Read-Only
 | 
				
			||||||
    pub fn recv(self: *@This(), comptime src: Source) u32 {
 | 
					    pub fn recv(self: *@This(), comptime proc: System.Process) u32 {
 | 
				
			||||||
        switch (src) {
 | 
					        switch (proc) {
 | 
				
			||||||
            .nds7 => {
 | 
					            .nds7 => {
 | 
				
			||||||
                const enabled = self._nds7.cnt.enable_fifos.read();
 | 
					                const enabled = self._nds7.cnt.enable_fifos.read();
 | 
				
			||||||
                const val_opt = if (enabled) self._nds9.fifo.pop() else self._nds9.fifo.peek();
 | 
					                const val_opt = if (enabled) self._nds9.fifo.pop() else self._nds9.fifo.peek();
 | 
				
			||||||
@@ -136,6 +185,8 @@ const IpcFifo = struct {
 | 
				
			|||||||
                    break :blk self._nds7.last_read orelse 0x0000_0000;
 | 
					                    break :blk self._nds7.last_read orelse 0x0000_0000;
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const empty_cache = self._nds9.cnt.send_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // update status bits
 | 
					                // update status bits
 | 
				
			||||||
                self._nds7.cnt.recv_fifo_empty.write(self._nds9.fifo._len() == 0);
 | 
					                self._nds7.cnt.recv_fifo_empty.write(self._nds9.fifo._len() == 0);
 | 
				
			||||||
                self._nds9.cnt.send_fifo_empty.write(self._nds9.fifo._len() == 0);
 | 
					                self._nds9.cnt.send_fifo_empty.write(self._nds9.fifo._len() == 0);
 | 
				
			||||||
@@ -143,6 +194,15 @@ const IpcFifo = struct {
 | 
				
			|||||||
                self._nds7.cnt.recv_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
					                self._nds7.cnt.recv_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
				
			||||||
                self._nds9.cnt.send_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
					                self._nds9.cnt.send_fifo_full.write(self._nds9.fifo._len() == 0x10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const empty = self._nds9.cnt.send_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (self._nds9.cnt.send_fifo_irq_enable.read() and (!empty_cache and empty)) {
 | 
				
			||||||
 | 
					                    const bus: *System.Bus9 = @ptrCast(@alignCast(self.arm946es.?.bus.ptr));
 | 
				
			||||||
 | 
					                    bus.io.irq.ipc_send_empty.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds9, self.arm946es.?);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return value;
 | 
					                return value;
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            .nds9 => {
 | 
					            .nds9 => {
 | 
				
			||||||
@@ -157,6 +217,8 @@ const IpcFifo = struct {
 | 
				
			|||||||
                    break :blk self._nds7.last_read orelse 0x0000_0000;
 | 
					                    break :blk self._nds7.last_read orelse 0x0000_0000;
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const empty_cache = self._nds7.cnt.send_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                // update status bits
 | 
					                // update status bits
 | 
				
			||||||
                self._nds9.cnt.recv_fifo_empty.write(self._nds7.fifo._len() == 0);
 | 
					                self._nds9.cnt.recv_fifo_empty.write(self._nds7.fifo._len() == 0);
 | 
				
			||||||
                self._nds7.cnt.send_fifo_empty.write(self._nds7.fifo._len() == 0);
 | 
					                self._nds7.cnt.send_fifo_empty.write(self._nds7.fifo._len() == 0);
 | 
				
			||||||
@@ -164,6 +226,15 @@ const IpcFifo = struct {
 | 
				
			|||||||
                self._nds9.cnt.recv_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
					                self._nds9.cnt.recv_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
				
			||||||
                self._nds7.cnt.send_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
					                self._nds7.cnt.send_fifo_full.write(self._nds7.fifo._len() == 0x10);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const empty = self._nds7.cnt.send_fifo_empty.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (self._nds7.cnt.send_fifo_irq_enable.read() and (!empty_cache and empty)) {
 | 
				
			||||||
 | 
					                    const bus: *System.Bus7 = @ptrCast(@alignCast(self.arm7tdmi.?.bus.ptr));
 | 
				
			||||||
 | 
					                    bus.io.irq.ipc_send_empty.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds7, self.arm7tdmi.?);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                return value;
 | 
					                return value;
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
@@ -237,7 +308,6 @@ pub const masks = struct {
 | 
				
			|||||||
        const err_mask: u32 = 0x4000; // bit 14
 | 
					        const err_mask: u32 = 0x4000; // bit 14
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const err_bit = (cnt & err_mask) & ~(value & err_mask);
 | 
					        const err_bit = (cnt & err_mask) & ~(value & err_mask);
 | 
				
			||||||
        if (value & 0b1000 != 0) log.err("TODO: handle IPCFIFOCNT.3", .{});
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const without_err = (@as(u32, value) & _mask) | (cnt & ~_mask);
 | 
					        const without_err = (@as(u32, value) & _mask) | (cnt & ~_mask);
 | 
				
			||||||
        return (without_err & ~err_mask) | err_bit;
 | 
					        return (without_err & ~err_mask) | err_bit;
 | 
				
			||||||
@@ -249,7 +319,22 @@ pub const masks = struct {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub const PostFlag = enum(u1) { in_progress = 0, completed };
 | 
					// FIXME: bitfields depends on NDS9 / NDS7
 | 
				
			||||||
 | 
					pub const IntEnable = extern union {
 | 
				
			||||||
 | 
					    vblank: Bit(u32, 0),
 | 
				
			||||||
 | 
					    hblank: Bit(u32, 1),
 | 
				
			||||||
 | 
					    coincidence: Bit(u32, 2),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    dma0: Bit(u32, 8),
 | 
				
			||||||
 | 
					    dma1: Bit(u32, 9),
 | 
				
			||||||
 | 
					    dma2: Bit(u32, 10),
 | 
				
			||||||
 | 
					    dma3: Bit(u32, 11),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ipcsync: Bit(u32, 16),
 | 
				
			||||||
 | 
					    ipc_send_empty: Bit(u32, 17),
 | 
				
			||||||
 | 
					    ipc_recv_not_empty: Bit(u32, 18),
 | 
				
			||||||
 | 
					    raw: u32,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Fifo = struct {
 | 
					const Fifo = struct {
 | 
				
			||||||
    const Index = u8;
 | 
					    const Index = u8;
 | 
				
			||||||
@@ -311,3 +396,71 @@ const Fifo = struct {
 | 
				
			|||||||
        return idx & _mask;
 | 
					        return idx & _mask;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Read Only
 | 
				
			||||||
 | 
					/// 0 = Pressed, 1 = Released
 | 
				
			||||||
 | 
					pub const KeyInput = extern union {
 | 
				
			||||||
 | 
					    a: Bit(u16, 0),
 | 
				
			||||||
 | 
					    b: Bit(u16, 1),
 | 
				
			||||||
 | 
					    select: Bit(u16, 2),
 | 
				
			||||||
 | 
					    start: Bit(u16, 3),
 | 
				
			||||||
 | 
					    right: Bit(u16, 4),
 | 
				
			||||||
 | 
					    left: Bit(u16, 5),
 | 
				
			||||||
 | 
					    up: Bit(u16, 6),
 | 
				
			||||||
 | 
					    down: Bit(u16, 7),
 | 
				
			||||||
 | 
					    shoulder_r: Bit(u16, 8),
 | 
				
			||||||
 | 
					    shoulder_l: Bit(u16, 9),
 | 
				
			||||||
 | 
					    raw: u16,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					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,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Input = struct {
 | 
				
			||||||
 | 
					    const AtomicOrder = std.builtin.AtomicOrder;
 | 
				
			||||||
 | 
					    const AtomicRmwOp = std.builtin.AtomicRmwOp;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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 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 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);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										46
									
								
								src/core/nds7/Bios.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								src/core/nds7/Bios.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const log = std.log.scoped(.nds7_bios);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const KiB = 0x400;
 | 
				
			||||||
 | 
					const len = 16 * KiB;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					buf: ?*align(4) [len]u8 = null,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FIXME: Currently we dupe here, should we just take ownership?
 | 
				
			||||||
 | 
					pub fn load(self: *@This(), allocator: Allocator, data: []u8) !void {
 | 
				
			||||||
 | 
					    if (data.len != len) {
 | 
				
			||||||
 | 
					        const acutal_size = @as(f32, @floatFromInt(data.len)) / KiB;
 | 
				
			||||||
 | 
					        log.warn("BIOS was {d:.2} KiB (should be {} KiB)", .{ acutal_size, len });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const buf = try allocator.alignedAlloc(u8, 4, len);
 | 
				
			||||||
 | 
					    @memset(buf, 0);
 | 
				
			||||||
 | 
					    @memcpy(buf[0..data.len], data);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.* = .{ .buf = buf[0..len] };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
				
			||||||
 | 
					    if (self.buf) |ptr| allocator.destroy(ptr);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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 byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // if (address >= len) return 0x0000_0000; // TODO: What is undefined actually?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const ptr = self.buf orelse {
 | 
				
			||||||
 | 
					        log.err("read(T: {}, address: 0x{X:0>8}) from BIOS but none was found!", .{ T, address });
 | 
				
			||||||
 | 
					        @panic("TODO: ability to load in NDS7 BIOS just-in-time");
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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 {
 | 
				
			||||||
 | 
					    log.err("write(T: {}, address: 0x{X:0>8}, value: 0x{X:}) but we're in the BIOS!", .{ T, address, value });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -2,12 +2,14 @@ const std = @import("std");
 | 
				
			|||||||
const io = @import("io.zig");
 | 
					const io = @import("io.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Scheduler = @import("../Scheduler.zig");
 | 
					const Scheduler = @import("../Scheduler.zig");
 | 
				
			||||||
const SharedIo = @import("../io.zig").Io;
 | 
					const SharedCtx = @import("../emu.zig").SharedCtx;
 | 
				
			||||||
const SharedContext = @import("../emu.zig").SharedContext;
 | 
					 | 
				
			||||||
const Wram = @import("../emu.zig").Wram;
 | 
					const Wram = @import("../emu.zig").Wram;
 | 
				
			||||||
const Vram = @import("../ppu.zig").Vram;
 | 
					const Vram = @import("../ppu/Vram.zig");
 | 
				
			||||||
 | 
					const Bios = @import("Bios.zig");
 | 
				
			||||||
const forceAlign = @import("../emu.zig").forceAlign;
 | 
					const forceAlign = @import("../emu.zig").forceAlign;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Controllers = @import("dma.zig").Controllers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Allocator = std.mem.Allocator;
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Mode = enum { normal, debug };
 | 
					const Mode = enum { normal, debug };
 | 
				
			||||||
@@ -18,28 +20,34 @@ const log = std.log.scoped(.nds7_bus);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
scheduler: *Scheduler,
 | 
					scheduler: *Scheduler,
 | 
				
			||||||
main: *[4 * MiB]u8,
 | 
					main: *[4 * MiB]u8,
 | 
				
			||||||
wram_shr: *Wram,
 | 
					shr_wram: *Wram,
 | 
				
			||||||
wram: *[64 * KiB]u8,
 | 
					wram: *[64 * KiB]u8,
 | 
				
			||||||
vram: *Vram,
 | 
					vram: *Vram,
 | 
				
			||||||
io: io.Io,
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedContext) !@This() {
 | 
					dma: Controllers = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					io: io.Io,
 | 
				
			||||||
 | 
					bios: Bios,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn init(allocator: Allocator, scheduler: *Scheduler, ctx: SharedCtx) !@This() {
 | 
				
			||||||
    const wram = try allocator.create([64 * KiB]u8);
 | 
					    const wram = try allocator.create([64 * KiB]u8);
 | 
				
			||||||
    errdefer allocator.destroy(wram);
 | 
					 | 
				
			||||||
    @memset(wram, 0);
 | 
					    @memset(wram, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return .{
 | 
					    return .{
 | 
				
			||||||
        .main = shared_ctx.main,
 | 
					        .main = ctx.main,
 | 
				
			||||||
        .wram_shr = shared_ctx.wram,
 | 
					        .shr_wram = ctx.wram,
 | 
				
			||||||
        .vram = shared_ctx.vram,
 | 
					        .vram = ctx.vram,
 | 
				
			||||||
        .wram = wram,
 | 
					        .wram = wram,
 | 
				
			||||||
        .scheduler = scheduler,
 | 
					        .scheduler = scheduler,
 | 
				
			||||||
        .io = io.Io.init(shared_ctx.io),
 | 
					        .io = io.Io.init(ctx.io),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        .bios = .{},
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn deinit(self: *@This(), allocator: Allocator) void {
 | 
					pub fn deinit(self: *@This(), allocator: Allocator) void {
 | 
				
			||||||
    allocator.destroy(self.wram);
 | 
					    allocator.destroy(self.wram);
 | 
				
			||||||
 | 
					    self.bios.deinit(allocator);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn reset(_: *@This()) void {}
 | 
					pub fn reset(_: *@This()) void {}
 | 
				
			||||||
@@ -54,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 {
 | 
					fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T {
 | 
				
			||||||
    const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
					    const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
				
			||||||
    const readInt = std.mem.readIntLittle;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const aligned_addr = forceAlign(T, address);
 | 
					    const aligned_addr = forceAlign(T, address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (mode) {
 | 
					    switch (mode) {
 | 
				
			||||||
@@ -65,15 +71,17 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return switch (aligned_addr) {
 | 
					    return switch (aligned_addr) {
 | 
				
			||||||
        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),
 | 
				
			||||||
        0x0300_0000...0x037F_FFFF => switch (self.io.shared.wramcnt.mode.read()) {
 | 
					        0x0200_0000...0x02FF_FFFF => std.mem.readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], .little),
 | 
				
			||||||
            0b00 => readInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count]),
 | 
					        0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) {
 | 
				
			||||||
            else => self.wram_shr.read(T, .nds7, aligned_addr),
 | 
					            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...0x0380_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),
 | 
					        0x0400_0000...0x04FF_FFFF => io.read(self, T, aligned_addr),
 | 
				
			||||||
        0x0600_0000...0x06FF_FFFF => self.vram.read(T, .nds7, aligned_addr),
 | 
					        0x0600_0000...0x06FF_FFFF => self.vram.read(T, .nds7, aligned_addr),
 | 
				
			||||||
        else => warn("unexpected read: 0x{x:0>8} -> {}", .{ aligned_addr, T }),
 | 
					
 | 
				
			||||||
 | 
					        else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -87,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 {
 | 
					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 byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
				
			||||||
    const writeInt = std.mem.writeIntLittle;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const aligned_addr = forceAlign(T, address);
 | 
					    const aligned_addr = forceAlign(T, address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -98,15 +105,16 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (aligned_addr) {
 | 
					    switch (aligned_addr) {
 | 
				
			||||||
        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),
 | 
				
			||||||
        0x0300_0000...0x037F_FFFF => switch (self.io.shared.wramcnt.mode.read()) {
 | 
					        0x0200_0000...0x02FF_FFFF => std.mem.writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][0..byte_count], value, .little),
 | 
				
			||||||
            0b00 => writeInt(T, self.wram[aligned_addr & 0x0000_FFFF ..][0..byte_count], value),
 | 
					        0x0300_0000...0x037F_FFFF => switch (self.io.shr.wramcnt.mode.read()) {
 | 
				
			||||||
            else => self.wram_shr.write(T, .nds7, aligned_addr, 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...0x0380_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),
 | 
					        0x0400_0000...0x04FF_FFFF => io.write(self, T, aligned_addr, value),
 | 
				
			||||||
        0x0600_0000...0x06FF_FFFF => self.vram.write(T, .nds7, aligned_addr, value),
 | 
					        0x0600_0000...0x06FF_FFFF => self.vram.write(T, .nds7, aligned_addr, value),
 | 
				
			||||||
        else => log.warn("unexpected write: 0x{X:}{} -> 0x{X:0>8}", .{ value, T, aligned_addr }),
 | 
					        else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										401
									
								
								src/core/nds7/dma.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										401
									
								
								src/core/nds7/dma.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,401 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const System = @import("../emu.zig").System;
 | 
				
			||||||
 | 
					const DmaCnt = @import("io.zig").DmaCnt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const rotr = std.math.rotr;
 | 
				
			||||||
 | 
					const shift = @import("../../util.zig").shift;
 | 
				
			||||||
 | 
					const subset = @import("../../util.zig").subset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const handleInterrupt = @import("../emu.zig").handleInterrupt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const log = std.log.scoped(.nds7_dma_transfer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const Controllers = struct {
 | 
				
			||||||
 | 
					    Controller(0) = Controller(0){},
 | 
				
			||||||
 | 
					    Controller(1) = Controller(1){},
 | 
				
			||||||
 | 
					    Controller(2) = Controller(2){},
 | 
				
			||||||
 | 
					    Controller(3) = Controller(3){},
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn read(comptime T: type, dma: *const Controllers, addr: u32) ?T {
 | 
				
			||||||
 | 
					    const byte_addr: u8 = @truncate(addr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return switch (T) {
 | 
				
			||||||
 | 
					        u32 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB4 => null, // DMA0SAD, DMA0DAD,
 | 
				
			||||||
 | 
					            0xB8 => @as(T, dma.*[0].dmacntH()) << 16, // DMA0CNT_L is write-only
 | 
				
			||||||
 | 
					            0xBC, 0xC0 => null, // DMA1SAD, DMA1DAD
 | 
				
			||||||
 | 
					            0xC4 => @as(T, dma.*[1].dmacntH()) << 16, // DMA1CNT_L is write-only
 | 
				
			||||||
 | 
					            0xC8, 0xCC => null, // DMA2SAD, DMA2DAD
 | 
				
			||||||
 | 
					            0xD0 => @as(T, dma.*[2].dmacntH()) << 16, // DMA2CNT_L is write-only
 | 
				
			||||||
 | 
					            0xD4, 0xD8 => null, // DMA3SAD, DMA3DAD
 | 
				
			||||||
 | 
					            0xDC => @as(T, dma.*[3].dmacntH()) << 16, // DMA3CNT_L is write-only
 | 
				
			||||||
 | 
					            else => warn("unaligned {} read from 0x{X:0>8}", .{ T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u16 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB2, 0xB4, 0xB6 => null, // DMA0SAD, DMA0DAD
 | 
				
			||||||
 | 
					            0xB8 => 0x0000, // DMA0CNT_L, suite.gba expects 0x0000 instead of 0xDEAD
 | 
				
			||||||
 | 
					            0xBA => dma.*[0].dmacntH(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC, 0xBE, 0xC0, 0xC2 => null, // DMA1SAD, DMA1DAD
 | 
				
			||||||
 | 
					            0xC4 => 0x0000, // DMA1CNT_L
 | 
				
			||||||
 | 
					            0xC6 => dma.*[1].dmacntH(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8, 0xCA, 0xCC, 0xCE => null, // DMA2SAD, DMA2DAD
 | 
				
			||||||
 | 
					            0xD0 => 0x0000, // DMA2CNT_L
 | 
				
			||||||
 | 
					            0xD2 => dma.*[2].dmacntH(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4, 0xD6, 0xD8, 0xDA => null, // DMA3SAD, DMA3DAD
 | 
				
			||||||
 | 
					            0xDC => 0x0000, // DMA3CNT_L
 | 
				
			||||||
 | 
					            0xDE => dma.*[3].dmacntH(),
 | 
				
			||||||
 | 
					            else => warn("unaligned {} read from 0x{X:0>8}", .{ T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u8 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0...0xB7 => null, // DMA0SAD, DMA0DAD
 | 
				
			||||||
 | 
					            0xB8, 0xB9 => 0x00, // DMA0CNT_L
 | 
				
			||||||
 | 
					            0xBA, 0xBB => @truncate(dma.*[0].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC...0xC3 => null, // DMA1SAD, DMA1DAD
 | 
				
			||||||
 | 
					            0xC4, 0xC5 => 0x00, // DMA1CNT_L
 | 
				
			||||||
 | 
					            0xC6, 0xC7 => @truncate(dma.*[1].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8...0xCF => null, // DMA2SAD, DMA2DAD
 | 
				
			||||||
 | 
					            0xD0, 0xD1 => 0x00, // DMA2CNT_L
 | 
				
			||||||
 | 
					            0xD2, 0xD3 => @truncate(dma.*[2].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4...0xDB => null, // DMA3SAD, DMA3DAD
 | 
				
			||||||
 | 
					            0xDC, 0xDD => 0x00, // DMA3CNT_L
 | 
				
			||||||
 | 
					            0xDE, 0xDF => @truncate(dma.*[3].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					            else => warn("unexpected {} read from 0x{X:0>8}", .{ T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        else => @compileError("DMA: Unsupported read width"),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn write(comptime T: type, dma: *Controllers, addr: u32, value: T) void {
 | 
				
			||||||
 | 
					    const byte_addr: u8 = @truncate(addr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch (T) {
 | 
				
			||||||
 | 
					        u32 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0 => dma.*[0].setDmasad(value),
 | 
				
			||||||
 | 
					            0xB4 => dma.*[0].setDmadad(value),
 | 
				
			||||||
 | 
					            0xB8 => dma.*[0].setDmacnt(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC => dma.*[1].setDmasad(value),
 | 
				
			||||||
 | 
					            0xC0 => dma.*[1].setDmadad(value),
 | 
				
			||||||
 | 
					            0xC4 => dma.*[1].setDmacnt(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8 => dma.*[2].setDmasad(value),
 | 
				
			||||||
 | 
					            0xCC => dma.*[2].setDmadad(value),
 | 
				
			||||||
 | 
					            0xD0 => dma.*[2].setDmacnt(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4 => dma.*[3].setDmasad(value),
 | 
				
			||||||
 | 
					            0xD8 => dma.*[3].setDmadad(value),
 | 
				
			||||||
 | 
					            0xDC => dma.*[3].setDmacnt(value),
 | 
				
			||||||
 | 
					            else => log.warn("Tried to write 0x{X:0>8}{} to 0x{X:0>8}", .{ value, T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u16 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB2 => dma.*[0].setDmasad(subset(u32, u16, byte_addr, dma.*[0].sad, value)),
 | 
				
			||||||
 | 
					            0xB4, 0xB6 => dma.*[0].setDmadad(subset(u32, u16, byte_addr, dma.*[0].dad, value)),
 | 
				
			||||||
 | 
					            0xB8 => dma.*[0].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xBA => dma.*[0].setDmacntH(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC, 0xBE => dma.*[1].setDmasad(subset(u32, u16, byte_addr, dma.*[1].sad, value)),
 | 
				
			||||||
 | 
					            0xC0, 0xC2 => dma.*[1].setDmadad(subset(u32, u16, byte_addr, dma.*[1].dad, value)),
 | 
				
			||||||
 | 
					            0xC4 => dma.*[1].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xC6 => dma.*[1].setDmacntH(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8, 0xCA => dma.*[2].setDmasad(subset(u32, u16, byte_addr, dma.*[2].sad, value)),
 | 
				
			||||||
 | 
					            0xCC, 0xCE => dma.*[2].setDmadad(subset(u32, u16, byte_addr, dma.*[2].dad, value)),
 | 
				
			||||||
 | 
					            0xD0 => dma.*[2].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xD2 => dma.*[2].setDmacntH(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4, 0xD6 => dma.*[3].setDmasad(subset(u32, u16, byte_addr, dma.*[3].sad, value)),
 | 
				
			||||||
 | 
					            0xD8, 0xDA => dma.*[3].setDmadad(subset(u32, u16, byte_addr, dma.*[3].dad, value)),
 | 
				
			||||||
 | 
					            0xDC => dma.*[3].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xDE => dma.*[3].setDmacntH(value),
 | 
				
			||||||
 | 
					            else => log.warn("Tried to write 0x{X:0>4}{} to 0x{X:0>8}", .{ value, T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u8 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB1, 0xB2, 0xB3 => dma.*[0].setDmasad(subset(u32, u8, byte_addr, dma.*[0].sad, value)),
 | 
				
			||||||
 | 
					            0xB4, 0xB5, 0xB6, 0xB7 => dma.*[0].setDmadad(subset(u32, u8, byte_addr, dma.*[0].dad, value)),
 | 
				
			||||||
 | 
					            0xB8, 0xB9 => dma.*[0].setDmacntL(subset(u16, u8, byte_addr, dma.*[0].word_count, value)),
 | 
				
			||||||
 | 
					            0xBA, 0xBB => dma.*[0].setDmacntH(subset(u16, u8, byte_addr, dma.*[0].cnt.raw, value)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC, 0xBD, 0xBE, 0xBF => dma.*[1].setDmasad(subset(u32, u8, byte_addr, dma.*[1].sad, value)),
 | 
				
			||||||
 | 
					            0xC0, 0xC1, 0xC2, 0xC3 => dma.*[1].setDmadad(subset(u32, u8, byte_addr, dma.*[1].dad, value)),
 | 
				
			||||||
 | 
					            0xC4, 0xC5 => dma.*[1].setDmacntL(subset(u16, u8, byte_addr, dma.*[1].word_count, value)),
 | 
				
			||||||
 | 
					            0xC6, 0xC7 => dma.*[1].setDmacntH(subset(u16, u8, byte_addr, dma.*[1].cnt.raw, value)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8, 0xC9, 0xCA, 0xCB => dma.*[2].setDmasad(subset(u32, u8, byte_addr, dma.*[2].sad, value)),
 | 
				
			||||||
 | 
					            0xCC, 0xCD, 0xCE, 0xCF => dma.*[2].setDmadad(subset(u32, u8, byte_addr, dma.*[2].dad, value)),
 | 
				
			||||||
 | 
					            0xD0, 0xD1 => dma.*[2].setDmacntL(subset(u16, u8, byte_addr, dma.*[2].word_count, value)),
 | 
				
			||||||
 | 
					            0xD2, 0xD3 => dma.*[2].setDmacntH(subset(u16, u8, byte_addr, dma.*[2].cnt.raw, value)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4, 0xD5, 0xD6, 0xD7 => dma.*[3].setDmasad(subset(u32, u8, byte_addr, dma.*[3].sad, value)),
 | 
				
			||||||
 | 
					            0xD8, 0xD9, 0xDA, 0xDB => dma.*[3].setDmadad(subset(u32, u8, byte_addr, dma.*[3].dad, value)),
 | 
				
			||||||
 | 
					            0xDC, 0xDD => dma.*[3].setDmacntL(subset(u16, u8, byte_addr, dma.*[3].word_count, value)),
 | 
				
			||||||
 | 
					            0xDE, 0xDF => dma.*[3].setDmacntH(subset(u16, u8, byte_addr, dma.*[3].cnt.raw, value)),
 | 
				
			||||||
 | 
					            else => log.warn("Tried to write 0x{X:0>2}{} to 0x{X:0>8}", .{ value, T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        else => @compileError("DMA: Unsupported write width"),
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn warn(comptime format: []const u8, args: anytype) u0 {
 | 
				
			||||||
 | 
					    log.warn(format, args);
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Function that creates a DMAController. Determines unique DMA Controller behaiour at compile-time
 | 
				
			||||||
 | 
					fn Controller(comptime id: u2) type {
 | 
				
			||||||
 | 
					    return struct {
 | 
				
			||||||
 | 
					        const Self = @This();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const sad_mask: u32 = if (id == 0) 0x07FF_FFFF else 0x0FFF_FFFF;
 | 
				
			||||||
 | 
					        const dad_mask: u32 = if (id != 3) 0x07FF_FFFF else 0x0FFF_FFFF;
 | 
				
			||||||
 | 
					        const WordCount = if (id == 3) u16 else u14;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Write-only. The first address in a DMA transfer. (DMASAD)
 | 
				
			||||||
 | 
					        /// Note: use writeSrc instead of manipulating src_addr directly
 | 
				
			||||||
 | 
					        sad: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Write-only. The final address in a DMA transffer. (DMADAD)
 | 
				
			||||||
 | 
					        /// Note: Use writeDst instead of manipulatig dst_addr directly
 | 
				
			||||||
 | 
					        dad: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Write-only. The Word Count for the DMA Transfer (DMACNT_L)
 | 
				
			||||||
 | 
					        word_count: WordCount = 0,
 | 
				
			||||||
 | 
					        /// Read / Write. DMACNT_H
 | 
				
			||||||
 | 
					        /// Note: Use writeControl instead of manipulating cnt directly.
 | 
				
			||||||
 | 
					        cnt: DmaCnt = .{ .raw = 0x0000 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Internal. The last successfully read value
 | 
				
			||||||
 | 
					        data_latch: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Internal. Currrent Source Address
 | 
				
			||||||
 | 
					        sad_latch: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Internal. Current Destination Address
 | 
				
			||||||
 | 
					        dad_latch: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Internal. Word Count
 | 
				
			||||||
 | 
					        _word_count: WordCount = 0,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Some DMA Transfers are enabled during Hblank / VBlank and / or
 | 
				
			||||||
 | 
					        /// have delays. Thefore bit 15 of DMACNT isn't actually something
 | 
				
			||||||
 | 
					        /// we can use to control when we do or do not execute a step in a DMA Transfer
 | 
				
			||||||
 | 
					        in_progress: bool = false,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn reset(self: *Self) void {
 | 
				
			||||||
 | 
					            self.* = Self.init();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmasad(self: *Self, addr: u32) void {
 | 
				
			||||||
 | 
					            self.sad = addr & sad_mask;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmadad(self: *Self, addr: u32) void {
 | 
				
			||||||
 | 
					            self.dad = addr & dad_mask;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmacntL(self: *Self, halfword: u16) void {
 | 
				
			||||||
 | 
					            self.word_count = @truncate(halfword);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn dmacntH(self: *const Self) u16 {
 | 
				
			||||||
 | 
					            return self.cnt.raw & if (id == 3) 0xFFE0 else 0xF7E0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmacntH(self: *Self, halfword: u16) void {
 | 
				
			||||||
 | 
					            const new = DmaCnt{ .raw = halfword };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (!self.cnt.enabled.read() and new.enabled.read()) {
 | 
				
			||||||
 | 
					                // Reload Internals on Rising Edge.
 | 
				
			||||||
 | 
					                self.sad_latch = self.sad;
 | 
				
			||||||
 | 
					                self.dad_latch = self.dad;
 | 
				
			||||||
 | 
					                self._word_count = if (self.word_count == 0) std.math.maxInt(WordCount) else self.word_count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Only a Start Timing of 00 has a DMA Transfer immediately begin
 | 
				
			||||||
 | 
					                self.in_progress = new.start_timing.read() == 0b00;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // this is just for debug purposes
 | 
				
			||||||
 | 
					                const start_timing: Kind = @enumFromInt(new.start_timing.read());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                switch (start_timing) {
 | 
				
			||||||
 | 
					                    .immediate, .vblank => {},
 | 
				
			||||||
 | 
					                    else => log.err("TODO: Implement DMA({}) {s} mode", .{ id, @tagName(start_timing) }),
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // 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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmacnt(self: *Self, word: u32) void {
 | 
				
			||||||
 | 
					            self.setDmacntL(@truncate(word));
 | 
				
			||||||
 | 
					            self.setDmacntH(@truncate(word >> 16));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn step(self: *Self, cpu: *System.Arm7tdmi) void {
 | 
				
			||||||
 | 
					            const bus_ptr: *System.Bus7 = @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());
 | 
				
			||||||
 | 
					            const dad_adj: Adjustment = if (is_fifo) .Fixed else @enumFromInt(self.cnt.dad_adj.read());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const transfer_type = is_fifo or self.cnt.transfer_type.read();
 | 
				
			||||||
 | 
					            const offset: u32 = if (transfer_type) @sizeOf(u32) else @sizeOf(u16);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const mask = if (transfer_type) ~@as(u32, 3) else ~@as(u32, 1);
 | 
				
			||||||
 | 
					            const sad_addr = self.sad_latch & mask;
 | 
				
			||||||
 | 
					            const dad_addr = self.dad_latch & mask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (transfer_type) {
 | 
				
			||||||
 | 
					                if (sad_addr >= 0x0200_0000) self.data_latch = cpu.bus.read(u32, sad_addr);
 | 
				
			||||||
 | 
					                cpu.bus.write(u32, dad_addr, self.data_latch);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (sad_addr >= 0x0200_0000) {
 | 
				
			||||||
 | 
					                    const value: u32 = cpu.bus.read(u16, sad_addr);
 | 
				
			||||||
 | 
					                    self.data_latch = value << 16 | value;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                cpu.bus.write(u16, dad_addr, @as(u16, @truncate(rotr(u32, self.data_latch, 8 * (dad_addr & 3)))));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (@as(u8, @truncate(sad_addr >> 24))) {
 | 
				
			||||||
 | 
					                // according to fleroviux, DMAs with a source address in ROM misbehave
 | 
				
			||||||
 | 
					                // the resultant behaviour is that the source address will increment despite what DMAXCNT says
 | 
				
			||||||
 | 
					                0x08...0x0D => self.sad_latch +%= offset, // obscure behaviour
 | 
				
			||||||
 | 
					                else => switch (sad_adj) {
 | 
				
			||||||
 | 
					                    .Increment => self.sad_latch +%= offset,
 | 
				
			||||||
 | 
					                    .Decrement => self.sad_latch -%= offset,
 | 
				
			||||||
 | 
					                    .IncrementReload => log.err("{} is a prohibited adjustment on SAD", .{sad_adj}),
 | 
				
			||||||
 | 
					                    .Fixed => {},
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (dad_adj) {
 | 
				
			||||||
 | 
					                .Increment, .IncrementReload => self.dad_latch +%= offset,
 | 
				
			||||||
 | 
					                .Decrement => self.dad_latch -%= offset,
 | 
				
			||||||
 | 
					                .Fixed => {},
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            self._word_count -= 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (self._word_count == 0) {
 | 
				
			||||||
 | 
					                if (self.cnt.irq.read()) {
 | 
				
			||||||
 | 
					                    switch (id) {
 | 
				
			||||||
 | 
					                        0 => bus_ptr.io.irq.dma0.set(),
 | 
				
			||||||
 | 
					                        1 => bus_ptr.io.irq.dma1.set(),
 | 
				
			||||||
 | 
					                        2 => bus_ptr.io.irq.dma2.set(),
 | 
				
			||||||
 | 
					                        3 => bus_ptr.io.irq.dma3.set(),
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds7, cpu);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // If we're not repeating, Fire the IRQs and disable the DMA
 | 
				
			||||||
 | 
					                if (!self.cnt.repeat.read()) self.cnt.enabled.unset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // We want to disable our internal enabled flag regardless of repeat
 | 
				
			||||||
 | 
					                // because we only want to step A DMA that repeats during it's specific
 | 
				
			||||||
 | 
					                // timing window
 | 
				
			||||||
 | 
					                self.in_progress = false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fn poll(self: *Self, comptime kind: Kind) void {
 | 
				
			||||||
 | 
					            if (self.in_progress) return; // If there's an ongoing DMA Transfer, exit early
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // No ongoing DMA Transfer, We want to check if we should repeat an existing one
 | 
				
			||||||
 | 
					            // Determined by the repeat bit and whether the DMA is in the right start_timing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (kind) {
 | 
				
			||||||
 | 
					                .vblank => self.in_progress = self.cnt.enabled.read() and self.cnt.start_timing.read() == 0b01,
 | 
				
			||||||
 | 
					                .cartridge_slot, .immediate, .special => {},
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // If we determined that the repeat bit is set (and now the Hblank / Vblank DMA is now in progress)
 | 
				
			||||||
 | 
					            // Reload internal word count latch
 | 
				
			||||||
 | 
					            // Reload internal DAD latch if we are in IncrementRelaod
 | 
				
			||||||
 | 
					            if (self.in_progress) {
 | 
				
			||||||
 | 
					                self._word_count = if (self.word_count == 0) std.math.maxInt(@TypeOf(self._word_count)) else self.word_count;
 | 
				
			||||||
 | 
					                if (@as(Adjustment, @enumFromInt(self.cnt.dad_adj.read())) == .IncrementReload) self.dad_latch = self.dad;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn requestAudio(self: *Self, _: u32) void {
 | 
				
			||||||
 | 
					            comptime std.debug.assert(id == 1 or id == 2);
 | 
				
			||||||
 | 
					            if (self.in_progress) return; // APU must wait their turn
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // DMA May not be configured for handling DMAs
 | 
				
			||||||
 | 
					            if (self.cnt.start_timing.read() != 0b11) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // We Assume the Repeat Bit is Set
 | 
				
			||||||
 | 
					            // We Assume that DAD is set to 0x0400_00A0 or 0x0400_00A4 (fifo_addr)
 | 
				
			||||||
 | 
					            // We Assume DMACNT_L is set to 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // FIXME: Safe to just assume whatever DAD is set to is the FIFO Address?
 | 
				
			||||||
 | 
					            // self.dad_latch = fifo_addr;
 | 
				
			||||||
 | 
					            self.cnt.repeat.set();
 | 
				
			||||||
 | 
					            self._word_count = 4;
 | 
				
			||||||
 | 
					            self.in_progress = true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn onVblank(bus: *System.Bus7) void {
 | 
				
			||||||
 | 
					    inline for (0..4) |i| bus.dma[i].poll(.vblank);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn step(cpu: *System.Arm7tdmi) bool {
 | 
				
			||||||
 | 
					    const bus: *System.Bus7 = @ptrCast(@alignCast(cpu.bus.ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    inline for (0..4) |i| {
 | 
				
			||||||
 | 
					        if (bus.dma[i].in_progress) {
 | 
				
			||||||
 | 
					            bus.dma[i].step(cpu);
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Adjustment = enum(u2) {
 | 
				
			||||||
 | 
					    Increment = 0,
 | 
				
			||||||
 | 
					    Decrement = 1,
 | 
				
			||||||
 | 
					    Fixed = 2,
 | 
				
			||||||
 | 
					    IncrementReload = 3,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Kind = enum(u2) {
 | 
				
			||||||
 | 
					    immediate = 0,
 | 
				
			||||||
 | 
					    vblank,
 | 
				
			||||||
 | 
					    cartridge_slot,
 | 
				
			||||||
 | 
					    special,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@@ -3,53 +3,99 @@ const std = @import("std");
 | 
				
			|||||||
const Bitfield = @import("bitfield").Bitfield;
 | 
					const Bitfield = @import("bitfield").Bitfield;
 | 
				
			||||||
const Bit = @import("bitfield").Bit;
 | 
					const Bit = @import("bitfield").Bit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Ppu = @import("../ppu.zig").Ppu;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Bus = @import("Bus.zig");
 | 
					const Bus = @import("Bus.zig");
 | 
				
			||||||
const SharedIo = @import("../io.zig").Io;
 | 
					const SharedCtx = @import("../emu.zig").SharedCtx;
 | 
				
			||||||
const PostFlag = @import("../io.zig").PostFlag;
 | 
					 | 
				
			||||||
const masks = @import("../io.zig").masks;
 | 
					const masks = @import("../io.zig").masks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const IntEnable = @import("../io.zig").IntEnable;
 | 
				
			||||||
 | 
					const IntRequest = @import("../io.zig").IntEnable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const dma = @import("dma.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const log = std.log.scoped(.nds7_io);
 | 
					const log = std.log.scoped(.nds7_io);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub const Io = struct {
 | 
					pub const Io = struct {
 | 
				
			||||||
    shared: *SharedIo,
 | 
					    shr: *SharedCtx.Io,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // IME - Interrupt Master Enable
 | 
					    /// IME - Interrupt Master Enable
 | 
				
			||||||
    /// Read / Write
 | 
					    /// Read/Write
 | 
				
			||||||
    ime: bool = false,
 | 
					    ime: bool = false,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// IE -  Interrupt Enable
 | 
					    /// IE -  Interrupt Enable
 | 
				
			||||||
    /// Read / Write
 | 
					    /// Read/Write
 | 
				
			||||||
    ie: Interrupt = .{ .raw = 0x0000_0000 },
 | 
					    ie: IntEnable = .{ .raw = 0x0000_0000 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// IF - Interrupt Request
 | 
					    /// IF - Interrupt Request
 | 
				
			||||||
    /// Read / Write
 | 
					    /// Read/Write
 | 
				
			||||||
    irq: Interrupt = .{ .raw = 0x0000_0000 },
 | 
					    irq: IntRequest = .{ .raw = 0x0000_0000 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// POSTFLG - Post Boot Flag
 | 
				
			||||||
 | 
					    /// Read/Write
 | 
				
			||||||
    postflg: PostFlag = .in_progress,
 | 
					    postflg: PostFlag = .in_progress,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn init(io: *SharedIo) @This() {
 | 
					    /// HALTCNT - Low Power Mode Control
 | 
				
			||||||
        return .{ .shared = io };
 | 
					    /// Read/Write
 | 
				
			||||||
 | 
					    haltcnt: Haltcnt = .execute,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    ppu: ?*Ppu.Io = null,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn init(io: *SharedCtx.Io) @This() {
 | 
				
			||||||
 | 
					        return .{ .shr = io };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn configure(self: *@This(), ppu: *Ppu) void {
 | 
				
			||||||
 | 
					        self.ppu = &ppu.io;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
 | 
					pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
 | 
				
			||||||
    return switch (T) {
 | 
					    return switch (T) {
 | 
				
			||||||
        u32 => switch (address) {
 | 
					        u32 => switch (address) {
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DC => dma.read(T, &bus.dma, address) orelse 0x000_0000,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Timers
 | 
				
			||||||
 | 
					            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),
 | 
					            0x0400_0208 => @intFromBool(bus.io.ime),
 | 
				
			||||||
            0x0400_0210 => bus.io.ie.raw,
 | 
					            0x0400_0210 => bus.io.ie.raw,
 | 
				
			||||||
            0x0400_0214 => bus.io.irq.raw,
 | 
					            0x0400_0214 => bus.io.irq.raw,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0410_0000 => bus.io.shared.ipc_fifo.recv(.nds7),
 | 
					            0x0410_0000 => bus.io.shr.ipc.recv(.nds7),
 | 
				
			||||||
            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
					            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u16 => switch (address) {
 | 
					        u16 => switch (address) {
 | 
				
			||||||
            0x0400_0180 => @truncate(bus.io.shared.ipc_fifo._nds7.sync.raw),
 | 
					            0x0400_0004 => bus.io.ppu.?.nds7.dispstat.raw,
 | 
				
			||||||
            0x0400_0184 => @truncate(bus.io.shared.ipc_fifo._nds7.cnt.raw),
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DE => dma.read(T, &bus.dma, address) orelse 0x0000,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Timers
 | 
				
			||||||
 | 
					            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_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 }),
 | 
					            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u8 => switch (address) {
 | 
					        u8 => switch (address) {
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DF => dma.read(T, &bus.dma, address) orelse 0x00,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Timers
 | 
				
			||||||
 | 
					            0x0400_0100...0x0400_010F => warn("TODO(timer): read(T: {}, addr: 0x{X:0>8}) {}", .{ T, address, T }),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // RTC
 | 
				
			||||||
 | 
					            0x0400_0138 => warn("TODO(rtc): read(T: {}, addr: 0x{X:0>8}) {}", .{ T, address, T }),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0240 => bus.vram.stat().raw,
 | 
					            0x0400_0240 => bus.vram.stat().raw,
 | 
				
			||||||
            0x0400_0241 => bus.io.shared.wramcnt.raw,
 | 
					            0x0400_0241 => bus.io.shr.wramcnt.raw,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0300 => @intFromEnum(bus.io.postflg),
 | 
				
			||||||
            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
					            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        else => @compileError(T ++ " is an unsupported bus read type"),
 | 
					        else => @compileError(T ++ " is an unsupported bus read type"),
 | 
				
			||||||
@@ -59,20 +105,56 @@ pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
 | 
				
			|||||||
pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
					pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
				
			||||||
    switch (T) {
 | 
					    switch (T) {
 | 
				
			||||||
        u32 => switch (address) {
 | 
					        u32 => switch (address) {
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DC => dma.write(T, &bus.dma, address, value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Timers
 | 
				
			||||||
 | 
					            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,
 | 
					            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
				
			||||||
            0x0400_0210 => bus.io.ie.raw = value,
 | 
					            0x0400_0210 => bus.io.ie.raw = value,
 | 
				
			||||||
            0x0400_0214 => bus.io.irq.raw = value,
 | 
					            0x0400_0214 => bus.io.irq.raw &= ~value,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0188 => bus.io.shared.ipc_fifo.send(.nds7, value) catch |e| std.debug.panic("FIFO error: {}", .{e}),
 | 
					            0x0400_0188 => bus.io.shr.ipc.send(.nds7, value),
 | 
				
			||||||
            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
					            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u16 => switch (address) {
 | 
					        u16 => switch (address) {
 | 
				
			||||||
            0x0400_0180 => bus.io.shared.ipc_fifo.setIpcSync(.nds7, value),
 | 
					            0x0400_0004 => bus.io.ppu.?.nds7.dispstat.raw = value,
 | 
				
			||||||
            0x0400_0184 => bus.io.shared.ipc_fifo.setIpcFifoCnt(.nds7, value),
 | 
					
 | 
				
			||||||
            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DE => dma.write(T, &bus.dma, address, value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Timers
 | 
				
			||||||
 | 
					            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),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
				
			||||||
 | 
					            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>4})", .{ T, address, value }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u8 => switch (address) {
 | 
					        u8 => switch (address) {
 | 
				
			||||||
            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DF => dma.write(T, &bus.dma, address, value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Timers
 | 
				
			||||||
 | 
					            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(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0301 => switch ((value >> 6) & 0b11) {
 | 
				
			||||||
 | 
					                0b00 => bus.io.haltcnt = .execute,
 | 
				
			||||||
 | 
					                0b10 => bus.io.haltcnt = .halt,
 | 
				
			||||||
 | 
					                else => |val| {
 | 
				
			||||||
 | 
					                    const tag: Haltcnt = @enumFromInt(val);
 | 
				
			||||||
 | 
					                    log.err("TODO: Implement {}", .{tag});
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>2})", .{ T, address, value }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        else => @compileError(T ++ " is an unsupported bus write type"),
 | 
					        else => @compileError(T ++ " is an unsupported bus write type"),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -89,6 +171,22 @@ pub const Vramstat = extern union {
 | 
				
			|||||||
    raw: u8,
 | 
					    raw: u8,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Interrupt = extern union {
 | 
					const Haltcnt = enum(u2) {
 | 
				
			||||||
    raw: u32,
 | 
					    execute = 0,
 | 
				
			||||||
 | 
					    gba_mode,
 | 
				
			||||||
 | 
					    halt,
 | 
				
			||||||
 | 
					    sleep,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const PostFlag = enum(u8) { in_progress = 0, completed };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const DmaCnt = extern union {
 | 
				
			||||||
 | 
					    dad_adj: Bitfield(u16, 5, 2),
 | 
				
			||||||
 | 
					    sad_adj: Bitfield(u16, 7, 2),
 | 
				
			||||||
 | 
					    repeat: Bit(u16, 9),
 | 
				
			||||||
 | 
					    transfer_type: Bit(u16, 10),
 | 
				
			||||||
 | 
					    start_timing: Bitfield(u16, 12, 2),
 | 
				
			||||||
 | 
					    irq: Bit(u16, 14),
 | 
				
			||||||
 | 
					    enabled: Bit(u16, 15),
 | 
				
			||||||
 | 
					    raw: u16,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										48
									
								
								src/core/nds9/Bios.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								src/core/nds9/Bios.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,48 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const log = std.log.scoped(.nds7_bios);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const KiB = 0x400;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const _len = 4 * KiB; // real size of the NDS9 BIOS
 | 
				
			||||||
 | 
					const len = 32 * KiB; // size allocated to NDS9 BIOS
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					buf: ?*align(4) [len]u8 = null,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// FIXME: Currently we dupe here, should we just take ownership?
 | 
				
			||||||
 | 
					pub fn load(self: *@This(), allocator: Allocator, data: []u8) !void {
 | 
				
			||||||
 | 
					    if (data.len != _len) {
 | 
				
			||||||
 | 
					        const acutal_size = @as(f32, @floatFromInt(data.len)) / KiB;
 | 
				
			||||||
 | 
					        log.warn("BIOS was {d:.2} KiB (should be {} KiB)", .{ acutal_size, len });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const buf = try allocator.alignedAlloc(u8, 4, len);
 | 
				
			||||||
 | 
					    @memcpy(buf[0..data.len], data);
 | 
				
			||||||
 | 
					    @memset(buf[data.len..], 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.* = .{ .buf = buf[0..len] };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
				
			||||||
 | 
					    if (self.buf) |ptr| allocator.destroy(ptr);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// 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 byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // if (address >= len) return 0x0000_0000; // TODO: What is undefined actually?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const ptr = self.buf orelse {
 | 
				
			||||||
 | 
					        log.err("read(T: {}, address: 0x{X:0>8}) from BIOS but none was found!", .{ T, address });
 | 
				
			||||||
 | 
					        @panic("TODO: ability to load in NDS9 BIOS just-in-time");
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    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 {
 | 
				
			||||||
 | 
					    log.err("write(T: {}, address: 0x{X:0>8}, value: 0x{X:}) but we're in the BIOS!", .{ T, address, value });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -3,10 +3,13 @@ const io = @import("io.zig");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const Ppu = @import("../ppu.zig").Ppu;
 | 
					const Ppu = @import("../ppu.zig").Ppu;
 | 
				
			||||||
const Scheduler = @import("../Scheduler.zig");
 | 
					const Scheduler = @import("../Scheduler.zig");
 | 
				
			||||||
const SharedContext = @import("../emu.zig").SharedContext;
 | 
					const SharedCtx = @import("../emu.zig").SharedCtx;
 | 
				
			||||||
const Wram = @import("../emu.zig").Wram;
 | 
					const Wram = @import("../emu.zig").Wram;
 | 
				
			||||||
 | 
					const Bios = @import("Bios.zig");
 | 
				
			||||||
const forceAlign = @import("../emu.zig").forceAlign;
 | 
					const forceAlign = @import("../emu.zig").forceAlign;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Controllers = @import("dma.zig").Controllers;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Allocator = std.mem.Allocator;
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Mode = enum { normal, debug };
 | 
					const Mode = enum { normal, debug };
 | 
				
			||||||
@@ -17,26 +20,36 @@ const log = std.log.scoped(.nds9_bus);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
main: *[4 * MiB]u8,
 | 
					main: *[4 * MiB]u8,
 | 
				
			||||||
wram: *Wram,
 | 
					wram: *Wram,
 | 
				
			||||||
io: io.Io,
 | 
					makeshift_palram: *[2 * KiB]u8,
 | 
				
			||||||
ppu: Ppu,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
scheduler: *Scheduler,
 | 
					scheduler: *Scheduler,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn init(allocator: Allocator, scheduler: *Scheduler, shared_ctx: SharedContext) !@This() {
 | 
					dma: Controllers = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					io: io.Io,
 | 
				
			||||||
 | 
					ppu: Ppu,
 | 
				
			||||||
 | 
					bios: Bios,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn init(allocator: Allocator, scheduler: *Scheduler, ctx: SharedCtx) !@This() {
 | 
				
			||||||
    const dots_per_cycle = 3; // ARM946E-S runs twice as fast as the ARM7TDMI
 | 
					    const dots_per_cycle = 3; // ARM946E-S runs twice as fast as the ARM7TDMI
 | 
				
			||||||
    scheduler.push(.{ .nds9 = .draw }, 256 * dots_per_cycle);
 | 
					    scheduler.push(.{ .nds9 = .draw }, 256 * dots_per_cycle);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return .{
 | 
					    return .{
 | 
				
			||||||
        .main = shared_ctx.main,
 | 
					        .main = ctx.main,
 | 
				
			||||||
        .wram = shared_ctx.wram,
 | 
					        .wram = ctx.wram,
 | 
				
			||||||
        .ppu = try Ppu.init(allocator, shared_ctx.vram),
 | 
					        .makeshift_palram = try allocator.create([2 * KiB]u8),
 | 
				
			||||||
 | 
					        .ppu = try Ppu.init(allocator, ctx.vram),
 | 
				
			||||||
        .scheduler = scheduler,
 | 
					        .scheduler = scheduler,
 | 
				
			||||||
        .io = io.Io.init(shared_ctx.io),
 | 
					        .io = io.Io.init(ctx.io),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        .bios = .{},
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn deinit(self: *@This(), allocator: Allocator) void {
 | 
					pub fn deinit(self: *@This(), allocator: Allocator) void {
 | 
				
			||||||
    self.ppu.deinit(allocator);
 | 
					    self.ppu.deinit(allocator);
 | 
				
			||||||
 | 
					    self.bios.deinit(allocator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    allocator.destroy(self.makeshift_palram);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn reset(_: *@This()) void {
 | 
					pub fn reset(_: *@This()) void {
 | 
				
			||||||
@@ -53,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 {
 | 
					fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T {
 | 
				
			||||||
    const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
					    const byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
				
			||||||
    const readInt = std.mem.readIntLittle;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const aligned_addr = forceAlign(T, address);
 | 
					    const aligned_addr = forceAlign(T, address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (mode) {
 | 
					    switch (mode) {
 | 
				
			||||||
@@ -64,11 +75,14 @@ fn _read(self: *@This(), comptime T: type, comptime mode: Mode, address: u32) T
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return switch (aligned_addr) {
 | 
					    return switch (aligned_addr) {
 | 
				
			||||||
        0x0200_0000...0x02FF_FFFF => readInt(T, self.main[aligned_addr & 0x003F_FFFF ..][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),
 | 
					        0x0300_0000...0x03FF_FFFF => self.wram.read(T, .nds9, aligned_addr),
 | 
				
			||||||
        0x0400_0000...0x04FF_FFFF => io.read(self, T, aligned_addr),
 | 
					        0x0400_0000...0x04FF_FFFF => io.read(self, T, aligned_addr),
 | 
				
			||||||
 | 
					        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),
 | 
					        0x0600_0000...0x06FF_FFFF => self.ppu.vram.read(T, .nds9, aligned_addr),
 | 
				
			||||||
        else => warn("unexpected read: 0x{x:0>8} -> {}", .{ aligned_addr, T }),
 | 
					        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 }),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -82,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 {
 | 
					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 byte_count = @divExact(@typeInfo(T).Int.bits, 8);
 | 
				
			||||||
    const writeInt = std.mem.writeIntLittle;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const aligned_addr = forceAlign(T, address);
 | 
					    const aligned_addr = forceAlign(T, address);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (mode) {
 | 
					    switch (mode) {
 | 
				
			||||||
@@ -93,11 +105,14 @@ fn _write(self: *@This(), comptime T: type, comptime mode: Mode, address: u32, v
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    switch (aligned_addr) {
 | 
					    switch (aligned_addr) {
 | 
				
			||||||
        0x0200_0000...0x02FF_FFFF => writeInt(T, self.main[aligned_addr & 0x003F_FFFF ..][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),
 | 
					        0x0300_0000...0x03FF_FFFF => self.wram.write(T, .nds9, aligned_addr, value),
 | 
				
			||||||
        0x0400_0000...0x04FF_FFFF => io.write(self, T, aligned_addr, value),
 | 
					        0x0400_0000...0x04FF_FFFF => io.write(self, T, aligned_addr, 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),
 | 
					        0x0600_0000...0x06FF_FFFF => self.ppu.vram.write(T, .nds9, aligned_addr, value),
 | 
				
			||||||
        else => log.warn("unexpected write: 0x{X:}{} -> 0x{X:0>8}", .{ value, T, aligned_addr }),
 | 
					        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 }),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,10 +2,14 @@ const std = @import("std");
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const log = std.log.scoped(.cp15);
 | 
					const log = std.log.scoped(.cp15);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
control: u32 = 0x0005_2078,
 | 
					const panic_on_unimplemented: bool = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					control: u32 = 0x0001_2078,
 | 
				
			||||||
dtcm_size_base: u32 = 0x0300_000A,
 | 
					dtcm_size_base: u32 = 0x0300_000A,
 | 
				
			||||||
itcm_size_base: u32 = 0x0000_0020,
 | 
					itcm_size_base: u32 = 0x0000_0020,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					wait_for_interrupt: bool = false,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Protection Unit
 | 
					// Protection Unit
 | 
				
			||||||
// cache_bits_data_unified: u32 = 0x0000_0000,
 | 
					// cache_bits_data_unified: u32 = 0x0000_0000,
 | 
				
			||||||
// cache_write_bufability: u32 = 0x0000_0000, // For Data Protection Regions
 | 
					// cache_write_bufability: u32 = 0x0000_0000, // For Data Protection Regions
 | 
				
			||||||
@@ -41,22 +45,57 @@ pub fn write(self: *@This(), op1: u3, cn: u4, cm: u4, op2: u3, value: u32) void
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            self.control = (value & ~zeroes) | ones;
 | 
					            self.control = (value & ~zeroes) | ones;
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        0b000_0010_0000_000 => log.err("TODO: write to PU cachability bits (data/unified region)", .{}),
 | 
				
			||||||
 | 
					        0b000_0010_0000_001 => log.err("TODO: write to PU cachability bits (instruction region)", .{}),
 | 
				
			||||||
 | 
					        0b000_0011_0000_000 => log.err("TODO: write to PU cache write-bufferability bits (data protection region)", .{}),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        0b000_0101_0000_000 => log.err("TODO: write to access permission protection region (data/unified)", .{}),
 | 
				
			||||||
 | 
					        0b000_0101_0000_001 => log.err("TODO: write to access permission protection region (insruction)", .{}),
 | 
				
			||||||
 | 
					        0b000_0101_0000_010 => log.err("TODO: write to extended access permission protection region (data/unified)", .{}),
 | 
				
			||||||
 | 
					        0b000_0101_0000_011 => log.err("TODO: write to extended access permission protection region (insruction)", .{}),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        0b000_0110_0000_000,
 | 
				
			||||||
 | 
					        0b000_0110_0001_000,
 | 
				
			||||||
 | 
					        0b000_0110_0010_000,
 | 
				
			||||||
 | 
					        0b000_0110_0011_000,
 | 
				
			||||||
 | 
					        0b000_0110_0100_000,
 | 
				
			||||||
 | 
					        0b000_0110_0101_000,
 | 
				
			||||||
 | 
					        0b000_0110_0110_000,
 | 
				
			||||||
 | 
					        0b000_0110_0111_000,
 | 
				
			||||||
 | 
					        => log.err("TODO: write to PU data/unified region #{}", .{cm}),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        0b000_0110_0000_001,
 | 
				
			||||||
 | 
					        0b000_0110_0001_001,
 | 
				
			||||||
 | 
					        0b000_0110_0010_001,
 | 
				
			||||||
 | 
					        0b000_0110_0011_001,
 | 
				
			||||||
 | 
					        0b000_0110_0100_001,
 | 
				
			||||||
 | 
					        0b000_0110_0101_001,
 | 
				
			||||||
 | 
					        0b000_0110_0110_001,
 | 
				
			||||||
 | 
					        0b000_0110_0111_001,
 | 
				
			||||||
 | 
					        => log.err("TODO: write to PU instruction region #{}", .{cm}),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        0b000_0111_0000_100 => self.wait_for_interrupt = true, // NDS9 Halt
 | 
				
			||||||
 | 
					        0b000_0111_0101_000 => log.err("TODO: invalidate instruction cache", .{}),
 | 
				
			||||||
 | 
					        0b000_0111_0110_000 => log.err("TODO: invalidate data cache", .{}),
 | 
				
			||||||
 | 
					        0b000_0111_1010_100 => log.err("TODO: drain write buffer", .{}),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        0b000_1001_0001_000 => { // Data TCM Size / Base
 | 
					        0b000_1001_0001_000 => { // Data TCM Size / Base
 | 
				
			||||||
            const zeroes: u32 = 0b00000000_00000000_00001111_11000001;
 | 
					            const zeroes: u32 = 0b00000000_00000000_00001111_11000001;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            self.dtcm_size_base = value & ~zeroes;
 | 
					            self.dtcm_size_base = value & ~zeroes;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const size_shamt: u5 = blk: {
 | 
					            // const size_shamt: u5 = blk: {
 | 
				
			||||||
                const size = self.dtcm_size_base >> 1 & 0x1F;
 | 
					            //     const size = self.dtcm_size_base >> 1 & 0x1F;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (size < 3) break :blk 3;
 | 
					            //     if (size < 3) break :blk 3;
 | 
				
			||||||
                if (size > 23) break :blk 23;
 | 
					            //     if (size > 23) break :blk 23;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                break :blk @intCast(size);
 | 
					            //     break :blk @intCast(size);
 | 
				
			||||||
            };
 | 
					            // };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            log.debug("DTCM Virtual Size: {}B", .{@as(u32, 0x200) << size_shamt});
 | 
					            // log.debug("DTCM Virtual Size: {}B", .{@as(u32, 0x200) << size_shamt});
 | 
				
			||||||
            log.debug("DTCM Region Base: 0x{X:0>8}", .{self.dtcm_size_base & 0xFFFF_F000});
 | 
					            // log.debug("DTCM Region Base: 0x{X:0>8}", .{self.dtcm_size_base & 0xFFFF_F000});
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        0b000_1001_0001_001 => { // Instruction TCM Size / Base
 | 
					        0b000_1001_0001_001 => { // Instruction TCM Size / Base
 | 
				
			||||||
            const zeroes: u32 = 0b00000000_00000000_00001111_11000001;
 | 
					            const zeroes: u32 = 0b00000000_00000000_00001111_11000001;
 | 
				
			||||||
@@ -64,18 +103,19 @@ pub fn write(self: *@This(), op1: u3, cn: u4, cm: u4, op2: u3, value: u32) void
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
            self.itcm_size_base = value & ~(zeroes | itcm_specific);
 | 
					            self.itcm_size_base = value & ~(zeroes | itcm_specific);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            const size_shamt: u5 = blk: {
 | 
					            // const size_shamt: u5 = blk: {
 | 
				
			||||||
                const size = self.dtcm_size_base >> 1 & 0x1F;
 | 
					            //     const size = self.dtcm_size_base >> 1 & 0x1F;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                if (size < 3) break :blk 3;
 | 
					            //     if (size < 3) break :blk 3;
 | 
				
			||||||
                if (size > 23) break :blk 23;
 | 
					            //     if (size > 23) break :blk 23;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                break :blk @intCast(size);
 | 
					            //     break :blk @intCast(size);
 | 
				
			||||||
            };
 | 
					            // };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            log.debug("ICTM Virtual Size: {}B", .{@as(u32, 0x200) << size_shamt});
 | 
					            // log.debug("ICTM Virtual Size: {}B", .{@as(u32, 0x200) << size_shamt});
 | 
				
			||||||
            log.debug("ICTM Region Base: 0x{X:0>8}", .{0x0000_0000});
 | 
					            // log.debug("ICTM Region Base: 0x{X:0>8}", .{0x0000_0000});
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        else => _ = panic("TODO: implement write to register {}, c{}, c{}, {}", .{ op1, cn, cm, op2 }),
 | 
					        else => _ = panic("TODO: implement write to register {}, c{}, c{}, {}", .{ op1, cn, cm, op2 }),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@@ -97,6 +137,7 @@ pub fn reset(self: *@This()) void {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
fn panic(comptime format: []const u8, args: anytype) u32 {
 | 
					fn panic(comptime format: []const u8, args: anytype) u32 {
 | 
				
			||||||
    log.err(format, args);
 | 
					    log.err(format, args);
 | 
				
			||||||
    // @panic("Coprocessor invariant broken");
 | 
					    if (panic_on_unimplemented) @panic("cp15 invariant broken");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										411
									
								
								src/core/nds9/dma.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										411
									
								
								src/core/nds9/dma.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,411 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const System = @import("../emu.zig").System;
 | 
				
			||||||
 | 
					const DmaCnt = @import("io.zig").DmaCnt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const rotr = std.math.rotr;
 | 
				
			||||||
 | 
					const shift = @import("../../util.zig").shift;
 | 
				
			||||||
 | 
					const subset = @import("../../util.zig").subset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const handleInterrupt = @import("../emu.zig").handleInterrupt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const log = std.log.scoped(.nds9_dma_transfer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const Controllers = struct {
 | 
				
			||||||
 | 
					    Controller(0) = Controller(0){},
 | 
				
			||||||
 | 
					    Controller(1) = Controller(1){},
 | 
				
			||||||
 | 
					    Controller(2) = Controller(2){},
 | 
				
			||||||
 | 
					    Controller(3) = Controller(3){},
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn read(comptime T: type, dma: *const Controllers, addr: u32) ?T {
 | 
				
			||||||
 | 
					    const byte_addr: u8 = @truncate(addr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return switch (T) {
 | 
				
			||||||
 | 
					        u32 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB4 => null, // DMA0SAD, DMA0DAD,
 | 
				
			||||||
 | 
					            0xB8 => @as(T, dma.*[0].dmacntH()) << 16, // DMA0CNT_L is write-only
 | 
				
			||||||
 | 
					            0xBC, 0xC0 => null, // DMA1SAD, DMA1DAD
 | 
				
			||||||
 | 
					            0xC4 => @as(T, dma.*[1].dmacntH()) << 16, // DMA1CNT_L is write-only
 | 
				
			||||||
 | 
					            0xC8, 0xCC => null, // DMA2SAD, DMA2DAD
 | 
				
			||||||
 | 
					            0xD0 => @as(T, dma.*[2].dmacntH()) << 16, // DMA2CNT_L is write-only
 | 
				
			||||||
 | 
					            0xD4, 0xD8 => null, // DMA3SAD, DMA3DAD
 | 
				
			||||||
 | 
					            0xDC => @as(T, dma.*[3].dmacntH()) << 16, // DMA3CNT_L is write-only
 | 
				
			||||||
 | 
					            else => warn("unaligned {} read from 0x{X:0>8}", .{ T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u16 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB2, 0xB4, 0xB6 => null, // DMA0SAD, DMA0DAD
 | 
				
			||||||
 | 
					            0xB8 => 0x0000, // DMA0CNT_L, suite.gba expects 0x0000 instead of 0xDEAD
 | 
				
			||||||
 | 
					            0xBA => dma.*[0].dmacntH(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC, 0xBE, 0xC0, 0xC2 => null, // DMA1SAD, DMA1DAD
 | 
				
			||||||
 | 
					            0xC4 => 0x0000, // DMA1CNT_L
 | 
				
			||||||
 | 
					            0xC6 => dma.*[1].dmacntH(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8, 0xCA, 0xCC, 0xCE => null, // DMA2SAD, DMA2DAD
 | 
				
			||||||
 | 
					            0xD0 => 0x0000, // DMA2CNT_L
 | 
				
			||||||
 | 
					            0xD2 => dma.*[2].dmacntH(),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4, 0xD6, 0xD8, 0xDA => null, // DMA3SAD, DMA3DAD
 | 
				
			||||||
 | 
					            0xDC => 0x0000, // DMA3CNT_L
 | 
				
			||||||
 | 
					            0xDE => dma.*[3].dmacntH(),
 | 
				
			||||||
 | 
					            else => warn("unaligned {} read from 0x{X:0>8}", .{ T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u8 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0...0xB7 => null, // DMA0SAD, DMA0DAD
 | 
				
			||||||
 | 
					            0xB8, 0xB9 => 0x00, // DMA0CNT_L
 | 
				
			||||||
 | 
					            0xBA, 0xBB => @truncate(dma.*[0].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC...0xC3 => null, // DMA1SAD, DMA1DAD
 | 
				
			||||||
 | 
					            0xC4, 0xC5 => 0x00, // DMA1CNT_L
 | 
				
			||||||
 | 
					            0xC6, 0xC7 => @truncate(dma.*[1].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8...0xCF => null, // DMA2SAD, DMA2DAD
 | 
				
			||||||
 | 
					            0xD0, 0xD1 => 0x00, // DMA2CNT_L
 | 
				
			||||||
 | 
					            0xD2, 0xD3 => @truncate(dma.*[2].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4...0xDB => null, // DMA3SAD, DMA3DAD
 | 
				
			||||||
 | 
					            0xDC, 0xDD => 0x00, // DMA3CNT_L
 | 
				
			||||||
 | 
					            0xDE, 0xDF => @truncate(dma.*[3].dmacntH() >> shift(u16, byte_addr)),
 | 
				
			||||||
 | 
					            else => warn("unexpected {} read from 0x{X:0>8}", .{ T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        else => @compileError("DMA: Unsupported read width"),
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn write(comptime T: type, dma: *Controllers, addr: u32, value: T) void {
 | 
				
			||||||
 | 
					    const byte_addr: u8 = @truncate(addr);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    switch (T) {
 | 
				
			||||||
 | 
					        u32 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0 => dma.*[0].setDmasad(value),
 | 
				
			||||||
 | 
					            0xB4 => dma.*[0].setDmadad(value),
 | 
				
			||||||
 | 
					            0xB8 => dma.*[0].setDmacnt(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC => dma.*[1].setDmasad(value),
 | 
				
			||||||
 | 
					            0xC0 => dma.*[1].setDmadad(value),
 | 
				
			||||||
 | 
					            0xC4 => dma.*[1].setDmacnt(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8 => dma.*[2].setDmasad(value),
 | 
				
			||||||
 | 
					            0xCC => dma.*[2].setDmadad(value),
 | 
				
			||||||
 | 
					            0xD0 => dma.*[2].setDmacnt(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4 => dma.*[3].setDmasad(value),
 | 
				
			||||||
 | 
					            0xD8 => dma.*[3].setDmadad(value),
 | 
				
			||||||
 | 
					            0xDC => dma.*[3].setDmacnt(value),
 | 
				
			||||||
 | 
					            else => log.warn("Tried to write 0x{X:0>8}{} to 0x{X:0>8}", .{ value, T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u16 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB2 => dma.*[0].setDmasad(subset(u32, u16, byte_addr, dma.*[0].sad, value)),
 | 
				
			||||||
 | 
					            0xB4, 0xB6 => dma.*[0].setDmadad(subset(u32, u16, byte_addr, dma.*[0].dad, value)),
 | 
				
			||||||
 | 
					            0xB8 => dma.*[0].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xBA => dma.*[0].setDmacntH(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC, 0xBE => dma.*[1].setDmasad(subset(u32, u16, byte_addr, dma.*[1].sad, value)),
 | 
				
			||||||
 | 
					            0xC0, 0xC2 => dma.*[1].setDmadad(subset(u32, u16, byte_addr, dma.*[1].dad, value)),
 | 
				
			||||||
 | 
					            0xC4 => dma.*[1].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xC6 => dma.*[1].setDmacntH(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8, 0xCA => dma.*[2].setDmasad(subset(u32, u16, byte_addr, dma.*[2].sad, value)),
 | 
				
			||||||
 | 
					            0xCC, 0xCE => dma.*[2].setDmadad(subset(u32, u16, byte_addr, dma.*[2].dad, value)),
 | 
				
			||||||
 | 
					            0xD0 => dma.*[2].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xD2 => dma.*[2].setDmacntH(value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4, 0xD6 => dma.*[3].setDmasad(subset(u32, u16, byte_addr, dma.*[3].sad, value)),
 | 
				
			||||||
 | 
					            0xD8, 0xDA => dma.*[3].setDmadad(subset(u32, u16, byte_addr, dma.*[3].dad, value)),
 | 
				
			||||||
 | 
					            0xDC => dma.*[3].setDmacntL(value),
 | 
				
			||||||
 | 
					            0xDE => dma.*[3].setDmacntH(value),
 | 
				
			||||||
 | 
					            else => log.warn("Tried to write 0x{X:0>4}{} to 0x{X:0>8}", .{ value, T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        u8 => switch (byte_addr) {
 | 
				
			||||||
 | 
					            0xB0, 0xB1, 0xB2, 0xB3 => dma.*[0].setDmasad(subset(u32, u8, byte_addr, dma.*[0].sad, value)),
 | 
				
			||||||
 | 
					            0xB4, 0xB5, 0xB6, 0xB7 => dma.*[0].setDmadad(subset(u32, u8, byte_addr, dma.*[0].dad, value)),
 | 
				
			||||||
 | 
					            0xB8, 0xB9 => dma.*[0].setDmacntL(subset(u16, u8, byte_addr, @as(u16, @truncate(dma.*[0].word_count)), value)), // FIXME: How wrong is this? lol
 | 
				
			||||||
 | 
					            0xBA, 0xBB => dma.*[0].setDmacntH(subset(u16, u8, byte_addr, dma.*[0].cnt.raw, value)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xBC, 0xBD, 0xBE, 0xBF => dma.*[1].setDmasad(subset(u32, u8, byte_addr, dma.*[1].sad, value)),
 | 
				
			||||||
 | 
					            0xC0, 0xC1, 0xC2, 0xC3 => dma.*[1].setDmadad(subset(u32, u8, byte_addr, dma.*[1].dad, value)),
 | 
				
			||||||
 | 
					            0xC4, 0xC5 => dma.*[1].setDmacntL(subset(u16, u8, byte_addr, @as(u16, @truncate(dma.*[1].word_count)), value)),
 | 
				
			||||||
 | 
					            0xC6, 0xC7 => dma.*[1].setDmacntH(subset(u16, u8, byte_addr, dma.*[1].cnt.raw, value)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xC8, 0xC9, 0xCA, 0xCB => dma.*[2].setDmasad(subset(u32, u8, byte_addr, dma.*[2].sad, value)),
 | 
				
			||||||
 | 
					            0xCC, 0xCD, 0xCE, 0xCF => dma.*[2].setDmadad(subset(u32, u8, byte_addr, dma.*[2].dad, value)),
 | 
				
			||||||
 | 
					            0xD0, 0xD1 => dma.*[2].setDmacntL(subset(u16, u8, byte_addr, @as(u16, @truncate(dma.*[2].word_count)), value)),
 | 
				
			||||||
 | 
					            0xD2, 0xD3 => dma.*[2].setDmacntH(subset(u16, u8, byte_addr, dma.*[2].cnt.raw, value)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0xD4, 0xD5, 0xD6, 0xD7 => dma.*[3].setDmasad(subset(u32, u8, byte_addr, dma.*[3].sad, value)),
 | 
				
			||||||
 | 
					            0xD8, 0xD9, 0xDA, 0xDB => dma.*[3].setDmadad(subset(u32, u8, byte_addr, dma.*[3].dad, value)),
 | 
				
			||||||
 | 
					            0xDC, 0xDD => dma.*[3].setDmacntL(subset(u16, u8, byte_addr, @as(u16, @truncate(dma.*[3].word_count)), value)),
 | 
				
			||||||
 | 
					            0xDE, 0xDF => dma.*[3].setDmacntH(subset(u16, u8, byte_addr, dma.*[3].cnt.raw, value)),
 | 
				
			||||||
 | 
					            else => log.warn("Tried to write 0x{X:0>2}{} to 0x{X:0>8}", .{ value, T, addr }),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        else => @compileError("DMA: Unsupported write width"),
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn warn(comptime format: []const u8, args: anytype) u0 {
 | 
				
			||||||
 | 
					    log.warn(format, args);
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// Function that creates a DMAController. Determines unique DMA Controller behaiour at compile-time
 | 
				
			||||||
 | 
					fn Controller(comptime id: u2) type {
 | 
				
			||||||
 | 
					    return struct {
 | 
				
			||||||
 | 
					        const Self = @This();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const sad_mask: u32 = 0x0FFF_FFFE;
 | 
				
			||||||
 | 
					        const dad_mask: u32 = 0x0FFF_FFFE;
 | 
				
			||||||
 | 
					        const WordCount = u21;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Write-only. The first address in a DMA transfer. (DMASAD)
 | 
				
			||||||
 | 
					        /// Note: use writeSrc instead of manipulating src_addr directly
 | 
				
			||||||
 | 
					        sad: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Write-only. The final address in a DMA transffer. (DMADAD)
 | 
				
			||||||
 | 
					        /// Note: Use writeDst instead of manipulatig dst_addr directly
 | 
				
			||||||
 | 
					        dad: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Write-only. The Word Count for the DMA Transfer (DMACNT_L)
 | 
				
			||||||
 | 
					        word_count: WordCount = 0,
 | 
				
			||||||
 | 
					        /// Read / Write. DMACNT_H
 | 
				
			||||||
 | 
					        /// Note: Use writeControl instead of manipulating cnt directly.
 | 
				
			||||||
 | 
					        cnt: DmaCnt = .{ .raw = 0x0000 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Internal. The last successfully read value
 | 
				
			||||||
 | 
					        data_latch: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Internal. Currrent Source Address
 | 
				
			||||||
 | 
					        sad_latch: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Internal. Current Destination Address
 | 
				
			||||||
 | 
					        dad_latch: u32 = 0x0000_0000,
 | 
				
			||||||
 | 
					        /// Internal. Word Count
 | 
				
			||||||
 | 
					        _word_count: WordCount = 0,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Some DMA Transfers are enabled during Hblank / VBlank and / or
 | 
				
			||||||
 | 
					        /// have delays. Thefore bit 15 of DMACNT isn't actually something
 | 
				
			||||||
 | 
					        /// we can use to control when we do or do not execute a step in a DMA Transfer
 | 
				
			||||||
 | 
					        in_progress: bool = false,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn reset(self: *Self) void {
 | 
				
			||||||
 | 
					            self.* = Self.init();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmasad(self: *Self, addr: u32) void {
 | 
				
			||||||
 | 
					            self.sad = addr & sad_mask;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmadad(self: *Self, addr: u32) void {
 | 
				
			||||||
 | 
					            self.dad = addr & dad_mask;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmacntL(self: *Self, halfword: u16) void {
 | 
				
			||||||
 | 
					            self.word_count = halfword;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn dmacntH(self: *const Self) u16 {
 | 
				
			||||||
 | 
					            return self.cnt.raw & if (id == 3) 0xFFE0 else 0xF7E0;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmacntH(self: *Self, halfword: u16) void {
 | 
				
			||||||
 | 
					            const new = DmaCnt{ .raw = halfword };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (!self.cnt.enabled.read() and new.enabled.read()) {
 | 
				
			||||||
 | 
					                // Reload Internals on Rising Edge.
 | 
				
			||||||
 | 
					                self.sad_latch = self.sad;
 | 
				
			||||||
 | 
					                self.dad_latch = self.dad;
 | 
				
			||||||
 | 
					                self._word_count = if (self.word_count == 0) std.math.maxInt(WordCount) else self.word_count;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Only a Start Timing of 00 has a DMA Transfer immediately begin
 | 
				
			||||||
 | 
					                self.in_progress = new.start_timing.read() == 0b00;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // this is just for debug purposes
 | 
				
			||||||
 | 
					                const start_timing: Kind = @enumFromInt(new.start_timing.read());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                switch (start_timing) {
 | 
				
			||||||
 | 
					                    .immediate, .vblank, .hblank => {},
 | 
				
			||||||
 | 
					                    else => log.err("TODO: Implement DMA({}) {s} mode", .{ id, @tagName(start_timing) }),
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // 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;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn setDmacnt(self: *Self, word: u32) void {
 | 
				
			||||||
 | 
					            self.setDmacntL(@truncate(word));
 | 
				
			||||||
 | 
					            self.setDmacntH(@truncate(word >> 16));
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn step(self: *Self, cpu: *System.Arm946es) void {
 | 
				
			||||||
 | 
					            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());
 | 
				
			||||||
 | 
					            const dad_adj: Adjustment = if (is_fifo) .Fixed else @enumFromInt(self.cnt.dad_adj.read());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const transfer_type = is_fifo or self.cnt.transfer_type.read();
 | 
				
			||||||
 | 
					            const offset: u32 = if (transfer_type) @sizeOf(u32) else @sizeOf(u16);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const mask = if (transfer_type) ~@as(u32, 3) else ~@as(u32, 1);
 | 
				
			||||||
 | 
					            const sad_addr = self.sad_latch & mask;
 | 
				
			||||||
 | 
					            const dad_addr = self.dad_latch & mask;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (transfer_type) {
 | 
				
			||||||
 | 
					                if (sad_addr >= 0x0200_0000) self.data_latch = cpu.bus.read(u32, sad_addr);
 | 
				
			||||||
 | 
					                cpu.bus.write(u32, dad_addr, self.data_latch);
 | 
				
			||||||
 | 
					            } else {
 | 
				
			||||||
 | 
					                if (sad_addr >= 0x0200_0000) {
 | 
				
			||||||
 | 
					                    const value: u32 = cpu.bus.read(u16, sad_addr);
 | 
				
			||||||
 | 
					                    self.data_latch = value << 16 | value;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                cpu.bus.write(u16, dad_addr, @as(u16, @truncate(rotr(u32, self.data_latch, 8 * (dad_addr & 3)))));
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (@as(u8, @truncate(sad_addr >> 24))) {
 | 
				
			||||||
 | 
					                // according to fleroviux, DMAs with a source address in ROM misbehave
 | 
				
			||||||
 | 
					                // the resultant behaviour is that the source address will increment despite what DMAXCNT says
 | 
				
			||||||
 | 
					                0x08...0x0D => self.sad_latch +%= offset, // obscure behaviour
 | 
				
			||||||
 | 
					                else => switch (sad_adj) {
 | 
				
			||||||
 | 
					                    .Increment => self.sad_latch +%= offset,
 | 
				
			||||||
 | 
					                    .Decrement => self.sad_latch -%= offset,
 | 
				
			||||||
 | 
					                    .IncrementReload => log.err("{} is a prohibited adjustment on SAD", .{sad_adj}),
 | 
				
			||||||
 | 
					                    .Fixed => {},
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (dad_adj) {
 | 
				
			||||||
 | 
					                .Increment, .IncrementReload => self.dad_latch +%= offset,
 | 
				
			||||||
 | 
					                .Decrement => self.dad_latch -%= offset,
 | 
				
			||||||
 | 
					                .Fixed => {},
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            self._word_count -= 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (self._word_count == 0) {
 | 
				
			||||||
 | 
					                if (self.cnt.irq.read()) {
 | 
				
			||||||
 | 
					                    switch (id) {
 | 
				
			||||||
 | 
					                        0 => bus_ptr.io.irq.dma0.set(),
 | 
				
			||||||
 | 
					                        1 => bus_ptr.io.irq.dma1.set(),
 | 
				
			||||||
 | 
					                        2 => bus_ptr.io.irq.dma2.set(),
 | 
				
			||||||
 | 
					                        3 => bus_ptr.io.irq.dma3.set(),
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    handleInterrupt(.nds9, cpu);
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // If we're not repeating, Fire the IRQs and disable the DMA
 | 
				
			||||||
 | 
					                if (!self.cnt.repeat.read()) self.cnt.enabled.unset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // We want to disable our internal enabled flag regardless of repeat
 | 
				
			||||||
 | 
					                // because we only want to step A DMA that repeats during it's specific
 | 
				
			||||||
 | 
					                // timing window
 | 
				
			||||||
 | 
					                self.in_progress = false;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fn poll(self: *Self, comptime kind: Kind) void {
 | 
				
			||||||
 | 
					            if (self.in_progress) return; // If there's an ongoing DMA Transfer, exit early
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // No ongoing DMA Transfer, We want to check if we should repeat an existing one
 | 
				
			||||||
 | 
					            // Determined by the repeat bit and whether the DMA is in the right start_timing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (kind) {
 | 
				
			||||||
 | 
					                .vblank => self.in_progress = self.cnt.enabled.read() and self.cnt.start_timing.read() == 0b01,
 | 
				
			||||||
 | 
					                .hblank => self.in_progress = self.cnt.enabled.read() and self.cnt.start_timing.read() == 0b10,
 | 
				
			||||||
 | 
					                else => {},
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // If we determined that the repeat bit is set (and now the Hblank / Vblank DMA is now in progress)
 | 
				
			||||||
 | 
					            // Reload internal word count latch
 | 
				
			||||||
 | 
					            // Reload internal DAD latch if we are in IncrementRelaod
 | 
				
			||||||
 | 
					            if (self.in_progress) {
 | 
				
			||||||
 | 
					                self._word_count = if (self.word_count == 0) std.math.maxInt(@TypeOf(self._word_count)) else self.word_count;
 | 
				
			||||||
 | 
					                if (@as(Adjustment, @enumFromInt(self.cnt.dad_adj.read())) == .IncrementReload) self.dad_latch = self.dad;
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn requestAudio(self: *Self, _: u32) void {
 | 
				
			||||||
 | 
					            comptime std.debug.assert(id == 1 or id == 2);
 | 
				
			||||||
 | 
					            if (self.in_progress) return; // APU must wait their turn
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // DMA May not be configured for handling DMAs
 | 
				
			||||||
 | 
					            if (self.cnt.start_timing.read() != 0b11) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // We Assume the Repeat Bit is Set
 | 
				
			||||||
 | 
					            // We Assume that DAD is set to 0x0400_00A0 or 0x0400_00A4 (fifo_addr)
 | 
				
			||||||
 | 
					            // We Assume DMACNT_L is set to 4
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // FIXME: Safe to just assume whatever DAD is set to is the FIFO Address?
 | 
				
			||||||
 | 
					            // self.dad_latch = fifo_addr;
 | 
				
			||||||
 | 
					            self.cnt.repeat.set();
 | 
				
			||||||
 | 
					            self._word_count = 4;
 | 
				
			||||||
 | 
					            self.in_progress = true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn onVblank(bus: *System.Bus9) void {
 | 
				
			||||||
 | 
					    inline for (0..4) |i| bus.dma[i].poll(.vblank);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn onHblank(bus: *System.Bus9) void {
 | 
				
			||||||
 | 
					    inline for (0..4) |i| bus.dma[i].poll(.hblank);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn step(cpu: *System.Arm946es) bool {
 | 
				
			||||||
 | 
					    const bus: *System.Bus9 = @ptrCast(@alignCast(cpu.bus.ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    inline for (0..4) |i| {
 | 
				
			||||||
 | 
					        if (bus.dma[i].in_progress) {
 | 
				
			||||||
 | 
					            bus.dma[i].step(cpu);
 | 
				
			||||||
 | 
					            return true;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return false;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Adjustment = enum(u2) {
 | 
				
			||||||
 | 
					    Increment = 0,
 | 
				
			||||||
 | 
					    Decrement = 1,
 | 
				
			||||||
 | 
					    Fixed = 2,
 | 
				
			||||||
 | 
					    IncrementReload = 3,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Kind = enum(u3) {
 | 
				
			||||||
 | 
					    immediate = 0,
 | 
				
			||||||
 | 
					    vblank,
 | 
				
			||||||
 | 
					    hblank,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    display_start_sync,
 | 
				
			||||||
 | 
					    main_mem_display,
 | 
				
			||||||
 | 
					    cartridge_slot,
 | 
				
			||||||
 | 
					    pak_slot,
 | 
				
			||||||
 | 
					    geo_cmd_fifo,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
@@ -4,135 +4,271 @@ const Bitfield = @import("bitfield").Bitfield;
 | 
				
			|||||||
const Bit = @import("bitfield").Bit;
 | 
					const Bit = @import("bitfield").Bit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Bus = @import("Bus.zig");
 | 
					const Bus = @import("Bus.zig");
 | 
				
			||||||
const SharedIo = @import("../io.zig").Io;
 | 
					const SharedCtx = @import("../emu.zig").SharedCtx;
 | 
				
			||||||
const PostFlag = @import("../io.zig").PostFlag;
 | 
					 | 
				
			||||||
const masks = @import("../io.zig").masks;
 | 
					const masks = @import("../io.zig").masks;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const IntEnable = @import("../io.zig").IntEnable;
 | 
				
			||||||
 | 
					const IntRequest = @import("../io.zig").IntEnable;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const dma = @import("dma.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const sext = @import("../../util.zig").sext;
 | 
					const sext = @import("../../util.zig").sext;
 | 
				
			||||||
 | 
					const shift = @import("../../util.zig").shift;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const log = std.log.scoped(.nds9_io);
 | 
					const log = std.log.scoped(.nds9_io);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub const Io = struct {
 | 
					pub const Io = struct {
 | 
				
			||||||
    shared: *SharedIo,
 | 
					    const fill_len = 0x10 * @sizeOf(u32);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // IME - Interrupt Master Enable
 | 
					    shr: *SharedCtx.Io,
 | 
				
			||||||
    /// Read / Write
 | 
					
 | 
				
			||||||
 | 
					    /// Interrupt Master Enable
 | 
				
			||||||
 | 
					    /// Read/Write
 | 
				
			||||||
    ime: bool = false,
 | 
					    ime: bool = false,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// IE - Interrupt Enable
 | 
					    /// Interrupt Enable
 | 
				
			||||||
    /// Read / Write
 | 
					    /// Read/Write
 | 
				
			||||||
    ie: Interrupt = .{ .raw = 0x0000_0000 },
 | 
					    ///
 | 
				
			||||||
 | 
					    /// Caller must cast the `u32` to either `nds7.IntEnable` or `nds9.IntEnable`
 | 
				
			||||||
 | 
					    ie: IntEnable = .{ .raw = 0x0000_0000 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// IF - Interrupt Request
 | 
					    /// IF - Interrupt Request
 | 
				
			||||||
    /// Read / Write
 | 
					    /// Read/Write
 | 
				
			||||||
    irq: Interrupt = .{ .raw = 0x0000_0000 },
 | 
					    ///
 | 
				
			||||||
 | 
					    /// Caller must cast the `u32` to either `nds7.IntRequest` or `nds9.IntRequest`
 | 
				
			||||||
    // TODO: move postflg
 | 
					    irq: IntRequest = .{ .raw = 0x0000_0000 },
 | 
				
			||||||
 | 
					 | 
				
			||||||
    postflg: PostFlag = .in_progress,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /// POWCNT1 - Graphics Power Control
 | 
					 | 
				
			||||||
    /// Read / Write
 | 
					 | 
				
			||||||
    powcnt: PowCnt = .{ .raw = 0x0000_0000 },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // Read Only
 | 
					 | 
				
			||||||
    keyinput: AtomicKeyInput = .{},
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// DS Maths
 | 
					    /// DS Maths
 | 
				
			||||||
    div: Divisor = .{},
 | 
					    div: Divisor = .{},
 | 
				
			||||||
    sqrt: SquareRootUnit = .{},
 | 
					    sqrt: SquareRootUnit = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn init(io: *SharedIo) @This() {
 | 
					    // TODO: move somewhere else?
 | 
				
			||||||
        return .{ .shared = io };
 | 
					    dma_fill: [fill_len]u8 = [_]u8{0} ** fill_len,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    pub fn init(io: *SharedCtx.Io) @This() {
 | 
				
			||||||
 | 
					        return .{ .shr = io };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
 | 
					pub fn read(bus: *const Bus, comptime T: type, address: u32) T {
 | 
				
			||||||
    return switch (T) {
 | 
					    return switch (T) {
 | 
				
			||||||
        u32 => switch (address) {
 | 
					        u32 => switch (address) {
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DC => dma.read(T, &bus.dma, address) orelse 0x0000_0000,
 | 
				
			||||||
 | 
					            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(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),
 | 
					            0x0400_0208 => @intFromBool(bus.io.ime),
 | 
				
			||||||
            0x0400_0210 => bus.io.ie.raw,
 | 
					            0x0400_0210 => bus.io.ie.raw,
 | 
				
			||||||
            0x0400_0214 => bus.io.irq.raw,
 | 
					            0x0400_0214 => bus.io.irq.raw,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_02A0 => @truncate(bus.io.div.result),
 | 
					            // zig fmt: off
 | 
				
			||||||
            0x0400_02A4 => @truncate(bus.io.div.result >> 32),
 | 
					            0x0400_0240 => @as(u32, bus.ppu.vram.io.cnt_d.raw) << 24
 | 
				
			||||||
            0x0400_02A8 => @truncate(bus.io.div.remainder),
 | 
					                | @as(u32, bus.ppu.vram.io.cnt_c.raw) << 16
 | 
				
			||||||
            0x0400_02AC => @truncate(bus.io.div.remainder >> 32),
 | 
					                | @as(u32, bus.ppu.vram.io.cnt_b.raw) << 8
 | 
				
			||||||
 | 
					                | bus.ppu.vram.io.cnt_a.raw << 0,
 | 
				
			||||||
 | 
					            // zig fmt: on
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0280 => bus.io.div.cnt.raw,
 | 
				
			||||||
 | 
					            0x0400_02A0, 0x0400_02A4 => @truncate(bus.io.div.result >> shift(u64, address)),
 | 
				
			||||||
 | 
					            0x0400_02A8, 0x0400_02AC => @truncate(bus.io.div.remainder >> shift(u64, address)),
 | 
				
			||||||
            0x0400_02B4 => @truncate(bus.io.sqrt.result),
 | 
					            0x0400_02B4 => @truncate(bus.io.sqrt.result),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0410_0000 => bus.io.shared.ipc_fifo.recv(.nds9),
 | 
					            0x0400_1000 => bus.ppu.engines[1].dispcnt.raw,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0410_0000 => bus.io.shr.ipc.recv(.nds9),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_4000, 0x0400_4008 => 0x0000_0000, // Lets software know this is NOT a DSi
 | 
				
			||||||
            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
					            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u16 => switch (address) {
 | 
					        u16 => switch (address) {
 | 
				
			||||||
            0x0400_0004 => bus.ppu.io.dispstat.raw,
 | 
					            0x0400_0006 => bus.ppu.io.nds9.vcount.raw,
 | 
				
			||||||
            0x0400_0130 => bus.io.keyinput.load(.Monotonic),
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0180 => @truncate(bus.io.shared.ipc_fifo._nds9.sync.raw),
 | 
					            // DMA Transfers
 | 
				
			||||||
            0x0400_0184 => @truncate(bus.io.shared.ipc_fifo._nds9.cnt.raw),
 | 
					            0x0400_00B0...0x0400_00DE => dma.read(T, &bus.dma, address) orelse 0x0000,
 | 
				
			||||||
 | 
					            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(timer): read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0004 => bus.ppu.io.nds9.dispstat.raw,
 | 
				
			||||||
 | 
					            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),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0280 => @truncate(bus.io.div.cnt.raw),
 | 
					            0x0400_0280 => @truncate(bus.io.div.cnt.raw),
 | 
				
			||||||
            0x0400_02B0 => @truncate(bus.io.sqrt.cnt.raw),
 | 
					            0x0400_02B0 => @truncate(bus.io.sqrt.cnt.raw),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_1008 => bus.ppu.engines[1].bg[0].cnt.raw,
 | 
				
			||||||
 | 
					            0x0400_100A => bus.ppu.engines[1].bg[1].cnt.raw,
 | 
				
			||||||
 | 
					            0x0400_100C => bus.ppu.engines[1].bg[2].cnt.raw,
 | 
				
			||||||
 | 
					            0x0400_100E => bus.ppu.engines[1].bg[3].cnt.raw,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_1010 => bus.ppu.engines[1].bg[0].hofs.raw,
 | 
				
			||||||
 | 
					            0x0400_1012 => bus.ppu.engines[1].bg[0].vofs.raw,
 | 
				
			||||||
 | 
					            0x0400_1014 => bus.ppu.engines[1].bg[1].hofs.raw,
 | 
				
			||||||
 | 
					            0x0400_1016 => bus.ppu.engines[1].bg[1].vofs.raw,
 | 
				
			||||||
 | 
					            0x0400_1018 => bus.ppu.engines[1].bg[2].hofs.raw,
 | 
				
			||||||
 | 
					            0x0400_101A => bus.ppu.engines[1].bg[2].vofs.raw,
 | 
				
			||||||
 | 
					            0x0400_101C => bus.ppu.engines[1].bg[3].hofs.raw,
 | 
				
			||||||
 | 
					            0x0400_101E => bus.ppu.engines[1].bg[3].vofs.raw,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
					            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u8 => switch (address) {
 | 
					        u8 => switch (address) {
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DF => dma.read(T, &bus.dma, address) orelse 0x00,
 | 
				
			||||||
 | 
					            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(timer): read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0208 => @intFromBool(bus.io.ime),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_4000 => 0x00, // Lets software know this is NOT a DSi
 | 
				
			||||||
            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
					            else => warn("unexpected: read(T: {}, addr: 0x{X:0>8}) {} ", .{ T, address, T }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        else => @compileError(T ++ " is an unsupported bus read type"),
 | 
					        else => @compileError(T ++ " is an unsupported bus read type"),
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const subset = @import("../../util.zig").subset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
					pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
				
			||||||
    switch (T) {
 | 
					    switch (T) {
 | 
				
			||||||
        u32 => switch (address) {
 | 
					        u32 => switch (address) {
 | 
				
			||||||
            0x0400_0000 => bus.ppu.io.dispcnt_a.raw = value,
 | 
					            0x0400_0000 => bus.ppu.engines[0].dispcnt.raw = value,
 | 
				
			||||||
            0x0400_0180 => bus.io.shared.ipc_fifo.setIpcSync(.nds9, value),
 | 
					            0x0400_0004 => bus.ppu.io.nds9.dispstat.raw = @truncate(value),
 | 
				
			||||||
            0x0400_0184 => bus.io.shared.ipc_fifo.setIpcFifoCnt(.nds9, value),
 | 
					            0x0400_0008 => {
 | 
				
			||||||
            0x0400_0188 => bus.io.shared.ipc_fifo.send(.nds9, value) catch |e| std.debug.panic("IPC FIFO Error: {}", .{e}),
 | 
					                bus.ppu.engines[0].bg[0].cnt.raw = @truncate(value >> 0); // 0x0400_0008
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[1].cnt.raw = @truncate(value >> 16); // 0x0400_000A
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x0400_000C => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[2].cnt.raw = @truncate(value >> 0); // 0x0400_000A
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[3].cnt.raw = @truncate(value >> 16); // 0x0400_000C
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_0010 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[0].hofs.raw = @truncate(value >> 0); // 0x0400_0010
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[0].vofs.raw = @truncate(value >> 16); // 0x0400_0012
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_0014 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[1].hofs.raw = @truncate(value >> 0); // 0x0400_0014
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[1].vofs.raw = @truncate(value >> 16); // 0x0400_0016
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_0018 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[2].hofs.raw = @truncate(value >> 0); // 0x0400_0018
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[2].vofs.raw = @truncate(value >> 16); // 0x0400_001A
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_001C => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[3].hofs.raw = @truncate(value >> 0); // 0x0400_001C
 | 
				
			||||||
 | 
					                bus.ppu.engines[0].bg[3].vofs.raw = @truncate(value >> 16); // 0x0400_001E
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DC => dma.write(T, &bus.dma, address, 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(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),
 | 
				
			||||||
 | 
					            0x0400_0188 => bus.io.shr.ipc.send(.nds9, value),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
				
			||||||
 | 
					            0x0400_0210 => bus.io.ie.raw = value,
 | 
				
			||||||
 | 
					            0x0400_0214 => bus.io.irq.raw &= ~value,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0240 => {
 | 
					            0x0400_0240 => {
 | 
				
			||||||
                bus.ppu.vram.io.cnt_a.raw = @truncate(value >> 0); // 0x0400_0240
 | 
					                bus.ppu.vram.io.cnt_a.raw = @truncate(value >> 0); // 0x0400_0240
 | 
				
			||||||
                bus.ppu.vram.io.cnt_b.raw = @truncate(value >> 8); // 0x0400_0241
 | 
					                bus.ppu.vram.io.cnt_b.raw = @truncate(value >> 8); // 0x0400_0241
 | 
				
			||||||
                bus.ppu.vram.io.cnt_c.raw = @truncate(value >> 16); // 0x0400_0242
 | 
					                bus.ppu.vram.io.cnt_c.raw = @truncate(value >> 16); // 0x0400_0242
 | 
				
			||||||
                bus.ppu.vram.io.cnt_d.raw = @truncate(value >> 24); // 0x0400_0243
 | 
					                bus.ppu.vram.io.cnt_d.raw = @truncate(value >> 24); // 0x0400_0243
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                bus.ppu.vram.update();
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x0400_0244 => {
 | 
				
			||||||
 | 
					                bus.ppu.vram.io.cnt_e.raw = @truncate(value >> 0); // 0x0400_0244
 | 
				
			||||||
 | 
					                bus.ppu.vram.io.cnt_f.raw = @truncate(value >> 8); // 0x0400_0245
 | 
				
			||||||
 | 
					                bus.ppu.vram.io.cnt_g.raw = @truncate(value >> 16); // 0x0400_0246
 | 
				
			||||||
 | 
					                bus.io.shr.wramcnt.raw = @truncate(value >> 24); // 0x0400_0247
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                bus.ppu.vram.update();
 | 
				
			||||||
 | 
					                bus.wram.update(bus.io.shr.wramcnt);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
					            0x0400_0280 => {
 | 
				
			||||||
            0x0400_0210 => bus.io.ie.raw = value,
 | 
					                bus.io.div.cnt.raw = value;
 | 
				
			||||||
            0x0400_0214 => bus.io.irq.raw = value,
 | 
					                bus.io.div.schedule(bus.scheduler);
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0290 => {
 | 
					            0x0400_0290, 0x0400_0294 => {
 | 
				
			||||||
                bus.io.div.numerator = masks.mask(bus.io.div.numerator, value, 0xFFFF_FFFF);
 | 
					                bus.io.div.numerator = subset(u64, u32, address, bus.io.div.numerator, value);
 | 
				
			||||||
                bus.io.div.schedule(bus.scheduler);
 | 
					                bus.io.div.schedule(bus.scheduler);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            0x0400_0294 => {
 | 
					
 | 
				
			||||||
                bus.io.div.numerator = masks.mask(bus.io.div.numerator, @as(u64, value) << 32, 0xFFFF_FFFF << 32);
 | 
					            0x0400_0298, 0x0400_029C => {
 | 
				
			||||||
 | 
					                bus.io.div.denominator = subset(u64, u32, address, bus.io.div.denominator, value);
 | 
				
			||||||
                bus.io.div.schedule(bus.scheduler);
 | 
					                bus.io.div.schedule(bus.scheduler);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            0x0400_0298 => {
 | 
					
 | 
				
			||||||
                bus.io.div.denominator = masks.mask(bus.io.div.denominator, value, 0xFFFF_FFFF);
 | 
					            0x0400_02B0 => {
 | 
				
			||||||
                bus.io.div.schedule(bus.scheduler);
 | 
					                bus.io.sqrt.cnt.raw = value;
 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            0x0400_029C => {
 | 
					 | 
				
			||||||
                bus.io.div.denominator = masks.mask(bus.io.div.denominator, @as(u64, value) << 32, 0xFFFF_FFFF << 32);
 | 
					 | 
				
			||||||
                bus.io.div.schedule(bus.scheduler);
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            0x0400_02B8 => {
 | 
					 | 
				
			||||||
                bus.io.sqrt.param = masks.mask(bus.io.sqrt.param, value, 0xFFFF_FFFF);
 | 
					 | 
				
			||||||
                bus.io.sqrt.schedule(bus.scheduler);
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            0x0400_02BC => {
 | 
					 | 
				
			||||||
                bus.io.sqrt.param = masks.mask(bus.io.sqrt.param, @as(u64, value) << 32, 0xFFFF_FFFF << 32);
 | 
					 | 
				
			||||||
                bus.io.sqrt.schedule(bus.scheduler);
 | 
					                bus.io.sqrt.schedule(bus.scheduler);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0304 => bus.io.powcnt.raw = value,
 | 
					            0x0400_02B8, 0x0400_02BC => {
 | 
				
			||||||
 | 
					                bus.io.sqrt.param = subset(u64, u32, address, bus.io.sqrt.param, value);
 | 
				
			||||||
 | 
					                bus.io.sqrt.schedule(bus.scheduler);
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // Engine B
 | 
				
			||||||
 | 
					            0x0400_0304 => bus.ppu.io.powcnt.raw = value,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_1000 => bus.ppu.engines[1].dispcnt.raw = value,
 | 
				
			||||||
 | 
					            0x0400_1008 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[0].cnt.raw = @truncate(value >> 0); // 0x0400_1008
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[1].cnt.raw = @truncate(value >> 16); // 0x0400_100A
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x0400_100C => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[2].cnt.raw = @truncate(value >> 0); // 0x0400_100A
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[3].cnt.raw = @truncate(value >> 16); // 0x0400_100C
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_1010 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[0].hofs.raw = @truncate(value >> 0); // 0x0400_1010
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[0].vofs.raw = @truncate(value >> 16); // 0x0400_1012
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_1014 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[1].hofs.raw = @truncate(value >> 0); // 0x0400_1014
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[1].vofs.raw = @truncate(value >> 16); // 0x0400_1016
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_1018 => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[2].hofs.raw = @truncate(value >> 0); // 0x0400_1018
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[2].vofs.raw = @truncate(value >> 16); // 0x0400_101A
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					            0x00400_101C => {
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[3].hofs.raw = @truncate(value >> 0); // 0x0400_101C
 | 
				
			||||||
 | 
					                bus.ppu.engines[1].bg[3].vofs.raw = @truncate(value >> 16); // 0x0400_101E
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
					            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u16 => switch (address) {
 | 
					        u16 => switch (address) {
 | 
				
			||||||
            0x0400_0180 => bus.io.shared.ipc_fifo.setIpcSync(.nds9, value),
 | 
					            0x0400_0004 => bus.ppu.io.nds9.dispstat.raw = value,
 | 
				
			||||||
            0x0400_0184 => bus.io.shared.ipc_fifo.setIpcFifoCnt(.nds9, value),
 | 
					
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DE => dma.write(T, &bus.dma, address, 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(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),
 | 
				
			||||||
            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
					            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0280 => {
 | 
					            0x0400_0280 => {
 | 
				
			||||||
@@ -145,9 +281,34 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
				
			|||||||
                bus.io.sqrt.schedule(bus.scheduler);
 | 
					                bus.io.sqrt.schedule(bus.scheduler);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
					            0x0400_0304 => bus.ppu.io.powcnt.raw = value,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_1008 => bus.ppu.engines[1].bg[0].cnt.raw = value,
 | 
				
			||||||
 | 
					            0x0400_100A => bus.ppu.engines[1].bg[1].cnt.raw = value,
 | 
				
			||||||
 | 
					            0x0400_100C => bus.ppu.engines[1].bg[2].cnt.raw = value,
 | 
				
			||||||
 | 
					            0x0400_100E => bus.ppu.engines[1].bg[3].cnt.raw = value,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_1010 => bus.ppu.engines[1].bg[0].hofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_1012 => bus.ppu.engines[1].bg[0].vofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_1014 => bus.ppu.engines[1].bg[1].hofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_1016 => bus.ppu.engines[1].bg[1].vofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_1018 => bus.ppu.engines[1].bg[2].hofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_101A => bus.ppu.engines[1].bg[2].vofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_101C => bus.ppu.engines[1].bg[3].hofs.raw = value,
 | 
				
			||||||
 | 
					            0x0400_101E => bus.ppu.engines[1].bg[3].vofs.raw = value,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>4})", .{ T, address, value }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        u8 => switch (address) {
 | 
					        u8 => switch (address) {
 | 
				
			||||||
 | 
					            // DMA Transfers
 | 
				
			||||||
 | 
					            0x0400_00B0...0x0400_00DF => dma.write(T, &bus.dma, address, 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(timer): write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            0x0400_0208 => bus.io.ime = value & 1 == 1,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            0x0400_0240 => {
 | 
					            0x0400_0240 => {
 | 
				
			||||||
                bus.ppu.vram.io.cnt_a.raw = value;
 | 
					                bus.ppu.vram.io.cnt_a.raw = value;
 | 
				
			||||||
                bus.ppu.vram.update();
 | 
					                bus.ppu.vram.update();
 | 
				
			||||||
@@ -177,8 +338,8 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
				
			|||||||
                bus.ppu.vram.update();
 | 
					                bus.ppu.vram.update();
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            0x0400_0247 => {
 | 
					            0x0400_0247 => {
 | 
				
			||||||
                bus.io.shared.wramcnt.raw = value;
 | 
					                bus.io.shr.wramcnt.raw = value;
 | 
				
			||||||
                bus.wram.update(bus.io.shared.wramcnt);
 | 
					                bus.wram.update(bus.io.shr.wramcnt);
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
            0x0400_0248 => {
 | 
					            0x0400_0248 => {
 | 
				
			||||||
                bus.ppu.vram.io.cnt_h.raw = value;
 | 
					                bus.ppu.vram.io.cnt_h.raw = value;
 | 
				
			||||||
@@ -189,7 +350,7 @@ pub fn write(bus: *Bus, comptime T: type, address: u32, value: T) void {
 | 
				
			|||||||
                bus.ppu.vram.update();
 | 
					                bus.ppu.vram.update();
 | 
				
			||||||
            },
 | 
					            },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8})", .{ T, address, value }),
 | 
					            else => log.warn("unexpected: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>2})", .{ T, address, value }),
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
        else => @compileError(T ++ " is an unsupported bus write type"),
 | 
					        else => @compileError(T ++ " is an unsupported bus write type"),
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@@ -200,13 +361,13 @@ fn warn(comptime format: []const u8, args: anytype) u0 {
 | 
				
			|||||||
    return 0;
 | 
					    return 0;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const PowCnt = extern union {
 | 
					pub const PowCnt = extern union {
 | 
				
			||||||
    // Enable flag for both LCDs
 | 
					    // Enable flag for both LCDs
 | 
				
			||||||
    lcd: Bit(u32, 0),
 | 
					    lcd: Bit(u32, 0),
 | 
				
			||||||
    gfx_2da: Bit(u32, 1),
 | 
					    engine2d_a: Bit(u32, 1),
 | 
				
			||||||
    render_3d: Bit(u32, 2),
 | 
					    render3d: Bit(u32, 2),
 | 
				
			||||||
    geometry_3d: Bit(u32, 3),
 | 
					    geometry3d: Bit(u32, 3),
 | 
				
			||||||
    gfx_2db: Bit(u32, 9),
 | 
					    engine2d_b: Bit(u32, 9),
 | 
				
			||||||
    display_swap: Bit(u32, 15),
 | 
					    display_swap: Bit(u32, 15),
 | 
				
			||||||
    raw: u32,
 | 
					    raw: u32,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -341,8 +502,7 @@ const SquareRootUnit = struct {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub const DispcntA = extern union {
 | 
					pub const DispcntA = extern union {
 | 
				
			||||||
    bg_mode: Bitfield(u32, 0, 2),
 | 
					    bg_mode: Bitfield(u32, 0, 3),
 | 
				
			||||||
 | 
					 | 
				
			||||||
    /// toggle between 2D and 3D for BG0
 | 
					    /// toggle between 2D and 3D for BG0
 | 
				
			||||||
    bg0_dimension: Bit(u32, 3),
 | 
					    bg0_dimension: Bit(u32, 3),
 | 
				
			||||||
    tile_obj_mapping: Bit(u32, 4),
 | 
					    tile_obj_mapping: Bit(u32, 4),
 | 
				
			||||||
@@ -358,8 +518,26 @@ pub const DispcntA = extern union {
 | 
				
			|||||||
    tile_obj_1d_boundary: Bitfield(u32, 20, 2),
 | 
					    tile_obj_1d_boundary: Bitfield(u32, 20, 2),
 | 
				
			||||||
    bitmap_obj_1d_boundary: Bit(u32, 22),
 | 
					    bitmap_obj_1d_boundary: Bit(u32, 22),
 | 
				
			||||||
    obj_during_hblank: Bit(u32, 23),
 | 
					    obj_during_hblank: Bit(u32, 23),
 | 
				
			||||||
    character_base: Bitfield(u32, 24, 3),
 | 
					    char_base: Bitfield(u32, 24, 3),
 | 
				
			||||||
    screen_base: Bitfield(u32, 27, 2),
 | 
					    screen_base: Bitfield(u32, 27, 3),
 | 
				
			||||||
 | 
					    bg_ext_pal_enable: Bit(u32, 30),
 | 
				
			||||||
 | 
					    obj_ext_pal_enable: Bit(u32, 31),
 | 
				
			||||||
 | 
					    raw: u32,
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const DispcntB = extern union {
 | 
				
			||||||
 | 
					    bg_mode: Bitfield(u32, 0, 3),
 | 
				
			||||||
 | 
					    tile_obj_mapping: Bit(u32, 4),
 | 
				
			||||||
 | 
					    bitmap_obj_2d_dimension: Bit(u32, 5),
 | 
				
			||||||
 | 
					    bitmap_obj_mapping: Bit(u32, 6),
 | 
				
			||||||
 | 
					    forced_blank: Bit(u32, 7),
 | 
				
			||||||
 | 
					    bg_enable: Bitfield(u32, 8, 4),
 | 
				
			||||||
 | 
					    obj_enable: Bit(u32, 12),
 | 
				
			||||||
 | 
					    win_enable: Bitfield(u32, 13, 2),
 | 
				
			||||||
 | 
					    obj_win_enable: Bit(u32, 15),
 | 
				
			||||||
 | 
					    display_mode: Bitfield(u32, 16, 2),
 | 
				
			||||||
 | 
					    tile_obj_1d_boundary: Bitfield(u32, 20, 2),
 | 
				
			||||||
 | 
					    obj_during_hblank: Bit(u32, 23),
 | 
				
			||||||
    bg_ext_pal_enable: Bit(u32, 30),
 | 
					    bg_ext_pal_enable: Bit(u32, 30),
 | 
				
			||||||
    obj_ext_pal_enable: Bit(u32, 31),
 | 
					    obj_ext_pal_enable: Bit(u32, 31),
 | 
				
			||||||
    raw: u32,
 | 
					    raw: u32,
 | 
				
			||||||
@@ -417,44 +595,33 @@ pub const Dispstat = extern union {
 | 
				
			|||||||
    raw: u16,
 | 
					    raw: u16,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// Read Only
 | 
					pub const Bgcnt = extern union {
 | 
				
			||||||
/// 0 = Pressed, 1 = Released
 | 
					    priority: Bitfield(u16, 0, 2),
 | 
				
			||||||
pub const KeyInput = extern union {
 | 
					    char_base: Bitfield(u16, 2, 4),
 | 
				
			||||||
    a: Bit(u16, 0),
 | 
					    mosaic_enable: Bit(u16, 6),
 | 
				
			||||||
    b: Bit(u16, 1),
 | 
					    colour_mode: Bit(u16, 7),
 | 
				
			||||||
    select: Bit(u16, 2),
 | 
					    screen_base: Bitfield(u16, 8, 5),
 | 
				
			||||||
    start: Bit(u16, 3),
 | 
					    display_overflow: Bit(u16, 13),
 | 
				
			||||||
    right: Bit(u16, 4),
 | 
					    size: Bitfield(u16, 14, 2),
 | 
				
			||||||
    left: Bit(u16, 5),
 | 
					 | 
				
			||||||
    up: Bit(u16, 6),
 | 
					 | 
				
			||||||
    down: Bit(u16, 7),
 | 
					 | 
				
			||||||
    shoulder_r: Bit(u16, 8),
 | 
					 | 
				
			||||||
    shoulder_l: Bit(u16, 9),
 | 
					 | 
				
			||||||
    raw: u16,
 | 
					    raw: u16,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const AtomicKeyInput = struct {
 | 
					/// Write Only
 | 
				
			||||||
    const Self = @This();
 | 
					const BackgroundOffset = extern union {
 | 
				
			||||||
    const Ordering = std.atomic.Ordering;
 | 
					    offset: Bitfield(u16, 0, 9),
 | 
				
			||||||
 | 
					    raw: u16,
 | 
				
			||||||
    inner: KeyInput = .{ .raw = 0x03FF },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    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),
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub inline fn fetchOr(self: *Self, value: u16, comptime ordering: Ordering) void {
 | 
					 | 
				
			||||||
        _ = @atomicRmw(u16, &self.inner.raw, .Or, value, ordering);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub inline fn fetchAnd(self: *Self, value: u16, comptime ordering: Ordering) void {
 | 
					 | 
				
			||||||
        _ = @atomicRmw(u16, &self.inner.raw, .And, value, ordering);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Interrupt = extern union {
 | 
					pub const Hofs = BackgroundOffset;
 | 
				
			||||||
    raw: u32,
 | 
					pub const Vofs = BackgroundOffset;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const DmaCnt = extern union {
 | 
				
			||||||
 | 
					    dad_adj: Bitfield(u16, 5, 2),
 | 
				
			||||||
 | 
					    sad_adj: Bitfield(u16, 7, 2),
 | 
				
			||||||
 | 
					    repeat: Bit(u16, 9),
 | 
				
			||||||
 | 
					    transfer_type: Bit(u16, 10),
 | 
				
			||||||
 | 
					    start_timing: Bitfield(u16, 11, 3),
 | 
				
			||||||
 | 
					    irq: Bit(u16, 14),
 | 
				
			||||||
 | 
					    enabled: Bit(u16, 15),
 | 
				
			||||||
 | 
					    raw: u16,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										533
									
								
								src/core/ppu.zig
									
									
									
									
									
								
							
							
						
						
									
										533
									
								
								src/core/ppu.zig
									
									
									
									
									
								
							@@ -4,6 +4,17 @@ const Allocator = std.mem.Allocator;
 | 
				
			|||||||
const Scheduler = @import("Scheduler.zig");
 | 
					const Scheduler = @import("Scheduler.zig");
 | 
				
			||||||
const System = @import("emu.zig").System;
 | 
					const System = @import("emu.zig").System;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Vram = @import("ppu/Vram.zig");
 | 
				
			||||||
 | 
					const Oam = @import("ppu/Oam.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const EngineA = @import("ppu/engine.zig").EngineA;
 | 
				
			||||||
 | 
					const EngineB = @import("ppu/engine.zig").EngineB;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const dma7 = @import("nds7/dma.zig");
 | 
				
			||||||
 | 
					const dma9 = @import("nds9/dma.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const handleInterrupt = @import("emu.zig").handleInterrupt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub const screen_width = 256;
 | 
					pub const screen_width = 256;
 | 
				
			||||||
pub const screen_height = 192;
 | 
					pub const screen_height = 192;
 | 
				
			||||||
const KiB = 0x400;
 | 
					const KiB = 0x400;
 | 
				
			||||||
@@ -15,91 +26,135 @@ pub const Ppu = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    vram: *Vram,
 | 
					    vram: *Vram,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // FIXME: do I need a pointer here?
 | 
				
			||||||
 | 
					    oam: *Oam,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    engines: struct { EngineA, EngineB },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    io: Io = .{},
 | 
					    io: Io = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const Io = struct {
 | 
					    pub const Io = struct {
 | 
				
			||||||
        const nds9 = @import("nds9/io.zig");
 | 
					        const ty = @import("nds9/io.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// Read / Write
 | 
					        nds9: struct {
 | 
				
			||||||
        dispcnt_a: nds9.DispcntA = .{ .raw = 0x0000_0000 },
 | 
					            dispstat: ty.Dispstat = .{ .raw = 0x00000 },
 | 
				
			||||||
        /// Read / Write
 | 
					            vcount: ty.Vcount = .{ .raw = 0x0000 },
 | 
				
			||||||
        dispstat: nds9.Dispstat = .{ .raw = 0x0000 },
 | 
					        } = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        /// Read-Only
 | 
					        nds7: struct {
 | 
				
			||||||
        vcount: nds9.Vcount = .{ .raw = 0x0000 },
 | 
					            dispstat: ty.Dispstat = .{ .raw = 0x0000 },
 | 
				
			||||||
 | 
					            vcount: ty.Vcount = .{ .raw = 0x00000 },
 | 
				
			||||||
 | 
					        } = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        powcnt: ty.PowCnt = .{ .raw = 0x0000_0000 },
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn init(allocator: Allocator, vram: *Vram) !@This() {
 | 
					    pub fn init(allocator: Allocator, vram: *Vram) !@This() {
 | 
				
			||||||
        return .{
 | 
					        return .{
 | 
				
			||||||
            .fb = try FrameBuffer.init(allocator),
 | 
					            .fb = try FrameBuffer.init(allocator),
 | 
				
			||||||
 | 
					            .engines = .{ try EngineA.init(allocator), try EngineB.init(allocator) },
 | 
				
			||||||
            .vram = vram,
 | 
					            .vram = vram,
 | 
				
			||||||
 | 
					            .oam = blk: {
 | 
				
			||||||
 | 
					                var oam = try allocator.create(Oam);
 | 
				
			||||||
 | 
					                oam.init();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                break :blk oam;
 | 
				
			||||||
 | 
					            },
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
					    pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
				
			||||||
        self.fb.deinit(allocator);
 | 
					        self.fb.deinit(allocator);
 | 
				
			||||||
 | 
					        inline for (self.engines) |eng| eng.deinit(allocator);
 | 
				
			||||||
 | 
					        allocator.destroy(self.oam);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn drawScanline(self: *@This(), bus: *System.Bus9) void {
 | 
					    pub fn drawScanline(self: *@This(), bus: *System.Bus9) void {
 | 
				
			||||||
        const bg_mode = self.io.dispcnt_a.display_mode.read();
 | 
					        if (self.io.powcnt.engine2d_a.read())
 | 
				
			||||||
        const scanline = self.io.vcount.scanline.read();
 | 
					            self.engines[0].drawScanline(bus, &self.fb);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        switch (bg_mode) {
 | 
					        if (self.io.powcnt.engine2d_b.read())
 | 
				
			||||||
            0x0 => {},
 | 
					            self.engines[1].drawScanline(bus, &self.fb);
 | 
				
			||||||
            0x1 => {},
 | 
					 | 
				
			||||||
            0x2 => {
 | 
					 | 
				
			||||||
                // Draw Top Screen
 | 
					 | 
				
			||||||
                {
 | 
					 | 
				
			||||||
                    const buf = self.fb.top(.back);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    const ptr: *[screen_width * screen_height]u32 = @ptrCast(@alignCast(buf.ptr));
 | 
					 | 
				
			||||||
                    const scanline_ptr = ptr[screen_width * @as(u32, scanline) ..][0..screen_width];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    const base_addr: u32 = 0x0680_0000 + (screen_width * @sizeOf(u16)) * @as(u32, scanline);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    // FIXME: I don't think it's okay to be accessing the ARM9 Bus instead of just working with
 | 
					 | 
				
			||||||
                    // memory directly. However, I do understand that VRAM-A, VRAM-B might change things
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                    for (scanline_ptr, 0..) |*rgba, i| {
 | 
					 | 
				
			||||||
                        const addr = base_addr + @as(u32, @intCast(i)) * @sizeOf(u16);
 | 
					 | 
				
			||||||
                        rgba.* = rgba888(bus.dbgRead(u16, addr));
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            0x3 => {},
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// HDraw -> HBlank
 | 
					    /// HDraw -> HBlank
 | 
				
			||||||
    pub fn onHdrawEnd(self: *@This(), scheduler: *Scheduler, late: u64) void {
 | 
					    pub fn onHdrawEnd(self: *@This(), system: System, scheduler: *Scheduler, late: u64) void {
 | 
				
			||||||
 | 
					        if (self.io.nds9.dispstat.hblank_irq.read()) {
 | 
				
			||||||
 | 
					            system.bus9.io.irq.hblank.set();
 | 
				
			||||||
 | 
					            handleInterrupt(.nds9, system.arm946es);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (self.io.nds7.dispstat.hblank_irq.read()) {
 | 
				
			||||||
 | 
					            system.bus7.io.irq.hblank.set();
 | 
				
			||||||
 | 
					            handleInterrupt(.nds7, system.arm7tdmi);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (!self.io.nds9.dispstat.vblank.read()) { // ensure we aren't in VBlank
 | 
				
			||||||
 | 
					            dma9.onHblank(system.bus9);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.io.nds9.dispstat.hblank.set();
 | 
				
			||||||
 | 
					        self.io.nds7.dispstat.hblank.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const dots_in_hblank = 99;
 | 
					        const dots_in_hblank = 99;
 | 
				
			||||||
        std.debug.assert(self.io.dispstat.hblank.read() == false);
 | 
					 | 
				
			||||||
        std.debug.assert(self.io.dispstat.vblank.read() == false);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // TODO: Signal HBlank IRQ
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        self.io.dispstat.hblank.set();
 | 
					 | 
				
			||||||
        scheduler.push(.{ .nds9 = .hblank }, dots_in_hblank * cycles_per_dot -| late);
 | 
					        scheduler.push(.{ .nds9 = .hblank }, dots_in_hblank * cycles_per_dot -| late);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn onHblankEnd(self: *@This(), scheduler: *Scheduler, late: u64) void {
 | 
					    /// VBlank -> HBlank (Still VBlank)
 | 
				
			||||||
        const scanline_count = 192 + 71;
 | 
					    pub fn onVblankEnd(self: *@This(), system: System, scheduler: *Scheduler, late: u64) void {
 | 
				
			||||||
 | 
					        if (self.io.nds9.dispstat.hblank_irq.read()) {
 | 
				
			||||||
 | 
					            system.bus9.io.irq.hblank.set();
 | 
				
			||||||
 | 
					            handleInterrupt(.nds9, system.arm946es);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const prev_scanline = self.io.vcount.scanline.read();
 | 
					        if (self.io.nds7.dispstat.hblank_irq.read()) {
 | 
				
			||||||
        const scanline = (prev_scanline + 1) % scanline_count;
 | 
					            system.bus7.io.irq.hblank.set();
 | 
				
			||||||
 | 
					            handleInterrupt(.nds7, system.arm7tdmi);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        self.io.vcount.scanline.write(scanline);
 | 
					        self.io.nds9.dispstat.hblank.set();
 | 
				
			||||||
        self.io.dispstat.hblank.unset();
 | 
					        self.io.nds7.dispstat.hblank.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const coincidence = scanline == self.io.dispstat.lyc.read();
 | 
					        // TODO: Run DMAs on HBlank
 | 
				
			||||||
        self.io.dispstat.coincidence.write(coincidence);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // TODO: LYC == LY IRQ
 | 
					        const dots_in_hblank = 99;
 | 
				
			||||||
 | 
					        scheduler.push(.{ .nds9 = .hblank }, dots_in_hblank * cycles_per_dot -| late);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// HBlank -> HDraw / VBlank
 | 
				
			||||||
 | 
					    pub fn onHblankEnd(self: *@This(), system: System, scheduler: *Scheduler, late: u64) void {
 | 
				
			||||||
 | 
					        const scanline_total = 263; // 192 visible, 71 blanking
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const prev_scanline = self.io.nds9.vcount.scanline.read();
 | 
				
			||||||
 | 
					        const scanline = (prev_scanline + 1) % scanline_total;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.io.nds9.vcount.scanline.write(scanline);
 | 
				
			||||||
 | 
					        self.io.nds7.vcount.scanline.write(scanline);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        self.io.nds9.dispstat.hblank.unset();
 | 
				
			||||||
 | 
					        self.io.nds7.dispstat.hblank.unset();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            const coincidence = scanline == self.io.nds9.dispstat.lyc.read();
 | 
				
			||||||
 | 
					            self.io.nds9.dispstat.coincidence.write(coincidence);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (coincidence and self.io.nds9.dispstat.vcount_irq.read()) {
 | 
				
			||||||
 | 
					                system.bus9.io.irq.coincidence.set();
 | 
				
			||||||
 | 
					                handleInterrupt(.nds9, system.arm946es);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        {
 | 
				
			||||||
 | 
					            const coincidence = scanline == self.io.nds7.dispstat.lyc.read();
 | 
				
			||||||
 | 
					            self.io.nds7.dispstat.coincidence.write(coincidence);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (coincidence and self.io.nds7.dispstat.vcount_irq.read()) {
 | 
				
			||||||
 | 
					                system.bus7.io.irq.coincidence.set();
 | 
				
			||||||
 | 
					                handleInterrupt(.nds7, system.arm7tdmi);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (scanline < 192) {
 | 
					        if (scanline < 192) {
 | 
				
			||||||
            std.debug.assert(self.io.dispstat.vblank.read() == false);
 | 
					 | 
				
			||||||
            std.debug.assert(self.io.dispstat.hblank.read() == false);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            // Draw Another Scanline
 | 
					            // Draw Another Scanline
 | 
				
			||||||
            const dots_in_hdraw = 256;
 | 
					            const dots_in_hdraw = 256;
 | 
				
			||||||
            return scheduler.push(.{ .nds9 = .draw }, dots_in_hdraw * cycles_per_dot -| late);
 | 
					            return scheduler.push(.{ .nds9 = .draw }, dots_in_hdraw * cycles_per_dot -| late);
 | 
				
			||||||
@@ -108,16 +163,35 @@ pub const Ppu = struct {
 | 
				
			|||||||
        if (scanline == 192) {
 | 
					        if (scanline == 192) {
 | 
				
			||||||
            // Transition from Hblank to Vblank
 | 
					            // Transition from Hblank to Vblank
 | 
				
			||||||
            self.fb.swap();
 | 
					            self.fb.swap();
 | 
				
			||||||
            self.io.dispstat.vblank.set();
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
            // TODO: Signal VBlank IRQ
 | 
					            if (self.io.nds9.dispstat.vblank_irq.read()) {
 | 
				
			||||||
 | 
					                system.bus9.io.irq.vblank.set();
 | 
				
			||||||
 | 
					                handleInterrupt(.nds9, system.arm946es);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (scanline == 262) self.io.dispstat.vblank.unset();
 | 
					            if (self.io.nds7.dispstat.vblank_irq.read()) {
 | 
				
			||||||
        std.debug.assert(self.io.dispstat.vblank.read() == (scanline != 262));
 | 
					                system.bus7.io.irq.vblank.set();
 | 
				
			||||||
 | 
					                handleInterrupt(.nds7, system.arm7tdmi);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const dots_in_scanline = 256 + 99;
 | 
					            self.io.nds9.dispstat.vblank.set();
 | 
				
			||||||
        scheduler.push(.{ .nds9 = .hblank }, dots_in_scanline * cycles_per_dot -| late);
 | 
					            self.io.nds7.dispstat.vblank.set();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // TODO: Affine BG Latches
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            dma7.onVblank(system.bus7);
 | 
				
			||||||
 | 
					            dma9.onVblank(system.bus9);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // TODO: VBlank DMA9 Transfers
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (scanline == 262) {
 | 
				
			||||||
 | 
					            self.io.nds9.dispstat.vblank.unset();
 | 
				
			||||||
 | 
					            self.io.nds7.dispstat.vblank.unset();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const dots_in_vblank = 256;
 | 
				
			||||||
 | 
					        scheduler.push(.{ .nds9 = .vblank }, dots_in_vblank * cycles_per_dot -| late);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -126,15 +200,15 @@ pub const FrameBuffer = struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    current: u1 = 0,
 | 
					    current: u1 = 0,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ptr: *[len * 4]u8,
 | 
					    ptr: *align(@sizeOf(u32)) [len * 4]u8,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const Position = enum { top, bottom };
 | 
					    const Position = enum { top, bottom };
 | 
				
			||||||
    const Layer = enum { front, back };
 | 
					    const Layer = enum { front, back };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn init(allocator: Allocator) !@This() {
 | 
					    pub fn init(allocator: Allocator) !@This() {
 | 
				
			||||||
        const ptr = try allocator.create([len * 4]u8);
 | 
					        const buf = try allocator.alignedAlloc(u8, @sizeOf(u32), len * 4);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return .{ .ptr = ptr };
 | 
					        return .{ .ptr = buf[0 .. len * 4] };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
					    pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
				
			||||||
@@ -162,338 +236,3 @@ pub const FrameBuffer = struct {
 | 
				
			|||||||
        return self.get(.bottom, layer);
 | 
					        return self.get(.bottom, layer);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					 | 
				
			||||||
inline fn rgba888(bgr555: u16) u32 {
 | 
					 | 
				
			||||||
    const b: u32 = bgr555 >> 10 & 0x1F;
 | 
					 | 
				
			||||||
    const g: u32 = bgr555 >> 5 & 0x1F;
 | 
					 | 
				
			||||||
    const r: u32 = bgr555 & 0x1F;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // zig fmt: off
 | 
					 | 
				
			||||||
    return (r << 3 | r >> 2) << 24
 | 
					 | 
				
			||||||
        |  (g << 3 | g >> 2) << 16
 | 
					 | 
				
			||||||
        |  (b << 3 | b >> 2) << 8
 | 
					 | 
				
			||||||
        |  0xFF;
 | 
					 | 
				
			||||||
    // zig fmt: on
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
pub const Vram = struct {
 | 
					 | 
				
			||||||
    const page_size = 16 * KiB; // smallest allocation is 16 KiB
 | 
					 | 
				
			||||||
    const addr_space_size = 0x0100_0000; // 0x0600_0000 -> 0x06FF_FFFF (inclusive)
 | 
					 | 
				
			||||||
    const table_len = addr_space_size / page_size;
 | 
					 | 
				
			||||||
    const buf_len = 656 * KiB;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const IntFittingRange = std.math.IntFittingRange;
 | 
					 | 
				
			||||||
    const log = std.log.scoped(.vram);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    io: Io = .{},
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    _buf: *[buf_len]u8,
 | 
					 | 
				
			||||||
    nds9_table: *const [table_len]?[*]u8,
 | 
					 | 
				
			||||||
    nds7_table: *const [table_len]?[*]u8,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const Io = struct {
 | 
					 | 
				
			||||||
        const nds9 = @import("nds9/io.zig");
 | 
					 | 
				
			||||||
        const nds7 = @import("nds7/io.zig");
 | 
					 | 
				
			||||||
        pub const Vramstat = @import("nds7/io.zig").Vramstat;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        stat: nds7.Vramstat = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /// Write-Only (according to melonDS these are readable lol)
 | 
					 | 
				
			||||||
        cnt_a: nds9.Vramcnt.A = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_b: nds9.Vramcnt.A = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_c: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_d: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_e: nds9.Vramcnt.E = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_f: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_g: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_h: nds9.Vramcnt.H = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
        cnt_i: nds9.Vramcnt.H = .{ .raw = 0x00 },
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn init(self: *@This(), allocator: Allocator) !void {
 | 
					 | 
				
			||||||
        const buf = try allocator.create([buf_len]u8);
 | 
					 | 
				
			||||||
        errdefer allocator.destroy(buf);
 | 
					 | 
				
			||||||
        @memset(buf, 0);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const tables = try allocator.alloc(?[*]u8, 2 * table_len);
 | 
					 | 
				
			||||||
        @memset(tables, null);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        self.* = .{
 | 
					 | 
				
			||||||
            .nds9_table = tables[0..table_len],
 | 
					 | 
				
			||||||
            .nds7_table = tables[table_len .. 2 * table_len],
 | 
					 | 
				
			||||||
            ._buf = buf,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        // ROMS like redpanda.nds won't write to VRAMCNT before trying to write to VRAM
 | 
					 | 
				
			||||||
        // therefore we assume some default allocation (in this casee VRAMCNT_A -> VRAMCNT_I are 0x00)
 | 
					 | 
				
			||||||
        self.update();
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
					 | 
				
			||||||
        allocator.destroy(self._buf);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        const ptr: [*]?[*]const u8 = @ptrCast(@constCast(self.nds9_table));
 | 
					 | 
				
			||||||
        allocator.free(ptr[0 .. 2 * table_len]);
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn stat(self: *const @This()) Io.Vramstat {
 | 
					 | 
				
			||||||
        const vram_c: u8 = @intFromBool(self.io.cnt_c.enable.read() and self.io.cnt_c.mst.read() == 2);
 | 
					 | 
				
			||||||
        const vram_d: u8 = @intFromBool(self.io.cnt_d.enable.read() and self.io.cnt_d.mst.read() == 2);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return .{ .raw = (vram_d << 1) | vram_c };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    const Range = struct { min: u32, max: u32 };
 | 
					 | 
				
			||||||
    const Kind = enum {
 | 
					 | 
				
			||||||
        a,
 | 
					 | 
				
			||||||
        b,
 | 
					 | 
				
			||||||
        c,
 | 
					 | 
				
			||||||
        d,
 | 
					 | 
				
			||||||
        e,
 | 
					 | 
				
			||||||
        f,
 | 
					 | 
				
			||||||
        g,
 | 
					 | 
				
			||||||
        h,
 | 
					 | 
				
			||||||
        i,
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        /// In Bytes
 | 
					 | 
				
			||||||
        inline fn size(self: @This()) u32 {
 | 
					 | 
				
			||||||
            return switch (self) {
 | 
					 | 
				
			||||||
                .a => 128 * KiB,
 | 
					 | 
				
			||||||
                .b => 128 * KiB,
 | 
					 | 
				
			||||||
                .c => 128 * KiB,
 | 
					 | 
				
			||||||
                .d => 128 * KiB,
 | 
					 | 
				
			||||||
                .e => 64 * KiB,
 | 
					 | 
				
			||||||
                .f => 16 * KiB,
 | 
					 | 
				
			||||||
                .g => 16 * KiB,
 | 
					 | 
				
			||||||
                .h => 32 * KiB,
 | 
					 | 
				
			||||||
                .i => 16 * KiB,
 | 
					 | 
				
			||||||
            };
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    /// max inclusive
 | 
					 | 
				
			||||||
    fn range(comptime kind: Kind, mst: u3, offset: u2) Range {
 | 
					 | 
				
			||||||
        const ofs: u32 = offset;
 | 
					 | 
				
			||||||
        // panic messages are from GBATEK
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return switch (kind) {
 | 
					 | 
				
			||||||
            .a => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0680_0000, .max = 0x0682_0000 },
 | 
					 | 
				
			||||||
                1 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0002_0000 * ofs);
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                2 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0640_0000 + (0x0002_0000 * (ofs & 0b01));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_A: Slot OFS(0-3)"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .b => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0682_0000, .max = 0x0684_0000 },
 | 
					 | 
				
			||||||
                1 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0002_0000 * ofs);
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                2 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0640_0000 + (0x0002_0000 * (ofs & 0b01));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_B: Slot OFS(0-3)"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .c => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0684_0000, .max = 0x0686_0000 },
 | 
					 | 
				
			||||||
                1 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0002_0000 * ofs);
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                2 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0002_0000 * (ofs & 0b01));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_C: Slot OFS(0-3)"),
 | 
					 | 
				
			||||||
                4 => .{ .min = 0x0620_0000, .max = 0x0620_0000 + kind.size() },
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .d => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0686_0000, .max = 0x0688_0000 },
 | 
					 | 
				
			||||||
                1 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0002_0000 * ofs);
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                2 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0002_0000 * (ofs & 0b01));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_D: Slot OFS(0-3)"),
 | 
					 | 
				
			||||||
                4 => .{ .min = 0x0660_0000, .max = 0x0660_0000 + kind.size() },
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .e => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0688_0000, .max = 0x0689_0000 },
 | 
					 | 
				
			||||||
                1 => .{ .min = 0x0600_0000, .max = 0x0600_0000 + kind.size() },
 | 
					 | 
				
			||||||
                2 => .{ .min = 0x0640_0000, .max = 0x0640_0000 + kind.size() },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_E: Slots 0-3"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .f => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0689_0000, .max = 0x0689_4000 },
 | 
					 | 
				
			||||||
                1 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                2 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0640_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_F: Slot (OFS.0*1)+(OFS.1*4)"),
 | 
					 | 
				
			||||||
                4 => @panic("VRAMCNT_F: Slot 0-1 (OFS=0), Slot 2-3 (OFS=1)"),
 | 
					 | 
				
			||||||
                5 => @panic("VRAMCNT_F: Slot 0"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .g => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0689_4000, .max = 0x0689_8000 },
 | 
					 | 
				
			||||||
                1 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0600_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                2 => blk: {
 | 
					 | 
				
			||||||
                    const base = 0x0640_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1));
 | 
					 | 
				
			||||||
                    break :blk .{ .min = base, .max = base + kind.size() };
 | 
					 | 
				
			||||||
                },
 | 
					 | 
				
			||||||
                3 => @panic("VRAMCNT_G: Slot (OFS.0*1)+(OFS.1*4)"),
 | 
					 | 
				
			||||||
                4 => @panic("VRAMCNT_G: Slot 0-1 (OFS=0), Slot 2-3 (OFS=1)"),
 | 
					 | 
				
			||||||
                5 => @panic("VRAMCNT_G: Slot 0"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .h => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x0689_8000, .max = 0x068A_0000 },
 | 
					 | 
				
			||||||
                1 => .{ .min = 0x0620_0000, .max = 0x0620_0000 + kind.size() },
 | 
					 | 
				
			||||||
                2 => @panic("VRAMCNT_H: Slot 0-3"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
            .i => switch (mst) {
 | 
					 | 
				
			||||||
                0 => .{ .min = 0x068A_0000, .max = 0x068A_4000 },
 | 
					 | 
				
			||||||
                1 => .{ .min = 0x0620_8000, .max = 0x0620_8000 + kind.size() },
 | 
					 | 
				
			||||||
                2 => .{ .min = 0x0660_0000, .max = 0x0660_0000 + kind.size() },
 | 
					 | 
				
			||||||
                3 => @panic("Slot 0"),
 | 
					 | 
				
			||||||
                else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
					 | 
				
			||||||
            },
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn buf_offset(comptime kind: Kind) usize {
 | 
					 | 
				
			||||||
        // zig fmt: off
 | 
					 | 
				
			||||||
        return switch (kind) {
 | 
					 | 
				
			||||||
            .a => 0,                                                            // 0x00000
 | 
					 | 
				
			||||||
            .b => (128 * KiB) * 1,                                              // 0x20000 (+ 0x20000)
 | 
					 | 
				
			||||||
            .c => (128 * KiB) * 2,                                              // 0x40000 (+ 0x20000)
 | 
					 | 
				
			||||||
            .d => (128 * KiB) * 3,                                              // 0x60000 (+ 0x20000)
 | 
					 | 
				
			||||||
            .e => (128 * KiB) * 4,                                              // 0x80000 (+ 0x20000)
 | 
					 | 
				
			||||||
            .f => (128 * KiB) * 4 + (64 * KiB),                                 // 0x90000 (+ 0x10000)
 | 
					 | 
				
			||||||
            .g => (128 * KiB) * 4 + (64 * KiB) + (16 * KiB) * 1,                // 0x94000 (+ 0x04000)
 | 
					 | 
				
			||||||
            .h => (128 * KiB) * 4 + (64 * KiB) + (16 * KiB) * 2,                // 0x98000 (+ 0x04000)
 | 
					 | 
				
			||||||
            .i => (128 * KiB) * 4 + (64 * KiB) + (16 * KiB) * 2 + (32 * KiB)    // 0xA0000 (+ 0x08000)
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
        // zig fmt: on
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn CntType(comptime kind: Kind) type {
 | 
					 | 
				
			||||||
        const io = @import("nds9/io.zig");
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        return switch (kind) {
 | 
					 | 
				
			||||||
            .a => io.Vramcnt.A,
 | 
					 | 
				
			||||||
            .b => io.Vramcnt.A,
 | 
					 | 
				
			||||||
            .c => io.Vramcnt.C,
 | 
					 | 
				
			||||||
            .d => io.Vramcnt.C,
 | 
					 | 
				
			||||||
            .e => io.Vramcnt.E,
 | 
					 | 
				
			||||||
            .f => io.Vramcnt.C,
 | 
					 | 
				
			||||||
            .g => io.Vramcnt.C,
 | 
					 | 
				
			||||||
            .h => io.Vramcnt.H,
 | 
					 | 
				
			||||||
            .i => io.Vramcnt.H,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn cntValue(self: *const @This(), comptime kind: Kind) CntType(kind) {
 | 
					 | 
				
			||||||
        return switch (kind) {
 | 
					 | 
				
			||||||
            .a => self.io.cnt_a,
 | 
					 | 
				
			||||||
            .b => self.io.cnt_b,
 | 
					 | 
				
			||||||
            .c => self.io.cnt_c,
 | 
					 | 
				
			||||||
            .d => self.io.cnt_d,
 | 
					 | 
				
			||||||
            .e => self.io.cnt_e,
 | 
					 | 
				
			||||||
            .f => self.io.cnt_f,
 | 
					 | 
				
			||||||
            .g => self.io.cnt_g,
 | 
					 | 
				
			||||||
            .h => self.io.cnt_h,
 | 
					 | 
				
			||||||
            .i => self.io.cnt_i,
 | 
					 | 
				
			||||||
        };
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn update(self: *@This()) void {
 | 
					 | 
				
			||||||
        const nds9_tbl = @constCast(self.nds9_table);
 | 
					 | 
				
			||||||
        const nds7_tbl = @constCast(self.nds7_table);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        for (nds9_tbl, nds7_tbl, 0..) |*nds9_ptr, *nds7_ptr, i| {
 | 
					 | 
				
			||||||
            const addr = 0x0600_0000 + (i * page_size);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            inline for (std.meta.fields(Kind)) |f| {
 | 
					 | 
				
			||||||
                const kind = @field(Kind, f.name);
 | 
					 | 
				
			||||||
                const cnt = cntValue(self, kind);
 | 
					 | 
				
			||||||
                const ofs = switch (kind) {
 | 
					 | 
				
			||||||
                    .e, .h, .i => 0,
 | 
					 | 
				
			||||||
                    else => cnt.offset.read(),
 | 
					 | 
				
			||||||
                };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                const rnge = range(kind, cnt.mst.read(), ofs);
 | 
					 | 
				
			||||||
                const offset = addr & (kind.size() - 1);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                if (rnge.min <= addr and addr < rnge.max) {
 | 
					 | 
				
			||||||
                    if ((kind == .c or kind == .d) and cnt.mst.read() == 2) {
 | 
					 | 
				
			||||||
                        // Allocate to ARM7
 | 
					 | 
				
			||||||
                        nds7_ptr.* = self._buf[buf_offset(kind) + offset ..].ptr;
 | 
					 | 
				
			||||||
                    } else {
 | 
					 | 
				
			||||||
                        nds9_ptr.* = self._buf[buf_offset(kind) + offset ..].ptr;
 | 
					 | 
				
			||||||
                    }
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    // TODO: Rename
 | 
					 | 
				
			||||||
    const Device = enum { nds9, nds7 };
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn read(self: @This(), comptime T: type, comptime dev: Device, address: u32) T {
 | 
					 | 
				
			||||||
        const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
					 | 
				
			||||||
        const masked_addr = address & (addr_space_size - 1);
 | 
					 | 
				
			||||||
        const page = masked_addr >> bits;
 | 
					 | 
				
			||||||
        const offset = masked_addr & (page_size - 1);
 | 
					 | 
				
			||||||
        const table = if (dev == .nds9) self.nds9_table else self.nds7_table;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (table[page]) |some_ptr| {
 | 
					 | 
				
			||||||
            const ptr: [*]const T = @ptrCast(@alignCast(some_ptr));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return ptr[offset / @sizeOf(T)];
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        log.err("{s}: read(T: {}, addr: 0x{X:0>8}) was in un-mapped VRAM space", .{ @tagName(dev), T, address });
 | 
					 | 
				
			||||||
        return 0x00;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    pub fn write(self: *@This(), comptime T: type, comptime dev: Device, address: u32, value: T) void {
 | 
					 | 
				
			||||||
        const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
					 | 
				
			||||||
        const masked_addr = address & (addr_space_size - 1);
 | 
					 | 
				
			||||||
        const page = masked_addr >> bits;
 | 
					 | 
				
			||||||
        const offset = masked_addr & (page_size - 1);
 | 
					 | 
				
			||||||
        const table = if (dev == .nds9) self.nds9_table else self.nds7_table;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        if (table[page]) |some_ptr| {
 | 
					 | 
				
			||||||
            const ptr: [*]T = @ptrCast(@alignCast(some_ptr));
 | 
					 | 
				
			||||||
            ptr[offset / @sizeOf(T)] = value;
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        log.err("{s}: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8}) was in un-mapped VRA< space", .{ @tagName(dev), T, address, value });
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										7
									
								
								src/core/ppu/Oam.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								src/core/ppu/Oam.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					const KiB = 0x400;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					buf: [2 * KiB]u8,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn init(self: *@This()) void {
 | 
				
			||||||
 | 
					    @memset(self.buf[0..], 0);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										290
									
								
								src/core/ppu/Vram.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										290
									
								
								src/core/ppu/Vram.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,290 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					const KiB = 0x400;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const System = @import("../emu.zig").System;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					const IntFittingRange = std.math.IntFittingRange;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const page_size = 16 * KiB; // smallest allocation is 16 KiB
 | 
				
			||||||
 | 
					const addr_space_size = 0x0100_0000; // 0x0600_0000 -> 0x06FF_FFFF (inclusive)
 | 
				
			||||||
 | 
					const table_len = addr_space_size / page_size;
 | 
				
			||||||
 | 
					const buf_len = 656 * KiB;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const log = std.log.scoped(.vram);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					io: Io = .{},
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					_buf: *[buf_len]u8,
 | 
				
			||||||
 | 
					nds9_table: *const [table_len]?[*]u8,
 | 
				
			||||||
 | 
					nds7_table: *const [table_len]?[*]u8,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Io = struct {
 | 
				
			||||||
 | 
					    pub const Vramstat = nds7.Vramstat;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const nds9 = @import("../nds9/io.zig");
 | 
				
			||||||
 | 
					    const nds7 = @import("../nds7/io.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    stat: nds7.Vramstat = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// Write-Only (according to melonDS these are readable lol)
 | 
				
			||||||
 | 
					    cnt_a: nds9.Vramcnt.A = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_b: nds9.Vramcnt.A = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_c: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_d: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_e: nds9.Vramcnt.E = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_f: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_g: nds9.Vramcnt.C = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_h: nds9.Vramcnt.H = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					    cnt_i: nds9.Vramcnt.H = .{ .raw = 0x00 },
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn init(self: *@This(), allocator: Allocator) !void {
 | 
				
			||||||
 | 
					    const buf = try allocator.create([buf_len]u8);
 | 
				
			||||||
 | 
					    errdefer allocator.destroy(buf);
 | 
				
			||||||
 | 
					    @memset(buf, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const tables = try allocator.alloc(?[*]u8, 2 * table_len);
 | 
				
			||||||
 | 
					    @memset(tables, null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self.* = .{
 | 
				
			||||||
 | 
					        .nds9_table = tables[0..table_len],
 | 
				
			||||||
 | 
					        .nds7_table = tables[table_len .. 2 * table_len],
 | 
				
			||||||
 | 
					        ._buf = buf,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // ROMS like redpanda.nds won't write to VRAMCNT before trying to write to VRAM
 | 
				
			||||||
 | 
					    // therefore we assume some default allocation (in this casee VRAMCNT_A -> VRAMCNT_I are 0x00)
 | 
				
			||||||
 | 
					    self.update();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn deinit(self: @This(), allocator: Allocator) void {
 | 
				
			||||||
 | 
					    allocator.destroy(self._buf);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const ptr: [*]?[*]const u8 = @ptrCast(@constCast(self.nds9_table));
 | 
				
			||||||
 | 
					    allocator.free(ptr[0 .. 2 * table_len]);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// NDS7 VRAMSTAT
 | 
				
			||||||
 | 
					pub fn stat(self: *const @This()) Io.Vramstat {
 | 
				
			||||||
 | 
					    const vram_c: u8 = @intFromBool(self.io.cnt_c.enable.read() and self.io.cnt_c.mst.read() == 2);
 | 
				
			||||||
 | 
					    const vram_d: u8 = @intFromBool(self.io.cnt_d.enable.read() and self.io.cnt_d.mst.read() == 2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return .{ .raw = (vram_d << 1) | vram_c };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Kind = enum {
 | 
				
			||||||
 | 
					    a,
 | 
				
			||||||
 | 
					    b,
 | 
				
			||||||
 | 
					    c,
 | 
				
			||||||
 | 
					    d,
 | 
				
			||||||
 | 
					    e,
 | 
				
			||||||
 | 
					    f,
 | 
				
			||||||
 | 
					    g,
 | 
				
			||||||
 | 
					    h,
 | 
				
			||||||
 | 
					    i,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    /// In Bytes
 | 
				
			||||||
 | 
					    inline fn size(self: @This()) u32 {
 | 
				
			||||||
 | 
					        return switch (self) {
 | 
				
			||||||
 | 
					            .a => 128 * KiB,
 | 
				
			||||||
 | 
					            .b => 128 * KiB,
 | 
				
			||||||
 | 
					            .c => 128 * KiB,
 | 
				
			||||||
 | 
					            .d => 128 * KiB,
 | 
				
			||||||
 | 
					            .e => 64 * KiB,
 | 
				
			||||||
 | 
					            .f => 16 * KiB,
 | 
				
			||||||
 | 
					            .g => 16 * KiB,
 | 
				
			||||||
 | 
					            .h => 32 * KiB,
 | 
				
			||||||
 | 
					            .i => 16 * KiB,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO: Rename
 | 
				
			||||||
 | 
					fn range(comptime kind: Kind, mst: u3, offset: u2) u32 {
 | 
				
			||||||
 | 
					    const ofs: u32 = offset;
 | 
				
			||||||
 | 
					    // panic messages are from GBATEK
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return switch (kind) {
 | 
				
			||||||
 | 
					        .a => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0680_0000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000 + (0x0002_0000 * ofs),
 | 
				
			||||||
 | 
					            2 => 0x0640_0000 + (0x0002_0000 * (ofs & 0b01)),
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_A: Slot OFS(0-3)"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .b => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0682_0000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000 + (0x0002_0000 * ofs),
 | 
				
			||||||
 | 
					            2 => 0x0640_0000 + (0x0002_0000 * (ofs & 0b01)),
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_B: Slot OFS(0-3)"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .c => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0684_0000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000 + (0x0002_0000 * ofs),
 | 
				
			||||||
 | 
					            2 => 0x0600_0000 + (0x0002_0000 * (ofs & 0b01)),
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_C: Slot OFS(0-3)"),
 | 
				
			||||||
 | 
					            4 => 0x0620_0000,
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .d => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0686_0000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000 + (0x0002_0000 * ofs),
 | 
				
			||||||
 | 
					            2 => 0x0600_0000 + (0x0002_0000 * (ofs & 0b01)),
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_D: Slot OFS(0-3)"),
 | 
				
			||||||
 | 
					            4 => 0x0660_0000,
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .e => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0688_0000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000,
 | 
				
			||||||
 | 
					            2 => 0x0640_0000,
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_E: Slots 0-3"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .f => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0689_0000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1)),
 | 
				
			||||||
 | 
					            2 => 0x0640_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1)),
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_F: Slot (OFS.0*1)+(OFS.1*4)"),
 | 
				
			||||||
 | 
					            4 => @panic("VRAMCNT_F: Slot 0-1 (OFS=0), Slot 2-3 (OFS=1)"),
 | 
				
			||||||
 | 
					            5 => @panic("VRAMCNT_F: Slot 0"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .g => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0689_4000,
 | 
				
			||||||
 | 
					            1 => 0x0600_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1)),
 | 
				
			||||||
 | 
					            2 => 0x0640_0000 + (0x0000_4000 * (ofs & 0b01)) + (0x0001_0000 * (ofs >> 1)),
 | 
				
			||||||
 | 
					            3 => @panic("VRAMCNT_G: Slot (OFS.0*1)+(OFS.1*4)"),
 | 
				
			||||||
 | 
					            4 => @panic("VRAMCNT_G: Slot 0-1 (OFS=0), Slot 2-3 (OFS=1)"),
 | 
				
			||||||
 | 
					            5 => @panic("VRAMCNT_G: Slot 0"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .h => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x0689_8000,
 | 
				
			||||||
 | 
					            1 => 0x0620_0000,
 | 
				
			||||||
 | 
					            2 => @panic("VRAMCNT_H: Slot 0-3"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					        .i => switch (mst) {
 | 
				
			||||||
 | 
					            0 => 0x068A_0000,
 | 
				
			||||||
 | 
					            1 => 0x0620_8000,
 | 
				
			||||||
 | 
					            2 => 0x0660_0000,
 | 
				
			||||||
 | 
					            3 => @panic("Slot 0"),
 | 
				
			||||||
 | 
					            else => std.debug.panic("Invalid MST for VRAMCNT_{s}", .{[_]u8{std.ascii.toUpper(@tagName(kind)[0])}}),
 | 
				
			||||||
 | 
					        },
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn buf_offset(comptime kind: Kind) usize {
 | 
				
			||||||
 | 
					    // zig fmt: off
 | 
				
			||||||
 | 
					        return switch (kind) {
 | 
				
			||||||
 | 
					            .a => 0,                                                            // 0x00000
 | 
				
			||||||
 | 
					            .b => (128 * KiB) * 1,                                              // 0x20000 (+ 0x20000)
 | 
				
			||||||
 | 
					            .c => (128 * KiB) * 2,                                              // 0x40000 (+ 0x20000)
 | 
				
			||||||
 | 
					            .d => (128 * KiB) * 3,                                              // 0x60000 (+ 0x20000)
 | 
				
			||||||
 | 
					            .e => (128 * KiB) * 4,                                              // 0x80000 (+ 0x20000)
 | 
				
			||||||
 | 
					            .f => (128 * KiB) * 4 + (64 * KiB),                                 // 0x90000 (+ 0x10000)
 | 
				
			||||||
 | 
					            .g => (128 * KiB) * 4 + (64 * KiB) + (16 * KiB) * 1,                // 0x94000 (+ 0x04000)
 | 
				
			||||||
 | 
					            .h => (128 * KiB) * 4 + (64 * KiB) + (16 * KiB) * 2,                // 0x98000 (+ 0x04000)
 | 
				
			||||||
 | 
					            .i => (128 * KiB) * 4 + (64 * KiB) + (16 * KiB) * 2 + (32 * KiB)    // 0xA0000 (+ 0x08000)
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					        // zig fmt: on
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn CntType(comptime kind: Kind) type {
 | 
				
			||||||
 | 
					    const Vramcnt = @import("../nds9/io.zig").Vramcnt;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return switch (kind) {
 | 
				
			||||||
 | 
					        .a => Vramcnt.A,
 | 
				
			||||||
 | 
					        .b => Vramcnt.A,
 | 
				
			||||||
 | 
					        .c => Vramcnt.C,
 | 
				
			||||||
 | 
					        .d => Vramcnt.C,
 | 
				
			||||||
 | 
					        .e => Vramcnt.E,
 | 
				
			||||||
 | 
					        .f => Vramcnt.C,
 | 
				
			||||||
 | 
					        .g => Vramcnt.C,
 | 
				
			||||||
 | 
					        .h => Vramcnt.H,
 | 
				
			||||||
 | 
					        .i => Vramcnt.H,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn cntValue(self: *const @This(), comptime kind: Kind) CntType(kind) {
 | 
				
			||||||
 | 
					    return switch (kind) {
 | 
				
			||||||
 | 
					        .a => self.io.cnt_a,
 | 
				
			||||||
 | 
					        .b => self.io.cnt_b,
 | 
				
			||||||
 | 
					        .c => self.io.cnt_c,
 | 
				
			||||||
 | 
					        .d => self.io.cnt_d,
 | 
				
			||||||
 | 
					        .e => self.io.cnt_e,
 | 
				
			||||||
 | 
					        .f => self.io.cnt_f,
 | 
				
			||||||
 | 
					        .g => self.io.cnt_g,
 | 
				
			||||||
 | 
					        .h => self.io.cnt_h,
 | 
				
			||||||
 | 
					        .i => self.io.cnt_i,
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// TODO: We always update the entirety of VRAM when that argubably isn't necessary
 | 
				
			||||||
 | 
					pub fn update(self: *@This()) void {
 | 
				
			||||||
 | 
					    const nds9_tbl = @constCast(self.nds9_table);
 | 
				
			||||||
 | 
					    const nds7_tbl = @constCast(self.nds7_table);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for (nds9_tbl, nds7_tbl, 0..) |*nds9_ptr, *nds7_ptr, i| {
 | 
				
			||||||
 | 
					        const addr = 0x0600_0000 + (i * page_size);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        inline for (std.meta.fields(Kind)) |f| {
 | 
				
			||||||
 | 
					            const kind = @field(Kind, f.name);
 | 
				
			||||||
 | 
					            const cnt = cntValue(self, kind);
 | 
				
			||||||
 | 
					            const ofs = switch (kind) {
 | 
				
			||||||
 | 
					                .e, .h, .i => 0,
 | 
				
			||||||
 | 
					                else => cnt.offset.read(),
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const min = range(kind, cnt.mst.read(), ofs);
 | 
				
			||||||
 | 
					            const max = min + kind.size();
 | 
				
			||||||
 | 
					            const offset = addr & (kind.size() - 1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            if (min <= addr and addr < max) {
 | 
				
			||||||
 | 
					                if ((kind == .c or kind == .d) and cnt.mst.read() == 2) {
 | 
				
			||||||
 | 
					                    // Allocate to ARM7
 | 
				
			||||||
 | 
					                    nds7_ptr.* = self._buf[buf_offset(kind) + offset ..].ptr;
 | 
				
			||||||
 | 
					                } else {
 | 
				
			||||||
 | 
					                    nds9_ptr.* = self._buf[buf_offset(kind) + offset ..].ptr;
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn read(self: @This(), comptime T: type, comptime proc: System.Process, address: u32) T {
 | 
				
			||||||
 | 
					    const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
				
			||||||
 | 
					    const masked_addr = address & (addr_space_size - 1);
 | 
				
			||||||
 | 
					    const page = masked_addr >> bits;
 | 
				
			||||||
 | 
					    const offset = masked_addr & (page_size - 1);
 | 
				
			||||||
 | 
					    const table = if (proc == .nds9) self.nds9_table else self.nds7_table;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (table[page]) |some_ptr| {
 | 
				
			||||||
 | 
					        const ptr: [*]const T = @ptrCast(@alignCast(some_ptr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return ptr[offset / @sizeOf(T)];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log.err("{s}: read(T: {}, addr: 0x{X:0>8}) was in un-mapped VRAM space", .{ @tagName(proc), T, address });
 | 
				
			||||||
 | 
					    return 0x00;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub fn write(self: *@This(), comptime T: type, comptime proc: System.Process, address: u32, value: T) void {
 | 
				
			||||||
 | 
					    const bits = @typeInfo(IntFittingRange(0, page_size - 1)).Int.bits;
 | 
				
			||||||
 | 
					    const masked_addr = address & (addr_space_size - 1);
 | 
				
			||||||
 | 
					    const page = masked_addr >> bits;
 | 
				
			||||||
 | 
					    const offset = masked_addr & (page_size - 1);
 | 
				
			||||||
 | 
					    const table = if (proc == .nds9) self.nds9_table else self.nds7_table;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (table[page]) |some_ptr| {
 | 
				
			||||||
 | 
					        const ptr: [*]T = @ptrCast(@alignCast(some_ptr));
 | 
				
			||||||
 | 
					        ptr[offset / @sizeOf(T)] = value;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    log.err("{s}: write(T: {}, addr: 0x{X:0>8}, value: 0x{X:0>8}) was in un-mapped VRA< space", .{ @tagName(proc), T, address, value });
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										340
									
								
								src/core/ppu/engine.zig
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										340
									
								
								src/core/ppu/engine.zig
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,340 @@
 | 
				
			|||||||
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					const Bus = @import("../nds9/Bus.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const FrameBuffer = @import("../ppu.zig").FrameBuffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const DispcntA = @import("../nds9/io.zig").DispcntA;
 | 
				
			||||||
 | 
					const DispcntB = @import("../nds9/io.zig").DispcntB;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Ppu = @import("../ppu.zig").Ppu;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const width = @import("../ppu.zig").screen_width;
 | 
				
			||||||
 | 
					const height = @import("../ppu.zig").screen_height;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const KiB = 0x400;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const EngineKind = enum { a, b };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					pub const EngineA = Engine(.a);
 | 
				
			||||||
 | 
					pub const EngineB = Engine(.b);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					fn Engine(comptime kind: EngineKind) type {
 | 
				
			||||||
 | 
					    const log = std.log.scoped(.engine2d); // TODO: specify between 2D-A and 2D-B
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // FIXME: don't commit zig crimes
 | 
				
			||||||
 | 
					    const Type = struct {
 | 
				
			||||||
 | 
					        fn inner(comptime TypeA: type, comptime TypeB: type) type {
 | 
				
			||||||
 | 
					            return if (kind == .a) TypeA else TypeB;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    }.inner;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return struct {
 | 
				
			||||||
 | 
					        dispcnt: Type(DispcntA, DispcntB) = .{ .raw = 0x0000_0000 },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        bg: [4]bg.Text = .{ .{}, .{}, .{}, .{} },
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // TODO: Rename
 | 
				
			||||||
 | 
					        scanline: Scanline,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn init(allocator: std.mem.Allocator) !@This() {
 | 
				
			||||||
 | 
					            return .{
 | 
				
			||||||
 | 
					                .scanline = try Scanline.init(allocator),
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn deinit(self: @This(), allocator: std.mem.Allocator) void {
 | 
				
			||||||
 | 
					            self.scanline.deinit(allocator);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn drawScanline(self: *@This(), bus: *Bus, fb: *FrameBuffer) void {
 | 
				
			||||||
 | 
					            const disp_mode = self.dispcnt.display_mode.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            switch (disp_mode) {
 | 
				
			||||||
 | 
					                0 => { // Display Off
 | 
				
			||||||
 | 
					                    const buf = switch (kind) {
 | 
				
			||||||
 | 
					                        .a => if (bus.ppu.io.powcnt.display_swap.read()) fb.top(.back) else fb.btm(.back),
 | 
				
			||||||
 | 
					                        .b => if (bus.ppu.io.powcnt.display_swap.read()) fb.btm(.back) else fb.top(.back),
 | 
				
			||||||
 | 
					                    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    @memset(buf, 0xFF); // set everything to white
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                1 => {
 | 
				
			||||||
 | 
					                    const bg_mode = self.dispcnt.bg_mode.read();
 | 
				
			||||||
 | 
					                    const bg_enable = self.dispcnt.bg_enable.read();
 | 
				
			||||||
 | 
					                    const scanline: u32 = bus.ppu.io.nds9.vcount.scanline.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    switch (bg_mode) {
 | 
				
			||||||
 | 
					                        //  BG0     BG1     BG2     BG3
 | 
				
			||||||
 | 
					                        //  Text/3D Text    Text    Text
 | 
				
			||||||
 | 
					                        0 => {
 | 
				
			||||||
 | 
					                            // TODO: Fetch Sprites
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            for (0..4) |layer| {
 | 
				
			||||||
 | 
					                                // TODO: Draw Sprites
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                                inline for (0..4) |i| {
 | 
				
			||||||
 | 
					                                    if (layer == self.bg[i].cnt.priority.read() and (bg_enable >> i) & 1 == 1) self.drawBackground(i, bus);
 | 
				
			||||||
 | 
					                                }
 | 
				
			||||||
 | 
					                            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            const buf = switch (kind) {
 | 
				
			||||||
 | 
					                                .a => if (bus.ppu.io.powcnt.display_swap.read()) fb.top(.back) else fb.btm(.back),
 | 
				
			||||||
 | 
					                                .b => if (bus.ppu.io.powcnt.display_swap.read()) fb.btm(.back) else fb.top(.back),
 | 
				
			||||||
 | 
					                            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            const scanline_buf = blk: {
 | 
				
			||||||
 | 
					                                const rgba_ptr: *[width * height]u32 = @ptrCast(@alignCast(buf));
 | 
				
			||||||
 | 
					                                break :blk rgba_ptr[width * scanline ..][0..width];
 | 
				
			||||||
 | 
					                            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                            self.renderTextMode(bus.dbgRead(u16, 0x0500_0000), scanline_buf);
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                        else => |mode| {
 | 
				
			||||||
 | 
					                            log.err("TODO: Implement Mode {}", .{mode});
 | 
				
			||||||
 | 
					                            @panic("fatal error");
 | 
				
			||||||
 | 
					                        },
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                2 => { // VRAM display
 | 
				
			||||||
 | 
					                    if (kind == .b) return;
 | 
				
			||||||
 | 
					                    // TODO: Master Brightness can still affect this mode
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    const scanline: u32 = bus.ppu.io.nds9.vcount.scanline.read();
 | 
				
			||||||
 | 
					                    const buf = if (bus.ppu.io.powcnt.display_swap.read()) fb.top(.back) else fb.btm(.back);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    const scanline_buf = blk: {
 | 
				
			||||||
 | 
					                        const rgba_ptr: *[width * height]u32 = @ptrCast(@alignCast(buf));
 | 
				
			||||||
 | 
					                        break :blk rgba_ptr[width * scanline ..][0..width];
 | 
				
			||||||
 | 
					                    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    const base_addr: u32 = 0x0680_0000 + (width * @sizeOf(u16)) * @as(u32, scanline);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    for (scanline_buf, 0..) |*rgba, i| {
 | 
				
			||||||
 | 
					                        const addr = base_addr + @as(u32, @intCast(i)) * @sizeOf(u16);
 | 
				
			||||||
 | 
					                        rgba.* = rgba888(bus.dbgRead(u16, addr));
 | 
				
			||||||
 | 
					                    }
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                3 => {
 | 
				
			||||||
 | 
					                    if (kind == .b) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                    @panic("TODO: main memory display");
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fn drawBackground(self: *@This(), comptime layer: u2, bus: *Bus) void {
 | 
				
			||||||
 | 
					            const screen_base = blk: {
 | 
				
			||||||
 | 
					                const bgcnt_off: u32 = self.bg[layer].cnt.screen_base.read();
 | 
				
			||||||
 | 
					                const dispcnt_off: u32 = if (kind == .a) self.dispcnt.screen_base.read() else 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                break :blk (2 * KiB) * bgcnt_off + (64 * KiB) * dispcnt_off;
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const char_base = blk: {
 | 
				
			||||||
 | 
					                const bgcnt_off: u32 = self.bg[layer].cnt.char_base.read();
 | 
				
			||||||
 | 
					                const dispcnt_off: u32 = if (kind == .a) self.dispcnt.char_base.read() else 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                break :blk (16 * KiB) * bgcnt_off + (64 * KiB) * dispcnt_off;
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const is_8bpp = self.bg[layer].cnt.colour_mode.read();
 | 
				
			||||||
 | 
					            const size = self.bg[layer].cnt.size.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            // In 4bpp: 1 byte represents two pixels so the length is (8 x 8) / 2
 | 
				
			||||||
 | 
					            // In 8bpp: 1 byte represents one pixel so the length is 8 x 8
 | 
				
			||||||
 | 
					            const tile_len: u32 = if (is_8bpp) 0x40 else 0x20;
 | 
				
			||||||
 | 
					            const tile_row_offset: u32 = if (is_8bpp) 0x8 else 0x4;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const vofs: u32 = self.bg[layer].vofs.offset.read();
 | 
				
			||||||
 | 
					            const hofs: u32 = self.bg[layer].hofs.offset.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            const y: u32 = vofs + bus.ppu.io.nds9.vcount.scanline.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            for (0..width) |idx| {
 | 
				
			||||||
 | 
					                const i: u32 = @intCast(idx);
 | 
				
			||||||
 | 
					                const x = hofs + i;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // TODO: Windowing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Grab the Screen Entry from VRAM
 | 
				
			||||||
 | 
					                const entry_addr = screen_base + tilemapOffset(size, x, y);
 | 
				
			||||||
 | 
					                const entry: bg.Screen.Entry = @bitCast(bus.read(u16, 0x0600_0000 + entry_addr));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Calculate the Address of the Tile in the designated Charblock
 | 
				
			||||||
 | 
					                // We also take this opportunity to flip tiles if necessary
 | 
				
			||||||
 | 
					                const tile_id: u32 = entry.tile_id.read();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                // Calculate row and column offsets. Understand that
 | 
				
			||||||
 | 
					                // `tile_len`, `tile_row_offset` and `col` are subject to different
 | 
				
			||||||
 | 
					                // values depending on whether we are in 4bpp or 8bpp mode.
 | 
				
			||||||
 | 
					                const row = @as(u3, @truncate(y)) ^ if (entry.v_flip.read()) 7 else @as(u3, 0);
 | 
				
			||||||
 | 
					                const col = @as(u3, @truncate(x)) ^ if (entry.h_flip.read()) 7 else @as(u3, 0);
 | 
				
			||||||
 | 
					                const tile_addr = char_base + (tile_id * tile_len) + (row * tile_row_offset) + if (is_8bpp) col else col >> 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const tile = bus.read(u8, 0x0600_0000 + tile_addr);
 | 
				
			||||||
 | 
					                // If we're in 8bpp, then the tile value is an index into the palette,
 | 
				
			||||||
 | 
					                // If we're in 4bpp, we have to account for a pal bank value in the Screen entry
 | 
				
			||||||
 | 
					                // and then we can index the palette
 | 
				
			||||||
 | 
					                const pal_addr: u32 = if (!is_8bpp) get4bppTilePalette(entry.pal_bank.read(), col, tile) else tile;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                if (pal_addr != 0) {
 | 
				
			||||||
 | 
					                    self.drawBackgroundPixel(layer, i, bus.read(u16, 0x0500_0000 + pal_addr * 2));
 | 
				
			||||||
 | 
					                }
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        inline fn get4bppTilePalette(pal_bank: u4, col: u3, tile: u8) u8 {
 | 
				
			||||||
 | 
					            const nybble_tile = tile >> ((col & 1) << 2) & 0xF;
 | 
				
			||||||
 | 
					            if (nybble_tile == 0) return 0;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return (@as(u8, pal_bank) << 4) | nybble_tile;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fn renderTextMode(self: *@This(), backdrop: u16, frame_buf: []u32) void {
 | 
				
			||||||
 | 
					            for (self.scanline.top(), self.scanline.btm(), frame_buf) |maybe_top, maybe_btm, *rgba| {
 | 
				
			||||||
 | 
					                _ = maybe_btm;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                const bgr555 = switch (maybe_top) {
 | 
				
			||||||
 | 
					                    .set => |px| px,
 | 
				
			||||||
 | 
					                    else => backdrop,
 | 
				
			||||||
 | 
					                };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                rgba.* = rgba888(bgr555);
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            self.scanline.reset();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // TODO: Comment this + get a better understanding
 | 
				
			||||||
 | 
					        fn tilemapOffset(size: u2, x: u32, y: u32) u32 {
 | 
				
			||||||
 | 
					            // Current Row: (y % PIXEL_COUNT) / 8
 | 
				
			||||||
 | 
					            // Current COlumn: (x % PIXEL_COUNT) / 8
 | 
				
			||||||
 | 
					            // Length of 1 row of Screen Entries: 0x40
 | 
				
			||||||
 | 
					            // Length of 1 Screen Entry: 0x2 is the size of a screen entry
 | 
				
			||||||
 | 
					            @setRuntimeSafety(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return switch (size) {
 | 
				
			||||||
 | 
					                0 => (x % 256 / 8) * 2 + (y % 256 / 8) * 0x40, // 256 x 256
 | 
				
			||||||
 | 
					                1 => blk: {
 | 
				
			||||||
 | 
					                    // 512 x 256
 | 
				
			||||||
 | 
					                    const offset: u32 = if (x & 0x1FF > 0xFF) 0x800 else 0;
 | 
				
			||||||
 | 
					                    break :blk offset + (x % 256 / 8) * 2 + (y % 256 / 8) * 0x40;
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                2 => blk: {
 | 
				
			||||||
 | 
					                    // 256 x 512
 | 
				
			||||||
 | 
					                    const offset: u32 = if (y & 0x1FF > 0xFF) 0x800 else 0;
 | 
				
			||||||
 | 
					                    break :blk offset + (x % 256 / 8) * 2 + (y % 256 / 8) * 0x40;
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					                3 => blk: {
 | 
				
			||||||
 | 
					                    // 512 x 512
 | 
				
			||||||
 | 
					                    const offset: u32 = if (x & 0x1FF > 0xFF) 0x800 else 0;
 | 
				
			||||||
 | 
					                    const offset_2: u32 = if (y & 0x1FF > 0xFF) 0x800 else 0;
 | 
				
			||||||
 | 
					                    break :blk offset + offset_2 + (x % 256 / 8) * 2 + (y % 512 / 8) * 0x40;
 | 
				
			||||||
 | 
					                },
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fn drawBackgroundPixel(self: *@This(), comptime layer: u2, i: u32, bgr555: u16) void {
 | 
				
			||||||
 | 
					            _ = layer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            self.scanline.top()[i] = Scanline.Pixel.from(.Background, bgr555);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Scanline = struct {
 | 
				
			||||||
 | 
					    const Pixel = union(enum) {
 | 
				
			||||||
 | 
					        // TODO: Rename
 | 
				
			||||||
 | 
					        const Layer = enum { Background, Sprite };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        set: u16,
 | 
				
			||||||
 | 
					        obj_set: u16,
 | 
				
			||||||
 | 
					        unset: void,
 | 
				
			||||||
 | 
					        hidden: void,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        fn from(comptime layer: Layer, bgr555: u16) Pixel {
 | 
				
			||||||
 | 
					            return switch (layer) {
 | 
				
			||||||
 | 
					                .Background => .{ .set = bgr555 },
 | 
				
			||||||
 | 
					                .Sprite => .{ .obj_set = bgr555 },
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        pub fn isSet(self: @This()) bool {
 | 
				
			||||||
 | 
					            return switch (self) {
 | 
				
			||||||
 | 
					                .set, .obj_set => true,
 | 
				
			||||||
 | 
					                .unset, .hidden => false,
 | 
				
			||||||
 | 
					            };
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    layers: [2][]Pixel,
 | 
				
			||||||
 | 
					    buf: []Pixel,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn init(allocator: std.mem.Allocator) !@This() {
 | 
				
			||||||
 | 
					        const buf = try allocator.alloc(Pixel, width * 2); // Top & Bottom Scanline
 | 
				
			||||||
 | 
					        @memset(buf, .unset);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return .{
 | 
				
			||||||
 | 
					            // Top & Bototm Layers
 | 
				
			||||||
 | 
					            .layers = [_][]Pixel{ buf[0..][0..width], buf[width..][0..width] },
 | 
				
			||||||
 | 
					            .buf = buf,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn reset(self: *@This()) void {
 | 
				
			||||||
 | 
					        @memset(self.buf, .unset);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn deinit(self: @This(), allocator: std.mem.Allocator) void {
 | 
				
			||||||
 | 
					        allocator.free(self.buf);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn top(self: *@This()) []Pixel {
 | 
				
			||||||
 | 
					        return self.layers[0];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn btm(self: *@This()) []Pixel {
 | 
				
			||||||
 | 
					        return self.layers[1];
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const bg = struct {
 | 
				
			||||||
 | 
					    const Text = struct {
 | 
				
			||||||
 | 
					        const io = @import("../nds9/io.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        /// Read / Write
 | 
				
			||||||
 | 
					        cnt: io.Bgcnt = .{ .raw = 0x0000 },
 | 
				
			||||||
 | 
					        /// Write Only
 | 
				
			||||||
 | 
					        hofs: io.Hofs = .{ .raw = 0x0000 },
 | 
				
			||||||
 | 
					        /// Write Only
 | 
				
			||||||
 | 
					        vofs: io.Vofs = .{ .raw = 0x0000 },
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const Screen = struct {
 | 
				
			||||||
 | 
					        const Entry = extern union {
 | 
				
			||||||
 | 
					            const Bitfield = @import("bitfield").Bitfield;
 | 
				
			||||||
 | 
					            const Bit = @import("bitfield").Bit;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            tile_id: Bitfield(u16, 0, 10),
 | 
				
			||||||
 | 
					            h_flip: Bit(u16, 10),
 | 
				
			||||||
 | 
					            v_flip: Bit(u16, 11),
 | 
				
			||||||
 | 
					            pal_bank: Bitfield(u16, 12, 4),
 | 
				
			||||||
 | 
					            raw: u16,
 | 
				
			||||||
 | 
					        };
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const Affine = @compileError("TODO: Implement Affine Backgrounds");
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					inline fn rgba888(bgr555: u16) u32 {
 | 
				
			||||||
 | 
					    const b: u32 = bgr555 >> 10 & 0x1F;
 | 
				
			||||||
 | 
					    const g: u32 = bgr555 >> 5 & 0x1F;
 | 
				
			||||||
 | 
					    const r: u32 = bgr555 & 0x1F;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // zig fmt: off
 | 
				
			||||||
 | 
					    return (r << 3 | r >> 2) << 24
 | 
				
			||||||
 | 
					        |  (g << 3 | g >> 2) << 16
 | 
				
			||||||
 | 
					        |  (b << 3 | b >> 2) << 8
 | 
				
			||||||
 | 
					        |  0xFF;
 | 
				
			||||||
 | 
					    // zig fmt: on
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										41
									
								
								src/main.zig
									
									
									
									
									
								
							
							
						
						
									
										41
									
								
								src/main.zig
									
									
									
									
									
								
							@@ -4,8 +4,9 @@ const clap = @import("zig-clap");
 | 
				
			|||||||
const emu = @import("core/emu.zig");
 | 
					const emu = @import("core/emu.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Ui = @import("platform.zig").Ui;
 | 
					const Ui = @import("platform.zig").Ui;
 | 
				
			||||||
const SharedContext = @import("core/emu.zig").SharedContext;
 | 
					const SharedCtx = @import("core/emu.zig").SharedCtx;
 | 
				
			||||||
const System = @import("core/emu.zig").System;
 | 
					const System = @import("core/emu.zig").System;
 | 
				
			||||||
 | 
					const Sync = @import("core/emu.zig").Sync;
 | 
				
			||||||
const Scheduler = @import("core/Scheduler.zig");
 | 
					const Scheduler = @import("core/Scheduler.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Allocator = std.mem.Allocator;
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
@@ -13,6 +14,8 @@ const ClapResult = clap.Result(clap.Help, &cli_params, clap.parsers.default);
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
const cli_params = clap.parseParamsComptime(
 | 
					const cli_params = clap.parseParamsComptime(
 | 
				
			||||||
    \\-h, --help        Display this help and exit.
 | 
					    \\-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
 | 
					    \\<str>             Path to the NDS ROM
 | 
				
			||||||
    \\
 | 
					    \\
 | 
				
			||||||
);
 | 
					);
 | 
				
			||||||
@@ -25,17 +28,17 @@ pub fn main() !void {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    const allocator = gpa.allocator();
 | 
					    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();
 | 
					    defer result.deinit();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const rom_path = try handlePositional(result);
 | 
					    const rom_path = try handlePositional(result);
 | 
				
			||||||
    log.debug("loading rom from: {s}", .{rom_path});
 | 
					    log.debug("loading rom from: {s}", .{rom_path});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const rom_file = try std.fs.cwd().openFile(rom_path, .{});
 | 
					    const firm_path = result.args.firm;
 | 
				
			||||||
    defer rom_file.close();
 | 
					    log.debug("loading firmware from from: {?s}", .{firm_path});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const shared_ctx = try SharedContext.init(allocator);
 | 
					    var ctx = try SharedCtx.init(allocator);
 | 
				
			||||||
    defer shared_ctx.deinit(allocator);
 | 
					    defer ctx.deinit(allocator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var scheduler = try Scheduler.init(allocator);
 | 
					    var scheduler = try Scheduler.init(allocator);
 | 
				
			||||||
    defer scheduler.deinit();
 | 
					    defer scheduler.deinit();
 | 
				
			||||||
@@ -47,8 +50,11 @@ pub fn main() !void {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        var cp15 = System.Cp15{};
 | 
					        var cp15 = System.Cp15{};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var bus7 = try System.Bus7.init(allocator, &scheduler, shared_ctx);
 | 
					        var bus7 = try System.Bus7.init(allocator, &scheduler, ctx);
 | 
				
			||||||
        var bus9 = try System.Bus9.init(allocator, &scheduler, shared_ctx);
 | 
					        var bus9 = try System.Bus9.init(allocator, &scheduler, ctx);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // TODO: Think of a better way to do this
 | 
				
			||||||
 | 
					        bus7.io.configure(&bus9.ppu);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var arm7tdmi = System.Arm7tdmi.init(IScheduler.init(&scheduler), IBus.init(&bus7));
 | 
					        var arm7tdmi = System.Arm7tdmi.init(IScheduler.init(&scheduler), IBus.init(&bus7));
 | 
				
			||||||
        var arm946es = System.Arm946es.init(IScheduler.init(&scheduler), IBus.init(&bus9), ICoprocessor.init(&cp15));
 | 
					        var arm946es = System.Arm946es.init(IScheduler.init(&scheduler), IBus.init(&bus9), ICoprocessor.init(&cp15));
 | 
				
			||||||
@@ -56,13 +62,28 @@ pub fn main() !void {
 | 
				
			|||||||
        break :blk .{ .arm7tdmi = &arm7tdmi, .arm946es = &arm946es, .bus7 = &bus7, .bus9 = &bus9, .cp15 = &cp15 };
 | 
					        break :blk .{ .arm7tdmi = &arm7tdmi, .arm946es = &arm946es, .bus7 = &bus7, .bus9 = &bus9, .cp15 = &cp15 };
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
    defer system.deinit(allocator);
 | 
					    defer system.deinit(allocator);
 | 
				
			||||||
    const rom_title = try emu.load(allocator, system, rom_file);
 | 
					
 | 
				
			||||||
 | 
					    ctx.io.ipc.configure(system); // Shared I/O needs access to both CPUs (e.g. IPCSYNC)
 | 
				
			||||||
 | 
					    const rom_title = try emu.load(allocator, system, rom_path);
 | 
				
			||||||
 | 
					    if (firm_path) |path| try emu.loadFirm(allocator, system, path);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    emu.fastBoot(system);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    var ui = try Ui.init(allocator);
 | 
					    var ui = try Ui.init(allocator);
 | 
				
			||||||
    defer ui.deinit(allocator);
 | 
					    defer ui.deinit(allocator);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ui.setTitle(rom_title);
 | 
					    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 {
 | 
					fn handlePositional(result: ClapResult) ![]const u8 {
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										253
									
								
								src/platform.zig
									
									
									
									
									
								
							
							
						
						
									
										253
									
								
								src/platform.zig
									
									
									
									
									
								
							@@ -8,8 +8,11 @@ const imgui = @import("ui/imgui.zig");
 | 
				
			|||||||
const emu = @import("core/emu.zig");
 | 
					const emu = @import("core/emu.zig");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const System = @import("core/emu.zig").System;
 | 
					const System = @import("core/emu.zig").System;
 | 
				
			||||||
const KeyInput = @import("core/nds9/io.zig").KeyInput;
 | 
					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 Scheduler = @import("core/Scheduler.zig");
 | 
				
			||||||
 | 
					const FrameBuffer = @import("core/ppu.zig").FrameBuffer;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const Allocator = std.mem.Allocator;
 | 
					const Allocator = std.mem.Allocator;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -33,7 +36,7 @@ pub const Ui = struct {
 | 
				
			|||||||
    state: imgui.State,
 | 
					    state: imgui.State,
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn init(allocator: Allocator) !Self {
 | 
					    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_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();
 | 
					        if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic();
 | 
				
			||||||
@@ -85,108 +88,42 @@ pub const Ui = struct {
 | 
				
			|||||||
        return SDL.SDL_GL_GetProcAddress(proc.ptr);
 | 
					        return SDL.SDL_GL_GetProcAddress(proc.ptr);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn run(self: *Self, scheduler: *Scheduler, system: System) !void {
 | 
					    pub fn setTitle(self: *@This(), title: [12]u8) void {
 | 
				
			||||||
        // TODO: Sort this out please
 | 
					        self.state.title = title ++ [_:0]u8{};
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const vao_id = opengl_impl.vao();
 | 
					    pub fn run(self: *Self, scheduler: *Scheduler, system: System, sync: *Sync) !void {
 | 
				
			||||||
        defer gl.deleteVertexArrays(1, &[_]GLuint{vao_id});
 | 
					        const id = try opengl_impl.runInit(&system.bus9.ppu.fb);
 | 
				
			||||||
 | 
					        defer id.deinit();
 | 
				
			||||||
        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);
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var event: SDL.SDL_Event = undefined;
 | 
					        var event: SDL.SDL_Event = undefined;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        emu_loop: while (true) {
 | 
					        while (!sync.should_quit.load(.monotonic)) {
 | 
				
			||||||
            emu.runFrame(scheduler, system);
 | 
					            emu.runFrame(scheduler, system); // TODO: run emu in separate thread
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            while (SDL.SDL_PollEvent(&event) != 0) {
 | 
					            while (SDL.SDL_PollEvent(&event) != 0) {
 | 
				
			||||||
                _ = zgui.backend.processEvent(&event);
 | 
					                _ = zgui.backend.processEvent(&event);
 | 
				
			||||||
 | 
					                handleInput(&event, system, &self.state, sync);
 | 
				
			||||||
                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.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.keyinput.fetchOr(keyinput.raw, .Monotonic);
 | 
					 | 
				
			||||||
                    },
 | 
					 | 
				
			||||||
                    else => {},
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                gl.bindFramebuffer(gl.FRAMEBUFFER, top_fbo);
 | 
					                gl.bindFramebuffer(gl.FRAMEBUFFER, id.top_fbo);
 | 
				
			||||||
                defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
 | 
					                defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                gl.viewport(0, 0, nds_width, nds_height);
 | 
					                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);
 | 
					                defer gl.bindFramebuffer(gl.FRAMEBUFFER, 0);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                gl.viewport(0, 0, nds_width, nds_height);
 | 
					                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
 | 
					            // Background Colour
 | 
				
			||||||
            const size = zgui.io.getDisplaySize();
 | 
					            const size = zgui.io.getDisplaySize();
 | 
				
			||||||
            gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
 | 
					            gl.viewport(0, 0, @intFromFloat(size[0]), @intFromFloat(size[1]));
 | 
				
			||||||
@@ -194,14 +131,118 @@ pub const Ui = struct {
 | 
				
			|||||||
            gl.clear(gl.COLOR_BUFFER_BIT);
 | 
					            gl.clear(gl.COLOR_BUFFER_BIT);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            zgui.backend.draw();
 | 
					            zgui.backend.draw();
 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
            SDL.SDL_GL_SwapWindow(self.window);
 | 
					            SDL.SDL_GL_SwapWindow(self.window);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    pub fn setTitle(self: *@This(), title: [12]u8) void {
 | 
					    pub fn debug_run(self: *Self, _: *Scheduler, system: System, sync: *Sync) !void {
 | 
				
			||||||
        self.state.title = title ++ [_:0]u8{};
 | 
					        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 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 {
 | 
					    fn drawScreen(tex_id: GLuint, prog_id: GLuint, vao_id: GLuint, buf: []const u8) void {
 | 
				
			||||||
        gl.bindTexture(gl.TEXTURE_2D, tex_id);
 | 
					        gl.bindTexture(gl.TEXTURE_2D, tex_id);
 | 
				
			||||||
        defer gl.bindTexture(gl.TEXTURE_2D, 0);
 | 
					        defer gl.bindTexture(gl.TEXTURE_2D, 0);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -18,7 +18,7 @@ pub const State = struct {
 | 
				
			|||||||
    dim: Dimensions = .{ .width = 1600, .height = 900 },
 | 
					    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;
 | 
					    _ = system;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    zgui.backend.newFrame(@floatFromInt(state.dim.width), @floatFromInt(state.dim.height));
 | 
					    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(top_tex), .{ .w = w, .h = h });
 | 
				
			||||||
        zgui.image(@ptrFromInt(btm_tex), .{ .w = w, .h = h });
 | 
					        zgui.image(@ptrFromInt(btm_tex), .{ .w = w, .h = h });
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					 | 
				
			||||||
    return true;
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										49
									
								
								src/util.zig
									
									
									
									
									
								
							
							
						
						
									
										49
									
								
								src/util.zig
									
									
									
									
									
								
							@@ -1,4 +1,5 @@
 | 
				
			|||||||
const std = @import("std");
 | 
					const std = @import("std");
 | 
				
			||||||
 | 
					const Log2Int = std.math.Log2Int;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const assert = std.debug.assert;
 | 
					const assert = std.debug.assert;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -102,3 +103,51 @@ test "FittingInt" {
 | 
				
			|||||||
    try std.testing.expect(FittingInt(0b101) == i3);
 | 
					    try std.testing.expect(FittingInt(0b101) == i3);
 | 
				
			||||||
    try std.testing.expect(FittingInt(0b1010) == i4);
 | 
					    try std.testing.expect(FittingInt(0b1010) == i4);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// TODO: Document this properly :)
 | 
				
			||||||
 | 
					pub inline fn shift(comptime T: type, addr: u32) Log2Int(T) {
 | 
				
			||||||
 | 
					    const offset: Log2Int(T) = @truncate(addr & (@sizeOf(T) - 1));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return offset << 3;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/// TODO: Document this properly :)
 | 
				
			||||||
 | 
					pub inline fn subset(comptime T: type, comptime U: type, addr: u32, left: T, right: U) T {
 | 
				
			||||||
 | 
					    const offset: Log2Int(T) = @truncate(addr & (@sizeOf(T) - 1));
 | 
				
			||||||
 | 
					    const mask = @as(T, std.math.maxInt(U)) << (offset << 3);
 | 
				
			||||||
 | 
					    const value = @as(T, right) << (offset << 3);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    return (value & mask) | (left & ~mask);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					test "subset" {
 | 
				
			||||||
 | 
					    const expectEqual = std.testing.expectEqual;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try expectEqual(@as(u16, 0xABAA), subset(u16, u8, 0x0000_0000, 0xABCD, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u16, 0xAACD), subset(u16, u8, 0x0000_0001, 0xABCD, 0xAA));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try expectEqual(@as(u32, 0xDEAD_BEAA), subset(u32, u8, 0x0000_0000, 0xDEAD_BEEF, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u32, 0xDEAD_AAEF), subset(u32, u8, 0x0000_0001, 0xDEAD_BEEF, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u32, 0xDEAA_BEEF), subset(u32, u8, 0x0000_0002, 0xDEAD_BEEF, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u32, 0xAAAD_BEEF), subset(u32, u8, 0x0000_0003, 0xDEAD_BEEF, 0xAA));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try expectEqual(@as(u32, 0xDEAD_AAAA), subset(u32, u16, 0x0000_0000, 0xDEAD_BEEF, 0xAAAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u32, 0xAAAA_BEEF), subset(u32, u16, 0x0000_0002, 0xDEAD_BEEF, 0xAAAA));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_DEAD_CAAA), subset(u64, u8, 0x0000_0000, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_DEAD_AAFE), subset(u64, u8, 0x0000_0001, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_DEAA_CAFE), subset(u64, u8, 0x0000_0002, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_AAAD_CAFE), subset(u64, u8, 0x0000_0003, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F0AA_DEAD_CAFE), subset(u64, u8, 0x0000_0004, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_AA0D_DEAD_CAFE), subset(u64, u8, 0x0000_0005, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAA_F00D_DEAD_CAFE), subset(u64, u8, 0x0000_0006, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xAAAD_F00D_DEAD_CAFE), subset(u64, u8, 0x0000_0007, 0xBAAD_F00D_DEAD_CAFE, 0xAA));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_DEAD_AAAA), subset(u64, u16, 0x0000_0000, 0xBAAD_F00D_DEAD_CAFE, 0xAAAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_AAAA_CAFE), subset(u64, u16, 0x0000_0002, 0xBAAD_F00D_DEAD_CAFE, 0xAAAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_AAAA_DEAD_CAFE), subset(u64, u16, 0x0000_0004, 0xBAAD_F00D_DEAD_CAFE, 0xAAAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xAAAA_F00D_DEAD_CAFE), subset(u64, u16, 0x0000_0006, 0xBAAD_F00D_DEAD_CAFE, 0xAAAA));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xBAAD_F00D_AAAA_AAAA), subset(u64, u32, 0x0000_0000, 0xBAAD_F00D_DEAD_CAFE, 0xAAAA_AAAA));
 | 
				
			||||||
 | 
					    try expectEqual(@as(u64, 0xAAAA_AAAA_DEAD_CAFE), subset(u64, u32, 0x0000_0004, 0xBAAD_F00D_DEAD_CAFE, 0xAAAA_AAAA));
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user