~ubuntu-branches/ubuntu/intrepid/zsnes/intrepid-proposed

« back to all changes in this revision

Viewing changes to src/chips/dsp3emu.c

  • Committer: Bazaar Package Importer
  • Author(s): Joshua Kwan
  • Date: 2007-06-04 21:46:47 UTC
  • mfrom: (1.2.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20070604214647-j5zowa8vplkug0yj
Tags: 1.510-1
* New upstream release - all patches merged. closes: #380734, #419270
* Add a .desktop file, thanks Nicholas Wheeler. closes: #367942
* Include a ton of documentation that ships with the tarball.
  closes: #392143 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (C) 1997-2007 ZSNES Team ( zsKnight, _Demo_, pagefault, Nach )
 
3
 
 
4
http://www.zsnes.com
 
5
http://sourceforge.net/projects/zsnes
 
6
https://zsnes.bountysource.com
 
7
 
 
8
This program is free software; you can redistribute it and/or
 
9
modify it under the terms of the GNU General Public License
 
10
version 2 as published by the Free Software Foundation.
 
11
 
 
12
This program is distributed in the hope that it will be useful,
 
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
GNU General Public License for more details.
 
16
 
 
17
You should have received a copy of the GNU General Public License
 
18
along with this program; if not, write to the Free Software
 
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
20
*/
 
21
 
 
22
typedef unsigned char bool8;
 
23
typedef unsigned char uint8;
 
24
typedef unsigned short uint16;
 
25
typedef unsigned int uint32;
 
26
typedef char int8;
 
27
typedef short int16;
 
28
typedef long int32;
 
29
 
 
30
//C++ in C
 
31
typedef unsigned char bool;
 
32
#define true 1
 
33
#define false 0
 
34
 
 
35
uint16 DSP3_DataROM[1024] = {
 
36
        0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100,
 
37
        0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001,
 
38
        0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,
 
39
        0x0000, 0x000f, 0x0400, 0x0200, 0x0140, 0x0400, 0x0200, 0x0040,
 
40
        0x007d, 0x007e, 0x007e, 0x007b, 0x007c, 0x007d, 0x007b, 0x007c,
 
41
        0x0002, 0x0020, 0x0030, 0x0000, 0x000d, 0x0019, 0x0026, 0x0032,
 
42
        0x003e, 0x004a, 0x0056, 0x0062, 0x006d, 0x0079, 0x0084, 0x008e,
 
43
        0x0098, 0x00a2, 0x00ac, 0x00b5, 0x00be, 0x00c6, 0x00ce, 0x00d5,
 
44
        0x00dc, 0x00e2, 0x00e7, 0x00ec, 0x00f1, 0x00f5, 0x00f8, 0x00fb,
 
45
        0x00fd, 0x00ff, 0x0100, 0x0100, 0x0100, 0x00ff, 0x00fd, 0x00fb,
 
46
        0x00f8, 0x00f5, 0x00f1, 0x00ed, 0x00e7, 0x00e2, 0x00dc, 0x00d5,
 
47
        0x00ce, 0x00c6, 0x00be, 0x00b5, 0x00ac, 0x00a2, 0x0099, 0x008e,
 
48
        0x0084, 0x0079, 0x006e, 0x0062, 0x0056, 0x004a, 0x003e, 0x0032,
 
49
        0x0026, 0x0019, 0x000d, 0x0000, 0xfff3, 0xffe7, 0xffdb, 0xffce,
 
50
        0xffc2, 0xffb6, 0xffaa, 0xff9e, 0xff93, 0xff87, 0xff7d, 0xff72,
 
51
        0xff68, 0xff5e, 0xff54, 0xff4b, 0xff42, 0xff3a, 0xff32, 0xff2b,
 
52
        0xff25, 0xff1e, 0xff19, 0xff14, 0xff0f, 0xff0b, 0xff08, 0xff05,
 
53
        0xff03, 0xff01, 0xff00, 0xff00, 0xff00, 0xff01, 0xff03, 0xff05,
 
54
        0xff08, 0xff0b, 0xff0f, 0xff13, 0xff18, 0xff1e, 0xff24, 0xff2b,
 
55
        0xff32, 0xff3a, 0xff42, 0xff4b, 0xff54, 0xff5d, 0xff67, 0xff72,
 
56
        0xff7c, 0xff87, 0xff92, 0xff9e, 0xffa9, 0xffb5, 0xffc2, 0xffce,
 
57
        0xffda, 0xffe7, 0xfff3, 0x002b, 0x007f, 0x0020, 0x00ff, 0xff00,
 
58
        0xffbe, 0x0000, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
59
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
60
        0x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0x0045,
 
61
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
62
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
63
        0xffc5, 0x0003, 0x0004, 0x0005, 0x0047, 0x0000, 0x0000, 0x0000,
 
64
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
65
        0x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,
 
66
        0x0009, 0x004a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
67
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
68
        0xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x004e, 0x0000,
 
69
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
70
        0x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,
 
71
        0x0012, 0x0013, 0x0014, 0x0053, 0x0000, 0x0000, 0x0000, 0x0000,
 
72
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
73
        0xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,
 
74
        0x0059, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
75
        0x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,
 
76
        0x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0x0060, 0x0000, 0x0000,
 
77
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
78
        0xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,
 
79
        0x002b, 0x002c, 0x0068, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
80
        0x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,
 
81
        0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0071,
 
82
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
83
        0xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
 
84
        0x003e, 0x003f, 0x0040, 0x0041, 0x007b, 0x0000, 0x0000, 0x0000,
 
85
        0x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,
 
86
        0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
 
87
        0x000b, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
88
        0xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,
 
89
        0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0050, 0x0000,
 
90
        0x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,
 
91
        0x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,
 
92
        0x0024, 0x0025, 0x0026, 0x005d, 0x0000, 0x0000, 0x0000, 0x0000,
 
93
        0xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,
 
94
        0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
 
95
        0x006b, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,
 
96
        0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
 
97
        0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0044, 0x0000, 0x0000,
 
98
        0xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
 
99
        0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,
 
100
        0x001f, 0x0020, 0x0054, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,
 
101
        0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,
 
102
        0x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0065,
 
103
        0xffbe, 0x0000, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
104
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
105
        0x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0xfead,
 
106
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
107
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
108
        0xffc5, 0x0003, 0x0004, 0x0005, 0xfeaf, 0x0000, 0x0000, 0x0000,
 
109
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
110
        0x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,
 
111
        0x0009, 0xfeb2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
112
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
113
        0xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0xfeb6, 0x0000,
 
114
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
115
        0x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,
 
116
        0x0012, 0x0013, 0x0014, 0xfebb, 0x0000, 0x0000, 0x0000, 0x0000,
 
117
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
118
        0xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,
 
119
        0xfec1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
120
        0x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,
 
121
        0x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0xfec8, 0x0000, 0x0000,
 
122
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
123
        0xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,
 
124
        0x002b, 0x002c, 0xfed0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
125
        0x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,
 
126
        0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0xfed9,
 
127
        0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
128
        0xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
 
129
        0x003e, 0x003f, 0x0040, 0x0041, 0xfee3, 0x0000, 0x0000, 0x0000,
 
130
        0x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,
 
131
        0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
 
132
        0x000b, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
 
133
        0xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,
 
134
        0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0xfeb8, 0x0000,
 
135
        0x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,
 
136
        0x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,
 
137
        0x0024, 0x0025, 0x0026, 0xfec5, 0x0000, 0x0000, 0x0000, 0x0000,
 
138
        0xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,
 
139
        0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
 
140
        0xfed3, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,
 
141
        0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
 
142
        0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0xfeac, 0x0000, 0x0000,
 
143
        0xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
 
144
        0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,
 
145
        0x001f, 0x0020, 0xfebc, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,
 
146
        0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,
 
147
        0x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0xfecd,
 
148
        0x0154, 0x0218, 0x0110, 0x00b0, 0x00cc, 0x00b0, 0x0088, 0x00b0,
 
149
        0x0044, 0x00b0, 0x0000, 0x00b0, 0x00fe, 0xff07, 0x0002, 0x00ff,
 
150
        0x00f8, 0x0007, 0x00fe, 0x00ee, 0x07ff, 0x0200, 0x00ef, 0xf800,
 
151
        0x0700, 0x00ee, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0001,
 
152
        0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
 
153
        0xffff, 0x0000, 0x0000, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000,
 
154
        0x0000, 0xffff, 0xffff, 0x0000, 0xffff, 0x0001, 0x0000, 0x0001,
 
155
        0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000,
 
156
        0xffff, 0x0001, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff,
 
157
        0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0044, 0x0088, 0x00cc,
 
158
        0x0110, 0x0154, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
 
159
        0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
 
160
        0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
 
161
        0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
 
162
        0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
 
163
        0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
 
164
};
 
165
 
 
166
void (*SetDSP3)();
 
167
void DSP3_Command();
 
168
 
 
169
uint16 DSP3_DR;
 
170
uint16 DSP3_SR;
 
171
uint16 DSP3_MemoryIndex;
 
172
 
 
173
void DSP3_Reset()
 
174
{
 
175
  DSP3_DR = 0x0080;
 
176
  DSP3_SR = 0x0084;
 
177
  SetDSP3 = &DSP3_Command;
 
178
}
 
179
 
 
180
void DSP3_MemorySize()
 
181
{
 
182
        DSP3_DR = 0x0300;
 
183
        SetDSP3 = &DSP3_Reset;
 
184
}
 
185
 
 
186
void DSP3_TestMemory()
 
187
{
 
188
        DSP3_DR = 0x0000;
 
189
        SetDSP3 = &DSP3_Reset;
 
190
}
 
191
 
 
192
void DSP3_DumpDataROM()
 
193
{
 
194
        DSP3_DR = DSP3_DataROM[DSP3_MemoryIndex++];
 
195
        if (DSP3_MemoryIndex == 1024)
 
196
                SetDSP3 = &DSP3_Reset;
 
197
}
 
198
 
 
199
void DSP3_MemoryDump()
 
200
{
 
201
        DSP3_MemoryIndex = 0;
 
202
        SetDSP3 = &DSP3_DumpDataROM;
 
203
        DSP3_DumpDataROM();
 
204
}
 
205
 
 
206
int16 DSP3_WinLo;
 
207
int16 DSP3_WinHi;
 
208
 
 
209
void DSP3_OP06()
 
210
{
 
211
        DSP3_WinLo = (uint8)(DSP3_DR);
 
212
        DSP3_WinHi = (uint8)(DSP3_DR >> 8);
 
213
        DSP3_Reset();
 
214
}
 
215
 
 
216
void DSP3_OP03()
 
217
{
 
218
        int16 Lo = (uint8)(DSP3_DR);
 
219
        int16 Hi = (uint8)(DSP3_DR >> 8);
 
220
        int16 Ofs = (DSP3_WinLo * Hi << 1) + (Lo << 1);
 
221
        DSP3_DR = Ofs >> 1;
 
222
        SetDSP3 = &DSP3_Reset;
 
223
}
 
224
 
 
225
int16 DSP3_AddLo;
 
226
int16 DSP3_AddHi;
 
227
 
 
228
void DSP3_OP07_B()
 
229
{
 
230
        int16 Ofs = (DSP3_WinLo * DSP3_AddHi << 1) + (DSP3_AddLo << 1);
 
231
        DSP3_DR = Ofs >> 1;
 
232
        SetDSP3 = &DSP3_Reset;
 
233
}
 
234
 
 
235
void DSP3_OP07_A()
 
236
{
 
237
        int16 Lo = (uint8)(DSP3_DR);
 
238
        int16 Hi = (uint8)(DSP3_DR >> 8);
 
239
 
 
240
        if (Lo & 1)     Hi += (DSP3_AddLo & 1);
 
241
 
 
242
        DSP3_AddLo += Lo;
 
243
        DSP3_AddHi += Hi;
 
244
 
 
245
        if (DSP3_AddLo < 0)
 
246
                DSP3_AddLo += DSP3_WinLo;
 
247
        else
 
248
                if (DSP3_AddLo >= DSP3_WinLo)
 
249
                        DSP3_AddLo -= DSP3_WinLo;
 
250
 
 
251
        if (DSP3_AddHi < 0)
 
252
                DSP3_AddHi += DSP3_WinHi;
 
253
        else
 
254
                if (DSP3_AddHi >= DSP3_WinHi)
 
255
                        DSP3_AddHi -= DSP3_WinHi;
 
256
 
 
257
        DSP3_DR = DSP3_AddLo | (DSP3_AddHi << 8) | ((DSP3_AddHi >> 8) & 0xff);
 
258
        SetDSP3 = &DSP3_OP07_B;
 
259
}
 
260
 
 
261
void DSP3_OP07()
 
262
{
 
263
        uint32 dataOfs = ((DSP3_DR << 1) + 0x03b2) & 0x03ff;
 
264
 
 
265
        DSP3_AddHi = DSP3_DataROM[dataOfs];
 
266
        DSP3_AddLo = DSP3_DataROM[dataOfs + 1];
 
267
 
 
268
        SetDSP3 = &DSP3_OP07_A;
 
269
        DSP3_SR = 0x0080;
 
270
}
 
271
 
 
272
uint16 DSP3_Codewords;
 
273
uint16 DSP3_Outwords;
 
274
uint16 DSP3_Symbol;
 
275
uint16 DSP3_BitCount;
 
276
uint16 DSP3_Index;
 
277
uint16 DSP3_Codes[512];
 
278
uint16 DSP3_BitsLeft;
 
279
uint16 DSP3_ReqBits;
 
280
uint16 DSP3_ReqData;
 
281
uint16 DSP3_BitCommand;
 
282
uint8  DSP3_BaseLength;
 
283
uint16 DSP3_BaseCodes;
 
284
uint16 DSP3_BaseCode;
 
285
uint8  DSP3_CodeLengths[8];
 
286
uint16 DSP3_CodeOffsets[8];
 
287
uint16 DSP3_LZCode;
 
288
uint8  DSP3_LZLength;
 
289
 
 
290
uint16 DSP3_X;
 
291
uint16 DSP3_Y;
 
292
 
 
293
void DSP3_Coordinate()
 
294
{
 
295
        DSP3_Index++;
 
296
 
 
297
        switch (DSP3_Index)
 
298
        {
 
299
        case 3:
 
300
                {
 
301
                        if (DSP3_DR == 0xffff)
 
302
                                DSP3_Reset();
 
303
                        break;
 
304
                }
 
305
        case 4:
 
306
                {
 
307
                        DSP3_X = DSP3_DR;
 
308
                        break;
 
309
                }
 
310
        case 5:
 
311
                {
 
312
                        DSP3_Y = DSP3_DR;
 
313
                        DSP3_DR = 1;
 
314
                        break;
 
315
                }
 
316
        case 6:
 
317
                {
 
318
                        DSP3_DR = DSP3_X;
 
319
                        break;
 
320
                }
 
321
        case 7:
 
322
                {
 
323
                        DSP3_DR = DSP3_Y;
 
324
                        DSP3_Index = 0;
 
325
                        break;
 
326
                }
 
327
        }
 
328
}
 
329
 
 
330
uint8  DSP3_Bitmap[8];
 
331
uint8  DSP3_Bitplane[8];
 
332
uint16 DSP3_BMIndex;
 
333
uint16 DSP3_BPIndex;
 
334
uint16 DSP3_Count;
 
335
 
 
336
void DSP3_Convert_A()
 
337
{
 
338
        if (DSP3_BMIndex < 8)
 
339
        {
 
340
                DSP3_Bitmap[DSP3_BMIndex++] = (uint8) (DSP3_DR);
 
341
                DSP3_Bitmap[DSP3_BMIndex++] = (uint8) (DSP3_DR >> 8);
 
342
 
 
343
                if (DSP3_BMIndex == 8)
 
344
                {
 
345
      short i, j;
 
346
                        for (i=0; i < 8; i++)
 
347
                                for (j=0; j < 8; j++)
 
348
                                {
 
349
                                        DSP3_Bitplane[j] <<= 1;
 
350
                                        DSP3_Bitplane[j] |= (DSP3_Bitmap[i] >> j) & 1;
 
351
                                }
 
352
 
 
353
                        DSP3_BPIndex = 0;
 
354
                        DSP3_Count--;
 
355
                }
 
356
        }
 
357
 
 
358
        if (DSP3_BMIndex == 8)
 
359
        {
 
360
                if (DSP3_BPIndex == 8)
 
361
                {
 
362
                        if (!DSP3_Count) DSP3_Reset();
 
363
                        DSP3_BMIndex = 0;
 
364
                }
 
365
                else
 
366
                {
 
367
                        DSP3_DR = DSP3_Bitplane[DSP3_BPIndex++];
 
368
                        DSP3_DR |= DSP3_Bitplane[DSP3_BPIndex++] << 8;
 
369
                }
 
370
        }
 
371
}
 
372
 
 
373
void DSP3_Convert()
 
374
{
 
375
        DSP3_Count = DSP3_DR;
 
376
        DSP3_BMIndex = 0;
 
377
        SetDSP3 = &DSP3_Convert_A;
 
378
}
 
379
 
 
380
bool DSP3_GetBits(uint8 Count)
 
381
{
 
382
        if (!DSP3_BitsLeft)
 
383
        {
 
384
                DSP3_BitsLeft = Count;
 
385
                DSP3_ReqBits = 0;
 
386
        }
 
387
 
 
388
        do {
 
389
                if (!DSP3_BitCount)
 
390
                {
 
391
                        DSP3_SR = 0xC0;
 
392
                        return false;
 
393
                }
 
394
 
 
395
                DSP3_ReqBits <<= 1;
 
396
                if (DSP3_ReqData & 0x8000) DSP3_ReqBits++;
 
397
                DSP3_ReqData <<= 1;
 
398
 
 
399
                DSP3_BitCount--;
 
400
                DSP3_BitsLeft--;
 
401
 
 
402
        } while (DSP3_BitsLeft);
 
403
 
 
404
        return true;
 
405
}
 
406
 
 
407
void DSP3_Decode_Data()
 
408
{
 
409
        if (!DSP3_BitCount)
 
410
        {
 
411
                if (DSP3_SR & 0x40)
 
412
                {
 
413
                        DSP3_ReqData = DSP3_DR;
 
414
                        DSP3_BitCount += 16;
 
415
                }
 
416
                else
 
417
                {
 
418
                        DSP3_SR = 0xC0;
 
419
                        return;
 
420
                }
 
421
        }
 
422
 
 
423
        if (DSP3_LZCode == 1)
 
424
        {
 
425
                if (!DSP3_GetBits(1))
 
426
                        return;
 
427
 
 
428
                if (DSP3_ReqBits)
 
429
                        DSP3_LZLength = 12;
 
430
                else
 
431
                        DSP3_LZLength = 8;
 
432
 
 
433
                DSP3_LZCode++;
 
434
        }
 
435
 
 
436
        if (DSP3_LZCode == 2)
 
437
        {
 
438
                if (!DSP3_GetBits(DSP3_LZLength))
 
439
                        return;
 
440
 
 
441
                DSP3_LZCode = 0;
 
442
                DSP3_Outwords--;
 
443
                if (!DSP3_Outwords) SetDSP3 = &DSP3_Reset;
 
444
 
 
445
                DSP3_SR = 0x80;
 
446
                DSP3_DR = DSP3_ReqBits;
 
447
                return;
 
448
        }
 
449
 
 
450
        if (DSP3_BaseCode == 0xffff)
 
451
        {
 
452
                if (!DSP3_GetBits(DSP3_BaseLength))
 
453
                        return;
 
454
 
 
455
                DSP3_BaseCode = DSP3_ReqBits;
 
456
        }
 
457
 
 
458
        if (!DSP3_GetBits(DSP3_CodeLengths[DSP3_BaseCode]))
 
459
                return;
 
460
 
 
461
        DSP3_Symbol = DSP3_Codes[DSP3_CodeOffsets[DSP3_BaseCode] + DSP3_ReqBits];
 
462
        DSP3_BaseCode = 0xffff;
 
463
 
 
464
        if (DSP3_Symbol & 0xff00)
 
465
        {
 
466
                DSP3_Symbol += 0x7f02;
 
467
                DSP3_LZCode++;
 
468
        }
 
469
        else
 
470
        {
 
471
                DSP3_Outwords--;
 
472
                if (!DSP3_Outwords)
 
473
                        SetDSP3 = &DSP3_Reset;
 
474
        }
 
475
 
 
476
        DSP3_SR = 0x80;
 
477
        DSP3_DR = DSP3_Symbol;
 
478
}
 
479
 
 
480
void DSP3_Decode_Tree()
 
481
{
 
482
        if (!DSP3_BitCount)
 
483
        {
 
484
                DSP3_ReqData = DSP3_DR;
 
485
                DSP3_BitCount += 16;
 
486
        }
 
487
 
 
488
        if (!DSP3_BaseCodes)
 
489
        {
 
490
                DSP3_GetBits(1);
 
491
                if (DSP3_ReqBits)
 
492
                {
 
493
                        DSP3_BaseLength = 3;
 
494
                        DSP3_BaseCodes = 8;
 
495
                }
 
496
                else
 
497
                {
 
498
                        DSP3_BaseLength = 2;
 
499
                        DSP3_BaseCodes = 4;
 
500
                }
 
501
        }
 
502
 
 
503
        while (DSP3_BaseCodes)
 
504
        {
 
505
                if (!DSP3_GetBits(3))
 
506
                        return;
 
507
 
 
508
                DSP3_ReqBits++;
 
509
 
 
510
                DSP3_CodeLengths[DSP3_Index] = (uint8) DSP3_ReqBits;
 
511
                DSP3_CodeOffsets[DSP3_Index] = DSP3_Symbol;
 
512
                DSP3_Index++;
 
513
 
 
514
                DSP3_Symbol += 1 << DSP3_ReqBits;
 
515
                DSP3_BaseCodes--;
 
516
        }
 
517
 
 
518
        DSP3_BaseCode = 0xffff;
 
519
        DSP3_LZCode = 0;
 
520
 
 
521
        SetDSP3 = &DSP3_Decode_Data;
 
522
        if (DSP3_BitCount) DSP3_Decode_Data();
 
523
}
 
524
 
 
525
void DSP3_Decode_Symbols()
 
526
{
 
527
        DSP3_ReqData = DSP3_DR;
 
528
        DSP3_BitCount += 16;
 
529
 
 
530
        do {
 
531
 
 
532
                if (DSP3_BitCommand == 0xffff)
 
533
                {
 
534
                        if (!DSP3_GetBits(2)) return;
 
535
                        DSP3_BitCommand = DSP3_ReqBits;
 
536
                }
 
537
 
 
538
                switch (DSP3_BitCommand)
 
539
                {
 
540
                case 0:
 
541
                        {
 
542
                                if (!DSP3_GetBits(9)) return;
 
543
                                DSP3_Symbol = DSP3_ReqBits;
 
544
                                break;
 
545
                        }
 
546
                case 1:
 
547
                        {
 
548
                                DSP3_Symbol++;
 
549
                                break;
 
550
                        }
 
551
                case 2:
 
552
                        {
 
553
                                if (!DSP3_GetBits(1)) return;
 
554
                                DSP3_Symbol += 2 + DSP3_ReqBits;
 
555
                                break;
 
556
                        }
 
557
                case 3:
 
558
                        {
 
559
                                if (!DSP3_GetBits(4)) return;
 
560
                                DSP3_Symbol += 4 + DSP3_ReqBits;
 
561
                                break;
 
562
                        }
 
563
                }
 
564
 
 
565
                DSP3_BitCommand = 0xffff;
 
566
 
 
567
                DSP3_Codes[DSP3_Index++] = DSP3_Symbol;
 
568
                DSP3_Codewords--;
 
569
 
 
570
        } while (DSP3_Codewords);
 
571
 
 
572
        DSP3_Index = 0;
 
573
        DSP3_Symbol = 0;
 
574
        DSP3_BaseCodes = 0;
 
575
 
 
576
        SetDSP3 = &DSP3_Decode_Tree;
 
577
        if (DSP3_BitCount) DSP3_Decode_Tree();
 
578
}
 
579
 
 
580
void DSP3_Decode_A()
 
581
{
 
582
        DSP3_Outwords = DSP3_DR;
 
583
        SetDSP3 = &DSP3_Decode_Symbols;
 
584
        DSP3_BitCount = 0;
 
585
        DSP3_BitsLeft = 0;
 
586
        DSP3_Symbol = 0;
 
587
        DSP3_Index = 0;
 
588
        DSP3_BitCommand = 0xffff;
 
589
        DSP3_SR = 0xC0;
 
590
}
 
591
 
 
592
void DSP3_Decode()
 
593
{
 
594
        DSP3_Codewords = DSP3_DR;
 
595
        SetDSP3 = &DSP3_Decode_A;
 
596
}
 
597
 
 
598
 
 
599
// Opcodes 1E/3E bit-perfect to 'dsp3-intro' log
 
600
// src: adapted from SD Gundam X/G-Next
 
601
 
 
602
int16 op3e_x;
 
603
int16 op3e_y;
 
604
 
 
605
int16 op1e_terrain[0x2000];
 
606
int16 op1e_cost[0x2000];
 
607
int16 op1e_weight[0x2000];
 
608
 
 
609
int16 op1e_cell;
 
610
int16 op1e_turn;
 
611
int16 op1e_search;
 
612
 
 
613
int16 op1e_x;
 
614
int16 op1e_y;
 
615
 
 
616
int16 op1e_min_radius;
 
617
int16 op1e_max_radius;
 
618
 
 
619
int16 op1e_max_search_radius;
 
620
int16 op1e_max_path_radius;
 
621
 
 
622
int16 op1e_lcv_radius;
 
623
int16 op1e_lcv_steps;
 
624
int16 op1e_lcv_turns;
 
625
 
 
626
void DSP3_OP3E()
 
627
{
 
628
        op3e_x = (uint8)(DSP3_DR & 0x00ff);
 
629
        op3e_y = (uint8)((DSP3_DR & 0xff00)>>8);
 
630
 
 
631
        DSP3_OP03();
 
632
 
 
633
        op1e_terrain[ DSP3_DR ] = 0x00;
 
634
        op1e_cost[ DSP3_DR ] = 0xff;
 
635
        op1e_weight[ DSP3_DR ] = 0;
 
636
 
 
637
        op1e_max_search_radius = 0;
 
638
        op1e_max_path_radius = 0;
 
639
}
 
640
 
 
641
void DSP3_OP1E_A();
 
642
void DSP3_OP1E_A1();
 
643
void DSP3_OP1E_A2();
 
644
void DSP3_OP1E_A3();
 
645
 
 
646
void DSP3_OP1E_B();
 
647
void DSP3_OP1E_B1();
 
648
void DSP3_OP1E_B2();
 
649
 
 
650
void DSP3_OP1E_C();
 
651
void DSP3_OP1E_C1();
 
652
void DSP3_OP1E_C2();
 
653
 
 
654
void DSP3_OP1E_D( int16, int16 *, int16 * );
 
655
void DSP3_OP1E_D1( int16 move, int16 *lo, int16 *hi );
 
656
 
 
657
void DSP3_OP1E()
 
658
{
 
659
        int lcv;
 
660
 
 
661
        op1e_min_radius = (uint8)(DSP3_DR & 0x00ff);
 
662
        op1e_max_radius = (uint8)((DSP3_DR & 0xff00)>>8);
 
663
 
 
664
        if( op1e_min_radius == 0 )
 
665
                op1e_min_radius++;
 
666
 
 
667
        if( op1e_max_search_radius >= op1e_min_radius )
 
668
                op1e_min_radius = op1e_max_search_radius+1;
 
669
 
 
670
        if( op1e_max_radius > op1e_max_search_radius )
 
671
                op1e_max_search_radius = op1e_max_radius;
 
672
 
 
673
        op1e_lcv_radius = op1e_min_radius;
 
674
        op1e_lcv_steps = op1e_min_radius;
 
675
 
 
676
        op1e_lcv_turns = 6;
 
677
        op1e_turn = 0;
 
678
 
 
679
        op1e_x = op3e_x;
 
680
        op1e_y = op3e_y;
 
681
 
 
682
        for( lcv = 0; lcv < op1e_min_radius; lcv++ )
 
683
                DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
 
684
 
 
685
        DSP3_OP1E_A();
 
686
}
 
687
 
 
688
void DSP3_OP1E_A()
 
689
{
 
690
        int lcv;
 
691
 
 
692
        if( op1e_lcv_steps == 0 ) {
 
693
                op1e_lcv_radius++;
 
694
 
 
695
                op1e_lcv_steps = op1e_lcv_radius;
 
696
 
 
697
                op1e_x = op3e_x;
 
698
                op1e_y = op3e_y;
 
699
 
 
700
                for( lcv = 0; lcv < op1e_lcv_radius; lcv++ )
 
701
                        DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
 
702
        }
 
703
 
 
704
        if( op1e_lcv_radius > op1e_max_radius ) {
 
705
                op1e_turn++;
 
706
                op1e_lcv_turns--;
 
707
 
 
708
                op1e_lcv_radius = op1e_min_radius;
 
709
                op1e_lcv_steps = op1e_min_radius;
 
710
 
 
711
                op1e_x = op3e_x;
 
712
                op1e_y = op3e_y;
 
713
 
 
714
                for( lcv = 0; lcv < op1e_min_radius; lcv++ )
 
715
                        DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
 
716
        }
 
717
 
 
718
        if( op1e_lcv_turns == 0 ) {
 
719
                DSP3_DR = 0xffff;
 
720
                DSP3_SR = 0x0080;
 
721
                SetDSP3 = &DSP3_OP1E_B;
 
722
                return;
 
723
        }
 
724
 
 
725
        DSP3_DR = (uint8)(op1e_x) | ((uint8)(op1e_y)<<8);
 
726
        DSP3_OP03();
 
727
 
 
728
        op1e_cell = DSP3_DR;
 
729
 
 
730
        DSP3_SR = 0x0080;
 
731
        SetDSP3 = &DSP3_OP1E_A1;
 
732
}
 
733
 
 
734
void DSP3_OP1E_A1()
 
735
{
 
736
        DSP3_SR = 0x0084;
 
737
        SetDSP3 = &DSP3_OP1E_A2;
 
738
}
 
739
 
 
740
void DSP3_OP1E_A2()
 
741
{
 
742
        op1e_terrain[ op1e_cell ] = (uint8)(DSP3_DR & 0x00ff);
 
743
 
 
744
        DSP3_SR = 0x0084;
 
745
        SetDSP3 = &DSP3_OP1E_A3;
 
746
}
 
747
 
 
748
void DSP3_OP1E_A3()
 
749
{
 
750
        op1e_cost[ op1e_cell ] = (uint8)(DSP3_DR & 0x00ff);
 
751
 
 
752
        if( op1e_lcv_radius == 1 ) {
 
753
                if( op1e_terrain[ op1e_cell ] & 1 ) {
 
754
                        op1e_weight[ op1e_cell ] = 0xff;
 
755
                } else {
 
756
                        op1e_weight[ op1e_cell ] = op1e_cost[ op1e_cell ];
 
757
                }
 
758
        }
 
759
        else {
 
760
                op1e_weight[ op1e_cell ] = 0xff;
 
761
        }
 
762
 
 
763
        DSP3_OP1E_D( (int16)(op1e_turn+2), &op1e_x, &op1e_y );
 
764
        op1e_lcv_steps--;
 
765
 
 
766
        DSP3_SR = 0x0080;
 
767
        DSP3_OP1E_A();
 
768
}
 
769
 
 
770
 
 
771
void DSP3_OP1E_B()
 
772
{
 
773
        op1e_x = op3e_x;
 
774
        op1e_y = op3e_y;
 
775
        op1e_lcv_radius = 1;
 
776
 
 
777
        op1e_search = 0;
 
778
 
 
779
        DSP3_OP1E_B1();
 
780
 
 
781
        SetDSP3 = &DSP3_OP1E_C;
 
782
}
 
783
 
 
784
 
 
785
void DSP3_OP1E_B1()
 
786
{
 
787
        while( op1e_lcv_radius < op1e_max_radius ) {
 
788
                op1e_y--;
 
789
 
 
790
                op1e_lcv_turns = 6;
 
791
                op1e_turn = 5;
 
792
 
 
793
                while( op1e_lcv_turns ) {
 
794
                        op1e_lcv_steps = op1e_lcv_radius;
 
795
 
 
796
                        while( op1e_lcv_steps ) {
 
797
                                DSP3_OP1E_D1( op1e_turn, &op1e_x, &op1e_y );
 
798
 
 
799
                                if( 0 <= op1e_y && op1e_y < DSP3_WinHi &&
 
800
                                                0 <= op1e_x && op1e_x < DSP3_WinLo ) {
 
801
                                        DSP3_DR = (uint8)(op1e_x) | ((uint8)(op1e_y)<<8);
 
802
                                        DSP3_OP03();
 
803
 
 
804
                                        op1e_cell = DSP3_DR;
 
805
                                        if( op1e_cost[ op1e_cell ] < 0x80 &&
 
806
                                                        op1e_terrain[ op1e_cell ] < 0x40 ) {
 
807
                                                DSP3_OP1E_B2();
 
808
                                        } // end cell perimeter
 
809
                                }
 
810
 
 
811
                                op1e_lcv_steps--;
 
812
                        } // end search line
 
813
 
 
814
                        op1e_turn--;
 
815
                        if( op1e_turn == 0 ) op1e_turn = 6;
 
816
 
 
817
                        op1e_lcv_turns--;
 
818
                } // end circle search
 
819
 
 
820
                op1e_lcv_radius++;
 
821
        } // end radius search
 
822
}
 
823
 
 
824
 
 
825
void DSP3_OP1E_B2()
 
826
{
 
827
        int16 cell;
 
828
        int16 path;
 
829
        int16 x,y;
 
830
        int16 lcv_turns;
 
831
 
 
832
        path = 0xff;
 
833
        lcv_turns = 6;
 
834
 
 
835
        while( lcv_turns ) {
 
836
                x = op1e_x;
 
837
                y = op1e_y;
 
838
 
 
839
                DSP3_OP1E_D1( lcv_turns, &x, &y );
 
840
 
 
841
                DSP3_DR = (uint8)(x) | ((uint8)(y)<<8);
 
842
                DSP3_OP03();
 
843
 
 
844
                cell = DSP3_DR;
 
845
 
 
846
                if( 0 <= y && y < DSP3_WinHi &&
 
847
                                0 <= x && x < DSP3_WinLo  ) {
 
848
 
 
849
                        if( op1e_terrain[ cell ] < 0x80 || op1e_weight[ cell ] == 0 ) {
 
850
                                if( op1e_weight[ cell ] < path ) {
 
851
                                        path = op1e_weight[ cell ];
 
852
                                }
 
853
                        }
 
854
                } // end step travel
 
855
 
 
856
                lcv_turns--;
 
857
        } // end while turns
 
858
 
 
859
        if( path != 0xff ) {
 
860
                op1e_weight[ op1e_cell ] = path + op1e_cost[ op1e_cell ];
 
861
        }
 
862
}
 
863
 
 
864
 
 
865
void DSP3_OP1E_C()
 
866
{
 
867
        int lcv;
 
868
 
 
869
        op1e_min_radius = (uint8)(DSP3_DR & 0x00ff);
 
870
        op1e_max_radius = (uint8)((DSP3_DR & 0xff00)>>8);
 
871
 
 
872
        if( op1e_min_radius == 0 )
 
873
                op1e_min_radius++;
 
874
 
 
875
        if( op1e_max_path_radius >= op1e_min_radius )
 
876
                op1e_min_radius = op1e_max_path_radius+1;
 
877
 
 
878
        if( op1e_max_radius > op1e_max_path_radius )
 
879
                op1e_max_path_radius = op1e_max_radius;
 
880
 
 
881
        op1e_lcv_radius = op1e_min_radius;
 
882
        op1e_lcv_steps = op1e_min_radius;
 
883
 
 
884
        op1e_lcv_turns = 6;
 
885
        op1e_turn = 0;
 
886
 
 
887
        op1e_x = op3e_x;
 
888
        op1e_y = op3e_y;
 
889
 
 
890
        for( lcv = 0; lcv < op1e_min_radius; lcv++ )
 
891
                DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
 
892
 
 
893
        DSP3_OP1E_C1();
 
894
}
 
895
 
 
896
 
 
897
void DSP3_OP1E_C1()
 
898
{
 
899
        int lcv;
 
900
 
 
901
        if( op1e_lcv_steps == 0 ) {
 
902
                op1e_lcv_radius++;
 
903
 
 
904
                op1e_lcv_steps = op1e_lcv_radius;
 
905
 
 
906
                op1e_x = op3e_x;
 
907
                op1e_y = op3e_y;
 
908
 
 
909
                for( lcv = 0; lcv < op1e_lcv_radius; lcv++ )
 
910
                        DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
 
911
        }
 
912
 
 
913
        if( op1e_lcv_radius > op1e_max_radius ) {
 
914
                op1e_turn++;
 
915
                op1e_lcv_turns--;
 
916
 
 
917
                op1e_lcv_radius = op1e_min_radius;
 
918
                op1e_lcv_steps = op1e_min_radius;
 
919
 
 
920
                op1e_x = op3e_x;
 
921
                op1e_y = op3e_y;
 
922
 
 
923
                for( lcv = 0; lcv < op1e_min_radius; lcv++ )
 
924
                        DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
 
925
        }
 
926
 
 
927
        if( op1e_lcv_turns == 0 ) {
 
928
                DSP3_DR = 0xffff;
 
929
                DSP3_SR = 0x0080;
 
930
                SetDSP3 = &DSP3_Reset;
 
931
                return;
 
932
        }
 
933
 
 
934
        DSP3_DR = (uint8)(op1e_x) | ((uint8)(op1e_y)<<8);
 
935
        DSP3_OP03();
 
936
 
 
937
        op1e_cell = DSP3_DR;
 
938
 
 
939
        DSP3_SR = 0x0080;
 
940
        SetDSP3 = &DSP3_OP1E_C2;
 
941
}
 
942
 
 
943
 
 
944
void DSP3_OP1E_C2()
 
945
{
 
946
        DSP3_DR = op1e_weight[ op1e_cell ];
 
947
 
 
948
        DSP3_OP1E_D( (int16)(op1e_turn+2), &op1e_x, &op1e_y );
 
949
        op1e_lcv_steps--;
 
950
 
 
951
        DSP3_SR = 0x0084;
 
952
        SetDSP3 = &DSP3_OP1E_C1;
 
953
}
 
954
 
 
955
 
 
956
void DSP3_OP1E_D( int16 move, int16 *lo, int16 *hi )
 
957
{
 
958
        uint32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
 
959
        int16 Lo;
 
960
        int16 Hi;
 
961
 
 
962
        DSP3_AddHi = DSP3_DataROM[dataOfs];
 
963
        DSP3_AddLo = DSP3_DataROM[dataOfs + 1];
 
964
 
 
965
        Lo = (uint8)(*lo);
 
966
        Hi = (uint8)(*hi);
 
967
 
 
968
        if (Lo & 1)     Hi += (DSP3_AddLo & 1);
 
969
 
 
970
        DSP3_AddLo += Lo;
 
971
        DSP3_AddHi += Hi;
 
972
 
 
973
        if (DSP3_AddLo < 0)
 
974
                DSP3_AddLo += DSP3_WinLo;
 
975
        else
 
976
                if (DSP3_AddLo >= DSP3_WinLo)
 
977
                        DSP3_AddLo -= DSP3_WinLo;
 
978
 
 
979
        if (DSP3_AddHi < 0)
 
980
                DSP3_AddHi += DSP3_WinHi;
 
981
        else
 
982
                if (DSP3_AddHi >= DSP3_WinHi)
 
983
                        DSP3_AddHi -= DSP3_WinHi;
 
984
 
 
985
        *lo = DSP3_AddLo;
 
986
        *hi = DSP3_AddHi;
 
987
}
 
988
 
 
989
 
 
990
void DSP3_OP1E_D1( int16 move, int16 *lo, int16 *hi )
 
991
{
 
992
        //uint32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
 
993
        int16 Lo;
 
994
        int16 Hi;
 
995
 
 
996
        const unsigned short HiAdd[] = {
 
997
                0x00, 0xFF, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00,
 
998
                0x00, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0xFF, 0x00
 
999
        };
 
1000
        const unsigned short LoAdd[] = {
 
1001
                0x00, 0x00, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x00
 
1002
        };
 
1003
 
 
1004
        if( (*lo) & 1 )
 
1005
                DSP3_AddHi = HiAdd[ move + 8 ];
 
1006
        else
 
1007
                DSP3_AddHi = HiAdd[ move + 0 ];
 
1008
        DSP3_AddLo = LoAdd[ move ];
 
1009
 
 
1010
        Lo = (uint8)(*lo);
 
1011
        Hi = (uint8)(*hi);
 
1012
 
 
1013
        if (Lo & 1)     Hi += (DSP3_AddLo & 1);
 
1014
 
 
1015
        DSP3_AddLo += Lo;
 
1016
        DSP3_AddHi += Hi;
 
1017
 
 
1018
        *lo = DSP3_AddLo;
 
1019
        *hi = DSP3_AddHi;
 
1020
}
 
1021
 
 
1022
 
 
1023
void DSP3_OP10()
 
1024
{
 
1025
        if( DSP3_DR == 0xffff ) {
 
1026
                DSP3_Reset();
 
1027
        } else {
 
1028
                // absorb 2 bytes
 
1029
                DSP3_DR = DSP3_DR;
 
1030
        }
 
1031
}
 
1032
 
 
1033
 
 
1034
void DSP3_OP0C_A()
 
1035
{
 
1036
        // absorb 2 bytes
 
1037
 
 
1038
        DSP3_DR = 0;
 
1039
        SetDSP3 = &DSP3_Reset;
 
1040
}
 
1041
 
 
1042
 
 
1043
void DSP3_OP0C()
 
1044
{
 
1045
        // absorb 2 bytes
 
1046
 
 
1047
        DSP3_DR = 0;
 
1048
        //SetDSP3 = &DSP3_OP0C_A;
 
1049
        SetDSP3 = &DSP3_Reset;
 
1050
}
 
1051
 
 
1052
 
 
1053
void DSP3_OP1C_C()
 
1054
{
 
1055
        // return 2 bytes
 
1056
        DSP3_DR = 0;
 
1057
        SetDSP3 = &DSP3_Reset;
 
1058
}
 
1059
 
 
1060
 
 
1061
void DSP3_OP1C_B()
 
1062
{
 
1063
        // absorb 2 bytes
 
1064
 
 
1065
        // return 2 bytes
 
1066
        DSP3_DR = 0;
 
1067
        SetDSP3 = &DSP3_OP1C_C;
 
1068
}
 
1069
 
 
1070
 
 
1071
void DSP3_OP1C_A()
 
1072
{
 
1073
        // absorb 2 bytes
 
1074
 
 
1075
        SetDSP3 = &DSP3_OP1C_B;
 
1076
}
 
1077
 
 
1078
 
 
1079
void DSP3_OP1C()
 
1080
{
 
1081
        // absorb 2 bytes
 
1082
 
 
1083
        SetDSP3 = &DSP3_OP1C_A;
 
1084
}
 
1085
 
 
1086
 
 
1087
void DSP3_Command()
 
1088
{
 
1089
        if (DSP3_DR < 0x40)
 
1090
        {
 
1091
                switch (DSP3_DR)
 
1092
                {
 
1093
    case 0x02: SetDSP3 = &DSP3_Coordinate; break;
 
1094
    case 0x03: SetDSP3 = &DSP3_OP03; break;
 
1095
    case 0x06: SetDSP3 = &DSP3_OP06; break;
 
1096
    case 0x07: SetDSP3 = &DSP3_OP07; return;
 
1097
    case 0x0c: SetDSP3 = &DSP3_OP0C; break;
 
1098
    case 0x0f: SetDSP3 = &DSP3_TestMemory; break;
 
1099
    case 0x10: SetDSP3 = &DSP3_OP10; break;
 
1100
    case 0x18: SetDSP3 = &DSP3_Convert; break;
 
1101
    case 0x1c: SetDSP3 = &DSP3_OP1C; break;
 
1102
    case 0x1e: SetDSP3 = &DSP3_OP1E; break;
 
1103
    case 0x1f: SetDSP3 = &DSP3_MemoryDump; break;
 
1104
    case 0x38: SetDSP3 = &DSP3_Decode; break;
 
1105
    case 0x3e: SetDSP3 = &DSP3_OP3E; break;
 
1106
    default:
 
1107
     return;
 
1108
                }
 
1109
                DSP3_SR = 0x0080;
 
1110
                DSP3_Index = 0;
 
1111
        }
 
1112
}
 
1113
 
 
1114
uint8 dsp3_byte;
 
1115
uint16 dsp3_address;
 
1116
 
 
1117
void DSP3SetByte()
 
1118
{
 
1119
  if (dsp3_address < 0xC000)
 
1120
  {
 
1121
                if (DSP3_SR & 0x04)
 
1122
                {
 
1123
                        DSP3_DR = (DSP3_DR & 0xff00) + dsp3_byte;
 
1124
                        (*SetDSP3)();
 
1125
                }
 
1126
                else
 
1127
                {
 
1128
                        DSP3_SR ^= 0x10;
 
1129
 
 
1130
                        if (DSP3_SR & 0x10)
 
1131
                                DSP3_DR = (DSP3_DR & 0xff00) + dsp3_byte;
 
1132
                        else
 
1133
                        {
 
1134
                                DSP3_DR = (DSP3_DR & 0x00ff) + (dsp3_byte << 8);
 
1135
                                (*SetDSP3)();
 
1136
                        }
 
1137
                }
 
1138
  }
 
1139
}
 
1140
 
 
1141
void DSP3GetByte()
 
1142
{
 
1143
  if (dsp3_address < 0xC000)
 
1144
  {
 
1145
                if (DSP3_SR & 0x04)
 
1146
                {
 
1147
                        dsp3_byte = (uint8) DSP3_DR;
 
1148
                        (*SetDSP3)();
 
1149
                }
 
1150
                else
 
1151
                {
 
1152
                        DSP3_SR ^= 0x10;
 
1153
 
 
1154
                        if (DSP3_SR & 0x10)
 
1155
                                dsp3_byte = (uint8) (DSP3_DR);
 
1156
                        else
 
1157
                        {
 
1158
                                dsp3_byte = (uint8) (DSP3_DR >> 8);
 
1159
                                (*SetDSP3)();
 
1160
                        }
 
1161
                }
 
1162
 
 
1163
  }
 
1164
  else
 
1165
  {
 
1166
    dsp3_byte = (uint8) DSP3_SR;
 
1167
  }
 
1168
}
 
1169
 
 
1170
void InitDSP3()
 
1171
{
 
1172
  DSP3_Reset();
 
1173
}
 
1174