|
| 1 | +/* |
| 2 | + This is part of libswirl |
| 3 | +*/ |
| 4 | +#include <license/bsd> |
| 5 | + |
| 6 | +#include <vector> |
| 7 | +#include <map> |
| 8 | +#include <string> |
| 9 | +#include <sstream> |
| 10 | +#include <fstream> |
| 11 | +#include "libswirl/hw/aica/dsp_backend.h" |
| 12 | + |
| 13 | +using namespace std; |
| 14 | + |
| 15 | +map<string, int> getDefaultInst() |
| 16 | +{ |
| 17 | + map<string, int> rv; |
| 18 | + |
| 19 | + rv["TRA"] = 0; |
| 20 | + rv["TWT"] = 0; |
| 21 | + rv["TWA"] = 0; |
| 22 | + rv["XSEL"] = 0; |
| 23 | + rv["YSEL"] = 0; |
| 24 | + rv["IRA"] = 0; |
| 25 | + rv["IWT"] = 0; |
| 26 | + rv["IWA"] = 0; |
| 27 | + rv["EWT"] = 0; |
| 28 | + rv["EWA"] = 0; |
| 29 | + rv["ADRL"] = 0; |
| 30 | + rv["FRCL"] = 0; |
| 31 | + rv["SHIFT"] = 0; |
| 32 | + rv["YRL"] = 0; |
| 33 | + rv["NEGB"] = 0; |
| 34 | + rv["ZERO"] = 0; |
| 35 | + rv["BSEL"] = 0; |
| 36 | + rv["NOFL"] = 0; |
| 37 | + rv["TABLE"] = 0; |
| 38 | + rv["MWT"] = 0; |
| 39 | + rv["MRD"] = 0; |
| 40 | + rv["MASA"] = 0; |
| 41 | + rv["ADREB"] = 0; |
| 42 | + rv["NXADR"] = 0; |
| 43 | + |
| 44 | + return rv; |
| 45 | +} |
| 46 | + |
| 47 | +map<string, int> decodeInst(u32 *Inst) |
| 48 | +{ |
| 49 | + _INST i = {0}; |
| 50 | + |
| 51 | + DSPBackend::DecodeInst(Inst, &i); |
| 52 | + |
| 53 | + map<string, int> rv; |
| 54 | + |
| 55 | + rv["TRA"] = i.TRA; |
| 56 | + rv["TWT"] = i.TWT; |
| 57 | + rv["TWA"] = i.TWA; |
| 58 | + rv["XSEL"] = i.XSEL; |
| 59 | + rv["YSEL"] = i.YSEL; |
| 60 | + rv["IRA"] = i.IRA; |
| 61 | + rv["IWT"] = i.IWT; |
| 62 | + rv["IWA"] = i.IWA; |
| 63 | + rv["EWT"] = i.EWT; |
| 64 | + rv["EWA"] = i.EWA; |
| 65 | + rv["ADRL"] = i.ADRL; |
| 66 | + rv["FRCL"] = i.FRCL; |
| 67 | + rv["SHIFT"] = i.SHIFT; |
| 68 | + rv["YRL"] = i.YRL; |
| 69 | + rv["NEGB"] = i.NEGB; |
| 70 | + rv["ZERO"] = i.ZERO; |
| 71 | + rv["BSEL"] = i.BSEL; |
| 72 | + rv["NOFL"] = i.NOFL; |
| 73 | + rv["TABLE"] = i.TABLE; |
| 74 | + rv["MWT"] = i.MWT; |
| 75 | + rv["MRD"] = i.MRD; |
| 76 | + rv["MASA"] = i.MASA; |
| 77 | + rv["ADREB"] = i.ADREB; |
| 78 | + rv["NXADR"] = i.NXADR; |
| 79 | + |
| 80 | + return rv; |
| 81 | +} |
| 82 | + |
| 83 | +void encodeInst(u32 *Inst, map<string, int> &desc) |
| 84 | +{ |
| 85 | + _INST i = {0}; |
| 86 | + |
| 87 | + i.TRA = desc["TRA"]; |
| 88 | + i.TWT = desc["TWT"]; |
| 89 | + i.TWA = desc["TWA"]; |
| 90 | + i.XSEL = desc["XSEL"]; |
| 91 | + i.YSEL = desc["YSEL"]; |
| 92 | + i.IRA = desc["IRA"]; |
| 93 | + i.IWT = desc["IWT"]; |
| 94 | + i.IWA = desc["IWA"]; |
| 95 | + i.EWT = desc["EWT"]; |
| 96 | + i.EWA = desc["EWA"]; |
| 97 | + i.ADRL = desc["ADRL"]; |
| 98 | + i.FRCL = desc["FRCL"]; |
| 99 | + i.SHIFT = desc["SHIFT"]; |
| 100 | + i.YRL = desc["YRL"]; |
| 101 | + i.NEGB = desc["NEGB"]; |
| 102 | + i.ZERO = desc["ZERO"]; |
| 103 | + i.BSEL = desc["BSEL"]; |
| 104 | + i.NOFL = desc["NOFL"]; |
| 105 | + i.TABLE = desc["TABLE"]; |
| 106 | + i.MWT = desc["MWT"]; |
| 107 | + i.MRD = desc["MRD"]; |
| 108 | + i.MASA = desc["MASA"]; |
| 109 | + i.ADREB = desc["ADREB"]; |
| 110 | + i.NXADR = desc["NXADR"]; |
| 111 | + |
| 112 | + DSPBackend::EncodeInst(Inst, &i); |
| 113 | +} |
| 114 | + |
| 115 | +string DisassembleDesc(map<string, int> &desc) |
| 116 | +{ |
| 117 | + stringstream rv; |
| 118 | + |
| 119 | + rv << "TRA:" << desc["TRA"] << " "; |
| 120 | + rv << "TWT:" << desc["TWT"] << " "; |
| 121 | + rv << "TWA:" << desc["TWA"] << " "; |
| 122 | + rv << "XSEL:" << desc["XSEL"] << " "; |
| 123 | + rv << "YSEL:" << desc["YSEL"] << " "; |
| 124 | + rv << "IRA:" << desc["IRA"] << " "; |
| 125 | + rv << "IWT:" << desc["IWT"] << " "; |
| 126 | + rv << "IWA:" << desc["IWA"] << " "; |
| 127 | + rv << "EWT:" << desc["EWT"] << " "; |
| 128 | + rv << "EWA:" << desc["EWA"] << " "; |
| 129 | + rv << "ADRL:" << desc["ADRL"] << " "; |
| 130 | + rv << "FRCL:" << desc["FRCL"] << " "; |
| 131 | + rv << "SHIFT:" << desc["SHIFT"] << " "; |
| 132 | + rv << "YRL:" << desc["YRL"] << " "; |
| 133 | + rv << "NEGB:" << desc["NEGB"] << " "; |
| 134 | + rv << "ZERO:" << desc["ZERO"] << " "; |
| 135 | + rv << "BSEL:" << desc["BSEL"] << " "; |
| 136 | + rv << "NOFL:" << desc["NOFL"] << " "; |
| 137 | + rv << "TABLE:" << desc["TABLE"] << " "; |
| 138 | + rv << "MWT:" << desc["MWT"] << " "; |
| 139 | + rv << "MRD:" << desc["MRD"] << " "; |
| 140 | + rv << "MASA:" << desc["MASA"] << " "; |
| 141 | + rv << "ADREB:" << desc["ADREB"] << " "; |
| 142 | + rv << "NXADR:" << desc["NXADR"]; |
| 143 | + |
| 144 | + return rv.str(); |
| 145 | +} |
| 146 | + |
| 147 | +map<string, int> AssembleDesc(string text) |
| 148 | +{ |
| 149 | + map<string, int> rv = getDefaultInst(); |
| 150 | + istringstream line(text); |
| 151 | + |
| 152 | + string word; |
| 153 | + while (line >> word) |
| 154 | + { |
| 155 | + istringstream wordstream(word); |
| 156 | + string decl, value; |
| 157 | + if (!getline(wordstream, decl, ':')) |
| 158 | + { |
| 159 | + printf("Invalid asm %s\n", text.c_str()); |
| 160 | + exit(-4); |
| 161 | + } |
| 162 | + if (!(wordstream >> value)) |
| 163 | + { |
| 164 | + printf("Invalid asm %s\n", text.c_str()); |
| 165 | + exit(-5); |
| 166 | + } |
| 167 | + |
| 168 | + rv[decl] = atoi(value.c_str()); |
| 169 | + } |
| 170 | + |
| 171 | + return rv; |
| 172 | +} |
| 173 | + |
| 174 | +int main(int argc, char **argv) |
| 175 | +{ |
| 176 | + |
| 177 | + if (argc != 2 && argc != 3) |
| 178 | + { |
| 179 | + printf("expected %s src_file [-d]\n", argv[0]); |
| 180 | + return -1; |
| 181 | + } |
| 182 | + |
| 183 | + uint32_t mpro[128 * 4] = {0}; |
| 184 | + |
| 185 | + if (argc == 2) |
| 186 | + { |
| 187 | + const string inputFile = argv[1]; |
| 188 | + |
| 189 | + ifstream infile(inputFile, std::ios_base::binary); |
| 190 | + |
| 191 | + std::string line; |
| 192 | + int op = 0; |
| 193 | + while (getline(infile, line)) |
| 194 | + { |
| 195 | + if (line.size() == 0 || line[0] == '#') |
| 196 | + continue; |
| 197 | + if (op == 128) |
| 198 | + { |
| 199 | + printf("more than 128 ops\n"); |
| 200 | + exit(-5); |
| 201 | + } |
| 202 | + |
| 203 | + auto desc = AssembleDesc(line); |
| 204 | + encodeInst(&mpro[op * 4], desc); |
| 205 | + op++; |
| 206 | + } |
| 207 | + |
| 208 | + freopen(NULL, "wb", stdout); |
| 209 | + fwrite(mpro, 4, 128 * 4, stdout); |
| 210 | + } |
| 211 | + else |
| 212 | + { |
| 213 | + FILE *f = fopen(argv[1], "rb"); |
| 214 | + if (!f) |
| 215 | + { |
| 216 | + printf("failed to open %s\n", argv[1]); |
| 217 | + return -2; |
| 218 | + } |
| 219 | + if (fread(mpro, 4, 128 * 4, f) != 128 * 4) |
| 220 | + { |
| 221 | + printf("file %s is not the right size\n", argv[1]); |
| 222 | + return -3; |
| 223 | + } |
| 224 | + fclose(f); |
| 225 | + |
| 226 | + for (int i = 0; i < 128; i++) |
| 227 | + { |
| 228 | + auto inst = decodeInst(&mpro[i * 4]); |
| 229 | + |
| 230 | + auto desc = decodeInst(&mpro[i * 4]); |
| 231 | + auto text = DisassembleDesc(desc); |
| 232 | + printf("#step %d\n", i); |
| 233 | + puts(text.c_str()); |
| 234 | + } |
| 235 | + } |
| 236 | +} |
0 commit comments