forked from sm64-port/sm64-port
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast3d.s
1766 lines (1658 loc) · 47.4 KB
/
fast3d.s
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
.rsp
.include "rsp/rsp_defs.inc"
.include "rsp/gbi.inc"
// This file assumes DATA_FILE and CODE_FILE are set on the command line
.if version() < 110
.error "armips 0.11 or newer is required"
.endif
// Overlay table data member offsets
overlay_load equ 0x0000
overlay_len equ 0x0004
overlay_imem equ 0x0006
.macro OverlayEntry, loadStart, loadEnd, imemAddr
.dw loadStart
.dh (loadEnd - loadStart - 1) & 0xFFFF
.dh (imemAddr) & 0xFFFF
.endmacro
.macro jumpTableEntry, addr
.dh addr & 0xFFFF
.endmacro
// RSP DMEM
.create DATA_FILE, 0x0000
// 0x0000-0x0027: Overlay Table
overlayInfo0:
OverlayEntry orga(Overlay0Address), orga(Overlay0End), Overlay0Address
overlayInfo1:
OverlayEntry orga(Overlay1Address), orga(Overlay1End), Overlay1Address
overlayInfo2:
OverlayEntry orga(Overlay2Address), orga(Overlay2End), Overlay2Address
overlayInfo3:
OverlayEntry orga(Overlay3Address), orga(Overlay3End), Overlay3Address
overlayInfo4:
OverlayEntry orga(Overlay4Address), orga(Overlay4End), Overlay4Address
// 0x0028-0x009F: ??
.dw 0x0FFAF006
.dw 0x7FFF0000
.dw 0x00000001
.dw 0x0002FFFF
.dw 0x40000004
.dw 0x06330200
.dw 0x7FFFFFF8
.dw 0x00080040
.dw 0x00208000
.dw 0x01CCCCCC
.dw 0x0001FFFF
.dw 0x00010001
.dw 0x0001FFFF
.dw 0x00010001
.dw 0x00020002
.dw 0x00020002
// 0x0068
.dw 0x00020002
.dw 0x00020002
data0070:
.dw 0x00010000
// 0x0074
.dh 0x0000
// 0x0076
.dh 0x0001
// 0x0078
.dw 0x00000001
.dw 0x00000001
.dw 0x00010000
.dw 0x0000FFFF
.dw 0x00000001
.dw 0x0000FFFF
.dw 0x00000000
.dw 0x0001FFFF
.dw 0x00000000
.dw 0x00010001
// 0x00A0-0x00A1
lightEntry:
jumpTableEntry load_lighting
// 0x00A2-0x00A3: ??
.dh 0x7FFF
// 0x00A4-0x00B3: ??
.dw 0x571D3A0C
.dw 0x00010002
.dw 0x01000200
.dw 0x40000040
// 0x00B4
.dh 0x0000
// 0x00B6-0x00B7
taskDoneEntry:
jumpTableEntry overlay_4_entry
// 0x00B8
lower24Mask:
.dw 0x00FFFFFF
// 0x00BC: Operation Types
operationJumpTable:
jumpTableEntry dispatch_dma // cmds 0x00-0x3f
spNoopEntry:
jumpTableEntry SP_NOOP // cmds 0x40-0x7f
jumpTableEntry dispatch_imm // cmds 0x80-0xbf
jumpTableEntry dispatch_rdp // cmds 0xc0-0xff
// 0x00C4: DMA operations
dmaJumpTable:
jumpTableEntry SP_NOOP // 0x00
jumpTableEntry dma_MTX // 0x01
jumpTableEntry SP_NOOP // 0x02
jumpTableEntry dma_MOVEMEM // 0x03
jumpTableEntry dma_VTX // 0x04
jumpTableEntry SP_NOOP // 0x05
jumpTableEntry dma_DL // 0x06
jumpTableEntry SP_NOOP // 0x07
jumpTableEntry SP_NOOP // 0x08
jumpTableEntry SP_NOOP // 0x09
// 0x00D8: Immediate operations
immediateJumpTableBase equ (immediateJumpTable - ((0xB2 << 1) & 0xFE))
.ifdef F3D_OLD
jumpTableEntry imm_UNKNOWN
.endif
immediateJumpTable:
jumpTableEntry imm_RDPHALF_CONT // 0xB2
jumpTableEntry imm_RDPHALF_2 // 0xB3
jumpTableEntry imm_RDPHALF_1 // 0xB4
jumpTableEntry SP_NOOP // 0xB5?
jumpTableEntry imm_CLEARGEOMETRYMODE // 0xB6
jumpTableEntry imm_SETGEOMETRYMODE // 0xB7
jumpTableEntry imm_ENDDL // 0xB8
jumpTableEntry imm_SETOTHERMODE_L // 0xB9
jumpTableEntry imm_SETOTHERMODE_H // 0xBA
jumpTableEntry imm_TEXTURE // 0xBB
jumpTableEntry imm_MOVEWORD // 0xBC
jumpTableEntry imm_POPMTX // 0xBD
jumpTableEntry imm_CULLDL // 0xBE
jumpTableEntry imm_TRI1 // 0xBF
// 0x00F6: Label constants
labelLUT:
jumpTableEntry found_in
foundOutEntry:
jumpTableEntry found_out
jumpTableEntry found_first_in
jumpTableEntry found_first_out
clipDrawEntry:
jumpTableEntry clip_draw_loop
performClipEntry:
jumpTableEntry perform_clip
nextClipEntry:
jumpTableEntry next_clip
DMAWaitEntry:
jumpTableEntry dma_wait_dl
// 0x0106: ??
data0106:
.dh 0x0000
.ifdef F3D_NEW
.dh 0x0000
.endif
// 0x0108: DRAM pointer
dramPtr:
.dw 0x00000000
.dh 0x0000 // 0x10C: RDPHALF_2
.dh 0x0000
// 0x110: display list stack size
displayListStackSize:
.dh 0x0000
.dh 0xFFFF // 0x112: RDPHALF_1
.dw 0x00000000 // 0x114: geometrymode (bit 1 is texture ON)
.dw 0xEF080CFF // 0x118: othermode
.dw 0x00000000
.dw 0x00000000 // 0x120: texture max mipmap levels, tile descriptor enable/disable
.dh 0x0000 // 0x124: texture scaling factor S axis (horizontal) U16 fraction
.dh 0x0000 // 0x126: texture scaling factor T axis (vertical)
.dw 0x00000000 // 0x128: some dpc dma address state
numLights:
.dw 0x80000040 // 0x12c: num lights, bit 31 = needs init, bits 11:0 = (num_lights+1)*32
.dw 0x00000000 // 0x130: dram stack pointer 1
.dw 0x00000000 // 0x134: dram stack pointer modelview matrices
data0138:
.dw 0x40004000 // 0x138: txtatt (unused?)
.dw 0x00000000
.dw 0x00000000
.dw 0x00000000
.dw 0x00000000
.dw 0x00000000
.dw 0x00000000 // 0x150: output buffer
.dw 0x00000000 // 0x154: output buffer size
data0158:
.dh 0x0000 // 0x158: ??
.dh 0x0000
.dw 0x00000000 // 0x15c: dram stack end?
// 0x160-0x19f: RSP memory segment table
segmentTable:
.fill 0x40, 0
// 0x1a0: Lights
.dw 0x80000000
.dw 0x80000000
.dw 0x00000000
.dw 0x00000000
lookAtY: // 0x1b0: lookaty
.dw 0x00800000, 0x00800000, 0x7F000000, 0x00000000
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
lookAtX: // 0x1d0: lookatx
.dw 0x00000000, 0x00000000, 0x007F0000, 0x00000000
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
// 0x1f0: L0..L7 light info (32 bytes each)
lightInfo0: // 0x1f0
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
.dw 0x00000000, 0x00000000, 0xE0011FFF, 0x00040000
lightInfo1: // 0x210
.dw 0xFF000000, 0xFF000000, 0x00000000, 0x00000000
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
lightInfo2: // 0x230
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
lightInfo3: // 0x250
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
lightInfo4: // 0x270
// L4-L7 overlap with version string
.definelabel lightInfo5, lightInfo4 + 0x20 // 0x290
.definelabel lightInfo6, lightInfo5 + 0x20 // 0x2b0
.definelabel lightInfo7, lightInfo6 + 0x20 // 0x2d0
.if defined(F3D_OLD) || defined(VERSION_EU)
.asciiz "RSP SW Version: 2.0D, 04-01-96"
.elseif defined(F3D_NEW)
.asciiz "RSP SW Version: 2.0H, 02-12-97"
.endif
.asciiz "SGI U64 GFX SW TEAM: S Anderson, S Carr, H Cheng, K Luster, R Moore, N Pooley, A Srinivasan", 0x0A
.dw 0x00000000
// 0x2f0-0x31f: DMEM table
dmemTableOffset equ (dmemTable - 0x80)
dmemTable:
.dh viewport, lookAtY, lookAtX, lightInfo0, lightInfo1, lightInfo2, lightInfo3, lightInfo4
.dh lightInfo5, lightInfo6, lightInfo7, data0138, mpMatrix+0x10, mpMatrix+0x20, mpMatrix+0x30, mpMatrix
.dh numLights, data0070, segmentTable, fogFactors, lightInfo0, pointsBuffer
.ifdef F3D_NEW
.dh displayListStackSize, 0x0000
.else
.dh 0x0000, 0x0000
.endif
// 0x320: Viewport (0x010 bytes)
viewport:
.dw 0x00000000, 0x00000000, 0x00000000, 0x00000000
// 0x330: fog factors (three 16-bit integers: mul, add, min)
fogFactors:
.dh 0x0100, 0x0000, 0x00FF
// 0x336: display list stack (return addresses)
displayListStack: // this is not 4-byte aligned
.fill 0x2a, 0
// 0x360-0x39f: Modelview matrix top of stack (0x40 bytes)
modelViewMatrixStack:
.fill 0x40, 0
// 0x3a0-0x3df Projection Matrix top of stack (0x40 bytes)
projectionMatrixStack:
.fill 0x40, 0
// 0x3e0-0x41f: MP matrix (Modelview * Projection)
mpMatrix:
.fill 0x40, 0
// 0x420: Points buffer (0x280 bytes)
pointsBuffer:
.fill 0x280, 0
// 0x6a0-0x7df: input display list buffer
inputDisplayList:
.fill 0x140, 0
// 0x7E0-0x7ff: input data
inputData:
.fill (0x800 - 0x7E0), 0
.close // DATA_FILE
// uninitialized variables
.definelabel setupTemp, 0x08E0
.definelabel data08e4, 0x08E4
.definelabel data08e8, 0x08E8
.definelabel data08ec, 0x08EC
.definelabel data08f0, 0x08F0
.definelabel clipTemp, 0x0940
.definelabel data0942, 0x0942
.definelabel data0944, 0x0944
.definelabel data0946, 0x0946
.definelabel rdpOutput, 0x09E0
.definelabel scratchSpace, 0x0DE0
.definelabel data0DE4, 0x0DE4
.definelabel data0DE8, 0x0DE8
.create CODE_FILE, 0x04001080
// Overlay 0
Overlay0Address:
j f3d_04001780
addi $29, $zero, displayListStackSize
jal segmented_to_physical
add $19, $24, $zero
add $20, $zero, $22
jal dma_read_write
addi $17, $zero, 0x00
// $1 = most significant 2 bits of cmd byte << 1
// $25 = first command word
dispatch_task:
lh $2, (operationJumpTable)($1)
jr $2
srl $2, $25, 23 // $2 = MSbyte << 1
SP_NOOP:
mfc0 $2, SP_STATUS
andi $2, $2, 0x0080
bne $2, $zero, f3d_040010cc
lh $21, 0x26($zero)
f3d_040010b8:
bgtz $28, read_next_task_entry
nop
j load_display_list_dma
lh $ra, DMAWaitEntry
f3d_040010c8:
lh $21, taskDoneEntry
f3d_040010cc:
j load_overlay
ori $30, $zero, overlayInfo4
load_display_list_dma:
addi $28, $zero, 0x0140 // size of display list
add $21, $zero, $ra
addi $20, $zero, inputDisplayList
add $19, $zero, $26 // TASK_DATA_PTR
addi $18, $zero, 0x013f
jal dma_read_write
addi $17, $zero, 0x00
jr $21
addi $27, $zero, inputDisplayList // initial pointer
// load overlay into IMEM
// $30 = offset into overlay table
// $21 = return address
load_overlay_fcn:
add $21, $zero, $ra
load_overlay:
lw $19, overlay_load($30)
lh $18, overlay_len($30)
lh $20, overlay_imem($30)
jal dma_read_write
addi $17, $zero, 0x00
jal wait_while_dma_busy
nop
jr $21
segmented_to_physical:
lw $11, lower24Mask
srl $12, $19, 22
andi $12, $12, 0x3c
and $19, $19, $11
add $13, $zero, $12
lw $12, 0x0160($13)
jr $ra
add $19, $19, $12
// $20 = SP_MEM address
// $19 = DRAM address
// $18 = length - 1
// $17 = 1:write, 0:read
dma_read_write:
@@dma_full:
mfc0 $11, SP_DMA_FULL
bne $11, $zero, @@dma_full
nop
mtc0 $20, SP_MEM_ADDR
bgtz $17, @@dma_write
mtc0 $19, SP_DRAM_ADDR
jr $ra
mtc0 $18, SP_RD_LEN
@@dma_write:
jr $ra
mtc0 $18, SP_WR_LEN
wait_while_dma_busy:
mfc0 $11, SP_DMA_BUSY
bne $11, $zero, wait_while_dma_busy
nop
jr $ra
nop
f3d_04001178: // sends stuff to RDP
add $21, $zero, $ra
lw $19, 0x18($29)
addi $18, $23, -0x09e0
lw $23, 0x44($29)
blez $18, f3d_040011f4
add $20, $19, $18
sub $20, $23, $20
bgez $20, f3d_040011b8
f3d_04001198:
mfc0 $20, DPC_STATUS
andi $20, $20, DPC_STATUS_START_VALID
bne $20, $zero, f3d_04001198
f3d_040011a4:
mfc0 $23, DPC_CURRENT
lw $19, 0x40($29)
beq $23, $19, f3d_040011a4
nop
mtc0 $19, DPC_START
f3d_040011b8:
mfc0 $23, DPC_CURRENT
sub $20, $19, $23
bgez $20, f3d_040011d4
add $20, $19, $18
sub $20, $20, $23
bgez $20, f3d_040011b8
nop
f3d_040011d4:
add $23, $19, $18
addi $18, $18, -0x01
addi $20, $zero, rdpOutput // output to RDP
jal dma_read_write
addi $17, $zero, 0x01
jal wait_while_dma_busy
sw $23, 0x18($29)
mtc0 $23, DPC_END
f3d_040011f4:
jr $21
addi $23, $zero, rdpOutput
// codes 0x80-0xBF
// $2 = immediate cmd byte << 1
dispatch_imm:
andi $2, $2, 0x00fe
lh $2, (immediateJumpTableBase)($2) // data IMM offset
jr $2
lbu $1, -0x01($27)
imm_TRI1:
lbu $5, -0x04($27)
lbu $1, -0x03($27)
lbu $2, -0x02($27)
lbu $3, -0x01($27)
sll $5, $5, 2
sll $1, $1, 2
sll $2, $2, 2
sll $3, $3, 2
addi $1, $1, pointsBuffer
addi $2, $2, pointsBuffer
addi $3, $3, pointsBuffer
sw $1, scratchSpace
sw $2, data0DE4
sw $3, data0DE8
lw $4, (scratchSpace)($5)
j f3d_04001998
lh $30, spNoopEntry
imm_POPMTX:
.ifdef F3D_NEW
sbv $v31[6], 0x1c($29)
.endif
lw $19, 0x24($29)
lw $3, 0x4c($29)
addi $20, $zero, modelViewMatrixStack
addi $18, $zero, 0x3f
sub $3, $3, $19
addi $3, $3, -0x0280
bgez $3, SP_NOOP // stop if stack is empty
addi $19, $19, -0x40
jal dma_read_write // read new top from DRAM
addi $17, $zero, 0x00
jal wait_while_dma_busy
addi $3, $zero, mpMatrix // MP matrix (modelview * projection)
j f3d_04001444 // recompute MP matrix
sw $19, 0x24($29)
imm_MOVEWORD:
lbu $1, -0x05($27)
lhu $2, -0x07($27)
lh $5, 0x030e($1)
add $5, $5, $2
j SP_NOOP
sw $24, 0x00($5)
imm_TEXTURE:
sw $25, 0x10($29)
sw $24, 0x14($29)
lh $2, 0x06($29)
andi $2, $2, 0xfffd
andi $3, $25, 0x0001
sll $3, $3, 1
or $2, $2, $3
j SP_NOOP
sh $2, 0x06($29)
imm_SETOTHERMODE_H:
j @f3d_040012d0
addi $7, $29, 8
imm_SETOTHERMODE_L:
addi $7, $29, 0x0c
@f3d_040012d0:
lw $3, 0x00($7)
addi $8, $zero, -1
lbu $5, -0x05($27)
lbu $6, -0x06($27)
addi $2, $zero, 1
sllv $2, $2, $5
addi $2, $2, -1
sllv $2, $2, $6
xor $2, $2, $8
and $2, $2, $3
or $3, $2, $24
sw $3, 0x00($7)
lw $25, 0x08($29)
j f3d_040013a8
lw $24, 0x0c($29)
imm_CULLDL:
andi $25, $25, 0x03ff
.ifdef F3D_OLD
ori $2, $zero, 0xffff
.else
ori $2, $zero, 0x7070
.endif
@@f3d_04001314:
lh $3, 0x0444($25)
addi $25, $25, 0x28
bne $25, $24, @@f3d_04001314
and $2, $2, $3
beq $2, $zero, SP_NOOP
DL_STACK_SIZE_OFFSET equ (defined(F3D_OLD) ? 0x00 : 0x4A)
imm_ENDDL:
lb $2, (DL_STACK_SIZE_OFFSET)($29)
addi $2, $2, -4
bltz $2, f3d_040010c8
addi $3, $2, displayListStack
lw $26, 0x00($3)
sb $2, (DL_STACK_SIZE_OFFSET)($29)
j SP_NOOP
addi $28, $zero, 0
imm_SETGEOMETRYMODE:
lw $2, 0x04($29)
or $2, $2, $24
j SP_NOOP
sw $2, 0x04($29)
imm_CLEARGEOMETRYMODE:
lw $2, 0x04($29)
addi $3, $zero, -1
xor $3, $3, $24
and $2, $2, $3
j SP_NOOP
sw $2, 0x04($29)
.ifdef F3D_OLD
imm_RDPHALF_1:
j SP_NOOP
sh $24, 0x02($29)
imm_RDPHALF_2:
.else
imm_RDPHALF_1:
.endif
j f3d_040010b8
sw $24, -0x04($29)
.ifdef F3D_OLD
imm_UNKNOWN:
.else
imm_RDPHALF_CONT:
.endif
ori $2, $zero, 0x0000
.ifdef F3D_OLD
imm_RDPHALF_CONT:
.else
imm_RDPHALF_2:
.endif
j f3d_040013a8
lw $25, -0x04($29)
// codes 0xC0-0xFF
dispatch_rdp:
sra $2, $25, 24
addi $2, $2, 3
bltz $2, f3d_040013a8
addi $2, $2, 0x18
jal segmented_to_physical
add $19, $24, $zero
add $24, $19, $zero
f3d_040013a8:
sw $25, 0x00($23)
sw $24, 0x04($23)
jal f3d_04001178
addi $23, $23, 0x08
bgtz $2, SP_NOOP
nop
j f3d_040010b8
dispatch_dma:
andi $2, $2, 0x01fe
lh $2, (dmaJumpTable)($2)
jal wait_while_dma_busy
lbu $1, -0x07($27)
jr $2
andi $6, $1, 0x000f
dma_MTX:
sbv $v31[6], 0x1c($29) // lights need re-init
andi $8, $1, 0x0001 // 1=projection, 0=modelview
bne $8, $zero, f3d_04001454
andi $7, $1, 0x0002 // 1=load, 0=multiply
addi $20, $zero, modelViewMatrixStack
andi $8, $1, 0x0004 // 1=push, 0=no push
beq $8, $zero, f3d_04001420
lqv $v26[0], 0x30($22)
lw $19, 0x24($29) // DRAM stack pointer 2
lw $8, 0x4c($29) // DRAM stack end
addi $17, $zero, 1
addi $1, $19, 0x40
beq $19, $8, f3d_04001420
addi $12, $zero, 0x3f // BUG: wrong register, should be $18
jal dma_read_write
sw $1, 0x24($29)
jal wait_while_dma_busy
f3d_04001420:
lqv $v28[0], 0x10($22)
beq $7, $zero, f3d_04001460
lqv $v27[0], 0x20($22)
sqv $v26[0], 0x30($20)
lqv $v29[0], 0x00($22)
sqv $v28[0], 0x10($20)
f3d_04001438:
addi $3, $zero, mpMatrix
sqv $v27[0], 0x20($20)
sqv $v29[0], 0x00($20)
f3d_04001444:
addi $1, $zero, modelViewMatrixStack
addi $2, $zero, projectionMatrixStack
j f3d_04001484
lh $ra, spNoopEntry
f3d_04001454:
lqv $v26[0], 0x30($22)
j f3d_04001420
addi $20, $zero, projectionMatrixStack
f3d_04001460:
addiu $3, $zero, scratchSpace
addu $1, $zero, $22 // input matrix from user
jal f3d_04001484
addu $2, $zero, $20 // current P matrix or M top
sqv $v6[0], 0x30($20) // store result to P or M
sqv $v5[0], 0x10($20)
lqv $v27[0], 0x00($3)
j f3d_04001438
lqv $v29[0], -0x20($3)
f3d_04001484:
addi $19, $3, 0x10
f3d_04001488:
vmudh $v5, $v31, $v31[0] // clear accumulator and $v5
addi $18, $1, 8
f3d_04001490:
ldv $v3[0], 0x00($2)
ldv $v4[0], 0x20($2)
lqv $v1[0], 0x00($1)
lqv $v2[0], 0x20($1)
ldv $v3[8], 0x00($2)
ldv $v4[8], 0x20($2)
vmadl $v6, $v4, $v2[0h]
addi $1, $1, 2
vmadm $v6, $v3, $v2[0h]
addi $2, $2, 8
vmadn $v6, $v4, $v1[0h]
vmadh $v5, $v3, $v1[0h]
bne $1, $18, f3d_04001490
vmadn $v6, $v31, $v31[0]
addi $2, $2, -0x20
addi $1, $1, 8
sqv $v5[0], 0x00($3)
sqv $v6[0], 0x20($3)
bne $3, $19, f3d_04001488
addi $3, $3, 0x10
jr $ra
nop
f3d_040014e8:
addi $8, $zero, viewport
lqv $v3[0], 0x50($zero)
lsv $v19[0], 0x02($29) // RDPHALF_1, contains persp normalize
lh $3, 0x04($29) // geometrymode
ldv $v0[0], 0x00($8) // viewport scale
ldv $v1[0], 0x08($8) // viewport translate
ldv $v0[8], 0x00($8)
ldv $v1[8], 0x08($8)
jr $ra
vmudh $v0, $v0, $v3 // negate Y?
load_mp_matrix:
addi $8, $zero, mpMatrix
ldv $v11[0], 0x18($8) // load into $v8-v15, dup lower half and higher half
ldv $v11[8], 0x18($8) // $v8-v11 integer parts, $v12-v15 frac parts
ldv $v15[0], 0x38($8)
ldv $v15[8], 0x38($8)
f3d_04001524:
ldv $v8[0], 0x00($8)
ldv $v9[0], 0x08($8)
ldv $v10[0], 0x10($8)
ldv $v12[0], 0x20($8)
ldv $v13[0], 0x28($8)
ldv $v14[0], 0x30($8)
ldv $v8[8], 0x00($8)
ldv $v9[8], 0x08($8)
ldv $v10[8], 0x10($8)
ldv $v12[8], 0x20($8)
ldv $v13[8], 0x28($8)
jr $ra
ldv $v14[8], 0x30($8)
dma_MOVEMEM:
lqv $v0[0], 0x00($22)
lh $5, (dmemTableOffset)($1)
j SP_NOOP
sqv $v0[0], 0x00($5)
dma_VTX:
lh $8, spNoopEntry
sh $8, data0106
srl $1, $1, 4
addi $5, $1, 1 // num vertex
addi $9, $5, 0
ldv $v2[0], 0x00($22) // input data
ldv $v2[8], 0x10($22) // load 2nd vertex (assuming it exists)
addi $7, $zero, pointsBuffer
sll $8, $6, 5 // dest index
sll $6, $6, 3
add $8, $6, $8 // 40 bytes per vertex
jal f3d_040014e8
add $7, $7, $8
llv $v17[0], 0x14($29) // texture scaling
jal load_mp_matrix
llv $v17[8], 0x14($29)
@f3d_040015a8:
vmudn $v28, $v12, $v2[0h] // x * first row frac
llv $v18[0], 0x08($22)
vmadh $v28, $v8, $v2[0h] // x * first row int
lw $15, 0x0c($22) // XR, YG, ZB, AA
vmadn $v28, $v13, $v2[1h] // y * second row frac
lw $16, 0x1c($22)
vmadh $v28, $v9, $v2[1h] // y * second row int
andi $1, $3, G_LIGHTING_H
vmadn $v28, $v14, $v2[2h] // z * third row frac
vmadh $v28, $v10, $v2[2h] // z * third row int
vmadn $v28, $v15, $v31[1] // 1 * fourth row frac
llv $v18[8], 0x18($22)
vmadh $v29, $v11, $v31[1] // 1 * fourth row int
bne $1, $zero, load_lighting
addi $22, $22, 0x20 // next 2 vertices
@f3d_040015e4:
vmudm $v18, $v18, $v17 // U *= S scale, V *= T scale (result >> 16)
@f3d_040015e8:
lsv $v21[0], 0x76($zero)
vmudn $v20, $v28, $v21[0]
vmadh $v21, $v29, $v21[0]
vch $v3, $v29, $v29[3h] // do trivial clip rejection
vcl $v3, $v28, $v28[3h] // by comparing xyz with w
cfc2 $13, vcc
vch $v3, $v29, $v21[3h]
vcl $v3, $v28, $v20[3h]
andi $8, $13, 0x0707 // filter out xyz clip result for 1st vertex
andi $13, $13, 0x7070 // filter out xyz clip result for 2nd vertex
sll $8, $8, 4
sll $13, $13, 16
or $13, $13, $8
cfc2 $14, vcc
andi $8, $14, 0x0707
vadd $v21, $v29, $v31[0]
andi $14, $14, 0x7070
vadd $v20, $v28, $v31[0]
sll $14, $14, 12
vmudl $v28, $v28, $v19[0] // persp normalize, used to improve precision
or $8, $8, $14
vmadm $v29, $v29, $v19[0]
or $8, $8, $13
vmadn $v28, $v31, $v31[0]
sh $8, 0x24($7)
jal f3d_04001000 // compute 1/w
lh $13, -0x1a($22) // $13 unused?
vge $v6, $v27, $v31[0] // 1/w >= 0?
sdv $v21[0], 0x00($7) // store xyzw int
vmrg $v6, $v27, $v30[0]
sdv $v20[0], 0x08($7) // store xyzw frac
vmudl $v5, $v20, $v26[3h] // mul xyzw with 1/w
vmadm $v5, $v21, $v26[3h]
vmadn $v5, $v20, $v6[3h]
vmadh $v4, $v21, $v6[3h]
addi $9, $9, -1 // decrement vertex input count
vmudl $v5, $v5, $v19[0] // take away persp normalize factor
vmadm $v4, $v4, $v19[0]
vmadn $v5, $v31, $v31[0]
andi $12, $3, G_FOG_H
ldv $v2[0], 0x00($22) // pre-load next vertices from input
vmudh $v7, $v1, $v31[1] // viewport translate * 0001
ldv $v2[8], 0x10($22)
vmadn $v7, $v5, $v0 // viewport scale
ldv $v29[0], 0x28($zero)
vmadh $v6, $v4, $v0
ldv $v29[8], 0x28($zero)
vmadn $v7, $v31, $v31[0] // $v6$v7 contains vertex results after viewport
vge $v6, $v6, $v29[1q] // some saturating 0FFA-F006
sw $15, 0x10($7)
beq $12, $zero, @@f3d_040016e0 // skip fog?
vlt $v6, $v6, $v29[0q]
lqv $v3[0], 0x0330($zero)
vmudn $v5, $v5, $v3[0] // mul fog factor (default 1)
vmadh $v4, $v4, $v3[0]
vadd $v4, $v4, $v3[1] // add parameter (default 0)
vge $v4, $v4, $v31[0]
vlt $v4, $v4, $v3[2] // min parameter (default 0xff)
sbv $v4[5], 0x13($7) // high z for 1st vertex, store in AA
sw $16, 0x18($7)
sbv $v4[13], 0x1b($7) // high z for 2nd vertex, store in AA
lw $16, 0x18($7)
@@f3d_040016e0:
slv $v18[0], 0x14($7) // texture coordinates, 1st vertex
sdv $v6[0], 0x18($7) // xyz_int after viewport
ssv $v7[4], 0x1e($7) // z_frac after viewport
ssv $v27[6], 0x20($7) // 1/w
ssv $v26[6], 0x22($7)
blez $9, @@f3d_04001728
addi $9, $9, -1 // decrement vertex input counter again
sdv $v21[8], 0x28($7)
sdv $v20[8], 0x30($7)
slv $v18[8], 0x3c($7) // texture coordinates, 2nd vertex
sw $16, 0x38($7)
sdv $v6[8], 0x40($7)
ssv $v7[12], 0x46($7)
ssv $v27[14], 0x48($7)
ssv $v26[14], 0x4a($7)
sw $8, 0x4c($7) // puts high hword first
addi $7, $7, 0x50
bgtz $9, @f3d_040015a8
@@f3d_04001728:
lh $8, data0106
jr $8
nop
dma_DL:
bgtz $1, @@f3d_04001754 // 0=store ret addr, 1=end DL after branch
lb $2, (DL_STACK_SIZE_OFFSET)($29)
addi $4, $2, -0x24 // DL stack full?
bgtz $4, SP_NOOP
addi $3, $2, displayListStack
addi $2, $2, 4
sw $26, 0x00($3) // store return address on DL stack
sb $2, (DL_STACK_SIZE_OFFSET)($29)
@@f3d_04001754:
jal segmented_to_physical
add $19, $24, $zero
add $26, $19, $zero
j SP_NOOP
addi $28, $zero, 0x00
// Overlays 2-4 will overwrite the following code
.org 0x04001768
f3d_04001768:
ori $30, $zero, overlayInfo2
b load_overlay
lh $21, performClipEntry
load_lighting:
ori $30, $zero, overlayInfo3
b load_overlay
lh $21, lightEntry
f3d_04001780:
ori $2, $zero, 0x2800 // clear yielded, clear taskdone
mtc0 $2, SP_STATUS
lqv $v31[0], 0x30($zero)
lqv $v30[0], 0x40($zero)
lw $4, OSTask_addr + OSTask_flags
andi $4, $4, 0x0001
bne $4, $zero, @@f3d_04001870
nop
lw $23, 0x28($1) // task output buff
lw $3, 0x2c($1) // task output buff size
sw $23, 0x40($29)
sw $3, 0x44($29)
mfc0 $4, DPC_STATUS
andi $4, $4, DPC_STATUS_XBUS_DMA
bne $4, $zero, @@f3d_040017e4
mfc0 $4, DPC_END
sub $23, $23, $4
bgtz $23, @@f3d_040017e4
mfc0 $5, DPC_CURRENT
beq $5, $zero, @@f3d_040017e4
nop
beq $5, $4, @@f3d_040017e4
nop
j @@f3d_04001800
ori $3, $4, 0x0000
@@f3d_040017e4:
mfc0 $4, DPC_STATUS
andi $4, $4, DPC_STATUS_START_VALID
bne $4, $zero, @@f3d_040017e4
addi $4, $zero, DPC_STATUS_CLR_XBUS
mtc0 $4, DPC_STATUS
mtc0 $3, DPC_START
mtc0 $3, DPC_END
@@f3d_04001800:
sw $3, 0x18($29)
addi $23, $zero, rdpOutput
lw $5, 0x10($1) // TASK_UCODE (DRAM address)
lw $2, overlayInfo1
lw $3, overlayInfo2
lw $4, overlayInfo3
lw $6, overlayInfo4
add $2, $2, $5 // apply DRAM offset
add $3, $3, $5
add $4, $4, $5
add $6, $6, $5
sw $2, overlayInfo1 // store back with DRAM offsets
sw $3, overlayInfo2
sw $4, overlayInfo3
sw $6, overlayInfo4
jal load_overlay_fcn
addi $30, $zero, overlayInfo1
jal load_display_list_dma
lw $26, 0x30($1) // TASK_DATA_PTR
lw $2, 0x20($1) // TASK_DRAM_STACK
sw $2, 0x20($29)
sw $2, 0x24($29)
addi $2, $2, 0x0280 // end of stack?
sw $2, 0x4c($29)
lw $2, -0x08($zero) // TASK_YIELD_DATA_PTR
sw $2, dramPtr
j dma_wait_dl
nop
@@f3d_04001870:
jal load_overlay_fcn
addi $30, $zero, overlayInfo1
lw $23, data08F0
lw $28, data08E4
lw $27, data08E8
j SP_NOOP
lw $26, data08EC
// 0x0400188c-0x04001987: bunch of nops
.fill 0xfc, 0
.ifdef F3D_OLD
.fill 16, 0
.endif
// from G_TRI1
f3d_04001998:
lh $11, 0x24($3)
lh $8, 0x24($2)
lh $9, 0x24($1)
and $12, $11, $8
or $11, $11, $8
and $12, $12, $9
andi $12, $12, 0x7070
bne $12, $zero, SP_NOOP // all vertices outside screen, return
or $11, $11, $9
andi $11, $11, 0x4343