Skip to content

Commit

Permalink
Generate standalone files for Regz by default
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnite committed Jan 18, 2025
1 parent 88b3f5a commit 132c31f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ pub fn MicroBuild(port_select: PortSelect) type {
.atdf, .svd => |file| blk: {
const regz_run = b.addRunArtifact(regz_exe);

regz_run.addArg("--microzig");
regz_run.addArg("--format");
regz_run.addArg(@tagName(target.chip.register_definition));

Expand Down
4 changes: 2 additions & 2 deletions tools/regz/src/Database.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2031,8 +2031,8 @@ pub fn apply_patch(db: *Database, ndjson: []const u8) !void {
}
}

pub fn to_zig(db: *Database, out_writer: anytype) !void {
try gen.to_zig(db, out_writer);
pub fn to_zig(db: *Database, for_microzig: bool, out_writer: anytype) !void {
try gen.to_zig(db, for_microzig, out_writer);
}

test "all" {
Expand Down
19 changes: 13 additions & 6 deletions tools/regz/src/gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const riscv = @import("arch/riscv.zig");

const log = std.log.scoped(.gen);

pub fn to_zig(db: *Database, out_writer: anytype) !void {
pub fn to_zig(db: *Database, for_microzig: bool, out_writer: anytype) !void {
var arena = std.heap.ArenaAllocator.init(db.gpa);
defer arena.deinit();

Expand All @@ -29,11 +29,18 @@ pub fn to_zig(db: *Database, out_writer: anytype) !void {
defer buffer.deinit();

const writer = buffer.writer();
try writer.writeAll(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
\\
);
if (for_microzig) {
try writer.writeAll(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
\\
);
} else {
try writer.writeAll(
\\const mmio = @import("mmio");
\\
);
}
try write_devices(db, allocator, writer);
try write_types(db, allocator, writer);
try writer.writeByte(0);
Expand Down
6 changes: 5 additions & 1 deletion tools/regz/src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const Arguments = struct {
output_path: ?[:0]const u8 = null,
patch_path: ?[]const u8 = null,
dump: bool = false,
microzig: bool = false,
help: bool = false,

fn deinit(args: *Arguments) void {
Expand All @@ -40,6 +41,7 @@ fn print_usage(writer: anytype) !void {
\\regz
\\ --help Display this help and exit
\\ --dump Dump SQLite file instead of generate code
\\ --microzig Generate for microzig instead of a standalone file
\\ --format <str> Explicitly set format type, one of: svd, atdf, json
\\ --output_path <str> Write to a file
\\ --patch_path <str> After reading format, apply NDJSON based patch file
Expand All @@ -63,6 +65,8 @@ fn parse_args(allocator: Allocator) !Arguments {
ret.help = true;
} else if (std.mem.eql(u8, args[i], "--dump")) {
ret.dump = true;
} else if (std.mem.eql(u8, args[i], "--microzig")) {
ret.microzig = true;
} else if (std.mem.eql(u8, args[i], "--format")) {
i += 1;
if (i >= args.len)
Expand Down Expand Up @@ -177,6 +181,6 @@ fn main_impl() anyerror!void {
std.io.getStdOut().writer();

var buffered = std.io.bufferedWriter(raw_writer);
try db.to_zig(buffered.writer());
try db.to_zig(args.microzig, buffered.writer());
try buffered.flush();
}

0 comments on commit 132c31f

Please sign in to comment.