c_sdk_output.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <algorithm>
  7. #include <iostream>
  8. #include "output_format.h"
  9. #include "pio_disassembler.h"
  10. struct c_sdk_output : public output_format {
  11. struct factory {
  12. factory() {
  13. output_format::add(new c_sdk_output());
  14. }
  15. };
  16. c_sdk_output() : output_format("c-sdk") {}
  17. std::string get_description() override {
  18. return "C header suitable for use with the Raspberry Pi Pico SDK";
  19. }
  20. void output_symbols(FILE *out, std::string prefix, const std::vector<compiled_source::symbol> &symbols) {
  21. int count = 0;
  22. for (const auto &s : symbols) {
  23. if (!s.is_label) {
  24. fprintf(out, "#define %s%s %d\n", prefix.c_str(), s.name.c_str(), s.value);
  25. count++;
  26. }
  27. }
  28. if (count) {
  29. fprintf(out, "\n");
  30. count = 0;
  31. }
  32. for (const auto &s : symbols) {
  33. if (s.is_label) {
  34. fprintf(out, "#define %soffset_%s %du\n", prefix.c_str(), s.name.c_str(), s.value);
  35. count++;
  36. }
  37. }
  38. if (count) {
  39. fprintf(out, "\n");
  40. }
  41. }
  42. void header(FILE *out, std::string msg) {
  43. std::string dashes = std::string(msg.length(), '-');
  44. fprintf(out, "// %s //\n", dashes.c_str());
  45. fprintf(out, "// %s //\n", msg.c_str());
  46. fprintf(out, "// %s //\n", dashes.c_str());
  47. fprintf(out, "\n");
  48. }
  49. int output(std::string destination, std::vector<std::string> output_options,
  50. const compiled_source &source) override {
  51. for (const auto &program : source.programs) {
  52. for(const auto &p : program.lang_opts) {
  53. if (p.first.size() >= name.size() && p.first.compare(0, name.size(), name) == 0) {
  54. std::cerr << "warning: " << name << " does not support output options; " << p.first << " lang_opt ignored.\n";
  55. }
  56. }
  57. }
  58. FILE *out = open_single_output(destination);
  59. if (!out) return 1;
  60. header(out, "This file is autogenerated by pioasm; do not edit!");
  61. fprintf(out, "#pragma once\n");
  62. fprintf(out, "\n");
  63. fprintf(out, "#if !PICO_NO_HARDWARE\n");
  64. fprintf(out, "#include \"hardware/pio.h\"\n");
  65. fprintf(out, "#endif\n");
  66. fprintf(out, "\n");
  67. output_symbols(out, "", source.global_symbols);
  68. for (const auto &program : source.programs) {
  69. header(out, program.name);
  70. std::string prefix = program.name + "_";
  71. fprintf(out, "#define %swrap_target %d\n", prefix.c_str(), program.wrap_target);
  72. fprintf(out, "#define %swrap %d\n", prefix.c_str(), program.wrap);
  73. fprintf(out, "\n");
  74. output_symbols(out, prefix, program.symbols);
  75. fprintf(out, "static const uint16_t %sprogram_instructions[] = {\n", prefix.c_str());
  76. for (int i = 0; i < (int)program.instructions.size(); i++) {
  77. const auto &inst = program.instructions[i];
  78. if (i == program.wrap_target) {
  79. fprintf(out, " // .wrap_target\n");
  80. }
  81. fprintf(out, " 0x%04x, // %2d: %s\n", inst, i,
  82. disassemble(inst, program.sideset_bits_including_opt.get(), program.sideset_opt).c_str());
  83. if (i == program.wrap) {
  84. fprintf(out, " // .wrap\n");
  85. }
  86. }
  87. fprintf(out, "};\n");
  88. fprintf(out, "\n");
  89. fprintf(out, "#if !PICO_NO_HARDWARE\n");
  90. fprintf(out, "static const struct pio_program %sprogram = {\n", prefix.c_str());
  91. fprintf(out, " .instructions = %sprogram_instructions,\n", prefix.c_str());
  92. fprintf(out, " .length = %d,\n", (int) program.instructions.size());
  93. fprintf(out, " .origin = %d,\n", program.origin.get());
  94. fprintf(out, "};\n");
  95. fprintf(out, "\n");
  96. fprintf(out, "static inline pio_sm_config %sprogram_get_default_config(uint offset) {\n", prefix.c_str());
  97. fprintf(out, " pio_sm_config c = pio_get_default_sm_config();\n");
  98. fprintf(out, " sm_config_set_wrap(&c, offset + %swrap_target, offset + %swrap);\n", prefix.c_str(),
  99. prefix.c_str());
  100. if (program.sideset_bits_including_opt.is_specified()) {
  101. fprintf(out, " sm_config_set_sideset(&c, %d, %s, %s);\n", program.sideset_bits_including_opt.get(),
  102. program.sideset_opt ? "true" : "false",
  103. program.sideset_pindirs ? "true" : "false");
  104. }
  105. fprintf(out, " return c;\n");
  106. fprintf(out, "}\n");
  107. // todo maybe have some code blocks inside or outside here?
  108. for(const auto& o : program.code_blocks) {
  109. fprintf(out, "\n");
  110. if (o.first == name) {
  111. for(const auto &contents : o.second) {
  112. fprintf(out, "%s", contents.c_str());
  113. fprintf(out, "\n");
  114. }
  115. }
  116. }
  117. fprintf(out, "#endif\n");
  118. fprintf(out, "\n");
  119. }
  120. if (out != stdout) { fclose(out); }
  121. return 0;
  122. }
  123. };
  124. static c_sdk_output::factory creator;