Skip to content

Add pioasm c++ constexpr output target #2400

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

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

audiobird
Copy link

Adds pioasm output that creates a c++ header file with a constexpr std::array containing the raw instructions.

Similar to the hex output option, only the raw instructions are generated. However, unlike the hex output option, multiple programs are supported in a single pio source file, with a std::array generated for each one.

One thing I'm unsure of is what namespace to put the generated arrays in. I went with "Pioasm" here but we could also go with "PIOInstructions" or something else entirely.

Input example: "ws2812.pio"

.program ws2812 
.side_set 1

.wrap_target
bitloop:
    out x, 1       side 0 [2];
    jmp !x do_zero side 1 [1];
do_one:
    jmp  bitloop   side 1 [4];
do_zero:
    nop            side 0 [4];
.wrap

pioasm -o constexpr "ws2812.pio.hpp" "ws2812.pio"

Output: "ws2812.pio.hpp"

#pragma once

#include <array>
#include <cstdint>

namespace Pioasm {

inline constexpr std::array<uint16_t, 4> ws2812 = {
            //     .wrap_target
    0x6221, //  0: out    x, 1            side 0 [2]
    0x1123, //  1: jmp    !x, 3           side 1 [1]
    0x1400, //  2: jmp    0               side 1 [4]
    0xa442, //  3: nop                    side 0 [4]
            //     .wrap
};

}

@kilograham kilograham added this to the 2.2.0 milestone Apr 7, 2025
@kilograham
Copy link
Contributor

seems like you could add an output option for the namespace

@audiobird
Copy link
Author

Great, currently it looks like none of the output formats actually use the std::vector<std::string> output_options argument in the virtual output function, so I have no reference on what the preferred syntax would look like for the switch.

How about:
pioasm -o constexpr -p namespace=some_namespace ...

And would it be best to have a default namespace value or to require one?

@kilograham
Copy link
Contributor

yes it seems ada has one, but i think i prefer yours (name=value)

i think it should have a default value (though perhaps that is no namespace)

@audiobird
Copy link
Author

I agree, no namespace by default makes sense. Should be good to go!

@magy00
Copy link

magy00 commented Apr 8, 2025

Maybe add -fimplicit-constexpr for g++.

@lurch lurch added the pioasm label Apr 26, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants