Path: blob/main/misc/emulator/xnes/snes9x/dsp3.cpp
28515 views
/***********************************************************************************1Snes9x - Portable Super Nintendo Entertainment System (TM) emulator.23(c) Copyright 1996 - 2002 Gary Henderson ([email protected]),4Jerremy Koot ([email protected])56(c) Copyright 2002 - 2004 Matthew Kendora78(c) Copyright 2002 - 2005 Peter Bortas ([email protected])910(c) Copyright 2004 - 2005 Joel Yliluoma (http://iki.fi/bisqwit/)1112(c) Copyright 2001 - 2006 John Weidman ([email protected])1314(c) Copyright 2002 - 2006 funkyass ([email protected]),15Kris Bleakley ([email protected])1617(c) Copyright 2002 - 2010 Brad Jorsch ([email protected]),18Nach ([email protected]),1920(c) Copyright 2002 - 2011 zones ([email protected])2122(c) Copyright 2006 - 2007 nitsuja2324(c) Copyright 2009 - 2011 BearOso,25OV2262728BS-X C emulator code29(c) Copyright 2005 - 2006 Dreamer Nom,30zones3132C4 x86 assembler and some C emulation code33(c) Copyright 2000 - 2003 _Demo_ ([email protected]),34Nach,35zsKnight ([email protected])3637C4 C++ code38(c) Copyright 2003 - 2006 Brad Jorsch,39Nach4041DSP-1 emulator code42(c) Copyright 1998 - 2006 _Demo_,43Andreas Naive ([email protected]),44Gary Henderson,45Ivar ([email protected]),46John Weidman,47Kris Bleakley,48Matthew Kendora,49Nach,50neviksti ([email protected])5152DSP-2 emulator code53(c) Copyright 2003 John Weidman,54Kris Bleakley,55Lord Nightmare ([email protected]),56Matthew Kendora,57neviksti5859DSP-3 emulator code60(c) Copyright 2003 - 2006 John Weidman,61Kris Bleakley,62Lancer,63z80 gaiden6465DSP-4 emulator code66(c) Copyright 2004 - 2006 Dreamer Nom,67John Weidman,68Kris Bleakley,69Nach,70z80 gaiden7172OBC1 emulator code73(c) Copyright 2001 - 2004 zsKnight,74pagefault ([email protected]),75Kris Bleakley76Ported from x86 assembler to C by sanmaiwashi7778SPC7110 and RTC C++ emulator code used in 1.39-1.5179(c) Copyright 2002 Matthew Kendora with research by80zsKnight,81John Weidman,82Dark Force8384SPC7110 and RTC C++ emulator code used in 1.52+85(c) Copyright 2009 byuu,86neviksti8788S-DD1 C emulator code89(c) Copyright 2003 Brad Jorsch with research by90Andreas Naive,91John Weidman9293S-RTC C emulator code94(c) Copyright 2001 - 2006 byuu,95John Weidman9697ST010 C++ emulator code98(c) Copyright 2003 Feather,99John Weidman,100Kris Bleakley,101Matthew Kendora102103Super FX x86 assembler emulator code104(c) Copyright 1998 - 2003 _Demo_,105pagefault,106zsKnight107108Super FX C emulator code109(c) Copyright 1997 - 1999 Ivar,110Gary Henderson,111John Weidman112113Sound emulator code used in 1.5-1.51114(c) Copyright 1998 - 2003 Brad Martin115(c) Copyright 1998 - 2006 Charles Bilyue'116117Sound emulator code used in 1.52+118(c) Copyright 2004 - 2007 Shay Green ([email protected])119120SH assembler code partly based on x86 assembler code121(c) Copyright 2002 - 2004 Marcus Comstedt ([email protected])1221232xSaI filter124(c) Copyright 1999 - 2001 Derek Liauw Kie Fa125126HQ2x, HQ3x, HQ4x filters127(c) Copyright 2003 Maxim Stepin ([email protected])128129NTSC filter130(c) Copyright 2006 - 2007 Shay Green131132GTK+ GUI code133(c) Copyright 2004 - 2011 BearOso134135Win32 GUI code136(c) Copyright 2003 - 2006 blip,137funkyass,138Matthew Kendora,139Nach,140nitsuja141(c) Copyright 2009 - 2011 OV2142143Mac OS GUI code144(c) Copyright 1998 - 2001 John Stiles145(c) Copyright 2001 - 2011 zones146147148Specific ports contains the works of other authors. See headers in149individual files.150151152Snes9x homepage: http://www.snes9x.com/153154Permission to use, copy, modify and/or distribute Snes9x in both binary155and source form, for non-commercial purposes, is hereby granted without156fee, providing that this license information and copyright notice appear157with all copies and any derived work.158159This software is provided 'as-is', without any express or implied160warranty. In no event shall the authors be held liable for any damages161arising from the use of this software or it's derivatives.162163Snes9x is freeware for PERSONAL USE only. Commercial users should164seek permission of the copyright holders first. Commercial use includes,165but is not limited to, charging money for Snes9x or software derived from166Snes9x, including Snes9x or derivatives in commercial game bundles, and/or167using Snes9x as a promotion for your commercial product.168169The copyright holders request that bug fixes and improvements to the code170should be forwarded to them so everyone can benefit from the modifications171in future versions.172173Super NES and Super Nintendo Entertainment System are trademarks of174Nintendo Co., Limited and its subsidiary companies.175***********************************************************************************/176177178#include "snes9x.h"179#include "memmap.h"180181static void (*SetDSP3) (void);182183static const uint16 DSP3_DataROM[1024] =184{1850x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100,1860x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001,1870x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,1880x0000, 0x000f, 0x0400, 0x0200, 0x0140, 0x0400, 0x0200, 0x0040,1890x007d, 0x007e, 0x007e, 0x007b, 0x007c, 0x007d, 0x007b, 0x007c,1900x0002, 0x0020, 0x0030, 0x0000, 0x000d, 0x0019, 0x0026, 0x0032,1910x003e, 0x004a, 0x0056, 0x0062, 0x006d, 0x0079, 0x0084, 0x008e,1920x0098, 0x00a2, 0x00ac, 0x00b5, 0x00be, 0x00c6, 0x00ce, 0x00d5,1930x00dc, 0x00e2, 0x00e7, 0x00ec, 0x00f1, 0x00f5, 0x00f8, 0x00fb,1940x00fd, 0x00ff, 0x0100, 0x0100, 0x0100, 0x00ff, 0x00fd, 0x00fb,1950x00f8, 0x00f5, 0x00f1, 0x00ed, 0x00e7, 0x00e2, 0x00dc, 0x00d5,1960x00ce, 0x00c6, 0x00be, 0x00b5, 0x00ac, 0x00a2, 0x0099, 0x008e,1970x0084, 0x0079, 0x006e, 0x0062, 0x0056, 0x004a, 0x003e, 0x0032,1980x0026, 0x0019, 0x000d, 0x0000, 0xfff3, 0xffe7, 0xffdb, 0xffce,1990xffc2, 0xffb6, 0xffaa, 0xff9e, 0xff93, 0xff87, 0xff7d, 0xff72,2000xff68, 0xff5e, 0xff54, 0xff4b, 0xff42, 0xff3a, 0xff32, 0xff2b,2010xff25, 0xff1e, 0xff19, 0xff14, 0xff0f, 0xff0b, 0xff08, 0xff05,2020xff03, 0xff01, 0xff00, 0xff00, 0xff00, 0xff01, 0xff03, 0xff05,2030xff08, 0xff0b, 0xff0f, 0xff13, 0xff18, 0xff1e, 0xff24, 0xff2b,2040xff32, 0xff3a, 0xff42, 0xff4b, 0xff54, 0xff5d, 0xff67, 0xff72,2050xff7c, 0xff87, 0xff92, 0xff9e, 0xffa9, 0xffb5, 0xffc2, 0xffce,2060xffda, 0xffe7, 0xfff3, 0x002b, 0x007f, 0x0020, 0x00ff, 0xff00,2070xffbe, 0x0000, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2080x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2090x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0x0045,2100x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2110x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2120xffc5, 0x0003, 0x0004, 0x0005, 0x0047, 0x0000, 0x0000, 0x0000,2130x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2140x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,2150x0009, 0x004a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2160x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2170xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x004e, 0x0000,2180x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2190x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,2200x0012, 0x0013, 0x0014, 0x0053, 0x0000, 0x0000, 0x0000, 0x0000,2210x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2220xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,2230x0059, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2240x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,2250x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0x0060, 0x0000, 0x0000,2260x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2270xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,2280x002b, 0x002c, 0x0068, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2290x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,2300x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0071,2310x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2320xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,2330x003e, 0x003f, 0x0040, 0x0041, 0x007b, 0x0000, 0x0000, 0x0000,2340x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,2350x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,2360x000b, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2370xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,2380x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0050, 0x0000,2390x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,2400x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,2410x0024, 0x0025, 0x0026, 0x005d, 0x0000, 0x0000, 0x0000, 0x0000,2420xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,2430x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,2440x006b, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,2450x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,2460x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0044, 0x0000, 0x0000,2470xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,2480x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,2490x001f, 0x0020, 0x0054, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,2500x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,2510x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0065,2520xffbe, 0x0000, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2530x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2540x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0xfead,2550x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2560x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2570xffc5, 0x0003, 0x0004, 0x0005, 0xfeaf, 0x0000, 0x0000, 0x0000,2580x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2590x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,2600x0009, 0xfeb2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2610x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2620xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0xfeb6, 0x0000,2630x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2640x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,2650x0012, 0x0013, 0x0014, 0xfebb, 0x0000, 0x0000, 0x0000, 0x0000,2660x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2670xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,2680xfec1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2690x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,2700x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0xfec8, 0x0000, 0x0000,2710x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2720xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,2730x002b, 0x002c, 0xfed0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2740x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,2750x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0xfed9,2760x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2770xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,2780x003e, 0x003f, 0x0040, 0x0041, 0xfee3, 0x0000, 0x0000, 0x0000,2790x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,2800x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,2810x000b, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,2820xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,2830x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0xfeb8, 0x0000,2840x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,2850x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,2860x0024, 0x0025, 0x0026, 0xfec5, 0x0000, 0x0000, 0x0000, 0x0000,2870xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,2880x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,2890xfed3, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,2900x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,2910x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0xfeac, 0x0000, 0x0000,2920xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,2930x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,2940x001f, 0x0020, 0xfebc, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,2950x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,2960x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0xfecd,2970x0154, 0x0218, 0x0110, 0x00b0, 0x00cc, 0x00b0, 0x0088, 0x00b0,2980x0044, 0x00b0, 0x0000, 0x00b0, 0x00fe, 0xff07, 0x0002, 0x00ff,2990x00f8, 0x0007, 0x00fe, 0x00ee, 0x07ff, 0x0200, 0x00ef, 0xf800,3000x0700, 0x00ee, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0001,3010x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,3020xffff, 0x0000, 0x0000, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000,3030x0000, 0xffff, 0xffff, 0x0000, 0xffff, 0x0001, 0x0000, 0x0001,3040x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000,3050xffff, 0x0001, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff,3060xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0044, 0x0088, 0x00cc,3070x0110, 0x0154, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,3080xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,3090xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,3100xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,3110xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,3120xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff313};314315static bool8 DSP3_GetBits (uint8);316//static void DSP3_MemorySize (void);317static void DSP3_TestMemory (void);318static void DSP3_DumpDataROM (void);319static void DSP3_MemoryDump (void);320static void DSP3_Coordinate (void);321static void DSP3_Command (void);322static void DSP3_Decode_Data (void);323static void DSP3_Decode_Tree (void);324static void DSP3_Decode_Symbols (void);325static void DSP3_Decode (void);326static void DSP3_Decode_A (void);327static void DSP3_Convert (void);328static void DSP3_Convert_A (void);329static void DSP3_OP03 (void);330static void DSP3_OP06 (void);331static void DSP3_OP07 (void);332static void DSP3_OP07_A (void);333static void DSP3_OP07_B (void);334static void DSP3_OP0C (void);335//static void DSP3_OP0C_A (void);336static void DSP3_OP10 (void);337static void DSP3_OP1C (void);338static void DSP3_OP1C_A (void);339static void DSP3_OP1C_B (void);340static void DSP3_OP1C_C (void);341static void DSP3_OP1E (void);342static void DSP3_OP1E_A (void);343static void DSP3_OP1E_A1 (void);344static void DSP3_OP1E_A2 (void);345static void DSP3_OP1E_A3 (void);346static void DSP3_OP1E_B (void);347static void DSP3_OP1E_B1 (void);348static void DSP3_OP1E_B2 (void);349static void DSP3_OP1E_C (void);350static void DSP3_OP1E_C1 (void);351static void DSP3_OP1E_C2 (void);352static void DSP3_OP1E_D (int16, int16 *, int16 *);353static void DSP3_OP1E_D1 (int16, int16 *, int16 *);354static void DSP3_OP3E (void);355356357void DSP3_Reset (void)358{359DSP3.DR = 0x0080;360DSP3.SR = 0x0084;361SetDSP3 = &DSP3_Command;362}363364/*365static void DSP3_MemorySize (void)366{367DSP3.DR = 0x0300;368SetDSP3 = &DSP3_Reset;369}370*/371372static void DSP3_TestMemory (void)373{374DSP3.DR = 0x0000;375SetDSP3 = &DSP3_Reset;376}377378static void DSP3_DumpDataROM (void)379{380DSP3.DR = DSP3_DataROM[DSP3.MemoryIndex++];381if (DSP3.MemoryIndex == 1024)382SetDSP3 = &DSP3_Reset;383}384385static void DSP3_MemoryDump (void)386{387DSP3.MemoryIndex = 0;388SetDSP3 = &DSP3_DumpDataROM;389DSP3_DumpDataROM();390}391392static void DSP3_OP06 (void)393{394DSP3.WinLo = (uint8) (DSP3.DR);395DSP3.WinHi = (uint8) (DSP3.DR >> 8);396DSP3_Reset();397}398399static void DSP3_OP03 (void)400{401int16 Lo = (uint8) (DSP3.DR);402int16 Hi = (uint8) (DSP3.DR >> 8);403int16 Ofs = (DSP3.WinLo * Hi << 1) + (Lo << 1);404405DSP3.DR = Ofs >> 1;406SetDSP3 = &DSP3_Reset;407}408409static void DSP3_OP07_B (void)410{411int16 Ofs = (DSP3.WinLo * DSP3.AddHi << 1) + (DSP3.AddLo << 1);412413DSP3.DR = Ofs >> 1;414SetDSP3 = &DSP3_Reset;415}416417static void DSP3_OP07_A (void)418{419int16 Lo = (uint8) (DSP3.DR);420int16 Hi = (uint8) (DSP3.DR >> 8);421422if (Lo & 1)423Hi += (DSP3.AddLo & 1);424425DSP3.AddLo += Lo;426DSP3.AddHi += Hi;427428if (DSP3.AddLo < 0)429DSP3.AddLo += DSP3.WinLo;430else431if (DSP3.AddLo >= DSP3.WinLo)432DSP3.AddLo -= DSP3.WinLo;433434if (DSP3.AddHi < 0)435DSP3.AddHi += DSP3.WinHi;436else437if (DSP3.AddHi >= DSP3.WinHi)438DSP3.AddHi -= DSP3.WinHi;439440DSP3.DR = DSP3.AddLo | (DSP3.AddHi << 8) | ((DSP3.AddHi >> 8) & 0xff);441SetDSP3 = &DSP3_OP07_B;442}443444static void DSP3_OP07 (void)445{446uint32 dataOfs = ((DSP3.DR << 1) + 0x03b2) & 0x03ff;447448DSP3.AddHi = DSP3_DataROM[dataOfs];449DSP3.AddLo = DSP3_DataROM[dataOfs + 1];450451SetDSP3 = &DSP3_OP07_A;452DSP3.SR = 0x0080;453}454455static void DSP3_Coordinate (void)456{457DSP3.Index++;458459switch (DSP3.Index)460{461case 3:462if (DSP3.DR == 0xffff)463DSP3_Reset();464break;465466case 4:467DSP3.X = DSP3.DR;468break;469470case 5:471DSP3.Y = DSP3.DR;472DSP3.DR = 1;473break;474475case 6:476DSP3.DR = DSP3.X;477break;478479case 7:480DSP3.DR = DSP3.Y;481DSP3.Index = 0;482break;483}484}485486static void DSP3_Convert_A (void)487{488if (DSP3.BMIndex < 8)489{490DSP3.Bitmap[DSP3.BMIndex++] = (uint8) (DSP3.DR);491DSP3.Bitmap[DSP3.BMIndex++] = (uint8) (DSP3.DR >> 8);492493if (DSP3.BMIndex == 8)494{495for (int i = 0; i < 8; i++)496{497for (int j = 0; j < 8; j++)498{499DSP3.Bitplane[j] <<= 1;500DSP3.Bitplane[j] |= (DSP3.Bitmap[i] >> j) & 1;501}502}503504DSP3.BPIndex = 0;505DSP3.Count--;506}507}508509if (DSP3.BMIndex == 8)510{511if (DSP3.BPIndex == 8)512{513if (!DSP3.Count)514DSP3_Reset();515516DSP3.BMIndex = 0;517}518else519{520DSP3.DR = DSP3.Bitplane[DSP3.BPIndex++];521DSP3.DR |= DSP3.Bitplane[DSP3.BPIndex++] << 8;522}523}524}525526static void DSP3_Convert (void)527{528DSP3.Count = DSP3.DR;529DSP3.BMIndex = 0;530SetDSP3 = &DSP3_Convert_A;531}532533static bool8 DSP3_GetBits (uint8 Count)534{535if (!DSP3.BitsLeft)536{537DSP3.BitsLeft = Count;538DSP3.ReqBits = 0;539}540541do542{543if (!DSP3.BitCount)544{545DSP3.SR = 0xC0;546return (FALSE);547}548549DSP3.ReqBits <<= 1;550if (DSP3.ReqData & 0x8000)551DSP3.ReqBits++;552DSP3.ReqData <<= 1;553554DSP3.BitCount--;555DSP3.BitsLeft--;556557}558while (DSP3.BitsLeft);559560return (TRUE);561}562563static void DSP3_Decode_Data (void)564{565if (!DSP3.BitCount)566{567if (DSP3.SR & 0x40)568{569DSP3.ReqData = DSP3.DR;570DSP3.BitCount += 16;571}572else573{574DSP3.SR = 0xC0;575return;576}577}578579if (DSP3.LZCode == 1)580{581if (!DSP3_GetBits(1))582return;583584if (DSP3.ReqBits)585DSP3.LZLength = 12;586else587DSP3.LZLength = 8;588589DSP3.LZCode++;590}591592if (DSP3.LZCode == 2)593{594if (!DSP3_GetBits(DSP3.LZLength))595return;596597DSP3.LZCode = 0;598DSP3.Outwords--;599if (!DSP3.Outwords)600SetDSP3 = &DSP3_Reset;601602DSP3.SR = 0x80;603DSP3.DR = DSP3.ReqBits;604return;605}606607if (DSP3.BaseCode == 0xffff)608{609if (!DSP3_GetBits(DSP3.BaseLength))610return;611612DSP3.BaseCode = DSP3.ReqBits;613}614615if (!DSP3_GetBits(DSP3.CodeLengths[DSP3.BaseCode]))616return;617618DSP3.Symbol = DSP3.Codes[DSP3.CodeOffsets[DSP3.BaseCode] + DSP3.ReqBits];619DSP3.BaseCode = 0xffff;620621if (DSP3.Symbol & 0xff00)622{623DSP3.Symbol += 0x7f02;624DSP3.LZCode++;625}626else627{628DSP3.Outwords--;629if (!DSP3.Outwords)630SetDSP3 = &DSP3_Reset;631}632633DSP3.SR = 0x80;634DSP3.DR = DSP3.Symbol;635}636637static void DSP3_Decode_Tree (void)638{639if (!DSP3.BitCount)640{641DSP3.ReqData = DSP3.DR;642DSP3.BitCount += 16;643}644645if (!DSP3.BaseCodes)646{647DSP3_GetBits(1);648649if (DSP3.ReqBits)650{651DSP3.BaseLength = 3;652DSP3.BaseCodes = 8;653}654else655{656DSP3.BaseLength = 2;657DSP3.BaseCodes = 4;658}659}660661while (DSP3.BaseCodes)662{663if (!DSP3_GetBits(3))664return;665666DSP3.ReqBits++;667668DSP3.CodeLengths[DSP3.Index] = (uint8) DSP3.ReqBits;669DSP3.CodeOffsets[DSP3.Index] = DSP3.Symbol;670DSP3.Index++;671672DSP3.Symbol += 1 << DSP3.ReqBits;673DSP3.BaseCodes--;674}675676DSP3.BaseCode = 0xffff;677DSP3.LZCode = 0;678679SetDSP3 = &DSP3_Decode_Data;680if (DSP3.BitCount)681DSP3_Decode_Data();682}683684static void DSP3_Decode_Symbols (void)685{686DSP3.ReqData = DSP3.DR;687DSP3.BitCount += 16;688689do690{691if (DSP3.BitCommand == 0xffff)692{693if (!DSP3_GetBits(2))694return;695696DSP3.BitCommand = DSP3.ReqBits;697}698699switch (DSP3.BitCommand)700{701case 0:702if (!DSP3_GetBits(9))703return;704DSP3.Symbol = DSP3.ReqBits;705break;706707case 1:708DSP3.Symbol++;709break;710711case 2:712if (!DSP3_GetBits(1))713return;714DSP3.Symbol += 2 + DSP3.ReqBits;715break;716717case 3:718if (!DSP3_GetBits(4))719return;720DSP3.Symbol += 4 + DSP3.ReqBits;721break;722}723724DSP3.BitCommand = 0xffff;725726DSP3.Codes[DSP3.Index++] = DSP3.Symbol;727DSP3.Codewords--;728729}730while (DSP3.Codewords);731732DSP3.Index = 0;733DSP3.Symbol = 0;734DSP3.BaseCodes = 0;735736SetDSP3 = &DSP3_Decode_Tree;737if (DSP3.BitCount)738DSP3_Decode_Tree();739}740741static void DSP3_Decode_A (void)742{743DSP3.Outwords = DSP3.DR;744SetDSP3 = &DSP3_Decode_Symbols;745DSP3.BitCount = 0;746DSP3.BitsLeft = 0;747DSP3.Symbol = 0;748DSP3.Index = 0;749DSP3.BitCommand = 0xffff;750DSP3.SR = 0xC0;751}752753static void DSP3_Decode (void)754{755DSP3.Codewords = DSP3.DR;756SetDSP3 = &DSP3_Decode_A;757}758759// Opcodes 1E/3E bit-perfect to 'dsp3-intro' log760// src: adapted from SD Gundam X/G-Next761762static void DSP3_OP3E (void)763{764DSP3. op3e_x = (uint8) (DSP3.DR & 0x00ff);765DSP3. op3e_y = (uint8) ((DSP3.DR & 0xff00) >> 8);766767DSP3_OP03();768769DSP3.op1e_terrain[DSP3.DR] = 0x00;770DSP3.op1e_cost[DSP3.DR] = 0xff;771DSP3.op1e_weight[DSP3.DR] = 0;772773DSP3.op1e_max_search_radius = 0;774DSP3.op1e_max_path_radius = 0;775}776777static void DSP3_OP1E (void)778{779DSP3.op1e_min_radius = (uint8) (DSP3.DR & 0x00ff);780DSP3.op1e_max_radius = (uint8) ((DSP3.DR & 0xff00) >> 8);781782if (DSP3.op1e_min_radius == 0)783DSP3.op1e_min_radius++;784785if (DSP3.op1e_max_search_radius >= DSP3.op1e_min_radius)786DSP3.op1e_min_radius = DSP3.op1e_max_search_radius + 1;787788if (DSP3.op1e_max_radius > DSP3.op1e_max_search_radius)789DSP3.op1e_max_search_radius = DSP3.op1e_max_radius;790791DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;792DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;793794DSP3.op1e_lcv_turns = 6;795DSP3.op1e_turn = 0;796797DSP3.op1e_x = DSP3. op3e_x;798DSP3.op1e_y = DSP3. op3e_y;799800for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)801DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);802803DSP3_OP1E_A();804}805806static void DSP3_OP1E_A (void)807{808if (DSP3.op1e_lcv_steps == 0)809{810DSP3.op1e_lcv_radius++;811812DSP3.op1e_lcv_steps = DSP3.op1e_lcv_radius;813814DSP3.op1e_x = DSP3. op3e_x;815DSP3.op1e_y = DSP3. op3e_y;816817for (int lcv = 0; lcv < DSP3.op1e_lcv_radius; lcv++)818DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);819}820821if (DSP3.op1e_lcv_radius > DSP3.op1e_max_radius)822{823DSP3.op1e_turn++;824DSP3.op1e_lcv_turns--;825826DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;827DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;828829DSP3.op1e_x = DSP3. op3e_x;830DSP3.op1e_y = DSP3. op3e_y;831832for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)833DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);834}835836if (DSP3.op1e_lcv_turns == 0)837{838DSP3.DR = 0xffff;839DSP3.SR = 0x0080;840SetDSP3 = &DSP3_OP1E_B;841return;842}843844DSP3.DR = (uint8) (DSP3.op1e_x) | ((uint8) (DSP3.op1e_y) << 8);845DSP3_OP03();846847DSP3.op1e_cell = DSP3.DR;848849DSP3.SR = 0x0080;850SetDSP3 = &DSP3_OP1E_A1;851}852853static void DSP3_OP1E_A1 (void)854{855DSP3.SR = 0x0084;856SetDSP3 = &DSP3_OP1E_A2;857}858859static void DSP3_OP1E_A2 (void)860{861DSP3.op1e_terrain[DSP3.op1e_cell] = (uint8) (DSP3.DR & 0x00ff);862863DSP3.SR = 0x0084;864SetDSP3 = &DSP3_OP1E_A3;865}866867static void DSP3_OP1E_A3 (void)868{869DSP3.op1e_cost[DSP3.op1e_cell] = (uint8) (DSP3.DR & 0x00ff);870871if (DSP3.op1e_lcv_radius == 1)872{873if (DSP3.op1e_terrain[DSP3.op1e_cell] & 1)874DSP3.op1e_weight[DSP3.op1e_cell] = 0xff;875else876DSP3.op1e_weight[DSP3.op1e_cell] = DSP3.op1e_cost[DSP3.op1e_cell];877}878else879DSP3.op1e_weight[DSP3.op1e_cell] = 0xff;880881DSP3_OP1E_D((int16) (DSP3.op1e_turn + 2), &DSP3.op1e_x, &DSP3.op1e_y);882DSP3.op1e_lcv_steps--;883884DSP3.SR = 0x0080;885DSP3_OP1E_A();886}887888static void DSP3_OP1E_B (void)889{890DSP3.op1e_x = DSP3. op3e_x;891DSP3.op1e_y = DSP3. op3e_y;892DSP3.op1e_lcv_radius = 1;893894DSP3.op1e_search = 0;895896DSP3_OP1E_B1();897898SetDSP3 = &DSP3_OP1E_C;899}900901static void DSP3_OP1E_B1 (void)902{903while (DSP3.op1e_lcv_radius < DSP3.op1e_max_radius)904{905DSP3.op1e_y--;906907DSP3.op1e_lcv_turns = 6;908DSP3.op1e_turn = 5;909910while (DSP3.op1e_lcv_turns)911{912DSP3.op1e_lcv_steps = DSP3.op1e_lcv_radius;913914while (DSP3.op1e_lcv_steps)915{916DSP3_OP1E_D1(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);917918if (0 <= DSP3.op1e_y && DSP3.op1e_y < DSP3.WinHi && 0 <= DSP3.op1e_x && DSP3.op1e_x < DSP3.WinLo)919{920DSP3.DR = (uint8) (DSP3.op1e_x) | ((uint8) (DSP3.op1e_y) << 8);921DSP3_OP03();922923DSP3.op1e_cell = DSP3.DR;924if (DSP3.op1e_cost[DSP3.op1e_cell] < 0x80 && DSP3.op1e_terrain[DSP3.op1e_cell] < 0x40)925DSP3_OP1E_B2(); // end cell perimeter926}927928DSP3.op1e_lcv_steps--;929} // end search line930931DSP3.op1e_turn--;932if (DSP3.op1e_turn == 0)933DSP3.op1e_turn = 6;934935DSP3.op1e_lcv_turns--;936} // end circle search937938DSP3.op1e_lcv_radius++;939} // end radius search940}941942static void DSP3_OP1E_B2 (void)943{944int16 cell;945int16 path;946int16 x, y;947int16 lcv_turns;948949path = 0xff;950lcv_turns = 6;951952while (lcv_turns)953{954x = DSP3.op1e_x;955y = DSP3.op1e_y;956957DSP3_OP1E_D1(lcv_turns, &x, &y);958959DSP3.DR = (uint8) (x) | ((uint8) (y) << 8);960DSP3_OP03();961962cell = DSP3.DR;963964if (0 <= y && y < DSP3.WinHi && 0 <= x && x < DSP3.WinLo)965{966if (DSP3.op1e_terrain[cell] < 0x80 || DSP3.op1e_weight[cell] == 0)967{968if (DSP3.op1e_weight[cell] < path)969path = DSP3.op1e_weight[cell];970}971} // end step travel972973lcv_turns--;974} // end while turns975976if (path != 0xff)977DSP3.op1e_weight[DSP3.op1e_cell] = path + DSP3.op1e_cost[DSP3.op1e_cell];978}979980static void DSP3_OP1E_C (void)981{982DSP3.op1e_min_radius = (uint8) (DSP3.DR & 0x00ff);983DSP3.op1e_max_radius = (uint8) ((DSP3.DR & 0xff00) >> 8);984985if (DSP3.op1e_min_radius == 0)986DSP3.op1e_min_radius++;987988if (DSP3.op1e_max_path_radius >= DSP3.op1e_min_radius)989DSP3.op1e_min_radius = DSP3.op1e_max_path_radius + 1;990991if (DSP3.op1e_max_radius > DSP3.op1e_max_path_radius)992DSP3.op1e_max_path_radius = DSP3.op1e_max_radius;993994DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;995DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;996997DSP3.op1e_lcv_turns = 6;998DSP3.op1e_turn = 0;9991000DSP3.op1e_x = DSP3. op3e_x;1001DSP3.op1e_y = DSP3. op3e_y;10021003for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)1004DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);10051006DSP3_OP1E_C1();1007}10081009static void DSP3_OP1E_C1 (void)1010{1011if (DSP3.op1e_lcv_steps == 0)1012{1013DSP3.op1e_lcv_radius++;10141015DSP3.op1e_lcv_steps = DSP3.op1e_lcv_radius;10161017DSP3.op1e_x = DSP3. op3e_x;1018DSP3.op1e_y = DSP3. op3e_y;10191020for (int lcv = 0; lcv < DSP3.op1e_lcv_radius; lcv++)1021DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);1022}10231024if (DSP3.op1e_lcv_radius > DSP3.op1e_max_radius)1025{1026DSP3.op1e_turn++;1027DSP3.op1e_lcv_turns--;10281029DSP3.op1e_lcv_radius = DSP3.op1e_min_radius;1030DSP3.op1e_lcv_steps = DSP3.op1e_min_radius;10311032DSP3.op1e_x = DSP3. op3e_x;1033DSP3.op1e_y = DSP3. op3e_y;10341035for (int lcv = 0; lcv < DSP3.op1e_min_radius; lcv++)1036DSP3_OP1E_D(DSP3.op1e_turn, &DSP3.op1e_x, &DSP3.op1e_y);1037}10381039if (DSP3.op1e_lcv_turns == 0)1040{1041DSP3.DR = 0xffff;1042DSP3.SR = 0x0080;1043SetDSP3 = &DSP3_Reset;1044return;1045}10461047DSP3.DR = (uint8) (DSP3.op1e_x) | ((uint8) (DSP3.op1e_y) << 8);1048DSP3_OP03();10491050DSP3.op1e_cell = DSP3.DR;10511052DSP3.SR = 0x0080;1053SetDSP3 = &DSP3_OP1E_C2;1054}10551056static void DSP3_OP1E_C2 (void)1057{1058DSP3.DR = DSP3.op1e_weight[DSP3.op1e_cell];10591060DSP3_OP1E_D((int16) (DSP3.op1e_turn + 2), &DSP3.op1e_x, &DSP3.op1e_y);1061DSP3.op1e_lcv_steps--;10621063DSP3.SR = 0x0084;1064SetDSP3 = &DSP3_OP1E_C1;1065}10661067static void DSP3_OP1E_D (int16 move, int16 *lo, int16 *hi)1068{1069uint32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;1070int16 Lo;1071int16 Hi;10721073DSP3.AddHi = DSP3_DataROM[dataOfs];1074DSP3.AddLo = DSP3_DataROM[dataOfs + 1];10751076Lo = (uint8) (*lo);1077Hi = (uint8) (*hi);10781079if (Lo & 1)1080Hi += (DSP3.AddLo & 1);10811082DSP3.AddLo += Lo;1083DSP3.AddHi += Hi;10841085if (DSP3.AddLo < 0)1086DSP3.AddLo += DSP3.WinLo;1087else1088if (DSP3.AddLo >= DSP3.WinLo)1089DSP3.AddLo -= DSP3.WinLo;10901091if (DSP3.AddHi < 0)1092DSP3.AddHi += DSP3.WinHi;1093else1094if (DSP3.AddHi >= DSP3.WinHi)1095DSP3.AddHi -= DSP3.WinHi;10961097*lo = DSP3.AddLo;1098*hi = DSP3.AddHi;1099}11001101static void DSP3_OP1E_D1 (int16 move, int16 *lo, int16 *hi)1102{1103const uint16 HiAdd[] =1104{11050x00, 0xFF, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0xFF, 0x001106};11071108const uint16 LoAdd[] =1109{11100x00, 0x00, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x001111};11121113int16 Lo;1114int16 Hi;11151116if ((*lo) & 1)1117DSP3.AddHi = HiAdd[move + 8];1118else1119DSP3.AddHi = HiAdd[move + 0];11201121DSP3.AddLo = LoAdd[move];11221123Lo = (uint8) (*lo);1124Hi = (uint8) (*hi);11251126if (Lo & 1)1127Hi += (DSP3.AddLo & 1);11281129DSP3.AddLo += Lo;1130DSP3.AddHi += Hi;11311132*lo = DSP3.AddLo;1133*hi = DSP3.AddHi;1134}11351136static void DSP3_OP10 (void)1137{1138if (DSP3.DR == 0xffff)1139DSP3_Reset();1140else1141// absorb 2 bytes1142DSP3.DR = DSP3.DR;1143}11441145/*1146static void DSP3_OP0C_A (void)1147{1148// absorb 2 bytes1149DSP3.DR = 0;1150SetDSP3 = &DSP3_Reset;1151}1152*/11531154static void DSP3_OP0C (void)1155{1156// absorb 2 bytes1157DSP3.DR = 0;1158//SetDSP3 = &DSP3_OP0C_A;1159SetDSP3 = &DSP3_Reset;1160}11611162static void DSP3_OP1C_C (void)1163{1164// return 2 bytes1165DSP3.DR = 0;1166SetDSP3 = &DSP3_Reset;1167}11681169static void DSP3_OP1C_B (void)1170{1171// return 2 bytes1172DSP3.DR = 0;1173SetDSP3 = &DSP3_OP1C_C;1174}11751176static void DSP3_OP1C_A (void)1177{1178// absorb 2 bytes1179SetDSP3 = &DSP3_OP1C_B;1180}11811182static void DSP3_OP1C (void)1183{1184// absorb 2 bytes1185SetDSP3 = &DSP3_OP1C_A;1186}11871188static void DSP3_Command (void)1189{1190if (DSP3.DR < 0x40)1191{1192switch (DSP3.DR)1193{1194case 0x02: SetDSP3 = &DSP3_Coordinate; break;1195case 0x03: SetDSP3 = &DSP3_OP03; break;1196case 0x06: SetDSP3 = &DSP3_OP06; break;1197case 0x07: SetDSP3 = &DSP3_OP07; return;1198case 0x0c: SetDSP3 = &DSP3_OP0C; break;1199case 0x0f: SetDSP3 = &DSP3_TestMemory; break;1200case 0x10: SetDSP3 = &DSP3_OP10; break;1201case 0x18: SetDSP3 = &DSP3_Convert; break;1202case 0x1c: SetDSP3 = &DSP3_OP1C; break;1203case 0x1e: SetDSP3 = &DSP3_OP1E; break;1204case 0x1f: SetDSP3 = &DSP3_MemoryDump; break;1205case 0x38: SetDSP3 = &DSP3_Decode; break;1206case 0x3e: SetDSP3 = &DSP3_OP3E; break;1207default:1208return;1209}12101211DSP3.SR = 0x0080;1212DSP3.Index = 0;1213}1214}12151216void DSP3SetByte (uint8 byte, uint16 address)1217{1218if (address < DSP0.boundary)1219{1220if (DSP3.SR & 0x04)1221{1222DSP3.DR = (DSP3.DR & 0xff00) + byte;1223(*SetDSP3)();1224}1225else1226{1227DSP3.SR ^= 0x10;12281229if (DSP3.SR & 0x10)1230DSP3.DR = (DSP3.DR & 0xff00) + byte;1231else1232{1233DSP3.DR = (DSP3.DR & 0x00ff) + (byte << 8);1234(*SetDSP3)();1235}1236}1237}1238}12391240uint8 DSP3GetByte (uint16 address)1241{1242if (address < DSP0.boundary)1243{1244uint8 byte;12451246if (DSP3.SR & 0x04)1247{1248byte = (uint8) DSP3.DR;1249(*SetDSP3)();1250}1251else1252{1253DSP3.SR ^= 0x10;12541255if (DSP3.SR & 0x10)1256byte = (uint8) (DSP3.DR);1257else1258{1259byte = (uint8) (DSP3.DR >> 8);1260(*SetDSP3)();1261}1262}12631264return (byte);1265}12661267return (uint8) DSP3.SR;1268}126912701271