~random-stuff/random-stuff/snes9x-OLD

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
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
/***********************************************************************************
  Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.

  (c) Copyright 1996 - 2002  Gary Henderson (gary.henderson@ntlworld.com),
                             Jerremy Koot (jkoot@snes9x.com)

  (c) Copyright 2002 - 2004  Matthew Kendora

  (c) Copyright 2002 - 2005  Peter Bortas (peter@bortas.org)

  (c) Copyright 2004 - 2005  Joel Yliluoma (http://iki.fi/bisqwit/)

  (c) Copyright 2001 - 2006  John Weidman (jweidman@slip.net)

  (c) Copyright 2002 - 2006  funkyass (funkyass@spam.shaw.ca),
                             Kris Bleakley (codeviolation@hotmail.com)

  (c) Copyright 2002 - 2010  Brad Jorsch (anomie@users.sourceforge.net),
                             Nach (n-a-c-h@users.sourceforge.net),

  (c) Copyright 2002 - 2011  zones (kasumitokoduck@yahoo.com)

  (c) Copyright 2006 - 2007  nitsuja

  (c) Copyright 2009 - 2016  BearOso,
                             OV2


  BS-X C emulator code
  (c) Copyright 2005 - 2006  Dreamer Nom,
                             zones

  C4 x86 assembler and some C emulation code
  (c) Copyright 2000 - 2003  _Demo_ (_demo_@zsnes.com),
                             Nach,
                             zsKnight (zsknight@zsnes.com)

  C4 C++ code
  (c) Copyright 2003 - 2006  Brad Jorsch,
                             Nach

  DSP-1 emulator code
  (c) Copyright 1998 - 2006  _Demo_,
                             Andreas Naive (andreasnaive@gmail.com),
                             Gary Henderson,
                             Ivar (ivar@snes9x.com),
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora,
                             Nach,
                             neviksti (neviksti@hotmail.com)

  DSP-2 emulator code
  (c) Copyright 2003         John Weidman,
                             Kris Bleakley,
                             Lord Nightmare (lord_nightmare@users.sourceforge.net),
                             Matthew Kendora,
                             neviksti

  DSP-3 emulator code
  (c) Copyright 2003 - 2006  John Weidman,
                             Kris Bleakley,
                             Lancer,
                             z80 gaiden

  DSP-4 emulator code
  (c) Copyright 2004 - 2006  Dreamer Nom,
                             John Weidman,
                             Kris Bleakley,
                             Nach,
                             z80 gaiden

  OBC1 emulator code
  (c) Copyright 2001 - 2004  zsKnight,
                             pagefault (pagefault@zsnes.com),
                             Kris Bleakley
                             Ported from x86 assembler to C by sanmaiwashi

  SPC7110 and RTC C++ emulator code used in 1.39-1.51
  (c) Copyright 2002         Matthew Kendora with research by
                             zsKnight,
                             John Weidman,
                             Dark Force

  SPC7110 and RTC C++ emulator code used in 1.52+
  (c) Copyright 2009         byuu,
                             neviksti

  S-DD1 C emulator code
  (c) Copyright 2003         Brad Jorsch with research by
                             Andreas Naive,
                             John Weidman

  S-RTC C emulator code
  (c) Copyright 2001 - 2006  byuu,
                             John Weidman

  ST010 C++ emulator code
  (c) Copyright 2003         Feather,
                             John Weidman,
                             Kris Bleakley,
                             Matthew Kendora

  Super FX x86 assembler emulator code
  (c) Copyright 1998 - 2003  _Demo_,
                             pagefault,
                             zsKnight

  Super FX C emulator code
  (c) Copyright 1997 - 1999  Ivar,
                             Gary Henderson,
                             John Weidman

  Sound emulator code used in 1.5-1.51
  (c) Copyright 1998 - 2003  Brad Martin
  (c) Copyright 1998 - 2006  Charles Bilyue'

  Sound emulator code used in 1.52+
  (c) Copyright 2004 - 2007  Shay Green (gblargg@gmail.com)

  S-SMP emulator code used in 1.54+
  (c) Copyright 2016         byuu

  SH assembler code partly based on x86 assembler code
  (c) Copyright 2002 - 2004  Marcus Comstedt (marcus@mc.pp.se)

  2xSaI filter
  (c) Copyright 1999 - 2001  Derek Liauw Kie Fa

  HQ2x, HQ3x, HQ4x filters
  (c) Copyright 2003         Maxim Stepin (maxim@hiend3d.com)

  NTSC filter
  (c) Copyright 2006 - 2007  Shay Green

  GTK+ GUI code
  (c) Copyright 2004 - 2016  BearOso

  Win32 GUI code
  (c) Copyright 2003 - 2006  blip,
                             funkyass,
                             Matthew Kendora,
                             Nach,
                             nitsuja
  (c) Copyright 2009 - 2016  OV2

  Mac OS GUI code
  (c) Copyright 1998 - 2001  John Stiles
  (c) Copyright 2001 - 2011  zones


  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/or 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 or it's derivatives.

  Snes9x is freeware for PERSONAL USE only. Commercial users should
  seek permission of the copyright holders first. Commercial use includes,
  but is not limited to, charging money for Snes9x or software derived from
  Snes9x, including Snes9x or derivatives in commercial game bundles, and/or
  using Snes9x as a promotion for your commercial product.

  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.
 ***********************************************************************************/


#include "snes9x.h"
#include "memmap.h"

static void (*SetDSP3) (void);

static const 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
};

static bool8 DSP3_GetBits (uint8);
//static void DSP3_MemorySize (void);
static void DSP3_TestMemory (void);
static void DSP3_DumpDataROM (void);
static void DSP3_MemoryDump (void);
static void DSP3_Coordinate (void);
static void DSP3_Command (void);
static void DSP3_Decode_Data (void);
static void DSP3_Decode_Tree (void);
static void DSP3_Decode_Symbols (void);
static void DSP3_Decode (void);
static void DSP3_Decode_A (void);
static void DSP3_Convert (void);
static void DSP3_Convert_A (void);
static void DSP3_OP03 (void);
static void DSP3_OP06 (void);
static void DSP3_OP07 (void);
static void DSP3_OP07_A (void);
static void DSP3_OP07_B (void);
static void DSP3_OP0C (void);
//static void DSP3_OP0C_A (void);
static void DSP3_OP10 (void);
static void DSP3_OP1C (void);
static void DSP3_OP1C_A (void);
static void DSP3_OP1C_B (void);
static void DSP3_OP1C_C (void);
static void DSP3_OP1E (void);
static void DSP3_OP1E_A (void);
static void DSP3_OP1E_A1 (void);
static void DSP3_OP1E_A2 (void);
static void DSP3_OP1E_A3 (void);
static void DSP3_OP1E_B (void);
static void DSP3_OP1E_B1 (void);
static void DSP3_OP1E_B2 (void);
static void DSP3_OP1E_C (void);
static void DSP3_OP1E_C1 (void);
static void DSP3_OP1E_C2 (void);
static void DSP3_OP1E_D (int16, int16 *, int16 *);
static void DSP3_OP1E_D1 (int16, int16 *, int16 *);
static void DSP3_OP3E (void);


void DSP3_Reset (void)
{
	DSP3.DR = 0x0080;
	DSP3.SR = 0x0084;
	SetDSP3 = &DSP3_Command;
}

/*
static void DSP3_MemorySize (void)
{
	DSP3.DR = 0x0300;
	SetDSP3 = &DSP3_Reset;
}
*/

static void DSP3_TestMemory (void)
{
	DSP3.DR = 0x0000;
	SetDSP3 = &DSP3_Reset;
}

static void DSP3_DumpDataROM (void)
{
	DSP3.DR = DSP3_DataROM[DSP3.MemoryIndex++];
	if (DSP3.MemoryIndex == 1024)
		SetDSP3 = &DSP3_Reset;
}

static void DSP3_MemoryDump (void)
{
	DSP3.MemoryIndex = 0;
	SetDSP3 = &DSP3_DumpDataROM;
	DSP3_DumpDataROM();
}

static void DSP3_OP06 (void)
{
	DSP3.WinLo = (uint8) (DSP3.DR);
	DSP3.WinHi = (uint8) (DSP3.DR >> 8);
	DSP3_Reset();
}

static void DSP3_OP03 (void)
{
	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;
}

static void DSP3_OP07_B (void)
{
	int16	Ofs = (DSP3.WinLo * DSP3.AddHi << 1) + (DSP3.AddLo << 1);

	DSP3.DR = Ofs >> 1;
	SetDSP3 = &DSP3_Reset;
}

static void DSP3_OP07_A (void)
{
	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;
}

static void DSP3_OP07 (void)
{
	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;
}

static void DSP3_Coordinate (void)
{
	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;
	}
}

static void DSP3_Convert_A (void)
{
	if (DSP3.BMIndex < 8)
	{
		DSP3.Bitmap[DSP3.BMIndex++] = (uint8) (DSP3.DR);
		DSP3.Bitmap[DSP3.BMIndex++] = (uint8) (DSP3.DR >> 8);

		if (DSP3.BMIndex == 8)
		{
			for (int i = 0; i < 8; i++)
			{
				for (int 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;
		}
	}
}

static void DSP3_Convert (void)
{
	DSP3.Count = DSP3.DR;
	DSP3.BMIndex = 0;
	SetDSP3 = &DSP3_Convert_A;
}

static bool8 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);
}

static void DSP3_Decode_Data (void)
{
	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;
}

static void DSP3_Decode_Tree (void)
{
	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();
}

static void DSP3_Decode_Symbols (void)
{
	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();
}

static void DSP3_Decode_A (void)
{
	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;
}

static void DSP3_Decode (void)
{
	DSP3.Codewords = DSP3.DR;
	SetDSP3 = &DSP3_Decode_A;
}

// Opcodes 1E/3E bit-perfect to 'dsp3-intro' log
// src: adapted from SD Gundam X/G-Next

static void DSP3_OP3E (void)
{
	DSP3. op3e_x = (uint8)  (DSP3.DR & 0x00ff);
	DSP3. op3e_y = (uint8) ((DSP3.DR & 0xff00) >> 8);

	DSP3_OP03();

	DSP3.op1e_terrain[DSP3.DR] = 0x00;
	DSP3.op1e_cost[DSP3.DR]    = 0xff;
	DSP3.op1e_weight[DSP3.DR]  = 0;

	DSP3.op1e_max_search_radius = 0;
	DSP3.op1e_max_path_radius   = 0;
}

static void DSP3_OP1E (void)
{
	DSP3.op1e_min_radius = (uint8)  (DSP3.DR & 0x00ff);
	DSP3.op1e_max_radius = (uint8) ((DSP3.DR & 0xff00) >> 8);

	if (DSP3.op1e_min_radius == 0)
		DSP3.op1e_min_radius++;

	if (DSP3.op1e_max_search_radius >= DSP3.op1e_min_radius)
		DSP3.op1e_min_radius = DSP3.op1e_max_search_radius + 1;

	if (DSP3.op1e_max_radius > DSP3.op1e_max_search_radius)
		DSP3.op1e_max_search_radius = DSP3.op1e_max_radius;

	DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;
	DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;

	DSP3.op1e_lcv_turns = 6;
	DSP3.op1e_turn = 0;

	DSP3.op1e_x = DSP3. op3e_x;
	DSP3.op1e_y = DSP3. op3e_y;

	for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)
		DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);

	DSP3_OP1E_A();
}

static void DSP3_OP1E_A (void)
{
	if (DSP3.op1e_lcv_steps == 0)
	{
		DSP3.op1e_lcv_radius++;

		DSP3.op1e_lcv_steps = DSP3.op1e_lcv_radius;

		DSP3.op1e_x = DSP3. op3e_x;
		DSP3.op1e_y = DSP3. op3e_y;

		for (int lcv = 0; lcv < DSP3.op1e_lcv_radius; lcv++)
			DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);
	}

	if (DSP3.op1e_lcv_radius > DSP3.op1e_max_radius)
	{
		DSP3.op1e_turn++;
		DSP3.op1e_lcv_turns--;

		DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;
		DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;

		DSP3.op1e_x = DSP3. op3e_x;
		DSP3.op1e_y = DSP3. op3e_y;

		for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)
			DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);
	}

	if (DSP3.op1e_lcv_turns == 0)
	{
		DSP3.DR = 0xffff;
		DSP3.SR = 0x0080;
		SetDSP3 = &DSP3_OP1E_B;
		return;
	}

	DSP3.DR = (uint8) (DSP3.op1e_x) | ((uint8) (DSP3.op1e_y) << 8);
	DSP3_OP03();

	DSP3.op1e_cell = DSP3.DR;

	DSP3.SR = 0x0080;
	SetDSP3 = &DSP3_OP1E_A1;
}

static void DSP3_OP1E_A1 (void)
{
	DSP3.SR = 0x0084;
	SetDSP3 = &DSP3_OP1E_A2;
}

static void DSP3_OP1E_A2 (void)
{
	DSP3.op1e_terrain[DSP3.op1e_cell] = (uint8) (DSP3.DR & 0x00ff);

	DSP3.SR = 0x0084;
	SetDSP3 = &DSP3_OP1E_A3;
}

static void DSP3_OP1E_A3 (void)
{
	DSP3.op1e_cost[DSP3.op1e_cell] = (uint8) (DSP3.DR & 0x00ff);

	if (DSP3.op1e_lcv_radius == 1)
	{
		if (DSP3.op1e_terrain[DSP3.op1e_cell] & 1)
			DSP3.op1e_weight[DSP3.op1e_cell] = 0xff;
		else
			DSP3.op1e_weight[DSP3.op1e_cell] = DSP3.op1e_cost[DSP3.op1e_cell];
	}
	else
		DSP3.op1e_weight[DSP3.op1e_cell] = 0xff;

	DSP3_OP1E_D((int16) (DSP3.op1e_turn + 2), &DSP3.op1e_x, &DSP3.op1e_y);
	DSP3.op1e_lcv_steps--;

	DSP3.SR = 0x0080;
	DSP3_OP1E_A();
}

static void DSP3_OP1E_B (void)
{
	DSP3.op1e_x = DSP3. op3e_x;
	DSP3.op1e_y = DSP3. op3e_y;
	DSP3.op1e_lcv_radius = 1;

	DSP3.op1e_search = 0;

	DSP3_OP1E_B1();

	SetDSP3 = &DSP3_OP1E_C;
}

static void DSP3_OP1E_B1 (void)
{
	while (DSP3.op1e_lcv_radius < DSP3.op1e_max_radius)
	{
		DSP3.op1e_y--;

		DSP3.op1e_lcv_turns = 6;
		DSP3.op1e_turn = 5;

		while (DSP3.op1e_lcv_turns)
		{
			DSP3.op1e_lcv_steps = DSP3.op1e_lcv_radius;

			while (DSP3.op1e_lcv_steps)
			{
				DSP3_OP1E_D1(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);

				if (0 <= DSP3.op1e_y && DSP3.op1e_y < DSP3.WinHi && 0 <= DSP3.op1e_x && DSP3.op1e_x < DSP3.WinLo)
				{
					DSP3.DR = (uint8) (DSP3.op1e_x) | ((uint8) (DSP3.op1e_y) << 8);
					DSP3_OP03();

					DSP3.op1e_cell = DSP3.DR;
					if (DSP3.op1e_cost[DSP3.op1e_cell] < 0x80 && DSP3.op1e_terrain[DSP3.op1e_cell] < 0x40)
						DSP3_OP1E_B2(); // end cell perimeter
				}

				DSP3.op1e_lcv_steps--;
			} // end search line

			DSP3.op1e_turn--;
			if (DSP3.op1e_turn == 0)
				DSP3.op1e_turn = 6;

			DSP3.op1e_lcv_turns--;
		} // end circle search

		DSP3.op1e_lcv_radius++;
	} // end radius search
}

static void DSP3_OP1E_B2 (void)
{
	int16	cell;
	int16	path;
	int16	x, y;
	int16	lcv_turns;

	path = 0xff;
	lcv_turns = 6;

	while (lcv_turns)
	{
		x = DSP3.op1e_x;
		y = DSP3.op1e_y;

		DSP3_OP1E_D1(lcv_turns, &x, &y);

		DSP3.DR = (uint8) (x) | ((uint8) (y) << 8);
		DSP3_OP03();

		cell = DSP3.DR;

		if (0 <= y && y < DSP3.WinHi && 0 <= x && x < DSP3.WinLo)
		{
			if (DSP3.op1e_terrain[cell] < 0x80 || DSP3.op1e_weight[cell] == 0)
			{
				if (DSP3.op1e_weight[cell] < path)
					path = DSP3.op1e_weight[cell];
			}
		} // end step travel

		lcv_turns--;
	} // end while turns

	if (path != 0xff)
		DSP3.op1e_weight[DSP3.op1e_cell] = path + DSP3.op1e_cost[DSP3.op1e_cell];
}

static void DSP3_OP1E_C (void)
{
	DSP3.op1e_min_radius = (uint8)  (DSP3.DR & 0x00ff);
	DSP3.op1e_max_radius = (uint8) ((DSP3.DR & 0xff00) >> 8);

	if (DSP3.op1e_min_radius == 0)
		DSP3.op1e_min_radius++;

	if (DSP3.op1e_max_path_radius >= DSP3.op1e_min_radius)
		DSP3.op1e_min_radius = DSP3.op1e_max_path_radius + 1;

	if (DSP3.op1e_max_radius > DSP3.op1e_max_path_radius)
		DSP3.op1e_max_path_radius = DSP3.op1e_max_radius;

	DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;
	DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;

	DSP3.op1e_lcv_turns = 6;
	DSP3.op1e_turn = 0;

	DSP3.op1e_x = DSP3. op3e_x;
	DSP3.op1e_y = DSP3. op3e_y;

	for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)
		DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);

	DSP3_OP1E_C1();
}

static void DSP3_OP1E_C1 (void)
{
	if (DSP3.op1e_lcv_steps == 0)
	{
		DSP3.op1e_lcv_radius++;

		DSP3.op1e_lcv_steps = DSP3.op1e_lcv_radius;

		DSP3.op1e_x = DSP3. op3e_x;
		DSP3.op1e_y = DSP3. op3e_y;

		for (int lcv = 0; lcv < DSP3.op1e_lcv_radius; lcv++)
			DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);
	}

	if (DSP3.op1e_lcv_radius > DSP3.op1e_max_radius)
	{
		DSP3.op1e_turn++;
		DSP3.op1e_lcv_turns--;

		DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;
		DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;

		DSP3.op1e_x = DSP3. op3e_x;
		DSP3.op1e_y = DSP3. op3e_y;

		for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)
			DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);
	}

	if (DSP3.op1e_lcv_turns == 0)
	{
		DSP3.DR = 0xffff;
		DSP3.SR = 0x0080;
		SetDSP3 = &DSP3_Reset;
		return;
	}

	DSP3.DR = (uint8) (DSP3.op1e_x) | ((uint8) (DSP3.op1e_y) << 8);
	DSP3_OP03();

	DSP3.op1e_cell = DSP3.DR;

	DSP3.SR = 0x0080;
	SetDSP3 = &DSP3_OP1E_C2;
}

static void DSP3_OP1E_C2 (void)
{
	DSP3.DR = DSP3.op1e_weight[DSP3.op1e_cell];

	DSP3_OP1E_D((int16) (DSP3.op1e_turn + 2), &DSP3.op1e_x, &DSP3.op1e_y);
	DSP3.op1e_lcv_steps--;

	DSP3.SR = 0x0084;
	SetDSP3 = &DSP3_OP1E_C1;
}

static void DSP3_OP1E_D (int16 move, int16 *lo, int16 *hi)
{
	uint32	dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
	int16	Lo;
	int16	Hi;

	DSP3.AddHi = DSP3_DataROM[dataOfs];
	DSP3.AddLo = DSP3_DataROM[dataOfs + 1];

	Lo = (uint8) (*lo);
	Hi = (uint8) (*hi);

	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;

	*lo = DSP3.AddLo;
	*hi = DSP3.AddHi;
}

static void DSP3_OP1E_D1 (int16 move, int16 *lo, int16 *hi)
{
	const uint16	HiAdd[] =
	{
		0x00, 0xFF, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0xFF, 0x00
	};

	const uint16	LoAdd[] =
	{
		0x00, 0x00, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x00
	};

	int16	Lo;
	int16	Hi;

	if ((*lo) & 1)
		DSP3.AddHi = HiAdd[move + 8];
	else
		DSP3.AddHi = HiAdd[move + 0];

	DSP3.AddLo = LoAdd[move];

	Lo = (uint8) (*lo);
	Hi = (uint8) (*hi);

	if (Lo & 1)
		Hi += (DSP3.AddLo & 1);

	DSP3.AddLo += Lo;
	DSP3.AddHi += Hi;

	*lo = DSP3.AddLo;
	*hi = DSP3.AddHi;
}

static void DSP3_OP10 (void)
{
	if (DSP3.DR == 0xffff)
		DSP3_Reset();
	else
		// absorb 2 bytes
		DSP3.DR = DSP3.DR; // FIXME?
}

/*
static void DSP3_OP0C_A (void)
{
	// absorb 2 bytes
	DSP3.DR = 0;
	SetDSP3 = &DSP3_Reset;
}
*/

static void DSP3_OP0C (void)
{
	// absorb 2 bytes
	DSP3.DR = 0;
	//SetDSP3 = &DSP3_OP0C_A;
	SetDSP3 = &DSP3_Reset;
}

static void DSP3_OP1C_C (void)
{
	// return 2 bytes
	DSP3.DR = 0;
	SetDSP3 = &DSP3_Reset;
}

static void DSP3_OP1C_B (void)
{
	// return 2 bytes
	DSP3.DR = 0;
	SetDSP3 = &DSP3_OP1C_C;
}

static void DSP3_OP1C_A (void)
{
	// absorb 2 bytes
	SetDSP3 = &DSP3_OP1C_B;
}

static void DSP3_OP1C (void)
{
	// absorb 2 bytes
	SetDSP3 = &DSP3_OP1C_A;
}

static void DSP3_Command (void)
{
	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 0x0c: SetDSP3 = &DSP3_OP0C;       break;
			case 0x0f: SetDSP3 = &DSP3_TestMemory; break;
			case 0x10: SetDSP3 = &DSP3_OP10;       break;
			case 0x18: SetDSP3 = &DSP3_Convert;    break;
			case 0x1c: SetDSP3 = &DSP3_OP1C;       break;
			case 0x1e: SetDSP3 = &DSP3_OP1E;       break;
			case 0x1f: SetDSP3 = &DSP3_MemoryDump; break;
			case 0x38: SetDSP3 = &DSP3_Decode;     break;
			case 0x3e: SetDSP3 = &DSP3_OP3E;       break;
			default:
				return;
		}

		DSP3.SR = 0x0080;
		DSP3.Index = 0;
	}
}

void DSP3SetByte (uint8 byte, uint16 address)
{
	if (address < DSP0.boundary)
	{
		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 < DSP0.boundary)
	{
		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;
}