Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/xnes/snes9x/clip.cpp
28515 views
1
/***********************************************************************************
2
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3
4
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),
5
Jerremy Koot ([email protected])
6
7
(c) Copyright 2002 - 2004 Matthew Kendora
8
9
(c) Copyright 2002 - 2005 Peter Bortas ([email protected])
10
11
(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
12
13
(c) Copyright 2001 - 2006 John Weidman ([email protected])
14
15
(c) Copyright 2002 - 2006 funkyass ([email protected]),
16
Kris Bleakley ([email protected])
17
18
(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),
19
Nach ([email protected]),
20
21
(c) Copyright 2002 - 2011 zones ([email protected])
22
23
(c) Copyright 2006 - 2007 nitsuja
24
25
(c) Copyright 2009 - 2011 BearOso,
26
OV2
27
28
29
BS-X C emulator code
30
(c) Copyright 2005 - 2006 Dreamer Nom,
31
zones
32
33
C4 x86 assembler and some C emulation code
34
(c) Copyright 2000 - 2003 _Demo_ ([email protected]),
35
Nach,
36
zsKnight ([email protected])
37
38
C4 C++ code
39
(c) Copyright 2003 - 2006 Brad Jorsch,
40
Nach
41
42
DSP-1 emulator code
43
(c) Copyright 1998 - 2006 _Demo_,
44
Andreas Naive ([email protected]),
45
Gary Henderson,
46
Ivar ([email protected]),
47
John Weidman,
48
Kris Bleakley,
49
Matthew Kendora,
50
Nach,
51
neviksti ([email protected])
52
53
DSP-2 emulator code
54
(c) Copyright 2003 John Weidman,
55
Kris Bleakley,
56
Lord Nightmare ([email protected]),
57
Matthew Kendora,
58
neviksti
59
60
DSP-3 emulator code
61
(c) Copyright 2003 - 2006 John Weidman,
62
Kris Bleakley,
63
Lancer,
64
z80 gaiden
65
66
DSP-4 emulator code
67
(c) Copyright 2004 - 2006 Dreamer Nom,
68
John Weidman,
69
Kris Bleakley,
70
Nach,
71
z80 gaiden
72
73
OBC1 emulator code
74
(c) Copyright 2001 - 2004 zsKnight,
75
pagefault ([email protected]),
76
Kris Bleakley
77
Ported from x86 assembler to C by sanmaiwashi
78
79
SPC7110 and RTC C++ emulator code used in 1.39-1.51
80
(c) Copyright 2002 Matthew Kendora with research by
81
zsKnight,
82
John Weidman,
83
Dark Force
84
85
SPC7110 and RTC C++ emulator code used in 1.52+
86
(c) Copyright 2009 byuu,
87
neviksti
88
89
S-DD1 C emulator code
90
(c) Copyright 2003 Brad Jorsch with research by
91
Andreas Naive,
92
John Weidman
93
94
S-RTC C emulator code
95
(c) Copyright 2001 - 2006 byuu,
96
John Weidman
97
98
ST010 C++ emulator code
99
(c) Copyright 2003 Feather,
100
John Weidman,
101
Kris Bleakley,
102
Matthew Kendora
103
104
Super FX x86 assembler emulator code
105
(c) Copyright 1998 - 2003 _Demo_,
106
pagefault,
107
zsKnight
108
109
Super FX C emulator code
110
(c) Copyright 1997 - 1999 Ivar,
111
Gary Henderson,
112
John Weidman
113
114
Sound emulator code used in 1.5-1.51
115
(c) Copyright 1998 - 2003 Brad Martin
116
(c) Copyright 1998 - 2006 Charles Bilyue'
117
118
Sound emulator code used in 1.52+
119
(c) Copyright 2004 - 2007 Shay Green ([email protected])
120
121
SH assembler code partly based on x86 assembler code
122
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
123
124
2xSaI filter
125
(c) Copyright 1999 - 2001 Derek Liauw Kie Fa
126
127
HQ2x, HQ3x, HQ4x filters
128
(c) Copyright 2003 Maxim Stepin ([email protected])
129
130
NTSC filter
131
(c) Copyright 2006 - 2007 Shay Green
132
133
GTK+ GUI code
134
(c) Copyright 2004 - 2011 BearOso
135
136
Win32 GUI code
137
(c) Copyright 2003 - 2006 blip,
138
funkyass,
139
Matthew Kendora,
140
Nach,
141
nitsuja
142
(c) Copyright 2009 - 2011 OV2
143
144
Mac OS GUI code
145
(c) Copyright 1998 - 2001 John Stiles
146
(c) Copyright 2001 - 2011 zones
147
148
149
Specific ports contains the works of other authors. See headers in
150
individual files.
151
152
153
Snes9x homepage: http://www.snes9x.com/
154
155
Permission to use, copy, modify and/or distribute Snes9x in both binary
156
and source form, for non-commercial purposes, is hereby granted without
157
fee, providing that this license information and copyright notice appear
158
with all copies and any derived work.
159
160
This software is provided 'as-is', without any express or implied
161
warranty. In no event shall the authors be held liable for any damages
162
arising from the use of this software or it's derivatives.
163
164
Snes9x is freeware for PERSONAL USE only. Commercial users should
165
seek permission of the copyright holders first. Commercial use includes,
166
but is not limited to, charging money for Snes9x or software derived from
167
Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
168
using Snes9x as a promotion for your commercial product.
169
170
The copyright holders request that bug fixes and improvements to the code
171
should be forwarded to them so everyone can benefit from the modifications
172
in future versions.
173
174
Super NES and Super Nintendo Entertainment System are trademarks of
175
Nintendo Co., Limited and its subsidiary companies.
176
***********************************************************************************/
177
178
179
#include "snes9x.h"
180
#include "memmap.h"
181
182
static uint8 region_map[6][6] =
183
{
184
{ 0, 0x01, 0x03, 0x07, 0x0f, 0x1f },
185
{ 0, 0, 0x02, 0x06, 0x0e, 0x1e },
186
{ 0, 0, 0, 0x04, 0x0c, 0x1c },
187
{ 0, 0, 0, 0, 0x08, 0x18 },
188
{ 0, 0, 0, 0, 0, 0x10 }
189
};
190
191
static inline uint8 CalcWindowMask (int, uint8, uint8);
192
static inline void StoreWindowRegions (uint8, struct ClipData *, int, int16 *, uint8 *, bool8, bool8 s = FALSE);
193
194
195
static inline uint8 CalcWindowMask (int i, uint8 W1, uint8 W2)
196
{
197
if (!PPU.ClipWindow1Enable[i])
198
{
199
if (!PPU.ClipWindow2Enable[i])
200
return (0);
201
else
202
{
203
if (!PPU.ClipWindow2Inside[i])
204
return (~W2);
205
return (W2);
206
}
207
}
208
else
209
{
210
if (!PPU.ClipWindow2Enable[i])
211
{
212
if (!PPU.ClipWindow1Inside[i])
213
return (~W1);
214
return (W1);
215
}
216
else
217
{
218
if (!PPU.ClipWindow1Inside[i])
219
W1 = ~W1;
220
if (!PPU.ClipWindow2Inside[i])
221
W2 = ~W2;
222
223
switch (PPU.ClipWindowOverlapLogic[i])
224
{
225
case 0: // OR
226
return (W1 | W2);
227
228
case 1: // AND
229
return (W1 & W2);
230
231
case 2: // XOR
232
return (W1 ^ W2);
233
234
case 3: // XNOR
235
return (~(W1 ^ W2));
236
}
237
}
238
}
239
240
// Never get here
241
return (0);
242
}
243
244
static inline void StoreWindowRegions (uint8 Mask, struct ClipData *Clip, int n_regions, int16 *windows, uint8 *drawing_modes, bool8 sub, bool8 StoreMode0)
245
{
246
int ct = 0;
247
248
for (int j = 0; j < n_regions; j++)
249
{
250
int DrawMode = drawing_modes[j];
251
if (sub)
252
DrawMode |= 1;
253
if (Mask & (1 << j))
254
DrawMode = 0;
255
256
if (!StoreMode0 && !DrawMode)
257
continue;
258
259
if (ct > 0 && Clip->Right[ct - 1] == windows[j] && Clip->DrawMode[ct - 1] == DrawMode)
260
Clip->Right[ct - 1] = windows[j + 1]; // This region borders with and has the same drawing mode as the previous region: merge them.
261
else
262
{
263
// Add a new region to the BG
264
Clip->Left[ct] = windows[j];
265
Clip->Right[ct] = windows[j + 1];
266
Clip->DrawMode[ct] = DrawMode;
267
ct++;
268
}
269
}
270
271
Clip->Count = ct;
272
}
273
274
void S9xComputeClipWindows (void)
275
{
276
int16 windows[6] = { 0, 256, 256, 256, 256, 256 };
277
uint8 drawing_modes[5] = { 0, 0, 0, 0, 0 };
278
int n_regions = 1;
279
int i, j;
280
281
// Calculate window regions. We have at most 5 regions, because we have 6 control points
282
// (screen edges, window 1 left & right, and window 2 left & right).
283
284
if (PPU.Window1Left <= PPU.Window1Right)
285
{
286
if (PPU.Window1Left > 0)
287
{
288
windows[2] = 256;
289
windows[1] = PPU.Window1Left;
290
n_regions = 2;
291
}
292
293
if (PPU.Window1Right < 255)
294
{
295
windows[n_regions + 1] = 256;
296
windows[n_regions] = PPU.Window1Right + 1;
297
n_regions++;
298
}
299
}
300
301
if (PPU.Window2Left <= PPU.Window2Right)
302
{
303
for (i = 0; i <= n_regions; i++)
304
{
305
if (PPU.Window2Left == windows[i])
306
break;
307
308
if (PPU.Window2Left < windows[i])
309
{
310
for (j = n_regions; j >= i; j--)
311
windows[j + 1] = windows[j];
312
313
windows[i] = PPU.Window2Left;
314
n_regions++;
315
break;
316
}
317
}
318
319
for (; i <= n_regions; i++)
320
{
321
if (PPU.Window2Right + 1 == windows[i])
322
break;
323
324
if (PPU.Window2Right + 1 < windows[i])
325
{
326
for (j = n_regions; j >= i; j--)
327
windows[j + 1] = windows[j];
328
329
windows[i] = PPU.Window2Right + 1;
330
n_regions++;
331
break;
332
}
333
}
334
}
335
336
// Get a bitmap of which regions correspond to each window.
337
338
uint8 W1, W2;
339
340
if (PPU.Window1Left <= PPU.Window1Right)
341
{
342
for (i = 0; windows[i] != PPU.Window1Left; i++) ;
343
for (j = i; windows[j] != PPU.Window1Right + 1; j++) ;
344
W1 = region_map[i][j];
345
}
346
else
347
W1 = 0;
348
349
if (PPU.Window2Left <= PPU.Window2Right)
350
{
351
for (i = 0; windows[i] != PPU.Window2Left; i++) ;
352
for (j = i; windows[j] != PPU.Window2Right + 1; j++) ;
353
W2 = region_map[i][j];
354
}
355
else
356
W2 = 0;
357
358
// Color Window affects the drawing mode for each region.
359
// Modes are: 3=Draw as normal, 2=clip color (math only), 1=no math (draw only), 0=nothing.
360
361
uint8 CW_color = 0, CW_math = 0;
362
uint8 CW = CalcWindowMask(5, W1, W2);
363
364
switch (Memory.FillRAM[0x2130] & 0xc0)
365
{
366
case 0x00: CW_color = 0; break;
367
case 0x40: CW_color = ~CW; break;
368
case 0x80: CW_color = CW; break;
369
case 0xc0: CW_color = 0xff; break;
370
}
371
372
switch (Memory.FillRAM[0x2130] & 0x30)
373
{
374
case 0x00: CW_math = 0; break;
375
case 0x10: CW_math = ~CW; break;
376
case 0x20: CW_math = CW; break;
377
case 0x30: CW_math = 0xff; break;
378
}
379
380
for (i = 0; i < n_regions; i++)
381
{
382
if (!(CW_color & (1 << i)))
383
drawing_modes[i] |= 1;
384
if (!(CW_math & (1 << i)))
385
drawing_modes[i] |= 2;
386
}
387
388
// Store backdrop clip window (draw everywhere color window allows)
389
390
StoreWindowRegions(0, &IPPU.Clip[0][5], n_regions, windows, drawing_modes, FALSE, TRUE);
391
StoreWindowRegions(0, &IPPU.Clip[1][5], n_regions, windows, drawing_modes, TRUE, TRUE);
392
393
// Store per-BG and OBJ clip windows
394
395
for (j = 0; j < 5; j++)
396
{
397
uint8 W = Settings.DisableGraphicWindows ? 0 : CalcWindowMask(j, W1, W2);
398
for (int sub = 0; sub < 2; sub++)
399
{
400
if (Memory.FillRAM[sub + 0x212e] & (1 << j))
401
StoreWindowRegions(W, &IPPU.Clip[sub][j], n_regions, windows, drawing_modes, sub);
402
else
403
StoreWindowRegions(0, &IPPU.Clip[sub][j], n_regions, windows, drawing_modes, sub);
404
}
405
}
406
}
407
408