Help with integer pointer to a 4k memory block on embedded target

This SoC has a 4k register that I have been previously succesfully wrapping with the following code. Now I am porting to latest Zig, and struggling to find a replacement since @memset() lost it’s size parameter. Any tips please?

pub fn Reg4k(comptime addr: u32) type {
    const size4k = 0x1000;
    return struct {
        const ptr: [*]volatile u8 = @intToPtr([*]volatile u8, addr);

        pub fn clear() void {
            @memset(ptr, 0, size4k);
        }
    };
}

if only there was a pointer type that has length information :3 @memset(ptr[0..size4k], 0)

you should probably specify an alignment for your pointer if the SoC specifies one.

you could also store a pointer to an array, instead of a multi item pointer, then you wouldn’t need to slice it (which actually makes a pointer to an array when the slicing is comptime know)

That was the solution I would go with.

Spent a while trying to muck about with pointer casting to a sized array instead of doing the slice, until I realized you could just store a pointer to the array instead.

I think this should work too, @whitehexagon:

pub fn Reg4k(comptime addr: u32) type {
    const size4k = 0x1000;
    return struct {
        const ptr: *volatile [size4k]u8 = @ptrFromInt(addr); // renamed recently

        pub fn clear() void {
            @memset(ptr, 0);
        }
    };
}

Thanks, I went wrong trying to add size info to the [*]volatile. That’s what happens after 2 years away from Zig, and soo much changed in that short time!

posted at same time. Yeah, that looks cleaner, thanks. I’ll let you know if I fry the chips :slight_smile:

yup seems to compile, wont know for sure until I get the rest of the display bringup done.

Just out of curiosity, what are you working on? Embedded SoC + display sounds like something that could be vaguely similar to what I’m doing professionally.

Currently hacking the PinePhone. I was previously following some nuttx tutorials but I decided to replicate the reverse engineering journey in Zig rather than C. I have most of the SoC represented in Zig since 2 years ago, just need to remove all the \t from my comments :wink: and complete the port from 0.8.1 to 0.14.

Although last time I got a bit stuck with the framebuffer declaration working from C but not Zig. But today some DSI stuff.

And yourself, if you are allowed to say?

Building accessories for industrial machinery, at the moment.

We’re looking at Zig as a potential replacement toolchain.

The PinePhone is a cool little device. Best of luck. :slight_smile:

Thanks. Code seems to be working, at least the screen lights up! So next is the framebuffer stuff. It’ll be nice to get away from using the device LED for debugging :slight_smile:

Your work sounds fun. My first electronics training was on a steel machine control system.