From 111fa7de2d761975e19872cf46718b2399e6c33d Mon Sep 17 00:00:00 2001 From: Graham Sanderson Date: Wed, 5 Feb 2025 10:55:24 -0600 Subject: [PATCH] small pioasm improvements (#2224) - fix disassembly of `wait jmppin` - fix incorrect error message - make python output emit `word(x)` for all unsupported instructions --- tools/pioasm/pio_assembler.cpp | 2 +- tools/pioasm/pio_disassembler.cpp | 11 ++++++--- tools/pioasm/python_output.cpp | 38 ++++++++++++++++++------------- tools/pioasm/test/amethyst.pio | 7 ++++++ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/tools/pioasm/pio_assembler.cpp b/tools/pioasm/pio_assembler.cpp index a427c22e..3a8a3009 100644 --- a/tools/pioasm/pio_assembler.cpp +++ b/tools/pioasm/pio_assembler.cpp @@ -63,7 +63,7 @@ void program::set_pio_version(const yy::location &l, int version) { void program::set_clock_div(const yy::location &l, float clock_div) { if (clock_div < 1.0f || clock_div >= 65536.0f) { - throw syntax_error(l, "clock divider must be between 1 and 65546"); + throw syntax_error(l, "clock divider must be between 1 and 65535"); } clock_div_int = (uint16_t)clock_div; if (clock_div_int == 0) { diff --git a/tools/pioasm/pio_disassembler.cpp b/tools/pioasm/pio_disassembler.cpp index 2a34796d..74d862bc 100644 --- a/tools/pioasm/pio_disassembler.cpp +++ b/tools/pioasm/pio_disassembler.cpp @@ -56,7 +56,9 @@ std::string disassemble(uint inst, uint sideset_bits_including_opt, bool sideset if (arg2 & 0x1cu) { invalid = true; } else if (arg2) { - guts = "jmppin " + std::to_string(arg2 & 3u); + guts = "jmppin + " + std::to_string(arg2 & 3u); + } else { + guts = "jmppin"; } break; } @@ -131,7 +133,7 @@ std::string disassemble(uint inst, uint sideset_bits_including_opt, bool sideset op("mov"); std::string guts = dest + ", "; if (operation == 1) { - guts += "!"; + guts += "~"; } else if (operation == 2) { guts += "::"; } @@ -193,6 +195,9 @@ std::string disassemble(uint inst, uint sideset_bits_including_opt, bool sideset } delay &= ((1u << (5 - sideset_bits_including_opt)) - 1u); ss << std::left << std::setw(4) << (delay ? ("[" + std::to_string(delay) + "]") : ""); - return ss.str(); + // remove trailing spaces + auto str = ss.str(); + str.erase(str.find_last_not_of(' ')+1); + return str; } diff --git a/tools/pioasm/python_output.cpp b/tools/pioasm/python_output.cpp index bcaa8d61..f4cf326b 100644 --- a/tools/pioasm/python_output.cpp +++ b/tools/pioasm/python_output.cpp @@ -204,6 +204,9 @@ struct python_output : public output_format { } } break; + default: + invalid = true; + break; } if (!invalid) { guts = ((arg1 & 4u) ? "1, " : "0, ") + guts; @@ -254,23 +257,24 @@ struct python_output : public output_format { uint operation = arg2 >> 3u; if (source.empty() || dest.empty() || operation == 3) { invalid = true; - } - if (dest == source && (arg1 == 1 || arg2 == 2) && operation == 0) { - op("nop"); - op_guts(""); } else { - op("mov"); - std::string guts = dest + ", "; - if (operation == 1) { - guts += "invert("; - } else if (operation == 2) { - guts += "reverse("; + if (dest == source && (arg1 == 1 || arg2 == 2) && operation == 0) { + op("nop"); + op_guts(""); + } else { + op("mov"); + std::string guts = dest + ", "; + if (operation == 1) { + guts += "invert("; + } else if (operation == 2) { + guts += "reverse("; + } + guts += source; + if (operation == 1 || operation == 2) { + guts += ")"; + } + op_guts(guts); } - guts += source; - if (operation == 1 || operation == 2) { - guts += ")"; - } - op_guts(guts); } break; } @@ -310,7 +314,9 @@ struct python_output : public output_format { if (invalid) { op("word"); ss << std::hex; - op_guts(std::to_string(inst)); + std::stringstream guts; + guts << std::hex << std::showbase << std::setfill('0') << std::setw(4) << inst; + op_guts(guts.str()); } uint delay = ((uint) inst >> 8u) & 0x1f; ss << std::left << std::setw(9); diff --git a/tools/pioasm/test/amethyst.pio b/tools/pioasm/test/amethyst.pio index 97422a22..6b816046 100644 --- a/tools/pioasm/test/amethyst.pio +++ b/tools/pioasm/test/amethyst.pio @@ -44,3 +44,10 @@ wait gpio 40 .pio_version 1 .mov_status txfifo < 12 .mov_status irq next set 3 + +.program python +.pio_version 1 +wait 0 jmppin +wait 0 jmppin + 3 +mov x, !x +.word 0x1234 \ No newline at end of file