1 /*
2 * Copyright © 2022 Mary Guillemard
3 * SPDX-License-Identifier: MIT
4 */
5 #include "mme_fermi.h"
6
7 #include "mme_bitpack_helpers.h"
8
9 #define OP_TO_STR(OP) [MME_FERMI_OP_##OP] = #OP
10 static const char *op_to_str[] = {
11 OP_TO_STR(ALU_REG),
12 OP_TO_STR(ADD_IMM),
13 OP_TO_STR(MERGE),
14 OP_TO_STR(BFE_LSL_IMM),
15 OP_TO_STR(BFE_LSL_REG),
16 OP_TO_STR(STATE),
17 OP_TO_STR(UNK6),
18 OP_TO_STR(BRANCH),
19 };
20 #undef OP_TO_STR
21
22 const char *
mme_fermi_op_to_str(enum mme_fermi_op op)23 mme_fermi_op_to_str(enum mme_fermi_op op)
24 {
25 assert(op < ARRAY_SIZE(op_to_str));
26 return op_to_str[op];
27 }
28
29 #define ALU_OP_TO_STR(OP) [MME_FERMI_ALU_OP_##OP] = #OP
30 static const char *alu_op_to_str[] = {
31 ALU_OP_TO_STR(ADD),
32 ALU_OP_TO_STR(ADDC),
33 ALU_OP_TO_STR(SUB),
34 ALU_OP_TO_STR(SUBB),
35 ALU_OP_TO_STR(RESERVED4),
36 ALU_OP_TO_STR(RESERVED5),
37 ALU_OP_TO_STR(RESERVED6),
38 ALU_OP_TO_STR(RESERVED7),
39 ALU_OP_TO_STR(XOR),
40 ALU_OP_TO_STR(OR),
41 ALU_OP_TO_STR(AND),
42 ALU_OP_TO_STR(AND_NOT),
43 ALU_OP_TO_STR(NAND),
44 ALU_OP_TO_STR(RESERVED13),
45 ALU_OP_TO_STR(RESERVED14),
46 ALU_OP_TO_STR(RESERVED15),
47 ALU_OP_TO_STR(RESERVED16),
48 ALU_OP_TO_STR(RESERVED17),
49 ALU_OP_TO_STR(RESERVED18),
50 ALU_OP_TO_STR(RESERVED19),
51 ALU_OP_TO_STR(RESERVED20),
52 ALU_OP_TO_STR(RESERVED21),
53 ALU_OP_TO_STR(RESERVED22),
54 ALU_OP_TO_STR(RESERVED23),
55 ALU_OP_TO_STR(RESERVED24),
56 ALU_OP_TO_STR(RESERVED25),
57 ALU_OP_TO_STR(RESERVED26),
58 ALU_OP_TO_STR(RESERVED27),
59 ALU_OP_TO_STR(RESERVED28),
60 ALU_OP_TO_STR(RESERVED29),
61 ALU_OP_TO_STR(RESERVED30),
62 ALU_OP_TO_STR(RESERVED31),
63 };
64 #undef ALU_OP_TO_STR
65
66 const char *
mme_fermi_alu_op_to_str(enum mme_fermi_alu_op op)67 mme_fermi_alu_op_to_str(enum mme_fermi_alu_op op)
68 {
69 assert(op < ARRAY_SIZE(alu_op_to_str));
70 return alu_op_to_str[op];
71 }
72
73 #define ASSIGN_OP_TO_STR(OP) [MME_FERMI_ASSIGN_OP_##OP] = #OP
74 static const char *assign_op_to_str[] = {
75 ASSIGN_OP_TO_STR(LOAD),
76 ASSIGN_OP_TO_STR(MOVE),
77 ASSIGN_OP_TO_STR(MOVE_SET_MADDR),
78 ASSIGN_OP_TO_STR(LOAD_EMIT),
79 ASSIGN_OP_TO_STR(MOVE_EMIT),
80 ASSIGN_OP_TO_STR(LOAD_SET_MADDR),
81 ASSIGN_OP_TO_STR(MOVE_SET_MADDR_LOAD_EMIT),
82 ASSIGN_OP_TO_STR(MOVE_SET_MADDR_LOAD_EMIT_HIGH),
83 };
84 #undef ASSIGN_OP_TO_STR
85
86 const char *
mme_fermi_assign_op_to_str(enum mme_fermi_assign_op op)87 mme_fermi_assign_op_to_str(enum mme_fermi_assign_op op)
88 {
89 assert(op < ARRAY_SIZE(assign_op_to_str));
90 return assign_op_to_str[op];
91 }
92
mme_fermi_encode(uint32_t * out,uint32_t inst_count,const struct mme_fermi_inst * insts)93 void mme_fermi_encode(uint32_t *out, uint32_t inst_count,
94 const struct mme_fermi_inst *insts)
95 {
96 for (uint32_t i = 0; i < inst_count; i++) {
97 uint32_t *b = &out[i];
98 *b = 0;
99
100 pack_uint(b, 0, 3, insts[i].op);
101 pack_uint(b, 7, 7, insts[i].end_next);
102 pack_uint(b, 8, 10, insts[i].dst);
103
104 if (insts[i].op != MME_FERMI_OP_BRANCH) {
105 pack_uint(b, 4, 6, insts[i].assign_op);
106 }
107
108 if (insts[i].op == MME_FERMI_OP_ALU_REG) {
109 pack_uint(b, 11, 13, insts[i].src[0]);
110 pack_uint(b, 14, 16, insts[i].src[1]);
111 pack_uint(b, 17, 21, insts[i].alu_op);
112 } else if (insts[i].op == MME_FERMI_OP_ADD_IMM ||
113 insts[i].op == MME_FERMI_OP_STATE) {
114 pack_uint(b, 11, 13, insts[i].src[0]);
115 pack_sint(b, 14, 31, insts[i].imm);
116 } else if (insts[i].op == MME_FERMI_OP_MERGE ||
117 insts[i].op == MME_FERMI_OP_BFE_LSL_IMM ||
118 insts[i].op == MME_FERMI_OP_BFE_LSL_REG) {
119 pack_uint(b, 11, 13, insts[i].src[0]);
120 pack_uint(b, 14, 16, insts[i].src[1]);
121 pack_uint(b, 17, 21, insts[i].bitfield.src_bit);
122 pack_uint(b, 22, 26, insts[i].bitfield.size);
123 pack_uint(b, 27, 31, insts[i].bitfield.dst_bit);
124 } else if (insts[i].op == MME_FERMI_OP_BRANCH) {
125 pack_uint(b, 4, 4, insts[i].branch.not_zero);
126 pack_uint(b, 5, 5, insts[i].branch.no_delay);
127 pack_uint(b, 11, 13, insts[i].src[0]);
128 pack_sint(b, 14, 31, insts[i].imm);
129 }
130 }
131 }
132
mme_fermi_decode(struct mme_fermi_inst * insts,const uint32_t * in,uint32_t inst_count)133 void mme_fermi_decode(struct mme_fermi_inst *insts,
134 const uint32_t *in, uint32_t inst_count)
135 {
136 for (uint32_t i = 0; i < inst_count; i++) {
137 const uint32_t *b = &in[i];
138
139 insts[i].op = unpack_uint(b, 0, 3);
140 insts[i].end_next = unpack_uint(b, 7, 7);
141 insts[i].dst = unpack_uint(b, 8, 10);
142
143 if (insts[i].op != MME_FERMI_OP_BRANCH) {
144 insts[i].assign_op = unpack_uint(b, 4, 6);
145 }
146
147 if (insts[i].op == MME_FERMI_OP_ALU_REG) {
148 insts[i].src[0] = unpack_uint(b, 11, 13);
149 insts[i].src[1] = unpack_uint(b, 14, 16);
150 insts[i].alu_op = unpack_uint(b, 17, 21);
151 } else if (insts[i].op == MME_FERMI_OP_ADD_IMM ||
152 insts[i].op == MME_FERMI_OP_STATE) {
153 insts[i].src[0] = unpack_uint(b, 11, 13);
154 insts[i].imm = unpack_sint(b, 14, 31);
155 } else if (insts[i].op == MME_FERMI_OP_MERGE ||
156 insts[i].op == MME_FERMI_OP_BFE_LSL_IMM ||
157 insts[i].op == MME_FERMI_OP_BFE_LSL_REG) {
158 insts[i].src[0] = unpack_uint(b, 11, 13);
159 insts[i].src[1] = unpack_uint(b, 14, 16);
160 insts[i].bitfield.src_bit = unpack_uint(b, 17, 21);
161 insts[i].bitfield.size = unpack_uint(b, 22, 26);
162 insts[i].bitfield.dst_bit = unpack_uint(b, 27, 31);
163 } else if (insts[i].op == MME_FERMI_OP_BRANCH) {
164 insts[i].branch.not_zero = unpack_uint(b, 4, 4);
165 insts[i].branch.no_delay = unpack_uint(b, 5, 5);
166 insts[i].src[0] = unpack_uint(b, 11, 13);
167 insts[i].imm = unpack_sint(b, 14, 31);
168 }
169 }
170 }
171
172 static void
print_indent(FILE * fp,unsigned depth)173 print_indent(FILE *fp, unsigned depth)
174 {
175 for (unsigned i = 0; i < depth; i++)
176 fprintf(fp, " ");
177 }
178
179 static void
print_reg(FILE * fp,enum mme_fermi_reg reg)180 print_reg(FILE *fp, enum mme_fermi_reg reg)
181 {
182 if (reg == MME_FERMI_REG_ZERO) {
183 fprintf(fp, " $zero");
184 } else {
185 fprintf(fp, " $r%u", (unsigned)reg);
186 }
187 }
188
189 static void
print_imm(FILE * fp,const struct mme_fermi_inst * inst)190 print_imm(FILE *fp, const struct mme_fermi_inst *inst)
191 {
192 int32_t imm = util_mask_sign_extend(inst->imm, 18);
193
194 fprintf(fp, " %d /* 0x%04x */", (int)imm, (unsigned)imm);
195 }
196
197 void
mme_fermi_print_inst(FILE * fp,unsigned indent,const struct mme_fermi_inst * inst)198 mme_fermi_print_inst(FILE *fp, unsigned indent,
199 const struct mme_fermi_inst *inst)
200 {
201 print_indent(fp, indent);
202
203 switch (inst->op) {
204 case MME_FERMI_OP_ALU_REG:
205 fprintf(fp, "%s", mme_fermi_alu_op_to_str(inst->alu_op));
206 print_reg(fp, inst->src[0]);
207 print_reg(fp, inst->src[1]);
208
209 if (inst->alu_op == MME_FERMI_ALU_OP_ADDC) {
210 fprintf(fp, " $carry");
211 } else if (inst->alu_op == MME_FERMI_ALU_OP_SUBB) {
212 fprintf(fp, " $borrow");
213 }
214 break;
215 case MME_FERMI_OP_ADD_IMM:
216 case MME_FERMI_OP_STATE:
217 fprintf(fp, "%s", mme_fermi_op_to_str(inst->op));
218 print_reg(fp, inst->src[0]);
219 print_imm(fp, inst);
220 break;
221 case MME_FERMI_OP_MERGE:
222 fprintf(fp, "%s", mme_fermi_op_to_str(inst->op));
223 print_reg(fp, inst->src[0]);
224 print_reg(fp, inst->src[1]);
225 fprintf(fp, " (%u, %u, %u)", inst->bitfield.src_bit,
226 inst->bitfield.size,
227 inst->bitfield.dst_bit);
228 break;
229 case MME_FERMI_OP_BFE_LSL_IMM:
230 fprintf(fp, "%s", mme_fermi_op_to_str(inst->op));
231 print_reg(fp, inst->src[0]);
232 print_reg(fp, inst->src[1]);
233 fprintf(fp, " (%u, %u)", inst->bitfield.dst_bit,
234 inst->bitfield.size);
235 break;
236 case MME_FERMI_OP_BFE_LSL_REG:
237 fprintf(fp, "%s", mme_fermi_op_to_str(inst->op));
238 print_reg(fp, inst->src[0]);
239 print_reg(fp, inst->src[1]);
240 fprintf(fp, " (%u, %u)", inst->bitfield.src_bit,
241 inst->bitfield.size);
242 break;
243 case MME_FERMI_OP_BRANCH:
244 if (inst->branch.not_zero) {
245 fprintf(fp, "BNZ");
246 } else {
247 fprintf(fp, "BZ");
248 }
249 print_reg(fp, inst->src[0]);
250 print_imm(fp, inst);
251
252 if (inst->branch.no_delay) {
253 fprintf(fp, " NO_DELAY");
254 }
255
256 break;
257 default:
258 fprintf(fp, "%s", mme_fermi_op_to_str(inst->op));
259 break;
260 }
261
262 if (inst->op != MME_FERMI_OP_BRANCH) {
263 fprintf(fp, "\n");
264 print_indent(fp, indent);
265
266 fprintf(fp, "%s", mme_fermi_assign_op_to_str(inst->assign_op));
267 print_reg(fp, inst->dst);
268
269 if (inst->assign_op != MME_FERMI_ASSIGN_OP_LOAD) {
270 fprintf(fp, " $scratch");
271 }
272 }
273
274 if (inst->end_next) {
275 fprintf(fp, "\n");
276 print_indent(fp, indent);
277 fprintf(fp, "END_NEXT");
278 }
279
280 fprintf(fp, "\n");
281
282 }
283
284 void
mme_fermi_print(FILE * fp,const struct mme_fermi_inst * insts,uint32_t inst_count)285 mme_fermi_print(FILE *fp, const struct mme_fermi_inst *insts,
286 uint32_t inst_count)
287 {
288 for (uint32_t i = 0; i < inst_count; i++) {
289 fprintf(fp, "%u:\n", i);
290 mme_fermi_print_inst(fp, 1, &insts[i]);
291 }
292 }
293
294 void
mme_fermi_dump(FILE * fp,uint32_t * encoded,size_t encoded_size)295 mme_fermi_dump(FILE *fp, uint32_t *encoded, size_t encoded_size)
296 {
297 uint32_t inst_count = encoded_size / 4;
298 for (uint32_t i = 0; i < inst_count; i++) {
299 struct mme_fermi_inst inst;
300 mme_fermi_decode(&inst, &encoded[i], 1);
301 mme_fermi_print_inst(fp, 1, &inst);
302 }
303 }
304