Previous 15994 Revisions Next

r5666 Thursday 10th September, 2009 at 07:11:19 UTC by Fabio Priuli
[SNES] Added emulation for the DSP-3 and DSP-4 add-on chips, based on latest ZSNES [ZSNES Team, myself]
[docs]messnew.txt
[src/mame]mame.mak
[src/mame/machine]snes.c snesdsp3.c* snesdsp4.c* snesdsp4.h*
[src/mess]mess.mak

src/mame/machine/snesdsp3.c
r0r5666
1/************************************************************************
2
3    DSP-3 emulator code
4
5    Copyright (c) 2003-2009 John Weidman, Kris Bleakley, Lancer, Nach,
6    z80 gaiden, and Jonas Quinn
7
8    This code is released by ZSNES Team under GNU General Public License
9    version 2 as published by the Free Software Foundation.
10    The implementation below is released under the MAME license for use
11    in MAME, MESS and derivatives by permission of the authors.
12
13************************************************************************/
14
15UINT16 DSP3_DataROM[1024] = {
16   0x8000, 0x4000, 0x2000, 0x1000, 0x0800, 0x0400, 0x0200, 0x0100,
17   0x0080, 0x0040, 0x0020, 0x0010, 0x0008, 0x0004, 0x0002, 0x0001,
18   0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100,
19   0x0000, 0x000f, 0x0400, 0x0200, 0x0140, 0x0400, 0x0200, 0x0040,
20   0x007d, 0x007e, 0x007e, 0x007b, 0x007c, 0x007d, 0x007b, 0x007c,
21   0x0002, 0x0020, 0x0030, 0x0000, 0x000d, 0x0019, 0x0026, 0x0032,
22   0x003e, 0x004a, 0x0056, 0x0062, 0x006d, 0x0079, 0x0084, 0x008e,
23   0x0098, 0x00a2, 0x00ac, 0x00b5, 0x00be, 0x00c6, 0x00ce, 0x00d5,
24   0x00dc, 0x00e2, 0x00e7, 0x00ec, 0x00f1, 0x00f5, 0x00f8, 0x00fb,
25   0x00fd, 0x00ff, 0x0100, 0x0100, 0x0100, 0x00ff, 0x00fd, 0x00fb,
26   0x00f8, 0x00f5, 0x00f1, 0x00ed, 0x00e7, 0x00e2, 0x00dc, 0x00d5,
27   0x00ce, 0x00c6, 0x00be, 0x00b5, 0x00ac, 0x00a2, 0x0099, 0x008e,
28   0x0084, 0x0079, 0x006e, 0x0062, 0x0056, 0x004a, 0x003e, 0x0032,
29   0x0026, 0x0019, 0x000d, 0x0000, 0xfff3, 0xffe7, 0xffdb, 0xffce,
30   0xffc2, 0xffb6, 0xffaa, 0xff9e, 0xff93, 0xff87, 0xff7d, 0xff72,
31   0xff68, 0xff5e, 0xff54, 0xff4b, 0xff42, 0xff3a, 0xff32, 0xff2b,
32   0xff25, 0xff1e, 0xff19, 0xff14, 0xff0f, 0xff0b, 0xff08, 0xff05,
33   0xff03, 0xff01, 0xff00, 0xff00, 0xff00, 0xff01, 0xff03, 0xff05,
34   0xff08, 0xff0b, 0xff0f, 0xff13, 0xff18, 0xff1e, 0xff24, 0xff2b,
35   0xff32, 0xff3a, 0xff42, 0xff4b, 0xff54, 0xff5d, 0xff67, 0xff72,
36   0xff7c, 0xff87, 0xff92, 0xff9e, 0xffa9, 0xffb5, 0xffc2, 0xffce,
37   0xffda, 0xffe7, 0xfff3, 0x002b, 0x007f, 0x0020, 0x00ff, 0xff00,
38   0xffbe, 0x0000, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
39   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
40   0x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0x0045,
41   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
42   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
43   0xffc5, 0x0003, 0x0004, 0x0005, 0x0047, 0x0000, 0x0000, 0x0000,
44   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
45   0x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,
46   0x0009, 0x004a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
47   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
48   0xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x004e, 0x0000,
49   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
50   0x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,
51   0x0012, 0x0013, 0x0014, 0x0053, 0x0000, 0x0000, 0x0000, 0x0000,
52   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
53   0xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,
54   0x0059, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
55   0x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,
56   0x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0x0060, 0x0000, 0x0000,
57   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
58   0xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,
59   0x002b, 0x002c, 0x0068, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
60   0x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,
61   0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0071,
62   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
63   0xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
64   0x003e, 0x003f, 0x0040, 0x0041, 0x007b, 0x0000, 0x0000, 0x0000,
65   0x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,
66   0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
67   0x000b, 0x0044, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
68   0xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,
69   0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0x0050, 0x0000,
70   0x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,
71   0x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,
72   0x0024, 0x0025, 0x0026, 0x005d, 0x0000, 0x0000, 0x0000, 0x0000,
73   0xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,
74   0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
75   0x006b, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,
76   0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
77   0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0x0044, 0x0000, 0x0000,
78   0xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
79   0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,
80   0x001f, 0x0020, 0x0054, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,
81   0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,
82   0x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0065,
83   0xffbe, 0x0000, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
84   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
85   0x0000, 0x0000, 0x0000, 0x0000, 0xffc1, 0x0001, 0x0002, 0xfead,
86   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
87   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
88   0xffc5, 0x0003, 0x0004, 0x0005, 0xfeaf, 0x0000, 0x0000, 0x0000,
89   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
90   0x0000, 0x0000, 0x0000, 0x0000, 0xffca, 0x0006, 0x0007, 0x0008,
91   0x0009, 0xfeb2, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
92   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
93   0xffd0, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0xfeb6, 0x0000,
94   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
95   0x0000, 0x0000, 0x0000, 0x0000, 0xffd7, 0x000f, 0x0010, 0x0011,
96   0x0012, 0x0013, 0x0014, 0xfebb, 0x0000, 0x0000, 0x0000, 0x0000,
97   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
98   0xffdf, 0x0015, 0x0016, 0x0017, 0x0018, 0x0019, 0x001a, 0x001b,
99   0xfec1, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
100   0x0000, 0x0000, 0x0000, 0x0000, 0xffe8, 0x001c, 0x001d, 0x001e,
101   0x001f, 0x0020, 0x0021, 0x0022, 0x0023, 0xfec8, 0x0000, 0x0000,
102   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
103   0xfff2, 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a,
104   0x002b, 0x002c, 0xfed0, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
105   0x0000, 0x0000, 0x0000, 0x0000, 0xfffd, 0x002d, 0x002e, 0x002f,
106   0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0xfed9,
107   0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
108   0xffc7, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d,
109   0x003e, 0x003f, 0x0040, 0x0041, 0xfee3, 0x0000, 0x0000, 0x0000,
110   0x0000, 0x0000, 0x0000, 0x0000, 0xffd4, 0x0000, 0x0001, 0x0002,
111   0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
112   0x000b, 0xfeac, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
113   0xffe2, 0x000c, 0x000d, 0x000e, 0x000f, 0x0010, 0x0011, 0x0012,
114   0x0013, 0x0014, 0x0015, 0x0016, 0x0017, 0x0018, 0xfeb8, 0x0000,
115   0x0000, 0x0000, 0x0000, 0x0000, 0xfff1, 0x0019, 0x001a, 0x001b,
116   0x001c, 0x001d, 0x001e, 0x001f, 0x0020, 0x0021, 0x0022, 0x0023,
117   0x0024, 0x0025, 0x0026, 0xfec5, 0x0000, 0x0000, 0x0000, 0x0000,
118   0xffcb, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d,
119   0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
120   0xfed3, 0x0000, 0x0000, 0x0000, 0xffdc, 0x0000, 0x0001, 0x0002,
121   0x0003, 0x0004, 0x0005, 0x0006, 0x0007, 0x0008, 0x0009, 0x000a,
122   0x000b, 0x000c, 0x000d, 0x000e, 0x000f, 0xfeac, 0x0000, 0x0000,
123   0xffee, 0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016,
124   0x0017, 0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e,
125   0x001f, 0x0020, 0xfebc, 0x0000, 0xffee, 0x0021, 0x0022, 0x0023,
126   0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b,
127   0x002c, 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0xfecd,
128   0x0154, 0x0218, 0x0110, 0x00b0, 0x00cc, 0x00b0, 0x0088, 0x00b0,
129   0x0044, 0x00b0, 0x0000, 0x00b0, 0x00fe, 0xff07, 0x0002, 0x00ff,
130   0x00f8, 0x0007, 0x00fe, 0x00ee, 0x07ff, 0x0200, 0x00ef, 0xf800,
131   0x0700, 0x00ee, 0xffff, 0xffff, 0xffff, 0x0000, 0x0000, 0x0001,
132   0x0001, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
133   0xffff, 0x0000, 0x0000, 0x0001, 0x0001, 0x0001, 0x0001, 0x0000,
134   0x0000, 0xffff, 0xffff, 0x0000, 0xffff, 0x0001, 0x0000, 0x0001,
135   0x0001, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff, 0xffff, 0x0000,
136   0xffff, 0x0001, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0xffff,
137   0xffff, 0xffff, 0x0000, 0x0000, 0x0000, 0x0044, 0x0088, 0x00cc,
138   0x0110, 0x0154, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
139   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
140   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
141   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
142   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
143   0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
144};
145
146void (*SetDSP3)( void );
147void DSP3_Command( void );
148
149UINT16 DSP3_DR;
150UINT16 DSP3_SR;
151UINT16 DSP3_MemoryIndex;
152
153void DSP3_Reset( void )
154{
155  DSP3_DR = 0x0080;
156  DSP3_SR = 0x0084;
157  SetDSP3 = &DSP3_Command;
158}
159
160void DSP3_MemorySize( void )
161{
162   DSP3_DR = 0x0300;
163   SetDSP3 = &DSP3_Reset;
164}
165
166void DSP3_TestMemory( void )
167{
168   DSP3_DR = 0x0000;
169   SetDSP3 = &DSP3_Reset;
170}
171
172void DSP3_DumpDataROM( void )
173{
174   DSP3_DR = DSP3_DataROM[DSP3_MemoryIndex++];
175   if (DSP3_MemoryIndex == 1024)
176      SetDSP3 = &DSP3_Reset;
177}
178
179void DSP3_MemoryDump( void )
180{
181   DSP3_MemoryIndex = 0;
182   SetDSP3 = &DSP3_DumpDataROM;
183   DSP3_DumpDataROM();
184}
185
186INT16 DSP3_WinLo;
187INT16 DSP3_WinHi;
188
189void DSP3_OP06( void )
190{
191   DSP3_WinLo = (UINT8)(DSP3_DR);
192   DSP3_WinHi = (UINT8)(DSP3_DR >> 8);
193   DSP3_Reset();
194}
195
196void DSP3_OP03( void )
197{
198   INT16 Lo = (UINT8)(DSP3_DR);
199   INT16 Hi = (UINT8)(DSP3_DR >> 8);
200   INT16 Ofs = (DSP3_WinLo * Hi << 1) + (Lo << 1);
201   DSP3_DR = Ofs >> 1;
202   SetDSP3 = &DSP3_Reset;
203}
204
205INT16 DSP3_AddLo;
206INT16 DSP3_AddHi;
207
208void DSP3_OP07_B( void )
209{
210   INT16 Ofs = (DSP3_WinLo * DSP3_AddHi << 1) + (DSP3_AddLo << 1);
211   DSP3_DR = Ofs >> 1;
212   SetDSP3 = &DSP3_Reset;
213}
214
215void DSP3_OP07_A( void )
216{
217   INT16 Lo = (UINT8)(DSP3_DR);
218   INT16 Hi = (UINT8)(DSP3_DR >> 8);
219
220   if (Lo & 1)   Hi += (DSP3_AddLo & 1);
221
222   DSP3_AddLo += Lo;
223   DSP3_AddHi += Hi;
224
225   if (DSP3_AddLo < 0)
226      DSP3_AddLo += DSP3_WinLo;
227   else
228      if (DSP3_AddLo >= DSP3_WinLo)
229         DSP3_AddLo -= DSP3_WinLo;
230
231   if (DSP3_AddHi < 0)
232      DSP3_AddHi += DSP3_WinHi;
233   else
234      if (DSP3_AddHi >= DSP3_WinHi)
235         DSP3_AddHi -= DSP3_WinHi;
236
237   DSP3_DR = DSP3_AddLo | (DSP3_AddHi << 8) | ((DSP3_AddHi >> 8) & 0xff);
238   SetDSP3 = &DSP3_OP07_B;
239}
240
241void DSP3_OP07( void )
242{
243   UINT32 dataOfs = ((DSP3_DR << 1) + 0x03b2) & 0x03ff;
244
245   DSP3_AddHi = DSP3_DataROM[dataOfs];
246   DSP3_AddLo = DSP3_DataROM[dataOfs + 1];
247
248   SetDSP3 = &DSP3_OP07_A;
249   DSP3_SR = 0x0080;
250}
251
252UINT16 DSP3_Codewords;
253UINT16 DSP3_Outwords;
254UINT16 DSP3_Symbol;
255UINT16 DSP3_BitCount;
256UINT16 DSP3_Index;
257UINT16 DSP3_Codes[512];
258UINT16 DSP3_BitsLeft;
259UINT16 DSP3_ReqBits;
260UINT16 DSP3_ReqData;
261UINT16 DSP3_BitCommand;
262UINT8  DSP3_BaseLength;
263UINT16 DSP3_BaseCodes;
264UINT16 DSP3_BaseCode;
265UINT8  DSP3_CodeLengths[8];
266UINT16 DSP3_CodeOffsets[8];
267UINT16 DSP3_LZCode;
268UINT8  DSP3_LZLength;
269
270UINT16 DSP3_X;
271UINT16 DSP3_Y;
272
273void DSP3_Coordinate( void )
274{
275   DSP3_Index++;
276
277   switch (DSP3_Index)
278   {
279   case 3:
280      {
281         if (DSP3_DR == 0xffff)
282            DSP3_Reset();
283         break;
284      }
285   case 4:
286      {
287         DSP3_X = DSP3_DR;
288         break;
289      }
290   case 5:
291      {
292         DSP3_Y = DSP3_DR;
293         DSP3_DR = 1;
294         break;
295      }
296   case 6:
297      {
298         DSP3_DR = DSP3_X;
299         break;
300      }
301   case 7:
302      {
303         DSP3_DR = DSP3_Y;
304         DSP3_Index = 0;
305         break;
306      }
307   }
308}
309
310UINT8  DSP3_Bitmap[8];
311UINT8  DSP3_Bitplane[8];
312UINT16 DSP3_BMIndex;
313UINT16 DSP3_BPIndex;
314UINT16 DSP3_Count;
315
316void DSP3_Convert_A( void )
317{
318   if (DSP3_BMIndex < 8)
319   {
320      DSP3_Bitmap[DSP3_BMIndex++] = (UINT8) (DSP3_DR);
321      DSP3_Bitmap[DSP3_BMIndex++] = (UINT8) (DSP3_DR >> 8);
322
323      if (DSP3_BMIndex == 8)
324      {
325      short i, j;
326         for (i=0; i < 8; i++)
327            for (j=0; j < 8; j++)
328            {
329               DSP3_Bitplane[j] <<= 1;
330               DSP3_Bitplane[j] |= (DSP3_Bitmap[i] >> j) & 1;
331            }
332
333         DSP3_BPIndex = 0;
334         DSP3_Count--;
335      }
336   }
337
338   if (DSP3_BMIndex == 8)
339   {
340      if (DSP3_BPIndex == 8)
341      {
342         if (!DSP3_Count) DSP3_Reset();
343         DSP3_BMIndex = 0;
344      }
345      else
346      {
347         DSP3_DR = DSP3_Bitplane[DSP3_BPIndex++];
348         DSP3_DR |= DSP3_Bitplane[DSP3_BPIndex++] << 8;
349      }
350   }
351}
352
353void DSP3_Convert( void )
354{
355   DSP3_Count = DSP3_DR;
356   DSP3_BMIndex = 0;
357   SetDSP3 = &DSP3_Convert_A;
358}
359
360UINT8 DSP3_GetBits(UINT8 Count)
361{
362   if (!DSP3_BitsLeft)
363   {
364      DSP3_BitsLeft = Count;
365      DSP3_ReqBits = 0;
366   }
367
368   do {
369      if (!DSP3_BitCount)
370      {
371         DSP3_SR = 0xC0;
372         return 0;
373      }
374
375      DSP3_ReqBits <<= 1;
376      if (DSP3_ReqData & 0x8000) DSP3_ReqBits++;
377      DSP3_ReqData <<= 1;
378
379      DSP3_BitCount--;
380      DSP3_BitsLeft--;
381
382   } while (DSP3_BitsLeft);
383
384   return 1;
385}
386
387void DSP3_Decode_Data( void )
388{
389   if (!DSP3_BitCount)
390   {
391      if (DSP3_SR & 0x40)
392      {
393         DSP3_ReqData = DSP3_DR;
394         DSP3_BitCount += 16;
395      }
396      else
397      {
398         DSP3_SR = 0xC0;
399         return;
400      }
401   }
402
403   if (DSP3_LZCode == 1)
404   {
405      if (!DSP3_GetBits(1))
406         return;
407
408      if (DSP3_ReqBits)
409         DSP3_LZLength = 12;
410      else
411         DSP3_LZLength = 8;
412
413      DSP3_LZCode++;
414   }
415
416   if (DSP3_LZCode == 2)
417   {
418      if (!DSP3_GetBits(DSP3_LZLength))
419         return;
420
421      DSP3_LZCode = 0;
422      DSP3_Outwords--;
423      if (!DSP3_Outwords) SetDSP3 = &DSP3_Reset;
424
425      DSP3_SR = 0x80;
426      DSP3_DR = DSP3_ReqBits;
427      return;
428   }
429
430   if (DSP3_BaseCode == 0xffff)
431   {
432      if (!DSP3_GetBits(DSP3_BaseLength))
433         return;
434
435      DSP3_BaseCode = DSP3_ReqBits;
436   }
437
438   if (!DSP3_GetBits(DSP3_CodeLengths[DSP3_BaseCode]))
439      return;
440
441   DSP3_Symbol = DSP3_Codes[DSP3_CodeOffsets[DSP3_BaseCode] + DSP3_ReqBits];
442   DSP3_BaseCode = 0xffff;
443
444   if (DSP3_Symbol & 0xff00)
445   {
446      DSP3_Symbol += 0x7f02;
447      DSP3_LZCode++;
448   }
449   else
450   {
451      DSP3_Outwords--;
452      if (!DSP3_Outwords)
453         SetDSP3 = &DSP3_Reset;
454   }
455
456   DSP3_SR = 0x80;
457   DSP3_DR = DSP3_Symbol;
458}
459
460void DSP3_Decode_Tree( void )
461{
462   if (!DSP3_BitCount)
463   {
464      DSP3_ReqData = DSP3_DR;
465      DSP3_BitCount += 16;
466   }
467
468   if (!DSP3_BaseCodes)
469   {
470      DSP3_GetBits(1);
471      if (DSP3_ReqBits)
472      {
473         DSP3_BaseLength = 3;
474         DSP3_BaseCodes = 8;
475      }
476      else
477      {
478         DSP3_BaseLength = 2;
479         DSP3_BaseCodes = 4;
480      }
481   }
482
483   while (DSP3_BaseCodes)
484   {
485      if (!DSP3_GetBits(3))
486         return;
487
488      DSP3_ReqBits++;
489
490      DSP3_CodeLengths[DSP3_Index] = (UINT8) DSP3_ReqBits;
491      DSP3_CodeOffsets[DSP3_Index] = DSP3_Symbol;
492      DSP3_Index++;
493
494      DSP3_Symbol += 1 << DSP3_ReqBits;
495      DSP3_BaseCodes--;
496   }
497
498   DSP3_BaseCode = 0xffff;
499   DSP3_LZCode = 0;
500
501   SetDSP3 = &DSP3_Decode_Data;
502   if (DSP3_BitCount) DSP3_Decode_Data();
503}
504
505void DSP3_Decode_Symbols( void )
506{
507   DSP3_ReqData = DSP3_DR;
508   DSP3_BitCount += 16;
509
510   do {
511
512      if (DSP3_BitCommand == 0xffff)
513      {
514         if (!DSP3_GetBits(2)) return;
515         DSP3_BitCommand = DSP3_ReqBits;
516      }
517
518      switch (DSP3_BitCommand)
519      {
520      case 0:
521         {
522            if (!DSP3_GetBits(9)) return;
523            DSP3_Symbol = DSP3_ReqBits;
524            break;
525         }
526      case 1:
527         {
528            DSP3_Symbol++;
529            break;
530         }
531      case 2:
532         {
533            if (!DSP3_GetBits(1)) return;
534            DSP3_Symbol += 2 + DSP3_ReqBits;
535            break;
536         }
537      case 3:
538         {
539            if (!DSP3_GetBits(4)) return;
540            DSP3_Symbol += 4 + DSP3_ReqBits;
541            break;
542         }
543      }
544
545      DSP3_BitCommand = 0xffff;
546
547      DSP3_Codes[DSP3_Index++] = DSP3_Symbol;
548      DSP3_Codewords--;
549
550   } while (DSP3_Codewords);
551
552   DSP3_Index = 0;
553   DSP3_Symbol = 0;
554   DSP3_BaseCodes = 0;
555
556   SetDSP3 = &DSP3_Decode_Tree;
557   if (DSP3_BitCount) DSP3_Decode_Tree();
558}
559
560void DSP3_Decode_A( void )
561{
562   DSP3_Outwords = DSP3_DR;
563   SetDSP3 = &DSP3_Decode_Symbols;
564   DSP3_BitCount = 0;
565   DSP3_BitsLeft = 0;
566   DSP3_Symbol = 0;
567   DSP3_Index = 0;
568   DSP3_BitCommand = 0xffff;
569   DSP3_SR = 0xC0;
570}
571
572void DSP3_Decode( void )
573{
574   DSP3_Codewords = DSP3_DR;
575   SetDSP3 = &DSP3_Decode_A;
576}
577
578
579// Opcodes 1E/3E bit-perfect to 'dsp3-intro' log
580// src: adapted from SD Gundam X/G-Next
581
582INT16 op3e_x;
583INT16 op3e_y;
584
585INT16 op1e_terrain[0x2000];
586INT16 op1e_cost[0x2000];
587INT16 op1e_weight[0x2000];
588
589INT16 op1e_cell;
590INT16 op1e_turn;
591INT16 op1e_search;
592
593INT16 op1e_x;
594INT16 op1e_y;
595
596INT16 op1e_min_radius;
597INT16 op1e_max_radius;
598
599INT16 op1e_max_search_radius;
600INT16 op1e_max_path_radius;
601
602INT16 op1e_lcv_radius;
603INT16 op1e_lcv_steps;
604INT16 op1e_lcv_turns;
605
606void DSP3_OP3E( void )
607{
608   op3e_x = (UINT8)(DSP3_DR & 0x00ff);
609   op3e_y = (UINT8)((DSP3_DR & 0xff00)>>8);
610
611   DSP3_OP03();
612
613   op1e_terrain[ DSP3_DR ] = 0x00;
614   op1e_cost[ DSP3_DR ] = 0xff;
615   op1e_weight[ DSP3_DR ] = 0;
616
617   op1e_max_search_radius = 0;
618   op1e_max_path_radius = 0;
619}
620
621void DSP3_OP1E_A( void );
622void DSP3_OP1E_A1( void );
623void DSP3_OP1E_A2( void );
624void DSP3_OP1E_A3( void );
625
626void DSP3_OP1E_B( void );
627void DSP3_OP1E_B1( void );
628void DSP3_OP1E_B2( void );
629
630void DSP3_OP1E_C( void );
631void DSP3_OP1E_C1( void );
632void DSP3_OP1E_C2( void );
633
634void DSP3_OP1E_D( INT16, INT16 *, INT16 * );
635void DSP3_OP1E_D1( INT16 move, INT16 *lo, INT16 *hi );
636
637void DSP3_OP1E( void )
638{
639   int lcv;
640
641   op1e_min_radius = (UINT8)(DSP3_DR & 0x00ff);
642   op1e_max_radius = (UINT8)((DSP3_DR & 0xff00)>>8);
643
644   if( op1e_min_radius == 0 )
645      op1e_min_radius++;
646
647   if( op1e_max_search_radius >= op1e_min_radius )
648      op1e_min_radius = op1e_max_search_radius+1;
649
650   if( op1e_max_radius > op1e_max_search_radius )
651      op1e_max_search_radius = op1e_max_radius;
652
653   op1e_lcv_radius = op1e_min_radius;
654   op1e_lcv_steps = op1e_min_radius;
655
656   op1e_lcv_turns = 6;
657   op1e_turn = 0;
658
659   op1e_x = op3e_x;
660   op1e_y = op3e_y;
661
662   for( lcv = 0; lcv < op1e_min_radius; lcv++ )
663      DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
664
665   DSP3_OP1E_A();
666}
667
668void DSP3_OP1E_A( void )
669{
670   int lcv;
671
672   if( op1e_lcv_steps == 0 ) {
673      op1e_lcv_radius++;
674
675      op1e_lcv_steps = op1e_lcv_radius;
676
677      op1e_x = op3e_x;
678      op1e_y = op3e_y;
679
680      for( lcv = 0; lcv < op1e_lcv_radius; lcv++ )
681         DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
682   }
683
684   if( op1e_lcv_radius > op1e_max_radius ) {
685      op1e_turn++;
686      op1e_lcv_turns--;
687
688      op1e_lcv_radius = op1e_min_radius;
689      op1e_lcv_steps = op1e_min_radius;
690
691      op1e_x = op3e_x;
692      op1e_y = op3e_y;
693
694      for( lcv = 0; lcv < op1e_min_radius; lcv++ )
695         DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
696   }
697
698   if( op1e_lcv_turns == 0 ) {
699      DSP3_DR = 0xffff;
700      DSP3_SR = 0x0080;
701      SetDSP3 = &DSP3_OP1E_B;
702      return;
703   }
704
705   DSP3_DR = (UINT8)(op1e_x) | ((UINT8)(op1e_y)<<8);
706   DSP3_OP03();
707
708   op1e_cell = DSP3_DR;
709
710   DSP3_SR = 0x0080;
711   SetDSP3 = &DSP3_OP1E_A1;
712}
713
714void DSP3_OP1E_A1( void )
715{
716   DSP3_SR = 0x0084;
717   SetDSP3 = &DSP3_OP1E_A2;
718}
719
720void DSP3_OP1E_A2( void )
721{
722   op1e_terrain[ op1e_cell ] = (UINT8)(DSP3_DR & 0x00ff);
723
724   DSP3_SR = 0x0084;
725   SetDSP3 = &DSP3_OP1E_A3;
726}
727
728void DSP3_OP1E_A3( void )
729{
730   op1e_cost[ op1e_cell ] = (UINT8)(DSP3_DR & 0x00ff);
731
732   if( op1e_lcv_radius == 1 ) {
733      if( op1e_terrain[ op1e_cell ] & 1 ) {
734         op1e_weight[ op1e_cell ] = 0xff;
735      } else {
736         op1e_weight[ op1e_cell ] = op1e_cost[ op1e_cell ];
737      }
738   }
739   else {
740      op1e_weight[ op1e_cell ] = 0xff;
741   }
742
743   DSP3_OP1E_D( (INT16)(op1e_turn+2), &op1e_x, &op1e_y );
744   op1e_lcv_steps--;
745
746   DSP3_SR = 0x0080;
747   DSP3_OP1E_A();
748}
749
750
751void DSP3_OP1E_B( void )
752{
753   op1e_x = op3e_x;
754   op1e_y = op3e_y;
755   op1e_lcv_radius = 1;
756
757   op1e_search = 0;
758
759   DSP3_OP1E_B1();
760
761   SetDSP3 = &DSP3_OP1E_C;
762}
763
764
765void DSP3_OP1E_B1( void )
766{
767   while( op1e_lcv_radius < op1e_max_radius ) {
768      op1e_y--;
769
770      op1e_lcv_turns = 6;
771      op1e_turn = 5;
772
773      while( op1e_lcv_turns ) {
774         op1e_lcv_steps = op1e_lcv_radius;
775
776         while( op1e_lcv_steps ) {
777            DSP3_OP1E_D1( op1e_turn, &op1e_x, &op1e_y );
778
779            if( 0 <= op1e_y && op1e_y < DSP3_WinHi &&
780                  0 <= op1e_x && op1e_x < DSP3_WinLo ) {
781               DSP3_DR = (UINT8)(op1e_x) | ((UINT8)(op1e_y)<<8);
782               DSP3_OP03();
783
784               op1e_cell = DSP3_DR;
785               if( op1e_cost[ op1e_cell ] < 0x80 &&
786                     op1e_terrain[ op1e_cell ] < 0x40 ) {
787                  DSP3_OP1E_B2();
788               } // end cell perimeter
789            }
790
791            op1e_lcv_steps--;
792         } // end search line
793
794         op1e_turn--;
795         if( op1e_turn == 0 ) op1e_turn = 6;
796
797         op1e_lcv_turns--;
798      } // end circle search
799
800      op1e_lcv_radius++;
801   } // end radius search
802}
803
804
805void DSP3_OP1E_B2( void )
806{
807   INT16 cell;
808   INT16 path;
809   INT16 x,y;
810   INT16 lcv_turns;
811
812   path = 0xff;
813   lcv_turns = 6;
814
815   while( lcv_turns ) {
816      x = op1e_x;
817      y = op1e_y;
818
819      DSP3_OP1E_D1( lcv_turns, &x, &y );
820
821      DSP3_DR = (UINT8)(x) | ((UINT8)(y)<<8);
822      DSP3_OP03();
823
824      cell = DSP3_DR;
825
826      if( 0 <= y && y < DSP3_WinHi &&
827            0 <= x && x < DSP3_WinLo  ) {
828
829         if( op1e_terrain[ cell ] < 0x80 || op1e_weight[ cell ] == 0 ) {
830            if( op1e_weight[ cell ] < path ) {
831               path = op1e_weight[ cell ];
832            }
833         }
834      } // end step travel
835
836      lcv_turns--;
837   } // end while turns
838
839   if( path != 0xff ) {
840      op1e_weight[ op1e_cell ] = path + op1e_cost[ op1e_cell ];
841   }
842}
843
844
845void DSP3_OP1E_C( void )
846{
847   int lcv;
848
849   op1e_min_radius = (UINT8)(DSP3_DR & 0x00ff);
850   op1e_max_radius = (UINT8)((DSP3_DR & 0xff00)>>8);
851
852   if( op1e_min_radius == 0 )
853      op1e_min_radius++;
854
855   if( op1e_max_path_radius >= op1e_min_radius )
856      op1e_min_radius = op1e_max_path_radius+1;
857
858   if( op1e_max_radius > op1e_max_path_radius )
859      op1e_max_path_radius = op1e_max_radius;
860
861   op1e_lcv_radius = op1e_min_radius;
862   op1e_lcv_steps = op1e_min_radius;
863
864   op1e_lcv_turns = 6;
865   op1e_turn = 0;
866
867   op1e_x = op3e_x;
868   op1e_y = op3e_y;
869
870   for( lcv = 0; lcv < op1e_min_radius; lcv++ )
871      DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
872
873   DSP3_OP1E_C1();
874}
875
876
877void DSP3_OP1E_C1( void )
878{
879   int lcv;
880
881   if( op1e_lcv_steps == 0 ) {
882      op1e_lcv_radius++;
883
884      op1e_lcv_steps = op1e_lcv_radius;
885
886      op1e_x = op3e_x;
887      op1e_y = op3e_y;
888
889      for( lcv = 0; lcv < op1e_lcv_radius; lcv++ )
890         DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
891   }
892
893   if( op1e_lcv_radius > op1e_max_radius ) {
894      op1e_turn++;
895      op1e_lcv_turns--;
896
897      op1e_lcv_radius = op1e_min_radius;
898      op1e_lcv_steps = op1e_min_radius;
899
900      op1e_x = op3e_x;
901      op1e_y = op3e_y;
902
903      for( lcv = 0; lcv < op1e_min_radius; lcv++ )
904         DSP3_OP1E_D( op1e_turn, &op1e_x, &op1e_y );
905   }
906
907   if( op1e_lcv_turns == 0 ) {
908      DSP3_DR = 0xffff;
909      DSP3_SR = 0x0080;
910      SetDSP3 = &DSP3_Reset;
911      return;
912   }
913
914   DSP3_DR = (UINT8)(op1e_x) | ((UINT8)(op1e_y)<<8);
915   DSP3_OP03();
916
917   op1e_cell = DSP3_DR;
918
919   DSP3_SR = 0x0080;
920   SetDSP3 = &DSP3_OP1E_C2;
921}
922
923
924void DSP3_OP1E_C2( void )
925{
926   DSP3_DR = op1e_weight[ op1e_cell ];
927
928   DSP3_OP1E_D( (INT16)(op1e_turn+2), &op1e_x, &op1e_y );
929   op1e_lcv_steps--;
930
931   DSP3_SR = 0x0084;
932   SetDSP3 = &DSP3_OP1E_C1;
933}
934
935
936void DSP3_OP1E_D( INT16 move, INT16 *lo, INT16 *hi )
937{
938   UINT32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
939   INT16 Lo;
940   INT16 Hi;
941
942   DSP3_AddHi = DSP3_DataROM[dataOfs];
943   DSP3_AddLo = DSP3_DataROM[dataOfs + 1];
944
945   Lo = (UINT8)(*lo);
946   Hi = (UINT8)(*hi);
947
948   if (Lo & 1)   Hi += (DSP3_AddLo & 1);
949
950   DSP3_AddLo += Lo;
951   DSP3_AddHi += Hi;
952
953   if (DSP3_AddLo < 0)
954      DSP3_AddLo += DSP3_WinLo;
955   else
956      if (DSP3_AddLo >= DSP3_WinLo)
957         DSP3_AddLo -= DSP3_WinLo;
958
959   if (DSP3_AddHi < 0)
960      DSP3_AddHi += DSP3_WinHi;
961   else
962      if (DSP3_AddHi >= DSP3_WinHi)
963         DSP3_AddHi -= DSP3_WinHi;
964
965   *lo = DSP3_AddLo;
966   *hi = DSP3_AddHi;
967}
968
969
970void DSP3_OP1E_D1( INT16 move, INT16 *lo, INT16 *hi )
971{
972   //UINT32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
973   INT16 Lo;
974   INT16 Hi;
975
976   const unsigned short HiAdd[] = {
977      0x00, 0xFF, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00,
978      0x00, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0xFF, 0x00
979   };
980   const unsigned short LoAdd[] = {
981      0x00, 0x00, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x00
982   };
983
984   if( (*lo) & 1 )
985      DSP3_AddHi = HiAdd[ move + 8 ];
986   else
987      DSP3_AddHi = HiAdd[ move + 0 ];
988   DSP3_AddLo = LoAdd[ move ];
989
990   Lo = (UINT8)(*lo);
991   Hi = (UINT8)(*hi);
992
993   if (Lo & 1)   Hi += (DSP3_AddLo & 1);
994
995   DSP3_AddLo += Lo;
996   DSP3_AddHi += Hi;
997
998   *lo = DSP3_AddLo;
999   *hi = DSP3_AddHi;
1000}
1001
1002
1003void DSP3_OP10( void )
1004{
1005   if( DSP3_DR == 0xffff ) {
1006      DSP3_Reset();
1007   } else {
1008      // absorb 2 bytes
1009      DSP3_DR = DSP3_DR;
1010   }
1011}
1012
1013
1014void DSP3_OP0C_A( void )
1015{
1016   // absorb 2 bytes
1017
1018   DSP3_DR = 0;
1019   SetDSP3 = &DSP3_Reset;
1020}
1021
1022
1023void DSP3_OP0C( void )
1024{
1025   // absorb 2 bytes
1026
1027   DSP3_DR = 0;
1028   //SetDSP3 = &DSP3_OP0C_A;
1029   SetDSP3 = &DSP3_Reset;
1030}
1031
1032
1033void DSP3_OP1C_C( void )
1034{
1035   // return 2 bytes
1036   DSP3_DR = 0;
1037   SetDSP3 = &DSP3_Reset;
1038}
1039
1040
1041void DSP3_OP1C_B( void )
1042{
1043   // absorb 2 bytes
1044
1045   // return 2 bytes
1046   DSP3_DR = 0;
1047   SetDSP3 = &DSP3_OP1C_C;
1048}
1049
1050
1051void DSP3_OP1C_A( void )
1052{
1053   // absorb 2 bytes
1054
1055   SetDSP3 = &DSP3_OP1C_B;
1056}
1057
1058
1059void DSP3_OP1C( void )
1060{
1061   // absorb 2 bytes
1062
1063   SetDSP3 = &DSP3_OP1C_A;
1064}
1065
1066
1067void DSP3_Command( void )
1068{
1069   if (DSP3_DR < 0x40)
1070   {
1071      switch (DSP3_DR)
1072      {
1073    case 0x02: SetDSP3 = &DSP3_Coordinate; break;
1074    case 0x03: SetDSP3 = &DSP3_OP03; break;
1075    case 0x06: SetDSP3 = &DSP3_OP06; break;
1076    case 0x07: SetDSP3 = &DSP3_OP07; return;
1077    case 0x0c: SetDSP3 = &DSP3_OP0C; break;
1078    case 0x0f: SetDSP3 = &DSP3_TestMemory; break;
1079    case 0x10: SetDSP3 = &DSP3_OP10; break;
1080    case 0x18: SetDSP3 = &DSP3_Convert; break;
1081    case 0x1c: SetDSP3 = &DSP3_OP1C; break;
1082    case 0x1e: SetDSP3 = &DSP3_OP1E; break;
1083    case 0x1f: SetDSP3 = &DSP3_MemoryDump; break;
1084    case 0x38: SetDSP3 = &DSP3_Decode; break;
1085    case 0x3e: SetDSP3 = &DSP3_OP3E; break;
1086    default:
1087     return;
1088      }
1089      DSP3_SR = 0x0080;
1090      DSP3_Index = 0;
1091   }
1092}
1093
1094
1095void DSP3_write( UINT16 dsp3_address, UINT8 dsp3_byte )
1096{
1097  if (dsp3_address < 0xC000)
1098  {
1099      if (DSP3_SR & 0x04)
1100      {
1101         DSP3_DR = (DSP3_DR & 0xff00) + dsp3_byte;
1102         (*SetDSP3)();
1103      }
1104      else
1105      {
1106         DSP3_SR ^= 0x10;
1107
1108         if (DSP3_SR & 0x10)
1109            DSP3_DR = (DSP3_DR & 0xff00) + dsp3_byte;
1110         else
1111         {
1112            DSP3_DR = (DSP3_DR & 0x00ff) + (dsp3_byte << 8);
1113            (*SetDSP3)();
1114         }
1115      }
1116  }
1117}
1118
1119UINT8 DSP3_read( UINT16 dsp3_address )
1120{
1121  UINT8 value;
1122  if (dsp3_address < 0xC000)
1123  {
1124      if (DSP3_SR & 0x04)
1125      {
1126         value = (UINT8) DSP3_DR;
1127         (*SetDSP3)();
1128      }
1129      else
1130      {
1131         DSP3_SR ^= 0x10;
1132
1133         if (DSP3_SR & 0x10)
1134            value = (UINT8) (DSP3_DR);
1135         else
1136         {
1137            value = (UINT8) (DSP3_DR >> 8);
1138            (*SetDSP3)();
1139         }
1140      }
1141
1142  }
1143  else
1144  {
1145    value = (UINT8) DSP3_SR;
1146  }
1147
1148  return value;
1149}
1150
1151void InitDSP3( void )
1152{
1153  DSP3_Reset();
1154}
src/mame/machine/snesdsp4.c
r0r5666
1/************************************************************************
2
3    DSP-4 emulator code
4
5    Copyright (c) 2004-2009 Dreamer Nom, John Weidman, Kris Bleakley,
6    Nach, z80 gaiden, and Jonas Quinn
7
8    This code is released by ZSNES Team under GNU General Public License
9    version 2 as published by the Free Software Foundation.
10    The implementation below is released under the MAME license for use
11    in MAME, MESS and derivatives by permission of the authors.
12
13************************************************************************/
14
15
16/*
17Due recognition and credit are given on Overload's DSP website.
18Thank those contributors for their hard work on this chip.
19
20
21Fixed-point math reminder:
22
23[sign, integer, fraction]
241.15.00 * 1.15.00 = 2.30.00 -> 1.30.00 (DSP) -> 1.31.00 (LSB is '0')
251.15.00 * 1.00.15 = 2.15.15 -> 1.15.15 (DSP) -> 1.15.16 (LSB is '0')
26*/
27
28#include "snesdsp4.h"
29
30struct DSP4_t DSP4;
31struct DSP4_vars_t DSP4_vars;
32
33INLINE UINT16 READ_WORD(UINT8 *addr)
34{
35   return (addr[0]) + (addr[1] << 8);
36}
37
38INLINE UINT32 READ_DWORD(UINT8 *addr)
39{
40   return (addr[0]) + (addr[1] << 8) + (addr[2] << 16) + (addr[3] << 24);
41}
42
43INLINE void WRITE_WORD(UINT8 *addr, UINT16 data)
44{
45   addr[0] = data;
46   addr[1] = data >> 8;
47}
48
49
50//////////////////////////////////////////////////////////////
51
52// input protocol
53
54static INT16 DSP4_READ_WORD( void )
55{
56  INT16 out;
57
58  out = READ_WORD(DSP4.parameters + DSP4.in_index);
59  DSP4.in_index += 2;
60
61  return out;
62}
63
64static INT32 DSP4_READ_DWORD( void )
65{
66  INT32 out;
67
68  out = READ_DWORD(DSP4.parameters + DSP4.in_index);
69  DSP4.in_index += 4;
70
71  return out;
72}
73
74
75//////////////////////////////////////////////////////////////
76
77// output protocol
78
79#define DSP4_CLEAR_OUT() \
80{ DSP4.out_count = 0; DSP4.out_index = 0; }
81
82#define DSP4_WRITE_BYTE( d ) \
83{ WRITE_WORD( DSP4.output + DSP4.out_count, ( d ) ); DSP4.out_count++; }
84
85#define DSP4_WRITE_WORD( d ) \
86{ WRITE_WORD( DSP4.output + DSP4.out_count, ( d ) ); DSP4.out_count += 2; }
87
88#ifndef MSB_FIRST
89#define DSP4_WRITE_16_WORD( d ) \
90{ memcpy(DSP4.output + DSP4.out_count, ( d ), 32); DSP4.out_count += 32; }
91#else
92#define DSP4_WRITE_16_WORD( d )                         \
93{ INT16 *p = ( d ), *end = ( d )+16;                    \
94  for (; p != end; p++)                                 \
95  {                                                     \
96    WRITE_WORD( DSP4.output + DSP4.out_count, *p );     \
97  }                                                     \
98  DSP4.out_count += 32;                                 \
99}
100#endif
101
102#ifdef PRINT_OP
103#define DSP4_WRITE_DEBUG( x, d ) \
104  WRITE_WORD( nop + x, d );
105#endif
106
107#ifdef DEBUG_DSP
108#define DSP4_WRITE_DEBUG( x, d ) \
109  WRITE_WORD( nop + x, d );
110#endif
111
112//////////////////////////////////////////////////////////////
113
114// used to wait for dsp i/o
115
116#define DSP4_WAIT( x ) \
117  DSP4.in_index = 0; DSP4_vars.DSP4_Logic = x; return;
118
119//////////////////////////////////////////////////////////////
120
121// 1.7.8 -> 1.15.16
122#define SEX78( a ) ( ( (INT32) ( (INT16) (a) ) ) << 8 )
123
124// 1.15.0 -> 1.15.16
125#define SEX16( a ) ( ( (INT32) ( (INT16) (a) ) ) << 16 )
126
127#ifdef PRINT_OP
128#define U16( a ) ( (UINT16) ( a ) )
129#endif
130
131#ifdef DEBUG_DSP
132#define U16( a ) ( (UINT16) ( a ) )
133#endif
134
135//////////////////////////////////////////////////////////////
136
137// Attention: This lookup table is not verified
138static const UINT16 div_lut[64] = { 0x0000, 0x8000, 0x4000, 0x2aaa, 0x2000, 0x1999, 0x1555, 0x1249, 0x1000, 0x0e38,
139                                    0x0ccc, 0x0ba2, 0x0aaa, 0x09d8, 0x0924, 0x0888, 0x0800, 0x0787, 0x071c, 0x06bc,
140                                    0x0666, 0x0618, 0x05d1, 0x0590, 0x0555, 0x051e, 0x04ec, 0x04bd, 0x0492, 0x0469,
141                                    0x0444, 0x0421, 0x0400, 0x03e0, 0x03c3, 0x03a8, 0x038e, 0x0375, 0x035e, 0x0348,
142                                    0x0333, 0x031f, 0x030c, 0x02fa, 0x02e8, 0x02d8, 0x02c8, 0x02b9, 0x02aa, 0x029c,
143                                    0x028f, 0x0282, 0x0276, 0x026a, 0x025e, 0x0253, 0x0249, 0x023e, 0x0234, 0x022b,
144                                    0x0222, 0x0219, 0x0210, 0x0208,  };
145INT16 DSP4_Inverse(INT16 value)
146{
147  // saturate bounds
148  if (value < 0)
149  {
150    value = 0;
151  }
152  if (value > 63)
153  {
154    value = 63;
155  }
156
157  return div_lut[value];
158}
159
160//////////////////////////////////////////////////////////////
161
162// Prototype
163void DSP4_OP0B(UINT8 *draw, INT16 sp_x, INT16 sp_y, INT16 sp_attr, UINT8 size, UINT8 stop);
164
165//////////////////////////////////////////////////////////////
166
167// OP00
168void DSP4_Multiply(INT16 Multiplicand, INT16 Multiplier, INT32 *Product)
169{
170  *Product = (Multiplicand * Multiplier << 1) >> 1;
171}
172
173//////////////////////////////////////////////////////////////
174
175
176void DSP4_OP01( void )
177{
178  DSP4.waiting4command = 0;
179
180  // op flow control
181  switch (DSP4_vars.DSP4_Logic)
182  {
183    case 1:
184      goto resume1; break;
185    case 2:
186      goto resume2; break;
187    case 3:
188      goto resume3; break;
189  }
190
191  ////////////////////////////////////////////////////
192  // process initial inputs
193
194  // sort inputs
195  DSP4_vars.world_y = DSP4_READ_DWORD();
196  DSP4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
197  DSP4_vars.poly_top[0][0] = DSP4_READ_WORD();
198  DSP4_vars.poly_cx[1][0] = DSP4_READ_WORD();
199  DSP4_vars.viewport_bottom = DSP4_READ_WORD();
200  DSP4_vars.world_x = DSP4_READ_DWORD();
201  DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
202  DSP4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
203  DSP4_vars.world_yofs = DSP4_READ_WORD();
204  DSP4_vars.world_dy = DSP4_READ_DWORD();
205  DSP4_vars.world_dx = DSP4_READ_DWORD();
206  DSP4_vars.distance = DSP4_READ_WORD();
207  DSP4_READ_WORD(); // 0x0000
208  DSP4_vars.world_xenv = DSP4_READ_DWORD();
209  DSP4_vars.world_ddy = DSP4_READ_WORD();
210  DSP4_vars.world_ddx = DSP4_READ_WORD();
211  DSP4_vars.view_yofsenv = DSP4_READ_WORD();
212
213  // initial (x,y,offset) at starting DSP4_vars.raster line
214  DSP4_vars.view_x1 = (INT16)((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16);
215  DSP4_vars.view_y1 = (INT16)(DSP4_vars.world_y >> 16);
216  DSP4_vars.view_xofs1 = (INT16)(DSP4_vars.world_x >> 16);
217  DSP4_vars.view_yofs1 = DSP4_vars.world_yofs;
218  DSP4_vars.view_turnoff_x = 0;
219  DSP4_vars.view_turnoff_dx = 0;
220
221  // first DSP4_vars.raster line
222  DSP4_vars.poly_raster[0][0] = DSP4_vars.poly_bottom[0][0];
223
224  do
225  {
226    ////////////////////////////////////////////////////
227    // process one iteration of projection
228
229    // perspective projection of world (x,y,scroll) points
230    // based on the current projection lines
231    DSP4_vars.view_x2 = (INT16)(( ( ( DSP4_vars.world_x + DSP4_vars.world_xenv ) >> 16 ) * DSP4_vars.distance >> 15 ) + ( DSP4_vars.view_turnoff_x * DSP4_vars.distance >> 15 ));
232    DSP4_vars.view_y2 = (INT16)((DSP4_vars.world_y >> 16) * DSP4_vars.distance >> 15);
233    DSP4_vars.view_xofs2 = DSP4_vars.view_x2;
234    DSP4_vars.view_yofs2 = (DSP4_vars.world_yofs * DSP4_vars.distance >> 15) + DSP4_vars.poly_bottom[0][0] - DSP4_vars.view_y2;
235
236
237    // 1. World x-location before transformation
238    // 2. Viewer x-position at the next
239    // 3. World y-location before perspective projection
240    // 4. Viewer y-position below the horizon
241    // 5. Number of DSP4_vars.raster lines drawn in this iteration
242
243    DSP4_CLEAR_OUT();
244    DSP4_WRITE_WORD((UINT16)((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16));
245    DSP4_WRITE_WORD(DSP4_vars.view_x2);
246    DSP4_WRITE_WORD((UINT16)(DSP4_vars.world_y >> 16));
247    DSP4_WRITE_WORD(DSP4_vars.view_y2);
248
249    //////////////////////////////////////////////////////
250
251    // SR = 0x00
252
253    // determine # of DSP4_vars.raster lines used
254    DSP4_vars.segments = DSP4_vars.poly_raster[0][0] - DSP4_vars.view_y2;
255
256    // prevent overdraw
257    if (DSP4_vars.view_y2 >= DSP4_vars.poly_raster[0][0])
258      DSP4_vars.segments = 0;
259    else
260      DSP4_vars.poly_raster[0][0] = DSP4_vars.view_y2;
261
262    // don't draw outside the window
263    if (DSP4_vars.view_y2 < DSP4_vars.poly_top[0][0])
264    {
265      DSP4_vars.segments = 0;
266
267      // flush remaining DSP4_vars.raster lines
268      if (DSP4_vars.view_y1 >= DSP4_vars.poly_top[0][0])
269        DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.poly_top[0][0];
270    }
271
272    // SR = 0x80
273
274    DSP4_WRITE_WORD(DSP4_vars.segments);
275
276    //////////////////////////////////////////////////////
277
278    // scan next command if no SR check needed
279    if (DSP4_vars.segments)
280    {
281      INT32 px_dx, py_dy;
282      INT32 x_scroll, y_scroll;
283
284      // SR = 0x00
285
286      // linear interpolation (lerp) between projected points
287      px_dx = (DSP4_vars.view_xofs2 - DSP4_vars.view_xofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
288      py_dy = (DSP4_vars.view_yofs2 - DSP4_vars.view_yofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
289
290      // starting step values
291      x_scroll = SEX16(DSP4_vars.poly_cx[0][0] + DSP4_vars.view_xofs1);
292      y_scroll = SEX16(-DSP4_vars.viewport_bottom + DSP4_vars.view_yofs1 + DSP4_vars.view_yofsenv + DSP4_vars.poly_cx[1][0] - DSP4_vars.world_yofs);
293
294      // SR = 0x80
295
296      // rasterize line
297      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < DSP4_vars.segments; DSP4_vars.lcv++)
298      {
299        // 1. HDMA memory pointer (bg1)
300        // 2. vertical scroll offset ($210E)
301        // 3. horizontal scroll offset ($210D)
302
303        DSP4_WRITE_WORD(DSP4_vars.poly_ptr[0][0]);
304        DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
305        DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
306
307
308        // update memory address
309        DSP4_vars.poly_ptr[0][0] -= 4;
310
311        // update screen values
312        x_scroll += px_dx;
313        y_scroll += py_dy;
314      }
315    }
316
317    ////////////////////////////////////////////////////
318    // Post-update
319
320    // update new viewer (x,y,scroll) to last DSP4_vars.raster line drawn
321    DSP4_vars.view_x1 = DSP4_vars.view_x2;
322    DSP4_vars.view_y1 = DSP4_vars.view_y2;
323    DSP4_vars.view_xofs1 = DSP4_vars.view_xofs2;
324    DSP4_vars.view_yofs1 = DSP4_vars.view_yofs2;
325
326    // add deltas for projection lines
327    DSP4_vars.world_dx += SEX78(DSP4_vars.world_ddx);
328    DSP4_vars.world_dy += SEX78(DSP4_vars.world_ddy);
329
330    // update projection lines
331    DSP4_vars.world_x += (DSP4_vars.world_dx + DSP4_vars.world_xenv);
332    DSP4_vars.world_y += DSP4_vars.world_dy;
333
334    // update road turnoff position
335    DSP4_vars.view_turnoff_x += DSP4_vars.view_turnoff_dx;
336
337    ////////////////////////////////////////////////////
338    // command check
339
340    // scan next command
341    DSP4.in_count = 2;
342    DSP4_WAIT(1) resume1 :
343
344    // check for termination
345    DSP4_vars.distance = DSP4_READ_WORD();
346    if (DSP4_vars.distance == -0x8000)
347      break;
348
349    // road turnoff
350    if( (UINT16) DSP4_vars.distance == 0x8001 )
351    {
352      DSP4.in_count = 6;
353      DSP4_WAIT(2) resume2:
354
355      DSP4_vars.distance = DSP4_READ_WORD();
356      DSP4_vars.view_turnoff_x = DSP4_READ_WORD();
357      DSP4_vars.view_turnoff_dx = DSP4_READ_WORD();
358
359      // factor in new changes
360      DSP4_vars.view_x1 += ( DSP4_vars.view_turnoff_x * DSP4_vars.distance >> 15 );
361      DSP4_vars.view_xofs1 += ( DSP4_vars.view_turnoff_x * DSP4_vars.distance >> 15 );
362
363      // update stepping values
364      DSP4_vars.view_turnoff_x += DSP4_vars.view_turnoff_dx;
365
366      DSP4.in_count = 2;
367      DSP4_WAIT(1)
368    }
369
370    // already have 2 bytes read
371    DSP4.in_count = 6;
372    DSP4_WAIT(3) resume3 :
373
374    // inspect inputs
375    DSP4_vars.world_ddy = DSP4_READ_WORD();
376    DSP4_vars.world_ddx = DSP4_READ_WORD();
377    DSP4_vars.view_yofsenv = DSP4_READ_WORD();
378
379    // no envelope here
380    DSP4_vars.world_xenv = 0;
381  }
382  while (1);
383
384  // terminate op
385  DSP4.waiting4command = 1;
386}
387
388//////////////////////////////////////////////////////////////
389
390
391void DSP4_OP03( void )
392{
393  DSP4_vars.OAM_RowMax = 33;
394  memset(DSP4_vars.OAM_Row, 0, 64);
395}
396
397
398//////////////////////////////////////////////////////////////
399
400
401void DSP4_OP05( void )
402{
403  DSP4_vars.OAM_index = 0;
404  DSP4_vars.OAM_bits = 0;
405  memset(DSP4_vars.OAM_attr, 0, 32);
406  DSP4_vars.sprite_count = 0;
407}
408
409
410//////////////////////////////////////////////////////////////
411
412void DSP4_OP06( void )
413{
414  DSP4_CLEAR_OUT();
415  DSP4_WRITE_16_WORD(DSP4_vars.OAM_attr);
416}
417
418//////////////////////////////////////////////////////////////
419
420
421void DSP4_OP07( void )
422{
423  DSP4.waiting4command = 0;
424
425  // op flow control
426  switch (DSP4_vars.DSP4_Logic)
427  {
428    case 1:
429      goto resume1; break;
430    case 2:
431      goto resume2; break;
432  }
433
434  ////////////////////////////////////////////////////
435  // sort inputs
436
437  DSP4_vars.world_y = DSP4_READ_DWORD();
438  DSP4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
439  DSP4_vars.poly_top[0][0] = DSP4_READ_WORD();
440  DSP4_vars.poly_cx[1][0] = DSP4_READ_WORD();
441  DSP4_vars.viewport_bottom = DSP4_READ_WORD();
442  DSP4_vars.world_x = DSP4_READ_DWORD();
443  DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
444  DSP4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
445  DSP4_vars.world_yofs = DSP4_READ_WORD();
446  DSP4_vars.distance = DSP4_READ_WORD();
447  DSP4_vars.view_y2 = DSP4_READ_WORD();
448  DSP4_vars.view_dy = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
449  DSP4_vars.view_x2 = DSP4_READ_WORD();
450  DSP4_vars.view_dx = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
451  DSP4_vars.view_yofsenv = DSP4_READ_WORD();
452
453  // initial (x,y,offset) at starting DSP4_vars.raster line
454  DSP4_vars.view_x1 = (INT16)(DSP4_vars.world_x >> 16);
455  DSP4_vars.view_y1 = (INT16)(DSP4_vars.world_y >> 16);
456  DSP4_vars.view_xofs1 = DSP4_vars.view_x1;
457  DSP4_vars.view_yofs1 = DSP4_vars.world_yofs;
458
459  // first DSP4_vars.raster line
460  DSP4_vars.poly_raster[0][0] = DSP4_vars.poly_bottom[0][0];
461
462
463  do
464  {
465    ////////////////////////////////////////////////////
466    // process one iteration of projection
467
468    // add shaping
469    DSP4_vars.view_x2 += DSP4_vars.view_dx;
470    DSP4_vars.view_y2 += DSP4_vars.view_dy;
471
472    // vertical scroll calculation
473    DSP4_vars.view_xofs2 = DSP4_vars.view_x2;
474    DSP4_vars.view_yofs2 = (DSP4_vars.world_yofs * DSP4_vars.distance >> 15) + DSP4_vars.poly_bottom[0][0] - DSP4_vars.view_y2;
475
476    // 1. Viewer x-position at the next
477    // 2. Viewer y-position below the horizon
478    // 3. Number of DSP4_vars.raster lines drawn in this iteration
479
480    DSP4_CLEAR_OUT();
481    DSP4_WRITE_WORD(DSP4_vars.view_x2);
482    DSP4_WRITE_WORD(DSP4_vars.view_y2);
483
484    //////////////////////////////////////////////////////
485
486    // SR = 0x00
487
488    // determine # of DSP4_vars.raster lines used
489    DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.view_y2;
490
491    // prevent overdraw
492    if (DSP4_vars.view_y2 >= DSP4_vars.poly_raster[0][0])
493      DSP4_vars.segments = 0;
494    else
495      DSP4_vars.poly_raster[0][0] = DSP4_vars.view_y2;
496
497    // don't draw outside the window
498    if (DSP4_vars.view_y2 < DSP4_vars.poly_top[0][0])
499    {
500      DSP4_vars.segments = 0;
501
502      // flush remaining DSP4_vars.raster lines
503      if (DSP4_vars.view_y1 >= DSP4_vars.poly_top[0][0])
504        DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.poly_top[0][0];
505    }
506
507    // SR = 0x80
508
509    DSP4_WRITE_WORD(DSP4_vars.segments);
510
511    //////////////////////////////////////////////////////
512
513    // scan next command if no SR check needed
514    if (DSP4_vars.segments)
515    {
516      INT32 px_dx, py_dy;
517      INT32 x_scroll, y_scroll;
518
519      // SR = 0x00
520
521      // linear interpolation (lerp) between projected points
522      px_dx = (DSP4_vars.view_xofs2 - DSP4_vars.view_xofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
523      py_dy = (DSP4_vars.view_yofs2 - DSP4_vars.view_yofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
524
525      // starting step values
526      x_scroll = SEX16(DSP4_vars.poly_cx[0][0] + DSP4_vars.view_xofs1);
527      y_scroll = SEX16(-DSP4_vars.viewport_bottom + DSP4_vars.view_yofs1 + DSP4_vars.view_yofsenv + DSP4_vars.poly_cx[1][0] - DSP4_vars.world_yofs);
528
529      // SR = 0x80
530
531      // rasterize line
532      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < DSP4_vars.segments; DSP4_vars.lcv++)
533      {
534        // 1. HDMA memory pointer (bg2)
535        // 2. vertical scroll offset ($2110)
536        // 3. horizontal scroll offset ($210F)
537
538        DSP4_WRITE_WORD(DSP4_vars.poly_ptr[0][0]);
539        DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
540        DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
541
542        // update memory address
543        DSP4_vars.poly_ptr[0][0] -= 4;
544
545        // update screen values
546        x_scroll += px_dx;
547        y_scroll += py_dy;
548      }
549    }
550
551    /////////////////////////////////////////////////////
552    // Post-update
553
554    // update new viewer (x,y,scroll) to last DSP4_vars.raster line drawn
555    DSP4_vars.view_x1 = DSP4_vars.view_x2;
556    DSP4_vars.view_y1 = DSP4_vars.view_y2;
557    DSP4_vars.view_xofs1 = DSP4_vars.view_xofs2;
558    DSP4_vars.view_yofs1 = DSP4_vars.view_yofs2;
559
560    ////////////////////////////////////////////////////
561    // command check
562
563    // scan next command
564    DSP4.in_count = 2;
565    DSP4_WAIT(1) resume1 :
566
567    // check for opcode termination
568    DSP4_vars.distance = DSP4_READ_WORD();
569    if (DSP4_vars.distance == -0x8000)
570      break;
571
572    // already have 2 bytes in queue
573    DSP4.in_count = 10;
574    DSP4_WAIT(2) resume2 :
575
576    // inspect inputs
577    DSP4_vars.view_y2 = DSP4_READ_WORD();
578    DSP4_vars.view_dy = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
579    DSP4_vars.view_x2 = DSP4_READ_WORD();
580    DSP4_vars.view_dx = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
581    DSP4_vars.view_yofsenv = DSP4_READ_WORD();
582  }
583  while (1);
584
585  DSP4.waiting4command = 1;
586}
587
588//////////////////////////////////////////////////////////////
589
590void DSP4_OP08( void )
591{
592  INT16 win_left, win_right;
593  INT16 view_x[2], view_y[2];
594  INT16 envelope[2][2];
595
596  DSP4.waiting4command = 0;
597
598  // op flow control
599  switch (DSP4_vars.DSP4_Logic)
600  {
601    case 1:
602      goto resume1; break;
603    case 2:
604      goto resume2; break;
605  }
606
607  ////////////////////////////////////////////////////
608  // process initial inputs for two polygons
609
610  // clip values
611  DSP4_vars.poly_clipRt[0][0] = DSP4_READ_WORD();
612  DSP4_vars.poly_clipRt[0][1] = DSP4_READ_WORD();
613  DSP4_vars.poly_clipRt[1][0] = DSP4_READ_WORD();
614  DSP4_vars.poly_clipRt[1][1] = DSP4_READ_WORD();
615
616  DSP4_vars.poly_clipLf[0][0] = DSP4_READ_WORD();
617  DSP4_vars.poly_clipLf[0][1] = DSP4_READ_WORD();
618  DSP4_vars.poly_clipLf[1][0] = DSP4_READ_WORD();
619  DSP4_vars.poly_clipLf[1][1] = DSP4_READ_WORD();
620
621  // unknown (constant) (ex. 1P/2P = $00A6, $00A6, $00A6, $00A6)
622  DSP4_READ_WORD();
623  DSP4_READ_WORD();
624  DSP4_READ_WORD();
625  DSP4_READ_WORD();
626
627  // unknown (constant) (ex. 1P/2P = $00A5, $00A5, $00A7, $00A7)
628  DSP4_READ_WORD();
629  DSP4_READ_WORD();
630  DSP4_READ_WORD();
631  DSP4_READ_WORD();
632
633  // polygon centering (left,right)
634  DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
635  DSP4_vars.poly_cx[0][1] = DSP4_READ_WORD();
636  DSP4_vars.poly_cx[1][0] = DSP4_READ_WORD();
637  DSP4_vars.poly_cx[1][1] = DSP4_READ_WORD();
638
639  // HDMA pointer locations
640  DSP4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
641  DSP4_vars.poly_ptr[0][1] = DSP4_READ_WORD();
642  DSP4_vars.poly_ptr[1][0] = DSP4_READ_WORD();
643  DSP4_vars.poly_ptr[1][1] = DSP4_READ_WORD();
644
645  // starting DSP4_vars.raster line below the horizon
646  DSP4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
647  DSP4_vars.poly_bottom[0][1] = DSP4_READ_WORD();
648  DSP4_vars.poly_bottom[1][0] = DSP4_READ_WORD();
649  DSP4_vars.poly_bottom[1][1] = DSP4_READ_WORD();
650
651  // top boundary line to clip
652  DSP4_vars.poly_top[0][0] = DSP4_READ_WORD();
653  DSP4_vars.poly_top[0][1] = DSP4_READ_WORD();
654  DSP4_vars.poly_top[1][0] = DSP4_READ_WORD();
655  DSP4_vars.poly_top[1][1] = DSP4_READ_WORD();
656
657  // unknown
658  // (ex. 1P = $2FC8, $0034, $FF5C, $0035)
659  //
660  // (ex. 2P = $3178, $0034, $FFCC, $0035)
661  // (ex. 2P = $2FC8, $0034, $FFCC, $0035)
662
663  DSP4_READ_WORD();
664  DSP4_READ_WORD();
665  DSP4_READ_WORD();
666  DSP4_READ_WORD();
667
668  // look at guidelines for both polygon shapes
669  DSP4_vars.distance = DSP4_READ_WORD();
670  view_x[0] = DSP4_READ_WORD();
671  view_y[0] = DSP4_READ_WORD();
672  view_x[1] = DSP4_READ_WORD();
673  view_y[1] = DSP4_READ_WORD();
674
675  // envelope shaping guidelines (one frame only)
676  envelope[0][0] = DSP4_READ_WORD();
677  envelope[0][1] = DSP4_READ_WORD();
678  envelope[1][0] = DSP4_READ_WORD();
679  envelope[1][1] = DSP4_READ_WORD();
680
681  // starting base values to project from
682  DSP4_vars.poly_start[0] = view_x[0];
683  DSP4_vars.poly_start[1] = view_x[1];
684
685  // starting DSP4_vars.raster lines to begin drawing
686  DSP4_vars.poly_raster[0][0] = view_y[0];
687  DSP4_vars.poly_raster[0][1] = view_y[0];
688  DSP4_vars.poly_raster[1][0] = view_y[1];
689  DSP4_vars.poly_raster[1][1] = view_y[1];
690
691  // starting distances
692  DSP4_vars.poly_plane[0] = DSP4_vars.distance;
693  DSP4_vars.poly_plane[1] = DSP4_vars.distance;
694
695  // SR = 0x00
696
697  // re-center coordinates
698  win_left = DSP4_vars.poly_cx[0][0] - view_x[0] + envelope[0][0];
699  win_right = DSP4_vars.poly_cx[0][1] - view_x[0] + envelope[0][1];
700
701  // saturate offscreen data for polygon #1
702  if (win_left < DSP4_vars.poly_clipLf[0][0])
703  {
704    win_left = DSP4_vars.poly_clipLf[0][0];
705  }
706  if (win_left > DSP4_vars.poly_clipRt[0][0])
707  {
708    win_left = DSP4_vars.poly_clipRt[0][0];
709  }
710  if (win_right < DSP4_vars.poly_clipLf[0][1])
711  {
712    win_right = DSP4_vars.poly_clipLf[0][1];
713  }
714  if (win_right > DSP4_vars.poly_clipRt[0][1])
715  {
716    win_right = DSP4_vars.poly_clipRt[0][1];
717  }
718
719  // SR = 0x80
720
721  // initial output for polygon #1
722  DSP4_CLEAR_OUT();
723  DSP4_WRITE_BYTE(win_left & 0xff);
724  DSP4_WRITE_BYTE(win_right & 0xff);
725
726
727  do
728  {
729    INT16 polygon;
730    ////////////////////////////////////////////////////
731    // command check
732
733    // scan next command
734    DSP4.in_count = 2;
735    DSP4_WAIT(1) resume1 :
736
737    // terminate op
738    DSP4_vars.distance = DSP4_READ_WORD();
739    if (DSP4_vars.distance == -0x8000)
740      break;
741
742    // already have 2 bytes in queue
743    DSP4.in_count = 16;
744
745    DSP4_WAIT(2) resume2 :
746
747    // look at guidelines for both polygon shapes
748    view_x[0] = DSP4_READ_WORD();
749    view_y[0] = DSP4_READ_WORD();
750    view_x[1] = DSP4_READ_WORD();
751    view_y[1] = DSP4_READ_WORD();
752
753    // envelope shaping guidelines (one frame only)
754    envelope[0][0] = DSP4_READ_WORD();
755    envelope[0][1] = DSP4_READ_WORD();
756    envelope[1][0] = DSP4_READ_WORD();
757    envelope[1][1] = DSP4_READ_WORD();
758
759    ////////////////////////////////////////////////////
760    // projection begins
761
762    // init
763    DSP4_CLEAR_OUT();
764
765
766    //////////////////////////////////////////////
767    // solid polygon renderer - 2 shapes
768
769    for (polygon = 0; polygon < 2; polygon++)
770    {
771      INT32 left_inc, right_inc;
772      INT16 x1_final, x2_final;
773      INT16 env[2][2];
774      INT16 poly;
775
776      // SR = 0x00
777
778      // # DSP4_vars.raster lines to draw
779      DSP4_vars.segments = DSP4_vars.poly_raster[polygon][0] - view_y[polygon];
780
781      // prevent overdraw
782      if (DSP4_vars.segments > 0)
783      {
784        // bump drawing cursor
785        DSP4_vars.poly_raster[polygon][0] = view_y[polygon];
786        DSP4_vars.poly_raster[polygon][1] = view_y[polygon];
787      }
788      else
789        DSP4_vars.segments = 0;
790
791      // don't draw outside the window
792      if (view_y[polygon] < DSP4_vars.poly_top[polygon][0])
793      {
794        DSP4_vars.segments = 0;
795
796        // flush remaining DSP4_vars.raster lines
797        if (view_y[polygon] >= DSP4_vars.poly_top[polygon][0])
798          DSP4_vars.segments = view_y[polygon] - DSP4_vars.poly_top[polygon][0];
799      }
800
801      // SR = 0x80
802
803      // tell user how many DSP4_vars.raster structures to read in
804      DSP4_WRITE_WORD(DSP4_vars.segments);
805
806      // normal parameters
807      poly = polygon;
808
809      /////////////////////////////////////////////////////
810
811      // scan next command if no SR check needed
812      if (DSP4_vars.segments)
813      {
814        INT32 win_left, win_right;
815
816        // road turnoff selection
817        if( (UINT16) envelope[ polygon ][ 0 ] == (UINT16) 0xc001 )
818          poly = 1;
819        else if( envelope[ polygon ][ 1 ] == 0x3fff )
820          poly = 1;
821
822        ///////////////////////////////////////////////
823        // left side of polygon
824
825        // perspective correction on additional shaping parameters
826        env[0][0] = envelope[polygon][0] * DSP4_vars.poly_plane[poly] >> 15;
827        env[0][1] = envelope[polygon][0] * DSP4_vars.distance >> 15;
828
829        // project new shapes (left side)
830        x1_final = view_x[poly] + env[0][0];
831        x2_final = DSP4_vars.poly_start[poly] + env[0][1];
832
833        // interpolate between projected points with shaping
834        left_inc = (x2_final - x1_final) * DSP4_Inverse(DSP4_vars.segments) << 1;
835        if (DSP4_vars.segments == 1)
836          left_inc = -left_inc;
837
838        ///////////////////////////////////////////////
839        // right side of polygon
840
841        // perspective correction on additional shaping parameters
842        env[1][0] = envelope[polygon][1] * DSP4_vars.poly_plane[poly] >> 15;;
843        env[1][1] = envelope[polygon][1] * DSP4_vars.distance >> 15;
844
845        // project new shapes (right side)
846        x1_final = view_x[poly] + env[1][0];
847        x2_final = DSP4_vars.poly_start[poly] + env[1][1];
848
849
850        // interpolate between projected points with shaping
851        right_inc = (x2_final - x1_final) * DSP4_Inverse(DSP4_vars.segments) << 1;
852        if (DSP4_vars.segments == 1)
853          right_inc = -right_inc;
854
855        ///////////////////////////////////////////////
856        // update each point on the line
857
858        win_left = SEX16(DSP4_vars.poly_cx[polygon][0] - DSP4_vars.poly_start[poly] + env[0][0]);
859        win_right = SEX16(DSP4_vars.poly_cx[polygon][1] - DSP4_vars.poly_start[poly] + env[1][0]);
860
861        // update DSP4_vars.distance drawn into world
862        DSP4_vars.poly_plane[polygon] = DSP4_vars.distance;
863
864        // rasterize line
865        for (DSP4_vars.lcv = 0; DSP4_vars.lcv < DSP4_vars.segments; DSP4_vars.lcv++)
866        {
867          INT16 x_left, x_right;
868
869          // project new coordinates
870          win_left += left_inc;
871          win_right += right_inc;
872
873          // grab integer portion, drop fraction (no rounding)
874          x_left = (INT16)(win_left >> 16);
875          x_right = (INT16)(win_right >> 16);
876
877          // saturate offscreen data
878          if (x_left < DSP4_vars.poly_clipLf[polygon][0])
879            x_left = DSP4_vars.poly_clipLf[polygon][0];
880          if (x_left > DSP4_vars.poly_clipRt[polygon][0])
881            x_left = DSP4_vars.poly_clipRt[polygon][0];
882          if (x_right < DSP4_vars.poly_clipLf[polygon][1])
883            x_right = DSP4_vars.poly_clipLf[polygon][1];
884          if (x_right > DSP4_vars.poly_clipRt[polygon][1])
885            x_right = DSP4_vars.poly_clipRt[polygon][1];
886
887          // 1. HDMA memory pointer
888          // 2. Left window position ($2126/$2128)
889          // 3. Right window position ($2127/$2129)
890
891          DSP4_WRITE_WORD(DSP4_vars.poly_ptr[polygon][0]);
892          DSP4_WRITE_BYTE(x_left & 0xff);
893          DSP4_WRITE_BYTE(x_right & 0xff);
894
895
896          // update memory pointers
897          DSP4_vars.poly_ptr[polygon][0] -= 4;
898          DSP4_vars.poly_ptr[polygon][1] -= 4;
899        } // end rasterize line
900      }
901
902      ////////////////////////////////////////////////
903      // Post-update
904
905      // new projection spot to continue rasterizing from
906      DSP4_vars.poly_start[polygon] = view_x[poly];
907    } // end polygon rasterizer
908  }
909  while (1);
910
911  // unknown output
912  DSP4_CLEAR_OUT();
913  DSP4_WRITE_WORD(0);
914
915
916  DSP4.waiting4command = 1;
917}
918
919//////////////////////////////////////////////////////////////
920
921void DSP4_OP09( void )
922{
923  DSP4.waiting4command = 0;
924
925  // op flow control
926  switch (DSP4_vars.DSP4_Logic)
927  {
928    case 1:
929      goto resume1; break;
930    case 2:
931      goto resume2; break;
932    case 3:
933      goto resume3; break;
934    case 4:
935      goto resume4; break;
936    case 5:
937      goto resume5; break;
938    case 6:
939      goto resume6; break;
940  }
941
942  ////////////////////////////////////////////////////
943  // process initial inputs
944
945  // grab screen information
946  DSP4_vars.viewport_cx = DSP4_READ_WORD();
947  DSP4_vars.viewport_cy = DSP4_READ_WORD();
948  DSP4_READ_WORD(); // 0x0000
949  DSP4_vars.viewport_left = DSP4_READ_WORD();
950  DSP4_vars.viewport_right = DSP4_READ_WORD();
951  DSP4_vars.viewport_top = DSP4_READ_WORD();
952  DSP4_vars.viewport_bottom = DSP4_READ_WORD();
953
954  // starting DSP4_vars.raster line below the horizon
955  DSP4_vars.poly_bottom[0][0] = DSP4_vars.viewport_bottom - DSP4_vars.viewport_cy;
956  DSP4_vars.poly_raster[0][0] = 0x100;
957
958  do
959  {
960    ////////////////////////////////////////////////////
961    // check for new sprites
962
963    DSP4.in_count = 4;
964    DSP4_WAIT(1) resume1 :
965
966    ////////////////////////////////////////////////
967    // DSP4_vars.raster overdraw check
968
969    DSP4_vars.raster = DSP4_READ_WORD();
970
971    // continue updating the DSP4_vars.raster line where overdraw begins
972    if (DSP4_vars.raster < DSP4_vars.poly_raster[0][0])
973    {
974      DSP4_vars.sprite_clipy = DSP4_vars.viewport_bottom - (DSP4_vars.poly_bottom[0][0] - DSP4_vars.raster);
975      DSP4_vars.poly_raster[0][0] = DSP4_vars.raster;
976    }
977
978    /////////////////////////////////////////////////
979    // identify sprite
980
981    // op termination
982    DSP4_vars.distance = DSP4_READ_WORD();
983    if (DSP4_vars.distance == -0x8000)
984      goto terminate;
985
986
987    // no sprite
988    if (DSP4_vars.distance == 0x0000)
989    {
990      continue;
991    }
992
993    ////////////////////////////////////////////////////
994    // process projection information
995
996    // vehicle sprite
997    if ((UINT16) DSP4_vars.distance == 0x9000)
998    {
999      INT16 car_left, car_right, car_back;
1000      INT16 impact_left, impact_back;
1001      INT16 world_spx, world_spy;
1002      INT16 view_spx, view_spy;
1003      UINT16 energy;
1004
1005      // we already have 4 bytes we want
1006      DSP4.in_count = 14;
1007      DSP4_WAIT(2) resume2 :
1008
1009      // filter inputs
1010      energy = DSP4_READ_WORD();
1011      impact_back = DSP4_READ_WORD();
1012      car_back = DSP4_READ_WORD();
1013      impact_left = DSP4_READ_WORD();
1014      car_left = DSP4_READ_WORD();
1015      DSP4_vars.distance = DSP4_READ_WORD();
1016      car_right = DSP4_READ_WORD();
1017
1018      // calculate car's world (x,y) values
1019      world_spx = car_right - car_left;
1020      world_spy = car_back;
1021
1022      // add in collision vector [needs bit-twiddling]
1023      world_spx -= energy * (impact_left - car_left) >> 16;
1024      world_spy -= energy * (car_back - impact_back) >> 16;
1025
1026      // perspective correction for world (x,y)
1027      view_spx = world_spx * DSP4_vars.distance >> 15;
1028      view_spy = world_spy * DSP4_vars.distance >> 15;
1029
1030      // convert to screen values
1031      DSP4_vars.sprite_x = DSP4_vars.viewport_cx + view_spx;
1032      DSP4_vars.sprite_y = DSP4_vars.viewport_bottom - (DSP4_vars.poly_bottom[0][0] - view_spy);
1033
1034      // make the car's (x)-coordinate available
1035      DSP4_CLEAR_OUT();
1036      DSP4_WRITE_WORD(world_spx);
1037
1038      // grab a few remaining vehicle values
1039      DSP4.in_count = 4;
1040      DSP4_WAIT(3) resume3 :
1041
1042      // add vertical lift factor
1043      DSP4_vars.sprite_y += DSP4_READ_WORD();
1044    }
1045    // terrain sprite
1046    else
1047    {
1048      INT16 world_spx, world_spy;
1049      INT16 view_spx, view_spy;
1050
1051      // we already have 4 bytes we want
1052      DSP4.in_count = 10;
1053      DSP4_WAIT(4) resume4 :
1054
1055      // sort loop inputs
1056      DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1057      DSP4_vars.poly_raster[0][1] = DSP4_READ_WORD();
1058      world_spx = DSP4_READ_WORD();
1059      world_spy = DSP4_READ_WORD();
1060
1061      // compute base DSP4_vars.raster line from the bottom
1062      DSP4_vars.segments = DSP4_vars.poly_bottom[0][0] - DSP4_vars.raster;
1063
1064      // perspective correction for world (x,y)
1065      view_spx = world_spx * DSP4_vars.distance >> 15;
1066      view_spy = world_spy * DSP4_vars.distance >> 15;
1067
1068      // convert to screen values
1069      DSP4_vars.sprite_x = DSP4_vars.viewport_cx + view_spx - DSP4_vars.poly_cx[0][0];
1070      DSP4_vars.sprite_y = DSP4_vars.viewport_bottom - DSP4_vars.segments + view_spy;
1071    }
1072
1073    // default sprite size: 16x16
1074    DSP4_vars.sprite_size = 1;
1075    DSP4_vars.sprite_attr = DSP4_READ_WORD();
1076
1077    ////////////////////////////////////////////////////
1078    // convert tile data to SNES OAM format
1079
1080    do
1081    {
1082      UINT16 header;
1083
1084      INT16 sp_x, sp_y, sp_attr, sp_dattr;
1085      INT16 sp_dx, sp_dy;
1086      INT16 pixels;
1087
1088      UINT8 draw;
1089
1090      DSP4.in_count = 2;
1091      DSP4_WAIT(5) resume5 :
1092
1093      draw = 1;
1094
1095      // opcode termination
1096      DSP4_vars.raster = DSP4_READ_WORD();
1097      if (DSP4_vars.raster == -0x8000)
1098        goto terminate;
1099
1100      // stop code
1101      if (DSP4_vars.raster == 0x0000 && !DSP4_vars.sprite_size)
1102        break;
1103
1104      // toggle sprite size
1105      if (DSP4_vars.raster == 0x0000)
1106      {
1107        DSP4_vars.sprite_size = !DSP4_vars.sprite_size;
1108        continue;
1109      }
1110
1111      // check for valid sprite header
1112      header = DSP4_vars.raster;
1113      header >>= 8;
1114      if (header != 0x20 &&
1115          header != 0x2e && //This is for attractor sprite
1116          header != 0x40 &&
1117          header != 0x60 &&
1118          header != 0xa0 &&
1119          header != 0xc0 &&
1120          header != 0xe0)
1121        break;
1122
1123      // read in rest of sprite data
1124      DSP4.in_count = 4;
1125      DSP4_WAIT(6) resume6 :
1126
1127      draw = 1;
1128
1129      /////////////////////////////////////
1130      // process tile data
1131
1132      // sprite deltas
1133      sp_dattr = DSP4_vars.raster;
1134      sp_dy = DSP4_READ_WORD();
1135      sp_dx = DSP4_READ_WORD();
1136
1137      // update coordinates to screen space
1138      sp_x = DSP4_vars.sprite_x + sp_dx;
1139      sp_y = DSP4_vars.sprite_y + sp_dy;
1140
1141      // update sprite nametable/attribute information
1142      sp_attr = DSP4_vars.sprite_attr + sp_dattr;
1143
1144      // allow partially visibile tiles
1145      pixels = DSP4_vars.sprite_size ? 15 : 7;
1146
1147      DSP4_CLEAR_OUT();
1148
1149      // transparent tile to clip off parts of a sprite (overdraw)
1150      if (DSP4_vars.sprite_clipy - pixels <= sp_y &&
1151          sp_y <= DSP4_vars.sprite_clipy &&
1152          sp_x >= DSP4_vars.viewport_left - pixels &&
1153          sp_x <= DSP4_vars.viewport_right &&
1154          DSP4_vars.sprite_clipy >= DSP4_vars.viewport_top - pixels &&
1155          DSP4_vars.sprite_clipy <= DSP4_vars.viewport_bottom)
1156      {
1157        DSP4_OP0B(&draw, sp_x, DSP4_vars.sprite_clipy, 0x00EE, DSP4_vars.sprite_size, 0);
1158      }
1159
1160
1161      // normal sprite tile
1162      if (sp_x >= DSP4_vars.viewport_left - pixels &&
1163          sp_x <= DSP4_vars.viewport_right &&
1164          sp_y >= DSP4_vars.viewport_top - pixels &&
1165          sp_y <= DSP4_vars.viewport_bottom &&
1166          sp_y <= DSP4_vars.sprite_clipy)
1167      {
1168        DSP4_OP0B(&draw, sp_x, sp_y, sp_attr, DSP4_vars.sprite_size, 0);
1169      }
1170
1171
1172      // no following OAM data
1173      DSP4_OP0B(&draw, 0, 0x0100, 0, 0, 1);
1174    }
1175    while (1);
1176  }
1177  while (1);
1178
1179  terminate : DSP4.waiting4command = 1;
1180}
1181
1182//////////////////////////////////////////////////////////////
1183
1184const UINT16 OP0A_Values[16] = { 0x0000, 0x0030, 0x0060, 0x0090, 0x00c0, 0x00f0, 0x0120, 0x0150, 0xfe80,
1185                                 0xfeb0, 0xfee0, 0xff10, 0xff40, 0xff70, 0xffa0, 0xffd0 };
1186
1187void DSP4_OP0A(INT16 n2, INT16 *o1, INT16 *o2, INT16 *o3, INT16 *o4)
1188{
1189  *o4 = OP0A_Values[(n2 & 0x000f)];
1190  *o3 = OP0A_Values[(n2 & 0x00f0) >> 4];
1191  *o2 = OP0A_Values[(n2 & 0x0f00) >> 8];
1192  *o1 = OP0A_Values[(n2 & 0xf000) >> 12];
1193}
1194
1195//////////////////////////////////////////////////////////////
1196
1197void DSP4_OP0B(UINT8 *draw, INT16 sp_x, INT16 sp_y, INT16 sp_attr, UINT8 size, UINT8 stop)
1198{
1199  INT16 Row1, Row2;
1200
1201  // SR = 0x00
1202
1203  // align to nearest 8-pixel row
1204  Row1 = (sp_y >> 3) & 0x1f;
1205  Row2 = (Row1 + 1) & 0x1f;
1206
1207  // check boundaries
1208  if (!((sp_y < 0) || ((sp_y & 0x01ff) < 0x00eb)))
1209  {
1210    *draw = 0;
1211  }
1212  if (size)
1213  {
1214    if (DSP4_vars.OAM_Row[Row1] + 1 >= DSP4_vars.OAM_RowMax)
1215      *draw = 0;
1216    if (DSP4_vars.OAM_Row[Row2] + 1 >= DSP4_vars.OAM_RowMax)
1217      *draw = 0;
1218  }
1219  else
1220  {
1221    if (DSP4_vars.OAM_Row[Row1] >= DSP4_vars.OAM_RowMax)
1222    {
1223      *draw = 0;
1224    }
1225  }
1226
1227  // emulator fail-safe (unknown if this really exists)
1228  if (DSP4_vars.sprite_count >= 128)
1229  {
1230    *draw = 0;
1231  }
1232
1233  // SR = 0x80
1234
1235  if (*draw)
1236  {
1237    // Row tiles
1238    if (size)
1239    {
1240      DSP4_vars.OAM_Row[Row1] += 2;
1241      DSP4_vars.OAM_Row[Row2] += 2;
1242    }
1243    else
1244    {
1245      DSP4_vars.OAM_Row[Row1]++;
1246    }
1247
1248    // yield OAM output
1249    DSP4_WRITE_WORD(1);
1250
1251    // pack OAM data: x,y,name,attr
1252    DSP4_WRITE_BYTE(sp_x & 0xff);
1253    DSP4_WRITE_BYTE(sp_y & 0xff);
1254    DSP4_WRITE_WORD(sp_attr);
1255
1256    DSP4_vars.sprite_count++;
1257
1258    // OAM: size,msb data
1259    // save post-oam table data for future retrieval
1260    DSP4_vars.OAM_attr[DSP4_vars.OAM_index] |= ((sp_x <0 || sp_x> 255) << DSP4_vars.OAM_bits);
1261    DSP4_vars.OAM_bits++;
1262
1263    DSP4_vars.OAM_attr[DSP4_vars.OAM_index] |= (size << DSP4_vars.OAM_bits);
1264    DSP4_vars.OAM_bits++;
1265
1266    // move to next byte in buffer
1267    if (DSP4_vars.OAM_bits == 16)
1268    {
1269      DSP4_vars.OAM_bits = 0;
1270      DSP4_vars.OAM_index++;
1271    }
1272  }
1273  else if (stop)
1274  {
1275    // yield no OAM output
1276    DSP4_WRITE_WORD(0);
1277  }
1278}
1279
1280//////////////////////////////////////////////////////////////
1281
1282void DSP4_OP0D( void )
1283{
1284  DSP4.waiting4command = 0;
1285
1286  // op flow control
1287  switch (DSP4_vars.DSP4_Logic)
1288  {
1289    case 1:
1290      goto resume1; break;
1291    case 2:
1292      goto resume2; break;
1293  }
1294
1295  ////////////////////////////////////////////////////
1296  // process initial inputs
1297
1298  // sort inputs
1299  DSP4_vars.world_y = DSP4_READ_DWORD();
1300  DSP4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
1301  DSP4_vars.poly_top[0][0] = DSP4_READ_WORD();
1302  DSP4_vars.poly_cx[1][0] = DSP4_READ_WORD();
1303  DSP4_vars.viewport_bottom = DSP4_READ_WORD();
1304  DSP4_vars.world_x = DSP4_READ_DWORD();
1305  DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1306  DSP4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
1307  DSP4_vars.world_yofs = DSP4_READ_WORD();
1308  DSP4_vars.world_dy = DSP4_READ_DWORD();
1309  DSP4_vars.world_dx = DSP4_READ_DWORD();
1310  DSP4_vars.distance = DSP4_READ_WORD();
1311  DSP4_READ_WORD(); // 0x0000
1312  DSP4_vars.world_xenv = SEX78(DSP4_READ_WORD());
1313  DSP4_vars.world_ddy = DSP4_READ_WORD();
1314  DSP4_vars.world_ddx = DSP4_READ_WORD();
1315  DSP4_vars.view_yofsenv = DSP4_READ_WORD();
1316
1317  // initial (x,y,offset) at starting DSP4_vars.raster line
1318  DSP4_vars.view_x1 = (INT16)((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16);
1319  DSP4_vars.view_y1 = (INT16)(DSP4_vars.world_y >> 16);
1320  DSP4_vars.view_xofs1 = (INT16)(DSP4_vars.world_x >> 16);
1321  DSP4_vars.view_yofs1 = DSP4_vars.world_yofs;
1322
1323  // first DSP4_vars.raster line
1324  DSP4_vars.poly_raster[0][0] = DSP4_vars.poly_bottom[0][0];
1325
1326
1327  do
1328  {
1329    ////////////////////////////////////////////////////
1330    // process one iteration of projection
1331
1332    // perspective projection of world (x,y,scroll) points
1333    // based on the current projection lines
1334    DSP4_vars.view_x2 = (INT16)(( ( ( DSP4_vars.world_x + DSP4_vars.world_xenv ) >> 16 ) * DSP4_vars.distance >> 15 ) + ( DSP4_vars.view_turnoff_x * DSP4_vars.distance >> 15 ));
1335    DSP4_vars.view_y2 = (INT16)((DSP4_vars.world_y >> 16) * DSP4_vars.distance >> 15);
1336    DSP4_vars.view_xofs2 = DSP4_vars.view_x2;
1337    DSP4_vars.view_yofs2 = (DSP4_vars.world_yofs * DSP4_vars.distance >> 15) + DSP4_vars.poly_bottom[0][0] - DSP4_vars.view_y2;
1338
1339    // 1. World x-location before transformation
1340    // 2. Viewer x-position at the current
1341    // 3. World y-location before perspective projection
1342    // 4. Viewer y-position below the horizon
1343    // 5. Number of DSP4_vars.raster lines drawn in this iteration
1344
1345    DSP4_CLEAR_OUT();
1346    DSP4_WRITE_WORD((UINT16)((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16));
1347    DSP4_WRITE_WORD(DSP4_vars.view_x2);
1348    DSP4_WRITE_WORD((UINT16)(DSP4_vars.world_y >> 16));
1349    DSP4_WRITE_WORD(DSP4_vars.view_y2);
1350
1351    //////////////////////////////////////////////////////////
1352
1353    // SR = 0x00
1354
1355    // determine # of DSP4_vars.raster lines used
1356    DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.view_y2;
1357
1358    // prevent overdraw
1359    if (DSP4_vars.view_y2 >= DSP4_vars.poly_raster[0][0])
1360      DSP4_vars.segments = 0;
1361    else
1362      DSP4_vars.poly_raster[0][0] = DSP4_vars.view_y2;
1363
1364    // don't draw outside the window
1365    if (DSP4_vars.view_y2 < DSP4_vars.poly_top[0][0])
1366    {
1367      DSP4_vars.segments = 0;
1368
1369      // flush remaining DSP4_vars.raster lines
1370      if (DSP4_vars.view_y1 >= DSP4_vars.poly_top[0][0])
1371        DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.poly_top[0][0];
1372    }
1373
1374    // SR = 0x80
1375
1376    DSP4_WRITE_WORD(DSP4_vars.segments);
1377
1378    //////////////////////////////////////////////////////////
1379
1380    // scan next command if no SR check needed
1381    if (DSP4_vars.segments)
1382    {
1383      INT32 px_dx, py_dy;
1384      INT32 x_scroll, y_scroll;
1385
1386      // SR = 0x00
1387
1388      // linear interpolation (lerp) between projected points
1389      px_dx = (DSP4_vars.view_xofs2 - DSP4_vars.view_xofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
1390      py_dy = (DSP4_vars.view_yofs2 - DSP4_vars.view_yofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
1391
1392      // starting step values
1393      x_scroll = SEX16(DSP4_vars.poly_cx[0][0] + DSP4_vars.view_xofs1);
1394      y_scroll = SEX16(-DSP4_vars.viewport_bottom + DSP4_vars.view_yofs1 + DSP4_vars.view_yofsenv + DSP4_vars.poly_cx[1][0] - DSP4_vars.world_yofs);
1395
1396      // SR = 0x80
1397
1398      // rasterize line
1399      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < DSP4_vars.segments; DSP4_vars.lcv++)
1400      {
1401        // 1. HDMA memory pointer (bg1)
1402        // 2. vertical scroll offset ($210E)
1403        // 3. horizontal scroll offset ($210D)
1404
1405        DSP4_WRITE_WORD(DSP4_vars.poly_ptr[0][0]);
1406        DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
1407        DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
1408
1409
1410        // update memory address
1411        DSP4_vars.poly_ptr[0][0] -= 4;
1412
1413        // update screen values
1414        x_scroll += px_dx;
1415        y_scroll += py_dy;
1416      }
1417    }
1418
1419    /////////////////////////////////////////////////////
1420    // Post-update
1421
1422    // update new viewer (x,y,scroll) to last DSP4_vars.raster line drawn
1423    DSP4_vars.view_x1 = DSP4_vars.view_x2;
1424    DSP4_vars.view_y1 = DSP4_vars.view_y2;
1425    DSP4_vars.view_xofs1 = DSP4_vars.view_xofs2;
1426    DSP4_vars.view_yofs1 = DSP4_vars.view_yofs2;
1427
1428    // add deltas for projection lines
1429    DSP4_vars.world_dx += SEX78(DSP4_vars.world_ddx);
1430    DSP4_vars.world_dy += SEX78(DSP4_vars.world_ddy);
1431
1432    // update projection lines
1433    DSP4_vars.world_x += (DSP4_vars.world_dx + DSP4_vars.world_xenv);
1434    DSP4_vars.world_y += DSP4_vars.world_dy;
1435
1436    ////////////////////////////////////////////////////
1437    // command check
1438
1439    // scan next command
1440    DSP4.in_count = 2;
1441    DSP4_WAIT(1) resume1 :
1442
1443    // inspect input
1444    DSP4_vars.distance = DSP4_READ_WORD();
1445
1446    // terminate op
1447    if (DSP4_vars.distance == -0x8000)
1448      break;
1449
1450    // already have 2 bytes in queue
1451    DSP4.in_count = 6;
1452    DSP4_WAIT(2) resume2:
1453
1454    // inspect inputs
1455    DSP4_vars.world_ddy = DSP4_READ_WORD();
1456    DSP4_vars.world_ddx = DSP4_READ_WORD();
1457    DSP4_vars.view_yofsenv = DSP4_READ_WORD();
1458
1459    // no envelope here
1460    DSP4_vars.world_xenv = 0;
1461  }
1462  while (1);
1463
1464  DSP4.waiting4command = 1;
1465}
1466
1467//////////////////////////////////////////////////////////////
1468
1469
1470void DSP4_OP0E( void )
1471{
1472  DSP4_vars.OAM_RowMax = 16;
1473  memset(DSP4_vars.OAM_Row, 0, 64);
1474}
1475
1476
1477//////////////////////////////////////////////////////////////
1478
1479void DSP4_OP0F( void )
1480{
1481  DSP4.waiting4command = 0;
1482
1483  // op flow control
1484  switch (DSP4_vars.DSP4_Logic)
1485  {
1486    case 1:
1487      goto resume1; break;
1488    case 2:
1489      goto resume2; break;
1490    case 3:
1491      goto resume3; break;
1492    case 4:
1493      goto resume4; break;
1494  }
1495
1496  ////////////////////////////////////////////////////
1497  // process initial inputs
1498
1499  // sort inputs
1500  DSP4_READ_WORD(); // 0x0000
1501  DSP4_vars.world_y = DSP4_READ_DWORD();
1502  DSP4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
1503  DSP4_vars.poly_top[0][0] = DSP4_READ_WORD();
1504  DSP4_vars.poly_cx[1][0] = DSP4_READ_WORD();
1505  DSP4_vars.viewport_bottom = DSP4_READ_WORD();
1506  DSP4_vars.world_x = DSP4_READ_DWORD();
1507  DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1508  DSP4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
1509  DSP4_vars.world_yofs = DSP4_READ_WORD();
1510  DSP4_vars.world_dy = DSP4_READ_DWORD();
1511  DSP4_vars.world_dx = DSP4_READ_DWORD();
1512  DSP4_vars.distance = DSP4_READ_WORD();
1513  DSP4_READ_WORD(); // 0x0000
1514  DSP4_vars.world_xenv = DSP4_READ_DWORD();
1515  DSP4_vars.world_ddy = DSP4_READ_WORD();
1516  DSP4_vars.world_ddx = DSP4_READ_WORD();
1517  DSP4_vars.view_yofsenv = DSP4_READ_WORD();
1518
1519  // initial (x,y,offset) at starting DSP4_vars.raster line
1520  DSP4_vars.view_x1 = (INT16)((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16);
1521  DSP4_vars.view_y1 = (INT16)(DSP4_vars.world_y >> 16);
1522  DSP4_vars.view_xofs1 = (INT16)(DSP4_vars.world_x >> 16);
1523  DSP4_vars.view_yofs1 = DSP4_vars.world_yofs;
1524  DSP4_vars.view_turnoff_x = 0;
1525  DSP4_vars.view_turnoff_dx = 0;
1526
1527  // first DSP4_vars.raster line
1528  DSP4_vars.poly_raster[0][0] = DSP4_vars.poly_bottom[0][0];
1529
1530
1531  do
1532  {
1533    ////////////////////////////////////////////////////
1534    // process one iteration of projection
1535
1536    // perspective projection of world (x,y,scroll) points
1537    // based on the current projection lines
1538    DSP4_vars.view_x2 = (INT16)(((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16) * DSP4_vars.distance >> 15);
1539    DSP4_vars.view_y2 = (INT16)((DSP4_vars.world_y >> 16) * DSP4_vars.distance >> 15);
1540    DSP4_vars.view_xofs2 = DSP4_vars.view_x2;
1541    DSP4_vars.view_yofs2 = (DSP4_vars.world_yofs * DSP4_vars.distance >> 15) + DSP4_vars.poly_bottom[0][0] - DSP4_vars.view_y2;
1542
1543    // 1. World x-location before transformation
1544    // 2. Viewer x-position at the next
1545    // 3. World y-location before perspective projection
1546    // 4. Viewer y-position below the horizon
1547    // 5. Number of DSP4_vars.raster lines drawn in this iteration
1548
1549    DSP4_CLEAR_OUT();
1550    DSP4_WRITE_WORD((UINT16)((DSP4_vars.world_x + DSP4_vars.world_xenv) >> 16));
1551    DSP4_WRITE_WORD(DSP4_vars.view_x2);
1552    DSP4_WRITE_WORD((UINT16)(DSP4_vars.world_y >> 16));
1553    DSP4_WRITE_WORD(DSP4_vars.view_y2);
1554
1555    //////////////////////////////////////////////////////
1556
1557    // SR = 0x00
1558
1559    // determine # of DSP4_vars.raster lines used
1560    DSP4_vars.segments = DSP4_vars.poly_raster[0][0] - DSP4_vars.view_y2;
1561
1562    // prevent overdraw
1563    if (DSP4_vars.view_y2 >= DSP4_vars.poly_raster[0][0])
1564      DSP4_vars.segments = 0;
1565    else
1566      DSP4_vars.poly_raster[0][0] = DSP4_vars.view_y2;
1567
1568    // don't draw outside the window
1569    if (DSP4_vars.view_y2 < DSP4_vars.poly_top[0][0])
1570    {
1571      DSP4_vars.segments = 0;
1572
1573      // flush remaining DSP4_vars.raster lines
1574      if (DSP4_vars.view_y1 >= DSP4_vars.poly_top[0][0])
1575        DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.poly_top[0][0];
1576    }
1577
1578    // SR = 0x80
1579
1580    DSP4_WRITE_WORD(DSP4_vars.segments);
1581
1582    //////////////////////////////////////////////////////
1583
1584    // scan next command if no SR check needed
1585    if (DSP4_vars.segments)
1586    {
1587      INT32 px_dx, py_dy;
1588      INT32 x_scroll, y_scroll;
1589
1590      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < 4; DSP4_vars.lcv++)
1591      {
1592        // grab inputs
1593        DSP4.in_count = 4;
1594        DSP4_WAIT(1);
1595        resume1 :
1596        for (;;)
1597        {
1598          INT16 distance;
1599          INT16 color, red, green, blue;
1600
1601          distance = DSP4_READ_WORD();
1602          color = DSP4_READ_WORD();
1603
1604          // U1+B5+G5+R5
1605          red = color & 0x1f;
1606          green = (color >> 5) & 0x1f;
1607          blue = (color >> 10) & 0x1f;
1608
1609          // dynamic lighting
1610          red = (red * distance >> 15) & 0x1f;
1611          green = (green * distance >> 15) & 0x1f;
1612          blue = (blue * distance >> 15) & 0x1f;
1613          color = red | (green << 5) | (blue << 10);
1614
1615          DSP4_CLEAR_OUT();
1616          DSP4_WRITE_WORD(color);
1617          break;
1618        }
1619      }
1620
1621      //////////////////////////////////////////////////////
1622
1623      // SR = 0x00
1624
1625      // linear interpolation (lerp) between projected points
1626      px_dx = (DSP4_vars.view_xofs2 - DSP4_vars.view_xofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
1627      py_dy = (DSP4_vars.view_yofs2 - DSP4_vars.view_yofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
1628
1629
1630      // starting step values
1631      x_scroll = SEX16(DSP4_vars.poly_cx[0][0] + DSP4_vars.view_xofs1);
1632      y_scroll = SEX16(-DSP4_vars.viewport_bottom + DSP4_vars.view_yofs1 + DSP4_vars.view_yofsenv + DSP4_vars.poly_cx[1][0] - DSP4_vars.world_yofs);
1633
1634      // SR = 0x80
1635
1636      // rasterize line
1637      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < DSP4_vars.segments; DSP4_vars.lcv++)
1638      {
1639        // 1. HDMA memory pointer
1640        // 2. vertical scroll offset ($210E)
1641        // 3. horizontal scroll offset ($210D)
1642
1643        DSP4_WRITE_WORD(DSP4_vars.poly_ptr[0][0]);
1644        DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
1645        DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
1646
1647        // update memory address
1648        DSP4_vars.poly_ptr[0][0] -= 4;
1649
1650        // update screen values
1651        x_scroll += px_dx;
1652        y_scroll += py_dy;
1653      }
1654    }
1655
1656    ////////////////////////////////////////////////////
1657    // Post-update
1658
1659    // update new viewer (x,y,scroll) to last DSP4_vars.raster line drawn
1660    DSP4_vars.view_x1 = DSP4_vars.view_x2;
1661    DSP4_vars.view_y1 = DSP4_vars.view_y2;
1662    DSP4_vars.view_xofs1 = DSP4_vars.view_xofs2;
1663    DSP4_vars.view_yofs1 = DSP4_vars.view_yofs2;
1664
1665    // add deltas for projection lines
1666    DSP4_vars.world_dx += SEX78(DSP4_vars.world_ddx);
1667    DSP4_vars.world_dy += SEX78(DSP4_vars.world_ddy);
1668
1669    // update projection lines
1670    DSP4_vars.world_x += (DSP4_vars.world_dx + DSP4_vars.world_xenv);
1671    DSP4_vars.world_y += DSP4_vars.world_dy;
1672
1673    // update road turnoff position
1674    DSP4_vars.view_turnoff_x += DSP4_vars.view_turnoff_dx;
1675
1676    ////////////////////////////////////////////////////
1677    // command check
1678
1679    // scan next command
1680    DSP4.in_count = 2;
1681    DSP4_WAIT(2) resume2:
1682
1683    // check for termination
1684    DSP4_vars.distance = DSP4_READ_WORD();
1685    if (DSP4_vars.distance == -0x8000)
1686      break;
1687
1688    // road splice
1689    if( (UINT16) DSP4_vars.distance == 0x8001 )
1690    {
1691      DSP4.in_count = 6;
1692      DSP4_WAIT(3) resume3:
1693
1694      DSP4_vars.distance = DSP4_READ_WORD();
1695      DSP4_vars.view_turnoff_x = DSP4_READ_WORD();
1696      DSP4_vars.view_turnoff_dx = DSP4_READ_WORD();
1697
1698      // factor in new changes
1699      DSP4_vars.view_x1 += ( DSP4_vars.view_turnoff_x * DSP4_vars.distance >> 15 );
1700      DSP4_vars.view_xofs1 += ( DSP4_vars.view_turnoff_x * DSP4_vars.distance >> 15 );
1701
1702      // update stepping values
1703      DSP4_vars.view_turnoff_x += DSP4_vars.view_turnoff_dx;
1704
1705      DSP4.in_count = 2;
1706      DSP4_WAIT(2)
1707    }
1708
1709    // already have 2 bytes in queue
1710    DSP4.in_count = 6;
1711    DSP4_WAIT(4) resume4 :
1712
1713    // inspect inputs
1714    DSP4_vars.world_ddy = DSP4_READ_WORD();
1715    DSP4_vars.world_ddx = DSP4_READ_WORD();
1716    DSP4_vars.view_yofsenv = DSP4_READ_WORD();
1717
1718    // no envelope here
1719    DSP4_vars.world_xenv = 0;
1720  }
1721  while (1);
1722
1723  // terminate op
1724  DSP4.waiting4command = 1;
1725}
1726
1727//////////////////////////////////////////////////////////////
1728
1729
1730void DSP4_OP10( void )
1731{
1732  DSP4.waiting4command = 0;
1733
1734  // op flow control
1735  switch (DSP4_vars.DSP4_Logic)
1736  {
1737    case 1:
1738      goto resume1; break;
1739    case 2:
1740      goto resume2; break;
1741    case 3:
1742      goto resume3; break;
1743  }
1744
1745  ////////////////////////////////////////////////////
1746  // sort inputs
1747
1748  DSP4_READ_WORD(); // 0x0000
1749  DSP4_vars.world_y = DSP4_READ_DWORD();
1750  DSP4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
1751  DSP4_vars.poly_top[0][0] = DSP4_READ_WORD();
1752  DSP4_vars.poly_cx[1][0] = DSP4_READ_WORD();
1753  DSP4_vars.viewport_bottom = DSP4_READ_WORD();
1754  DSP4_vars.world_x = DSP4_READ_DWORD();
1755  DSP4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1756  DSP4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
1757  DSP4_vars.world_yofs = DSP4_READ_WORD();
1758  DSP4_vars.distance = DSP4_READ_WORD();
1759  DSP4_vars.view_y2 = DSP4_READ_WORD();
1760  DSP4_vars.view_dy = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
1761  DSP4_vars.view_x2 = DSP4_READ_WORD();
1762  DSP4_vars.view_dx = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
1763  DSP4_vars.view_yofsenv = DSP4_READ_WORD();
1764
1765  // initial (x,y,offset) at starting DSP4_vars.raster line
1766  DSP4_vars.view_x1 = (INT16)(DSP4_vars.world_x >> 16);
1767  DSP4_vars.view_y1 = (INT16)(DSP4_vars.world_y >> 16);
1768  DSP4_vars.view_xofs1 = DSP4_vars.view_x1;
1769  DSP4_vars.view_yofs1 = DSP4_vars.world_yofs;
1770
1771  // first DSP4_vars.raster line
1772  DSP4_vars.poly_raster[0][0] = DSP4_vars.poly_bottom[0][0];
1773
1774  do
1775  {
1776    ////////////////////////////////////////////////////
1777    // process one iteration of projection
1778
1779    // add shaping
1780    DSP4_vars.view_x2 += DSP4_vars.view_dx;
1781    DSP4_vars.view_y2 += DSP4_vars.view_dy;
1782
1783    // vertical scroll calculation
1784    DSP4_vars.view_xofs2 = DSP4_vars.view_x2;
1785    DSP4_vars.view_yofs2 = (DSP4_vars.world_yofs * DSP4_vars.distance >> 15) + DSP4_vars.poly_bottom[0][0] - DSP4_vars.view_y2;
1786
1787    // 1. Viewer x-position at the next
1788    // 2. Viewer y-position below the horizon
1789    // 3. Number of DSP4_vars.raster lines drawn in this iteration
1790
1791    DSP4_CLEAR_OUT();
1792    DSP4_WRITE_WORD(DSP4_vars.view_x2);
1793    DSP4_WRITE_WORD(DSP4_vars.view_y2);
1794
1795    //////////////////////////////////////////////////////
1796
1797    // SR = 0x00
1798
1799    // determine # of DSP4_vars.raster lines used
1800    DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.view_y2;
1801
1802    // prevent overdraw
1803    if (DSP4_vars.view_y2 >= DSP4_vars.poly_raster[0][0])
1804      DSP4_vars.segments = 0;
1805    else
1806      DSP4_vars.poly_raster[0][0] = DSP4_vars.view_y2;
1807
1808    // don't draw outside the window
1809    if (DSP4_vars.view_y2 < DSP4_vars.poly_top[0][0])
1810    {
1811      DSP4_vars.segments = 0;
1812
1813      // flush remaining DSP4_vars.raster lines
1814      if (DSP4_vars.view_y1 >= DSP4_vars.poly_top[0][0])
1815        DSP4_vars.segments = DSP4_vars.view_y1 - DSP4_vars.poly_top[0][0];
1816    }
1817
1818    // SR = 0x80
1819
1820    DSP4_WRITE_WORD(DSP4_vars.segments);
1821
1822    //////////////////////////////////////////////////////
1823
1824    // scan next command if no SR check needed
1825    if (DSP4_vars.segments)
1826    {
1827      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < 4; DSP4_vars.lcv++)
1828      {
1829        // grab inputs
1830        DSP4.in_count = 4;
1831        DSP4_WAIT(1);
1832        resume1 :
1833        for (;;)
1834        {
1835          INT16 distance;
1836          INT16 color, red, green, blue;
1837
1838          distance = DSP4_READ_WORD();
1839          color = DSP4_READ_WORD();
1840
1841          // U1+B5+G5+R5
1842          red = color & 0x1f;
1843          green = (color >> 5) & 0x1f;
1844          blue = (color >> 10) & 0x1f;
1845
1846          // dynamic lighting
1847          red = (red * distance >> 15) & 0x1f;
1848          green = (green * distance >> 15) & 0x1f;
1849          blue = (blue * distance >> 15) & 0x1f;
1850          color = red | (green << 5) | (blue << 10);
1851
1852          DSP4_CLEAR_OUT();
1853          DSP4_WRITE_WORD(color);
1854          break;
1855        }
1856      }
1857    }
1858
1859    //////////////////////////////////////////////////////
1860
1861    // scan next command if no SR check needed
1862    if (DSP4_vars.segments)
1863    {
1864      INT32 px_dx, py_dy;
1865      INT32 x_scroll, y_scroll;
1866
1867      // SR = 0x00
1868
1869      // linear interpolation (lerp) between projected points
1870      px_dx = (DSP4_vars.view_xofs2 - DSP4_vars.view_xofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
1871      py_dy = (DSP4_vars.view_yofs2 - DSP4_vars.view_yofs1) * DSP4_Inverse(DSP4_vars.segments) << 1;
1872
1873      // starting step values
1874      x_scroll = SEX16(DSP4_vars.poly_cx[0][0] + DSP4_vars.view_xofs1);
1875      y_scroll = SEX16(-DSP4_vars.viewport_bottom + DSP4_vars.view_yofs1 + DSP4_vars.view_yofsenv + DSP4_vars.poly_cx[1][0] - DSP4_vars.world_yofs);
1876
1877      // SR = 0x80
1878
1879      // rasterize line
1880      for (DSP4_vars.lcv = 0; DSP4_vars.lcv < DSP4_vars.segments; DSP4_vars.lcv++)
1881      {
1882        // 1. HDMA memory pointer (bg2)
1883        // 2. vertical scroll offset ($2110)
1884        // 3. horizontal scroll offset ($210F)
1885
1886        DSP4_WRITE_WORD(DSP4_vars.poly_ptr[0][0]);
1887        DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
1888        DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
1889
1890        // update memory address
1891        DSP4_vars.poly_ptr[0][0] -= 4;
1892
1893        // update screen values
1894        x_scroll += px_dx;
1895        y_scroll += py_dy;
1896      }
1897    }
1898
1899    /////////////////////////////////////////////////////
1900    // Post-update
1901
1902    // update new viewer (x,y,scroll) to last DSP4_vars.raster line drawn
1903    DSP4_vars.view_x1 = DSP4_vars.view_x2;
1904    DSP4_vars.view_y1 = DSP4_vars.view_y2;
1905    DSP4_vars.view_xofs1 = DSP4_vars.view_xofs2;
1906    DSP4_vars.view_yofs1 = DSP4_vars.view_yofs2;
1907
1908    ////////////////////////////////////////////////////
1909    // command check
1910
1911    // scan next command
1912    DSP4.in_count = 2;
1913    DSP4_WAIT(2) resume2 :
1914
1915    // check for opcode termination
1916    DSP4_vars.distance = DSP4_READ_WORD();
1917    if (DSP4_vars.distance == -0x8000)
1918      break;
1919
1920    // already have 2 bytes in queue
1921    DSP4.in_count = 10;
1922    DSP4_WAIT(3) resume3 :
1923
1924
1925    // inspect inputs
1926    DSP4_vars.view_y2 = DSP4_READ_WORD();
1927    DSP4_vars.view_dy = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
1928    DSP4_vars.view_x2 = DSP4_READ_WORD();
1929    DSP4_vars.view_dx = DSP4_READ_WORD() * DSP4_vars.distance >> 15;
1930  }
1931  while (1);
1932
1933  DSP4.waiting4command = 1;
1934}
1935
1936//////////////////////////////////////////////////////////////
1937
1938void DSP4_OP11(INT16 A, INT16 B, INT16 C, INT16 D, INT16 *M)
1939{
1940  // 0x155 = 341 = Horizontal Width of the Screen
1941  *M = ((A * 0x0155 >> 2) & 0xf000) |
1942       ((B * 0x0155 >> 6) & 0x0f00) |
1943       ((C * 0x0155 >> 10) & 0x00f0) |
1944       ((D * 0x0155 >> 14) & 0x000f);
1945}
1946
1947
1948
1949
1950
1951/////////////////////////////////////////////////////////////
1952//Processing Code
1953/////////////////////////////////////////////////////////////
1954
1955void InitDSP4( void )
1956{
1957  memset(&DSP4, 0, sizeof(DSP4));
1958  DSP4.waiting4command = 1;
1959}
1960
1961void DSP4_write( UINT8 dsp4_byte )
1962{
1963  // clear pending read
1964  if (DSP4.out_index < DSP4.out_count)
1965  {
1966    DSP4.out_index++;
1967    return;
1968  }
1969
1970  if (DSP4.waiting4command)
1971  {
1972    if (DSP4.half_command)
1973    {
1974      DSP4.command |= (dsp4_byte << 8);
1975      DSP4.in_index = 0;
1976      DSP4.waiting4command = 0;
1977      DSP4.half_command = 0;
1978      DSP4.out_count = 0;
1979      DSP4.out_index = 0;
1980
1981      DSP4_vars.DSP4_Logic = 0;
1982
1983
1984      switch (DSP4.command)
1985      {
1986        case 0x0000:
1987          DSP4.in_count = 4; break;
1988        case 0x0001:
1989          DSP4.in_count = 44; break;
1990        case 0x0003:
1991          DSP4.in_count = 0; break;
1992        case 0x0005:
1993          DSP4.in_count = 0; break;
1994        case 0x0006:
1995          DSP4.in_count = 0; break;
1996        case 0x0007:
1997          DSP4.in_count = 34; break;
1998        case 0x0008:
1999          DSP4.in_count = 90; break;
2000        case 0x0009:
2001          DSP4.in_count = 14; break;
2002        case 0x000a:
2003          DSP4.in_count = 6; break;
2004        case 0x000b:
2005          DSP4.in_count = 6; break;
2006        case 0x000d:
2007          DSP4.in_count = 42; break;
2008        case 0x000e:
2009          DSP4.in_count = 0; break;
2010        case 0x000f:
2011          DSP4.in_count = 46; break;
2012        case 0x0010:
2013          DSP4.in_count = 36; break;
2014        case 0x0011:
2015          DSP4.in_count = 8; break;
2016        default:
2017          DSP4.waiting4command = 1;
2018          break;
2019      }
2020    }
2021    else
2022    {
2023      DSP4.command = dsp4_byte;
2024      DSP4.half_command = 1;
2025    }
2026  }
2027  else
2028  {
2029    DSP4.parameters[DSP4.in_index] = dsp4_byte;
2030    DSP4.in_index++;
2031  }
2032
2033  if (!DSP4.waiting4command && DSP4.in_count == DSP4.in_index)
2034  {
2035    // Actually execute the command
2036    DSP4.waiting4command = 1;
2037    DSP4.out_index = 0;
2038    DSP4.in_index = 0;
2039
2040    switch (DSP4.command)
2041    {
2042        // 16-bit multiplication
2043      case 0x0000:
2044      {
2045        INT16 multiplier, multiplicand;
2046        INT32 product;
2047
2048        multiplier = DSP4_READ_WORD();
2049        multiplicand = DSP4_READ_WORD();
2050
2051        DSP4_Multiply(multiplicand, multiplier, &product);
2052
2053        DSP4_CLEAR_OUT();
2054        DSP4_WRITE_WORD((UINT16)(product));
2055        DSP4_WRITE_WORD((UINT16)(product >> 16));
2056      }
2057      break;
2058
2059      // single-player track projection
2060      case 0x0001:
2061        DSP4_OP01(); break;
2062
2063      // single-player selection
2064      case 0x0003:
2065        DSP4_OP03(); break;
2066
2067      // clear OAM
2068      case 0x0005:
2069        DSP4_OP05(); break;
2070
2071      // transfer OAM
2072      case 0x0006:
2073        DSP4_OP06(); break;
2074
2075      // single-player track turnoff projection
2076      case 0x0007:
2077        DSP4_OP07(); break;
2078
2079      // solid polygon projection
2080      case 0x0008:
2081        DSP4_OP08(); break;
2082
2083      // sprite projection
2084      case 0x0009:
2085        DSP4_OP09(); break;
2086
2087      // unknown
2088      case 0x000A:
2089      {
2090        INT16 in2a;
2091        INT16 out1a, out2a, out3a, out4a;
2092
2093        /* in1a = */ DSP4_READ_WORD();
2094        in2a = DSP4_READ_WORD();
2095        /* in3a = */ DSP4_READ_WORD();
2096
2097        DSP4_OP0A(in2a, &out2a, &out1a, &out4a, &out3a);
2098
2099        DSP4_CLEAR_OUT();
2100        DSP4_WRITE_WORD(out1a);
2101        DSP4_WRITE_WORD(out2a);
2102        DSP4_WRITE_WORD(out3a);
2103        DSP4_WRITE_WORD(out4a);
2104      }
2105      break;
2106
2107      // set OAM
2108      case 0x000B:
2109      {
2110        INT16 sp_x = DSP4_READ_WORD();
2111        INT16 sp_y = DSP4_READ_WORD();
2112        INT16 sp_attr = DSP4_READ_WORD();
2113        UINT8 draw = 1;
2114
2115        DSP4_CLEAR_OUT();
2116
2117        DSP4_OP0B(&draw, sp_x, sp_y, sp_attr, 0, 1);
2118      }
2119      break;
2120
2121      // multi-player track projection
2122      case 0x000D:
2123        DSP4_OP0D(); break;
2124
2125      // multi-player selection
2126      case 0x000E:
2127        DSP4_OP0E(); break;
2128
2129      // single-player track projection with lighting
2130      case 0x000F:
2131        DSP4_OP0F(); break;
2132
2133      // single-player track turnoff projection with lighting
2134      case 0x0010:
2135        DSP4_OP10(); break;
2136
2137      // unknown: horizontal mapping command
2138      case 0x0011:
2139      {
2140        INT16 a, b, c, d, m;
2141
2142
2143        d = DSP4_READ_WORD();
2144        c = DSP4_READ_WORD();
2145        b = DSP4_READ_WORD();
2146        a = DSP4_READ_WORD();
2147
2148        DSP4_OP11(a, b, c, d, &m);
2149
2150        DSP4_CLEAR_OUT();
2151        DSP4_WRITE_WORD(m);
2152
2153        break;
2154      }
2155
2156      default:
2157        break;
2158    }
2159  }
2160}
2161
2162UINT8 DSP4_read( void )
2163{
2164  UINT8 value;
2165
2166  if (DSP4.out_count)
2167  {
2168    value = (UINT8) DSP4.output[DSP4.out_index&0x1FF];
2169    DSP4.out_index++;
2170    if (DSP4.out_count == DSP4.out_index)
2171      DSP4.out_count = 0;
2172  }
2173  else
2174  {
2175    value = 0xff;
2176  }
2177
2178  return value;
2179}
src/mame/machine/snesdsp4.h
r0r5666
1#ifndef SNESDSP4_H
2#define SNESDSP4_H
3
4#define FALSE 0
5#define TRUE 1
6
7
8struct DSP4_t
9{
10  UINT8 waiting4command;
11  UINT8 half_command;
12  UINT16 command;
13  UINT32 in_count;
14  UINT32 in_index;
15  UINT32 out_count;
16  UINT32 out_index;
17  UINT8 parameters[512];
18  UINT8 output[512];
19};
20
21extern struct DSP4_t DSP4;
22
23struct DSP4_vars_t
24{
25  // op control
26  INT8 DSP4_Logic;            // controls op flow
27
28
29  // projection format
30  INT16 lcv;                  // loop-control variable
31  INT16 distance;             // z-position INTo virtual world
32  INT16 raster;               // current raster line
33  INT16 segments;             // number of raster lines drawn
34
35  // 1.15.16 or 1.15.0 [sign, INTeger, fraction]
36  INT32 world_x;              // line of x-projection in world
37  INT32 world_y;              // line of y-projection in world
38  INT32 world_dx;             // projection line x-delta
39  INT32 world_dy;             // projection line y-delta
40  INT16 world_ddx;            // x-delta increment
41  INT16 world_ddy;            // y-delta increment
42  INT32 world_xenv;           // world x-shaping factor
43  INT16 world_yofs;           // world y-vertical scroll
44
45  INT16 view_x1;              // current viewer-x
46  INT16 view_y1;              // current viewer-y
47  INT16 view_x2;              // future viewer-x
48  INT16 view_y2;              // future viewer-y
49  INT16 view_dx;              // view x-delta factor
50  INT16 view_dy;              // view y-delta factor
51  INT16 view_xofs1;           // current viewer x-vertical scroll
52  INT16 view_yofs1;           // current viewer y-vertical scroll
53  INT16 view_xofs2;           // future viewer x-vertical scroll
54  INT16 view_yofs2;           // future viewer y-vertical scroll
55  INT16 view_yofsenv;         // y-scroll shaping factor
56  INT16 view_turnoff_x;       // road turnoff data
57  INT16 view_turnoff_dx;      // road turnoff delta factor
58
59
60  // drawing area
61
62  INT16 viewport_cx;          // x-center of viewport window
63  INT16 viewport_cy;          // y-center of render window
64  INT16 viewport_left;        // x-left of viewport
65  INT16 viewport_right;       // x-right of viewport
66  INT16 viewport_top;         // y-top of viewport
67  INT16 viewport_bottom;      // y-bottom of viewport
68
69
70  // sprite structure
71
72  INT16 sprite_x;             // projected x-pos of sprite
73  INT16 sprite_y;             // projected y-pos of sprite
74  INT16 sprite_attr;          // obj attributes
75  UINT8 sprite_size;          // sprite size: 8x8 or 16x16
76  INT16 sprite_clipy;         // visible line to clip pixels off
77  INT16 sprite_count;
78
79  // generic projection variables designed for
80  // two solid polygons + two polygon sides
81
82  INT16 poly_clipLf[2][2];    // left clip boundary
83  INT16 poly_clipRt[2][2];    // right clip boundary
84  INT16 poly_ptr[2][2];       // HDMA structure poINTers
85  INT16 poly_raster[2][2];    // current raster line below horizon
86  INT16 poly_top[2][2];       // top clip boundary
87  INT16 poly_bottom[2][2];    // bottom clip boundary
88  INT16 poly_cx[2][2];        // center for left/right poINTs
89  INT16 poly_start[2];        // current projection poINTs
90  INT16 poly_plane[2];        // previous z-plane distance
91
92
93  // OAM
94  INT16 OAM_attr[16];         // OAM (size,MSB) data
95  INT16 OAM_index;            // index INTo OAM table
96  INT16 OAM_bits;             // offset INTo OAM table
97
98  INT16 OAM_RowMax;           // maximum number of tiles per 8 aligned pixels (row)
99  INT16 OAM_Row[32];          // current number of tiles per row
100};
101
102extern struct DSP4_vars_t DSP4_vars;
103
104#endif
src/mame/machine/snes.c
r5665r5666
5858// add-on chip emulators
5959#include "machine/snesdsp1.c"
6060#include "machine/snesdsp2.c"
61#include "machine/snesdsp3.c"
62#include "machine/snesdsp4.c"
6163#include "machine/snesobc1.c"
6264#include "machine/snesrtc.c"
6365#include "machine/snessdd1.c"
r5665r5666
14751477      value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr();
14761478   else if ((snes_cart.mode == SNES_MODE_20) && (snes_has_addon_chip == HAS_DSP2) && (offset >= 0x200000))
14771479      value = (address < 0xc000) ? DSP2_read() : 0x00;
1480   else if ((snes_has_addon_chip == HAS_DSP3) && (offset >= 0x200000))
1481      value = DSP3_read(address);
14781482   else
14791483      value = snes_ram[offset];
14801484
r5665r5666
15141518      value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr();
15151519   else if ((snes_cart.mode == SNES_MODE_20) && (snes_has_addon_chip == HAS_DSP2))
15161520      value = (address < 0xc000) ? DSP2_read() : 0x00;
1521   else if (snes_has_addon_chip == HAS_DSP3)
1522      value = DSP3_read(address);
1523   else if (snes_has_addon_chip == HAS_DSP4)
1524      value = (address < 0xc000) ? DSP4_read() : 0x80;
15171525   else
15181526      value = snes_ram[0x300000 + offset];
15191527
r5665r5666
16371645      value = (address < 0xc000) ? DSP1_getDr() : DSP1_getSr();
16381646   else if ((snes_cart.mode == SNES_MODE_20) && (snes_has_addon_chip == HAS_DSP2) && (offset >= 0x200000))
16391647      value = (address < 0xc000) ? DSP2_read() : 0x00;
1648   else if ((snes_has_addon_chip == HAS_DSP3) && (offset >= 0x200000))
1649      value = DSP3_read(address);
1650   else if ((snes_has_addon_chip == HAS_DSP4) && (offset >= 0x300000))
1651      value = (address < 0xc000) ? DSP4_read() : 0x80;
16401652   else
16411653      value = snes_ram[0x800000 + offset];
16421654
r5665r5666
16991711      DSP1_setDr(data);
17001712   else if ((snes_cart.mode == SNES_MODE_20) && (snes_has_addon_chip == HAS_DSP2) && (offset >= 0x200000) && (address < 0xc000))
17011713      DSP2_write(data);
1714   else if ((snes_has_addon_chip == HAS_DSP3) && (offset >= 0x200000))
1715      DSP3_write(address, data);
17021716   else
17031717      logerror( "Attempt to write to ROM address: %X\n", offset );
17041718}
r5665r5666
17331747      DSP1_setDr(data);
17341748   else if ((snes_cart.mode == SNES_MODE_20) && (snes_has_addon_chip == HAS_DSP2) && (address < 0xc000))
17351749      DSP2_write(data);
1750   else if (snes_has_addon_chip == HAS_DSP3)
1751      DSP3_write(address, data);
1752   else if ((snes_has_addon_chip == HAS_DSP4) && (address < 0xc000))
1753      DSP4_write(data);
17361754   else
17371755      logerror("Attempt to write to ROM address: %X\n", offset + 0x300000);
17381756}
r5665r5666
18371855      DSP1_setDr(data);
18381856   else if ((snes_cart.mode == SNES_MODE_20) && (snes_has_addon_chip == HAS_DSP2) && (offset >= 0x200000) && (address < 0xc000))
18391857      DSP2_write(data);
1858   else if ((snes_has_addon_chip == HAS_DSP3) && (offset >= 0x200000))
1859      DSP3_write(address, data);
1860   else if ((snes_has_addon_chip == HAS_DSP4) && (offset >= 0x300000) && (address < 0xc000))
1861      DSP4_write(data);
18401862   else if (snes_has_addon_chip == HAS_SUPERFX && cputag_get_cpu(space->machine, "superfx") != NULL)
18411863      logerror( "snes_w_bank6 hit (ROM) in Super FX mode, please fix me\n" );
18421864   else
r5665r5666
19571979         DSP2_reset();
19581980         break;
19591981
1982      case HAS_DSP3:
1983         InitDSP3();
1984         break;
1985
1986      case HAS_DSP4:
1987         InitDSP4();
1988         break;
1989
19601990      case HAS_OBC1:
19611991         obc1_init();
19621992         break;
src/mame/mame.mak
r5665r5666
18481848$(DRIVERS)/neogeo.o:   $(MAMESRC)/drivers/neodrvr.c
18491849$(MACHINE)/snes.o:       $(MAMESRC)/machine/snesdsp1.c \
18501850            $(MAMESRC)/machine/snesdsp2.c \
1851            $(MAMESRC)/machine/snesdsp3.c \
1852            $(MAMESRC)/machine/snesdsp4.c \
18511853            $(MAMESRC)/machine/snesobc1.c \
18521854            $(MAMESRC)/machine/snesrtc.c \
18531855            $(MAMESRC)/machine/snessdd1.c
src/mess/mess.mak
r5665r5666
15261526
15271527$(MAME_MACHINE)/snes.o:    $(MAMESRC)/machine/snesdsp1.c \
15281528            $(MAMESRC)/machine/snesdsp2.c \
1529            $(MAMESRC)/machine/snesdsp3.c \
1530            $(MAMESRC)/machine/snesdsp4.c \
15291531            $(MAMESRC)/machine/snesobc1.c \
15301532            $(MAMESRC)/machine/snesrtc.c \
15311533            $(MAMESRC)/machine/snessdd1.c
docs/messnew.txt
r5665r5666
9090
9191- [SNES] Added support for SuperFX games. [Harmony]
9292
93- [SNES] Added support for DSP-3 and DSP-4 add-on chips. [ZSNES Team, Fabio
94  Priuli]
95
9396- [SNES] Added preliminary support for S-DD1 and S-RTC emulation. [Harmony]
9497
9598- [TI81] Added v2.0 BIOS (possibly bad dump). [FloppusMaximus]

Previous 15994 Revisions Next


© 1998-2018 The MESS Team