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
|
#include "../copyright"
#include <stdlib.h>
#include "snes9x.h"
#include "memmap.h"
#include "ppu.h"
typedef struct
{
uint32_t Left;
uint32_t Right;
} Band;
#define BAND_EMPTY(B) (B.Left >= B.Right)
#define BANDS_INTERSECT(A,B) ((A.Left >= B.Left && A.Left < B.Right) || (B.Left >= A.Left && B.Left < A.Right))
#define OR_BANDS(R,A,B) \
{ \
R.Left = MIN(A.Left, B.Left); \
R.Right = MAX(A.Right, B.Right); \
}
#define AND_BANDS(R,A,B) \
{ \
R.Left = MAX(A.Left, B.Left); \
R.Right = MIN(A.Right, B.Right); \
}
static int32_t IntCompare(const void* d1, const void* d2)
{
return *(uint32_t*) d1 - *(uint32_t*) d2;
}
static int32_t BandCompare(const void* d1, const void* d2)
{
return ((Band*) d1)->Left - ((Band*) d2)->Left;
}
void ComputeClipWindows()
{
ClipData* pClip = &IPPU.Clip [0];
int32_t c, w, i;
/* Loop around the main screen then the sub-screen. */
for (c = 0; c < 2; c++, pClip++)
{
/* Loop around the colour window then a clip window for each of the
* background layers. */
for (w = 5; w >= 0; w--)
{
pClip->Count[w] = 0;
if (w == 5) /* The colour window... */
{
if (c == 0) /* ... on the main screen */
{
if ((Memory.FillRAM [0x2130] & 0xc0) == 0xc0)
{
/* The whole of the main screen is switched off, completely clip everything. */
for (i = 0; i < 6; i++)
{
IPPU.Clip [c].Count [i] = 1;
IPPU.Clip [c].Left [0][i] = 1;
IPPU.Clip [c].Right [0][i] = 0;
}
continue;
}
else if ((Memory.FillRAM [0x2130] & 0xc0) == 0x00)
continue;
}
else if ((Memory.FillRAM [0x2130] & 0x30) == 0x30) /* .. colour window on the sub-screen. */
{
/* The sub-screen is switched off, completely clip everything. */
int32_t i;
for (i = 0; i < 6; i++)
{
IPPU.Clip [1].Count [i] = 1;
IPPU.Clip [1].Left [0][i] = 1;
IPPU.Clip [1].Right [0][i] = 0;
}
return;
}
else if ((Memory.FillRAM [0x2130] & 0x30) == 0x00)
continue;
}
if (w == 5 || pClip->Count [5] || (Memory.FillRAM [0x212c + c] & Memory.FillRAM [0x212e + c] & (1 << w)))
{
Band Win1[3];
Band Win2[3];
uint32_t Window1Enabled = 0;
uint32_t Window2Enabled = 0;
bool invert = (w == 5 && ((c == 1 && (Memory.FillRAM [0x2130] & 0x30) == 0x10) || (c == 0 && (Memory.FillRAM [0x2130] & 0xc0) == 0x40)));
if (w == 5 || (Memory.FillRAM [0x212c + c] & Memory.FillRAM [0x212e + c] & (1 << w)))
{
if (PPU.ClipWindow1Enable [w])
{
if (!PPU.ClipWindow1Inside [w])
{
Win1[Window1Enabled].Left = PPU.Window1Left;
Win1[Window1Enabled++].Right = PPU.Window1Right + 1;
}
else if (PPU.Window1Left <= PPU.Window1Right)
{
if (PPU.Window1Left > 0)
{
Win1[Window1Enabled].Left = 0;
Win1[Window1Enabled++].Right = PPU.Window1Left;
}
if (PPU.Window1Right < 255)
{
Win1[Window1Enabled].Left = PPU.Window1Right + 1;
Win1[Window1Enabled++].Right = 256;
}
if (Window1Enabled == 0)
{
Win1[Window1Enabled].Left = 1;
Win1[Window1Enabled++].Right = 0;
}
}
else
{
/* 'outside' a window with no range -
* appears to be the whole screen. */
Win1[Window1Enabled].Left = 0;
Win1[Window1Enabled++].Right = 256;
}
}
if (PPU.ClipWindow2Enable [w])
{
if (!PPU.ClipWindow2Inside [w])
{
Win2[Window2Enabled].Left = PPU.Window2Left;
Win2[Window2Enabled++].Right = PPU.Window2Right + 1;
}
else
{
if (PPU.Window2Left <= PPU.Window2Right)
{
if (PPU.Window2Left > 0)
{
Win2[Window2Enabled].Left = 0;
Win2[Window2Enabled++].Right = PPU.Window2Left;
}
if (PPU.Window2Right < 255)
{
Win2[Window2Enabled].Left = PPU.Window2Right + 1;
Win2[Window2Enabled++].Right = 256;
}
if (Window2Enabled == 0)
{
Win2[Window2Enabled].Left = 1;
Win2[Window2Enabled++].Right = 0;
}
}
else
{
Win2[Window2Enabled].Left = 0;
Win2[Window2Enabled++].Right = 256;
}
}
}
}
if (Window1Enabled && Window2Enabled)
{
/* Overlap logic
*
* Each window will be in one of three states:
* 1. <no range> (Left > Right. One band)
* 2. | ---------------- | (Left >= 0, Right <= 255, Left <= Right. One band)
* 3. |------------ ----------| (Left1 == 0, Right1 < Left2; Left2 > Right1, Right2 == 255. Two bands) */
Band Bands [6];
int32_t B = 0;
switch (PPU.ClipWindowOverlapLogic [w] ^ 1)
{
case CLIP_OR:
if (Window1Enabled == 1)
{
if (BAND_EMPTY(Win1[0]))
{
B = Window2Enabled;
/* memmove converted: Different stack allocations [Neb] */
memcpy(Bands, Win2, sizeof(Win2[0]) * Window2Enabled);
}
else
{
if (Window2Enabled == 1)
{
if (BAND_EMPTY(Win2[0]))
Bands[B++] = Win1[0];
else if (BANDS_INTERSECT(Win1[0], Win2[0]))
{
OR_BANDS(Bands[0], Win1[0], Win2[0])
B = 1;
}
else
{
Bands[B++] = Win1[0];
Bands[B++] = Win2[0];
}
}
else if (BANDS_INTERSECT(Win1[0], Win2[0]))
{
OR_BANDS(Bands[0], Win1[0], Win2[0])
if (BANDS_INTERSECT(Win1[0], Win2[1]))
OR_BANDS(Bands[1], Win1[0], Win2[1])
else
Bands[1] = Win2[1];
B = 1;
if (BANDS_INTERSECT(Bands[0], Bands[1]))
OR_BANDS(Bands[0], Bands[0], Bands[1])
else
B = 2;
}
else if (BANDS_INTERSECT(Win1[0], Win2[1]))
{
Bands[B++] = Win2[0];
OR_BANDS(Bands[B], Win1[0], Win2[1]);
B++;
}
else
{
Bands[0] = Win2[0];
Bands[1] = Win1[0];
Bands[2] = Win2[1];
B = 3;
}
}
}
else if (Window2Enabled == 1)
{
if (BAND_EMPTY(Win2[0]))
{
/* Window 2 defines an empty range - just
* use window 1 as the clipping (which
* could also be empty). */
B = Window1Enabled;
/* memmove converted: Different stack allocations [Neb] */
memcpy(Bands, Win1, sizeof(Win1[0]) * Window1Enabled);
}
else
{
/* Window 1 has two bands and Window 2 has one.
* Neither is an empty region. */
if (BANDS_INTERSECT(Win2[0], Win1[0]))
{
OR_BANDS(Bands[0], Win2[0], Win1[0])
if (BANDS_INTERSECT(Win2[0], Win1[1]))
OR_BANDS(Bands[1], Win2[0], Win1[1])
else
Bands[1] = Win1[1];
B = 1;
if (BANDS_INTERSECT(Bands[0], Bands[1]))
OR_BANDS(Bands[0], Bands[0], Bands[1])
else
B = 2;
}
else if (BANDS_INTERSECT(Win2[0], Win1[1]))
{
Bands[B++] = Win1[0];
OR_BANDS(Bands[B], Win2[0], Win1[1]);
B++;
}
else
{
Bands[0] = Win1[0];
Bands[1] = Win2[0];
Bands[2] = Win1[1];
B = 3;
}
}
}
else
{
/* Both windows have two bands */
OR_BANDS(Bands[0], Win1[0], Win2[0]);
OR_BANDS(Bands[1], Win1[1], Win2[1]);
B = 1;
if (BANDS_INTERSECT(Bands[0], Bands[1]))
OR_BANDS(Bands[0], Bands[0], Bands[1])
else
B = 2;
}
break;
case CLIP_AND:
if (Window1Enabled == 1)
{
/* Window 1 has one band */
if (BAND_EMPTY(Win1[0]))
Bands [B++] = Win1[0];
else if (Window2Enabled == 1)
{
if (BAND_EMPTY(Win2[0]))
Bands [B++] = Win2[0];
else
{
AND_BANDS(Bands[0], Win1[0], Win2[0]);
B = 1;
}
}
else
{
AND_BANDS(Bands[0], Win1[0], Win2[0]);
AND_BANDS(Bands[1], Win1[0], Win2[1]);
B = 2;
}
}
else if (Window2Enabled == 1)
{
if (BAND_EMPTY(Win2[0]))
Bands[B++] = Win2[0];
else
{
/* Window 1 has two bands. */
AND_BANDS(Bands[0], Win1[0], Win2[0]);
AND_BANDS(Bands[1], Win1[1], Win2[0]);
B = 2;
}
}
else
{
/* Both windows have two bands. */
AND_BANDS(Bands[0], Win1[0], Win2[0]);
AND_BANDS(Bands[1], Win1[1], Win2[1]);
B = 2;
if (BANDS_INTERSECT(Win1[0], Win2[1]))
{
AND_BANDS(Bands[2], Win1[0], Win2[1]);
B = 3;
}
else if (BANDS_INTERSECT(Win1[1], Win2[0]))
{
AND_BANDS(Bands[2], Win1[1], Win2[0]);
B = 3;
}
}
break;
case CLIP_XNOR:
invert = !invert;
/* Fall... */
case CLIP_XOR:
if (Window1Enabled == 1 && BAND_EMPTY(Win1[0]))
{
B = Window2Enabled;
/* memmove converted: Different stack allocations [Neb] */
memcpy(Bands, Win2, sizeof(Win2[0]) * Window2Enabled);
}
else if (Window2Enabled == 1 && BAND_EMPTY(Win2[0]))
{
B = Window1Enabled;
/* memmove converted: Different stack allocations [Neb] */
memcpy(Bands, Win1, sizeof(Win1[0]) * Window1Enabled);
}
else
{
uint32_t p = 0;
uint32_t points [10];
uint32_t i;
invert = !invert;
/* Build an array of points (window edges) */
points [p++] = 0;
for (i = 0; i < Window1Enabled; i++)
{
points [p++] = Win1[i].Left;
points [p++] = Win1[i].Right;
}
for (i = 0; i < Window2Enabled; i++)
{
points [p++] = Win2[i].Left;
points [p++] = Win2[i].Right;
}
points [p++] = 256;
/* Sort them */
qsort((void*) points, p, sizeof(points [0]), IntCompare);
for (i = 0; i < p; i += 2)
{
if (points [i] == points [i + 1])
continue;
Bands [B].Left = points [i];
while (i + 2 < p && points [i + 1] == points [i + 2])
i += 2;
Bands [B++].Right = points [i + 1];
}
}
break;
}
if (invert)
{
int32_t b;
int32_t j = 0;
int32_t empty_band_count = 0;
/* First remove all empty bands from the list. */
for (b = 0; b < B; b++)
{
if (!BAND_EMPTY(Bands[b]))
{
if (b != j)
Bands[j] = Bands[b];
j++;
}
else
empty_band_count++;
}
if (j > 0)
{
if (j == 1)
{
j = 0;
/* Easy case to deal with, so special case it. */
if (Bands[0].Left > 0)
{
pClip->Left[j][w] = 0;
pClip->Right[j++][w] = Bands[0].Left + 1;
}
if (Bands[0].Right < 256)
{
pClip->Left[j][w] = Bands[0].Right;
pClip->Right[j++][w] = 256;
}
if (j == 0)
{
pClip->Left[j][w] = 1;
pClip->Right[j++][w] = 0;
}
}
else
{
/* Now sort the bands into order */
B = j;
qsort((void*) Bands, B, sizeof(Bands [0]), BandCompare);
/* Now invert the area the bands cover */
j = 0;
for (b = 0; b < B; b++)
{
if (b == 0 && Bands[b].Left > 0)
{
pClip->Left[j][w] = 0;
pClip->Right[j++][w] = Bands[b].Left + 1;
}
else if (b == B - 1 && Bands[b].Right < 256)
{
pClip->Left[j][w] = Bands[b].Right;
pClip->Right[j++][w] = 256;
}
if (b < B - 1)
{
pClip->Left[j][w] = Bands[b].Right;
pClip->Right[j++][w] = Bands[b + 1].Left + 1;
}
}
}
}
else
{
/* Inverting a window that consisted of only
* empty bands is the whole width of the screen.
* Needed for Mario Kart's rear-view mirror display. */
if (empty_band_count)
{
pClip->Left[j][w] = 0;
pClip->Right[j][w] = 256;
j++;
}
}
pClip->Count[w] = j;
}
else
{
int32_t j;
for (j = 0; j < B; j++)
{
pClip->Left[j][w] = Bands[j].Left;
pClip->Right[j][w] = Bands[j].Right;
}
pClip->Count [w] = B;
}
}
else
{
/* Only one window enabled so no need to perform
* complex overlap logic... */
if (Window1Enabled)
{
if (invert)
{
int32_t j = 0;
if (Window1Enabled == 1)
{
if (Win1[0].Left <= Win1[0].Right)
{
if (Win1[0].Left > 0)
{
pClip->Left[j][w] = 0;
pClip->Right[j++][w] = Win1[0].Left;
}
if (Win1[0].Right < 256)
{
pClip->Left[j][w] = Win1[0].Right;
pClip->Right[j++][w] = 256;
}
if (j == 0)
{
pClip->Left[j][w] = 1;
pClip->Right[j++][w] = 0;
}
}
else
{
pClip->Left[j][w] = 0;
pClip->Right[j++][w] = 256;
}
}
else
{
pClip->Left [j][w] = Win1[0].Right;
pClip->Right[j++][w] = Win1[1].Left;
}
pClip->Count [w] = j;
}
else
{
uint32_t j;
for (j = 0; j < Window1Enabled; j++)
{
pClip->Left [j][w] = Win1[j].Left;
pClip->Right [j][w] = Win1[j].Right;
}
pClip->Count [w] = Window1Enabled;
}
}
else if (Window2Enabled)
{
if (invert)
{
int32_t j = 0;
if (Window2Enabled == 1)
{
if (Win2[0].Left <= Win2[0].Right)
{
if (Win2[0].Left > 0)
{
pClip->Left[j][w] = 0;
pClip->Right[j++][w] = Win2[0].Left;
}
if (Win2[0].Right < 256)
{
pClip->Left[j][w] = Win2[0].Right;
pClip->Right[j++][w] = 256;
}
if (j == 0)
{
pClip->Left[j][w] = 1;
pClip->Right[j++][w] = 0;
}
}
else
{
pClip->Left[j][w] = 0;
pClip->Right[j++][w] = 256;
}
}
else
{
pClip->Left [j][w] = Win2[0].Right;
pClip->Right[j++][w] = Win2[1].Left + 1;
}
pClip->Count [w] = j;
}
else
{
uint32_t j;
for (j = 0; j < Window2Enabled; j++)
{
pClip->Left [j][w] = Win2[j].Left;
pClip->Right [j][w] = Win2[j].Right;
}
pClip->Count [w] = Window2Enabled;
}
}
}
if (w != 5 && pClip->Count [5])
{
/* Colour window enabled. Set the
* clip windows for all remaining backgrounds to be
* the same as the colour window. */
if (pClip->Count [w] == 0)
{
uint32_t i;
pClip->Count [w] = pClip->Count [5];
for (i = 0; i < pClip->Count [w]; i++)
{
pClip->Left [i][w] = pClip->Left [i][5];
pClip->Right [i][w] = pClip->Right [i][5];
}
}
else
{
/* Intersect the colour window with the bg's
* own clip window. */
uint32_t i, j;
for (i = 0; i < pClip->Count [w]; i++)
{
for (j = 0; j < pClip->Count [5]; j++)
{
if ((pClip->Left[i][w] >= pClip->Left[j][5] &&
pClip->Left[i][w] < pClip->Right[j][5]) ||
(pClip->Left[j][5] >= pClip->Left[i][w] &&
pClip->Left[j][5] < pClip->Right[i][w]))
{
/* Found an intersection! */
pClip->Left[i][w] = MAX(pClip->Left[i][w], pClip->Left[j][5]);
pClip->Right[i][w] = MIN(pClip->Right[i][w], pClip->Right[j][5]);
goto Clip_ok;
}
}
/* no intersection, nullify it */
pClip->Left[i][w] = 1;
pClip->Right[i][w] = 0;
Clip_ok:;
}
}
}
} /* if (w == 5 || pClip->Count [5] ... */
} /* for (w... */
} /* for (c... */
}
|