-
Notifications
You must be signed in to change notification settings - Fork 90
/
blitter.cpp
2599 lines (2296 loc) · 62.1 KB
/
blitter.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* Custom chip emulation
*
* (c) 1995 Bernd Schmidt, Alessandro Bissacco
* (c) 2002 - 2021 Toni Wilen
*/
#define SPEEDUP 1
#define BLITTER_DEBUG 0
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "uae.h"
#include "memory.h"
#include "custom.h"
#include "events.h"
#include "newcpu.h"
#include "blitter.h"
#include "blit.h"
#include "savestate.h"
#include "debug.h"
// 1 = logging
// 2 = no wait detection
// 4 = no D
// 8 = instant
// 16 = activate debugger if weird things
// 32 = logging (no line)
#if BLITTER_DEBUG
int log_blitter = 1 | 16;
#else
int log_blitter = 0;
#endif
#define BLTCHA 0x800
#define BLTCHB 0x400
#define BLTCHC 0x200
#define BLTCHD 0x100
#define BLTDOFF 0x80
#define BLTEFE 0x10
#define BLTIFE 0x08
#define BLTFILL (BLTEFE | BLTIFE)
#define BLTFC 0x04
#define BLTDESC 0x02
#define BLTLINE 0x01
#define BLTSIGN 0x40
#define BLTOVF 0x20
#define BLTSUD 0x10
#define BLTSUL 0x08
#define BLTAUL 0x04
#define BLTSING 0x02
#define BLITTER_PIPELINE_BLIT 0x0008
#define BLITTER_PIPELINE_ADDMOD 0x0010
#define BLITTER_PIPELINE_LINE 0x0020
#define BLITTER_PIPELINE_PROCESS 0x0200
#define BLITTER_PIPELINE_FINISHED 0x0400
#define BLITTER_PIPELINE_LASTD 0x1000
#define BLITTER_PIPELINE_FIRST 0x2000
#define BLITTER_PIPELINE_LAST 0x4000
/* we must not change ce-mode while blitter is running.. */
static int blitter_cycle_exact, immediate_blits;
static int blt_statefile_type;
uae_u16 bltcon0, bltcon1;
uae_u32 bltapt, bltbpt, bltcpt, bltdpt;
uae_u32 bltptx;
int bltptxpos, bltptxc;
static uae_u16 blineb;
static int blitline, blitfc, blitfill, blitife, blitdesc, blit_ovf;
static bool blitfill_idle;
static int blitline_started, blitlineloop;
static int blitonedot, blitlinepixel;
static int blit_add;
static int blit_modadda, blit_modaddb, blit_modaddc, blit_modaddd;
static int blit_ch;
static bool shifter_skip_b, shifter_skip_y;
static bool shifter_skip_b_old, shifter_skip_y_old;
static uae_u16 bltcon0_old, bltcon1_old;
static bool shifter[4], shifter_out;
static int shifter_first;
static bool blitline_c, blitfill_c;
static int blitter_delayed_debug;
#ifdef BLITTER_SLOWDOWNDEBUG
static int blitter_slowdowndebug;
#endif
struct bltinfo blt_info;
static uae_u8 blit_filltable[256][4][2];
uae_u32 blit_masktable[BLITTER_MAX_WORDS];
static int blit_cyclecounter, blit_waitcyclecounter;
static int blit_maxcyclecounter, blit_slowdown, blit_totalcyclecounter;
static int blit_misscyclecounter;
#ifdef CPUEMU_13
static int blitter_cyclecounter;
static int blitter_hcounter;
static int blitter_vcounter;
#endif
static evt_t blit_firstline_cycles;
static evt_t blit_first_cycle;
static int blit_last_cycle, blit_dmacount, blit_cyclecount;
static int blit_faulty;
static int blt_delayed_irq;
static uae_u16 ddat;
static int ddatuse;
static int blit_dof;
static int last_blitter_hpos;
static uae_u16 debug_bltcon0, debug_bltcon1;
static uae_u32 debug_bltapt, debug_bltbpt, debug_bltcpt, debug_bltdpt;
static uae_u16 debug_bltamod, debug_bltbmod, debug_bltcmod, debug_bltdmod;
static uae_u32 debug_bltafwm, debug_bltalwm;
static uae_u32 debug_bltpc;
static int debug_bltcop;
static uae_u16 debug_bltsizev, debug_bltsizeh;
static uae_u16 debug_bltadat, debug_bltbdat, debug_bltcdat;
#define BLITTER_MAX_PIPELINED_CYCLES 4
#define CYCLECOUNT_FINISHED -1000
#define CYCLECOUNT_START 3
/*
Blitter Idle Cycle:
Cycles that are free cycles (available for CPU) and
are not used by any other Agnus DMA channel. Blitter
idle cycle is not "used" by blitter, CPU can still use
it normally if it needs the bus.
same in both block and line modes
number of cycles, initial cycle, main cycle
*/
#if 0
#define DIAGSIZE 10
static const int blit_cycle_diagram[][DIAGSIZE] =
{
{ 2, 0,0, 0,0 }, /* 0 -- */
{ 2, 0,0, 0,4 }, /* 1 -D */
{ 2, 0,3, 0,3 }, /* 2 -C */
{ 3, 0,3,0, 0,3,4 }, /* 3 -CD */
{ 3, 0,2,0, 0,2,0 }, /* 4 -B- */
{ 3, 0,2,0, 0,2,4 }, /* 5 -BD */
{ 3, 0,2,3, 0,2,3 }, /* 6 -BC */
{ 4, 0,2,3,0, 0,2,3,4 }, /* 7 -BCD */
{ 2, 1,0, 1,0 }, /* 8 A- */
{ 2, 1,0, 1,4 }, /* 9 AD */
{ 2, 1,3, 1,3 }, /* A AC */
{ 3, 1,3,0, 1,3,4, }, /* B ACD */
{ 3, 1,2,0, 1,2,0 }, /* C AB- */
{ 3, 1,2,0, 1,2,4 }, /* D ABD */
{ 3, 1,2,3, 1,2,3 }, /* E ABC */
{ 4, 1,2,3,0, 1,2,3,4 } /* F ABCD */
};
#endif
/*
following 4 channel combinations in fill mode have extra
idle cycle added (still requires free bus cycle)
Condition: If D without C: Add extra cycle.
*/
#if 0
// Cycle sequences are now generated using same
// logic as real blitter. Tables are not used anymore.
static const int blit_cycle_diagram_fill[][DIAGSIZE] =
{
{ 0 }, /* 0 -- */
{ 3, 0,0,0, 0,4,0 }, /* 1 -D */
{ 0 }, /* 2 -C */
{ 0 }, /* 3 -CD */
{ 0 }, /* 4 -B- */
{ 4, 0,2,0,0, 0,2,4,0 }, /* 5 -BD */
{ 0 }, /* 6 -BC */
{ 0 }, /* 7 -BCD */
{ 0 }, /* 8 A- */
{ 3, 1,0,0, 1,4,0 }, /* 9 AD */
{ 0 }, /* A AC */
{ 0 }, /* B ACD */
{ 0 }, /* C AB- */
{ 4, 1,2,0,0, 1,2,4,0 }, /* D ABD */
{ 0 }, /* E ABC */
{ 0 }, /* F ABCD */
};
#endif
/*
-C-D C-D- ... C-D- --
line draw takes 4 cycles (-C-D)
idle cycles do the same as above, 2 dma fetches
(read from C, write to D, but see below)
Oddities:
- first word is written to address pointed by BLTDPT
but all following writes go to address pointed by BLTCPT!
(some kind of internal copy because all bus cyles are
using normal BLTDDAT)
- BLTDMOD is ignored by blitter (BLTCMOD is used)
- state of D-channel enable bit does not matter!
- disabling A-channel freezes the content of BPLAPT
- C-channel disabled: nothing is written
There is one tricky situation, writing to DFF058 just before
last D write cycle (which is normally free) does not disturb
blitter operation, final D is still written correctly before
blitter starts normally (after 2 idle cycles)
There is at least one demo that does this..
*/
/* Copper pointer to Blitter register copy bug
1: -d = D (-D)
2: -c = C (-C)
3: - (-CD)
4: - (-B-)
5: - (-BD)
6: - (-BC)
7: -BcD = C, -BCd = D
8: - (A-)
9: - (AD)
A: - (AC)
B: A (ACD)
C: - (AB-)
D: - (ABD-)
E: - (ABC)
F: AxBxCxD = -, aBxCxD = A,
1FE,8C,RGA,8C
*/
void build_blitfilltable (void)
{
unsigned int d, fillmask;
int i;
for (i = 0; i < BLITTER_MAX_WORDS; i++)
blit_masktable[i] = 0xFFFF;
for (d = 0; d < 256; d++) {
for (i = 0; i < 4; i++) {
int fc = i & 1;
uae_u8 data = d;
for (fillmask = 1; fillmask != 0x100; fillmask <<= 1) {
uae_u16 tmp = data;
if (fc) {
if (i & 2)
data |= fillmask;
else
data ^= fillmask;
}
if (tmp & fillmask) fc = !fc;
}
blit_filltable[d][i][0] = data;
blit_filltable[d][i][1] = fc;
}
}
}
static void record_dma_blit_val(uae_u32 v)
{
#ifdef DEBUGGER
if (debug_dma && blitter_cycle_exact) {
record_dma_read_value(v);
}
if (memwatch_enabled) {
debug_getpeekdma_value(v);
}
#endif
}
static void record_dma_blit(uae_u16 reg, uae_u16 v, uae_u32 addr, int hpos)
{
#ifdef DEBUGGER
if (debug_dma && blitter_cycle_exact) {
if (reg == 0) {
record_dma_write(reg, v, addr, hpos, vpos, DMARECORD_BLITTER, 3 + (blitline_c ? 0x20 : (blitfill_c ? 0x10 : 0)));
} else {
int r = 0;
if (reg == 0x70)
r = 2;
if (reg == 0x72)
r = 1;
if (reg == 0x74)
r = 0;
record_dma_read(reg, addr, hpos, vpos, DMARECORD_BLITTER, r + (blitline_c ? 0x20 : (blitfill_c ? 0x10 : 0)));
}
}
if (memwatch_enabled) {
if (reg == 0) {
uae_u32 mask = MW_MASK_BLITTER_D_N;
if (blitfill_c)
mask = MW_MASK_BLITTER_D_F;
if (blitline_c)
mask = MW_MASK_BLITTER_D_L;
debug_putpeekdma_chipram(addr, v, mask, reg, 0x054);
} else if (reg == 0x70) {
debug_getpeekdma_chipram(addr, MW_MASK_BLITTER_C, reg, 0x48);
} else if (reg == 0x72) {
debug_getpeekdma_chipram(addr, MW_MASK_BLITTER_B, reg, 0x4c);
} else if (reg == 0x74) {
debug_getpeekdma_chipram(addr, MW_MASK_BLITTER_A, reg, 0x52);
}
}
#endif
}
static void blitter_debugsave(int copper, uaecptr pc)
{
debug_bltcon0 = bltcon0;
debug_bltcon1 = bltcon1;
debug_bltsizev = blt_info.vblitsize;
debug_bltsizeh = blt_info.hblitsize;
debug_bltapt = bltapt;
debug_bltbpt = bltbpt;
debug_bltcpt = bltcpt;
debug_bltdpt = bltdpt;
debug_bltadat = blt_info.bltadat;
debug_bltbdat = blt_info.bltbdat;
debug_bltcdat = blt_info.bltcdat;
debug_bltamod = blt_info.bltamod;
debug_bltbmod = blt_info.bltbmod;
debug_bltcmod = blt_info.bltcmod;
debug_bltdmod = blt_info.bltdmod;
debug_bltafwm = blt_info.bltafwm;
debug_bltalwm = blt_info.bltalwm;
debug_bltpc = pc;
debug_bltcop = copper;
}
static void blitter_dump (void)
{
int chipsize = currprefs.chipmem.size;
console_out_f(_T("PT A=%08X B=%08X C=%08X D=%08X\n"), bltapt, bltbpt, bltcpt, bltdpt);
console_out_f(_T("CON0=%04X CON1=%04X DAT A=%04X B=%04X C=%04X\n"),
bltcon0, bltcon1, blt_info.bltadat, blt_info.bltbdat, blt_info.bltcdat);
console_out_f(_T("AFWM=%04X ALWM=%04X MOD A=%04X B=%04X C=%04X D=%04X\n"),
blt_info.bltafwm, blt_info.bltalwm,
blt_info.bltamod & 0xffff, blt_info.bltbmod & 0xffff, blt_info.bltcmod & 0xffff, blt_info.bltdmod & 0xffff);
console_out_f(_T("PC=%08X DMA=%d\n"), m68k_getpc(), dmaen (DMA_BLITTER));
if (((bltcon0 & BLTCHA) && bltapt >= chipsize) || ((bltcon0 & BLTCHB) && bltbpt >= chipsize) ||
((bltcon0 & BLTCHC) && bltcpt >= chipsize) || ((bltcon0 & BLTCHD) && bltdpt >= chipsize))
console_out_f(_T("PT outside of chipram\n"));
}
void blitter_debugdump(void)
{
console_out(_T("Blitter registers at start:\n"));
console_out_f(_T("PT A=%08X B=%08X C=%08X D=%08X\n"), debug_bltapt, debug_bltbpt, debug_bltcpt, debug_bltdpt);
console_out_f(_T("CON0=%04X CON1=%04X DAT A=%04X B=%04X C=%04X\n"),
debug_bltcon0, debug_bltcon1, debug_bltadat, debug_bltbdat, debug_bltcdat);
console_out_f(_T("AFWM=%04X ALWM=%04X MOD A=%04X B=%04X C=%04X D=%04X\n"),
debug_bltafwm, debug_bltalwm, debug_bltamod, debug_bltbmod, debug_bltcmod, debug_bltdmod);
console_out_f(_T("COP=%d PC=%08X\n"), debug_bltcop, debug_bltpc);
console_out(_T("Blitter registers now:\n"));
blitter_dump();
}
static void markidlecycle(int hpos)
{
#ifdef DEBUGGER
if (debug_dma) {
record_dma_event(DMA_EVENT_BLITSTARTFINISH, hpos, vpos);
}
#endif
}
static void check_channel_mods(int hpos, int ch, uaecptr *pt)
{
static int blit_warned = 100;
if (bltptxpos < 0)
return;
if (bltptxpos != hpos)
return;
// if CPU write and non-CE: skip
if (bltptxc < 0) {
if (currprefs.cpu_model >= 68020 || !currprefs.cpu_cycle_exact) {
bltptxpos = -1;
return;
}
}
if (ch == bltptxc || ch == -bltptxc) {
if (blit_warned > 0) {
write_log(_T("BLITTER: %08X -> %08X write to %cPT ignored! %08x\n"), bltptx, *pt, ch + 'A' - 1, m68k_getpc());
blit_warned--;
}
bltptxpos = -1;
*pt = bltptx;
}
}
// blitter interrupt is set (and busy bit cleared) when
// last "main" cycle has been finished, any non-linedraw
// D-channel blit still needs 2 more cycles before final
// D is written (idle cycle, final D write)
//
// According to schematics, AGA has workaround delay circuit
// that adds 2 extra cycles if D is enabled and not line mode.
//
// line draw interrupt triggers when last D is written
// (or cycle where last D write would have been if
// ONEDOT was active)
static void blitter_interrupt(void)
{
if (blt_info.blit_interrupt) {
return;
}
blt_info.blit_interrupt = 1;
INTREQ_INT(6, 3);
#ifdef DEBUGGER
if (debug_dma) {
record_dma_event(DMA_EVENT_BLITIRQ, current_hpos(), vpos);
}
#endif
}
static void blitter_end(void)
{
blt_info.blit_main = 0;
blt_info.blit_finald = 0;
blt_info.blit_queued = 0;
event2_remevent(ev2_blitter);
unset_special(SPCFLAG_BLTNASTY);
if (log_blitter & 1) {
write_log(_T("cycles %d, missed %d, total %d\n"),
blit_totalcyclecounter, blit_misscyclecounter, blit_totalcyclecounter + blit_misscyclecounter);
}
blt_info.blitter_dangerous_bpl = 0;
}
static void blitter_done_all(int hpos)
{
blt_info.blit_main = 0;
blt_info.blit_finald = 0;
if (m68k_interrupt_delay && hpos >= 0) {
blt_info.finishhpos = hpos;
} else {
blt_info.finishhpos = -1;
}
blitter_interrupt();
blitter_done_notify(blitline);
if (!blt_info.blit_queued && !blt_info.blit_finald) {
blitter_end();
}
}
static void blitter_done_except_d(int hpos)
{
blt_info.blit_main = 0;
if (m68k_interrupt_delay && hpos >= 0) {
blt_info.finishhpos = hpos;
} else {
blt_info.finishhpos = -1;
}
blitter_interrupt();
blitter_done_notify(blitline);
}
static void blit_chipmem_agnus_wput(uaecptr addr, uae_u32 w, uae_u32 typemask)
{
if (!(log_blitter & 4)) {
if (blit_dof) {
w = regs.chipset_latch_rw;
}
#ifdef DEBUGGER
debug_putpeekdma_chipram(addr, w, typemask, 0x000, 0x054);
#endif
chipmem_wput_indirect(addr, w);
regs.chipset_latch_rw = w;
}
}
static void blitter_dofast (void)
{
int i,j;
uaecptr bltadatptr = 0, bltbdatptr = 0, bltcdatptr = 0, bltddatptr = 0;
uae_u8 mt = bltcon0 & 0xFF;
uae_u16 ashift = bltcon0 >> 12;
uae_u16 bshift = bltcon1 >> 12;
blit_masktable[0] = blt_info.bltafwm;
blit_masktable[blt_info.hblitsize - 1] &= blt_info.bltalwm;
if (bltcon0 & BLTCHA) {
bltadatptr = bltapt;
bltapt += (blt_info.hblitsize * 2 + blt_info.bltamod) * blt_info.vblitsize;
}
if (bltcon0 & BLTCHB) {
bltbdatptr = bltbpt;
bltbpt += (blt_info.hblitsize * 2 + blt_info.bltbmod) * blt_info.vblitsize;
}
if (bltcon0 & BLTCHC) {
bltcdatptr = bltcpt;
bltcpt += (blt_info.hblitsize * 2 + blt_info.bltcmod) * blt_info.vblitsize;
}
if (bltcon0 & BLTCHD) {
bltddatptr = bltdpt;
bltdpt += (blt_info.hblitsize * 2 + blt_info.bltdmod) * blt_info.vblitsize;
}
#if SPEEDUP
if (blitfunc_dofast[mt] && !blitfill) {
(*blitfunc_dofast[mt])(bltadatptr, bltbdatptr, bltcdatptr, bltddatptr, &blt_info);
} else
#endif
{
uae_u32 blitbhold = blt_info.bltbhold;
int ashift = bltcon0 >> 12;
int bshift = bltcon1 >> 12;
uaecptr dstp = 0;
int dodst = 0;
for (j = 0; j < blt_info.vblitsize; j++) {
blitfc = !!(bltcon1 & BLTFC);
for (i = 0; i < blt_info.hblitsize; i++) {
uae_u32 bltadat, blitahold;
if (bltadatptr) {
blt_info.bltadat = bltadat = chipmem_wget_indirect (bltadatptr);
bltadatptr += 2;
} else
bltadat = blt_info.bltadat;
bltadat &= blit_masktable[i];
blitahold = (((uae_u32)blt_info.bltaold << 16) | bltadat) >> ashift;
blt_info.bltaold = bltadat;
if (bltbdatptr) {
uae_u16 bltbdat = chipmem_wget_indirect (bltbdatptr);
bltbdatptr += 2;
blitbhold = (((uae_u32)blt_info.bltbold << 16) | bltbdat) >> bshift;
blt_info.bltbold = bltbdat;
blt_info.bltbdat = bltbdat;
}
if (bltcdatptr) {
blt_info.bltcdat = chipmem_wget_indirect (bltcdatptr);
bltcdatptr += 2;
}
if (dodst)
blit_chipmem_agnus_wput(dstp, blt_info.bltddat, MW_MASK_BLITTER_D_N);
blt_info.bltddat = blit_func (blitahold, blitbhold, blt_info.bltcdat, mt) & 0xFFFF;
if (blitfill) {
uae_u16 d = blt_info.bltddat;
int ifemode = blitife ? 2 : 0;
int fc1 = blit_filltable[d & 255][ifemode + blitfc][1];
blt_info.bltddat = (blit_filltable[d & 255][ifemode + blitfc][0]
+ (blit_filltable[d >> 8][ifemode + fc1][0] << 8));
blitfc = blit_filltable[d >> 8][ifemode + fc1][1];
}
if (blt_info.bltddat)
blt_info.blitzero = 0;
if (bltddatptr) {
dodst = 1;
dstp = bltddatptr;
bltddatptr += 2;
}
}
if (bltadatptr)
bltadatptr += blt_info.bltamod;
if (bltbdatptr)
bltbdatptr += blt_info.bltbmod;
if (bltcdatptr)
bltcdatptr += blt_info.bltcmod;
if (bltddatptr)
bltddatptr += blt_info.bltdmod;
}
if (dodst)
blit_chipmem_agnus_wput(dstp, blt_info.bltddat, MW_MASK_BLITTER_D_N);
blt_info.bltbhold = blitbhold;
}
blit_masktable[0] = 0xFFFF;
blit_masktable[blt_info.hblitsize - 1] = 0xFFFF;
blt_info.blit_main = 0;
}
static void blitter_dofast_desc (void)
{
int i,j;
uaecptr bltadatptr = 0, bltbdatptr = 0, bltcdatptr = 0, bltddatptr = 0;
uae_u8 mt = bltcon0 & 0xFF;
uae_u16 ashift = 16 - (bltcon0 >> 12);
uae_u16 bshift = 16 - (bltcon1 >> 12);
blit_masktable[0] = blt_info.bltafwm;
blit_masktable[blt_info.hblitsize - 1] &= blt_info.bltalwm;
if (bltcon0 & BLTCHA) {
bltadatptr = bltapt;
bltapt -= (blt_info.hblitsize * 2 + blt_info.bltamod) * blt_info.vblitsize;
}
if (bltcon0 & BLTCHB) {
bltbdatptr = bltbpt;
bltbpt -= (blt_info.hblitsize * 2 + blt_info.bltbmod) * blt_info.vblitsize;
}
if (bltcon0 & BLTCHC) {
bltcdatptr = bltcpt;
bltcpt -= (blt_info.hblitsize * 2 + blt_info.bltcmod) * blt_info.vblitsize;
}
if (bltcon0 & BLTCHD) {
bltddatptr = bltdpt;
bltdpt -= (blt_info.hblitsize * 2 + blt_info.bltdmod) * blt_info.vblitsize;
}
#if SPEEDUP
if (blitfunc_dofast_desc[mt] && !blitfill) {
(*blitfunc_dofast_desc[mt])(bltadatptr, bltbdatptr, bltcdatptr, bltddatptr, &blt_info);
} else
#endif
{
uae_u32 blitbhold = blt_info.bltbhold;
uaecptr dstp = 0;
int dodst = 0;
for (j = 0; j < blt_info.vblitsize; j++) {
blitfc = !!(bltcon1 & BLTFC);
for (i = 0; i < blt_info.hblitsize; i++) {
uae_u32 bltadat, blitahold;
if (bltadatptr) {
bltadat = blt_info.bltadat = chipmem_wget_indirect (bltadatptr);
bltadatptr -= 2;
} else
bltadat = blt_info.bltadat;
bltadat &= blit_masktable[i];
blitahold = (((uae_u32)bltadat << 16) | blt_info.bltaold) >> ashift;
blt_info.bltaold = bltadat;
if (bltbdatptr) {
uae_u16 bltbdat = chipmem_wget_indirect (bltbdatptr);
bltbdatptr -= 2;
blitbhold = (((uae_u32)bltbdat << 16) | blt_info.bltbold) >> bshift;
blt_info.bltbold = bltbdat;
blt_info.bltbdat = bltbdat;
}
if (bltcdatptr) {
blt_info.bltcdat = blt_info.bltbdat = chipmem_wget_indirect (bltcdatptr);
bltcdatptr -= 2;
}
if (dodst)
blit_chipmem_agnus_wput(dstp, blt_info.bltddat, MW_MASK_BLITTER_D_N);
blt_info.bltddat = blit_func (blitahold, blitbhold, blt_info.bltcdat, mt) & 0xFFFF;
if (blitfill) {
uae_u16 d = blt_info.bltddat;
int ifemode = blitife ? 2 : 0;
int fc1 = blit_filltable[d & 255][ifemode + blitfc][1];
blt_info.bltddat = (blit_filltable[d & 255][ifemode + blitfc][0]
+ (blit_filltable[d >> 8][ifemode + fc1][0] << 8));
blitfc = blit_filltable[d >> 8][ifemode + fc1][1];
}
if (blt_info.bltddat)
blt_info.blitzero = 0;
if (bltddatptr) {
dstp = bltddatptr;
dodst = 1;
bltddatptr -= 2;
}
}
if (bltadatptr)
bltadatptr -= blt_info.bltamod;
if (bltbdatptr)
bltbdatptr -= blt_info.bltbmod;
if (bltcdatptr)
bltcdatptr -= blt_info.bltcmod;
if (bltddatptr)
bltddatptr -= blt_info.bltdmod;
}
if (dodst)
blit_chipmem_agnus_wput(dstp, blt_info.bltddat, MW_MASK_BLITTER_D_N);
blt_info.bltbhold = blitbhold;
}
blit_masktable[0] = 0xFFFF;
blit_masktable[blt_info.hblitsize - 1] = 0xFFFF;
blt_info.blit_main = 0;
}
static void blitter_line_read_b(void)
{
if (bltcon0 & BLTCHB) {
// B (normally not enabled)
record_dma_blit(0x72, 0, bltbpt, last_blitter_hpos);
blt_info.bltbdat = chipmem_wget_indirect(bltbpt);
regs.chipset_latch_rw = blt_info.bltbdat;
record_dma_blit_val(blt_info.bltbdat);
bltbpt += blt_info.bltbmod;
}
}
static void blitter_line_read_c(void)
{
if (bltcon0 & BLTCHC) {
// C
record_dma_blit(0x70, 0, bltcpt, last_blitter_hpos);
blt_info.bltcdat = chipmem_wget_indirect(bltcpt);
regs.chipset_latch_rw = blt_info.bltcdat;
record_dma_blit_val(blt_info.bltcdat);
}
}
static void blitter_line_write(void)
{
/* D-channel state has no effect on linedraw, but C must be enabled or nothing is drawn! */
if (bltcon0 & BLTCHC) {
blit_chipmem_agnus_wput(bltdpt, blt_info.bltddat, MW_MASK_BLITTER_D_L);
}
}
static void blitter_line_minterm(uae_u16 dat)
{
uae_u16 mask = blt_info.bltafwm;
if (dat & BLITTER_PIPELINE_LAST) {
mask &= blt_info.bltalwm;
}
int ashift = bltcon0 >> 12;
uae_u16 blitahold = (blt_info.bltadat & mask) >> ashift;
if (bltcon0 & BLTCHB) {
// B special case if enabled
int bshift = bltcon1 >> 12;
blineb = (((uae_u32)blt_info.bltbold << 16) | blt_info.bltbdat) >> bshift;
}
blt_info.bltbhold = (blineb & 0x0001) ? 0xFFFF : 0;
blt_info.bltddat = blit_func(blitahold, blt_info.bltbhold, blt_info.bltcdat, bltcon0 & 0xFF);
if (blt_info.bltddat) {
blt_info.blitzero = 0;
}
}
static void blitter_line_minterm_extra(void)
{
int ashift = bltcon0 >> 12;
// never first or last, no masking needed.
uae_u16 blitahold = blt_info.bltadat >> ashift;
blt_info.bltddat = blit_func(blitahold, blt_info.bltbhold, blt_info.bltcdat, bltcon0 & 0xFF);
}
static void blitter_line_adat(void)
{
int ashift = bltcon0 >> 12;
blt_info.bltaold = (((uae_u32)blt_info.bltaold << 16) | (blt_info.bltadat & blt_info.bltafwm)) >> ashift;
}
static void blitter_line_proc_apt(void)
{
if (bltcon0 & BLTCHA) {
bool sign = (bltcon1 & BLTSIGN) != 0;
if (sign)
bltapt += (uae_s16)blt_info.bltbmod;
else
bltapt += (uae_s16)blt_info.bltamod;
}
}
static void blitter_line_ovf(void)
{
uae_u16 ashift = bltcon0 >> 12;
ashift += blit_ovf;
ashift &= 15;
bltcon0 &= 0x0fff;
bltcon0 |= ashift << 12;
blit_ovf = 0;
}
static void blitter_line_incx(void)
{
uae_u16 ashift = bltcon0 >> 12;
if (ashift == 15) {
bltcpt += 2;
}
blit_ovf = 1;
}
static void blitter_line_decx(void)
{
uae_u16 ashift = bltcon0 >> 12;
if (ashift == 0) {
bltcpt -= 2;
}
blit_ovf = -1;
}
static void blitter_line_decy(void)
{
if (bltcon0 & BLTCHC) {
bltcpt -= blt_info.bltcmod;
blitonedot = 0;
}
}
static void blitter_line_incy(void)
{
if (bltcon0 & BLTCHC) {
bltcpt += blt_info.bltcmod;
blitonedot = 0;
}
}
static void blitter_line_proc_cpt_y(void)
{
bool sign = (bltcon1 & BLTSIGN) != 0;
if (!sign) {
if (bltcon1 & BLTSUD) {
if (bltcon1 & BLTSUL)
blitter_line_decy();
else
blitter_line_incy();
}
}
if (!(bltcon1 & BLTSUD)) {
if (bltcon1 & BLTAUL)
blitter_line_decy();
else
blitter_line_incy();
}
}
static void blitter_line_proc_cpt_x(void)
{
bool sign = (bltcon1 & BLTSIGN) != 0;
if (!sign) {
if (!(bltcon1 & BLTSUD)) {
if (bltcon1 & BLTSUL)
blitter_line_decx();
else
blitter_line_incx();
}
}
if (bltcon1 & BLTSUD) {
if (bltcon1 & BLTAUL)
blitter_line_decx();
else
blitter_line_incx();
}
}
static void blitter_line_proc_status(void)
{
blitlinepixel = !(bltcon1 & BLTSING) || ((bltcon1 & BLTSING) && !blitonedot);
blitonedot = 1;
}
static void blitter_line_sign(void)
{
bool blitsign = (uae_s16)bltapt < 0;
if (blitsign) {
bltcon1 |= BLTSIGN;
} else {
bltcon1 &= ~BLTSIGN;
}
}
static void blitter_nxline(void)
{
int bshift = bltcon1 >> 12;
bshift--;
bshift &= 15;
blineb = (blt_info.bltbdat >> bshift) | (blt_info.bltbdat << (16 - bshift));
bltcon1 &= 0x0fff;
bltcon1 |= bshift << 12;
}
static void actually_do_blit (void)
{
if (blitline) {
do {
blitter_line_proc_status();
blitter_line_proc_apt();
if (blt_info.hblitsize > 1) {
blitter_line_read_b();
blitter_line_read_c();
blitter_line_minterm(BLITTER_PIPELINE_FIRST);
blitter_line_proc_cpt_x();
}
if (blt_info.hblitsize > 2) {
if (blitlineloop && !(bltcon1 & BLTSUD)) {
blitter_line_proc_cpt_y();
blitlineloop = 0;
}
blitter_line_read_c();
blitter_line_minterm_extra();
}
blitter_line_adat();
blitter_line_ovf();
if (blt_info.hblitsize >= 2) {
if (blitlineloop) {
blitter_line_proc_cpt_y();
blitlineloop = 0;
}
}
blitter_line_sign();
blitter_nxline();
if (blitlinepixel) {
blitter_line_write();
blitlinepixel = 0;
}
bltdpt = bltcpt;
blitter_line_minterm(BLITTER_PIPELINE_FIRST | BLITTER_PIPELINE_LAST);
blitlineloop = 1;
blt_info.vblitsize--;
} while (blt_info.vblitsize != 0);
} else {
if (blitdesc)
blitter_dofast_desc();
else
blitter_dofast();
}
blt_info.blit_main = 0;
}
static void blitter_doit (int hpos)
{
if (blt_info.vblitsize == 0) {
blitter_done_all(hpos);
return;
}
actually_do_blit();
blitter_done_all(hpos);
}
static int makebliteventtime(int delay)
{
if (delay) {
delay += delay - (int)(delay * (1 + currprefs.blitter_speed_throttle) + 0.5);
if (delay <= 0)
delay = 1;
}
return delay;
}
void blitter_handler(uae_u32 data)
{
static int blitter_stuck;
if (!dmaen (DMA_BLITTER)) {
event2_newevent (ev2_blitter, 10, 0);
blitter_stuck++;
if (blitter_stuck < 20000 || !immediate_blits)
return; /* gotta come back later. */
/* "free" blitter in immediate mode if it has been "stuck" ~3 frames
* fixes some JIT game incompatibilities
*/
#ifdef DEBUGGER
debugtest (DEBUGTEST_BLITTER, _T("force-unstuck!\n"));
#endif
}
blitter_stuck = 0;
if (blit_slowdown > 0 && !immediate_blits) {
event2_newevent (ev2_blitter, makebliteventtime(blit_slowdown), 0);
blit_slowdown = -1;
return;
}
blitter_doit(-1);
}
#ifdef CPUEMU_13
static bool blitshifterdebug(uae_u16 con0, bool check)
{
int cnt = 0;
for (int i = 0; i < 4; i++) {
if (shifter[i]) {
cnt++;
}