Skip to content

pioasm: support set x -1 #3205

Open
Open

Description

@magy00

Sometimes, it could be convenient when set x, -1 could also generate mov x, ~null (especially when -1 is a calculated value like (n-1 << 25 >> 25)). Not for pins and pindirs (different pin mappings) but for x and y:

diff --git tools/pioasm/pio_assembler.cpp tools/pioasm/pio_assembler.cpp
index a36854b2..5de3d972 100644
--- tools/pioasm/pio_assembler.cpp
+++ tools/pioasm/pio_assembler.cpp
@@ -385,8 +385,12 @@ raw_encoding instr_out::raw_encode(program& program) {

 raw_encoding instr_set::raw_encode(program& program) {
     int v = value->resolve(program);
+    if (v == ~0 && (dest == in_out_set::in_out_set_x || dest == in_out_set::in_out_set_y)) {
+        return {inst_type::mov, (uint) dest, (uint) mov::null | ((uint) mov_op::invert << 3u)};
+    }
+
     if (v < 0 || v > 31) {
-        throw syntax_error(value->location, "'set' bit count must be >= 0 and <= 31");
+        throw syntax_error(value->location, "'set' value must be >= 0 and <= 31 (or -1 for x and y)");
     }
     return {inst_type::set, (uint) dest, (uint) v};
 }

Activity

  1. magy00 commented on Sep 22, 2026

    @magy00
    ContributorAuthor

    An ugly workaround could be:

    .define set_y   0b1110000001000000  // set  y, 0
    .define mov_y   0b1010000001001011  // mov  y, ~null
    //  switch between  set y, (n-1 << 25 >> 25)  and  mov y, ~null
    .word (set_y + (n-1 << 25 >> 25) + (set_y-mov_y-1) * (n-1 << 25 >> 31))
    
  2. kvnloo commented on Sep 29, 2026

    @kvnloo

    I think this can work as an explicit pioasm pseudo-instruction, but it is worth being precise that the assembler would be lowering:

    set x, -1

    to a different hardware instruction:

    mov x, ~null

    rather than encoding a special SET value.

    For x and y the resulting register value is equivalent. pins and pindirs should continue to reject -1.

    The current instruction::encode() adds delay and side-set after raw_encode(), so those fields should remain preserved even if instr_set::raw_encode() returns a MOV encoding.

    Before landing it I'd cover:

    • set x, -1
    • set y, -1
    • an expression that resolves to -1
    • rejection for pins
    • rejection for pindirs
    • generated instruction/disassembly reflecting the actual MOV encoding

    Also, if -1 becomes a supported value, the existing error should probably say set value rather than set bit count.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions