microzig icon indicating copy to clipboard operation
microzig copied to clipboard

Idea: Convert APIs that takes addresses in HALs to take `anytype`.

Open Grazfather opened this issue 8 months ago • 3 comments

We have a bunch of functions that take addresses in our various HALs, but the type is often u32 or usize.

We should evaluate how they are used, and see if people tend to call them with points (e.g. by providing a slice or array) or an actual int.

In the former case, they'd have to do something like @intFromPtr, which is fine, but a bit annoying.

We can improve the ergonomics of these APIs (if we find that usage common) by having the type be anytype, and make type checks to ensure it is a pointer or appropriate type.

@@ -95,14 +95,16 @@ pub const Channel = enum(u4) {
 
     pub fn trigger_transfer(
         chan: Channel,
-        write_addr: u32,
-        read_addr: u32,
+        write_addr: anytype,
+        read_addr: anytype,
         count: u32,
         config: TransferConfig,
     ) void {
+        comptime if (@typeInfo(@TypeOf(write_addr)) != .pointer) @compileError("write_addr must be a pointer");
+        comptime if (@typeInfo(@TypeOf(read_addr)) != .pointer) @compileError("read_addr must be a pointer");
         const regs = chan.get_regs();
-        regs.read_addr = read_addr;
-        regs.write_addr = write_addr;
+        regs.read_addr = @intFromPtr(read_addr);
+        regs.write_addr = @intFromPtr(write_addr);
         regs.trans_count = count;
         regs.ctrl_trig.modify(.{
             .EN = @intFromBool(config.enable),

This allows the client to have a cleaner call:

     channel.?.trigger_transfer(
-        @intFromPtr(dst[0..dst.len].ptr),
-        @intFromPtr(hello[0..src.len].ptr),
+        dst[0..dst.len].ptr,
+        src[0..hello.len].ptr,
         src.len,
         .{

Of course, we can allow ints of the appropriate size as well, and we can write a helper to verify the type.

Grazfather avatar Apr 16 '25 11:04 Grazfather

I think that's pretty good as long as we don't allow slices to be passed (as one would expect slice.len to be considered).

ikskuh avatar Apr 16 '25 11:04 ikskuh

We could even take *anyopaque and *const anyopaque. The pointer casting to anyopaque is implicit afaik. This way we only allow pointers (which will probably be the most common passed type).

tact1m4n3 avatar Sep 07 '25 12:09 tact1m4n3

Ouch, i missed this issue, it is partially done by #517

vesim987 avatar Sep 11 '25 21:09 vesim987