1848b8605Smrg/*
2848b8605Smrg * Copyright (c) 2012 Rob Clark <robdclark@gmail.com>
3848b8605Smrg *
4848b8605Smrg * Permission is hereby granted, free of charge, to any person obtaining a
5848b8605Smrg * copy of this software and associated documentation files (the "Software"),
6848b8605Smrg * to deal in the Software without restriction, including without limitation
7848b8605Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8848b8605Smrg * and/or sell copies of the Software, and to permit persons to whom the
9848b8605Smrg * Software is furnished to do so, subject to the following conditions:
10848b8605Smrg *
11848b8605Smrg * The above copyright notice and this permission notice (including the next
12848b8605Smrg * paragraph) shall be included in all copies or substantial portions of the
13848b8605Smrg * Software.
14848b8605Smrg *
15848b8605Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16848b8605Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17848b8605Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18848b8605Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19848b8605Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20848b8605Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21848b8605Smrg * SOFTWARE.
22848b8605Smrg */
23848b8605Smrg
24848b8605Smrg#include <stdio.h>
25848b8605Smrg#include <stdlib.h>
26848b8605Smrg#include <stdint.h>
27848b8605Smrg#include <unistd.h>
28848b8605Smrg#include <sys/types.h>
29848b8605Smrg#include <sys/stat.h>
30848b8605Smrg#include <fcntl.h>
31848b8605Smrg#include <string.h>
32848b8605Smrg
33848b8605Smrg#include "disasm.h"
34848b8605Smrg#include "instr-a2xx.h"
35848b8605Smrg
36848b8605Smrgstatic const char *levels[] = {
37848b8605Smrg		"\t",
38848b8605Smrg		"\t\t",
39848b8605Smrg		"\t\t\t",
40848b8605Smrg		"\t\t\t\t",
41848b8605Smrg		"\t\t\t\t\t",
42848b8605Smrg		"\t\t\t\t\t\t",
43848b8605Smrg		"\t\t\t\t\t\t\t",
44848b8605Smrg		"\t\t\t\t\t\t\t\t",
45848b8605Smrg		"\t\t\t\t\t\t\t\t\t",
46848b8605Smrg		"x",
47848b8605Smrg		"x",
48848b8605Smrg		"x",
49848b8605Smrg		"x",
50848b8605Smrg		"x",
51848b8605Smrg		"x",
52848b8605Smrg};
53848b8605Smrg
54848b8605Smrgstatic enum debug_t debug;
55848b8605Smrg
56848b8605Smrg/*
57848b8605Smrg * ALU instructions:
58848b8605Smrg */
59848b8605Smrg
60848b8605Smrgstatic const char chan_names[] = {
61848b8605Smrg		'x', 'y', 'z', 'w',
62848b8605Smrg		/* these only apply to FETCH dst's: */
63848b8605Smrg		'0', '1', '?', '_',
64848b8605Smrg};
65848b8605Smrg
66848b8605Smrgstatic void print_srcreg(uint32_t num, uint32_t type,
67848b8605Smrg		uint32_t swiz, uint32_t negate, uint32_t abs)
68848b8605Smrg{
69848b8605Smrg	if (negate)
70848b8605Smrg		printf("-");
71848b8605Smrg	if (abs)
72848b8605Smrg		printf("|");
73848b8605Smrg	printf("%c%u", type ? 'R' : 'C', num);
74848b8605Smrg	if (swiz) {
75848b8605Smrg		int i;
76848b8605Smrg		printf(".");
77848b8605Smrg		for (i = 0; i < 4; i++) {
78848b8605Smrg			printf("%c", chan_names[(swiz + i) & 0x3]);
79848b8605Smrg			swiz >>= 2;
80848b8605Smrg		}
81848b8605Smrg	}
82848b8605Smrg	if (abs)
83848b8605Smrg		printf("|");
84848b8605Smrg}
85848b8605Smrg
86848b8605Smrgstatic void print_dstreg(uint32_t num, uint32_t mask, uint32_t dst_exp)
87848b8605Smrg{
88848b8605Smrg	printf("%s%u", dst_exp ? "export" : "R", num);
89848b8605Smrg	if (mask != 0xf) {
90848b8605Smrg		int i;
91848b8605Smrg		printf(".");
92848b8605Smrg		for (i = 0; i < 4; i++) {
93848b8605Smrg			printf("%c", (mask & 0x1) ? chan_names[i] : '_');
94848b8605Smrg			mask >>= 1;
95848b8605Smrg		}
96848b8605Smrg	}
97848b8605Smrg}
98848b8605Smrg
99b8e80941Smrgstatic void print_export_comment(uint32_t num, gl_shader_stage type)
100848b8605Smrg{
101848b8605Smrg	const char *name = NULL;
102848b8605Smrg	switch (type) {
103b8e80941Smrg	case MESA_SHADER_VERTEX:
104848b8605Smrg		switch (num) {
105848b8605Smrg		case 62: name = "gl_Position";  break;
106848b8605Smrg		case 63: name = "gl_PointSize"; break;
107848b8605Smrg		}
108848b8605Smrg		break;
109b8e80941Smrg	case MESA_SHADER_FRAGMENT:
110848b8605Smrg		switch (num) {
111848b8605Smrg		case 0:  name = "gl_FragColor"; break;
112848b8605Smrg		}
113848b8605Smrg		break;
114b8e80941Smrg	default:
115b8e80941Smrg		unreachable("not reached");
116848b8605Smrg	}
117848b8605Smrg	/* if we had a symbol table here, we could look
118848b8605Smrg	 * up the name of the varying..
119848b8605Smrg	 */
120848b8605Smrg	if (name) {
121848b8605Smrg		printf("\t; %s", name);
122848b8605Smrg	}
123848b8605Smrg}
124848b8605Smrg
125848b8605Smrgstruct {
126848b8605Smrg	uint32_t num_srcs;
127848b8605Smrg	const char *name;
128848b8605Smrg} vector_instructions[0x20] = {
129848b8605Smrg#define INSTR(opc, num_srcs) [opc] = { num_srcs, #opc }
130848b8605Smrg		INSTR(ADDv, 2),
131848b8605Smrg		INSTR(MULv, 2),
132848b8605Smrg		INSTR(MAXv, 2),
133848b8605Smrg		INSTR(MINv, 2),
134848b8605Smrg		INSTR(SETEv, 2),
135848b8605Smrg		INSTR(SETGTv, 2),
136848b8605Smrg		INSTR(SETGTEv, 2),
137848b8605Smrg		INSTR(SETNEv, 2),
138848b8605Smrg		INSTR(FRACv, 1),
139848b8605Smrg		INSTR(TRUNCv, 1),
140848b8605Smrg		INSTR(FLOORv, 1),
141848b8605Smrg		INSTR(MULADDv, 3),
142848b8605Smrg		INSTR(CNDEv, 3),
143848b8605Smrg		INSTR(CNDGTEv, 3),
144848b8605Smrg		INSTR(CNDGTv, 3),
145848b8605Smrg		INSTR(DOT4v, 2),
146848b8605Smrg		INSTR(DOT3v, 2),
147848b8605Smrg		INSTR(DOT2ADDv, 3),  // ???
148848b8605Smrg		INSTR(CUBEv, 2),
149848b8605Smrg		INSTR(MAX4v, 1),
150848b8605Smrg		INSTR(PRED_SETE_PUSHv, 2),
151848b8605Smrg		INSTR(PRED_SETNE_PUSHv, 2),
152848b8605Smrg		INSTR(PRED_SETGT_PUSHv, 2),
153848b8605Smrg		INSTR(PRED_SETGTE_PUSHv, 2),
154848b8605Smrg		INSTR(KILLEv, 2),
155848b8605Smrg		INSTR(KILLGTv, 2),
156848b8605Smrg		INSTR(KILLGTEv, 2),
157848b8605Smrg		INSTR(KILLNEv, 2),
158848b8605Smrg		INSTR(DSTv, 2),
159848b8605Smrg		INSTR(MOVAv, 1),
160848b8605Smrg}, scalar_instructions[0x40] = {
161848b8605Smrg		INSTR(ADDs, 1),
162848b8605Smrg		INSTR(ADD_PREVs, 1),
163848b8605Smrg		INSTR(MULs, 1),
164848b8605Smrg		INSTR(MUL_PREVs, 1),
165848b8605Smrg		INSTR(MUL_PREV2s, 1),
166848b8605Smrg		INSTR(MAXs, 1),
167848b8605Smrg		INSTR(MINs, 1),
168848b8605Smrg		INSTR(SETEs, 1),
169848b8605Smrg		INSTR(SETGTs, 1),
170848b8605Smrg		INSTR(SETGTEs, 1),
171848b8605Smrg		INSTR(SETNEs, 1),
172848b8605Smrg		INSTR(FRACs, 1),
173848b8605Smrg		INSTR(TRUNCs, 1),
174848b8605Smrg		INSTR(FLOORs, 1),
175848b8605Smrg		INSTR(EXP_IEEE, 1),
176848b8605Smrg		INSTR(LOG_CLAMP, 1),
177848b8605Smrg		INSTR(LOG_IEEE, 1),
178848b8605Smrg		INSTR(RECIP_CLAMP, 1),
179848b8605Smrg		INSTR(RECIP_FF, 1),
180848b8605Smrg		INSTR(RECIP_IEEE, 1),
181848b8605Smrg		INSTR(RECIPSQ_CLAMP, 1),
182848b8605Smrg		INSTR(RECIPSQ_FF, 1),
183848b8605Smrg		INSTR(RECIPSQ_IEEE, 1),
184848b8605Smrg		INSTR(MOVAs, 1),
185848b8605Smrg		INSTR(MOVA_FLOORs, 1),
186848b8605Smrg		INSTR(SUBs, 1),
187848b8605Smrg		INSTR(SUB_PREVs, 1),
188848b8605Smrg		INSTR(PRED_SETEs, 1),
189848b8605Smrg		INSTR(PRED_SETNEs, 1),
190848b8605Smrg		INSTR(PRED_SETGTs, 1),
191848b8605Smrg		INSTR(PRED_SETGTEs, 1),
192848b8605Smrg		INSTR(PRED_SET_INVs, 1),
193848b8605Smrg		INSTR(PRED_SET_POPs, 1),
194848b8605Smrg		INSTR(PRED_SET_CLRs, 1),
195848b8605Smrg		INSTR(PRED_SET_RESTOREs, 1),
196848b8605Smrg		INSTR(KILLEs, 1),
197848b8605Smrg		INSTR(KILLGTs, 1),
198848b8605Smrg		INSTR(KILLGTEs, 1),
199848b8605Smrg		INSTR(KILLNEs, 1),
200848b8605Smrg		INSTR(KILLONEs, 1),
201848b8605Smrg		INSTR(SQRT_IEEE, 1),
202848b8605Smrg		INSTR(MUL_CONST_0, 1),
203848b8605Smrg		INSTR(MUL_CONST_1, 1),
204848b8605Smrg		INSTR(ADD_CONST_0, 1),
205848b8605Smrg		INSTR(ADD_CONST_1, 1),
206848b8605Smrg		INSTR(SUB_CONST_0, 1),
207848b8605Smrg		INSTR(SUB_CONST_1, 1),
208848b8605Smrg		INSTR(SIN, 1),
209848b8605Smrg		INSTR(COS, 1),
210848b8605Smrg		INSTR(RETAIN_PREV, 1),
211848b8605Smrg#undef INSTR
212848b8605Smrg};
213848b8605Smrg
214848b8605Smrgstatic int disasm_alu(uint32_t *dwords, uint32_t alu_off,
215b8e80941Smrg		int level, int sync, gl_shader_stage type)
216848b8605Smrg{
217848b8605Smrg	instr_alu_t *alu = (instr_alu_t *)dwords;
218848b8605Smrg
219848b8605Smrg	printf("%s", levels[level]);
220848b8605Smrg	if (debug & PRINT_RAW) {
221848b8605Smrg		printf("%02x: %08x %08x %08x\t", alu_off,
222848b8605Smrg				dwords[0], dwords[1], dwords[2]);
223848b8605Smrg	}
224848b8605Smrg
225848b8605Smrg	printf("   %sALU:\t", sync ? "(S)" : "   ");
226848b8605Smrg
227848b8605Smrg	printf("%s", vector_instructions[alu->vector_opc].name);
228848b8605Smrg
229848b8605Smrg	if (alu->pred_select & 0x2) {
230848b8605Smrg		/* seems to work similar to conditional execution in ARM instruction
231848b8605Smrg		 * set, so let's use a similar syntax for now:
232848b8605Smrg		 */
233848b8605Smrg		printf((alu->pred_select & 0x1) ? "EQ" : "NE");
234848b8605Smrg	}
235848b8605Smrg
236848b8605Smrg	printf("\t");
237848b8605Smrg
238848b8605Smrg	print_dstreg(alu->vector_dest, alu->vector_write_mask, alu->export_data);
239848b8605Smrg	printf(" = ");
240848b8605Smrg	if (vector_instructions[alu->vector_opc].num_srcs == 3) {
241848b8605Smrg		print_srcreg(alu->src3_reg, alu->src3_sel, alu->src3_swiz,
242848b8605Smrg				alu->src3_reg_negate, alu->src3_reg_abs);
243848b8605Smrg		printf(", ");
244848b8605Smrg	}
245848b8605Smrg	print_srcreg(alu->src1_reg, alu->src1_sel, alu->src1_swiz,
246848b8605Smrg			alu->src1_reg_negate, alu->src1_reg_abs);
247848b8605Smrg	if (vector_instructions[alu->vector_opc].num_srcs > 1) {
248848b8605Smrg		printf(", ");
249848b8605Smrg		print_srcreg(alu->src2_reg, alu->src2_sel, alu->src2_swiz,
250848b8605Smrg				alu->src2_reg_negate, alu->src2_reg_abs);
251848b8605Smrg	}
252848b8605Smrg
253848b8605Smrg	if (alu->vector_clamp)
254848b8605Smrg		printf(" CLAMP");
255848b8605Smrg
256848b8605Smrg	if (alu->export_data)
257848b8605Smrg		print_export_comment(alu->vector_dest, type);
258848b8605Smrg
259848b8605Smrg	printf("\n");
260848b8605Smrg
261848b8605Smrg	if (alu->scalar_write_mask || !alu->vector_write_mask) {
262848b8605Smrg		/* 2nd optional scalar op: */
263848b8605Smrg
264848b8605Smrg		printf("%s", levels[level]);
265848b8605Smrg		if (debug & PRINT_RAW)
266848b8605Smrg			printf("                          \t");
267848b8605Smrg
268848b8605Smrg		if (scalar_instructions[alu->scalar_opc].name) {
269848b8605Smrg			printf("\t    \t%s\t", scalar_instructions[alu->scalar_opc].name);
270848b8605Smrg		} else {
271848b8605Smrg			printf("\t    \tOP(%u)\t", alu->scalar_opc);
272848b8605Smrg		}
273848b8605Smrg
274848b8605Smrg		print_dstreg(alu->scalar_dest, alu->scalar_write_mask, alu->export_data);
275848b8605Smrg		printf(" = ");
276848b8605Smrg		print_srcreg(alu->src3_reg, alu->src3_sel, alu->src3_swiz,
277848b8605Smrg				alu->src3_reg_negate, alu->src3_reg_abs);
278848b8605Smrg		// TODO ADD/MUL must have another src?!?
279848b8605Smrg		if (alu->scalar_clamp)
280848b8605Smrg			printf(" CLAMP");
281848b8605Smrg		if (alu->export_data)
282848b8605Smrg			print_export_comment(alu->scalar_dest, type);
283848b8605Smrg		printf("\n");
284848b8605Smrg	}
285848b8605Smrg
286848b8605Smrg	return 0;
287848b8605Smrg}
288848b8605Smrg
289848b8605Smrg
290848b8605Smrg/*
291848b8605Smrg * FETCH instructions:
292848b8605Smrg */
293848b8605Smrg
294848b8605Smrgstruct {
295848b8605Smrg	const char *name;
296848b8605Smrg} fetch_types[0xff] = {
297848b8605Smrg#define TYPE(id) [id] = { #id }
298848b8605Smrg		TYPE(FMT_1_REVERSE),
299848b8605Smrg		TYPE(FMT_32_FLOAT),
300848b8605Smrg		TYPE(FMT_32_32_FLOAT),
301848b8605Smrg		TYPE(FMT_32_32_32_FLOAT),
302848b8605Smrg		TYPE(FMT_32_32_32_32_FLOAT),
303848b8605Smrg		TYPE(FMT_16),
304848b8605Smrg		TYPE(FMT_16_16),
305848b8605Smrg		TYPE(FMT_16_16_16_16),
306848b8605Smrg		TYPE(FMT_8),
307848b8605Smrg		TYPE(FMT_8_8),
308848b8605Smrg		TYPE(FMT_8_8_8_8),
309848b8605Smrg		TYPE(FMT_32),
310848b8605Smrg		TYPE(FMT_32_32),
311848b8605Smrg		TYPE(FMT_32_32_32_32),
312848b8605Smrg#undef TYPE
313848b8605Smrg};
314848b8605Smrg
315848b8605Smrgstatic void print_fetch_dst(uint32_t dst_reg, uint32_t dst_swiz)
316848b8605Smrg{
317848b8605Smrg	int i;
318848b8605Smrg	printf("\tR%u.", dst_reg);
319848b8605Smrg	for (i = 0; i < 4; i++) {
320848b8605Smrg		printf("%c", chan_names[dst_swiz & 0x7]);
321848b8605Smrg		dst_swiz >>= 3;
322848b8605Smrg	}
323848b8605Smrg}
324848b8605Smrg
325848b8605Smrgstatic void print_fetch_vtx(instr_fetch_t *fetch)
326848b8605Smrg{
327848b8605Smrg	instr_fetch_vtx_t *vtx = &fetch->vtx;
328848b8605Smrg
329848b8605Smrg	if (vtx->pred_select) {
330848b8605Smrg		/* seems to work similar to conditional execution in ARM instruction
331848b8605Smrg		 * set, so let's use a similar syntax for now:
332848b8605Smrg		 */
333848b8605Smrg		printf(vtx->pred_condition ? "EQ" : "NE");
334848b8605Smrg	}
335848b8605Smrg
336848b8605Smrg	print_fetch_dst(vtx->dst_reg, vtx->dst_swiz);
337848b8605Smrg	printf(" = R%u.", vtx->src_reg);
338848b8605Smrg	printf("%c", chan_names[vtx->src_swiz & 0x3]);
339848b8605Smrg	if (fetch_types[vtx->format].name) {
340848b8605Smrg		printf(" %s", fetch_types[vtx->format].name);
341848b8605Smrg	} else  {
342848b8605Smrg		printf(" TYPE(0x%x)", vtx->format);
343848b8605Smrg	}
344848b8605Smrg	printf(" %s", vtx->format_comp_all ? "SIGNED" : "UNSIGNED");
345848b8605Smrg	if (!vtx->num_format_all)
346848b8605Smrg		printf(" NORMALIZED");
347848b8605Smrg	printf(" STRIDE(%u)", vtx->stride);
348848b8605Smrg	if (vtx->offset)
349848b8605Smrg		printf(" OFFSET(%u)", vtx->offset);
350848b8605Smrg	printf(" CONST(%u, %u)", vtx->const_index, vtx->const_index_sel);
351848b8605Smrg	if (0) {
352848b8605Smrg		// XXX
353848b8605Smrg		printf(" src_reg_am=%u", vtx->src_reg_am);
354848b8605Smrg		printf(" dst_reg_am=%u", vtx->dst_reg_am);
355848b8605Smrg		printf(" num_format_all=%u", vtx->num_format_all);
356848b8605Smrg		printf(" signed_rf_mode_all=%u", vtx->signed_rf_mode_all);
357848b8605Smrg		printf(" exp_adjust_all=%u", vtx->exp_adjust_all);
358848b8605Smrg	}
359848b8605Smrg}
360848b8605Smrg
361848b8605Smrgstatic void print_fetch_tex(instr_fetch_t *fetch)
362848b8605Smrg{
363848b8605Smrg	static const char *filter[] = {
364848b8605Smrg			[TEX_FILTER_POINT] = "POINT",
365848b8605Smrg			[TEX_FILTER_LINEAR] = "LINEAR",
366848b8605Smrg			[TEX_FILTER_BASEMAP] = "BASEMAP",
367848b8605Smrg	};
368848b8605Smrg	static const char *aniso_filter[] = {
369848b8605Smrg			[ANISO_FILTER_DISABLED] = "DISABLED",
370848b8605Smrg			[ANISO_FILTER_MAX_1_1] = "MAX_1_1",
371848b8605Smrg			[ANISO_FILTER_MAX_2_1] = "MAX_2_1",
372848b8605Smrg			[ANISO_FILTER_MAX_4_1] = "MAX_4_1",
373848b8605Smrg			[ANISO_FILTER_MAX_8_1] = "MAX_8_1",
374848b8605Smrg			[ANISO_FILTER_MAX_16_1] = "MAX_16_1",
375848b8605Smrg	};
376848b8605Smrg	static const char *arbitrary_filter[] = {
377848b8605Smrg			[ARBITRARY_FILTER_2X4_SYM] = "2x4_SYM",
378848b8605Smrg			[ARBITRARY_FILTER_2X4_ASYM] = "2x4_ASYM",
379848b8605Smrg			[ARBITRARY_FILTER_4X2_SYM] = "4x2_SYM",
380848b8605Smrg			[ARBITRARY_FILTER_4X2_ASYM] = "4x2_ASYM",
381848b8605Smrg			[ARBITRARY_FILTER_4X4_SYM] = "4x4_SYM",
382848b8605Smrg			[ARBITRARY_FILTER_4X4_ASYM] = "4x4_ASYM",
383848b8605Smrg	};
384848b8605Smrg	static const char *sample_loc[] = {
385848b8605Smrg			[SAMPLE_CENTROID] = "CENTROID",
386848b8605Smrg			[SAMPLE_CENTER] = "CENTER",
387848b8605Smrg	};
388848b8605Smrg	instr_fetch_tex_t *tex = &fetch->tex;
389848b8605Smrg	uint32_t src_swiz = tex->src_swiz;
390848b8605Smrg	int i;
391848b8605Smrg
392848b8605Smrg	if (tex->pred_select) {
393848b8605Smrg		/* seems to work similar to conditional execution in ARM instruction
394848b8605Smrg		 * set, so let's use a similar syntax for now:
395848b8605Smrg		 */
396848b8605Smrg		printf(tex->pred_condition ? "EQ" : "NE");
397848b8605Smrg	}
398848b8605Smrg
399848b8605Smrg	print_fetch_dst(tex->dst_reg, tex->dst_swiz);
400848b8605Smrg	printf(" = R%u.", tex->src_reg);
401848b8605Smrg	for (i = 0; i < 3; i++) {
402848b8605Smrg		printf("%c", chan_names[src_swiz & 0x3]);
403848b8605Smrg		src_swiz >>= 2;
404848b8605Smrg	}
405848b8605Smrg	printf(" CONST(%u)", tex->const_idx);
406848b8605Smrg	if (tex->fetch_valid_only)
407848b8605Smrg		printf(" VALID_ONLY");
408848b8605Smrg	if (tex->tx_coord_denorm)
409848b8605Smrg		printf(" DENORM");
410848b8605Smrg	if (tex->mag_filter != TEX_FILTER_USE_FETCH_CONST)
411848b8605Smrg		printf(" MAG(%s)", filter[tex->mag_filter]);
412848b8605Smrg	if (tex->min_filter != TEX_FILTER_USE_FETCH_CONST)
413848b8605Smrg		printf(" MIN(%s)", filter[tex->min_filter]);
414848b8605Smrg	if (tex->mip_filter != TEX_FILTER_USE_FETCH_CONST)
415848b8605Smrg		printf(" MIP(%s)", filter[tex->mip_filter]);
416848b8605Smrg	if (tex->aniso_filter != ANISO_FILTER_USE_FETCH_CONST)
417848b8605Smrg		printf(" ANISO(%s)", aniso_filter[tex->aniso_filter]);
418848b8605Smrg	if (tex->arbitrary_filter != ARBITRARY_FILTER_USE_FETCH_CONST)
419848b8605Smrg		printf(" ARBITRARY(%s)", arbitrary_filter[tex->arbitrary_filter]);
420848b8605Smrg	if (tex->vol_mag_filter != TEX_FILTER_USE_FETCH_CONST)
421848b8605Smrg		printf(" VOL_MAG(%s)", filter[tex->vol_mag_filter]);
422848b8605Smrg	if (tex->vol_min_filter != TEX_FILTER_USE_FETCH_CONST)
423848b8605Smrg		printf(" VOL_MIN(%s)", filter[tex->vol_min_filter]);
424848b8605Smrg	if (!tex->use_comp_lod) {
425848b8605Smrg		printf(" LOD(%u)", tex->use_comp_lod);
426848b8605Smrg		printf(" LOD_BIAS(%u)", tex->lod_bias);
427848b8605Smrg	}
428848b8605Smrg	if (tex->use_reg_gradients)
429848b8605Smrg		printf(" USE_REG_GRADIENTS");
430848b8605Smrg	printf(" LOCATION(%s)", sample_loc[tex->sample_location]);
431848b8605Smrg	if (tex->offset_x || tex->offset_y || tex->offset_z)
432848b8605Smrg		printf(" OFFSET(%u,%u,%u)", tex->offset_x, tex->offset_y, tex->offset_z);
433848b8605Smrg}
434848b8605Smrg
435848b8605Smrgstruct {
436848b8605Smrg	const char *name;
437848b8605Smrg	void (*fxn)(instr_fetch_t *cf);
438848b8605Smrg} fetch_instructions[] = {
439848b8605Smrg#define INSTR(opc, name, fxn) [opc] = { name, fxn }
440848b8605Smrg		INSTR(VTX_FETCH, "VERTEX", print_fetch_vtx),
441848b8605Smrg		INSTR(TEX_FETCH, "SAMPLE", print_fetch_tex),
442848b8605Smrg		INSTR(TEX_GET_BORDER_COLOR_FRAC, "?", print_fetch_tex),
443848b8605Smrg		INSTR(TEX_GET_COMP_TEX_LOD, "?", print_fetch_tex),
444848b8605Smrg		INSTR(TEX_GET_GRADIENTS, "?", print_fetch_tex),
445848b8605Smrg		INSTR(TEX_GET_WEIGHTS, "?", print_fetch_tex),
446848b8605Smrg		INSTR(TEX_SET_TEX_LOD, "SET_TEX_LOD", print_fetch_tex),
447848b8605Smrg		INSTR(TEX_SET_GRADIENTS_H, "?", print_fetch_tex),
448848b8605Smrg		INSTR(TEX_SET_GRADIENTS_V, "?", print_fetch_tex),
449848b8605Smrg		INSTR(TEX_RESERVED_4, "?", print_fetch_tex),
450848b8605Smrg#undef INSTR
451848b8605Smrg};
452848b8605Smrg
453848b8605Smrgstatic int disasm_fetch(uint32_t *dwords, uint32_t alu_off, int level, int sync)
454848b8605Smrg{
455848b8605Smrg	instr_fetch_t *fetch = (instr_fetch_t *)dwords;
456848b8605Smrg
457848b8605Smrg	printf("%s", levels[level]);
458848b8605Smrg	if (debug & PRINT_RAW) {
459848b8605Smrg		printf("%02x: %08x %08x %08x\t", alu_off,
460848b8605Smrg				dwords[0], dwords[1], dwords[2]);
461848b8605Smrg	}
462848b8605Smrg
463848b8605Smrg	printf("   %sFETCH:\t", sync ? "(S)" : "   ");
464848b8605Smrg	printf("%s", fetch_instructions[fetch->opc].name);
465848b8605Smrg	fetch_instructions[fetch->opc].fxn(fetch);
466848b8605Smrg	printf("\n");
467848b8605Smrg
468848b8605Smrg	return 0;
469848b8605Smrg}
470848b8605Smrg
471848b8605Smrg/*
472848b8605Smrg * CF instructions:
473848b8605Smrg */
474848b8605Smrg
475848b8605Smrgstatic int cf_exec(instr_cf_t *cf)
476848b8605Smrg{
477848b8605Smrg	return (cf->opc == EXEC) ||
478848b8605Smrg			(cf->opc == EXEC_END) ||
479848b8605Smrg			(cf->opc == COND_EXEC) ||
480848b8605Smrg			(cf->opc == COND_EXEC_END) ||
481848b8605Smrg			(cf->opc == COND_PRED_EXEC) ||
482848b8605Smrg			(cf->opc == COND_PRED_EXEC_END) ||
483848b8605Smrg			(cf->opc == COND_EXEC_PRED_CLEAN) ||
484848b8605Smrg			(cf->opc == COND_EXEC_PRED_CLEAN_END);
485848b8605Smrg}
486848b8605Smrg
487848b8605Smrgstatic int cf_cond_exec(instr_cf_t *cf)
488848b8605Smrg{
489848b8605Smrg	return (cf->opc == COND_EXEC) ||
490848b8605Smrg			(cf->opc == COND_EXEC_END) ||
491848b8605Smrg			(cf->opc == COND_PRED_EXEC) ||
492848b8605Smrg			(cf->opc == COND_PRED_EXEC_END) ||
493848b8605Smrg			(cf->opc == COND_EXEC_PRED_CLEAN) ||
494848b8605Smrg			(cf->opc == COND_EXEC_PRED_CLEAN_END);
495848b8605Smrg}
496848b8605Smrg
497848b8605Smrgstatic void print_cf_nop(instr_cf_t *cf)
498848b8605Smrg{
499848b8605Smrg}
500848b8605Smrg
501848b8605Smrgstatic void print_cf_exec(instr_cf_t *cf)
502848b8605Smrg{
503848b8605Smrg	printf(" ADDR(0x%x) CNT(0x%x)", cf->exec.address, cf->exec.count);
504848b8605Smrg	if (cf->exec.yeild)
505848b8605Smrg		printf(" YIELD");
506848b8605Smrg	if (cf->exec.vc)
507848b8605Smrg		printf(" VC(0x%x)", cf->exec.vc);
508848b8605Smrg	if (cf->exec.bool_addr)
509848b8605Smrg		printf(" BOOL_ADDR(0x%x)", cf->exec.bool_addr);
510848b8605Smrg	if (cf->exec.address_mode == ABSOLUTE_ADDR)
511848b8605Smrg		printf(" ABSOLUTE_ADDR");
512848b8605Smrg	if (cf_cond_exec(cf))
513848b8605Smrg		printf(" COND(%d)", cf->exec.condition);
514848b8605Smrg}
515848b8605Smrg
516848b8605Smrgstatic void print_cf_loop(instr_cf_t *cf)
517848b8605Smrg{
518848b8605Smrg	printf(" ADDR(0x%x) LOOP_ID(%d)", cf->loop.address, cf->loop.loop_id);
519848b8605Smrg	if (cf->loop.address_mode == ABSOLUTE_ADDR)
520848b8605Smrg		printf(" ABSOLUTE_ADDR");
521848b8605Smrg}
522848b8605Smrg
523848b8605Smrgstatic void print_cf_jmp_call(instr_cf_t *cf)
524848b8605Smrg{
525848b8605Smrg	printf(" ADDR(0x%x) DIR(%d)", cf->jmp_call.address, cf->jmp_call.direction);
526848b8605Smrg	if (cf->jmp_call.force_call)
527848b8605Smrg		printf(" FORCE_CALL");
528848b8605Smrg	if (cf->jmp_call.predicated_jmp)
529848b8605Smrg		printf(" COND(%d)", cf->jmp_call.condition);
530848b8605Smrg	if (cf->jmp_call.bool_addr)
531848b8605Smrg		printf(" BOOL_ADDR(0x%x)", cf->jmp_call.bool_addr);
532848b8605Smrg	if (cf->jmp_call.address_mode == ABSOLUTE_ADDR)
533848b8605Smrg		printf(" ABSOLUTE_ADDR");
534848b8605Smrg}
535848b8605Smrg
536848b8605Smrgstatic void print_cf_alloc(instr_cf_t *cf)
537848b8605Smrg{
538848b8605Smrg	static const char *bufname[] = {
539848b8605Smrg			[SQ_NO_ALLOC] = "NO ALLOC",
540848b8605Smrg			[SQ_POSITION] = "POSITION",
541848b8605Smrg			[SQ_PARAMETER_PIXEL] = "PARAM/PIXEL",
542848b8605Smrg			[SQ_MEMORY] = "MEMORY",
543848b8605Smrg	};
544848b8605Smrg	printf(" %s SIZE(0x%x)", bufname[cf->alloc.buffer_select], cf->alloc.size);
545848b8605Smrg	if (cf->alloc.no_serial)
546848b8605Smrg		printf(" NO_SERIAL");
547848b8605Smrg	if (cf->alloc.alloc_mode) // ???
548848b8605Smrg		printf(" ALLOC_MODE");
549848b8605Smrg}
550848b8605Smrg
551848b8605Smrgstruct {
552848b8605Smrg	const char *name;
553848b8605Smrg	void (*fxn)(instr_cf_t *cf);
554848b8605Smrg} cf_instructions[] = {
555848b8605Smrg#define INSTR(opc, fxn) [opc] = { #opc, fxn }
556848b8605Smrg		INSTR(NOP, print_cf_nop),
557848b8605Smrg		INSTR(EXEC, print_cf_exec),
558848b8605Smrg		INSTR(EXEC_END, print_cf_exec),
559848b8605Smrg		INSTR(COND_EXEC, print_cf_exec),
560848b8605Smrg		INSTR(COND_EXEC_END, print_cf_exec),
561848b8605Smrg		INSTR(COND_PRED_EXEC, print_cf_exec),
562848b8605Smrg		INSTR(COND_PRED_EXEC_END, print_cf_exec),
563848b8605Smrg		INSTR(LOOP_START, print_cf_loop),
564848b8605Smrg		INSTR(LOOP_END, print_cf_loop),
565848b8605Smrg		INSTR(COND_CALL, print_cf_jmp_call),
566848b8605Smrg		INSTR(RETURN, print_cf_jmp_call),
567848b8605Smrg		INSTR(COND_JMP, print_cf_jmp_call),
568848b8605Smrg		INSTR(ALLOC, print_cf_alloc),
569848b8605Smrg		INSTR(COND_EXEC_PRED_CLEAN, print_cf_exec),
570848b8605Smrg		INSTR(COND_EXEC_PRED_CLEAN_END, print_cf_exec),
571848b8605Smrg		INSTR(MARK_VS_FETCH_DONE, print_cf_nop),  // ??
572848b8605Smrg#undef INSTR
573848b8605Smrg};
574848b8605Smrg
575848b8605Smrgstatic void print_cf(instr_cf_t *cf, int level)
576848b8605Smrg{
577848b8605Smrg	printf("%s", levels[level]);
578848b8605Smrg	if (debug & PRINT_RAW) {
579848b8605Smrg		uint16_t *words = (uint16_t *)cf;
580848b8605Smrg		printf("    %04x %04x %04x            \t",
581848b8605Smrg				words[0], words[1], words[2]);
582848b8605Smrg	}
583848b8605Smrg	printf("%s", cf_instructions[cf->opc].name);
584848b8605Smrg	cf_instructions[cf->opc].fxn(cf);
585848b8605Smrg	printf("\n");
586848b8605Smrg}
587848b8605Smrg
588848b8605Smrg/*
589848b8605Smrg * The adreno shader microcode consists of two parts:
590848b8605Smrg *   1) A CF (control-flow) program, at the header of the compiled shader,
591848b8605Smrg *      which refers to ALU/FETCH instructions that follow it by address.
592848b8605Smrg *   2) ALU and FETCH instructions
593848b8605Smrg */
594848b8605Smrg
595b8e80941Smrgint disasm_a2xx(uint32_t *dwords, int sizedwords, int level, gl_shader_stage type)
596848b8605Smrg{
597848b8605Smrg	instr_cf_t *cfs = (instr_cf_t *)dwords;
598848b8605Smrg	int idx, max_idx;
599848b8605Smrg
600848b8605Smrg	for (idx = 0; ; idx++) {
601848b8605Smrg		instr_cf_t *cf = &cfs[idx];
602848b8605Smrg		if (cf_exec(cf)) {
603848b8605Smrg			max_idx = 2 * cf->exec.address;
604848b8605Smrg			break;
605848b8605Smrg		}
606848b8605Smrg	}
607848b8605Smrg
608848b8605Smrg	for (idx = 0; idx < max_idx; idx++) {
609848b8605Smrg		instr_cf_t *cf = &cfs[idx];
610848b8605Smrg
611848b8605Smrg		print_cf(cf, level);
612848b8605Smrg
613848b8605Smrg		if (cf_exec(cf)) {
614848b8605Smrg			uint32_t sequence = cf->exec.serialize;
615848b8605Smrg			uint32_t i;
616848b8605Smrg			for (i = 0; i < cf->exec.count; i++) {
617848b8605Smrg				uint32_t alu_off = (cf->exec.address + i);
618848b8605Smrg				if (sequence & 0x1) {
619848b8605Smrg					disasm_fetch(dwords + alu_off * 3, alu_off, level, sequence & 0x2);
620848b8605Smrg				} else {
621848b8605Smrg					disasm_alu(dwords + alu_off * 3, alu_off, level, sequence & 0x2, type);
622848b8605Smrg				}
623848b8605Smrg				sequence >>= 2;
624848b8605Smrg			}
625848b8605Smrg		}
626848b8605Smrg	}
627848b8605Smrg
628848b8605Smrg	return 0;
629848b8605Smrg}
630848b8605Smrg
631848b8605Smrgvoid disasm_set_debug(enum debug_t d)
632848b8605Smrg{
633848b8605Smrg	debug = d;
634848b8605Smrg}
635