-
Notifications
You must be signed in to change notification settings - Fork 1
/
custom.c
6493 lines (5790 loc) · 173 KB
/
custom.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
/*
* UAE - The Un*x Amiga Emulator
*
* Custom chip emulation
*
* Copyright 1995-2002 Bernd Schmidt
* Copyright 1995 Alessandro Bissacco
* Copyright 2000-2008 Toni Wilen
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include <ctype.h>
#include <assert.h>
#include "options.h"
#include "uae.h"
#include "events.h"
#include "memory.h"
#include "custom.h"
#include "custom_private.h"
#include "newcpu.h"
#include "cia.h"
#include "disk.h"
#include "blitter.h"
#include "xwin.h"
#include "inputdevice.h"
#include "audio.h"
#include "keybuf.h"
#include "serial.h"
#include "osemu.h"
#include "autoconf.h"
#include "traps.h"
#include "gui.h"
#include "picasso96.h"
#include "drawing.h"
#include "savestate.h"
#include "ar.h"
#ifdef AVIOUTPUT
#include "avioutput.h"
#endif
#include "debug.h"
#include "akiko.h"
#if defined(ENFORCER)
#include "enforcer.h"
#endif
#include "hrtimer.h"
#include "sleep.h"
//#define CUSTOM_DEBUG
#define SPRITE_DEBUG 0
#define SPRITE_DEBUG_MINY 0xb0
#define SPRITE_DEBUG_MAXY 0xf8
#ifdef NEWHSYNC
#define SPR0_HPOS 0x19
#else
#define SPR0_HPOS 0x15
#endif
#define MAX_SPRITES 8
#define SPRITE_COLLISIONS
#define SPEEDUP
#define AUTOSCALE_SPRITES 1
#define NEW_BPL 1
#define SPRBORDER 0
STATIC_INLINE int nocustom(void)
{
if (picasso_on && currprefs.picasso96_nocustom)
return 1;
return 0;
}
static void uae_abort (const char *format,...)
{
static int nomore;
va_list parms;
char buffer[1000];
va_start (parms, format);
#ifdef _WIN32
_vsnprintf( buffer, sizeof (buffer) -1, format, parms );
#else
vsnprintf( buffer, sizeof (buffer) -1, format, parms );
#endif
va_end (parms);
if (nomore) {
write_log ("%s\n", buffer);
return;
}
gui_message (buffer);
nomore = 1;
}
#if 0
void customhack_put (struct customhack *ch, uae_u16 v, unsigned int hpos)
{
ch->v = v;
ch->vpos = vpos;
ch->hpos = hpos;
}
uae_u16 customhack_get (struct customhack *ch, unsigned int hpos)
{
if (ch->vpos == vpos && ch->hpos == hpos) {
ch->vpos = -1;
return 0xffff;
}
return ch->v;
}
#endif
uae_u16 last_custom_value;
static unsigned int n_consecutive_skipped = 0;
static unsigned int total_skipped = 0;
STATIC_INLINE void sync_copper (int hpos);
/* Events */
unsigned long int event_cycles, nextevent, is_lastline, currcycle;
long cycles_to_next_event;
long max_cycles_to_next_event;
long cycles_to_hsync_event;
static int rpt_did_reset;
struct ev eventtab[ev_max];
struct ev2 eventtab2[ev2_max];
volatile frame_time_t vsynctime, vsyncmintime;
#ifdef JIT
extern uae_u8* compiled_code;
#endif
int vpos;
int hack_vpos;
static int lof;
static int next_lineno, prev_lineno;
static enum nln_how nextline_how;
static int lof_changed = 0;
static int scandoubled_line;
/* Stupid genlock-detection prevention hack.
* We should stop calling vsync_handler() and
* hstop_handler() completely but it is not
* worth the trouble..
*/
static int vpos_previous, hpos_previous;
static int vpos_lpen, hpos_lpen, lightpen_triggered;
int lightpen_x, lightpen_y, lightpen_cx, lightpen_cy;
static uae_u32 sprtaba[256],sprtabb[256];
static uae_u32 sprite_ab_merge[256];
/* Tables for collision detection. */
static uae_u32 sprclx[16], clxmask[16];
/* AGA T genlock bit in color registers */
static uae_u8 color_regs_aga_genlock[256];
/*
* Hardware registers of all sorts.
*/
static int custom_wput_1 (int, uaecptr, uae_u32, int) REGPARAM;
static uae_u16 cregs[256];
uae_u16 intena, intreq, intreqr;
uae_u16 dmacon;
uae_u16 adkcon; /* used by audio code */
static uae_u32 cop1lc, cop2lc, copcon;
int maxhpos = MAXHPOS_PAL;
int maxvpos = MAXVPOS_PAL;
int maxvpos_max = MAXVPOS_PAL;
int minfirstline = VBLANK_ENDLINE_PAL;
int vblank_hz = VBLANK_HZ_PAL, fake_vblank_hz, vblank_skip, doublescan;
frame_time_t syncbase;
static int fmode;
uae_u16 beamcon0, new_beamcon0;
uae_u16 vtotal = MAXVPOS_PAL, htotal = MAXHPOS_PAL;
static uae_u16 hsstop, hbstrt, hbstop, vsstop, vbstrt, vbstop, hsstrt, vsstrt, hcenter;
static int ciavsyncmode;
/* This is but an educated guess. It seems to be correct, but this stuff
* isn't documented well. */
struct sprite {
uaecptr pt;
int xpos;
int vstart;
int vstop;
int dblscan; /* AGA SSCAN2 */
int armed;
int dmastate;
int dmacycle;
};
static struct sprite spr[MAX_SPRITES];
uaecptr sprite_0;
int sprite_0_width, sprite_0_height, sprite_0_doubled;
uae_u32 sprite_0_colors[4];
static uae_u8 magic_sprite_mask = 0xff;
static int sprite_vblank_endline = VBLANK_SPRITE_PAL;
static unsigned int sprctl[MAX_SPRITES], sprpos[MAX_SPRITES];
#ifdef AGA
static uae_u16 sprdata[MAX_SPRITES][4], sprdatb[MAX_SPRITES][4];
#else
static uae_u16 sprdata[MAX_SPRITES][1], sprdatb[MAX_SPRITES][1];
#endif
static int sprite_last_drawn_at[MAX_SPRITES];
static int last_sprite_point, nr_armed;
int sprite_width, sprres;
int sprite_buffer_res;
#ifdef CPUEMU_6
uae_u8 cycle_line[256];
#endif
static uae_u16 bplxdat[8];
static uae_s16 bpl1mod, bpl2mod;
static uaecptr prevbpl[2][MAXVPOS][8];
static uaecptr bplpt[8], bplptx[8], f_bplpt[8];
/* Used as a debugging aid, to offset any bitplane temporarily. */
int bpl_off[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
/*static int blitcount[256]; blitter debug */
static struct color_entry current_colors;
static unsigned int bplcon0, bplcon1, bplcon2, bplcon3, bplcon4;
static unsigned int ta_bplcon0_res, td_bplcon0_res, t_bplcon0_planes, t_bplcon0_planes_limit;
static unsigned int diwstrt, diwstop, diwhigh;
static int diwhigh_written;
static unsigned int ddfstrt, ddfstop, ddfstrt_old_hpos, ddfstrt_old_vpos;
static int ddf_change, badmode;
/* The display and data fetch windows */
enum diw_states
{
DIW_waiting_start, DIW_waiting_stop
};
int plffirstline, plflastline;
int plffirstline_total, plflastline_total;
int plfstrt_start, plfstrt, plfstop;
static int sprite_minx, sprite_maxx;
static int first_bpl_vpos;
static int last_diw_pix_hpos, last_ddf_pix_hpos;
static int last_decide_line_hpos, last_sprite_decide_line_hpos;
static int last_fetch_hpos, last_sprite_hpos;
int diwfirstword, diwlastword;
static enum diw_states diwstate, hdiwstate, ddfstate;
int first_planes_vpos, last_planes_vpos;
int diwfirstword_total, diwlastword_total;
int firstword_bplcon1;
/* Sprite collisions */
static unsigned int clxdat, clxcon, clxcon2, clxcon_bpl_enable, clxcon_bpl_match;
enum copper_states {
COP_stop,
COP_read1,
COP_read2,
COP_bltwait,
COP_wait_in2,
COP_skip_in2,
COP_wait1,
COP_wait,
COP_skip1,
COP_strobe_delay1,
COP_strobe_delay2
};
struct copper {
/* The current instruction words. */
unsigned int i1, i2;
unsigned int saved_i1, saved_i2;
enum copper_states state, state_prev;
/* Instruction pointer. */
uaecptr ip, saved_ip;
int hpos, vpos;
unsigned int ignore_next;
int vcmp, hcmp;
int strobe; /* COPJMP1 / COPJMP2 accessed */
int last_write, last_write_hpos;
int moveaddr, movedata, movedelay;
};
static struct copper cop_state;
static int copper_enabled_thisline;
static int cop_min_waittime;
static uae_u16 f_bplcon0, f_fmode;
static int fa_bplcon0_res, fd_bplcon0_res, f_bplcon0_planes, f_bplcon0_planes_limit;
static int f_fetchunit, f_fetchunit_mask;
static int f_fetchstart, f_fetchstart_mask, f_fetchstart_shift;
static int f_fm_maxplane_shift, f_fm_maxplane;
static int f_fetch_modulo_cycle, f_fetchmode;
/*
* Statistics
*/
unsigned long int hsync_counter;
/* Used also by bebox.cpp */
unsigned long int frametime = 0, lastframetime = 0, timeframes = 0;
unsigned long hsync_counter = 0, vsync_counter = 0, ciavsync_counter = 0;
unsigned long int idletime;
int bogusframe;
/* Recording of custom chip register changes. */
static int current_change_set;
#ifdef OS_WITHOUT_MEMORY_MANAGEMENT
/* sam: Those arrays uses around 7Mb of BSS... That seems */
/* too much for AmigaDOS (uae crashes as soon as one loads */
/* it. So I use a different strategy here (realloc the */
/* arrays when needed. That strategy might be usefull for */
/* computer with low memory. */
struct sprite_entry *sprite_entries[2];
struct color_change *color_changes[2];
static int max_sprite_entry = 400;
static int delta_sprite_entry = 0;
static int max_color_change = 400;
static int delta_color_change = 0;
#else
struct sprite_entry sprite_entries[2][MAX_SPR_PIXELS / 16];
struct color_change color_changes[2][MAX_REG_CHANGE];
#endif
struct decision line_decisions[2 * (MAXVPOS + 1) + 1];
struct draw_info line_drawinfo[2][2 * (MAXVPOS + 1) + 1];
struct color_entry color_tables[2][(MAXVPOS + 1) * 2];
int next_sprite_entry = 0;
static int prev_next_sprite_entry;
static int next_sprite_forced = 1;
struct sprite_entry *curr_sprite_entries, *prev_sprite_entries;
struct color_change *curr_color_changes, *prev_color_changes;
struct draw_info *curr_drawinfo, *prev_drawinfo;
struct color_entry *curr_color_tables, *prev_color_tables;
static int next_color_change;
static int next_color_entry, remembered_color_entry;
static int color_src_match, color_dest_match, color_compare_result;
static uae_u32 thisline_changed;
#ifdef SMART_UPDATE
#define MARK_LINE_CHANGED do { thisline_changed = 1; } while (0)
#else
#define MARK_LINE_CHANGED do { ; } while (0)
#endif
static struct decision thisline_decision;
static int fetch_cycle;
enum plfstate
{
plf_idle,
plf_start,
plf_active,
plf_passed_stop,
plf_passed_stop2,
plf_end
} plfstate;
enum fetchstate {
fetch_not_started,
fetch_started,
fetch_was_plane0
} fetch_state;
/*
* helper functions
*/
STATIC_INLINE int ecsshres(void)
{
return fd_bplcon0_res == RES_SUPERHIRES && (currprefs.chipset_mask & CSMASK_ECS_DENISE) && !(currprefs.chipset_mask & CSMASK_AGA);
}
STATIC_INLINE int nodraw (void)
{
return !currprefs.cpu_cycle_exact && framecnt != 0;
}
static int doflickerfix (void)
{
return currprefs.gfx_linedbl && doublescan < 0;
}
uae_u32 get_copper_address (int copno)
{
switch (copno) {
case 1: return cop1lc;
case 2: return cop2lc;
case -1: return cop_state.ip;
default: return 0;
}
}
int rpt_available = 0;
void reset_frame_rate_hack (void)
{
if (currprefs.m68k_speed != -1)
return;
if (! rpt_available) {
currprefs.m68k_speed = 0;
return;
}
rpt_did_reset = 1;
is_lastline = 0;
vsyncmintime = uae_gethrtime () + vsynctime;
write_log ("Resetting frame rate hack\n");
}
STATIC_INLINE void setclr (uae_u16 *p, uae_u16 val)
{
if (val & 0x8000)
*p |= val & 0x7FFF;
else
*p &= ~val;
}
STATIC_INLINE alloc_cycle (int hpos, int type)
{
#ifdef CPUEMU_6
cycle_line[hpos] = type;
#endif
}
STATIC_INLINE alloc_cycle_maybe (int hpos, int type)
{
if (cycle_line[hpos] == 0)
alloc_cycle (hpos, type);
}
void alloc_cycle_ext(int hpos, int type)
{
alloc_cycle (hpos, type);
}
static void hsyncdelay (void)
{
#if 0
static int prevhpos;
while (current_hpos () == prevhpos)
do_cycles(CYCLE_UNIT);
prevhpos = current_hpos();
#endif
}
static void update_mirrors (void)
{
aga_mode = (currprefs.chipset_mask & CSMASK_AGA) ? 1 : 0;
direct_rgb = aga_mode;
}
STATIC_INLINE unsigned int current_hpos (void)
{
return (get_cycles () - eventtab[ev_hsync].oldcycles) / CYCLE_UNIT;
}
STATIC_INLINE uae_u8 *pfield_xlateptr (uaecptr plpt, int bytecount)
{
if (!chipmem_bank.check (plpt, bytecount)) {
static int count = 0;
if (!count)
count++, write_log ("Warning: Bad playfield pointer\n");
return NULL;
}
return chipmem_bank.xlateaddr (plpt);
}
STATIC_INLINE void docols (struct color_entry *colentry)
{
int i;
#ifdef AGA
if (currprefs.chipset_mask & CSMASK_AGA) {
for (i = 0; i < 256; i++) {
int v = color_reg_get (colentry, i);
if (v < 0 || v > 16777215)
continue;
colentry->acolors[i] = getxcolor (v);
}
} else {
#endif
for (i = 0; i < 32; i++) {
int v = color_reg_get (colentry, i);
if (v < 0 || v > 4095)
continue;
colentry->acolors[i] = getxcolor (v);
}
#ifdef AGA
}
#endif
}
extern struct color_entry colors_for_drawing;
void notice_new_xcolors (void)
{
int i;
update_mirrors ();
docols (¤t_colors);
docols (&colors_for_drawing);
for (i = 0; i < (MAXVPOS + 1) * 2; i++) {
docols (color_tables[0] + i);
docols (color_tables[1] + i);
}
}
static void do_sprites (int currhp);
static void remember_ctable (void)
{
if (remembered_color_entry == -1) {
/* The colors changed since we last recorded a color map. Record a
* new one. */
color_reg_cpy (curr_color_tables + next_color_entry, ¤t_colors);
remembered_color_entry = next_color_entry++;
}
thisline_decision.ctable = remembered_color_entry;
if (color_src_match == -1 || color_dest_match != remembered_color_entry
|| line_decisions[next_lineno].ctable != color_src_match)
{
/* The remembered comparison didn't help us - need to compare again. */
int oldctable = line_decisions[next_lineno].ctable;
int changed = 0;
if (oldctable == -1) {
changed = 1;
color_src_match = color_dest_match = -1;
} else {
color_compare_result = color_reg_cmp (&prev_color_tables[oldctable], ¤t_colors) != 0;
if (color_compare_result)
changed = 1;
color_src_match = oldctable;
color_dest_match = remembered_color_entry;
}
thisline_changed |= changed;
} else {
/* We know the result of the comparison */
if (color_compare_result)
thisline_changed = 1;
}
}
static void remember_ctable_for_border (void)
{
remember_ctable ();
}
/* Called to determine the state of the horizontal display window state
* machine at the current position. It might have changed since we last
* checked. */
static void decide_diw (int hpos)
{
/* Last hpos = hpos + 0.5, eg. normal PAL end hpos is 227.5 * 2 = 455 */
int pix_hpos = coord_diw_to_window_x (hpos == maxhpos ? hpos * 2 + 1 : hpos * 2);
if (hdiwstate == DIW_waiting_start && thisline_decision.diwfirstword == -1
&& pix_hpos >= diwfirstword && last_diw_pix_hpos < diwfirstword)
{
thisline_decision.diwfirstword = diwfirstword < 0 ? 0 : diwfirstword;
hdiwstate = DIW_waiting_stop;
}
if (hdiwstate == DIW_waiting_stop && thisline_decision.diwlastword == -1
&& pix_hpos >= diwlastword && last_diw_pix_hpos < diwlastword)
{
thisline_decision.diwlastword = diwlastword < 0 ? 0 : diwlastword;
hdiwstate = DIW_waiting_start;
}
last_diw_pix_hpos = pix_hpos;
}
static int fetchmode;
static int real_bitplane_number[3][3][9];
/* Disable bitplane DMA if planes > available DMA slots. This is needed
e.g. by the Sanity WOC demo (at the "Party Effect"). */
STATIC_INLINE int GET_PLANES_LIMIT (uae_u16 bc0)
{
int res = GET_RES_AGNUS (bc0);
int planes = GET_PLANES (bc0);
return real_bitplane_number[f_fetchmode][res][planes];
}
#ifdef NEWHSYNC
#define HARD_DDF_STOP 0xd8
#define HARD_DDF_START 0x18
#else
/* The HRM says 0xD8, but that can't work... */
#define HARD_DDF_STOP 0xd4
#define HARD_DDF_START 0x18
#endif
static void add_modulos (void)
{
int m1, m2;
if (f_fmode & 0x4000) {
if (((diwstrt >> 8) ^ vpos) & 1)
m1 = m2 = bpl2mod;
else
m1 = m2 = bpl1mod;
} else {
m1 = bpl1mod;
m2 = bpl2mod;
}
switch (f_bplcon0_planes_limit) {
#ifdef AGA
case 8: bplpt[7] += m2; bplptx[7] += m2;
case 7: bplpt[6] += m1; bplptx[6] += m1;
#endif
case 6: bplpt[5] += m2; bplptx[5] += m2;
case 5: bplpt[4] += m1; bplptx[4] += m1;
case 4: bplpt[3] += m2; bplptx[3] += m2;
case 3: bplpt[2] += m1; bplptx[2] += m1;
case 2: bplpt[1] += m2; bplptx[1] += m2;
case 1: bplpt[0] += m1; bplptx[0] += m1;
}
}
static void finish_playfield_line (void)
{
/* The latter condition might be able to happen in interlaced frames. */
if (vpos >= minfirstline && (thisframe_first_drawn_line == -1 || vpos < thisframe_first_drawn_line))
thisframe_first_drawn_line = vpos;
thisframe_last_drawn_line = vpos;
#ifdef SMART_UPDATE
if (line_decisions[next_lineno].plflinelen != thisline_decision.plflinelen
|| line_decisions[next_lineno].plfleft != thisline_decision.plfleft
|| line_decisions[next_lineno].bplcon0 != thisline_decision.bplcon0
|| line_decisions[next_lineno].bplcon2 != thisline_decision.bplcon2
#ifdef ECS_DENISE
|| line_decisions[next_lineno].bplcon3 != thisline_decision.bplcon3
#endif
#ifdef AGA
|| line_decisions[next_lineno].bplcon4 != thisline_decision.bplcon4
#endif
)
#endif /* SMART_UPDATE */
thisline_changed = 1;
}
/* The fetch unit mainly controls ddf stop. It's the number of cycles that
are contained in an indivisible block during which ddf is active. E.g.
if DDF starts at 0x30, and fetchunit is 8, then possible DDF stops are
0x30 + n * 8. */
static int t_fetchunit, t_fetchunit_mask;
/* The delay before fetching the same bitplane again. Can be larger than
the number of bitplanes; in that case there are additional empty cycles
with no data fetch (this happens for high fetchmodes and low
resolutions). */
static int t_fetchstart, t_fetchstart_shift, t_fetchstart_mask;
/* fm_maxplane holds the maximum number of planes possible with the current
fetch mode. This selects the cycle diagram:
8 planes: 73516240
4 planes: 3120
2 planes: 10. */
static int f_fm_maxplane, f_fm_maxplane_shift;
/* The corresponding values, by fetchmode and display resolution. */
static const int fetchunits[] = { 8,8,8,0, 16,8,8,0, 32,16,8,0 };
static const int fetchstarts[] = { 3,2,1,0, 4,3,2,0, 5,4,3,0 };
static const int fm_maxplanes[] = { 3,2,1,0, 3,3,2,0, 3,3,3,0 };
static int cycle_diagram_table[3][3][9][32];
static int cycle_diagram_free_cycles[3][3][9];
static int cycle_diagram_total_cycles[3][3][9];
static int *curr_diagram, curr_diagram_change;
static const int cycle_sequences[3 * 8] = { 2,1,2,1,2,1,2,1, 4,2,3,1,4,2,3,1, 8,4,6,2,7,3,5,1 };
#if 0
static void debug_cycle_diagram (void)
{
unsigned int fm, res, planes, cycle, v;
char aa;
for (fm = 0; fm < 3; fm++) {
write_log ("FMODE %d\n=======\n", fm);
for (res = 0; res <= 2; res++) {
for (planes = 0; planes <= 8; planes++) {
write_log ("%d: ",planes);
for (cycle = 0; cycle < 32; cycle++) {
v=cycle_diagram_table[fm][res][planes][cycle];
if (v==0) aa='-'; else if(v>0) aa='1'; else aa='X';
write_log ("%c",aa);
}
write_log (" %d:%d\n",
cycle_diagram_free_cycles[fm][res][planes], cycle_diagram_total_cycles[fm][res][planes]);
}
write_log ("\n");
}
}
fm=0;
}
#endif
static void create_cycle_diagram_table (void)
{
int fm, res, cycle, planes, rplanes, v;
int fetch_start, max_planes, freecycles;
const int *cycle_sequence;
for (fm = 0; fm <= 2; fm++) {
for (res = 0; res <= 2; res++) {
max_planes = fm_maxplanes[fm * 4 + res];
fetch_start = 1 << fetchstarts[fm * 4 + res];
cycle_sequence = &cycle_sequences[(max_planes - 1) * 8];
max_planes = 1 << max_planes;
for (planes = 0; planes <= 8; planes++) {
freecycles = 0;
for (cycle = 0; cycle < 32; cycle++)
cycle_diagram_table[fm][res][planes][cycle] = -1;
if (planes <= max_planes) {
for (cycle = 0; cycle < fetch_start; cycle++) {
if (cycle < max_planes && planes >= cycle_sequence[cycle & 7]) {
v = cycle_sequence[cycle & 7];
} else {
v = 0;
freecycles++;
}
cycle_diagram_table[fm][res][planes][cycle] = v;
}
}
cycle_diagram_free_cycles[fm][res][planes] = freecycles;
cycle_diagram_total_cycles[fm][res][planes] = fetch_start;
rplanes = planes;
if (rplanes > max_planes)
rplanes = 0;
if (rplanes == 7 && fm == 0 && res == 0 && !(currprefs.chipset_mask & CSMASK_AGA))
rplanes = 4;
real_bitplane_number[fm][res][planes] = rplanes;
}
}
}
#if 0
debug_cycle_diagram ();
#endif
}
/* Used by the copper. */
static int estimated_last_fetch_cycle;
static int cycle_diagram_shift;
static void estimate_last_fetch_cycle (int hpos)
{
int fetchunit = fetchunits[f_fetchmode * 4 + fa_bplcon0_res];
if (plfstate < plf_passed_stop) {
int stop = plfstop < hpos || plfstop > HARD_DDF_STOP ? HARD_DDF_STOP : plfstop;
/* We know that fetching is up-to-date up until hpos, so we can use fetch_cycle. */
int fetch_cycle_at_stop = fetch_cycle + (stop - hpos);
int starting_last_block_at = (fetch_cycle_at_stop + f_fetchunit - 1) & ~(f_fetchunit - 1);
estimated_last_fetch_cycle = hpos + (starting_last_block_at - fetch_cycle) + f_fetchunit;
} else {
int starting_last_block_at = (fetch_cycle + f_fetchunit - 1) & ~(f_fetchunit - 1);
if (plfstate == plf_passed_stop2)
starting_last_block_at -= fetchunit;
estimated_last_fetch_cycle = hpos + (starting_last_block_at - fetch_cycle) + f_fetchunit;
}
}
static uae_u32 outword[MAX_PLANES];
static int out_nbits, out_offs;
static uae_u32 todisplay[MAX_PLANES][4];
static uae_u32 fetched[MAX_PLANES];
#ifdef AGA
static uae_u32 fetched_aga0[MAX_PLANES];
static uae_u32 fetched_aga1[MAX_PLANES];
#endif
/* Expansions from bplcon0/bplcon1. */
static int toscr_res, toscr_nr_planes, toscr_nr_planes2, fetchwidth;
static int toscr_delay1x, toscr_delay2x, toscr_delay1, toscr_delay2;
/* The number of bits left from the last fetched words.
This is an optimization - conceptually, we have to make sure the result is
the same as if toscr is called in each clock cycle. However, to speed this
up, we accumulate display data; this variable keeps track of how much.
Thus, once we do call toscr_nbits (which happens at least every 16 bits),
we can do more work at once. */
static int toscr_nbits;
/* undocumented bitplane delay hardware feature */
static int delayoffset;
STATIC_INLINE void compute_delay_offset (void)
{
#ifdef NEWHSYNC
int v = 4;
#else
int v = 0;
#endif
delayoffset = (16 << f_fetchmode) - (((plfstrt - v - HARD_DDF_START) & f_fetchstart_mask) << 1);
#if 0
/* maybe we can finally get rid of this stupid table.. */
if (tmp == 4)
delayoffset = 4; // Loons Docs
else if (tmp == 8)
delayoffset = 8;
else if (tmp == 12) // Loons Docs
delayoffset = 4;
else if (tmp == 16) /* Overkill AGA */
delayoffset = 48;
else if (tmp == 24) /* AB 2 */
delayoffset = 8;
else if (tmp == 32)
delayoffset = 32;
else if (tmp == 48) /* Pinball Illusions AGA, ingame */
delayoffset = 16;
else /* what about 40 and 56? */
delayoffset = 0;
//write_log ("%d:%d ", vpos, delayoffset);
#endif
}
static void record_color_change2 (int hpos, int regno, unsigned long value)
{
curr_color_changes[next_color_change].linepos = hpos;
curr_color_changes[next_color_change].regno = regno;
curr_color_changes[next_color_change++].value = value;
curr_color_changes[next_color_change].regno = -1;
}
// OCS/ECS, lores, 7 planes = 4 "real" planes + BPL5DAT and BPL6DAT as 5th and 6th plane
STATIC_INLINE int isocs7planes (void)
{
return !(currprefs.chipset_mask & CSMASK_AGA) && fa_bplcon0_res == 0 && f_bplcon0_planes == 7;
}
static uae_u16 tmp_bplcon0, tmp_fmode;
STATIC_INLINE fetch_bpl_params (void)
{
tmp_bplcon0 = bplcon0;
tmp_fmode = fmode;
}
static void copy_bpl_params (int pos)
{
int changed = 0;
int fm;
if (f_bplcon0 != tmp_bplcon0)
changed = 1;
if (f_fmode != tmp_fmode)
changed = 1;
if (changed)
record_color_change2 (pos + 4, 0x100 + 0x1000, tmp_bplcon0);
f_bplcon0 = tmp_bplcon0;
f_fmode = tmp_fmode;
fm = fmode & 3;
if (fm == 0)
f_fetchmode = 0;
else if (fm == 1 || fm == 2)
f_fetchmode = 1;
else
f_fetchmode = 2;
fa_bplcon0_res = GET_RES_AGNUS (f_bplcon0);
fd_bplcon0_res = GET_RES_DENISE (f_bplcon0);
f_bplcon0_planes = GET_PLANES (f_bplcon0);
f_bplcon0_planes_limit = GET_PLANES_LIMIT (f_bplcon0);
f_fetchunit = fetchunits[fetchmode * 4 + fa_bplcon0_res];
f_fetchunit_mask = t_fetchunit - 1;
f_fetchstart_shift = fetchstarts[f_fetchmode * 4 + fa_bplcon0_res];
f_fetchstart = 1 << t_fetchstart_shift;
f_fetchstart_mask = t_fetchstart - 1;
f_fm_maxplane_shift = fm_maxplanes[f_fetchmode * 4 + fa_bplcon0_res];
f_fm_maxplane = 1 << f_fm_maxplane_shift;
curr_diagram = cycle_diagram_table[f_fetchmode][fa_bplcon0_res][f_bplcon0_planes_limit];
f_fetch_modulo_cycle = t_fetchunit - f_fetchstart;
if (toscr_nr_planes < f_bplcon0_planes_limit)
toscr_nr_planes = f_bplcon0_planes_limit;
if (isocs7planes ()) {
if (toscr_nr_planes2 < 6)
toscr_nr_planes2 = 6;
} else {
toscr_nr_planes2 = toscr_nr_planes;
}
toscr_res = fd_bplcon0_res;
}
static void expand_fmodes (void)
{
ta_bplcon0_res = GET_RES_AGNUS (bplcon0);
td_bplcon0_res = GET_RES_DENISE (bplcon0);
t_bplcon0_planes = GET_PLANES (bplcon0);
t_bplcon0_planes_limit = GET_PLANES_LIMIT (bplcon0);
t_fetchunit = fetchunits[fetchmode * 4 + ta_bplcon0_res];
t_fetchunit_mask = t_fetchunit - 1;
t_fetchstart_shift = fetchstarts[fetchmode * 4 + ta_bplcon0_res];
t_fetchstart = 1 << t_fetchstart_shift;
t_fetchstart_mask = t_fetchstart - 1;
}
/* Expand bplcon0/bplcon1 into the toscr_xxx variables. */
static void compute_toscr_delay_1 (void)
{
int delay1 = (bplcon1 & 0x0f) | ((bplcon1 & 0x0c00) >> 6);
int delay2 = ((bplcon1 >> 4) & 0x0f) | (((bplcon1 >> 4) & 0x0c00) >> 6);
int shdelay1 = (bplcon1 >> 12) & 3;
int shdelay2 = (bplcon1 >> 8) & 3;
int delaymask;
int fetchwidth = 16 << f_fetchmode;
delay1 += delayoffset;
delay2 += delayoffset;
delaymask = (fetchwidth - 1) >> toscr_res;
toscr_delay1x = (delay1 & delaymask) << toscr_res;
toscr_delay1x |= shdelay1 >> (RES_MAX - toscr_res);
toscr_delay2x = (delay2 & delaymask) << toscr_res;
toscr_delay2x |= shdelay2 >> (RES_MAX - toscr_res);
}
static void compute_toscr_delay (int hpos)
{
toscr_res = fd_bplcon0_res;
toscr_nr_planes = f_bplcon0_planes_limit;
toscr_nr_planes2 = f_bplcon0_planes;
compute_toscr_delay_1 ();
}
STATIC_INLINE void maybe_first_bpl1dat (int hpos)
{
if (thisline_decision.plfleft == -1) {
thisline_decision.plfleft = hpos;
compute_delay_offset ();
compute_toscr_delay_1 ();
}
}
STATIC_INLINE void fetch (int nr, int fm)
{
if (nr < toscr_nr_planes) {
#if NEW_BPL
uaecptr p = f_bplpt[nr] + bpl_off[nr];
#else
uaecptr p = bplpt[nr];
bplpt[nr] += 2 << fm;
#endif
switch (fm)
{
case 0:
fetched[nr] = bplxdat[nr] = last_custom_value = chipmem_agnus_wget (p);
break;
#ifdef AGA
case 1:
fetched_aga0[nr] = chipmem_lget (p);
last_custom_value = (uae_u16)fetched_aga0[nr];
break;
case 2:
fetched_aga1[nr] = chipmem_lget (p);
fetched_aga0[nr] = chipmem_lget (p + 4);
last_custom_value = (uae_u16)fetched_aga0[nr];
break;
#endif
}
if (plfstate == plf_passed_stop2 && fetch_cycle >= (fetch_cycle & ~f_fetchunit_mask) + f_fetch_modulo_cycle) {
int mod;
if (f_fmode & 0x4000) {
if (((diwstrt >> 8) ^ vpos) & 1)
mod = bpl2mod;
else
mod = bpl1mod;
} else if (nr & 1)
mod = bpl2mod;
else
mod = bpl1mod;
bplpt[nr] += mod;
bplptx[nr] += mod;
}
} else if (isocs7planes ()) {
fetched[nr] = bplxdat[nr];
}
if (nr == 0)
fetch_state = fetch_was_plane0;
#if NEW_BPL