workaround a problem with the harmattan gcc
[drnoksnes] / dsp2emu.c
1 /*******************************************************************************
2   Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
3  
4   (c) Copyright 1996 - 2002 Gary Henderson (gary.henderson@ntlworld.com) and
5                             Jerremy Koot (jkoot@snes9x.com)
6
7   (c) Copyright 2001 - 2004 John Weidman (jweidman@slip.net)
8
9   (c) Copyright 2002 - 2004 Brad Jorsch (anomie@users.sourceforge.net),
10                             funkyass (funkyass@spam.shaw.ca),
11                             Joel Yliluoma (http://iki.fi/bisqwit/)
12                             Kris Bleakley (codeviolation@hotmail.com),
13                             Matthew Kendora,
14                             Nach (n-a-c-h@users.sourceforge.net),
15                             Peter Bortas (peter@bortas.org) and
16                             zones (kasumitokoduck@yahoo.com)
17
18   C4 x86 assembler and some C emulation code
19   (c) Copyright 2000 - 2003 zsKnight (zsknight@zsnes.com),
20                             _Demo_ (_demo_@zsnes.com), and Nach
21
22   C4 C++ code
23   (c) Copyright 2003 Brad Jorsch
24
25   DSP-1 emulator code
26   (c) Copyright 1998 - 2004 Ivar (ivar@snes9x.com), _Demo_, Gary Henderson,
27                             John Weidman, neviksti (neviksti@hotmail.com),
28                             Kris Bleakley, Andreas Naive
29
30   DSP-2 emulator code
31   (c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
32                      Lord Nightmare (lord_nightmare@users.sourceforge.net
33
34   OBC1 emulator code
35   (c) Copyright 2001 - 2004 zsKnight, pagefault (pagefault@zsnes.com) and
36                             Kris Bleakley
37   Ported from x86 assembler to C by sanmaiwashi
38
39   SPC7110 and RTC C++ emulator code
40   (c) Copyright 2002 Matthew Kendora with research by
41                      zsKnight, John Weidman, and Dark Force
42
43   S-DD1 C emulator code
44   (c) Copyright 2003 Brad Jorsch with research by
45                      Andreas Naive and John Weidman
46  
47   S-RTC C emulator code
48   (c) Copyright 2001 John Weidman
49   
50   ST010 C++ emulator code
51   (c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora
52
53   Super FX x86 assembler emulator code 
54   (c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault 
55
56   Super FX C emulator code 
57   (c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman
58
59
60   SH assembler code partly based on x86 assembler code
61   (c) Copyright 2002 - 2004 Marcus Comstedt (marcus@mc.pp.se) 
62
63  
64   Specific ports contains the works of other authors. See headers in
65   individual files.
66  
67   Snes9x homepage: http://www.snes9x.com
68  
69   Permission to use, copy, modify and distribute Snes9x in both binary and
70   source form, for non-commercial purposes, is hereby granted without fee,
71   providing that this license information and copyright notice appear with
72   all copies and any derived work.
73  
74   This software is provided 'as-is', without any express or implied
75   warranty. In no event shall the authors be held liable for any damages
76   arising from the use of this software.
77  
78   Snes9x is freeware for PERSONAL USE only. Commercial users should
79   seek permission of the copyright holders first. Commercial use includes
80   charging money for Snes9x or software derived from Snes9x.
81  
82   The copyright holders request that bug fixes and improvements to the code
83   should be forwarded to them so everyone can benefit from the modifications
84   in future versions.
85  
86   Super NES and Super Nintendo Entertainment System are trademarks of
87   Nintendo Co., Limited and its subsidiary companies.
88 *******************************************************************************/
89
90 #include "port.h"
91
92 uint16 DSP2Op09Word1=0;
93 uint16 DSP2Op09Word2=0;
94 bool DSP2Op05HasLen=false;
95 int DSP2Op05Len=0;
96 bool DSP2Op06HasLen=false;
97 int DSP2Op06Len=0;
98 uint8 DSP2Op05Transparent=0;
99
100 void DSP2_Op05 ()
101 {
102         uint8 color;
103         // Overlay bitmap with transparency.
104         // Input:
105         //
106         //   Bitmap 1:  i[0] <=> i[size-1]
107         //   Bitmap 2:  i[size] <=> i[2*size-1]
108         //
109         // Output:
110         //
111         //   Bitmap 3:  o[0] <=> o[size-1]
112         //
113         // Processing:
114         //
115         //   Process all 4-bit pixels (nibbles) in the bitmap
116         //
117         //   if ( BM2_pixel == transparent_color )
118         //      pixelout = BM1_pixel
119         //   else
120         //      pixelout = BM2_pixel
121
122         // The max size bitmap is limited to 255 because the size parameter is a byte
123         // I think size=0 is an error.  The behavior of the chip on size=0 is to
124         // return the last value written to DR if you read DR on Op05 with
125         // size = 0.  I don't think it's worth implementing this quirk unless it's
126         // proven necessary.
127
128         int n;
129         unsigned char c1;
130         unsigned char c2;
131         unsigned char *p1 = DSP1.parameters;
132         unsigned char *p2 = &DSP1.parameters[DSP2Op05Len];
133         unsigned char *p3 = DSP1.output;
134
135         color = DSP2Op05Transparent&0x0f;
136
137         for( n = 0; n < DSP2Op05Len; n++ )
138         {
139                 c1 = *p1++;
140                 c2 = *p2++;
141                 *p3++ = ( ((c2 >> 4) == color ) ? c1 & 0xf0: c2 & 0xf0 ) |
142                         ( ((c2 & 0x0f)==color) ? c1 & 0x0f: c2 & 0x0f );
143         }
144 }
145
146 void DSP2_Op01 ()
147 {
148         // Op01 size is always 32 bytes input and output.
149         // The hardware does strange things if you vary the size.
150         
151         int j;
152         unsigned char c0, c1, c2, c3;
153         unsigned char *p1 = DSP1.parameters;
154         unsigned char *p2a = DSP1.output;
155         unsigned char *p2b = &DSP1.output[16];  // halfway
156
157         // Process 8 blocks of 4 bytes each
158
159         for ( j = 0; j < 8; j++ )
160         {
161                 c0 = *p1++;
162                 c1 = *p1++;
163                 c2 = *p1++;
164                 c3 = *p1++;
165
166                 *p2a++ = (c0 & 0x10) << 3 |
167                              (c0 & 0x01) << 6 |
168                              (c1 & 0x10) << 1 |
169                              (c1 & 0x01) << 4 |
170                              (c2 & 0x10) >> 1 |
171                              (c2 & 0x01) << 2 |
172                              (c3 & 0x10) >> 3 |
173                              (c3 & 0x01);
174
175                 *p2a++ = (c0 & 0x20) << 2 |
176                              (c0 & 0x02) << 5 |
177                              (c1 & 0x20)      |
178                              (c1 & 0x02) << 3 |
179                              (c2 & 0x20) >> 2 |
180                              (c2 & 0x02) << 1 |
181                              (c3 & 0x20) >> 4 |
182                              (c3 & 0x02) >> 1;
183
184                 *p2b++ = (c0 & 0x40) << 1 |
185                              (c0 & 0x04) << 4 |
186                              (c1 & 0x40) >> 1 |
187                              (c1 & 0x04) << 2 |
188                              (c2 & 0x40) >> 3 |
189                              (c2 & 0x04)      |
190                              (c3 & 0x40) >> 5 |
191                              (c3 & 0x04) >> 2;
192
193
194                 *p2b++ = (c0 & 0x80)      |
195                              (c0 & 0x08) << 3 |
196                              (c1 & 0x80) >> 2 |
197                              (c1 & 0x08) << 1 |
198                              (c2 & 0x80) >> 4 |
199                              (c2 & 0x08) >> 1 |
200                              (c3 & 0x80) >> 6 |
201                              (c3 & 0x08) >> 3;
202         }
203         return;
204 }
205
206 void DSP2_Op06 ()
207 {
208         // Input:
209         //    size
210         //    bitmap
211
212         int     i, j;
213
214         for ( i = 0, j = DSP2Op06Len - 1; i < DSP2Op06Len; i++, j-- )
215         {
216                 DSP1.output[j] = (DSP1.parameters[i] << 4) | (DSP1.parameters[i] >> 4);
217         }
218 }
219
220 bool DSP2Op0DHasLen=false;
221 int DSP2Op0DOutLen=0;
222 int DSP2Op0DInLen=0;
223
224 #ifndef DSP2_BIT_ACCURRATE_CODE
225
226 // Scale bitmap based on input length out output length
227
228 void DSP2_Op0D()
229 {
230         // Overload's algorithm - use this unless doing hardware testing
231
232         // One note:  the HW can do odd byte scaling but since we divide
233         // by two to get the count of bytes this won't work well for
234         // odd byte scaling (in any of the current algorithm implementations).
235         // So far I haven't seen Dungeon Master use it.
236         // If it does we can adjust the parameters and code to work with it
237
238         int i;
239         int pixel_offset;
240         uint8 pixelarray[512];
241
242         for(i=0; i<DSP2Op0DOutLen*2; i++)
243         {
244                 pixel_offset = (i * DSP2Op0DInLen) / DSP2Op0DOutLen;
245                 if ( (pixel_offset&1) == 0 )
246                         pixelarray[i] = DSP1.parameters[pixel_offset>>1] >> 4;
247                 else
248                         pixelarray[i] = DSP1.parameters[pixel_offset>>1] & 0x0f; 
249         }
250
251         for ( i=0; i < DSP2Op0DOutLen; i++ )
252                 DSP1.output[i] = ( pixelarray[i<<1] << 4 ) | pixelarray[(i<<1)+1];
253 }
254
255 #else
256
257 void DSP2_Op0D()
258 {
259         // Bit accurate hardware algorithm - uses fixed point math
260         // This should match the DSP2 Op0D output exactly
261         // I wouldn't recommend using this unless you're doing hardware debug.
262         // In some situations it has small visual artifacts that
263         // are not readily apparent on a TV screen but show up clearly
264         // on a monitor.  Use Overload's scaling instead.
265         // This is for hardware verification testing.
266         //
267         // One note:  the HW can do odd byte scaling but since we divide
268         // by two to get the count of bytes this won't work well for
269         // odd byte scaling (in any of the current algorithm implementations).
270         // So far I haven't seen Dungeon Master use it.
271         // If it does we can adjust the parameters and code to work with it
272
273
274         uint32 multiplier;      // Any size int >= 32-bits
275         uint32 pixloc;          // match size of multiplier
276         int     i, j;
277         uint8 pixelarray[512];
278
279         if (DSP2Op0DInLen <= DSP2Op0DOutLen)
280                 multiplier = 0x10000;   // In our self defined fixed point 0x10000 == 1
281         else
282                 multiplier = (DSP2Op0DInLen << 17) / ((DSP2Op0DOutLen<<1) + 1);
283
284         pixloc = 0;
285         for ( i=0; i < DSP2Op0DOutLen * 2; i++ )
286         {
287                 j = pixloc >> 16;
288
289                 if ( j & 1 )
290                         pixelarray[i] = DSP1.parameters[j>>1] & 0x0f;
291                 else
292                         pixelarray[i] = (DSP1.parameters[j>>1] & 0xf0) >> 4;
293
294                 pixloc += multiplier;
295         }
296
297         for ( i=0; i < DSP2Op0DOutLen; i++ )
298                 DSP1.output[i] = ( pixelarray[i<<1] << 4 ) | pixelarray[(i<<1)+1];
299 }
300
301 #endif
302
303 #if 0   // Probably no reason to use this code - it's not quite bit accurate and it doesn't look as good as Overload's algorithm
304
305 void DSP2_Op0D()
306 {
307         // Float implementation of Neviksti's algorithm
308         // This is the right algorithm to match the DSP2 bits but the precision
309         // of the PC float does not match the precision of the fixed point math
310         // on the DSP2 causing occasional one off data mismatches (which should
311         // be no problem because its just a one pixel difference in a scaled image
312         // to be displayed).
313
314         float multiplier;
315         float pixloc;
316         int     i, j;
317         uint8 pixelarray[512];
318
319         if (DSP2Op0DInLen <= DSP2Op0DOutLen)
320                 multiplier = (float) 1.0;
321         else
322                 multiplier = (float) ((DSP2Op0DInLen * 2.0) / (DSP2Op0DOutLen * 2.0 + 1.0));
323
324         pixloc = 0.0;
325         for ( i=0; i < DSP2Op0DOutLen * 2; i++ )
326         {
327                 // j = (int)(i * multiplier);
328                 j = (int) pixloc;
329
330                 if ( j & 1 )
331                         pixelarray[i] = DSP1.parameters[j>>1] & 0x0f;
332                 else
333                         pixelarray[i] = (DSP1.parameters[j>>1] & 0xf0) >> 4;
334
335                 pixloc += multiplier;   // use an add in the loop instead of multiply to increase loop speed
336         }
337
338         for ( i=0; i < DSP2Op0DOutLen; i++ )
339                 DSP1.output[i] = ( pixelarray[i<<1] << 4 ) | pixelarray[(i<<1)+1];
340 }
341
342 #endif
343