src/mame/machine/snes.c
| r7657 | r7658 | |
| 36 | 36 | static const UINT16 vram_fgr_shiftab[4] = { 0, 5, 6, 7 }; |
| 37 | 37 | |
| 38 | 38 | static void snes_dma(const address_space *space, UINT8 channels); |
| 39 | | static void snes_hdma_init(running_machine *machine); |
| 39 | static void snes_hdma_init(const address_space *space); |
| 40 | 40 | static void snes_hdma(const address_space *space); |
| 41 | 41 | |
| 42 | 42 | struct snes_cart_info snes_cart; |
| r7657 | r7658 | |
| 119 | 119 | |
| 120 | 120 | static TIMER_CALLBACK( snes_reset_hdma ) |
| 121 | 121 | { |
| 122 | | snes_hdma_init(machine); |
| 122 | snes_state *state = (snes_state *)machine->driver_data; |
| 123 | const address_space *cpu0space = cpu_get_address_space(state->maincpu, ADDRESS_SPACE_PROGRAM); |
| 124 | snes_hdma_init(cpu0space); |
| 123 | 125 | } |
| 124 | 126 | |
| 125 | 127 | static TIMER_CALLBACK( snes_scanline_tick ) |
| r7657 | r7658 | |
| 190 | 192 | // hdma reset happens at scanline 0, H=~6 |
| 191 | 193 | if (snes_ppu.beam.current_vert == 0) |
| 192 | 194 | { |
| 193 | | snes_hdma_init(machine); |
| 195 | const address_space *cpu0space = cpu_get_address_space(state->maincpu, ADDRESS_SPACE_PROGRAM); |
| 196 | snes_hdma_init(cpu0space); |
| 194 | 197 | } |
| 195 | 198 | |
| 196 | 199 | /* three lines after start of vblank we update the controllers (value from snes9x) */ |
| r7657 | r7658 | |
| 440 | 443 | } |
| 441 | 444 | } |
| 442 | 445 | |
| 446 | /************************************************* |
| 447 | |
| 448 | SNES OAM accesses: |
| 449 | |
| 450 | OAM accesses during active display are allowed. |
| 451 | The actual address varies during rendering, as the |
| 452 | PPU reads in data itself for processing. |
| 453 | Unfortunately, no one has been able (yet) to |
| 454 | determine how this works. The only known game to |
| 455 | actually access OAM during active display is |
| 456 | Uniracers and it expects accesses to map to |
| 457 | offset 0x0218. Hence, following byuu's choice |
| 458 | we rerouted OAM accesses during active display |
| 459 | to 0x0218 (0x010c in our snes_oam). |
| 460 | This is a hack, but it is more accurate than |
| 461 | writing to the 'expected' address set by |
| 462 | $2102,$2103. |
| 463 | |
| 464 | Notice that, since snes_ram[OAMDATA] is never |
| 465 | read/written directly, we use it as an index |
| 466 | to choose the high/low byte of the snes_oam word. |
| 467 | *************************************************/ |
| 468 | |
| 469 | static READ8_HANDLER( snes_oam_read ) |
| 470 | { |
| 471 | offset &= 0x1ff; |
| 472 | |
| 473 | if (offset & 0x100) |
| 474 | offset &= 0x10f; |
| 475 | |
| 476 | if (!snes_ppu.screen_disabled) |
| 477 | { |
| 478 | UINT16 v = video_screen_get_vpos(space->machine->primary_screen); |
| 479 | |
| 480 | if (v < snes_ppu.beam.last_visible_line) |
| 481 | offset = 0x010c; |
| 482 | } |
| 483 | |
| 484 | return (snes_oam[offset] >> (snes_ram[OAMDATA] << 3)) & 0xff;; |
| 485 | } |
| 486 | |
| 487 | static WRITE8_HANDLER( snes_oam_write ) |
| 488 | { |
| 489 | offset &= 0x1ff; |
| 490 | |
| 491 | if (offset & 0x100) |
| 492 | offset &= 0x10f; |
| 493 | |
| 494 | if (!snes_ppu.screen_disabled) |
| 495 | { |
| 496 | UINT16 v = video_screen_get_vpos(space->machine->primary_screen); |
| 497 | |
| 498 | if (v < snes_ppu.beam.last_visible_line) |
| 499 | offset = 0x010c; |
| 500 | } |
| 501 | |
| 502 | if (!(snes_ram[OAMDATA])) |
| 503 | snes_oam[offset] = (snes_oam[offset] & 0xff00) | (data << 0); |
| 504 | else |
| 505 | snes_oam[offset] = (snes_oam[offset] & 0x00ff) | (data << 8); |
| 506 | } |
| 507 | |
| 508 | /************************************************* |
| 509 | |
| 510 | SNES CGRAM accesses: |
| 511 | |
| 512 | CGRAM writes during hblank are valid. During |
| 513 | active display, the actual address the data |
| 514 | is written to varies, as the PPU itself changes |
| 515 | the address. Like OAM, it is not known the exact |
| 516 | algorithm used, but no commercial software seems |
| 517 | to attempt this. While byuu, in his emu, maps |
| 518 | those accesses to 0x01ff, because it is more |
| 519 | accurate to invalidate the 'expected' address |
| 520 | than not, MESS has issues if we don't write to |
| 521 | the expected address (see e.g. Tokimeki Memorial). |
| 522 | This is because writes should work during hblank |
| 523 | (so that the game can produce color fading), but |
| 524 | ends up not working with the conditions below. |
| 525 | Hence, for the moment, we only document the |
| 526 | solution adopted by BSNES without enabling it. |
| 527 | *************************************************/ |
| 528 | |
| 529 | static READ8_HANDLER( snes_cgram_read ) |
| 530 | { |
| 531 | UINT8 res = 0; |
| 532 | offset &= 0x1ff; |
| 533 | |
| 534 | #if 0 |
| 535 | if (!snes_ppu.screen_disabled) |
| 536 | { |
| 537 | UINT16 v = video_screen_get_vpos(space->machine->primary_screen); |
| 538 | UINT16 h = video_screen_get_hpos(space->machine->primary_screen); |
| 539 | |
| 540 | if (v < snes_ppu.beam.last_visible_line && h >= 128 && h < 1096) |
| 541 | offset = 0x1ff; |
| 542 | } |
| 543 | #endif |
| 544 | |
| 545 | res = ((UINT8 *)snes_cgram)[offset]; |
| 546 | |
| 547 | // CGRAM palette data format is 15-bits (0,bbbbb,ggggg,rrrrr). |
| 548 | // Highest bit is simply ignored. |
| 549 | if (offset & 0x01) |
| 550 | res &= 0x7f; |
| 551 | |
| 552 | return res; |
| 553 | } |
| 554 | |
| 555 | static WRITE8_HANDLER( snes_cgram_write ) |
| 556 | { |
| 557 | offset &= 0x1ff; |
| 558 | |
| 559 | #if 0 |
| 560 | // FIXME: this currently breaks some games (e.g. Tokimeki Memorial), |
| 561 | // even if it's expected to be more accurate than allowing for |
| 562 | // writes to the cgram address |
| 563 | if (!snes_ppu.screen_disabled) |
| 564 | { |
| 565 | UINT16 v = video_screen_get_vpos(space->machine->primary_screen); |
| 566 | UINT16 h = video_screen_get_hpos(space->machine->primary_screen); |
| 567 | |
| 568 | if (v < snes_ppu.beam.last_visible_line && h >= 128 && h < 1096) |
| 569 | offset = 0x1ff; |
| 570 | } |
| 571 | #endif |
| 572 | |
| 573 | // CGRAM palette data format is 15-bits (0,bbbbb,ggggg,rrrrr). |
| 574 | // Highest bit is simply ignored. |
| 575 | if (offset & 0x01) |
| 576 | data &= 0x7f; |
| 577 | |
| 578 | ((UINT8 *)snes_cgram)[offset] = data; |
| 579 | } |
| 580 | |
| 443 | 581 | /* |
| 444 | 582 | * DR - Double read : address is read twice to return a 16bit value. |
| 445 | 583 | * low - This is the low byte of a 16 or 24 bit value |
| r7657 | r7658 | |
| 539 | 677 | snes_latch_counters(space->machine); |
| 540 | 678 | return snes_open_bus_r(space, 0); /* Return value is meaningless */ |
| 541 | 679 | case ROAMDATA: /* Read data from OAM (DR) */ |
| 680 | snes_ppu.ppu1_open_bus = snes_oam_read(space, snes_ppu.oam.address); |
| 681 | snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2; |
| 682 | if (!snes_ram[OAMDATA]) |
| 542 | 683 | { |
| 543 | | int oam_addr = snes_ppu.oam.address; |
| 544 | | |
| 545 | | if (oam_addr & 0x100) |
| 546 | | { |
| 547 | | oam_addr &= 0x10f; |
| 548 | | } |
| 549 | | else |
| 550 | | { |
| 551 | | oam_addr &= 0x1ff; |
| 552 | | } |
| 553 | | |
| 554 | | snes_ppu.ppu1_open_bus = (snes_oam[oam_addr] >> (snes_ram[OAMDATA] << 3)) & 0xff; |
| 555 | | snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2; |
| 556 | | if (!snes_ram[OAMDATA]) |
| 557 | | { |
| 558 | | snes_ppu.oam.address++; |
| 559 | | snes_ppu.oam.address &= 0x1ff; |
| 560 | | snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0; |
| 561 | | } |
| 562 | | return snes_ppu.ppu1_open_bus; |
| 684 | snes_ppu.oam.address++; |
| 685 | snes_ppu.oam.address &= 0x1ff; |
| 686 | snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0; |
| 563 | 687 | } |
| 688 | return snes_ppu.ppu1_open_bus; |
| 564 | 689 | case RVMDATAL: /* Read data from VRAM (low) */ |
| 565 | 690 | { |
| 566 | 691 | UINT32 addr = snes_get_vram_address(space->machine) << 1; |
| r7657 | r7658 | |
| 596 | 721 | return snes_ppu.ppu1_open_bus; |
| 597 | 722 | } |
| 598 | 723 | case RCGDATA: /* Read data from CGRAM */ |
| 599 | | if (!(state->cgram_address & 0x01)) |
| 600 | | { |
| 601 | | snes_ppu.ppu2_open_bus = ((UINT8 *)snes_cgram)[state->cgram_address] & 0xff; |
| 602 | | } |
| 603 | | else |
| 604 | | { |
| 605 | | snes_ppu.ppu2_open_bus &= 0x80; |
| 606 | | snes_ppu.ppu2_open_bus |= ((UINT8 *)snes_cgram)[state->cgram_address] & 0x7f; |
| 607 | | } |
| 724 | if (!(state->cgram_address & 0x01)) |
| 725 | snes_ppu.ppu2_open_bus = snes_cgram_read(space, state->cgram_address); |
| 726 | else |
| 727 | { |
| 728 | snes_ppu.ppu2_open_bus &= 0x80; |
| 729 | snes_ppu.ppu2_open_bus |= snes_cgram_read(space, state->cgram_address) & 0x7f; |
| 730 | } |
| 608 | 731 | |
| 609 | | state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2); |
| 610 | | return snes_ppu.ppu2_open_bus; |
| 732 | state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2); |
| 733 | return snes_ppu.ppu2_open_bus; |
| 611 | 734 | case OPHCT: /* Horizontal counter data by ext/soft latch */ |
| 735 | if (state->read_ophct) |
| 612 | 736 | { |
| 613 | | if (state->read_ophct) |
| 614 | | { |
| 615 | | snes_ppu.ppu2_open_bus &= 0xfe; |
| 616 | | snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_horz >> 8) & 0x01; |
| 617 | | } |
| 618 | | else |
| 619 | | { |
| 620 | | snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_horz & 0xff; |
| 621 | | } |
| 622 | | state->read_ophct ^= 1; |
| 623 | | return snes_ppu.ppu2_open_bus; |
| 737 | snes_ppu.ppu2_open_bus &= 0xfe; |
| 738 | snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_horz >> 8) & 0x01; |
| 624 | 739 | } |
| 740 | else |
| 741 | { |
| 742 | snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_horz & 0xff; |
| 743 | } |
| 744 | state->read_ophct ^= 1; |
| 745 | return snes_ppu.ppu2_open_bus; |
| 625 | 746 | case OPVCT: /* Vertical counter data by ext/soft latch */ |
| 747 | if (state->read_opvct) |
| 626 | 748 | { |
| 627 | | if (state->read_opvct) |
| 628 | | { |
| 629 | | snes_ppu.ppu2_open_bus &= 0xfe; |
| 630 | | snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_vert >> 8) & 0x01; |
| 631 | | } |
| 632 | | else |
| 633 | | { |
| 634 | | snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_vert & 0xff; |
| 635 | | } |
| 636 | | state->read_opvct ^= 1; |
| 637 | | return snes_ppu.ppu2_open_bus; |
| 749 | snes_ppu.ppu2_open_bus &= 0xfe; |
| 750 | snes_ppu.ppu2_open_bus |= (snes_ppu.beam.latch_vert >> 8) & 0x01; |
| 638 | 751 | } |
| 752 | else |
| 753 | { |
| 754 | snes_ppu.ppu2_open_bus = snes_ppu.beam.latch_vert & 0xff; |
| 755 | } |
| 756 | state->read_opvct ^= 1; |
| 757 | return snes_ppu.ppu2_open_bus; |
| 639 | 758 | case STAT77: /* PPU status flag and version number */ |
| 640 | 759 | value = snes_ppu.stat77_flags & 0xc0; // 0x80 & 0x40 are Time Over / Range Over Sprite flags, set by the video code |
| 641 | 760 | // 0x20 - Master/slave mode select. Little is known about this bit. We always seem to read back 0 here. |
| r7657 | r7658 | |
| 669 | 788 | case WMADDH: /* Address to read/write to wram (high) */ |
| 670 | 789 | return snes_ram[offset]; |
| 671 | 790 | case OLDJOY1: /* Data for old NES controllers (JOYSER1) */ |
| 672 | | { |
| 673 | | if (snes_ram[offset] & 0x1) |
| 674 | | { |
| 675 | | return 0 | (snes_open_bus_r(space, 0) & 0xfc); //correct? |
| 676 | | } |
| 791 | if (snes_ram[offset] & 0x1) |
| 792 | return 0 | (snes_open_bus_r(space, 0) & 0xfc); //correct? |
| 677 | 793 | |
| 678 | | value = state->oldjoy1_read(space->machine); |
| 794 | value = state->oldjoy1_read(space->machine); |
| 679 | 795 | |
| 680 | | return (value & 0x03) | (snes_open_bus_r(space, 0) & 0xfc); //correct? |
| 681 | | } |
| 796 | return (value & 0x03) | (snes_open_bus_r(space, 0) & 0xfc); //correct? |
| 682 | 797 | case OLDJOY2: /* Data for old NES controllers (JOYSER2) */ |
| 683 | | { |
| 684 | | if (snes_ram[OLDJOY1] & 0x1) |
| 685 | | { |
| 686 | | return 0 | 0x1c | (snes_open_bus_r(space, 0) & 0xe0); //correct? |
| 687 | | } |
| 798 | if (snes_ram[OLDJOY1] & 0x1) |
| 799 | return 0 | 0x1c | (snes_open_bus_r(space, 0) & 0xe0); //correct? |
| 688 | 800 | |
| 689 | | value = state->oldjoy2_read(space->machine); |
| 801 | value = state->oldjoy2_read(space->machine); |
| 690 | 802 | |
| 691 | | return value | 0x1c | (snes_open_bus_r(space, 0) & 0xe0); //correct? |
| 692 | | } |
| 803 | return value | 0x1c | (snes_open_bus_r(space, 0) & 0xe0); //correct? |
| 693 | 804 | case HTIMEL: |
| 694 | 805 | case HTIMEH: |
| 695 | 806 | case VTIMEL: |
| r7657 | r7658 | |
| 878 | 989 | snes_ram[OAMDATA] = 0; |
| 879 | 990 | break; |
| 880 | 991 | case OAMDATA: /* Data for OAM write (DW) */ |
| 992 | if (snes_ppu.oam.address >= 0x100) |
| 993 | snes_oam_write(space, snes_ppu.oam.address, data); |
| 994 | else |
| 881 | 995 | { |
| 882 | | int oam_addr = snes_ppu.oam.address; |
| 883 | | |
| 884 | | if (oam_addr >= 0x100) |
| 885 | | { |
| 886 | | oam_addr &= 0x10f; |
| 887 | | if (!(snes_ram[OAMDATA])) |
| 888 | | { |
| 889 | | snes_oam[oam_addr] &= 0xff00; |
| 890 | | snes_oam[oam_addr] |= data; |
| 891 | | } |
| 892 | | else |
| 893 | | { |
| 894 | | snes_oam[oam_addr] &= 0x00ff; |
| 895 | | snes_oam[oam_addr] |= (data << 8); |
| 896 | | } |
| 897 | | } |
| 996 | if (!snes_ram[OAMDATA]) |
| 997 | snes_ppu.oam.write_latch = data; |
| 898 | 998 | else |
| 899 | 999 | { |
| 900 | | oam_addr &= 0x1ff; |
| 901 | | if (!(snes_ram[OAMDATA])) |
| 902 | | { |
| 903 | | snes_ppu.oam.write_latch = data; |
| 904 | | } |
| 905 | | else |
| 906 | | { |
| 907 | | snes_oam[oam_addr] = (data << 8) | snes_ppu.oam.write_latch; |
| 908 | | } |
| 909 | | } |
| 910 | | snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2; |
| 911 | | if (snes_ram[OAMDATA] == 0) |
| 912 | | { |
| 1000 | // in this case, we not only write data to the upper byte of the word, |
| 1001 | // but also snes_ppu.oam.write_latch to the lower byte (recall that |
| 1002 | // snes_ram[OAMDATA] is used to select high/low byte) |
| 1003 | snes_oam_write(space, snes_ppu.oam.address, data); |
| 913 | 1004 | snes_ram[OAMDATA] = 0; |
| 914 | | snes_ppu.oam.address++; |
| 915 | | snes_ppu.oam.address &= 0x1ff; |
| 916 | | snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0; |
| 1005 | snes_oam_write(space, snes_ppu.oam.address, snes_ppu.oam.write_latch); |
| 1006 | snes_ram[OAMDATA] = 1; |
| 917 | 1007 | } |
| 918 | | return; |
| 919 | 1008 | } |
| 1009 | snes_ram[OAMDATA] = (snes_ram[OAMDATA] + 1) % 2; |
| 1010 | if (!snes_ram[OAMDATA]) |
| 1011 | { |
| 1012 | snes_ppu.oam.address++; |
| 1013 | snes_ppu.oam.address &= 0x1ff; |
| 1014 | snes_ppu.oam.first_sprite = snes_ppu.oam.priority_rotation ? (snes_ppu.oam.address >> 1) & 127 : 0; |
| 1015 | } |
| 1016 | return; |
| 920 | 1017 | case BGMODE: /* BG mode and character size settings */ |
| 921 | 1018 | snes_ppu.mode = data & 0x07; |
| 922 | 1019 | snes_dynamic_res_change(space->machine); |
| r7657 | r7658 | |
| 1084 | 1181 | snes_ppu.mode7_last_scroll = data; |
| 1085 | 1182 | break; |
| 1086 | 1183 | case CGADD: /* Initial address for colour RAM writing */ |
| 1087 | | /* CGRAM is 16-bit, but when reading/writing we treat it as |
| 1088 | | * 8-bit, so we need to double the address */ |
| 1184 | /* CGRAM is 16-bit, but when reading/writing we treat it as 8-bit, so we need to double the address */ |
| 1089 | 1185 | state->cgram_address = data << 1; |
| 1090 | 1186 | break; |
| 1091 | 1187 | case CGDATA: /* Data for colour RAM */ |
| 1092 | | ((UINT8 *)snes_cgram)[state->cgram_address] = data; |
| 1188 | snes_cgram_write(space, state->cgram_address, data); |
| 1093 | 1189 | state->cgram_address = (state->cgram_address + 1) % (SNES_CGRAM_SIZE - 2); |
| 1094 | 1190 | break; |
| 1095 | 1191 | case W12SEL: /* Window mask settings for BG1-2 */ |
| r7657 | r7658 | |
| 2489 | 2585 | memory_write_byte(space, dst, memory_read_byte(space, src)); |
| 2490 | 2586 | } |
| 2491 | 2587 | |
| 2492 | | #if 0 |
| 2493 | | // FIXME: we need to use snes_state->hdma_iaddr[i] where appropriate (we now still |
| 2494 | | // use snes_state->trans_size[i] to avoid regressions in this first wip) |
| 2495 | | // and try to make the addr recovering/updating a bit more abstract |
| 2588 | /* WIP: These have the advantage to automatically update the address, but then we would need to |
| 2589 | check again if the transfer is direct/indirect at each step... is it worth? */ |
| 2496 | 2590 | INLINE UINT32 snes_get_hdma_addr( running_machine *machine, int dma ) |
| 2497 | 2591 | { |
| 2498 | 2592 | snes_state *state = (snes_state *)machine->driver_data; |
| r7657 | r7658 | |
| 2502 | 2596 | INLINE UINT32 snes_get_hdma_iaddr( running_machine *machine, int dma ) |
| 2503 | 2597 | { |
| 2504 | 2598 | snes_state *state = (snes_state *)machine->driver_data; |
| 2505 | | return (state->dma_channel[dma].ibank << 16) | (state->dma_channel[dma].hdma_iaddr++); |
| 2599 | return (state->dma_channel[dma].ibank << 16) | (state->dma_channel[dma].trans_size++); |
| 2506 | 2600 | } |
| 2507 | | #endif |
| 2508 | 2601 | |
| 2509 | | static void snes_hdma_init( running_machine *machine ) |
| 2602 | static UINT8 snes_hdma_read( const address_space *space, UINT32 abus ) |
| 2510 | 2603 | { |
| 2604 | if (!dma_abus_valid(abus)) |
| 2605 | return 0; |
| 2606 | |
| 2607 | return memory_read_byte(space, abus); |
| 2608 | } |
| 2609 | |
| 2610 | static int is_last_active_channel( running_machine *machine, int dma ) |
| 2611 | { |
| 2511 | 2612 | snes_state *state = (snes_state *)machine->driver_data; |
| 2512 | 2613 | int i; |
| 2513 | 2614 | |
| 2615 | for (i = dma + 1; i < 8; i++) |
| 2616 | { |
| 2617 | if (BIT(state->hdmaen, i) && state->dma_channel[i].hdma_line_counter) |
| 2618 | return 0; // there is still at least another channel with incomplete HDMA |
| 2619 | } |
| 2620 | |
| 2621 | // if we arrive here, all hdma transfers from (dma + 1) to 7 are completed or not active |
| 2622 | return 1; |
| 2623 | } |
| 2624 | |
| 2625 | static void snes_hdma_update( const address_space *space, int dma ) |
| 2626 | { |
| 2627 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 2628 | UINT32 abus = snes_get_hdma_addr(space->machine, dma); |
| 2629 | |
| 2630 | state->dma_channel[dma].hdma_line_counter = snes_hdma_read(space, abus); |
| 2631 | |
| 2632 | if (state->dma_channel[dma].dmap & 0x40) |
| 2633 | { |
| 2634 | /* One oddity: if $43xA is 0 and this is the last active HDMA channel for this scanline, only load |
| 2635 | one byte for Address, and use the $00 for the low byte. So Address ends up incremented one less than |
| 2636 | otherwise expected */ |
| 2637 | |
| 2638 | abus = snes_get_hdma_addr(space->machine, dma); |
| 2639 | state->dma_channel[dma].trans_size = snes_hdma_read(space, abus) << 8; |
| 2640 | |
| 2641 | if (state->dma_channel[dma].hdma_line_counter || !is_last_active_channel(space->machine, dma)) |
| 2642 | { |
| 2643 | // we enter here if we have more transfers to be done or if there are other active channels after this one |
| 2644 | abus = snes_get_hdma_addr(space->machine, dma); |
| 2645 | state->dma_channel[dma].trans_size >>= 8; |
| 2646 | state->dma_channel[dma].trans_size |= snes_hdma_read(space, abus) << 8; |
| 2647 | } |
| 2648 | } |
| 2649 | |
| 2650 | if (!state->dma_channel[dma].hdma_line_counter) |
| 2651 | state->hdmaen &= ~(1 << dma); |
| 2652 | |
| 2653 | state->dma_channel[dma].do_transfer = 1; |
| 2654 | } |
| 2655 | |
| 2656 | static void snes_hdma_init( const address_space *space ) |
| 2657 | { |
| 2658 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 2659 | int i; |
| 2660 | |
| 2514 | 2661 | state->hdmaen = snes_ram[HDMAEN]; |
| 2515 | 2662 | for (i = 0; i < 8; i++) |
| 2516 | 2663 | { |
| 2517 | 2664 | if (BIT(state->hdmaen, i)) |
| 2518 | 2665 | { |
| 2519 | 2666 | state->dma_channel[i].hdma_addr = state->dma_channel[i].src_addr; |
| 2520 | | state->dma_channel[i].hdma_line_counter = 0; |
| 2667 | snes_hdma_update(space, i); |
| 2521 | 2668 | } |
| 2522 | 2669 | } |
| 2523 | 2670 | } |
| r7657 | r7658 | |
| 2525 | 2672 | static void snes_hdma( const address_space *space ) |
| 2526 | 2673 | { |
| 2527 | 2674 | snes_state *state = (snes_state *)space->machine->driver_data; |
| 2528 | | UINT8 i, contmode; |
| 2529 | 2675 | UINT16 bbus; |
| 2530 | 2676 | UINT32 abus; |
| 2677 | int i; |
| 2531 | 2678 | |
| 2532 | | /* Assume priority of the 8 DMA channels is 0-7 */ |
| 2533 | 2679 | for (i = 0; i < 8; i++) |
| 2534 | 2680 | { |
| 2535 | 2681 | if (BIT(state->hdmaen, i)) |
| 2536 | 2682 | { |
| 2537 | | /* Check if we need to read a new line from the table */ |
| 2538 | | if (!(state->dma_channel[i].hdma_line_counter & 0x7f)) |
| 2683 | if (state->dma_channel[i].do_transfer) |
| 2539 | 2684 | { |
| 2540 | | abus = (state->dma_channel[i].bank << 16) + state->dma_channel[i].hdma_addr; |
| 2685 | /* Get transfer addresses */ |
| 2686 | if (state->dma_channel[i].dmap & 0x40) /* Indirect */ |
| 2687 | abus = (state->dma_channel[i].ibank << 16) + state->dma_channel[i].trans_size; |
| 2688 | else /* Absolute */ |
| 2689 | abus = (state->dma_channel[i].bank << 16) + state->dma_channel[i].hdma_addr; |
| 2541 | 2690 | |
| 2542 | | /* Get the number of lines */ |
| 2543 | | state->dma_channel[i].hdma_line_counter = memory_read_byte(space, abus); |
| 2544 | | if (!state->dma_channel[i].hdma_line_counter) |
| 2691 | bbus = state->dma_channel[i].dest_addr + 0x2100; |
| 2692 | |
| 2693 | switch (state->dma_channel[i].dmap & 0x07) |
| 2545 | 2694 | { |
| 2546 | | /* No more lines so clear HDMA */ |
| 2547 | | state->hdmaen &= ~(1 << i); |
| 2548 | | continue; |
| 2549 | | } |
| 2550 | | abus++; |
| 2551 | | state->dma_channel[i].hdma_addr = abus; |
| 2552 | | if (state->dma_channel[i].dmap & 0x40) |
| 2553 | | { |
| 2554 | | state->dma_channel[i].trans_size = memory_read_byte(space, abus++); |
| 2555 | | state->dma_channel[i].trans_size |= (memory_read_byte(space, abus++) << 8); |
| 2556 | | state->dma_channel[i].hdma_addr = abus; |
| 2557 | | } |
| 2558 | | } |
| 2559 | | |
| 2560 | | contmode = (--state->dma_channel[i].hdma_line_counter) & 0x80; |
| 2561 | | |
| 2562 | | /* Transfer addresses */ |
| 2563 | | if (state->dma_channel[i].dmap & 0x40) /* Indirect */ |
| 2564 | | abus = (state->dma_channel[i].ibank << 16) + state->dma_channel[i].trans_size; |
| 2565 | | else /* Absolute */ |
| 2566 | | abus = (state->dma_channel[i].bank << 16) + state->dma_channel[i].hdma_addr; |
| 2567 | | |
| 2568 | | bbus = state->dma_channel[i].dest_addr + 0x2100; |
| 2569 | | |
| 2570 | | #ifdef SNES_DBG_HDMA |
| 2571 | | mame_printf_debug( "HDMA-Ch: %d(%s) abus: %X bbus: %X type: %d(%X)\n", i, state->dma_channel[i].dmap & 0x40 ? "Indirect" : "Absolute", abus, bbus, state->dma_channel[i].dmap & 0x07, state->dma_channel[i].hdma_addr); |
| 2572 | | #endif |
| 2573 | | |
| 2574 | | switch (state->dma_channel[i].dmap & 0x07) |
| 2575 | | { |
| 2576 | | case 0: /* 1 register write once (1 byte: p ) */ |
| 2577 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2578 | | break; |
| 2579 | | case 5: /* 2 registers write twice alternate (4 bytes: p, p+1, p, p+1) */ |
| 2580 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2581 | | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2582 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2583 | | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2584 | | break; |
| 2585 | | case 1: /* 2 registers write once (2 bytes: p, p+1 ) */ |
| 2586 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2587 | | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2588 | | break; |
| 2589 | | case 2: /* 1 register write twice (2 bytes: p, p ) */ |
| 2590 | | case 6: |
| 2591 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2592 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2593 | | break; |
| 2594 | | case 3: /* 2 registers write twice each (4 bytes: p, p, p+1, p+1) */ |
| 2595 | | case 7: |
| 2596 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2597 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2598 | | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2599 | | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2600 | | break; |
| 2601 | | case 4: /* 4 registers write once (4 bytes: p, p+1, p+2, p+3) */ |
| 2602 | | snes_dma_transfer(space, i, abus++, bbus); |
| 2603 | | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2604 | | snes_dma_transfer(space, i, abus++, bbus + 2); |
| 2605 | | snes_dma_transfer(space, i, abus++, bbus + 3); |
| 2606 | | break; |
| 2607 | | default: |
| 2695 | case 0: /* 1 register write once (1 byte: p ) */ |
| 2696 | snes_dma_transfer(space, i, abus++, bbus); |
| 2697 | break; |
| 2698 | case 5: /* 2 registers write twice alternate (4 bytes: p, p+1, p, p+1) */ |
| 2699 | snes_dma_transfer(space, i, abus++, bbus); |
| 2700 | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2701 | snes_dma_transfer(space, i, abus++, bbus); |
| 2702 | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2703 | break; |
| 2704 | case 1: /* 2 registers write once (2 bytes: p, p+1 ) */ |
| 2705 | snes_dma_transfer(space, i, abus++, bbus); |
| 2706 | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2707 | break; |
| 2708 | case 2: /* 1 register write twice (2 bytes: p, p ) */ |
| 2709 | case 6: |
| 2710 | snes_dma_transfer(space, i, abus++, bbus); |
| 2711 | snes_dma_transfer(space, i, abus++, bbus); |
| 2712 | break; |
| 2713 | case 3: /* 2 registers write twice each (4 bytes: p, p, p+1, p+1) */ |
| 2714 | case 7: |
| 2715 | snes_dma_transfer(space, i, abus++, bbus); |
| 2716 | snes_dma_transfer(space, i, abus++, bbus); |
| 2717 | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2718 | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2719 | break; |
| 2720 | case 4: /* 4 registers write once (4 bytes: p, p+1, p+2, p+3) */ |
| 2721 | snes_dma_transfer(space, i, abus++, bbus); |
| 2722 | snes_dma_transfer(space, i, abus++, bbus + 1); |
| 2723 | snes_dma_transfer(space, i, abus++, bbus + 2); |
| 2724 | snes_dma_transfer(space, i, abus++, bbus + 3); |
| 2725 | break; |
| 2726 | default: |
| 2608 | 2727 | #ifdef MAME_DEBUG |
| 2609 | | mame_printf_debug( " HDMA of unsupported type: %d\n", state->dma_channel[i].dmap & 0x07); |
| 2728 | mame_printf_debug( " HDMA of unsupported type: %d\n", state->dma_channel[i].dmap & 0x07); |
| 2610 | 2729 | #endif |
| 2611 | | break; |
| 2612 | | } |
| 2730 | break; |
| 2731 | } |
| 2613 | 2732 | |
| 2614 | | /* Update address */ |
| 2615 | | if (contmode) |
| 2616 | | { |
| 2617 | 2733 | if (state->dma_channel[i].dmap & 0x40) /* Indirect */ |
| 2618 | 2734 | state->dma_channel[i].trans_size = abus; |
| 2619 | 2735 | else /* Absolute */ |
| 2620 | 2736 | state->dma_channel[i].hdma_addr = abus; |
| 2737 | |
| 2621 | 2738 | } |
| 2739 | } |
| 2740 | } |
| 2622 | 2741 | |
| 2742 | for (i = 0; i < 8; i++) |
| 2743 | { |
| 2744 | if (BIT(state->hdmaen, i)) |
| 2745 | { |
| 2746 | state->dma_channel[i].do_transfer = (--state->dma_channel[i].hdma_line_counter) & 0x80; |
| 2747 | |
| 2623 | 2748 | if (!(state->dma_channel[i].hdma_line_counter & 0x7f)) |
| 2624 | | { |
| 2625 | | if (!(state->dma_channel[i].dmap & 0x40)) /* Absolute */ |
| 2626 | | { |
| 2627 | | if (!contmode) |
| 2628 | | state->dma_channel[i].hdma_addr = abus; |
| 2629 | | } |
| 2630 | | } |
| 2749 | snes_hdma_update(space, i); |
| 2631 | 2750 | } |
| 2632 | 2751 | } |
| 2633 | 2752 | } |