-
Notifications
You must be signed in to change notification settings - Fork 30
/
dsp1.cpp
575 lines (499 loc) · 15 KB
/
dsp1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*******************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2003 Gary Henderson ([email protected]) and
Jerremy Koot ([email protected])
(c) Copyright 2002 - 2003 Matthew Kendora and
Brad Jorsch ([email protected])
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 zsKnight ([email protected]),
_Demo_ ([email protected]), and
Nach ([email protected])
C4 C++ code
(c) Copyright 2003 Brad Jorsch
DSP-1 emulator code
(c) Copyright 1998 - 2003 Ivar ([email protected]), _Demo_, Gary Henderson,
John Weidman ([email protected]),
neviksti ([email protected]), and
Kris Bleakley ([email protected])
DSP-2 emulator code
(c) Copyright 2003 Kris Bleakley, John Weidman, neviksti, Matthew Kendora, and
Lord Nightmare ([email protected]
OBC1 emulator code
(c) Copyright 2001 - 2003 zsKnight, pagefault ([email protected])
Ported from x86 assembler to C by sanmaiwashi
SPC7110 and RTC C++ emulator code
(c) Copyright 2002 Matthew Kendora with research by
zsKnight, John Weidman, and Dark Force
S-RTC C emulator code
(c) Copyright 2001 John Weidman
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar and Gary Henderson.
Specific ports contains the works of other authors. See headers in
individual files.
Snes9x homepage: http://www.snes9x.com
Permission to use, copy, modify and distribute Snes9x in both binary and
source form, for non-commercial purposes, is hereby granted without fee,
providing that this license information and copyright notice appear with
all copies and any derived work.
This software is provided 'as-is', without any express or implied
warranty. In no event shall the authors be held liable for any damages
arising from the use of this software.
Snes9x is freeware for PERSONAL USE only. Commercial users should
seek permission of the copyright holders first. Commercial use includes
charging money for Snes9x or software derived from Snes9x.
The copyright holders request that bug fixes and improvements to the code
should be forwarded to them so everyone can benefit from the modifications
in future versions.
Super NES and Super Nintendo Entertainment System are trademarks of
Nintendo Co., Limited and its subsidiary companies.
*******************************************************************************/
// modify to little endian(PSP) version by ruka
#include "snes9x.h"
#include "dsp1.h"
#include "missing.h"
#include "memmap.h"
//#include "math.h"
#include "dsp1emu.c"
//#include "dsp2emu.c"
void S9xInitDSP1 ()
{
static bool8 init = FALSE;
if (!init)
{
InitDSP ();
init = TRUE;
}
}
void S9xResetDSP1 ()
{
S9xInitDSP1 ();
DSP1.waiting4command = TRUE;
DSP1.in_count = 0;
DSP1.out_count = 0;
DSP1.in_index = 0;
DSP1.out_index = 0;
DSP1.first_parameter = TRUE;
}
void DSP1SetByte(uint8 byte, uint16 address)
{
if( (address & 0xf000) == 0x6000 || (address & 0x7fff) < 0x4000 )
{
// if ((address & 1) == 0)
// {
if((DSP1.command==0x0A||DSP1.command==0x1A)&&DSP1.out_count!=0)
{
DSP1.out_count--;
DSP1.out_index++;
return;
}
else if (DSP1.waiting4command)
{
DSP1.command = byte;
DSP1.in_index = 0;
DSP1.waiting4command = FALSE;
DSP1.first_parameter = TRUE;
// printf("Op%02X\n",byte);
// Mario Kart uses 0x00, 0x02, 0x06, 0x0c, 0x28, 0x0a
switch (byte)
{
case 0x00: DSP1.in_count = 2; break;
case 0x30:
case 0x10: DSP1.in_count = 2; break;
case 0x20: DSP1.in_count = 2; break;
case 0x24:
case 0x04: DSP1.in_count = 2; break;
case 0x08: DSP1.in_count = 3; break;
case 0x18: DSP1.in_count = 4; break;
case 0x28: DSP1.in_count = 3; break;
case 0x38: DSP1.in_count = 4; break;
case 0x2c:
case 0x0c: DSP1.in_count = 3; break;
case 0x3c:
case 0x1c: DSP1.in_count = 6; break;
case 0x32:
case 0x22:
case 0x12:
case 0x02: DSP1.in_count = 7; break;
case 0x0a: DSP1.in_count = 1; break;
case 0x3a:
case 0x2a:
case 0x1a:
DSP1. command =0x1a;
DSP1.in_count = 1; break;
case 0x16:
case 0x26:
case 0x36:
case 0x06: DSP1.in_count = 3; break;
case 0x1e:
case 0x2e:
case 0x3e:
case 0x0e: DSP1.in_count = 2; break;
case 0x05:
case 0x35:
case 0x31:
case 0x01: DSP1.in_count = 4; break;
case 0x15:
case 0x11: DSP1.in_count = 4; break;
case 0x25:
case 0x21: DSP1.in_count = 4; break;
case 0x09:
case 0x39:
case 0x3d:
case 0x0d: DSP1.in_count = 3; break;
case 0x19:
case 0x1d: DSP1.in_count = 3; break;
case 0x29:
case 0x2d: DSP1.in_count = 3; break;
case 0x33:
case 0x03: DSP1.in_count = 3; break;
case 0x13: DSP1.in_count = 3; break;
case 0x23: DSP1.in_count = 3; break;
case 0x3b:
case 0x0b: DSP1.in_count = 3; break;
case 0x1b: DSP1.in_count = 3; break;
case 0x2b: DSP1.in_count = 3; break;
case 0x34:
case 0x14: DSP1.in_count = 6; break;
case 0x07:
case 0x0f: DSP1.in_count = 1; break;
case 0x27:
case 0x2F: DSP1.in_count=1; break;
case 0x17:
case 0x37:
case 0x3F:
DSP1.command=0x1f;
case 0x1f: DSP1.in_count = 1; break;
// case 0x80: DSP1.in_count = 2; break;
default:
//printf("Op%02X\n",byte);
case 0x80:
DSP1.in_count = 0;
DSP1.waiting4command = TRUE;
DSP1.first_parameter = TRUE;
break;
}
DSP1.in_count<<=1;
}
else
{
DSP1.parameters [DSP1.in_index] = byte;
DSP1.first_parameter = FALSE;
DSP1.in_index++;
}
if (DSP1.waiting4command ||
(DSP1.first_parameter && byte == 0x80))
{
DSP1.waiting4command = TRUE;
DSP1.first_parameter = FALSE;
}
else if(DSP1.first_parameter && (DSP1.in_count != 0 || (DSP1.in_count==0&&DSP1.in_index==0)))
{
}
else
{
if (DSP1.in_count)
{
if (--DSP1.in_count == 0)
{
// Actually execute the command
DSP1.waiting4command = TRUE;
DSP1.out_index = 0;
switch (DSP1.command)
{
case 0x1f:
DSP1.out_count=2048;
break;
case 0x00: // Multiple
{
short Op00Multiplicand = (int16) DSP1.parameters16 [0];
short Op00Multiplier = (int16) DSP1.parameters16 [1];
short Op00Result= Op00Multiplicand * Op00Multiplier >> 15;
DSP1.out_count = 2;
DSP1.output16 [0] = Op00Result;
}
break;
case 0x20: // Multiple
{
short Op20Multiplicand = (int16) DSP1.parameters16 [0];
short Op20Multiplier = (int16) DSP1.parameters16 [1];
short Op20Result= Op20Multiplicand * Op20Multiplier >> 15;
Op20Result++;
DSP1.out_count = 2;
DSP1.output16 [0] = Op20Result;
}
break;
case 0x30:
case 0x10: // Inverse
{
signed short Op10Coefficient = (int16) DSP1.parameters16 [0];
signed short Op10Exponent = (int16) DSP1.parameters16 [1];
signed short Op10CoefficientR;
signed short Op10ExponentR;
DSP1_Inverse(Op10Coefficient, Op10Exponent, &Op10CoefficientR, &Op10ExponentR);
DSP1.out_count = 4;
DSP1.output16 [0] = Op10CoefficientR;
DSP1.output16 [1] = Op10ExponentR;
}
break;
case 0x24:
case 0x04: // Sin and Cos of angle
{
short Op04Angle = (int16) DSP1.parameters16 [0];
short Op04Radius = (uint16) DSP1.parameters16 [1];
short Op04Sin = DSP1_Sin(Op04Angle) * Op04Radius >> 15;
short Op04Cos = DSP1_Cos(Op04Angle) * Op04Radius >> 15;
DSP1.out_count = 4;
DSP1.output16 [0] = Op04Sin;
DSP1.output16 [1] = Op04Cos;
}
break;
case 0x08: // Radius
{
short Op08X = (int16) DSP1.parameters16 [0];
short Op08Y = (int16) DSP1.parameters16 [1];
short Op08Z = (int16) DSP1.parameters16 [2];
int Op08Size = (Op08X * Op08X + Op08Y * Op08Y + Op08Z * Op08Z) << 1;
DSP1.out_count = 4;
DSP1.output32 [0] = Op08Size;
}
break;
case 0x18: // Range
{
short Op18X = (int16) DSP1.parameters16 [0];
short Op18Y = (int16) DSP1.parameters16 [1];
short Op18Z = (int16) DSP1.parameters16 [2];
short Op18R = (int16) DSP1.parameters16 [3];
short Op18D = (Op18X * Op18X + Op18Y * Op18Y + Op18Z * Op18Z - Op18R * Op18R) >> 15;
DSP1.out_count = 2;
DSP1.output16 [0] = Op18D;
}
break;
case 0x38: // Range
{
short Op38X = (int16) DSP1.parameters16 [0];
short Op38Y = (int16) DSP1.parameters16 [1];
short Op38Z = (int16) DSP1.parameters16 [2];
short Op38R = (int16) DSP1.parameters16 [3];
short Op38D = (Op38X * Op38X + Op38Y * Op38Y + Op38Z * Op38Z - Op38R * Op38R) >> 15;
Op38D++;
DSP1.out_count = 2;
DSP1.output16 [0] = Op38D;
}
break;
case 0x28: // Distance (vector length)
{
short Op28X = (int16) DSP1.parameters16 [0];
short Op28Y = (int16) DSP1.parameters16 [1];
short Op28Z = (int16) DSP1.parameters16 [2];
int Radius = Op28X * Op28X + Op28Y * Op28Y + Op28Z * Op28Z;
short Op28R;
if (Radius == 0) Op28R = 0;
else
{
short C, E;
DSP1_Normalizefloat(Radius, &C, &E);
if (E & 1) C = C * 0x4000 >> 15;
short Pos = C * 0x0040 >> 15;
short Node1 = DSP1ROM[(0x00d5 + Pos)&1023];
short Node2 = DSP1ROM[(0x00d6 + Pos)&1023];
Op28R = ((Node2 - Node1) * (C & 0x1ff) >> 9) + Node1;
Op28R >>= (E >> 1);
}
DSP1.out_count = 2;
DSP1.output16 [0] = Op28R;
}
break;
case 0x2c:
case 0x0c: // Rotate (2D rotate)
{
short Op0CA = (int16) DSP1.parameters16 [0];
short Op0CX1 = (int16) DSP1.parameters16 [1];
short Op0CY1 = (int16) DSP1.parameters16 [2];
short Op0CX2 = (Op0CY1 * DSP1_Sin(Op0CA) >> 15) + (Op0CX1 * DSP1_Cos(Op0CA) >> 15);
short Op0CY2 = (Op0CY1 * DSP1_Cos(Op0CA) >> 15) - (Op0CX1 * DSP1_Sin(Op0CA) >> 15);
DSP1.out_count = 4;
DSP1.output16 [0] = Op0CX2;
DSP1.output16 [1] = Op0CY2;
}
break;
case 0x3c:
case 0x1c: // Polar (3D rotate)
{
short Op1CZ = DSP1.parameters16 [0];
//MK: reversed X and Y on neviksti and John's advice.
short Op1CY = DSP1.parameters16 [1];
short Op1CX = DSP1.parameters16 [2];
short Op1CXBR = DSP1.parameters16 [3];
short Op1CYBR = DSP1.parameters16 [4];
short Op1CZBR = DSP1.parameters16 [5];
// Rotate Around Op1CZ1
short Op1CX1 = (Op1CYBR * DSP1_Sin(Op1CZ) >> 15) + (Op1CXBR * DSP1_Cos(Op1CZ) >> 15);
short Op1CY1 = (Op1CYBR * DSP1_Cos(Op1CZ) >> 15) - (Op1CXBR * DSP1_Sin(Op1CZ) >> 15);
Op1CXBR = Op1CX1; Op1CYBR = Op1CY1;
// Rotate Around Op1CY1
short Op1CZ1 = (Op1CXBR * DSP1_Sin(Op1CY) >> 15) + (Op1CZBR * DSP1_Cos(Op1CY) >> 15);
Op1CX1 = (Op1CXBR * DSP1_Cos(Op1CY) >> 15) - (Op1CZBR * DSP1_Sin(Op1CY) >> 15);
short Op1CXAR = Op1CX1; Op1CZBR = Op1CZ1;
// Rotate Around Op1CX1
Op1CY1 = (Op1CZBR * DSP1_Sin(Op1CX) >> 15) + (Op1CYBR * DSP1_Cos(Op1CX) >> 15);
Op1CZ1 = (Op1CZBR * DSP1_Cos(Op1CX) >> 15) - (Op1CYBR * DSP1_Sin(Op1CX) >> 15);
short Op1CYAR = Op1CY1; short Op1CZAR = Op1CZ1;
DSP1.out_count = 6;
DSP1.output16 [0] = Op1CXAR;
DSP1.output16 [1] = Op1CYAR;
DSP1.output16 [2] = Op1CZAR;
}
break;
case 0x32:
case 0x22:
case 0x12:
case 0x02: // Parameter (Projection)
DSPOp02 ();
break;
case 0x3a: //1a Mirror
case 0x2a: //1a Mirror
case 0x1a: // Raster mode 7 matrix data
case 0x0a:
DSP1.Op0AVS = (short)DSP1.parameters16 [0];
DSPOp0A ();
DSP1.out_count = 8;
DSP1.output16 [0] = DSP1.Op0AA;
DSP1.output16 [1] = DSP1.Op0AB;
DSP1.output16 [2] = DSP1.Op0AC;
DSP1.output16 [3] = DSP1.Op0AD;
DSP1.in_index=0;
break;
case 0x16:
case 0x26:
case 0x36:
case 0x06: // Project object
DSPOp06 ();
break;
case 0x1e:
case 0x2e:
case 0x3e:
case 0x0e: // Target
{
short Op0EH = (int16) DSP1.parameters16 [0];
short Op0EV = (int16) DSP1.parameters16 [1];
// screen Directions UP
GetRXYPos(Op0EV, Op0EH);
DSP1.out_count = 4;
DSP1.output16 [0] = (short)(DSP1.RXRes);
DSP1.output16 [1] = (short)(DSP1.RYRes);
}
break;
// Extra commands used by Pilot Wings
case 0x05:
case 0x35:
case 0x31:
case 0x01: // Set attitude matrix A
DSPOp01 ();
break;
case 0x15:
case 0x11: // Set attitude matrix B
DSPOp11 ();
break;
case 0x25:
case 0x21: // Set attitude matrix C
DSPOp21 ();
break;
case 0x09:
case 0x39:
case 0x3d:
case 0x0d: // Objective matrix A
DSPOp0D ();
break;
case 0x19:
case 0x1d: // Objective matrix B
DSPOp1D ();
break;
case 0x29:
case 0x2d: // Objective matrix C
DSPOp2D ();
break;
case 0x33:
case 0x03: // Subjective matrix A
DSPOp03 ();
break;
case 0x13: // Subjective matrix B
DSPOp13 ();
break;
case 0x23: // Subjective matrix C
DSPOp23 ();
break;
case 0x3b:
case 0x0b:
DSPOp0B ();
break;
case 0x1b:
DSPOp1B ();
break;
case 0x2b:
DSPOp2B ();
break;
case 0x34:
case 0x14:
DSPOp14 ();
break;
case 0x27:
case 0x2F:
DSP1.out_count = 2;
DSP1.output16 [0] = 0x100;
break;
case 0x07:
case 0x0F:
DSP1.out_count = 2;
DSP1.output16 [0] = 0x0000;
break;
default:
break;
}
}
}
}
}
}
uint8 DSP1GetByte(uint16 address)
{
uint8 t;
if ((address & 0xf000) == 0x6000 ||
(address&0x7fff) < 0x4000)
{
if (DSP1.out_count)
{
t = (uint8) DSP1.output [DSP1.out_index];
DSP1.out_index++;
if (--DSP1.out_count == 0)
{
if (DSP1.command == 0x1a || DSP1.command == 0x0a)
{
DSPOp0A ();
DSP1.out_count = 8;
DSP1.out_index = 0;
DSP1.output16 [0] = DSP1.Op0AA;
DSP1.output16 [1] = DSP1.Op0AB;
DSP1.output16 [2] = DSP1.Op0AC;
DSP1.output16 [3] = DSP1.Op0AD;
}
if(DSP1.command==0x1f)
{
t=((uint8*)DSP1ROM)[DSP1.out_index-1];
}
}
DSP1.waiting4command = TRUE;
}
else
{
// Top Gear 3000 requires this value....
// if(4==Settings.DSPVersion)
t = 0xff;
//Ballz3d requires this one:
// else t = 0x00;
}
}
else t = 0x80;
return t;
}