Skip to content

Commit cd3180c

Browse files
authored
regz and stm32: Support array fields in registers (#343)
1 parent 2703496 commit cd3180c

File tree

8 files changed

+18163
-3508
lines changed

8 files changed

+18163
-3508
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ boxzer-out
99
microzig-deploy/
1010
zig-cache/
1111
zig-out/
12+
13+
*.regz

port/stmicro/stm32/src/chips/all.zig

+18,048-3,437
Large diffs are not rendered by default.

port/stmicro/stm32/src/generate.zig

+9-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ pub fn main() !void {
310310
// it's in bytes
311311
const stride = array.object.get("stride").?.integer;
312312
if (stride != 4) {
313-
std.log.warn("found stride: {} for {s} in {s} in {s}", .{ stride, register_name, key["block/".len..], name });
313+
std.log.warn("ignoring register array with unsupported stride: {} != 4 for register {s} in {s} in {s}", .{ stride, register_name, key["block/".len..], name });
314314
break :blk null;
315315
}
316316

@@ -340,13 +340,21 @@ pub fn main() !void {
340340
if (enums.get(enum_name.string)) |enum_id| enum_id else null
341341
else
342342
null;
343+
var array_count: ?u16 = null;
344+
var array_stride: ?u8 = null;
345+
if (field.object.get("array")) |array| {
346+
array_count = if (array.object.get("len")) |len| @intCast(len.integer) else null;
347+
array_stride = if (array.object.get("stride")) |stride| @intCast(stride.integer) else null;
348+
}
343349

344350
try db.add_register_field(register_id, .{
345351
.name = field_name,
346352
.description = field_description,
347353
.offset_bits = @intCast(bit_offset),
348354
.size_bits = @intCast(bit_size),
349355
.enum_id = enum_id,
356+
.count = array_count,
357+
.stride = array_stride,
350358
});
351359
}
352360
}

tools/regz/src/Database.zig

+13-5
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,8 @@ pub const StructField = struct {
201201
size_bits: u8,
202202
offset_bits: u8,
203203
enum_id: ?EnumID,
204-
count: ?u64,
204+
count: ?u16,
205+
stride: ?u8,
205206

206207
pub const sql_opts = SQL_Options{
207208
.foreign_keys = &.{
@@ -217,6 +218,8 @@ pub const StructField = struct {
217218
};
218219

219220
pub fn get_size_bits(field: *const StructField) u32 {
221+
if (field.count != null and field.count.? > 1 and field.stride > field.size_bits)
222+
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 });
220223
return if (field.count) |count|
221224
@intCast(field.size_bits * count)
222225
else
@@ -1141,6 +1144,7 @@ pub fn get_register_fields(
11411144
\\ sf.offset_bits,
11421145
\\ sf.enum_id,
11431146
\\ sf.count,
1147+
\\ sf.stride,
11441148
\\ ROW_NUMBER() OVER (
11451149
\\ PARTITION BY sf.struct_id, sf.offset_bits
11461150
\\ ORDER BY sf.offset_bits ASC, sf.size_bits ASC
@@ -1157,7 +1161,8 @@ pub fn get_register_fields(
11571161
\\ size_bits,
11581162
\\ offset_bits,
11591163
\\ enum_id,
1160-
\\ count
1164+
\\ count,
1165+
\\ stride
11611166
\\ FROM OrderedFields
11621167
\\ WHERE row_num = 1
11631168
\\)
@@ -1714,6 +1719,7 @@ pub const AddStructFieldOptions = struct {
17141719
offset_bits: u8,
17151720
enum_id: ?EnumID = null,
17161721
count: ?u16 = null,
1722+
stride: ?u8 = null,
17171723
};
17181724

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

17441750
try db.exec(
17451751
\\INSERT INTO struct_fields
1746-
\\ (struct_id, name, description, size_bits, offset_bits, enum_id, count)
1752+
\\ (struct_id, name, description, size_bits, offset_bits, enum_id, count, stride)
17471753
\\VALUES
1748-
\\ (?, ?, ?, ?, ?, ?, ?)
1754+
\\ (?, ?, ?, ?, ?, ?, ?, ?)
17491755
, .{
17501756
.struct_id = parent,
17511757
.name = opts.name,
@@ -1754,17 +1760,19 @@ pub fn add_struct_field(db: *Database, parent: StructID, opts: AddStructFieldOpt
17541760
.offset_bits = opts.offset_bits,
17551761
.enum_id = opts.enum_id,
17561762
.count = opts.count,
1763+
.stride = opts.stride,
17571764
});
17581765

17591766
savepoint.commit();
17601767

1761-
log.debug("add_struct_field: parent={} name='{s}' offset_bits={} size_bits={} enum_id={?} count={?}", .{
1768+
log.debug("add_struct_field: parent={} name='{s}' offset_bits={} size_bits={} enum_id={?} count={?} stride={?}", .{
17621769
parent,
17631770
opts.name,
17641771
opts.offset_bits,
17651772
opts.size_bits,
17661773
opts.enum_id,
17671774
opts.count,
1775+
opts.stride,
17681776
});
17691777
}
17701778

tools/regz/src/arch/arm.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ pub fn write_interrupt_vector(
127127
}
128128

129129
if (interrupt.description) |description|
130-
try gen.write_comment(db.gpa, description, writer);
130+
try gen.write_doc_comment(db.gpa, description, writer);
131131

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

tools/regz/src/arch/avr.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub fn write_interrupt_vector(
4747
}
4848

4949
if (interrupt.description) |description|
50-
try gen.write_comment(arena, description, writer);
50+
try gen.write_doc_comment(arena, description, writer);
5151

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

tools/regz/src/arch/riscv.zig

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ pub fn write_interrupt_vector(
9595
}
9696

9797
if (interrupt.description) |description|
98-
try gen.write_comment(db.gpa, description, writer);
98+
try gen.write_doc_comment(db.gpa, description, writer);
9999

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

0 commit comments

Comments
 (0)