Previous 15994 Revisions Next

r7669 Tuesday 30th March, 2010 at 22:47:07 UTC by Fabio Priuli
[SNES] wip:
- fixed a SRAM size problem which caused copy-protection checks to fail in various games (e.g. Super Moero Pro Yakyuu! or Super Tetris 3)
- moved PPU registers handling to video/snes.c
[src/mame/includes]snes.h
[src/mame/machine]snes.c
[src/mame/video]snes.c

src/mame/machine/snes.c
r7668r7669
3030UINT8 snes_has_addon_chip;
3131UINT32 snes_rom_size;
3232
33// full graphic variables
34static const UINT16 vram_fgr_inctab[4] = { 1, 32, 128, 128 };
35static const UINT16 vram_fgr_inccnts[4] = { 0, 32, 64, 128 };
36static const UINT16 vram_fgr_shiftab[4] = { 0, 5, 6, 7 };
37
3833static void snes_dma(const address_space *space, UINT8 channels);
3934static void snes_hdma_init(const address_space *space);
4035static void snes_hdma(const address_space *space);
41static READ8_HANDLER(snes_open_bus_r);
4236
4337struct snes_cart_info snes_cart;
4438
r7668r7669
6054
6155*************************************/
6256
63// utility function - latches the H/V counters.  Used by IRQ, writes to WRIO, etc.
64void snes_latch_counters( running_machine *machine )
65{
66   snes_state *state = (snes_state *)machine->driver_data;
67
68   snes_ppu.beam.current_horz = video_screen_get_hpos(machine->primary_screen) / state->htmult;
69   snes_ppu.beam.latch_vert = video_screen_get_vpos(machine->primary_screen);
70   snes_ppu.beam.latch_horz = snes_ppu.beam.current_horz;
71   snes_ram[STAT78] |= 0x40;   // indicate we latched
72//  state->read_ophct = state->read_opvct = 0;    // clear read flags - 2009-08: I think we must clear these when STAT78 is read...
73
74//  printf("latched @ H %d V %d\n", snes_ppu.beam.latch_horz, snes_ppu.beam.latch_vert);
75}
76
7757static TIMER_CALLBACK( snes_nmi_tick )
7858{
7959   snes_state *state = (snes_state *)machine->driver_data;
r7668r7669
298278
299279*************************************/
300280
301static void snes_dynamic_res_change( running_machine *machine )
281READ8_HANDLER( snes_open_bus_r )
302282{
303   snes_state *state = (snes_state *)machine->driver_data;
304   rectangle visarea = *video_screen_get_visible_area(machine->primary_screen);
305   attoseconds_t refresh;
306
307   visarea.min_x = visarea.min_y = 0;
308   visarea.max_y = snes_ppu.beam.last_visible_line*snes_ppu.interlace - 1;
309   visarea.max_x = (SNES_SCR_WIDTH * 2) - 1;
310
311   // fixme: should compensate for SNES_DBG_video
312   if (snes_ppu.mode == 5 || snes_ppu.mode == 6 || snes_ppu.pseudo_hires)
313      state->htmult = 2;
314   else
315      state->htmult = 1;
316
317   /* FIXME: does the timing changes when the gfx mode is equal to 5 or 6? */
318   if ((snes_ram[STAT78] & 0x10) == SNES_NTSC)
319      refresh = HZ_TO_ATTOSECONDS(DOTCLK_NTSC) * SNES_HTOTAL * SNES_VTOTAL_NTSC;
320   else
321      refresh = HZ_TO_ATTOSECONDS(DOTCLK_PAL) * SNES_HTOTAL * SNES_VTOTAL_PAL;
322
323   if ((snes_ram[STAT78] & 0x10) == SNES_NTSC)
324      video_screen_configure(machine->primary_screen, SNES_HTOTAL*2, SNES_VTOTAL_NTSC*snes_ppu.interlace, &visarea, refresh);
325   else
326      video_screen_configure(machine->primary_screen, SNES_HTOTAL*2, SNES_VTOTAL_PAL*snes_ppu.interlace, &visarea, refresh);
327}
328
329static READ8_HANDLER( snes_open_bus_r )
330{
331283   static UINT8 recurse = 0;
332284   UINT16 result;
333285
r7668r7669
341293   return result;
342294}
343295
344/*************************************************
345
346 SNES VRAM accesses:
347
348 VRAM accesses during active display are invalid.
349 Unlike OAM and CGRAM, they will not be written
350 anywhere at all. Thanks to byuu's researches,
351 the ranges where writes are invalid have been
352 validated on hardware, as has the edge case where
353 the S-CPU open bus can be written if the write
354 occurs during the very last clock cycle of
355 vblank.
356 Our implementation could be not 100% accurate
357 when interlace is active.
358*************************************************/
359
360INLINE UINT32 snes_get_vram_address( running_machine *machine )
361{
362   snes_state *state = (snes_state *)machine->driver_data;
363   UINT32 addr = (snes_ram[VMADDH] << 8) | snes_ram[VMADDL];
364
365   if (state->vram_fgr_count)
366   {
367      UINT32 rem = addr & state->vram_fgr_mask;
368      UINT32 faddr = (addr & ~state->vram_fgr_mask) + (rem >> state->vram_fgr_shift) + ((rem & (state->vram_fgr_count - 1)) << 3);
369      return faddr;
370   }
371
372   return addr;
373}
374
375static READ8_HANDLER( snes_vram_read )
376{
377   UINT8 res = 0;
378   offset &= 0x1ffff;
379
380   if (snes_ppu.screen_disabled)
381      res = snes_vram[offset];
382   else
383   {
384      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
385      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
386      UINT16 ls = (((snes_ram[STAT78] & 0x10) == SNES_NTSC ? 525 : 625) >> 1) - 1;
387
388      if (snes_ppu.interlace == 2)
389         ls++;
390
391      if (v == ls && h == 1362)
392         res = 0;
393      else if (v < snes_ppu.beam.last_visible_line - 1)
394         res = 0;
395      else if (v == snes_ppu.beam.last_visible_line - 1)
396      {
397         if (h == 1362)
398            res = snes_vram[offset];
399         else
400            res = 0;
401      }
402      else
403         res = snes_vram[offset];
404   }
405   return res;
406}
407
408static WRITE8_HANDLER( snes_vram_write )
409{
410   offset &= 0x1ffff;
411
412   if (snes_ppu.screen_disabled)
413      snes_vram[offset] = data;
414   else
415   {
416      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
417      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
418      if (v == 0)
419      {
420         if (h <= 4)
421            snes_vram[offset] = data;
422         else if (h == 6)
423            snes_vram[offset] = snes_open_bus_r(space, 0);
424         else
425         {
426            //no write
427         }
428      }
429      else if (v < snes_ppu.beam.last_visible_line)
430      {
431         //no write
432      }
433      else if (v == snes_ppu.beam.last_visible_line)
434      {
435         if (h <= 4)
436         {
437            //no write
438         }
439         else
440            snes_vram[offset] = data;
441      }
442      else
443         snes_vram[offset] = data;
444   }
445}
446
447/*************************************************
448
449 SNES OAM accesses:
450
451 OAM accesses during active display are allowed.
452 The actual address varies during rendering, as the
453 PPU reads in data itself for processing.
454 Unfortunately, no one has been able (yet) to
455 determine how this works. The only known game to
456 actually access OAM during active display is
457 Uniracers and it expects accesses to map to
458 offset 0x0218. Hence, following byuu's choice
459 we rerouted OAM accesses during active display
460 to 0x0218 (0x010c in our snes_oam).
461 This is a hack, but it is more accurate than
462 writing to the 'expected' address set by
463 $2102,$2103.
464
465 Notice that, since snes_ram[OAMDATA] is never
466 read/written directly, we use it as an index
467 to choose the high/low byte of the snes_oam word.
468*************************************************/
469
470static READ8_HANDLER( snes_oam_read )
471{
472   offset &= 0x1ff;
473
474   if (offset & 0x100)
475      offset &= 0x10f;
476
477   if (!snes_ppu.screen_disabled)
478   {
479      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
480
481      if (v < snes_ppu.beam.last_visible_line)
482         offset = 0x010c;
483   }
484
485   return (snes_oam[offset] >> (snes_ram[OAMDATA] << 3)) & 0xff;;
486}
487
488static WRITE8_HANDLER( snes_oam_write )
489{
490   offset &= 0x1ff;
491
492   if (offset & 0x100)
493      offset &= 0x10f;
494
495   if (!snes_ppu.screen_disabled)
496   {
497      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
498
499      if (v < snes_ppu.beam.last_visible_line)
500         offset = 0x010c;
501   }
502
503   if (!(snes_ram[OAMDATA]))
504      snes_oam[offset] = (snes_oam[offset] & 0xff00) | (data << 0);
505   else
506      snes_oam[offset] = (snes_oam[offset] & 0x00ff) | (data << 8);
507}
508
509/*************************************************
510
511 SNES CGRAM accesses:
512
513 CGRAM writes during hblank are valid. During
514 active display, the actual address the data
515 is written to varies, as the PPU itself changes
516 the address. Like OAM, it is not known the exact
517 algorithm used, but no commercial software seems
518 to attempt this. While byuu, in his emu, maps
519 those accesses to 0x01ff, because it is more
520 accurate to invalidate the 'expected' address
521 than not, MESS has issues if we don't write to
522 the expected address (see e.g. Tokimeki Memorial).
523 This is because writes should work during hblank
524 (so that the game can produce color fading), but
525 ends up not working with the conditions below.
526 Hence, for the moment, we only document the
527 solution adopted by BSNES without enabling it.
528*************************************************/
529
530static READ8_HANDLER( snes_cgram_read )
531{
532   UINT8 res = 0;
533   offset &= 0x1ff;
534
535#if 0
536   if (!snes_ppu.screen_disabled)
537   {
538      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
539      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
540
541      if (v < snes_ppu.beam.last_visible_line && h >= 128 && h < 1096)
542         offset = 0x1ff;
543   }
544#endif
545
546   res = ((UINT8 *)snes_cgram)[offset];
547
548   // CGRAM palette data format is 15-bits (0,bbbbb,ggggg,rrrrr).
549   // Highest bit is simply ignored.
550   if (offset & 0x01)
551      res &= 0x7f;
552
553   return res;
554}
555
556static WRITE8_HANDLER( snes_cgram_write )
557{
558   offset &= 0x1ff;
559
560#if 0
561   // FIXME: this currently breaks some games (e.g. Tokimeki Memorial),
562   // even if it's expected to be more accurate than allowing for
563   // writes to the cgram address
564   if (!snes_ppu.screen_disabled)
565   {
566      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
567      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
568
569      if (v < snes_ppu.beam.last_visible_line && h >= 128 && h < 1096)
570         offset = 0x1ff;
571   }
572#endif
573
574   // CGRAM palette data format is 15-bits (0,bbbbb,ggggg,rrrrr).
575   // Highest bit is simply ignored.
576   if (offset & 0x01)
577      data &= 0x7f;
578
579   ((UINT8 *)snes_cgram)[offset] = data;
580}
581
582296/*
583297 * DR   - Double read : address is read twice to return a 16bit value.
584298 * low  - This is the low byte of a 16 or 24 bit value
r7668r7669
590304   snes_state *state = (snes_state *)space->machine->driver_data;
591305   UINT8 value = 0;
592306
307   // PPU accesses are from 2100 to 213f
308   if (offset >= INIDISP && offset < APU00)
309   {
310      return snes_ppu_read(space, offset);
311   }
312
593313   // APU is mirrored from 2140 to 217f
594314   if (offset >= APU00 && offset < WMDATA)
595315   {
r7668r7669
633353   /* offset is from 0x000000 */
634354   switch (offset)
635355   {
636      case OAMDATA:   /* 21xy for x=0,1,2 and y=4,5,6,8,9,a returns PPU1 open bus*/
637      case BGMODE:
638      case MOSAIC:
639      case BG2SC:
640      case BG3SC:
641      case BG4SC:
642      case BG4VOFS:
643      case VMAIN:
644      case VMADDL:
645      case VMDATAL:
646      case VMDATAH:
647      case M7SEL:
648      case W34SEL:
649      case WOBJSEL:
650      case WH0:
651      case WH2:
652      case WH3:
653      case WBGLOG:
654         return snes_ppu.ppu1_open_bus;
655
656      case MPYL:      /* Multiplication result (low) */
657         {
658            /* Perform 16bit * 8bit multiply */
659            UINT32 c = (INT16)snes_ppu.mode7.matrix_a * (INT8)(snes_ppu.mode7.matrix_b >> 8);
660            snes_ppu.ppu1_open_bus = c & 0xff;
661            return snes_ppu.ppu1_open_bus;
662         }
663      case MPYM:      /* Multiplication result (mid) */
664         {
665            /* Perform 16bit * 8bit multiply */
666            UINT32 c = (INT16)snes_ppu.mode7.matrix_a * (INT8)(snes_ppu.mode7.matrix_b >> 8);
667            snes_ppu.ppu1_open_bus = (c >> 8) & 0xff;
668            return snes_ppu.ppu1_open_bus;
669         }
670      case MPYH:      /* Multiplication result (high) */
671         {
672            /* Perform 16bit * 8bit multiply */
673            UINT32 c = (INT16)snes_ppu.mode7.matrix_a * (INT8)(snes_ppu.mode7.matrix_b >> 8);
674            snes_ppu.ppu1_open_bus = (c >> 16) & 0xff;
675            return snes_ppu.ppu1_open_bus;
676         }
677      case SLHV:      /* Software latch for H/V counter */
678         snes_latch_counters(space->machine);
679         return snes_open_bus_r(space, 0);      /* Return value is meaningless */
680      case ROAMDATA:   /* Read data from OAM (DR) */
681         snes_ppu.ppu1_open_bus = snes_oam_read(space, snes_ppu.oam.address);
682         snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2;
683         if (!snes_ram[OAMDATA])
684         {
685            snes_ppu.oam.address++;
686            snes_ppu.oam.address &= 0x1ff;
687            snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
688         }
689         return snes_ppu.ppu1_open_bus;
690      case RVMDATAL:   /* Read data from VRAM (low) */
691         {
692            UINT32 addr = snes_get_vram_address(space->machine) << 1;
693            snes_ppu.ppu1_open_bus = state->vram_read_buffer & 0xff;
694
695            if (!state->vram_fgr_high)
696            {
697               state->vram_read_buffer = snes_vram_read(space, addr);
698               state->vram_read_buffer |= (snes_vram_read(space, addr + 1) << 8);
699
700               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
701               snes_ram[VMADDL] = addr & 0xff;
702               snes_ram[VMADDH] = (addr >> 8) & 0xff;
703            }
704
705            return snes_ppu.ppu1_open_bus;
706         }
707      case RVMDATAH:   /* Read data from VRAM (high) */
708         {
709            UINT32 addr = snes_get_vram_address(space->machine) << 1;
710            snes_ppu.ppu1_open_bus = (state->vram_read_buffer >> 8) & 0xff;
711
712            if (state->vram_fgr_high)
713            {
714               state->vram_read_buffer = snes_vram_read(space, addr);
715               state->vram_read_buffer |= (snes_vram_read(space, addr + 1) << 8);
716
717               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
718               snes_ram[VMADDL] = addr & 0xff;
719               snes_ram[VMADDH] = (addr >> 8) & 0xff;
720            }
721
722            return snes_ppu.ppu1_open_bus;
723         }
724      case RCGDATA:   /* Read data from CGRAM */
725         if (!(state->cgram_address & 0x01))
726            snes_ppu.ppu2_open_bus = snes_cgram_read(space, state->cgram_address);
727         else
728         {
729            snes_ppu.ppu2_open_bus &= 0x80;
730            snes_ppu.ppu2_open_bus |= snes_cgram_read(space, state->cgram_address) & 0x7f;
731         }
732
733         state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2);
734         return snes_ppu.ppu2_open_bus;
735      case OPHCT:      /* Horizontal counter data by ext/soft latch */
736         if (state->read_ophct)
737         {
738            snes_ppu.ppu2_open_bus &= 0xfe;
739            snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_horz >> 8) & 0x01;
740         }
741         else
742         {
743            snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_horz & 0xff;
744         }
745         state->read_ophct ^= 1;
746         return snes_ppu.ppu2_open_bus;
747      case OPVCT:      /* Vertical counter data by ext/soft latch */
748         if (state->read_opvct)
749         {
750            snes_ppu.ppu2_open_bus &= 0xfe;
751            snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_vert >> 8) & 0x01;
752         }
753         else
754         {
755            snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_vert & 0xff;
756         }
757         state->read_opvct ^= 1;
758         return snes_ppu.ppu2_open_bus;
759      case STAT77:   /* PPU status flag and version number */
760         value = snes_ppu.stat77_flags & 0xc0; // 0x80 & 0x40 are Time Over / Range Over Sprite flags, set by the video code
761         // 0x20 - Master/slave mode select. Little is known about this bit. We always seem to read back 0 here.
762         value |= (snes_ppu.ppu1_open_bus & 0x10);
763         value |= (snes_ppu.ppu1_version & 0x0f);
764         snes_ppu.stat77_flags = value;   // not sure if this is needed...
765         snes_ppu.ppu1_open_bus = value;
766         return snes_ppu.ppu1_open_bus;
767      case STAT78:   /* PPU status flag and version number */
768         state->read_ophct = 0;
769         state->read_opvct = 0;
770         value = snes_ram[offset];
771         value |= (snes_ppu.ppu2_open_bus & 0x20);
772         value |= (snes_ppu.ppu2_version & 0x0f);
773         snes_ram[offset] = value;   // not sure if this is needed...
774         snes_ppu.ppu2_open_bus = value;
775         return snes_ppu.ppu2_open_bus;
776356      case WMDATA:   /* Data to read from WRAM */
777357         value = memory_read_byte(space, 0x7e0000 + state->wram_address++);
778358         state->wram_address &= 0x1ffff;
r7668r7669
894474{
895475   snes_state *state = (snes_state *)space->machine->driver_data;
896476
477   // PPU accesses are from 2100 to 213f
478   if (offset >= INIDISP && offset < APU00)
479   {
480      snes_ppu_write(space, offset, data);
481      return;
482   }
483
897484   // APU is mirrored from 2140 to 217f
898485   if (offset >= APU00 && offset < WMDATA)
899486   {
r7668r7669
945532   /* offset is from 0x000000 */
946533   switch (offset)
947534   {
948      case INIDISP:   /* Initial settings for screen */
949         if ((snes_ppu.screen_disabled & 0x80) && (!(data & 0x80))) //a 1->0 force blank transition causes a reset OAM address
950         {
951            memory_write_byte(space, OAMADDL, snes_ppu.oam.saved_address_low);
952            memory_write_byte(space, OAMADDH, snes_ppu.oam.saved_address_high);
953            snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
954         }
955         snes_ppu.screen_disabled = data & 0x80;
956         snes_ppu.screen_brightness = (data & 0x0f) + 1;
957         break;
958      case OBSEL:      /* Object size and data area designation */
959         snes_ppu.oam.next_charmap = (data & 0x03) << 1;
960         snes_ppu.oam.next_name_select = (((data & 0x18) >> 3) * 0x1000) << 1;
961         snes_ppu.oam.next_size = (data & 0xe0) >> 5;
962         break;
963      case OAMADDL:   /* Address for accessing OAM (low) */
964         snes_ppu.oam.saved_address_low = data;
965         snes_ppu.oam.address = (snes_ppu.oam.address & 0xff00) + data;
966         snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
967         snes_ram[OAMDATA] = 0;
968         break;
969      case OAMADDH:   /* Address for accessing OAM (high) */
970         snes_ppu.oam.saved_address_high = data;
971         snes_ppu.oam.address = (snes_ppu.oam.address & 0x00ff) | ((data & 0x01) << 8);
972         snes_ppu.oam.priority_rotation = BIT(data, 7);
973         snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
974         snes_ram[OAMDATA] = 0;
975         break;
976      case OAMDATA:   /* Data for OAM write (DW) */
977         if (snes_ppu.oam.address >= 0x100)
978            snes_oam_write(space, snes_ppu.oam.address, data);
979         else
980         {
981            if (!snes_ram[OAMDATA])
982               snes_ppu.oam.write_latch = data;
983            else
984            {
985               // in this case, we not only write data to the upper byte of the word,
986               // but also snes_ppu.oam.write_latch to the lower byte (recall that
987               // snes_ram[OAMDATA] is used to select high/low byte)
988               snes_oam_write(space, snes_ppu.oam.address, data);
989               snes_ram[OAMDATA] = 0;
990               snes_oam_write(space, snes_ppu.oam.address, snes_ppu.oam.write_latch);
991               snes_ram[OAMDATA] = 1;
992            }
993         }
994         snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2;
995         if (!snes_ram[OAMDATA])
996         {
997            snes_ppu.oam.address++;
998            snes_ppu.oam.address &= 0x1ff;
999            snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
1000         }
1001         return;
1002      case BGMODE:   /* BG mode and character size settings */
1003         snes_ppu.mode = data & 0x07;
1004         snes_dynamic_res_change(space->machine);
1005         snes_ppu.bg3_priority_bit = BIT(data, 3);
1006         snes_ppu.layer[SNES_BG1].tile_size = BIT(data, 4);
1007         snes_ppu.layer[SNES_BG2].tile_size = BIT(data, 5);
1008         snes_ppu.layer[SNES_BG3].tile_size = BIT(data, 6);
1009         snes_ppu.layer[SNES_BG4].tile_size = BIT(data, 7);
1010         snes_ppu.update_offsets = 1;
1011         break;
1012      case MOSAIC:   /* Size and screen designation for mosaic */
1013         snes_ppu.mosaic_size = (data & 0xf0) >> 4;
1014         snes_ppu.layer[SNES_BG1].mosaic_enabled = BIT(data, 0);
1015         snes_ppu.layer[SNES_BG2].mosaic_enabled = BIT(data, 1);
1016         snes_ppu.layer[SNES_BG3].mosaic_enabled = BIT(data, 2);
1017         snes_ppu.layer[SNES_BG4].mosaic_enabled = BIT(data, 3);
1018         break;
1019      case BG1SC:      /* Address for storing SC data BG1 SC size designation */
1020      case BG2SC:      /* Address for storing SC data BG2 SC size designation  */
1021      case BG3SC:      /* Address for storing SC data BG3 SC size designation  */
1022      case BG4SC:      /* Address for storing SC data BG4 SC size designation  */
1023         snes_ppu.layer[offset - BG1SC].tilemap = data & 0xfc;
1024         snes_ppu.layer[offset - BG1SC].tilemap_size = data & 0x3;
1025         break;
1026      case BG12NBA:   /* Address for BG 1 and 2 character data */
1027         snes_ppu.layer[SNES_BG1].charmap = (data & 0x0f);
1028         snes_ppu.layer[SNES_BG2].charmap = (data & 0xf0) >> 4;
1029         break;
1030      case BG34NBA:   /* Address for BG 3 and 4 character data */
1031         snes_ppu.layer[SNES_BG3].charmap = (data & 0x0f);
1032         snes_ppu.layer[SNES_BG4].charmap = (data & 0xf0) >> 4;
1033         break;
1034
1035      // Anomie says "H Current = (Byte<<8) | (Prev&~7) | ((Current>>8)&7); V Current = (Current<<8) | Prev;" and Prev is shared by all scrolls but in Mode 7!
1036      case BG1HOFS:   /* BG1 - horizontal scroll (DW) */
1037         /* In Mode 0->6 we use ppu_last_scroll as Prev */
1038         snes_ppu.layer[SNES_BG1].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG1].hoffs >> 8) & 7);
1039         snes_ppu.ppu_last_scroll = data;
1040         /* In Mode 7 we use mode7_last_scroll as Prev */
1041         snes_ppu.mode7.hor_offset = (data << 8) | (snes_ppu.mode7_last_scroll & ~7) | ((snes_ppu.mode7.hor_offset >> 8) & 7);
1042         snes_ppu.mode7_last_scroll = data;
1043         snes_ppu.update_offsets = 1;
1044         return;
1045      case BG1VOFS:   /* BG1 - vertical scroll (DW) */
1046         /* In Mode 0->6 we use ppu_last_scroll as Prev */
1047         snes_ppu.layer[SNES_BG1].voffs = (data << 8) | snes_ppu.ppu_last_scroll;
1048         snes_ppu.ppu_last_scroll = data;
1049         /* In Mode 7 we use mode7_last_scroll as Prev */
1050         snes_ppu.mode7.ver_offset = (data << 8) | snes_ppu.mode7_last_scroll;
1051         snes_ppu.mode7_last_scroll = data;
1052         snes_ppu.update_offsets = 1;
1053         return;
1054      case BG2HOFS:   /* BG2 - horizontal scroll (DW) */
1055         snes_ppu.layer[SNES_BG2].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG2].hoffs >> 8) & 7);
1056         snes_ppu.ppu_last_scroll = data;
1057         snes_ppu.update_offsets = 1;
1058         return;
1059      case BG2VOFS:   /* BG2 - vertical scroll (DW) */
1060         snes_ppu.layer[SNES_BG2].voffs = (data << 8) | (snes_ppu.ppu_last_scroll);
1061         snes_ppu.ppu_last_scroll = data;
1062         snes_ppu.update_offsets = 1;
1063         return;
1064      case BG3HOFS:   /* BG3 - horizontal scroll (DW) */
1065         snes_ppu.layer[SNES_BG3].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG3].hoffs >> 8) & 7);
1066         snes_ppu.ppu_last_scroll = data;
1067         snes_ppu.update_offsets = 1;
1068         return;
1069      case BG3VOFS:   /* BG3 - vertical scroll (DW) */
1070         snes_ppu.layer[SNES_BG3].voffs = (data << 8) | (snes_ppu.ppu_last_scroll);
1071         snes_ppu.ppu_last_scroll = data;
1072         snes_ppu.update_offsets = 1;
1073         return;
1074      case BG4HOFS:   /* BG4 - horizontal scroll (DW) */
1075         snes_ppu.layer[SNES_BG4].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG4].hoffs >> 8) & 7);
1076         snes_ppu.ppu_last_scroll = data;
1077         snes_ppu.update_offsets = 1;
1078         return;
1079      case BG4VOFS:   /* BG4 - vertical scroll (DW) */
1080         snes_ppu.layer[SNES_BG4].voffs = (data << 8) | (snes_ppu.ppu_last_scroll);
1081         snes_ppu.ppu_last_scroll = data;
1082         snes_ppu.update_offsets = 1;
1083         return;
1084      case VMAIN:      /* VRAM address increment value designation */
1085         state->vram_fgr_high = (data & 0x80);
1086         state->vram_fgr_increment = vram_fgr_inctab[data & 3];
1087
1088         if (data & 0xc)
1089         {
1090            int md = (data & 0xc) >> 2;
1091
1092            state->vram_fgr_count = vram_fgr_inccnts[md];         // 0x20, 0x40, 0x80
1093            state->vram_fgr_mask = (state->vram_fgr_count * 8) - 1;   // 0xff, 0x1ff, 0x2ff
1094            state->vram_fgr_shift = vram_fgr_shiftab[md];         // 5, 6, 7
1095         }
1096         else
1097         {
1098            state->vram_fgr_count = 0;
1099         }
1100//          printf("VMAIN: high %x inc %x count %x mask %x shift %x\n", state->vram_fgr_high, state->vram_fgr_increment, state->vram_fgr_count, state->vram_fgr_mask, state->vram_fgr_shift);
1101         break;
1102      case VMADDL:   /* Address for VRAM read/write (low) */
1103      case VMADDH:   /* Address for VRAM read/write (high) */
1104         {
1105            UINT32 addr;
1106            snes_ram[offset] = data;
1107            addr = snes_get_vram_address(space->machine) << 1;
1108            state->vram_read_buffer = snes_vram_read(space, addr);
1109            state->vram_read_buffer |= (snes_vram_read(space, addr + 1) << 8);
1110         }
1111         break;
1112      case VMDATAL:   /* 2118: Data for VRAM write (low) */
1113         {
1114            UINT32 addr = snes_get_vram_address(space->machine) << 1;
1115            snes_vram_write(space, addr, data);
1116
1117            if (!state->vram_fgr_high)
1118            {
1119               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
1120               snes_ram[VMADDL] = addr & 0xff;
1121               snes_ram[VMADDH] = (addr >> 8) & 0xff;
1122            }
1123         }
1124         return;
1125      case VMDATAH:   /* 2119: Data for VRAM write (high) */
1126         {
1127            UINT32 addr = snes_get_vram_address(space->machine) << 1;
1128            snes_vram_write(space, addr + 1, data);
1129
1130            if (state->vram_fgr_high)
1131            {
1132               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
1133               snes_ram[VMADDL] = addr & 0xff;
1134               snes_ram[VMADDH] = (addr >> 8) & 0xff;
1135            }
1136         }
1137         return;
1138      case M7SEL:      /* Mode 7 initial settings */
1139         snes_ppu.mode7.repeat = (data >> 6) & 3;
1140         snes_ppu.mode7.vflip  = BIT(data, 1);
1141         snes_ppu.mode7.hflip  = BIT(data, 0);
1142         break;
1143      /* As per Anomie's doc: Reg = (Current<<8) | Prev; and there is only one Prev, shared by these matrix regs and Mode 7 scroll regs */
1144      case M7A:      /* Mode 7 COS angle/x expansion (DW) */
1145         snes_ppu.mode7.matrix_a = snes_ppu.mode7_last_scroll + (data << 8);
1146         snes_ppu.mode7_last_scroll = data;
1147         break;
1148      case M7B:      /* Mode 7 SIN angle/ x expansion (DW) */
1149         snes_ppu.mode7.matrix_b = snes_ppu.mode7_last_scroll + (data << 8);
1150         snes_ppu.mode7_last_scroll = data;
1151         break;
1152      case M7C:      /* Mode 7 SIN angle/y expansion (DW) */
1153         snes_ppu.mode7.matrix_c = snes_ppu.mode7_last_scroll + (data << 8);
1154         snes_ppu.mode7_last_scroll = data;
1155         break;
1156      case M7D:      /* Mode 7 COS angle/y expansion (DW) */
1157         snes_ppu.mode7.matrix_d = snes_ppu.mode7_last_scroll + (data << 8);
1158         snes_ppu.mode7_last_scroll = data;
1159         break;
1160      case M7X:      /* Mode 7 x center position (DW) */
1161         snes_ppu.mode7.origin_x = snes_ppu.mode7_last_scroll + (data << 8);
1162         snes_ppu.mode7_last_scroll = data;
1163         break;
1164      case M7Y:      /* Mode 7 y center position (DW) */
1165         snes_ppu.mode7.origin_y = snes_ppu.mode7_last_scroll + (data << 8);
1166         snes_ppu.mode7_last_scroll = data;
1167         break;
1168      case CGADD:      /* Initial address for colour RAM writing */
1169         /* CGRAM is 16-bit, but when reading/writing we treat it as 8-bit, so we need to double the address */
1170         state->cgram_address = data << 1;
1171         break;
1172      case CGDATA:   /* Data for colour RAM */
1173         snes_cgram_write(space, state->cgram_address, data);
1174         state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2);
1175         break;
1176      case W12SEL:   /* Window mask settings for BG1-2 */
1177         if (data != snes_ram[offset])
1178         {
1179            snes_ppu.layer[SNES_BG1].window1_invert  = BIT(data, 0);
1180            snes_ppu.layer[SNES_BG1].window1_enabled = BIT(data, 1);
1181            snes_ppu.layer[SNES_BG1].window2_invert  = BIT(data, 2);
1182            snes_ppu.layer[SNES_BG1].window2_enabled = BIT(data, 3);
1183            snes_ppu.layer[SNES_BG2].window1_invert  = BIT(data, 4);
1184            snes_ppu.layer[SNES_BG2].window1_enabled = BIT(data, 5);
1185            snes_ppu.layer[SNES_BG2].window2_invert  = BIT(data, 6);
1186            snes_ppu.layer[SNES_BG2].window2_enabled = BIT(data, 7);
1187            snes_ppu.update_windows = 1;
1188         }
1189         break;
1190      case W34SEL:   /* Window mask settings for BG3-4 */
1191         if (data != snes_ram[offset])
1192         {
1193            snes_ppu.layer[SNES_BG3].window1_invert  = BIT(data, 0);
1194            snes_ppu.layer[SNES_BG3].window1_enabled = BIT(data, 1);
1195            snes_ppu.layer[SNES_BG3].window2_invert  = BIT(data, 2);
1196            snes_ppu.layer[SNES_BG3].window2_enabled = BIT(data, 3);
1197            snes_ppu.layer[SNES_BG4].window1_invert  = BIT(data, 4);
1198            snes_ppu.layer[SNES_BG4].window1_enabled = BIT(data, 5);
1199            snes_ppu.layer[SNES_BG4].window2_invert  = BIT(data, 6);
1200            snes_ppu.layer[SNES_BG4].window2_enabled = BIT(data, 7);
1201            snes_ppu.update_windows = 1;
1202         }
1203         break;
1204      case WOBJSEL:   /* Window mask settings for objects */
1205         if (data != snes_ram[offset])
1206         {
1207            snes_ppu.layer[SNES_OAM].window1_invert  = BIT(data, 0);
1208            snes_ppu.layer[SNES_OAM].window1_enabled = BIT(data, 1);
1209            snes_ppu.layer[SNES_OAM].window2_invert  = BIT(data, 2);
1210            snes_ppu.layer[SNES_OAM].window2_enabled = BIT(data, 3);
1211            snes_ppu.layer[SNES_COLOR].window1_invert  = BIT(data, 4);
1212            snes_ppu.layer[SNES_COLOR].window1_enabled = BIT(data, 5);
1213            snes_ppu.layer[SNES_COLOR].window2_invert  = BIT(data, 6);
1214            snes_ppu.layer[SNES_COLOR].window2_enabled = BIT(data, 7);
1215            snes_ppu.update_windows = 1;
1216         }
1217         break;
1218      case WH0:      /* Window 1 left position */
1219         if (data != snes_ram[offset])
1220         {
1221            snes_ppu.window1_left = data;
1222            snes_ppu.update_windows = 1;
1223         }
1224         break;
1225      case WH1:      /* Window 1 right position */
1226         if (data != snes_ram[offset])
1227         {
1228            snes_ppu.window1_right = data;
1229            snes_ppu.update_windows = 1;
1230         }
1231         break;
1232      case WH2:      /* Window 2 left position */
1233         if (data != snes_ram[offset])
1234         {
1235            snes_ppu.window2_left = data;
1236            snes_ppu.update_windows = 1;
1237         }
1238         break;
1239      case WH3:      /* Window 2 right position */
1240         if (data != snes_ram[offset])
1241         {
1242            snes_ppu.window2_right = data;
1243            snes_ppu.update_windows = 1;
1244         }
1245         break;
1246      case WBGLOG:   /* Window mask logic for BG's */
1247         if (data != snes_ram[offset])
1248         {
1249            snes_ppu.layer[SNES_BG1].wlog_mask = data & 0x03;
1250            snes_ppu.layer[SNES_BG2].wlog_mask = (data & 0x0c) >> 2;
1251            snes_ppu.layer[SNES_BG3].wlog_mask = (data & 0x30) >> 4;
1252            snes_ppu.layer[SNES_BG4].wlog_mask = (data & 0xc0) >> 6;
1253            snes_ppu.update_windows = 1;
1254         }
1255         break;
1256      case WOBJLOG:   /* Window mask logic for objects */
1257         if (data != snes_ram[offset])
1258         {
1259            snes_ppu.layer[SNES_OAM].wlog_mask = data & 0x03;
1260            snes_ppu.layer[SNES_COLOR].wlog_mask = (data & 0x0c) >> 2;
1261            snes_ppu.update_windows = 1;
1262         }
1263         break;
1264      case TM:      /* Main screen designation */
1265         snes_ppu.layer[SNES_BG1].main_bg_enabled = BIT(data, 0);
1266         snes_ppu.layer[SNES_BG2].main_bg_enabled = BIT(data, 1);
1267         snes_ppu.layer[SNES_BG3].main_bg_enabled = BIT(data, 2);
1268         snes_ppu.layer[SNES_BG4].main_bg_enabled = BIT(data, 3);
1269         snes_ppu.layer[SNES_OAM].main_bg_enabled = BIT(data, 4);
1270         break;
1271      case TS:      /* Subscreen designation */
1272         snes_ppu.layer[SNES_BG1].sub_bg_enabled = BIT(data, 0);
1273         snes_ppu.layer[SNES_BG2].sub_bg_enabled = BIT(data, 1);
1274         snes_ppu.layer[SNES_BG3].sub_bg_enabled = BIT(data, 2);
1275         snes_ppu.layer[SNES_BG4].sub_bg_enabled = BIT(data, 3);
1276         snes_ppu.layer[SNES_OAM].sub_bg_enabled = BIT(data, 4);
1277         break;
1278      case TMW:      /* Window mask for main screen designation */
1279         snes_ppu.layer[SNES_BG1].main_window_enabled = BIT(data, 0);
1280         snes_ppu.layer[SNES_BG2].main_window_enabled = BIT(data, 1);
1281         snes_ppu.layer[SNES_BG3].main_window_enabled = BIT(data, 2);
1282         snes_ppu.layer[SNES_BG4].main_window_enabled = BIT(data, 3);
1283         snes_ppu.layer[SNES_OAM].main_window_enabled = BIT(data, 4);
1284         break;
1285      case TSW:      /* Window mask for subscreen designation */
1286         snes_ppu.layer[SNES_BG1].sub_window_enabled = BIT(data, 0);
1287         snes_ppu.layer[SNES_BG2].sub_window_enabled = BIT(data, 1);
1288         snes_ppu.layer[SNES_BG3].sub_window_enabled = BIT(data, 2);
1289         snes_ppu.layer[SNES_BG4].sub_window_enabled = BIT(data, 3);
1290         snes_ppu.layer[SNES_OAM].sub_window_enabled = BIT(data, 4);
1291         break;
1292      case CGWSEL:   /* Initial settings for Fixed colour addition or screen addition */
1293         snes_ppu.clip_to_black = (data >> 6) & 0x03;
1294         snes_ppu.prevent_color_math = (data >> 4) & 0x03;
1295         snes_ppu.sub_add_mode = BIT(data, 1);
1296         snes_ppu.direct_color = BIT(data, 0);
1297#ifdef SNES_DBG_REG_W
1298         if ((data & 0x2) != (snes_ram[CGWSEL] & 0x2))
1299            mame_printf_debug( "Add/Sub Layer: %s\n", ((data & 0x2) >> 1) ? "Subscreen" : "Fixed colour" );
1300#endif
1301         break;
1302      case CGADSUB:   /* Addition/Subtraction designation for each screen */
1303         snes_ppu.color_modes = data & 0xc0;
1304         snes_ppu.layer[SNES_BG1].color_math = BIT(data, 0);
1305         snes_ppu.layer[SNES_BG2].color_math = BIT(data, 1);
1306         snes_ppu.layer[SNES_BG3].color_math = BIT(data, 2);
1307         snes_ppu.layer[SNES_BG4].color_math = BIT(data, 3);
1308         snes_ppu.layer[SNES_OAM].color_math = BIT(data, 4);
1309         snes_ppu.layer[SNES_COLOR].color_math = BIT(data, 5);
1310         break;
1311      case COLDATA:   /* Fixed colour data for fixed colour addition/subtraction */
1312         {
1313            /* Store it in the extra space we made in the CGRAM. It doesn't really go there, but it's as good a place as any. */
1314            UINT8 r, g, b;
1315
1316            /* Get existing value. */
1317            r = snes_cgram[FIXED_COLOUR] & 0x1f;
1318            g = (snes_cgram[FIXED_COLOUR] & 0x3e0) >> 5;
1319            b = (snes_cgram[FIXED_COLOUR] & 0x7c00) >> 10;
1320            /* Set new value */
1321            if (data & 0x20)
1322               r = data & 0x1f;
1323            if (data & 0x40)
1324               g = data & 0x1f;
1325            if (data & 0x80)
1326               b = data & 0x1f;
1327            snes_cgram[FIXED_COLOUR] = (r | (g << 5) | (b << 10));
1328         } break;
1329      case SETINI:   /* Screen mode/video select */
1330         snes_ppu.interlace = (data & 0x01) ? 2 : 1;
1331         snes_ppu.obj_interlace = (data & 0x02) ? 2 : 1;
1332         snes_ppu.beam.last_visible_line = (data & 0x04) ? 240 : 225;
1333         snes_ppu.pseudo_hires = BIT(data, 3);
1334         snes_ppu.mode7.extbg = BIT(data, 6);
1335         snes_dynamic_res_change(space->machine);
1336#ifdef SNES_DBG_REG_W
1337         if ((data & 0x8) != (snes_ram[SETINI] & 0x8))
1338            mame_printf_debug( "Pseudo 512 mode: %s\n", (data & 0x8) ? "on" : "off" );
1339#endif
1340         break;
1341535      case WMDATA:   /* Data to write to WRAM */
1342536         memory_write_byte(space, 0x7e0000 + state->wram_address++, data );
1343537         state->wram_address &= 0x1ffff;
r7668r7669
1681875      }
1682876      else if ((snes_cart.mode == SNES_MODE_21) && (snes_cart.sram > 0))
1683877      {
1684         int mask = ((snes_cart.sram * 1024) - 1);            /* Limit SRAM size to what's actually present */
878         int mask = (snes_cart.sram - 1);            /* Limit SRAM size to what's actually present */
1685879         offset -= 0x6000;
1686880         value = snes_ram[0x306000 + (offset & mask)];
1687881      }
r7668r7669
1797991   {
1798992      if (snes_cart.sram > 0)
1799993      {
1800         int mask = ((snes_cart.sram * 1024) - 1) | 0xff0000;   /* Limit SRAM size to what's actually present */
994         int mask = (snes_cart.sram - 1) | 0xff0000;   /* Limit SRAM size to what's actually present */
1801995         value = snes_ram[0x700000 + (offset & mask)];
1802996      }
1803997      else
r7668r7669
18321026            value = memory_read_byte(space, offset);
18331027         else if ((offset >= 0x300000) && (snes_cart.sram > 0))
18341028         {
1835            int mask = ((snes_cart.sram * 1024) - 1);      /* Limit SRAM size to what's actually present */
1029            int mask = (snes_cart.sram - 1);      /* Limit SRAM size to what's actually present */
18361030            offset -= 0x6000;
18371031            value = snes_ram[0x806000 + (offset & mask)];   /* SRAM */
18381032         }
r7668r7669
19741168      }
19751169      else if ((snes_cart.mode == SNES_MODE_21) && (snes_cart.sram > 0))
19761170      {
1977         int mask = ((snes_cart.sram * 1024) - 1);         /* Limit SRAM size to what's actually present */
1171         int mask = (snes_cart.sram - 1);         /* Limit SRAM size to what's actually present */
19781172         offset -= 0x6000;
19791173         snes_ram[0x306000 + (offset & mask)] = data;
19801174      }
r7668r7669
20321226   {
20331227      if (snes_cart.sram > 0)
20341228      {
2035         int mask = ((snes_cart.sram * 1024) - 1) | 0xff0000;   /* Limit SRAM size to what's actually present */
1229         int mask = (snes_cart.sram - 1) | 0xff0000;   /* Limit SRAM size to what's actually present */
20361230         snes_ram[0x700000 + (offset & mask)] = data;
20371231      }
20381232      else
r7668r7669
20621256            memory_write_byte(space, offset, data);
20631257         else if ((offset >= 0x300000) && (snes_cart.sram > 0))
20641258         {
2065            int mask = ((snes_cart.sram * 1024) - 1);         /* Limit SRAM size to what's actually present */
1259            int mask = (snes_cart.sram - 1);         /* Limit SRAM size to what's actually present */
20661260            offset -= 0x6000;
20671261            snes_ram[0xb06000 + (offset & mask)] = data;
20681262         }
r7668r7669
24751669   snes_cart.sram = snes_r_bank1(space, 0x00ffd8);
24761670   if (snes_cart.sram > 0)
24771671   {
2478      snes_cart.sram = ((1 << (snes_cart.sram + 3)) / 8);
1672      snes_cart.sram = (1024 << snes_cart.sram);
24791673      if (snes_cart.sram > snes_cart.sram_max)
24801674         snes_cart.sram = snes_cart.sram_max;
24811675   }
r7668r7669
25341728   snes_cart.sram = snes_r_bank1(space, 0x00ffd8);
25351729   if (snes_cart.sram > 0)
25361730   {
2537      snes_cart.sram = ((1 << (snes_cart.sram + 3)) / 8);
1731      snes_cart.sram = (1024 << snes_cart.sram);
25381732      if (snes_cart.sram > snes_cart.sram_max)
25391733         snes_cart.sram = snes_cart.sram_max;
25401734   }
src/mame/includes/snes.h
r7668r7669
394394   UINT16                vram_fgr_high, vram_fgr_increment, vram_fgr_count, vram_fgr_mask, vram_fgr_shift, vram_read_buffer;
395395   UINT32                wram_address;
396396   UINT16                htime, vtime;
397   UINT16                vmadd;
397398
398399   /* timers */
399400   emu_timer             *scanline_timer;
r7668r7669
493494extern MACHINE_START( snes );
494495extern MACHINE_RESET( snes );
495496
497READ8_HANDLER( snes_open_bus_r );
498
496499extern READ8_HANDLER( snes_r_io );
497500extern WRITE8_HANDLER( snes_w_io );
498501
r7668r7669
651654extern VIDEO_START( snes );
652655extern VIDEO_UPDATE( snes );
653656
657extern READ8_HANDLER( snes_ppu_read );
658extern WRITE8_HANDLER( snes_ppu_write );
654659
655660#endif /* _SNES_H_ */
src/mame/video/snes.c
r7668r7669
16691669}
16701670
16711671
1672/* CPU <-> PPU comms */
1673
1674// full graphic variables
1675static const UINT16 vram_fgr_inctab[4] = { 1, 32, 128, 128 };
1676static const UINT16 vram_fgr_inccnts[4] = { 0, 32, 64, 128 };
1677static const UINT16 vram_fgr_shiftab[4] = { 0, 5, 6, 7 };
1678
1679// utility function - latches the H/V counters.  Used by IRQ, writes to WRIO, etc.
1680void snes_latch_counters( running_machine *machine )
1681{
1682   snes_state *state = (snes_state *)machine->driver_data;
1683
1684   snes_ppu.beam.current_horz = video_screen_get_hpos(machine->primary_screen) / state->htmult;
1685   snes_ppu.beam.latch_vert = video_screen_get_vpos(machine->primary_screen);
1686   snes_ppu.beam.latch_horz = snes_ppu.beam.current_horz;
1687   snes_ram[STAT78] |= 0x40;   // indicate we latched
1688//  state->read_ophct = state->read_opvct = 0;    // clear read flags - 2009-08: I think we must clear these when STAT78 is read...
1689
1690//  printf("latched @ H %d V %d\n", snes_ppu.beam.latch_horz, snes_ppu.beam.latch_vert);
1691}
1692
1693static void snes_dynamic_res_change( running_machine *machine )
1694{
1695   snes_state *state = (snes_state *)machine->driver_data;
1696   rectangle visarea = *video_screen_get_visible_area(machine->primary_screen);
1697   attoseconds_t refresh;
1698
1699   visarea.min_x = visarea.min_y = 0;
1700   visarea.max_y = snes_ppu.beam.last_visible_line * snes_ppu.interlace - 1;
1701   visarea.max_x = (SNES_SCR_WIDTH * 2) - 1;
1702
1703   // fixme: should compensate for SNES_DBG_video
1704   if (snes_ppu.mode == 5 || snes_ppu.mode == 6 || snes_ppu.pseudo_hires)
1705      state->htmult = 2;
1706   else
1707      state->htmult = 1;
1708
1709   /* FIXME: does the timing changes when the gfx mode is equal to 5 or 6? */
1710   if ((snes_ram[STAT78] & 0x10) == SNES_NTSC)
1711      refresh = HZ_TO_ATTOSECONDS(DOTCLK_NTSC) * SNES_HTOTAL * SNES_VTOTAL_NTSC;
1712   else
1713      refresh = HZ_TO_ATTOSECONDS(DOTCLK_PAL) * SNES_HTOTAL * SNES_VTOTAL_PAL;
1714
1715   if ((snes_ram[STAT78] & 0x10) == SNES_NTSC)
1716      video_screen_configure(machine->primary_screen, SNES_HTOTAL * 2, SNES_VTOTAL_NTSC * snes_ppu.interlace, &visarea, refresh);
1717   else
1718      video_screen_configure(machine->primary_screen, SNES_HTOTAL * 2, SNES_VTOTAL_PAL * snes_ppu.interlace, &visarea, refresh);
1719}
1720
1721/*************************************************
1722
1723 SNES VRAM accesses:
1724
1725 VRAM accesses during active display are invalid.
1726 Unlike OAM and CGRAM, they will not be written
1727 anywhere at all. Thanks to byuu's researches,
1728 the ranges where writes are invalid have been
1729 validated on hardware, as has the edge case where
1730 the S-CPU open bus can be written if the write
1731 occurs during the very last clock cycle of
1732 vblank.
1733 Our implementation could be not 100% accurate
1734 when interlace is active.
1735*************************************************/
1736
1737INLINE UINT32 snes_get_vram_address( running_machine *machine )
1738{
1739   snes_state *state = (snes_state *)machine->driver_data;
1740   UINT32 addr = (snes_ram[VMADDH] << 8) | snes_ram[VMADDL];
1741
1742   if (state->vram_fgr_count)
1743   {
1744      UINT32 rem = addr & state->vram_fgr_mask;
1745      UINT32 faddr = (addr & ~state->vram_fgr_mask) + (rem >> state->vram_fgr_shift) + ((rem & (state->vram_fgr_count - 1)) << 3);
1746      return faddr;
1747   }
1748
1749   return addr;
1750}
1751
1752static READ8_HANDLER( snes_vram_read )
1753{
1754   UINT8 res = 0;
1755   offset &= 0x1ffff;
1756
1757   if (snes_ppu.screen_disabled)
1758      res = snes_vram[offset];
1759   else
1760   {
1761      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
1762      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
1763      UINT16 ls = (((snes_ram[STAT78] & 0x10) == SNES_NTSC ? 525 : 625) >> 1) - 1;
1764
1765      if (snes_ppu.interlace == 2)
1766         ls++;
1767
1768      if (v == ls && h == 1362)
1769         res = 0;
1770      else if (v < snes_ppu.beam.last_visible_line - 1)
1771         res = 0;
1772      else if (v == snes_ppu.beam.last_visible_line - 1)
1773      {
1774         if (h == 1362)
1775            res = snes_vram[offset];
1776         else
1777            res = 0;
1778      }
1779      else
1780         res = snes_vram[offset];
1781   }
1782   return res;
1783}
1784
1785static WRITE8_HANDLER( snes_vram_write )
1786{
1787   offset &= 0x1ffff;
1788
1789   if (snes_ppu.screen_disabled)
1790      snes_vram[offset] = data;
1791   else
1792   {
1793      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
1794      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
1795      if (v == 0)
1796      {
1797         if (h <= 4)
1798            snes_vram[offset] = data;
1799         else if (h == 6)
1800            snes_vram[offset] = snes_open_bus_r(space, 0);
1801         else
1802         {
1803            //no write
1804         }
1805      }
1806      else if (v < snes_ppu.beam.last_visible_line)
1807      {
1808         //no write
1809      }
1810      else if (v == snes_ppu.beam.last_visible_line)
1811      {
1812         if (h <= 4)
1813         {
1814            //no write
1815         }
1816         else
1817            snes_vram[offset] = data;
1818      }
1819      else
1820         snes_vram[offset] = data;
1821   }
1822}
1823
1824/*************************************************
1825
1826 SNES OAM accesses:
1827
1828 OAM accesses during active display are allowed.
1829 The actual address varies during rendering, as the
1830 PPU reads in data itself for processing.
1831 Unfortunately, no one has been able (yet) to
1832 determine how this works. The only known game to
1833 actually access OAM during active display is
1834 Uniracers and it expects accesses to map to
1835 offset 0x0218. Hence, following byuu's choice
1836 we rerouted OAM accesses during active display
1837 to 0x0218 (0x010c in our snes_oam).
1838 This is a hack, but it is more accurate than
1839 writing to the 'expected' address set by
1840 $2102,$2103.
1841
1842 Notice that, since snes_ram[OAMDATA] is never
1843 read/written directly, we use it as an index
1844 to choose the high/low byte of the snes_oam word.
1845*************************************************/
1846
1847static READ8_HANDLER( snes_oam_read )
1848{
1849   offset &= 0x1ff;
1850
1851   if (offset & 0x100)
1852      offset &= 0x10f;
1853
1854   if (!snes_ppu.screen_disabled)
1855   {
1856      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
1857
1858      if (v < snes_ppu.beam.last_visible_line)
1859         offset = 0x010c;
1860   }
1861
1862   return (snes_oam[offset] >> (snes_ram[OAMDATA] << 3)) & 0xff;;
1863}
1864
1865static WRITE8_HANDLER( snes_oam_write )
1866{
1867   offset &= 0x1ff;
1868
1869   if (offset & 0x100)
1870      offset &= 0x10f;
1871
1872   if (!snes_ppu.screen_disabled)
1873   {
1874      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
1875
1876      if (v < snes_ppu.beam.last_visible_line)
1877         offset = 0x010c;
1878   }
1879
1880   if (!(snes_ram[OAMDATA]))
1881      snes_oam[offset] = (snes_oam[offset] & 0xff00) | (data << 0);
1882   else
1883      snes_oam[offset] = (snes_oam[offset] & 0x00ff) | (data << 8);
1884}
1885
1886/*************************************************
1887
1888 SNES CGRAM accesses:
1889
1890 CGRAM writes during hblank are valid. During
1891 active display, the actual address the data
1892 is written to varies, as the PPU itself changes
1893 the address. Like OAM, it is not known the exact
1894 algorithm used, but no commercial software seems
1895 to attempt this. While byuu, in his emu, maps
1896 those accesses to 0x01ff, because it is more
1897 accurate to invalidate the 'expected' address
1898 than not, MESS has issues if we don't write to
1899 the expected address (see e.g. Tokimeki Memorial).
1900 This is because writes should work during hblank
1901 (so that the game can produce color fading), but
1902 ends up not working with the conditions below.
1903 Hence, for the moment, we only document the
1904 solution adopted by BSNES without enabling it.
1905*************************************************/
1906
1907static READ8_HANDLER( snes_cgram_read )
1908{
1909   UINT8 res = 0;
1910   offset &= 0x1ff;
1911
1912#if 0
1913   if (!snes_ppu.screen_disabled)
1914   {
1915      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
1916      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
1917
1918      if (v < snes_ppu.beam.last_visible_line && h >= 128 && h < 1096)
1919         offset = 0x1ff;
1920   }
1921#endif
1922
1923   res = ((UINT8 *)snes_cgram)[offset];
1924
1925   // CGRAM palette data format is 15-bits (0,bbbbb,ggggg,rrrrr).
1926   // Highest bit is simply ignored.
1927   if (offset & 0x01)
1928      res &= 0x7f;
1929
1930   return res;
1931}
1932
1933static WRITE8_HANDLER( snes_cgram_write )
1934{
1935   offset &= 0x1ff;
1936
1937#if 0
1938   // FIXME: this currently breaks some games (e.g. Tokimeki Memorial),
1939   // even if it's expected to be more accurate than allowing for
1940   // writes to the cgram address
1941   if (!snes_ppu.screen_disabled)
1942   {
1943      UINT16 v = video_screen_get_vpos(space->machine->primary_screen);
1944      UINT16 h = video_screen_get_hpos(space->machine->primary_screen);
1945
1946      if (v < snes_ppu.beam.last_visible_line && h >= 128 && h < 1096)
1947         offset = 0x1ff;
1948   }
1949#endif
1950
1951   // CGRAM palette data format is 15-bits (0,bbbbb,ggggg,rrrrr).
1952   // Highest bit is simply ignored.
1953   if (offset & 0x01)
1954      data &= 0x7f;
1955
1956   ((UINT8 *)snes_cgram)[offset] = data;
1957}
1958
1959READ8_HANDLER( snes_ppu_read )
1960{
1961   snes_state *state = (snes_state *)space->machine->driver_data;
1962   UINT8 value;
1963
1964   switch (offset)
1965   {
1966      case OAMDATA:   /* 21xy for x=0,1,2 and y=4,5,6,8,9,a returns PPU1 open bus*/
1967      case BGMODE:
1968      case MOSAIC:
1969      case BG2SC:
1970      case BG3SC:
1971      case BG4SC:
1972      case BG4VOFS:
1973      case VMAIN:
1974      case VMADDL:
1975      case VMDATAL:
1976      case VMDATAH:
1977      case M7SEL:
1978      case W34SEL:
1979      case WOBJSEL:
1980      case WH0:
1981      case WH2:
1982      case WH3:
1983      case WBGLOG:
1984         return snes_ppu.ppu1_open_bus;
1985
1986      case MPYL:      /* Multiplication result (low) */
1987         {
1988            /* Perform 16bit * 8bit multiply */
1989            UINT32 c = (INT16)snes_ppu.mode7.matrix_a * (INT8)(snes_ppu.mode7.matrix_b >> 8);
1990            snes_ppu.ppu1_open_bus = c & 0xff;
1991            return snes_ppu.ppu1_open_bus;
1992         }
1993      case MPYM:      /* Multiplication result (mid) */
1994         {
1995            /* Perform 16bit * 8bit multiply */
1996            UINT32 c = (INT16)snes_ppu.mode7.matrix_a * (INT8)(snes_ppu.mode7.matrix_b >> 8);
1997            snes_ppu.ppu1_open_bus = (c >> 8) & 0xff;
1998            return snes_ppu.ppu1_open_bus;
1999         }
2000      case MPYH:      /* Multiplication result (high) */
2001         {
2002            /* Perform 16bit * 8bit multiply */
2003            UINT32 c = (INT16)snes_ppu.mode7.matrix_a * (INT8)(snes_ppu.mode7.matrix_b >> 8);
2004            snes_ppu.ppu1_open_bus = (c >> 16) & 0xff;
2005            return snes_ppu.ppu1_open_bus;
2006         }
2007      case SLHV:      /* Software latch for H/V counter */
2008         snes_latch_counters(space->machine);
2009         return snes_open_bus_r(space, 0);      /* Return value is meaningless */
2010      case ROAMDATA:   /* Read data from OAM (DR) */
2011         snes_ppu.ppu1_open_bus = snes_oam_read(space, snes_ppu.oam.address);
2012         snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2;
2013         if (!snes_ram[OAMDATA])
2014         {
2015            snes_ppu.oam.address++;
2016            snes_ppu.oam.address &= 0x1ff;
2017            snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
2018         }
2019         return snes_ppu.ppu1_open_bus;
2020      case RVMDATAL:   /* Read data from VRAM (low) */
2021         {
2022            UINT32 addr = snes_get_vram_address(space->machine) << 1;
2023            snes_ppu.ppu1_open_bus = state->vram_read_buffer & 0xff;
2024
2025            if (!state->vram_fgr_high)
2026            {
2027               state->vram_read_buffer = snes_vram_read(space, addr);
2028               state->vram_read_buffer |= (snes_vram_read(space, addr + 1) << 8);
2029
2030               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
2031               snes_ram[VMADDL] = addr & 0xff;
2032               snes_ram[VMADDH] = (addr >> 8) & 0xff;
2033            }
2034
2035            return snes_ppu.ppu1_open_bus;
2036         }
2037      case RVMDATAH:   /* Read data from VRAM (high) */
2038         {
2039            UINT32 addr = snes_get_vram_address(space->machine) << 1;
2040            snes_ppu.ppu1_open_bus = (state->vram_read_buffer >> 8) & 0xff;
2041
2042            if (state->vram_fgr_high)
2043            {
2044               state->vram_read_buffer = snes_vram_read(space, addr);
2045               state->vram_read_buffer |= (snes_vram_read(space, addr + 1) << 8);
2046
2047               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
2048               snes_ram[VMADDL] = addr & 0xff;
2049               snes_ram[VMADDH] = (addr >> 8) & 0xff;
2050            }
2051
2052            return snes_ppu.ppu1_open_bus;
2053         }
2054      case RCGDATA:   /* Read data from CGRAM */
2055         if (!(state->cgram_address & 0x01))
2056            snes_ppu.ppu2_open_bus = snes_cgram_read(space, state->cgram_address);
2057         else
2058         {
2059            snes_ppu.ppu2_open_bus &= 0x80;
2060            snes_ppu.ppu2_open_bus |= snes_cgram_read(space, state->cgram_address) & 0x7f;
2061         }
2062
2063         state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2);
2064         return snes_ppu.ppu2_open_bus;
2065      case OPHCT:      /* Horizontal counter data by ext/soft latch */
2066         if (state->read_ophct)
2067         {
2068            snes_ppu.ppu2_open_bus &= 0xfe;
2069            snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_horz >> 8) & 0x01;
2070         }
2071         else
2072         {
2073            snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_horz & 0xff;
2074         }
2075         state->read_ophct ^= 1;
2076         return snes_ppu.ppu2_open_bus;
2077      case OPVCT:      /* Vertical counter data by ext/soft latch */
2078         if (state->read_opvct)
2079         {
2080            snes_ppu.ppu2_open_bus &= 0xfe;
2081            snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_vert >> 8) & 0x01;
2082         }
2083         else
2084         {
2085            snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_vert & 0xff;
2086         }
2087         state->read_opvct ^= 1;
2088         return snes_ppu.ppu2_open_bus;
2089      case STAT77:   /* PPU status flag and version number */
2090         value = snes_ppu.stat77_flags & 0xc0; // 0x80 & 0x40 are Time Over / Range Over Sprite flags, set by the video code
2091         // 0x20 - Master/slave mode select. Little is known about this bit. We always seem to read back 0 here.
2092         value |= (snes_ppu.ppu1_open_bus & 0x10);
2093         value |= (snes_ppu.ppu1_version & 0x0f);
2094         snes_ppu.stat77_flags = value;   // not sure if this is needed...
2095         snes_ppu.ppu1_open_bus = value;
2096         return snes_ppu.ppu1_open_bus;
2097      case STAT78:   /* PPU status flag and version number */
2098         state->read_ophct = 0;
2099         state->read_opvct = 0;
2100         value = snes_ram[offset];
2101         value |= (snes_ppu.ppu2_open_bus & 0x20);
2102         value |= (snes_ppu.ppu2_version & 0x0f);
2103         snes_ram[offset] = value;   // not sure if this is needed...
2104         snes_ppu.ppu2_open_bus = value;
2105         return snes_ppu.ppu2_open_bus;
2106   }
2107
2108   return snes_ppu.ppu1_open_bus;
2109}
2110
2111
2112WRITE8_HANDLER( snes_ppu_write )
2113{
2114   snes_state *state = (snes_state *)space->machine->driver_data;
2115
2116   switch (offset)
2117   {
2118      case INIDISP:   /* Initial settings for screen */
2119         if ((snes_ppu.screen_disabled & 0x80) && (!(data & 0x80))) //a 1->0 force blank transition causes a reset OAM address
2120         {
2121            memory_write_byte(space, OAMADDL, snes_ppu.oam.saved_address_low);
2122            memory_write_byte(space, OAMADDH, snes_ppu.oam.saved_address_high);
2123            snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
2124         }
2125         snes_ppu.screen_disabled = data & 0x80;
2126         snes_ppu.screen_brightness = (data & 0x0f) + 1;
2127         break;
2128      case OBSEL:      /* Object size and data area designation */
2129         snes_ppu.oam.next_charmap = (data & 0x03) << 1;
2130         snes_ppu.oam.next_name_select = (((data & 0x18) >> 3) * 0x1000) << 1;
2131         snes_ppu.oam.next_size = (data & 0xe0) >> 5;
2132         break;
2133      case OAMADDL:   /* Address for accessing OAM (low) */
2134         snes_ppu.oam.saved_address_low = data;
2135         snes_ppu.oam.address = (snes_ppu.oam.address & 0xff00) + data;
2136         snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
2137         snes_ram[OAMDATA] = 0;
2138         break;
2139      case OAMADDH:   /* Address for accessing OAM (high) */
2140         snes_ppu.oam.saved_address_high = data;
2141         snes_ppu.oam.address = (snes_ppu.oam.address & 0x00ff) | ((data & 0x01) << 8);
2142         snes_ppu.oam.priority_rotation = BIT(data, 7);
2143         snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
2144         snes_ram[OAMDATA] = 0;
2145         break;
2146      case OAMDATA:   /* Data for OAM write (DW) */
2147         if (snes_ppu.oam.address >= 0x100)
2148            snes_oam_write(space, snes_ppu.oam.address, data);
2149         else
2150         {
2151            if (!snes_ram[OAMDATA])
2152               snes_ppu.oam.write_latch = data;
2153            else
2154            {
2155               // in this case, we not only write data to the upper byte of the word,
2156               // but also snes_ppu.oam.write_latch to the lower byte (recall that
2157               // snes_ram[OAMDATA] is used to select high/low byte)
2158               snes_oam_write(space, snes_ppu.oam.address, data);
2159               snes_ram[OAMDATA] = 0;
2160               snes_oam_write(space, snes_ppu.oam.address, snes_ppu.oam.write_latch);
2161               snes_ram[OAMDATA] = 1;
2162            }
2163         }
2164         snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2;
2165         if (!snes_ram[OAMDATA])
2166         {
2167            snes_ppu.oam.address++;
2168            snes_ppu.oam.address &= 0x1ff;
2169            snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0;
2170         }
2171         return;
2172      case BGMODE:   /* BG mode and character size settings */
2173         snes_ppu.mode = data & 0x07;
2174         snes_dynamic_res_change(space->machine);
2175         snes_ppu.bg3_priority_bit = BIT(data, 3);
2176         snes_ppu.layer[SNES_BG1].tile_size = BIT(data, 4);
2177         snes_ppu.layer[SNES_BG2].tile_size = BIT(data, 5);
2178         snes_ppu.layer[SNES_BG3].tile_size = BIT(data, 6);
2179         snes_ppu.layer[SNES_BG4].tile_size = BIT(data, 7);
2180         snes_ppu.update_offsets = 1;
2181         break;
2182      case MOSAIC:   /* Size and screen designation for mosaic */
2183         snes_ppu.mosaic_size = (data & 0xf0) >> 4;
2184         snes_ppu.layer[SNES_BG1].mosaic_enabled = BIT(data, 0);
2185         snes_ppu.layer[SNES_BG2].mosaic_enabled = BIT(data, 1);
2186         snes_ppu.layer[SNES_BG3].mosaic_enabled = BIT(data, 2);
2187         snes_ppu.layer[SNES_BG4].mosaic_enabled = BIT(data, 3);
2188         break;
2189      case BG1SC:      /* Address for storing SC data BG1 SC size designation */
2190      case BG2SC:      /* Address for storing SC data BG2 SC size designation  */
2191      case BG3SC:      /* Address for storing SC data BG3 SC size designation  */
2192      case BG4SC:      /* Address for storing SC data BG4 SC size designation  */
2193         snes_ppu.layer[offset - BG1SC].tilemap = data & 0xfc;
2194         snes_ppu.layer[offset - BG1SC].tilemap_size = data & 0x3;
2195         break;
2196      case BG12NBA:   /* Address for BG 1 and 2 character data */
2197         snes_ppu.layer[SNES_BG1].charmap = (data & 0x0f);
2198         snes_ppu.layer[SNES_BG2].charmap = (data & 0xf0) >> 4;
2199         break;
2200      case BG34NBA:   /* Address for BG 3 and 4 character data */
2201         snes_ppu.layer[SNES_BG3].charmap = (data & 0x0f);
2202         snes_ppu.layer[SNES_BG4].charmap = (data & 0xf0) >> 4;
2203         break;
2204
2205      // Anomie says "H Current = (Byte<<8) | (Prev&~7) | ((Current>>8)&7); V Current = (Current<<8) | Prev;" and Prev is shared by all scrolls but in Mode 7!
2206      case BG1HOFS:   /* BG1 - horizontal scroll (DW) */
2207         /* In Mode 0->6 we use ppu_last_scroll as Prev */
2208         snes_ppu.layer[SNES_BG1].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG1].hoffs >> 8) & 7);
2209         snes_ppu.ppu_last_scroll = data;
2210         /* In Mode 7 we use mode7_last_scroll as Prev */
2211         snes_ppu.mode7.hor_offset = (data << 8) | (snes_ppu.mode7_last_scroll & ~7) | ((snes_ppu.mode7.hor_offset >> 8) & 7);
2212         snes_ppu.mode7_last_scroll = data;
2213         snes_ppu.update_offsets = 1;
2214         return;
2215      case BG1VOFS:   /* BG1 - vertical scroll (DW) */
2216         /* In Mode 0->6 we use ppu_last_scroll as Prev */
2217         snes_ppu.layer[SNES_BG1].voffs = (data << 8) | snes_ppu.ppu_last_scroll;
2218         snes_ppu.ppu_last_scroll = data;
2219         /* In Mode 7 we use mode7_last_scroll as Prev */
2220         snes_ppu.mode7.ver_offset = (data << 8) | snes_ppu.mode7_last_scroll;
2221         snes_ppu.mode7_last_scroll = data;
2222         snes_ppu.update_offsets = 1;
2223         return;
2224      case BG2HOFS:   /* BG2 - horizontal scroll (DW) */
2225         snes_ppu.layer[SNES_BG2].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG2].hoffs >> 8) & 7);
2226         snes_ppu.ppu_last_scroll = data;
2227         snes_ppu.update_offsets = 1;
2228         return;
2229      case BG2VOFS:   /* BG2 - vertical scroll (DW) */
2230         snes_ppu.layer[SNES_BG2].voffs = (data << 8) | (snes_ppu.ppu_last_scroll);
2231         snes_ppu.ppu_last_scroll = data;
2232         snes_ppu.update_offsets = 1;
2233         return;
2234      case BG3HOFS:   /* BG3 - horizontal scroll (DW) */
2235         snes_ppu.layer[SNES_BG3].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG3].hoffs >> 8) & 7);
2236         snes_ppu.ppu_last_scroll = data;
2237         snes_ppu.update_offsets = 1;
2238         return;
2239      case BG3VOFS:   /* BG3 - vertical scroll (DW) */
2240         snes_ppu.layer[SNES_BG3].voffs = (data << 8) | (snes_ppu.ppu_last_scroll);
2241         snes_ppu.ppu_last_scroll = data;
2242         snes_ppu.update_offsets = 1;
2243         return;
2244      case BG4HOFS:   /* BG4 - horizontal scroll (DW) */
2245         snes_ppu.layer[SNES_BG4].hoffs = (data << 8) | (snes_ppu.ppu_last_scroll & ~7) | ((snes_ppu.layer[SNES_BG4].hoffs >> 8) & 7);
2246         snes_ppu.ppu_last_scroll = data;
2247         snes_ppu.update_offsets = 1;
2248         return;
2249      case BG4VOFS:   /* BG4 - vertical scroll (DW) */
2250         snes_ppu.layer[SNES_BG4].voffs = (data << 8) | (snes_ppu.ppu_last_scroll);
2251         snes_ppu.ppu_last_scroll = data;
2252         snes_ppu.update_offsets = 1;
2253         return;
2254      case VMAIN:      /* VRAM address increment value designation */
2255         state->vram_fgr_high = (data & 0x80);
2256         state->vram_fgr_increment = vram_fgr_inctab[data & 3];
2257
2258         if (data & 0xc)
2259         {
2260            int md = (data & 0xc) >> 2;
2261
2262            state->vram_fgr_count = vram_fgr_inccnts[md];         // 0x20, 0x40, 0x80
2263            state->vram_fgr_mask = (state->vram_fgr_count * 8) - 1;   // 0xff, 0x1ff, 0x2ff
2264            state->vram_fgr_shift = vram_fgr_shiftab[md];         // 5, 6, 7
2265         }
2266         else
2267         {
2268            state->vram_fgr_count = 0;
2269         }
2270//          printf("VMAIN: high %x inc %x count %x mask %x shift %x\n", state->vram_fgr_high, state->vram_fgr_increment, state->vram_fgr_count, state->vram_fgr_mask, state->vram_fgr_shift);
2271         break;
2272      case VMADDL:   /* Address for VRAM read/write (low) */
2273      case VMADDH:   /* Address for VRAM read/write (high) */
2274         {
2275            UINT32 addr;
2276            snes_ram[offset] = data;
2277            addr = snes_get_vram_address(space->machine) << 1;
2278            state->vram_read_buffer = snes_vram_read(space, addr);
2279            state->vram_read_buffer |= (snes_vram_read(space, addr + 1) << 8);
2280         }
2281         break;
2282      case VMDATAL:   /* 2118: Data for VRAM write (low) */
2283         {
2284            UINT32 addr = snes_get_vram_address(space->machine) << 1;
2285            snes_vram_write(space, addr, data);
2286
2287            if (!state->vram_fgr_high)
2288            {
2289               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
2290               snes_ram[VMADDL] = addr & 0xff;
2291               snes_ram[VMADDH] = (addr >> 8) & 0xff;
2292            }
2293         }
2294         return;
2295      case VMDATAH:   /* 2119: Data for VRAM write (high) */
2296         {
2297            UINT32 addr = snes_get_vram_address(space->machine) << 1;
2298            snes_vram_write(space, addr + 1, data);
2299
2300            if (state->vram_fgr_high)
2301            {
2302               addr = ((snes_ram[VMADDH] << 8) | snes_ram[VMADDL]) + state->vram_fgr_increment;
2303               snes_ram[VMADDL] = addr & 0xff;
2304               snes_ram[VMADDH] = (addr >> 8) & 0xff;
2305            }
2306         }
2307         return;
2308      case M7SEL:      /* Mode 7 initial settings */
2309         snes_ppu.mode7.repeat = (data >> 6) & 3;
2310         snes_ppu.mode7.vflip  = BIT(data, 1);
2311         snes_ppu.mode7.hflip  = BIT(data, 0);
2312         break;
2313      /* As per Anomie's doc: Reg = (Current<<8) | Prev; and there is only one Prev, shared by these matrix regs and Mode 7 scroll regs */
2314      case M7A:      /* Mode 7 COS angle/x expansion (DW) */
2315         snes_ppu.mode7.matrix_a = snes_ppu.mode7_last_scroll + (data << 8);
2316         snes_ppu.mode7_last_scroll = data;
2317         break;
2318      case M7B:      /* Mode 7 SIN angle/ x expansion (DW) */
2319         snes_ppu.mode7.matrix_b = snes_ppu.mode7_last_scroll + (data << 8);
2320         snes_ppu.mode7_last_scroll = data;
2321         break;
2322      case M7C:      /* Mode 7 SIN angle/y expansion (DW) */
2323         snes_ppu.mode7.matrix_c = snes_ppu.mode7_last_scroll + (data << 8);
2324         snes_ppu.mode7_last_scroll = data;
2325         break;
2326      case M7D:      /* Mode 7 COS angle/y expansion (DW) */
2327         snes_ppu.mode7.matrix_d = snes_ppu.mode7_last_scroll + (data << 8);
2328         snes_ppu.mode7_last_scroll = data;
2329         break;
2330      case M7X:      /* Mode 7 x center position (DW) */
2331         snes_ppu.mode7.origin_x = snes_ppu.mode7_last_scroll + (data << 8);
2332         snes_ppu.mode7_last_scroll = data;
2333         break;
2334      case M7Y:      /* Mode 7 y center position (DW) */
2335         snes_ppu.mode7.origin_y = snes_ppu.mode7_last_scroll + (data << 8);
2336         snes_ppu.mode7_last_scroll = data;
2337         break;
2338      case CGADD:      /* Initial address for colour RAM writing */
2339         /* CGRAM is 16-bit, but when reading/writing we treat it as 8-bit, so we need to double the address */
2340         state->cgram_address = data << 1;
2341         break;
2342      case CGDATA:   /* Data for colour RAM */
2343         snes_cgram_write(space, state->cgram_address, data);
2344         state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2);
2345         break;
2346      case W12SEL:   /* Window mask settings for BG1-2 */
2347         if (data != snes_ram[offset])
2348         {
2349            snes_ppu.layer[SNES_BG1].window1_invert  = BIT(data, 0);
2350            snes_ppu.layer[SNES_BG1].window1_enabled = BIT(data, 1);
2351            snes_ppu.layer[SNES_BG1].window2_invert  = BIT(data, 2);
2352            snes_ppu.layer[SNES_BG1].window2_enabled = BIT(data, 3);
2353            snes_ppu.layer[SNES_BG2].window1_invert  = BIT(data, 4);
2354            snes_ppu.layer[SNES_BG2].window1_enabled = BIT(data, 5);
2355            snes_ppu.layer[SNES_BG2].window2_invert  = BIT(data, 6);
2356            snes_ppu.layer[SNES_BG2].window2_enabled = BIT(data, 7);
2357            snes_ppu.update_windows = 1;
2358         }
2359         break;
2360      case W34SEL:   /* Window mask settings for BG3-4 */
2361         if (data != snes_ram[offset])
2362         {
2363            snes_ppu.layer[SNES_BG3].window1_invert  = BIT(data, 0);
2364            snes_ppu.layer[SNES_BG3].window1_enabled = BIT(data, 1);
2365            snes_ppu.layer[SNES_BG3].window2_invert  = BIT(data, 2);
2366            snes_ppu.layer[SNES_BG3].window2_enabled = BIT(data, 3);
2367            snes_ppu.layer[SNES_BG4].window1_invert  = BIT(data, 4);
2368            snes_ppu.layer[SNES_BG4].window1_enabled = BIT(data, 5);
2369            snes_ppu.layer[SNES_BG4].window2_invert  = BIT(data, 6);
2370            snes_ppu.layer[SNES_BG4].window2_enabled = BIT(data, 7);
2371            snes_ppu.update_windows = 1;
2372         }
2373         break;
2374      case WOBJSEL:   /* Window mask settings for objects */
2375         if (data != snes_ram[offset])
2376         {
2377            snes_ppu.layer[SNES_OAM].window1_invert  = BIT(data, 0);
2378            snes_ppu.layer[SNES_OAM].window1_enabled = BIT(data, 1);
2379            snes_ppu.layer[SNES_OAM].window2_invert  = BIT(data, 2);
2380            snes_ppu.layer[SNES_OAM].window2_enabled = BIT(data, 3);
2381            snes_ppu.layer[SNES_COLOR].window1_invert  = BIT(data, 4);
2382            snes_ppu.layer[SNES_COLOR].window1_enabled = BIT(data, 5);
2383            snes_ppu.layer[SNES_COLOR].window2_invert  = BIT(data, 6);
2384            snes_ppu.layer[SNES_COLOR].window2_enabled = BIT(data, 7);
2385            snes_ppu.update_windows = 1;
2386         }
2387         break;
2388      case WH0:      /* Window 1 left position */
2389         if (data != snes_ram[offset])
2390         {
2391            snes_ppu.window1_left = data;
2392            snes_ppu.update_windows = 1;
2393         }
2394         break;
2395      case WH1:      /* Window 1 right position */
2396         if (data != snes_ram[offset])
2397         {
2398            snes_ppu.window1_right = data;
2399            snes_ppu.update_windows = 1;
2400         }
2401         break;
2402      case WH2:      /* Window 2 left position */
2403         if (data != snes_ram[offset])
2404         {
2405            snes_ppu.window2_left = data;
2406            snes_ppu.update_windows = 1;
2407         }
2408         break;
2409      case WH3:      /* Window 2 right position */
2410         if (data != snes_ram[offset])
2411         {
2412            snes_ppu.window2_right = data;
2413            snes_ppu.update_windows = 1;
2414         }
2415         break;
2416      case WBGLOG:   /* Window mask logic for BG's */
2417         if (data != snes_ram[offset])
2418         {
2419            snes_ppu.layer[SNES_BG1].wlog_mask = data & 0x03;
2420            snes_ppu.layer[SNES_BG2].wlog_mask = (data & 0x0c) >> 2;
2421            snes_ppu.layer[SNES_BG3].wlog_mask = (data & 0x30) >> 4;
2422            snes_ppu.layer[SNES_BG4].wlog_mask = (data & 0xc0) >> 6;
2423            snes_ppu.update_windows = 1;
2424         }
2425         break;
2426      case WOBJLOG:   /* Window mask logic for objects */
2427         if (data != snes_ram[offset])
2428         {
2429            snes_ppu.layer[SNES_OAM].wlog_mask = data & 0x03;
2430            snes_ppu.layer[SNES_COLOR].wlog_mask = (data & 0x0c) >> 2;
2431            snes_ppu.update_windows = 1;
2432         }
2433         break;
2434      case TM:      /* Main screen designation */
2435         snes_ppu.layer[SNES_BG1].main_bg_enabled = BIT(data, 0);
2436         snes_ppu.layer[SNES_BG2].main_bg_enabled = BIT(data, 1);
2437         snes_ppu.layer[SNES_BG3].main_bg_enabled = BIT(data, 2);
2438         snes_ppu.layer[SNES_BG4].main_bg_enabled = BIT(data, 3);
2439         snes_ppu.layer[SNES_OAM].main_bg_enabled = BIT(data, 4);
2440         break;
2441      case TS:      /* Subscreen designation */
2442         snes_ppu.layer[SNES_BG1].sub_bg_enabled = BIT(data, 0);
2443         snes_ppu.layer[SNES_BG2].sub_bg_enabled = BIT(data, 1);
2444         snes_ppu.layer[SNES_BG3].sub_bg_enabled = BIT(data, 2);
2445         snes_ppu.layer[SNES_BG4].sub_bg_enabled = BIT(data, 3);
2446         snes_ppu.layer[SNES_OAM].sub_bg_enabled = BIT(data, 4);
2447         break;
2448      case TMW:      /* Window mask for main screen designation */
2449         snes_ppu.layer[SNES_BG1].main_window_enabled = BIT(data, 0);
2450         snes_ppu.layer[SNES_BG2].main_window_enabled = BIT(data, 1);
2451         snes_ppu.layer[SNES_BG3].main_window_enabled = BIT(data, 2);
2452         snes_ppu.layer[SNES_BG4].main_window_enabled = BIT(data, 3);
2453         snes_ppu.layer[SNES_OAM].main_window_enabled = BIT(data, 4);
2454         break;
2455      case TSW:      /* Window mask for subscreen designation */
2456         snes_ppu.layer[SNES_BG1].sub_window_enabled = BIT(data, 0);
2457         snes_ppu.layer[SNES_BG2].sub_window_enabled = BIT(data, 1);
2458         snes_ppu.layer[SNES_BG3].sub_window_enabled = BIT(data, 2);
2459         snes_ppu.layer[SNES_BG4].sub_window_enabled = BIT(data, 3);
2460         snes_ppu.layer[SNES_OAM].sub_window_enabled = BIT(data, 4);
2461         break;
2462      case CGWSEL:   /* Initial settings for Fixed colour addition or screen addition */
2463         snes_ppu.clip_to_black = (data >> 6) & 0x03;
2464         snes_ppu.prevent_color_math = (data >> 4) & 0x03;
2465         snes_ppu.sub_add_mode = BIT(data, 1);
2466         snes_ppu.direct_color = BIT(data, 0);
2467#ifdef SNES_DBG_REG_W
2468         if ((data & 0x2) != (snes_ram[CGWSEL] & 0x2))
2469            mame_printf_debug( "Add/Sub Layer: %s\n", ((data & 0x2) >> 1) ? "Subscreen" : "Fixed colour" );
2470#endif
2471         break;
2472      case CGADSUB:   /* Addition/Subtraction designation for each screen */
2473         snes_ppu.color_modes = data & 0xc0;
2474         snes_ppu.layer[SNES_BG1].color_math = BIT(data, 0);
2475         snes_ppu.layer[SNES_BG2].color_math = BIT(data, 1);
2476         snes_ppu.layer[SNES_BG3].color_math = BIT(data, 2);
2477         snes_ppu.layer[SNES_BG4].color_math = BIT(data, 3);
2478         snes_ppu.layer[SNES_OAM].color_math = BIT(data, 4);
2479         snes_ppu.layer[SNES_COLOR].color_math = BIT(data, 5);
2480         break;
2481      case COLDATA:   /* Fixed colour data for fixed colour addition/subtraction */
2482         {
2483            /* Store it in the extra space we made in the CGRAM. It doesn't really go there, but it's as good a place as any. */
2484            UINT8 r, g, b;
2485
2486            /* Get existing value. */
2487            r = snes_cgram[FIXED_COLOUR] & 0x1f;
2488            g = (snes_cgram[FIXED_COLOUR] & 0x3e0) >> 5;
2489            b = (snes_cgram[FIXED_COLOUR] & 0x7c00) >> 10;
2490            /* Set new value */
2491            if (data & 0x20)
2492               r = data & 0x1f;
2493            if (data & 0x40)
2494               g = data & 0x1f;
2495            if (data & 0x80)
2496               b = data & 0x1f;
2497            snes_cgram[FIXED_COLOUR] = (r | (g << 5) | (b << 10));
2498         } break;
2499      case SETINI:   /* Screen mode/video select */
2500         snes_ppu.interlace = (data & 0x01) ? 2 : 1;
2501         snes_ppu.obj_interlace = (data & 0x02) ? 2 : 1;
2502         snes_ppu.beam.last_visible_line = (data & 0x04) ? 240 : 225;
2503         snes_ppu.pseudo_hires = BIT(data, 3);
2504         snes_ppu.mode7.extbg = BIT(data, 6);
2505         snes_dynamic_res_change(space->machine);
2506#ifdef SNES_DBG_REG_W
2507         if ((data & 0x8) != (snes_ram[SETINI] & 0x8))
2508            mame_printf_debug( "Pseudo 512 mode: %s\n", (data & 0x8) ? "on" : "off" );
2509#endif
2510         break;
2511      }
2512
2513   snes_ram[offset] = data;
2514}
2515
16722516/***** Debug Functions *****/
16732517
16742518#ifdef SNES_LAYER_DEBUG

Previous 15994 Revisions Next


© 1998-2018 The MESS Team