Previous 199869 Revisions Next

r20881 Sunday 10th February, 2013 at 09:14:58 UTC by Fabio Priuli
(MESS) snes.c: refactored SPC7110 and SDD1 implementations to be more class-friendly
and not to use running_machine to access the ROM data. no whatsnew.
[src/mame/machine]snes7110.c snessdd1.c

trunk/src/mame/machine/snes7110.c
r20880r20881
1515***************************************************************************/
1616
1717
18static const UINT32 spc7110_decomp_buffer_size = 64;
18#define SPC7110_DECOMP_BUFFER_SIZE 64
1919
2020static const UINT8 spc7110_evolution_table[53][4] =
2121{
r20880r20881
118118   { 31, 31 },
119119};
120120
121struct SPC7110Decomp
121class SPC7110Decomp
122122{
123   running_machine &machine() const { assert(m_machine != NULL); return *m_machine; }
123public:
124   SPC7110Decomp(running_machine &machine, UINT32 size);
124125
125   running_machine *m_machine;
126   running_machine &machine() const { return m_machine; }
126127
127   UINT32 decomp_mode;
128   UINT32 decomp_offset;
128   void init(running_machine &machine, UINT8 *ROM, UINT32 mode, UINT32 offset, UINT32 index);
129   void reset();
130   
131   UINT8 read(UINT8 *ROM);
132   void write(UINT8 data);
133   void mode0(UINT8 init, UINT8 *ROM);
134   void mode1(UINT8 init, UINT8 *ROM);
135   void mode2(UINT8 init, UINT8 *ROM);
129136
130   UINT8 *decomp_buffer;
131   UINT32 decomp_buffer_rdoffset;
132   UINT32 decomp_buffer_wroffset;
133   UINT32 decomp_buffer_length;
137   UINT8 dataread(UINT8 *ROM);
138   UINT8 probability(UINT32 n);
139   UINT8 next_lps(UINT32 n);
140   UINT8 next_mps(UINT32 n);
141   UINT8 toggle_invert(UINT32 n);
142   UINT32 morton_2x8(UINT32 data);
143   UINT32 morton_4x8(UINT32 data);
134144
145   UINT32 m_decomp_mode;
146   UINT32 m_decomp_offset;
147
148   UINT8 *m_decomp_buffer;
149   UINT32 m_decomp_buffer_rdoffset;
150   UINT32 m_decomp_buffer_wroffset;
151   UINT32 m_decomp_buffer_length;
152
135153   struct ContextState
136154   {
137155      UINT8 index;
138156      UINT8 invert;
139   } context[32];
157   } m_context[32];
140158
141   UINT32 morton16[2][256];
142   UINT32 morton32[4][256];
159   UINT32 m_morton16[2][256];
160   UINT32 m_morton32[4][256];
143161
144   UINT32 rom_size;
162   
163private:
164   running_machine& m_machine;
165   UINT32 m_rom_size;
145166};
146167
147static SPC7110Decomp* SPC7110Decomp_ctor(running_machine &machine, UINT32 size);
148static void SPC7110Decomp_reset(SPC7110Decomp *thisptr);
149static void SPC7110Decomp_init(SPC7110Decomp *thisptr, running_machine &machine, UINT32 mode, UINT32 offset, UINT32 index);
150static UINT8 SPC7110Decomp_read(SPC7110Decomp *thisptr);
151static void SPC7110Decomp_write(SPC7110Decomp *thisptr, UINT8 data);
152static UINT8 SPC7110Decomp_dataread(SPC7110Decomp *thisptr);
153static void SPC7110Decomp_mode0(SPC7110Decomp *thisptr, UINT8 init);
154static void SPC7110Decomp_mode1(SPC7110Decomp *thisptr, UINT8 init);
155static void SPC7110Decomp_mode2(SPC7110Decomp *thisptr, UINT8 init);
156static UINT8 SPC7110Decomp_probability(SPC7110Decomp *thisptr, UINT32 n);
157static UINT8 SPC7110Decomp_next_lps(SPC7110Decomp *thisptr, UINT32 n);
158static UINT8 SPC7110Decomp_next_mps(SPC7110Decomp *thisptr, UINT32 n);
159static UINT8 SPC7110Decomp_toggle_invert(SPC7110Decomp *thisptr, UINT32 n);
160static UINT32 SPC7110Decomp_morton_2x8(SPC7110Decomp *thisptr, UINT32 data);
161static UINT32 SPC7110Decomp_morton_4x8(SPC7110Decomp *thisptr, UINT32 data);
162
163static SPC7110Decomp* SPC7110Decomp_ctor(running_machine &machine, UINT32 size)
168SPC7110Decomp::SPC7110Decomp(running_machine &machine, UINT32 size)
169      :  m_machine(machine),
170      m_rom_size(size)
164171{
165   UINT32 i;
166   SPC7110Decomp* newclass = (SPC7110Decomp*)auto_alloc_array(machine, UINT8, sizeof(SPC7110Decomp));
167   newclass->decomp_buffer = (UINT8*)auto_alloc_array(machine, UINT8, spc7110_decomp_buffer_size);
168   SPC7110Decomp_reset(newclass);
172   m_decomp_buffer = (UINT8*)auto_alloc_array(machine, UINT8, SPC7110_DECOMP_BUFFER_SIZE);
173   reset();
169174
170   for(i = 0; i < 256; i++)
175   for (int i = 0; i < 256; i++)
171176   {
172177      #define map(x, y) (((i >> x) & 1) << y)
173178      //2x8-bit
174      newclass->morton16[1][i] = map(7, 15) + map(6,  7) + map(5, 14) + map(4,  6)
175                           + map(3, 13) + map(2,  5) + map(1, 12) + map(0,  4);
176      newclass->morton16[0][i] = map(7, 11) + map(6,  3) + map(5, 10) + map(4,  2)
177                           + map(3,  9) + map(2,  1) + map(1,  8) + map(0,  0);
179      m_morton16[1][i] = map(7, 15) + map(6,  7) + map(5, 14) + map(4,  6)
180                     + map(3, 13) + map(2,  5) + map(1, 12) + map(0,  4);
181      m_morton16[0][i] = map(7, 11) + map(6,  3) + map(5, 10) + map(4,  2)
182                     + map(3,  9) + map(2,  1) + map(1,  8) + map(0,  0);
178183      //4x8-bit
179      newclass->morton32[3][i] = map(7, 31) + map(6, 23) + map(5, 15) + map(4,  7)
180                           + map(3, 30) + map(2, 22) + map(1, 14) + map(0,  6);
181      newclass->morton32[2][i] = map(7, 29) + map(6, 21) + map(5, 13) + map(4,  5)
182                           + map(3, 28) + map(2, 20) + map(1, 12) + map(0,  4);
183      newclass->morton32[1][i] = map(7, 27) + map(6, 19) + map(5, 11) + map(4,  3)
184                           + map(3, 26) + map(2, 18) + map(1, 10) + map(0,  2);
185      newclass->morton32[0][i] = map(7, 25) + map(6, 17) + map(5,  9) + map(4,  1)
186                           + map(3, 24) + map(2, 16) + map(1,  8) + map(0,  0);
184      m_morton32[3][i] = map(7, 31) + map(6, 23) + map(5, 15) + map(4,  7)
185                     + map(3, 30) + map(2, 22) + map(1, 14) + map(0,  6);
186      m_morton32[2][i] = map(7, 29) + map(6, 21) + map(5, 13) + map(4,  5)
187                     + map(3, 28) + map(2, 20) + map(1, 12) + map(0,  4);
188      m_morton32[1][i] = map(7, 27) + map(6, 19) + map(5, 11) + map(4,  3)
189                     + map(3, 26) + map(2, 18) + map(1, 10) + map(0,  2);
190      m_morton32[0][i] = map(7, 25) + map(6, 17) + map(5,  9) + map(4,  1)
191                     + map(3, 24) + map(2, 16) + map(1,  8) + map(0,  0);
187192      #undef map
188193   }
189
190   newclass->rom_size = size;
191
192   return newclass;
193194}
194195
195static void SPC7110Decomp_reset(SPC7110Decomp *thisptr)
196void SPC7110Decomp::reset()
196197{
197198   //mode 3 is invalid; this is treated as a special case to always return 0x00
198199   //set to mode 3 so that reading decomp port before starting first decomp will return 0x00
199   thisptr->decomp_mode = 3;
200   m_decomp_mode = 3;
200201
201   thisptr->decomp_buffer_rdoffset = 0;
202   thisptr->decomp_buffer_wroffset = 0;
203   thisptr->decomp_buffer_length   = 0;
202   m_decomp_buffer_rdoffset = 0;
203   m_decomp_buffer_wroffset = 0;
204   m_decomp_buffer_length   = 0;
204205}
205206
206static void SPC7110Decomp_init(SPC7110Decomp *thisptr, running_machine &machine, UINT32 mode, UINT32 offset, UINT32 index)
207void SPC7110Decomp::init(running_machine &machine, UINT8 *ROM, UINT32 mode, UINT32 offset, UINT32 index)
207208{
208   UINT32 i;
209   m_decomp_mode = mode;
210   m_decomp_offset = offset;
209211
210   thisptr->m_machine = &machine;
212   m_decomp_buffer_rdoffset = 0;
213   m_decomp_buffer_wroffset = 0;
214   m_decomp_buffer_length   = 0;
211215
212   thisptr->decomp_mode = mode;
213   thisptr->decomp_offset = offset;
214
215   thisptr->decomp_buffer_rdoffset = 0;
216   thisptr->decomp_buffer_wroffset = 0;
217   thisptr->decomp_buffer_length   = 0;
218
219216   //reset context states
220   for(i = 0; i < 32; i++)
217   for (int i = 0; i < 32; i++)
221218   {
222      thisptr->context[i].index  = 0;
223      thisptr->context[i].invert = 0;
219      m_context[i].index  = 0;
220      m_context[i].invert = 0;
224221   }
225222
226   switch(thisptr->decomp_mode)
223   switch (m_decomp_mode)
227224   {
228      case 0: SPC7110Decomp_mode0(thisptr, 1); break;
229      case 1: SPC7110Decomp_mode1(thisptr, 1); break;
230      case 2: SPC7110Decomp_mode2(thisptr, 1); break;
225      case 0: mode0(1, ROM); break;
226      case 1: mode1(1, ROM); break;
227      case 2: mode2(1, ROM); break;
231228   }
232229
233230   //decompress up to requested output data index
234   while(index--)
231   while (index--)
235232   {
236      SPC7110Decomp_read(thisptr);
233      read(ROM);
237234   }
238235}
239236
240static UINT8 SPC7110Decomp_read(SPC7110Decomp *thisptr)
237UINT8 SPC7110Decomp::read(UINT8 *ROM)
241238{
242239   UINT8 data;
243240
244   if(thisptr->decomp_buffer_length == 0) {
245      //decompress at least (spc7110_decomp_buffer_size / 2) bytes to the buffer
246      switch(thisptr->decomp_mode) {
241   if (m_decomp_buffer_length == 0)
242   {
243      //decompress at least (SPC7110_DECOMP_BUFFER_SIZE / 2) bytes to the buffer
244      switch (m_decomp_mode)
245      {
247246         case 0:
248            SPC7110Decomp_mode0(thisptr, 0);
247            mode0(0, ROM);
249248            break;
250249
251250         case 1:
252            SPC7110Decomp_mode1(thisptr, 0);
251            mode1(0, ROM);
253252            break;
254253
255254         case 2:
256            SPC7110Decomp_mode2(thisptr, 0);
255            mode2(0, ROM);
257256            break;
258257
259258         default:
r20880r20881
261260      }
262261   }
263262
264   data = thisptr->decomp_buffer[thisptr->decomp_buffer_rdoffset++];
265   thisptr->decomp_buffer_rdoffset &= spc7110_decomp_buffer_size - 1;
266   thisptr->decomp_buffer_length--;
263   data = m_decomp_buffer[m_decomp_buffer_rdoffset++];
264   m_decomp_buffer_rdoffset &= SPC7110_DECOMP_BUFFER_SIZE - 1;
265   m_decomp_buffer_length--;
267266   return data;
268267}
269268
270static void SPC7110Decomp_write(SPC7110Decomp *thisptr, UINT8 data)
269void SPC7110Decomp::write(UINT8 data)
271270{
272   thisptr->decomp_buffer[thisptr->decomp_buffer_wroffset++] = data;
273   thisptr->decomp_buffer_wroffset &= spc7110_decomp_buffer_size - 1;
274   thisptr->decomp_buffer_length++;
271   m_decomp_buffer[m_decomp_buffer_wroffset++] = data;
272   m_decomp_buffer_wroffset &= SPC7110_DECOMP_BUFFER_SIZE - 1;
273   m_decomp_buffer_length++;
275274}
276275
277static UINT8 SPC7110Decomp_dataread(SPC7110Decomp *thisptr)
276UINT8 SPC7110Decomp::dataread(UINT8 *ROM)
278277{
279   UINT8 *ROM = thisptr->machine().root_device().memregion("cart")->base();
280   UINT32 size = thisptr->rom_size - 0x100000;
281   while(thisptr->decomp_offset >= size)
278   UINT32 size = m_rom_size - 0x100000;
279   while (m_decomp_offset >= size)
282280   {
283      thisptr->decomp_offset -= size;
281      m_decomp_offset -= size;
284282   }
285   return ROM[0x100000 + thisptr->decomp_offset++];
283   return ROM[0x100000 + m_decomp_offset++];
286284}
287285
288static void SPC7110Decomp_mode0(SPC7110Decomp *thisptr, UINT8 init)
286void SPC7110Decomp::mode0(UINT8 init, UINT8 *ROM)
289287{
290288   static UINT8 val, in, span;
291289   static INT32 out, inverts, lps, in_count;
292290
293   if(init == 1)
291   if (init == 1)
294292   {
295293      out = inverts = lps = 0;
296294      span = 0xff;
297      val = SPC7110Decomp_dataread(thisptr);
298      in = SPC7110Decomp_dataread(thisptr);
295      val = dataread(ROM);
296      in = dataread(ROM);
299297      in_count = 8;
300298      return;
301299   }
302300
303   while(thisptr->decomp_buffer_length < (spc7110_decomp_buffer_size >> 1))
301   while (m_decomp_buffer_length < (SPC7110_DECOMP_BUFFER_SIZE >> 1))
304302   {
305      UINT32 bit;
306      for(bit = 0; bit < 8; bit++)
303      for (int bit = 0; bit < 8; bit++)
307304      {
308305         //get context
309306         UINT8 mask = (1 << (bit & 3)) - 1;
310307         UINT8 con = mask + ((inverts & mask) ^ (lps & mask));
311308         UINT32 prob, mps, flag_lps;
312309         UINT32 shift = 0;
313         if(bit > 3)
310         if (bit > 3)
314311         {
315312            con += 15;
316313         }
317314
318315         //get prob and mps
319         prob = SPC7110Decomp_probability(thisptr, con);
320         mps = (((out >> 15) & 1) ^ thisptr->context[con].invert);
316         prob = probability(con);
317         mps = (((out >> 15) & 1) ^ m_context[con].invert);
321318
322319         //get bit
323         if(val <= span - prob) //mps
320         if (val <= span - prob) //mps
324321         {
325322            span = span - prob;
326323            out = (out << 1) + mps;
r20880r20881
335332         }
336333
337334         //renormalize
338         while(span < 0x7f)
335         while (span < 0x7f)
339336         {
340337            shift++;
341338
r20880r20881
343340            val = (val << 1) + (in >> 7);
344341
345342            in <<= 1;
346            if(--in_count == 0)
343            if (--in_count == 0)
347344            {
348               in = SPC7110Decomp_dataread(thisptr);
345               in = dataread(ROM);
349346               in_count = 8;
350347            }
351348         }
352349
353350         //update processing info
354351         lps = (lps << 1) + flag_lps;
355         inverts = (inverts << 1) + thisptr->context[con].invert;
352         inverts = (inverts << 1) + m_context[con].invert;
356353
357354         //update context state
358         if(flag_lps & SPC7110Decomp_toggle_invert(thisptr, con))
355         if (flag_lps & toggle_invert(con))
359356         {
360            thisptr->context[con].invert ^= 1;
357            m_context[con].invert ^= 1;
361358         }
362         if(flag_lps)
359         if (flag_lps)
363360         {
364            thisptr->context[con].index = SPC7110Decomp_next_lps(thisptr, con);
361            m_context[con].index = next_lps(con);
365362         }
366         else if(shift)
363         else if (shift)
367364         {
368            thisptr->context[con].index = SPC7110Decomp_next_mps(thisptr, con);
365            m_context[con].index = next_mps(con);
369366         }
370367      }
371368
372369      //save byte
373      SPC7110Decomp_write(thisptr, out);
370      write(out);
374371   }
375372}
376373
377static void SPC7110Decomp_mode1(SPC7110Decomp *thisptr, UINT8 init)
374void SPC7110Decomp::mode1(UINT8 init, UINT8 *ROM)
378375{
379376   static INT32 pixelorder[4], realorder[4];
380377   static UINT8 in, val, span;
381378   static INT32 out, inverts, lps, in_count;
382379
383   if(init == 1)
380   if (init == 1)
384381   {
385      UINT32 i;
386      for(i = 0; i < 4; i++)
382      for (int i = 0; i < 4; i++)
387383      {
388384         pixelorder[i] = i;
389385      }
390386      out = inverts = lps = 0;
391387      span = 0xff;
392      val = SPC7110Decomp_dataread(thisptr);
393      in = SPC7110Decomp_dataread(thisptr);
388      val = dataread(ROM);
389      in = dataread(ROM);
394390      in_count = 8;
395391      return;
396392   }
397393
398   while(thisptr->decomp_buffer_length < (spc7110_decomp_buffer_size >> 1))
394   while (m_decomp_buffer_length < (SPC7110_DECOMP_BUFFER_SIZE >> 1))
399395   {
400396      UINT16 data;
401      UINT32 pixel;
402      for(pixel = 0; pixel < 8; pixel++)
397      for (int pixel = 0; pixel < 8; pixel++)
403398      {
404399         //get first symbol context
405400         UINT32 a = ((out >> (1 * 2)) & 3);
406401         UINT32 b = ((out >> (7 * 2)) & 3);
407402         UINT32 c = ((out >> (8 * 2)) & 3);
408403         UINT32 con = (a == b) ? (b != c) : (b == c) ? 2 : 4 - (a == c);
409         UINT32 bit;
410404
411405         //update pixel order
412406         UINT32 m, n;
413         for(m = 0; m < 4; m++)
407         for (m = 0; m < 4; m++)
414408         {
415            if(pixelorder[m] == a)
409            if (pixelorder[m] == a)
416410            {
417411               break;
418412            }
419413         }
420         for(n = m; n > 0; n--)
414         for (n = m; n > 0; n--)
421415         {
422416            pixelorder[n] = pixelorder[n - 1];
423417         }
424418         pixelorder[0] = a;
425419
426420         //calculate the real pixel order
427         for(m = 0; m < 4; m++)
421         for (m = 0; m < 4; m++)
428422         {
429423            realorder[m] = pixelorder[m];
430424         }
431425
432426         //rotate reference pixel c value to top
433         for(m = 0; m < 4; m++)
427         for (m = 0; m < 4; m++)
434428         {
435            if(realorder[m] == c)
429            if (realorder[m] == c)
436430            {
437431               break;
438432            }
439433         }
440         for(n = m; n > 0; n--)
434         for (n = m; n > 0; n--)
441435         {
442436            realorder[n] = realorder[n - 1];
443437         }
444438         realorder[0] = c;
445439
446440         //rotate reference pixel b value to top
447         for(m = 0; m < 4; m++)
441         for (m = 0; m < 4; m++)
448442         {
449            if(realorder[m] == b)
443            if (realorder[m] == b)
450444            {
451445               break;
452446            }
453447         }
454         for(n = m; n > 0; n--)
448         for (n = m; n > 0; n--)
455449         {
456450            realorder[n] = realorder[n - 1];
457451         }
458452         realorder[0] = b;
459453
460454         //rotate reference pixel a value to top
461         for(m = 0; m < 4; m++)
455         for (m = 0; m < 4; m++)
462456         {
463            if(realorder[m] == a)
457            if (realorder[m] == a)
464458            {
465459               break;
466460            }
467461         }
468         for(n = m; n > 0; n--)
462         for (n = m; n > 0; n--)
469463         {
470464            realorder[n] = realorder[n - 1];
471465         }
472466         realorder[0] = a;
473467
474468         //get 2 symbols
475         for(bit = 0; bit < 2; bit++)
469         for (int bit = 0; bit < 2; bit++)
476470         {
477471            //get prob
478            UINT32 prob = SPC7110Decomp_probability(thisptr, con);
472            UINT32 prob = probability(con);
479473            UINT32 shift = 0;
480474
481475            //get symbol
482476            UINT32 flag_lps;
483            if(val <= span - prob) //mps
477            if (val <= span - prob) //mps
484478            {
485479               span = span - prob;
486480               flag_lps = 0;
r20880r20881
493487            }
494488
495489            //renormalize
496            while(span < 0x7f)
490            while (span < 0x7f)
497491            {
498492               shift++;
499493
r20880r20881
501495               val = (val << 1) + (in >> 7);
502496
503497               in <<= 1;
504               if(--in_count == 0)
498               if (--in_count == 0)
505499               {
506                  in = SPC7110Decomp_dataread(thisptr);
500                  in = dataread(ROM);
507501                  in_count = 8;
508502               }
509503            }
510504
511505            //update processing info
512506            lps = (lps << 1) + flag_lps;
513            inverts = (inverts << 1) + thisptr->context[con].invert;
507            inverts = (inverts << 1) + m_context[con].invert;
514508
515509            //update context state
516            if(flag_lps & SPC7110Decomp_toggle_invert(thisptr, con))
510            if (flag_lps & toggle_invert(con))
517511            {
518               thisptr->context[con].invert ^= 1;
512               m_context[con].invert ^= 1;
519513            }
520            if(flag_lps)
514            if (flag_lps)
521515            {
522               thisptr->context[con].index = SPC7110Decomp_next_lps(thisptr, con);
516               m_context[con].index = next_lps(con);
523517            }
524            else if(shift)
518            else if (shift)
525519            {
526               thisptr->context[con].index = SPC7110Decomp_next_mps(thisptr, con);
520               m_context[con].index = next_mps(con);
527521            }
528522
529523            //get next context
r20880r20881
536530      }
537531
538532      //turn pixel data into bitplanes
539      data = SPC7110Decomp_morton_2x8(thisptr, out);
540      SPC7110Decomp_write(thisptr, data >> 8);
541      SPC7110Decomp_write(thisptr, data >> 0);
533      data = morton_2x8(out);
534      write(data >> 8);
535      write(data >> 0);
542536   }
543537}
544538
545static void SPC7110Decomp_mode2(SPC7110Decomp *thisptr, UINT8 init)
539void SPC7110Decomp::mode2(UINT8 init, UINT8 *ROM)
546540{
547541   static INT32 pixelorder[16], realorder[16];
548542   static UINT8 bitplanebuffer[16], buffer_index;
549543   static UINT8 in, val, span;
550544   static INT32 out0, out1, inverts, lps, in_count;
551545
552   if(init == 1)
546   if (init == 1)
553547   {
554      UINT32 i;
555      for(i = 0; i < 16; i++)
548      for (int i = 0; i < 16; i++)
556549      {
557550         pixelorder[i] = i;
558551      }
559552      buffer_index = 0;
560553      out0 = out1 = inverts = lps = 0;
561554      span = 0xff;
562      val = SPC7110Decomp_dataread(thisptr);
563      in = SPC7110Decomp_dataread(thisptr);
555      val = dataread(ROM);
556      in = dataread(ROM);
564557      in_count = 8;
565558      return;
566559   }
567560
568   while(thisptr->decomp_buffer_length < (spc7110_decomp_buffer_size >> 1))
561   while (m_decomp_buffer_length < (SPC7110_DECOMP_BUFFER_SIZE >> 1))
569562   {
570563      UINT32 data;
571      UINT32 pixel;
572      for(pixel = 0; pixel < 8; pixel++)
564      for (int pixel = 0; pixel < 8; pixel++)
573565      {
574566         //get first symbol context
575567         UINT32 a = ((out0 >> (0 * 4)) & 15);
r20880r20881
577569         UINT32 c = ((out1 >> (0 * 4)) & 15);
578570         UINT32 con = 0;
579571         UINT32 refcon = (a == b) ? (b != c) : (b == c) ? 2 : 4 - (a == c);
580         UINT32 bit;
581572
582573         //update pixel order
583574         UINT32 m, n;
584         for(m = 0; m < 16; m++)
575         for (m = 0; m < 16; m++)
585576         {
586            if(pixelorder[m] == a)
577            if (pixelorder[m] == a)
587578            {
588579               break;
589580            }
590581         }
591         for(n = m; n >  0; n--)
582         for (n = m; n >  0; n--)
592583         {
593584            pixelorder[n] = pixelorder[n - 1];
594585         }
595586         pixelorder[0] = a;
596587
597588         //calculate the real pixel order
598         for(m = 0; m < 16; m++)
589         for (m = 0; m < 16; m++)
599590         {
600591            realorder[m] = pixelorder[m];
601592         }
602593
603594         //rotate reference pixel c value to top
604         for(m = 0; m < 16; m++)
595         for (m = 0; m < 16; m++)
605596         {
606            if(realorder[m] == c)
597            if (realorder[m] == c)
607598            {
608599               break;
609600            }
610601         }
611         for(n = m; n >  0; n--)
602         for (n = m; n >  0; n--)
612603         {
613604            realorder[n] = realorder[n - 1];
614605         }
615606         realorder[0] = c;
616607
617608         //rotate reference pixel b value to top
618         for(m = 0; m < 16; m++)
609         for (m = 0; m < 16; m++)
619610         {
620            if(realorder[m] == b)
611            if (realorder[m] == b)
621612            {
622613               break;
623614            }
624615         }
625         for(n = m; n >  0; n--)
616         for (n = m; n >  0; n--)
626617         {
627618            realorder[n] = realorder[n - 1];
628619         }
629620         realorder[0] = b;
630621
631622         //rotate reference pixel a value to top
632         for(m = 0; m < 16; m++)
623         for (m = 0; m < 16; m++)
633624         {
634            if(realorder[m] == a)
625            if (realorder[m] == a)
635626            {
636627               break;
637628            }
638629         }
639         for(n = m; n >  0; n--)
630         for (n = m; n >  0; n--)
640631         {
641632            realorder[n] = realorder[n - 1];
642633         }
643634         realorder[0] = a;
644635
645636         //get 4 symbols
646         for(bit = 0; bit < 4; bit++)
637         for (int bit = 0; bit < 4; bit++)
647638         {
648639            UINT32 invertbit, shift;
649640
650641            //get prob
651            UINT32 prob = SPC7110Decomp_probability(thisptr, con);
642            UINT32 prob = probability(con);
652643
653644            //get symbol
654645            UINT32 flag_lps;
655            if(val <= span - prob) //mps
646            if (val <= span - prob) //mps
656647            {
657648               span = span - prob;
658649               flag_lps = 0;
r20880r20881
666657
667658            //renormalize
668659            shift = 0;
669            while(span < 0x7f)
660            while (span < 0x7f)
670661            {
671662               shift++;
672663
r20880r20881
674665               val = (val << 1) + (in >> 7);
675666
676667               in <<= 1;
677               if(--in_count == 0)
668               if (--in_count == 0)
678669               {
679                  in = SPC7110Decomp_dataread(thisptr);
670                  in = dataread(ROM);
680671                  in_count = 8;
681672               }
682673            }
683674
684675            //update processing info
685676            lps = (lps << 1) + flag_lps;
686            invertbit = thisptr->context[con].invert;
677            invertbit = m_context[con].invert;
687678            inverts = (inverts << 1) + invertbit;
688679
689680            //update context state
690            if(flag_lps & SPC7110Decomp_toggle_invert(thisptr, con))
681            if (flag_lps & toggle_invert(con))
691682            {
692               thisptr->context[con].invert ^= 1;
683               m_context[con].invert ^= 1;
693684            }
694            if(flag_lps)
685            if (flag_lps)
695686            {
696               thisptr->context[con].index = SPC7110Decomp_next_lps(thisptr, con);
687               m_context[con].index = next_lps(con);
697688            }
698            else if(shift)
689            else if (shift)
699690            {
700               thisptr->context[con].index = SPC7110Decomp_next_mps(thisptr, con);
691               m_context[con].index = next_mps(con);
701692            }
702693
703694            //get next context
r20880r20881
711702      }
712703
713704      //convert pixel data into bitplanes
714      data = SPC7110Decomp_morton_4x8(thisptr, out0);
715      SPC7110Decomp_write(thisptr, data >> 24);
716      SPC7110Decomp_write(thisptr, data >> 16);
705      data = morton_4x8(out0);
706      write(data >> 24);
707      write(data >> 16);
717708      bitplanebuffer[buffer_index++] = data >> 8;
718709      bitplanebuffer[buffer_index++] = data >> 0;
719710
720      if(buffer_index == 16)
711      if (buffer_index == 16)
721712      {
722         UINT32 i;
723         for(i = 0; i < 16; i++)
713         for (int i = 0; i < 16; i++)
724714         {
725            SPC7110Decomp_write(thisptr, bitplanebuffer[i]);
715            write(bitplanebuffer[i]);
726716         }
727717         buffer_index = 0;
728718      }
729719   }
730720}
731721
732static UINT8 SPC7110Decomp_probability(SPC7110Decomp *thisptr, UINT32 n)
722UINT8 SPC7110Decomp::probability(UINT32 n)
733723{
734   return spc7110_evolution_table[thisptr->context[n].index][0];
724   return spc7110_evolution_table[m_context[n].index][0];
735725}
736726
737static UINT8 SPC7110Decomp_next_lps(SPC7110Decomp *thisptr, UINT32 n)
727UINT8 SPC7110Decomp::next_lps(UINT32 n)
738728{
739   return spc7110_evolution_table[thisptr->context[n].index][1];
729   return spc7110_evolution_table[m_context[n].index][1];
740730}
741731
742static UINT8 SPC7110Decomp_next_mps(SPC7110Decomp *thisptr, UINT32 n)
732UINT8 SPC7110Decomp::next_mps(UINT32 n)
743733{
744   return spc7110_evolution_table[thisptr->context[n].index][2];
734   return spc7110_evolution_table[m_context[n].index][2];
745735}
746736
747static UINT8 SPC7110Decomp_toggle_invert(SPC7110Decomp *thisptr, UINT32 n)
737UINT8 SPC7110Decomp::toggle_invert(UINT32 n)
748738{
749   return spc7110_evolution_table[thisptr->context[n].index][3];
739   return spc7110_evolution_table[m_context[n].index][3];
750740}
751741
752static UINT32 SPC7110Decomp_morton_2x8(SPC7110Decomp *thisptr, UINT32 data)
742UINT32 SPC7110Decomp::morton_2x8(UINT32 data)
753743{
754744   //reverse morton lookup: de-interleave two 8-bit values
755745   //15, 13, 11,  9,  7,  5,  3,  1 -> 15- 8
756746   //14, 12, 10,  8,  6,  4,  2,  0 ->  7- 0
757   return thisptr->morton16[0][(data >>  0) & 255] + thisptr->morton16[1][(data >>  8) & 255];
747   return m_morton16[0][(data >>  0) & 255] + m_morton16[1][(data >>  8) & 255];
758748}
759749
760static UINT32 SPC7110Decomp_morton_4x8(SPC7110Decomp *thisptr, UINT32 data)
750UINT32 SPC7110Decomp::morton_4x8(UINT32 data)
761751{
762752   //reverse morton lookup: de-interleave four 8-bit values
763753   //31, 27, 23, 19, 15, 11,  7,  3 -> 31-24
764754   //30, 26, 22, 18, 14, 10,  6,  2 -> 23-16
765755   //29, 25, 21, 17, 13,  9,  5,  1 -> 15- 8
766756   //28, 24, 20, 16, 12,  8,  4,  0 ->  7- 0
767   return thisptr->morton32[0][(data >>  0) & 255] + thisptr->morton32[1][(data >>  8) & 255]
768      + thisptr->morton32[2][(data >> 16) & 255] + thisptr->morton32[3][(data >> 24) & 255];
757   return m_morton32[0][(data >>  0) & 255] + m_morton32[1][(data >>  8) & 255]
758      + m_morton32[2][(data >> 16) & 255] + m_morton32[3][(data >> 24) & 255];
769759}
770760
771761static void spc7110_mmio_write(running_machine &machine, UINT32 addr, UINT8 data);
r20880r20881
934924
935925   snes_spc7110.size = state->m_cart_size;
936926
937   snes_spc7110.decomp = SPC7110Decomp_ctor(machine, snes_spc7110.size);
927   snes_spc7110.decomp = auto_alloc(machine, SPC7110Decomp(machine, snes_spc7110.size));
938928}
939929
940930static void spc7110rtc_init(running_machine& machine)
r20880r20881
953943static UINT32 spc7110_datarom_addr(UINT32 addr)
954944{
955945   UINT32 size = snes_spc7110.size - 0x100000;
956   while(addr >= size)
946   while (addr >= size)
957947   {
958948      addr -= size;
959949   }
r20880r20881
10871077         counter--;
10881078         snes_spc7110.r4809 = counter;
10891079         snes_spc7110.r480a = counter >> 8;
1090         return SPC7110Decomp_read(snes_spc7110.decomp);
1080         return snes_spc7110.decomp->read(ROM);
10911081      }
10921082   case 0x4801: return snes_spc7110.r4801;
10931083   case 0x4802: return snes_spc7110.r4802;
r20880r20881
11161106         UINT8 data;
11171107         UINT32 address, adjust, adjustaddr;
11181108
1119         if(snes_spc7110.r481x != 0x07) return 0x00;
1109         if (snes_spc7110.r481x != 0x07) return 0x00;
11201110
11211111         address = spc7110_data_pointer();
11221112         adjust = spc7110_data_adjust();
1123         if(snes_spc7110.r4818 & 8)
1113         if (snes_spc7110.r4818 & 8)
11241114         {
11251115            adjust = (INT16)adjust;  //16-bit sign extend
11261116         }
11271117
11281118         adjustaddr = address;
1129         if(snes_spc7110.r4818 & 2)
1119         if (snes_spc7110.r4818 & 2)
11301120         {
11311121            adjustaddr += adjust;
11321122            spc7110_set_data_adjust(adjust + 1);
11331123         }
11341124
11351125         data = ROM[spc7110_datarom_addr(adjustaddr)];
1136         if(!(snes_spc7110.r4818 & 2))
1126         if (!(snes_spc7110.r4818 & 2))
11371127         {
11381128            UINT32 increment = (snes_spc7110.r4818 & 1) ? spc7110_data_increment() : 1;
1139            if(snes_spc7110.r4818 & 4)
1129            if (snes_spc7110.r4818 & 4)
11401130            {
11411131               increment = (INT16)increment;  //16-bit sign extend
11421132            }
11431133
1144            if((snes_spc7110.r4818 & 16) == 0)
1134            if ((snes_spc7110.r4818 & 16) == 0)
11451135            {
11461136               spc7110_set_data_pointer(address + increment);
11471137            }
r20880r20881
11651155      {
11661156         UINT8 data;
11671157         UINT32 address, adjust;
1168         if(snes_spc7110.r481x != 0x07)
1158         if (snes_spc7110.r481x != 0x07)
11691159         {
11701160            return 0x00;
11711161         }
11721162
11731163         address = spc7110_data_pointer();
11741164         adjust = spc7110_data_adjust();
1175         if(snes_spc7110.r4818 & 8)
1165         if (snes_spc7110.r4818 & 8)
11761166         {
11771167            adjust = (INT16)adjust;  //16-bit sign extend
11781168         }
11791169
11801170         data = ROM[spc7110_datarom_addr(address + adjust)];
1181         if((snes_spc7110.r4818 & 0x60) == 0x60)
1171         if ((snes_spc7110.r4818 & 0x60) == 0x60)
11821172         {
1183            if((snes_spc7110.r4818 & 16) == 0)
1173            if ((snes_spc7110.r4818 & 16) == 0)
11841174            {
11851175               spc7110_set_data_pointer(address + adjust);
11861176            }
r20880r20881
12861276               + (ROM[address + 2] <<  8)
12871277               + (ROM[address + 3] <<  0);
12881278
1289         SPC7110Decomp_init(snes_spc7110.decomp, machine, mode, offset, (snes_spc7110.r4805 + (snes_spc7110.r4806 << 8)) << mode);
1279         snes_spc7110.decomp->init(machine, ROM, mode, offset, (snes_spc7110.r4805 + (snes_spc7110.r4806 << 8)) << mode);
12901280         snes_spc7110.r480c = 0x80;
12911281      }
12921282      break;
r20880r20881
13081298      {
13091299         snes_spc7110.r4814 = data;
13101300         snes_spc7110.r4814_latch = 1;
1311         if(!snes_spc7110.r4815_latch)
1301         if (!snes_spc7110.r4815_latch)
13121302         {
13131303            break;
13141304         }
1315         if(!(snes_spc7110.r4818 & 2))
1305         if (!(snes_spc7110.r4818 & 2))
13161306         {
13171307            break;
13181308         }
1319         if(snes_spc7110.r4818 & 0x10)
1309         if (snes_spc7110.r4818 & 0x10)
13201310         {
13211311            break;
13221312         }
13231313
1324         if((snes_spc7110.r4818 & 0x60) == 0x20)
1314         if ((snes_spc7110.r4818 & 0x60) == 0x20)
13251315         {
13261316            UINT32 increment = spc7110_data_adjust() & 0xff;
1327            if(snes_spc7110.r4818 & 8)
1317            if (snes_spc7110.r4818 & 8)
13281318            {
13291319               increment = (INT8)increment;  //8-bit sign extend
13301320            }
13311321            spc7110_set_data_pointer(spc7110_data_pointer() + increment);
13321322         }
1333         else if((snes_spc7110.r4818 & 0x60) == 0x40)
1323         else if ((snes_spc7110.r4818 & 0x60) == 0x40)
13341324         {
13351325            UINT32 increment = spc7110_data_adjust();
1336            if(snes_spc7110.r4818 & 8)
1326            if (snes_spc7110.r4818 & 8)
13371327            {
13381328               increment = (INT16)increment;  //16-bit sign extend
13391329            }
r20880r20881
13461336      {
13471337         snes_spc7110.r4815 = data;
13481338         snes_spc7110.r4815_latch = 1;
1349         if(!snes_spc7110.r4814_latch)
1339         if (!snes_spc7110.r4814_latch)
13501340         {
13511341            break;
13521342         }
1353         if(!(snes_spc7110.r4818 & 2))
1343         if (!(snes_spc7110.r4818 & 2))
13541344         {
13551345            break;
13561346         }
1357         if(snes_spc7110.r4818 & 0x10)
1347         if (snes_spc7110.r4818 & 0x10)
13581348         {
13591349            break;
13601350         }
13611351
1362         if((snes_spc7110.r4818 & 0x60) == 0x20)
1352         if ((snes_spc7110.r4818 & 0x60) == 0x20)
13631353         {
13641354            UINT32 increment = spc7110_data_adjust() & 0xff;
1365            if(snes_spc7110.r4818 & 8)
1355            if (snes_spc7110.r4818 & 8)
13661356            {
13671357               increment = (INT8)increment;  //8-bit sign extend
13681358            }
13691359            spc7110_set_data_pointer(spc7110_data_pointer() + increment);
13701360         }
1371         else if((snes_spc7110.r4818 & 0x60) == 0x40)
1361         else if ((snes_spc7110.r4818 & 0x60) == 0x40)
13721362         {
13731363            UINT32 increment = spc7110_data_adjust();
1374            if(snes_spc7110.r4818 & 8)
1364            if (snes_spc7110.r4818 & 8)
13751365            {
13761366               increment = (INT16)increment;  //16-bit sign extend
13771367            }
r20880r20881
13841374   case 0x4817: snes_spc7110.r4817 = data; break;
13851375   case 0x4818:
13861376      {
1387            if(snes_spc7110.r481x != 0x07)
1377            if (snes_spc7110.r481x != 0x07)
13881378            break;
13891379
13901380            snes_spc7110.r4818 = data;
r20880r20881
14051395      {
14061396            snes_spc7110.r4825 = data;
14071397
1408            if(snes_spc7110.r482e & 1)
1398            if (snes_spc7110.r482e & 1)
14091399         {
14101400               //signed 16-bit x 16-bit multiplication
14111401               INT16 r0 = (INT16)(snes_spc7110.r4824 + (snes_spc7110.r4825 << 8));
r20880r20881
14391429      {
14401430         snes_spc7110.r4827 = data;
14411431
1442         if(snes_spc7110.r482e & 1)
1432         if (snes_spc7110.r482e & 1)
14431433         {
14441434            //signed 32-bit x 16-bit division
14451435            INT32 dividend = (INT32)(snes_spc7110.r4820 + (snes_spc7110.r4821 << 8) + (snes_spc7110.r4822 << 16) + (snes_spc7110.r4823 << 24));
r20880r20881
14481438            INT32 quotient;
14491439            INT16 remainder;
14501440
1451            if(divisor)
1441            if (divisor)
14521442            {
14531443               quotient  = (INT32)(dividend / divisor);
14541444               remainder = (INT32)(dividend % divisor);
r20880r20881
14771467            UINT32 quotient;
14781468            UINT16 remainder;
14791469
1480            if(divisor)
1470            if (divisor)
14811471            {
14821472               quotient  = (UINT32)(dividend / divisor);
14831473               remainder = (UINT16)(dividend % divisor);
r20880r20881
16281618               }
16291619
16301620               //disable timer
1631               if((data & 2) && !(snes_spc7110.rtc_ram[15] & 2))
1621               if ((data & 2) && !(snes_spc7110.rtc_ram[15] & 2))
16321622                  spc7110_update_time(machine, 0);
16331623            }
16341624
trunk/src/mame/machine/snessdd1.c
r20880r20881
88
99***************************************************************************/
1010
11static UINT8 sdd1_read(running_machine& machine, UINT32 addr);
1211
12#define SSD1_ADD(addr)\
13mmc[(addr >> 20) & 3] + (addr & 0x0fffff)
14
1315class SDD1_IM //Input Manager
1416{
1517public:
16   SDD1_IM(running_machine &machine)
17      : m_machine(machine) { }
1818
19   running_machine &machine() const { return m_machine; }
19   UINT32 m_byte_ptr;
20   UINT8 m_bit_count;
2021
21   UINT32 byte_ptr;
22   UINT8 bit_count;
23
24private:
25   running_machine& m_machine;
22   void IM_prepareDecomp(UINT32 in_buf);
23   UINT8 IM_getCodeword(UINT8 *ROM, UINT32 *mmc, const UINT8 code_len);
2624};
2725
28static void SDD1_IM_prepareDecomp(SDD1_IM* thisptr, UINT32 in_buf)
26void SDD1_IM::IM_prepareDecomp(UINT32 in_buf)
2927{
30   thisptr->byte_ptr = in_buf;
31   thisptr->bit_count = 4;
28   m_byte_ptr = in_buf;
29   m_bit_count = 4;
3230}
3331
34static UINT8 SDD1_IM_getCodeword(SDD1_IM* thisptr, const UINT8 code_len)
32UINT8 SDD1_IM::IM_getCodeword(UINT8 *ROM, UINT32 *mmc, const UINT8 code_len)
3533{
36   UINT8 codeword;
34   UINT8 codeword = ROM[SSD1_ADD(m_byte_ptr)] << m_bit_count;
3735
38   codeword = sdd1_read(thisptr->machine(), thisptr->byte_ptr) << thisptr->bit_count;
36   ++m_bit_count;
3937
40   ++thisptr->bit_count;
41
4238   if (codeword & 0x80)
4339   {
44      codeword |= sdd1_read(thisptr->machine(), thisptr->byte_ptr + 1) >> (9 - thisptr->bit_count);
45      thisptr->bit_count += code_len;
40      codeword |= ROM[SSD1_ADD(m_byte_ptr + 1)] >> (9 - m_bit_count);
41      m_bit_count += code_len;
4642   }
4743
48   if (thisptr->bit_count & 0x08)
44   if (m_bit_count & 0x08)
4945   {
50      thisptr->byte_ptr++;
51      thisptr->bit_count &= 0x07;
46      m_byte_ptr++;
47      m_bit_count &= 0x07;
5248   }
5349
5450   return codeword;
r20880r20881
5753class SDD1_GCD //Golomb-Code Decoder
5854{
5955public:
60   SDD1_GCD(running_machine &machine, SDD1_IM* associatedIM)
61      : IM(associatedIM),
62         m_machine(machine) { }
56   SDD1_GCD(SDD1_IM* associatedIM)
57      : m_IM(associatedIM) { }
6358
64   running_machine &machine() const { return m_machine; }
59   SDD1_IM* m_IM;
6560
66   SDD1_IM* IM;
67
68private:
69   running_machine& m_machine;
61   void GCD_getRunCount(UINT8 *ROM, UINT32 *mmc, UINT8 code_num, UINT8* MPScount, UINT8* LPSind);
7062};
7163
72static void SDD1_GCD_getRunCount(SDD1_GCD* thisptr, UINT8 code_num, UINT8* MPScount, UINT8* LPSind)
64void SDD1_GCD::GCD_getRunCount(UINT8 *ROM, UINT32 *mmc, UINT8 code_num, UINT8* MPScount, UINT8* LPSind)
7365{
7466   const UINT8 run_count[] =
7567   {
r20880r20881
10799      0x70, 0x30, 0x50, 0x10, 0x60, 0x20, 0x40, 0x00,
108100   };
109101
110   UINT8 codeword = SDD1_IM_getCodeword(thisptr->IM, code_num);
102   UINT8 codeword = m_IM->IM_getCodeword(ROM, mmc, code_num);
111103
112104   if (codeword & 0x80)
113105   {
r20880r20881
123115class SDD1_BG // Bits Generator
124116{
125117public:
126   SDD1_BG(running_machine &machine, SDD1_GCD* associatedGCD, UINT8 code)
127      : code_num(code),
128         GCD(associatedGCD),
129         m_machine(machine) { }
118   SDD1_BG(SDD1_GCD* associatedGCD, UINT8 code)
119      : m_code_num(code),
120         m_GCD(associatedGCD) { }
130121
131   running_machine &machine() const { return m_machine; }
122   UINT8 m_code_num;
123   UINT8 m_MPScount;
124   UINT8 m_LPSind;
125   SDD1_GCD* m_GCD;
132126
133   UINT8 code_num;
134   UINT8 MPScount;
135   UINT8 LPSind;
136   SDD1_GCD* GCD;
137
138public:
139   running_machine& m_machine;
127   void BG_prepareDecomp();
128   UINT8 BG_getBit(UINT8 *ROM, UINT32 *mmc, UINT8* endOfRun);
140129} ;
141130
142static void SDD1_BG_prepareDecomp(SDD1_BG* thisptr)
131void SDD1_BG::BG_prepareDecomp()
143132{
144   thisptr->MPScount = 0;
145   thisptr->LPSind = 0;
133   m_MPScount = 0;
134   m_LPSind = 0;
146135}
147136
148static UINT8 SDD1_BG_getBit(SDD1_BG* thisptr, UINT8* endOfRun)
137UINT8 SDD1_BG::BG_getBit(UINT8 *ROM, UINT32 *mmc, UINT8* endOfRun)
149138{
150139   UINT8 bit;
151140
152   if (!(thisptr->MPScount || thisptr->LPSind))
141   if (!(m_MPScount || m_LPSind))
153142   {
154      SDD1_GCD_getRunCount(thisptr->GCD, thisptr->code_num, &(thisptr->MPScount), &(thisptr->LPSind));
143      m_GCD->GCD_getRunCount(ROM, mmc, m_code_num, &(m_MPScount), &(m_LPSind));
155144   }
156145
157   if (thisptr->MPScount)
146   if (m_MPScount)
158147   {
159148      bit = 0;
160      thisptr->MPScount--;
149      m_MPScount--;
161150   }
162151   else
163152   {
164153      bit = 1;
165      thisptr->LPSind = 0;
154      m_LPSind = 0;
166155   }
167156
168   if (thisptr->MPScount || thisptr->LPSind)
157   if (m_MPScount || m_LPSind)
169158   {
170159      (*endOfRun) = 0;
171160   }
r20880r20881
185174   UINT8 nextIfLPS;
186175};
187176
188static const SDD1_PEM_state SDD1_PEM_evolution_table[33] =
177static const SDD1_PEM_state PEM_evolution_table[33] =
189178{
190179   { 0,25,25},
191180   { 0, 2, 1},
r20880r20881
231220class SDD1_PEM //Probability Estimation Module
232221{
233222public:
234   SDD1_PEM(running_machine& machine,
235                        SDD1_BG* associatedBG0, SDD1_BG* associatedBG1,
236                        SDD1_BG* associatedBG2, SDD1_BG* associatedBG3,
237                        SDD1_BG* associatedBG4, SDD1_BG* associatedBG5,
238                        SDD1_BG* associatedBG6, SDD1_BG* associatedBG7)
239      : m_machine(machine)
223   SDD1_PEM(
224         SDD1_BG* associatedBG0, SDD1_BG* associatedBG1,
225         SDD1_BG* associatedBG2, SDD1_BG* associatedBG3,
226         SDD1_BG* associatedBG4, SDD1_BG* associatedBG5,
227         SDD1_BG* associatedBG6, SDD1_BG* associatedBG7)
240228   {
241      BG[0] = associatedBG0;
242      BG[1] = associatedBG1;
243      BG[2] = associatedBG2;
244      BG[3] = associatedBG3;
245      BG[4] = associatedBG4;
246      BG[5] = associatedBG5;
247      BG[6] = associatedBG6;
248      BG[7] = associatedBG7;
229      m_BG[0] = associatedBG0;
230      m_BG[1] = associatedBG1;
231      m_BG[2] = associatedBG2;
232      m_BG[3] = associatedBG3;
233      m_BG[4] = associatedBG4;
234      m_BG[5] = associatedBG5;
235      m_BG[6] = associatedBG6;
236      m_BG[7] = associatedBG7;
249237   }
250238
251   running_machine &machine() const { return m_machine; }
239   SDD1_PEM_ContextInfo m_contextInfo[32];
240   SDD1_BG* m_BG[8];
252241
253   SDD1_PEM_ContextInfo contextInfo[32];
254   SDD1_BG* BG[8];
255
256private:
257   running_machine& m_machine;
242   void PEM_prepareDecomp();
243   UINT8 PEM_getBit(UINT8 *ROM, UINT32 *mmc, UINT8 context);
258244} ;
259245
260static void SDD1_PEM_prepareDecomp(SDD1_PEM* thisptr)
246void SDD1_PEM::PEM_prepareDecomp()
261247{
262   UINT8 i;
263   for(i = 0; i < 32; i++)
248   for (int i = 0; i < 32; i++)
264249   {
265      thisptr->contextInfo[i].status = 0;
266      thisptr->contextInfo[i].MPS = 0;
250      m_contextInfo[i].status = 0;
251      m_contextInfo[i].MPS = 0;
267252   }
268253}
269254
270static UINT8 SDD1_PEM_getBit(SDD1_PEM* thisptr, UINT8 context)
255UINT8 SDD1_PEM::PEM_getBit(UINT8 *ROM, UINT32 *mmc, UINT8 context)
271256{
272257   UINT8 endOfRun;
273258   UINT8 bit;
274259
275   SDD1_PEM_ContextInfo *pContInfo = &(thisptr->contextInfo)[context];
260   SDD1_PEM_ContextInfo *pContInfo = &(m_contextInfo)[context];
276261   UINT8 currStatus = pContInfo->status;
277   const SDD1_PEM_state* pState = &(SDD1_PEM_evolution_table[currStatus]);
262   const SDD1_PEM_state* pState = &(PEM_evolution_table[currStatus]);
278263   UINT8 currentMPS = pContInfo->MPS;
279264
280   bit = SDD1_BG_getBit(thisptr->BG[pState->code_num], &endOfRun);
265   bit = m_BG[pState->code_num]->BG_getBit(ROM, mmc, &endOfRun);
281266
282267   if (endOfRun)
283268   {
r20880r20881
301286class SDD1_CM
302287{
303288public:
304   SDD1_CM(running_machine& machine, SDD1_PEM* associatedPEM)
305      : PEM(associatedPEM),
306         m_machine(machine) { }
289   SDD1_CM(SDD1_PEM* associatedPEM)
290      : m_PEM(associatedPEM) { }
307291
308   running_machine &machine() const { return m_machine; }
292   UINT8 m_bitplanesInfo;
293   UINT8 m_contextBitsInfo;
294   UINT8 m_bit_number;
295   UINT8 m_currBitplane;
296   UINT16 m_prevBitplaneBits[8];
297   SDD1_PEM* m_PEM;
309298
310   UINT8 bitplanesInfo;
311   UINT8 contextBitsInfo;
312   UINT8 bit_number;
313   UINT8 currBitplane;
314   UINT16 prevBitplaneBits[8];
315   SDD1_PEM* PEM;
316
317private:
318   running_machine& m_machine;
299   void CM_prepareDecomp(UINT8 *ROM, UINT32 *mmc, UINT32 first_byte);
300   UINT8 CM_getBit(UINT8 *ROM, UINT32 *mmc);
319301} ;
320302
321static void SDD1_CM_prepareDecomp(SDD1_CM* thisptr, UINT32 first_byte)
303void SDD1_CM::CM_prepareDecomp(UINT8 *ROM, UINT32 *mmc, UINT32 first_byte)
322304{
323305   INT32 i = 0;
324   thisptr->bitplanesInfo = sdd1_read(thisptr->machine(), first_byte) & 0xc0;
325   thisptr->contextBitsInfo = sdd1_read(thisptr->machine(), first_byte) & 0x30;
326   thisptr->bit_number = 0;
306   m_bitplanesInfo = ROM[SSD1_ADD(first_byte)] & 0xc0;
307   m_contextBitsInfo = ROM[SSD1_ADD(first_byte)] & 0x30;
308   m_bit_number = 0;
327309   for (i = 0; i < 8; i++)
328310   {
329      thisptr->prevBitplaneBits[i] = 0;
311      m_prevBitplaneBits[i] = 0;
330312   }
331   switch(thisptr->bitplanesInfo)
313   switch (m_bitplanesInfo)
332314   {
333315      case 0x00:
334         thisptr->currBitplane = 1;
316         m_currBitplane = 1;
335317         break;
336318      case 0x40:
337         thisptr->currBitplane = 7;
319         m_currBitplane = 7;
338320         break;
339321      case 0x80:
340         thisptr->currBitplane = 3;
322         m_currBitplane = 3;
341323         break;
342324   }
343325}
344326
345static UINT8 SDD1_CM_getBit(SDD1_CM* thisptr)
327UINT8 SDD1_CM::CM_getBit(UINT8 *ROM, UINT32 *mmc)
346328{
347329   UINT8 currContext;
348330   UINT16 *context_bits;
349331   UINT8 bit = 0;
350332
351   switch (thisptr->bitplanesInfo)
333   switch (m_bitplanesInfo)
352334   {
353335      case 0x00:
354         thisptr->currBitplane ^= 0x01;
336         m_currBitplane ^= 0x01;
355337         break;
356338      case 0x40:
357         thisptr->currBitplane ^= 0x01;
358         if (!(thisptr->bit_number & 0x7f))
359         {
360            thisptr->currBitplane = ((thisptr->currBitplane + 2) & 0x07);
361         }
339         m_currBitplane ^= 0x01;
340         if (!(m_bit_number & 0x7f))
341            m_currBitplane = ((m_currBitplane + 2) & 0x07);
362342         break;
363343      case 0x80:
364         thisptr->currBitplane ^= 0x01;
365         if (!(thisptr->bit_number & 0x7f))
366         {
367            thisptr->currBitplane ^= 0x02;
368         }
344         m_currBitplane ^= 0x01;
345         if (!(m_bit_number & 0x7f))
346            m_currBitplane ^= 0x02;
369347         break;
370348      case 0xc0:
371         thisptr->currBitplane = thisptr->bit_number & 0x07;
349         m_currBitplane = m_bit_number & 0x07;
372350         break;
373351   }
374352
375   context_bits = &(thisptr->prevBitplaneBits)[thisptr->currBitplane];
353   context_bits = &(m_prevBitplaneBits)[m_currBitplane];
376354
377   currContext = (thisptr->currBitplane & 0x01) << 4;
378   switch (thisptr->contextBitsInfo)
355   currContext = (m_currBitplane & 0x01) << 4;
356   switch (m_contextBitsInfo)
379357   {
380358      case 0x00:
381359         currContext |= ((*context_bits & 0x01c0) >> 5) | (*context_bits & 0x0001);
r20880r20881
391369         break;
392370   }
393371
394   bit = SDD1_PEM_getBit(thisptr->PEM, currContext);
372   bit = m_PEM->PEM_getBit(ROM, mmc, currContext);
395373
396374   *context_bits <<= 1;
397375   *context_bits |= bit;
398376
399   thisptr->bit_number++;
377   m_bit_number++;
400378
401379   return bit;
402380}
r20880r20881
404382class SDD1_OL
405383{
406384public:
407   SDD1_OL(running_machine& machine, SDD1_CM* associatedCM)
408      : CM(associatedCM),
409         m_machine(machine) { }
385   SDD1_OL(SDD1_CM* associatedCM)
386      : m_CM(associatedCM) { }
410387
411   running_machine &machine() const { return m_machine; }
388   UINT8 m_bitplanesInfo;
389   UINT16 m_length;
390   UINT8* m_buffer;
391   SDD1_CM* m_CM;
412392
413   UINT8 bitplanesInfo;
414   UINT16 length;
415   UINT8* buffer;
416   SDD1_CM* CM;
417
418private:
419   running_machine& m_machine;
393   void OL_prepareDecomp(UINT8 *ROM, UINT32 *mmc, UINT32 first_byte, UINT16 out_len, UINT8 *out_buf);
394   void OL_launch(UINT8 *ROM, UINT32 *mmc);
420395} ;
421396
422static void SDD1_OL_prepareDecomp(SDD1_OL* thisptr, UINT32 first_byte, UINT16 out_len, UINT8 *out_buf)
397void SDD1_OL::OL_prepareDecomp(UINT8 *ROM, UINT32 *mmc, UINT32 first_byte, UINT16 out_len, UINT8 *out_buf)
423398{
424   thisptr->bitplanesInfo = sdd1_read(thisptr->machine(), first_byte) & 0xc0;
425   thisptr->length = out_len;
426   thisptr->buffer = out_buf;
399   m_bitplanesInfo = ROM[SSD1_ADD(first_byte)] & 0xc0;
400   m_length = out_len;
401   m_buffer = out_buf;
427402}
428403
429static void SDD1_OL_launch(SDD1_OL* thisptr)
404void SDD1_OL::OL_launch(UINT8 *ROM, UINT32 *mmc)
430405{
431406   UINT8 i;
432407   UINT8 register1 = 0, register2 = 0;
433408
434   switch(thisptr->bitplanesInfo)
409   switch (m_bitplanesInfo)
435410   {
436411      case 0x00:
437412      case 0x40:
r20880r20881
439414         i = 1;
440415         do
441416         {   // if length == 0, we output 2^16 bytes
442            if(!i)
417            if (!i)
443418            {
444               *(thisptr->buffer++) = register2;
419               *(m_buffer++) = register2;
445420               i = ~i;
446421            }
447422            else
448423            {
449               for(register1 = register2 = 0, i = 0x80; i; i >>= 1)
424               for (register1 = register2 = 0, i = 0x80; i; i >>= 1)
450425               {
451                  if(SDD1_CM_getBit(thisptr->CM))
452                  {
426                  if (m_CM->CM_getBit(ROM, mmc))
453427                     register1 |= i;
454                  }
455                  if(SDD1_CM_getBit(thisptr->CM))
456                  {
428
429                  if (m_CM->CM_getBit(ROM, mmc))
457430                     register2 |= i;
458                  }
459431               }
460               *(thisptr->buffer++) = register1;
432               *(m_buffer++) = register1;
461433            }
462         } while(--(thisptr->length));
434         } while (--(m_length));
463435         break;
464436      case 0xc0:
465437         do
466438         {
467            for(register1 = 0, i = 0x01; i; i <<= 1)
439            for (register1 = 0, i = 0x01; i; i <<= 1)
468440            {
469               if(SDD1_CM_getBit(thisptr->CM))
441               if (m_CM->CM_getBit(ROM, mmc))
470442               {
471443                  register1 |= i;
472444               }
473445            }
474            *(thisptr->buffer++) = register1;
475         } while(--(thisptr->length));
446            *(m_buffer++) = register1;
447         } while (--(m_length));
476448         break;
477449   }
478450}
r20880r20881
484456
485457   running_machine &machine() const { return m_machine; }
486458
487   SDD1_IM* IM;
488   SDD1_GCD* GCD;
489   SDD1_BG* BG0;   SDD1_BG* BG1;   SDD1_BG* BG2;   SDD1_BG* BG3;
490   SDD1_BG* BG4;   SDD1_BG* BG5;   SDD1_BG* BG6;   SDD1_BG* BG7;
491   SDD1_PEM* PEM;
492   SDD1_CM* CM;
493   SDD1_OL* OL;
459   SDD1_IM* m_IM;
460   SDD1_GCD* m_GCD;
461   SDD1_BG* m_BG0;   SDD1_BG* m_BG1;   SDD1_BG* m_BG2;   SDD1_BG* m_BG3;
462   SDD1_BG* m_BG4;   SDD1_BG* m_BG5;   SDD1_BG* m_BG6;   SDD1_BG* m_BG7;
463   SDD1_PEM* m_PEM;
464   SDD1_CM* m_CM;
465   SDD1_OL* m_OL;
494466
467   void SDD1emu_decompress(UINT8 *ROM, UINT32 *mmc, UINT32 in_buf, UINT16 out_len, UINT8 *out_buf);
468
495469private:
496470   running_machine& m_machine;
497471};
r20880r20881
499473SDD1emu::SDD1emu(running_machine &machine)
500474   : m_machine(machine)
501475{
502   IM = auto_alloc(machine, SDD1_IM(machine));
503   GCD = auto_alloc(machine, SDD1_GCD(machine, IM));
504   BG0 = auto_alloc(machine, SDD1_BG(machine, GCD, 0));
505   BG1 = auto_alloc(machine, SDD1_BG(machine, GCD, 1));
506   BG2 = auto_alloc(machine, SDD1_BG(machine, GCD, 2));
507   BG3 = auto_alloc(machine, SDD1_BG(machine, GCD, 3));
508   BG4 = auto_alloc(machine, SDD1_BG(machine, GCD, 4));
509   BG5 = auto_alloc(machine, SDD1_BG(machine, GCD, 5));
510   BG6 = auto_alloc(machine, SDD1_BG(machine, GCD, 6));
511   BG7 = auto_alloc(machine, SDD1_BG(machine, GCD, 7));
512   PEM = auto_alloc(machine, SDD1_PEM(machine, BG0, BG1, BG2, BG3,
513                                 BG4, BG5, BG6, BG7));
514   CM = auto_alloc(machine, SDD1_CM(machine, PEM));
515   OL = auto_alloc(machine, SDD1_OL(machine, CM));
476   m_IM = auto_alloc(machine, SDD1_IM());
477   m_GCD = auto_alloc(machine, SDD1_GCD(m_IM));
478   m_BG0 = auto_alloc(machine, SDD1_BG(m_GCD, 0));
479   m_BG1 = auto_alloc(machine, SDD1_BG(m_GCD, 1));
480   m_BG2 = auto_alloc(machine, SDD1_BG(m_GCD, 2));
481   m_BG3 = auto_alloc(machine, SDD1_BG(m_GCD, 3));
482   m_BG4 = auto_alloc(machine, SDD1_BG(m_GCD, 4));
483   m_BG5 = auto_alloc(machine, SDD1_BG(m_GCD, 5));
484   m_BG6 = auto_alloc(machine, SDD1_BG(m_GCD, 6));
485   m_BG7 = auto_alloc(machine, SDD1_BG(m_GCD, 7));
486   m_PEM = auto_alloc(machine, SDD1_PEM(m_BG0, m_BG1, m_BG2, m_BG3,
487                               m_BG4, m_BG5, m_BG6, m_BG7));
488   m_CM = auto_alloc(machine, SDD1_CM(m_PEM));
489   m_OL = auto_alloc(machine, SDD1_OL(m_CM));
516490}
517491
518static void SDD1emu_decompress(SDD1emu* thisptr, UINT32 in_buf, UINT16 out_len, UINT8 *out_buf)
492void SDD1emu::SDD1emu_decompress(UINT8 *ROM, UINT32 *mmc, UINT32 in_buf, UINT16 out_len, UINT8 *out_buf)
519493{
520   SDD1_IM_prepareDecomp(thisptr->IM, in_buf);
521   SDD1_BG_prepareDecomp(thisptr->BG0);
522   SDD1_BG_prepareDecomp(thisptr->BG1);
523   SDD1_BG_prepareDecomp(thisptr->BG2);
524   SDD1_BG_prepareDecomp(thisptr->BG3);
525   SDD1_BG_prepareDecomp(thisptr->BG4);
526   SDD1_BG_prepareDecomp(thisptr->BG5);
527   SDD1_BG_prepareDecomp(thisptr->BG6);
528   SDD1_BG_prepareDecomp(thisptr->BG7);
529   SDD1_PEM_prepareDecomp(thisptr->PEM);
530   SDD1_CM_prepareDecomp(thisptr->CM, in_buf);
531   SDD1_OL_prepareDecomp(thisptr->OL, in_buf, out_len, out_buf);
494   m_IM->IM_prepareDecomp(in_buf);
495   m_BG0->BG_prepareDecomp();
496   m_BG1->BG_prepareDecomp();
497   m_BG2->BG_prepareDecomp();
498   m_BG3->BG_prepareDecomp();
499   m_BG4->BG_prepareDecomp();
500   m_BG5->BG_prepareDecomp();
501   m_BG6->BG_prepareDecomp();
502   m_BG7->BG_prepareDecomp();
503   m_PEM->PEM_prepareDecomp();
504   m_CM->CM_prepareDecomp(ROM, mmc, in_buf);
505   m_OL->OL_prepareDecomp(ROM, mmc, in_buf, out_len, out_buf);
532506
533   SDD1_OL_launch(thisptr->OL);
507   m_OL->OL_launch(ROM, mmc);
534508}
535509
536510struct snes_sdd1_t
r20880r20881
559533
560534static void sdd1_init(running_machine& machine)
561535{
562   UINT8 i;
563
564536   snes_sdd1.sdd1_enable = 0x00;
565537   snes_sdd1.xfer_enable = 0x00;
566538
r20880r20881
569541   snes_sdd1.mmc[2] = 2 << 20;
570542   snes_sdd1.mmc[3] = 3 << 20;
571543
572   for(i = 0; i < 8; i++)
544   for (int i = 0; i < 8; i++)
573545   {
574546      snes_sdd1.dma[i].addr = 0;
575547      snes_sdd1.dma[i].size = 0;
r20880r20881
585557{
586558   addr &= 0xffff;
587559
588   if((addr & 0x4380) == 0x4300)
560   if ((addr & 0x4380) == 0x4300)
589561   {
590562      return snes_r_io(space, addr & 0x7f);
591563   }
r20880r20881
609581{
610582   addr &= 0xffff;
611583
612   if((addr & 0x4380) == 0x4300)
584   if ((addr & 0x4380) == 0x4300)
613585   {
614586      UINT8 channel = (addr >> 4) & 7;
615587      switch(addr & 15)
r20880r20881
664636{
665637   unsigned char *ROM = machine.root_device().memregion("cart")->base();
666638
667   if(snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable)
639   if (snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable)
668640   {
669641      // at least one channel has S-DD1 decompression enabled...
670      UINT32 i;
671      for(i = 0; i < 8; i++)
642      for (int i = 0; i < 8; i++)
672643      {
673         if(snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable & (1 << i))
644         if (snes_sdd1.sdd1_enable & snes_sdd1.xfer_enable & (1 << i))
674645         {
675646            // S-DD1 always uses fixed transfer mode, so address will not change during transfer
676            if((addr + 0xc00000) == snes_sdd1.dma[i].addr)
647            if ((addr + 0xc00000) == snes_sdd1.dma[i].addr)
677648            {
678649               UINT8 data;
679               if(!snes_sdd1.buffer.ready)
650               if (!snes_sdd1.buffer.ready)
680651               {
681652                  UINT8 temp;
682653                  // first byte read for channel performs full decompression.
r20880r20881
688659                  // so temporarily disable decompression mode for decompress() call.
689660                  temp = snes_sdd1.sdd1_enable;
690661                  snes_sdd1.sdd1_enable = 0;
691                  SDD1emu_decompress(snes_sdd1.sdd1emu, addr, snes_sdd1.buffer.size, snes_sdd1.buffer.data);
662                  snes_sdd1.sdd1emu->SDD1emu_decompress(ROM, snes_sdd1.mmc, addr, snes_sdd1.buffer.size, snes_sdd1.buffer.data);
692663                  snes_sdd1.sdd1_enable = temp;
693664
694665                  snes_sdd1.buffer.ready = 1;
r20880r20881
696667
697668               // fetch a decompressed byte; once buffer is depleted, disable channel and invalidate buffer
698669               data = snes_sdd1.buffer.data[(UINT16)snes_sdd1.buffer.offset++];
699               if(snes_sdd1.buffer.offset >= snes_sdd1.buffer.size)
670               if (snes_sdd1.buffer.offset >= snes_sdd1.buffer.size)
700671               {
701672                  snes_sdd1.buffer.ready = 0;
702673                  snes_sdd1.xfer_enable &= ~(1 << i);

Previous 199869 Revisions Next


© 1997-2024 The MAME Team