Skip to content

Commit

Permalink
regz and stm32: Support array fields in registers (#343)
Browse files Browse the repository at this point in the history
  • Loading branch information
marnix authored Dec 30, 2024
1 parent 2703496 commit cd3180c
Show file tree
Hide file tree
Showing 8 changed files with 18,163 additions and 3,508 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ boxzer-out
microzig-deploy/
zig-cache/
zig-out/

*.regz
21,485 changes: 18,048 additions & 3,437 deletions port/stmicro/stm32/src/chips/all.zig

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion port/stmicro/stm32/src/generate.zig
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn main() !void {
// it's in bytes
const stride = array.object.get("stride").?.integer;
if (stride != 4) {
std.log.warn("found stride: {} for {s} in {s} in {s}", .{ stride, register_name, key["block/".len..], name });
std.log.warn("ignoring register array with unsupported stride: {} != 4 for register {s} in {s} in {s}", .{ stride, register_name, key["block/".len..], name });
break :blk null;
}

Expand Down Expand Up @@ -340,13 +340,21 @@ pub fn main() !void {
if (enums.get(enum_name.string)) |enum_id| enum_id else null
else
null;
var array_count: ?u16 = null;
var array_stride: ?u8 = null;
if (field.object.get("array")) |array| {
array_count = if (array.object.get("len")) |len| @intCast(len.integer) else null;
array_stride = if (array.object.get("stride")) |stride| @intCast(stride.integer) else null;
}

try db.add_register_field(register_id, .{
.name = field_name,
.description = field_description,
.offset_bits = @intCast(bit_offset),
.size_bits = @intCast(bit_size),
.enum_id = enum_id,
.count = array_count,
.stride = array_stride,
});
}
}
Expand Down
18 changes: 13 additions & 5 deletions tools/regz/src/Database.zig
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,8 @@ pub const StructField = struct {
size_bits: u8,
offset_bits: u8,
enum_id: ?EnumID,
count: ?u64,
count: ?u16,
stride: ?u8,

pub const sql_opts = SQL_Options{
.foreign_keys = &.{
Expand All @@ -217,6 +218,8 @@ pub const StructField = struct {
};

pub fn get_size_bits(field: *const StructField) u32 {
if (field.count != null and field.count.? > 1 and field.stride > field.size_bits)
log.warn("get_size_bits() result is unreliable for field array {s} with stride {d} bits > size {d} bits", .{ field.name, field.stride, field.size_bits });
return if (field.count) |count|
@intCast(field.size_bits * count)
else
Expand Down Expand Up @@ -1141,6 +1144,7 @@ pub fn get_register_fields(
\\ sf.offset_bits,
\\ sf.enum_id,
\\ sf.count,
\\ sf.stride,
\\ ROW_NUMBER() OVER (
\\ PARTITION BY sf.struct_id, sf.offset_bits
\\ ORDER BY sf.offset_bits ASC, sf.size_bits ASC
Expand All @@ -1157,7 +1161,8 @@ pub fn get_register_fields(
\\ size_bits,
\\ offset_bits,
\\ enum_id,
\\ count
\\ count,
\\ stride
\\ FROM OrderedFields
\\ WHERE row_num = 1
\\)
Expand Down Expand Up @@ -1714,6 +1719,7 @@ pub const AddStructFieldOptions = struct {
offset_bits: u8,
enum_id: ?EnumID = null,
count: ?u16 = null,
stride: ?u8 = null,
};

pub fn add_register_field(db: *Database, parent: RegisterID, opts: AddStructFieldOptions) !void {
Expand Down Expand Up @@ -1743,9 +1749,9 @@ pub fn add_struct_field(db: *Database, parent: StructID, opts: AddStructFieldOpt

try db.exec(
\\INSERT INTO struct_fields
\\ (struct_id, name, description, size_bits, offset_bits, enum_id, count)
\\ (struct_id, name, description, size_bits, offset_bits, enum_id, count, stride)
\\VALUES
\\ (?, ?, ?, ?, ?, ?, ?)
\\ (?, ?, ?, ?, ?, ?, ?, ?)
, .{
.struct_id = parent,
.name = opts.name,
Expand All @@ -1754,17 +1760,19 @@ pub fn add_struct_field(db: *Database, parent: StructID, opts: AddStructFieldOpt
.offset_bits = opts.offset_bits,
.enum_id = opts.enum_id,
.count = opts.count,
.stride = opts.stride,
});

savepoint.commit();

log.debug("add_struct_field: parent={} name='{s}' offset_bits={} size_bits={} enum_id={?} count={?}", .{
log.debug("add_struct_field: parent={} name='{s}' offset_bits={} size_bits={} enum_id={?} count={?} stride={?}", .{
parent,
opts.name,
opts.offset_bits,
opts.size_bits,
opts.enum_id,
opts.count,
opts.stride,
});
}

Expand Down
2 changes: 1 addition & 1 deletion tools/regz/src/arch/arm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ pub fn write_interrupt_vector(
}

if (interrupt.description) |description|
try gen.write_comment(db.gpa, description, writer);
try gen.write_doc_comment(db.gpa, description, writer);

try writer.print("{}: Handler = unhandled,\n", .{
std.zig.fmtId(interrupt.name),
Expand Down
2 changes: 1 addition & 1 deletion tools/regz/src/arch/avr.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn write_interrupt_vector(
}

if (interrupt.description) |description|
try gen.write_comment(arena, description, writer);
try gen.write_doc_comment(arena, description, writer);

try writer.print("{}: Handler = unhandled,\n", .{
std.zig.fmtId(interrupt.name),
Expand Down
2 changes: 1 addition & 1 deletion tools/regz/src/arch/riscv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ pub fn write_interrupt_vector(
}

if (interrupt.description) |description|
try gen.write_comment(db.gpa, description, writer);
try gen.write_doc_comment(db.gpa, description, writer);

try writer.print("{}: Handler = unhandled,\n", .{
std.zig.fmtId(interrupt.name),
Expand Down
Loading

0 comments on commit cd3180c

Please sign in to comment.