pioasm: support set x -1 #3205

Open
opened 2026-09-22 04:58:00 -05:00 by magy00 · 1 comment
magy00 commented 2026-09-22 04:58:00 -05:00 (Migrated from github.com)

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 == -1 && (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};
 }
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 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 == -1 && (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}; } ```
magy00 commented 2026-09-22 05:03:48 -05:00 (Migrated from github.com)

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))
An ugly workaround could be: ```text .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)) ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-raspberrypi/pico-sdk#3205
No description provided.