Skip to content

Commit 2f3e78e

Browse files
committed
feat: init yaz, lz10, lz11 decompressors + darc + make cxi!
* compress: init generic lz decompressor + yaz, lz10 and lz11 specializations * horizon/fmt: init audio and archive * hardware: init cpu * tools/Compress: init Yaz, Lz10 and Lz11 * tools/Archive: init Darc * tools/Ncch: init `make cxi` * build: init MakeFirm * all: zig fmt, general fixes
1 parent 8b683e4 commit 2f3e78e

58 files changed

Lines changed: 2438 additions & 324 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/release-tag.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ jobs:
1818
with:
1919
version: 0.15.2
2020

21-
- name: Build
22-
run: zig build -Drelease
21+
- name: Build Release
22+
run: zig build release -Doptimize=ReleaseSafe
2323

2424
- name: Package artifacts
2525
working-directory: zig-out

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,18 @@ You can (and are encouraged) to look at the `tools` directory as it is a good ex
116116
- 🟢 Disassemble: Outputs **Z**itrus**P**ica**A**sse**m**bly. Either RAW instructions, ZPSH's or DVL's (.shbin) can be disassembled.
117117
- 🟢 Firm (tools/Firm): Make / Info / Dump
118118
- 🟢 Make: Confirmed to build (and boot!) Luma3DS from source, however needs more testing as the firm is not 1:1.
119-
- 🟡 Ncch (tools/Ncch): Dump (/ Info)
119+
- 🟡 Ncch (tools/Ncch): Make CXI / Dump / Info
120120
- 🟢 ExeFS (tools/ExeFs): Make / List / Dump
121121
- 🟢 RomFS (tools/RomFs): Make / List / Dump
122-
- 🟡 LZrev (tools/LzRev): Decompression
122+
- 🟡 Compression (tools/Compress):
123+
- 🟡 LZrev (Compress/LzRev): Decompression
124+
- 🟡 Yaz0 (Compress/Yaz): Decompression
125+
- 🟡 LZ10 (Compress/Lz10): Decompression
126+
- 🟡 LZ11 (Compress/Lz11): Decompression
127+
- 🟡 Archives:
128+
- 🟡 Darc (Archive/Darc): List
129+
- 🔴 Sarc
123130
- 🔴 Cro0 / Crr0
124-
- 🔴 Arc / Bcma / SArc / Etc
125131
- 🔴 Cia
126132

127133
## Horizon

build.zig

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
pub const version = "0.0.0-pre1";
22

3+
pub const MakeFirm = @import("build/MakeFirm.zig");
34
pub const Make3dsx = @import("build/Make3dsx.zig");
45
pub const MakeSmdh = @import("build/MakeSmdh.zig");
56
pub const MakeRomFs = @import("build/MakeRomFs.zig");
@@ -47,8 +48,6 @@ pub const target = struct {
4748
};
4849

4950
pub fn build(b: *Build) void {
50-
const release = b.option(bool, "release", "Perform a release build") orelse false;
51-
5251
const optimize = b.standardOptimizeOption(.{});
5352
const tools_target = b.standardTargetOptions(.{});
5453

@@ -79,10 +78,7 @@ pub fn build(b: *Build) void {
7978

8079
zitrus.addImport("zitrus", zitrus);
8180

82-
if (release) {
83-
buildReleases(b, config, zdap, zigimg, zitrus);
84-
return;
85-
}
81+
makeReleaseStep(b, optimize, config, zdap, zigimg, zitrus);
8682

8783
// XXX: Yes, this is really needed for each target / optimize...
8884
const zitrus_lib = b.addLibrary(.{
@@ -147,22 +143,32 @@ pub fn build(b: *Build) void {
147143
}
148144

149145
const release_targets: []const std.Target.Query = &.{
146+
// Everyone is welcome to the party!
147+
// NOTE: Even if your platform is not included here it may be supported if zig supports it.
150148
.{ .cpu_arch = .x86, .os_tag = .linux },
151149
.{ .cpu_arch = .x86, .os_tag = .windows },
150+
.{ .cpu_arch = .x86, .os_tag = .netbsd },
152151
.{ .cpu_arch = .x86_64, .os_tag = .linux },
153152
.{ .cpu_arch = .x86_64, .os_tag = .windows },
154153
.{ .cpu_arch = .x86_64, .os_tag = .macos },
155154
.{ .cpu_arch = .x86_64, .os_tag = .freebsd },
155+
.{ .cpu_arch = .x86_64, .os_tag = .netbsd },
156+
.{ .cpu_arch = .arm, .os_tag = .linux },
157+
.{ .cpu_arch = .arm, .os_tag = .freebsd },
158+
.{ .cpu_arch = .arm, .os_tag = .netbsd },
156159
.{ .cpu_arch = .aarch64, .os_tag = .linux },
157160
.{ .cpu_arch = .aarch64, .os_tag = .windows },
158161
.{ .cpu_arch = .aarch64, .os_tag = .macos },
159162
.{ .cpu_arch = .aarch64, .os_tag = .freebsd },
163+
.{ .cpu_arch = .aarch64, .os_tag = .netbsd },
160164
.{ .cpu_arch = .riscv64, .os_tag = .linux },
161165
};
162166

163-
fn buildReleases(b: *Build, config: *Build.Step.Options, zdap: *Build.Module, zigimg: *Build.Module, zitrus: *Build.Module) void {
167+
fn makeReleaseStep(b: *Build, optimize: std.builtin.OptimizeMode, config: *Build.Step.Options, zdap: *Build.Module, zigimg: *Build.Module, zitrus: *Build.Module) void {
168+
const release_step = b.step("release", "Perform a release build");
169+
164170
for (release_targets) |release_target| {
165-
_, const tools = buildTools(b, config, .ReleaseSafe, b.resolveTargetQuery(release_target), zdap, zigimg, zitrus);
171+
_, const tools = buildTools(b, config, optimize, b.resolveTargetQuery(release_target), zdap, zigimg, zitrus);
166172

167173
tools.root_module.strip = true;
168174

@@ -174,7 +180,7 @@ fn buildReleases(b: *Build, config: *Build.Step.Options, zdap: *Build.Module, zi
174180
},
175181
});
176182

177-
b.getInstallStep().dependOn(&tools_output.step);
183+
release_step.dependOn(&tools_output.step);
178184
}
179185
}
180186

build/MakeFirm.zig

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
//! A wrapper to simplify making a FIRM with the build system.
2+
3+
pub const Options = struct {
4+
pub const CopyMethod = enum { ndma, xdma, memcpy };
5+
pub const Section = union(enum) {
6+
pub const Binary = struct {
7+
path: Build.LazyPath,
8+
address: u32,
9+
method: CopyMethod,
10+
};
11+
12+
pub const Executable = struct {
13+
exe: *Build.Step.Compile,
14+
method: CopyMethod,
15+
};
16+
17+
none,
18+
binary: Binary,
19+
executable: Executable,
20+
21+
pub fn bin(path: Build.LazyPath, address: u32, method: CopyMethod) Section {
22+
return .{ .binary = .{ .path = path, .address = address, .method = method } };
23+
}
24+
25+
pub fn exe(artifact: *Build.Step.Compile, method: CopyMethod) Section {
26+
return .{ .executable = .{ .exe = artifact, .method = method } };
27+
}
28+
};
29+
30+
name: []const u8,
31+
arm9: Section.Executable,
32+
arm11: Section.Executable,
33+
boot_priority: u32 = 0,
34+
extra: [2]Section = .{ .none, .none },
35+
};
36+
37+
pub const Config = struct {
38+
tools_artifact: *Build.Step.Compile,
39+
};
40+
41+
pub const InstallOptions = struct {
42+
pub const default: InstallOptions = .{ .install_dir = .bin, .dest_sub_path = null };
43+
44+
/// Which installation directory to put the main output file into.
45+
install_dir: Build.InstallDir = .bin,
46+
47+
/// If non-null, adds additional path components relative to bin dir, and
48+
/// overrides the basename of the Compile step for installation purposes.
49+
dest_sub_path: ?[]const u8 = null,
50+
};
51+
52+
name: []const u8,
53+
54+
/// The underlying `Build.Step.Run` which makes the FIRM.
55+
run: *Build.Step.Run,
56+
57+
/// The generated FIRM file by zitrus. You are encouraged to use this
58+
/// directly.
59+
out: Build.LazyPath,
60+
61+
pub fn init(zitrus_dep: *Build.Dependency, options: Options) MakeFirm {
62+
return initInner(zitrus_dep.builder, .{
63+
.tools_artifact = zitrus_dep.artifact("zitrus"),
64+
}, options);
65+
}
66+
67+
/// This is intended to be used by **zitrus** itself,
68+
/// prefer `init` instead.
69+
pub fn initInner(b: *Build, config: Config, options: Options) MakeFirm {
70+
const make = b.addRunArtifact(config.tools_artifact);
71+
make.setName(b.fmt("make firm ({s})", .{options.name}));
72+
make.addArgs(&.{ "firm", "make" });
73+
74+
const Cpu = enum { arm9, arm11 };
75+
const entries: []const Options.Section.Executable = &.{ options.arm9, options.arm11 };
76+
77+
for (entries, std.enums.values(Cpu)) |entry, cpu| {
78+
make.addArg("--elf");
79+
make.addArtifactArg(entry.exe);
80+
make.addArg(@tagName(cpu));
81+
make.addArg(@tagName(entry.method));
82+
}
83+
84+
for (&options.extra) |extra| switch (extra) {
85+
.none => {},
86+
.binary => |bin| {
87+
make.addArg("--section");
88+
make.addFileArg(bin.path);
89+
make.addArg(b.fmt("0x{X:0>8}", .{bin.address}));
90+
make.addArg(@tagName(bin.method));
91+
},
92+
.executable => |exe| {
93+
make.addArg("--elf");
94+
make.addArtifactArg(exe.exe);
95+
make.addArg("raw");
96+
make.addArg(@tagName(exe.method));
97+
},
98+
};
99+
100+
make.addArg("--output");
101+
const out = make.addOutputFileArg(options.name);
102+
103+
return .{
104+
.name = options.name,
105+
.run = make,
106+
.out = out,
107+
};
108+
}
109+
110+
pub fn install(make: MakeFirm, b: *Build, options: InstallOptions) void {
111+
const dest_sub_path = options.dest_sub_path orelse make.name;
112+
const install_firm = b.addInstallFileWithDir(make.out, options.install_dir, dest_sub_path);
113+
114+
b.getInstallStep().dependOn(&install_firm.step);
115+
}
116+
117+
const MakeFirm = @This();
118+
119+
const std = @import("std");
120+
const Build = std.Build;

build/ld/arm-3ds.ld

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Good sources:
23
* https://sourceware.org/binutils/docs/ld/Scripts.html
34
* https://gabi.xinuos.com/elf/
45
**/
@@ -36,7 +37,7 @@ SECTIONS
3637
. = ALIGN(0x1000);
3738
.text : {
3839
KEEP(*(.text.base))
39-
ASSERT(SIZEOF(.prm) == 0 || SIZEOF(.prm) > 0 && SIZEOF(.text.base) == 4, "When embedding/using program metadata, the first and only instruction in `_start` must be a branch to a function");
40+
ASSERT(SIZEOF(.prm) == 0 || (SIZEOF(.prm) > 0 && SIZEOF(.text.base) == 4), "When embedding/using program metadata, the first and only instruction in `_start` must be a branch to a function");
4041
*(.prm)
4142

4243
*(.text*)

src/compress.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1+
pub const lz = @import("compress/lz.zig");
2+
pub const yaz = @import("compress/yaz.zig");
3+
pub const lz10 = @import("compress/lz10.zig");
4+
pub const lz11 = @import("compress/lz11.zig");
15
pub const lzrev = @import("compress/lzrev.zig");
26

37
comptime {
48
_ = lzrev;
9+
_ = yaz;
10+
_ = lz10;
11+
_ = lz11;
512
}

0 commit comments

Comments
 (0)