forked from dmitrysmagin/snes9x4d-rzx50
-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsp3emu.cpp
728 lines (626 loc) · 20.1 KB
/
dsp3emu.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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
/*******************************************************************************
Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.
(c) Copyright 1996 - 2002 Gary Henderson ([email protected]) and
Jerremy Koot ([email protected])
(c) Copyright 2001 - 2004 John Weidman ([email protected])
(c) Copyright 2002 - 2004 Brad Jorsch ([email protected]),
funkyass ([email protected]),
Joel Yliluoma (http://iki.fi/bisqwit/)
Kris Bleakley ([email protected]),
Matthew Kendora,
Nach ([email protected]),
Peter Bortas ([email protected]) and
zones ([email protected])
C4 x86 assembler and some C emulation code
(c) Copyright 2000 - 2003 zsKnight ([email protected]),
_Demo_ ([email protected]), and Nach
C4 C++ code
(c) Copyright 2003 Brad Jorsch
DSP-1 emulator code
(c) Copyright 1998 - 2004 Ivar ([email protected]), _Demo_, Gary Henderson,
John Weidman, neviksti ([email protected]),
Kris Bleakley, Andreas Naive
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 - 2004 zsKnight, pagefault ([email protected]) and
Kris Bleakley
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-DD1 C emulator code
(c) Copyright 2003 Brad Jorsch with research by
Andreas Naive and John Weidman
S-RTC C emulator code
(c) Copyright 2001 John Weidman
ST010 C++ emulator code
(c) Copyright 2003 Feather, Kris Bleakley, John Weidman and Matthew Kendora
Super FX x86 assembler emulator code
(c) Copyright 1998 - 2003 zsKnight, _Demo_, and pagefault
Super FX C emulator code
(c) Copyright 1997 - 1999 Ivar, Gary Henderson and John Weidman
SH assembler code partly based on x86 assembler code
(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])
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.
*******************************************************************************/
uint16 DSP3_DataROM[1024] = {
0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100,
0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001,
0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,
0x0000, 0x000f, 0x0400, 0x0200, 0x0140, 0x0400, 0x0200, 0x0040,
0x007d, 0x007e, 0x007e, 0x007b, 0x007c, 0x007d, 0x007b, 0x007c,
0x0002, 0x0020, 0x0030, 0x0000, 0x000d, 0x0019, 0x0026, 0x0032,
0x003e, 0x004a, 0x0056, 0x0062, 0x006d, 0x0079, 0x0084, 0x008e,
0x0098, 0x00a2, 0x00ac, 0x00b5, 0x00be, 0x00c6, 0x00ce, 0x00d5,
0x00dc, 0x00e2, 0x00e7, 0x00ec, 0x00f1, 0x00f5, 0x00f8, 0x00fb,
0x00fd, 0x00ff, 0x0100, 0x0100, 0x0100, 0x00ff, 0x00fd, 0x00fb,
0x00f8, 0x00f5, 0x00f1, 0x00ed, 0x00e7, 0x00e2, 0x00dc, 0x00d5,
0x00ce, 0x00c6, 0x00be, 0x00b5, 0x00ac, 0x00a2, 0x0099, 0x008e,
0x0084, 0x0079, 0x006e, 0x0062, 0x0056, 0x004a, 0x003e, 0x0032,
0x0026, 0x0019, 0x000d, 0x0000, 0xfff3, 0xffe7, 0xffdb, 0xffce,
0xffc2, 0xffb6, 0xffaa, 0xff9e, 0xff93, 0xff87, 0xff7d, 0xff72,
0xff68, 0xff5e, 0xff54, 0xff4b, 0xff42, 0xff3a, 0xff32, 0xff2b,
0xff25, 0xff1e, 0xff19, 0xff14, 0xff0f, 0xff0b, 0xff08, 0xff05,
0xff03, 0xff01, 0xff00, 0xff00, 0xff00, 0xff01, 0xff03, 0xff05,
0xff08, 0xff0b, 0xff0f, 0xff13, 0xff18, 0xff1e, 0xff24, 0xff2b,
0xff32, 0xff3a, 0xff42, 0xff4b, 0xff54, 0xff5d, 0xff67, 0xff72,
0xff7c, 0xff87, 0xff92, 0xff9e, 0xffa9, 0xffb5, 0xffc2, 0xffce,
0xffda, 0xffe7, 0xfff3, 0x002b, 0x007f, 0x0020, 0x00ff, 0xff00,
0xffbe, 0x0000, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0x0045,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffc5, 0x0003, 0x0004, 0x0005, 0x0047, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,
0x0009, 0x004a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x004e, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,
0x0012, 0x0013, 0x0014, 0x0053, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,
0x0059, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,
0x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0x0060, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,
0x002b, 0x002c, 0x0068, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0071,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
0x003e, 0x003f, 0x0040, 0x0041, 0x007b, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,
0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
0x000b, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,
0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0050, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,
0x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,
0x0024, 0x0025, 0x0026, 0x005d, 0x0000, 0x0000, 0x0000, 0x0000,
0xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,
0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
0x006b, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,
0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0044, 0x0000, 0x0000,
0xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,
0x001f, 0x0020, 0x0054, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,
0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,
0x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0065,
0xffbe, 0x0000, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0xfead,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffc5, 0x0003, 0x0004, 0x0005, 0xfeaf, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,
0x0009, 0xfeb2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0xfeb6, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,
0x0012, 0x0013, 0x0014, 0xfebb, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,
0xfec1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,
0x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0xfec8, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,
0x002b, 0x002c, 0xfed0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,
0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0xfed9,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
0x003e, 0x003f, 0x0040, 0x0041, 0xfee3, 0x0000, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,
0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
0x000b, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,
0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0xfeb8, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,
0x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,
0x0024, 0x0025, 0x0026, 0xfec5, 0x0000, 0x0000, 0x0000, 0x0000,
0xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,
0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
0xfed3, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,
0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0xfeac, 0x0000, 0x0000,
0xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,
0x001f, 0x0020, 0xfebc, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,
0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,
0x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0xfecd,
0x0154, 0x0218, 0x0110, 0x00b0, 0x00cc, 0x00b0, 0x0088, 0x00b0,
0x0044, 0x00b0, 0x0000, 0x00b0, 0x00fe, 0xff07, 0x0002, 0x00ff,
0x00f8, 0x0007, 0x00fe, 0x00ee, 0x07ff, 0x0200, 0x00ef, 0xf800,
0x0700, 0x00ee, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0001,
0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
0xffff, 0x0000, 0x0000, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000,
0x0000, 0xffff, 0xffff, 0x0000, 0xffff, 0x0001, 0x0000, 0x0001,
0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000,
0xffff, 0x0001, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff,
0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0044, 0x0088, 0x00cc,
0x0110, 0x0154, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
};
#ifdef __WIN32__
void (__cdecl *SetDSP3)(void);
#else
void (*SetDSP3)(void);
#endif
uint16 DSP3_DR;
uint16 DSP3_SR;
uint16 DSP3_MemoryIndex;
void DSP3_MemorySize()
{
DSP3_DR = 0x0300;
SetDSP3 = &DSP3_Reset;
}
void DSP3_TestMemory()
{
DSP3_DR = 0x0000;
SetDSP3 = &DSP3_Reset;
}
void DSP3_DumpDataROM()
{
DSP3_DR = DSP3_DataROM[DSP3_MemoryIndex++];
if (DSP3_MemoryIndex == 1024)
SetDSP3 = &DSP3_Reset;
}
void DSP3_MemoryDump()
{
DSP3_MemoryIndex = 0;
SetDSP3 = &DSP3_DumpDataROM;
DSP3_DumpDataROM();
}
int16 DSP3_WinLo;
int16 DSP3_WinHi;
void DSP3_OP06()
{
DSP3_WinLo = (uint8)(DSP3_DR);
DSP3_WinHi = (uint8)(DSP3_DR >> 8);
DSP3_Reset();
}
void DSP3_OP03()
{
int16 Lo = (uint8)(DSP3_DR);
int16 Hi = (uint8)(DSP3_DR >> 8);
int16 Ofs = (DSP3_WinLo * Hi << 1) + (Lo << 1);
DSP3_DR = Ofs >> 1;
SetDSP3 = &DSP3_Reset;
}
int16 DSP3_AddLo;
int16 DSP3_AddHi;
void DSP3_OP07_B()
{
int16 Ofs = (DSP3_WinLo * DSP3_AddHi << 1) + (DSP3_AddLo << 1);
DSP3_DR = Ofs >> 1;
SetDSP3 = &DSP3_Reset;
}
void DSP3_OP07_A()
{
int16 Lo = (uint8)(DSP3_DR);
int16 Hi = (uint8)(DSP3_DR >> 8);
if (Lo & 1) Hi += (DSP3_AddLo & 1);
DSP3_AddLo += Lo;
DSP3_AddHi += Hi;
if (DSP3_AddLo < 0)
DSP3_AddLo += DSP3_WinLo;
else
if (DSP3_AddLo >= DSP3_WinLo)
DSP3_AddLo -= DSP3_WinLo;
if (DSP3_AddHi < 0)
DSP3_AddHi += DSP3_WinHi;
else
if (DSP3_AddHi >= DSP3_WinHi)
DSP3_AddHi -= DSP3_WinHi;
DSP3_DR = DSP3_AddLo | (DSP3_AddHi << 8) | ((DSP3_AddHi >> 8) & 0xff);
SetDSP3 = &DSP3_OP07_B;
}
void DSP3_OP07()
{
uint32 dataOfs = ((DSP3_DR << 1) + 0x03b2) & 0x03ff;
DSP3_AddHi = DSP3_DataROM[dataOfs];
DSP3_AddLo = DSP3_DataROM[dataOfs + 1];
SetDSP3 = &DSP3_OP07_A;
DSP3_SR = 0x0080;
}
uint16 DSP3_Codewords;
uint16 DSP3_Outwords;
uint16 DSP3_Symbol;
uint16 DSP3_BitCount;
uint16 DSP3_Index;
uint16 DSP3_Codes[512];
uint16 DSP3_BitsLeft;
uint16 DSP3_ReqBits;
uint16 DSP3_ReqData;
uint16 DSP3_BitCommand;
uint8 DSP3_BaseLength;
uint16 DSP3_BaseCodes;
uint16 DSP3_BaseCode;
uint8 DSP3_CodeLengths[8];
uint16 DSP3_CodeOffsets[8];
uint16 DSP3_LZCode;
uint8 DSP3_LZLength;
uint16 DSP3_X;
uint16 DSP3_Y;
void DSP3_Coordinate()
{
DSP3_Index++;
switch (DSP3_Index)
{
case 3:
{
if (DSP3_DR == 0xffff)
DSP3_Reset();
break;
}
case 4:
{
DSP3_X = DSP3_DR;
break;
}
case 5:
{
DSP3_Y = DSP3_DR;
DSP3_DR = 1;
break;
}
case 6:
{
DSP3_DR = DSP3_X;
break;
}
case 7:
{
DSP3_DR = DSP3_Y;
DSP3_Index = 0;
break;
}
}
}
uint8 DSP3_Bitmap[8];
uint8 DSP3_Bitplane[8];
uint16 DSP3_BMIndex;
uint16 DSP3_BPIndex;
uint16 DSP3_Count;
void DSP3_Convert_A()
{
if (DSP3_BMIndex < 8)
{
DSP3_Bitmap[DSP3_BMIndex++] = (uint8) (DSP3_DR);
DSP3_Bitmap[DSP3_BMIndex++] = (uint8) (DSP3_DR >> 8);
if (DSP3_BMIndex == 8)
{
for (short i=0; i < 8; i++)
for (short j=0; j < 8; j++)
{
DSP3_Bitplane[j] <<= 1;
DSP3_Bitplane[j] |= (DSP3_Bitmap[i] >> j) & 1;
}
DSP3_BPIndex = 0;
DSP3_Count--;
}
}
if (DSP3_BMIndex == 8)
{
if (DSP3_BPIndex == 8)
{
if (!DSP3_Count) DSP3_Reset();
DSP3_BMIndex = 0;
}
else
{
DSP3_DR = DSP3_Bitplane[DSP3_BPIndex++];
DSP3_DR |= DSP3_Bitplane[DSP3_BPIndex++] << 8;
}
}
}
void DSP3_Convert()
{
DSP3_Count = DSP3_DR;
DSP3_BMIndex = 0;
SetDSP3 = &DSP3_Convert_A;
}
bool DSP3_GetBits(uint8 Count)
{
if (!DSP3_BitsLeft)
{
DSP3_BitsLeft = Count;
DSP3_ReqBits = 0;
}
do {
if (!DSP3_BitCount)
{
DSP3_SR = 0xC0;
return false;
}
DSP3_ReqBits <<= 1;
if (DSP3_ReqData & 0x8000) DSP3_ReqBits++;
DSP3_ReqData <<= 1;
DSP3_BitCount--;
DSP3_BitsLeft--;
} while (DSP3_BitsLeft);
return true;
}
void DSP3_Decode_Data()
{
if (!DSP3_BitCount)
{
if (DSP3_SR & 0x40)
{
DSP3_ReqData = DSP3_DR;
DSP3_BitCount += 16;
}
else
{
DSP3_SR = 0xC0;
return;
}
}
if (DSP3_LZCode == 1)
{
if (!DSP3_GetBits(1))
return;
if (DSP3_ReqBits)
DSP3_LZLength = 12;
else
DSP3_LZLength = 8;
DSP3_LZCode++;
}
if (DSP3_LZCode == 2)
{
if (!DSP3_GetBits(DSP3_LZLength))
return;
DSP3_LZCode = 0;
DSP3_Outwords--;
if (!DSP3_Outwords) SetDSP3 = &DSP3_Reset;
DSP3_SR = 0x80;
DSP3_DR = DSP3_ReqBits;
return;
}
if (DSP3_BaseCode == 0xffff)
{
if (!DSP3_GetBits(DSP3_BaseLength))
return;
DSP3_BaseCode = DSP3_ReqBits;
}
if (!DSP3_GetBits(DSP3_CodeLengths[DSP3_BaseCode]))
return;
DSP3_Symbol = DSP3_Codes[DSP3_CodeOffsets[DSP3_BaseCode] + DSP3_ReqBits];
DSP3_BaseCode = 0xffff;
if (DSP3_Symbol & 0xff00)
{
DSP3_Symbol += 0x7f02;
DSP3_LZCode++;
}
else
{
DSP3_Outwords--;
if (!DSP3_Outwords)
SetDSP3 = &DSP3_Reset;
}
DSP3_SR = 0x80;
DSP3_DR = DSP3_Symbol;
}
void DSP3_Decode_Tree()
{
if (!DSP3_BitCount)
{
DSP3_ReqData = DSP3_DR;
DSP3_BitCount += 16;
}
if (!DSP3_BaseCodes)
{
DSP3_GetBits(1);
if (DSP3_ReqBits)
{
DSP3_BaseLength = 3;
DSP3_BaseCodes = 8;
}
else
{
DSP3_BaseLength = 2;
DSP3_BaseCodes = 4;
}
}
while (DSP3_BaseCodes)
{
if (!DSP3_GetBits(3))
return;
DSP3_ReqBits++;
DSP3_CodeLengths[DSP3_Index] = (uint8) DSP3_ReqBits;
DSP3_CodeOffsets[DSP3_Index] = DSP3_Symbol;
DSP3_Index++;
DSP3_Symbol += 1 << DSP3_ReqBits;
DSP3_BaseCodes--;
}
DSP3_BaseCode = 0xffff;
DSP3_LZCode = 0;
SetDSP3 = &DSP3_Decode_Data;
if (DSP3_BitCount) DSP3_Decode_Data();
}
void DSP3_Decode_Symbols()
{
DSP3_ReqData = DSP3_DR;
DSP3_BitCount += 16;
do {
if (DSP3_BitCommand == 0xffff)
{
if (!DSP3_GetBits(2)) return;
DSP3_BitCommand = DSP3_ReqBits;
}
switch (DSP3_BitCommand)
{
case 0:
{
if (!DSP3_GetBits(9)) return;
DSP3_Symbol = DSP3_ReqBits;
break;
}
case 1:
{
DSP3_Symbol++;
break;
}
case 2:
{
if (!DSP3_GetBits(1)) return;
DSP3_Symbol += 2 + DSP3_ReqBits;
break;
}
case 3:
{
if (!DSP3_GetBits(4)) return;
DSP3_Symbol += 4 + DSP3_ReqBits;
break;
}
}
DSP3_BitCommand = 0xffff;
DSP3_Codes[DSP3_Index++] = DSP3_Symbol;
DSP3_Codewords--;
} while (DSP3_Codewords);
DSP3_Index = 0;
DSP3_Symbol = 0;
DSP3_BaseCodes = 0;
SetDSP3 = &DSP3_Decode_Tree;
if (DSP3_BitCount) DSP3_Decode_Tree();
}
void DSP3_Decode_A()
{
DSP3_Outwords = DSP3_DR;
SetDSP3 = &DSP3_Decode_Symbols;
DSP3_BitCount = 0;
DSP3_BitsLeft = 0;
DSP3_Symbol = 0;
DSP3_Index = 0;
DSP3_BitCommand = 0xffff;
DSP3_SR = 0xC0;
}
void DSP3_Decode()
{
DSP3_Codewords = DSP3_DR;
SetDSP3 = &DSP3_Decode_A;
}
void DSP3_Command()
{
if (DSP3_DR < 0x40)
{
switch (DSP3_DR)
{
case 0x02: SetDSP3 = &DSP3_Coordinate; break;
case 0x03: SetDSP3 = &DSP3_OP03; break;
case 0x06: SetDSP3 = &DSP3_OP06; break;
case 0x07: SetDSP3 = &DSP3_OP07; return;
case 0x0f: SetDSP3 = &DSP3_TestMemory; break;
case 0x18: SetDSP3 = &DSP3_Convert; break;
case 0x1f: SetDSP3 = &DSP3_MemoryDump; break;
case 0x2f: SetDSP3 = &DSP3_MemorySize; break;
case 0x38: SetDSP3 = &DSP3_Decode; break;
}
DSP3_SR = 0x0080;
DSP3_Index = 0;
}
}
void DSP3_Reset()
{
DSP3_DR = 0x0080;
DSP3_SR = 0x0084;
SetDSP3 = &DSP3_Command;
}
void DSP3SetByte(uint8 byte, uint16 address)
{
if ((address & 0xC000) == 0x8000)
{
if (DSP3_SR & 0x04)
{
DSP3_DR = (DSP3_DR & 0xff00) + byte;
(*SetDSP3)();
}
else
{
DSP3_SR ^= 0x10;
if (DSP3_SR & 0x10)
DSP3_DR = (DSP3_DR & 0xff00) + byte;
else
{
DSP3_DR = (DSP3_DR & 0x00ff) + (byte << 8);
(*SetDSP3)();
}
}
}
}
uint8 DSP3GetByte(uint16 address)
{
if ((address & 0xC000) == 0x8000)
{
uint8 byte;
if (DSP3_SR & 0x04)
{
byte = (uint8) DSP3_DR;
(*SetDSP3)();
}
else
{
DSP3_SR ^= 0x10;
if (DSP3_SR & 0x10)
byte = (uint8) (DSP3_DR);
else
{
byte = (uint8) (DSP3_DR >> 8);
(*SetDSP3)();
}
}
return byte;
}
return (uint8) DSP3_SR;
}