-
Notifications
You must be signed in to change notification settings - Fork 112
/
Copy pathrdpq_debug.c
1709 lines (1583 loc) · 84.8 KB
/
rdpq_debug.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file rdpq_debug.c
* @brief RDP Command queue: debugging helpers
* @ingroup rdpq
*/
#include "rdpq_debug.h"
#include "rdpq_debug_internal.h"
#ifdef N64
#include "rdpq.h"
#include "rspq.h"
#include "rdpq_rect.h"
#include "rdpq_mode.h"
#include "rdpq_internal.h"
#include "rdp.h"
#include "debug.h"
#include "interrupt.h"
#include "utils.h"
#include "rspq_constants.h"
#include "rdpq_constants.h"
#else
///@cond
#define debugf(msg, ...) fprintf(stderr, msg, ##__VA_ARGS__)
#define MIN(a,b) ((a)<(b)?(a):(b))
#define MAX(a,b) ((a)>(b)?(a):(b))
///@endcond
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <assert.h>
///@cond
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
///@endcond
/** @brief RDP Debug command: turn on/off logging */
#define RDPQ_CMD_DEBUG_SHOWLOG 0x00010000
/** @brief RDP Debug command: debug message */
#define RDPQ_CMD_DEBUG_MESSAGE 0x00020000
/** @brief Flags that configure the logging */
int __rdpq_debug_log_flags;
#ifndef RDPQ_DEBUG_DEBUG
/**
* @brief Internal debugging of rdpq_debug.
*
* Define to 1 to active internal debugging of the rdpq debug module.
* This is useful to trace bugs of rdpq itself, but it should not be
* necessary for standard debugging sessions of application code, so it
* is turned off by default.
*/
#define RDPQ_DEBUG_DEBUG 0
#endif
#if RDPQ_DEBUG_DEBUG
/** @brief Like debugf, but guarded by #RDPQ_DEBUG_DEBUG */
#define intdebugf(...) debugf(__VA_ARGS__)
#else
/** @brief Like debugf, but guarded by #RDPQ_DEBUG_DEBUG */
#define intdebugf(...) ({ })
#endif
/** @brief Extract bits from word */
#define BITS(v, b, e) ((unsigned int)((v) << (63-(e)) >> (63-(e)+(b))))
/** @brief Extract bit from word */
#define BIT(v, b) BITS(v, b, b)
/** @brief Extract bits from word as signed quantity */
#define SBITS(v, b, e) (int)BITS((int64_t)(v), b, e)
/** @brief Extract command ID from RDP command word */
#define CMD(v) BITS((v), 56, 61)
/** @brief Check if a command is a triangle */
#define CMD_IS_TRI(cmd) ((cmd) >= 0x8 && (cmd) <= 0xF)
/** @brief A buffer sent to RDP via DMA */
typedef struct {
uint64_t *start; ///< Start pointer
uint64_t *end; ///< End pointer
uint64_t *traced; ///< End pointer of already-traced commands
} rdp_buffer_t;
/** @brief Decoded SET_COMBINE command */
typedef struct {
///@cond
struct cc_cycle_s {
struct { uint8_t suba, subb, mul, add; } rgb;
struct { uint8_t suba, subb, mul, add; } alpha;
} cyc[2];
///@endcond
} colorcombiner_t;
/** @brief Decoded SET_OTHER_MODES command */
typedef struct {
///@cond
bool atomic;
uint8_t cycle_type;
struct { bool persp, detail, sharpen, lod; } tex;
struct { bool enable; uint8_t type; } tlut;
uint8_t sample_type;
uint8_t tf_mode;
bool chromakey;
struct { uint8_t rgb, alpha; } dither;
struct blender_s { uint8_t p, a, q, b; } blender[2];
bool blend, read, aa;
struct { uint8_t mode; bool color; } cvg;
struct { bool cvg; bool mul_cc; } blalpha;
struct { uint8_t mode; bool upd, cmp, prim; } z;
struct { bool enable, noise; } alphacmp;
struct { bool fog, freeze, bl2; } rdpqx; // rdpq extensions
///@endcond
} setothermodes_t;
/**
* @brief Current RDP state
*
* This structure represents a mirror of the internal state of the RDP.
* It is updated by the validator as commands flow through, and is then used
* to validate the consistency of next commands.
*/
static struct {
struct {
bool pipe; ///< True if the pipe is busy (SYNC_PIPE required)
bool tile[8]; ///< True if each tile is a busy (SYNC_TILE required)
uint8_t tmem[64]; ///< Bitarray: busy state for each 8-byte word of TMEM (SYNC_LOAD required)
} busy; ///< Busy entities (for SYNC commands)
struct {
bool sent_scissor : 1; ///< True if at least one SET_SCISSOR was sent since reset
bool sent_zprim : 1; ///< True if SET_PRIM_DEPTH was sent
bool mode_changed : 1; ///< True if there is a pending mode change to validate (SET_OTHER_MODES / SET_COMBINE)
bool rendertarget_changed : 1; ///< True if there is a pending render target change to validate (SET_COLOR_IMAGE / SET_SCISSOR)
};
uint64_t *last_som; ///< Pointer to last SOM command sent
uint64_t last_som_data; ///< Last SOM command (raw)
uint64_t *last_cc; ///< Pointer to last CC command sent
uint64_t last_cc_data; ///< Last CC command (raw)
uint64_t *last_col; ///< Pointer to last SET_COLOR_IMAGE command sent
uint64_t last_col_data; ///< Last COLOR command (raw)
uint64_t *last_tex; ///< Pointer to last SET_TEX_IMAGE command sent
uint64_t last_tex_data; ///< Last TEX command (raw)
uint64_t *last_z; ///< Pointer to last SET_Z_IMAGE command sent
uint64_t last_z_data; ///< Last Z command (raw)
setothermodes_t som; ///< Current SOM state
colorcombiner_t cc; ///< Current CC state
struct tile_s {
uint64_t *last_settile; ///< Pointer to last SET_TILE command sent
uint64_t *last_setsize; ///< Pointer to last LOAD_TILE/SET_TILE_SIZE command sent
uint64_t last_settile_data; ///< Last SET_TILE command (raw)
uint64_t last_setsize_data; ///< Last LOAD_TILE/SET_TILE_SIZE command (raw)
uint8_t fmt, size; ///< Format & size (RDP format/size bits)
uint8_t pal; ///< Palette number
bool has_extents; ///< True if extents were set (via LOAD_TILE / SET_TILE_SIZE)
float s0, t0, s1, t1; ///< Extents of tile in TMEM
int16_t tmem_addr; ///< Address in TMEM
int16_t tmem_pitch; ///< Pitch in TMEM
struct {
uint8_t mask; ///< Mask (RDP mask bits)
bool clamp; ///< Clamping enabled
bool mirror; ///< Mirroring enabled
} s, t; ///< Settings for S&T coordinates
} tile[8]; ///< Current tile descriptors
struct {
uint8_t fmt, size; ///< Format & size (RDP format/size bits)
uint16_t width, height; ///< Dimensions of the color image
} col; ///< Current associated color image
struct {
uint32_t physaddr; ///< Physical address of the texture
uint8_t fmt, size; ///< Format & size (RDP format/size bits)
} tex; ///< Current associated texture image
struct {
uint16_t x0,y0,x1,y1; ///< Scissor extents
} clip; ///< Current scissor extents
} rdp;
/**
* @brief Validator context
*/
struct {
uint64_t *buf; ///< Current instruction
uint32_t flags; ///< Flags (see RDPQ_VALIDATION_*)
int warns, errs; ///< Validators warnings/errors (stats)
bool crashed; ///< True if the RDP chip crashed
} vctx;
/** @brief Triangle primitives names */
static const char *tri_name[] = { "TRI", "TRI_Z", "TRI_TEX", "TRI_TEX_Z", "TRI_SHADE", "TRI_SHADE_Z", "TRI_TEX_SHADE", "TRI_TEX_SHADE_Z"};
static const char *tex_fmt_name[] = { "RGBA", "YUV", "CI", "IA", "I", "?", "?", "?" };
/** @brief Helper function to coalesce disassembled triangles */
static bool log_coalesce_tris(uint8_t cmd, uint8_t *last_tri_cmd, int *num_tris);
#ifdef N64
#define MAX_BUFFERS 12 ///< Maximum number of pending RDP buffers
#define MAX_HOOKS 4 ///< Maximum number of custom hooks
static rdp_buffer_t buffers[MAX_BUFFERS]; ///< Pending RDP buffers (ring buffer)
static volatile int buf_ridx, buf_widx; ///< Read/write index into the ring buffer of RDP buffers
static bool buf_changed; ///< True if the RDP has just switched buffer
static rdp_buffer_t last_buffer; ///< Last RDP buffer that was processed
static int show_log; ///< != 0 if logging is enabled
static void (*hooks[MAX_HOOKS])(void*, uint64_t*, int); ///< Custom hooks
static void* hooks_ctx[MAX_HOOKS]; ///< Context for the hooks
// Documented in rdpq_debug_internal.h
void (*rdpq_trace)(void);
void (*rdpq_trace_fetch)(bool new_buffer);
/** @brief Run the actual trace flushing the cached buffers */
void __rdpq_trace_flush(void);
/** @brief Implementation of #rdpq_trace_fetch */
void __rdpq_trace_fetch(bool new_buffer)
{
disable_interrupts();
// Extract current start/end pointers from RDP registers (in the uncached segment)
// Avoid race conditions versus RSP by reading the status register twice and retrying
// if it changed in between.
uint64_t *start, *end, status, status_prev;
do {
status_prev = *DP_STATUS;
start = (void*)(*DP_START | 0xA0000000);
end = (void*)(*DP_END | 0xA0000000);
status = *DP_STATUS;
} while (status != status_prev);
// If the registers contain a new start pointer without its associated end pointer,
// it means that we can't use this data: we don't know the full new buffer yet.
// In this case, we just return and wait for the next call.
if ((status & DP_STATUS_START_VALID) && !(status & DP_STATUS_END_VALID))
{
enable_interrupts();
return;
}
#if RDPQ_DEBUG_DEBUG
intdebugf("__rdpq_trace_fetch: %p-%p\n", start, end);
extern void *rspq_rdp_dynamic_buffers[2];
for (int i=0;i<2;i++)
if ((void*)start >= rspq_rdp_dynamic_buffers[i] && (void*)end <= rspq_rdp_dynamic_buffers[i]+RDPQ_DYNAMIC_BUFFER_SIZE)
intdebugf(" -> dynamic buffer %d\n", i);
#endif
assertf(start <= end, "rdpq_debug: invalid RDP buffer: %p-%p\n", start, end);
// Coalesce with last written buffer if possible. Notice that rdpq_trace put the start
// pointer to NULL to avoid coalescing when it begins dumping it, so this should avoid
// race conditions.
int prev = buf_widx ? buf_widx - 1 : MAX_BUFFERS-1;
if (!buf_changed && buffers[prev].start == start) {
if (buffers[prev].end == end) {
buf_changed = new_buffer;
enable_interrupts();
intdebugf(" -> ignored because coalescing\n");
return;
}
// If the previous buffer was bigger, it is a logic error, as RDP buffers should only grow
assertf(buffers[prev].end <= end, "rdpq_debug: RDP buffer shrinking (%p-%p => %p-%p)\n",
buffers[prev].start, buffers[prev].end, start, end);
buffers[prev].end = end;
// If the previous buffer was already dumped, dump it again as we added more
// information to it. We do not modify the "traced" pointer so that previously
// dumped commands are not dumped again.
if (buf_ridx == buf_widx) {
intdebugf(" -> replaying from %p\n", buffers[prev].traced);
buf_ridx = prev;
}
intdebugf(" -> coalesced\n");
buf_changed = new_buffer;
__rdpq_trace_flush(); // FIXME: remove this (see __rdpq_trace)
enable_interrupts();
return;
}
// If the buffer is full, we could continue logging by skipping a buffer, but the validator
// is done with. So for now just abort.
assertf((buf_widx + 1) % MAX_BUFFERS != buf_ridx, "validator buffer full\n");
// Write the new buffer. It should be an empty slot
buffers[buf_widx] = (rdp_buffer_t){ .start = start, .end = end, .traced = start };
intdebugf(" -> written to slot %d\n", buf_widx);
buf_widx = (buf_widx + 1) % MAX_BUFFERS;
// If we know for sure that the RDP is about the change buffer, remember it so that
// next reads will surely be a new one. For instance, this allows to process twice
// a same buffer sent two times in a row.
buf_changed = new_buffer;
__rdpq_trace_flush(); // FIXME: remove this (see __rdpq_trace)
enable_interrupts();
}
/** @brief Process a RDPQ_DEBUG command */
void __rdpq_debug_cmd(uint64_t cmd)
{
switch(BITS(cmd, 48, 55)) {
case 0x01: // Show log
show_log += BIT(cmd, 0) ? 1 : -1;
return;
case 0x02: // Message
// Nothing to do. Debugging messages are shown by the disassembler
return;
}
}
/** @brief Implementation of #rdpq_trace */
void __rdpq_trace(void)
{
// Update buffers to current RDP status. This make sure the trace
// is up to date.
__rdpq_trace_fetch(false);
__rdpq_trace_flush();
}
void __rdpq_trace_flush(void)
{
while (1) {
uint64_t *cur = 0, *end = 0;
// Pop next RDP buffer from ring buffer. Do it atomically to avoid races
disable_interrupts();
if (buf_ridx != buf_widx) {
cur = buffers[buf_ridx].traced;
end = buffers[buf_ridx].end;
buffers[buf_ridx].traced = end;
buf_ridx = (buf_ridx + 1) % MAX_BUFFERS;
}
enable_interrupts();
// If there are no more pending buffers, we are done
if (!cur) break;
// Go through the RDP buffer. If log is active, disassemble.
// Run the validator on all the commands.
while (cur < end) {
uint8_t cmd = BITS(cur[0],56,61);
int sz = rdpq_debug_disasm_size(cur);
// Disassemble the command
bool shown = false;
if (show_log > 0)
shown = rdpq_debug_disasm(cur, stderr);
// Validate the command: if the command was already shown, we don't need
// to further echo it.
uint32_t val_flags = shown ? RDPQ_VALIDATE_FLAG_NOECHO : 0;
rdpq_validate(cur, val_flags, NULL, NULL);
// Run trace hooks
for (int i=0;i<MAX_HOOKS && hooks[i];i++)
hooks[i](hooks_ctx[i], cur, sz);
// If this is a RDPQ_DEBUG command, execute it
if (cmd == RDPQ_CMD_DEBUG) __rdpq_debug_cmd(cur[0]);
cur += sz;
}
}
// show the accumulated tris (if any)
rdpq_debug_disasm(NULL, stderr);
}
void rdpq_debug_start(void)
{
memset(buffers, 0, sizeof(buffers));
memset(&last_buffer, 0, sizeof(last_buffer));
memset(&rdp, 0, sizeof(rdp));
memset(&vctx, 0, sizeof(vctx));
memset(&hooks, 0, sizeof(hooks));
buf_widx = buf_ridx = 0;
show_log = 0;
__rdpq_debug_log_flags = 0;
rdpq_trace = __rdpq_trace;
rdpq_trace_fetch = __rdpq_trace_fetch;
assertf(__rdpq_inited, "rdpq_init() must be called before rdpq_debug_start()");
rspq_write(RDPQ_OVL_ID, RDPQ_CMD_SET_DEBUG_MODE, 1);
debugf("\n**************************************************\nRDPQ debug engine started (slowdowns are expected)\n**************************************************\n\n");
}
void rdpq_debug_log(bool log)
{
static bool warning = false;
if (log && !rdpq_trace && !warning) {
debugf("WARNING: rdpq_debug_log(true) ignored because trace engine was not started\n");
warning = true;
}
if (rdpq_trace)
rdpq_passthrough_write((RDPQ_CMD_DEBUG, RDPQ_CMD_DEBUG_SHOWLOG, log ? 1 : 0));
}
void rdpq_debug_log_msg(const char *msg)
{
if (rdpq_trace)
rdpq_passthrough_write((RDPQ_CMD_DEBUG, RDPQ_CMD_DEBUG_MESSAGE, PhysicalAddr(msg)));
}
void rdpq_debug_stop(void)
{
rdpq_trace = NULL;
rdpq_trace_fetch = NULL;
rspq_write(RDPQ_OVL_ID, RDPQ_CMD_SET_DEBUG_MODE, 0);
}
void rdpq_debug_install_hook(void (*hook)(void*, uint64_t*, int), void* ctx)
{
for (int i=0;i<MAX_HOOKS;i++)
if (!hooks[i]) {
hooks[i] = hook;
hooks_ctx[i] = ctx;
return;
}
assertf(0, "reached maximum number of hooks (%d)", MAX_HOOKS);
}
#endif
/** @brief Decode a SET_COMBINE command into a #colorcombiner_t structure */
static inline colorcombiner_t decode_cc(uint64_t cc) {
return (colorcombiner_t){
.cyc = {{
.rgb = { BITS(cc, 52, 55), BITS(cc, 28, 31), BITS(cc, 47, 51), BITS(cc, 15, 17) },
.alpha = { BITS(cc, 44, 46), BITS(cc, 12, 14), BITS(cc, 41, 43), BITS(cc, 9, 11) },
},{
.rgb = { BITS(cc, 37, 40), BITS(cc, 24, 27), BITS(cc, 32, 36), BITS(cc, 6, 8) },
.alpha = { BITS(cc, 21, 23), BITS(cc, 3, 5), BITS(cc, 18, 20), BITS(cc, 0, 2) },
}}
};
}
/** @brief Decode a SET_OTHER_MODES command into a #setothermodes_t structure */
static inline setothermodes_t decode_som(uint64_t som) {
return (setothermodes_t){
.atomic = BIT(som, 55),
.cycle_type = BITS(som, 52, 53),
.tex = { .persp = BIT(som, 51), .detail = BIT(som, 50), .sharpen = BIT(som, 49), .lod = BIT(som, 48) },
.tlut = { .enable = BIT(som, 47), .type = BIT(som, 46) },
.sample_type = BITS(som, 44, 45),
.tf_mode = BITS(som, 41, 43),
.chromakey = BIT(som, 40),
.dither = { .rgb = BITS(som, 38, 39), .alpha = BITS(som, 36, 37) },
.blender = {
{ BITS(som, 30, 31), BITS(som, 26, 27), BITS(som, 22, 23), BITS(som, 18, 19) },
{ BITS(som, 28, 29), BITS(som, 24, 25), BITS(som, 20, 21), BITS(som, 16, 17) },
},
.blend = BIT(som, 14), .read = BIT(som, 6), .aa = BIT(som, 3),
.cvg = { .mode = BITS(som, 8, 9), .color = BIT(som, 7) },
.blalpha = { .cvg = BIT(som, 13), .mul_cc = BIT(som, 12) },
.z = { .mode = BITS(som, 10, 11), .upd = BIT(som, 5), .cmp = BIT(som, 4), .prim = BIT(som, 2) },
.alphacmp = { .enable = BIT(som, 0), .noise = BIT(som, 1) },
.rdpqx = { .fog = BIT(som, 32), .freeze = BIT(som, 33), .bl2 = BIT(som, 15) },
};
}
int rdpq_debug_disasm_size(uint64_t *buf) {
switch (CMD(buf[0])) {
default: return 1;
case 0x24: return 2; // TEX_RECT
case 0x25: return 2; // TEX_RECT_FLIP
case 0x08: return 4; // TRI_FILL
case 0x09: return 6; // TRI_FILL_ZBUF
case 0x0A: return 12; // TRI_TEX
case 0x0B: return 14; // TRI_TEX_ZBUF
case 0x0C: return 12; // TRI_SHADE
case 0x0D: return 14; // TRI_SHADE_ZBUF
case 0x0E: return 20; // TRI_SHADE_TEX
case 0x0F: return 22; // TRI_SHADE_TEX_ZBUF
}
}
char* rdpq_debug_disasm_cc(uint64_t cc64)
{
static const char* rgb_suba1[16] = {"COMBINED", "TEX0", "TEX1", "PRIM", "SHADE", "ENV", "1", "NOISE", "0","0","0","0","0","0","0","0"};
static const char* rgb_suba2[16] = {"COMBINED", "TEX1", "TEX0_BUG", "PRIM", "SHADE", "ENV", "1", "NOISE", "0","0","0","0","0","0","0","0"};
static const char* rgb_subb1[16] = {"COMBINED", "TEX0", "TEX1", "PRIM", "SHADE", "ENV", "KEYCENTER", "K4", "0","0","0","0","0","0","0","0"};
static const char* rgb_subb2[16] = {"COMBINED", "TEX1", "TEX0_BUG", "PRIM", "SHADE", "ENV", "KEYCENTER", "K4", "0","0","0","0","0","0","0","0"};
static const char* rgb_mul1[32] = {"COMBINED", "TEX0", "TEX1", "PRIM", "SHADE", "ENV", "KEYSCALE", "COMBINED_ALPHA", "TEX0_ALPHA", "TEX1_ALPHA", "PRIM_ALPHA", "SHADE_ALPHA", "ENV_ALPHA", "LOD_FRAC", "PRIM_LOD_FRAC", "K5", "0","0","0","0","0","0","0","0", "0","0","0","0","0","0","0","0"};
static const char* rgb_mul2[32] = {"COMBINED", "TEX1", "TEX0_BUG", "PRIM", "SHADE", "ENV", "KEYSCALE", "COMBINED_ALPHA", "TEX0_ALPHA", "TEX1_ALPHA", "PRIM_ALPHA", "SHADE_ALPHA", "ENV_ALPHA", "LOD_FRAC", "PRIM_LOD_FRAC", "K5", "0","0","0","0","0","0","0","0", "0","0","0","0","0","0","0","0"};
static const char* rgb_add1[8] = {"COMBINED", "TEX0", "TEX1", "PRIM", "SHADE", "ENV", "1", "0"};
static const char* rgb_add2[8] = {"COMBINED", "TEX1", "TEX0_BUG", "PRIM", "SHADE", "ENV", "1", "0"};
static const char* alpha_addsub1[8] = {"COMBINED", "TEX0", "TEX1", "PRIM", "SHADE", "ENV", "1", "0"};
static const char* alpha_mul1[8] = {"LOD_FRAC", "TEX1", "TEX0_BUG", "PRIM", "SHADE", "ENV", "PRIM_LOD_FRAC", "0"};
static const char* alpha_addsub2[8] = {"COMBINED", "TEX0", "TEX1", "PRIM", "SHADE", "ENV", "1", "0"};
static const char* alpha_mul2[8] = {"LOD_FRAC", "TEX1", "TEX0_BUG", "PRIM", "SHADE", "ENV", "PRIM_LOD_FRAC", "0"};
char buf[256];
colorcombiner_t cc = decode_cc(cc64);
if (!(cc64 & (1ull<<63)) && memcmp(&cc.cyc[0], &cc.cyc[1], sizeof(struct cc_cycle_s)) == 0) {
snprintf(buf, sizeof(buf), "RDPQ_COMBINER1((%s,%s,%s,%s),(%s,%s,%s,%s))",
rgb_suba1[cc.cyc[0].rgb.suba], rgb_subb1[cc.cyc[0].rgb.subb], rgb_mul1[cc.cyc[0].rgb.mul], rgb_add1[cc.cyc[0].rgb.add],
alpha_addsub1[cc.cyc[0].alpha.suba], alpha_addsub1[cc.cyc[0].alpha.subb], alpha_mul1[cc.cyc[0].alpha.mul], alpha_addsub1[cc.cyc[0].alpha.add]);
} else {
snprintf(buf, sizeof(buf), "RDPQ_COMBINER2((%s,%s,%s,%s),(%s,%s,%s,%s),(%s,%s,%s,%s),(%s,%s,%s,%s))",
rgb_suba1[cc.cyc[0].rgb.suba], rgb_subb1[cc.cyc[0].rgb.subb], rgb_mul1[cc.cyc[0].rgb.mul], rgb_add1[cc.cyc[0].rgb.add],
alpha_addsub1[cc.cyc[0].alpha.suba], alpha_addsub1[cc.cyc[0].alpha.subb], alpha_mul1[cc.cyc[0].alpha.mul], alpha_addsub1[cc.cyc[0].alpha.add],
rgb_suba2[cc.cyc[1].rgb.suba], rgb_subb2[cc.cyc[1].rgb.subb], rgb_mul2[cc.cyc[1].rgb.mul], rgb_add2[cc.cyc[1].rgb.add],
alpha_addsub2[cc.cyc[1].alpha.suba], alpha_addsub2[cc.cyc[1].alpha.subb], alpha_mul2[cc.cyc[1].alpha.mul], alpha_addsub2[cc.cyc[1].alpha.add]);
}
return strdup(buf);
}
/** @brief Multiplication factor to convert a number to fixed point with precision n */
#define FX(n) (1.0f / (1<<(n)))
/** @brief Convert a 16.16 fixed point number into floating point */
#define FX32(hi,lo) ((int16_t)(hi) + (lo) * (1.f / 65536.f))
static void __rdpq_debug_disasm(uint64_t *addr, uint64_t *buf, FILE *out)
{
const char* flag_prefix = "";
///@cond
#define FLAG_RESET() ({ flag_prefix = ""; })
#define FLAG(v, s) ({ if (v) fprintf(out, "%s%s", flag_prefix, s), flag_prefix = " "; })
///@endcond
static const char *fmt[8] = {"rgba", "yuv", "ci", "ia", "i", "?fmt=5?", "?fmt=6?", "?fmt=7?"};
static const char *size[4] = {"4", "8", "16", "32" };
fprintf(out, "[%p] %016" PRIx64 " ", addr, buf[0]);
switch (CMD(buf[0])) {
default: fprintf(out, "???\n"); return;
case 0x00: fprintf(out, "NOP\n"); return;
case 0x27: fprintf(out, "SYNC_PIPE\n"); return;
case 0x28: fprintf(out, "SYNC_TILE\n"); return;
case 0x29: fprintf(out, "SYNC_FULL\n"); return;
case 0x26: fprintf(out, "SYNC_LOAD\n"); return;
case 0x2A: fprintf(out, "SET_KEY_GB WidthG=%d CenterG=%d ScaleG=%d, WidthB=%d CenterB=%d ScaleB=%d\n",
BITS(buf[0], 44, 55), BITS(buf[0], 24, 31), BITS(buf[0], 16, 23), BITS(buf[0], 32, 43), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7)); return;
case 0x2B: fprintf(out, "SET_KEY_R WidthR=%d CenterR=%d ScaleR=%d\n",
BITS(buf[0], 16, 27), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7)); return;
case 0x2C: fprintf(out, "SET_CONVERT k0=%d k1=%d k2=%d k3=%d k4=%d k5=%d\n",
BITS(buf[0], 45, 53), BITS(buf[0], 36, 44), BITS(buf[0], 27, 35), BITS(buf[0], 18, 26), BITS(buf[0], 9, 17), BITS(buf[0], 0, 8)); return;
case 0x2D: fprintf(out, "SET_SCISSOR xy=(%.2f,%.2f)-(%.2f,%.2f)",
BITS(buf[0], 44, 55)*FX(2), BITS(buf[0], 32, 43)*FX(2), BITS(buf[0], 12, 23)*FX(2), BITS(buf[0], 0, 11)*FX(2));
if(BITS(buf[0], 25, 25)) fprintf(out, " field=%s", BITS(buf[0], 24, 24) ? "odd" : "even");
fprintf(out, "\n"); return;
case 0x36: fprintf(out, "FILL_RECT xy=(%.2f,%.2f)-(%.2f,%.2f)\n",
BITS(buf[0], 12, 23)*FX(2), BITS(buf[0], 0, 11)*FX(2), BITS(buf[0], 44, 55)*FX(2), BITS(buf[0], 32, 43)*FX(2)); return;
case 0x2E: fprintf(out, "SET_PRIM_DEPTH z=0x%x deltaz=0x%x\n", BITS(buf[0], 16, 31), BITS(buf[1], 0, 15)); return;
case 0x37: fprintf(out, "SET_FILL_COLOR rgba16=(%d,%d,%d,%d) rgba32=(%d,%d,%d,%d)\n",
BITS(buf[0], 11, 15), BITS(buf[0], 6, 10), BITS(buf[0], 1, 5), BITS(buf[0], 0, 0),
BITS(buf[0], 24, 31), BITS(buf[0], 16, 23), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7)); return;
case 0x38: fprintf(out, "SET_FOG_COLOR rgba32=(%d,%d,%d,%d)\n", BITS(buf[0], 24, 31), BITS(buf[0], 16, 23), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7)); return;
case 0x39: fprintf(out, "SET_BLEND_COLOR rgba32=(%d,%d,%d,%d)\n", BITS(buf[0], 24, 31), BITS(buf[0], 16, 23), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7)); return;
case 0x3A: {
fprintf(out, "SET_PRIM_COLOR rgba32=(%d,%d,%d,%d)", BITS(buf[0], 24, 31), BITS(buf[0], 16, 23), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7));
int prim_lod_frac = BITS(buf[0], 32, 39);
if (prim_lod_frac) fprintf(out, " prim_lod_frac=%d", prim_lod_frac);
int min_lod = BITS(buf[0], 40, 44);
if (min_lod) fprintf(out, " min_lod=%d", min_lod);
fprintf(out, "\n");
} return;
case 0x3B: fprintf(out, "SET_ENV_COLOR rgba32=(%d,%d,%d,%d)\n", BITS(buf[0], 24, 31), BITS(buf[0], 16, 23), BITS(buf[0], 8, 15), BITS(buf[0], 0, 7)); return;
case 0x2F: { fprintf(out, "SET_OTHER_MODES ");
static const char* cyc[] = { "1cyc", "2cyc", "copy", "fill" };
static const char* texinterp[] = { "point", "point", "bilinear", "median" };
static const char* yuv1[] = { "yuv1", "yuv1_tex0" };
static const char* zmode[] = { "opaque", "inter", "trans", "decal" };
static const char* rgbdither[] = { "square", "bayer", "noise", "none" };
static const char* alphadither[] = { "pat", "inv", "noise", "none" };
static const char* cvgmode[] = { "clamp", "wrap", "zap", "save" };
static const char* blend1_a[] = { "in", "mem", "blend", "fog" };
static const char* blend1_b1[] = { "in.a", "fog.a", "shade.a", "0" };
static const char* blend1_b1inv[] = { "(1-in.a)", "(1-fog.a)", "(1-shade.a)", "1" };
static const char* blend1_b2[] = { "", "mem.a", "1", "0" };
static const char* blend2_a[] = { "cyc1", "mem", "blend", "fog" };
static const char* blend2_b1[] = { "in.a", "fog.a", "shade.a", "0" };
static const char* blend2_b1inv[] = { "(1-in.a)", "(1-fog.a)", "(1-shade.a)", "1" };
static const char* blend2_b2[] = { "", "mem.a", "1", "0" };
setothermodes_t som = decode_som(buf[0]);
fprintf(out, "%s", cyc[som.cycle_type]);
if((som.cycle_type < 2) && (som.tex.persp || som.tex.detail || som.tex.sharpen || som.tex.lod || som.sample_type != 0 || som.tf_mode != 6)) {
fprintf(out, " tex=["); FLAG_RESET();
FLAG(som.tex.persp, "persp"); FLAG(som.tex.detail, "detail"); FLAG(som.tex.sharpen, "sharpen"); FLAG(som.tex.lod, "lod");
FLAG(!(som.tf_mode & 4), "yuv0"); FLAG(!(som.tf_mode & 2), yuv1[som.tf_mode&1]);
FLAG(som.sample_type != 0, texinterp[som.sample_type]);
fprintf(out, "]");
}
if(som.tlut.enable) fprintf(out, " tlut%s", som.tlut.type ? "=[ia]" : "");
if(BITS(buf[0], 16, 31)) {
if (som.blender[0].p==0 && som.blender[0].a==0 && som.blender[0].q==0 && som.blender[0].b==0)
fprintf(out, " blend=[<passthrough>, ");
else
fprintf(out, " blend=[%s*%s + %s*%s, ",
blend1_a[som.blender[0].p], blend1_b1[som.blender[0].a], blend1_a[som.blender[0].q], som.blender[0].b ? blend1_b2[som.blender[0].b] : blend1_b1inv[som.blender[0].a]);
fprintf(out, "%s*%s + %s*%s]",
blend2_a[som.blender[1].p], blend2_b1[som.blender[1].a], blend2_a[som.blender[1].q], som.blender[1].b ? blend2_b2[som.blender[1].b] : blend2_b1inv[som.blender[1].a]);
}
if(som.z.upd || som.z.cmp || som.z.prim) {
fprintf(out, " z=["); FLAG_RESET();
FLAG(som.z.cmp, "cmp"); FLAG(som.z.upd, "upd"); FLAG(som.z.prim, "prim"); FLAG(true, zmode[som.z.mode]);
fprintf(out, "]");
}
flag_prefix = " ";
FLAG(som.aa, "aa"); FLAG(som.read, "read"); FLAG(som.blend, "blend");
FLAG(som.chromakey, "chroma_key"); FLAG(som.atomic, "atomic");
if(som.alphacmp.enable) fprintf(out, " alpha_compare%s", som.alphacmp.noise ? "[noise]" : "");
if((som.cycle_type < 2) && (som.dither.rgb != 3 || som.dither.alpha != 3)) fprintf(out, " dither=[%s,%s]", rgbdither[som.dither.rgb], alphadither[som.dither.alpha]);
if(som.cvg.mode || som.cvg.color) {
fprintf(out, " cvg=["); FLAG_RESET();
FLAG(som.cvg.mode, cvgmode[som.cvg.mode]); FLAG(som.cvg.color, "color_on_ovf");
fprintf(out, "]");
}
if(som.blalpha.cvg || som.blalpha.mul_cc) {
fprintf(out, " blend_inalpha=["); FLAG_RESET();
FLAG(som.blalpha.cvg, "cvg"); FLAG(som.blalpha.mul_cc, "mul_cc");
fprintf(out, "]");
}
if(som.rdpqx.bl2 || som.rdpqx.freeze || som.rdpqx.fog) {
fprintf(out, " rdpq=["); FLAG_RESET();
FLAG(som.rdpqx.bl2, "bl2"); FLAG(som.rdpqx.freeze, "freeze");
FLAG(som.rdpqx.fog, "fog");
fprintf(out, "]");
}
fprintf(out, "\n");
}; return;
case 0x3C: { fprintf(out, "SET_COMBINE_MODE ");
static const char* rgb_suba[16] = {"comb", "tex0", "tex1", "prim", "shade", "env", "1", "noise", "0","0","0","0","0","0","0","0"};
static const char* rgb_subb[16] = {"comb", "tex0", "tex1", "prim", "shade", "env", "keycenter", "k4", "0","0","0","0","0","0","0","0"};
static const char* rgb_mul[32] = {"comb", "tex0", "tex1", "prim", "shade", "env", "keyscale", "comb.a", "tex0.a", "tex1.a", "prim.a", "shade.a", "env.a", "lod_frac", "prim_lod_frac", "k5", "0","0","0","0","0","0","0","0", "0","0","0","0","0","0","0","0"};
static const char* rgb_add[8] = {"comb", "tex0", "tex1", "prim", "shade", "env", "1", "0"};
static const char* alpha_addsub[8] = {"comb", "tex0", "tex1", "prim", "shade", "env", "1", "0"};
static const char* alpha_mul[8] = {"lod_frac", "tex0", "tex1", "prim", "shade", "env", "prim_lod_frac", "0"};
colorcombiner_t cc = decode_cc(buf[0]);
fprintf(out, "cyc0=[(%s-%s)*%s+%s, (%s-%s)*%s+%s], ",
rgb_suba[cc.cyc[0].rgb.suba], rgb_subb[cc.cyc[0].rgb.subb], rgb_mul[cc.cyc[0].rgb.mul], rgb_add[cc.cyc[0].rgb.add],
alpha_addsub[cc.cyc[0].alpha.suba], alpha_addsub[cc.cyc[0].alpha.subb], alpha_mul[cc.cyc[0].alpha.mul], alpha_addsub[cc.cyc[0].alpha.add]);
const struct cc_cycle_s passthrough = {0};
if (!__builtin_memcmp(&cc.cyc[1], &passthrough, sizeof(struct cc_cycle_s))) fprintf(out, "cyc1=[<passthrough>]\n");
else fprintf(out, "cyc1=[(%s-%s)*%s+%s, (%s-%s)*%s+%s]\n",
rgb_suba[cc.cyc[1].rgb.suba], rgb_subb[cc.cyc[1].rgb.subb], rgb_mul[cc.cyc[1].rgb.mul], rgb_add[cc.cyc[1].rgb.add],
alpha_addsub[cc.cyc[1].alpha.suba], alpha_addsub[cc.cyc[1].alpha.subb], alpha_mul[cc.cyc[1].alpha.mul], alpha_addsub[cc.cyc[1].alpha.add]);
} return;
case 0x35: { fprintf(out, "SET_TILE ");
uint8_t f = BITS(buf[0], 53, 55);
fprintf(out, "tile=%d %s%s tmem[0x%x,line=%d]",
BITS(buf[0], 24, 26), fmt[f], size[BITS(buf[0], 51, 52)],
BITS(buf[0], 32, 40)*8, BITS(buf[0], 41, 49)*8);
if (f==2) fprintf(out, " pal=%d", BITS(buf[0], 20, 23));
fprintf(out, " mask=[%d, %d]", 1<<BITS(buf[0], 4, 7), 1<<BITS(buf[0], 14, 17));
bool clamp = BIT(buf[0], 19) || BIT(buf[0], 9);
bool mirror = BIT(buf[0], 18) || BIT(buf[0], 8);
if (clamp) {
fprintf(out, " clamp=["); FLAG_RESET();
FLAG(BIT(buf[0], 9), "s"); FLAG(BIT(buf[0], 19), "t");
fprintf(out, "]");
}
if (mirror) {
fprintf(out, " mirror=["); FLAG_RESET();
FLAG(BIT(buf[0], 8), "s"); FLAG(BIT(buf[0], 18), "t");
fprintf(out, "]");
}
if (BITS(buf[0], 0, 3) || BITS(buf[0], 10, 13))
fprintf(out, " shift=[%d, %d]", ((BITS(buf[0],0,3)+5)&15)-5, ((BITS(buf[0], 10, 13)+5)&15)-5);
fprintf(out, "\n");
} return;
case 0x24 ... 0x25:
if(CMD(buf[0]) == 0x24)
fprintf(out, "TEX_RECT ");
else
fprintf(out, "TEX_RECT_FLIP ");
fprintf(out, "tile=%d xy=(%.2f,%.2f)-(%.2f,%.2f)\n", BITS(buf[0], 24, 26),
BITS(buf[0], 12, 23)*FX(2), BITS(buf[0], 0, 11)*FX(2), BITS(buf[0], 44, 55)*FX(2), BITS(buf[0], 32, 43)*FX(2));
fprintf(out, "[%p] %016" PRIx64 " ", &addr[1], buf[1]);
fprintf(out, "st=(%.2f,%.2f) dst=(%.5f,%.5f)\n",
SBITS(buf[1], 48, 63)*FX(5), SBITS(buf[1], 32, 47)*FX(5), SBITS(buf[1], 16, 31)*FX(10), SBITS(buf[1], 0, 15)*FX(10));
return;
case 0x32: case 0x34:
if(CMD(buf[0]) == 0x32)
fprintf(out, "SET_TILE_SIZE ");
else
fprintf(out, "LOAD_TILE ");
fprintf(out, "tile=%d st=(%.2f,%.2f)-(%.2f,%.2f)\n",
BITS(buf[0], 24, 26), BITS(buf[0], 44, 55)*FX(2), BITS(buf[0], 32, 43)*FX(2),
BITS(buf[0], 12, 23)*FX(2), BITS(buf[0], 0, 11)*FX(2));
return;
case 0x30: fprintf(out, "LOAD_TLUT tile=%d palidx=(%d-%d)\n",
BITS(buf[0], 24, 26), BITS(buf[0], 46, 55), BITS(buf[0], 14, 23)); return;
case 0x33: fprintf(out, "LOAD_BLOCK tile=%d st=(%d,%d) n=%d dxt=%.5f\n",
BITS(buf[0], 24, 26), BITS(buf[0], 44, 55), BITS(buf[0], 32, 43),
BITS(buf[0], 12, 23)+1, BITS(buf[0], 0, 11)*FX(11)); return;
case 0x08 ... 0x0F: {
int cmd = CMD(buf[0])-0x8;
fprintf(out, "%-17s", tri_name[cmd]);
fprintf(out, "%s tile=%d lvl=%d y=(%.2f, %.2f, %.2f)\n",
BITS(buf[0], 55, 55) ? "left" : "right", BITS(buf[0], 48, 50), BITS(buf[0], 51, 53)+1,
SBITS(buf[0], 0, 13)*FX(2), SBITS(buf[0], 16, 29)*FX(2), SBITS(buf[0], 32, 45)*FX(2));
fprintf(out, "[%p] %016" PRIx64 " xl=%.4f isl=%.4f\n", &addr[1], buf[1],
SBITS(buf[1], 32, 63)*FX(16), SBITS(buf[1], 0, 31)*FX(16));
fprintf(out, "[%p] %016" PRIx64 " xh=%.4f ish=%.4f\n", &addr[2], buf[2],
SBITS(buf[2], 32, 63)*FX(16), SBITS(buf[2], 0, 31)*FX(16));
fprintf(out, "[%p] %016" PRIx64 " xm=%.4f ism=%.4f\n", &addr[3], buf[3],
SBITS(buf[3], 32, 63)*FX(16), SBITS(buf[3], 0, 31)*FX(16));
int i=4;
if (cmd & 0x4) {
fprintf(out, "[%p] %016" PRIx64 " r=%.5f g=%.5f b=%.5f a=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31)),
FX32(BITS(buf[i], 0, 15), BITS(buf[i+2], 0, 15))); i++;
fprintf(out, "[%p] %016" PRIx64 " drdx=%.5f dgdx=%.5f dbdx=%.5f dadx=%.5f\n", &buf[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31)),
FX32(BITS(buf[i], 0, 15), BITS(buf[i+2], 0, 15))); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
fprintf(out, "[%p] %016" PRIx64 " drde=%.5f dgde=%.5f dbde=%.5f dade=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31)),
FX32(BITS(buf[i], 0, 15), BITS(buf[i+2], 0, 15))); i++;
fprintf(out, "[%p] %016" PRIx64 " drdy=%.5f dgdy=%.5f dbdy=%.5f dady=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31)),
FX32(BITS(buf[i], 0, 15), BITS(buf[i+2], 0, 15))); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
}
if (cmd & 0x2) {
fprintf(out, "[%p] %016" PRIx64 " s=%.5f t=%.5f w=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31))); i++;
fprintf(out, "[%p] %016" PRIx64 " dsdx=%.5f dtdx=%.5f dwdx=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31))); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
fprintf(out, "[%p] %016" PRIx64 " dsde=%.5f dtde=%.5f dwde=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31))); i++;
fprintf(out, "[%p] %016" PRIx64 " dsdy=%.5f dtdy=%.5f dwdy=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i+2], 48, 63)),
FX32(BITS(buf[i], 32, 47), BITS(buf[i+2], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i+2], 16, 31))); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
fprintf(out, "[%p] %016" PRIx64 " \n", &addr[i], buf[i]); i++;
}
if (cmd & 0x1) {
fprintf(out, "[%p] %016" PRIx64 " z=%.5f dzdx=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i], 0, 15))); i++;
fprintf(out, "[%p] %016" PRIx64 " dzde=%.5f dzdy=%.5f\n", &addr[i], buf[i],
FX32(BITS(buf[i], 48, 63), BITS(buf[i], 32, 47)),
FX32(BITS(buf[i], 16, 31), BITS(buf[i], 0, 15))); i++;
}
return;
}
case 0x3e: {
fprintf(out, "SET_Z_IMAGE ");
uint32_t addr = BITS(buf[0], 0, 25);
if (addr == RDPQ_VALIDATE_DETACH_ADDR) fprintf(out, "<detach>\n");
else fprintf(out, "dram=%08" PRIx32 "\n", addr);
} return;
case 0x3d: fprintf(out, "SET_TEX_IMAGE dram=%08x w=%d %s%s\n",
BITS(buf[0], 0, 25), BITS(buf[0], 32, 41)+1, fmt[BITS(buf[0], 53, 55)], size[BITS(buf[0], 51, 52)]);
return;
case 0x3f: {
fprintf(out, "SET_COLOR_IMAGE ");
uint32_t addr = BITS(buf[0], 0, 25);
if (addr == RDPQ_VALIDATE_DETACH_ADDR) fprintf(out, "<detach>\n");
else {
fprintf(out, "dram=%08" PRIx32 " w=%d ", addr, BITS(buf[0], 32, 41)+1);
int height = BITS(buf[0], 42, 50) | (BIT(buf[0], 31) << 9);
if (height) fprintf(out, "h=%d ", height+1); // libdragon extension
fprintf(out, "%s%s\n", fmt[BITS(buf[0], 53, 55)], size[BITS(buf[0], 51, 52)]);
}
} return;
case 0x31: switch(BITS(buf[0], 48, 55)) {
case 0x01: fprintf(out, "RDPQ_SHOWLOG show=%d\n", BIT(buf[0], 0)); return;
#ifdef N64
case 0x02: fprintf(out, "RDPQ_MESSAGE %s\n", (char*)CachedAddr(0x80000000|BITS(buf[0], 0, 24))); return;
#endif
default: fprintf(out, "RDPQ_DEBUG <unkwnown>\n"); return;
}
}
}
static bool log_coalesce_tris(uint8_t cmd, uint8_t *last_tri_cmd, int *num_tris) {
if (!CMD_IS_TRI(cmd)) {
if (*last_tri_cmd) {
debugf("[..........] ................ %-16s num_cmds=%d\n", tri_name[*last_tri_cmd - 0x08], *num_tris);
*last_tri_cmd = 0;
*num_tris = 0;
}
return true;
} else {
if (*last_tri_cmd && *last_tri_cmd != cmd) {
debugf("[..........] ................ %-16s num_cmds=%d\n", tri_name[*last_tri_cmd - 0x08], *num_tris);
*last_tri_cmd = 0;
*num_tris = 0;
}
*last_tri_cmd = cmd;
*num_tris = *num_tris+1;
return false;
}
}
bool rdpq_debug_disasm(uint64_t *buf, FILE *out) {
static uint8_t last_tri_cmd = 0; static int num_tris = 0;
if (buf) {
uint8_t cmd = BITS(buf[0],56,61);
if ((__rdpq_debug_log_flags & RDPQ_LOG_FLAG_SHOWTRIS) || log_coalesce_tris(cmd, &last_tri_cmd, &num_tris)) {
__rdpq_debug_disasm(buf, buf, out);
return true;
}
} else {
log_coalesce_tris(0, &last_tri_cmd, &num_tris);
}
return false;
}
#define EMIT_TYPE 0x3 ///< Type of message (mask)
#define EMIT_CRASH 0x0 ///< Message is a RDP crash
#define EMIT_ERROR 0x1 ///< Message is an error
#define EMIT_WARN 0x2 ///< Message is a warning
#define EMIT_CTX_SOM 0x4 ///< Message context must show last SOM
#define EMIT_CTX_CC 0x8 ///< Message context must show last CC
#define EMIT_CTX_TEX 0x10 ///< Message context must show last SET_TEX_IMAGE
#define EMIT_CTX_TILES (0xFF << 5) ///< Message context must show SET_TILE (mask)
#define EMIT_CTX_TILE(n) (0x20 << (n)) ///< Message context must show tile n
#define EMIT_CTX_TILESIZE 0x2000 ///< Message context must show LOAD_TILE/SET_TILE_SIZE instead of SET_TILE
__attribute__((format(printf, 2, 3)))
static void validate_emit_error(int flags, const char *msg, ...)
{
va_list args;
if (!(vctx.flags & RDPQ_VALIDATE_FLAG_NOECHO)) {
if (flags & EMIT_CTX_SOM) __rdpq_debug_disasm(rdp.last_som, &rdp.last_som_data, stderr);
if (flags & EMIT_CTX_CC) __rdpq_debug_disasm(rdp.last_cc, &rdp.last_cc_data, stderr);
if (flags & EMIT_CTX_TEX) __rdpq_debug_disasm(rdp.last_tex, &rdp.last_tex_data, stderr);
if (flags & EMIT_CTX_TILES) {
for (int i = 0; i < 8; i++) {
if (flags & EMIT_CTX_TILE(i)) {
__rdpq_debug_disasm(rdp.tile[i].last_settile, &rdp.tile[i].last_settile_data, stderr);
if (rdp.tile[i].has_extents)
__rdpq_debug_disasm(rdp.tile[i].last_setsize, &rdp.tile[i].last_setsize_data, stderr);
break;
}
}
}
rdpq_debug_disasm(vctx.buf, stderr);
}
switch (flags & EMIT_TYPE) {
case EMIT_CRASH:
fprintf(stderr, "[RDPQ_VALIDATION] CRASH: ");
vctx.crashed = true;
vctx.errs += 1;
break;
case EMIT_ERROR:
fprintf(stderr, "[RDPQ_VALIDATION] ERROR: ");
vctx.errs += 1;
break;
case EMIT_WARN:
fprintf(stderr, "[RDPQ_VALIDATION] WARN: ");
vctx.warns += 1;
break;
}
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
if ((flags & EMIT_TYPE) == EMIT_CRASH)
fprintf(stderr, "[RDPQ_VALIDATION] This is a fatal error: a real RDP chip would stop working until reboot\n");
if (flags & EMIT_CTX_SOM) fprintf(stderr, "[RDPQ_VALIDATION] SET_OTHER_MODES last sent at %p\n", rdp.last_som);
if (flags & EMIT_CTX_CC) fprintf(stderr, "[RDPQ_VALIDATION] SET_COMBINE_MODE last sent at %p\n", rdp.last_cc);
if (flags & EMIT_CTX_TEX) fprintf(stderr, "[RDPQ_VALIDATION] SET_TEX_IMAGE last sent at %p\n", rdp.last_tex);
if (flags & EMIT_CTX_TILES) {
for (int i = 0; i < 8; i++) {
if (flags & EMIT_CTX_TILE(i)) {
if (flags & EMIT_CTX_TILESIZE)
fprintf(stderr, "[RDPQ_VALIDATION] %s last sent at %p\n",
CMD(rdp.tile[i].last_setsize_data) == 0x32 ? "SET_TILE_SIZE" : "LOAD_TILE",
rdp.tile[i].last_setsize);
else
fprintf(stderr, "[RDPQ_VALIDATION] SET_TILE last sent at %p\n", rdp.tile[i].last_settile);
break;
}
}
}
#ifdef N64
// On a real N64, let's assert on RDP crashes. This makes them very visible to everybody,
// including people that don't have the debugging log on.
// We just dump the message here, more information are in the log.
if ((flags & EMIT_TYPE) == EMIT_CRASH) {
char buf[1024];
va_start(args, msg);
vsprintf(buf, msg, args);
va_end(args);
assertf(0, "RDP CRASHED: the code triggered a RDP hardware bug.\n%s", buf);
}
#endif
}
/** @brief Internal validation macros (for both errors and warnings) */
#define __VALIDATE(flags, cond, msg, ...) ({ \
if (!(cond)) validate_emit_error(flags, msg "\n", ##__VA_ARGS__); \
})
/**
* @brief Check and trigger a RDP crash.
*
* This is the most fatal error condition, in which the RDP chip freezes and stop processing
* commands until reboot.
*/
#define VALIDATE_CRASH(cond, msg, ...) __VALIDATE(0, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a crash, with SOM context */
#define VALIDATE_CRASH_SOM(cond, msg, ...) __VALIDATE(4, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a crash, with CC context */
#define VALIDATE_CRASH_CC(cond, msg, ...) __VALIDATE(8, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a crash, with SET_TEX_IMAGE context */
#define VALIDATE_CRASH_TEX(cond, msg, ...) __VALIDATE(16, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a crash, with tile context */
#define VALIDATE_CRASH_TILE(cond, tidx, msg, ...) __VALIDATE(EMIT_CRASH | EMIT_CTX_TILE(tidx), cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a crash, with tile extents context */
#define VALIDATE_CRASH_TILESIZE(cond, tidx, msg, ...) __VALIDATE(EMIT_CRASH | EMIT_CTX_TILE(tidx) | EMIT_CTX_TILESIZE, cond, msg, ##__VA_ARGS__)
/**
* @brief Check and trigger a RDP validation error.
*
* This should be triggered only whenever the commands rely on an undefined hardware
* behaviour or in general strongly misbehave with respect to the reasonable
* expectation of the programmer. Typical expected outcome on real hardware should be
* garbled graphcis. */
#define VALIDATE_ERR(cond, msg, ...) __VALIDATE(1, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger an error, with SOM context */
#define VALIDATE_ERR_SOM(cond, msg, ...) __VALIDATE(5, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger an error, with CC context */
#define VALIDATE_ERR_CC(cond, msg, ...) __VALIDATE(9, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger an error, with SET_TEX_IMAGE context */
#define VALIDATE_ERR_TEX(cond, msg, ...) __VALIDATE(17, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger an error, with tile context */
#define VALIDATE_ERR_TILE(cond, tidx, msg, ...) __VALIDATE(EMIT_ERROR | EMIT_CTX_TILE(tidx), cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger an error, with tile extents context */
#define VALIDATE_ERR_TILESIZE(cond, tidx, msg, ...) __VALIDATE(EMIT_ERROR | EMIT_CTX_TILE(tidx) | EMIT_CTX_TILESIZE, cond, msg, ##__VA_ARGS__)
/**
* @brief Check and trigger a RDP validation warning.
*
* This should be triggered whenever the commands deviate from standard practice or
* in general are dubious in their use. It does not necessarily mean that the RDP
* is going to misbehave but it is likely that the programmer did not fully understand
* what the RDP is going to do. It is OK to have false positives here -- if the situation
* becomes too unwiedly, we can later add a way to disable classes of warning in specific
* programs.
*/
#define VALIDATE_WARN(cond, msg, ...) __VALIDATE(2, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a warning, with SOM context */
#define VALIDATE_WARN_SOM(cond, msg, ...) __VALIDATE(6, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a warning, with CC context */
#define VALIDATE_WARN_CC(cond, msg, ...) __VALIDATE(10, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a warning, with SET_TEX_IMAGE context */
#define VALIDATE_WARN_TEX(cond, msg, ...) __VALIDATE(18, cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger an error, with tile context */
#define VALIDATE_WARN_TILE(cond, tidx, msg, ...) __VALIDATE(EMIT_WARN | EMIT_CTX_TILE(tidx), cond, msg, ##__VA_ARGS__)
/** @brief Validate and trigger a warning, with tile extents context */
#define VALIDATE_WARN_TILESIZE(cond, tidx, msg, ...) __VALIDATE(EMIT_WARN | EMIT_CTX_TILE(tidx) | EMIT_CTX_TILESIZE, cond, msg, ##__VA_ARGS__)
/**
* @brief Perform lazy evaluation of render target changes (color buffer and scissoring).
*/
static void lazy_validate_rendertarget(void) {
if (!rdp.rendertarget_changed) return;
rdp.rendertarget_changed = false;
VALIDATE_ERR(rdp.last_col,
"undefined behavior: drawing command before a SET_COLOR_IMAGE was sent");
VALIDATE_ERR(rdp.sent_scissor,
"undefined behavior: drawing command before a SET_SCISSOR was sent");
if (!rdp.last_col || !rdp.sent_scissor) return;
// copy/fill mode use inclusive X coordinates for most things, including scissor
int x1 = rdp.clip.x1;
if (rdp.som.cycle_type >= 2) x1++;
VALIDATE_WARN(rdp.clip.x0 < x1,
"drawing command with null scissor rectangle (X:%d-%d)", rdp.clip.x0, rdp.clip.x1);
VALIDATE_WARN(rdp.clip.y0 < rdp.clip.y1,
"drawing command with null scissor rectangle (Y:%d-%d)", rdp.clip.y0, rdp.clip.y1);
VALIDATE_WARN(rdp.clip.x1 <= rdp.col.width,
"drawing command with scissor rectangle (X1=%d) outside of color buffer (W=%d)", rdp.clip.x1, rdp.col.width);
if (rdp.col.height > 1) { // libdragon extension
VALIDATE_WARN(rdp.clip.y1 <= rdp.col.height,
"drawing command with scissor rectangle (Y1=%d) outside of color buffer (H=%d)", rdp.clip.y1, rdp.col.height);