-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtapetext.s
2344 lines (2030 loc) · 47.8 KB
/
tapetext.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
IFD DEMOSYSTEM
IFD STANDALONE
ELSE
STANDALONE equ 1
ENDC
ELSE
STANDALONE equ 0
ENDC
IFEQ STANDALONE
TRUE equ 0
FALSE equ 1
true equ 0
false equ 1
ENDC
useblitter equ 1
optdraw equ true
number_of_vertices_text equ 4
sintable_size_tapetext equ 512*2 ; 512 entries of wordsize
; optimizations
OPT_SMC equ 0
OPT_C2P equ 0
OPT_A7 equ 1
TT_EFFECT_WIDTH equ 128
TT_EFFECT_HEIGHT equ 128
_sinA equr d1
_cosA equr d2
_sinB equr d3
_cosB equr d4
_sinC equr d5
_cosC equr d6
incdir gfx
incdir lib
incdir msx
incdir res/smfx
section DATA
PREPTAPETEXT equ 1
IFEQ STANDALONE
include macro.s
initAndRun init_effect
init_effect
IFEQ PREPTAPETEXT
jsr prepTapeText
ENDC
jsr init_demo
jsr init_tapetext
move.w #300,effect_vbl_counter
jsr tapetext_mainloop
init_demo
move.l #memBase+65536,d0
sub.w d0,d0
move.l d0,screenpointer
move.l d0,screen1
add.l #$10000,d0
move.l d0,screenpointer2
move.l d0,screen2
rts
ENDC
init_tapetext_pointers
move.l screen1,d0
; move.l d0,screenpointer
add.l #$8000,d0
IFNE PREPTAPETEXT
move.l d0,tapeBufferPointer
ENDC
move.l screen2,d0
; move.l d0,screenpointer2
add.l #$10000,d0
move.l d0,texturepointer
add.l #$11000,d0
move.l d0,canvasPointer_text
add.l #$10000-$1000,d0
; add the pointers for the moving tape here
; move.l tape
add.l #$60000,d0
move.l d0,divtablepointer
add.l #$10000,d0
move.l d0,tapePicBufferPointer
move.w #$4e75,init_tapetext_pointers
rts
init_tapetext
; pointers
jsr init_tapetext_pointers
move.l screen1,screenpointer
move.l screen2,screenpointer2
; add.l #$10000,d0
; move.l #canvas,canvasPointer_text
move.w #$777,timer_b_open_curtain+2
jsr initDivTable
jsr initDivTable2
IFNE PREPTAPETEXT
lea tapetextcrk,a0
move.l tapeBufferPointer,a1
jsr cranker
ENDC
IFNE STANDALONE
lea tapecrk,a0
move.l tapePicBufferPointer,a1
jsr cranker
ENDC
jsr planarToChunky_text
jsr generateOptimizedTabs
jsr clearCanvas_text
; jsr makeBorder
move.w #0,vblcount
move.l screen1,a0
move.l screen2,a1
add.l #200*160,a0
add.l #200*160,a1
move.w #40-1,d7
moveq #0,d0
.ll
REPT 40
move.l d0,(a0)+
move.l d0,(a1)+
ENDR
dbra d7,.ll
; movem.l tapeTextPal+7*32+2,d0-d7
; movem.l tapetext+4,d0-d7
; movem.l d0-d6,$ffff8242
; swap d7
; move.w d7,$ffff8240+15*2
; init div table
; jsr initDivTable
; init music
move.w #0,_currentStepX
move.w #0,_currentStepY
move.w #0,_currentStepZ
; move.w #$777,$ffff8240+15*2
move.w #TAPETEXT_INTRO_DELAY_VBL,d7
.www
wait_for_vbl
dbra d7,.www
move.w #$2700,sr
move.l #tapetext_vbl,$70
move.w #$2300,sr
rts
;genXOR
; moveq #0,d1
;; lea texture,a0
; move.l texturepointer,a0
; move.l a0,a1
; move.w #256-1,d7 ;y 256 height
; move.w #$f,d4
;.oloop
; move.w #256-1,d6 ;x 256 width
; moveq #0,d0 ;x
;.iloop
; move.w d0,d5
; eor.w d1,d5
; IFEQ OPT_C2P
; lsl.w #3,d5
; addq.w #6,d5
; ELSE
; lsl.w #2,d5
; ENDC
; move.b d5,(a0)+
; addq.w #1,d0
; and.w d4,d0
; dbra d6,.iloop
; addq.w #1,d1
; and.w d4,d1
; dbra d7,.oloop
;
; move.w #16-1,d7
; move.l #$06060606,d0
;.l
; REPT 256
; move.l d0,(a0)+
; move.l d0,-(a1)
; ENDR
; dbra d7,.l
;
;
; rts
;makeBorder
;
; move.l screenpointer2,a0
; move.l screenpointer,a1
; move.w #200-1,d7
; moveq #-1,d0
;.ol
; move.w #20-1,d6
;.il
; move.l d0,(a0)+
; move.l d0,(a0)+
; move.l d0,(a1)+
; move.l d0,(a1)+
; dbra d6,.il
; dbra d7,.ol
; rts
tapetext_mainloop
move.w #0,$466.w
.mainloop
tst.w $466.w
beq .mainloop
move.w #0,$466.w
addq.w #1,effectcount
; move.w #$030,$ffff8240
jsr clearCanvas_text
; move.w #$300,$ffff8240
jsr calculateRotatedProjection
; move.w #$003,$ffff8240
lea trianglePointers,a0
jsr drawTriangle2
; lea trianglePointers+12,a0
jsr drawTriangle2
; move.w #$500,$ffff8240
jsr c2p_2to4_optimized
; move.w #$050,$ffff8240
jsr copyLines
; move.w #$005,$ffff8240
move.l screenpointer2,a0
add.w #112,a0
moveq #0,d0
; move.w #-1,d0
; swap d0
REPT 7
move.l d0,-(a0)
move.l d0,-(a0)
ENDR
move.l screenpointer2,$ffff8200
move.l screenpointer2,d0
move.l screenpointer,screenpointer2
move.l d0,screenpointer
; move.w #$300,$ffff8240
cmp.w #119,vblcount
ble .kkk
rts
moveq #0,d0
moveq #0,d1
move.w vblcount,d0
move.w effectcount,d1
jmp tapetext_mainloop2
; move.b #0,$ffffc123
.kkk
tst.w leaveTapeText
bne .end
jmp .mainloop
.end
rts
tapetext_mainloop2
move.w #$0,466.w
.mainloop
tst.w $466.w
beq .mainloop
move.w #0,$466.w
addq.w #1,effectcount
; move.w #-1,doCopyTape
tst.w leaveTapeText
bne .end
jmp .mainloop
.end
rts
doCopyTape dc.w 0
tapetext_vbl
addq.w #1,$466.w
addq.w #1,cummulativeCount
addq.w #1,vblcount
move.w #0,$ffff8240
; clr.b $fffffa1b.w ;Timer B control (stop)
; bset #0,$fffffa07.w ;Interrupt enable A (Timer B)
; bset #0,$fffffa13.w ;Interrupt mask A (Timer B)
; move.b #1,$fffffa21.w ;Timer B data (number of scanlines to next interrupt)
; move.l #timer_b_open_curtain,$120.w
; bclr #3,$fffffa17.w ;Automatic end of interrupt
; move.b #8,$fffffa1b.w
;Start up Timer B each VBL
clr.b $fffffa1b.w ;Timer B control (stop)
bset #0,$fffffa07.w ;Interrupt enable A (Timer B)
bset #0,$fffffa13.w ;Interrupt mask A (Timer B)
move.l #timer_b_open_curtain_stable,$120.w
move.b #191,$fffffa21.w ;Timer B data
move.b #4,$fffffa1b.w ;Timer B control (delay mode)
bclr #3,$fffffa17.w ;Automatic end of interrupt
subq.w #1,.fadeC
bge .nof
pushall
move.w #5,.fadeC
sub.w #32,tapeTextPalOff
bge .kkkk
move.w #0,tapeTextPalOff
.kkkk
lea tapeTextPal,a0
add.w tapeTextPalOff,a0
movem.l 2(a0),d0-d7
movem.l d0-d6,$ffff8240+2
swap d7
move.w d7,$ffff8240+15*2
popall
.nof
subq.w #1,effect_vbl_counter
bge .kkk
move.w #-1,leaveTapeText
.kkk
tst.w doCopyTape
beq .nocopy
IFNE STANDALONE
; move.b #0,$ffffc123
move.l screenpointer,$ffff8200
swapscreens
subq.w #1,.doIt
blt .skip
move.l screenpointer,a0
jsr copyTape
movem.l tapePal+2,d0-d7
movem.l d0-d6,$ffff8240+2*1
swap d7
move.w d7,$ffff8240+2*15
.skip
ENDC
.nocopy
IFNE STANDALONE
pushall
jsr musicPlayer+8
popall
ENDC
rte
.fadeC dc.w 2
.doIt dc.w 2
leaveTapeText dc.w 0
IFEQ STANDALONE
timer_b_open_curtain
move.w #$111,$ffff8240
move.b #0,$fffffa1b.w ;Timer B control (stop)
move.b #199,$fffffa21.w ;Timer B data (number of scanlines to next interrupt)
move.l #timer_b_close_curtain,$120.w
move.b #8,$fffffa1b.w ;Timer B control (event mode (HBL))
rte
timer_b_close_curtain
move.w #$0,$ffff8240
rte
timer_b_open_curtain_stable
movem.l d1-d2/a0,-(sp)
move.w #$2100,sr
stop #$2100
move.w #$2700,sr
lea $ffff8209.w,a0 ;Hardsync
moveq #127,d1
.sync: tst.b (a0)
beq.s .sync
move.b (a0),d2
sub.b d2,d1
lsr.l d1,d1
clr.b $fffffa1b.w ;Timer B control (stop)
dcb.w 59-6,$4e71
move.w #$777,$ffff8240.w
movem.l (sp)+,d1-d2/a0
move.l #timer_b_close_curtain_stable,$120.w
move.b #198,$fffffa21.w ;Timer B data (number of scanlines to next interrupt)
move.b #8,$fffffa1b.w ;Timer B control (event mode (HBL))
rte
timer_b_close_curtain_stable:
move.w #$2700,sr
movem.l d0/a0,-(sp)
moveq #96,d0 ;Hardsync with branch offset
lea $ffff8209.w,a0
.sync: cmp.b (a0),d0
beq.s .sync
move.b (a0),d0
move.w d0,.jump+2 ;SMC bra.w below
.jump: bra.w timer_b_open_curtain_stable
.wait: dcb.w 134,$4e71
move.w #$000,$ffff8240.w
movem.l (sp)+,d0/a0
rte
ENDC
effectcount dc.w 0
;256 by 256 texture
; 16*8 = 128
planarToChunky_text
; lea tapetext+128,a0
move.l tapeBufferPointer,a0
; lea tapeTextBuffer,a0
move.l texturepointer,a1
move.l a1,a2
move.l #128-1,d6
move.l #$06060606,a3
; REPT 128*3
; move.l a3,-(a2)
; ENDR
.height
move.l #8-1,d7
.width
movem.w (a0)+,d0-d3 ; 4 words
REPT 16
moveq #0,d4
moveq #0,d5
roxl.w d5
roxl.w d3
roxl.w d4
roxl.w d2
roxl.w d4
roxl.w d1
roxl.w d4
roxl.w d0
roxl.w d4
lsl.w #3,d4
addq.w #6,d4
move.b d4,(a1)+ ; 128 * 8 * 16 = 16k
ENDR
dbra d7,.width
REPT 32
move.l a3,(a1)+
move.l a3,-(a2)
ENDR
; 320 width is 160 bytes
; 128 width is 64 bytes
; 160-64 added
; add.w #160-64,a0
dbra d6,.height ; 128 * ( 32*4 + 8*16 ) = 128 * (128+128) = 32768
; 16384*3 left
; move.w #64*3-1,d7
;.llll
; REPT 64
; move.l a3,(a1)+
; ENDR
; dbra d7,.llll
rts
canvaswidth equ 128
clearCanvas_text
move.l #$06060606,d0
move.l d0,d1
move.l d0,d2
move.l d0,d3
move.l d0,d4
move.l d0,d5
move.l d0,d6
move.l d0,d7
move.l d0,a0
move.l d0,a1
move.l d0,a2
move.l d0,a3
move.l d0,a4
move.l d0,a5
;SPACESAVE
; lea canvas,a6
move.l canvasPointer_text,a6
; 128 x 128 bytes = 16384 ;d0-a5 = 14*4 = 56 = 16352
.off set 0
REPT 292
movem.l d0-a5,.off(a6)
.off set .off+14*4
ENDR
;32 left
movem.l d0-d7,.off(a6)
rts
;void DrawTextureTriangle(vertex * vtx, char * bitmap)
; vertex * v1 = vtx;
; vertex * v2 = vtx+1;
; vertex * v3 = vtx+2;
; // sort stuff
; if(v1->y > v2->y) { vertex * v = v1; v1 = v2; v2 = v; }
; if(v1->y > v3->y) { vertex * v = v1; v1 = v3; v3 = v; }
; if(v2->y > v3->y) { vertex * v = v2; v2 = v3; v3 = v; }
; // We start out by calculating the length of the longest scanline.
; int height = v3->y - v1->y;
; int temp = ((v2->y - v1->y) << 16) / height;
; int longest = temp * (v3->x - v1->x) + ((v1->x - v2->x) << 16);
; if(longest == 0)
; return;
;
; // Now that we have the length of the longest scanline we can use that
; // to tell us which is left and which is the right side of the triangle.
;
; if(longest < 0)
; {
; // If longest is neg. we have the middle vertex on the right side.
; // Store the pointers for the right and left edge of the triangle.
; right_array[0] = v3;
; right_array[1] = v2;
; right_array[2] = v1;
; right_section = 2;
; left_array[0] = v3;
; left_array[1] = v1;
; left_section = 1;
;
; // Calculate initial left and right parameters
; if(LeftSection() <= 0)
; return;
; if(RightSection() <= 0)
; {
; // The first right section had zero height. Use the next section.
; right_section--;
; if(RightSection() <= 0)
; return;
; }
;
; // Ugly compensation so that the dudx,dvdx divides won't overflow
; // if the longest scanline is very short.
; if(longest > -0x1000)
; longest = -0x1000;
; }
; else
; {
; // If longest is pos. we have the middle vertex on the left side.
; // Store the pointers for the left and right edge of the triangle.
; left_array[0] = v3;
; left_array[1] = v2;
; left_array[2] = v1;
; left_section = 2;
; right_array[0] = v3;
; right_array[1] = v1;
; right_section = 1;
;
; // Calculate initial right and left parameters
; if(RightSection() <= 0)
; return;
; if(LeftSection() <= 0)
; {
; // The first left section had zero height. Use the next section.
; left_section--;
; if(LeftSection() <= 0)
; return;
; }
;
; // Ugly compensation so that the dudx,dvdx divides won't overflow
; // if the longest scanline is very short.
; if(longest < 0x1000)
; longest = 0x1000;
; }
;
; // Now we calculate the constant deltas for u and v (dudx, dvdx)
;
; int dudx = shl10idiv(temp*(v3->u - v1->u)+((v1->u - v2->u)<<16),longest);
; int dvdx = shl10idiv(temp*(v3->v - v1->v)+((v1->v - v2->v)<<16),longest);
;
; char * destptr = (char *) (v1->y * 320 + 0xa0000);
;
; // If you are using a table lookup inner loop you should setup the
; // lookup table here.
;
; // Here starts the outer loop (for each scanline)
;
; for(;;)
; {
; int x1 = left_x >> 16;
; int width = (right_x >> 16) - x1;
;
; if(width > 0)
; {
; // This is the inner loop setup and the actual inner loop.
; // If you keep everything else in C that's up to you but at
; // least remove this inner loop in C and insert some of
; // the Assembly versions.
;
; char * dest = destptr + x1;
; int u = left_u >> 8;
; int v = left_v >> 8;
; int du = dudx >> 8;
; int dv = dvdx >> 8;
;
; // Watcom C/C++ 10.0 can't get this inner loop any tighter
; // than about 10-12 clock ticks.
;
; do
; {
; *dest++ = bitmap[ (v & 0xff00) + ((u & 0xff00) >> 8) ];
; u += du;
; v += dv;
; }
; while(--width);
; }
;
; destptr += 320;
;
; // Interpolate along the left edge of the triangle
; if(--left_section_height <= 0) // At the bottom of this section?
; {
; if(--left_section <= 0) // All sections done?
; return;
; if(LeftSection() <= 0) // Nope, do the last section
; return;
; }
; else
; {
; left_x += delta_left_x;
; left_u += delta_left_u;
; left_v += delta_left_v;
; }
;
; // Interpolate along the right edge of the triangle
; if(--right_section_height <= 0) // At the bottom of this section?
; {
; if(--right_section <= 0) // All sections done?
; return;
; if(RightSection() <= 0) // Nope, do the last section
; return;
; }
; else
; {
; right_x += delta_right_x;
; }
; }
;}
equal equ 0 ; . 10,30
right equ 0 ;
;
;
currentVertices_tape
dc.w -40*4,-25*4,0 ; 2.................1
dc.w 41*4,-25*4,0 ; .....
dc.w -40*4,26*4,0 ; ..... .
; ......3
dc.w 41*4,26*4,0 ; 2...
; . ......
; . .......
; 4.................3
;point1 dc.w 22*2,2*2,22*2<<7,2*2<<7
;point2 dc.w 2*2,2*2,2*2<<7,2*2<<7
;point3 dc.w 22*2,22*2,22*2<<7,22*2<<7
point1_t dc.w 54,4,4<<7,55<<7
point2_t dc.w 4,4,85<<7,55<<7
point3_t dc.w 64,54,4<<7,4<<7
point4_t dc.w 4,54,85<<7,4<<7
;
;;50,0
;;10,5
;;40,45
;
;
; IFEQ equal
; IFEQ right
;point1 dc.w 60,05,65<<7,15<<7
;point2 dc.w 05,05,15<<7,15<<7
;point3 dc.w 40,50,50<<7,65<<7
;
; ELSE
;point1 dc.w 10,05,10<<7,05<<7
;point2 dc.w 50,05,50<<7,05<<7
;point3 dc.w 40,50,40<<7,50<<7
; ENDC
;
; ELSE
; IFEQ right
; ;Px ,Py ,Tx ,Ty
;point1 dc.w 10,50,10<<7,50<<7
;point2 dc.w 50,40,50<<7,40<<7
;point3 dc.w 30,05,30<<7,05<<7
; ELSE
;point1 dc.w 10,10,10,10
;point2 dc.w 10,40,10,40
;point3 dc.w 60,60,60,60
; ENDC
; ENDC
trianglePointers dc.l point3_t,point2_t,point1_t ; --> proper order top-> bot = p3,p2,p1
dc.l point2_t,point3_t,point4_t ; --> proper order top-> bot = p3,p2,p1
;y_factor dc.l 0
;x_factor dc.l 0
y1_y3 dc.w 0
y1_y2 dc.w 0
y2_y3 dc.w 0
;testtimes dc.w 400
; optimization 1: divs/muls gone
; optimization 2: innerloop construction smc using addx
; optimization 3: order of registers, and using them smart
drawTriangle2
move.l texturepointer,d6
move.l divtablepointer,a5 ;20
move.l a5,usp
movem.l (a0)+,a1-a3 ;36 ;a1,a2,a3 ; get points
move.l a0,-(sp)
;;;;;;;;;;; sort points ; determine order
move.w 2(a2),d0 ;12
cmp.w 2(a1),d0 ;12
bge .point1_lt_point2 ;8
exg a1,a2 ;8 *3
.point1_lt_point2
move.w 2(a3),d0
cmp.w 2(a1),d0
bge .point1_lt_point3
exg a1,a3
.point1_lt_point3
move.w 2(a3),d0
cmp.w 2(a2),d0
bge .point2_lt_point3
exg a2,a3
.point2_lt_point3
;;;;;;;;;;; done sorting
;;;;;;;;;;; determine max span
; points sorted, top to bottom a1,a2,a3
;------------------------------------------------------- determine y segments
move.w 2(a3),d5 ;12 y3 move.w (a3)+,d5 -4 ;$39
move.w d5,d7 ;4
move.w 2(a1),d4 ;12 y1 move.w (a1)+,d4 -4 ;$06
sub.w d4,d7 ;4 y3-y1 ;$33
beq .end ; if 0, then quit
move.w d7,y1_y3 ; save
move.w 2(a2),d2 ;12 y2 move.w (a2)+,d2 -4 ;$06
move.w d2,d3 ;4
sub.w d4,d2 ;4 y2-y1
move.w d2,y1_y2 ; save
sub.w d3,d5 ;4 y3-y2
move.w d5,y2_y3 ; save ;$33
;------------------------------------------------------- determine max x span
move.l usp,a5
ext.l d2 ;4
asl.l #7,d2 ;20
or.w d7,d2 ;4
add.l d2,d2 ;4
add.l d2,d2 ;4
move.l (a5,d2.l),d2 ;20
asr.l #8,d2 ;24 => 80 y2-y1 / y3-y1 ;0
move.w (a1),d0 ;8 x1 move.w (a1)+,d0 0 ;$17 = 23
move.w (a3),d7 ;8 x3 move.w (a3)+.d7 0 ;$68
sub.w d0,d7 ;4 x3-x1 ;$51
muls d2,d7 ;56 (x3-x1) * (y2-y1)/(y3-y1)
asr.l #8,d7 ;24
add.w d0,d7 ;4 (x3-x1) * (y2-y1)/(y3-y1) + x1
sub.w (a2),d7 ;8 max span ((x3-x1) * (y2-y1)/(y3-y1)) + x1 - x2 sub.w (a2)+,d7
bne .nozeromaxwidth
move.w #1,d7
.nozeromaxwidth
;------------------------------------------------------- determine uv stepping
move.w 4(a1),d1 ;12 u1 move.w (a1)+,d1 -4
move.w 4(a3),d0 ;12 u3 move.w (a3)+,d0 -4
sub.w d1,d0 ;4 u3-u1
add.w d0,d0 ;4
muls d2,d0 ;56 ((u3-u1) * (y2-y1)/(y3-y1))
sub.w 4(a2),d1 ;12 u1-u2 move.w (a2)+,d1 -4
asr.w #7,d1 ;20
swap d1 ;4
sub.w d1,d1 ;4
add.l d1,d0 ;8 ((u3-u1) * (y2-y1)/(y3-y1)) + (u1-u2)
lea divTablePivot,a4 ;8
add.w d7,d7 ;4
move.w (a4,d7.w),d6 ;20 1/width
swap d0 ;4
muls d6,d0 ;56 8 ( (u3-u1) * (y2-y1)/(y3-y1)) + (u1-u2) / width
move.w 6(a1),d4 ;12 v1 move.w (a1),d4 -4
move.w 6(a3),d1 ;12 v3 move.w (a3),d1 -4
sub.w d4,d1 ;4 v3-v1
add.w d1,d1 ;4
muls d2,d1 ;56 (v3-v1) * (y2-y1)/(y3-y1)
sub.w 6(a2),d4 ;12 v1-v2 sub.w (a2),d4 -4
asr.w #7,d4 ;20
swap d1 ;4
add.w d4,d1 ;4 (((v3-v1) * (y2-y1)/(y3-y1)) + (v1-v2)) / width
muls d6,d1 ;56
asr.l #7,d1 ;20
;--------------------------------------------------------- smc unrolled innerloop code with stepping offsets
IFEQ OPT_SMC
add.l d0,d0 ;8 to correct from the <<7 from the 1/x muls
swap d0 ;
sub.w d5,d5
ELSE
add.l d0,d0
swap d1
ENDC
move.l d0,d2 ; local du
move.l d1,d3 ; local dv
moveq #0,d4
IFEQ OPT_SMC ; 8 or 16 bit precision for x?
smcLoopSize equ -14
ELSE
smcLoopSize equ -18
ENDC
move.w d7,d6
bge .noneg
neg.w d6
.noneg
; muls #smcLoopSize,d6
move.w d6,a6
asl.w #3,d6
IFEQ OPT_SMC
sub.w a6,d6
ELSE
add.w a6,d6
ENDC
neg.w d6
.kkk
lea .doSMCLoopEnd+2*smcLoopSize,a6 ; 2 more than needed, because this way we prevent reading outside the texture, hax...
lea myTable2+2+127*4,a4 ; first load the end of the canvas table
sub.w 2(a6,d6.w),a4 ; then we rectify the table offset
jmp (a6,d6.w) ; and we need to load the proper address to a4, combined with the offset of the width span
.doSMCLoop
.offset set 0 ; offset into the unrolled loop of move.b textoff(a0),-(a1)
REPT 128
IFEQ OPT_SMC
move.w d4,.offset(a4) ;12
move.w d3,d4 ;4 ; 0.16 -> 0. 8.8 dv.du
move.b d2,d4 ;4 ; 0.16 -> 0. 8.8
add.l d0,d2 ;8....8 ;8
addx.b d5,d2 ;4
add.w d1,d3 ;... 8.8 ;4
ELSE ;---> 36
move.w d4,.offset(a4) ;12 4 smc the offset into the writing to canvas code
move.l d3,d4 ;4 2 local dv
swap d4 ;4 2 int lower word
swap d2 ;4 2 local du
move.b d2,d4 ;4 2 move du in so that VVVV UUUU
swap d2 ;4 2 swap back
add.l d0,d2 ;8 2 increment ; if xxxx ---- ---- XXXX, then add.l d0,d2 add.x DX,d2 move.b d2,d4
add.l d1,d3 ;8 2 increment
ENDC ;---> 48
.offset set .offset-4
ENDR
.doSMCLoopEnd
;--------------------------------------------------------- time to draw triangles
; lea canvas,a6 ;8 load canvas
move.l canvasPointer_text,a6
move.w 2(a1),d0 ;8 get top y move.w -6(a1),d0
asl.w #7,d0 ;18 canvas *128
add.w d0,a6 ;8 add y offset to canvas
tst.w d7 ;4 d7 is still span, but negative and positive determine where the 2nd point is
blt .middleRight
.middleLeft
move.w y1_y2,d0 ; y1_y2
beq .two_top_left ; if 0 ==> then p1 and p2 top
.first_slice_p1_p2_middle_left
move.l a2,-(sp) ;16 save a2
move.l a3,-(sp) ;16 save a3
move.w (a1),d1 ;8 startx move.w -6(a1),d1 +4
move.w (a3),d7 ;8 x3 move.w -6(a3),d7 +4
move.w 4(a1),d5 ;12 u1 global move.w -2(a1),d5 0
move.w 4(a2),d2 ;12 u2 move.w -2(a2),d2 0
sub.w d5,d2 ;4 u2-u1
ext.l d2
or.w d0,d2 ;4
add.w d2,d2 ;4
add.w d2,d2 ;4
move.l (a5,d2.l),a3 ;20 a3 = du/dy u2-u1 / y2-y1
move.w 6(a1),d4 ;12 v1 global move.w (a1),d4 -4
move.w 6(a2),d2 ;12 v2 move.w (a2),d2 -4
sub.w d4,d2 ;4 v2-v1
ext.l d2
or.w d0,d2 ;4
add.w d2,d2 ;4
add.w d2,d2 ;4
move.l (a5,d2.l),d2 ;20 v2-v1 / y2-y1
asr.l #8,d2 ;24 >>8
move.w d2,a4 ;4 a4 = dv/dy
asr.w #7,d5 ;20 >>7
swap d5 ;4
sub.w d5,d5 ;4 u1 global 16.16 format
move.w (a2),d3 ;8 x2 move.w -6(a2),d4 +4
sub.w d1,d3 ;4 x2-x1
ext.l d3
asl.l #7,d3 ;20 >>7
or.w d0,d3 ;4
add.l d3,d3 ;4
add.l d3,d3 ;4
move.l (a5,d3.l),a1 ;20 a1 = dx/dy left x2-x1 / y2-y1
sub.w d1,d7 ;4 x3-x1
ext.l d7
asl.l #7,d7 ;20 >> 7
or.w y1_y3(pc),d7 ;16
add.l d7,d7 ;4
add.l d7,d7 ;4
move.l (a5,d7.l),a5 ;20 a5 = dx/dy right x3-x1 / y3-y1
add.w d4,d4 ;4
swap d1 ;4
sub.w d1,d1 ;4
move.l d1,d2 ;4
subq.w #1,d0 ;4 94 nop
jsr drawHLine2
; d0, loopvar
; d1, x_start
; d2, x_end
; d3, x_end LOCAL
; d4, dv_global
; d5, du_global