blob: 8598f33452c8275e15a9d337f750621c37c0b9b9 [file] [log] [blame]
Austin Schuh208337d2022-01-01 14:29:11 -08001/*
2 * Copyright (c) 2021 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 * Ada specifications generated by this assembler depend on the RP.PIO package,
7 * available in rp2040_hal.
8 *
9 * https://github.com/JeremyGrosser/rp2040_hal
10 * https://github.com/JeremyGrosser/pico_bsp
11 * https://github.com/JeremyGrosser/pico_examples
12 */
13
14#include <algorithm>
15#include <iostream>
16#include "output_format.h"
17#include "pio_disassembler.h"
18
19struct ada_output : public output_format {
20 struct factory {
21 factory() {
22 output_format::add(new ada_output());
23 }
24 };
25
26 ada_output() : output_format("ada") {}
27
28 std::string get_description() override {
29 return "Ada specification";
30 }
31
32 void output_symbols(FILE *out, const std::vector<compiled_source::symbol> &symbols) {
33 int count = 0;
34 for (const auto &s : symbols) {
35 if (!s.is_label) {
36 fprintf(out, "%s : constant := %d;\n", s.name.c_str(), s.value);
37 count++;
38 }
39 }
40 if (count) {
41 fprintf(out, "\n");
42 count = 0;
43 }
44 for (const auto &s : symbols) {
45 if (s.is_label) {
46 fprintf(out, " Offset_%s : constant := %d;\n", s.name.c_str(), s.value);
47 count++;
48 }
49 }
50 if (count) {
51 fprintf(out, "\n");
52 }
53 }
54
55 void ada_case(std::string &identifier) {
56 for(std::string::size_type i = 0; i < identifier.size(); ++i) {
57 if ((i == 0) || (identifier[i - 1] == '_')) {
58 identifier[i] = toupper(identifier[i]);
59 }
60 }
61 }
62
63 void header(FILE *out, const std::string msg, const int indent) {
64 const std::string dashes = std::string(msg.length() + 6, '-');
65 const std::string indent_str= std::string(indent, ' ');
66 fprintf(out, "%s%s\n", indent_str.c_str(), dashes.c_str());
67 fprintf(out, "%s-- %s --\n", indent_str.c_str(), msg.c_str());
68 fprintf(out, "%s%s\n", indent_str.c_str(), dashes.c_str());
69 fprintf(out, "\n");
70 }
71
72 int output(std::string destination, std::vector<std::string> output_options,
73 const compiled_source &source) override {
74
75 for (const auto &program : source.programs) {
76 for(const auto &p : program.lang_opts) {
77 if (p.first.size() >= name.size() && p.first.compare(0, name.size(), name) == 0) {
78 std::cerr << "warning: " << name << " does not support output options; " << p.first << " lang_opt ignored.\n";
79 }
80 }
81 }
82
83 std::string package_name;
84
85 switch (output_options.size()) {
86 case 0:
87 std::cerr << "error: missing package name options for Ada format" << std::endl;
88 return 1;
89 case 1:
90 package_name = output_options[0]; // Package name from command options
91 break;
92 default:
93 std::cerr << "error: too many options for Ada format" << std::endl;
94 return 1;
95 }
96
97 FILE *out = open_single_output(destination);
98 if (!out) return 1;
99
100 header(out, "This file is autogenerated by pioasm; do not edit!", 0);
101 fprintf(out, "pragma Style_Checks (Off);\n\n");
102 fprintf(out, "with RP.PIO;\n\n");
103
104 fprintf(out, "package %s is\n", package_name.c_str());
105
106 for (const auto &program : source.programs) {
107 std::string trailing_comma = ", ";
108
109 std::string prog_name= program.name;
110 ada_case(prog_name);
111
112 fprintf(out, "\n");
113 header(out, prog_name, 3);
114
115
116 output_symbols(out, source.global_symbols);
117 fprintf(out, " %s_Wrap_Target : constant := %d;\n", prog_name.c_str(), program.wrap_target);
118 fprintf(out, " %s_Wrap : constant := %d;\n", prog_name.c_str(), program.wrap);
119 fprintf(out, "\n");
120
121 output_symbols(out, program.symbols);
122
123 fprintf(out, " %s_Program_Instructions : RP.PIO.Program := (\n", prog_name.c_str());
124 for (int i = 0; i < (int)program.instructions.size(); i++) {
125 const auto &inst = program.instructions[i];
126 if (i == program.wrap_target) {
127 fprintf(out, " -- .wrap_target\n");
128 }
129 if (i == (int)program.instructions.size() - 1) {
130 trailing_comma = ");";
131 }
132 fprintf(out, " 16#%04x#%s -- %2d: %s\n", inst, trailing_comma.c_str(), i,
133 disassemble(inst, program.sideset_bits_including_opt.get(), program.sideset_opt).c_str());
134 if (i == program.wrap) {
135 fprintf(out, " -- .wrap\n");
136 }
137 }
138 }
139 fprintf(out, "\n");
140 fprintf(out, "end %s;\n", package_name.c_str());
141 fclose(out);
142 return 0;
143 }
144};
145
146static ada_output::factory creator;