Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: improving the generated output, to create structs ? #7

Open
frett27 opened this issue Mar 14, 2020 · 7 comments
Open

RFC: improving the generated output, to create structs ? #7

frett27 opened this issue Mar 14, 2020 · 7 comments

Comments

@frett27
Copy link

frett27 commented Mar 14, 2020

struct is a first class citizen in zig, from looking other repos, the natural way to expose the mappings, seems, using const packed structs :
https://github.com/tralamazza/embedded_zig/blob/master/core_cm3.zig
or
https://github.com/tralamazza/embedded_zig/blob/master/stm32f10.zig

this probably will need to leverage the XML parsing.

@justinbalexander
Copy link
Owner

Take a look at #4 which I think is the same as this proposal except that the structs aren't extern.

For accessing memory locations, that is how I am doing it in my repo zig-cortex as the amount of work to hand roll the structs is relatively small, but for this one I wasn't sure if it would be worth the effort until the proposal to deprecate the volatile keyword is resolved. Andrew said that the issue wouldn't be resolved or decided on until the next release cycle. Until then, I don't want to put in the effort since this repo was just to get another project of mine off the ground.

Just the svd parsing needs a lot of work as the other vendor's file formats have many currently unsupported svd fields. I will spiff this whole thing up once the language stabilizes a little bit more.

I would gladly accept a pull request implementing the struct representation, however.

@rbino
Copy link
Contributor

rbino commented Mar 24, 2021

My 2 cents on this: I found both this project and https://github.com/lynaghk/svd2zig while I was trying having a go at Zig for the first time trying to use it to blink an LED on an STM32 board.

I liked the packed struct approach (there's also a blogpost about it) but the "other" svd2zig was written in Clojure and it was oriented towards Nordic devices (and by looking both at the svd and at generated code it seems that every vendor is using svd in a different way).

So I tried to make this repo's svd2zig to produce that kind of output, and I actually got it to work (see my branch here) but it seems that support for packed structs is currently a little broken so the first 3 or 4 registers worked but then I stumbled upon one which didn't work, which boils down to this test failing:

const std = @import("std");
const assert = std.debug.assert;

/// ACR
const ACR_val = packed struct {
    /// LATENCY [0:2]
    /// Latency
    LATENCY: u3 = 0,
    _unused3: u5 = 0,
    /// PRFTEN [8:8]
    /// Prefetch enable
    PRFTEN: u1 = 0,
    /// ICEN [9:9]
    /// Instruction cache enable
    ICEN: u1 = 0,
    /// DCEN [10:10]
    /// Data cache enable
    DCEN: u1 = 0,
    /// ICRST [11:11]
    /// Instruction cache reset
    ICRST: u1 = 0,
    /// DCRST [12:12]
    /// Data cache reset
    DCRST: u1 = 0,
    _unused13: u19 = 0,
};

test "correct size" {
    assert(@sizeOf(ACR_val) == 4);
}

The packed struct sums to 32 bits hence should have a size of 4, but it actually returns 5.

So I think that packed struct have to be fixed in the compiler before they can be used reliably for MMIO. When it does I'd be happy to merge my branch and/or use it as a base for some different kind of output.

@N00byEdge
Copy link

@rbino I'm pretty sure you can't use u1 et al for mmio anyways, since you usually need to write e.g. an entire 32 bit regsiter at a time. I made a small bitfield type which can be used for mmio in zig. It can be used like this.

@rbino
Copy link
Contributor

rbino commented Apr 5, 2021

@N00byEdge the u1 fields get packed in a struct which represents the whole 32 bit register, which is read/written at once (see the blogpost linked above).

I've actually spent some more time on the struct output stuff and I think I've found a workaround for the packed structs bug (namely, splitting the unused chunks at 8 bit boundaries).

The result is here, since the new format is a major breaking change I created a fork but if @justinbalexander is ok with it I can open a pull request to upstream those changes.

@N00byEdge
Copy link

@rbino Oh okay, didn't read your blog post, just the code you posted here. Looks neat!

@rbino
Copy link
Contributor

rbino commented Apr 5, 2021

@N00byEdge the blogpost is not mine actually, I just took that idea and used it to make svd2zig generate that kind of output. I find the resulting API extremely nice to use, especially when combined with register names taken directly from the datasheet.

@justinbalexander
Copy link
Owner

@rbino Very cool that you got that to work. I am actually not as much of a fan of that API, personally but I'm glad it is working for you. I am used to C and like that Vim's autocompletion works on just text. I have started using zls, though and so maybe I will change my mind.

I actually got tired of working on my main project in Zig because of the bugs and switched back to C. Looks like you are carrying the torch now!

Maybe when the self-hosted compiler is up and running I will take a look at this again and setup more configuration options where you can select the API you desire. For now I will consider yours to the be the "most official repo".

Thanks again for the pull requests and good luck!

P.S. Are you sure all your bugs are ironed out? These "simple" bugs finally broke my will...
https://github.com/ziglang/zig/issues/2627#issuecomment-813028755

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants