1 /***********************************************************************************
2 Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3
4 (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com),
5 Jerremy Koot (jkoot@snes9x.com)
6
7 (c) Copyright 2002 - 2004 Matthew Kendora
8
9 (c) Copyright 2002 - 2005 Peter Bortas (peter@bortas.org)
10
11 (c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)
12
13 (c) Copyright 2001 - 2006 John Weidman (jweidman@slip.net)
14
15 (c) Copyright 2002 - 2006 funkyass (funkyass@spam.shaw.ca),
16 Kris Bleakley (codeviolation@hotmail.com)
17
18 (c) Copyright 2002 - 2010 Brad Jorsch (anomie@users.sourceforge.net),
19 Nach (n-a-c-h@users.sourceforge.net),
20
21 (c) Copyright 2002 - 2011 zones (kasumitokoduck@yahoo.com)
22
23 (c) Copyright 2006 - 2007 nitsuja
24
25 (c) Copyright 2009 - 2016 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_ (_demo_@zsnes.com),
35 Nach,
36 zsKnight (zsknight@zsnes.com)
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 (andreasnaive@gmail.com),
45 Gary Henderson,
46 Ivar (ivar@snes9x.com),
47 John Weidman,
48 Kris Bleakley,
49 Matthew Kendora,
50 Nach,
51 neviksti (neviksti@hotmail.com)
52
53 DSP-2 emulator code
54 (c) Copyright 2003 John Weidman,
55 Kris Bleakley,
56 Lord Nightmare (lord_nightmare@users.sourceforge.net),
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 (pagefault@zsnes.com),
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 (gblargg@gmail.com)
120
121 S-SMP emulator code used in 1.54+
122 (c) Copyright 2016 byuu
123
124 SH assembler code partly based on x86 assembler code
125 (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se)
126
127 2xSaI filter
128 (c) Copyright 1999 - 2001 Derek Liauw Kie Fa
129
130 HQ2x, HQ3x, HQ4x filters
131 (c) Copyright 2003 Maxim Stepin (maxim@hiend3d.com)
132
133 NTSC filter
134 (c) Copyright 2006 - 2007 Shay Green
135
136 GTK+ GUI code
137 (c) Copyright 2004 - 2016 BearOso
138
139 Win32 GUI code
140 (c) Copyright 2003 - 2006 blip,
141 funkyass,
142 Matthew Kendora,
143 Nach,
144 nitsuja
145 (c) Copyright 2009 - 2016 OV2
146
147 Mac OS GUI code
148 (c) Copyright 1998 - 2001 John Stiles
149 (c) Copyright 2001 - 2011 zones
150
151
152 Specific ports contains the works of other authors. See headers in
153 individual files.
154
155
156 Snes9x homepage: http://www.snes9x.com/
157
158 Permission to use, copy, modify and/or distribute Snes9x in both binary
159 and source form, for non-commercial purposes, is hereby granted without
160 fee, providing that this license information and copyright notice appear
161 with all copies and any derived work.
162
163 This software is provided 'as-is', without any express or implied
164 warranty. In no event shall the authors be held liable for any damages
165 arising from the use of this software or it's derivatives.
166
167 Snes9x is freeware for PERSONAL USE only. Commercial users should
168 seek permission of the copyright holders first. Commercial use includes,
169 but is not limited to, charging money for Snes9x or software derived from
170 Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
171 using Snes9x as a promotion for your commercial product.
172
173 The copyright holders request that bug fixes and improvements to the code
174 should be forwarded to them so everyone can benefit from the modifications
175 in future versions.
176
177 Super NES and Super Nintendo Entertainment System are trademarks of
178 Nintendo Co., Limited and its subsidiary companies.
179 ***********************************************************************************/
180
181
182 #include <math.h>
183 #include "snes9x.h"
184 #include "memmap.h"
185
186 #define C4_PI 3.14159265
187
188 int16 C4WFXVal;
189 int16 C4WFYVal;
190 int16 C4WFZVal;
191 int16 C4WFX2Val;
192 int16 C4WFY2Val;
193 int16 C4WFDist;
194 int16 C4WFScale;
195 int16 C41FXVal;
196 int16 C41FYVal;
197 int16 C41FAngleRes;
198 int16 C41FDist;
199 int16 C41FDistVal;
200
201 static double tanval;
202 static double c4x, c4y, c4z;
203 static double c4x2, c4y2, c4z2;
204
205
C4TransfWireFrame(void)206 void C4TransfWireFrame (void)
207 {
208 c4x = (double) C4WFXVal;
209 c4y = (double) C4WFYVal;
210 c4z = (double) C4WFZVal - 0x95;
211
212 // Rotate X
213 tanval = -(double) C4WFX2Val * C4_PI * 2 / 128;
214 c4y2 = c4y * cos(tanval) - c4z * sin(tanval);
215 c4z2 = c4y * sin(tanval) + c4z * cos(tanval);
216
217 // Rotate Y
218 tanval = -(double) C4WFY2Val * C4_PI * 2 / 128;
219 c4x2 = c4x * cos(tanval) + c4z2 * sin(tanval);
220 c4z = c4x * -sin(tanval) + c4z2 * cos(tanval);
221
222 // Rotate Z
223 tanval = -(double) C4WFDist * C4_PI * 2 / 128;
224 c4x = c4x2 * cos(tanval) - c4y2 * sin(tanval);
225 c4y = c4x2 * sin(tanval) + c4y2 * cos(tanval);
226
227 // Scale
228 C4WFXVal = (int16) (c4x * (double) C4WFScale / (0x90 * (c4z + 0x95)) * 0x95);
229 C4WFYVal = (int16) (c4y * (double) C4WFScale / (0x90 * (c4z + 0x95)) * 0x95);
230 }
231
C4TransfWireFrame2(void)232 void C4TransfWireFrame2 (void)
233 {
234 c4x = (double) C4WFXVal;
235 c4y = (double) C4WFYVal;
236 c4z = (double) C4WFZVal;
237
238 // Rotate X
239 tanval = -(double) C4WFX2Val * C4_PI * 2 / 128;
240 c4y2 = c4y * cos(tanval) - c4z * sin(tanval);
241 c4z2 = c4y * sin(tanval) + c4z * cos(tanval);
242
243 // Rotate Y
244 tanval = -(double) C4WFY2Val * C4_PI * 2 / 128;
245 c4x2 = c4x * cos(tanval) + c4z2 * sin(tanval);
246 c4z = c4x * -sin(tanval) + c4z2 * cos(tanval);
247
248 // Rotate Z
249 tanval = -(double) C4WFDist * C4_PI * 2 / 128;
250 c4x = c4x2 * cos(tanval) - c4y2 * sin(tanval);
251 c4y = c4x2 * sin(tanval) + c4y2 * cos(tanval);
252
253 // Scale
254 C4WFXVal = (int16) (c4x * (double) C4WFScale / 0x100);
255 C4WFYVal = (int16) (c4y * (double) C4WFScale / 0x100);
256 }
257
C4CalcWireFrame(void)258 void C4CalcWireFrame (void)
259 {
260 C4WFXVal = C4WFX2Val - C4WFXVal;
261 C4WFYVal = C4WFY2Val - C4WFYVal;
262
263 if (abs(C4WFXVal) > abs(C4WFYVal))
264 {
265 C4WFDist = abs(C4WFXVal) + 1;
266 C4WFYVal = (int16) (256 * (double) C4WFYVal / abs(C4WFXVal));
267 if (C4WFXVal < 0)
268 C4WFXVal = -256;
269 else
270 C4WFXVal = 256;
271 }
272 else
273 {
274 if (C4WFYVal != 0)
275 {
276 C4WFDist = abs(C4WFYVal) + 1;
277 C4WFXVal = (int16) (256 * (double) C4WFXVal / abs(C4WFYVal));
278 if (C4WFYVal < 0)
279 C4WFYVal = -256;
280 else
281 C4WFYVal = 256;
282 }
283 else
284 C4WFDist = 0;
285 }
286 }
287
C4Op1F(void)288 void C4Op1F (void)
289 {
290 if (C41FXVal == 0)
291 {
292 if (C41FYVal > 0)
293 C41FAngleRes = 0x80;
294 else
295 C41FAngleRes = 0x180;
296 }
297 else
298 {
299 tanval = (double) C41FYVal / C41FXVal;
300 C41FAngleRes = (int16) (atan(tanval) / (C4_PI * 2) * 512);
301 if (C41FXVal< 0)
302 C41FAngleRes += 0x100;
303 C41FAngleRes &= 0x1FF;
304 }
305 }
306
C4Op15(void)307 void C4Op15 (void)
308 {
309 tanval = sqrt((double) C41FYVal * C41FYVal + (double) C41FXVal * C41FXVal);
310 C41FDist = (int16) tanval;
311 }
312
C4Op0D(void)313 void C4Op0D (void)
314 {
315 tanval = sqrt((double) C41FYVal * C41FYVal + (double) C41FXVal * C41FXVal);
316 tanval = C41FDistVal / tanval;
317 C41FYVal = (int16) (C41FYVal * tanval * 0.99);
318 C41FXVal = (int16) (C41FXVal * tanval * 0.98);
319 }
320
S9xGetBasePointerC4(uint16 Address)321 uint8 * S9xGetBasePointerC4 (uint16 Address)
322 {
323 if (Address >= 0x7f40 && Address <= 0x7f5e)
324 return (NULL);
325 return (Memory.C4RAM - 0x6000);
326 }
327
S9xGetMemPointerC4(uint16 Address)328 uint8 * S9xGetMemPointerC4 (uint16 Address)
329 {
330 if (Address >= 0x7f40 && Address <= 0x7f5e)
331 return (NULL);
332 return (Memory.C4RAM - 0x6000 + (Address & 0xffff));
333 }
334