Previous 15994 Revisions Next

r9894 Sunday 2nd January, 2011 at 22:15:02 UTC by R. Belmont
SNES: Remove now-unused HLE.
[src/mame/machine]snesdsp1.c snesdsp2.c snesdsp3.c snesdsp4.c snesdsp4.h

src/mame/machine/snesdsp2.c
r9893r9894
1/***************************************************************************
2
3  snesdsp2.c
4
5  File to handle emulation of the SNES "DSP-2" add-on chip.
6
7  Original C++ code by byuu, based on research by Overload.
8  Byuu's code is released 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
11  for use in MAME, MESS and derivatives by permission of the
12  author
13
14***************************************************************************/
15
16struct _snes_dsp2_state
17{
18   int waiting_for_command;
19   int command;
20   int in_count, in_index;
21   int out_count, out_index;
22
23   UINT8 parameters[512];
24   UINT8 output[512];
25
26   UINT8 op05transparent;
27   int op05haslen;
28   int op05len;
29   int op06haslen;
30   int op06len;
31   UINT16 op09word1;
32   UINT16 op09word2;
33   int op0dhaslen;
34   int op0doutlen;
35   int op0dinlen;
36};
37
38static struct _snes_dsp2_state  dsp2_state;
39
40
41static void dsp2_register_save(running_machine *machine);
42
43//convert bitmap to bitplane tile
44static void dsp2_op01( void )
45{
46//op01 size is always 32 bytes input and output
47//the hardware does strange things if you vary the size
48
49   unsigned char c0, c1, c2, c3;
50   unsigned char *p1  = dsp2_state.parameters;
51   unsigned char *p2a = dsp2_state.output;
52   unsigned char *p2b = dsp2_state.output + 16; //halfway
53   int j;
54
55//process 8 blocks of 4 bytes each
56   for (j = 0; j < 8; j++)
57   {
58      c0 = *p1++;
59      c1 = *p1++;
60      c2 = *p1++;
61      c3 = *p1++;
62
63      *p2a++ = (c0 & 0x10) << 3 |
64            (c0 & 0x01) << 6 |
65            (c1 & 0x10) << 1 |
66            (c1 & 0x01) << 4 |
67            (c2 & 0x10) >> 1 |
68            (c2 & 0x01) << 2 |
69            (c3 & 0x10) >> 3 |
70            (c3 & 0x01);
71
72      *p2a++ = (c0 & 0x20) << 2 |
73            (c0 & 0x02) << 5 |
74            (c1 & 0x20)      |
75            (c1 & 0x02) << 3 |
76            (c2 & 0x20) >> 2 |
77            (c2 & 0x02) << 1 |
78            (c3 & 0x20) >> 4 |
79            (c3 & 0x02) >> 1;
80
81      *p2b++ = (c0 & 0x40) << 1 |
82            (c0 & 0x04) << 4 |
83            (c1 & 0x40) >> 1 |
84            (c1 & 0x04) << 2 |
85            (c2 & 0x40) >> 3 |
86            (c2 & 0x04)      |
87            (c3 & 0x40) >> 5 |
88            (c3 & 0x04) >> 2;
89
90      *p2b++ = (c0 & 0x80)      |
91            (c0 & 0x08) << 3 |
92            (c1 & 0x80) >> 2 |
93            (c1 & 0x08) << 1 |
94            (c2 & 0x80) >> 4 |
95            (c2 & 0x08) >> 1 |
96            (c3 & 0x80) >> 6 |
97            (c3 & 0x08) >> 3;
98   }
99}
100
101//set transparent color
102static void dsp2_op03( void )
103{
104   dsp2_state.op05transparent = dsp2_state.parameters[0];
105}
106
107//replace bitmap using transparent color
108static void dsp2_op05( void )
109{
110   UINT8 color;
111// Overlay bitmap with transparency.
112// Input:
113//
114//   Bitmap 1:  i[0] <=> i[size-1]
115//   Bitmap 2:  i[size] <=> i[2*size-1]
116//
117// Output:
118//
119//   Bitmap 3:  o[0] <=> o[size-1]
120//
121// Processing:
122//
123//   Process all 4-bit pixels (nibbles) in the bitmap
124//
125//   if ( BM2_pixel == transparent_color )
126//      pixelout = BM1_pixel
127//   else
128//      pixelout = BM2_pixel
129
130// The max size bitmap is limited to 255 because the size parameter is a byte
131// I think size=0 is an error.  The behavior of the chip on size=0 is to
132// return the last value written to DR if you read DR on Op05 with
133// size = 0.  I don't think it's worth implementing this quirk unless it's
134// proven necessary.
135
136   unsigned char c1, c2;
137   unsigned char *p1 = dsp2_state.parameters;
138   unsigned char *p2 = dsp2_state.parameters + dsp2_state.op05len;
139   unsigned char *p3 = dsp2_state.output;
140   int n;
141
142   color = dsp2_state.op05transparent & 0x0f;
143
144   for (n = 0; n < dsp2_state.op05len; n++)
145   {
146      c1 = *p1++;
147      c2 = *p2++;
148      *p3++ = (((c2 >> 4) == color) ? c1 & 0xf0 : c2 & 0xf0) |
149            (((c2 & 0x0f) == color) ? c1 & 0x0f : c2 & 0x0f);
150   }
151}
152
153//reverse bitmap
154static void dsp2_op06( void )
155{
156// Input:
157//    size
158//    bitmap
159   int i, j;
160   for (i = 0, j = dsp2_state.op06len - 1; i < dsp2_state.op06len; i++, j--)
161   {
162      dsp2_state.output[j] = (dsp2_state.parameters[i] << 4) | (dsp2_state.parameters[i] >> 4);
163   }
164}
165
166//multiply
167static void dsp2_op09( void )
168{
169   UINT32 r = 0;
170   dsp2_state.out_count = 4;
171
172   dsp2_state.op09word1 = dsp2_state.parameters[0] | (dsp2_state.parameters[1] << 8);
173   dsp2_state.op09word2 = dsp2_state.parameters[2] | (dsp2_state.parameters[3] << 8);
174
175   r = dsp2_state.op09word1 * dsp2_state.op09word2;
176   dsp2_state.output[0] = r;
177   dsp2_state.output[1] = r >> 8;
178   dsp2_state.output[2] = r >> 16;
179   dsp2_state.output[3] = r >> 24;
180}
181
182//scale bitmap
183static void dsp2_op0d( void )
184{
185// Bit accurate hardware algorithm - uses fixed point math
186// This should match the DSP2 Op0D output exactly
187// I wouldn't recommend using this unless you're doing hardware debug.
188// In some situations it has small visual artifacts that
189// are not readily apparent on a TV screen but show up clearly
190// on a monitor.  Use Overload's scaling instead.
191// This is for hardware verification testing.
192//
193// One note:  the HW can do odd byte scaling but since we divide
194// by two to get the count of bytes this won't work well for
195// odd byte scaling (in any of the current algorithm implementations).
196// So far I haven't seen Dungeon Master use it.
197// If it does we can adjust the parameters and code to work with it
198
199   UINT32 multiplier; // Any size int >= 32-bits
200   UINT32 pixloc;     // match size of multiplier
201   int    i, j;
202   UINT8  pixelarray[512];
203   if (dsp2_state.op0dinlen <= dsp2_state.op0doutlen)
204   {
205      multiplier = 0x10000; // In our self defined fixed point 0x10000 == 1
206   }
207   else
208   {
209      multiplier = (dsp2_state.op0dinlen << 17) / ((dsp2_state.op0doutlen << 1) + 1);
210   }
211
212   pixloc = 0;
213   for (i = 0; i < dsp2_state.op0doutlen * 2; i++)
214   {
215      j = pixloc >> 16;
216
217      if (j & 1)
218         pixelarray[i] = (dsp2_state.parameters[j >> 1] & 0x0f);
219      else
220         pixelarray[i] = (dsp2_state.parameters[j >> 1] & 0xf0) >> 4;
221
222      pixloc += multiplier;
223   }
224
225   for (i = 0; i < dsp2_state.op0doutlen; i++)
226   {
227      dsp2_state.output[i] = (pixelarray[i << 1] << 4) | pixelarray[(i << 1) + 1];
228   }
229}
230
231
232static void dsp2_init( running_machine *machine )
233{
234   dsp2_state.waiting_for_command = 1;
235   dsp2_state.in_count  = 0;
236   dsp2_state.in_index  = 0;
237   dsp2_state.out_count = 0;
238   dsp2_state.out_index = 0;
239
240   memset(dsp2_state.parameters, 0 , ARRAY_LENGTH(dsp2_state.parameters));
241   memset(dsp2_state.output, 0 , ARRAY_LENGTH(dsp2_state.output));
242
243   dsp2_state.op05transparent = 0;
244   dsp2_state.op05haslen      = 0;
245   dsp2_state.op05len         = 0;
246   dsp2_state.op06haslen      = 0;
247   dsp2_state.op06len         = 0;
248   dsp2_state.op09word1       = 0;
249   dsp2_state.op09word2       = 0;
250   dsp2_state.op0dhaslen      = 0;
251   dsp2_state.op0doutlen      = 0;
252   dsp2_state.op0dinlen       = 0;
253
254   dsp2_register_save(machine);
255}
256
257static UINT8 dsp2_dr_read( void )
258{
259   UINT8 r = 0xff;
260   if (dsp2_state.out_count)
261   {
262      r = dsp2_state.output[dsp2_state.out_index++];
263      dsp2_state.out_index &= 511;
264      if (dsp2_state.out_count == dsp2_state.out_index)
265         dsp2_state.out_count = 0;
266   }
267   return r;
268}
269
270static void dsp2_dr_write(UINT8 data)
271{
272   if (dsp2_state.waiting_for_command)
273   {
274      dsp2_state.command  = data;
275      dsp2_state.in_index = 0;
276      dsp2_state.waiting_for_command = 0;
277
278      switch (data)
279      {
280         case 0x01: dsp2_state.in_count = 32; break;
281         case 0x03: dsp2_state.in_count =  1; break;
282         case 0x05: dsp2_state.in_count =  1; break;
283         case 0x06: dsp2_state.in_count =  1; break;
284         case 0x07: break;
285         case 0x08: break;
286         case 0x09: dsp2_state.in_count =  4; break;
287         case 0x0d: dsp2_state.in_count =  2; break;
288         case 0x0f: dsp2_state.in_count =  0; break;
289      }
290   }
291   else
292   {
293      dsp2_state.parameters[dsp2_state.in_index++] = data;
294      dsp2_state.in_index &= 511;
295   }
296
297   if (dsp2_state.in_count == dsp2_state.in_index)
298   {
299      dsp2_state.waiting_for_command = 1;
300      dsp2_state.out_index = 0;
301      switch (dsp2_state.command)
302      {
303      case 0x01:
304         dsp2_state.out_count = 32;
305         dsp2_op01();
306         break;
307
308      case 0x03:
309         dsp2_op03();
310         break;
311
312      case 0x05:
313         if (dsp2_state.op05haslen)
314         {
315            dsp2_state.op05haslen = 0;
316            dsp2_state.out_count  = dsp2_state.op05len;
317            dsp2_op05();
318         }
319         else
320         {
321            dsp2_state.op05len    = dsp2_state.parameters[0];
322            dsp2_state.in_index   = 0;
323            dsp2_state.in_count   = dsp2_state.op05len * 2;
324            dsp2_state.op05haslen = 1;
325            if (data)
326               dsp2_state.waiting_for_command = 0;
327         }
328         break;
329
330      case 0x06:
331         if (dsp2_state.op06haslen)
332         {
333            dsp2_state.op06haslen = 0;
334            dsp2_state.out_count  = dsp2_state.op06len;
335            dsp2_op06();
336         }
337         else
338         {
339            dsp2_state.op06len    = dsp2_state.parameters[0];
340            dsp2_state.in_index   = 0;
341            dsp2_state.in_count   = dsp2_state.op06len;
342            dsp2_state.op06haslen = 1;
343            if (data)
344               dsp2_state.waiting_for_command = 0;
345         }
346         break;
347
348      case 0x07: break;
349      case 0x08: break;
350
351      case 0x09:
352         dsp2_op09();
353         break;
354
355      case 0x0d:
356         if (dsp2_state.op0dhaslen)
357         {
358            dsp2_state.op0dhaslen = 0;
359            dsp2_state.out_count  = dsp2_state.op0doutlen;
360            dsp2_op0d();
361         }
362         else
363         {
364            dsp2_state.op0dinlen  = dsp2_state.parameters[0];
365            dsp2_state.op0doutlen = dsp2_state.parameters[1];
366            dsp2_state.in_index   = 0;
367            dsp2_state.in_count   = (dsp2_state.op0dinlen + 1) >> 1;
368            dsp2_state.op0dhaslen = 1;
369            if (data)
370               dsp2_state.waiting_for_command = 0;
371         }
372         break;
373
374      case 0x0f:
375         break;
376      }
377   }
378}
379
380static UINT8 dsp2_sr_read( void )
381{
382   return 0x00;
383}
384
385static void dsp2_sr_write( UINT8 data )
386{
387   // do nothing
388}
389
390static void dsp2_register_save( running_machine *machine )
391{
392   state_save_register_global(machine, dsp2_state.waiting_for_command);
393   state_save_register_global(machine, dsp2_state.command);
394   state_save_register_global(machine, dsp2_state.in_count);
395   state_save_register_global(machine, dsp2_state.in_index);
396   state_save_register_global(machine, dsp2_state.out_count);
397   state_save_register_global(machine, dsp2_state.out_index);
398
399   state_save_register_global_array(machine, dsp2_state.parameters);
400   state_save_register_global_array(machine, dsp2_state.output);
401
402   state_save_register_global(machine, dsp2_state.op05transparent);
403   state_save_register_global(machine, dsp2_state.op05haslen);
404   state_save_register_global(machine, dsp2_state.op05len);
405   state_save_register_global(machine, dsp2_state.op06haslen);
406   state_save_register_global(machine, dsp2_state.op06len);
407   state_save_register_global(machine, dsp2_state.op09word1);
408   state_save_register_global(machine, dsp2_state.op09word2);
409   state_save_register_global(machine, dsp2_state.op0dhaslen);
410   state_save_register_global(machine, dsp2_state.op0doutlen);
411   state_save_register_global(machine, dsp2_state.op0dinlen);
412}
src/mame/machine/snesdsp3.c
r9893r9894
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
15static void dsp3_Command(void);
16
17struct _snes_dsp3_state
18{
19   UINT16  DR;
20   UINT16  SR;
21   UINT16  MemoryIndex;
22
23   INT16   AddLo;
24   INT16   AddHi;
25   INT16   WinLo;
26   INT16   WinHi;
27   UINT16  Codewords;
28   UINT16  Outwords;
29   UINT16  Symbol;
30   UINT16  BitCount;
31   UINT16  Index;
32   UINT16  Codes[512];
33   UINT16  BitsLeft;
34   UINT16  ReqBits;
35   UINT16  ReqData;
36   UINT16  BitCommand;
37   UINT8   BaseLength;
38   UINT16  BaseCodes;
39   UINT16  BaseCode;
40   UINT8   CodeLengths[8];
41   UINT16  CodeOffsets[8];
42   UINT16  LZCode;
43   UINT8   LZLength;
44
45   UINT16  X;
46   UINT16  Y;
47
48   UINT8   Bitmap[8];
49   UINT8   Bitplane[8];
50   UINT16  BMIndex;
51   UINT16  BPIndex;
52   UINT16  Count;
53
54   INT16 op3e_x;
55   INT16 op3e_y;
56
57   INT16 op1e_terrain[0x2000];
58   INT16 op1e_cost[0x2000];
59   INT16 op1e_weight[0x2000];
60
61   INT16 op1e_cell;
62   INT16 op1e_turn;
63   INT16 op1e_search;
64
65   INT16 op1e_x;
66   INT16 op1e_y;
67
68   INT16 op1e_min_radius;
69   INT16 op1e_max_radius;
70
71   INT16 op1e_max_search_radius;
72   INT16 op1e_max_path_radius;
73
74   INT16 op1e_lcv_radius;
75   INT16 op1e_lcv_steps;
76   INT16 op1e_lcv_turns;
77
78   void(*SetDSP3)(void);
79
80   UINT16 DataROM[1024];
81};
82
83static struct _snes_dsp3_state  dsp3_state;
84
85
86static void dsp3_Reset(void)
87{
88   dsp3_state.DR = 0x0080;
89   dsp3_state.SR = 0x0084;
90   dsp3_state.SetDSP3 = &dsp3_Command;
91}
92
93#ifdef UNUSED_FUNCTION
94void dsp3_MemorySize(void)
95{
96   dsp3_state.DR = 0x0300;
97   dsp3_state.SetDSP3 = &dsp3_Reset;
98}
99#endif
100
101static void dsp3_TestMemory(void)
102{
103   dsp3_state.DR = 0x0000;
104   dsp3_state.SetDSP3 = &dsp3_Reset;
105}
106
107static void dsp3_DumpDataROM(void)
108{
109   dsp3_state.DR = dsp3_state.DataROM[dsp3_state.MemoryIndex++];
110   if (dsp3_state.MemoryIndex == 1024)
111      dsp3_state.SetDSP3 = &dsp3_Reset;
112}
113
114static void dsp3_MemoryDump(void)
115{
116   dsp3_state.MemoryIndex = 0;
117   dsp3_state.SetDSP3 = &dsp3_DumpDataROM;
118   dsp3_DumpDataROM();
119}
120
121static void dsp3_OP06(void)
122{
123   dsp3_state.WinLo = (UINT8)(dsp3_state.DR);
124   dsp3_state.WinHi = (UINT8)(dsp3_state.DR >> 8);
125   dsp3_Reset();
126}
127
128static void dsp3_OP03(void)
129{
130   INT16 Lo = (UINT8)(dsp3_state.DR);
131   INT16 Hi = (UINT8)(dsp3_state.DR >> 8);
132   INT16 Ofs = (dsp3_state.WinLo * Hi << 1) + (Lo << 1);
133   dsp3_state.DR = Ofs >> 1;
134   dsp3_state.SetDSP3 = &dsp3_Reset;
135}
136
137static void dsp3_OP07_B(void)
138{
139   INT16 Ofs = (dsp3_state.WinLo * dsp3_state.AddHi << 1) + (dsp3_state.AddLo << 1);
140   dsp3_state.DR = Ofs >> 1;
141   dsp3_state.SetDSP3 = &dsp3_Reset;
142}
143
144static void dsp3_OP07_A(void)
145{
146   INT16 Lo = (UINT8)(dsp3_state.DR);
147   INT16 Hi = (UINT8)(dsp3_state.DR >> 8);
148
149   if (Lo & 1)
150      Hi += (dsp3_state.AddLo & 1);
151
152   dsp3_state.AddLo += Lo;
153   dsp3_state.AddHi += Hi;
154
155   if (dsp3_state.AddLo < 0)
156      dsp3_state.AddLo += dsp3_state.WinLo;
157   else if (dsp3_state.AddLo >= dsp3_state.WinLo)
158      dsp3_state.AddLo -= dsp3_state.WinLo;
159
160   if (dsp3_state.AddHi < 0)
161      dsp3_state.AddHi += dsp3_state.WinHi;
162   else if (dsp3_state.AddHi >= dsp3_state.WinHi)
163      dsp3_state.AddHi -= dsp3_state.WinHi;
164
165   dsp3_state.DR = dsp3_state.AddLo | (dsp3_state.AddHi << 8) | ((dsp3_state.AddHi >> 8) & 0xff);
166   dsp3_state.SetDSP3 = &dsp3_OP07_B;
167}
168
169static void dsp3_OP07(void)
170{
171   UINT32 dataOfs = ((dsp3_state.DR << 1) + 0x03b2) & 0x03ff;
172
173   dsp3_state.AddHi = dsp3_state.DataROM[dataOfs];
174   dsp3_state.AddLo = dsp3_state.DataROM[dataOfs + 1];
175
176   dsp3_state.SetDSP3 = &dsp3_OP07_A;
177   dsp3_state.SR = 0x0080;
178}
179
180static void dsp3_Coordinate(void)
181{
182   dsp3_state.Index++;
183
184   switch (dsp3_state.Index)
185   {
186   case 3:
187      if (dsp3_state.DR == 0xffff)
188         dsp3_Reset();
189      break;
190   case 4:
191      dsp3_state.X = dsp3_state.DR;
192      break;
193   case 5:
194      dsp3_state.Y = dsp3_state.DR;
195      dsp3_state.DR = 1;
196      break;
197   case 6:
198      dsp3_state.DR = dsp3_state.X;
199      break;
200   case 7:
201      dsp3_state.DR = dsp3_state.Y;
202      dsp3_state.Index = 0;
203      break;
204   }
205}
206
207static void dsp3_Convert_A(void)
208{
209   if (dsp3_state.BMIndex < 8)
210   {
211      dsp3_state.Bitmap[dsp3_state.BMIndex++] = (UINT8) (dsp3_state.DR);
212      dsp3_state.Bitmap[dsp3_state.BMIndex++] = (UINT8) (dsp3_state.DR >> 8);
213
214      if (dsp3_state.BMIndex == 8)
215      {
216         short i, j;
217         for (i = 0; i < 8; i++)
218            for (j = 0; j < 8; j++)
219            {
220               dsp3_state.Bitplane[j] <<= 1;
221               dsp3_state.Bitplane[j] |= (dsp3_state.Bitmap[i] >> j) & 1;
222            }
223
224         dsp3_state.BPIndex = 0;
225         dsp3_state.Count--;
226      }
227   }
228
229   if (dsp3_state.BMIndex == 8)
230   {
231      if (dsp3_state.BPIndex == 8)
232      {
233         if (!dsp3_state.Count)
234            dsp3_Reset();
235         dsp3_state.BMIndex = 0;
236      }
237      else
238      {
239         dsp3_state.DR =  dsp3_state.Bitplane[dsp3_state.BPIndex++];
240         dsp3_state.DR |= dsp3_state.Bitplane[dsp3_state.BPIndex++] << 8;
241      }
242   }
243}
244
245static void dsp3_Convert(void)
246{
247   dsp3_state.Count = dsp3_state.DR;
248   dsp3_state.BMIndex = 0;
249   dsp3_state.SetDSP3 = &dsp3_Convert_A;
250}
251
252static UINT8 dsp3_GetBits(UINT8 Count)
253{
254   if (!dsp3_state.BitsLeft)
255   {
256      dsp3_state.BitsLeft = Count;
257      dsp3_state.ReqBits = 0;
258   }
259
260   do {
261      if (!dsp3_state.BitCount)
262      {
263         dsp3_state.SR = 0xC0;
264         return 0;
265      }
266
267      dsp3_state.ReqBits <<= 1;
268      if (dsp3_state.ReqData & 0x8000)
269         dsp3_state.ReqBits++;
270      dsp3_state.ReqData <<= 1;
271
272      dsp3_state.BitCount--;
273      dsp3_state.BitsLeft--;
274
275   } while (dsp3_state.BitsLeft);
276
277   return 1;
278}
279
280static void dsp3_Decode_Data(void)
281{
282   if (!dsp3_state.BitCount)
283   {
284      if (dsp3_state.SR & 0x40)
285      {
286         dsp3_state.ReqData = dsp3_state.DR;
287         dsp3_state.BitCount += 16;
288      }
289      else
290      {
291         dsp3_state.SR = 0xC0;
292         return;
293      }
294   }
295
296   if (dsp3_state.LZCode == 1)
297   {
298      if (!dsp3_GetBits(1))
299         return;
300
301      if (dsp3_state.ReqBits)
302         dsp3_state.LZLength = 12;
303      else
304         dsp3_state.LZLength = 8;
305
306      dsp3_state.LZCode++;
307   }
308
309   if (dsp3_state.LZCode == 2)
310   {
311      if (!dsp3_GetBits(dsp3_state.LZLength))
312         return;
313
314      dsp3_state.LZCode = 0;
315      dsp3_state.Outwords--;
316      if (!dsp3_state.Outwords) dsp3_state.SetDSP3 = &dsp3_Reset;
317
318      dsp3_state.SR = 0x80;
319      dsp3_state.DR = dsp3_state.ReqBits;
320      return;
321   }
322
323   if (dsp3_state.BaseCode == 0xffff)
324   {
325      if (!dsp3_GetBits(dsp3_state.BaseLength))
326         return;
327
328      dsp3_state.BaseCode = dsp3_state.ReqBits;
329   }
330
331   if (!dsp3_GetBits(dsp3_state.CodeLengths[dsp3_state.BaseCode]))
332      return;
333
334   dsp3_state.Symbol = dsp3_state.Codes[dsp3_state.CodeOffsets[dsp3_state.BaseCode] + dsp3_state.ReqBits];
335   dsp3_state.BaseCode = 0xffff;
336
337   if (dsp3_state.Symbol & 0xff00)
338   {
339      dsp3_state.Symbol += 0x7f02;
340      dsp3_state.LZCode++;
341   }
342   else
343   {
344      dsp3_state.Outwords--;
345      if (!dsp3_state.Outwords)
346         dsp3_state.SetDSP3 = &dsp3_Reset;
347   }
348
349   dsp3_state.SR = 0x80;
350   dsp3_state.DR = dsp3_state.Symbol;
351}
352
353static void dsp3_Decode_Tree(void)
354{
355   if (!dsp3_state.BitCount)
356   {
357      dsp3_state.ReqData = dsp3_state.DR;
358      dsp3_state.BitCount += 16;
359   }
360
361   if (!dsp3_state.BaseCodes)
362   {
363      dsp3_GetBits(1);
364      if (dsp3_state.ReqBits)
365      {
366         dsp3_state.BaseLength = 3;
367         dsp3_state.BaseCodes = 8;
368      }
369      else
370      {
371         dsp3_state.BaseLength = 2;
372         dsp3_state.BaseCodes = 4;
373      }
374   }
375
376   while (dsp3_state.BaseCodes)
377   {
378      if (!dsp3_GetBits(3))
379         return;
380
381      dsp3_state.ReqBits++;
382
383      dsp3_state.CodeLengths[dsp3_state.Index] = (UINT8) dsp3_state.ReqBits;
384      dsp3_state.CodeOffsets[dsp3_state.Index] = dsp3_state.Symbol;
385      dsp3_state.Index++;
386
387      dsp3_state.Symbol += 1 << dsp3_state.ReqBits;
388      dsp3_state.BaseCodes--;
389   }
390
391   dsp3_state.BaseCode = 0xffff;
392   dsp3_state.LZCode = 0;
393
394   dsp3_state.SetDSP3 = &dsp3_Decode_Data;
395   if (dsp3_state.BitCount)
396      dsp3_Decode_Data();
397}
398
399static void dsp3_Decode_Symbols(void)
400{
401   dsp3_state.ReqData = dsp3_state.DR;
402   dsp3_state.BitCount += 16;
403
404   do {
405
406      if (dsp3_state.BitCommand == 0xffff)
407      {
408         if (!dsp3_GetBits(2)) return;
409         dsp3_state.BitCommand = dsp3_state.ReqBits;
410      }
411
412      switch (dsp3_state.BitCommand)
413      {
414      case 0:
415         {
416            if (!dsp3_GetBits(9)) return;
417            dsp3_state.Symbol = dsp3_state.ReqBits;
418            break;
419         }
420      case 1:
421         {
422            dsp3_state.Symbol++;
423            break;
424         }
425      case 2:
426         {
427            if (!dsp3_GetBits(1)) return;
428            dsp3_state.Symbol += 2 + dsp3_state.ReqBits;
429            break;
430         }
431      case 3:
432         {
433            if (!dsp3_GetBits(4)) return;
434            dsp3_state.Symbol += 4 + dsp3_state.ReqBits;
435            break;
436         }
437      }
438
439      dsp3_state.BitCommand = 0xffff;
440
441      dsp3_state.Codes[dsp3_state.Index++] = dsp3_state.Symbol;
442      dsp3_state.Codewords--;
443
444   } while (dsp3_state.Codewords);
445
446   dsp3_state.Index = 0;
447   dsp3_state.Symbol = 0;
448   dsp3_state.BaseCodes = 0;
449
450   dsp3_state.SetDSP3 = &dsp3_Decode_Tree;
451   if (dsp3_state.BitCount) dsp3_Decode_Tree();
452}
453
454static void dsp3_Decode_A(void)
455{
456   dsp3_state.Outwords = dsp3_state.DR;
457   dsp3_state.SetDSP3 = &dsp3_Decode_Symbols;
458   dsp3_state.BitCount = 0;
459   dsp3_state.BitsLeft = 0;
460   dsp3_state.Symbol = 0;
461   dsp3_state.Index = 0;
462   dsp3_state.BitCommand = 0xffff;
463   dsp3_state.SR = 0xC0;
464}
465
466static void dsp3_Decode(void)
467{
468   dsp3_state.Codewords = dsp3_state.DR;
469   dsp3_state.SetDSP3 = &dsp3_Decode_A;
470}
471
472
473// Opcodes 1E/3E bit-perfect to 'dsp3-intro' log
474// src: adapted from SD Gundam X/G-Next
475
476static void dsp3_OP3E(void)
477{
478   dsp3_state.op3e_x = (UINT8)(dsp3_state.DR & 0x00ff);
479   dsp3_state.op3e_y = (UINT8)((dsp3_state.DR & 0xff00) >> 8);
480
481   dsp3_OP03();
482
483   dsp3_state.op1e_terrain[dsp3_state.DR] = 0x00;
484   dsp3_state.op1e_cost[dsp3_state.DR] = 0xff;
485   dsp3_state.op1e_weight[dsp3_state.DR] = 0;
486
487   dsp3_state.op1e_max_search_radius = 0;
488   dsp3_state.op1e_max_path_radius = 0;
489}
490
491static void dsp3_OP1E_A(void);
492static void dsp3_OP1E_A1(void);
493static void dsp3_OP1E_A2(void);
494static void dsp3_OP1E_A3(void);
495
496static void dsp3_OP1E_B(void);
497static void dsp3_OP1E_B1(void);
498static void dsp3_OP1E_B2(void);
499
500static void dsp3_OP1E_C(void);
501static void dsp3_OP1E_C1(void);
502static void dsp3_OP1E_C2(void);
503
504static void dsp3_OP1E_D(INT16, INT16 *, INT16 *);
505static void dsp3_OP1E_D1(INT16 move, INT16 *lo, INT16 *hi);
506
507static void dsp3_OP1E(void)
508{
509   int lcv;
510
511   dsp3_state.op1e_min_radius = (UINT8)(dsp3_state.DR & 0x00ff);
512   dsp3_state.op1e_max_radius = (UINT8)((dsp3_state.DR & 0xff00)>>8);
513
514   if (dsp3_state.op1e_min_radius == 0)
515      dsp3_state.op1e_min_radius++;
516
517   if (dsp3_state.op1e_max_search_radius >= dsp3_state.op1e_min_radius)
518      dsp3_state.op1e_min_radius = dsp3_state.op1e_max_search_radius + 1;
519
520   if (dsp3_state.op1e_max_radius > dsp3_state.op1e_max_search_radius)
521      dsp3_state.op1e_max_search_radius = dsp3_state.op1e_max_radius;
522
523   dsp3_state.op1e_lcv_radius = dsp3_state.op1e_min_radius;
524   dsp3_state.op1e_lcv_steps = dsp3_state.op1e_min_radius;
525
526   dsp3_state.op1e_lcv_turns = 6;
527   dsp3_state.op1e_turn = 0;
528
529   dsp3_state.op1e_x = dsp3_state.op3e_x;
530   dsp3_state.op1e_y = dsp3_state.op3e_y;
531
532   for (lcv = 0; lcv < dsp3_state.op1e_min_radius; lcv++)
533      dsp3_OP1E_D(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
534
535   dsp3_OP1E_A();
536}
537
538static void dsp3_OP1E_A(void)
539{
540   int lcv;
541
542   if (dsp3_state.op1e_lcv_steps == 0)
543   {
544      dsp3_state.op1e_lcv_radius++;
545
546      dsp3_state.op1e_lcv_steps = dsp3_state.op1e_lcv_radius;
547
548      dsp3_state.op1e_x = dsp3_state.op3e_x;
549      dsp3_state.op1e_y = dsp3_state.op3e_y;
550
551      for (lcv = 0; lcv < dsp3_state.op1e_lcv_radius; lcv++)
552         dsp3_OP1E_D(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
553   }
554
555   if (dsp3_state.op1e_lcv_radius > dsp3_state.op1e_max_radius)
556   {
557      dsp3_state.op1e_turn++;
558      dsp3_state.op1e_lcv_turns--;
559
560      dsp3_state.op1e_lcv_radius = dsp3_state.op1e_min_radius;
561      dsp3_state.op1e_lcv_steps = dsp3_state.op1e_min_radius;
562
563      dsp3_state.op1e_x = dsp3_state.op3e_x;
564      dsp3_state.op1e_y = dsp3_state.op3e_y;
565
566      for (lcv = 0; lcv < dsp3_state.op1e_min_radius; lcv++)
567         dsp3_OP1E_D(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
568   }
569
570   if (dsp3_state.op1e_lcv_turns == 0)
571   {
572      dsp3_state.DR = 0xffff;
573      dsp3_state.SR = 0x0080;
574      dsp3_state.SetDSP3 = &dsp3_OP1E_B;
575      return;
576   }
577
578   dsp3_state.DR = (UINT8)(dsp3_state.op1e_x) | ((UINT8)(dsp3_state.op1e_y) << 8);
579   dsp3_OP03();
580
581   dsp3_state.op1e_cell = dsp3_state.DR;
582
583   dsp3_state.SR = 0x0080;
584   dsp3_state.SetDSP3 = &dsp3_OP1E_A1;
585}
586
587static void dsp3_OP1E_A1(void)
588{
589   dsp3_state.SR = 0x0084;
590   dsp3_state.SetDSP3 = &dsp3_OP1E_A2;
591}
592
593static void dsp3_OP1E_A2(void)
594{
595   dsp3_state.op1e_terrain[dsp3_state.op1e_cell] = (UINT8)(dsp3_state.DR & 0x00ff);
596
597   dsp3_state.SR = 0x0084;
598   dsp3_state.SetDSP3 = &dsp3_OP1E_A3;
599}
600
601static void dsp3_OP1E_A3(void)
602{
603   dsp3_state.op1e_cost[dsp3_state.op1e_cell] = (UINT8)(dsp3_state.DR & 0x00ff);
604
605   if (dsp3_state.op1e_lcv_radius == 1)
606   {
607      if (dsp3_state.op1e_terrain[dsp3_state.op1e_cell] & 1)
608      {
609         dsp3_state.op1e_weight[dsp3_state.op1e_cell] = 0xff;
610      }
611      else
612      {
613         dsp3_state.op1e_weight[dsp3_state.op1e_cell] = dsp3_state.op1e_cost[dsp3_state.op1e_cell];
614      }
615   }
616   else
617   {
618      dsp3_state.op1e_weight[dsp3_state.op1e_cell] = 0xff;
619   }
620
621   dsp3_OP1E_D((INT16)(dsp3_state.op1e_turn + 2), &dsp3_state.op1e_x, &dsp3_state.op1e_y);
622   dsp3_state.op1e_lcv_steps--;
623
624   dsp3_state.SR = 0x0080;
625   dsp3_OP1E_A();
626}
627
628
629static void dsp3_OP1E_B(void)
630{
631   dsp3_state.op1e_x = dsp3_state.op3e_x;
632   dsp3_state.op1e_y = dsp3_state.op3e_y;
633   dsp3_state.op1e_lcv_radius = 1;
634
635   dsp3_state.op1e_search = 0;
636
637   dsp3_OP1E_B1();
638
639   dsp3_state.SetDSP3 = &dsp3_OP1E_C;
640}
641
642
643static void dsp3_OP1E_B1(void)
644{
645   while (dsp3_state.op1e_lcv_radius < dsp3_state.op1e_max_radius)
646   {
647      dsp3_state.op1e_y--;
648
649      dsp3_state.op1e_lcv_turns = 6;
650      dsp3_state.op1e_turn = 5;
651
652      while (dsp3_state.op1e_lcv_turns)
653      {
654         dsp3_state.op1e_lcv_steps = dsp3_state.op1e_lcv_radius;
655
656         while (dsp3_state.op1e_lcv_steps)
657         {
658            dsp3_OP1E_D1(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
659
660            if (0 <= dsp3_state.op1e_y && dsp3_state.op1e_y < dsp3_state.WinHi &&
661                  0 <= dsp3_state.op1e_x && dsp3_state.op1e_x < dsp3_state.WinLo )
662            {
663               dsp3_state.DR = (UINT8)(dsp3_state.op1e_x) | ((UINT8)(dsp3_state.op1e_y) << 8);
664               dsp3_OP03();
665
666               dsp3_state.op1e_cell = dsp3_state.DR;
667               if (dsp3_state.op1e_cost[dsp3_state.op1e_cell ] < 0x80 && dsp3_state.op1e_terrain[dsp3_state.op1e_cell] < 0x40)
668               {
669                  dsp3_OP1E_B2();
670               } // end cell perimeter
671            }
672
673            dsp3_state.op1e_lcv_steps--;
674         } // end search line
675
676         dsp3_state.op1e_turn--;
677         if (dsp3_state.op1e_turn == 0)
678            dsp3_state.op1e_turn = 6;
679
680         dsp3_state.op1e_lcv_turns--;
681      } // end circle search
682
683      dsp3_state.op1e_lcv_radius++;
684   } // end radius search
685}
686
687
688static void dsp3_OP1E_B2(void)
689{
690   INT16 cell;
691   INT16 path;
692   INT16 x,y;
693   INT16 lcv_turns;
694
695   path = 0xff;
696   lcv_turns = 6;
697
698   while (lcv_turns)
699   {
700      x = dsp3_state.op1e_x;
701      y = dsp3_state.op1e_y;
702
703      dsp3_OP1E_D1(lcv_turns, &x, &y);
704
705      dsp3_state.DR = (UINT8)(x) | ((UINT8)(y)<<8);
706      dsp3_OP03();
707
708      cell = dsp3_state.DR;
709
710      if (0 <= y && y < dsp3_state.WinHi && 0 <= x && x < dsp3_state.WinLo)
711      {
712         if (dsp3_state.op1e_terrain[cell] < 0x80 || dsp3_state.op1e_weight[cell] == 0)
713         {
714            if (dsp3_state.op1e_weight[cell] < path)
715               path = dsp3_state.op1e_weight[cell];
716         }
717      } // end step travel
718
719      lcv_turns--;
720   } // end while turns
721
722   if (path != 0xff)
723      dsp3_state.op1e_weight[dsp3_state.op1e_cell] = path + dsp3_state.op1e_cost[dsp3_state.op1e_cell];
724}
725
726
727static void dsp3_OP1E_C(void)
728{
729   int lcv;
730
731   dsp3_state.op1e_min_radius = (UINT8)(dsp3_state.DR & 0x00ff);
732   dsp3_state.op1e_max_radius = (UINT8)((dsp3_state.DR & 0xff00) >> 8);
733
734   if (dsp3_state.op1e_min_radius == 0)
735      dsp3_state.op1e_min_radius++;
736
737   if (dsp3_state.op1e_max_path_radius >= dsp3_state.op1e_min_radius)
738      dsp3_state.op1e_min_radius = dsp3_state.op1e_max_path_radius + 1;
739
740   if (dsp3_state.op1e_max_radius > dsp3_state.op1e_max_path_radius)
741      dsp3_state.op1e_max_path_radius = dsp3_state.op1e_max_radius;
742
743   dsp3_state.op1e_lcv_radius = dsp3_state.op1e_min_radius;
744   dsp3_state.op1e_lcv_steps = dsp3_state.op1e_min_radius;
745
746   dsp3_state.op1e_lcv_turns = 6;
747   dsp3_state.op1e_turn = 0;
748
749   dsp3_state.op1e_x = dsp3_state.op3e_x;
750   dsp3_state.op1e_y = dsp3_state.op3e_y;
751
752   for (lcv = 0; lcv < dsp3_state.op1e_min_radius; lcv++)
753      dsp3_OP1E_D(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
754
755   dsp3_OP1E_C1();
756}
757
758
759static void dsp3_OP1E_C1(void)
760{
761   int lcv;
762
763   if (dsp3_state.op1e_lcv_steps == 0)
764   {
765      dsp3_state.op1e_lcv_radius++;
766
767      dsp3_state.op1e_lcv_steps = dsp3_state.op1e_lcv_radius;
768
769      dsp3_state.op1e_x = dsp3_state.op3e_x;
770      dsp3_state.op1e_y = dsp3_state.op3e_y;
771
772      for (lcv = 0; lcv < dsp3_state.op1e_lcv_radius; lcv++)
773         dsp3_OP1E_D(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
774   }
775
776   if (dsp3_state.op1e_lcv_radius > dsp3_state.op1e_max_radius)
777   {
778      dsp3_state.op1e_turn++;
779      dsp3_state.op1e_lcv_turns--;
780
781      dsp3_state.op1e_lcv_radius = dsp3_state.op1e_min_radius;
782      dsp3_state.op1e_lcv_steps = dsp3_state.op1e_min_radius;
783
784      dsp3_state.op1e_x = dsp3_state.op3e_x;
785      dsp3_state.op1e_y = dsp3_state.op3e_y;
786
787      for (lcv = 0; lcv < dsp3_state.op1e_min_radius; lcv++)
788         dsp3_OP1E_D(dsp3_state.op1e_turn, &dsp3_state.op1e_x, &dsp3_state.op1e_y);
789   }
790
791   if (dsp3_state.op1e_lcv_turns == 0)
792   {
793      dsp3_state.DR = 0xffff;
794      dsp3_state.SR = 0x0080;
795      dsp3_state.SetDSP3 = &dsp3_Reset;
796      return;
797   }
798
799   dsp3_state.DR = (UINT8)(dsp3_state.op1e_x) | ((UINT8)(dsp3_state.op1e_y) << 8);
800   dsp3_OP03();
801
802   dsp3_state.op1e_cell = dsp3_state.DR;
803
804   dsp3_state.SR = 0x0080;
805   dsp3_state.SetDSP3 = &dsp3_OP1E_C2;
806}
807
808
809static void dsp3_OP1E_C2(void)
810{
811   dsp3_state.DR = dsp3_state.op1e_weight[dsp3_state.op1e_cell];
812
813   dsp3_OP1E_D((INT16)(dsp3_state.op1e_turn + 2), &dsp3_state.op1e_x, &dsp3_state.op1e_y);
814   dsp3_state.op1e_lcv_steps--;
815
816   dsp3_state.SR = 0x0084;
817   dsp3_state.SetDSP3 = &dsp3_OP1E_C1;
818}
819
820
821static void dsp3_OP1E_D( INT16 move, INT16 *lo, INT16 *hi )
822{
823   UINT32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
824   INT16 Lo;
825   INT16 Hi;
826
827   dsp3_state.AddHi = dsp3_state.DataROM[dataOfs];
828   dsp3_state.AddLo = dsp3_state.DataROM[dataOfs + 1];
829
830   Lo = (UINT8)(*lo);
831   Hi = (UINT8)(*hi);
832
833   if (Lo & 1)   Hi += (dsp3_state.AddLo & 1);
834
835   dsp3_state.AddLo += Lo;
836   dsp3_state.AddHi += Hi;
837
838   if (dsp3_state.AddLo < 0)
839      dsp3_state.AddLo += dsp3_state.WinLo;
840   else if (dsp3_state.AddLo >= dsp3_state.WinLo)
841      dsp3_state.AddLo -= dsp3_state.WinLo;
842
843   if (dsp3_state.AddHi < 0)
844      dsp3_state.AddHi += dsp3_state.WinHi;
845   else if (dsp3_state.AddHi >= dsp3_state.WinHi)
846      dsp3_state.AddHi -= dsp3_state.WinHi;
847
848   *lo = dsp3_state.AddLo;
849   *hi = dsp3_state.AddHi;
850}
851
852
853static void dsp3_OP1E_D1( INT16 move, INT16 *lo, INT16 *hi )
854{
855   //UINT32 dataOfs = ((move << 1) + 0x03b2) & 0x03ff;
856   INT16 Lo;
857   INT16 Hi;
858
859   const unsigned short HiAdd[] = {
860      0x00, 0xFF, 0x00, 0x01, 0x01, 0x01, 0x00, 0x00,
861      0x00, 0xFF, 0xFF, 0x00, 0x01, 0x00, 0xFF, 0x00
862   };
863   const unsigned short LoAdd[] = {
864      0x00, 0x00, 0x01, 0x01, 0x00, 0xFF, 0xFF, 0x00
865   };
866
867   if ((*lo) & 1)
868      dsp3_state.AddHi = HiAdd[move + 8];
869   else
870      dsp3_state.AddHi = HiAdd[move + 0];
871   dsp3_state.AddLo = LoAdd[move];
872
873   Lo = (UINT8)(*lo);
874   Hi = (UINT8)(*hi);
875
876   if (Lo & 1)
877      Hi += (dsp3_state.AddLo & 1);
878
879   dsp3_state.AddLo += Lo;
880   dsp3_state.AddHi += Hi;
881
882   *lo = dsp3_state.AddLo;
883   *hi = dsp3_state.AddHi;
884}
885
886
887static void dsp3_OP10(void)
888{
889   if (dsp3_state.DR == 0xffff )
890      dsp3_Reset();
891   else
892   {
893      // absorb 2 bytes
894      dsp3_state.DR = dsp3_state.DR;
895   }
896}
897
898#ifdef UNUSED_FUNCTION
899static void dsp3_OP0C_A(void)
900{
901   // absorb 2 bytes
902
903   dsp3_state.DR = 0;
904   dsp3_state.SetDSP3 = &dsp3_Reset;
905}
906#endif
907
908static void dsp3_OP0C(void)
909{
910   // absorb 2 bytes
911
912   dsp3_state.DR = 0;
913   //dsp3_state.SetDSP3 = &dsp3_OP0C_A;
914   dsp3_state.SetDSP3 = &dsp3_Reset;
915}
916
917
918static void dsp3_OP1C_C(void)
919{
920   // return 2 bytes
921   dsp3_state.DR = 0;
922   dsp3_state.SetDSP3 = &dsp3_Reset;
923}
924
925
926static void dsp3_OP1C_B(void)
927{
928   // absorb 2 bytes
929
930   // return 2 bytes
931   dsp3_state.DR = 0;
932   dsp3_state.SetDSP3 = &dsp3_OP1C_C;
933}
934
935
936static void dsp3_OP1C_A(void)
937{
938   // absorb 2 bytes
939
940   dsp3_state.SetDSP3 = &dsp3_OP1C_B;
941}
942
943
944static void dsp3_OP1C(void)
945{
946   // absorb 2 bytes
947
948   dsp3_state.SetDSP3 = &dsp3_OP1C_A;
949}
950
951
952static void dsp3_Command(void)
953{
954   if (dsp3_state.DR < 0x40)
955   {
956      switch (dsp3_state.DR)
957      {
958      case 0x02: dsp3_state.SetDSP3 = &dsp3_Coordinate; break;
959      case 0x03: dsp3_state.SetDSP3 = &dsp3_OP03; break;
960      case 0x06: dsp3_state.SetDSP3 = &dsp3_OP06; break;
961      case 0x07: dsp3_state.SetDSP3 = &dsp3_OP07; return;
962      case 0x0c: dsp3_state.SetDSP3 = &dsp3_OP0C; break;
963      case 0x0f: dsp3_state.SetDSP3 = &dsp3_TestMemory; break;
964      case 0x10: dsp3_state.SetDSP3 = &dsp3_OP10; break;
965      case 0x18: dsp3_state.SetDSP3 = &dsp3_Convert; break;
966      case 0x1c: dsp3_state.SetDSP3 = &dsp3_OP1C; break;
967      case 0x1e: dsp3_state.SetDSP3 = &dsp3_OP1E; break;
968      case 0x1f: dsp3_state.SetDSP3 = &dsp3_MemoryDump; break;
969      case 0x38: dsp3_state.SetDSP3 = &dsp3_Decode; break;
970      case 0x3e: dsp3_state.SetDSP3 = &dsp3_OP3E; break;
971      default: return;
972      }
973
974      dsp3_state.SR = 0x0080;
975      dsp3_state.Index = 0;
976   }
977}
978
979
980static void dsp3_write( UINT16 dsp3_address, UINT8 dsp3_byte )
981{
982   if (dsp3_address < 0xC000)
983   {
984      if (dsp3_state.SR & 0x04)
985      {
986         dsp3_state.DR = (dsp3_state.DR & 0xff00) + dsp3_byte;
987         (*dsp3_state.SetDSP3)();
988      }
989      else
990      {
991         dsp3_state.SR ^= 0x10;
992
993         if (dsp3_state.SR & 0x10)
994            dsp3_state.DR = (dsp3_state.DR & 0xff00) + dsp3_byte;
995         else
996         {
997            dsp3_state.DR = (dsp3_state.DR & 0x00ff) + (dsp3_byte << 8);
998            (*dsp3_state.SetDSP3)();
999         }
1000      }
1001   }
1002}
1003
1004static UINT8 dsp3_read( UINT16 dsp3_address )
1005{
1006   UINT8 value;
1007   if (dsp3_address < 0xC000)
1008   {
1009      if (dsp3_state.SR & 0x04)
1010      {
1011         value = (UINT8) dsp3_state.DR;
1012         (*dsp3_state.SetDSP3)();
1013      }
1014      else
1015      {
1016         dsp3_state.SR ^= 0x10;
1017
1018         if (dsp3_state.SR & 0x10)
1019            value = (UINT8) (dsp3_state.DR);
1020         else
1021         {
1022            value = (UINT8) (dsp3_state.DR >> 8);
1023            (*dsp3_state.SetDSP3)();
1024         }
1025      }
1026
1027   }
1028   else
1029   {
1030      value = (UINT8) dsp3_state.SR;
1031   }
1032
1033   return value;
1034}
1035
1036static void dsp3_register_save( running_machine *machine )
1037{
1038   state_save_register_global(machine, dsp3_state.DR);
1039   state_save_register_global(machine, dsp3_state.SR);
1040   state_save_register_global(machine, dsp3_state.MemoryIndex);
1041
1042   state_save_register_global(machine, dsp3_state.AddLo);
1043   state_save_register_global(machine, dsp3_state.AddHi);
1044   state_save_register_global(machine, dsp3_state.WinLo);
1045   state_save_register_global(machine, dsp3_state.WinHi);
1046   state_save_register_global(machine, dsp3_state.Codewords);
1047   state_save_register_global(machine, dsp3_state.Outwords);
1048   state_save_register_global(machine, dsp3_state.Symbol);
1049   state_save_register_global(machine, dsp3_state.BitCount);
1050   state_save_register_global(machine, dsp3_state.Index);
1051   state_save_register_global_array(machine, dsp3_state.Codes);
1052   state_save_register_global(machine, dsp3_state.BitsLeft);
1053   state_save_register_global(machine, dsp3_state.ReqBits);
1054   state_save_register_global(machine, dsp3_state.ReqData);
1055   state_save_register_global(machine, dsp3_state.BitCommand);
1056   state_save_register_global(machine, dsp3_state.BaseLength);
1057   state_save_register_global(machine, dsp3_state.BaseCodes);
1058   state_save_register_global(machine, dsp3_state.BaseCode);
1059   state_save_register_global_array(machine, dsp3_state.CodeLengths);
1060   state_save_register_global_array(machine, dsp3_state.CodeOffsets);
1061   state_save_register_global(machine, dsp3_state.LZCode);
1062   state_save_register_global(machine, dsp3_state.LZLength);
1063
1064   state_save_register_global(machine, dsp3_state.X);
1065   state_save_register_global(machine, dsp3_state.Y);
1066
1067   state_save_register_global_array(machine, dsp3_state.Bitmap);
1068   state_save_register_global_array(machine, dsp3_state.Bitplane);
1069   state_save_register_global(machine, dsp3_state.BMIndex);
1070   state_save_register_global(machine, dsp3_state.BPIndex);
1071   state_save_register_global(machine, dsp3_state.Count);
1072
1073   state_save_register_global(machine, dsp3_state.op3e_x);
1074   state_save_register_global(machine, dsp3_state.op3e_y);
1075
1076   state_save_register_global_array(machine, dsp3_state.op1e_terrain);
1077   state_save_register_global_array(machine, dsp3_state.op1e_cost);
1078   state_save_register_global_array(machine, dsp3_state.op1e_weight);
1079
1080   state_save_register_global(machine, dsp3_state.op1e_cell);
1081   state_save_register_global(machine, dsp3_state.op1e_turn);
1082   state_save_register_global(machine, dsp3_state.op1e_search);
1083
1084   state_save_register_global(machine, dsp3_state.op1e_x);
1085   state_save_register_global(machine, dsp3_state.op1e_y);
1086
1087   state_save_register_global(machine, dsp3_state.op1e_min_radius);
1088   state_save_register_global(machine, dsp3_state.op1e_max_radius);
1089
1090   state_save_register_global(machine, dsp3_state.op1e_max_search_radius);
1091   state_save_register_global(machine, dsp3_state.op1e_max_path_radius);
1092
1093   state_save_register_global(machine, dsp3_state.op1e_lcv_radius);
1094   state_save_register_global(machine, dsp3_state.op1e_lcv_steps);
1095   state_save_register_global(machine, dsp3_state.op1e_lcv_turns);
1096
1097}
1098
1099static void dsp3_init( running_machine *machine )
1100{
1101   UINT32 i;
1102   UINT8 *dspin = machine->region("addons")->base();
1103
1104   dsp3_Reset();
1105
1106   // expand the DSP-3 data ROM
1107   for (i = 0; i < 2048; i += 2)
1108   {
1109      dsp3_state.DataROM[i / 2] = dspin[0x800 + i] << 8 | dspin[0x800 + i + 1];
1110   }
1111
1112   dsp3_register_save(machine);
1113}
src/mame/machine/snesdsp4.c
r9893r9894
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
30static struct dsp4_t dsp4;
31static struct 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{                                                           \
94   INT16 *p = ( d ), *end = ( d )+16;                    \
95   for (; p != end; p++)                                 \
96   {                                                     \
97      WRITE_WORD( dsp4.output + dsp4.out_count, *p ); \
98   }                                                     \
99   dsp4.out_count += 32;                                 \
100}
101#endif
102
103#ifdef PRINT_OP
104#define DSP4_WRITE_DEBUG( x, d ) \
105   WRITE_WORD( nop + x, d );
106#endif
107
108#ifdef DEBUG_DSP
109#define DSP4_WRITE_DEBUG( x, d ) \
110   WRITE_WORD( nop + x, d );
111#endif
112
113//////////////////////////////////////////////////////////////
114
115// used to wait for dsp i/o
116
117#define DSP4_WAIT( x ) \
118   dsp4.in_index = 0; dsp4_vars.Logic = x; return;
119
120//////////////////////////////////////////////////////////////
121
122// 1.7.8 -> 1.15.16
123#define SEX78( a ) ( ( (INT32) ( (INT16) (a) ) ) << 8 )
124
125// 1.15.0 -> 1.15.16
126#define SEX16( a ) ( ( (INT32) ( (INT16) (a) ) ) << 16 )
127
128#ifdef PRINT_OP
129#define U16( a ) ( (UINT16) ( a ) )
130#endif
131
132#ifdef DEBUG_DSP
133#define U16( a ) ( (UINT16) ( a ) )
134#endif
135
136//////////////////////////////////////////////////////////////
137
138// Attention: This lookup table is not verified
139static const UINT16 div_lut[64] = { 0x0000, 0x8000, 0x4000, 0x2aaa, 0x2000, 0x1999, 0x1555, 0x1249, 0x1000, 0x0e38,
140                                    0x0ccc, 0x0ba2, 0x0aaa, 0x09d8, 0x0924, 0x0888, 0x0800, 0x0787, 0x071c, 0x06bc,
141                                    0x0666, 0x0618, 0x05d1, 0x0590, 0x0555, 0x051e, 0x04ec, 0x04bd, 0x0492, 0x0469,
142                                    0x0444, 0x0421, 0x0400, 0x03e0, 0x03c3, 0x03a8, 0x038e, 0x0375, 0x035e, 0x0348,
143                                    0x0333, 0x031f, 0x030c, 0x02fa, 0x02e8, 0x02d8, 0x02c8, 0x02b9, 0x02aa, 0x029c,
144                                    0x028f, 0x0282, 0x0276, 0x026a, 0x025e, 0x0253, 0x0249, 0x023e, 0x0234, 0x022b,
145                                    0x0222, 0x0219, 0x0210, 0x0208,  };
146static INT16 dsp4_Inverse(INT16 value)
147{
148   // saturate bounds
149   if (value < 0)
150   {
151      value = 0;
152   }
153   if (value > 63)
154   {
155      value = 63;
156   }
157
158   return div_lut[value];
159}
160
161//////////////////////////////////////////////////////////////
162
163// Prototype
164static void dsp4_OP0B(UINT8 *draw, INT16 sp_x, INT16 sp_y, INT16 sp_attr, UINT8 size, UINT8 stop);
165
166//////////////////////////////////////////////////////////////
167
168// OP00
169static void dsp4_Multiply(INT16 Multiplicand, INT16 Multiplier, INT32 *Product)
170{
171   *Product = (Multiplicand * Multiplier << 1) >> 1;
172}
173
174//////////////////////////////////////////////////////////////
175
176
177static void dsp4_OP01( void )
178{
179   dsp4.waiting4command = 0;
180
181   // op flow control
182   switch (dsp4_vars.Logic)
183   {
184      case 1:
185         goto resume1; break;
186      case 2:
187         goto resume2; break;
188      case 3:
189         goto resume3; break;
190   }
191
192   ////////////////////////////////////////////////////
193   // process initial inputs
194
195   // sort inputs
196   dsp4_vars.world_y = DSP4_READ_DWORD();
197   dsp4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
198   dsp4_vars.poly_top[0][0] = DSP4_READ_WORD();
199   dsp4_vars.poly_cx[1][0] = DSP4_READ_WORD();
200   dsp4_vars.viewport_bottom = DSP4_READ_WORD();
201   dsp4_vars.world_x = DSP4_READ_DWORD();
202   dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
203   dsp4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
204   dsp4_vars.world_yofs = DSP4_READ_WORD();
205   dsp4_vars.world_dy = DSP4_READ_DWORD();
206   dsp4_vars.world_dx = DSP4_READ_DWORD();
207   dsp4_vars.distance = DSP4_READ_WORD();
208   DSP4_READ_WORD(); // 0x0000
209   dsp4_vars.world_xenv = DSP4_READ_DWORD();
210   dsp4_vars.world_ddy = DSP4_READ_WORD();
211   dsp4_vars.world_ddx = DSP4_READ_WORD();
212   dsp4_vars.view_yofsenv = DSP4_READ_WORD();
213
214   // initial (x,y,offset) at starting dsp4_vars.raster line
215   dsp4_vars.view_x1 = (INT16)((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16);
216   dsp4_vars.view_y1 = (INT16)(dsp4_vars.world_y >> 16);
217   dsp4_vars.view_xofs1 = (INT16)(dsp4_vars.world_x >> 16);
218   dsp4_vars.view_yofs1 = dsp4_vars.world_yofs;
219   dsp4_vars.view_turnoff_x = 0;
220   dsp4_vars.view_turnoff_dx = 0;
221
222   // first dsp4_vars.raster line
223   dsp4_vars.poly_raster[0][0] = dsp4_vars.poly_bottom[0][0];
224
225   do
226   {
227      ////////////////////////////////////////////////////
228      // process one iteration of projection
229
230      // perspective projection of world (x,y,scroll) points
231      // based on the current projection lines
232      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 ));
233      dsp4_vars.view_y2 = (INT16)((dsp4_vars.world_y >> 16) * dsp4_vars.distance >> 15);
234      dsp4_vars.view_xofs2 = dsp4_vars.view_x2;
235      dsp4_vars.view_yofs2 = (dsp4_vars.world_yofs * dsp4_vars.distance >> 15) + dsp4_vars.poly_bottom[0][0] - dsp4_vars.view_y2;
236
237
238      // 1. World x-location before transformation
239      // 2. Viewer x-position at the next
240      // 3. World y-location before perspective projection
241      // 4. Viewer y-position below the horizon
242      // 5. Number of dsp4_vars.raster lines drawn in this iteration
243
244      DSP4_CLEAR_OUT();
245      DSP4_WRITE_WORD((UINT16)((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16));
246      DSP4_WRITE_WORD(dsp4_vars.view_x2);
247      DSP4_WRITE_WORD((UINT16)(dsp4_vars.world_y >> 16));
248      DSP4_WRITE_WORD(dsp4_vars.view_y2);
249
250      //////////////////////////////////////////////////////
251
252      // SR = 0x00
253
254      // determine # of dsp4_vars.raster lines used
255      dsp4_vars.segments = dsp4_vars.poly_raster[0][0] - dsp4_vars.view_y2;
256
257      // prevent overdraw
258      if (dsp4_vars.view_y2 >= dsp4_vars.poly_raster[0][0])
259         dsp4_vars.segments = 0;
260      else
261         dsp4_vars.poly_raster[0][0] = dsp4_vars.view_y2;
262
263      // don't draw outside the window
264      if (dsp4_vars.view_y2 < dsp4_vars.poly_top[0][0])
265      {
266         dsp4_vars.segments = 0;
267
268         // flush remaining dsp4_vars.raster lines
269         if (dsp4_vars.view_y1 >= dsp4_vars.poly_top[0][0])
270            dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.poly_top[0][0];
271      }
272
273      // SR = 0x80
274
275      DSP4_WRITE_WORD(dsp4_vars.segments);
276
277      //////////////////////////////////////////////////////
278
279      // scan next command if no SR check needed
280      if (dsp4_vars.segments)
281      {
282         INT32 px_dx, py_dy;
283         INT32 x_scroll, y_scroll;
284
285         // SR = 0x00
286
287         // linear interpolation (lerp) between projected points
288         px_dx = (dsp4_vars.view_xofs2 - dsp4_vars.view_xofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
289         py_dy = (dsp4_vars.view_yofs2 - dsp4_vars.view_yofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
290
291         // starting step values
292         x_scroll = SEX16(dsp4_vars.poly_cx[0][0] + dsp4_vars.view_xofs1);
293         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);
294
295         // SR = 0x80
296
297         // rasterize line
298         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < dsp4_vars.segments; dsp4_vars.lcv++)
299         {
300            // 1. HDMA memory pointer (bg1)
301            // 2. vertical scroll offset ($210E)
302            // 3. horizontal scroll offset ($210D)
303
304            DSP4_WRITE_WORD(dsp4_vars.poly_ptr[0][0]);
305            DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
306            DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
307
308
309            // update memory address
310            dsp4_vars.poly_ptr[0][0] -= 4;
311
312            // update screen values
313            x_scroll += px_dx;
314            y_scroll += py_dy;
315         }
316      }
317
318      ////////////////////////////////////////////////////
319      // Post-update
320
321      // update new viewer (x,y,scroll) to last dsp4_vars.raster line drawn
322      dsp4_vars.view_x1 = dsp4_vars.view_x2;
323      dsp4_vars.view_y1 = dsp4_vars.view_y2;
324      dsp4_vars.view_xofs1 = dsp4_vars.view_xofs2;
325      dsp4_vars.view_yofs1 = dsp4_vars.view_yofs2;
326
327      // add deltas for projection lines
328      dsp4_vars.world_dx += SEX78(dsp4_vars.world_ddx);
329      dsp4_vars.world_dy += SEX78(dsp4_vars.world_ddy);
330
331      // update projection lines
332      dsp4_vars.world_x += (dsp4_vars.world_dx + dsp4_vars.world_xenv);
333      dsp4_vars.world_y += dsp4_vars.world_dy;
334
335      // update road turnoff position
336      dsp4_vars.view_turnoff_x += dsp4_vars.view_turnoff_dx;
337
338      ////////////////////////////////////////////////////
339      // command check
340
341      // scan next command
342      dsp4.in_count = 2;
343      DSP4_WAIT(1) resume1 :
344
345      // check for termination
346      dsp4_vars.distance = DSP4_READ_WORD();
347      if (dsp4_vars.distance == -0x8000)
348         break;
349
350      // road turnoff
351      if( (UINT16) dsp4_vars.distance == 0x8001 )
352      {
353         dsp4.in_count = 6;
354         DSP4_WAIT(2) resume2:
355
356         dsp4_vars.distance = DSP4_READ_WORD();
357         dsp4_vars.view_turnoff_x = DSP4_READ_WORD();
358         dsp4_vars.view_turnoff_dx = DSP4_READ_WORD();
359
360         // factor in new changes
361         dsp4_vars.view_x1 += ( dsp4_vars.view_turnoff_x * dsp4_vars.distance >> 15 );
362         dsp4_vars.view_xofs1 += ( dsp4_vars.view_turnoff_x * dsp4_vars.distance >> 15 );
363
364         // update stepping values
365         dsp4_vars.view_turnoff_x += dsp4_vars.view_turnoff_dx;
366
367         dsp4.in_count = 2;
368         DSP4_WAIT(1)
369      }
370
371      // already have 2 bytes read
372      dsp4.in_count = 6;
373      DSP4_WAIT(3) resume3 :
374
375      // inspect inputs
376      dsp4_vars.world_ddy = DSP4_READ_WORD();
377      dsp4_vars.world_ddx = DSP4_READ_WORD();
378      dsp4_vars.view_yofsenv = DSP4_READ_WORD();
379
380      // no envelope here
381      dsp4_vars.world_xenv = 0;
382   }
383   while (1);
384
385   // terminate op
386   dsp4.waiting4command = 1;
387}
388
389//////////////////////////////////////////////////////////////
390
391
392static void dsp4_OP03( void )
393{
394   dsp4_vars.OAM_RowMax = 33;
395   memset(dsp4_vars.OAM_Row, 0, 64);
396}
397
398
399//////////////////////////////////////////////////////////////
400
401
402static void dsp4_OP05( void )
403{
404   dsp4_vars.OAM_index = 0;
405   dsp4_vars.OAM_bits = 0;
406   memset(dsp4_vars.OAM_attr, 0, 32);
407   dsp4_vars.sprite_count = 0;
408}
409
410
411//////////////////////////////////////////////////////////////
412
413static void dsp4_OP06( void )
414{
415   DSP4_CLEAR_OUT();
416   DSP4_WRITE_16_WORD(dsp4_vars.OAM_attr);
417}
418
419//////////////////////////////////////////////////////////////
420
421
422static void dsp4_OP07( void )
423{
424   dsp4.waiting4command = 0;
425
426   // op flow control
427   switch (dsp4_vars.Logic)
428   {
429      case 1:
430         goto resume1; break;
431      case 2:
432         goto resume2; break;
433   }
434
435   ////////////////////////////////////////////////////
436   // sort inputs
437
438   dsp4_vars.world_y = DSP4_READ_DWORD();
439   dsp4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
440   dsp4_vars.poly_top[0][0] = DSP4_READ_WORD();
441   dsp4_vars.poly_cx[1][0] = DSP4_READ_WORD();
442   dsp4_vars.viewport_bottom = DSP4_READ_WORD();
443   dsp4_vars.world_x = DSP4_READ_DWORD();
444   dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
445   dsp4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
446   dsp4_vars.world_yofs = DSP4_READ_WORD();
447   dsp4_vars.distance = DSP4_READ_WORD();
448   dsp4_vars.view_y2 = DSP4_READ_WORD();
449   dsp4_vars.view_dy = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
450   dsp4_vars.view_x2 = DSP4_READ_WORD();
451   dsp4_vars.view_dx = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
452   dsp4_vars.view_yofsenv = DSP4_READ_WORD();
453
454   // initial (x,y,offset) at starting dsp4_vars.raster line
455   dsp4_vars.view_x1 = (INT16)(dsp4_vars.world_x >> 16);
456   dsp4_vars.view_y1 = (INT16)(dsp4_vars.world_y >> 16);
457   dsp4_vars.view_xofs1 = dsp4_vars.view_x1;
458   dsp4_vars.view_yofs1 = dsp4_vars.world_yofs;
459
460   // first dsp4_vars.raster line
461   dsp4_vars.poly_raster[0][0] = dsp4_vars.poly_bottom[0][0];
462
463
464   do
465   {
466      ////////////////////////////////////////////////////
467      // process one iteration of projection
468
469      // add shaping
470      dsp4_vars.view_x2 += dsp4_vars.view_dx;
471      dsp4_vars.view_y2 += dsp4_vars.view_dy;
472
473      // vertical scroll calculation
474      dsp4_vars.view_xofs2 = dsp4_vars.view_x2;
475      dsp4_vars.view_yofs2 = (dsp4_vars.world_yofs * dsp4_vars.distance >> 15) + dsp4_vars.poly_bottom[0][0] - dsp4_vars.view_y2;
476
477      // 1. Viewer x-position at the next
478      // 2. Viewer y-position below the horizon
479      // 3. Number of dsp4_vars.raster lines drawn in this iteration
480
481      DSP4_CLEAR_OUT();
482      DSP4_WRITE_WORD(dsp4_vars.view_x2);
483      DSP4_WRITE_WORD(dsp4_vars.view_y2);
484
485      //////////////////////////////////////////////////////
486
487      // SR = 0x00
488
489      // determine # of dsp4_vars.raster lines used
490      dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.view_y2;
491
492      // prevent overdraw
493      if (dsp4_vars.view_y2 >= dsp4_vars.poly_raster[0][0])
494         dsp4_vars.segments = 0;
495      else
496         dsp4_vars.poly_raster[0][0] = dsp4_vars.view_y2;
497
498      // don't draw outside the window
499      if (dsp4_vars.view_y2 < dsp4_vars.poly_top[0][0])
500      {
501         dsp4_vars.segments = 0;
502
503         // flush remaining dsp4_vars.raster lines
504         if (dsp4_vars.view_y1 >= dsp4_vars.poly_top[0][0])
505            dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.poly_top[0][0];
506      }
507
508      // SR = 0x80
509
510      DSP4_WRITE_WORD(dsp4_vars.segments);
511
512      //////////////////////////////////////////////////////
513
514      // scan next command if no SR check needed
515      if (dsp4_vars.segments)
516      {
517         INT32 px_dx, py_dy;
518         INT32 x_scroll, y_scroll;
519
520         // SR = 0x00
521
522         // linear interpolation (lerp) between projected points
523         px_dx = (dsp4_vars.view_xofs2 - dsp4_vars.view_xofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
524         py_dy = (dsp4_vars.view_yofs2 - dsp4_vars.view_yofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
525
526         // starting step values
527         x_scroll = SEX16(dsp4_vars.poly_cx[0][0] + dsp4_vars.view_xofs1);
528         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);
529
530         // SR = 0x80
531
532         // rasterize line
533         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < dsp4_vars.segments; dsp4_vars.lcv++)
534         {
535            // 1. HDMA memory pointer (bg2)
536            // 2. vertical scroll offset ($2110)
537            // 3. horizontal scroll offset ($210F)
538
539            DSP4_WRITE_WORD(dsp4_vars.poly_ptr[0][0]);
540            DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
541            DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
542
543            // update memory address
544            dsp4_vars.poly_ptr[0][0] -= 4;
545
546            // update screen values
547            x_scroll += px_dx;
548            y_scroll += py_dy;
549         }
550      }
551
552      /////////////////////////////////////////////////////
553      // Post-update
554
555      // update new viewer (x,y,scroll) to last dsp4_vars.raster line drawn
556      dsp4_vars.view_x1 = dsp4_vars.view_x2;
557      dsp4_vars.view_y1 = dsp4_vars.view_y2;
558      dsp4_vars.view_xofs1 = dsp4_vars.view_xofs2;
559      dsp4_vars.view_yofs1 = dsp4_vars.view_yofs2;
560
561      ////////////////////////////////////////////////////
562      // command check
563
564      // scan next command
565      dsp4.in_count = 2;
566      DSP4_WAIT(1) resume1 :
567
568      // check for opcode termination
569      dsp4_vars.distance = DSP4_READ_WORD();
570      if (dsp4_vars.distance == -0x8000)
571         break;
572
573      // already have 2 bytes in queue
574      dsp4.in_count = 10;
575      DSP4_WAIT(2) resume2 :
576
577      // inspect inputs
578      dsp4_vars.view_y2 = DSP4_READ_WORD();
579      dsp4_vars.view_dy = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
580      dsp4_vars.view_x2 = DSP4_READ_WORD();
581      dsp4_vars.view_dx = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
582      dsp4_vars.view_yofsenv = DSP4_READ_WORD();
583   }
584   while (1);
585
586   dsp4.waiting4command = 1;
587}
588
589//////////////////////////////////////////////////////////////
590
591static void dsp4_OP08( void )
592{
593   INT16 wleft, wright;
594   INT16 view_x[2], view_y[2];
595   INT16 envelope[2][2];
596
597   dsp4.waiting4command = 0;
598
599   // op flow control
600   switch (dsp4_vars.Logic)
601   {
602      case 1:
603         goto resume1; break;
604      case 2:
605         goto resume2; break;
606   }
607
608   ////////////////////////////////////////////////////
609   // process initial inputs for two polygons
610
611   // clip values
612   dsp4_vars.poly_clipRt[0][0] = DSP4_READ_WORD();
613   dsp4_vars.poly_clipRt[0][1] = DSP4_READ_WORD();
614   dsp4_vars.poly_clipRt[1][0] = DSP4_READ_WORD();
615   dsp4_vars.poly_clipRt[1][1] = DSP4_READ_WORD();
616
617   dsp4_vars.poly_clipLf[0][0] = DSP4_READ_WORD();
618   dsp4_vars.poly_clipLf[0][1] = DSP4_READ_WORD();
619   dsp4_vars.poly_clipLf[1][0] = DSP4_READ_WORD();
620   dsp4_vars.poly_clipLf[1][1] = DSP4_READ_WORD();
621
622   // unknown (constant) (ex. 1P/2P = $00A6, $00A6, $00A6, $00A6)
623   DSP4_READ_WORD();
624   DSP4_READ_WORD();
625   DSP4_READ_WORD();
626   DSP4_READ_WORD();
627
628   // unknown (constant) (ex. 1P/2P = $00A5, $00A5, $00A7, $00A7)
629   DSP4_READ_WORD();
630   DSP4_READ_WORD();
631   DSP4_READ_WORD();
632   DSP4_READ_WORD();
633
634   // polygon centering (left,right)
635   dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
636   dsp4_vars.poly_cx[0][1] = DSP4_READ_WORD();
637   dsp4_vars.poly_cx[1][0] = DSP4_READ_WORD();
638   dsp4_vars.poly_cx[1][1] = DSP4_READ_WORD();
639
640   // HDMA pointer locations
641   dsp4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
642   dsp4_vars.poly_ptr[0][1] = DSP4_READ_WORD();
643   dsp4_vars.poly_ptr[1][0] = DSP4_READ_WORD();
644   dsp4_vars.poly_ptr[1][1] = DSP4_READ_WORD();
645
646   // starting dsp4_vars.raster line below the horizon
647   dsp4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
648   dsp4_vars.poly_bottom[0][1] = DSP4_READ_WORD();
649   dsp4_vars.poly_bottom[1][0] = DSP4_READ_WORD();
650   dsp4_vars.poly_bottom[1][1] = DSP4_READ_WORD();
651
652   // top boundary line to clip
653   dsp4_vars.poly_top[0][0] = DSP4_READ_WORD();
654   dsp4_vars.poly_top[0][1] = DSP4_READ_WORD();
655   dsp4_vars.poly_top[1][0] = DSP4_READ_WORD();
656   dsp4_vars.poly_top[1][1] = DSP4_READ_WORD();
657
658   // unknown
659   // (ex. 1P = $2FC8, $0034, $FF5C, $0035)
660   //
661   // (ex. 2P = $3178, $0034, $FFCC, $0035)
662   // (ex. 2P = $2FC8, $0034, $FFCC, $0035)
663
664   DSP4_READ_WORD();
665   DSP4_READ_WORD();
666   DSP4_READ_WORD();
667   DSP4_READ_WORD();
668
669   // look at guidelines for both polygon shapes
670   dsp4_vars.distance = DSP4_READ_WORD();
671   view_x[0] = DSP4_READ_WORD();
672   view_y[0] = DSP4_READ_WORD();
673   view_x[1] = DSP4_READ_WORD();
674   view_y[1] = DSP4_READ_WORD();
675
676   // envelope shaping guidelines (one frame only)
677   envelope[0][0] = DSP4_READ_WORD();
678   envelope[0][1] = DSP4_READ_WORD();
679   envelope[1][0] = DSP4_READ_WORD();
680   envelope[1][1] = DSP4_READ_WORD();
681
682   // starting base values to project from
683   dsp4_vars.poly_start[0] = view_x[0];
684   dsp4_vars.poly_start[1] = view_x[1];
685
686   // starting dsp4_vars.raster lines to begin drawing
687   dsp4_vars.poly_raster[0][0] = view_y[0];
688   dsp4_vars.poly_raster[0][1] = view_y[0];
689   dsp4_vars.poly_raster[1][0] = view_y[1];
690   dsp4_vars.poly_raster[1][1] = view_y[1];
691
692   // starting distances
693   dsp4_vars.poly_plane[0] = dsp4_vars.distance;
694   dsp4_vars.poly_plane[1] = dsp4_vars.distance;
695
696   // SR = 0x00
697
698   // re-center coordinates
699   wleft = dsp4_vars.poly_cx[0][0] - view_x[0] + envelope[0][0];
700   wright = dsp4_vars.poly_cx[0][1] - view_x[0] + envelope[0][1];
701
702   // saturate offscreen data for polygon #1
703   if (wleft < dsp4_vars.poly_clipLf[0][0])
704   {
705      wleft = dsp4_vars.poly_clipLf[0][0];
706   }
707   if (wleft > dsp4_vars.poly_clipRt[0][0])
708   {
709      wleft = dsp4_vars.poly_clipRt[0][0];
710   }
711   if (wright < dsp4_vars.poly_clipLf[0][1])
712   {
713      wright = dsp4_vars.poly_clipLf[0][1];
714   }
715   if (wright > dsp4_vars.poly_clipRt[0][1])
716   {
717      wright = dsp4_vars.poly_clipRt[0][1];
718   }
719
720   // SR = 0x80
721
722   // initial output for polygon #1
723   DSP4_CLEAR_OUT();
724   DSP4_WRITE_BYTE(wleft & 0xff);
725   DSP4_WRITE_BYTE(wright & 0xff);
726
727
728   do
729   {
730      INT16 polygon;
731      ////////////////////////////////////////////////////
732      // command check
733
734      // scan next command
735      dsp4.in_count = 2;
736      DSP4_WAIT(1) resume1 :
737
738      // terminate op
739      dsp4_vars.distance = DSP4_READ_WORD();
740      if (dsp4_vars.distance == -0x8000)
741         break;
742
743      // already have 2 bytes in queue
744      dsp4.in_count = 16;
745
746      DSP4_WAIT(2) resume2 :
747
748      // look at guidelines for both polygon shapes
749      view_x[0] = DSP4_READ_WORD();
750      view_y[0] = DSP4_READ_WORD();
751      view_x[1] = DSP4_READ_WORD();
752      view_y[1] = DSP4_READ_WORD();
753
754      // envelope shaping guidelines (one frame only)
755      envelope[0][0] = DSP4_READ_WORD();
756      envelope[0][1] = DSP4_READ_WORD();
757      envelope[1][0] = DSP4_READ_WORD();
758      envelope[1][1] = DSP4_READ_WORD();
759
760      ////////////////////////////////////////////////////
761      // projection begins
762
763      // init
764      DSP4_CLEAR_OUT();
765
766
767      //////////////////////////////////////////////
768      // solid polygon renderer - 2 shapes
769
770      for (polygon = 0; polygon < 2; polygon++)
771      {
772         INT32 left_inc, right_inc;
773         INT16 x1_final, x2_final;
774         INT16 env[2][2];
775         INT16 poly;
776
777         // SR = 0x00
778
779         // # dsp4_vars.raster lines to draw
780         dsp4_vars.segments = dsp4_vars.poly_raster[polygon][0] - view_y[polygon];
781
782         // prevent overdraw
783         if (dsp4_vars.segments > 0)
784         {
785            // bump drawing cursor
786            dsp4_vars.poly_raster[polygon][0] = view_y[polygon];
787            dsp4_vars.poly_raster[polygon][1] = view_y[polygon];
788         }
789         else
790            dsp4_vars.segments = 0;
791
792         // don't draw outside the window
793         if (view_y[polygon] < dsp4_vars.poly_top[polygon][0])
794         {
795            dsp4_vars.segments = 0;
796
797            // flush remaining dsp4_vars.raster lines
798            if (view_y[polygon] >= dsp4_vars.poly_top[polygon][0])
799               dsp4_vars.segments = view_y[polygon] - dsp4_vars.poly_top[polygon][0];
800         }
801
802         // SR = 0x80
803
804         // tell user how many dsp4_vars.raster structures to read in
805         DSP4_WRITE_WORD(dsp4_vars.segments);
806
807         // normal parameters
808         poly = polygon;
809
810         /////////////////////////////////////////////////////
811
812         // scan next command if no SR check needed
813         if (dsp4_vars.segments)
814         {
815            INT32 win_left, win_right;
816
817            // road turnoff selection
818            if( (UINT16) envelope[ polygon ][ 0 ] == (UINT16) 0xc001 )
819               poly = 1;
820            else if( envelope[ polygon ][ 1 ] == 0x3fff )
821               poly = 1;
822
823            ///////////////////////////////////////////////
824            // left side of polygon
825
826            // perspective correction on additional shaping parameters
827            env[0][0] = envelope[polygon][0] * dsp4_vars.poly_plane[poly] >> 15;
828            env[0][1] = envelope[polygon][0] * dsp4_vars.distance >> 15;
829
830            // project new shapes (left side)
831            x1_final = view_x[poly] + env[0][0];
832            x2_final = dsp4_vars.poly_start[poly] + env[0][1];
833
834            // interpolate between projected points with shaping
835            left_inc = (x2_final - x1_final) * dsp4_Inverse(dsp4_vars.segments) << 1;
836            if (dsp4_vars.segments == 1)
837               left_inc = -left_inc;
838
839            ///////////////////////////////////////////////
840            // right side of polygon
841
842            // perspective correction on additional shaping parameters
843            env[1][0] = envelope[polygon][1] * dsp4_vars.poly_plane[poly] >> 15;;
844            env[1][1] = envelope[polygon][1] * dsp4_vars.distance >> 15;
845
846            // project new shapes (right side)
847            x1_final = view_x[poly] + env[1][0];
848            x2_final = dsp4_vars.poly_start[poly] + env[1][1];
849
850
851            // interpolate between projected points with shaping
852            right_inc = (x2_final - x1_final) * dsp4_Inverse(dsp4_vars.segments) << 1;
853            if (dsp4_vars.segments == 1)
854               right_inc = -right_inc;
855
856            ///////////////////////////////////////////////
857            // update each point on the line
858
859            win_left = SEX16(dsp4_vars.poly_cx[polygon][0] - dsp4_vars.poly_start[poly] + env[0][0]);
860            win_right = SEX16(dsp4_vars.poly_cx[polygon][1] - dsp4_vars.poly_start[poly] + env[1][0]);
861
862            // update dsp4_vars.distance drawn into world
863            dsp4_vars.poly_plane[polygon] = dsp4_vars.distance;
864
865            // rasterize line
866            for (dsp4_vars.lcv = 0; dsp4_vars.lcv < dsp4_vars.segments; dsp4_vars.lcv++)
867            {
868               INT16 x_left, x_right;
869
870               // project new coordinates
871               win_left += left_inc;
872               win_right += right_inc;
873
874               // grab integer portion, drop fraction (no rounding)
875               x_left = (INT16)(win_left >> 16);
876               x_right = (INT16)(win_right >> 16);
877
878               // saturate offscreen data
879               if (x_left < dsp4_vars.poly_clipLf[polygon][0])
880                  x_left = dsp4_vars.poly_clipLf[polygon][0];
881               if (x_left > dsp4_vars.poly_clipRt[polygon][0])
882                  x_left = dsp4_vars.poly_clipRt[polygon][0];
883               if (x_right < dsp4_vars.poly_clipLf[polygon][1])
884                  x_right = dsp4_vars.poly_clipLf[polygon][1];
885               if (x_right > dsp4_vars.poly_clipRt[polygon][1])
886                  x_right = dsp4_vars.poly_clipRt[polygon][1];
887
888               // 1. HDMA memory pointer
889               // 2. Left window position ($2126/$2128)
890               // 3. Right window position ($2127/$2129)
891
892               DSP4_WRITE_WORD(dsp4_vars.poly_ptr[polygon][0]);
893               DSP4_WRITE_BYTE(x_left & 0xff);
894               DSP4_WRITE_BYTE(x_right & 0xff);
895
896
897               // update memory pointers
898               dsp4_vars.poly_ptr[polygon][0] -= 4;
899               dsp4_vars.poly_ptr[polygon][1] -= 4;
900            } // end rasterize line
901         }
902
903         ////////////////////////////////////////////////
904         // Post-update
905
906         // new projection spot to continue rasterizing from
907         dsp4_vars.poly_start[polygon] = view_x[poly];
908      } // end polygon rasterizer
909   }
910   while (1);
911
912   // unknown output
913   DSP4_CLEAR_OUT();
914   DSP4_WRITE_WORD(0);
915
916
917   dsp4.waiting4command = 1;
918}
919
920//////////////////////////////////////////////////////////////
921
922static void dsp4_OP09( void )
923{
924   dsp4.waiting4command = 0;
925
926   // op flow control
927   switch (dsp4_vars.Logic)
928   {
929      case 1:
930         goto resume1; break;
931      case 2:
932         goto resume2; break;
933      case 3:
934         goto resume3; break;
935      case 4:
936         goto resume4; break;
937      case 5:
938         goto resume5; break;
939      case 6:
940         goto resume6; break;
941   }
942
943   ////////////////////////////////////////////////////
944   // process initial inputs
945
946   // grab screen information
947   dsp4_vars.viewport_cx = DSP4_READ_WORD();
948   dsp4_vars.viewport_cy = DSP4_READ_WORD();
949   DSP4_READ_WORD(); // 0x0000
950   dsp4_vars.viewport_left = DSP4_READ_WORD();
951   dsp4_vars.viewport_right = DSP4_READ_WORD();
952   dsp4_vars.viewport_top = DSP4_READ_WORD();
953   dsp4_vars.viewport_bottom = DSP4_READ_WORD();
954
955   // starting dsp4_vars.raster line below the horizon
956   dsp4_vars.poly_bottom[0][0] = dsp4_vars.viewport_bottom - dsp4_vars.viewport_cy;
957   dsp4_vars.poly_raster[0][0] = 0x100;
958
959   do
960   {
961      ////////////////////////////////////////////////////
962      // check for new sprites
963
964      dsp4.in_count = 4;
965      DSP4_WAIT(1) resume1 :
966
967      ////////////////////////////////////////////////
968      // dsp4_vars.raster overdraw check
969
970      dsp4_vars.raster = DSP4_READ_WORD();
971
972      // continue updating the dsp4_vars.raster line where overdraw begins
973      if (dsp4_vars.raster < dsp4_vars.poly_raster[0][0])
974      {
975         dsp4_vars.sprite_clipy = dsp4_vars.viewport_bottom - (dsp4_vars.poly_bottom[0][0] - dsp4_vars.raster);
976         dsp4_vars.poly_raster[0][0] = dsp4_vars.raster;
977      }
978
979      /////////////////////////////////////////////////
980      // identify sprite
981
982      // op termination
983      dsp4_vars.distance = DSP4_READ_WORD();
984      if (dsp4_vars.distance == -0x8000)
985         goto terminate;
986
987
988      // no sprite
989      if (dsp4_vars.distance == 0x0000)
990      {
991         continue;
992      }
993
994      ////////////////////////////////////////////////////
995      // process projection information
996
997      // vehicle sprite
998      if ((UINT16) dsp4_vars.distance == 0x9000)
999      {
1000         INT16 car_left, car_right, car_back;
1001         INT16 impact_left, impact_back;
1002         INT16 world_spx, world_spy;
1003         INT16 view_spx, view_spy;
1004         UINT16 energy;
1005
1006         // we already have 4 bytes we want
1007         dsp4.in_count = 14;
1008         DSP4_WAIT(2) resume2 :
1009
1010         // filter inputs
1011         energy = DSP4_READ_WORD();
1012         impact_back = DSP4_READ_WORD();
1013         car_back = DSP4_READ_WORD();
1014         impact_left = DSP4_READ_WORD();
1015         car_left = DSP4_READ_WORD();
1016         dsp4_vars.distance = DSP4_READ_WORD();
1017         car_right = DSP4_READ_WORD();
1018
1019         // calculate car's world (x,y) values
1020         world_spx = car_right - car_left;
1021         world_spy = car_back;
1022
1023         // add in collision vector [needs bit-twiddling]
1024         world_spx -= energy * (impact_left - car_left) >> 16;
1025         world_spy -= energy * (car_back - impact_back) >> 16;
1026
1027         // perspective correction for world (x,y)
1028         view_spx = world_spx * dsp4_vars.distance >> 15;
1029         view_spy = world_spy * dsp4_vars.distance >> 15;
1030
1031         // convert to screen values
1032         dsp4_vars.sprite_x = dsp4_vars.viewport_cx + view_spx;
1033         dsp4_vars.sprite_y = dsp4_vars.viewport_bottom - (dsp4_vars.poly_bottom[0][0] - view_spy);
1034
1035         // make the car's (x)-coordinate available
1036         DSP4_CLEAR_OUT();
1037         DSP4_WRITE_WORD(world_spx);
1038
1039         // grab a few remaining vehicle values
1040         dsp4.in_count = 4;
1041         DSP4_WAIT(3) resume3 :
1042
1043         // add vertical lift factor
1044         dsp4_vars.sprite_y += DSP4_READ_WORD();
1045      }
1046      // terrain sprite
1047      else
1048      {
1049         INT16 world_spx, world_spy;
1050         INT16 view_spx, view_spy;
1051
1052         // we already have 4 bytes we want
1053         dsp4.in_count = 10;
1054         DSP4_WAIT(4) resume4 :
1055
1056         // sort loop inputs
1057         dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1058         dsp4_vars.poly_raster[0][1] = DSP4_READ_WORD();
1059         world_spx = DSP4_READ_WORD();
1060         world_spy = DSP4_READ_WORD();
1061
1062         // compute base dsp4_vars.raster line from the bottom
1063         dsp4_vars.segments = dsp4_vars.poly_bottom[0][0] - dsp4_vars.raster;
1064
1065         // perspective correction for world (x,y)
1066         view_spx = world_spx * dsp4_vars.distance >> 15;
1067         view_spy = world_spy * dsp4_vars.distance >> 15;
1068
1069         // convert to screen values
1070         dsp4_vars.sprite_x = dsp4_vars.viewport_cx + view_spx - dsp4_vars.poly_cx[0][0];
1071         dsp4_vars.sprite_y = dsp4_vars.viewport_bottom - dsp4_vars.segments + view_spy;
1072      }
1073
1074      // default sprite size: 16x16
1075      dsp4_vars.sprite_size = 1;
1076      dsp4_vars.sprite_attr = DSP4_READ_WORD();
1077
1078      ////////////////////////////////////////////////////
1079      // convert tile data to SNES OAM format
1080
1081      do
1082      {
1083         UINT16 header;
1084
1085         INT16 sp_x, sp_y, sp_attr, sp_dattr;
1086         INT16 sp_dx, sp_dy;
1087         INT16 pixels;
1088
1089         UINT8 draw;
1090
1091         dsp4.in_count = 2;
1092         DSP4_WAIT(5) resume5 :
1093
1094         draw = 1;
1095
1096         // opcode termination
1097         dsp4_vars.raster = DSP4_READ_WORD();
1098         if (dsp4_vars.raster == -0x8000)
1099            goto terminate;
1100
1101         // stop code
1102         if (dsp4_vars.raster == 0x0000 && !dsp4_vars.sprite_size)
1103            break;
1104
1105         // toggle sprite size
1106         if (dsp4_vars.raster == 0x0000)
1107         {
1108            dsp4_vars.sprite_size = !dsp4_vars.sprite_size;
1109            continue;
1110         }
1111
1112         // check for valid sprite header
1113         header = dsp4_vars.raster;
1114         header >>= 8;
1115         if (header != 0x20 &&
1116               header != 0x2e && //This is for attractor sprite
1117               header != 0x40 &&
1118               header != 0x60 &&
1119               header != 0xa0 &&
1120               header != 0xc0 &&
1121               header != 0xe0)
1122            break;
1123
1124         // read in rest of sprite data
1125         dsp4.in_count = 4;
1126         DSP4_WAIT(6) resume6 :
1127
1128         draw = 1;
1129
1130         /////////////////////////////////////
1131         // process tile data
1132
1133         // sprite deltas
1134         sp_dattr = dsp4_vars.raster;
1135         sp_dy = DSP4_READ_WORD();
1136         sp_dx = DSP4_READ_WORD();
1137
1138         // update coordinates to screen space
1139         sp_x = dsp4_vars.sprite_x + sp_dx;
1140         sp_y = dsp4_vars.sprite_y + sp_dy;
1141
1142         // update sprite nametable/attribute information
1143         sp_attr = dsp4_vars.sprite_attr + sp_dattr;
1144
1145         // allow partially visibile tiles
1146         pixels = dsp4_vars.sprite_size ? 15 : 7;
1147
1148         DSP4_CLEAR_OUT();
1149
1150         // transparent tile to clip off parts of a sprite (overdraw)
1151         if (dsp4_vars.sprite_clipy - pixels <= sp_y &&
1152               sp_y <= dsp4_vars.sprite_clipy &&
1153               sp_x >= dsp4_vars.viewport_left - pixels &&
1154               sp_x <= dsp4_vars.viewport_right &&
1155               dsp4_vars.sprite_clipy >= dsp4_vars.viewport_top - pixels &&
1156               dsp4_vars.sprite_clipy <= dsp4_vars.viewport_bottom)
1157         {
1158            dsp4_OP0B(&draw, sp_x, dsp4_vars.sprite_clipy, 0x00EE, dsp4_vars.sprite_size, 0);
1159         }
1160
1161
1162         // normal sprite tile
1163         if (sp_x >= dsp4_vars.viewport_left - pixels &&
1164               sp_x <= dsp4_vars.viewport_right &&
1165               sp_y >= dsp4_vars.viewport_top - pixels &&
1166               sp_y <= dsp4_vars.viewport_bottom &&
1167               sp_y <= dsp4_vars.sprite_clipy)
1168         {
1169            dsp4_OP0B(&draw, sp_x, sp_y, sp_attr, dsp4_vars.sprite_size, 0);
1170         }
1171
1172
1173         // no following OAM data
1174         dsp4_OP0B(&draw, 0, 0x0100, 0, 0, 1);
1175      }
1176      while (1);
1177   }
1178   while (1);
1179
1180   terminate : dsp4.waiting4command = 1;
1181}
1182
1183//////////////////////////////////////////////////////////////
1184
1185static const UINT16 OP0A_Values[16] = { 0x0000, 0x0030, 0x0060, 0x0090, 0x00c0, 0x00f0, 0x0120, 0x0150, 0xfe80,
1186                                 0xfeb0, 0xfee0, 0xff10, 0xff40, 0xff70, 0xffa0, 0xffd0 };
1187
1188static void dsp4_OP0A(INT16 n2, INT16 *o1, INT16 *o2, INT16 *o3, INT16 *o4)
1189{
1190   *o4 = OP0A_Values[(n2 & 0x000f)];
1191   *o3 = OP0A_Values[(n2 & 0x00f0) >> 4];
1192   *o2 = OP0A_Values[(n2 & 0x0f00) >> 8];
1193   *o1 = OP0A_Values[(n2 & 0xf000) >> 12];
1194}
1195
1196//////////////////////////////////////////////////////////////
1197
1198static void dsp4_OP0B(UINT8 *draw, INT16 sp_x, INT16 sp_y, INT16 sp_attr, UINT8 size, UINT8 stop)
1199{
1200   INT16 Row1, Row2;
1201
1202   // SR = 0x00
1203
1204   // align to nearest 8-pixel row
1205   Row1 = (sp_y >> 3) & 0x1f;
1206   Row2 = (Row1 + 1) & 0x1f;
1207
1208   // check boundaries
1209   if (!((sp_y < 0) || ((sp_y & 0x01ff) < 0x00eb)))
1210   {
1211      *draw = 0;
1212   }
1213   if (size)
1214   {
1215      if (dsp4_vars.OAM_Row[Row1] + 1 >= dsp4_vars.OAM_RowMax)
1216         *draw = 0;
1217      if (dsp4_vars.OAM_Row[Row2] + 1 >= dsp4_vars.OAM_RowMax)
1218         *draw = 0;
1219   }
1220   else
1221   {
1222      if (dsp4_vars.OAM_Row[Row1] >= dsp4_vars.OAM_RowMax)
1223      {
1224         *draw = 0;
1225      }
1226   }
1227
1228   // emulator fail-safe (unknown if this really exists)
1229   if (dsp4_vars.sprite_count >= 128)
1230   {
1231      *draw = 0;
1232   }
1233
1234   // SR = 0x80
1235
1236   if (*draw)
1237   {
1238      // Row tiles
1239      if (size)
1240      {
1241         dsp4_vars.OAM_Row[Row1] += 2;
1242         dsp4_vars.OAM_Row[Row2] += 2;
1243      }
1244      else
1245      {
1246         dsp4_vars.OAM_Row[Row1]++;
1247      }
1248
1249      // yield OAM output
1250      DSP4_WRITE_WORD(1);
1251
1252      // pack OAM data: x,y,name,attr
1253      DSP4_WRITE_BYTE(sp_x & 0xff);
1254      DSP4_WRITE_BYTE(sp_y & 0xff);
1255      DSP4_WRITE_WORD(sp_attr);
1256
1257      dsp4_vars.sprite_count++;
1258
1259      // OAM: size,msb data
1260      // save post-oam table data for future retrieval
1261      dsp4_vars.OAM_attr[dsp4_vars.OAM_index] |= ((sp_x <0 || sp_x> 255) << dsp4_vars.OAM_bits);
1262      dsp4_vars.OAM_bits++;
1263
1264      dsp4_vars.OAM_attr[dsp4_vars.OAM_index] |= (size << dsp4_vars.OAM_bits);
1265      dsp4_vars.OAM_bits++;
1266
1267      // move to next byte in buffer
1268      if (dsp4_vars.OAM_bits == 16)
1269      {
1270         dsp4_vars.OAM_bits = 0;
1271         dsp4_vars.OAM_index++;
1272      }
1273   }
1274   else if (stop)
1275   {
1276      // yield no OAM output
1277      DSP4_WRITE_WORD(0);
1278   }
1279}
1280
1281//////////////////////////////////////////////////////////////
1282
1283static void dsp4_OP0D( void )
1284{
1285   dsp4.waiting4command = 0;
1286
1287   // op flow control
1288   switch (dsp4_vars.Logic)
1289   {
1290      case 1:
1291         goto resume1; break;
1292      case 2:
1293         goto resume2; break;
1294   }
1295
1296   ////////////////////////////////////////////////////
1297   // process initial inputs
1298
1299   // sort inputs
1300   dsp4_vars.world_y = DSP4_READ_DWORD();
1301   dsp4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
1302   dsp4_vars.poly_top[0][0] = DSP4_READ_WORD();
1303   dsp4_vars.poly_cx[1][0] = DSP4_READ_WORD();
1304   dsp4_vars.viewport_bottom = DSP4_READ_WORD();
1305   dsp4_vars.world_x = DSP4_READ_DWORD();
1306   dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1307   dsp4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
1308   dsp4_vars.world_yofs = DSP4_READ_WORD();
1309   dsp4_vars.world_dy = DSP4_READ_DWORD();
1310   dsp4_vars.world_dx = DSP4_READ_DWORD();
1311   dsp4_vars.distance = DSP4_READ_WORD();
1312   DSP4_READ_WORD(); // 0x0000
1313   dsp4_vars.world_xenv = SEX78(DSP4_READ_WORD());
1314   dsp4_vars.world_ddy = DSP4_READ_WORD();
1315   dsp4_vars.world_ddx = DSP4_READ_WORD();
1316   dsp4_vars.view_yofsenv = DSP4_READ_WORD();
1317
1318   // initial (x,y,offset) at starting dsp4_vars.raster line
1319   dsp4_vars.view_x1 = (INT16)((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16);
1320   dsp4_vars.view_y1 = (INT16)(dsp4_vars.world_y >> 16);
1321   dsp4_vars.view_xofs1 = (INT16)(dsp4_vars.world_x >> 16);
1322   dsp4_vars.view_yofs1 = dsp4_vars.world_yofs;
1323
1324   // first dsp4_vars.raster line
1325   dsp4_vars.poly_raster[0][0] = dsp4_vars.poly_bottom[0][0];
1326
1327
1328   do
1329   {
1330      ////////////////////////////////////////////////////
1331      // process one iteration of projection
1332
1333      // perspective projection of world (x,y,scroll) points
1334      // based on the current projection lines
1335      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 ));
1336      dsp4_vars.view_y2 = (INT16)((dsp4_vars.world_y >> 16) * dsp4_vars.distance >> 15);
1337      dsp4_vars.view_xofs2 = dsp4_vars.view_x2;
1338      dsp4_vars.view_yofs2 = (dsp4_vars.world_yofs * dsp4_vars.distance >> 15) + dsp4_vars.poly_bottom[0][0] - dsp4_vars.view_y2;
1339
1340      // 1. World x-location before transformation
1341      // 2. Viewer x-position at the current
1342      // 3. World y-location before perspective projection
1343      // 4. Viewer y-position below the horizon
1344      // 5. Number of dsp4_vars.raster lines drawn in this iteration
1345
1346      DSP4_CLEAR_OUT();
1347      DSP4_WRITE_WORD((UINT16)((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16));
1348      DSP4_WRITE_WORD(dsp4_vars.view_x2);
1349      DSP4_WRITE_WORD((UINT16)(dsp4_vars.world_y >> 16));
1350      DSP4_WRITE_WORD(dsp4_vars.view_y2);
1351
1352      //////////////////////////////////////////////////////////
1353
1354      // SR = 0x00
1355
1356      // determine # of dsp4_vars.raster lines used
1357      dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.view_y2;
1358
1359      // prevent overdraw
1360      if (dsp4_vars.view_y2 >= dsp4_vars.poly_raster[0][0])
1361         dsp4_vars.segments = 0;
1362      else
1363         dsp4_vars.poly_raster[0][0] = dsp4_vars.view_y2;
1364
1365      // don't draw outside the window
1366      if (dsp4_vars.view_y2 < dsp4_vars.poly_top[0][0])
1367      {
1368         dsp4_vars.segments = 0;
1369
1370         // flush remaining dsp4_vars.raster lines
1371         if (dsp4_vars.view_y1 >= dsp4_vars.poly_top[0][0])
1372            dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.poly_top[0][0];
1373      }
1374
1375      // SR = 0x80
1376
1377      DSP4_WRITE_WORD(dsp4_vars.segments);
1378
1379      //////////////////////////////////////////////////////////
1380
1381      // scan next command if no SR check needed
1382      if (dsp4_vars.segments)
1383      {
1384         INT32 px_dx, py_dy;
1385         INT32 x_scroll, y_scroll;
1386
1387         // SR = 0x00
1388
1389         // linear interpolation (lerp) between projected points
1390         px_dx = (dsp4_vars.view_xofs2 - dsp4_vars.view_xofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
1391         py_dy = (dsp4_vars.view_yofs2 - dsp4_vars.view_yofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
1392
1393         // starting step values
1394         x_scroll = SEX16(dsp4_vars.poly_cx[0][0] + dsp4_vars.view_xofs1);
1395         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);
1396
1397         // SR = 0x80
1398
1399         // rasterize line
1400         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < dsp4_vars.segments; dsp4_vars.lcv++)
1401         {
1402            // 1. HDMA memory pointer (bg1)
1403            // 2. vertical scroll offset ($210E)
1404            // 3. horizontal scroll offset ($210D)
1405
1406            DSP4_WRITE_WORD(dsp4_vars.poly_ptr[0][0]);
1407            DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
1408            DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
1409
1410
1411            // update memory address
1412            dsp4_vars.poly_ptr[0][0] -= 4;
1413
1414            // update screen values
1415            x_scroll += px_dx;
1416            y_scroll += py_dy;
1417         }
1418      }
1419
1420      /////////////////////////////////////////////////////
1421      // Post-update
1422
1423      // update new viewer (x,y,scroll) to last dsp4_vars.raster line drawn
1424      dsp4_vars.view_x1 = dsp4_vars.view_x2;
1425      dsp4_vars.view_y1 = dsp4_vars.view_y2;
1426      dsp4_vars.view_xofs1 = dsp4_vars.view_xofs2;
1427      dsp4_vars.view_yofs1 = dsp4_vars.view_yofs2;
1428
1429      // add deltas for projection lines
1430      dsp4_vars.world_dx += SEX78(dsp4_vars.world_ddx);
1431      dsp4_vars.world_dy += SEX78(dsp4_vars.world_ddy);
1432
1433      // update projection lines
1434      dsp4_vars.world_x += (dsp4_vars.world_dx + dsp4_vars.world_xenv);
1435      dsp4_vars.world_y += dsp4_vars.world_dy;
1436
1437      ////////////////////////////////////////////////////
1438      // command check
1439
1440      // scan next command
1441      dsp4.in_count = 2;
1442      DSP4_WAIT(1) resume1 :
1443
1444      // inspect input
1445      dsp4_vars.distance = DSP4_READ_WORD();
1446
1447      // terminate op
1448      if (dsp4_vars.distance == -0x8000)
1449         break;
1450
1451      // already have 2 bytes in queue
1452      dsp4.in_count = 6;
1453      DSP4_WAIT(2) resume2:
1454
1455      // inspect inputs
1456      dsp4_vars.world_ddy = DSP4_READ_WORD();
1457      dsp4_vars.world_ddx = DSP4_READ_WORD();
1458      dsp4_vars.view_yofsenv = DSP4_READ_WORD();
1459
1460      // no envelope here
1461      dsp4_vars.world_xenv = 0;
1462   }
1463   while (1);
1464
1465   dsp4.waiting4command = 1;
1466}
1467
1468//////////////////////////////////////////////////////////////
1469
1470
1471static void dsp4_OP0E( void )
1472{
1473   dsp4_vars.OAM_RowMax = 16;
1474   memset(dsp4_vars.OAM_Row, 0, 64);
1475}
1476
1477
1478//////////////////////////////////////////////////////////////
1479
1480static void dsp4_OP0F( void )
1481{
1482   dsp4.waiting4command = 0;
1483
1484   // op flow control
1485   switch (dsp4_vars.Logic)
1486   {
1487      case 1:
1488         goto resume1; break;
1489      case 2:
1490         goto resume2; break;
1491      case 3:
1492         goto resume3; break;
1493      case 4:
1494         goto resume4; break;
1495   }
1496
1497   ////////////////////////////////////////////////////
1498   // process initial inputs
1499
1500   // sort inputs
1501   DSP4_READ_WORD(); // 0x0000
1502   dsp4_vars.world_y = DSP4_READ_DWORD();
1503   dsp4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
1504   dsp4_vars.poly_top[0][0] = DSP4_READ_WORD();
1505   dsp4_vars.poly_cx[1][0] = DSP4_READ_WORD();
1506   dsp4_vars.viewport_bottom = DSP4_READ_WORD();
1507   dsp4_vars.world_x = DSP4_READ_DWORD();
1508   dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1509   dsp4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
1510   dsp4_vars.world_yofs = DSP4_READ_WORD();
1511   dsp4_vars.world_dy = DSP4_READ_DWORD();
1512   dsp4_vars.world_dx = DSP4_READ_DWORD();
1513   dsp4_vars.distance = DSP4_READ_WORD();
1514   DSP4_READ_WORD(); // 0x0000
1515   dsp4_vars.world_xenv = DSP4_READ_DWORD();
1516   dsp4_vars.world_ddy = DSP4_READ_WORD();
1517   dsp4_vars.world_ddx = DSP4_READ_WORD();
1518   dsp4_vars.view_yofsenv = DSP4_READ_WORD();
1519
1520   // initial (x,y,offset) at starting dsp4_vars.raster line
1521   dsp4_vars.view_x1 = (INT16)((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16);
1522   dsp4_vars.view_y1 = (INT16)(dsp4_vars.world_y >> 16);
1523   dsp4_vars.view_xofs1 = (INT16)(dsp4_vars.world_x >> 16);
1524   dsp4_vars.view_yofs1 = dsp4_vars.world_yofs;
1525   dsp4_vars.view_turnoff_x = 0;
1526   dsp4_vars.view_turnoff_dx = 0;
1527
1528   // first dsp4_vars.raster line
1529   dsp4_vars.poly_raster[0][0] = dsp4_vars.poly_bottom[0][0];
1530
1531
1532   do
1533   {
1534      ////////////////////////////////////////////////////
1535      // process one iteration of projection
1536
1537      // perspective projection of world (x,y,scroll) points
1538      // based on the current projection lines
1539      dsp4_vars.view_x2 = (INT16)(((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16) * dsp4_vars.distance >> 15);
1540      dsp4_vars.view_y2 = (INT16)((dsp4_vars.world_y >> 16) * dsp4_vars.distance >> 15);
1541      dsp4_vars.view_xofs2 = dsp4_vars.view_x2;
1542      dsp4_vars.view_yofs2 = (dsp4_vars.world_yofs * dsp4_vars.distance >> 15) + dsp4_vars.poly_bottom[0][0] - dsp4_vars.view_y2;
1543
1544      // 1. World x-location before transformation
1545      // 2. Viewer x-position at the next
1546      // 3. World y-location before perspective projection
1547      // 4. Viewer y-position below the horizon
1548      // 5. Number of dsp4_vars.raster lines drawn in this iteration
1549
1550      DSP4_CLEAR_OUT();
1551      DSP4_WRITE_WORD((UINT16)((dsp4_vars.world_x + dsp4_vars.world_xenv) >> 16));
1552      DSP4_WRITE_WORD(dsp4_vars.view_x2);
1553      DSP4_WRITE_WORD((UINT16)(dsp4_vars.world_y >> 16));
1554      DSP4_WRITE_WORD(dsp4_vars.view_y2);
1555
1556      //////////////////////////////////////////////////////
1557
1558      // SR = 0x00
1559
1560      // determine # of dsp4_vars.raster lines used
1561      dsp4_vars.segments = dsp4_vars.poly_raster[0][0] - dsp4_vars.view_y2;
1562
1563      // prevent overdraw
1564      if (dsp4_vars.view_y2 >= dsp4_vars.poly_raster[0][0])
1565         dsp4_vars.segments = 0;
1566      else
1567         dsp4_vars.poly_raster[0][0] = dsp4_vars.view_y2;
1568
1569      // don't draw outside the window
1570      if (dsp4_vars.view_y2 < dsp4_vars.poly_top[0][0])
1571      {
1572         dsp4_vars.segments = 0;
1573
1574         // flush remaining dsp4_vars.raster lines
1575         if (dsp4_vars.view_y1 >= dsp4_vars.poly_top[0][0])
1576            dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.poly_top[0][0];
1577      }
1578
1579      // SR = 0x80
1580
1581      DSP4_WRITE_WORD(dsp4_vars.segments);
1582
1583      //////////////////////////////////////////////////////
1584
1585      // scan next command if no SR check needed
1586      if (dsp4_vars.segments)
1587      {
1588         INT32 px_dx, py_dy;
1589         INT32 x_scroll, y_scroll;
1590
1591         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < 4; dsp4_vars.lcv++)
1592         {
1593            // grab inputs
1594            dsp4.in_count = 4;
1595            DSP4_WAIT(1);
1596            resume1 :
1597            for (;;)
1598            {
1599               INT16 distance;
1600               INT16 color, red, green, blue;
1601
1602               distance = DSP4_READ_WORD();
1603               color = DSP4_READ_WORD();
1604
1605               // U1+B5+G5+R5
1606               red = color & 0x1f;
1607               green = (color >> 5) & 0x1f;
1608               blue = (color >> 10) & 0x1f;
1609
1610               // dynamic lighting
1611               red = (red * distance >> 15) & 0x1f;
1612               green = (green * distance >> 15) & 0x1f;
1613               blue = (blue * distance >> 15) & 0x1f;
1614               color = red | (green << 5) | (blue << 10);
1615
1616               DSP4_CLEAR_OUT();
1617               DSP4_WRITE_WORD(color);
1618               break;
1619            }
1620         }
1621
1622         //////////////////////////////////////////////////////
1623
1624         // SR = 0x00
1625
1626         // linear interpolation (lerp) between projected points
1627         px_dx = (dsp4_vars.view_xofs2 - dsp4_vars.view_xofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
1628         py_dy = (dsp4_vars.view_yofs2 - dsp4_vars.view_yofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
1629
1630
1631         // starting step values
1632         x_scroll = SEX16(dsp4_vars.poly_cx[0][0] + dsp4_vars.view_xofs1);
1633         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);
1634
1635         // SR = 0x80
1636
1637         // rasterize line
1638         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < dsp4_vars.segments; dsp4_vars.lcv++)
1639         {
1640            // 1. HDMA memory pointer
1641            // 2. vertical scroll offset ($210E)
1642            // 3. horizontal scroll offset ($210D)
1643
1644            DSP4_WRITE_WORD(dsp4_vars.poly_ptr[0][0]);
1645            DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
1646            DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
1647
1648            // update memory address
1649            dsp4_vars.poly_ptr[0][0] -= 4;
1650
1651            // update screen values
1652            x_scroll += px_dx;
1653            y_scroll += py_dy;
1654         }
1655      }
1656
1657      ////////////////////////////////////////////////////
1658      // Post-update
1659
1660      // update new viewer (x,y,scroll) to last dsp4_vars.raster line drawn
1661      dsp4_vars.view_x1 = dsp4_vars.view_x2;
1662      dsp4_vars.view_y1 = dsp4_vars.view_y2;
1663      dsp4_vars.view_xofs1 = dsp4_vars.view_xofs2;
1664      dsp4_vars.view_yofs1 = dsp4_vars.view_yofs2;
1665
1666      // add deltas for projection lines
1667      dsp4_vars.world_dx += SEX78(dsp4_vars.world_ddx);
1668      dsp4_vars.world_dy += SEX78(dsp4_vars.world_ddy);
1669
1670      // update projection lines
1671      dsp4_vars.world_x += (dsp4_vars.world_dx + dsp4_vars.world_xenv);
1672      dsp4_vars.world_y += dsp4_vars.world_dy;
1673
1674      // update road turnoff position
1675      dsp4_vars.view_turnoff_x += dsp4_vars.view_turnoff_dx;
1676
1677      ////////////////////////////////////////////////////
1678      // command check
1679
1680      // scan next command
1681      dsp4.in_count = 2;
1682      DSP4_WAIT(2) resume2:
1683
1684      // check for termination
1685      dsp4_vars.distance = DSP4_READ_WORD();
1686      if (dsp4_vars.distance == -0x8000)
1687         break;
1688
1689      // road splice
1690      if( (UINT16) dsp4_vars.distance == 0x8001 )
1691      {
1692         dsp4.in_count = 6;
1693         DSP4_WAIT(3) resume3:
1694
1695         dsp4_vars.distance = DSP4_READ_WORD();
1696         dsp4_vars.view_turnoff_x = DSP4_READ_WORD();
1697         dsp4_vars.view_turnoff_dx = DSP4_READ_WORD();
1698
1699         // factor in new changes
1700         dsp4_vars.view_x1 += ( dsp4_vars.view_turnoff_x * dsp4_vars.distance >> 15 );
1701         dsp4_vars.view_xofs1 += ( dsp4_vars.view_turnoff_x * dsp4_vars.distance >> 15 );
1702
1703         // update stepping values
1704         dsp4_vars.view_turnoff_x += dsp4_vars.view_turnoff_dx;
1705
1706         dsp4.in_count = 2;
1707         DSP4_WAIT(2)
1708      }
1709
1710      // already have 2 bytes in queue
1711      dsp4.in_count = 6;
1712      DSP4_WAIT(4) resume4 :
1713
1714      // inspect inputs
1715      dsp4_vars.world_ddy = DSP4_READ_WORD();
1716      dsp4_vars.world_ddx = DSP4_READ_WORD();
1717      dsp4_vars.view_yofsenv = DSP4_READ_WORD();
1718
1719      // no envelope here
1720      dsp4_vars.world_xenv = 0;
1721   }
1722   while (1);
1723
1724   // terminate op
1725   dsp4.waiting4command = 1;
1726}
1727
1728//////////////////////////////////////////////////////////////
1729
1730
1731static void dsp4_OP10( void )
1732{
1733   dsp4.waiting4command = 0;
1734
1735   // op flow control
1736   switch (dsp4_vars.Logic)
1737   {
1738      case 1:
1739         goto resume1; break;
1740      case 2:
1741         goto resume2; break;
1742      case 3:
1743         goto resume3; break;
1744   }
1745
1746   ////////////////////////////////////////////////////
1747   // sort inputs
1748
1749   DSP4_READ_WORD(); // 0x0000
1750   dsp4_vars.world_y = DSP4_READ_DWORD();
1751   dsp4_vars.poly_bottom[0][0] = DSP4_READ_WORD();
1752   dsp4_vars.poly_top[0][0] = DSP4_READ_WORD();
1753   dsp4_vars.poly_cx[1][0] = DSP4_READ_WORD();
1754   dsp4_vars.viewport_bottom = DSP4_READ_WORD();
1755   dsp4_vars.world_x = DSP4_READ_DWORD();
1756   dsp4_vars.poly_cx[0][0] = DSP4_READ_WORD();
1757   dsp4_vars.poly_ptr[0][0] = DSP4_READ_WORD();
1758   dsp4_vars.world_yofs = DSP4_READ_WORD();
1759   dsp4_vars.distance = DSP4_READ_WORD();
1760   dsp4_vars.view_y2 = DSP4_READ_WORD();
1761   dsp4_vars.view_dy = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
1762   dsp4_vars.view_x2 = DSP4_READ_WORD();
1763   dsp4_vars.view_dx = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
1764   dsp4_vars.view_yofsenv = DSP4_READ_WORD();
1765
1766   // initial (x,y,offset) at starting dsp4_vars.raster line
1767   dsp4_vars.view_x1 = (INT16)(dsp4_vars.world_x >> 16);
1768   dsp4_vars.view_y1 = (INT16)(dsp4_vars.world_y >> 16);
1769   dsp4_vars.view_xofs1 = dsp4_vars.view_x1;
1770   dsp4_vars.view_yofs1 = dsp4_vars.world_yofs;
1771
1772   // first dsp4_vars.raster line
1773   dsp4_vars.poly_raster[0][0] = dsp4_vars.poly_bottom[0][0];
1774
1775   do
1776   {
1777      ////////////////////////////////////////////////////
1778      // process one iteration of projection
1779
1780      // add shaping
1781      dsp4_vars.view_x2 += dsp4_vars.view_dx;
1782      dsp4_vars.view_y2 += dsp4_vars.view_dy;
1783
1784      // vertical scroll calculation
1785      dsp4_vars.view_xofs2 = dsp4_vars.view_x2;
1786      dsp4_vars.view_yofs2 = (dsp4_vars.world_yofs * dsp4_vars.distance >> 15) + dsp4_vars.poly_bottom[0][0] - dsp4_vars.view_y2;
1787
1788      // 1. Viewer x-position at the next
1789      // 2. Viewer y-position below the horizon
1790      // 3. Number of dsp4_vars.raster lines drawn in this iteration
1791
1792      DSP4_CLEAR_OUT();
1793      DSP4_WRITE_WORD(dsp4_vars.view_x2);
1794      DSP4_WRITE_WORD(dsp4_vars.view_y2);
1795
1796      //////////////////////////////////////////////////////
1797
1798      // SR = 0x00
1799
1800      // determine # of dsp4_vars.raster lines used
1801      dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.view_y2;
1802
1803      // prevent overdraw
1804      if (dsp4_vars.view_y2 >= dsp4_vars.poly_raster[0][0])
1805         dsp4_vars.segments = 0;
1806      else
1807         dsp4_vars.poly_raster[0][0] = dsp4_vars.view_y2;
1808
1809      // don't draw outside the window
1810      if (dsp4_vars.view_y2 < dsp4_vars.poly_top[0][0])
1811      {
1812         dsp4_vars.segments = 0;
1813
1814         // flush remaining dsp4_vars.raster lines
1815         if (dsp4_vars.view_y1 >= dsp4_vars.poly_top[0][0])
1816            dsp4_vars.segments = dsp4_vars.view_y1 - dsp4_vars.poly_top[0][0];
1817      }
1818
1819      // SR = 0x80
1820
1821      DSP4_WRITE_WORD(dsp4_vars.segments);
1822
1823      //////////////////////////////////////////////////////
1824
1825      // scan next command if no SR check needed
1826      if (dsp4_vars.segments)
1827      {
1828         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < 4; dsp4_vars.lcv++)
1829         {
1830            // grab inputs
1831            dsp4.in_count = 4;
1832            DSP4_WAIT(1);
1833            resume1 :
1834            for (;;)
1835            {
1836               INT16 distance;
1837               INT16 color, red, green, blue;
1838
1839               distance = DSP4_READ_WORD();
1840               color = DSP4_READ_WORD();
1841
1842               // U1+B5+G5+R5
1843               red = color & 0x1f;
1844               green = (color >> 5) & 0x1f;
1845               blue = (color >> 10) & 0x1f;
1846
1847               // dynamic lighting
1848               red = (red * distance >> 15) & 0x1f;
1849               green = (green * distance >> 15) & 0x1f;
1850               blue = (blue * distance >> 15) & 0x1f;
1851               color = red | (green << 5) | (blue << 10);
1852
1853               DSP4_CLEAR_OUT();
1854               DSP4_WRITE_WORD(color);
1855               break;
1856            }
1857         }
1858      }
1859
1860      //////////////////////////////////////////////////////
1861
1862      // scan next command if no SR check needed
1863      if (dsp4_vars.segments)
1864      {
1865         INT32 px_dx, py_dy;
1866         INT32 x_scroll, y_scroll;
1867
1868         // SR = 0x00
1869
1870         // linear interpolation (lerp) between projected points
1871         px_dx = (dsp4_vars.view_xofs2 - dsp4_vars.view_xofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
1872         py_dy = (dsp4_vars.view_yofs2 - dsp4_vars.view_yofs1) * dsp4_Inverse(dsp4_vars.segments) << 1;
1873
1874         // starting step values
1875         x_scroll = SEX16(dsp4_vars.poly_cx[0][0] + dsp4_vars.view_xofs1);
1876         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);
1877
1878         // SR = 0x80
1879
1880         // rasterize line
1881         for (dsp4_vars.lcv = 0; dsp4_vars.lcv < dsp4_vars.segments; dsp4_vars.lcv++)
1882         {
1883            // 1. HDMA memory pointer (bg2)
1884            // 2. vertical scroll offset ($2110)
1885            // 3. horizontal scroll offset ($210F)
1886
1887            DSP4_WRITE_WORD(dsp4_vars.poly_ptr[0][0]);
1888            DSP4_WRITE_WORD((UINT16)((y_scroll + 0x8000) >> 16));
1889            DSP4_WRITE_WORD((UINT16)((x_scroll + 0x8000) >> 16));
1890
1891            // update memory address
1892            dsp4_vars.poly_ptr[0][0] -= 4;
1893
1894            // update screen values
1895            x_scroll += px_dx;
1896            y_scroll += py_dy;
1897         }
1898      }
1899
1900      /////////////////////////////////////////////////////
1901      // Post-update
1902
1903      // update new viewer (x,y,scroll) to last dsp4_vars.raster line drawn
1904      dsp4_vars.view_x1 = dsp4_vars.view_x2;
1905      dsp4_vars.view_y1 = dsp4_vars.view_y2;
1906      dsp4_vars.view_xofs1 = dsp4_vars.view_xofs2;
1907      dsp4_vars.view_yofs1 = dsp4_vars.view_yofs2;
1908
1909      ////////////////////////////////////////////////////
1910      // command check
1911
1912      // scan next command
1913      dsp4.in_count = 2;
1914      DSP4_WAIT(2) resume2 :
1915
1916      // check for opcode termination
1917      dsp4_vars.distance = DSP4_READ_WORD();
1918      if (dsp4_vars.distance == -0x8000)
1919         break;
1920
1921      // already have 2 bytes in queue
1922      dsp4.in_count = 10;
1923      DSP4_WAIT(3) resume3 :
1924
1925
1926      // inspect inputs
1927      dsp4_vars.view_y2 = DSP4_READ_WORD();
1928      dsp4_vars.view_dy = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
1929      dsp4_vars.view_x2 = DSP4_READ_WORD();
1930      dsp4_vars.view_dx = DSP4_READ_WORD() * dsp4_vars.distance >> 15;
1931   }
1932   while (1);
1933
1934   dsp4.waiting4command = 1;
1935}
1936
1937//////////////////////////////////////////////////////////////
1938
1939static void dsp4_OP11(INT16 A, INT16 B, INT16 C, INT16 D, INT16 *M)
1940{
1941   // 0x155 = 341 = Horizontal Width of the Screen
1942   *M = ((A * 0x0155 >> 2) & 0xf000) |
1943          ((B * 0x0155 >> 6) & 0x0f00) |
1944          ((C * 0x0155 >> 10) & 0x00f0) |
1945          ((D * 0x0155 >> 14) & 0x000f);
1946}
1947
1948
1949
1950
1951
1952/////////////////////////////////////////////////////////////
1953//Processing Code
1954/////////////////////////////////////////////////////////////
1955
1956static void dsp4_register_save( running_machine *machine )
1957{
1958   state_save_register_global(machine, dsp4.waiting4command);
1959   state_save_register_global(machine, dsp4.half_command);
1960   state_save_register_global(machine, dsp4.command);
1961   state_save_register_global(machine, dsp4.in_count);
1962   state_save_register_global(machine, dsp4.in_index);
1963   state_save_register_global(machine, dsp4.out_count);
1964   state_save_register_global(machine, dsp4.out_index);
1965   state_save_register_global_array(machine, dsp4.parameters);
1966   state_save_register_global_array(machine, dsp4.output);
1967
1968   state_save_register_global(machine, dsp4_vars.Logic);
1969   state_save_register_global(machine, dsp4_vars.lcv);
1970   state_save_register_global(machine, dsp4_vars.distance);
1971   state_save_register_global(machine, dsp4_vars.raster);
1972   state_save_register_global(machine, dsp4_vars.segments);
1973
1974   state_save_register_global(machine, dsp4_vars.world_x);
1975   state_save_register_global(machine, dsp4_vars.world_y);
1976   state_save_register_global(machine, dsp4_vars.world_dx);
1977   state_save_register_global(machine, dsp4_vars.world_dy);
1978   state_save_register_global(machine, dsp4_vars.world_ddx);
1979   state_save_register_global(machine, dsp4_vars.world_ddy);
1980   state_save_register_global(machine, dsp4_vars.world_xenv);
1981   state_save_register_global(machine, dsp4_vars.world_yofs);
1982
1983   state_save_register_global(machine, dsp4_vars.view_x1);
1984   state_save_register_global(machine, dsp4_vars.view_y1);
1985   state_save_register_global(machine, dsp4_vars.view_x2);
1986   state_save_register_global(machine, dsp4_vars.view_y2);
1987   state_save_register_global(machine, dsp4_vars.view_dx);
1988   state_save_register_global(machine, dsp4_vars.view_dy);
1989   state_save_register_global(machine, dsp4_vars.view_xofs1);
1990   state_save_register_global(machine, dsp4_vars.view_yofs1);
1991   state_save_register_global(machine, dsp4_vars.view_xofs2);
1992   state_save_register_global(machine, dsp4_vars.view_yofs2);
1993   state_save_register_global(machine, dsp4_vars.view_yofsenv);
1994   state_save_register_global(machine, dsp4_vars.view_turnoff_x);
1995   state_save_register_global(machine, dsp4_vars.view_turnoff_dx);
1996
1997
1998   state_save_register_global(machine, dsp4_vars.viewport_cx);
1999   state_save_register_global(machine, dsp4_vars.viewport_cy);
2000   state_save_register_global(machine, dsp4_vars.viewport_left);
2001   state_save_register_global(machine, dsp4_vars.viewport_right);
2002   state_save_register_global(machine, dsp4_vars.viewport_top);
2003   state_save_register_global(machine, dsp4_vars.viewport_bottom);
2004
2005
2006   state_save_register_global(machine, dsp4_vars.sprite_x);
2007   state_save_register_global(machine, dsp4_vars.sprite_y);
2008   state_save_register_global(machine, dsp4_vars.sprite_attr);
2009   state_save_register_global(machine, dsp4_vars.sprite_size);
2010   state_save_register_global(machine, dsp4_vars.sprite_clipy);
2011   state_save_register_global(machine, dsp4_vars.sprite_count);
2012
2013   state_save_register_global_array(machine, dsp4_vars.poly_clipLf[0]);
2014   state_save_register_global_array(machine, dsp4_vars.poly_clipLf[1]);
2015   state_save_register_global_array(machine, dsp4_vars.poly_clipRt[0]);
2016   state_save_register_global_array(machine, dsp4_vars.poly_clipRt[1]);
2017   state_save_register_global_array(machine, dsp4_vars.poly_ptr[0]);
2018   state_save_register_global_array(machine, dsp4_vars.poly_ptr[1]);
2019   state_save_register_global_array(machine, dsp4_vars.poly_raster[0]);
2020   state_save_register_global_array(machine, dsp4_vars.poly_raster[1]);
2021   state_save_register_global_array(machine, dsp4_vars.poly_top[0]);
2022   state_save_register_global_array(machine, dsp4_vars.poly_top[1]);
2023   state_save_register_global_array(machine, dsp4_vars.poly_bottom[0]);
2024   state_save_register_global_array(machine, dsp4_vars.poly_bottom[1]);
2025   state_save_register_global_array(machine, dsp4_vars.poly_cx[0]);
2026   state_save_register_global_array(machine, dsp4_vars.poly_cx[1]);
2027   state_save_register_global_array(machine, dsp4_vars.poly_start);
2028   state_save_register_global_array(machine, dsp4_vars.poly_plane);
2029
2030   state_save_register_global_array(machine, dsp4_vars.OAM_attr);
2031   state_save_register_global(machine, dsp4_vars.OAM_index);
2032   state_save_register_global(machine, dsp4_vars.OAM_bits);
2033
2034   state_save_register_global(machine, dsp4_vars.OAM_RowMax);
2035   state_save_register_global_array(machine, dsp4_vars.OAM_Row);
2036}
2037
2038static void dsp4_init( running_machine *machine )
2039{
2040   memset(&dsp4, 0, sizeof(dsp4));
2041   dsp4.waiting4command = 1;
2042
2043   dsp4_register_save(machine);
2044}
2045
2046static void dsp4_write( UINT8 dsp4_byte )
2047{
2048   // clear pending read
2049   if (dsp4.out_index < dsp4.out_count)
2050   {
2051      dsp4.out_index++;
2052      return;
2053   }
2054
2055   if (dsp4.waiting4command)
2056   {
2057      if (dsp4.half_command)
2058      {
2059         dsp4.command |= (dsp4_byte << 8);
2060         dsp4.in_index = 0;
2061         dsp4.waiting4command = 0;
2062         dsp4.half_command = 0;
2063         dsp4.out_count = 0;
2064         dsp4.out_index = 0;
2065
2066         dsp4_vars.Logic = 0;
2067
2068
2069         switch (dsp4.command)
2070         {
2071            case 0x0000:
2072               dsp4.in_count = 4; break;
2073            case 0x0001:
2074               dsp4.in_count = 44; break;
2075            case 0x0003:
2076               dsp4.in_count = 0; break;
2077            case 0x0005:
2078               dsp4.in_count = 0; break;
2079            case 0x0006:
2080               dsp4.in_count = 0; break;
2081            case 0x0007:
2082               dsp4.in_count = 34; break;
2083            case 0x0008:
2084               dsp4.in_count = 90; break;
2085            case 0x0009:
2086               dsp4.in_count = 14; break;
2087            case 0x000a:
2088               dsp4.in_count = 6; break;
2089            case 0x000b:
2090               dsp4.in_count = 6; break;
2091            case 0x000d:
2092               dsp4.in_count = 42; break;
2093            case 0x000e:
2094               dsp4.in_count = 0; break;
2095            case 0x000f:
2096               dsp4.in_count = 46; break;
2097            case 0x0010:
2098               dsp4.in_count = 36; break;
2099            case 0x0011:
2100               dsp4.in_count = 8; break;
2101            default:
2102               dsp4.waiting4command = 1;
2103               break;
2104         }
2105      }
2106      else
2107      {
2108         dsp4.command = dsp4_byte;
2109         dsp4.half_command = 1;
2110      }
2111   }
2112   else
2113   {
2114      dsp4.parameters[dsp4.in_index] = dsp4_byte;
2115      dsp4.in_index++;
2116   }
2117
2118   if (!dsp4.waiting4command && dsp4.in_count == dsp4.in_index)
2119   {
2120      // Actually execute the command
2121      dsp4.waiting4command = 1;
2122      dsp4.out_index = 0;
2123      dsp4.in_index = 0;
2124
2125      switch (dsp4.command)
2126      {
2127            // 16-bit multiplication
2128         case 0x0000:
2129         {
2130            INT16 multiplier, multiplicand;
2131            INT32 product;
2132
2133            multiplier = DSP4_READ_WORD();
2134            multiplicand = DSP4_READ_WORD();
2135
2136            dsp4_Multiply(multiplicand, multiplier, &product);
2137
2138            DSP4_CLEAR_OUT();
2139            DSP4_WRITE_WORD((UINT16)(product));
2140            DSP4_WRITE_WORD((UINT16)(product >> 16));
2141         }
2142         break;
2143
2144         // single-player track projection
2145         case 0x0001:
2146            dsp4_OP01(); break;
2147
2148         // single-player selection
2149         case 0x0003:
2150            dsp4_OP03(); break;
2151
2152         // clear OAM
2153         case 0x0005:
2154            dsp4_OP05(); break;
2155
2156         // transfer OAM
2157         case 0x0006:
2158            dsp4_OP06(); break;
2159
2160         // single-player track turnoff projection
2161         case 0x0007:
2162            dsp4_OP07(); break;
2163
2164         // solid polygon projection
2165         case 0x0008:
2166            dsp4_OP08(); break;
2167
2168         // sprite projection
2169         case 0x0009:
2170            dsp4_OP09(); break;
2171
2172         // unknown
2173         case 0x000A:
2174         {
2175            INT16 in2a;
2176            INT16 out1a, out2a, out3a, out4a;
2177
2178            /* in1a = */ DSP4_READ_WORD();
2179            in2a = DSP4_READ_WORD();
2180            /* in3a = */ DSP4_READ_WORD();
2181
2182            dsp4_OP0A(in2a, &out2a, &out1a, &out4a, &out3a);
2183
2184            DSP4_CLEAR_OUT();
2185            DSP4_WRITE_WORD(out1a);
2186            DSP4_WRITE_WORD(out2a);
2187            DSP4_WRITE_WORD(out3a);
2188            DSP4_WRITE_WORD(out4a);
2189         }
2190         break;
2191
2192         // set OAM
2193         case 0x000B:
2194         {
2195            INT16 sp_x = DSP4_READ_WORD();
2196            INT16 sp_y = DSP4_READ_WORD();
2197            INT16 sp_attr = DSP4_READ_WORD();
2198            UINT8 draw = 1;
2199
2200            DSP4_CLEAR_OUT();
2201
2202            dsp4_OP0B(&draw, sp_x, sp_y, sp_attr, 0, 1);
2203         }
2204         break;
2205
2206         // multi-player track projection
2207         case 0x000D:
2208            dsp4_OP0D(); break;
2209
2210         // multi-player selection
2211         case 0x000E:
2212            dsp4_OP0E(); break;
2213
2214         // single-player track projection with lighting
2215         case 0x000F:
2216            dsp4_OP0F(); break;
2217
2218         // single-player track turnoff projection with lighting
2219         case 0x0010:
2220            dsp4_OP10(); break;
2221
2222         // unknown: horizontal mapping command
2223         case 0x0011:
2224         {
2225            INT16 a, b, c, d, m;
2226
2227
2228            d = DSP4_READ_WORD();
2229            c = DSP4_READ_WORD();
2230            b = DSP4_READ_WORD();
2231            a = DSP4_READ_WORD();
2232
2233            dsp4_OP11(a, b, c, d, &m);
2234
2235            DSP4_CLEAR_OUT();
2236            DSP4_WRITE_WORD(m);
2237
2238            break;
2239         }
2240
2241         default:
2242            break;
2243      }
2244   }
2245}
2246
2247static UINT8 dsp4_read( void )
2248{
2249   UINT8 value;
2250
2251   if (dsp4.out_count)
2252   {
2253      value = (UINT8) dsp4.output[dsp4.out_index&0x1FF];
2254      dsp4.out_index++;
2255      if (dsp4.out_count == dsp4.out_index)
2256         dsp4.out_count = 0;
2257   }
2258   else
2259   {
2260      value = 0xff;
2261   }
2262
2263   return value;
2264}
src/mame/machine/snesdsp4.h
r9893r9894
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
21struct dsp4_vars_t
22{
23   // op control
24   INT8 Logic;            // controls op flow
25
26
27   // projection format
28   INT16 lcv;                  // loop-control variable
29   INT16 distance;             // z-position into virtual world
30   INT16 raster;               // current raster line
31   INT16 segments;             // number of raster lines drawn
32
33   // 1.15.16 or 1.15.0 [sign, INTeger, fraction]
34   INT32 world_x;              // line of x-projection in world
35   INT32 world_y;              // line of y-projection in world
36   INT32 world_dx;             // projection line x-delta
37   INT32 world_dy;             // projection line y-delta
38   INT16 world_ddx;            // x-delta increment
39   INT16 world_ddy;            // y-delta increment
40   INT32 world_xenv;           // world x-shaping factor
41   INT16 world_yofs;           // world y-vertical scroll
42
43   INT16 view_x1;              // current viewer-x
44   INT16 view_y1;              // current viewer-y
45   INT16 view_x2;              // future viewer-x
46   INT16 view_y2;              // future viewer-y
47   INT16 view_dx;              // view x-delta factor
48   INT16 view_dy;              // view y-delta factor
49   INT16 view_xofs1;           // current viewer x-vertical scroll
50   INT16 view_yofs1;           // current viewer y-vertical scroll
51   INT16 view_xofs2;           // future viewer x-vertical scroll
52   INT16 view_yofs2;           // future viewer y-vertical scroll
53   INT16 view_yofsenv;         // y-scroll shaping factor
54   INT16 view_turnoff_x;       // road turnoff data
55   INT16 view_turnoff_dx;      // road turnoff delta factor
56
57
58   // drawing area
59
60   INT16 viewport_cx;          // x-center of viewport window
61   INT16 viewport_cy;          // y-center of render window
62   INT16 viewport_left;        // x-left of viewport
63   INT16 viewport_right;       // x-right of viewport
64   INT16 viewport_top;         // y-top of viewport
65   INT16 viewport_bottom;      // y-bottom of viewport
66
67
68   // sprite structure
69
70   INT16 sprite_x;             // projected x-pos of sprite
71   INT16 sprite_y;             // projected y-pos of sprite
72   INT16 sprite_attr;          // obj attributes
73   UINT8 sprite_size;          // sprite size: 8x8 or 16x16
74   INT16 sprite_clipy;         // visible line to clip pixels off
75   INT16 sprite_count;
76
77   // generic projection variables designed for
78   // two solid polygons + two polygon sides
79
80   INT16 poly_clipLf[2][2];    // left clip boundary
81   INT16 poly_clipRt[2][2];    // right clip boundary
82   INT16 poly_ptr[2][2];       // HDMA structure pointers
83   INT16 poly_raster[2][2];    // current raster line below horizon
84   INT16 poly_top[2][2];       // top clip boundary
85   INT16 poly_bottom[2][2];    // bottom clip boundary
86   INT16 poly_cx[2][2];        // center for left/right points
87   INT16 poly_start[2];        // current projection points
88   INT16 poly_plane[2];        // previous z-plane distance
89
90
91   // OAM
92   INT16 OAM_attr[16];         // OAM (size,MSB) data
93   INT16 OAM_index;            // index into OAM table
94   INT16 OAM_bits;             // offset into OAM table
95
96   INT16 OAM_RowMax;           // maximum number of tiles per 8 aligned pixels (row)
97   INT16 OAM_Row[32];          // current number of tiles per row
98};
99
100#endif
src/mame/machine/snesdsp1.c
r9893r9894
1/***************************************************************************
2
3  snesdsp1.c
4
5  File to handle emulation of the SNES "DSP-1" add-on chip.
6
7  Original C++ "dsp1emul.cpp" by Andreas Naive
8  Based on research by Overload, The Dumper, Neviksti and Andreas Naive
9  MAME/MESS C conversion by R. Belmont
10
11  This is up to date with the source version dated June 2006.
12
13***************************************************************************/
14
15#define dsp1_VERSION 0x0102
16
17// The DSP-1 status register has 16 bits, but only
18// the upper 8 bits can be accessed from an external device, so all these
19// positions are referred to the upper byte (bits D8 to D15)
20
21enum SrFlags { DRC = 0x04, DRS = 0x10, RQM = 0x80 };
22
23// According to Overload's docs, these are the meanings of the flags:
24// DRC: The Data Register Control (DRC) bit specifies the data transfer length to and from the host CPU.
25//  0: Data transfer to and from the DSP-1 is 16 bits.
26//  1: Data transfer to and from the DSP-1 is 8 bits.
27// DRS: The Data Register Status (DRS) bit indicates the data transfer status in the case of transfering 16-bit data.
28//  0: Data transfer has terminated.
29//  1: Data transfer in progress.
30// RQM: The Request for Master (RQM) indicates that the DSP1 is requesting host CPU for data read/write.
31//  0: Internal Data Register Transfer.
32//  1: External Data Register Transfer.
33
34enum Fsm_Major_State { WAIT_COMMAND, READ_DATA, WRITE_DATA };
35enum Max_Data_Accesses { MAX_READS = 7, MAX_WRITES = 1024 };
36
37struct dsp1_Command
38{
39   void (*callback)(INT16 *, INT16 *);
40   unsigned int reads;
41   unsigned int writes;
42};
43
44struct SharedData
45{
46   // some RAM variables shared between commands
47   INT16 MatrixA[3][3];          // attitude matrix A
48   INT16 MatrixB[3][3];
49   INT16 MatrixC[3][3];
50   INT16 CentreX, CentreY, CentreZ;   // center of projection
51   INT16 CentreZ_C, CentreZ_E;
52   INT16 VOffset;                     // vertical offset of the screen with regard to the centre of projection
53   INT16 Les, C_Les, E_Les;
54   INT16 SinAas, CosAas;
55   INT16 SinAzs, CosAzs;
56   INT16 SinAZS, CosAZS;
57   INT16 SecAZS_C1, SecAZS_E1;
58   INT16 SecAZS_C2, SecAZS_E2;
59   INT16 Nx, Ny, Nz;    // normal vector to the screen (norm 1, points toward the center of projection)
60   INT16 Gx, Gy, Gz;    // center of the screen (global coordinates)
61   INT16 Hx, Hy;        // horizontal vector of the screen (Hz=0, norm 1, points toward the right of the screen)
62   INT16 Vx, Vy, Vz;    // vertical vector of the screen (norm 1, points toward the top of the screen)
63};
64
65struct _snes_dsp1_state
66{
67   SharedData       shared;
68   UINT8            Sr;                       // status register
69   int              SrLowByteAccess;
70   UINT16           Dr;                       // "internal" representation of the data register
71   Fsm_Major_State  FsmMajorState;            // current major state of the FSM
72   UINT8            Command;                  // current command processed by the FSM
73   UINT8            DataCounter;              // #UINT16 read/writes counter used by the FSM
74   INT16            ReadBuffer[MAX_READS];
75   INT16            WriteBuffer[MAX_WRITES];
76   UINT8            Freeze;                   // need explanation?  ;)
77
78
79
80//////////////////////////////////////////////////////////////////
81
82// Data ROM, as logged from a DSP-1B with the 0x1f command;
83// it contains the tables and constants used by the commands.
84// The tables used are: two shift tables (0x022-0x031 and 0x031-0x040 -this last one
85// with an error in 0x03c which has survived to all the DSP-1 revisions-); a inverse
86// table (used as initial guess) at 0x065-0x0e4; a square root table (used also
87// as initial guess) at 0x0e5-0x115; two sin and cos tables (used as nodes to construct
88// a interpolation curve) at, respectively, 0x116-0x197 and 0x196-0x215.
89// As a curiosity, in the positions 0x21c-0x31c it's contained a
90// 257-points arccos table that, apparently, have been not used anywhere
91// (maybe for the MaxAZS_Exp table?).
92
93   UINT16         DataRom[1024];
94};
95
96static struct _snes_dsp1_state  dsp1_state;
97
98
99/* Prototypes */
100
101static void dsp1_fsm_step(UINT8 read, UINT8 *data);            // FSM logic
102
103// commands
104static void dsp1_memory_test(INT16 *input, INT16 *output);
105static void dsp1_memory_dump(INT16 *input, INT16 *output);
106static void dsp1_memory_size(INT16 *input, INT16 *output);
107static void dsp1_multiply(INT16* input, INT16* output);
108static void dsp1_multiply2(INT16* input, INT16* output);
109static void dsp1_inverse(INT16 *input, INT16 *output);
110static void dsp1_triangle(INT16 *input, INT16 *output);
111static void dsp1_radius(INT16 *input, INT16 *output);
112static void dsp1_range(INT16 *input, INT16 *output);
113static void dsp1_range2(INT16 *input, INT16 *output);
114static void dsp1_distance(INT16 *input, INT16 *output);
115static void dsp1_rotate(INT16 *input, INT16 *output);
116static void dsp1_polar(INT16 *input, INT16 *output);
117static void dsp1_attitudeA(INT16 *input, INT16 *output);
118static void dsp1_attitudeB(INT16 *input, INT16 *output);
119static void dsp1_attitudeC(INT16 *input, INT16 *output);
120static void dsp1_objectiveA(INT16 *input, INT16 *output);
121static void dsp1_objectiveB(INT16 *input, INT16 *output);
122static void dsp1_objectiveC(INT16 *input, INT16 *output);
123static void dsp1_subjectiveA(INT16 *input, INT16 *output);
124static void dsp1_subjectiveB(INT16 *input, INT16 *output);
125static void dsp1_subjectiveC(INT16 *input, INT16 *output);
126static void dsp1_scalarA(INT16 *input, INT16 *output);
127static void dsp1_scalarB(INT16 *input, INT16 *output);
128static void dsp1_scalarC(INT16 *input, INT16 *output);
129static void dsp1_gyrate(INT16 *input, INT16 *output);
130static void dsp1_parameter(INT16 *input, INT16 *output);
131static void dsp1_raster(INT16 *input, INT16 *output);
132static void dsp1_target(INT16 *input, INT16 *output);
133static void dsp1_project(INT16 *input, INT16 *output);
134
135// auxiliar functions
136static INT16 dsp1_sin(INT16 Angle);
137static INT16 dsp1_cos(INT16 Angle);
138static void inverse(INT16 Coefficient, INT16 Exponent, INT16 *iCoefficient, INT16 *iExponent);
139static INT16 denormalize_and_clip(INT16 C, INT16 E);
140static void normalize(INT16 m, INT16 *Coefficient, INT16 *Exponent);
141static void normalize_double(INT32 Product, INT16 *Coefficient, INT16 *Exponent);
142static INT16 shiftR(INT16 C, INT16 E);
143
144// register saves
145static void dsp1_register_save(running_machine *machine);
146
147//////////////////////////////////////////////////////////////////
148
149// The info on this table follows Overload's docs.
150
151static const struct dsp1_Command mCommandTable[0x40] =
152{
153   {&dsp1_multiply, 2, 1},   //0x00
154   {&dsp1_attitudeA, 4, 0},    //0x01
155   {&dsp1_parameter, 7, 4},   //0x02
156   {&dsp1_subjectiveA, 3, 3},    //0x03
157   {&dsp1_triangle, 2, 2},   //0x04
158   {&dsp1_attitudeA, 4, 0},   //0x01
159   {&dsp1_project, 3, 3},   //0x06
160   {&dsp1_memory_test, 1, 1},   //0x0f
161   {&dsp1_radius, 3, 2},   //0x08
162   {&dsp1_objectiveA, 3, 3},   //0x0d
163   {&dsp1_raster, 1, 4},   // 0x0a. This will normally work in continuous mode
164   {&dsp1_scalarA, 3, 1},   //0x0b
165   {&dsp1_rotate, 3, 2},   //0x0c
166   {&dsp1_objectiveA, 3, 3},   //0x0d
167   {&dsp1_target, 2, 2},   //0x0e
168   {&dsp1_memory_test, 1, 1},   //0x0f
169
170   {&dsp1_inverse, 2, 2},   //0x10
171   {&dsp1_attitudeB, 4, 0},   //0x11
172   {&dsp1_parameter, 7, 4},   //0x02
173   {&dsp1_subjectiveB, 3, 3},   //0x13
174   {&dsp1_gyrate, 6, 3},   //0x14
175   {&dsp1_attitudeB, 4, 0},   //0x11
176   {&dsp1_project, 3, 3},   //0x06
177   {&dsp1_memory_dump, 1, 1024},   //0x1f
178   {&dsp1_range, 4, 1},   //0x18
179   {&dsp1_objectiveB, 3, 3},   //0x1d
180   {0, 0, 0},   // 0x1a; the chip freezes
181   {&dsp1_scalarB, 3, 1},   //0x1b
182   {&dsp1_polar, 6, 3},   //0x1c
183   {&dsp1_objectiveB, 3, 3},   //0x1d
184   {&dsp1_target, 2, 2},   //0x0e
185   {&dsp1_memory_dump, 1, 1024},   //0x1f
186
187   {&dsp1_multiply2, 2, 1},   //0x20
188   {&dsp1_attitudeC, 4, 0},   //0x21
189   {&dsp1_parameter, 7, 4},   //0x02
190   {&dsp1_subjectiveC, 3, 3},   //0x23
191   {&dsp1_triangle, 2, 2},   //0x04
192   {&dsp1_attitudeC, 4, 0},   //0x21
193   {&dsp1_project, 3, 3},   //0x06
194   {&dsp1_memory_size, 1, 1},    //0x2f
195   {&dsp1_distance, 3, 1},   //0x28
196   {&dsp1_objectiveC, 3, 3},   //0x2d
197   {0, 0, 0},   // 0x1a; the chip freezes
198   {&dsp1_scalarC, 3, 1},   //0x2b
199   {&dsp1_rotate, 3, 2},   //0x0c
200   {&dsp1_objectiveC, 3, 3},   //0x2d
201   {&dsp1_target, 2, 2},   //0x0e
202   {&dsp1_memory_size, 1, 1},   //0x2f
203
204   {&dsp1_inverse, 2, 2},   //0x10
205   {&dsp1_attitudeA, 4, 0},   //0x01
206   {&dsp1_parameter, 7, 4},   //0x02
207   {&dsp1_subjectiveA, 3, 3},   //0x03
208   {&dsp1_gyrate, 6, 3},   //0x14
209   {&dsp1_attitudeA, 4, 0},   //0x01
210   {&dsp1_project, 3, 3},   //0x06
211   {&dsp1_memory_dump, 1, 1024},   //0x1f
212   {&dsp1_range2, 4, 1},   //0x38
213   {&dsp1_objectiveA, 3, 3},   //0x0d
214   {0, 0, 0},   // 0x1a; the chip freezes
215   {&dsp1_scalarA, 3, 1},   //0x0b
216   {&dsp1_polar, 6, 3},   //0x1c
217   {&dsp1_objectiveA, 3, 3},   //0x0d
218   {&dsp1_target, 2, 2},   //0x0e
219   {&dsp1_memory_dump, 1, 1024},   //0x1f
220};
221
222//////////////////////////////////////////////////////////////////
223
224static const INT16 dsp1_MaxAZS_Exp[16] = {
225   0x38b4, 0x38b7, 0x38ba, 0x38be, 0x38c0, 0x38c4, 0x38c7, 0x38ca,
226   0x38ce, 0x38d0, 0x38d4, 0x38d7, 0x38da, 0x38dd, 0x38e0, 0x38e4
227};
228
229//////////////////////////////////////////////////////////////////
230
231static const INT16 dsp1_sin_table[256] = {
232   0x0000,  0x0324,  0x0647,  0x096a,  0x0c8b,  0x0fab,  0x12c8,  0x15e2,
233   0x18f8,  0x1c0b,  0x1f19,  0x2223,  0x2528,  0x2826,  0x2b1f,  0x2e11,
234   0x30fb,  0x33de,  0x36ba,  0x398c,  0x3c56,  0x3f17,  0x41ce,  0x447a,
235   0x471c,  0x49b4,  0x4c3f,  0x4ebf,  0x5133,  0x539b,  0x55f5,  0x5842,
236   0x5a82,  0x5cb4,  0x5ed7,  0x60ec,  0x62f2,  0x64e8,  0x66cf,  0x68a6,
237   0x6a6d,  0x6c24,  0x6dca,  0x6f5f,  0x70e2,  0x7255,  0x73b5,  0x7504,
238   0x7641,  0x776c,  0x7884,  0x798a,  0x7a7d,  0x7b5d,  0x7c29,  0x7ce3,
239   0x7d8a,  0x7e1d,  0x7e9d,  0x7f09,  0x7f62,  0x7fa7,  0x7fd8,  0x7ff6,
240   0x7fff,  0x7ff6,  0x7fd8,  0x7fa7,  0x7f62,  0x7f09,  0x7e9d,  0x7e1d,
241   0x7d8a,  0x7ce3,  0x7c29,  0x7b5d,  0x7a7d,  0x798a,  0x7884,  0x776c,
242   0x7641,  0x7504,  0x73b5,  0x7255,  0x70e2,  0x6f5f,  0x6dca,  0x6c24,
243   0x6a6d,  0x68a6,  0x66cf,  0x64e8,  0x62f2,  0x60ec,  0x5ed7,  0x5cb4,
244   0x5a82,  0x5842,  0x55f5,  0x539b,  0x5133,  0x4ebf,  0x4c3f,  0x49b4,
245   0x471c,  0x447a,  0x41ce,  0x3f17,  0x3c56,  0x398c,  0x36ba,  0x33de,
246   0x30fb,  0x2e11,  0x2b1f,  0x2826,  0x2528,  0x2223,  0x1f19,  0x1c0b,
247   0x18f8,  0x15e2,  0x12c8,  0x0fab,  0x0c8b,  0x096a,  0x0647,  0x0324,
248   -0x0000, -0x0324, -0x0647, -0x096a, -0x0c8b, -0x0fab, -0x12c8, -0x15e2,
249   -0x18f8, -0x1c0b, -0x1f19, -0x2223, -0x2528, -0x2826, -0x2b1f, -0x2e11,
250   -0x30fb, -0x33de, -0x36ba, -0x398c, -0x3c56, -0x3f17, -0x41ce, -0x447a,
251   -0x471c, -0x49b4, -0x4c3f, -0x4ebf, -0x5133, -0x539b, -0x55f5, -0x5842,
252   -0x5a82, -0x5cb4, -0x5ed7, -0x60ec, -0x62f2, -0x64e8, -0x66cf, -0x68a6,
253   -0x6a6d, -0x6c24, -0x6dca, -0x6f5f, -0x70e2, -0x7255, -0x73b5, -0x7504,
254   -0x7641, -0x776c, -0x7884, -0x798a, -0x7a7d, -0x7b5d, -0x7c29, -0x7ce3,
255   -0x7d8a, -0x7e1d, -0x7e9d, -0x7f09, -0x7f62, -0x7fa7, -0x7fd8, -0x7ff6,
256   -0x7fff, -0x7ff6, -0x7fd8, -0x7fa7, -0x7f62, -0x7f09, -0x7e9d, -0x7e1d,
257   -0x7d8a, -0x7ce3, -0x7c29, -0x7b5d, -0x7a7d, -0x798a, -0x7884, -0x776c,
258   -0x7641, -0x7504, -0x73b5, -0x7255, -0x70e2, -0x6f5f, -0x6dca, -0x6c24,
259   -0x6a6d, -0x68a6, -0x66cf, -0x64e8, -0x62f2, -0x60ec, -0x5ed7, -0x5cb4,
260   -0x5a82, -0x5842, -0x55f5, -0x539b, -0x5133, -0x4ebf, -0x4c3f, -0x49b4,
261   -0x471c, -0x447a, -0x41ce, -0x3f17, -0x3c56, -0x398c, -0x36ba, -0x33de,
262   -0x30fb, -0x2e11, -0x2b1f, -0x2826, -0x2528, -0x2223, -0x1f19, -0x1c0b,
263   -0x18f8, -0x15e2, -0x12c8, -0x0fab, -0x0c8b, -0x096a, -0x0647, -0x0324};
264
265   //////////////////////////////////////////////////////////////////
266
267// Optimised for Performance
268static const INT16 dsp1_mul_table[256] = {
269      0x0000,  0x0003,  0x0006,  0x0009,  0x000c,  0x000f,  0x0012,  0x0015,
270      0x0019,  0x001c,  0x001f,  0x0022,  0x0025,  0x0028,  0x002b,  0x002f,
271      0x0032,  0x0035,  0x0038,  0x003b,  0x003e,  0x0041,  0x0045,  0x0048,
272      0x004b,  0x004e,  0x0051,  0x0054,  0x0057,  0x005b,  0x005e,  0x0061,
273      0x0064,  0x0067,  0x006a,  0x006d,  0x0071,  0x0074,  0x0077,  0x007a,
274      0x007d,  0x0080,  0x0083,  0x0087,  0x008a,  0x008d,  0x0090,  0x0093,
275      0x0096,  0x0099,  0x009d,  0x00a0,  0x00a3,  0x00a6,  0x00a9,  0x00ac,
276      0x00af,  0x00b3,  0x00b6,  0x00b9,  0x00bc,  0x00bf,  0x00c2,  0x00c5,
277      0x00c9,  0x00cc,  0x00cf,  0x00d2,  0x00d5,  0x00d8,  0x00db,  0x00df,
278      0x00e2,  0x00e5,  0x00e8,  0x00eb,  0x00ee,  0x00f1,  0x00f5,  0x00f8,
279      0x00fb,  0x00fe,  0x0101,  0x0104,  0x0107,  0x010b,  0x010e,  0x0111,
280      0x0114,  0x0117,  0x011a,  0x011d,  0x0121,  0x0124,  0x0127,  0x012a,
281      0x012d,  0x0130,  0x0133,  0x0137,  0x013a,  0x013d,  0x0140,  0x0143,
282      0x0146,  0x0149,  0x014d,  0x0150,  0x0153,  0x0156,  0x0159,  0x015c,
283      0x015f,  0x0163,  0x0166,  0x0169,  0x016c,  0x016f,  0x0172,  0x0175,
284      0x0178,  0x017c,  0x017f,  0x0182,  0x0185,  0x0188,  0x018b,  0x018e,
285      0x0192,  0x0195,  0x0198,  0x019b,  0x019e,  0x01a1,  0x01a4,  0x01a8,
286      0x01ab,  0x01ae,  0x01b1,  0x01b4,  0x01b7,  0x01ba,  0x01be,  0x01c1,
287      0x01c4,  0x01c7,  0x01ca,  0x01cd,  0x01d0,  0x01d4,  0x01d7,  0x01da,
288      0x01dd,  0x01e0,  0x01e3,  0x01e6,  0x01ea,  0x01ed,  0x01f0,  0x01f3,
289      0x01f6,  0x01f9,  0x01fc,  0x0200,  0x0203,  0x0206,  0x0209,  0x020c,
290      0x020f,  0x0212,  0x0216,  0x0219,  0x021c,  0x021f,  0x0222,  0x0225,
291      0x0228,  0x022c,  0x022f,  0x0232,  0x0235,  0x0238,  0x023b,  0x023e,
292      0x0242,  0x0245,  0x0248,  0x024b,  0x024e,  0x0251,  0x0254,  0x0258,
293      0x025b,  0x025e,  0x0261,  0x0264,  0x0267,  0x026a,  0x026e,  0x0271,
294      0x0274,  0x0277,  0x027a,  0x027d,  0x0280,  0x0284,  0x0287,  0x028a,
295      0x028d,  0x0290,  0x0293,  0x0296,  0x029a,  0x029d,  0x02a0,  0x02a3,
296      0x02a6,  0x02a9,  0x02ac,  0x02b0,  0x02b3,  0x02b6,  0x02b9,  0x02bc,
297      0x02bf,  0x02c2,  0x02c6,  0x02c9,  0x02cc,  0x02cf,  0x02d2,  0x02d5,
298      0x02d8,  0x02db,  0x02df,  0x02e2,  0x02e5,  0x02e8,  0x02eb,  0x02ee,
299      0x02f1,  0x02f5,  0x02f8,  0x02fb,  0x02fe,  0x0301,  0x0304,  0x0307,
300      0x030b,  0x030e,  0x0311,  0x0314,  0x0317,  0x031a,  0x031d,  0x0321};
301
302//////////////////////////////////////////////////////////////////
303
304static UINT8 dsp1_get_sr( void )
305{
306   dsp1_state.SrLowByteAccess = ~dsp1_state.SrLowByteAccess;
307
308   if (dsp1_state.SrLowByteAccess)
309      return 0;
310   else
311      return dsp1_state.Sr;
312}
313
314//////////////////////////////////////////////////////////////////
315
316static UINT8 dsp1_get_dr( void )
317{
318   UINT8 oDr;
319
320   dsp1_fsm_step(1, &oDr);
321   return oDr;
322}
323
324//////////////////////////////////////////////////////////////////
325
326static void dsp1_set_dr( UINT8 iDr )
327{
328   dsp1_fsm_step(0, &iDr);
329}
330
331//////////////////////////////////////////////////////////////////
332
333static void dsp1_init( running_machine *machine )
334{
335   UINT32 i;
336   UINT8 *dspin = machine->region("addons")->base();
337
338   dsp1_state.Sr = DRC|RQM;
339   dsp1_state.SrLowByteAccess = FALSE;
340   dsp1_state.Dr = 0x0080;      // Only a supposition. Is this correct?
341   dsp1_state.Freeze = FALSE;
342   dsp1_state.FsmMajorState = WAIT_COMMAND;
343   memset(&dsp1_state.shared, 0, sizeof(struct SharedData)); // another supposition
344
345   // expand the DSP-1 data ROM
346   for (i = 0; i < 2048; i += 2)
347   {
348      dsp1_state.DataRom[i / 2] = (dspin[i] << 8) | dspin[i + 1];
349   }
350
351   dsp1_register_save(machine);
352}
353
354//////////////////////////////////////////////////////////////////
355
356// Though the DSP-1 is unaware of the type of operation (read or write)
357// we need to know what is being done by the program, as the class
358// is responsible for maintaining the binding between the
359// "external" and "internal" representations of the DR (data register).
360
361static void dsp1_fsm_step( UINT8 read, UINT8 *data )
362{
363   if (0 == (dsp1_state.Sr & RQM))
364      return;
365
366   // Now RQM would be cleared; however, as this code is not to be used in
367   // a multithread environment, we will simply fake RQM operation.
368   // (The only exception would be Op1A's freeze.)
369
370   // binding
371   if (read)
372   {
373      if (dsp1_state.Sr & DRS)
374         *data = (UINT8)(dsp1_state.Dr >> 8);
375      else
376         *data = (UINT8)(dsp1_state.Dr);
377   }
378   else
379   {
380      if (dsp1_state.Sr & DRS)
381      {
382         dsp1_state.Dr &= 0x00ff;
383         dsp1_state.Dr |= *data << 8;
384      }
385      else
386      {
387         dsp1_state.Dr &= 0xff00;
388         dsp1_state.Dr |= *data;
389      }
390   }
391
392
393   switch (dsp1_state.FsmMajorState)
394   {
395   case WAIT_COMMAND:
396      dsp1_state.Command = (UINT8)(dsp1_state.Dr);
397      if (!(dsp1_state.Command & 0xc0))   // valid command?
398      {
399         switch(dsp1_state.Command)
400         {
401         // freeze cases
402         case 0x1a:
403         case 0x2a:
404         case 0x3a:
405            dsp1_state.Freeze = TRUE;
406            break;
407
408         // normal cases
409         default:
410            dsp1_state.DataCounter = 0;
411            dsp1_state.FsmMajorState = READ_DATA;
412            dsp1_state.Sr &= ~DRC;
413            break;
414         }
415      }
416      break;
417
418   case READ_DATA:
419      dsp1_state.Sr ^= DRS;
420      if (!(dsp1_state.Sr & DRS))
421      {
422         dsp1_state.ReadBuffer[dsp1_state.DataCounter++] = (INT16)(dsp1_state.Dr);
423         if (dsp1_state.DataCounter >= mCommandTable[dsp1_state.Command].reads)
424         {
425            (*mCommandTable[dsp1_state.Command].callback)(dsp1_state.ReadBuffer, dsp1_state.WriteBuffer);
426            if (0 != mCommandTable[dsp1_state.Command].writes)  // any output?
427            {
428               dsp1_state.DataCounter = 0;
429               dsp1_state.Dr = (UINT16)(dsp1_state.WriteBuffer[dsp1_state.DataCounter]);
430               dsp1_state.FsmMajorState = WRITE_DATA;
431            }
432            else
433            {
434               dsp1_state.Dr = 0x0080;  // valid command completion
435               dsp1_state.FsmMajorState = WAIT_COMMAND;
436               dsp1_state.Sr |= DRC;
437            }
438         }
439      }
440      break;
441
442   case WRITE_DATA:
443      dsp1_state.Sr ^= DRS;
444      if (!(dsp1_state.Sr & DRS))
445      {
446         ++dsp1_state.DataCounter;
447         if (dsp1_state.DataCounter >= mCommandTable[dsp1_state.Command].writes)
448         {
449            if ((dsp1_state.Command == 0x0a) && (dsp1_state.Dr != 0x8000))
450            {
451               // works in continuous mode
452               dsp1_state.ReadBuffer[0]++;   // next raster line
453               (*mCommandTable[dsp1_state.Command].callback)(dsp1_state.ReadBuffer, dsp1_state.WriteBuffer);
454               dsp1_state.DataCounter = 0;
455               dsp1_state.Dr = (UINT16)(dsp1_state.WriteBuffer[dsp1_state.DataCounter]);
456            }
457            else
458            {
459               dsp1_state.Dr = 0x0080;  // valid command completion
460               dsp1_state.FsmMajorState = WAIT_COMMAND;
461               dsp1_state.Sr |= DRC;
462            }
463         }
464         else
465         {
466            dsp1_state.Dr = (UINT16)(dsp1_state.WriteBuffer[dsp1_state.DataCounter]);
467         }
468      }
469      break;
470   }
471
472
473
474   // Now RQM would be set (except when executing Op1A -command equals 0x1a, 0x2a or 0x3a-).
475   if (dsp1_state.Freeze)
476      dsp1_state.Sr &= ~RQM;
477}
478
479//////////////////////////////////////////////////////////////////
480
481static void dsp1_memory_test( INT16 *input, INT16 *output )
482{
483//  INT16 *Size = &input[0];
484   INT16 *Result = &output[0];
485
486   *Result = 0x0000;
487}
488
489//////////////////////////////////////////////////////////////////
490
491static void dsp1_memory_dump( INT16 *input, INT16 *output )
492{
493   memcpy(output, dsp1_state.DataRom, 1024);
494}
495
496//////////////////////////////////////////////////////////////////
497
498static void dsp1_memory_size( INT16 *input, INT16 *output )
499{
500   INT16* Size = &output[0];
501
502   *Size = 0x0100;
503}
504
505//////////////////////////////////////////////////////////////////
506
507// 16-bit multiplication
508
509static void dsp1_multiply( INT16 *input, INT16 *output )
510{
511   INT16 Multiplicand = input[0];
512   INT16 Multiplier = input[1];
513   INT16* Product = &output[0];
514
515   *Product = Multiplicand * Multiplier >> 15;
516}
517
518//////////////////////////////////////////////////////////////////
519
520// 16-bit multiplication. 'Alternative' method. Can anyone check this carefully?
521
522static void dsp1_multiply2( INT16 *input, INT16 *output )
523{
524   INT16 Multiplicand = input[0];
525   INT16 Multiplier = input[1];
526   INT16* Product = &output[0];
527
528   *Product = (Multiplicand * Multiplier >> 15) + 1;
529}
530
531//////////////////////////////////////////////////////////////////
532
533// This command determines the inverse of a floating point decimal number.
534
535static void dsp1_inverse( INT16 *input, INT16 *output )
536{
537   INT16 Coefficient = input[0];
538   INT16 Exponent = input[1];
539   INT16* iCoefficient = &output[0];
540   INT16* iExponent = &output[1];
541
542   inverse(Coefficient, Exponent, iCoefficient, iExponent);
543}
544
545//////////////////////////////////////////////////////////////////
546
547// Vector component calculation. Determines the X and Y components for a
548// two-dimensional vector whose size and direction is known.
549// Y = Radius * sin(Angle)
550// X = Radius * cos(Angle)
551
552static void dsp1_triangle( INT16 *input, INT16 *output )
553{
554   INT16 Angle = input[0];
555   INT16 Radius = input[1];
556   INT16* Y = &output[0];
557   INT16* X = &output[1];
558
559   *Y = dsp1_sin(Angle) * Radius >> 15;
560   *X = dsp1_cos(Angle) * Radius >> 15;
561}
562
563//////////////////////////////////////////////////////////////////
564
565// Determines the squared norm of a vector (X,Y,Z)
566// The output is Radius = X^2+Y^2+Z^2 (double integer)
567
568static void dsp1_radius( INT16 *input, INT16 *output )
569{
570   INT16 X = input[0];
571   INT16 Y = input[1];
572   INT16 Z = input[2];
573   INT16* RadiusLow = &output[0];
574   INT16* RadiusHigh = &output[1];
575
576   INT32 Radius;
577
578   Radius = (X * X + Y * Y + Z * Z) << 1;
579   *RadiusLow = (INT16)(Radius);
580   *RadiusHigh = (INT16)(Radius >> 16);
581}
582
583//////////////////////////////////////////////////////////////////
584
585// Vector size comparison. This command compares the size of the vector (X,Y,Z) and the distance (R)
586// from a particular point, and so may be used to determine if a point is within the sphere or radius R.
587// The output is D = X^2+Y^2+Z^2-R^2
588
589static void dsp1_range( INT16 *input, INT16 *output )
590{
591   INT16 X = input[0];
592   INT16 Y = input[1];
593   INT16 Z = input[2];
594   INT16 Radius = input[3];
595   INT16* Range = &output[0];
596
597   *Range = (X * X + Y * Y + Z * Z - Radius * Radius) >> 15;
598}
599
600//////////////////////////////////////////////////////////////////
601
602// Vector size comparison. 'Alternative' method.
603
604static void dsp1_range2( INT16 *input, INT16 *output )
605{
606   INT16 X = input[0];
607   INT16 Y = input[1];
608   INT16 Z = input[2];
609   INT16 Radius = input[3];
610   INT16* Range = &output[0];
611
612   *Range = ((X * X + Y * Y + Z * Z - Radius * Radius) >> 15) + 1;
613}
614
615//////////////////////////////////////////////////////////////////
616
617// This command calculates the norm of a (X,Y,Z) vector, or the distance from
618// the point (X,Y,Z) to (0,0,0), as you prefer to see it.
619// Distance = sqrt(X^2+Y^2+Z^2)
620// The square root of a number 'a' is calculated by doing this: you
621// write 'a' as b*2^2n, with 'b' between 1/4 and 1; then, you calculate
622// c=sqrt(b) by using lineal interpolation between points of a
623// look-up table and, finally, you output the result as c*2^n.
624
625static void dsp1_distance( INT16 *input, INT16 *output )
626{
627   INT16 X = input[0];
628   INT16 Y = input[1];
629   INT16 Z = input[2];
630   INT16* Distance = &output[0];
631   INT16 Pos, Node1, Node2;
632
633   INT32 Radius = X * X + Y * Y + Z * Z;
634
635
636   if (Radius == 0)
637      Distance = 0;
638   else
639   {
640      INT16 C, E;
641      normalize_double(Radius, &C, &E);
642      if (E & 1)
643      C = C * 0x4000 >> 15;
644
645      Pos = C * 0x0040 >> 15;
646
647      Node1 = dsp1_state.DataRom[0x00d5 + Pos];
648      Node2 = dsp1_state.DataRom[0x00d6 + Pos];
649
650      *Distance = ((Node2 - Node1) * (C & 0x1ff) >> 9) + Node1;
651
652#if dsp1_VERSION < 0x0102
653      if (Pos & 1)
654         *Distance -= (Node2 - Node1);
655#endif
656      *Distance >>= (E >> 1);
657   }
658}
659
660//////////////////////////////////////////////////////////////////
661
662// Determines the (X2, Y2) coordinates obtained by rotating (X1, Y1)
663// clockwise for an angle 'Angle'. The official documentation says
664// 'counterclockwise', but it's obviously wrong (surprise! :P)
665//
666// In matrix notation:
667// |X2|    |cos(Angle)    sin(Angle)| |X1|
668// |  | =  |                        | |  |
669// |Y2|    |-sin(Angle    cos(Angle)| |Y1|
670
671static void dsp1_rotate( INT16 *input, INT16 *output )
672{
673   INT16 Angle = input[0];
674   INT16 X1 = input[1];
675   INT16 Y1 = input[2];
676   INT16* X2 = &output[0];
677   INT16* Y2 = &output[1];
678
679   *X2 = (Y1 * dsp1_sin(Angle) >> 15) + (X1 * dsp1_cos(Angle) >> 15);
680   *Y2 = (Y1 * dsp1_cos(Angle) >> 15) - (X1 * dsp1_sin(Angle) >> 15);
681}
682
683//////////////////////////////////////////////////////////////////
684
685// Calculate the coordinates (X2, Y2, Z2) obtained when rotating (X1, Y1, Z1)
686// three-dimensionally. Rotation is done in the order of Az around the Z axis,
687// Ay around the Y axis and Ax around the X axis. As occur with the "attitude" commands
688// (see comments in the "gyrate" command), qthis doesn't match what explained in
689// the official documentation, but it's coherent with what it is done in the "attitude"
690// command (but not with the "gyrate" command).
691//
692// In matrix notation:
693// |X2|   |1      0       0  | |cosRy   0   -sinRy| | cosRz  sinRz   0| |X1|
694// |Y2| = |0     cosRx  sinRx| |  0     1      0  | |-sinRz  cosRz   0| |Y1|
695// |Z2|   |0    -sinRx  cosRx| |sinRy   0    cosRy| |   0       0    1| |Z1|
696
697static void dsp1_polar( INT16 *input, INT16 *output )
698{
699   INT16 Az = input[0];
700   INT16 Ay = input[1];
701   INT16 Ax = input[2];
702   INT16 X1 = input[3];
703   INT16 Y1 = input[4];
704   INT16 Z1 = input[5];
705   INT16* X2 = &output[0];
706   INT16* Y2 = &output[1];
707   INT16* Z2 = &output[2];
708
709   INT16 X, Y, Z;
710
711   // Rotate Around Z
712   X = (Y1 * dsp1_sin(Az) >> 15) + (X1 * dsp1_cos(Az) >> 15);
713   Y = (Y1 * dsp1_cos(Az) >> 15) - (X1 * dsp1_sin(Az) >> 15);
714   X1 = X; Y1 = Y;
715
716   // Rotate Around Y
717   Z = (X1 * dsp1_sin(Ay) >> 15) + (Z1 * dsp1_cos(Ay) >> 15);
718   X = (X1 * dsp1_cos(Ay) >> 15) - (Z1 * dsp1_sin(Ay) >> 15);
719   *X2 = X; Z1 = Z;
720
721   // Rotate Around X
722   Y = (Z1 * dsp1_sin(Ax) >> 15) + (Y1 * dsp1_cos(Ax) >> 15);
723   Z = (Z1 * dsp1_cos(Ax) >> 15) - (Y1 * dsp1_sin(Ax) >> 15);
724   *Y2 = Y; *Z2 = Z;
725}
726
727//////////////////////////////////////////////////////////////////
728
729// Set up the elements of an "attitude matrix" (there are other ones):
730//           S | cosRz  sinRz   0| |cosRy  0   -sinRy| |1     0      0  |
731// MatrixA = - |-sinRz  cosRz   0| |  0    1      0  | |0  cosRx   sinRx|
732//           2 |   0       0    1| |sinRy  0    cosRy| |0  -sinRx  cosRx|
733// This matrix is thought to be used within the following framework:
734// let's suppose we define positive rotations around a system of orthogonal axes in this manner:
735// a rotation of +90 degrees around axis3 converts axis2 into axis1
736// a rotation of +90 degrees around axis2 converts axis1 into axis3
737// a rotation of +90 degrees around axis1 converts axis3 into axis2
738// and let's suppose that we have defined a new orthonormal axes system (FLU)
739// by doing the following operations about the standard one (XYZ):
740// first rotating the XYZ system around Z by an angle Rz (obtaining X'Y'Z'),
741// then rotating the resulting system around Y by an angle Ry (obtaining X''Y''Z'')
742// and, finally, rotating the resulting system around X by an angle Rx (obtaining FLU)
743// This FLU (forward/left/up) system represents an "attitude" and, then, the matrix here defined
744// is the change of coordinates matrix that transform coordinates in the FLU
745// system (the "object coordinates") into the standard XYZ system (the "global coordinates"),
746// multiplied by a scale factor S/2, that is:
747// |x|   S             |f|
748// |y| * - = MatrixA * |l|
749// |z|   2             |u|
750// In a similar way, if we use the transpose of the matrix, we can transform global coordinates
751// into object coordinates:
752// |f|   S                        |x|
753// |l| * - = MatrixA_transposed * |y|
754// |u|   2                        |z|
755//
756// input[0]: S
757// input[1]: Rz
758// input[2]: Ry
759// input[3]: Rx
760
761static void dsp1_attitudeA( INT16 *input, INT16 *output )
762{
763   INT16 S = input[0];
764   INT16 Rz = input[1];
765   INT16 Ry = input[2];
766   INT16 Rx = input[3];
767
768   INT16 SinRz = dsp1_sin(Rz);
769   INT16 CosRz = dsp1_cos(Rz);
770   INT16 SinRy = dsp1_sin(Ry);
771   INT16 CosRy = dsp1_cos(Ry);
772   INT16 SinRx = dsp1_sin(Rx);
773   INT16 CosRx = dsp1_cos(Rx);
774
775   S >>= 1;
776
777   dsp1_state.shared.MatrixA[0][0] = (S * CosRz >> 15) * CosRy >> 15;
778   dsp1_state.shared.MatrixA[0][1] = ((S * SinRz >> 15) * CosRx >> 15) + (((S * CosRz >> 15) * SinRx >> 15) * SinRy >> 15);
779   dsp1_state.shared.MatrixA[0][2] = ((S * SinRz >> 15) * SinRx >> 15) - (((S * CosRz >> 15) * CosRx >> 15) * SinRy >> 15);
780
781   dsp1_state.shared.MatrixA[1][0] = -((S * SinRz >> 15) * CosRy >> 15);
782   dsp1_state.shared.MatrixA[1][1] = ((S * CosRz >> 15) * CosRx >> 15) - (((S * SinRz >> 15) * SinRx >> 15) * SinRy >> 15);
783   dsp1_state.shared.MatrixA[1][2] = ((S * CosRz >> 15) * SinRx >> 15) + (((S * SinRz >> 15) * CosRx >> 15) * SinRy >> 15);
784
785   dsp1_state.shared.MatrixA[2][0] = S * SinRy >> 15;
786   dsp1_state.shared.MatrixA[2][1] = -((S * SinRx >> 15) * CosRy >> 15);
787   dsp1_state.shared.MatrixA[2][2] = (S * CosRx >> 15) * CosRy >> 15;
788}
789
790//////////////////////////////////////////////////////////////////
791
792// Same than 'attitudeA', but with a difference attitude matrix (matrixB)
793
794static void dsp1_attitudeB( INT16 *input, INT16 *output )
795{
796   INT16 S = input[0];
797   INT16 Rz = input[1];
798   INT16 Ry = input[2];
799   INT16 Rx = input[3];
800
801   INT16 SinRz = dsp1_sin(Rz);
802   INT16 CosRz = dsp1_cos(Rz);
803   INT16 SinRy = dsp1_sin(Ry);
804   INT16 CosRy = dsp1_cos(Ry);
805   INT16 SinRx = dsp1_sin(Rx);
806   INT16 CosRx = dsp1_cos(Rx);
807
808   S >>= 1;
809
810   dsp1_state.shared.MatrixB[0][0] = (S * CosRz >> 15) * CosRy >> 15;
811   dsp1_state.shared.MatrixB[0][1] = ((S * SinRz >> 15) * CosRx >> 15) + (((S * CosRz >> 15) * SinRx >> 15) * SinRy >> 15);
812   dsp1_state.shared.MatrixB[0][2] = ((S * SinRz >> 15) * SinRx >> 15) - (((S * CosRz >> 15) * CosRx >> 15) * SinRy >> 15);
813
814   dsp1_state.shared.MatrixB[1][0] = -((S * SinRz >> 15) * CosRy >> 15);
815   dsp1_state.shared.MatrixB[1][1] = ((S * CosRz >> 15) * CosRx >> 15) - (((S * SinRz >> 15) * SinRx >> 15) * SinRy >> 15);
816   dsp1_state.shared.MatrixB[1][2] = ((S * CosRz >> 15) * SinRx >> 15) + (((S * SinRz >> 15) * CosRx >> 15) * SinRy >> 15);
817
818   dsp1_state.shared.MatrixB[2][0] = S * SinRy >> 15;
819   dsp1_state.shared.MatrixB[2][1] = -((S * SinRx >> 15) * CosRy >> 15);
820   dsp1_state.shared.MatrixB[2][2] = (S * CosRx >> 15) * CosRy >> 15;
821}
822
823//////////////////////////////////////////////////////////////////
824
825// Same than 'attitudeA', but with a difference attitude matrix (matrixC)
826
827static void dsp1_attitudeC( INT16 *input, INT16 *output )
828{
829   INT16 S = input[0];
830   INT16 Rz = input[1];
831   INT16 Ry = input[2];
832   INT16 Rx = input[3];
833
834   INT16 SinRz = dsp1_sin(Rz);
835   INT16 CosRz = dsp1_cos(Rz);
836   INT16 SinRy = dsp1_sin(Ry);
837   INT16 CosRy = dsp1_cos(Ry);
838   INT16 SinRx = dsp1_sin(Rx);
839   INT16 CosRx = dsp1_cos(Rx);
840
841   S >>= 1;
842
843   dsp1_state.shared.MatrixC[0][0] = (S * CosRz >> 15) * CosRy >> 15;
844   dsp1_state.shared.MatrixC[0][1] = ((S * SinRz >> 15) * CosRx >> 15) + (((S * CosRz >> 15) * SinRx >> 15) * SinRy >> 15);
845   dsp1_state.shared.MatrixC[0][2] = ((S * SinRz >> 15) * SinRx >> 15) - (((S * CosRz >> 15) * CosRx >> 15) * SinRy >> 15);
846
847   dsp1_state.shared.MatrixC[1][0] = -((S * SinRz >> 15) * CosRy >> 15);
848   dsp1_state.shared.MatrixC[1][1] = ((S * CosRz >> 15) * CosRx >> 15) - (((S * SinRz >> 15) * SinRx >> 15) * SinRy >> 15);
849   dsp1_state.shared.MatrixC[1][2] = ((S * CosRz >> 15) * SinRx >> 15) + (((S * SinRz >> 15) * CosRx >> 15) * SinRy >> 15);
850
851   dsp1_state.shared.MatrixC[2][0] = S * SinRy >> 15;
852   dsp1_state.shared.MatrixC[2][1] = -((S * SinRx >> 15) * CosRy >> 15);
853   dsp1_state.shared.MatrixC[2][2] = (S * CosRx >> 15) * CosRy >> 15;
854}
855
856//////////////////////////////////////////////////////////////////
857
858// Convert global coordinates (X,Y,Z) to object coordinates (F,L,U)
859// See the comment in "attitudeA" for a explanation about the calculation.
860//
861// input[0]: X ; input[1]: Y ; input[2]: Z
862// &output[0]: F ; &output[1]: L ; &output[2]: U
863
864static void dsp1_objectiveA( INT16 *input, INT16 *output )
865{
866   INT16 X = input[0];
867   INT16 Y = input[1];
868   INT16 Z = input[2];
869   INT16* F = &output[0];
870   INT16* L = &output[1];
871   INT16* U = &output[2];
872
873   *F = (dsp1_state.shared.MatrixA[0][0] * X >> 15) + (dsp1_state.shared.MatrixA[1][0] * Y >> 15) + (dsp1_state.shared.MatrixA[2][0] * Z >> 15);
874   *L = (dsp1_state.shared.MatrixA[0][1] * X >> 15) + (dsp1_state.shared.MatrixA[1][1] * Y >> 15) + (dsp1_state.shared.MatrixA[2][1] * Z >> 15);
875   *U = (dsp1_state.shared.MatrixA[0][2] * X >> 15) + (dsp1_state.shared.MatrixA[1][2] * Y >> 15) + (dsp1_state.shared.MatrixA[2][2] * Z >> 15);
876}
877
878//////////////////////////////////////////////////////////////////
879
880// Same than 'objectiveA', but for the 'B' attitude
881
882static void dsp1_objectiveB( INT16 *input, INT16 *output )
883{
884   INT16 X = input[0];
885   INT16 Y = input[1];
886   INT16 Z = input[2];
887   INT16* F = &output[0];
888   INT16* L = &output[1];
889   INT16* U = &output[2];
890
891   *F = (dsp1_state.shared.MatrixB[0][0] * X >> 15) + (dsp1_state.shared.MatrixB[1][0] * Y >> 15) + (dsp1_state.shared.MatrixB[2][0] * Z >> 15);
892   *L = (dsp1_state.shared.MatrixB[0][1] * X >> 15) + (dsp1_state.shared.MatrixB[1][1] * Y >> 15) + (dsp1_state.shared.MatrixB[2][1] * Z >> 15);
893   *U = (dsp1_state.shared.MatrixB[0][2] * X >> 15) + (dsp1_state.shared.MatrixB[1][2] * Y >> 15) + (dsp1_state.shared.MatrixB[2][2] * Z >> 15);
894}
895
896//////////////////////////////////////////////////////////////////
897
898// Same than 'objectiveA', but for the 'C' attitude
899
900static void dsp1_objectiveC( INT16 *input, INT16 *output )
901{
902   INT16 X = input[0];
903   INT16 Y = input[1];
904   INT16 Z = input[2];
905   INT16* F = &output[0];
906   INT16* L = &output[1];
907   INT16* U = &output[2];
908
909   *F = (dsp1_state.shared.MatrixC[0][0] * X >> 15) + (dsp1_state.shared.MatrixC[1][0] * Y >> 15) + (dsp1_state.shared.MatrixC[2][0] * Z >> 15);
910   *L = (dsp1_state.shared.MatrixC[0][1] * X >> 15) + (dsp1_state.shared.MatrixC[1][1] * Y >> 15) + (dsp1_state.shared.MatrixC[2][1] * Z >> 15);
911   *U = (dsp1_state.shared.MatrixC[0][2] * X >> 15) + (dsp1_state.shared.MatrixC[1][2] * Y >> 15) + (dsp1_state.shared.MatrixC[2][2] * Z >> 15);
912}
913
914//////////////////////////////////////////////////////////////////
915
916// Convert object coordinates (F,L,U) to object coordinates (X,Y,Z)
917// See the comment in "attitudeA" for a explanation about the calculation.
918//
919// input[0]: F ; input[1]: L ; input[2]: U
920// &output[0]: X ; &output[1]: Y ; &output[2]: Z
921
922static void dsp1_subjectiveA( INT16 *input, INT16 *output )
923{
924   INT16 F = input[0];
925   INT16 L = input[1];
926   INT16 U = input[2];
927   INT16* X = &output[0];
928   INT16* Y = &output[1];
929   INT16* Z = &output[2];
930
931   *X = (dsp1_state.shared.MatrixA[0][0] * F >> 15) + (dsp1_state.shared.MatrixA[0][1] * L >> 15) + (dsp1_state.shared.MatrixA[0][2] * U >> 15);
932   *Y = (dsp1_state.shared.MatrixA[1][0] * F >> 15) + (dsp1_state.shared.MatrixA[1][1] * L >> 15) + (dsp1_state.shared.MatrixA[1][2] * U >> 15);
933   *Z = (dsp1_state.shared.MatrixA[2][0] * F >> 15) + (dsp1_state.shared.MatrixA[2][1] * L >> 15) + (dsp1_state.shared.MatrixA[2][2] * U >> 15);
934}
935
936//////////////////////////////////////////////////////////////////
937
938// Same than 'subjectiveA', but for the 'B' attitude
939
940static void dsp1_subjectiveB( INT16 *input, INT16 *output )
941{
942   INT16 F = input[0];
943   INT16 L = input[1];
944   INT16 U = input[2];
945   INT16* X = &output[0];
946   INT16* Y = &output[1];
947   INT16* Z = &output[2];
948
949   *X = (dsp1_state.shared.MatrixB[0][0] * F >> 15) + (dsp1_state.shared.MatrixB[0][1] * L >> 15) + (dsp1_state.shared.MatrixB[0][2] * U >> 15);
950   *Y = (dsp1_state.shared.MatrixB[1][0] * F >> 15) + (dsp1_state.shared.MatrixB[1][1] * L >> 15) + (dsp1_state.shared.MatrixB[1][2] * U >> 15);
951   *Z = (dsp1_state.shared.MatrixB[2][0] * F >> 15) + (dsp1_state.shared.MatrixB[2][1] * L >> 15) + (dsp1_state.shared.MatrixB[2][2] * U >> 15);
952}
953
954//////////////////////////////////////////////////////////////////
955
956// Same than 'subjectiveA', but for the 'C' attitude
957
958static void dsp1_subjectiveC(INT16 *input, INT16 *output)
959{
960   INT16 F = input[0];
961   INT16 L = input[1];
962   INT16 U = input[2];
963   INT16* X = &output[0];
964   INT16* Y = &output[1];
965   INT16* Z = &output[2];
966
967   *X = (dsp1_state.shared.MatrixC[0][0] * F >> 15) + (dsp1_state.shared.MatrixC[0][1] * L >> 15) + (dsp1_state.shared.MatrixC[0][2] * U >> 15);
968   *Y = (dsp1_state.shared.MatrixC[1][0] * F >> 15) + (dsp1_state.shared.MatrixC[1][1] * L >> 15) + (dsp1_state.shared.MatrixC[1][2] * U >> 15);
969   *Z = (dsp1_state.shared.MatrixC[2][0] * F >> 15) + (dsp1_state.shared.MatrixC[2][1] * L >> 15) + (dsp1_state.shared.MatrixC[2][2] * U >> 15);
970}
971
972//////////////////////////////////////////////////////////////////
973
974// This command calculates the inner product (S) of a vector (X,Y,Z) and
975// the first column of MatrixA. It should be noted that that first column
976// represent the global coordinates of an unity vector in the forward
977// direction in the object coordinate system (coordinates (1,0,0) in the FLU
978// axes system).
979//
980// input[0]: X ; input[1]: Y ; input[2]: Z
981// &output[0]: S
982
983static void dsp1_scalarA( INT16 *input, INT16 *output )
984{
985   INT16 X = input[0];
986   INT16 Y = input[1];
987   INT16 Z = input[2];
988   INT16* S = &output[0];
989
990   *S = (X * dsp1_state.shared.MatrixA[0][0] + Y * dsp1_state.shared.MatrixA[1][0] + Z * dsp1_state.shared.MatrixA[2][0]) >> 15;
991}
992
993//////////////////////////////////////////////////////////////////
994
995// Same than 'scalarA', but for the 'B' attitude
996
997static void dsp1_scalarB( INT16 *input, INT16 *output )
998{
999   INT16 X = input[0];
1000   INT16 Y = input[1];
1001   INT16 Z = input[2];
1002   INT16* S = &output[0];
1003
1004   *S = (X * dsp1_state.shared.MatrixB[0][0] + Y * dsp1_state.shared.MatrixB[1][0] + Z * dsp1_state.shared.MatrixB[2][0]) >> 15;
1005}
1006
1007//////////////////////////////////////////////////////////////////
1008
1009// Same than 'scalarA', but for the 'C' attitude
1010
1011static void dsp1_scalarC( INT16 *input, INT16 *output )
1012{
1013   INT16 X = input[0];
1014   INT16 Y = input[1];
1015   INT16 Z = input[2];
1016   INT16* S = &output[0];
1017
1018   *S = (X * dsp1_state.shared.MatrixC[0][0] + Y * dsp1_state.shared.MatrixC[1][0] + Z * dsp1_state.shared.MatrixC[2][0]) >> 15;
1019}
1020
1021//////////////////////////////////////////////////////////////////
1022
1023// This command determines the final attitude angles after the body with attitude angles (Ax, Ay, Az) with
1024// respect to the global coordinates is rotated by the minor angular displacements (DeltaF, DeltaL, DeltaU).
1025// It means that the XYZ axes are rotated by (Ax, Ay, Az) to obtain the FLU axes and, then, these
1026// are rotated by (DeltaF, DeltaL, DeltaU). The command calculates and return the new FLU angles respect to the
1027// XYZ system (Rx, Ry, Rz)
1028// The formulae are:
1029// Rx = Ax + (DeltaU*sin(Ay)+DeltaF*cos(Ay))
1030// Ry = Ay + DeltaL - tan(Ax)*(DeltaU*cos(Ay)+DeltaF*sin(Ay))
1031// Rz = Az + sec(Ax)*(DeltaU*cos(Ay)-DeltaF*sin(Ay))
1032//
1033// Now the discussion: according to the official documentation, as described in various commands, you pass from
1034// XYZ to FLU by doing the rotations in the order Y, X, Z. In this command, the formulae are coherent with the
1035// fact that Y is the first axis to do a rotation around it. However, in the "attitude" command, while the official
1036// document describe it that way, we have discovered, when reverse engineering the command, that the calculated
1037// matrix do the rotation around Y in the second place. This incoherent behaviour of various commands is, in my
1038// opinion, a pretty severe implementation error. However, if you only use small "minor displacements", the error term
1039// introduced by that incoherence should be almost negligible.
1040
1041static void dsp1_gyrate( INT16 *input, INT16 *output )
1042{
1043   INT16 Az = input[0];
1044   INT16 Ax = input[1];
1045   INT16 Ay = input[2];
1046   INT16 U = input[3];
1047   INT16 F = input[4];
1048   INT16 L = input[5];
1049   INT16* Rz = &output[0];
1050   INT16* Rx = &output[1];
1051   INT16* Ry = &output[2];
1052
1053   INT16 CSec, ESec, CSin, C, E;
1054   INT16 SinAy = dsp1_sin(Ay);
1055   INT16 CosAy = dsp1_cos(Ay);
1056
1057   inverse(dsp1_cos(Ax), 0, &CSec, &ESec);
1058
1059   // Rotation Around Z
1060   normalize_double(U * CosAy - F * SinAy, &C, &E);
1061
1062   E = ESec - E;
1063
1064   normalize(C * CSec >> 15, &C, &E);
1065
1066   *Rz = Az + denormalize_and_clip(C, E);
1067
1068   // Rotation Around X
1069   *Rx = Ax + (U * SinAy >> 15) + (F * CosAy >> 15);
1070
1071   // Rotation Around Y
1072   normalize_double(U * CosAy + F * SinAy, &C, &E);
1073
1074   E = ESec - E;
1075
1076   normalize(dsp1_sin(Ax), &CSin, &E);
1077
1078   normalize(-(C * (CSec * CSin >> 15) >> 15), &C, &E);
1079
1080   *Ry = Ay + denormalize_and_clip(C, E) + L;
1081}
1082
1083//////////////////////////////////////////////////////////////////
1084
1085
1086// Set-up the projection framework. Besides returning some values, it store in RAM some values that
1087// will be used by the other three projection commands (raster, target an project)
1088// Input:
1089// (Fx, Fy, Fz)-> coordinates of base point (global coordinates)
1090// Lfe-> distance between the base point and the viewpoint (center of projection)
1091// Les-> distance between the base point and the screen
1092// Aas-> azimuth angle (0 degrees is east; 90 degrees is north)
1093// Azs-> zenith angle (0 degrees is zenith)
1094// Output:
1095// Vof-> raster line of imaginary center (whatever it means ;) )
1096// Vva-> raster line representing the horizon line
1097// (Cx, Cy)-> coordinates of the projection of the center of the screen over the ground (ground coordinates)
1098
1099static void dsp1_parameter( INT16 *input, INT16 *output )
1100{
1101   INT16 Fx = input[0];
1102   INT16 Fy = input[1];
1103   INT16 Fz = input[2];
1104   INT16 Lfe = input[3];
1105   INT16 Les = input[4];
1106   INT16 Aas = input[5];
1107   INT16 Azs = input[6];
1108   INT16* Vof = &output[0];
1109   INT16* Vva = &output[1];
1110   INT16* Cx = &output[2];
1111   INT16* Cy = &output[3];
1112
1113   INT16 CSec, C, E;
1114   INT16 LfeNx, LfeNy, LfeNz;
1115   INT16 LesNx, LesNy, LesNz;
1116   INT16 AZS, MaxAZS;
1117
1118   // Copy Zenith angle for clipping
1119   AZS = Azs;
1120
1121   // Store Les and his coefficient and exponent when normalized
1122   dsp1_state.shared.Les = Les;
1123   dsp1_state.shared.E_Les = 0;
1124   normalize(Les, &dsp1_state.shared.C_Les, &dsp1_state.shared.E_Les);
1125
1126   // Store Sine and Cosine of Azimuth and Zenith angle
1127   dsp1_state.shared.SinAas = dsp1_sin(Aas);
1128   dsp1_state.shared.CosAas = dsp1_cos(Aas);
1129   dsp1_state.shared.SinAzs = dsp1_sin(Azs);
1130   dsp1_state.shared.CosAzs = dsp1_cos(Azs);
1131
1132   // normal vector to the screen (norm 1, points toward the center of projection)
1133   dsp1_state.shared.Nx = dsp1_state.shared.SinAzs * -dsp1_state.shared.SinAas >> 15;
1134   dsp1_state.shared.Ny = dsp1_state.shared.SinAzs * dsp1_state.shared.CosAas >> 15;
1135   dsp1_state.shared.Nz = dsp1_state.shared.CosAzs * 0x7fff >> 15;
1136
1137   // horizontal vector of the screen (Hz=0, norm 1, points toward the right of the screen)
1138   dsp1_state.shared.Hx = dsp1_state.shared.CosAas*0x7fff>>15;
1139   dsp1_state.shared.Hy = dsp1_state.shared.SinAas*0x7fff>>15;
1140
1141   // vertical vector of the screen (norm 1, points toward the top of the screen)
1142   dsp1_state.shared.Vx = dsp1_state.shared.CosAzs * -dsp1_state.shared.SinAas >> 15;
1143   dsp1_state.shared.Vy = dsp1_state.shared.CosAzs * dsp1_state.shared.CosAas >> 15;
1144   dsp1_state.shared.Vz = -dsp1_state.shared.SinAzs * 0x7fff>>15;
1145
1146   LfeNx = Lfe * dsp1_state.shared.Nx >> 15;
1147   LfeNy = Lfe * dsp1_state.shared.Ny >> 15;
1148   LfeNz = Lfe * dsp1_state.shared.Nz >> 15;
1149
1150   // Center of Projection
1151   dsp1_state.shared.CentreX = Fx + LfeNx;
1152   dsp1_state.shared.CentreY = Fy + LfeNy;
1153   dsp1_state.shared.CentreZ = Fz + LfeNz;
1154
1155   LesNx = Les * dsp1_state.shared.Nx >> 15;
1156   LesNy = Les * dsp1_state.shared.Ny >> 15;
1157   LesNz = Les * dsp1_state.shared.Nz >> 15;
1158
1159   // center of the screen (global coordinates)
1160   dsp1_state.shared.Gx = dsp1_state.shared.CentreX - LesNx;
1161   dsp1_state.shared.Gy = dsp1_state.shared.CentreY - LesNy;
1162   dsp1_state.shared.Gz = dsp1_state.shared.CentreZ - LesNz;
1163
1164   E = 0;
1165   normalize(dsp1_state.shared.CentreZ, &C, &E);
1166
1167   dsp1_state.shared.CentreZ_C = C;
1168   dsp1_state.shared.CentreZ_E = E;
1169
1170   // Determine clip boundary and clip Zenith angle if necessary
1171   // (Why to clip? Maybe to avoid the screen can only show sky with no ground? Only a guess...)
1172   MaxAZS = dsp1_MaxAZS_Exp[-E];
1173
1174   if (AZS < 0)
1175   {
1176      MaxAZS = -MaxAZS;
1177      if (AZS < MaxAZS + 1)
1178         AZS = MaxAZS + 1;
1179   }
1180   else
1181   {
1182      if (AZS > MaxAZS)
1183         AZS = MaxAZS;
1184   }
1185
1186   // Store Sine and Cosine of clipped Zenith angle
1187   dsp1_state.shared.SinAZS = dsp1_sin(AZS);
1188   dsp1_state.shared.CosAZS = dsp1_cos(AZS);
1189
1190   // calculate the separation of (cx, cy) from the projection of
1191   // the 'centre of projection' over the ground... (CentreZ*tg(AZS))
1192   inverse(dsp1_state.shared.CosAZS, 0, &dsp1_state.shared.SecAZS_C1, &dsp1_state.shared.SecAZS_E1);
1193   normalize(C * dsp1_state.shared.SecAZS_C1 >> 15, &C, &E);
1194   E += dsp1_state.shared.SecAZS_E1;
1195   C = denormalize_and_clip(C, E) * dsp1_state.shared.SinAZS >> 15;
1196
1197   // ... and then take into account the position of the centre of
1198   // projection and the azimuth angle
1199   dsp1_state.shared.CentreX += C * dsp1_state.shared.SinAas >> 15;
1200   dsp1_state.shared.CentreY -= C * dsp1_state.shared.CosAas >> 15;
1201
1202   *Cx = dsp1_state.shared.CentreX;
1203   *Cy = dsp1_state.shared.CentreY;
1204
1205   // Raster number of imaginary center and horizontal line
1206   *Vof = 0;
1207
1208   if ((Azs != AZS) || (Azs == MaxAZS))
1209   {
1210      INT16 Aux;
1211
1212      // correct vof and vva when Azs is outside the 'non-clipping interval'
1213      // we have only some few Taylor coefficients, so we cannot guess which ones
1214      // are the approximated functions and, what is worse, we don't know why
1215      // the own clipping stuff (and, particularly, this correction) is done
1216      if (Azs == -32768)
1217      Azs = -32767;
1218
1219      C = Azs - MaxAZS;
1220      if (C >= 0)
1221         C--;
1222      Aux = ~(C << 2);
1223
1224      // Vof += x+(1/3)*x^3, where x ranges from 0 to PI/4 when Azs-MaxAZS goes from 0 to 0x2000
1225      C = Aux * dsp1_state.DataRom[0x0328] >> 15;
1226      C = (C * Aux >> 15) + dsp1_state.DataRom[0x0327];
1227      *Vof -= (C * Aux >> 15) * Les >> 15;
1228
1229      // CosAZS *= 1+(1/2)*x^2+(5/24)*x^24, where x ranges from 0 to PI/4 when Azs-MaxAZS goes from 0 to 0x2000
1230      C = Aux * Aux >> 15;
1231      Aux = (C * dsp1_state.DataRom[0x0324] >> 15) + dsp1_state.DataRom[0x0325];
1232      dsp1_state.shared.CosAZS += (C * Aux >> 15) * dsp1_state.shared.CosAZS >> 15;
1233   }
1234
1235   // vertical offset of the screen with regard to the horizontal plane
1236   // containing the centre of projection
1237   dsp1_state.shared.VOffset = Les * dsp1_state.shared.CosAZS >> 15;
1238
1239   // The horizon line (the line in the screen that is crossed by the horizon plane
1240   // -the horizontal plane containing the 'centre of projection'-),
1241   // will be at distance Les*cotg(AZS) from the centre of the screen. This is difficult
1242   // to explain but easily seen in a graph. To better see it, consider it in this way:
1243   // Les*tg(AZS-90), draw some lines and apply basic trigonometry. ;)
1244   inverse(dsp1_state.shared.SinAZS, 0, &CSec, &E);
1245   normalize(dsp1_state.shared.VOffset, &C, &E);
1246   normalize(C * CSec >> 15, &C, &E);
1247
1248   if (C == -32768)
1249   {
1250      C >>= 1;
1251      E++;
1252   }
1253
1254   *Vva = denormalize_and_clip(-C, E);
1255
1256   // Store Secant of clipped Zenith angle
1257   inverse(dsp1_state.shared.CosAZS, 0, &dsp1_state.shared.SecAZS_C2, &dsp1_state.shared.SecAZS_E2);
1258}
1259
1260//////////////////////////////////////////////////////////////////
1261
1262// Calculates the matrix which transform an object situated on a raster line (Vs) into
1263// his projection over the ground. The modified SecAZS is used here, so
1264// i don't understand the fine details, but, basically, it's done
1265// this way: The vertical offset between the point of projection and the
1266// raster line is calculated (Vs*SinAzs>>15)+VOffset, then the height of
1267// the center of projection is measured in that units (*CentreZ_C). If, now
1268// you consider the "reference case" (center of projection at an unit of height),
1269// the projection of a thin strip containing the raster line will have the same
1270// width (as the raster line would be on the ground in this case, but will suffer a
1271// change of scale in height (as the ground and the vertical axis would form an angle of 180-Azs degrees).
1272// This scale factor, when the angle 'center of screen-center of projection-raster line' is small,
1273// can be aproximated by the one of the center of the screen, 1/cos(Azs).(**) (Here is when it's used
1274// SecAZS). By last, you have to consider the effect of the azimuth angle Aas, and you are done.
1275//
1276// Using matrix notation:
1277//                    |A      B|     Centre_ZS    | cos(Aas)    -sin(Aas)|   | 1         0   |
1278// ProjectionMatrix = |        | = ----------- *  |                      | * |               |
1279//                    |C      D|    Vs*sin(Azs)   | sin(Aas)     cos(Aas)|   | 0     sec(Azs)|
1280//
1281// (**)
1282// If Les=1, the vertical offset between the center
1283// of projection and the center of the screen is Cos(Azs); then, if the vertical
1284// offset is 1, the ratio of the projection over the ground respect to the
1285// line on the screen is 1/cos(Azs).
1286
1287static void dsp1_raster( INT16 *input, INT16 *output )
1288{
1289   INT16 Vs = input[0];
1290   INT16* An = &output[0];
1291   INT16* Bn = &output[1];
1292   INT16* Cn = &output[2];
1293   INT16* Dn = &output[3];
1294
1295   INT16 C, E, C1, E1;
1296
1297   inverse((Vs * dsp1_state.shared.SinAzs >> 15) + dsp1_state.shared.VOffset, 7, &C, &E);
1298
1299   E += dsp1_state.shared.CentreZ_E;
1300   C1 = C * dsp1_state.shared.CentreZ_C >> 15;
1301
1302   E1 = E + dsp1_state.shared.SecAZS_E2;
1303
1304   normalize(C1, &C, &E);
1305   C = denormalize_and_clip(C, E);
1306
1307   *An = C * dsp1_state.shared.CosAas >> 15;
1308   *Cn = C * dsp1_state.shared.SinAas >> 15;
1309
1310   normalize(C1 * dsp1_state.shared.SecAZS_C2 >> 15, &C, &E1);
1311   C = denormalize_and_clip(C, E1);
1312
1313   *Bn = C * -dsp1_state.shared.SinAas >> 15;
1314   *Dn = C * dsp1_state.shared.CosAas >> 15;
1315}
1316
1317//////////////////////////////////////////////////////////////////
1318
1319// Calculate the projection over the ground of a selected point of screen
1320// It simply apply the projection matrix described in the "Raster" command
1321// to the vector (H,V) transposed, and add the result to the position of
1322// the centre of projection.
1323// The only special point to take into account is the directions on the screen:
1324// H is positive rightward, but V is positive downward; this is why
1325// the signs take that configuration
1326
1327static void dsp1_target( INT16 *input, INT16 *output )
1328{
1329   INT16 H = input[0];
1330   INT16 V = input[1];
1331   INT16* X = &output[0];
1332   INT16* Y = &output[1];
1333
1334   INT16 C, E, C1, E1;
1335
1336   inverse((V * dsp1_state.shared.SinAzs >> 15) + dsp1_state.shared.VOffset, 8, &C, &E);
1337
1338   E += dsp1_state.shared.CentreZ_E;
1339   C1 = C * dsp1_state.shared.CentreZ_C >> 15;
1340
1341   E1 = E + dsp1_state.shared.SecAZS_E1;
1342
1343   H <<= 8;
1344   normalize(C1, &C, &E);
1345   C = denormalize_and_clip(C, E) * H >> 15;
1346
1347   *X = dsp1_state.shared.CentreX + (C * dsp1_state.shared.CosAas >> 15);
1348   *Y = dsp1_state.shared.CentreY - (C * dsp1_state.shared.SinAas >> 15);
1349
1350   V <<= 8;
1351   normalize(C1 * dsp1_state.shared.SecAZS_C1 >> 15, &C, &E1);
1352   C = denormalize_and_clip(C, E1) * V >> 15;
1353
1354   *X += C * -dsp1_state.shared.SinAas >> 15;
1355   *Y += C * dsp1_state.shared.CosAas >> 15;
1356}
1357
1358//////////////////////////////////////////////////////////////////
1359
1360// Calculation of the projection over the screen (H,V) of an object (X,Y,Z) and his
1361// 'enlargement ratio' (M). The positive directions on the screen are as described
1362// in the targe command. M is scaled down by 2^-7, that is, M==0x0100 means ratio 1:1
1363
1364static void dsp1_project( INT16 *input, INT16 *output )
1365{
1366   INT16 X = input[0];
1367   INT16 Y = input[1];
1368   INT16 Z = input[2];
1369   INT16* H = &output[0];
1370   INT16* V = &output[1];
1371   INT16* M = &output[2];
1372
1373   INT32 aux, aux4;
1374   INT16 E, E2, E3, E4, E5, refE, E6, E7;
1375   INT16 C2, C4, C6, C8, C9, C10, C11, C12, C16, C17, C18, C19, C20, C21, C22, C23, C24, C25, C26;
1376   INT16 Px, Py, Pz;
1377
1378   E4 = E3 = E2 = E = E5 = 0;
1379
1380   normalize_double((INT32)(X) - dsp1_state.shared.Gx, &Px, &E4);
1381   normalize_double((INT32)(Y) - dsp1_state.shared.Gy, &Py, &E);
1382   normalize_double((INT32)(Z) - dsp1_state.shared.Gz, &Pz, &E3);
1383   Px >>= 1; E4--;   // to avoid overflows when calculating the scalar products
1384   Py >>= 1; E--;
1385   Pz >>= 1; E3--;
1386
1387   refE = (E < E3) ? E : E3;
1388   refE = (refE < E4) ? refE : E4;
1389
1390   Px = shiftR(Px, E4 - refE);    // normalize them to the same exponent
1391   Py = shiftR(Py, E  - refE);
1392   Pz = shiftR(Pz, E3 - refE);
1393
1394   C11 = -(Px * dsp1_state.shared.Nx >> 15);
1395   C8  = -(Py * dsp1_state.shared.Ny >> 15);
1396   C9  = -(Pz * dsp1_state.shared.Nz >> 15);
1397   C12 = C11 + C8 + C9;   // this cannot overflow!
1398
1399   aux4 = C12;   // de-normalization with 32-bits arithmetic
1400   refE = 16 - refE;    // refE can be up to 3
1401   if (refE >= 0)
1402      aux4 <<=  (refE);
1403   else
1404      aux4 >>= -(refE);
1405   if (aux4 == -1)
1406      aux4 = 0;      // why?
1407   aux4 >>= 1;
1408
1409   aux = (UINT16)(dsp1_state.shared.Les) + aux4;   // Les - the scalar product of P with the normal vector of the screen
1410   normalize_double(aux, &C10, &E2);
1411   E2 = 15 - E2;
1412
1413   inverse(C10, 0, &C4, &E4);
1414   C2 = C4 * dsp1_state.shared.C_Les >> 15;                 // scale factor
1415
1416
1417   // H
1418   E7 = 0;
1419   C16 = (Px * dsp1_state.shared.Hx >> 15);
1420   C20 = (Py * dsp1_state.shared.Hy >> 15);
1421   C17 = C16 + C20;   // scalar product of P with the normalized horizontal vector of the screen...
1422
1423   C18 = C17 * C2 >> 15;    // ... multiplied by the scale factor
1424   normalize(C18, &C19, &E7);
1425   *H = denormalize_and_clip(C19, dsp1_state.shared.E_Les - E2 + refE + E7);
1426
1427   // V
1428   E6 = 0;
1429   C21 = Px * dsp1_state.shared.Vx >> 15;
1430   C22 = Py * dsp1_state.shared.Vy >> 15;
1431   C23 = Pz * dsp1_state.shared.Vz >> 15;
1432   C24 = C21 + C22 + C23;   // scalar product of P with the normalized vertical vector of the screen...
1433
1434   C26 = C24 * C2 >> 15;    // ... multiplied by the scale factor
1435   normalize(C26, &C25, &E6);
1436   *V = denormalize_and_clip(C25, dsp1_state.shared.E_Les - E2 + refE + E6);
1437
1438   // M
1439   normalize(C2, &C6, &E4);
1440   *M = denormalize_and_clip(C6, E4 + dsp1_state.shared.E_Les - E2 - 7); // M is the scale factor divided by 2^7
1441}
1442
1443//////////////////////////////////////////////////////////////////
1444
1445// Calculate the sine of the input parameter
1446// this is done by linear interpolation between
1447// the points of a look-up table
1448
1449static INT16 dsp1_sin( INT16 Angle )
1450{
1451   INT32 S;
1452
1453   if (Angle < 0)
1454   {
1455      if (Angle == -32768)
1456         return 0;
1457
1458      return -dsp1_sin(-Angle);
1459   }
1460   S = dsp1_sin_table[Angle >> 8] + (dsp1_mul_table[Angle & 0xff] * dsp1_sin_table[0x40 + (Angle >> 8)] >> 15);
1461   if (S > 32767)
1462      S = 32767;
1463   return (INT16) S;
1464}
1465
1466//////////////////////////////////////////////////////////////////
1467
1468// Calculate the cosine of the input parameter.
1469// It's used the same method than in sin(INT16)
1470
1471static INT16 dsp1_cos( INT16 Angle )
1472{
1473   INT32 S;
1474
1475   if (Angle < 0)
1476   {
1477      if (Angle == -32768)
1478         return -32768;
1479      Angle = -Angle;
1480   }
1481   S = dsp1_sin_table[0x40 + (Angle >> 8)] - (dsp1_mul_table[Angle & 0xff] * dsp1_sin_table[Angle >> 8] >> 15);
1482   if (S < -32768)
1483      S = -32767;
1484   return (INT16) S;
1485}
1486
1487//////////////////////////////////////////////////////////////////
1488
1489// Determines the inverse of a floating point decimal number
1490// iCoefficient*2^iExponent = 1/(Coefficient*2^Exponent), with the output
1491// normalized (iCoefficient represents a number whose absolute value is between 1/2 and 1)
1492// To invert 'Coefficient' a first initial guess is taken from a look-up table
1493// and, then, two iterations of the Newton method (applied to the function
1494// f(x)=1/(2*x)-Coefficient) are done. This results in a close approximation (iCoefficient) to a number 'y'
1495// that verify Coefficient*y=1/2. This is why you have to correct the exponent by one
1496// unit at the end.
1497
1498static void inverse( INT16 Coefficient, INT16 Exponent, INT16 *iCoefficient, INT16 *iExponent )
1499{
1500   // Step One: Division by Zero
1501   if (Coefficient == 0x0000)
1502   {
1503      *iCoefficient = 0x7fff;
1504      *iExponent = 0x002f;
1505   }
1506   else
1507   {
1508      INT16 Sign = 1;
1509
1510      // Step Two: Remove Sign
1511      if (Coefficient < 0)
1512      {
1513         if (Coefficient < -32767)
1514            Coefficient = -32767;
1515         Coefficient = -Coefficient;
1516         Sign = -1;
1517      }
1518
1519      // Step Three: Normalize
1520      while (Coefficient < 0x4000)
1521      {
1522         Coefficient <<= 1;
1523         Exponent--;
1524      }
1525
1526      // Step Four: Special Case
1527      if (Coefficient == 0x4000)
1528         if (Sign == 1) *iCoefficient = 0x7fff;
1529      else  {
1530         *iCoefficient = -0x4000;
1531         Exponent--;
1532      }
1533      else {
1534         // Step Five: Initial Guess
1535         INT16 i = dsp1_state.DataRom[((Coefficient - 0x4000) >> 7) + 0x0065];
1536
1537         // Step Six: Iterate Newton's Method
1538         i = (i + (-i * (Coefficient * i >> 15) >> 15)) << 1;
1539         i = (i + (-i * (Coefficient * i >> 15) >> 15)) << 1;
1540
1541         *iCoefficient = i * Sign;
1542      }
1543
1544      *iExponent = 1 - Exponent;
1545   }
1546}
1547
1548//////////////////////////////////////////////////////////////////
1549
1550static INT16 denormalize_and_clip( INT16 C, INT16 E )
1551{
1552   if (E > 0)
1553   {
1554      if (C > 0)
1555         return 32767;
1556      else if (C < 0)
1557         return -32767;
1558   }
1559   else
1560   {
1561      if (E < 0)
1562         return C * dsp1_state.DataRom[0x0031 + E] >> 15;
1563   }
1564   return C;
1565}
1566
1567//////////////////////////////////////////////////////////////////
1568
1569// Normalize the input number (m), understood as ranging from -1 to 1,
1570// to the form: Coefficient*2^Exponent,
1571// where the absolute value of Coefficient is >= 1/2
1572// (Coefficient>=0x4000 or Coefficient <= (INT16)0xc001)
1573
1574static void normalize( INT16 m, INT16 *Coefficient, INT16 *Exponent )
1575{
1576   INT16 i = 0x4000;
1577   INT16 e = 0;
1578
1579   if (m < 0)
1580      while ((m & i) && i)
1581      {
1582         i >>= 1;
1583         e++;
1584      }
1585   else
1586      while (!(m & i) && i)
1587      {
1588         i >>= 1;
1589         e++;
1590      }
1591
1592   if (e > 0)
1593      *Coefficient = m * dsp1_state.DataRom[0x21 + e] << 1;
1594   else
1595      *Coefficient = m;
1596
1597   *Exponent -= e;
1598}
1599
1600//////////////////////////////////////////////////////////////////
1601
1602// Same than 'normalize' but with an INT32 input
1603
1604static void normalize_double( INT32 Product, INT16 *Coefficient, INT16 *Exponent )
1605{
1606   INT16 n = Product & 0x7fff;
1607   INT16 m = Product >> 15;
1608   INT16 i = 0x4000;
1609   INT16 e = 0;
1610
1611   if (m < 0)
1612      while ((m & i) && i)
1613      {
1614         i >>= 1;
1615         e++;
1616      }
1617   else
1618      while (!(m & i) && i)
1619      {
1620         i >>= 1;
1621         e++;
1622      }
1623
1624   if (e > 0)
1625   {
1626      *Coefficient = m * dsp1_state.DataRom[0x0021 + e] << 1;
1627
1628      if (e < 15)
1629         *Coefficient += n * dsp1_state.DataRom[0x0040 - e] >> 15;
1630      else
1631      {
1632         i = 0x4000;
1633
1634         if (m < 0)
1635            while ((n & i) && i)
1636            {
1637               i >>= 1;
1638               e++;
1639            }
1640         else
1641            while (!(n & i) && i)
1642            {
1643               i >>= 1;
1644               e++;
1645            }
1646
1647         if (e > 15)
1648            *Coefficient = n * dsp1_state.DataRom[0x0012 + e] << 1;
1649         else
1650            *Coefficient += n;
1651      }
1652   }
1653   else
1654      *Coefficient = m;
1655
1656   *Exponent = e;
1657}
1658
1659//////////////////////////////////////////////////////////////////
1660
1661// Shift to the right
1662
1663static INT16 shiftR( INT16 C, INT16 E )
1664{
1665   return (C * dsp1_state.DataRom[0x0031 + E] >> 15);
1666}
1667
1668//////////////////////////////////////////////////////////////////
1669
1670// Save DSP1 variables
1671
1672static void dsp1_register_save( running_machine *machine )
1673{
1674   state_save_register_global(machine, dsp1_state.Sr);
1675   state_save_register_global(machine, dsp1_state.Dr);
1676   state_save_register_global(machine, dsp1_state.SrLowByteAccess);
1677   state_save_register_global(machine, dsp1_state.FsmMajorState);
1678   state_save_register_global(machine, dsp1_state.Command);
1679   state_save_register_global(machine, dsp1_state.DataCounter);
1680   state_save_register_global(machine, dsp1_state.Freeze);
1681   state_save_register_global_array(machine, dsp1_state.ReadBuffer);
1682   state_save_register_global_array(machine, dsp1_state.WriteBuffer);
1683   state_save_register_global_array(machine, dsp1_state.shared.MatrixA[0]);
1684   state_save_register_global_array(machine, dsp1_state.shared.MatrixA[1]);
1685   state_save_register_global_array(machine, dsp1_state.shared.MatrixA[2]);
1686   state_save_register_global_array(machine, dsp1_state.shared.MatrixB[0]);
1687   state_save_register_global_array(machine, dsp1_state.shared.MatrixB[1]);
1688   state_save_register_global_array(machine, dsp1_state.shared.MatrixB[2]);
1689   state_save_register_global_array(machine, dsp1_state.shared.MatrixC[0]);
1690   state_save_register_global_array(machine, dsp1_state.shared.MatrixC[1]);
1691   state_save_register_global_array(machine, dsp1_state.shared.MatrixC[2]);
1692   state_save_register_global(machine, dsp1_state.shared.CentreX);
1693   state_save_register_global(machine, dsp1_state.shared.CentreY);
1694   state_save_register_global(machine, dsp1_state.shared.CentreZ);
1695   state_save_register_global(machine, dsp1_state.shared.CentreZ_C);
1696   state_save_register_global(machine, dsp1_state.shared.CentreZ_E);
1697   state_save_register_global(machine, dsp1_state.shared.VOffset);
1698   state_save_register_global(machine, dsp1_state.shared.Les);
1699   state_save_register_global(machine, dsp1_state.shared.C_Les);
1700   state_save_register_global(machine, dsp1_state.shared.E_Les);
1701   state_save_register_global(machine, dsp1_state.shared.SinAas);
1702   state_save_register_global(machine, dsp1_state.shared.CosAas);
1703   state_save_register_global(machine, dsp1_state.shared.SinAzs);
1704   state_save_register_global(machine, dsp1_state.shared.CosAzs);
1705   state_save_register_global(machine, dsp1_state.shared.SinAZS);
1706   state_save_register_global(machine, dsp1_state.shared.CosAZS);
1707   state_save_register_global(machine, dsp1_state.shared.SecAZS_C1);
1708   state_save_register_global(machine, dsp1_state.shared.SecAZS_E1);
1709   state_save_register_global(machine, dsp1_state.shared.SecAZS_C2);
1710   state_save_register_global(machine, dsp1_state.shared.SecAZS_E2);
1711   state_save_register_global(machine, dsp1_state.shared.Nx);
1712   state_save_register_global(machine, dsp1_state.shared.Ny);
1713   state_save_register_global(machine, dsp1_state.shared.Nz);
1714   state_save_register_global(machine, dsp1_state.shared.Gx);
1715   state_save_register_global(machine, dsp1_state.shared.Gy);
1716   state_save_register_global(machine, dsp1_state.shared.Gz);
1717   state_save_register_global(machine, dsp1_state.shared.Hx);
1718   state_save_register_global(machine, dsp1_state.shared.Hy);
1719   state_save_register_global(machine, dsp1_state.shared.Vx);
1720   state_save_register_global(machine, dsp1_state.shared.Vy);
1721   state_save_register_global(machine, dsp1_state.shared.Vz);
1722}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team