Previous 15994 Revisions Next

r7670 Tuesday 30th March, 2010 at 22:54:25 UTC by Fabio Priuli
[NES] sync'd with MAME
[src/mame/video]ppu2c0x.c ppu2c0x.h

src/mame/video/ppu2c0x.c
r7669r7670
3333***************************************************************************/
3434
3535/* constant definitions */
36#define VISIBLE_SCREEN_WIDTH   (32*8)   /* Visible screen width */
37#define VISIBLE_SCREEN_HEIGHT   (30*8)   /* Visible screen height */
36#define VISIBLE_SCREEN_WIDTH        (32*8)   /* Visible screen width */
37#define VISIBLE_SCREEN_HEIGHT        (30*8)   /* Visible screen height */
3838#define VIDEOMEM_SIZE         0x1000   /* videomem size */
3939#define VIDEOMEM_PAGE_SIZE      0x400   /* videomem page size */
4040#define SPRITERAM_SIZE         0x100   /* spriteram size */
r7669r7670
7777    TYPE DEFINITIONS
7878***************************************************************************/
7979
80/* our chip state */
81typedef struct
80
81typedef struct _ppu2c0x_state  ppu2c0x_state;
82struct _ppu2c0x_state
8283{
83   bitmap_t            *bitmap;            /* target bitmap */
84   UINT8               *spriteram;            /* sprite ram */
85   pen_t               *colortable;         /* color table modified at run time */
86   pen_t               *colortable_mono;      /* monochromatic color table modified at run time */
87   emu_timer            *scanline_timer;      /* scanline timer */
88   emu_timer            *hblank_timer;         /* hblank period at end of each scanline */
89   emu_timer            *nmi_timer;            /* NMI timer */
90   int                  scanline;            /* scanline count */
91   ppu2c0x_scanline_cb      scanline_callback_proc;   /* optional scanline callback */
92   ppu2c0x_hblank_cb      hblank_callback_proc;   /* optional hblank callback */
93   ppu2c0x_vidaccess_cb   vidaccess_callback_proc;/* optional video access callback */
94   int                  regs[PPU_MAX_REG];      /* registers */
95   int                  refresh_data;         /* refresh-related */
96   int                  refresh_latch;         /* refresh-related */
97   int                  x_fine;               /* refresh-related */
98   int                  toggle;               /* used to latch hi-lo scroll */
99   int                  add;               /* vram increment amount */
100   int                  videomem_addr;         /* videomem address pointer */
101   int                  addr_latch;            /* videomem address latch */
102   int                  data_latch;            /* latched videomem data */
103   int                  buffered_data;
104   int                  tile_page;            /* current tile page */
105   int                  sprite_page;         /* current sprite page */
106   int                  back_color;            /* background color */
107   UINT8               palette_ram[0x20];      /* shouldn't be in main memory! */
108   int                  scan_scale;            /* scan scale */
109   int                  scanlines_per_frame;   /* number of scanlines per frame */
110   int                  mirror_state;
111   rgb_t               palette[64*4];         /* palette for this chip */
112} ppu2c0x_chip;
84   bitmap_t                    *bitmap;         /* target bitmap */
85   UINT8                       *spriteram;         /* sprite ram */
86   pen_t                       *colortable;         /* color table modified at run time */
87   pen_t                       *colortable_mono;      /* monochromatic color table modified at run time */
88   emu_timer                   *scanline_timer;      /* scanline timer */
89   emu_timer                   *hblank_timer;      /* hblank period at end of each scanline */
90   emu_timer                   *nmi_timer;         /* NMI timer */
91   int                         scanline;         /* scanline count */
92   ppu2c0x_scanline_cb         scanline_callback_proc;   /* optional scanline callback */
93   ppu2c0x_hblank_cb           hblank_callback_proc;   /* optional hblank callback */
94   ppu2c0x_vidaccess_cb        vidaccess_callback_proc;   /* optional video access callback */
95   ppu2c0x_nmi_cb              nmi_callback_proc;      /* nmi access callback from interface */
96   int                         regs[PPU_MAX_REG];      /* registers */
97   int                         refresh_data;         /* refresh-related */
98   int                         refresh_latch;      /* refresh-related */
99   int                         x_fine;            /* refresh-related */
100   int                         toggle;            /* used to latch hi-lo scroll */
101   int                         add;            /* vram increment amount */
102   int                         videomem_addr;      /* videomem address pointer */
103   int                         addr_latch;         /* videomem address latch */
104   int                         data_latch;         /* latched videomem data */
105   int                         buffered_data;
106   int                         tile_page;         /* current tile page */
107   int                         sprite_page;         /* current sprite page */
108   int                         back_color;         /* background color */
109   int                         color_base;
110   UINT8                       palette_ram[0x20];      /* shouldn't be in main memory! */
111   int                         scan_scale;         /* scan scale */
112   int                         scanlines_per_frame;   /* number of scanlines per frame */
113   rgb_t                       palette[64*4];      /* palette for this chip */
114};
113115
114
115116/***************************************************************************
116117    PROTOTYPES
117118***************************************************************************/
r7669r7670
133134    INLINE FUNCTIONS
134135***************************************************************************/
135136
136INLINE ppu2c0x_chip *get_token( running_device *device )
137INLINE ppu2c0x_state *get_token( running_device *device )
137138{
138139   assert(device != NULL);
139140   assert((device->type == PPU_2C02) || (device->type == PPU_2C03B)
140141       || (device->type == PPU_2C04) || (device->type == PPU_2C05)
141142       || (device->type == PPU_2C07));
142   return (ppu2c0x_chip *) device->token;
143   return (ppu2c0x_state *) device->token;
143144}
144145
145146
r7669r7670
179180   /* 16 colors, with 4 luminance levels for each color     */
180181   /* The 16 colors circle around the YUV color space,      */
181182
182   int colorIntensity, colorNum, colorEmphasis;
183   int color_intensity, color_num, color_emphasis;
183184
184185   double R, G, B;
185186
r7669r7670
199200   };
200201
201202   /* Loop through the emphasis modes (8 total) */
202   for (colorEmphasis = 0; colorEmphasis < 8; colorEmphasis ++)
203   for (color_emphasis = 0; color_emphasis < 8; color_emphasis ++)
203204   {
204205      /*
205206        double r_mod = 0.0;
206207        double g_mod = 0.0;
207208        double b_mod = 0.0;
208209
209        switch (colorEmphasis)
210        switch (color_emphasis)
210211        {
211            case 0: r_mod = 1.0; g_mod = 1.0; b_mod = 1.0; break;
212            case 0: r_mod = 1.0; g_mod = 1.0; b_mod = 1.0; break;
212213            case 1: r_mod = 1.24; g_mod = .915; b_mod = .743; break;
213214            case 2: r_mod = .794; g_mod = 1.09; b_mod = .882; break;
214215            case 3: r_mod = .905; g_mod = 1.03; b_mod = 1.28; break;
215            case 4: r_mod = .741; g_mod = .987; b_mod = 1.0; break;
216            case 4: r_mod = .741; g_mod = .987; b_mod = 1.0; break;
216217            case 5: r_mod = 1.02; g_mod = .908; b_mod = .979; break;
217            case 6: r_mod = 1.02; g_mod = .98; b_mod = .653; break;
218            case 7: r_mod = .75; g_mod = .75; b_mod = .75; break;
218            case 6: r_mod = 1.02; g_mod = .98;  b_mod = .653; break;
219            case 7: r_mod = .75;  g_mod = .75;  b_mod = .75;  break;
219220        }
220221        */
221222
222223      /* loop through the 4 intensities */
223      for (colorIntensity = 0; colorIntensity < 4; colorIntensity++)
224      for (color_intensity = 0; color_intensity < 4; color_intensity++)
224225      {
225226         /* loop through the 16 colors */
226         for (colorNum = 0; colorNum < 16; colorNum++)
227         for (color_num = 0; color_num < 16; color_num++)
227228         {
228229            double sat;
229230            double y, u, v;
230231            double rad;
231232
232            switch (colorNum)
233            switch (color_num)
233234            {
234235               case 0:
235236                  sat = 0; rad = 0;
236                  y = brightness[0][colorIntensity];
237                  y = brightness[0][color_intensity];
237238                  break;
238239
239240               case 13:
240241                  sat = 0; rad = 0;
241                  y = brightness[2][colorIntensity];
242                  y = brightness[2][color_intensity];
242243                  break;
243244
244245               case 14:
r7669r7670
248249
249250               default:
250251                  sat = tint;
251                  rad = M_PI * ((colorNum * 30 + hue) / 180.0);
252                  y = brightness[1][colorIntensity];
252                  rad = M_PI * ((color_num * 30 + hue) / 180.0);
253                  y = brightness[1][color_intensity];
253254                  break;
254255            }
255256
r7669r7670
302303 *
303304 *************************************/
304305
305static DEVICE_START( ppu2c0x )
306{
307   ppu2c0x_chip *chip = get_token(device);
308
309   memset(chip, 0, sizeof(*chip));
310   chip->scanlines_per_frame = (int) device->get_config_int(PPU2C0XINFO_INT_SCANLINES_PER_FRAME);
311
312   /* initialize the scanline handling portion */
313   chip->scanline_timer = timer_alloc(device->machine, scanline_callback, (void *) device);
314   chip->hblank_timer = timer_alloc(device->machine, hblank_callback, (void *) device);
315   chip->nmi_timer = timer_alloc(device->machine, nmi_callback, (void *) device);
316   chip->scanline = 0;
317   chip->scan_scale = 1;
318
319   /* allocate a screen bitmap, videomem and spriteram, a dirtychar array and the monochromatic colortable */
320   chip->bitmap = auto_bitmap_alloc(device->machine, VISIBLE_SCREEN_WIDTH, VISIBLE_SCREEN_HEIGHT, video_screen_get_format(device->machine->primary_screen));
321   chip->spriteram = auto_alloc_array_clear(device->machine, UINT8, SPRITERAM_SIZE);
322   chip->colortable = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable));
323   chip->colortable_mono = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable_mono));
324}
325
326306static TIMER_CALLBACK( hblank_callback )
327307{
328308   running_device *device = (running_device *)ptr;
329   ppu2c0x_chip *this_ppu = get_token(device);
330   int *ppu_regs = &this_ppu->regs[0];
309   ppu2c0x_state *ppu2c0x = get_token(device);
310   int *ppu_regs = &ppu2c0x->regs[0];
331311
332312   int blanked = (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0;
333   int vblank = ((this_ppu->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (this_ppu->scanline < this_ppu->scanlines_per_frame - 1)) ? 1 : 0;
313   int vblank = ((ppu2c0x->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (ppu2c0x->scanline < ppu2c0x->scanlines_per_frame - 1)) ? 1 : 0;
334314
335315//  update_scanline(device);
336316
337   if (this_ppu->hblank_callback_proc)
338      (*this_ppu->hblank_callback_proc) (device, this_ppu->scanline, vblank, blanked);
317   if (ppu2c0x->hblank_callback_proc)
318      (*ppu2c0x->hblank_callback_proc) (device, ppu2c0x->scanline, vblank, blanked);
339319
340   timer_adjust_oneshot(this_ppu->hblank_timer, attotime_never, 0);
320   timer_adjust_oneshot(ppu2c0x->hblank_timer, attotime_never, 0);
341321}
342322
343323static TIMER_CALLBACK( nmi_callback )
344324{
345325   running_device *device = (running_device *)ptr;
346   ppu2c0x_chip *this_ppu = get_token(device);
347   const ppu2c0x_interface *intf = get_interface(device);
348   int *ppu_regs = &this_ppu->regs[0];
326   ppu2c0x_state *ppu2c0x = get_token(device);
327   int *ppu_regs = &ppu2c0x->regs[0];
349328
350329   // Actually fire the VMI
351   if (intf->nmi_handler != NULL)
352      (*intf->nmi_handler) (device, ppu_regs);
330   if (ppu2c0x->nmi_callback_proc != NULL)
331      (*ppu2c0x->nmi_callback_proc) (device, ppu_regs);
353332
354   timer_adjust_oneshot(this_ppu->nmi_timer, attotime_never, 0);
333   timer_adjust_oneshot(ppu2c0x->nmi_timer, attotime_never, 0);
355334}
356335
357336static void draw_background( running_device *device, UINT8 *line_priority )
358337{
359   const ppu2c0x_interface *intf = get_interface(device);
360   ppu2c0x_chip *this_ppu = get_token(device);
338   ppu2c0x_state *ppu2c0x = get_token(device);
361339
362340   /* cache some values locally */
363   bitmap_t *bitmap = this_ppu->bitmap;
364   const int *ppu_regs = &this_ppu->regs[0];
365   const int scanline = this_ppu->scanline;
366   const int refresh_data = this_ppu->refresh_data;
367   const int tile_page = this_ppu->tile_page;
341   bitmap_t *bitmap = ppu2c0x->bitmap;
342   const int *ppu_regs = &ppu2c0x->regs[0];
343   const int scanline = ppu2c0x->scanline;
344   const int refresh_data = ppu2c0x->refresh_data;
345   const int tile_page = ppu2c0x->tile_page;
368346
369   int   start_x = (this_ppu->x_fine ^ 0x07) - 7;
347   int   start_x = (ppu2c0x->x_fine ^ 0x07) - 7;
370348   UINT16 back_pen;
371349   UINT16 *dest;
372350
r7669r7670
382360   if (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO)
383361   {
384362      color_mask = 0xf0;
385      color_table = this_ppu->colortable_mono;
363      color_table = ppu2c0x->colortable_mono;
386364   }
387365   else
388366   {
389367      color_mask = 0xff;
390      color_table = this_ppu->colortable;
368      color_table = ppu2c0x->colortable;
391369   }
392370
393371   /* cache the background pen */
394   back_pen = (this_ppu->back_color & color_mask) + intf->color_base;
372   back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base;
395373
396374   /* determine where in the nametable to start drawing from */
397375   /* based on the current scanline and scroll regs */
r7669r7670
445423         paldata = &color_table[4 * (((color_byte >> color_bits) & 0x03))];
446424
447425         // need to read 0x0000 or 0x1000 + 16*nametable data
448         address = ((this_ppu->tile_page) ? 0x1000 : 0) + (page2 * 16);
426         address = ((ppu2c0x->tile_page) ? 0x1000 : 0) + (page2 * 16);
449427         // plus something that accounts for y
450428         address += scroll_y_fine;
451429
r7669r7670
503481
504482static void draw_sprites( running_device *device, UINT8 *line_priority )
505483{
506   ppu2c0x_chip *this_ppu = get_token(device);
484   ppu2c0x_state *ppu2c0x = get_token(device);
507485
508486   /* cache some values locally */
509   bitmap_t *bitmap = this_ppu->bitmap;
510   const int scanline = this_ppu->scanline;
511   const int sprite_page = this_ppu->sprite_page;
512   const UINT8 *sprite_ram = this_ppu->spriteram;
513   pen_t *color_table = this_ppu->colortable;
514   int *ppu_regs = &this_ppu->regs[0];
487   bitmap_t *bitmap = ppu2c0x->bitmap;
488   const int scanline = ppu2c0x->scanline;
489   const int sprite_page = ppu2c0x->sprite_page;
490   const UINT8 *sprite_ram = ppu2c0x->spriteram;
491   pen_t *color_table = ppu2c0x->colortable;
492   int *ppu_regs = &ppu2c0x->regs[0];
515493
516   int spriteXPos, spriteYPos, spriteIndex;
494   int sprite_xpos, sprite_ypos, sprite_index;
517495   int tile, index1, page;
518496   int pri;
519497
520498   int flipx, flipy, color;
521499   int size;
522   int spriteCount = 0;
500   int sprite_count = 0;
523501   int sprite_line;
524502
525503   int first_pixel;
r7669r7670
532510
533511   first_pixel = (ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES_L8)? 0: 8;
534512
535   for (spriteIndex = 0; spriteIndex < SPRITERAM_SIZE; spriteIndex += 4)
513   for (sprite_index = 0; sprite_index < SPRITERAM_SIZE; sprite_index += 4)
536514   {
537515      UINT8 plane1;
538516      UINT8 plane2;
539517
540      spriteYPos = sprite_ram[spriteIndex] + 1;
541      spriteXPos = sprite_ram[spriteIndex + 3];
518      sprite_ypos = sprite_ram[sprite_index] + 1;
519      sprite_xpos = sprite_ram[sprite_index + 3];
542520
543521      // The sprite collision acts funny on the last pixel of a scanline.
544522      // The various scanline latches update while the last few pixels
r7669r7670
547525      // when, e.g., sprite x is 254, sprite y is 29 and we're rendering
548526      // at the end of scanline 28.
549527      // Battletoads needs this level of precision to be playable.
550      if ((spriteIndex == 0) && (spriteXPos == 254))
528      if ((sprite_index == 0) && (sprite_xpos == 254))
551529      {
552         spriteYPos --;
530         sprite_ypos--;
553531         /* set the "sprite 0 hit" flag if appropriate */
554         if (line_priority[spriteXPos] & 0x02)
532         if (line_priority[sprite_xpos] & 0x02)
555533            ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT;
556534      }
557535
558536      /* if the sprite isn't visible, skip it */
559      if ((spriteYPos + size <= scanline) || (spriteYPos > scanline))
537      if ((sprite_ypos + size <= scanline) || (sprite_ypos > scanline))
560538         continue;
561539
562      tile = sprite_ram[spriteIndex + 1];
563      color = (sprite_ram[spriteIndex + 2] & 0x03) + 4;
564      pri = sprite_ram[spriteIndex + 2] & 0x20;
565      flipx = sprite_ram[spriteIndex + 2] & 0x40;
566      flipy = sprite_ram[spriteIndex + 2] & 0x80;
540      tile  =  sprite_ram[sprite_index + 1];
541      color = (sprite_ram[sprite_index + 2] & 0x03) + 4;
542      pri   =  sprite_ram[sprite_index + 2] & 0x20;
543      flipx =  sprite_ram[sprite_index + 2] & 0x40;
544      flipy =  sprite_ram[sprite_index + 2] & 0x80;
567545
568546      if (size == 16)
569547      {
r7669r7670
583561         (*ppu_latch)(device, (sprite_page << 10) | ((tile & 0xff) << 4));
584562
585563      /* compute the character's line to draw */
586      sprite_line = scanline - spriteYPos;
564      sprite_line = scanline - sprite_ypos;
587565
588566      if (flipy)
589567         sprite_line = (size - 1) - sprite_line;
r7669r7670
600578      if (size == 8)
601579         index1 += ((sprite_page == 0) ? 0 : 0x1000);
602580
603      plane1 = memory_read_byte(device->space(), (index1 + sprite_line) & 0x1fff);
581      plane1 = memory_read_byte(device->space(), (index1 + sprite_line + 0) & 0x1fff);
604582      plane2 = memory_read_byte(device->space(), (index1 + sprite_line + 8) & 0x1fff);
605583
606584      /* if there are more than 8 sprites on this line, set the flag */
607      if (spriteCount == 8)
585      if (sprite_count == 8)
608586      {
609587         ppu_regs[PPU_STATUS] |= PPU_STATUS_8SPRITES;
610588//          logerror ("> 8 sprites, scanline: %d\n", scanline);
r7669r7670
613591         break;
614592      }
615593
616      spriteCount++;
594      sprite_count++;
617595
618596      /* abort drawing if sprites aren't rendered */
619597      if (!(ppu_regs[PPU_CONTROL1] & PPU_CONTROL1_SPRITES))
r7669r7670
624602         /* draw the low-priority sprites */
625603         for (pixel = 0; pixel < 8; pixel++)
626604         {
627            UINT8 pixelData;
605            UINT8 pixel_data;
628606            if (flipx)
629607            {
630               pixelData = (plane1 & 1) + ((plane2 & 1) << 1);
608               pixel_data = (plane1 & 1) + ((plane2 & 1) << 1);
631609               plane1 = plane1 >> 1;
632610               plane2 = plane2 >> 1;
633611            }
634612            else
635613            {
636               pixelData = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1);
614               pixel_data = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1);
637615               plane1 = plane1 << 1;
638616               plane2 = plane2 << 1;
639617            }
640618
641619            /* is this pixel non-transparent? */
642            if (spriteXPos + pixel >= first_pixel)
620            if (sprite_xpos + pixel >= first_pixel)
643621            {
644               if (pixelData)
622               if (pixel_data)
645623               {
646624                  /* has the background (or another sprite) already been drawn here? */
647                  if (!line_priority[spriteXPos + pixel])
625                  if (!line_priority[sprite_xpos + pixel])
648626                  {
649627                     /* no, draw */
650                     if ((spriteXPos + pixel) < VISIBLE_SCREEN_WIDTH)
651                        *BITMAP_ADDR16(bitmap, scanline, spriteXPos + pixel) = paldata[pixelData];
628                     if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH)
629                        *BITMAP_ADDR16(bitmap, scanline, sprite_xpos + pixel) = paldata[pixel_data];
652630                  }
653631                  /* indicate that a sprite was drawn at this location, even if it's not seen */
654                  if ((spriteXPos + pixel) < VISIBLE_SCREEN_WIDTH)
655                     line_priority[spriteXPos + pixel] |= 0x01;
632                  if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH)
633                     line_priority[sprite_xpos + pixel] |= 0x01;
656634               }
657635
658636               /* set the "sprite 0 hit" flag if appropriate */
659               if (spriteIndex == 0 && (pixelData & 0x03) && ((spriteXPos + pixel) < 255) && (line_priority[spriteXPos + pixel] & 0x02))
637               if (sprite_index == 0 && (pixel_data & 0x03) && ((sprite_xpos + pixel) < 255) && (line_priority[sprite_xpos + pixel] & 0x02))
660638                  ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT;
661639            }
662640         }
r7669r7670
666644         /* draw the high-priority sprites */
667645         for (pixel = 0; pixel < 8; pixel++)
668646         {
669            UINT8 pixelData;
647            UINT8 pixel_data;
670648            if (flipx)
671649            {
672               pixelData = (plane1 & 1) + ((plane2 & 1) << 1);
650               pixel_data = (plane1 & 1) + ((plane2 & 1) << 1);
673651               plane1 = plane1 >> 1;
674652               plane2 = plane2 >> 1;
675653            }
676654            else
677655            {
678               pixelData = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1);
656               pixel_data = ((plane1 >> 7) & 1) | (((plane2 >> 7) & 1) << 1);
679657               plane1 = plane1 << 1;
680658               plane2 = plane2 << 1;
681659            }
682660
683661            /* is this pixel non-transparent? */
684            if (spriteXPos + pixel >= first_pixel)
662            if (sprite_xpos + pixel >= first_pixel)
685663            {
686               if (pixelData)
664               if (pixel_data)
687665               {
688666                  /* has another sprite been drawn here? */
689                  if (!(line_priority[spriteXPos + pixel] & 0x01))
667                  if (!(line_priority[sprite_xpos + pixel] & 0x01))
690668                  {
691669                     /* no, draw */
692                     if ((spriteXPos + pixel) < VISIBLE_SCREEN_WIDTH)
670                     if ((sprite_xpos + pixel) < VISIBLE_SCREEN_WIDTH)
693671                     {
694                        *BITMAP_ADDR16(bitmap, scanline, spriteXPos + pixel) = paldata[pixelData];
695                        line_priority[spriteXPos + pixel] |= 0x01;
672                        *BITMAP_ADDR16(bitmap, scanline, sprite_xpos + pixel) = paldata[pixel_data];
673                        line_priority[sprite_xpos + pixel] |= 0x01;
696674                     }
697675                  }
698676               }
699677
700678               /* set the "sprite 0 hit" flag if appropriate */
701               if (spriteIndex == 0 && (pixelData & 0x03) && ((spriteXPos + pixel) < 255) && (line_priority[spriteXPos + pixel] & 0x02))
679               if (sprite_index == 0 && (pixel_data & 0x03) && ((sprite_xpos + pixel) < 255) && (line_priority[sprite_xpos + pixel] & 0x02))
702680                  ppu_regs[PPU_STATUS] |= PPU_STATUS_SPRITE0_HIT;
703681            }
704682         }
r7669r7670
714692
715693static void render_scanline( running_device *device )
716694{
717   ppu2c0x_chip *this_ppu = get_token(device);
718   const ppu2c0x_interface *intf = get_interface(device);
695   ppu2c0x_state *ppu2c0x = get_token(device);
719696   UINT8 line_priority[VISIBLE_SCREEN_WIDTH];
720   int *ppu_regs = &this_ppu->regs[0];
697   int *ppu_regs = &ppu2c0x->regs[0];
721698
722699   /* lets see how long it takes */
723700   profiler_mark_start(PROFILER_USER1);
r7669r7670
730707      draw_background(device, line_priority);
731708   else
732709   {
733      bitmap_t *bitmap = this_ppu->bitmap;
734      const int scanline = this_ppu->scanline;
710      bitmap_t *bitmap = ppu2c0x->bitmap;
711      const int scanline = ppu2c0x->scanline;
735712      UINT8 color_mask;
736713      UINT16 back_pen;
737714      int i;
r7669r7670
743720         color_mask = 0xff;
744721
745722      /* cache the background pen */
746      back_pen = (this_ppu->back_color & color_mask) + intf->color_base;
723      back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base;
747724
748725      // Fill this scanline with the background pen.
749726      for (i = 0; i < bitmap->width; i++)
r7669r7670
759736
760737static void update_scanline( running_device *device )
761738{
762   ppu2c0x_chip *this_ppu = get_token(device);
763   const ppu2c0x_interface *intf = get_interface(device);
764   int scanline = this_ppu->scanline;
765   int *ppu_regs = &this_ppu->regs[0];
739   ppu2c0x_state *ppu2c0x = get_token(device);
740   int scanline = ppu2c0x->scanline;
741   int *ppu_regs = &ppu2c0x->regs[0];
766742
767743   if (scanline <= PPU_BOTTOM_VISIBLE_SCANLINE)
768744   {
r7669r7670
771747      {
772748         /* If background or sprites are enabled, copy the ppu address latch */
773749         /* Copy only the scroll x-coarse and the x-overflow bit */
774         this_ppu->refresh_data &= ~0x041f;
775         this_ppu->refresh_data |= (this_ppu->refresh_latch & 0x041f);
750         ppu2c0x->refresh_data &= ~0x041f;
751         ppu2c0x->refresh_data |= (ppu2c0x->refresh_latch & 0x041f);
776752
777//          logerror("updating refresh_data: %04x (scanline: %d)\n", this_ppu->refresh_data, this_ppu->scanline);
753//          logerror("updating refresh_data: %04x (scanline: %d)\n", ppu2c0x->refresh_data, ppu2c0x->scanline);
778754         render_scanline(device);
779755      }
780756      else
781757      {
782         bitmap_t *bitmap = this_ppu->bitmap;
783         const int scanline = this_ppu->scanline;
758         bitmap_t *bitmap = ppu2c0x->bitmap;
759         const int scanline = ppu2c0x->scanline;
784760         UINT8 color_mask;
785761         UINT16 back_pen;
786762         int i;
r7669r7670
792768            color_mask = 0xff;
793769
794770         /* cache the background pen */
795         if (this_ppu->videomem_addr >= 0x3f00)
771         if (ppu2c0x->videomem_addr >= 0x3f00)
796772         {
797773            // If the PPU's VRAM address happens to point into palette ram space while
798774            // both the sprites and background are disabled, the PPU paints the scanline
799775            // with the palette entry at the VRAM address instead of the usual background
800776            // pen. Micro Machines makes use of this feature.
801            int penNum;
777            int pen_num;
802778
803            if (this_ppu->videomem_addr & 0x03)
804               penNum = this_ppu->palette_ram[this_ppu->videomem_addr & 0x1f];
779            if (ppu2c0x->videomem_addr & 0x03)
780               pen_num = ppu2c0x->palette_ram[ppu2c0x->videomem_addr & 0x1f];
805781            else
806               penNum = this_ppu->palette_ram[0];
782               pen_num = ppu2c0x->palette_ram[0];
807783
808            back_pen = penNum + intf->color_base;
784            back_pen = pen_num + ppu2c0x->color_base;
809785         }
810786         else
811            back_pen = (this_ppu->back_color & color_mask) + intf->color_base;
787            back_pen = (ppu2c0x->back_color & color_mask) + ppu2c0x->color_base;
812788
813789         // Fill this scanline with the background pen.
814790         for (i = 0; i < bitmap->width; i++)
r7669r7670
816792      }
817793
818794      /* increment the fine y-scroll */
819      this_ppu->refresh_data += 0x1000;
795      ppu2c0x->refresh_data += 0x1000;
820796
821797      /* if it's rolled, increment the coarse y-scroll */
822      if (this_ppu->refresh_data & 0x8000)
798      if (ppu2c0x->refresh_data & 0x8000)
823799      {
824800         UINT16 tmp;
825         tmp = (this_ppu->refresh_data & 0x03e0) + 0x20;
826         this_ppu->refresh_data &= 0x7c1f;
801         tmp = (ppu2c0x->refresh_data & 0x03e0) + 0x20;
802         ppu2c0x->refresh_data &= 0x7c1f;
827803
828804         /* handle bizarro scrolling rollover at the 30th (not 32nd) vertical tile */
829805         if (tmp == 0x03c0)
830            this_ppu->refresh_data ^= 0x0800;
806            ppu2c0x->refresh_data ^= 0x0800;
831807         else
832            this_ppu->refresh_data |= (tmp & 0x03e0);
808            ppu2c0x->refresh_data |= (tmp & 0x03e0);
833809
834//          logerror("updating refresh_data: %04x\n", this_ppu->refresh_data);
810//          logerror("updating refresh_data: %04x\n", ppu2c0x->refresh_data);
835811      }
836812   }
837813
r7669r7670
840816static TIMER_CALLBACK( scanline_callback )
841817{
842818   running_device *device = (running_device *)ptr;
843   ppu2c0x_chip *this_ppu = get_token(device);
844   int *ppu_regs = &this_ppu->regs[0];
819   ppu2c0x_state *ppu2c0x = get_token(device);
820   int *ppu_regs = &ppu2c0x->regs[0];
845821   int blanked = (ppu_regs[PPU_CONTROL1] & (PPU_CONTROL1_BACKGROUND | PPU_CONTROL1_SPRITES)) == 0;
846   int vblank = ((this_ppu->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (this_ppu->scanline < this_ppu->scanlines_per_frame - 1)) ? 1 : 0;
822   int vblank = ((ppu2c0x->scanline >= PPU_VBLANK_FIRST_SCANLINE - 1) && (ppu2c0x->scanline < ppu2c0x->scanlines_per_frame - 1)) ? 1 : 0;
847823   int next_scanline;
848824
849825   /* if a callback is available, call it */
850   if (this_ppu->scanline_callback_proc != NULL)
851      (*this_ppu->scanline_callback_proc)(device, this_ppu->scanline, vblank, blanked);
826   if (ppu2c0x->scanline_callback_proc != NULL)
827      (*ppu2c0x->scanline_callback_proc)(device, ppu2c0x->scanline, vblank, blanked);
852828
853829   /* update the scanline that just went by */
854830   update_scanline(device);
855831
856832   /* increment our scanline count */
857   this_ppu->scanline++;
833   ppu2c0x->scanline++;
858834
859//  logerror("starting scanline %d (MAME %d, beam %d)\n", this_ppu->scanline, video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen));
835//  logerror("starting scanline %d (MAME %d, beam %d)\n", ppu2c0x->scanline, video_screen_get_vpos(device->machine->primary_screen), video_screen_get_hpos(device->machine->primary_screen));
860836
861837   /* Note: this is called at the _end_ of each scanline */
862   if (this_ppu->scanline == PPU_VBLANK_FIRST_SCANLINE)
838   if (ppu2c0x->scanline == PPU_VBLANK_FIRST_SCANLINE)
863839   {
864840      // logerror("vblank starting\n");
865841      /* We just entered VBLANK */
r7669r7670
872848         // a game can read the high bit of $2002 before the NMI is called (potentially resetting the bit
873849         // via a read from $2002 in the NMI handler).
874850         // B-Wings is an example game that needs this.
875         timer_adjust_oneshot(this_ppu->nmi_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 4), 0);
851         timer_adjust_oneshot(ppu2c0x->nmi_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 4), 0);
876852      }
877853   }
878854
879   if (this_ppu->scanline == this_ppu->scanlines_per_frame - 1)
855   if (ppu2c0x->scanline == ppu2c0x->scanlines_per_frame - 1)
880856   {
881857      // logerror("vblank ending\n");
882858      /* clear the vblank & sprite hit flag */
r7669r7670
884860   }
885861
886862   /* see if we rolled */
887   else if (this_ppu->scanline == this_ppu->scanlines_per_frame)
863   else if (ppu2c0x->scanline == ppu2c0x->scanlines_per_frame)
888864   {
889865      /* if background or sprites are enabled, copy the ppu address latch */
890866      if (!blanked)
891         this_ppu->refresh_data = this_ppu->refresh_latch;
867         ppu2c0x->refresh_data = ppu2c0x->refresh_latch;
892868
893869      /* reset the scanline count */
894      this_ppu->scanline = 0;
895//      logerror("sprite 0 x: %d y: %d num: %d\n", this_ppu->spriteram[3], this_ppu->spriteram[0] + 1, this_ppu->spriteram[1]);
870      ppu2c0x->scanline = 0;
871//      logerror("sprite 0 x: %d y: %d num: %d\n", ppu2c0x->spriteram[3], ppu2c0x->spriteram[0] + 1, ppu2c0x->spriteram[1]);
896872   }
897873
898   next_scanline = this_ppu->scanline + 1;
899   if (next_scanline == this_ppu->scanlines_per_frame)
874   next_scanline = ppu2c0x->scanline + 1;
875   if (next_scanline == ppu2c0x->scanlines_per_frame)
900876      next_scanline = 0;
901877
902878   // Call us back when the hblank starts for this scanline
903   timer_adjust_oneshot(this_ppu->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation
879   timer_adjust_oneshot(ppu2c0x->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation
904880
905881   // trigger again at the start of the next scanline
906   timer_adjust_oneshot(this_ppu->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, next_scanline * this_ppu->scan_scale, 0), 0);
882   timer_adjust_oneshot(ppu2c0x->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, next_scanline * ppu2c0x->scan_scale, 0), 0);
907883}
908884
909885/*************************************
910 *
911 *  PPU Reset
912 *
913 *************************************/
914
915static DEVICE_RESET( ppu2c0x )
916{
917   ppu2c0x_chip *this_ppu = get_token(device);
918   const ppu2c0x_interface *intf = get_interface(device);
919   int scan_scale = 1;
920   int i;
921
922   /* reset the scanline count */
923   this_ppu->scanline = 0;
924
925   /* set the scan scale (this is for dual monitor vertical setups) */
926   this_ppu->scan_scale = scan_scale;
927
928   timer_adjust_oneshot(this_ppu->nmi_timer, attotime_never, 0);
929
930   // Call us back when the hblank starts for this scanline
931   timer_adjust_oneshot(this_ppu->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation
932
933   // Call us back at the start of the next scanline
934   timer_adjust_oneshot(this_ppu->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, 1, 0), 0);
935
936   /* reset the callbacks */
937   this_ppu->scanline_callback_proc = 0;
938   this_ppu->vidaccess_callback_proc = 0;
939
940   for (i = 0; i < PPU_MAX_REG; i++)
941      this_ppu->regs[i] = 0;
942
943   /* initialize the rest of the members */
944   this_ppu->refresh_data = 0;
945   this_ppu->refresh_latch = 0;
946   this_ppu->x_fine = 0;
947   this_ppu->toggle = 0;
948   this_ppu->add = 1;
949   this_ppu->videomem_addr = 0;
950   this_ppu->addr_latch = 0;
951   this_ppu->data_latch = 0;
952   this_ppu->tile_page = 0;
953   this_ppu->sprite_page = 0;
954   this_ppu->back_color = 0;
955
956   /* initialize the color tables */
957   {
958      int color_base = intf->color_base;
959
960      for (i = 0; i < ARRAY_LENGTH(default_colortable_mono); i++)
961      {
962         /* monochromatic table */
963         this_ppu->colortable_mono[i] = default_colortable_mono[i] + color_base;
964
965         /* color table */
966         this_ppu->colortable[i] = default_colortable[i] + color_base;
967      }
968   }
969}
970
971/*************************************
972886*
973887*   PPU Memory functions
974888*
r7669r7670
976890
977891static WRITE8_HANDLER( ppu2c0x_palette_write )
978892{
979   ppu2c0x_chip *this_ppu = get_token(space->cpu);
980   const ppu2c0x_interface *intf = get_interface(space->cpu);
981   int color_base = intf->color_base;
893   ppu2c0x_state *ppu2c0x = get_token(space->cpu);
894   int color_base = ppu2c0x->color_base;
982895
983   int colorEmphasis = (this_ppu->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS) * 2;
896   int color_emphasis = (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS) * 2;
984897   // it's a palette
985898   // transparent pens are mirrored!
986899   if (offset & 0x3)
987900   {
988      this_ppu->palette_ram[offset & 0x1f] = data;
989      this_ppu->colortable[offset & 0x1f] = color_base + data + colorEmphasis;
990      this_ppu->colortable_mono[offset & 0x1f] = color_base + (data & 0xf0) + colorEmphasis;
901      ppu2c0x->palette_ram[offset & 0x1f] = data;
902      ppu2c0x->colortable[offset & 0x1f] = color_base + data + color_emphasis;
903      ppu2c0x->colortable_mono[offset & 0x1f] = color_base + (data & 0xf0) + color_emphasis;
991904   }
992905   else
993906   {
994907      int i;
995908      if (0 == (offset & 0xf))
996909      {
997         this_ppu->back_color = data;
910         ppu2c0x->back_color = data;
998911         for (i = 0; i < 32; i += 4)
999912         {
1000            this_ppu->colortable[i] = color_base + data + colorEmphasis;
1001            this_ppu->colortable_mono[i] = color_base + (data & 0xf0) + colorEmphasis;
913            ppu2c0x->colortable[i] = color_base + data + color_emphasis;
914            ppu2c0x->colortable_mono[i] = color_base + (data & 0xf0) + color_emphasis;
1002915         }
1003916      }
1004      this_ppu->palette_ram[offset & 0xf] = this_ppu->palette_ram[(offset & 0xf) + 0x10] = data;
917      ppu2c0x->palette_ram[offset & 0xf] = ppu2c0x->palette_ram[(offset & 0xf) + 0x10] = data;
1005918   }
1006919}
1007920
1008921static READ8_HANDLER( ppu2c0x_palette_read )
1009922{
1010   ppu2c0x_chip *this_ppu = get_token(space->cpu);
923   ppu2c0x_state *ppu2c0x = get_token(space->cpu);
1011924   {
1012      if (this_ppu->regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO)
1013         return (this_ppu->palette_ram[offset & 0x1f] & 0x30);
925      if (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_DISPLAY_MONO)
926         return (ppu2c0x->palette_ram[offset & 0x1f] & 0x30);
1014927
1015928      else
1016         return (this_ppu->palette_ram[offset & 0x1f] & 0x3f);
929         return (ppu2c0x->palette_ram[offset & 0x1f] & 0x3f);
1017930   }
1018931}
1019932
r7669r7670
1025938
1026939READ8_DEVICE_HANDLER( ppu2c0x_r )
1027940{
1028   ppu2c0x_chip *this_ppu = get_token(device);
941   ppu2c0x_state *ppu2c0x = get_token(device);
1029942
1030943   if (offset >= PPU_MAX_REG)
1031944   {
r7669r7670
1039952      case PPU_STATUS:
1040953         // The top 3 bits of the status register are the only ones that report data. The
1041954         // remainder contain whatever was last in the PPU data latch.
1042         this_ppu->data_latch = this_ppu->regs[PPU_STATUS] | (this_ppu->data_latch & 0x1f);
955         ppu2c0x->data_latch = ppu2c0x->regs[PPU_STATUS] | (ppu2c0x->data_latch & 0x1f);
1043956
1044957         // Reset hi/lo scroll toggle
1045         this_ppu->toggle = 0;
958         ppu2c0x->toggle = 0;
1046959
1047960         // If the vblank bit is set, clear all status bits but the 2 sprite flags
1048         if (this_ppu->data_latch & PPU_STATUS_VBLANK)
1049            this_ppu->regs[PPU_STATUS] &= 0x60;
961         if (ppu2c0x->data_latch & PPU_STATUS_VBLANK)
962            ppu2c0x->regs[PPU_STATUS] &= 0x60;
1050963         break;
1051964
1052965      case PPU_SPRITE_DATA:
1053         this_ppu->data_latch = this_ppu->spriteram[this_ppu->regs[PPU_SPRITE_ADDRESS]];
966         ppu2c0x->data_latch = ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]];
1054967         break;
1055968
1056969      case PPU_DATA:
1057970         if (ppu_latch)
1058            (*ppu_latch)(device, this_ppu->videomem_addr & 0x3fff);
971            (*ppu_latch)(device, ppu2c0x->videomem_addr & 0x3fff);
1059972
1060         if (this_ppu->videomem_addr >= 0x3f00)
973         if (ppu2c0x->videomem_addr >= 0x3f00)
1061974         {
1062            this_ppu->data_latch = memory_read_byte(device->space(), this_ppu->videomem_addr);
975            ppu2c0x->data_latch = memory_read_byte(device->space(), ppu2c0x->videomem_addr);
1063976            // buffer the mirrored NT data
1064            this_ppu->buffered_data = memory_read_byte(device->space(), this_ppu->videomem_addr & 0x2fff);
977            ppu2c0x->buffered_data = memory_read_byte(device->space(), ppu2c0x->videomem_addr & 0x2fff);
1065978         }
1066979         else
1067980         {
1068            this_ppu->data_latch = this_ppu->buffered_data;
1069            this_ppu->buffered_data = memory_read_byte(device->space(), this_ppu->videomem_addr);
981            ppu2c0x->data_latch = ppu2c0x->buffered_data;
982            ppu2c0x->buffered_data = memory_read_byte(device->space(), ppu2c0x->videomem_addr);
1070983         }
1071984
1072         this_ppu->videomem_addr += this_ppu->add;
985         ppu2c0x->videomem_addr += ppu2c0x->add;
1073986         break;
1074987
1075988      default:
1076989         break;
1077990   }
1078991
1079   return this_ppu->data_latch;
992   return ppu2c0x->data_latch;
1080993}
1081994
1082995
r7669r7670
10881001
10891002WRITE8_DEVICE_HANDLER( ppu2c0x_w )
10901003{
1091   ppu2c0x_chip *this_ppu = get_token(device);
1092   const ppu2c0x_interface *intf = get_interface(device);
1004   ppu2c0x_state *ppu2c0x = get_token(device);
10931005   int color_base;
10941006
1095   color_base = intf->color_base;
1007   color_base = ppu2c0x->color_base;
10961008
10971009   if (offset >= PPU_MAX_REG)
10981010   {
r7669r7670
11011013   }
11021014
11031015#ifdef MAME_DEBUG
1104   if (this_ppu->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE)
1016   if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE)
11051017   {
11061018      running_device *screen = device->machine->primary_screen;
1107      logerror("PPU register %d write %02x during non-vblank scanline %d (MAME %d, beam pos: %d)\n", offset, data, this_ppu->scanline, video_screen_get_vpos(screen), video_screen_get_hpos(screen));
1019      logerror("PPU register %d write %02x during non-vblank scanline %d (MAME %d, beam pos: %d)\n", offset, data, ppu2c0x->scanline, video_screen_get_vpos(screen), video_screen_get_hpos(screen));
11081020   }
11091021#endif
11101022
11111023   switch (offset & 7)
11121024   {
11131025      case PPU_CONTROL0:
1114         this_ppu->regs[PPU_CONTROL0] = data;
1026         ppu2c0x->regs[PPU_CONTROL0] = data;
11151027
11161028         /* update the name table number on our refresh latches */
1117         this_ppu->refresh_latch &= 0x73ff;
1118         this_ppu->refresh_latch |= (data & 3) << 10;
1029         ppu2c0x->refresh_latch &= 0x73ff;
1030         ppu2c0x->refresh_latch |= (data & 3) << 10;
11191031
11201032         /* the char ram bank points either 0x0000 or 0x1000 (page 0 or page 4) */
1121         this_ppu->tile_page = (data & PPU_CONTROL0_CHR_SELECT) >> 2;
1122         this_ppu->sprite_page = (data & PPU_CONTROL0_SPR_SELECT) >> 1;
1033         ppu2c0x->tile_page = (data & PPU_CONTROL0_CHR_SELECT) >> 2;
1034         ppu2c0x->sprite_page = (data & PPU_CONTROL0_SPR_SELECT) >> 1;
11231035
1124         this_ppu->add = (data & PPU_CONTROL0_INC) ? 32 : 1;
1125//          logerror("control0 write: %02x (scanline: %d)\n", data, this_ppu->scanline);
1036         ppu2c0x->add = (data & PPU_CONTROL0_INC) ? 32 : 1;
1037//          logerror("control0 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline);
11261038         break;
11271039
11281040      case PPU_CONTROL1:
11291041         /* if color intensity has changed, change all the color tables to reflect them */
1130         if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (this_ppu->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS))
1042         if ((data & PPU_CONTROL1_COLOR_EMPHASIS) != (ppu2c0x->regs[PPU_CONTROL1] & PPU_CONTROL1_COLOR_EMPHASIS))
11311043         {
11321044            int i;
11331045            for (i = 0; i <= 0x1f; i ++)
11341046            {
1135               UINT8 oldColor = this_ppu->palette_ram[i];
1047               UINT8 oldColor = ppu2c0x->palette_ram[i];
11361048
1137               this_ppu->colortable[i] = color_base + oldColor + (data & PPU_CONTROL1_COLOR_EMPHASIS) * 2;
1049               ppu2c0x->colortable[i] = color_base + oldColor + (data & PPU_CONTROL1_COLOR_EMPHASIS) * 2;
11381050            }
11391051         }
11401052
1141//          logerror("control1 write: %02x (scanline: %d)\n", data, this_ppu->scanline);
1142         this_ppu->regs[PPU_CONTROL1] = data;
1053//          logerror("control1 write: %02x (scanline: %d)\n", data, ppu2c0x->scanline);
1054         ppu2c0x->regs[PPU_CONTROL1] = data;
11431055         break;
11441056
11451057      case PPU_SPRITE_ADDRESS:
1146         this_ppu->regs[PPU_SPRITE_ADDRESS] = data;
1058         ppu2c0x->regs[PPU_SPRITE_ADDRESS] = data;
11471059         break;
11481060
11491061      case PPU_SPRITE_DATA:
11501062         // If the PPU is currently rendering the screen, 0xff is written instead of the desired data.
1151         if (this_ppu->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE)
1063         if (ppu2c0x->scanline <= PPU_BOTTOM_VISIBLE_SCANLINE)
11521064            data = 0xff;
1153         this_ppu->spriteram[this_ppu->regs[PPU_SPRITE_ADDRESS]] = data;
1154         this_ppu->regs[PPU_SPRITE_ADDRESS] = (this_ppu->regs[PPU_SPRITE_ADDRESS] + 1) & 0xff;
1065         ppu2c0x->spriteram[ppu2c0x->regs[PPU_SPRITE_ADDRESS]] = data;
1066         ppu2c0x->regs[PPU_SPRITE_ADDRESS] = (ppu2c0x->regs[PPU_SPRITE_ADDRESS] + 1) & 0xff;
11551067         break;
11561068
11571069      case PPU_SCROLL:
1158         if (this_ppu->toggle)
1070         if (ppu2c0x->toggle)
11591071         {
11601072            /* second write */
1161            this_ppu->refresh_latch &= 0x0c1f;
1162            this_ppu->refresh_latch |= (data & 0xf8) << 2;
1163            this_ppu->refresh_latch |= (data & 0x07) << 12;
1164//              logerror("scroll write 2: %d, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline);
1073            ppu2c0x->refresh_latch &= 0x0c1f;
1074            ppu2c0x->refresh_latch |= (data & 0xf8) << 2;
1075            ppu2c0x->refresh_latch |= (data & 0x07) << 12;
1076//              logerror("scroll write 2: %d, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline);
11651077         }
11661078         else
11671079         {
11681080            /* first write */
1169            this_ppu->refresh_latch &= 0x7fe0;
1170            this_ppu->refresh_latch |= (data & 0xf8) >> 3;
1081            ppu2c0x->refresh_latch &= 0x7fe0;
1082            ppu2c0x->refresh_latch |= (data & 0xf8) >> 3;
11711083
1172            this_ppu->x_fine = data & 7;
1173//              logerror("scroll write 1: %d, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline);
1084            ppu2c0x->x_fine = data & 7;
1085//              logerror("scroll write 1: %d, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline);
11741086         }
11751087
1176         this_ppu->toggle ^= 1;
1088         ppu2c0x->toggle ^= 1;
11771089         break;
11781090
11791091      case PPU_ADDRESS:
1180         if (this_ppu->toggle)
1092         if (ppu2c0x->toggle)
11811093         {
11821094            /* second write */
1183            this_ppu->refresh_latch &= 0x7f00;
1184            this_ppu->refresh_latch |= data;
1185            this_ppu->refresh_data = this_ppu->refresh_latch;
1095            ppu2c0x->refresh_latch &= 0x7f00;
1096            ppu2c0x->refresh_latch |= data;
1097            ppu2c0x->refresh_data = ppu2c0x->refresh_latch;
11861098
1187            this_ppu->videomem_addr = this_ppu->refresh_latch;
1188//              logerror("vram addr write 2: %02x, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline);
1099            ppu2c0x->videomem_addr = ppu2c0x->refresh_latch;
1100//              logerror("vram addr write 2: %02x, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline);
11891101         }
11901102         else
11911103         {
11921104            /* first write */
1193            this_ppu->refresh_latch &= 0x00ff;
1194            this_ppu->refresh_latch |= (data & 0x3f) << 8;
1195//              logerror("vram addr write 1: %02x, %04x (scanline: %d)\n", data, this_ppu->refresh_latch, this_ppu->scanline);
1105            ppu2c0x->refresh_latch &= 0x00ff;
1106            ppu2c0x->refresh_latch |= (data & 0x3f) << 8;
1107//              logerror("vram addr write 1: %02x, %04x (scanline: %d)\n", data, ppu2c0x->refresh_latch, ppu2c0x->scanline);
11961108         }
11971109
1198         this_ppu->toggle ^= 1;
1110         ppu2c0x->toggle ^= 1;
11991111         break;
12001112
12011113      case PPU_DATA:
12021114         {
1203            int tempAddr = this_ppu->videomem_addr & 0x3fff;
1115            int tempAddr = ppu2c0x->videomem_addr & 0x3fff;
12041116
12051117            if (ppu_latch)
12061118               (*ppu_latch)(device, tempAddr);
12071119
12081120            /* if there's a callback, call it now */
1209            if (this_ppu->vidaccess_callback_proc)
1210               data = (*this_ppu->vidaccess_callback_proc)(device, tempAddr, data);
1121            if (ppu2c0x->vidaccess_callback_proc)
1122               data = (*ppu2c0x->vidaccess_callback_proc)(device, tempAddr, data);
12111123
12121124            /* see if it's on the chargen portion */
12131125            if (tempAddr < 0x2000)
r7669r7670
12211133               memory_write_byte(device->space(), tempAddr, data);
12221134            }
12231135            /* increment the address */
1224            this_ppu->videomem_addr += this_ppu->add;
1136            ppu2c0x->videomem_addr += ppu2c0x->add;
12251137         }
12261138         break;
12271139
r7669r7670
12601172
12611173void ppu2c0x_render( running_device *device, bitmap_t *bitmap, int flipx, int flipy, int sx, int sy )
12621174{
1263   ppu2c0x_chip *this_ppu = get_token(device);
1264   copybitmap(bitmap, this_ppu->bitmap, flipx, flipy, sx, sy, 0);
1175   ppu2c0x_state *ppu2c0x = get_token(device);
1176   copybitmap(bitmap, ppu2c0x->bitmap, flipx, flipy, sx, sy, 0);
12651177}
12661178
12671179/*************************************
r7669r7670
12721184
12731185int ppu2c0x_get_pixel( running_device *device, int x, int y )
12741186{
1275   ppu2c0x_chip *this_ppu = get_token(device);
1187   ppu2c0x_state *ppu2c0x = get_token(device);
12761188
12771189   if (x >= VISIBLE_SCREEN_WIDTH)
12781190      x = VISIBLE_SCREEN_WIDTH - 1;
r7669r7670
12801192   if (y >= VISIBLE_SCREEN_HEIGHT)
12811193      y = VISIBLE_SCREEN_HEIGHT - 1;
12821194
1283   return *BITMAP_ADDR16(this_ppu->bitmap, y, x);
1195   return *BITMAP_ADDR16(ppu2c0x->bitmap, y, x);
12841196}
12851197
12861198int ppu2c0x_get_colorbase( running_device *device )
12871199{
1288   const ppu2c0x_interface *intf = get_interface(device);
1289   return intf->color_base;
1200   ppu2c0x_state *ppu2c0x = get_token(device);
1201   return ppu2c0x->color_base;
12901202}
12911203
12921204int ppu2c0x_get_current_scanline( running_device *device )
12931205{
1294   ppu2c0x_chip *this_ppu = get_token(device);
1295   return this_ppu->scanline;
1206   ppu2c0x_state *ppu2c0x = get_token(device);
1207   return ppu2c0x->scanline;
12961208}
12971209
12981210void ppu2c0x_set_scanline_callback( running_device *device, ppu2c0x_scanline_cb cb )
12991211{
1300   ppu2c0x_chip *this_ppu = get_token(device);
1301   this_ppu->scanline_callback_proc = cb;
1212   ppu2c0x_state *ppu2c0x = get_token(device);
1213   ppu2c0x->scanline_callback_proc = cb;
13021214}
13031215
13041216void ppu2c0x_set_hblank_callback( running_device *device, ppu2c0x_hblank_cb cb )
13051217{
1306   ppu2c0x_chip *this_ppu = get_token(device);
1307   this_ppu->hblank_callback_proc = cb;
1218   ppu2c0x_state *ppu2c0x = get_token(device);
1219   ppu2c0x->hblank_callback_proc = cb;
13081220}
13091221
13101222void ppu2c0x_set_vidaccess_callback( running_device *device, ppu2c0x_vidaccess_cb cb )
13111223{
1312   ppu2c0x_chip *this_ppu = get_token(device);
1313   this_ppu->vidaccess_callback_proc = cb;
1224   ppu2c0x_state *ppu2c0x = get_token(device);
1225   ppu2c0x->vidaccess_callback_proc = cb;
13141226}
13151227
13161228void ppu2c0x_set_scanlines_per_frame( running_device *device, int scanlines )
13171229{
1318   ppu2c0x_chip *this_ppu = get_token(device);
1319   this_ppu->scanlines_per_frame = scanlines;
1230   ppu2c0x_state *ppu2c0x = get_token(device);
1231   ppu2c0x->scanlines_per_frame = scanlines;
13201232}
13211233
13221234
1235/*************************************
1236 *
1237 *  PPU Start
1238 *
1239 *************************************/
1240
1241static DEVICE_START( ppu2c0x )
1242{
1243   ppu2c0x_state *ppu2c0x = get_token(device);
1244   const ppu2c0x_interface *intf = get_interface(device);
1245
1246   memset(ppu2c0x, 0, sizeof(*ppu2c0x));
1247   ppu2c0x->scanlines_per_frame = (int) device->get_config_int(PPU2C0XINFO_INT_SCANLINES_PER_FRAME);
1248
1249   /* initialize the scanline handling portion */
1250   ppu2c0x->scanline_timer = timer_alloc(device->machine, scanline_callback, (void *) device);
1251   timer_adjust_oneshot(ppu2c0x->scanline_timer, video_screen_get_time_until_pos(device->machine->primary_screen, 1, 0), 0);
1252
1253   ppu2c0x->hblank_timer = timer_alloc(device->machine, hblank_callback, (void *) device);
1254   timer_adjust_oneshot(ppu2c0x->hblank_timer, cputag_clocks_to_attotime(device->machine, "maincpu", 86.67), 0); // ??? FIXME - hardcoding NTSC, need better calculation
1255
1256   ppu2c0x->nmi_timer = timer_alloc(device->machine, nmi_callback, (void *) device);
1257   timer_adjust_oneshot(ppu2c0x->nmi_timer, attotime_never, 0);
1258
1259   ppu2c0x->nmi_callback_proc = intf->nmi_handler;
1260   ppu2c0x->color_base = intf->color_base;
1261
1262   /* allocate a screen bitmap, videomem and spriteram, a dirtychar array and the monochromatic colortable */
1263   ppu2c0x->bitmap = auto_bitmap_alloc(device->machine, VISIBLE_SCREEN_WIDTH, VISIBLE_SCREEN_HEIGHT, video_screen_get_format(device->machine->primary_screen));
1264   ppu2c0x->spriteram = auto_alloc_array_clear(device->machine, UINT8, SPRITERAM_SIZE);
1265   ppu2c0x->colortable = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable));
1266   ppu2c0x->colortable_mono = auto_alloc_array(device->machine, pen_t, ARRAY_LENGTH(default_colortable_mono));
1267
1268   state_save_register_device_item(device, 0, ppu2c0x->scanline);
1269   state_save_register_device_item(device, 0, ppu2c0x->refresh_data);
1270   state_save_register_device_item(device, 0, ppu2c0x->refresh_latch);
1271   state_save_register_device_item(device, 0, ppu2c0x->x_fine);
1272   state_save_register_device_item(device, 0, ppu2c0x->toggle);
1273   state_save_register_device_item(device, 0, ppu2c0x->add);
1274   state_save_register_device_item(device, 0, ppu2c0x->videomem_addr);
1275   state_save_register_device_item(device, 0, ppu2c0x->addr_latch);
1276   state_save_register_device_item(device, 0, ppu2c0x->data_latch);
1277   state_save_register_device_item(device, 0, ppu2c0x->buffered_data);
1278   state_save_register_device_item(device, 0, ppu2c0x->tile_page);
1279   state_save_register_device_item(device, 0, ppu2c0x->sprite_page);
1280   state_save_register_device_item(device, 0, ppu2c0x->back_color);
1281   state_save_register_device_item(device, 0, ppu2c0x->scan_scale);
1282   state_save_register_device_item(device, 0, ppu2c0x->scanlines_per_frame);
1283   state_save_register_device_item_array(device, 0, ppu2c0x->regs);
1284   state_save_register_device_item_array(device, 0, ppu2c0x->palette_ram);
1285   state_save_register_device_item_pointer(device, 0, ppu2c0x->spriteram, SPRITERAM_SIZE);
1286   state_save_register_device_item_pointer(device, 0, ppu2c0x->colortable, ARRAY_LENGTH(default_colortable));
1287   state_save_register_device_item_pointer(device, 0, ppu2c0x->colortable_mono, ARRAY_LENGTH(default_colortable_mono));
1288   state_save_register_device_item_bitmap(device, 0, ppu2c0x->bitmap);
1289}
1290
1291/*************************************
1292 *
1293 *  PPU Reset
1294 *
1295 *************************************/
1296
1297static DEVICE_RESET( ppu2c0x )
1298{
1299   ppu2c0x_state *ppu2c0x = get_token(device);
1300   const ppu2c0x_interface *intf = get_interface(device);
1301   int i;
1302
1303   /* reset the scanline count */
1304   ppu2c0x->scanline = 0;
1305
1306   /* set the scan scale (this is for dual monitor vertical setups) */
1307   ppu2c0x->scan_scale = 1;
1308
1309   /* reset the callbacks */
1310   ppu2c0x->scanline_callback_proc = NULL;
1311   ppu2c0x->vidaccess_callback_proc = NULL;
1312
1313   for (i = 0; i < PPU_MAX_REG; i++)
1314      ppu2c0x->regs[i] = 0;
1315
1316   memset(ppu2c0x->palette_ram, 0, ARRAY_LENGTH(ppu2c0x->palette_ram));
1317
1318   /* initialize the rest of the members */
1319   ppu2c0x->refresh_data = 0;
1320   ppu2c0x->refresh_latch = 0;
1321   ppu2c0x->x_fine = 0;
1322   ppu2c0x->toggle = 0;
1323   ppu2c0x->add = 1;
1324   ppu2c0x->videomem_addr = 0;
1325   ppu2c0x->addr_latch = 0;
1326   ppu2c0x->data_latch = 0;
1327   ppu2c0x->tile_page = 0;
1328   ppu2c0x->sprite_page = 0;
1329   ppu2c0x->back_color = 0;
1330   ppu2c0x->buffered_data = 0;
1331
1332   /* initialize the color tables */
1333   {
1334      int color_base = intf->color_base;
1335
1336      for (i = 0; i < ARRAY_LENGTH(default_colortable_mono); i++)
1337      {
1338         /* monochromatic table */
1339         ppu2c0x->colortable_mono[i] = default_colortable_mono[i] + color_base;
1340
1341         /* color table */
1342         ppu2c0x->colortable[i] = default_colortable[i] + color_base;
1343      }
1344   }
1345}
1346
13231347/***************************************************************************
13241348    GET INFO FUNCTIONS
13251349***************************************************************************/
r7669r7670
13291353   switch (state)
13301354   {
13311355      /* --- the following bits of info are returned as 64-bit signed integers --- */
1332      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(ppu2c0x_chip);            break;
1356      case DEVINFO_INT_TOKEN_BYTES:               info->i = sizeof(ppu2c0x_state);            break;
13331357      case DEVINFO_INT_INLINE_CONFIG_BYTES:         info->i = 0;                        break;
13341358      case DEVINFO_INT_CLASS:                     info->i = DEVICE_CLASS_PERIPHERAL;         break;
13351359      case PPU2C0XINFO_INT_SCANLINES_PER_FRAME:      info->i = PPU_NTSC_SCANLINES_PER_FRAME;      break;
src/mame/video/ppu2c0x.h
r7669r7670
1616***************************************************************************/
1717
1818/* mirroring types */
19#define PPU_MIRROR_NONE      0
20#define PPU_MIRROR_VERT      1
21#define PPU_MIRROR_HORZ      2
22#define PPU_MIRROR_HIGH      3
23#define PPU_MIRROR_LOW      4
24#define PPU_MIRROR_4SCREEN   5   // Same effect as NONE, but signals that we should never mirror
19#define PPU_MIRROR_NONE       0
20#define PPU_MIRROR_VERT       1
21#define PPU_MIRROR_HORZ       2
22#define PPU_MIRROR_HIGH       3
23#define PPU_MIRROR_LOW        4
24#define PPU_MIRROR_4SCREEN    5   // Same effect as NONE, but signals that we should never mirror
2525
2626/* registers definition */
2727enum
r7669r7670
4040/* bit definitions for (some of) the registers */
4141enum
4242{
43   PPU_CONTROL0_INC         = 0x04,
44   PPU_CONTROL0_SPR_SELECT      = 0x08,
45   PPU_CONTROL0_CHR_SELECT      = 0x10,
46   PPU_CONTROL0_SPRITE_SIZE   = 0x20,
47   PPU_CONTROL0_NMI         = 0x80,
43   PPU_CONTROL0_INC              = 0x04,
44   PPU_CONTROL0_SPR_SELECT       = 0x08,
45   PPU_CONTROL0_CHR_SELECT       = 0x10,
46   PPU_CONTROL0_SPRITE_SIZE      = 0x20,
47   PPU_CONTROL0_NMI              = 0x80,
4848
49   PPU_CONTROL1_DISPLAY_MONO   = 0x01,
50   PPU_CONTROL1_BACKGROUND_L8   = 0x02,
51   PPU_CONTROL1_SPRITES_L8      = 0x04,
52   PPU_CONTROL1_BACKGROUND      = 0x08,
53   PPU_CONTROL1_SPRITES      = 0x10,
54   PPU_CONTROL1_COLOR_EMPHASIS   = 0xe0,
49   PPU_CONTROL1_DISPLAY_MONO     = 0x01,
50   PPU_CONTROL1_BACKGROUND_L8    = 0x02,
51   PPU_CONTROL1_SPRITES_L8       = 0x04,
52   PPU_CONTROL1_BACKGROUND       = 0x08,
53   PPU_CONTROL1_SPRITES          = 0x10,
54   PPU_CONTROL1_COLOR_EMPHASIS   = 0xe0,
5555
56   PPU_STATUS_8SPRITES         = 0x20,
57   PPU_STATUS_SPRITE0_HIT      = 0x40,
58   PPU_STATUS_VBLANK         = 0x80
56   PPU_STATUS_8SPRITES           = 0x20,
57   PPU_STATUS_SPRITE0_HIT        = 0x40,
58   PPU_STATUS_VBLANK             = 0x80
5959};
6060
6161enum
6262{
63   PPU_NTSC_SCANLINES_PER_FRAME   = 262,
64   PPU_PAL_SCANLINES_PER_FRAME      = 312,
63   PPU_NTSC_SCANLINES_PER_FRAME  = 262,
64   PPU_PAL_SCANLINES_PER_FRAME   = 312,
6565
66   PPU_BOTTOM_VISIBLE_SCANLINE      = 239,
67   PPU_VBLANK_FIRST_SCANLINE      = 241,
68   PPU_VBLANK_LAST_SCANLINE_NTSC   = 260,
69   PPU_VBLANK_LAST_SCANLINE_PAL   = 310
66   PPU_BOTTOM_VISIBLE_SCANLINE   = 239,
67   PPU_VBLANK_FIRST_SCANLINE     = 241,
68   PPU_VBLANK_LAST_SCANLINE_NTSC = 260,
69   PPU_VBLANK_LAST_SCANLINE_PAL  = 310
7070
7171   // Both the sacnline immediately before and immediately after VBLANK
7272   // are non-rendering and non-vblank.
r7669r7670
8787typedef struct _ppu2c0x_interface ppu2c0x_interface;
8888struct _ppu2c0x_interface
8989{
90   int            gfx_layout_number;      /* gfx layout number used by each chip */
91   int            color_base;            /* color base to use per ppu */
92   int            mirroring;            /* mirroring options (PPU_MIRROR_* flag) */
93   ppu2c0x_nmi_cb   nmi_handler;         /* NMI handler */
90   int               gfx_layout_number;      /* gfx layout number used by each chip */
91   int               color_base;            /* color base to use per ppu */
92   int               mirroring;            /* mirroring options (PPU_MIRROR_* flag) */
93   ppu2c0x_nmi_cb    nmi_handler;         /* NMI handler */
9494};
9595
9696
r7669r7670
128128    DEVICE CONFIGURATION MACROS
129129***************************************************************************/
130130
131#define MDRV_PPU2C02_ADD(_tag, _intrf) \
131#define MDRV_PPU2C02_ADD(_tag, _intrf)   \
132132   MDRV_DEVICE_ADD(_tag, PPU_2C02, 0) \
133133   MDRV_DEVICE_CONFIG(_intrf)
134134
135#define MDRV_PPU2C03B_ADD(_tag, _intrf) \
135#define MDRV_PPU2C03B_ADD(_tag, _intrf)   \
136136   MDRV_DEVICE_ADD(_tag, PPU_2C03B, 0) \
137137   MDRV_DEVICE_CONFIG(_intrf)
138138
139#define MDRV_PPU2C04_ADD(_tag, _intrf) \
139#define MDRV_PPU2C04_ADD(_tag, _intrf)   \
140140   MDRV_DEVICE_ADD(_tag, PPU_2C04, 0) \
141141   MDRV_DEVICE_CONFIG(_intrf)
142142
143#define MDRV_PPU2C05_ADD(_tag, _intrf) \
143#define MDRV_PPU2C05_ADD(_tag, _intrf)   \
144144   MDRV_DEVICE_ADD(_tag, PPU_2C05, 0) \
145145   MDRV_DEVICE_CONFIG(_intrf)
146146
147#define MDRV_PPU2C07_ADD(_tag, _intrf) \
147#define MDRV_PPU2C07_ADD(_tag, _intrf)   \
148148   MDRV_DEVICE_ADD(_tag, PPU_2C07, 0) \
149149   MDRV_DEVICE_CONFIG(_intrf)
150150

Previous 15994 Revisions Next


© 1998-2018 The MESS Team