Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add standalone option to regz to generate register api files without microzig framework dependency #258

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions tools/regz/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,22 @@ pub fn build(b: *Build) !void {
.iconv = false,
});

const assets = [_]struct { []const u8, []const u8 }{
.{ "../../core/src/mmio.zig", "mmio_file" },
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't grab a file directly from another package like this. Instead you need to use the handle for the core package to retrieve it.

Suggested change
.{ "../../core/src/mmio.zig", "mmio_file" },
.{ core_dep.path("src/mmio.zig"), "mmio_file" },

If this causes a dependency loop then MMIO needs to be its own package. I think this would be ideal, since we could put some tooling for simulated MMIO in there as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattnite Yeah I see what you mean. if I use the core_dep to reference it then regz can no longer be built by running zig build in the tools/regz directory. Not sure if there is a way to tell in the build.zig if it is being run from a parent or in the current directory. Let me look into this a bit and see what I can do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be easier to just copy the Mmio struct in the regz project to be used in standalone mode, because you also need a definition of Handler and unhandled.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mattnite I tried to reference via core_dep but I can't seem to figure out how to make it work. None of the tools have any dependencies on the core module that I can use as a reference so I'm not quite sure how to access that dependency in the regz build.zig. Sorry but I'm pretty new to zig and the build system on this project is a bit more complicated than I am used to. Can you provide some guidance on how to access within the build.zig of regz?

};

const regz = b.addExecutable(.{
.name = "regz",
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
});

for (assets) |asset| {
const path, const name = asset;
regz.root_module.addAnonymousImport(name, .{ .root_source_file = b.path(path) });
}

regz.linkLibrary(libxml2_dep.artifact("xml2"));
b.installArtifact(regz);

Expand Down Expand Up @@ -65,6 +75,12 @@ pub fn build(b: *Build) !void {
.target = target,
.optimize = optimize,
});

for (assets) |asset| {
const path, const name = asset;
tests.root_module.addAnonymousImport(name, .{ .root_source_file = b.path(path) });
}

tests.linkLibrary(libxml2_dep.artifact("xml2"));
const run_tests = b.addRunArtifact(tests);
const test_step = b.step("test", "Run unit tests");
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 @@ -985,8 +985,8 @@ pub fn format(
_ = writer;
}

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

test "all" {
Expand Down
69 changes: 40 additions & 29 deletions tools/regz/src/gen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,26 @@ const EntityWithOffset = struct {
}
};

pub fn to_zig(db: Database, out_writer: anytype) !void {
pub fn to_zig(db: Database, out_writer: anytype, standalone: bool) !void {
var buffer = std.ArrayList(u8).init(db.arena.allocator());
defer buffer.deinit();

const writer = buffer.writer();
try writer.writeAll(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
\\
);
try write_devices(db, writer);

if (standalone) {
const mmio_content = @embedFile("mmio_file");
try writer.writeAll("pub const mmio = struct {");
try writer.writeAll(mmio_content);
try writer.writeAll("};");
} else {
try writer.writeAll(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
\\
);
}

try write_devices(db, writer, standalone);
try write_types(db, writer);
try writer.writeByte(0);

Expand Down Expand Up @@ -65,7 +74,7 @@ pub fn to_zig(db: Database, out_writer: anytype) !void {
try out_writer.writeAll(text);
}

fn write_devices(db: Database, writer: anytype) !void {
fn write_devices(db: Database, writer: anytype, standalone: bool) !void {
if (db.instances.devices.count() == 0)
return;

Expand All @@ -77,7 +86,7 @@ fn write_devices(db: Database, writer: anytype) !void {

// TODO: order devices alphabetically
for (db.instances.devices.keys()) |device_id| {
write_device(db, device_id, writer) catch |err| {
write_device(db, device_id, writer, standalone) catch |err| {
log.warn("failed to write device: {}", .{err});
};
}
Expand Down Expand Up @@ -119,7 +128,7 @@ fn write_string(str: []const u8, writer: anytype) !void {
}
}

fn write_device(db: Database, device_id: EntityId, out_writer: anytype) !void {
fn write_device(db: Database, device_id: EntityId, out_writer: anytype, standalone: bool) !void {
assert(db.entity_is("instance.device", device_id));
const name = db.attrs.name.get(device_id) orelse return error.MissingDeviceName;

Expand Down Expand Up @@ -153,8 +162,10 @@ fn write_device(db: Database, device_id: EntityId, out_writer: anytype) !void {
try writer.writeAll("};\n\n");
}

write_vector_table(db, device_id, writer) catch |err|
log.warn("failed to write vector table: {}", .{err});
if (!standalone) {
write_vector_table(db, device_id, writer) catch |err|
log.warn("failed to write vector table: {}", .{err});
}

if (db.children.peripherals.get(device_id)) |peripheral_set| {
var list = std.ArrayList(EntityWithOffset).init(db.gpa);
Expand Down Expand Up @@ -891,7 +902,7 @@ test "gen.peripheral type with register and field" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand All @@ -917,7 +928,7 @@ test "gen.peripheral instantiation" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -951,7 +962,7 @@ test "gen.peripherals with a shared type" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -986,7 +997,7 @@ test "gen.peripheral with modes" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1048,7 +1059,7 @@ test "gen.peripheral with enum" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1077,7 +1088,7 @@ test "gen.peripheral with enum, enum is exhausted of values" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1105,7 +1116,7 @@ test "gen.field with named enum" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1140,7 +1151,7 @@ test "gen.field with anonymous enum" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1173,7 +1184,7 @@ test "gen.namespaced register groups" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1215,7 +1226,7 @@ test "gen.peripheral with reserved register" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1248,7 +1259,7 @@ test "gen.peripheral with count" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1281,7 +1292,7 @@ test "gen.peripheral with count, padding required" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1315,7 +1326,7 @@ test "gen.register with count" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1348,7 +1359,7 @@ test "gen.register with count and fields" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand Down Expand Up @@ -1384,7 +1395,7 @@ test "gen.field with count, width of one, offset, and padding" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand All @@ -1411,7 +1422,7 @@ test "gen.field with count, multi-bit width, offset, and padding" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
Expand All @@ -1438,7 +1449,7 @@ test "gen.interrupts.avr" {
var buffer = std.ArrayList(u8).init(std.testing.allocator);
defer buffer.deinit();

try db.to_zig(buffer.writer());
try db.to_zig(buffer.writer(), false);
try std.testing.expectEqualStrings(
\\const micro = @import("microzig");
\\const mmio = micro.mmio;
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 @@ -22,6 +22,7 @@ const Arguments = struct {
input_path: ?[]const u8 = null,
output_path: ?[]const u8 = null,
output_json: bool = false,
standalone: bool = false,
help: bool = false,

fn deinit(args: *Arguments) void {
Expand All @@ -37,6 +38,7 @@ fn print_usage(writer: anytype) !void {
\\ --schema <str> Explicitly set schema type, one of: svd, atdf, json
\\ --output_path <str> Write to a file
\\ --json Write output as JSON
\\ --standalone Write standalone output with no microzig dependencies
\\<str>
\\
);
Expand Down Expand Up @@ -73,6 +75,8 @@ fn parse_args(allocator: Allocator) !Arguments {
ret.output_path = try allocator.dupe(u8, args[i]);
} else if (std.mem.eql(u8, args[i], "--json")) {
ret.output_json = true;
} else if (std.mem.eql(u8, args[i], "--standalone")) {
ret.standalone = true;
} else if (std.mem.startsWith(u8, args[i], "-")) {
std.log.err("Unknown argument '{s}'", .{args[i]});
try print_usage(std.io.getStdErr().writer());
Expand Down Expand Up @@ -156,7 +160,7 @@ fn main_impl() anyerror!void {
buffered.writer(),
)
else
try db.to_zig(buffered.writer());
try db.to_zig(buffered.writer(), args.standalone);

try buffered.flush();
}
49 changes: 0 additions & 49 deletions tools/regz/src/mmio.zig

This file was deleted.

Loading