Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 1 | /* |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 2 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <array> |
| 8 | #include <algorithm> |
| 9 | #include <sstream> |
| 10 | #include <iomanip> |
| 11 | #include <iostream> |
| 12 | #include "output_format.h" |
| 13 | #include "pio_disassembler.h" |
| 14 | |
| 15 | struct python_output : public output_format { |
| 16 | struct factory { |
| 17 | factory() { |
| 18 | output_format::add(new python_output()); |
| 19 | } |
| 20 | }; |
| 21 | |
| 22 | python_output() : output_format("python") {} |
| 23 | |
| 24 | std::string get_description() override { |
| 25 | return "Python file suitable for use with MicroPython"; |
| 26 | } |
| 27 | |
| 28 | void output_symbols(FILE *out, std::string prefix, const std::vector<compiled_source::symbol> &symbols) { |
| 29 | int count = 0; |
| 30 | for (const auto &s : symbols) { |
| 31 | if (!s.is_label) { |
| 32 | fprintf(out, "%s%s = %d\n", prefix.c_str(), s.name.c_str(), s.value); |
| 33 | count++; |
| 34 | } |
| 35 | } |
| 36 | if (count) { |
| 37 | fprintf(out, "\n"); |
| 38 | count = 0; |
| 39 | } |
| 40 | for (const auto &s : symbols) { |
| 41 | if (s.is_label) { |
| 42 | fprintf(out, "%soffset_%s = %d\n", prefix.c_str(), s.name.c_str(), s.value); |
| 43 | count++; |
| 44 | } |
| 45 | } |
| 46 | if (count) { |
| 47 | fprintf(out, "\n"); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void header(FILE *out, std::string msg) { |
| 52 | std::string dashes = std::string(msg.length(), '-'); |
| 53 | fprintf(out, "# %s #\n", dashes.c_str()); |
| 54 | fprintf(out, "# %s #\n", msg.c_str()); |
| 55 | fprintf(out, "# %s #\n", dashes.c_str()); |
| 56 | fprintf(out, "\n"); |
| 57 | } |
| 58 | |
| 59 | int output(std::string destination, std::vector<std::string> output_options, |
| 60 | const compiled_source &source) override { |
| 61 | FILE *out = open_single_output(destination); |
| 62 | if (!out) return 1; |
| 63 | |
| 64 | header(out, "This file is autogenerated by pioasm; do not edit!"); |
| 65 | |
| 66 | fprintf(out, "import rp2\n"); |
| 67 | fprintf(out, "from machine import Pin"); |
| 68 | fprintf(out, "\n"); |
| 69 | |
| 70 | output_symbols(out, "", source.global_symbols); |
| 71 | |
| 72 | for (const auto &program : source.programs) { |
| 73 | header(out, program.name); |
| 74 | |
| 75 | std::string prefix = program.name + "_"; |
| 76 | |
| 77 | output_symbols(out, prefix, program.symbols); |
| 78 | |
| 79 | int param_count = 0; |
| 80 | auto write_opt = [&] (std::string name, std::string value) { |
| 81 | if (param_count++) { |
| 82 | fprintf(out, ", "); |
| 83 | } |
| 84 | fprintf(out, "%s=%s", name.c_str(), value.c_str()); |
| 85 | }; |
| 86 | fprintf(out, "@rp2.asm_pio("); |
| 87 | for(const auto &p : program.lang_opts) { |
| 88 | if (p.first.size() >= name.size() && p.first.compare(0, name.size(), name) == 0) { |
| 89 | for (const auto &p2 : p.second) { |
| 90 | write_opt(p2.first, p2.second); |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | fprintf(out, ")\n"); |
| 95 | fprintf(out, "def %s():\n", program.name.c_str()); |
| 96 | |
| 97 | std::map<uint, std::string> jmp_labels; |
| 98 | // for now just use numeric labels |
| 99 | for (int i = 0; i < (int)program.instructions.size(); i++) { |
| 100 | const auto &inst = program.instructions[i]; |
| 101 | if (!(inst >> 13u)) { |
| 102 | // a jump |
| 103 | uint target = inst &0x1fu; |
| 104 | jmp_labels.insert(std::pair<uint,std::string>(target, std::to_string(target))); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | for (uint i = 0; i < (int)program.instructions.size(); i++) { |
| 109 | const auto &inst = program.instructions[i]; |
| 110 | if (i == program.wrap_target) { |
| 111 | fprintf(out, " wrap_target()\n"); |
| 112 | } |
| 113 | auto it = jmp_labels.find(i); |
| 114 | if (it != jmp_labels.end()) { |
| 115 | fprintf(out, " label(\"%s\")\n", it->second.c_str()); |
| 116 | } |
| 117 | fprintf(out, " %s # %d\n", disassemble(jmp_labels, inst, program.sideset_bits_including_opt.get(), program.sideset_opt).c_str(), i); |
| 118 | if (i == program.wrap) { |
| 119 | fprintf(out, " wrap()\n"); |
| 120 | } |
| 121 | } |
| 122 | fprintf(out, "\n"); |
| 123 | |
| 124 | /* |
| 125 | fprintf(out, "static inline pio_sm_config %sprogram_default_config(uint offset) {\n", prefix.c_str()); |
| 126 | fprintf(out, " pio_sm_config c = pio_sm_default_config();\n"); |
| 127 | fprintf(out, " sm_config_wrap(&c, offset + %swrap_target, offset + %swrap);\n", prefix.c_str(), |
| 128 | prefix.c_str()); |
| 129 | if (program.sideset_bits_including_opt.is_specified()) { |
| 130 | fprintf(out, " sm_config_sideset(&c, %d, %s, %s);\n", program.sideset_bits_including_opt.get(), |
| 131 | program.sideset_opt ? "true" : "false", |
| 132 | program.sideset_pindirs ? "true" : "false"); |
| 133 | } |
| 134 | fprintf(out, " return c;\n"); |
| 135 | fprintf(out, "}\n"); |
| 136 | */ |
| 137 | // todo maybe have some code blocks inside or outside here? |
| 138 | for(const auto& o : program.code_blocks) { |
| 139 | fprintf(out, "\n"); |
| 140 | if (o.first == name) { |
| 141 | for(const auto &contents : o.second) { |
| 142 | fprintf(out, "%s", contents.c_str()); |
| 143 | fprintf(out, "\n"); |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fprintf(out, "\n"); |
| 149 | } |
| 150 | if (out != stdout) { fclose(out); } |
| 151 | return 0; |
| 152 | } |
| 153 | |
| 154 | static std::string disassemble(const std::map<uint, std::string>& jmp_labels, uint16_t inst, uint sideset_bits_including_opt, bool sideset_opt) { |
| 155 | std::stringstream ss; |
| 156 | uint major = inst >> 13u; |
| 157 | uint arg1 = ((uint) inst >> 5u) & 0x7u; |
| 158 | uint arg2 = inst & 0x1fu; |
| 159 | std::string op_string; |
| 160 | auto op = [&](const std::string &s) { |
| 161 | op_string = s; |
| 162 | }; |
| 163 | auto op_guts = [&](const std::string &s) { |
| 164 | ss << std::left << std::setw(24) << (op_string + "(" + s + ")"); |
| 165 | }; |
| 166 | |
| 167 | bool invalid = false; |
| 168 | switch (major) { |
| 169 | case 0b000: { |
| 170 | static std::array<std::string, 8> conditions{"", "not_x", "x_dec", "not_y", "y_dec", "x_not_y", "pin", |
| 171 | "not_osre"}; |
| 172 | op("jmp"); |
| 173 | auto it = jmp_labels.find(arg2); |
| 174 | std::string label = "?"; |
| 175 | if (it != jmp_labels.end()) { |
| 176 | label = it->second; |
| 177 | } |
| 178 | if (arg1) |
| 179 | op_guts(conditions[arg1] + ", \"" + label +"\""); |
| 180 | else |
| 181 | op_guts("\"" + label + "\""); |
| 182 | break; |
| 183 | } |
| 184 | case 0b001: { |
| 185 | uint source = arg1 & 3u; |
| 186 | std::string guts; |
| 187 | switch (source) { |
| 188 | case 0b00: |
| 189 | guts = "gpio, " + std::to_string(arg2); |
| 190 | break; |
| 191 | case 0b01: |
| 192 | guts = "pin, " + std::to_string(arg2); |
| 193 | break; |
| 194 | case 0b10: |
| 195 | if (arg2 & 0x8u) { |
| 196 | invalid = true; |
| 197 | } else { |
| 198 | guts = "irq, "; |
| 199 | auto irq = std::to_string(arg2 & 7u); |
| 200 | if (arg2 & 0x10u) { |
| 201 | guts += "rel(" + irq + ")"; |
| 202 | } else { |
| 203 | guts += irq; |
| 204 | } |
| 205 | } |
| 206 | break; |
| 207 | } |
| 208 | if (!invalid) { |
| 209 | guts = ((arg1 & 4u) ? "1, " : "0, ") + guts; |
| 210 | op("wait"); |
| 211 | op_guts(guts); |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | case 0b010: { |
| 216 | static std::array<std::string, 8> sources { "pins", "x", "y", "null", "", "status", "isr", "osr"}; |
| 217 | std::string source = sources[arg1]; |
| 218 | if (source.empty()) { |
| 219 | invalid = true; |
| 220 | } else { |
| 221 | op("in_"); |
| 222 | op_guts(source + ", " + std::to_string(arg2 ? arg2 : 32)); |
| 223 | } |
| 224 | break; |
| 225 | } |
| 226 | case 0b011: { |
| 227 | static std::array<std::string, 8> dests { "pins", "x", "y", "null", "pindirs", "pc", "isr", "exec"}; |
| 228 | op("out"); |
| 229 | op_guts(dests[arg1] + ", " + std::to_string(arg2 ? arg2 : 32)); |
| 230 | break; |
| 231 | } |
| 232 | case 0b100: { |
| 233 | if (arg2) { |
| 234 | invalid = true; |
| 235 | } else { |
| 236 | std::string guts = ""; |
| 237 | if (arg1 & 4u) { |
| 238 | op("pull"); |
| 239 | if (arg1 & 2u) guts = "ifempty, "; |
| 240 | } else { |
| 241 | op("push"); |
| 242 | if (arg1 & 2u) guts = "iffull, "; |
| 243 | } |
| 244 | guts += ((arg1 & 0x1u) ? "block" : "noblock"); |
| 245 | op_guts(guts); |
| 246 | } |
| 247 | break; |
| 248 | } |
| 249 | case 0b101: { |
| 250 | static std::array<std::string, 8> dests { "pins", "x", "y", "", "exec", "pc", "isr", "osr"}; |
| 251 | static std::array<std::string, 8> sources { "pins", "x", "y", "null", "", "status", "isr", "osr"}; |
| 252 | std::string dest = dests[arg1]; |
| 253 | std::string source = sources[arg2 & 7u]; |
| 254 | uint operation = arg2 >> 3u; |
| 255 | if (source.empty() || dest.empty() || operation == 3) { |
| 256 | invalid = true; |
| 257 | } |
| 258 | if (dest == source && (arg1 == 1 || arg2 == 2)) { |
| 259 | op("nop"); |
| 260 | op_guts(""); |
| 261 | } else { |
| 262 | op("mov"); |
| 263 | std::string guts = dest + ", "; |
| 264 | if (operation == 1) { |
| 265 | guts += "invert("; |
| 266 | } else if (operation == 2) { |
| 267 | guts += "reverse("; |
| 268 | } |
| 269 | guts += source; |
| 270 | if (operation == 1 || operation == 2) { |
| 271 | guts += ")"; |
| 272 | } |
| 273 | op_guts(guts); |
| 274 | } |
| 275 | break; |
| 276 | } |
| 277 | case 0b110: { |
| 278 | if ((arg1 & 0x4u) || (arg2 & 0x8u)) { |
| 279 | invalid = true; |
| 280 | } else { |
| 281 | op("irq"); |
| 282 | std::string guts; |
| 283 | if (arg1 & 0x2u) { |
| 284 | guts += "clear, "; |
| 285 | } else if (arg1 & 0x1u) { |
Ravago Jones | d208ae7 | 2023-02-13 02:24:07 -0800 | [diff] [blame^] | 286 | guts += "block, "; |
Austin Schuh | 208337d | 2022-01-01 14:29:11 -0800 | [diff] [blame] | 287 | } |
| 288 | auto irq = std::to_string(arg2 & 7u); |
| 289 | if (arg2 & 0x10u) { |
| 290 | guts += "rel(" + irq + ")"; |
| 291 | } else { |
| 292 | guts += irq; |
| 293 | } |
| 294 | op_guts(guts); |
| 295 | } |
| 296 | break; |
| 297 | } |
| 298 | case 0b111: { |
| 299 | static std::array<std::string, 8> dests{"pins", "x", "y", "", "pindirs", "", "", ""}; |
| 300 | std::string dest = dests[arg1]; |
| 301 | if (dest.empty()) { |
| 302 | invalid = true; |
| 303 | } else { |
| 304 | op("set"); |
| 305 | op_guts(dests[arg1] + ", " + std::to_string(arg2)); |
| 306 | } |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | if (invalid) { |
| 311 | op("word"); |
| 312 | ss << std::hex; |
| 313 | op_guts(std::to_string(inst)); |
| 314 | } |
| 315 | uint delay = ((uint) inst >> 8u) & 0x1f; |
| 316 | ss << std::left << std::setw(9); |
| 317 | if (sideset_bits_including_opt && (!sideset_opt || (delay & 0x10u))) { |
| 318 | ss << (".side("+ std::to_string((delay & (sideset_opt ? 0xfu : 0x1fu)) >> (5u - sideset_bits_including_opt))+")"); |
| 319 | } else { |
| 320 | ss << ""; |
| 321 | } |
| 322 | delay &= ((1u << (5 - sideset_bits_including_opt)) - 1u); |
| 323 | ss << std::left << std::setw(4) << (delay ? ("[" + std::to_string(delay) + "]") : ""); |
| 324 | return ss.str(); |
| 325 | } |
| 326 | }; |
| 327 | |
| 328 | static python_output::factory creator; |