-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.c
executable file
·6004 lines (5348 loc) · 170 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-2004 Toni Wilen
*/
//#define CUSTOM_DEBUG
#define SPRITE_DEBUG 0
#define SPRITE_DEBUG_MINY 0x6a
#define SPRITE_DEBUG_MAXY 0x70
#define SPR0_HPOS 0x15
#define MAX_SPRITES 8
#define SPRITE_COLLISIONS
#define SPEEDUP
#include "sysconfig.h"
#include "sysdeps.h"
#include <ctype.h>
#include <assert.h>
#include "options.h"
#include "uae.h"
#include "gensound.h"
#include "sounddep/sound.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"
//ric
//#include "profiling.h"
//#define PROFILINGINDEX 300
//#undef PROFILING
//end ric
static void uae_abort (const char *format,...)
{
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);
gui_message (buffer);
}
#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
static uae_u16 last_custom_value;
static unsigned int total_skipped __attribute__((__section__(".sdata"))) = 0;
STATIC_INLINE void sync_copper (unsigned int hpos);
/* Events */
unsigned int is_lastline __attribute__((__section__(".sdata")));
static int rpt_did_reset __attribute__((__section__(".sdata")));
volatile frame_time_t vsynctime, vsyncmintime;
#ifdef JIT
extern uae_u8* compiled_code;
#endif
unsigned int vpos __attribute__((__section__(".sdata")));
int hack_vpos __attribute__((__section__(".sdata")));
static uae_u16 lof;
static int next_lineno __attribute__((__section__(".sdata")));
static enum nln_how nextline_how;
int lof_changed __attribute__((__section__(".sdata"))) = 0; //ric
/* 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 __attribute__((__section__(".sdata")));
static int hpos_previous __attribute__((__section__(".sdata")));
static int vpos_lpen __attribute__((__section__(".sdata")));
static int hpos_lpen __attribute__((__section__(".sdata")));
static uae_u32 sprtaba[256] __attribute__((__section__(".sdata")));
static uae_u32 sprtabb[256] __attribute__((__section__(".sdata")));
static uae_u32 sprite_ab_merge[256] __attribute__((__section__(".sdata")));
/* Tables for collision detection. */
static uae_u32 sprclx[16] __attribute__((__section__(".sdata")));
static uae_u32 clxmask[16] __attribute__((__section__(".sdata")));
/*
* Hardware registers of all sorts.
*/
static int custom_wput_1 (unsigned int, uaecptr, uae_u32, int) REGPARAM;
uae_u16 intena,intreq;
uae_u16 dmacon;
uae_u16 adkcon; /* used by audio code */
static uae_u32 cop1lc __attribute__((__section__(".sdata")));
static uae_u32 cop2lc __attribute__((__section__(".sdata")));
static uae_u32 copcon __attribute__((__section__(".sdata")));
unsigned int maxhpos __attribute__((__section__(".sdata"))) = MAXHPOS_PAL;
unsigned int maxvpos __attribute__((__section__(".sdata"))) = MAXVPOS_PAL;
unsigned int minfirstline = VBLANK_ENDLINE_PAL;
int vblank_hz = VBLANK_HZ_PAL, fake_vblank_hz, vblank_skip;
frame_time_t syncbase __attribute__((__section__(".sdata")));
static int fmode __attribute__((__section__(".sdata")));
unsigned int beamcon0 __attribute__((__section__(".sdata")));
unsigned int new_beamcon0 __attribute__((__section__(".sdata")));
uae_u16 vtotal = MAXVPOS_PAL, htotal = MAXHPOS_PAL;
static uae_u16 hsstop, hbstrt, hbstop, vsstop, vbstrt, vbstop, hsstrt, vsstrt, hcenter;
/* This is but an educated guess. It seems to be correct, but this stuff
* isn't documented well. */
struct sprite {
uaecptr pt;
unsigned int xpos;
unsigned int vstart;
unsigned int vstop;
int armed;
int dmastate;
int dmacycle;
};
#ifdef GP2X
static struct sprite spr[8];
static unsigned int sprite_vblank_endline = 25;
#else
static struct sprite spr[MAX_SPRITES] __attribute__((__section__(".sdata")));
static unsigned int sprite_vblank_endline = VBLANK_SPRITE_PAL __attribute__((__section__(".sdata")));
#endif
// thinkp
static int mctl0 __attribute__((__section__(".sdata")));
static int mpos0 __attribute__((__section__(".sdata")));
static int mctl1 __attribute__((__section__(".sdata"))) = 0;
static int mpos1 __attribute__((__section__(".sdata"))) = 0;
static int mcnt __attribute__((__section__(".sdata"))) = 0;
// thinkp
static unsigned int sprctl[MAX_SPRITES] __attribute__((__section__(".sdata")));
static unsigned int sprpos[MAX_SPRITES] __attribute__((__section__(".sdata")));
#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 unsigned int last_sprite_point __attribute__((__section__(".sdata")));
static int nr_armed __attribute__((__section__(".sdata")));
static unsigned int sprite_width __attribute__((__section__(".sdata")));
static unsigned int sprres __attribute__((__section__(".sdata")));
static unsigned int sprite_buffer_res __attribute__((__section__(".sdata")));
#ifdef CPUEMU_6
uae_u8 cycle_line[256];
#endif
static uae_u32 bpl1dat __attribute__((__section__(".sdata")));
#if 0 /* useless */
static uae_u32 bpl2dat, bpl3dat, bpl4dat, bpl5dat, bpl6dat, bpl7dat, bpl8dat;
#endif
static uae_s16 bpl1mod, bpl2mod;
static uaecptr bplpt[8] __attribute__((__section__(".sdata")));
uae_u8 *real_bplpt[8] __attribute__((__section__(".sdata")));
/* Used as a debugging aid, to offset any bitplane temporarily. */
int bpl_off[8] __attribute__((__section__(".sdata"))) = { 0, 0, 0, 0, 0, 0, 0, 0 };
/*static int blitcount[256]; blitter debug */
static struct color_entry current_colors __attribute__((__section__(".sdata")));
static unsigned int bplcon0 __attribute__((__section__(".sdata")));
static unsigned int bplcon1 __attribute__((__section__(".sdata")));
static unsigned int bplcon2 __attribute__((__section__(".sdata")));
static unsigned int bplcon3 __attribute__((__section__(".sdata")));
static unsigned int bplcon4 __attribute__((__section__(".sdata")));
static unsigned int diwstrt __attribute__((__section__(".sdata")));
static unsigned int diwstop __attribute__((__section__(".sdata")));
static unsigned int diwhigh __attribute__((__section__(".sdata")));
static int diwhigh_written __attribute__((__section__(".sdata")));
static unsigned int ddfstrt __attribute__((__section__(".sdata")));
static unsigned int ddfstop __attribute__((__section__(".sdata")));
static unsigned int ddfstrt_old_hpos __attribute__((__section__(".sdata")));
static unsigned int ddfstrt_old_vpos __attribute__((__section__(".sdata")));
static unsigned int ddf_change __attribute__((__section__(".sdata")));
/* The display and data fetch windows */
enum diw_states
{
DIW_waiting_start, DIW_waiting_stop
};
unsigned int plffirstline __attribute__((__section__(".sdata")));
unsigned int plflastline __attribute__((__section__(".sdata")));
unsigned int plfstrt __attribute__((__section__(".sdata")));
unsigned int plfstop __attribute__((__section__(".sdata")));
static int last_diw_pix_hpos __attribute__((__section__(".sdata")));
static int last_ddf_pix_hpos __attribute__((__section__(".sdata")));
static int last_decide_line_hpos __attribute__((__section__(".sdata")));
static int last_sprite_decide_line_hpos __attribute__((__section__(".sdata")));
static int last_fetch_hpos __attribute__((__section__(".sdata")));
static unsigned int last_sprite_hpos __attribute__((__section__(".sdata")));
int diwfirstword __attribute__((__section__(".sdata")));
int diwlastword __attribute__((__section__(".sdata")));
static enum diw_states diwstate, hdiwstate, ddfstate;
/* Sprite collisions */
static unsigned int clxdat __attribute__((__section__(".sdata")));
static unsigned int clxcon __attribute__((__section__(".sdata")));
static unsigned int clxcon2 __attribute__((__section__(".sdata")));
static unsigned int clxcon_bpl_enable __attribute__((__section__(".sdata")));
static unsigned int clxcon_bpl_match __attribute__((__section__(".sdata")));
enum copper_states {
COP_stop,
COP_read1_in2,
COP_read1_wr_in4,
COP_read1_wr_in2,
COP_read1,
COP_read2_wr_in2,
COP_read2,
COP_bltwait,
COP_wait_in4,
COP_wait_in2,
COP_skip_in4,
COP_skip_in2,
COP_wait1,
COP_wait,
COP_skip1,
COP_strobe_delay
};
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;
unsigned int hpos;
unsigned int vpos;
unsigned int ignore_next;
unsigned int vcmp;
unsigned int hcmp;
int strobe; /* COPJMP1 / COPJMP2 accessed */
int last_write, last_write_hpos;
};
static struct copper cop_state __attribute__((__section__(".sdata")));
static int copper_enabled_thisline __attribute__((__section__(".sdata")));
//static int cop_min_waittime;
/*
* Statistics
*/
unsigned int frametime;
frame_time_t lastframetime;
unsigned int timeframes;
frame_time_t 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] __attribute__((__section__(".sdata")));
struct color_change *color_changes[2] __attribute__((__section__(".sdata")));
#ifdef GP2X
#define DEFAULT_MAX_SPRITE_ENTRY (1024*6)
#define DEFAULT_MAX_COLOR_CHANGE (1024*10)
#else
#define DEFAULT_MAX_SPRITE_ENTRY 400
#define DEFAULT_MAX_COLOR_CHANGE 400
#endif
static int max_sprite_entry __attribute__((__section__(".sdata")));
static int delta_sprite_entry __attribute__((__section__(".sdata")));
static int max_color_change __attribute__((__section__(".sdata")));
static int delta_color_change __attribute__((__section__(".sdata")));
#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];
static int next_sprite_entry __attribute__((__section__(".sdata"))) = 0;
static int prev_next_sprite_entry __attribute__((__section__(".sdata")));
static int next_sprite_forced __attribute__((__section__(".sdata"))) = 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 __attribute__((__section__(".sdata")));
static int next_color_entry __attribute__((__section__(".sdata")));
static int remembered_color_entry __attribute__((__section__(".sdata")));
static int color_src_match __attribute__((__section__(".sdata")));
static int color_dest_match __attribute__((__section__(".sdata")));
static int color_compare_result __attribute__((__section__(".sdata")));
static uae_u32 thisline_changed __attribute__((__section__(".sdata")));
#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 __attribute__((__section__(".sdata")));
static unsigned int passed_plfstop __attribute__((__section__(".sdata")));
static unsigned int fetch_cycle __attribute__((__section__(".sdata")));
static int fetch_modulo_cycle __attribute__((__section__(".sdata")));
enum fetchstate {
fetch_not_started,
fetch_started,
fetch_was_plane0
} fetch_state;
/*
* helper functions
*/
#ifdef ADVANCED_FS
extern unsigned char frameskipTable[50][50];
STATIC_INLINE int nodraw (void)
{
return (frameskipTable[currprefs.gfx_framerate][framecnt]==0);
}
#else
STATIC_INLINE int nodraw (void)
{
return framecnt != 0;
}
#endif
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;
}
}
void reset_frame_rate_hack (void)
{
if (currprefs.m68k_speed != -1)
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 void hsyncdelay(void)
{
#if 0
static unsigned int prevhpos;
#ifdef ADVANCED_CUST
while (current_hpos () >= prevhpos) //FOL
#else
while (current_hpos () == prevhpos)
do_cycles (CYCLE_UNIT);
#endif
prevhpos = current_hpos ();
#endif
}
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)
{
plpt -= chipmem_start & chipmem_mask;
plpt &= chipmem_mask;
if ((plpt + bytecount) <= allocated_chipmem)
return chipmemory + plpt;
else {
static int count = 0;
if (!count) {
count++;
write_log ("Warning: Bad playfield pointer\n");
}
return 0;
}
}
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] = CONVERT_RGB (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] = xcolors[v];
}
#ifdef AGA
}
#endif
}
void notice_new_xcolors (void)
{
int i;
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 (unsigned int currhp);
static void remember_ctable (void)
{
#ifdef PROFILING
PROFILINGBEGIN
#endif
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;
}
#ifdef PROFILING
PROFILINGEND(PROFILINGINDEX+12)
#endif
}
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_INLINE void decide_diw (unsigned int hpos)
{
int pix_hpos = coord_diw_to_window_x (hpos == 227 ? 455 : hpos * 2); /* (227.5*2 = 455) */
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 __attribute__((__section__(".sdata")));
static int real_bitplane_number[3][3][9] __attribute__((__section__(".sdata")));
/* 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(bc0);
int planes = GET_PLANES(bc0);
return real_bitplane_number[fetchmode][res][planes];
}
/* The HRM says 0xD8, but that can't work... */
#define HARD_DDF_STOP 0xd4
#define HARD_DDF_START 0x18
static void add_modulos (void)
{
int m1, m2;
if (fmode & 0x4000) {
if (((diwstrt >> 8) ^ vpos) & 1)
m1 = m2 = bpl2mod;
else
m1 = m2 = bpl1mod;
} else {
m1 = bpl1mod;
m2 = bpl2mod;
}
switch (GET_PLANES_LIMIT (bplcon0)) {
#ifdef AGA
case 8: bplpt[7] += m2;
case 7: bplpt[6] += m1;
#endif
case 6: bplpt[5] += m2;
case 5: bplpt[4] += m1;
case 4: bplpt[3] += m2;
case 3: bplpt[2] += m1;
case 2: bplpt[1] += m2;
case 1: bplpt[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 || (int)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 AGA
|| line_decisions[next_lneno].bplcon3 != thisline_decision.bplcon3
|| 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 unsigned int fetchunit __attribute__((__section__(".sdata")));
static unsigned int fetchunit_mask __attribute__((__section__(".sdata")));
/* 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 unsigned int fetchstart __attribute__((__section__(".sdata")));
static unsigned int fetchstart_shift __attribute__((__section__(".sdata")));
static unsigned int fetchstart_mask __attribute__((__section__(".sdata")));
/* 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 fm_maxplane __attribute__((__section__(".sdata")));
static int fm_maxplane_shift __attribute__((__section__(".sdata")));
/* The corresponding values, by fetchmode and display resolution. */
static unsigned int fetchunits[] __attribute__((__section__(".sdata"))) = { 8,8,8,0, 16,8,8,0, 32,16,8,0 };
static unsigned int fetchstarts[] __attribute__((__section__(".sdata"))) = { 3,2,1,0, 4,3,2,0, 5,4,3,0 };
static unsigned int fm_maxplanes[] __attribute__((__section__(".sdata"))) = { 3,2,1,0, 3,3,2,0, 3,3,3,0 };
static uae_u8 cycle_diagram_table[3][3][9][32] __attribute__((__section__(".sdata")));
static uae_u8 cycle_diagram_free_cycles[3][3][9] __attribute__((__section__(".sdata")));
static uae_u8 cycle_diagram_total_cycles[3][3][9] __attribute__((__section__(".sdata")));
static uae_u8 *curr_diagram __attribute__((__section__(".sdata")));
static uae_u8 cycle_sequences[3 * 8] __attribute__((__section__(".sdata"))) = { 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)
{
unsigned int fm, res, cycle, planes, rplanes, v;
unsigned int fetch_start, max_planes, freecycles;
uae_u8 *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 unsigned int estimated_last_fetch_cycle __attribute__((__section__(".sdata")));
static unsigned int cycle_diagram_shift __attribute__((__section__(".sdata")));
static void estimate_last_fetch_cycle (unsigned int hpos)
{
unsigned int fetchunit = fetchunits[fetchmode * 4 + GET_RES (bplcon0)];
if (! passed_plfstop) {
unsigned 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. */
unsigned int fetch_cycle_at_stop = fetch_cycle + (stop - hpos);
unsigned int starting_last_block_at = (fetch_cycle_at_stop + fetchunit - 1) & ~(fetchunit - 1);
estimated_last_fetch_cycle = hpos + (starting_last_block_at - fetch_cycle) + fetchunit;
} else {
unsigned int starting_last_block_at = (fetch_cycle + fetchunit - 1) & ~(fetchunit - 1);
if (passed_plfstop == 2)
starting_last_block_at -= fetchunit;
estimated_last_fetch_cycle = hpos + (starting_last_block_at - fetch_cycle) + fetchunit;
}
}
static uae_u32 outword[MAX_PLANES] __attribute__((__section__(".sdata")));
static int out_nbits __attribute__((__section__(".sdata")));
static int out_offs __attribute__((__section__(".sdata")));
static uae_u32 todisplay[MAX_PLANES][4] __attribute__((__section__(".sdata")));
static uae_u32 fetched[MAX_PLANES] __attribute__((__section__(".sdata")));
#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 __attribute__((__section__(".sdata")));
static int toscr_nr_planes __attribute__((__section__(".sdata")));
static int fetchwidth __attribute__((__section__(".sdata")));
static int toscr_delay1x __attribute__((__section__(".sdata")));
static int toscr_delay2x __attribute__((__section__(".sdata")));
static int toscr_delay1 __attribute__((__section__(".sdata")));
static int toscr_delay2 __attribute__((__section__(".sdata")));
/* 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 __attribute__((__section__(".sdata")));
/* undocumented bitplane delay hardware feature */
static int delayoffset __attribute__((__section__(".sdata")));
STATIC_INLINE void compute_delay_offset (void)
{
delayoffset = (16 << fetchmode) - (((plfstrt - HARD_DDF_START) & 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 expand_fmodes (void)
{
int res = GET_RES(bplcon0);
int fm = fetchmode;
fetchunit = fetchunits[fm * 4 + res];
fetchunit_mask = fetchunit - 1;
fetchstart_shift = fetchstarts[fm * 4 + res];
fetchstart = 1 << fetchstart_shift;
fetchstart_mask = fetchstart - 1;
fm_maxplane_shift = fm_maxplanes[fm * 4 + res];
fm_maxplane = 1 << fm_maxplane_shift;
curr_diagram = cycle_diagram_table[fm][res][GET_PLANES_LIMIT (bplcon0)];
fetch_modulo_cycle = fetchunit - fetchstart;
}
/* 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 delaymask;
int fetchwidth = 16 << fetchmode;
delay1 += delayoffset;
delay2 += delayoffset;
delaymask = (fetchwidth - 1) >> toscr_res;
toscr_delay1x = (delay1 & delaymask) << toscr_res;
toscr_delay2x = (delay2 & delaymask) << toscr_res;
}
static void compute_toscr_delay (int hpos)
{
toscr_res = GET_RES (bplcon0);
toscr_nr_planes = GET_PLANES_LIMIT (bplcon0);
compute_toscr_delay_1 ();
}
STATIC_INLINE void maybe_first_bpl1dat (unsigned int hpos)
{
if (thisline_decision.plfleft == -1) {
thisline_decision.plfleft = hpos;
compute_delay_offset ();
compute_toscr_delay_1 ();
}
}
//ric perf STATIC_INLINE void fetch (int nr, int fm)
STATIC_INLINE void fetch (int nr)
{
uaecptr p;
if (nr >= toscr_nr_planes)
return;
p = bplpt[nr];
//ric perf switch (fm) {
//ric perf case 0:
fetched[nr] = last_custom_value = chipmem_wget (p);
bplpt[nr] += 2;
//ric perf break;
#ifdef AGA
case 1:
fetched_aga0[nr] = chipmem_lget (p);
last_custom_value = (uae_u16)fetched_aga0[nr];
bplpt[nr] += 4;
break;
case 2:
fetched_aga1[nr] = chipmem_lget (p);
fetched_aga0[nr] = chipmem_lget (p + 4);
last_custom_value = (uae_u16)fetched_aga0[nr];
bplpt[nr] += 8;
break;
#endif
//ric perf }
if (passed_plfstop == 2 && fetch_cycle >= (fetch_cycle & ~fetchunit_mask) + fetch_modulo_cycle) {
int mod;
if (fmode & 0x4000) {
if (((diwstrt >> 8) ^ vpos) & 1)
mod = bpl2mod;
else
mod = bpl1mod;
} else if (nr & 1)
mod = bpl2mod;
else
mod = bpl1mod;
bplpt[nr] += mod;
}
if (nr == 0)
fetch_state = fetch_was_plane0;
}
static void clear_fetchbuffer (uae_u32 *ptr, int nwords)
{
int i;
if (! thisline_changed)
for (i = 0; i < nwords; i++)
if (ptr[i]) {
thisline_changed = 1;
break;
}
memset (ptr, 0, nwords * 4);
}
static void update_toscr_planes (void)
{
if (toscr_nr_planes > thisline_decision.nr_planes) {
int j;
for (j = thisline_decision.nr_planes; j < toscr_nr_planes; j++)
clear_fetchbuffer ((uae_u32 *)(line_data[next_lineno] + 2 * MAX_WORDS_PER_LINE * j), out_offs);
#if 0
if (thisline_decision.nr_planes > 0)
printf ("Planes from %d to %d\n", thisline_decision.nr_planes, toscr_nr_planes);
#endif
thisline_decision.nr_planes = toscr_nr_planes;
}
}
STATIC_INLINE void toscr_3_ecs (int nbits)
{
int delay1 = toscr_delay1;
int delay2 = toscr_delay2;
int i,j=1;
uae_u32 mask = 0xFFFF >> (16 - nbits);
/*ric perf
for (i = 0; i < toscr_nr_planes; i += 2) {
outword[i] <<= nbits;
outword[i] |= (todisplay[i][0] >> (16 - nbits + delay1)) & mask;
todisplay[i][0] <<= nbits;
}
for (i = 1; i < toscr_nr_planes; i += 2) {
outword[i] <<= nbits;
outword[i] |= (todisplay[i][0] >> (16 - nbits + delay2)) & mask;
todisplay[i][0] <<= nbits;
}
*/
for (i = 0; i < toscr_nr_planes; i += 2) {
outword[i] <<= nbits;
outword[i] |= (todisplay[i][0] >> (16 - nbits + delay1)) & mask;
todisplay[i][0] <<= nbits;
outword[j] <<= nbits;
outword[j] |= (todisplay[j][0] >> (16 - nbits + delay2)) & mask;
todisplay[j][0] <<= nbits;
j+=2;
}
}
STATIC_INLINE void shift32plus (uae_u32 *p, int n)
{
uae_u32 t = p[1];
t <<= n;
t |= p[0] >> (32 - n);
p[1] = t;
}
#ifdef AGA
STATIC_INLINE void aga_shift (uae_u32 *p, int n, int fm)
{
if (fm == 2) {
shift32plus (p + 2, n);
shift32plus (p + 1, n);
}
shift32plus (p + 0, n);
p[0] <<= n;
}
STATIC_INLINE void toscr_3_aga (int nbits, int fm)
{
int delay1 = toscr_delay1;
int delay2 = toscr_delay2;
int i;
uae_u32 mask = 0xFFFF >> (16 - nbits);
{
int offs = (16 << fm) - nbits + delay1;
int off1 = offs >> 5;
if (off1 == 3)
off1 = 2;
offs -= off1 * 32;
for (i = 0; i < toscr_nr_planes; i += 2) {
uae_u32 t0 = todisplay[i][off1];