forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
drawing.cpp
4654 lines (4146 loc) · 141 KB
/
drawing.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
*
* Screen drawing functions
*
* Copyright 1995-2000 Bernd Schmidt
* Copyright 1995 Alessandro Bissacco
* Copyright 2000-2008 Toni Wilen
*/
/* There are a couple of concepts of "coordinates" in this file.
- DIW coordinates
- DDF coordinates (essentially cycles, resolution lower than lores by a factor of 2)
- Pixel coordinates
* in the Amiga's resolution as determined by BPLCON0 ("Amiga coordinates")
* in the window resolution as determined by the preferences ("window coordinates").
* in the window resolution, and with the origin being the topmost left corner of
the window ("native coordinates")
One note about window coordinates. The visible area depends on the width of the
window, and the centering code. The first visible horizontal window coordinate is
often _not_ 0, but the value of VISIBLE_LEFT_BORDER instead.
One important thing to remember: DIW coordinates are in the lowest possible
resolution.
To prevent extremely bad things (think pixels cut in half by window borders) from
happening, all ports should restrict window widths to be multiples of 16 pixels. */
#define SPRITE_DEBUG_HIDE 0
#include "sysconfig.h"
#include "sysdeps.h"
#include <ctype.h>
#include <assert.h>
#include "options.h"
#include "threaddep/thread.h"
#include "uae.h"
#include "memory.h"
#include "custom.h"
#include "newcpu.h"
#include "xwin.h"
#include "autoconf.h"
#include "gui.h"
#include "picasso96.h"
#include "drawing.h"
#include "savestate.h"
#include "statusline.h"
#include "inputdevice.h"
#include "debug.h"
#include "cd32_fmv.h"
#include "specialmonitors.h"
#include "devices.h"
#include "gfxboard.h"
#define BG_COLOR_DEBUG 0
//#define XLINECHECK
struct amigadisplay adisplays[MAX_AMIGADISPLAYS];
typedef enum
{
CMODE_NORMAL,
CMODE_DUALPF,
CMODE_EXTRAHB,
CMODE_EXTRAHB_ECS_KILLEHB,
CMODE_HAM
} CMODE_T;
extern int sprite_buffer_res;
static int lores_factor;
int lores_shift, shres_shift;
static void pfield_set_linetoscr(void);
int debug_bpl_mask = 0xff, debug_bpl_mask_one;
static void lores_set(int lores)
{
int old = lores_shift;
lores_shift = lores;
if (lores_shift != old || shres_shift != RES_MAX - lores) {
shres_shift = RES_MAX - lores;
pfield_set_linetoscr();
}
}
static void lores_reset (void)
{
lores_factor = currprefs.gfx_resolution ? 2 : 1;
lores_set(currprefs.gfx_resolution);
if (doublescan > 0) {
int ls = lores_shift;
if (ls < 2) {
ls++;
}
lores_factor = 2;
lores_set(ls);
}
sprite_buffer_res = currprefs.gfx_resolution;
if (doublescan > 0 && sprite_buffer_res < RES_SUPERHIRES)
sprite_buffer_res++;
}
bool aga_mode; /* mirror of chipset_mask & CSMASK_AGA */
bool direct_rgb;
/* The shift factor to apply when converting between Amiga coordinates and window
coordinates. Zero if the resolution is the same, positive if window coordinates
have a higher resolution (i.e. we're stretching the image), negative if window
coordinates have a lower resolution (i.e. we're shrinking the image). */
static int res_shift;
static int linedbl, linedbld;
int interlace_seen;
int detected_screen_resolution;
#define AUTO_LORES_FRAMES 10
static int can_use_lores = 0, frame_res, frame_res_lace;
static int resolution_count[RES_MAX + 1], lines_count;
static bool center_reset;
static bool init_genlock_data;
bool need_genlock_data;
/* Lookup tables for dual playfields. The dblpf_*1 versions are for the case
that playfield 1 has the priority, dbplpf_*2 are used if playfield 2 has
priority. If we need an array for non-dual playfield mode, it has no number. */
/* The dbplpf_ms? arrays contain a shift value. plf_spritemask is initialized
to contain two 16 bit words, with the appropriate mask if pf1 is in the
foreground being at bit offset 0, the one used if pf2 is in front being at
offset 16. */
static int dblpf_ms1[256], dblpf_ms2[256], dblpf_ms[256];
static int dblpf_ind1[256], dblpf_ind2[256];
static int dblpf_2nd1[256], dblpf_2nd2[256];
static const int dblpfofs[] = { 0, 2, 4, 8, 16, 32, 64, 128 };
static int sprite_offs[256];
static uae_u32 clxtab[256];
/* Video buffer description structure. Filled in by the graphics system
* dependent code. */
/* OCS/ECS color lookup table. */
xcolnr xcolors[4096];
struct spritepixelsbuf {
bool attach;
uae_u8 stdata;
uae_u16 stfmdata;
uae_u16 data;
};
static struct spritepixelsbuf spritepixels_buffer[MAX_PIXELS_PER_LINE];
static struct spritepixelsbuf *spritepixels;
static int sprite_first_x, sprite_last_x;
#ifdef AGA
/* AGA mode color lookup tables */
unsigned int xredcolors[256], xgreencolors[256], xbluecolors[256];
static int dblpf_ind1_aga[256], dblpf_ind2_aga[256];
#else
static uae_u8 spriteagadpfpixels[1];
static int dblpf_ind1_aga[1], dblpf_ind2_aga[1];
#endif
int xredcolor_s, xredcolor_b, xredcolor_m;
int xgreencolor_s, xgreencolor_b, xgreencolor_m;
int xbluecolor_s, xbluecolor_b, xbluecolor_m;
struct color_entry colors_for_drawing;
static struct color_entry direct_colors_for_drawing;
static xcolnr *p_acolors;
static xcolnr *p_xcolors;
/* The size of these arrays is pretty arbitrary; it was chosen to be "more
than enough". The coordinates used for indexing into these arrays are
almost, but not quite, Amiga coordinates (there's a constant offset). */
static union {
uae_u64 apixels_q[MAX_PIXELS_PER_LINE * 2 / sizeof(uae_u64)];
uae_u32 apixels_l[MAX_PIXELS_PER_LINE * 2 / sizeof(uae_u32)];
uae_u8 apixels[MAX_PIXELS_PER_LINE * 2];
} pixdata;
static uae_u8 *refresh_indicator_buffer;
static uae_u8 *refresh_indicator_changed, *refresh_indicator_changed_prev;
static int refresh_indicator_height;
#ifdef OS_WITHOUT_MEMORY_MANAGEMENT
uae_u16 *spixels;
#else
uae_u16 spixels[2 * MAX_SPR_PIXELS];
#endif
struct sprite_stb spixstate;
static uae_u32 ham_linebuf[MAX_PIXELS_PER_LINE * 2];
static uae_u8 *real_bplpt[8];
static uae_u8 all_ones[MAX_PIXELS_PER_LINE];
static uae_u8 all_zeros[MAX_PIXELS_PER_LINE];
uae_u8 *xlinebuffer, *xlinebuffer_genlock;
static int *amiga2aspect_line_map, *native2amiga_line_map;
static uae_u8 **row_map;
static uae_u8 *row_map_genlock_buffer;
static uae_u8 row_tmp[MAX_PIXELS_PER_LINE * 32 / 8];
static int max_drawn_amiga_line;
uae_u8 **row_map_genlock;
uae_u8 *row_map_color_burst_buffer;
/* line_draw_funcs: pfield_do_linetoscr, pfield_do_fill_line, decode_ham */
typedef void (*line_draw_func)(int, int, int);
#define LINE_UNDECIDED 1
#define LINE_DECIDED 2
#define LINE_DECIDED_DOUBLE 3
#define LINE_AS_PREVIOUS 4
#define LINE_BLACK 5
#define LINE_REMEMBERED_AS_BLACK 6
#define LINE_DONE 7
#define LINE_DONE_AS_PREVIOUS 8
#define LINE_REMEMBERED_AS_PREVIOUS 9
#define LINESTATE_SIZE ((MAXVPOS + 2) * 2 + 1)
static uae_u8 linestate[LINESTATE_SIZE];
uae_u8 line_data[(MAXVPOS + 2) * 2][MAX_PLANES * MAX_WORDS_PER_LINE * 2];
/* Centering variables. */
static int min_diwstart, max_diwstop;
/* The visible window: VISIBLE_LEFT_BORDER contains the left border of the visible
area, VISIBLE_RIGHT_BORDER the right border. These are in window coordinates. */
int visible_left_border, visible_right_border;
/* Pixels outside of visible_start and visible_stop are always black */
static int visible_left_start, visible_right_stop;
static int visible_top_start, visible_bottom_stop;
/* same for hblank */
static int hblank_left_start, hblank_right_stop;
static int linetoscr_x_adjust_pixbytes, linetoscr_x_adjust_pixels;
static int thisframe_y_adjust;
static int thisframe_y_adjust_real, max_ypos_thisframe, min_ypos_for_screen;
int thisframe_first_drawn_line, thisframe_last_drawn_line;
/* A frame counter that forces a redraw after at least one skipped frame in
interlace mode. */
static int last_redraw_point;
#define MAX_STOP 30000
static int first_drawn_line, last_drawn_line;
/* These are generated by the drawing code from the line_decisions array for
each line that needs to be drawn. These are basically extracted out of
bit fields in the hardware registers. */
static int bplmode, bplehb, bplham, bpldualpf, bpldualpfpri;
static int bpldualpf2of, bplplanecnt, ecsshres;
static int bplbypass, bplcolorburst, bplcolorburst_field;
static bool issprites;
static int bplres;
static int plf1pri, plf2pri, bplxor, bpland, bpldelay_sh;
static uae_u32 plf_sprite_mask;
static int sbasecol[2] = { 16, 16 };
static int hposblank;
static bool ecs_genlock_features_active;
static uae_u8 ecs_genlock_features_mask;
static bool ecs_genlock_features_colorkey;
static int hsync_shift_hack;
static bool sprite_smaller_than_64, sprite_smaller_than_64_inuse;
uae_sem_t gui_sem;
void set_inhibit_frame(int monid, int bit)
{
struct amigadisplay *ad = &adisplays[monid];
ad->inhibit_frame |= 1 << bit;
}
void clear_inhibit_frame(int monid, int bit)
{
struct amigadisplay *ad = &adisplays[monid];
ad->inhibit_frame &= ~(1 << bit);
}
void toggle_inhibit_frame(int monid, int bit)
{
struct amigadisplay *ad = &adisplays[monid];
ad->inhibit_frame ^= 1 << bit;
}
#ifdef XLINECHECK
static void xlinecheck (unsigned int start, unsigned int end)
{
unsigned int xstart = (unsigned int)xlinebuffer + start * vidinfo->drawbuffer.pixbytes;
unsigned int xend = (unsigned int)xlinebuffer + end * vidinfo->drawbuffer.pixbytes;
unsigned int end1 = (unsigned int)vidinfo->drawbuffer.bufmem + vidinfo->drawbuffer.rowbytes * vidinfo->drawbuffer.height;
int min = linetoscr_x_adjust_bytes / vidinfo->drawbuffer.pixbytes;
int ok = 1;
if (xstart >= vidinfo->drawbuffer.emergmem && xstart < vidinfo->drawbuffer.emergmem + 4096 * vidinfo->drawbuffer.pixbytes &&
xend >= vidinfo->drawbuffer.emergmem && xend < vidinfo->drawbuffer.emergmem + 4096 * vidinfo->drawbuffer.pixbytes)
return;
if (xstart < (unsigned int)vidinfo->drawbuffer.bufmem || xend < (unsigned int)vidinfo->drawbuffer.bufmem)
ok = 0;
if (xend > end1 || xstart >= end1)
ok = 0;
xstart -= (unsigned int)vidinfo->drawbuffer.bufmem;
xend -= (unsigned int)vidinfo->drawbuffer.bufmem;
if ((xstart % vidinfo->drawbuffer.rowbytes) >= vidinfo->drawbuffer.width * vidinfo->drawbuffer.pixbytes)
ok = 0;
if ((xend % vidinfo->drawbuffer.rowbytes) >= vidinfo->drawbuffer.width * vidinfo->drawbuffer.pixbytes)
ok = 0;
if (xstart >= xend)
ok = 0;
if (xend - xstart > vidinfo->drawbuffer.width * vidinfo->drawbuffer.pixbytes)
ok = 0;
if (!ok) {
write_log (_T("*** %d-%d (%dx%dx%d %d) %p\n"),
start - min, end - min, vidinfo->drawbuffer.width, vidinfo->drawbuffer.height,
vidinfo->drawbuffer.pixbytes, vidinfo->drawbuffer.rowbytes,
xlinebuffer);
}
}
#else
#define xlinecheck(start, end)
#endif
static void clearbuffer (struct vidbuffer *dst)
{
if (!dst->bufmem_allocated)
return;
uae_u8 *p = dst->bufmem_allocated;
for (int y = 0; y < dst->height_allocated; y++) {
memset (p, 0, dst->width_allocated * dst->pixbytes);
p += dst->rowbytes;
}
}
static void reset_decision_table (void)
{
for (int i = 0; i < sizeof linestate / sizeof *linestate; i++) {
linestate[i] = LINE_UNDECIDED;
}
}
static void count_frame(int monid)
{
struct amigadisplay *ad = &adisplays[monid];
ad->framecnt++;
if (ad->framecnt >= currprefs.gfx_framerate || currprefs.monitoremu == MONITOREMU_A2024)
ad->framecnt = 0;
if (ad->inhibit_frame)
ad->framecnt = 1;
}
STATIC_INLINE int xshift (int x, int shift)
{
if (shift < 0)
return x >> (-shift);
else
return x << shift;
}
int coord_native_to_amiga_x (int x)
{
x += visible_left_border;
x = xshift (x, 1 - lores_shift);
return x + 2 * DISPLAY_LEFT_SHIFT - 2 * DIW_DDF_OFFSET;
}
int coord_native_to_amiga_y (int y)
{
if (!native2amiga_line_map)
return -1;
return native2amiga_line_map[y] + thisframe_y_adjust - minfirstline;
}
STATIC_INLINE int res_shift_from_window (int x)
{
if (res_shift >= 0)
return x >> res_shift;
return x << -res_shift;
}
STATIC_INLINE int res_shift_from_amiga (int x)
{
if (res_shift >= 0)
return x >> res_shift;
return x << -res_shift;
}
void notice_screen_contents_lost(int monid)
{
struct amigadisplay *ad = &adisplays[monid];
ad->picasso_redraw_necessary = 1;
ad->frame_redraw_necessary = 2;
}
bool isnativevidbuf(int monid)
{
struct vidbuf_description *vidinfo = &adisplays[monid].gfxvidinfo;
if (vidinfo->outbuffer == NULL)
return false;
if (vidinfo->outbuffer == &vidinfo->drawbuffer)
return true;
return vidinfo->outbuffer->nativepositioning;
}
extern int plffirstline_total, plflastline_total;
extern int first_planes_vpos, last_planes_vpos;
extern int diwfirstword_total, diwlastword_total;
extern int ddffirstword_total, ddflastword_total;
extern bool vertical_changed, horizontal_changed;
extern int firstword_bplcon1;
extern int lof_store;
#define MIN_DISPLAY_W 256
#define MIN_DISPLAY_H 192
#define MAX_DISPLAY_W 362
#define MAX_DISPLAY_H 283
static int gclow, gcloh, gclox, gcloy, gclorealh;
static int stored_left_start, stored_top_start, stored_width, stored_height;
void get_custom_topedge (int *xp, int *yp, bool max)
{
if (isnativevidbuf(0) && !max) {
int x, y;
x = visible_left_border + (DISPLAY_LEFT_SHIFT << currprefs.gfx_resolution);
y = minfirstline << currprefs.gfx_vresolution;
#if 0
int dbl1, dbl2;
dbl2 = dbl1 = currprefs.gfx_vresolution;
if (doublescan > 0 && interlace_seen <= 0) {
dbl1--;
dbl2--;
}
x = -(visible_left_border + (DISPLAY_LEFT_SHIFT << currprefs.gfx_resolution));
y = -minfirstline << currprefs.gfx_vresolution;
y = xshift (y, dbl2);
#endif
*xp = x;
*yp = y;
} else {
*xp = 0;
*yp = 0;
}
}
static void reset_custom_limits (void)
{
gclow = gcloh = gclox = gcloy = 0;
gclorealh = -1;
center_reset = true;
}
static void set_blanking_limits (void)
{
hblank_left_start = visible_left_start;
hblank_right_stop = visible_right_stop;
if (programmedmode) {
if (hblank_left_start < coord_hw_to_window_x (hsyncendpos * 2))
hblank_left_start = coord_hw_to_window_x (hsyncendpos * 2);
if (hblank_right_stop > coord_hw_to_window_x (hsyncstartpos * 2))
hblank_right_stop = coord_hw_to_window_x (hsyncstartpos * 2);
}
}
void get_custom_raw_limits (int *pw, int *ph, int *pdx, int *pdy)
{
if (stored_width > 0) {
*pw = stored_width;
*ph = stored_height;
*pdx = stored_left_start;
*pdy = stored_top_start;
} else {
int x = visible_left_border;
if (x < visible_left_start)
x = visible_left_start;
*pdx = x;
int x2 = visible_right_border;
if (x2 > visible_right_stop)
x2 = visible_right_stop;
*pw = x2 - x;
int y = min_ypos_for_screen;
if (y < visible_top_start)
y = visible_top_start;
*pdy = y;
int y2 = max_ypos_thisframe;
if (y2 > visible_bottom_stop)
y2 = visible_bottom_stop;
*ph = y2 - y;
}
}
void check_custom_limits(void)
{
struct gfx_filterdata *fd = &currprefs.gf[0];
int vls = visible_left_start;
int vrs = visible_right_stop;
int vts = visible_top_start;
int vbs = visible_bottom_stop;
int left = fd->gfx_filter_left_border < 0 ? 0 : fd->gfx_filter_left_border >> (RES_MAX - currprefs.gfx_resolution);
int right = fd->gfx_filter_right_border < 0 ? 0 : fd->gfx_filter_right_border >> (RES_MAX - currprefs.gfx_resolution);
int top = fd->gfx_filter_top_border < 0 ? 0 : fd->gfx_filter_top_border;
int bottom = fd->gfx_filter_bottom_border < 0 ? 0 : fd->gfx_filter_bottom_border;
if (left > visible_left_start)
visible_left_start = left;
if (right > left && right < visible_right_stop)
visible_right_stop = right;
if (top > visible_top_start)
visible_top_start = top;
if (bottom > top && bottom < visible_bottom_stop)
visible_bottom_stop = bottom;
set_blanking_limits ();
}
void set_custom_limits (int w, int h, int dx, int dy)
{
struct gfx_filterdata *fd = &currprefs.gf[0];
int vls = visible_left_start;
int vrs = visible_right_stop;
int vts = visible_top_start;
int vbs = visible_bottom_stop;
if (fd->gfx_filter_left_border == 0) {
w = 0;
dx = 0;
}
if (fd->gfx_filter_top_border == 0) {
h = 0;
dy = 0;
}
if (specialmonitor_uses_control_lines()) {
w = -1;
h = -1;
}
if (w <= 0 || dx < 0) {
visible_left_start = 0;
visible_right_stop = MAX_STOP;
} else {
visible_left_start = visible_left_border + dx;
visible_right_stop = visible_left_start + w;
}
if (h <= 0 || dy < 0) {
visible_top_start = 0;
visible_bottom_stop = MAX_STOP;
} else {
visible_top_start = min_ypos_for_screen + dy;
visible_bottom_stop = visible_top_start + h;
}
if (vls != visible_left_start || vrs != visible_right_stop ||
vts != visible_top_start || vbs != visible_bottom_stop)
notice_screen_contents_lost(0);
check_custom_limits();
}
void store_custom_limits (int w, int h, int x, int y)
{
stored_left_start = x;
stored_top_start = y;
stored_width = w;
stored_height = h;
#if 0
write_log (_T("%dx%d %dx%d %dx%d %dx%d %d\n"), x, y, w, h,
currprefs.gfx_xcenter_pos,
currprefs.gfx_ycenter_pos,
currprefs.gfx_xcenter_size,
currprefs.gfx_ycenter_size,
currprefs.gfx_filter_autoscale);
#endif
}
int get_custom_limits (int *pw, int *ph, int *pdx, int *pdy, int *prealh)
{
struct vidbuf_description *vidinfo = &adisplays[0].gfxvidinfo;
int w, h, dx, dy, y1, y2, dbl1, dbl2;
int ret = 0;
if (!pw || !ph || !pdx || !pdy) {
reset_custom_limits ();
return 0;
}
if (!isnativevidbuf(0)) {
*pw = vidinfo->outbuffer->outwidth;
*ph = vidinfo->outbuffer->outheight;
*pdx = 0;
*pdy = 0;
*prealh = -1;
return 1;
}
*pw = gclow;
*ph = gcloh;
*pdx = gclox;
*pdy = gcloy;
*prealh = gclorealh;
if (gclow > 0 && gcloh > 0)
ret = -1;
if (interlace_seen) {
static int interlace_count;
// interlace = only use long frames
if (lof_store && (interlace_count & 1) == 0)
interlace_count++;
if (!lof_store && (interlace_count & 1) != 0)
interlace_count++;
if (interlace_count < 3)
return ret;
if (!lof_store)
return ret;
interlace_count = 0;
/* program may have set last visible line as last possible line (CD32 boot screen) */
if (last_planes_vpos < maxvpos)
last_planes_vpos++;
if (plflastline_total < maxvpos)
plflastline_total++;
}
if (plflastline_total < 4)
plflastline_total = last_planes_vpos;
ddffirstword_total = coord_hw_to_window_x (ddffirstword_total * 2 + DIW_DDF_OFFSET);
ddflastword_total = coord_hw_to_window_x (ddflastword_total * 2 + DIW_DDF_OFFSET);
if (doublescan <= 0 && !programmedmode) {
int min = coord_diw_lores_to_window_x (92);
int max = coord_diw_lores_to_window_x (460);
if (diwfirstword_total < min)
diwfirstword_total = min;
if (diwlastword_total > max)
diwlastword_total = max;
if (ddffirstword_total < min)
ddffirstword_total = min;
if (ddflastword_total > max)
ddflastword_total = max;
if (0 && !(currprefs.chipset_mask & CSMASK_AGA)) {
if (ddffirstword_total > diwfirstword_total)
diwfirstword_total = ddffirstword_total;
if (ddflastword_total < diwlastword_total)
diwlastword_total = ddflastword_total;
}
}
w = diwlastword_total - diwfirstword_total;
dx = diwfirstword_total - visible_left_border;
y2 = plflastline_total;
if (y2 > last_planes_vpos)
y2 = last_planes_vpos;
y1 = plffirstline_total;
if (first_planes_vpos > y1)
y1 = first_planes_vpos;
if (minfirstline > y1)
y1 = minfirstline;
dbl2 = dbl1 = currprefs.gfx_vresolution;
if (doublescan > 0 && interlace_seen <= 0) {
dbl1--;
dbl2--;
}
h = y2 - y1;
dy = y1 - minfirstline;
if (first_planes_vpos == 0) {
// no planes enabled during frame
if (ret < 0)
return 1;
h = currprefs.ntscmode ? 200 : 240;
w = 320 << currprefs.gfx_resolution;
dy = 36 / 2;
dx = 58;
}
if (dx < 0)
dx = 0;
*prealh = -1;
if (!programmedmode && first_planes_vpos) {
int th = (maxvpos - minfirstline) * 95 / 100;
if (th > h) {
th = xshift (th, dbl1);
*prealh = th;
}
}
dy = xshift (dy, dbl2);
h = xshift (h, dbl1);
if (w == 0 || h == 0)
return 0;
if (doublescan <= 0 && !programmedmode) {
if ((w >> currprefs.gfx_resolution) < MIN_DISPLAY_W) {
dx += (w - (MIN_DISPLAY_W << currprefs.gfx_resolution)) / 2;
w = MIN_DISPLAY_W << currprefs.gfx_resolution;
}
if ((h >> dbl1) < MIN_DISPLAY_H) {
dy += (h - (MIN_DISPLAY_H << dbl1)) / 2;
h = MIN_DISPLAY_H << dbl1;
}
if ((w >> currprefs.gfx_resolution) > MAX_DISPLAY_W) {
dx += (w - (MAX_DISPLAY_W << currprefs.gfx_resolution)) / 2;
w = MAX_DISPLAY_W << currprefs.gfx_resolution;
}
if ((h >> dbl1) > MAX_DISPLAY_H) {
dy += (h - (MAX_DISPLAY_H << dbl1)) / 2;
h = MAX_DISPLAY_H << dbl1;
}
}
if (gclow == w && gcloh == h && gclox == dx && gcloy == dy)
return ret;
if (w <= 0 || h <= 0 || dx < 0 || dy < 0)
return ret;
if (doublescan <= 0 && !programmedmode) {
if (dx > vidinfo->outbuffer->inwidth / 3)
return ret;
if (dy > vidinfo->outbuffer->inheight / 3)
return ret;
}
gclow = w;
gcloh = h;
gclox = dx;
gcloy = dy;
gclorealh = *prealh;
*pw = w;
*ph = h;
*pdx = dx;
*pdy = dy;
#if 1
write_log (_T("Display Size: %dx%d Offset: %dx%d\n"), w, h, dx, dy);
write_log (_T("First: %d Last: %d MinV: %d MaxV: %d Min: %d\n"),
plffirstline_total, plflastline_total,
first_planes_vpos, last_planes_vpos, minfirstline);
#endif
center_reset = true;
return 1;
}
void get_custom_mouse_limits (int *pw, int *ph, int *pdx, int *pdy, int dbl)
{
int delay1, delay2;
int w, h, dx, dy, dbl1, dbl2, y1, y2;
w = diwlastword_total - diwfirstword_total;
dx = diwfirstword_total - visible_left_border;
y2 = plflastline_total;
if (y2 > last_planes_vpos)
y2 = last_planes_vpos;
y1 = plffirstline_total;
if (first_planes_vpos > y1)
y1 = first_planes_vpos;
if (minfirstline > y1)
y1 = minfirstline;
h = y2 - y1;
dy = y1 - minfirstline;
if (*pw > 0)
w = *pw;
w = xshift (w, res_shift);
if (*ph > 0)
h = *ph;
delay1 = (firstword_bplcon1 & 0x0f) | ((firstword_bplcon1 & 0x0c00) >> 6);
delay2 = ((firstword_bplcon1 >> 4) & 0x0f) | (((firstword_bplcon1 >> 4) & 0x0c00) >> 6);
// if (delay1 == delay2)
// dx += delay1;
dx = xshift (dx, res_shift);
dbl2 = dbl1 = currprefs.gfx_vresolution;
if ((doublescan > 0 || interlace_seen > 0) && !dbl) {
dbl1--;
dbl2--;
}
if (interlace_seen > 0)
dbl2++;
if (interlace_seen <= 0 && dbl)
dbl2--;
h = xshift (h, dbl1);
dy = xshift (dy, dbl2);
if (w < 1)
w = 1;
if (h < 1)
h = 1;
if (dx < 0)
dx = 0;
if (dy < 0)
dy = 0;
*pw = w; *ph = h;
*pdx = dx; *pdy = dy;
}
static struct decision *dp_for_drawing;
static struct draw_info *dip_for_drawing;
/* Record DIW of the current line for use by centering code. */
void record_diw_line (int plfstrt, int first, int last)
{
if (last > max_diwstop)
max_diwstop = last;
if (first < min_diwstart) {
min_diwstart = first;
/*
if (plfstrt * 2 > min_diwstart)
min_diwstart = plfstrt * 2;
*/
}
}
STATIC_INLINE int get_shdelay_add(void)
{
if (bplres == RES_SUPERHIRES)
return 0;
if (currprefs.chipset_hr)
return 0;
int add = bpldelay_sh;
add >>= RES_MAX - currprefs.gfx_resolution;
return add;
}
/*
* Screen update macros/functions
*/
/* The important positions in the line: where do we start drawing the left border,
where do we start drawing the playfield, where do we start drawing the right border.
All of these are forced into the visible window (VISIBLE_LEFT_BORDER .. VISIBLE_RIGHT_BORDER).
PLAYFIELD_START and PLAYFIELD_END are in window coordinates. */
static int playfield_start_pre, playfield_end_pre;
static int playfield_start, playfield_end;
static int real_playfield_start, real_playfield_end;
static int playfield_diff;
static int sprite_playfield_start, sprite_end;
static int may_require_hard_way;
static int linetoscr_diw_start, linetoscr_diw_end;
static int native_ddf_left, native_ddf_right;
static int pixels_offset;
static int src_pixel;
/* How many pixels in window coordinates which are to the left of the left border. */
static int unpainted;
STATIC_INLINE xcolnr getbgc (int blank)
{
#if BG_COLOR_DEBUG
if (blank > 0)
return xcolors[0x088];
else if (blank < 0)
return xcolors[0x0f8];
else if (hposblank == 1)
return xcolors[0xf00];
else if (hposblank == 2)
return xcolors[0x0f0];
else if (hposblank == 3)
return xcolors[0x00f];
else if (ce_is_borderblank(colors_for_drawing.extra))
return xcolors[0x880];
//return colors_for_drawing.acolors[0];
return xcolors[0xf0f];
#endif
return (blank >= 0 && (blank > 0 || hposblank || ce_is_borderblank(colors_for_drawing.extra))) ? 0 : colors_for_drawing.acolors[0];
}
static void set_res_shift(void)
{
int shift = lores_shift - bplres;
int old = res_shift;
res_shift = shift;
if (res_shift != old)
pfield_set_linetoscr();
}
/* Initialize the variables necessary for drawing a line.
* This involves setting up start/stop positions and display window
* borders. */
static void pfield_init_linetoscr (bool border)
{
/* First, get data fetch start/stop in DIW coordinates. */
int ddf_left = dp_for_drawing->plfleft * 2 + DIW_DDF_OFFSET;
int ddf_right = dp_for_drawing->plfright * 2 + DIW_DDF_OFFSET;
int leftborderhidden;
int native_ddf_left2;
bool expanded = false;
hsync_shift_hack = 0;
if (border)
ddf_left = DISPLAY_LEFT_SHIFT;
/* Compute datafetch start/stop in pixels; native display coordinates. */
native_ddf_left = coord_hw_to_window_x (ddf_left);
native_ddf_right = coord_hw_to_window_x (ddf_right);
// Blerkenwiegel/Scoopex workaround
native_ddf_left2 = native_ddf_left;
if (native_ddf_left < 0)
native_ddf_left = 0;
if (native_ddf_right > MAX_PIXELS_PER_LINE)
native_ddf_right = MAX_PIXELS_PER_LINE;
if (native_ddf_right < native_ddf_left)
native_ddf_right = native_ddf_left;
linetoscr_diw_start = dp_for_drawing->diwfirstword;
linetoscr_diw_end = dp_for_drawing->diwlastword;
/* Perverse cases happen. */
if (linetoscr_diw_end < linetoscr_diw_start)
linetoscr_diw_end = linetoscr_diw_start;
set_res_shift();
playfield_start = linetoscr_diw_start;
playfield_end = linetoscr_diw_end;
if (playfield_start < native_ddf_left)
playfield_start = native_ddf_left;
if (playfield_end > native_ddf_right)
playfield_end = native_ddf_right;
if (playfield_start < visible_left_border)
playfield_start = visible_left_border;
if (playfield_start > visible_right_border)
playfield_start = visible_right_border;
if (playfield_end < visible_left_border)
playfield_end = visible_left_border;
if (playfield_end > visible_right_border)
playfield_end = visible_right_border;
real_playfield_start = playfield_start;
sprite_playfield_start = playfield_start;
real_playfield_end = playfield_end;
sprite_end = linetoscr_diw_end;
// Sprite hpos don't include DIW_DDF_OFFSET and can appear 1 lores pixel
// before first bitplane pixel appears.
// This means "bordersprite" condition is possible under OCS/ECS too. Argh!
if (dip_for_drawing->nr_sprites) {
if (!ce_is_borderblank(colors_for_drawing.extra)) {
/* bordersprite off or not supported: sprites are visible until diw_end */
if (playfield_end < linetoscr_diw_end && hblank_right_stop > playfield_end) {
playfield_end = linetoscr_diw_end;
}
int left = coord_hw_to_window_x (dp_for_drawing->plfleft * 2);
if (left < visible_left_border)
left = visible_left_border;
if (left < playfield_start && left >= linetoscr_diw_start) {
playfield_start = left;
}
} else {
sprite_playfield_start = 0;
if (playfield_end < linetoscr_diw_end && hblank_right_stop > playfield_end) {
playfield_end = linetoscr_diw_end;
}
}
}
#ifdef AGA
// if BPLCON4 is non-zero or borderblank: it can affect background color until end of DIW.
if (dp_for_drawing->xor_seen || ce_is_borderblank(colors_for_drawing.extra)) {
if (playfield_end < linetoscr_diw_end && hblank_right_stop > playfield_end) {
playfield_end = linetoscr_diw_end;
expanded = true;
}
}
playfield_diff = 0;
may_require_hard_way = 0;
if (dp_for_drawing->bordersprite_seen && !ce_is_borderblank(colors_for_drawing.extra) && dip_for_drawing->nr_sprites) {
int min = visible_right_border, max = visible_left_border, i;
for (i = 0; i < dip_for_drawing->nr_sprites; i++) {