description word wrapping and fixed unhandled interrupt function

wch-ch32v003
Matt Knight 3 years ago committed by Matt Knight
parent fed1a8f3fd
commit 1a1a281c01

@ -395,6 +395,46 @@ pub fn deinit(self: *Self) void {
self.arena.deinit(); self.arena.deinit();
} }
fn writeDescription(
allocator: Allocator,
writer: anytype,
description: []const u8,
indent: usize,
) !void {
const max_line_width = 80;
// keep explicit lines from svd
var line_it = std.mem.tokenize(u8, description, "\n\r");
while (line_it.next()) |input_line| {
var it = std.mem.tokenize(u8, input_line, " \t");
var line = std.ArrayList(u8).init(allocator);
defer line.deinit();
while (it.next()) |token| {
if (line.items.len + token.len > max_line_width) {
if (line.items.len > 0) {
try writer.writeByteNTimes(' ', indent * 4);
try writer.print("///{s}\n", .{line.items});
line.clearRetainingCapacity();
try line.append(' ');
try line.appendSlice(token);
} else {
try writer.writeByteNTimes(' ', indent * 4);
try writer.print("/// {s}\n", .{token});
}
} else {
try line.append(' ');
try line.appendSlice(token);
}
}
if (line.items.len > 0) {
try writer.writeByteNTimes(' ', indent * 4);
try writer.print("///{s}\n", .{line.items});
}
}
}
pub fn toZig(self: *Self, writer: anytype) !void { pub fn toZig(self: *Self, writer: anytype) !void {
try writer.writeAll("// this file is generated by regz\n//\n"); try writer.writeAll("// this file is generated by regz\n//\n");
if (self.device.vendor) |vendor_name| if (self.device.vendor) |vendor_name|
@ -463,7 +503,7 @@ pub fn toZig(self: *Self, writer: anytype) !void {
} }
if (interrupt.description) |description| if (!isUselessDescription(description)) if (interrupt.description) |description| if (!isUselessDescription(description))
try writer.print("\n /// {s}\n", .{description}); try writeDescription(self.arena.child_allocator, writer, description, 1);
try writer.print(" {s}: InterruptVector = unhandled,\n", .{std.zig.fmtId(interrupt.name)}); try writer.print(" {s}: InterruptVector = unhandled,\n", .{std.zig.fmtId(interrupt.name)});
expected += 1; expected += 1;
@ -495,7 +535,7 @@ pub fn toZig(self: *Self, writer: anytype) !void {
const registers = self.registers.items[reg_range.begin..reg_range.end]; const registers = self.registers.items[reg_range.begin..reg_range.end];
if (registers.len != 0 or has_clusters) { if (registers.len != 0 or has_clusters) {
if (peripheral.description) |description| if (!isUselessDescription(description)) if (peripheral.description) |description| if (!isUselessDescription(description))
try writer.print("\n /// {s}\n", .{description}); try writeDescription(self.arena.child_allocator, writer, description, 1);
try writer.print( try writer.print(
\\ pub const {s} = struct {{ \\ pub const {s} = struct {{
\\ pub const base_address = 0x{x}; \\ pub const base_address = 0x{x};
@ -554,12 +594,9 @@ fn genZigCluster(
.namespaced => if (db.registers_in_clusters.get(cluster_idx)) |range| { .namespaced => if (db.registers_in_clusters.get(cluster_idx)) |range| {
const registers = db.registers.items[range.begin..range.end]; const registers = db.registers.items[range.begin..range.end];
try writer.writeByte('\n'); try writer.writeByte('\n');
if (cluster.description) |description| { if (cluster.description) |description|
if (!isUselessDescription(description)) { if (!isUselessDescription(description))
try writer.writeByteNTimes(' ', indent * 4); try writeDescription(db.arena.child_allocator, writer, description, indent);
try writer.print("/// {s}\n", .{description});
}
}
if (dimension_opt) |dimension| { if (dimension_opt) |dimension| {
const name = try std.mem.replaceOwned(u8, db.arena.allocator(), cluster.name, "[%s]", ""); const name = try std.mem.replaceOwned(u8, db.arena.allocator(), cluster.name, "[%s]", "");
@ -762,12 +799,9 @@ fn genZigFields(
} }
// TODO: default values? // TODO: default values?
if (field.description) |description| { if (field.description) |description|
if (!isUselessDescription(description)) { if (!isUselessDescription(description))
try writer.writeByteNTimes(' ', indent * 4); try writeDescription(self.arena.child_allocator, writer, description, indent);
try writer.print("/// {s}\n", .{description});
}
}
try writer.writeByteNTimes(' ', indent * 4); try writer.writeByteNTimes(' ', indent * 4);
try writer.print("{s}: u{},\n", .{ std.zig.fmtId(field.name), field.width }); try writer.print("{s}: u{},\n", .{ std.zig.fmtId(field.name), field.width });
@ -828,10 +862,8 @@ fn genZigRegister(
try writer.print("/// address: 0x{x}\n", .{base_addr + addr_offset}); try writer.print("/// address: 0x{x}\n", .{base_addr + addr_offset});
} }
if (register.description) |description| { if (register.description) |description|
try writer.writeByteNTimes(' ', indent * 4); try writeDescription(self.arena.child_allocator, writer, description, indent);
try writer.print("/// {s}\n", .{description});
}
try self.genZigSingleRegister( try self.genZigSingleRegister(
writer, writer,
@ -865,10 +897,8 @@ fn genZigRegister(
try writer.print("/// address: 0x{x}\n", .{base_addr + addr_offset}); try writer.print("/// address: 0x{x}\n", .{base_addr + addr_offset});
} }
if (register.description) |description| { if (register.description) |description|
try writer.writeByteNTimes(' ', indent * 4); try writeDescription(self.arena.child_allocator, writer, description, indent);
try writer.print("/// {s}\n", .{description});
}
try self.genZigSingleRegister( try self.genZigSingleRegister(
writer, writer,
@ -923,10 +953,8 @@ fn genZigRegister(
try writer.print("/// address: 0x{x}\n", .{base_addr + register.addr_offset}); try writer.print("/// address: 0x{x}\n", .{base_addr + register.addr_offset});
} }
if (register.description) |description| { if (register.description) |description|
try writer.writeByteNTimes(' ', indent * 4); try writeDescription(self.arena.child_allocator, writer, description, indent);
try writer.print("/// {s}\n", .{description});
}
try self.genZigSingleRegister( try self.genZigSingleRegister(
writer, writer,

@ -82,6 +82,10 @@ const InterruptVector = extern union {
// Interrupt is not supported on arm // Interrupt is not supported on arm
}; };
fn unhandled() callconv(.C) noreturn { const unhandled = InterruptVector{
@panic("unhandled interrupt"); .C = struct {
} fn tmp() callconv(.C) noreturn {
@panic("unhandled interrupt");
}
}.tmp,
};

@ -56,19 +56,10 @@ pub fn findValueForKey(node: ?*Node, key: []const u8) ?[]const u8 {
} }
pub fn parseDescription(allocator: Allocator, node: ?*Node, key: []const u8) !?[]const u8 { pub fn parseDescription(allocator: Allocator, node: ?*Node, key: []const u8) !?[]const u8 {
return if (findValueForKey(node, key)) |value| blk: { return if (findValueForKey(node, key)) |value|
var str = std.ArrayList(u8).init(allocator); try allocator.dupe(u8, value)
errdefer str.deinit(); else
null;
var it = std.mem.tokenize(u8, value, " \n\t\r");
try str.appendSlice(it.next() orelse return null);
while (it.next()) |token| {
try str.append(' ');
try str.appendSlice(token);
}
break :blk str.toOwnedSlice();
} else null;
} }
pub fn parseIntForKey(comptime T: type, allocator: std.mem.Allocator, node: ?*Node, key: []const u8) !?T { pub fn parseIntForKey(comptime T: type, allocator: std.mem.Allocator, node: ?*Node, key: []const u8) !?T {

Loading…
Cancel
Save