From 4328b2c75f49bc0ae9f727ab9c8a982a6694b836 Mon Sep 17 00:00:00 2001 From: Uri Shaked Date: Tue, 8 Jun 2021 06:02:04 +0300 Subject: [PATCH] fix pioasm python output (#479) While working on the [online pioasm](https://wokwi.com/tools/pioasm), I found several PIO instructions that result in invalid python code. Here is a small program that demonstrate the issue: ``` .program python_issue push block wait 0 irq 1 rel irq clear 1 rel ``` And the resulting Python program: ```python # -------------------------------------------------- # # This file is autogenerated by pioasm; do not edit! # # -------------------------------------------------- # import rp2 from machine import Pin # ----------- # # python_test # # ----------- # @rp2.asm_pio() def python_test(): wrap_target() push(, block) # 0 wait(0, irq, 1 rel) # 1 irq(clear 1 rel) # 2 wrap() ``` After this fix, the above program compiles to a valid python syntax: ```python # -------------------------------------------------- # # This file is autogenerated by pioasm; do not edit! # # -------------------------------------------------- # import rp2 from machine import Pin # ----------- # # python_test # # ----------- # @rp2.asm_pio() def python_test(): wrap_target() push(block) # 0 wait(0, irq, rel(1)) # 1 irq(clear, rel(1)) # 2 wrap() ``` --- tools/pioasm/python_output.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/pioasm/python_output.cpp b/tools/pioasm/python_output.cpp index fb94e52..38f7ffe 100644 --- a/tools/pioasm/python_output.cpp +++ b/tools/pioasm/python_output.cpp @@ -195,9 +195,12 @@ struct python_output : public output_format { if (arg2 & 0x8u) { invalid = true; } else { - guts = "irq, " + std::to_string(arg2 & 7u); + guts = "irq, "; + auto irq = std::to_string(arg2 & 7u); if (arg2 & 0x10u) { - guts += " rel"; + guts += "rel(" + irq + ")"; + } else { + guts += irq; } } break; @@ -233,12 +236,11 @@ struct python_output : public output_format { std::string guts = ""; if (arg1 & 4u) { op("pull"); - if (arg1 & 2u) guts = "ifempty"; + if (arg1 & 2u) guts = "ifempty, "; } else { op("push"); - if (arg1 & 2u) guts = "iffull"; + if (arg1 & 2u) guts = "iffull, "; } - guts += ", "; guts += ((arg1 & 0x1u) ? "block" : "noblock"); op_guts(guts); } @@ -279,15 +281,17 @@ struct python_output : public output_format { op("irq"); std::string guts; if (arg1 & 0x2u) { - guts += "clear "; + guts += "clear, "; } else if (arg1 & 0x1u) { - guts += "wait "; + guts += "wait, "; } else { - guts += "nowait "; + guts += "nowait, "; } - guts += std::to_string(arg2 & 7u); + auto irq = std::to_string(arg2 & 7u); if (arg2 & 0x10u) { - guts += " rel"; + guts += "rel(" + irq + ")"; + } else { + guts += irq; } op_guts(guts); }