149d44b3f978556a000e13ee04a6d0c816bd9871
[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
91 uint16 DSP2Op09Word1=0;
92 uint16 DSP2Op09Word2=0;
93 bool DSP2Op05HasLen=false;
94 int DSP2Op05Len=0;
95 bool DSP2Op06HasLen=false;
96 int DSP2Op06Len=0;
97 uint8 DSP2Op05Transparent=0;
98
99 void DSP2_Op05 ()
100 {
101         uint8 color;
102         // Overlay bitmap with transparency.
103         // Input:
104         //
105         //   Bitmap 1:  i[0] <=> i[size-1]
106         //   Bitmap 2:  i[size] <=> i[2*size-1]
107         //
108         // Output:
109         //
110         //   Bitmap 3:  o[0] <=> o[size-1]
111         //
112         // Processing:
113         //
114         //   Process all 4-bit pixels (nibbles) in the bitmap
115         //
116         //   if ( BM2_pixel == transparent_color )
117         //      pixelout = BM1_pixel
118         //   else
119         //      pixelout = BM2_pixel
120
121         // The max size bitmap is limited to 255 because the size parameter is a byte
122         // I think size=0 is an error.  The behavior of the chip on size=0 is to
123         // return the last value written to DR if you read DR on Op05 with
124         // size = 0.  I don't think it's worth implementing this quirk unless it's
125         // proven necessary.
126
127         int n;
128         unsigned char c1;
129         unsigned char c2;
130         unsigned char *p1 = DSP1.parameters;
131         unsigned char *p2 = &DSP1.parameters[DSP2Op05Len];
132         unsigned char *p3 = DSP1.output;
133
134         color = DSP2Op05Transparent&0x0f;
135
136         for( n = 0; n < DSP2Op05Len; n++ )
137         {
138                 c1 = *p1++;
139                 c2 = *p2++;
140                 *p3++ = ( ((c2 >> 4) == color ) ? c1 & 0xf0: c2 & 0xf0 ) |
141                         ( ((c2 & 0x0f)==color) ? c1 & 0x0f: c2 & 0x0f );
142         }
143 }
144
145 void DSP2_Op01 ()
146 {
147         // Op01 size is always 32 bytes input and output.
148         // The hardware does strange things if you vary the size.
149         
150         int j;
151         unsigned char c0, c1, c2, c3;
152         unsigned char *p1 = DSP1.parameters;
153         unsigned char *p2a = DSP1.output;
154         unsigned char *p2b = &DSP1.output[16];  // halfway
155
156         // Process 8 blocks of 4 bytes each
157
158         for ( j = 0; j < 8; j++ )
159         {
160                 c0 = *p1++;
161                 c1 = *p1++;
162                 c2 = *p1++;
163                 c3 = *p1++;
164
165                 *p2a++ = (c0 & 0x10) << 3 |
166                              (c0 & 0x01) << 6 |
167                              (c1 & 0x10) << 1 |
168                              (c1 & 0x01) << 4 |
169                              (c2 & 0x10) >> 1 |
170                              (c2 & 0x01) << 2 |
171                              (c3 & 0x10) >> 3 |
172                              (c3 & 0x01);
173
174                 *p2a++ = (c0 & 0x20) << 2 |
175                              (c0 & 0x02) << 5 |
176                              (c1 & 0x20)      |
177                              (c1 & 0x02) << 3 |
178                              (c2 & 0x20) >> 2 |
179                              (c2 & 0x02) << 1 |
180                              (c3 & 0x20) >> 4 |
181                              (c3 & 0x02) >> 1;
182
183                 *p2b++ = (c0 & 0x40) << 1 |
184                              (c0 & 0x04) << 4 |
185                              (c1 & 0x40) >> 1 |
186                              (c1 & 0x04) << 2 |
187                              (c2 & 0x40) >> 3 |
188                              (c2 & 0x04)      |
189                              (c3 & 0x40) >> 5 |
190                              (c3 & 0x04) >> 2;
191
192
193                 *p2b++ = (c0 & 0x80)      |
194                              (c0 & 0x08) << 3 |
195                              (c1 & 0x80) >> 2 |
196                              (c1 & 0x08) << 1 |
197                              (c2 & 0x80) >> 4 |
198                              (c2 & 0x08) >> 1 |
199                              (c3 & 0x80) >> 6 |
200                              (c3 & 0x08) >> 3;
201         }
202         return;
203 }
204
205 void DSP2_Op06 ()
206 {
207         // Input:
208         //    size
209         //    bitmap
210
211         int     i, j;
212
213         for ( i = 0, j = DSP2Op06Len - 1; i < DSP2Op06Len; i++, j-- )
214         {
215                 DSP1.output[j] = (DSP1.parameters[i] << 4) | (DSP1.parameters[i] >> 4);
216         }
217 }
218
219 bool DSP2Op0DHasLen=false;
220 int DSP2Op0DOutLen=0;
221 int DSP2Op0DInLen=0;
222
223 #ifndef DSP2_BIT_ACCURRATE_CODE
224
225 // Scale bitmap based on input length out output length
226
227 void DSP2_Op0D()
228 {
229         // Overload's algorithm - use this unless doing hardware testing
230
231         // One note:  the HW can do odd byte scaling but since we divide
232         // by two to get the count of bytes this won't work well for
233         // odd byte scaling (in any of the current algorithm implementations).
234         // So far I haven't seen Dungeon Master use it.
235         // If it does we can adjust the parameters and code to work with it
236
237         int i;
238         int pixel_offset;
239         uint8 pixelarray[512];
240
241         for(i=0; i<DSP2Op0DOutLen*2; i++)
242         {
243                 pixel_offset = (i * DSP2Op0DInLen) / DSP2Op0DOutLen;
244                 if ( (pixel_offset&1) == 0 )
245                         pixelarray[i] = DSP1.parameters[pixel_offset>>1] >> 4;
246                 else
247                         pixelarray[i] = DSP1.parameters[pixel_offset>>1] & 0x0f; 
248         }
249
250         for ( i=0; i < DSP2Op0DOutLen; i++ )
251                 DSP1.output[i] = ( pixelarray[i<<1] << 4 ) | pixelarray[(i<<1)+1];
252 }
253
254 #else
255
256 void DSP2_Op0D()
257 {
258         // Bit accurate hardware algorithm - uses fixed point math
259         // This should match the DSP2 Op0D output exactly
260         // I wouldn't recommend using this unless you're doing hardware debug.
261         // In some situations it has small visual artifacts that
262         // are not readily apparent on a TV screen but show up clearly
263         // on a monitor.  Use Overload's scaling instead.
264         // This is for hardware verification testing.
265         //
266         // One note:  the HW can do odd byte scaling but since we divide
267         // by two to get the count of bytes this won't work well for
268         // odd byte scaling (in any of the current algorithm implementations).
269         // So far I haven't seen Dungeon Master use it.
270         // If it does we can adjust the parameters and code to work with it
271
272
273         uint32 multiplier;      // Any size int >= 32-bits
274         uint32 pixloc;          // match size of multiplier
275         int     i, j;
276         uint8 pixelarray[512];
277
278         if (DSP2Op0DInLen <= DSP2Op0DOutLen)
279                 multiplier = 0x10000;   // In our self defined fixed point 0x10000 == 1
280         else
281                 multiplier = (DSP2Op0DInLen << 17) / ((DSP2Op0DOutLen<<1) + 1);
282
283         pixloc = 0;
284         for ( i=0; i < DSP2Op0DOutLen * 2; i++ )
285         {
286                 j = pixloc >> 16;
287
288                 if ( j & 1 )
289                         pixelarray[i] = DSP1.parameters[j>>1] & 0x0f;
290                 else
291                         pixelarray[i] = (DSP1.parameters[j>>1] & 0xf0) >> 4;
292
293                 pixloc += multiplier;
294         }
295
296         for ( i=0; i < DSP2Op0DOutLen; i++ )
297                 DSP1.output[i] = ( pixelarray[i<<1] << 4 ) | pixelarray[(i<<1)+1];
298 }
299
300 #endif
301
302 #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
303
304 void DSP2_Op0D()
305 {
306         // Float implementation of Neviksti's algorithm
307         // This is the right algorithm to match the DSP2 bits but the precision
308         // of the PC float does not match the precision of the fixed point math
309         // on the DSP2 causing occasional one off data mismatches (which should
310         // be no problem because its just a one pixel difference in a scaled image
311         // to be displayed).
312
313         float multiplier;
314         float pixloc;
315         int     i, j;
316         uint8 pixelarray[512];
317
318         if (DSP2Op0DInLen <= DSP2Op0DOutLen)
319                 multiplier = (float) 1.0;
320         else
321                 multiplier = (float) ((DSP2Op0DInLen * 2.0) / (DSP2Op0DOutLen * 2.0 + 1.0));
322
323         pixloc = 0.0;
324         for ( i=0; i < DSP2Op0DOutLen * 2; i++ )
325         {
326                 // j = (int)(i * multiplier);
327                 j = (int) pixloc;
328
329                 if ( j & 1 )
330                         pixelarray[i] = DSP1.parameters[j>>1] & 0x0f;
331                 else
332                         pixelarray[i] = (DSP1.parameters[j>>1] & 0xf0) >> 4;
333
334                 pixloc += multiplier;   // use an add in the loop instead of multiply to increase loop speed
335         }
336
337         for ( i=0; i < DSP2Op0DOutLen; i++ )
338                 DSP1.output[i] = ( pixelarray[i<<1] << 4 ) | pixelarray[(i<<1)+1];
339 }
340
341 #endif