Previous 15994 Revisions Next

r7661 Sunday 28th March, 2010 at 17:53:19 UTC by Fabio Priuli
[SNES] wip:
- enforced abus validity check on dma reads
- fixed possible tile and vram out of bounds bugs
[src/mame/includes]snes.h
[src/mame/machine]snes.c
[src/mame/video]snes.c

src/mame/machine/snes.c
r7660r7661
25352535
25362536*************************************/
25372537
2538static int dma_abus_valid( UINT32 address )
2538INLINE int dma_abus_valid( UINT32 address )
25392539{
25402540   if((address & 0x40ff00) == 0x2100) return 0;  //$[00-3f|80-bf]:[2100-21ff]
25412541   if((address & 0x40fe00) == 0x4000) return 0;  //$[00-3f|80-bf]:[4000-41ff]
r7660r7661
25452545   return 1;
25462546}
25472547
2548INLINE UINT8 snes_abus_read( const address_space *space, UINT32 abus )
2549{
2550   if (!dma_abus_valid(abus))
2551      return 0;
2552
2553   return memory_read_byte(space, abus);
2554}
2555
25482556INLINE void snes_dma_transfer( const address_space *space, UINT8 dma, UINT32 abus, UINT16 bbus )
25492557{
25502558   snes_state *state = (snes_state *)space->machine->driver_data;
2551   UINT32 src, dst;
25522559
25532560   if (state->dma_channel[dma].dmap & 0x80)   /* PPU->CPU */
25542561   {
r7660r7661
25642571         if (!dma_abus_valid(abus))
25652572            return;
25662573
2567         src = bbus; dst = abus;
2574         memory_write_byte(space, abus, memory_read_byte(space, bbus));
2575         return;
25682576      }
25692577   }
25702578   else                           /* CPU->PPU */
r7660r7661
25782586      }
25792587      else
25802588      {
2581         src = abus; dst = bbus;
2589         memory_write_byte(space, bbus, snes_abus_read(space, abus));
2590         return;
25822591      }
25832592   }
2584
2585   memory_write_byte(space, dst, memory_read_byte(space, src));
25862593}
25872594
25882595/* WIP: These have the advantage to automatically update the address, but then we would need to
r7660r7661
25992606   return (state->dma_channel[dma].ibank << 16) | (state->dma_channel[dma].trans_size++);
26002607}
26012608
2602static UINT8 snes_hdma_read( const address_space *space, UINT32 abus )
2609INLINE int is_last_active_channel( running_machine *machine, int dma )
26032610{
2604   if (!dma_abus_valid(abus))
2605      return 0;
2606
2607   return memory_read_byte(space, abus);
2608}
2609
2610static int is_last_active_channel( running_machine *machine, int dma )
2611{
26122611   snes_state *state = (snes_state *)machine->driver_data;
26132612   int i;
26142613
r7660r7661
26272626   snes_state *state = (snes_state *)space->machine->driver_data;
26282627   UINT32 abus = snes_get_hdma_addr(space->machine, dma);
26292628
2630   state->dma_channel[dma].hdma_line_counter = snes_hdma_read(space, abus);
2629   state->dma_channel[dma].hdma_line_counter = snes_abus_read(space, abus);
26312630
26322631   if (state->dma_channel[dma].dmap & 0x40)
26332632   {
r7660r7661
26362635      otherwise expected */
26372636
26382637      abus = snes_get_hdma_addr(space->machine, dma);
2639      state->dma_channel[dma].trans_size = snes_hdma_read(space, abus) << 8;
2638      state->dma_channel[dma].trans_size = snes_abus_read(space, abus) << 8;
26402639
26412640      if (state->dma_channel[dma].hdma_line_counter || !is_last_active_channel(space->machine, dma))
26422641      {
26432642         // we enter here if we have more transfers to be done or if there are other active channels after this one
26442643         abus = snes_get_hdma_addr(space->machine, dma);
26452644         state->dma_channel[dma].trans_size >>= 8;
2646         state->dma_channel[dma].trans_size |= snes_hdma_read(space, abus) << 8;
2645         state->dma_channel[dma].trans_size |= snes_abus_read(space, abus) << 8;
26472646      }
26482647   }
26492648
src/mame/includes/snes.h
r7660r7661
2323#define DOTCLK_NTSC   (MCLK_NTSC/4)
2424#define DOTCLK_PAL   (MCLK_PAL/4)
2525
26#define SNES_LAYER_DEBUG  1
26#define SNES_LAYER_DEBUG 
2727
2828/* Debug definitions */
2929#ifdef MAME_DEBUG
src/mame/video/snes.c
r7660r7661
298298/*****************************************
299299 * snes_draw_tile()
300300 *
301 * Get pixel color with variable bit planes
302 * from snes_vram and draw the tiles by
303 * calling the appropriate routine
301 * Draw 8 pixels from the expected tile
302 * by reading the color planes from vram
303 * and by calling the appropriate routine
304 * (depending on layer and resolution)
304305 *****************************************/
305306
306INLINE void snes_draw_tile( UINT8 planes, UINT8 layer, UINT16 tileaddr, INT16 x, int tile_size, UINT8 priority, UINT8 flip, UINT8 direct_colors, UINT16 pal, UINT8 hires )
307INLINE void snes_draw_tile( UINT8 planes, UINT8 layer, UINT32 tileaddr, INT16 x, UINT8 priority, UINT8 flip, UINT8 direct_colors, UINT16 pal, UINT8 hires )
307308{
308309   UINT8 plane[8];
309   INT16 ii, jj, start_pixel;
310   int tile, x_mos;
310   INT16 ii, jj;
311   int x_mos;
311312
312   start_pixel = x;
313   for (ii = 0; ii < planes / 2; ii++)
314   {
315      plane[2 * ii + 0] = snes_vram[(tileaddr + 16 * ii + 0) % SNES_VRAM_SIZE];
316      plane[2 * ii + 1] = snes_vram[(tileaddr + 16 * ii + 1) % SNES_VRAM_SIZE];
317   }
313318
314   /* if 8x8 tile we draw once; if 16x16 we draw twice */
315   for (tile = 0; tile < (1 << tile_size); tile++)
319   for (ii = x; ii < (x + 8); ii++)
316320   {
317      for (ii = 0; ii < planes / 2; ii++)
318      {
319         plane[2 * ii] = snes_vram[tileaddr + 16 * ii];
320         plane[2 * ii + 1] = snes_vram[tileaddr + 16 * ii + 1];
321      }
321      UINT8 colour = 0;
322      UINT8 mosaic = snes_ppu.layer[layer].mosaic_enabled;
322323
323      for (ii = x; ii < (x + 8); ii++)
324      {
325         UINT8 colour = 0;
326         UINT8 mosaic = snes_ppu.layer[layer].mosaic_enabled;
327
328324#ifdef SNES_LAYER_DEBUG
329         if (debug_options.mosaic_disabled)
330            mosaic = 0;
325      if (debug_options.mosaic_disabled)
326         mosaic = 0;
331327#endif /* SNES_LAYER_DEBUG */
332328
333         if (flip)
329      if (flip)
330      {
331         for (jj = 0; jj < planes; jj++)
332            colour |= BIT(plane[jj], ii - x) ? (1 << jj) : 0;
333      }
334      else
335      {
336         for (jj = 0; jj < planes; jj++)
337            colour |= BIT(plane[jj], 7 - (ii - x)) ? (1 << jj) : 0;
338      }
339
340      if (layer == SNES_OAM)
341         snes_draw_oamtile(ii, colour, pal, priority);
342      else if (!hires)
343      {
344         if (mosaic)   
334345         {
335            for (jj = 0; jj < planes; jj++)
336               colour |= BIT(plane[jj], ii - x) ? (1 << jj) : 0;
346            for (x_mos = 0; x_mos < (snes_ppu.mosaic_size + 1); x_mos++)
347               snes_draw_bgtile_lores(layer, ii + x_mos, colour, pal, direct_colors, priority);
348            ii += x_mos - 1;
337349         }
338350         else
351            snes_draw_bgtile_lores(layer, ii, colour, pal, direct_colors, priority);
352      }
353      else /* hires */
354      {
355         if (mosaic)   
339356         {
340            for (jj = 0; jj < planes; jj++)
341               colour |= BIT(plane[jj], 7 - (ii - x)) ? (1 << jj) : 0;
357            for (x_mos = 0; x_mos < (snes_ppu.mosaic_size + 1); x_mos++)
358               snes_draw_bgtile_hires(layer, ii + x_mos, colour, pal, direct_colors, priority);
359            ii += x_mos - 1;
342360         }
343
344         if (layer == SNES_OAM)
345            snes_draw_oamtile(ii, colour, pal, priority);
346         else if (!hires)
347         {
348            if (mosaic)   
349            {
350               for (x_mos = 0; x_mos < (snes_ppu.mosaic_size + 1); x_mos++)
351                  snes_draw_bgtile_lores(layer, ii + x_mos, colour, pal, direct_colors, priority);
352               ii += x_mos - 1;
353            }
354            else
355               snes_draw_bgtile_lores(layer, ii, colour, pal, direct_colors, priority);
356         }
357         else /* hires */
358         {
359            if (mosaic)   
360            {
361               for (x_mos = 0; x_mos < (snes_ppu.mosaic_size + 1); x_mos++)
362                  snes_draw_bgtile_hires(layer, ii + x_mos, colour, pal, direct_colors, priority);
363               ii += x_mos - 1;
364            }
365            else
366               snes_draw_bgtile_hires(layer, ii, colour, pal, direct_colors, priority);
367         }
361         else
362            snes_draw_bgtile_hires(layer, ii, colour, pal, direct_colors, priority);
368363      }
369
370      /* adjust tileaddr & x in case we have to draw again (for 16x16 tiles) */
371      if (!flip)
372         tileaddr += (8 * planes);
373      else
374         tileaddr -= (8 * planes);
375
376      x += 8;
377364   }
378365}
379366
r7660r7661
425412
426413INLINE void snes_update_line( UINT16 curline, UINT8 layer, UINT8 priority_b, UINT8 priority_a, UINT8 color_depth, UINT8 hires, UINT8 offset_per_tile, UINT8 direct_colors )
427414{
428   UINT32 tmap, tile, xoff, yoff;
429   UINT32 charaddr;
430   UINT16 ii = 0, vflip, hflip, pal, pal_direct;
415   UINT32 tmap, tile, xoff, yoff, charaddr, addr;
416   UINT16 ii = 0, vflip, hflip, pal, pal_direct, tilemap;
417   UINT8 xscroll, priority;
431418   INT8 yscroll;
432   UINT8 xscroll;
433   UINT8 priority;
434   UINT32 addr;
435   UINT16 tilemap;
436   /* scrolling */
419   int tile_incr = 0;
437420   UINT16 opt_bit = (layer == SNES_BG1) ? 13 : (layer == SNES_BG2) ? 14 : 0;
438421   UINT8 tile_size;
439422   /* variables depending on color_depth */
r7660r7661
494477            case SNES_OPT_MODE6:
495478               haddr = snes_get_tmap_addr(SNES_BG3, snes_ppu.layer[SNES_BG3].tile_size, snes_ppu.layer[SNES_BG3].tilemap << 9, (opt_x - 8) + ((snes_ppu.layer[SNES_BG3].hoffs & 0x3ff) & ~7), (snes_ppu.layer[SNES_BG3].voffs & 0x3ff));
496479               vaddr = snes_get_tmap_addr(SNES_BG3, snes_ppu.layer[SNES_BG3].tile_size, snes_ppu.layer[SNES_BG3].tilemap << 9, (opt_x - 8) + ((snes_ppu.layer[SNES_BG3].hoffs & 0x3ff) & ~7), (snes_ppu.layer[SNES_BG3].voffs & 0x3ff) + 8);
497               hval = snes_vram[haddr] | (snes_vram[haddr + 1] << 8);
498               vval = snes_vram[vaddr] | (snes_vram[vaddr + 1] << 8);
480               hval = snes_vram[haddr % SNES_VRAM_SIZE] | (snes_vram[(haddr + 1) % SNES_VRAM_SIZE] << 8);
481               vval = snes_vram[vaddr % SNES_VRAM_SIZE] | (snes_vram[(vaddr + 1) % SNES_VRAM_SIZE] << 8);
499482               if (BIT(hval, opt_bit))
500483                  xpos = opt_x + (hval & ~7);
501484               if (BIT(vval, opt_bit))
r7660r7661
503486               break;
504487            case SNES_OPT_MODE4:
505488               haddr = snes_get_tmap_addr(SNES_BG3, snes_ppu.layer[SNES_BG3].tile_size, snes_ppu.layer[SNES_BG3].tilemap << 9, (opt_x - 8) + ((snes_ppu.layer[SNES_BG3].hoffs & 0x3ff) & ~7), (snes_ppu.layer[SNES_BG3].voffs & 0x3ff));
506               hval = snes_vram[haddr] | (snes_vram[haddr + 1] << 8);
489               hval = snes_vram[haddr % SNES_VRAM_SIZE] | (snes_vram[(haddr + 1) % SNES_VRAM_SIZE] << 8);
507490               if (BIT(hval, opt_bit))
508491               {
509492                  if (!BIT(hval, 15))
r7660r7661
527510          ppp  = Tile palette. The number of entries in the palette depends on the Mode and the BG.
528511          cccccccccc = Tile number.
529512        */
530      tilemap = snes_vram[addr] | (snes_vram[addr + 1] << 8);
513      tilemap = snes_vram[addr % SNES_VRAM_SIZE] | (snes_vram[(addr + 1) % SNES_VRAM_SIZE] << 8);
531514      vflip = BIT(tilemap, 15);
532515      hflip = BIT(tilemap, 14);
533516      priority = BIT(tilemap, 13) ? priority_a : priority_b;
r7660r7661
566549         yscroll &= 7;
567550
568551      if (vflip)
569         yscroll = -yscroll + 7;
552         yscroll = 7 - yscroll;
570553
571554      yscroll <<= 1;
572555
573      if (hires)
556      /* if we have to draw 16 pixels, set tile_incr and adjust tile for horizontal flip */
557      if (tile_size || hires)
574558      {
575         /* adjust tile for horizontal flip */
576559         if (hflip)
560         {
577561            tile += 1;
562            tile_incr = -1;   // next 8 pixels from previous tile (because of hflip)
563         }
564         else
565            tile_incr = 1;   // next 8 pixels from next tile
566      }
578567
568      if (hires)
569      {
579570         /* draw 16 pixels (the routine will automatically send half of them to the mainscreen scanline and half to the subscreen one) */
580         snes_draw_tile(color_planes, layer, charaddr + (tile  * 8 * color_planes) + yscroll, (ii - xscroll) * 2, 1, priority, hflip, direct_colors, direct_colors ? pal_direct : pal, hires);
571         snes_draw_tile(color_planes, layer, charaddr + (((tile + 0)         & 0x3ff) * 8 * color_planes) + yscroll, (ii - xscroll) * 2,     priority, hflip, direct_colors, direct_colors ? pal_direct : pal, hires);
572         snes_draw_tile(color_planes, layer, charaddr + (((tile + tile_incr) & 0x3ff) * 8 * color_planes) + yscroll, (ii - xscroll) * 2 + 8, priority, hflip, direct_colors, direct_colors ? pal_direct : pal, hires);
581573         ii += 8;
582574      }
583575      else
584576      {
585         /* adjust tile for horizontal flip */
586         if (tile_size && hflip)
587            tile += 1;
577         snes_draw_tile(color_planes, layer, charaddr + ((tile & 0x3ff) * 8 * color_planes) + yscroll, ii - xscroll, priority, hflip, direct_colors, direct_colors ? pal_direct : pal, hires);
578         ii += 8;
588579
589         snes_draw_tile(color_planes, layer, charaddr + (tile  * 8 * color_planes) + yscroll, ii - xscroll, tile_size, priority, hflip, direct_colors, direct_colors ? pal_direct : pal, hires);
590         ii += (8 << tile_size);
580         if (tile_size)
581         {
582            snes_draw_tile(color_planes, layer, charaddr + (((tile + tile_incr) & 0x3ff) * 8 * color_planes) + yscroll, ii - xscroll, priority, hflip, direct_colors, direct_colors ? pal_direct : pal, hires);
583            ii += 8;
584         }
591585      }
592586   }
593587}
r7660r7661
698692         case 0x01:   /* Repeat if outside screen area */
699693            tx &= 0x3ff;
700694            ty &= 0x3ff;
701            tiled = snes_vram[(((tx >> 3) & 0x7f) + (((ty >> 3) & 0x7f) * 128)) * 2] << 7;
702            colour = snes_vram[tiled + ((tx & 0x07) * 2) + ((ty & 0x07) * 16) + 1];
695            tiled = snes_vram[((((tx >> 3) & 0x7f) + (((ty >> 3) & 0x7f) * 128)) * 2) % SNES_VRAM_SIZE] << 7;
696            colour = snes_vram[(tiled + ((tx & 0x07) * 2) + ((ty & 0x07) * 16) + 1) % SNES_VRAM_SIZE];
703697            break;
704698         case 0x02:   /* Single colour backdrop screen if outside screen area */
705699            if ((tx >= 0) && (tx < 1024) && (ty >= 0) && (ty < 1024))
706700            {
707               tiled = snes_vram[(((tx >> 3) & 0x7f) + (((ty >> 3) & 0x7f) * 128)) * 2] << 7;
708               colour = snes_vram[tiled + ((tx & 0x07) * 2) + ((ty & 0x07) * 16) + 1];
701               tiled = snes_vram[((((tx >> 3) & 0x7f) + (((ty >> 3) & 0x7f) * 128)) * 2) % SNES_VRAM_SIZE] << 7;
702               colour = snes_vram[(tiled + ((tx & 0x07) * 2) + ((ty & 0x07) * 16) + 1) % SNES_VRAM_SIZE];
709703            }
710704            else
711705               colour = 0;
712706            break;
713707         case 0x03:   /* Character 0x00 repeat if outside screen area */
714708            if ((tx >= 0) && (tx < 1024) && (ty >= 0) && (ty < 1024))
715               tiled = snes_vram[(((tx >> 3) & 0x7f) + (((ty >> 3) & 0x7f) * 128)) * 2] << 7;
709               tiled = snes_vram[((((tx >> 3) & 0x7f) + (((ty >> 3) & 0x7f) * 128)) * 2) % SNES_VRAM_SIZE] << 7;
716710            else
717711               tiled = 0;
718712
719            colour = snes_vram[tiled + ((tx & 0x07) * 2) + ((ty & 0x07) * 16) + 1];
713            colour = snes_vram[(tiled + ((tx & 0x07) * 2) + ((ty & 0x07) * 16) + 1) % SNES_VRAM_SIZE];
720714            break;
721715      }
722716
r7660r7661
10271021      if (vflip)
10281022      {
10291023         ys = height - ys - 1;
1030         line = (-1 * line) + 7;
1024         line = 7 - line;
10311025      }
10321026      line <<= 1;
10331027      tile <<= 5;
r7660r7661
11151109      }
11161110#endif /* SNES_LAYER_DEBUG */
11171111
1118      /* OAM tiles have fixed planes (4), fixed tile_size (8x8), no direct color and no hires, but otherwise work the same as BG ones */
1119      snes_draw_tile(4, SNES_OAM, charaddr + oam_tilelist[tile].tileaddr, oam_tilelist[tile].x, 0, pri, oam_tilelist[tile].hflip, 0, oam_tilelist[tile].pal, 0);
1112      /* OAM tiles have fixed planes (4), no direct color and no hires, but otherwise work the same as BG ones */
1113      snes_draw_tile(4, SNES_OAM, charaddr + oam_tilelist[tile].tileaddr, oam_tilelist[tile].x, pri, oam_tilelist[tile].hflip, 0, oam_tilelist[tile].pal, 0);
11201114   }
11211115}
11221116

Previous 15994 Revisions Next


© 1998-2018 The MESS Team