diff --git a/zsnes/src/chips/dsp1emu.c b/zsnes/src/chips/dsp1emu.c index 570d329b..81fbc8e9 100644 --- a/zsnes/src/chips/dsp1emu.c +++ b/zsnes/src/chips/dsp1emu.c @@ -15,16 +15,18 @@ //along with this program; if not, write to the Free Software //Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -#ifdef __LINUX__ +#define __ZSNES__ + +#if (defined __ZSNES__ && __LINUX__) #include "../gblhdr.h" #else + #include #include #include #include #include #endif - //#define DebugDSP1 // uncomment some lines to test @@ -83,6 +85,137 @@ void Stop_Log (void) #endif +const short DSP1ROM[1024] = { + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, + 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, + 0x4000, 0x7fff, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, + 0x0100, 0x0080, 0x0040, 0x0020, 0x0001, 0x0008, 0x0004, 0x0002, + 0x0001, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, + 0x0000, 0x0000, 0x8000, 0xffe5, 0x0100, 0x7fff, 0x7f02, 0x7e08, + 0x7d12, 0x7c1f, 0x7b30, 0x7a45, 0x795d, 0x7878, 0x7797, 0x76ba, + 0x75df, 0x7507, 0x7433, 0x7361, 0x7293, 0x71c7, 0x70fe, 0x7038, + 0x6f75, 0x6eb4, 0x6df6, 0x6d3a, 0x6c81, 0x6bca, 0x6b16, 0x6a64, + 0x69b4, 0x6907, 0x685b, 0x67b2, 0x670b, 0x6666, 0x65c4, 0x6523, + 0x6484, 0x63e7, 0x634c, 0x62b3, 0x621c, 0x6186, 0x60f2, 0x6060, + 0x5fd0, 0x5f41, 0x5eb5, 0x5e29, 0x5d9f, 0x5d17, 0x5c91, 0x5c0c, + 0x5b88, 0x5b06, 0x5a85, 0x5a06, 0x5988, 0x590b, 0x5890, 0x5816, + 0x579d, 0x5726, 0x56b0, 0x563b, 0x55c8, 0x5555, 0x54e4, 0x5474, + 0x5405, 0x5398, 0x532b, 0x52bf, 0x5255, 0x51ec, 0x5183, 0x511c, + 0x50b6, 0x5050, 0x4fec, 0x4f89, 0x4f26, 0x4ec5, 0x4e64, 0x4e05, + 0x4da6, 0x4d48, 0x4cec, 0x4c90, 0x4c34, 0x4bda, 0x4b81, 0x4b28, + 0x4ad0, 0x4a79, 0x4a23, 0x49cd, 0x4979, 0x4925, 0x48d1, 0x487f, + 0x482d, 0x47dc, 0x478c, 0x473c, 0x46ed, 0x469f, 0x4651, 0x4604, + 0x45b8, 0x456c, 0x4521, 0x44d7, 0x448d, 0x4444, 0x43fc, 0x43b4, + 0x436d, 0x4326, 0x42e0, 0x429a, 0x4255, 0x4211, 0x41cd, 0x4189, + 0x4146, 0x4104, 0x40c2, 0x4081, 0x4040, 0x3fff, 0x41f7, 0x43e1, + 0x45bd, 0x478d, 0x4951, 0x4b0b, 0x4cbb, 0x4e61, 0x4fff, 0x5194, + 0x5322, 0x54a9, 0x5628, 0x57a2, 0x5914, 0x5a81, 0x5be9, 0x5d4a, + 0x5ea7, 0x5fff, 0x6152, 0x62a0, 0x63ea, 0x6530, 0x6672, 0x67b0, + 0x68ea, 0x6a20, 0x6b53, 0x6c83, 0x6daf, 0x6ed9, 0x6fff, 0x7122, + 0x7242, 0x735f, 0x747a, 0x7592, 0x76a7, 0x77ba, 0x78cb, 0x79d9, + 0x7ae5, 0x7bee, 0x7cf5, 0x7dfa, 0x7efe, 0x7fff, 0x0000, 0x0324, + 0x0647, 0x096a, 0x0c8b, 0x0fab, 0x12c8, 0x15e2, 0x18f8, 0x1c0b, + 0x1f19, 0x2223, 0x2528, 0x2826, 0x2b1f, 0x2e11, 0x30fb, 0x33de, + 0x36ba, 0x398c, 0x3c56, 0x3f17, 0x41ce, 0x447a, 0x471c, 0x49b4, + 0x4c3f, 0x4ebf, 0x5133, 0x539b, 0x55f5, 0x5842, 0x5a82, 0x5cb4, + 0x5ed7, 0x60ec, 0x62f2, 0x64e8, 0x66cf, 0x68a6, 0x6a6d, 0x6c24, + 0x6dca, 0x6f5f, 0x70e2, 0x7255, 0x73b5, 0x7504, 0x7641, 0x776c, + 0x7884, 0x798a, 0x7a7d, 0x7b5d, 0x7c29, 0x7ce3, 0x7d8a, 0x7e1d, + 0x7e9d, 0x7f09, 0x7f62, 0x7fa7, 0x7fd8, 0x7ff6, 0x7fff, 0x7ff6, + 0x7fd8, 0x7fa7, 0x7f62, 0x7f09, 0x7e9d, 0x7e1d, 0x7d8a, 0x7ce3, + 0x7c29, 0x7b5d, 0x7a7d, 0x798a, 0x7884, 0x776c, 0x7641, 0x7504, + 0x73b5, 0x7255, 0x70e2, 0x6f5f, 0x6dca, 0x6c24, 0x6a6d, 0x68a6, + 0x66cf, 0x64e8, 0x62f2, 0x60ec, 0x5ed7, 0x5cb4, 0x5a82, 0x5842, + 0x55f5, 0x539b, 0x5133, 0x4ebf, 0x4c3f, 0x49b4, 0x471c, 0x447a, + 0x41ce, 0x3f17, 0x3c56, 0x398c, 0x36ba, 0x33de, 0x30fb, 0x2e11, + 0x2b1f, 0x2826, 0x2528, 0x2223, 0x1f19, 0x1c0b, 0x18f8, 0x15e2, + 0x12c8, 0x0fab, 0x0c8b, 0x096a, 0x0647, 0x0324, 0x7fff, 0x7ff6, + 0x7fd8, 0x7fa7, 0x7f62, 0x7f09, 0x7e9d, 0x7e1d, 0x7d8a, 0x7ce3, + 0x7c29, 0x7b5d, 0x7a7d, 0x798a, 0x7884, 0x776c, 0x7641, 0x7504, + 0x73b5, 0x7255, 0x70e2, 0x6f5f, 0x6dca, 0x6c24, 0x6a6d, 0x68a6, + 0x66cf, 0x64e8, 0x62f2, 0x60ec, 0x5ed7, 0x5cb4, 0x5a82, 0x5842, + 0x55f5, 0x539b, 0x5133, 0x4ebf, 0x4c3f, 0x49b4, 0x471c, 0x447a, + 0x41ce, 0x3f17, 0x3c56, 0x398c, 0x36ba, 0x33de, 0x30fb, 0x2e11, + 0x2b1f, 0x2826, 0x2528, 0x2223, 0x1f19, 0x1c0b, 0x18f8, 0x15e2, + 0x12c8, 0x0fab, 0x0c8b, 0x096a, 0x0647, 0x0324, 0x0000, 0xfcdc, + 0xf9b9, 0xf696, 0xf375, 0xf055, 0xed38, 0xea1e, 0xe708, 0xe3f5, + 0xe0e7, 0xdddd, 0xdad8, 0xd7da, 0xd4e1, 0xd1ef, 0xcf05, 0xcc22, + 0xc946, 0xc674, 0xc3aa, 0xc0e9, 0xbe32, 0xbb86, 0xb8e4, 0xb64c, + 0xb3c1, 0xb141, 0xaecd, 0xac65, 0xaa0b, 0xa7be, 0xa57e, 0xa34c, + 0xa129, 0x9f14, 0x9d0e, 0x9b18, 0x9931, 0x975a, 0x9593, 0x93dc, + 0x9236, 0x90a1, 0x8f1e, 0x8dab, 0x8c4b, 0x8afc, 0x89bf, 0x8894, + 0x877c, 0x8676, 0x8583, 0x84a3, 0x83d7, 0x831d, 0x8276, 0x81e3, + 0x8163, 0x80f7, 0x809e, 0x8059, 0x8028, 0x800a, 0x6488, 0x0080, + 0x03ff, 0x0116, 0x0002, 0x0080, 0x4000, 0x3fd7, 0x3faf, 0x3f86, + 0x3f5d, 0x3f34, 0x3f0c, 0x3ee3, 0x3eba, 0x3e91, 0x3e68, 0x3e40, + 0x3e17, 0x3dee, 0x3dc5, 0x3d9c, 0x3d74, 0x3d4b, 0x3d22, 0x3cf9, + 0x3cd0, 0x3ca7, 0x3c7f, 0x3c56, 0x3c2d, 0x3c04, 0x3bdb, 0x3bb2, + 0x3b89, 0x3b60, 0x3b37, 0x3b0e, 0x3ae5, 0x3abc, 0x3a93, 0x3a69, + 0x3a40, 0x3a17, 0x39ee, 0x39c5, 0x399c, 0x3972, 0x3949, 0x3920, + 0x38f6, 0x38cd, 0x38a4, 0x387a, 0x3851, 0x3827, 0x37fe, 0x37d4, + 0x37aa, 0x3781, 0x3757, 0x372d, 0x3704, 0x36da, 0x36b0, 0x3686, + 0x365c, 0x3632, 0x3609, 0x35df, 0x35b4, 0x358a, 0x3560, 0x3536, + 0x350c, 0x34e1, 0x34b7, 0x348d, 0x3462, 0x3438, 0x340d, 0x33e3, + 0x33b8, 0x338d, 0x3363, 0x3338, 0x330d, 0x32e2, 0x32b7, 0x328c, + 0x3261, 0x3236, 0x320b, 0x31df, 0x31b4, 0x3188, 0x315d, 0x3131, + 0x3106, 0x30da, 0x30ae, 0x3083, 0x3057, 0x302b, 0x2fff, 0x2fd2, + 0x2fa6, 0x2f7a, 0x2f4d, 0x2f21, 0x2ef4, 0x2ec8, 0x2e9b, 0x2e6e, + 0x2e41, 0x2e14, 0x2de7, 0x2dba, 0x2d8d, 0x2d60, 0x2d32, 0x2d05, + 0x2cd7, 0x2ca9, 0x2c7b, 0x2c4d, 0x2c1f, 0x2bf1, 0x2bc3, 0x2b94, + 0x2b66, 0x2b37, 0x2b09, 0x2ada, 0x2aab, 0x2a7c, 0x2a4c, 0x2a1d, + 0x29ed, 0x29be, 0x298e, 0x295e, 0x292e, 0x28fe, 0x28ce, 0x289d, + 0x286d, 0x283c, 0x280b, 0x27da, 0x27a9, 0x2777, 0x2746, 0x2714, + 0x26e2, 0x26b0, 0x267e, 0x264c, 0x2619, 0x25e7, 0x25b4, 0x2581, + 0x254d, 0x251a, 0x24e6, 0x24b2, 0x247e, 0x244a, 0x2415, 0x23e1, + 0x23ac, 0x2376, 0x2341, 0x230b, 0x22d6, 0x229f, 0x2269, 0x2232, + 0x21fc, 0x21c4, 0x218d, 0x2155, 0x211d, 0x20e5, 0x20ad, 0x2074, + 0x203b, 0x2001, 0x1fc7, 0x1f8d, 0x1f53, 0x1f18, 0x1edd, 0x1ea1, + 0x1e66, 0x1e29, 0x1ded, 0x1db0, 0x1d72, 0x1d35, 0x1cf6, 0x1cb8, + 0x1c79, 0x1c39, 0x1bf9, 0x1bb8, 0x1b77, 0x1b36, 0x1af4, 0x1ab1, + 0x1a6e, 0x1a2a, 0x19e6, 0x19a1, 0x195c, 0x1915, 0x18ce, 0x1887, + 0x183f, 0x17f5, 0x17ac, 0x1761, 0x1715, 0x16c9, 0x167c, 0x162e, + 0x15df, 0x158e, 0x153d, 0x14eb, 0x1497, 0x1442, 0x13ec, 0x1395, + 0x133c, 0x12e2, 0x1286, 0x1228, 0x11c9, 0x1167, 0x1104, 0x109e, + 0x1036, 0x0fcc, 0x0f5f, 0x0eef, 0x0e7b, 0x0e04, 0x0d89, 0x0d0a, + 0x0c86, 0x0bfd, 0x0b6d, 0x0ad6, 0x0a36, 0x098d, 0x08d7, 0x0811, + 0x0736, 0x063e, 0x0519, 0x039a, 0x0000, 0x7fff, 0x0100, 0x0080, + 0x021d, 0x00c8, 0x00ce, 0x0048, 0x0a26, 0x277a, 0x00ce, 0x6488, + 0x14ac, 0x0001, 0x00f9, 0x00fc, 0x00ff, 0x00fc, 0x00f9, 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, 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, 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, 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, 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}; + + /***************************************************************************\ * Math tables * \***************************************************************************/ @@ -91,7 +224,7 @@ void Stop_Log (void) #define Angle(x) (((x)/(65536/INCR)) & (INCR-1)) #define Cos(x) ((double) CosTable2[x]) #define Sin(x) ((double) SinTable2[x]) -#define PI 3.14159265358979323846264338327 +#define PI 3.1415926535897932384626433832795 double CosTable2[INCR]; double SinTable2[INCR]; @@ -104,10 +237,12 @@ double Atan(double x) return (PI/2 - Atan(1/x)); } -#if 1 +#ifdef __ZSNES__ /***************************************************************************\ * C4 C code * \***************************************************************************/ + + short C4WFXVal; short C4WFYVal; short C4WFZVal; @@ -266,6 +401,22 @@ void DSPOp00() #endif } +short Op20Multiplicand; // int16 +short Op20Multiplier; // int16 +short Op20Result; // int16 + +void DSPOp20() +{ + // Use the shift 15, don't divide by 32768, it doesn't round the same. + + // This expression is bit accurate to DSP1B on MSVC 6. + Op20Result=Op20Multiplicand*Op20Multiplier >> 15; + Op20Result++; + #ifdef DebugDSP1 + Log_Message("OP20 MULT %d*%d/32768=%d",Op20Multiplicand,Op20Multiplier,Op20Result); + #endif +} + signed short Op10Coefficient; signed short Op10Exponent; signed short Op10CoefficientR; @@ -273,108 +424,80 @@ signed short Op10ExponentR; //float Op10Temp; short InvTable[128] = { - -0x8000, 0x7f02, 0x7e08, 0x7d12, 0x7c1f, 0x7b30, 0x7a45, 0x795d, - 0x7878, 0x7797, 0x76ba, 0x75df, 0x7512, 0x7433, 0x7361, 0x7293, - 0x71d7, 0x70fe, 0x7038, 0x6f75, 0x6eb4, 0x6df6, 0x6d3a, 0x6c81, - 0x6bca, 0x6b16, 0x6a69, 0x69b4, 0x6907, 0x685b, 0x67b2, 0x670b, + 0x7fff, 0x7f02, 0x7e08, 0x7d12, 0x7c1f, 0x7b30, 0x7a45, 0x795d, + 0x7878, 0x7797, 0x76ba, 0x75df, 0x7507, 0x7433, 0x7361, 0x7293, + 0x71c7, 0x70fe, 0x7038, 0x6f75, 0x6eb4, 0x6df6, 0x6d3a, 0x6c81, + 0x6bca, 0x6b16, 0x6a64, 0x69b4, 0x6907, 0x685b, 0x67b2, 0x670b, 0x6666, 0x65c4, 0x6523, 0x6484, 0x63e7, 0x634c, 0x62b3, 0x621c, - 0x6186, 0x60f2, 0x6060, 0x5ffd, 0x5f41, 0x5eb5, 0x5e29, 0x5d9f, + 0x6186, 0x60f2, 0x6060, 0x5fd0, 0x5f41, 0x5eb5, 0x5e29, 0x5d9f, 0x5d17, 0x5c91, 0x5c0c, 0x5b88, 0x5b06, 0x5a85, 0x5a06, 0x5988, - 0x5912, 0x5890, 0x5816, 0x57a8, 0x5726, 0x56b0, 0x563b, 0x55c8, - 0x55a9, 0x54e4, 0x5474, 0x5405, 0x53a9, 0x532b, 0x52c8, 0x525e, - 0x5231, 0x5183, 0x512a, 0x50b6, 0x5050, 0x4ffb, 0x4f89, 0x4f26, - 0x4ec5, 0x4e64, 0x4e05, 0x4da6, 0x4d48, 0x4cec, 0x4c96, 0x4c34, - 0x4bda, 0x4b81, 0x4b36, 0x4b30, 0x4a79, 0x4a77, 0x4a27, 0x4979, - 0x494d, 0x48e9, 0x487f, 0x486a, 0x4851, 0x478c, 0x479a, 0x4751, - 0x469f, 0x46b6, 0x4610, 0x45b8, 0x45d1, 0x4589, 0x452e, 0x44e1, - 0x44ae, 0x4470, 0x4413, 0x43c7, 0x437e, 0x433f, 0x430c, 0x42a6, - 0x4276, 0x4231, 0x41f3, 0x41c9, 0x417c, 0x413c, 0x4082, 0x40b6}; + 0x590b, 0x5890, 0x5816, 0x579d, 0x5726, 0x56b0, 0x563b, 0x55c8, + 0x5555, 0x54e4, 0x5474, 0x5405, 0x5398, 0x532b, 0x52bf, 0x5255, + 0x51ec, 0x5183, 0x511c, 0x50b6, 0x5050, 0x4fec, 0x4f89, 0x4f26, + 0x4ec5, 0x4e64, 0x4e05, 0x4da6, 0x4d48, 0x4cec, 0x4c90, 0x4c34, + 0x4bda, 0x4b81, 0x4b28, 0x4ad0, 0x4a79, 0x4a23, 0x49cd, 0x4979, + 0x4925, 0x48d1, 0x487f, 0x482d, 0x47dc, 0x478c, 0x473c, 0x46ed, + 0x469f, 0x4651, 0x4604, 0x45b8, 0x456c, 0x4521, 0x44d7, 0x448d, + 0x4444, 0x43fc, 0x43b4, 0x436d, 0x4326, 0x42e0, 0x429a, 0x4255, + 0x4211, 0x41cd, 0x4189, 0x4146, 0x4104, 0x40c2, 0x4081, 0x4040}; -void DSPOp10() +void DSP1_Inverse(short Coefficient, short Exponent, short *iCoefficient, short *iExponent) { - // Hard to get bit accurate here but it's very close... - // within 2 lsb's on the coefficient of the DSP1B. Emu is more accurate. - - if (Op10Coefficient == 0x0000) + // Step One: Division by Zero + if (Coefficient == 0x0000) { - Op10CoefficientR = 0x7fff; - Op10ExponentR = 0x002f; + *iCoefficient = 0x7fff; + *iExponent = 0x002f; } else { short Sign = 1; - if (Op10Coefficient < -32767) - Op10Coefficient = -32767; - - if (Op10Coefficient < 0) + // Step Two: Remove Sign + if (Coefficient < 0) { - Op10Coefficient = -Op10Coefficient; + if (Coefficient < -32767) Coefficient = -32767; + Coefficient = -Coefficient; Sign = -1; } - while (Op10Coefficient < 0x4000) + // Step Three: Normalize + while (Coefficient < 0x4000) { - Op10Coefficient <<= 1; - Op10Exponent--; + Coefficient <<= 1; + Exponent--; } - if (Op10Coefficient == 0x4000) - { - if (Sign == 1) - Op10CoefficientR = 0x7fff; - else - { - Op10CoefficientR = -16384;//0xc000; - Op10Exponent--; + // Step Four: Special Case + if (Coefficient == 0x4000) + if (Sign == 1) *iCoefficient = 0x7fff; + else { + *iCoefficient = -0x4000; + Exponent--; } - } else { - short i = InvTable[(Op10Coefficient - 0x4000) >> 7]; + // Step Five: Initial Guess + short i = InvTable[(Coefficient - 0x4000) >> 7]; - i = (i << 1) + (((-i * ((Op10Coefficient * i) >> 15)) >> 15) << 1); - i = (i << 1) + (((-i * ((Op10Coefficient * i) >> 15)) >> 15) << 1); + // Step Six: Iterate "estimated" Newton's Method + i = (i << 1) + (((-i * ((Coefficient * i) >> 15)) >> 15) << 1); + i = (i << 1) + (((-i * ((Coefficient * i) >> 15)) >> 15) << 1); - Op10CoefficientR = i * Sign; + *iCoefficient = i * Sign; } - Op10ExponentR = 1 - Op10Exponent; + *iExponent = 1 - Exponent; } - - #ifdef DebugDSP1 - Log_Message("OP10 INV %d*2^%d = %d*2^%d", Op10Coefficient, Op10Exponent, Op10CoefficientR, Op10ExponentR); - #endif } -/* + + void DSPOp10() { - // Hard to get bit accurate here but it's very close... - // within 2 lsb's on the coefficient of the DSP1B. Emu is more accurate. - - if (Op10Coefficient == 0) { - Op10CoefficientR = 0x7fff; // DSP1B - Strange but true - Op10ExponentR = 0x002f; - } else { - Op10ExponentR = -Op10Exponent; - Op10Temp = (float)(Op10Coefficient / 32768.0); - Op10Temp = 1/Op10Temp; - if (Op10Temp > 0) - while (Op10Temp >= 1.0) { - Op10Temp = (float)(Op10Temp/2.0); - Op10ExponentR++; - } - else - while (Op10Temp <= -1.0) { - Op10Temp=(float)(Op10Temp/2.0); - Op10ExponentR++; - } - Op10CoefficientR = (short)(Op10Temp*32768); - } + DSP1_Inverse(Op10Coefficient, Op10Exponent, &Op10CoefficientR, &Op10ExponentR); #ifdef DebugDSP1 Log_Message("OP10 INV %d*2^%d = %d*2^%d", Op10Coefficient, Op10Exponent, Op10CoefficientR, Op10ExponentR); #endif } -*/ short Op04Angle; short Op04Radius; // This is signed @@ -458,7 +581,8 @@ short SinInt(short Angle) if (Angle == -32768) return 0; - if (Angle < 0) return -SinInt(-Angle); + if (Angle < 0) + return -SinInt(-Angle); else { int S = SinTable[Hi(Angle)] + ((MulTable[Lo(Angle)] * SinTable[Hi((Angle + 0x4000))]) >> 15); @@ -472,8 +596,9 @@ short SinInt(short Angle) short CosInt(short Angle) { - int S; + int S; if (Angle == -32768) return -32768; + if (Angle < 0) Angle = -Angle; S = SinTable[Hi((Angle + 0x4000))] - ((MulTable[Lo(Angle)] * (-SinTable[Hi((Angle + 0x8000))])) >> 15); @@ -490,43 +615,7 @@ void DSPOp04() Op04Cos = CosInt(Op04Angle) * Op04Radius >> 15; } -/* -#ifdef __OPT04__ - -void DSPOp04() -{ - int angle; - - angle = Angle(Op04Angle); - - Op04Sin = Sin(angle) * Op04Radius; - Op04Cos = Cos(angle) * Op04Radius; - - #ifdef DebugDSP1 - Log_Message("OP04 Angle:%d Radius:%d",(Op04Angle/256)&255,Op04Radius); - Log_Message("OP04 SIN:%d COS:%d",Op04Sin,Op04Cos); - #endif -} -#else - -void DSPOp04() -{ - double angle; - - angle = Op04Angle*2*PI/65536.0; - - Op04Sin = (short)(sin(angle) * Op04Radius); - Op04Cos = (short)(cos(angle) * Op04Radius); - - #ifdef DebugDSP1 - Log_Message("OP04 Angle:%d Radius:%d",(Op04Angle/256)&255,Op04Radius); - Log_Message("OP04 SIN:%d COS:%d",Op04Sin,Op04Cos); - #endif -} -#endif -*/ -#define Op0CAngle Op0CA //Fix for name change -Nach -short Op0CAngle; // Angles are signed +short Op0CA; // Angles are signed short Op0CX1; short Op0CY1; short Op0CX2; @@ -534,45 +623,12 @@ short Op0CY2; void DSPOp0C() -//{ -// Op0CX2 = ((Op0CY1 * sin_int(Op0CAngle)) >> 15) + ((Op0CX1 * cos_int(Op0CAngle)) >> 15); -// Op0CY2 = ((Op0CY1 * cos_int(Op0CAngle)) >> 15) - ((Op0CX1 * sin_int(Op0CAngle)) >> 15); -//} -//void DSP1_Rotate(short Angle, short X1, short Y1, short &X2, short &Y2) { - Op0CX2 = (Op0CY1 * SinInt(Op0CAngle) >> 15) + (Op0CX1 * CosInt(Op0CAngle) >> 15); - Op0CY2 = (Op0CY1 * CosInt(Op0CAngle) >> 15) - (Op0CX1 * SinInt(Op0CAngle) >> 15); + Op0CX2 = (Op0CY1 * SinInt(Op0CA) >> 15) + (Op0CX1 * CosInt(Op0CA) >> 15); + Op0CY2 = (Op0CY1 * CosInt(Op0CA) >> 15) - (Op0CX1 * SinInt(Op0CA) >> 15); } -/* -unsigned short Op0CA; -short Op0CX1; -short Op0CY1; -short Op0CX2; -short Op0CY2; -#ifdef __OPT0C__ -void DSPOp0C() -{ - Op0CX2=(short)((Op0CX1*Cos(Angle(Op0CA))+Op0CY1*Sin(Angle(Op0CA)))); - Op0CY2=(short)((Op0CX1*-Sin(Angle(Op0CA))+Op0CY1*Cos(Angle(Op0CA)))); - #ifdef DebugDSP1 - Log_Message("OP0C Angle:%d X:%d Y:%d CX:%d CY:%d",(Op0CA/256)&255,Op0CX1,Op0CY1,Op0CX2,Op0CY2); - #endif -} -#else -void DSPOp0C() -{ - - Op0CX2=(Op0CX1*cos(Op0CA*2*PI/65536.0)+Op0CY1*sin(Op0CA*2*PI/65536.0)); - Op0CY2=(Op0CX1*-sin(Op0CA*2*PI/65536.0)+Op0CY1*cos(Op0CA*2*PI/65536.0)); - #ifdef DebugDSP1 - Log_Message("OP0C Angle:%d X:%d Y:%d CX:%d CY:%d",(Op0CA/256)&255,Op0CX1,Op0CY1,Op0CX2,Op0CY2); - #endif -} - -#endif -*/ short Op02FX; short Op02FY; short Op02FZ; @@ -1048,31 +1104,9 @@ void DSPOp06() #endif - -double matrixB[3][3]; -double matrixB2[3][3]; -double matrixB3[3][3]; - -double matrixA[3][3]; -double matrixA2[3][3]; -double matrixA3[3][3]; - -void MultMatrixB(double result[3][3],double mat1[3][3],double mat2[3][3]) -{ - result[0][0]=(mat1[0][0]*mat2[0][0]+mat1[0][1]*mat2[1][0]+mat1[0][2]*mat2[2][0]); - result[0][1]=(mat1[0][0]*mat2[0][1]+mat1[0][1]*mat2[1][1]+mat1[0][2]*mat2[2][1]); - result[0][2]=(mat1[0][0]*mat2[0][2]+mat1[0][1]*mat2[1][2]+mat1[0][2]*mat2[2][2]); - - result[1][0]=(mat1[1][0]*mat2[0][0]+mat1[1][1]*mat2[1][0]+mat1[1][2]*mat2[2][0]); - result[1][1]=(mat1[1][0]*mat2[0][1]+mat1[1][1]*mat2[1][1]+mat1[1][2]*mat2[2][1]); - result[1][2]=(mat1[1][0]*mat2[0][2]+mat1[1][1]*mat2[1][2]+mat1[1][2]*mat2[2][2]); - - result[2][0]=(mat1[2][0]*mat2[0][0]+mat1[2][1]*mat2[1][0]+mat1[2][2]*mat2[2][0]); - result[2][1]=(mat1[2][0]*mat2[0][1]+mat1[2][1]*mat2[1][1]+mat1[2][2]*mat2[2][1]); - result[2][2]=(mat1[2][0]*mat2[0][2]+mat1[2][1]*mat2[1][2]+mat1[2][2]*mat2[2][2]); - -} - +short matrixC[3][3]; +short matrixB[3][3]; +short matrixA[3][3]; short Op01m; short Op01Zr; @@ -1088,226 +1122,77 @@ short Op21Xr; short Op21Yr; double sc,sc2,sc3; - - -#ifdef __OPT01__ -void DSPOp01() +void DSPOp01 () { - unsigned short zr,yr,xr; + short SinAz = SinInt(Op01Zr); + short CosAz = CosInt(Op01Zr); + short SinAy = SinInt(Op01Yr); + short CosAy = CosInt(Op01Yr); + short SinAx = SinInt(Op01Xr); + short CosAx = CosInt(Op01Xr); - zr = Angle(Op01Zr); - xr = Angle(Op01Yr); - yr = Angle(Op01Xr); + Op01m >>= 1; - matrixB[0][0]=1; matrixB[0][1]=0; matrixB[0][2]=0; - matrixB[1][0]=0; matrixB[1][1]=Cos(xr); matrixB[1][2]=-Sin(xr); - matrixB[2][0]=0; matrixB[2][1]=Sin(xr); matrixB[2][2]=Cos(xr); + matrixA[0][0] = (Op01m * CosAz >> 15) * CosAy >> 15; + matrixA[0][1] = -((Op01m * SinAz >> 15) * CosAy >> 15); + matrixA[0][2] = Op01m * SinAy >> 15; - matrixB2[0][0]=Cos(yr); matrixB2[0][1]=0; matrixB2[0][2]=Sin(yr); - matrixB2[1][0]=0; matrixB2[1][1]=1; matrixB2[1][2]=0; - matrixB2[2][0]=-Sin(yr); matrixB2[2][1]=0; matrixB2[2][2]=Cos(yr); + matrixA[1][0] = ((Op01m * SinAz >> 15) * CosAx >> 15) + (((Op01m * CosAz >> 15) * SinAx >> 15) * SinAy >> 15); + matrixA[1][1] = ((Op01m * CosAz >> 15) * CosAx >> 15) - (((Op01m * SinAz >> 15) * SinAx >> 15) * SinAy >> 15); + matrixA[1][2] = -((Op01m * SinAx >> 15) * CosAy >> 15); - MultMatrixB(matrixB3,matrixB,matrixB2); - - matrixB2[0][0]=Cos(zr); matrixB2[0][1]=-Sin(zr); matrixB2[0][2]=0; - matrixB2[1][0]=Sin(zr); matrixB2[1][1]=Cos(zr); matrixB2[1][2]=0; - matrixB2[2][0]=0; matrixB2[2][1]=0; matrixB2[2][2]=1; - - MultMatrixB(matrixB,matrixB3,matrixB2); - - sc = ((double)Op01m)/32768.0; - - matrixA[0][0]=matrixB[0][0]; matrixA[0][1]=matrixB[0][1]; matrixA[0][2]=matrixB[0][2]; - matrixA[1][0]=matrixB[1][0]; matrixA[1][1]=matrixB[1][1]; matrixA[1][2]=matrixB[1][2]; - matrixA[2][0]=matrixB[2][0]; matrixA[2][1]=matrixB[2][1]; matrixA[2][2]=matrixB[2][2]; - - #ifdef DebugDSP1 - Log_Message("OP01 ZR: %d XR: %d YR: %d",Op01Zr,Op01Xr,Op01Yr); - #endif + matrixA[2][0] = ((Op01m * SinAz >> 15) * SinAx >> 15) - (((Op01m * CosAz >> 15) * CosAx >> 15) * SinAy >> 15); + matrixA[2][1] = ((Op01m * CosAz >> 15) * SinAx >> 15) + (((Op01m * SinAz >> 15) * CosAx >> 15) * SinAy >> 15); + matrixA[2][2] = (Op01m * CosAx >> 15) * CosAy >> 15; } -#else - -void DSPOp01() -{ - double zr,yr,xr; - - zr = ((double)Op01Zr)*6.2832/65536; - xr = ((double)Op01Yr)*6.2832/65536; - yr = ((double)Op01Xr)*6.2832/65536; - - matrixB[0][0]=1; matrixB[0][1]=0; matrixB[0][2]=0; - matrixB[1][0]=0; matrixB[1][1]=cos(xr); matrixB[1][2]=-sin(xr); - matrixB[2][0]=0; matrixB[2][1]=sin(xr); matrixB[2][2]=cos(xr); - - matrixB2[0][0]=cos(yr); matrixB2[0][1]=0; matrixB2[0][2]=sin(yr); - matrixB2[1][0]=0; matrixB2[1][1]=1; matrixB2[1][2]=0; - matrixB2[2][0]=-sin(yr); matrixB2[2][1]=0; matrixB2[2][2]=cos(yr); - - MultMatrixB(matrixB3,matrixB,matrixB2); - - matrixB2[0][0]=cos(zr); matrixB2[0][1]=-sin(zr); matrixB2[0][2]=0; - matrixB2[1][0]=sin(zr); matrixB2[1][1]=cos(zr); matrixB2[1][2]=0; - matrixB2[2][0]=0; matrixB2[2][1]=0; matrixB2[2][2]=1; - - MultMatrixB(matrixB,matrixB3,matrixB2); - - sc = ((double)Op01m)/32768.0; - - matrixA[0][0]=matrixB[0][0]; matrixA[0][1]=matrixB[0][1]; matrixA[0][2]=matrixB[0][2]; - matrixA[1][0]=matrixB[1][0]; matrixA[1][1]=matrixB[1][1]; matrixA[1][2]=matrixB[1][2]; - matrixA[2][0]=matrixB[2][0]; matrixA[2][1]=matrixB[2][1]; matrixA[2][2]=matrixB[2][2]; - - #ifdef DebugDSP1 - Log_Message("OP01 ZR: %d XR: %d YR: %d",Op01Zr,Op01Xr,Op01Yr); - #endif -} -#endif - - -#ifdef __OPT11__ -void DSPOp11() -{ - short zr,yr,xr; - - zr = Angle(Op11Zr); - xr = Angle(Op11Yr); - yr = Angle(Op11Xr); - - matrixB[0][0]=1; matrixB[0][1]=0; matrixB[0][2]=0; - matrixB[1][0]=0; matrixB[1][1]=Cos(xr); matrixB[1][2]=-Sin(xr); - matrixB[2][0]=0; matrixB[2][1]=Sin(xr); matrixB[2][2]=Cos(xr); - - matrixB2[0][0]=Cos(yr); matrixB2[0][1]=0; matrixB2[0][2]=Sin(yr); - matrixB2[1][0]=0; matrixB2[1][1]=1; matrixB2[1][2]=0; - matrixB2[2][0]=-Sin(yr); matrixB2[2][1]=0; matrixB2[2][2]=Cos(yr); - - MultMatrixB(matrixB3,matrixB,matrixB2); - - matrixB2[0][0]=Cos(zr); matrixB2[0][1]=-Sin(zr); matrixB2[0][2]=0; - matrixB2[1][0]=Sin(zr); matrixB2[1][1]=Cos(zr); matrixB2[1][2]=0; - matrixB2[2][0]=0; matrixB2[2][1]=0; matrixB2[2][2]=1; - - MultMatrixB(matrixB,matrixB3,matrixB2); - - sc2 = ((double)Op11m)/32768.0; - - matrixA2[0][0]=matrixB[0][0]; matrixA2[0][1]=matrixB[0][1]; matrixA2[0][2]=matrixB[0][2]; - matrixA2[1][0]=matrixB[1][0]; matrixA2[1][1]=matrixB[1][1]; matrixA2[1][2]=matrixB[1][2]; - matrixA2[2][0]=matrixB[2][0]; matrixA2[2][1]=matrixB[2][1]; matrixA2[2][2]=matrixB[2][2]; - #ifdef DebugDSP1 - Log_Message("OP11 ZR: %d XR: %d YR: %d SC: %d",Op11Zr,Op11Xr,Op11Yr,Op11m); - #endif -} -#else - void DSPOp11() { - double zr,yr,xr; + short SinAz = SinInt(Op11Zr); + short CosAz = CosInt(Op11Zr); + short SinAy = SinInt(Op11Yr); + short CosAy = CosInt(Op11Yr); + short SinAx = SinInt(Op11Xr); + short CosAx = CosInt(Op11Xr); - zr = ((double)Op11Zr)*6.2832/65536; - xr = ((double)Op11Yr)*6.2832/65536; - yr = ((double)Op11Xr)*6.2832/65536; + Op11m >>= 1; - matrixB[0][0]=1; matrixB[0][1]=0; matrixB[0][2]=0; - matrixB[1][0]=0; matrixB[1][1]=cos(xr); matrixB[1][2]=-sin(xr); - matrixB[2][0]=0; matrixB[2][1]=sin(xr); matrixB[2][2]=cos(xr); + matrixB[0][0] = (Op11m * CosAz >> 15) * CosAy >> 15; + matrixB[0][1] = -((Op11m * SinAz >> 15) * CosAy >> 15); + matrixB[0][2] = Op11m * SinAy >> 15; - matrixB2[0][0]=cos(yr); matrixB2[0][1]=0; matrixB2[0][2]=sin(yr); - matrixB2[1][0]=0; matrixB2[1][1]=1; matrixB2[1][2]=0; - matrixB2[2][0]=-sin(yr); matrixB2[2][1]=0; matrixB2[2][2]=cos(yr); + matrixB[1][0] = ((Op11m * SinAz >> 15) * CosAx >> 15) + (((Op11m * CosAz >> 15) * SinAx >> 15) * SinAy >> 15); + matrixB[1][1] = ((Op11m * CosAz >> 15) * CosAx >> 15) - (((Op11m * SinAz >> 15) * SinAx >> 15) * SinAy >> 15); + matrixB[1][2] = -((Op11m * SinAx >> 15) * CosAy >> 15); - MultMatrixB(matrixB3,matrixB,matrixB2); - - matrixB2[0][0]=cos(zr); matrixB2[0][1]=-sin(zr); matrixB2[0][2]=0; - matrixB2[1][0]=sin(zr); matrixB2[1][1]=cos(zr); matrixB2[1][2]=0; - matrixB2[2][0]=0; matrixB2[2][1]=0; matrixB2[2][2]=1; - - MultMatrixB(matrixB,matrixB3,matrixB2); - - sc2 = ((double)Op11m)/32768.0; - - matrixA2[0][0]=matrixB[0][0]; matrixA2[0][1]=matrixB[0][1]; matrixA2[0][2]=matrixB[0][2]; - matrixA2[1][0]=matrixB[1][0]; matrixA2[1][1]=matrixB[1][1]; matrixA2[1][2]=matrixB[1][2]; - matrixA2[2][0]=matrixB[2][0]; matrixA2[2][1]=matrixB[2][1]; matrixA2[2][2]=matrixB[2][2]; - #ifdef DebugDSP1 - Log_Message("OP11 ZR: %d XR: %d YR: %d SC: %d",Op11Zr,Op11Xr,Op11Yr,Op11m); - #endif + matrixB[2][0] = ((Op11m * SinAz >> 15) * SinAx >> 15) - (((Op11m * CosAz >> 15) * CosAx >> 15) * SinAy >> 15); + matrixB[2][1] = ((Op11m * CosAz >> 15) * SinAx >> 15) + (((Op11m * SinAz >> 15) * CosAx >> 15) * SinAy >> 15); + matrixB[2][2] = (Op11m * CosAx >> 15) * CosAy >> 15; } -#endif - - -#ifdef __OPT21__ -void DSPOp21() -{ - short zr,yr,xr; - - zr = Angle(Op21Zr); - xr = Angle(Op21Yr); - yr = Angle(Op21Xr); - - - matrixB[0][0]=1; matrixB[0][1]=0; matrixB[0][2]=0; - matrixB[1][0]=0; matrixB[1][1]=Cos(xr); matrixB[1][2]=-Sin(xr); - matrixB[2][0]=0; matrixB[2][1]=Sin(xr); matrixB[2][2]=Cos(xr); - - matrixB2[0][0]=Cos(yr); matrixB2[0][1]=0; matrixB2[0][2]=Sin(yr); - matrixB2[1][0]=0; matrixB2[1][1]=1; matrixB2[1][2]=0; - matrixB2[2][0]=-Sin(yr); matrixB2[2][1]=0; matrixB2[2][2]=Cos(yr); - - MultMatrixB(matrixB3,matrixB,matrixB2); - - matrixB2[0][0]=Cos(zr); matrixB2[0][1]=-Sin(zr); matrixB2[0][2]=0; - matrixB2[1][0]=Sin(zr); matrixB2[1][1]=Cos(zr); matrixB2[1][2]=0; - matrixB2[2][0]=0; matrixB2[2][1]=0; matrixB2[2][2]=1; - - MultMatrixB(matrixB,matrixB3,matrixB2); - - sc3 = ((double)Op21m)/32768.0; - - matrixA3[0][0]=matrixB[0][0]; matrixA3[0][1]=matrixB[0][1]; matrixA3[0][2]=matrixB[0][2]; - matrixA3[1][0]=matrixB[1][0]; matrixA3[1][1]=matrixB[1][1]; matrixA3[1][2]=matrixB[1][2]; - matrixA3[2][0]=matrixB[2][0]; matrixA3[2][1]=matrixB[2][1]; matrixA3[2][2]=matrixB[2][2]; - #ifdef DebugDSP1 - Log_Message("OP21 ZR: %d XR: %d YR: %d",Op21Zr,Op21Xr,Op21Yr); - #endif -} -#else void DSPOp21() { - double zr,yr,xr; + short SinAz = SinInt(Op21Zr); + short CosAz = CosInt(Op21Zr); + short SinAy = SinInt(Op21Yr); + short CosAy = CosInt(Op21Yr); + short SinAx = SinInt(Op21Xr); + short CosAx = CosInt(Op21Xr); - zr = ((double)Op21Zr)*6.2832/65536; - xr = ((double)Op21Yr)*6.2832/65536; - yr = ((double)Op21Xr)*6.2832/65536; + Op21m >>= 1; - matrixB[0][0]=1; matrixB[0][1]=0; matrixB[0][2]=0; - matrixB[1][0]=0; matrixB[1][1]=cos(xr); matrixB[1][2]=-sin(xr); - matrixB[2][0]=0; matrixB[2][1]=sin(xr); matrixB[2][2]=cos(xr); + matrixC[0][0] = (Op21m * CosAz >> 15) * CosAy >> 15; + matrixC[0][1] = -((Op21m * SinAz >> 15) * CosAy >> 15); + matrixC[0][2] = Op21m * SinAy >> 15; - matrixB2[0][0]=cos(yr); matrixB2[0][1]=0; matrixB2[0][2]=sin(yr); - matrixB2[1][0]=0; matrixB2[1][1]=1; matrixB2[1][2]=0; - matrixB2[2][0]=-sin(yr); matrixB2[2][1]=0; matrixB2[2][2]=cos(yr); + matrixC[1][0] = ((Op21m * SinAz >> 15) * CosAx >> 15) + (((Op21m * CosAz >> 15) * SinAx >> 15) * SinAy >> 15); + matrixC[1][1] = ((Op21m * CosAz >> 15) * CosAx >> 15) - (((Op21m * SinAz >> 15) * SinAx >> 15) * SinAy >> 15); + matrixC[1][2] = -((Op21m * SinAx >> 15) * CosAy >> 15); - MultMatrixB(matrixB3,matrixB,matrixB2); - - matrixB2[0][0]=cos(zr); matrixB2[0][1]=-sin(zr); matrixB2[0][2]=0; - matrixB2[1][0]=sin(zr); matrixB2[1][1]=cos(zr); matrixB2[1][2]=0; - matrixB2[2][0]=0; matrixB2[2][1]=0; matrixB2[2][2]=1; - - MultMatrixB(matrixB,matrixB3,matrixB2); - - sc3 = ((double)Op21m)/32768.0; - - matrixA3[0][0]=matrixB[0][0]; matrixA3[0][1]=matrixB[0][1]; matrixA3[0][2]=matrixB[0][2]; - matrixA3[1][0]=matrixB[1][0]; matrixA3[1][1]=matrixB[1][1]; matrixA3[1][2]=matrixB[1][2]; - matrixA3[2][0]=matrixB[2][0]; matrixA3[2][1]=matrixB[2][1]; matrixA3[2][2]=matrixB[2][2]; - #ifdef DebugDSP1 - Log_Message("OP21 ZR: %d XR: %d YR: %d",Op21Zr,Op21Xr,Op21Yr); - #endif + matrixC[2][0] = ((Op21m * SinAz >> 15) * SinAx >> 15) - (((Op21m * CosAz >> 15) * CosAx >> 15) * SinAy >> 15); + matrixC[2][1] = ((Op21m * CosAz >> 15) * SinAx >> 15) + (((Op21m * SinAz >> 15) * CosAx >> 15) * SinAy >> 15); + matrixC[2][2] = (Op21m * CosAx >> 15) * CosAy >> 15; } -#endif short Op0DX; short Op0DY; @@ -1328,38 +1213,11 @@ short Op2DF; short Op2DL; short Op2DU; -#define swap(a,b) temp=a;a=b;b=temp; - void DSPOp0D() { - double a,b,c,d,e,f,g,h,i,det,temp; - double a2,b2,c2,d2,e2,f2,g2,h2,i2,x,y,z; - - a = matrixA[0][0]; b=matrixA[0][1]; c=matrixA[0][2]; - d = matrixA[1][0]; e=matrixA[1][1]; f=matrixA[1][2]; - g = matrixA[2][0]; h=matrixA[2][1]; i=matrixA[2][2]; - //abc - //def - //ghi - det = a*e*i+b*f*g+c*d*h-g*e*c-h*f*a-i*d*b; - if (det==0) { - Op0DF=Op0DX; - Op0DL=Op0DY; - Op0DU=Op0DZ; - #ifdef DebugDSP1 - Log_Message("OP0D Error! Det == 0"); - #endif - return; - } - swap(d,b); swap(g,c); swap(h,f); - b=-b; d=-d; f=-f; h=-h; - a2=(e*i-h*f)/det; b2=(d*i-g*f)/det; c2=(d*h-g*e)/det; - d2=(b*i-h*c)/det; e2=(a*i-g*c)/det; f2=(a*h-g*b)/det; - g2=(b*f-e*c)/det; h2=(a*f-d*c)/det; i2=(a*e-d*b)/det; - x=Op0DX; y=Op0DY; z=Op0DZ; - Op0DF=(short)((x*a2+y*d2+z*g2)/2*sc); - Op0DL=(short)((x*b2+y*e2+z*h2)/2*sc); - Op0DU=(short)((x*c2+y*f2+z*i2)/2*sc); + Op0DF = (Op0DX * matrixA[0][0] >> 15) + (Op0DY * matrixA[0][1] >> 15) + (Op0DZ * matrixA[0][2] >> 15); + Op0DL = (Op0DX * matrixA[1][0] >> 15) + (Op0DY * matrixA[1][1] >> 15) + (Op0DZ * matrixA[1][2] >> 15); + Op0DU = (Op0DX * matrixA[2][0] >> 15) + (Op0DY * matrixA[2][1] >> 15) + (Op0DZ * matrixA[2][2] >> 15); #ifdef DebugDSP1 Log_Message("OP0D X: %d Y: %d Z: %d / F: %d L: %d U: %d",Op0DX,Op0DY,Op0DZ,Op0DF,Op0DL,Op0DU); @@ -1368,58 +1226,21 @@ void DSPOp0D() void DSPOp1D() { - double a,b,c,d,e,f,g,h,i,det,temp; - double a2,b2,c2,d2,e2,f2,g2,h2,i2,x,y,z; - a = matrixA2[0][0]; b=matrixA2[0][1]; c=matrixA2[0][2]; - d = matrixA2[1][0]; e=matrixA2[1][1]; f=matrixA2[1][2]; - g = matrixA2[2][0]; h=matrixA2[2][1]; i=matrixA2[2][2]; - //abc - //def - //ghi - det = a*e*i+b*f*g+c*d*h-g*e*c-h*f*a-i*d*b; - if (det==0) { - Op1DF=0; Op1DL=0; Op1DU=0; - return; - } - swap(d,b); swap(g,c); swap(h,f); - b=-b; d=-d; f=-f; h=-h; - a2=(e*i-h*f)/det; b2=(d*i-g*f)/det; c2=(d*h-g*e)/det; - d2=(b*i-h*c)/det; e2=(a*i-g*c)/det; f2=(a*h-g*b)/det; - g2=(b*f-e*c)/det; h2=(a*f-d*c)/det; i2=(a*e-d*b)/det; - x=Op1DX; y=Op1DY; z=Op1DZ; - Op1DF=(short)((x*a2+y*d2+z*g2)/2*sc2); - Op1DL=(short)((x*b2+y*e2+z*h2)/2*sc2); - Op1DU=(short)((x*c2+y*f2+z*i2)/2*sc2); - #ifdef DebugDSP1 - Log_Message("OP1D X: %d Y: %d Z: %d / F: %d L: %d U: %d",Op1DX,Op1DY,Op1DZ,Op1DF,Op1DL,Op1DU); - #endif + Op1DF = (Op1DX * matrixB[0][0] >> 15) + (Op1DY * matrixB[0][1] >> 15) + (Op1DZ * matrixB[0][2] >> 15); + Op1DL = (Op1DX * matrixB[1][0] >> 15) + (Op1DY * matrixB[1][1] >> 15) + (Op1DZ * matrixB[1][2] >> 15); + Op1DU = (Op1DX * matrixB[2][0] >> 15) + (Op1DY * matrixB[2][1] >> 15) + (Op1DZ * matrixB[2][2] >> 15); + + #ifdef DebugDSP1 + Log_Message("OP1D X: %d Y: %d Z: %d / F: %d L: %d U: %d",Op1DX,Op1DY,Op1DZ,Op1DF,Op1DL,Op1DU); + #endif } void DSPOp2D() { - double a,b,c,d,e,f,g,h,i,det,temp; - double a2,b2,c2,d2,e2,f2,g2,h2,i2,x,y,z; - a = matrixA3[0][0]; b=matrixA3[0][1]; c=matrixA3[0][2]; - d = matrixA3[1][0]; e=matrixA3[1][1]; f=matrixA3[1][2]; - g = matrixA3[2][0]; h=matrixA3[2][1]; i=matrixA3[2][2]; - //abc - //def - //ghi - det = a*e*i+b*f*g+c*d*h-g*e*c-h*f*a-i*d*b; - if (det==0) { - Op2DF=0; Op2DL=0; Op2DU=0; - return; - } - swap(d,b); swap(g,c); swap(h,f); - b=-b; d=-d; f=-f; h=-h; - a2=(e*i-h*f)/det; b2=(d*i-g*f)/det; c2=(d*h-g*e)/det; - d2=(b*i-h*c)/det; e2=(a*i-g*c)/det; f2=(a*h-g*b)/det; - g2=(b*f-e*c)/det; h2=(a*f-d*c)/det; i2=(a*e-d*b)/det; - x=Op2DX; y=Op2DY; z=Op2DZ; - Op2DF=(short)((x*a2+y*d2+z*g2)/2*sc3); - Op2DL=(short)((x*b2+y*e2+z*h2)/2*sc3); - Op2DU=(short)((x*c2+y*f2+z*i2)/2*sc3); - #ifdef DebugDSP1 + Op2DF = (Op2DX * matrixC[0][0] >> 15) + (Op2DY * matrixC[0][1] >> 15) + (Op2DZ * matrixC[0][2] >> 15); + Op2DL = (Op2DX * matrixC[1][0] >> 15) + (Op2DY * matrixC[1][1] >> 15) + (Op2DZ * matrixC[1][2] >> 15); + Op2DU = (Op2DX * matrixC[2][0] >> 15) + (Op2DY * matrixC[2][1] >> 15) + (Op2DZ * matrixC[2][2] >> 15); +#ifdef DebugDSP1 Log_Message("OP2D X: %d Y: %d Z: %d / F: %d L: %d U: %d",Op2DX,Op2DY,Op2DZ,Op2DF,Op2DL,Op2DU); #endif } @@ -1445,12 +1266,9 @@ short Op23Z; void DSPOp03() { - double F,L,U; - - F=Op03F; L=Op03L; U=Op03U; - Op03X=(short)((F*matrixA[0][0]+L*matrixA[1][0]+U*matrixA[2][0])/2*sc); - Op03Y=(short)((F*matrixA[0][1]+L*matrixA[1][1]+U*matrixA[2][1])/2*sc); - Op03Z=(short)((F*matrixA[0][2]+L*matrixA[1][2]+U*matrixA[2][2])/2*sc); + Op03X = (Op03F * matrixA[0][0] >> 15) + (Op03L * matrixA[1][0] >> 15) + (Op03U * matrixA[2][0] >> 15); + Op03Y = (Op03F * matrixA[0][1] >> 15) + (Op03L * matrixA[1][1] >> 15) + (Op03U * matrixA[2][1] >> 15); + Op03Z = (Op03F * matrixA[0][2] >> 15) + (Op03L * matrixA[1][2] >> 15) + (Op03U * matrixA[2][2] >> 15); #ifdef DebugDSP1 Log_Message("OP03 F: %d L: %d U: %d / X: %d Y: %d Z: %d",Op03F,Op03L,Op03U,Op03X,Op03Y,Op03Z); @@ -1459,11 +1277,9 @@ void DSPOp03() void DSPOp13() { - double F,L,U; - F=Op13F; L=Op13L; U=Op13U; - Op13X=(short)((F*matrixA2[0][0]+L*matrixA2[1][0]+U*matrixA2[2][0])/2*sc2); - Op13Y=(short)((F*matrixA2[0][1]+L*matrixA2[1][1]+U*matrixA2[2][1])/2*sc2); - Op13Z=(short)((F*matrixA2[0][2]+L*matrixA2[1][2]+U*matrixA2[2][2])/2*sc2); + Op13X = (Op13F * matrixB[0][0] >> 15) + (Op13L * matrixB[1][0] >> 15) + (Op13U * matrixB[2][0] >> 15); + Op13Y = (Op13F * matrixB[0][1] >> 15) + (Op13L * matrixB[1][1] >> 15) + (Op13U * matrixB[2][1] >> 15); + Op13Z = (Op13F * matrixB[0][2] >> 15) + (Op13L * matrixB[1][2] >> 15) + (Op13U * matrixB[2][2] >> 15); #ifdef DebugDSP1 Log_Message("OP13 F: %d L: %d U: %d / X: %d Y: %d Z: %d",Op13F,Op13L,Op13U,Op13X,Op13Y,Op13Z); #endif @@ -1471,11 +1287,9 @@ void DSPOp13() void DSPOp23() { - double F,L,U; - F=Op23F; L=Op23L; U=Op23U; - Op23X=(short)((F*matrixA3[0][0]+L*matrixA3[1][0]+U*matrixA3[2][0])/2*sc3); - Op23Y=(short)((F*matrixA3[0][1]+L*matrixA3[1][1]+U*matrixA3[2][1])/2*sc3); - Op23Z=(short)((F*matrixA3[0][2]+L*matrixA3[1][2]+U*matrixA3[2][2])/2*sc3); + Op23X = (Op23F * matrixC[0][0] >> 15) + (Op23L * matrixC[1][0] >> 15) + (Op23U * matrixC[2][0] >> 15); + Op23Y = (Op23F * matrixC[0][1] >> 15) + (Op23L * matrixC[1][1] >> 15) + (Op23U * matrixC[2][1] >> 15); + Op23Z = (Op23F * matrixC[0][2] >> 15) + (Op23L * matrixC[1][2] >> 15) + (Op23U * matrixC[2][2] >> 15); #ifdef DebugDSP1 Log_Message("OP23 F: %d L: %d U: %d / X: %d Y: %d Z: %d",Op23F,Op23L,Op23U,Op23X,Op23Y,Op23Z); #endif @@ -1491,19 +1305,34 @@ short Op14Zrr; short Op14Xrr; short Op14Yrr; -double Op14Temp; void DSPOp14() { - Op14Temp=(Op14Zr*6.2832/65536.0)+(1/cos(Op14Xr*6.2832/65536.0))*((Op14U*6.2832/65536.0)*cos(Op14Yr*6.2832/65536.0)-(Op14F*6.2832/65536.0)*sin(Op14Yr*6.2832/65536.0)); - Op14Zrr=(short)(Op14Temp*65536.0/6.2832); - Op14Temp=(Op14Xr*6.2832/65536.0)+((Op14U*6.2832/65536.0)*sin(Op14Yr*6.2832/65536.0)+(Op14F*6.2832/65536.0)*cos(Op14Yr*6.2832/65536.0)); - Op14Xrr=(short)(Op14Temp*65536.0/6.2832); - Op14Temp=(Op14Yr*6.2832/65536.0)-tan(Op14Xr*6.2832/65536.0)*((Op14U*6.2832/65536.0)*cos(Op14Yr*6.2832/65536.0)+(Op14F*6.2832/65536.0)*sin(Op14Yr*6.2832/65536.0))+(Op14L*6.2832/65536.0); - Op14Yrr=(short)(Op14Temp*65536.0/6.2832); - #ifdef DebugDSP1 - Log_Message("OP14 X:%d Y%d Z:%D U:%d F:%d L:%d",Op14Xr,Op14Yr,Op14Zr,Op14U,Op14F,Op14L); - Log_Message("OP14 X:%d Y%d Z:%D",Op14Xrr,Op14Yrr,Op14Zrr); - #endif + short Coefficient; + short Exponent; + int Rtmp; + + DSP1_Inverse(CosInt(Op14Xr), 0, &Coefficient, &Exponent); + + // Rotation Around Z + Rtmp = ((Op14U * CosInt(Op14Yr)) >> 15) - ((Op14F * SinInt(Op14Yr)) >> 15); + Rtmp = (Coefficient * (short) Rtmp) >> (15 - Exponent); + if (Rtmp > 32767) + Rtmp = 32767; + if (Rtmp < -32768) + Rtmp = -32767; + Op14Zrr = Op14Zr + Rtmp; + + // Rotation Around X + Op14Xrr = Op14Xr + ((Op14U * SinInt(Op14Yr)) >> 15) + ((Op14F * CosInt(Op14Yr)) >> 15); + + // Rotation Around Y + Rtmp = ((Op14U * CosInt(Op14Yr)) >> 15) + ((Op14F * SinInt(Op14Yr)) >> 15); + Rtmp = (((Coefficient * (short) Rtmp) >> 15) * SinInt(Op14Xr)) >> (15 - Exponent); + if (Rtmp > 32767) + Rtmp = 32767; + if (Rtmp < -32768) + Rtmp = -32767; + Op14Yrr = Op14Yr - Rtmp + Op14L; } short Op0EH; @@ -1541,7 +1370,7 @@ short Op2BS; void DSPOp0B() { - Op0BS = (short)((Op0BX*matrixA[0][0]+Op0BY*matrixA2[0][1]+Op0BZ*matrixA2[0][2])); + Op0BS = ((Op0BX*matrixA[0][0]+Op0BY*matrixA[0][1]+Op0BZ*matrixA[0][2])>>15); #ifdef DebugDSP1 Log_Message("OP0B"); #endif @@ -1549,17 +1378,17 @@ void DSPOp0B() void DSPOp1B() { - Op1BS = (short)((Op1BX*matrixA2[0][0]+Op1BY*matrixA2[0][1]+Op1BZ*matrixA2[0][2])); + Op1BS = ((Op1BX*matrixB[0][0]+Op1BY*matrixB[0][1]+Op1BZ*matrixB[0][2])>>15); #ifdef DebugDSP1 Log_Message("OP1B X: %d Y: %d Z: %d S: %d",Op1BX,Op1BY,Op1BZ,Op1BS); - Log_Message(" MX: %d MY: %d MZ: %d Scale: %d",(short)(matrixA2[0][0]*100),(short)(matrixA2[0][1]*100),(short)(matrixA2[0][2]*100),(short)(sc2*100)); + Log_Message(" MX: %d MY: %d MZ: %d Scale: %d",(short)(matrixB[0][0]*100),(short)(matrixB[0][1]*100),(short)(matrixB[0][2]*100),(short)(sc2*100)); #endif } void DSPOp2B() { - Op2BS = (short)((Op2BX*matrixA3[0][0]+Op2BY*matrixA3[0][1]+Op2BZ*matrixA3[0][2])); + Op2BS = (short)((Op2BX*matrixC[0][0]+Op2BY*matrixC[0][1]+Op2BZ*matrixC[0][2])>>15); #ifdef DebugDSP1 Log_Message("OP2B"); #endif @@ -1596,6 +1425,23 @@ void DSPOp18() #endif } +short Op38X,Op38Y,Op38Z,Op38R,Op38D; + +void DSPOp38() +{ + // This is bit accurate to DSP1B when compiled with VC6 on a P4 + + int x,y,z,r; // int32 + x=Op38X; y=Op38Y; z=Op38Z; r=Op38R; + r = (x*x+y*y+z*z-r*r); + Op38D=(short) (r >> 15); + Op38D++; + #ifdef DebugDSP1 + Log_Message("OP38 X: %d Y: %d Z: %d R: %D DIFF %d",Op38X,Op38Y,Op38Z,Op38D); + #endif +} + + short Op28X; short Op28Y; short Op28Z; @@ -1622,48 +1468,8 @@ short Op1CX2; short Op1CY2; short Op1CZ2; -/* -void DSP1_Op1C(short Az, short Ay, short Ax, //<--notice input order - short x1, short y1, short z1, - short *x2, short *y2, short *z2) -{ - short x,y,z; - //Rotate around Z: - x = ((x1*CosInt(Az))>>15) + ((y1*SinInt(Az))>>15); - y =-((x1*SinInt(Az))>>15) + ((y1*CosInt(Az))>>15); - z = z1; - x1=x; - y1=y; - z1=z; - //Rotate around Y: - x = ((x1*CosInt(Ay))>>15) - ((z1*SinInt(Ay))>>15); - y = y1; - z = ((x1*SinInt(Ay))>>15) + ((z1*CosInt(Ay))>>15); - x1=x; - y1=y; - z1=z; - //Rotate around X: - x = x1; - y = ((y1*CosInt(Ax))>>15) + ((z1*SinInt(Ax))>>15); - z =-((y1*SinInt(Ax))>>15) + ((z1*CosInt(Ax))>>15); - x1=x; - y1=y; - z1=z; - //return result - *x2 = x1; - *y2 = y1; - *z2 = z1; -} -*/ - -#ifdef __OPT1C__ void DSPOp1C() { -// short ya,xa,za; -// ya = Angle(Op1CX); -// xa = Angle(Op1CY); -// za = Angle(Op1CZ); - // rotate around Z Op1CX1=((Op1CXBR*CosInt(Op1CZ))>>15)+((Op1CYBR*SinInt(Op1CZ))>>15); Op1CY1=-(((Op1CXBR*SinInt(Op1CZ))>>15)+((Op1CYBR*CosInt(Op1CZ))>>15)); @@ -1681,59 +1487,6 @@ void DSPOp1C() Log_Message("OP1C Apply Matrix CX:%d CY:%d CZ",Op1CXAR,Op1CYAR,Op1CZAR); #endif } -/*void DSPOp1C() -{ - short ya,xa,za; - ya = Angle(Op1CX); - xa = Angle(Op1CY); - za = Angle(Op1CZ); - - // rotate around Z - Op1CX1=(short)((Op1CXBR*Cos(za)+Op1CYBR*Sin(za))); - Op1CY1=(short)((Op1CXBR*-Sin(za)+Op1CYBR*Cos(za))); - Op1CZ1=Op1CZBR; - // rotate around Y - Op1CX2=(short)((Op1CX1*Cos(ya)+Op1CZ1*-Sin(ya))); - Op1CY2=Op1CY1; - Op1CZ2=(short)((Op1CX1*Sin(ya)+Op1CZ1*Cos(ya))); - // rotate around X - Op1CXAR=Op1CX2; - Op1CYAR=(short)((Op1CY2*Cos(xa)+Op1CZ2*Sin(xa))); - Op1CZAR=(short)((Op1CY2*-Sin(xa)+Op1CZ2*Cos(xa))); - - #ifdef DebugDSP1 - Log_Message("OP1C Apply Matrix CX:%d CY:%d CZ",Op1CXAR,Op1CYAR,Op1CZAR); - #endif -} -*/ -#else -void DSPOp1C() -{ - double ya,xa,za; - ya = Op1CX/32768.0*PI; - xa = Op1CY/32768.0*PI; - za = Op1CZ/32768.0*PI; - - // rotate around Z - Op1CX1=(Op1CXBR*cos(za)+Op1CYBR*sin(za)); - Op1CY1=(Op1CXBR*-sin(za)+Op1CYBR*cos(za)); - Op1CZ1=Op1CZBR; - // rotate around Y - Op1CX2=(Op1CX1*cos(ya)+Op1CZ1*-sin(ya)); - Op1CY2=Op1CY1; - Op1CZ2=(Op1CX1*sin(ya)+Op1CZ1*cos(ya)); - // rotate around X - Op1CXAR=Op1CX2; - Op1CYAR=(Op1CY2*cos(xa)+Op1CZ2*sin(xa)); - Op1CZAR=(Op1CY2*-sin(xa)+Op1CZ2*cos(xa)); - - #ifdef DebugDSP1 - Log_Message("OP1C Apply Matrix CX:%d CY:%d CZ",Op1CXAR,Op1CYAR,Op1CZAR); - #endif -} - -#endif - // Op 0F = Test DSP1 RAM // Returns 0x0000 if RAM checks OK @@ -1751,3 +1504,10 @@ void DSPOp0F() #endif } +short Op2FUnknown; +short Op2FSize; + +void DSPOp2F() +{ + Op2FSize=0x100; +} diff --git a/zsnes/src/chips/dsp1proc.asm b/zsnes/src/chips/dsp1proc.asm index ee5cf0a7..aad3a2ac 100644 --- a/zsnes/src/chips/dsp1proc.asm +++ b/zsnes/src/chips/dsp1proc.asm @@ -461,9 +461,9 @@ DSP1_1C: ; 3D Coordinate Rotation mov ax,[DSP1VARS] mov [Op1CZ],ax mov ax,[DSP1VARS+2] - mov [Op1CX],ax - mov ax,[DSP1VARS+4] mov [Op1CY],ax + mov ax,[DSP1VARS+4] + mov [Op1CX],ax mov ax,[DSP1VARS+6] mov [Op1CXBR],ax mov ax,[DSP1VARS+8] @@ -638,9 +638,9 @@ DSP1_01: ; Set Attitude Matrix A mov ax,[DSP1VARS+2] mov [Op01Zr],ax mov ax,[DSP1VARS+4] - mov [Op01Xr],ax - mov ax,[DSP1VARS+6] mov [Op01Yr],ax + mov ax,[DSP1VARS+6] + mov [Op01Xr],ax pushad call DSPOp01 popad @@ -654,9 +654,9 @@ DSP1_11: ; Set Attitude Matrix B mov ax,[DSP1VARS+2] mov [Op11Zr],ax mov ax,[DSP1VARS+4] - mov [Op11Xr],ax - mov ax,[DSP1VARS+6] mov [Op11Yr],ax + mov ax,[DSP1VARS+6] + mov [Op11Xr],ax pushad call DSPOp11 popad @@ -670,9 +670,9 @@ DSP1_21: ; Set Attitude Matrix C mov ax,[DSP1VARS+2] mov [Op21Zr],ax mov ax,[DSP1VARS+4] - mov [Op21Xr],ax - mov ax,[DSP1VARS+6] mov [Op21Yr],ax + mov ax,[DSP1VARS+6] + mov [Op21Xr],ax pushad call DSPOp21 popad