workaround a problem with the harmattan gcc
[drnoksnes] / c4.cpp
1 /*******************************************************************************
2   Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  
4   (c) Copyright 1996 - 2003 Gary Henderson (gary.henderson@ntlworld.com) and
5                             Jerremy Koot (jkoot@snes9x.com)
6
7   (c) Copyright 2002 - 2003 Matthew Kendora and
8                             Brad Jorsch (anomie@users.sourceforge.net)
9  
10
11                       
12   C4 x86 assembler and some C emulation code
13   (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),
14                             _Demo_ (_demo_@zsnes.com), and
15                             Nach (n-a-c-h@users.sourceforge.net)
16                                           
17   C4 C++ code
18   (c) Copyright 2003 Brad Jorsch
19
20   DSP-1 emulator code
21   (c) Copyright 1998 - 2003 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,
22                             John Weidman (jweidman@slip.net),
23                             neviksti (neviksti@hotmail.com), and
24                             Kris Bleakley (stinkfish@bigpond.com)
25  
26   DSP-2 emulator code
27   (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
28                      Lord Nightmare (lord_nightmare@users.sourceforge.net
29
30   OBC1 emulator code
31   (c) Copyright 2001 - 2003 zsKnight, pagefault (pagefault@zsnes.com)
32   Ported from x86 assembler to C by sanmaiwashi
33
34   SPC7110 and RTC C++ emulator code
35   (c) Copyright 2002 Matthew Kendora with research by
36                      zsKnight, John Weidman, and Dark Force
37
38   S-RTC C emulator code
39   (c) Copyright 2001 John Weidman
40   
41   Super FX x86 assembler emulator code 
42   (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault 
43
44   Super FX C emulator code 
45   (c) Copyright 1997 - 1999 Ivar and Gary Henderson.
46
47
48
49  
50   Specific ports contains the works of other authors. See headers in
51   individual files.
52  
53   Snes9x homepage: http://www.snes9x.com
54  
55   Permission to use, copy, modify and distribute Snes9x in both binary and
56   source form, for non-commercial purposes, is hereby granted without fee,
57   providing that this license information and copyright notice appear with
58   all copies and any derived work.
59  
60   This software is provided 'as-is', without any express or implied
61   warranty. In no event shall the authors be held liable for any damages
62   arising from the use of this software.
63  
64   Snes9x is freeware for PERSONAL USE only. Commercial users should
65   seek permission of the copyright holders first. Commercial use includes
66   charging money for Snes9x or software derived from Snes9x.
67  
68   The copyright holders request that bug fixes and improvements to the code
69   should be forwarded to them so everyone can benefit from the modifications
70   in future versions.
71  
72   Super NES and Super Nintendo Entertainment System are trademarks of
73   Nintendo Co., Limited and its subsidiary companies.
74 *******************************************************************************/
75
76 #ifndef __GP32__
77 #include <stdlib.h>
78 #endif
79
80 #include <math.h>
81 #include "c4.h"
82 //#include "memmap.h"
83
84 extern "C" {
85
86 short C4WFXVal;
87 short C4WFYVal;
88 short C4WFZVal;
89 short C4WFX2Val;
90 short C4WFY2Val;
91 short C4WFDist;
92 short C4WFScale;
93
94 static double tanval;
95 static double c4x, c4y, c4z;
96 static double c4x2, c4y2, c4z2;
97
98 void C4TransfWireFrame ()
99 {
100     c4x = (double) C4WFXVal;
101     c4y = (double) C4WFYVal;
102     c4z = (double) C4WFZVal - 0x95;
103     
104     // Rotate X
105     tanval = -(double) C4WFX2Val * 3.14159265 * 2 / 128;
106     c4y2 = c4y * cos (tanval) - c4z * sin (tanval);
107     c4z2 = c4y * sin (tanval) + c4z * cos (tanval);
108     
109     // Rotate Y
110     tanval = -(double)C4WFY2Val*3.14159265*2/128;
111     c4x2 = c4x * cos (tanval) + c4z2 * sin (tanval);
112     c4z = c4x * - sin (tanval) + c4z2 * cos (tanval);
113     
114     // Rotate Z
115     tanval = -(double) C4WFDist * 3.14159265*2 / 128;
116     c4x = c4x2 * cos (tanval) - c4y2 * sin (tanval);
117     c4y = c4x2 * sin (tanval) + c4y2 * cos (tanval);
118     
119     // Scale
120     C4WFXVal = (short) (c4x*(double)C4WFScale/(0x90*(c4z+0x95))*0x95);
121     C4WFYVal = (short) (c4y*(double)C4WFScale/(0x90*(c4z+0x95))*0x95);
122 }
123
124 void C4TransfWireFrame2 ()
125 {
126     c4x = (double)C4WFXVal;
127     c4y = (double)C4WFYVal;
128     c4z = (double)C4WFZVal;
129     
130     // Rotate X
131     tanval = -(double) C4WFX2Val * 3.14159265 * 2 / 128;
132     c4y2 = c4y * cos (tanval) - c4z * sin (tanval);
133     c4z2 = c4y * sin (tanval) + c4z * cos (tanval);
134     
135     // Rotate Y
136     tanval = -(double) C4WFY2Val * 3.14159265 * 2 / 128;
137     c4x2 = c4x * cos (tanval) + c4z2 * sin (tanval);
138     c4z = c4x * -sin (tanval) + c4z2 * cos (tanval);
139     
140     // Rotate Z
141     tanval = -(double)C4WFDist * 3.14159265 * 2 / 128;
142     c4x = c4x2 * cos (tanval) - c4y2 * sin (tanval);
143     c4y = c4x2 * sin (tanval) + c4y2 * cos (tanval);
144     
145     // Scale
146     C4WFXVal =(short)(c4x * (double)C4WFScale / 0x100);
147     C4WFYVal =(short)(c4y * (double)C4WFScale / 0x100);
148 }
149
150 void C4CalcWireFrame ()
151 {
152     C4WFXVal = C4WFX2Val - C4WFXVal;
153     C4WFYVal = C4WFY2Val - C4WFYVal;
154     if (abs (C4WFXVal) > abs (C4WFYVal))
155     {
156         C4WFDist = abs (C4WFXVal) + 1;
157         C4WFYVal = (short) (256 * (double) C4WFYVal / abs (C4WFXVal));
158         if (C4WFXVal < 0)
159             C4WFXVal = -256;
160         else 
161             C4WFXVal = 256;
162     }
163     else
164     {
165         if (C4WFYVal != 0) 
166         {
167             C4WFDist = abs(C4WFYVal)+1;
168             C4WFXVal = (short) (256 * (double)C4WFXVal / abs (C4WFYVal));
169             if (C4WFYVal < 0)
170                 C4WFYVal = -256;
171             else 
172                 C4WFYVal = 256;
173         }
174         else 
175             C4WFDist = 0;
176     }
177 }
178
179 short C41FXVal;
180 short C41FYVal;
181 short C41FAngleRes;
182 short C41FDist;
183 short C41FDistVal;
184
185 void C4Op1F ()
186 {
187     if (C41FXVal == 0) 
188     {
189         if (C41FYVal > 0) 
190             C41FAngleRes = 0x80;
191         else 
192             C41FAngleRes = 0x180;
193     }
194     else 
195     {
196         tanval = (double) C41FYVal / C41FXVal;
197         C41FAngleRes = (short) (atan (tanval) / (3.141592675 * 2) * 512);
198         C41FAngleRes = C41FAngleRes;
199         if (C41FXVal< 0) 
200             C41FAngleRes += 0x100;
201         C41FAngleRes &= 0x1FF;
202     }
203 }
204
205 void C4Op15()
206 {
207     tanval = sqrt ((double) C41FYVal * C41FYVal + (double) C41FXVal * C41FXVal);
208     C41FDist = (short) tanval;
209 }
210
211 void C4Op0D()
212 {
213     tanval = sqrt ((double) C41FYVal * C41FYVal + (double) C41FXVal * C41FXVal);
214     tanval = C41FDistVal / tanval;
215     C41FYVal = (short) (C41FYVal * tanval * 0.99);
216     C41FXVal = (short) (C41FXVal * tanval * 0.98);
217 }
218
219 #ifdef ZSNES_C4
220 void C4LoaDMem(char *C4RAM)
221 {
222   memmove(C4RAM+(READ_WORD(C4RAM+0x1f45)&0x1fff), 
223           S9xGetMemPointer(READ_3WORD(C4RAM+0x1f40)),
224           READ_WORD(C4RAM+0x1f43));
225 }
226 #endif
227 }//end extern C
228