src/mess/video/gb.c
| r1512 | r1513 | |
| 7 | 7 | Original code Carsten Sorensen 1998 |
| 8 | 8 | Mess modifications, bug fixes and speedups Hans de Goede 1998 |
| 9 | 9 | Bug fixes, SGB and GBC code Anthony Kruize 2002 |
| 10 | | Improvements to match real hardware Wilbert Pol 2006,2007 |
| 10 | Improvements to match real hardware Wilbert Pol 2006-2008 |
| 11 | 11 | |
| 12 | 12 | ***************************************************************************/ |
| 13 | 13 | |
| r1512 | r1513 | |
| 38 | 38 | #define GBCOCPS gb_vid_regs[0x2A] /* Object palette spec */ |
| 39 | 39 | #define GBCOCPD gb_vid_regs[0x2B] /* Object palette data */ |
| 40 | 40 | |
| 41 | enum { |
| 42 | UNLOCKED=0, |
| 43 | LOCKED |
| 44 | }; |
| 45 | |
| 41 | 46 | #define _NR_GB_VID_REGS 0x40 |
| 42 | 47 | |
| 43 | 48 | static UINT8 bg_zbuf[160]; |
| r1512 | r1513 | |
| 92 | 97 | int delayed_line_irq; |
| 93 | 98 | int sprite_cycles; |
| 94 | 99 | int scrollx_adjust; |
| 100 | int oam_locked; |
| 101 | int vram_locked; |
| 95 | 102 | struct layer_struct layer[2]; |
| 96 | 103 | emu_timer *lcd_timer; |
| 97 | 104 | } gb_lcd; |
| r1512 | r1513 | |
| 1264 | 1271 | LCDSTAT = ( LCDSTAT & 0xF8 ) | 0x01; |
| 1265 | 1272 | gb_lcd.mode = 1; |
| 1266 | 1273 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(292,0), GB_LCD_STATE_LY9X_M1_INC, attotime_never ); |
| 1274 | gb_lcd.oam_locked = UNLOCKED; |
| 1275 | gb_lcd.vram_locked = UNLOCKED; |
| 1267 | 1276 | break; |
| 1268 | 1277 | } |
| 1269 | 1278 | } |
| r1512 | r1513 | |
| 1331 | 1340 | /* Set Mode 0 lcdstate */ |
| 1332 | 1341 | gb_lcd.mode = 0; |
| 1333 | 1342 | LCDSTAT &= 0xFC; |
| 1343 | gb_lcd.oam_locked = UNLOCKED; |
| 1344 | gb_lcd.vram_locked = UNLOCKED; |
| 1334 | 1345 | /* |
| 1335 | 1346 | There seems to a kind of feature in the gameboy hardware when the lowest bits of the |
| 1336 | 1347 | SCROLLX register equals 3 or 7, then the delayed M0 irq is triggered 4 cycles later |
| r1512 | r1513 | |
| 1399 | 1410 | /* Set Mode 2 lcdstate */ |
| 1400 | 1411 | gb_lcd.mode = 2; |
| 1401 | 1412 | LCDSTAT = ( LCDSTAT & 0xFC ) | 0x02; |
| 1413 | gb_lcd.oam_locked = LOCKED; |
| 1402 | 1414 | /* Generate lcd interrupt if requested */ |
| 1403 | 1415 | if ( ( LCDSTAT & 0x20 ) && ! gb_lcd.line_irq && ! gb_lcd.line_irq ) { |
| 1404 | 1416 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| r1512 | r1513 | |
| 1411 | 1423 | case GB_LCD_STATE_LYXX_M2: /* Switch to mode 2 */ |
| 1412 | 1424 | /* Update STAT register to the correct state */ |
| 1413 | 1425 | LCDSTAT = (LCDSTAT & 0xFC) | 0x02; |
| 1426 | gb_lcd.oam_locked = LOCKED; |
| 1414 | 1427 | /* Generate lcd interrupt if requested */ |
| 1415 | 1428 | if ( ( gb_lcd.delayed_line_irq && gb_lcd.triggering_line_irq && ! ( LCDSTAT & 0x20 ) ) || |
| 1416 | 1429 | ( !gb_lcd.mode_irq && ! gb_lcd.line_irq && ! gb_lcd.delayed_line_irq && ( LCDSTAT & 0x20 ) ) ) { |
| r1512 | r1513 | |
| 1432 | 1445 | /* Set Mode 3 lcdstate */ |
| 1433 | 1446 | gb_lcd.mode = 3; |
| 1434 | 1447 | LCDSTAT = (LCDSTAT & 0xFC) | 0x03; |
| 1448 | gb_lcd.vram_locked = LOCKED; |
| 1435 | 1449 | /* Check for compensations of x-scroll register */ |
| 1436 | 1450 | /* Mode 3 lasts for approximately 172+cycles needed to handle sprites clock cycles */ |
| 1437 | 1451 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(168 + gb_lcd.scrollx_adjust + gb_lcd.sprite_cycles,0), GB_LCD_STATE_LYXX_PRE_M0, attotime_never ); |
| r1512 | r1513 | |
| 1557 | 1571 | /* Set Mode 0 lcdstate */ |
| 1558 | 1572 | gb_lcd.mode = 0; |
| 1559 | 1573 | LCDSTAT &= 0xFC; |
| 1574 | gb_lcd.oam_locked = UNLOCKED; |
| 1575 | gb_lcd.vram_locked = UNLOCKED; |
| 1560 | 1576 | /* |
| 1561 | 1577 | There seems to a kind of feature in the gameboy hardware when the lowest bits of the |
| 1562 | 1578 | SCROLLX register equals 3 or 7, then the delayed M0 irq is triggered 4 cycles later |
| r1512 | r1513 | |
| 1625 | 1641 | /* Set Mode 2 lcdstate */ |
| 1626 | 1642 | gb_lcd.mode = 2; |
| 1627 | 1643 | LCDSTAT = ( LCDSTAT & 0xFC ) | 0x02; |
| 1644 | gb_lcd.oam_locked = LOCKED; |
| 1628 | 1645 | /* Generate lcd interrupt if requested */ |
| 1629 | 1646 | if ( ( LCDSTAT & 0x20 ) && ! gb_lcd.line_irq && ! gb_lcd.line_irq ) { |
| 1630 | 1647 | cpunum_set_input_line( 0, LCD_INT, HOLD_LINE ); |
| r1512 | r1513 | |
| 1637 | 1654 | case GB_LCD_STATE_LYXX_M2: /* Switch to mode 2 */ |
| 1638 | 1655 | /* Update STAT register to the correct state */ |
| 1639 | 1656 | LCDSTAT = (LCDSTAT & 0xFC) | 0x02; |
| 1657 | gb_lcd.oam_locked = LOCKED; |
| 1640 | 1658 | /* Generate lcd interrupt if requested */ |
| 1641 | 1659 | if ( ( gb_lcd.delayed_line_irq && gb_lcd.triggering_line_irq && ! ( LCDSTAT & 0x20 ) ) || |
| 1642 | 1660 | ( !gb_lcd.mode_irq && ! gb_lcd.line_irq && ! gb_lcd.delayed_line_irq && ( LCDSTAT & 0x20 ) ) ) { |
| r1512 | r1513 | |
| 1655 | 1673 | case GB_LCD_STATE_LYXX_M3: /* Switch to mode 3 */ |
| 1656 | 1674 | gb_select_sprites(); |
| 1657 | 1675 | gb_lcd.sprite_cycles = sprite_cycles[ gb_lcd.sprCount ]; |
| 1676 | printf("sprite_cycles is %d\n", gb_lcd.sprite_cycles); |
| 1658 | 1677 | /* Set Mode 3 lcdstate */ |
| 1659 | 1678 | gb_lcd.mode = 3; |
| 1660 | 1679 | LCDSTAT = (LCDSTAT & 0xFC) | 0x03; |
| 1680 | gb_lcd.vram_locked = LOCKED; |
| 1661 | 1681 | /* Check for compensations of x-scroll register */ |
| 1662 | 1682 | /* Mode 3 lasts for approximately 172+cycles needed to handle sprites clock cycles */ |
| 1663 | 1683 | timer_adjust( gb_lcd.lcd_timer, ATTOTIME_IN_CYCLES(168 + gb_lcd.scrollx_adjust + gb_lcd.sprite_cycles,0), GB_LCD_STATE_LYXX_PRE_M0, attotime_never ); |
| r1512 | r1513 | |
| 1755 | 1775 | gb_lcd.line_irq = 0; |
| 1756 | 1776 | gb_lcd.delayed_line_irq = 0; |
| 1757 | 1777 | gb_lcd.mode = 0; |
| 1778 | gb_lcd.oam_locked = LOCKED; /* TODO: Investigate whether this OAM locking is correct. */ |
| 1758 | 1779 | /* Check for LY=LYC coincidence */ |
| 1759 | 1780 | if ( CURLINE == CMPLINE ) { |
| 1760 | 1781 | LCDSTAT |= 0x04; |
| r1512 | r1513 | |
| 1772 | 1793 | } |
| 1773 | 1794 | |
| 1774 | 1795 | READ8_HANDLER( gb_video_r ) { |
| 1775 | | // if ( 0 && activecpu_get_pc() > 0x100 ) { |
| 1776 | | // logerror("LCDSTAT/LY read, cycles left is %d\n", (int)ATTOTIME_TO_CYCLES(0,timer_timeleft( gb_lcd.lcd_timer )) ); |
| 1777 | | // } |
| 1778 | 1796 | gb_video_up_to_date(); |
| 1779 | 1797 | return gb_vid_regs[offset]; |
| 1780 | 1798 | } |
| 1781 | 1799 | |
| 1782 | | /* Returns true when LCD is on and STAT is 02 */ |
| 1783 | | INLINE int gb_video_oam_locked( void ) { |
| 1784 | | if ( ( LCDCONT & 0x80 ) && ( ( LCDSTAT & 0x03 ) == 0x02 ) ) { |
| 1785 | | return 1; |
| 1786 | | } |
| 1787 | | return 0; |
| 1788 | | } |
| 1789 | | |
| 1790 | | /* Returns true when LCD is on and STAT is 03 */ |
| 1791 | | INLINE int gb_video_vram_locked( void ) { |
| 1792 | | if ( ( LCDCONT & 0x80 ) && ( ( LCDSTAT & 0x03 ) == 0x03 ) ) { |
| 1793 | | return 1; |
| 1794 | | } |
| 1795 | | return 0; |
| 1796 | | } |
| 1797 | | |
| 1798 | 1800 | READ8_HANDLER( gb_vram_r ) { |
| 1799 | 1801 | gb_video_up_to_date(); |
| 1800 | | return gb_video_vram_locked() ? 0xFF : gb_vram_ptr[offset]; |
| 1802 | return ( gb_lcd.vram_locked == LOCKED ) ? 0xFF : gb_vram_ptr[offset]; |
| 1801 | 1803 | } |
| 1802 | 1804 | |
| 1803 | 1805 | WRITE8_HANDLER( gb_vram_w ) { |
| 1804 | 1806 | gb_video_up_to_date(); |
| 1805 | | if ( gb_video_vram_locked() ) { |
| 1807 | if ( gb_lcd.vram_locked == LOCKED ) { |
| 1806 | 1808 | return; |
| 1807 | 1809 | } |
| 1808 | 1810 | gb_vram_ptr[offset] = data; |
| r1512 | r1513 | |
| 1810 | 1812 | |
| 1811 | 1813 | READ8_HANDLER( gb_oam_r ) { |
| 1812 | 1814 | gb_video_up_to_date(); |
| 1813 | | return ( gb_video_oam_locked() || gb_video_vram_locked() ) ? 0xFF : gb_oam[offset]; |
| 1815 | return ( gb_lcd.oam_locked == LOCKED ) ? 0xFF : gb_oam[offset]; |
| 1814 | 1816 | } |
| 1815 | 1817 | |
| 1816 | 1818 | WRITE8_HANDLER( gb_oam_w ) { |
| 1817 | 1819 | gb_video_up_to_date(); |
| 1818 | | if ( gb_video_oam_locked() || gb_video_vram_locked() || offset >= 0xa0 ) { |
| 1820 | if ( gb_lcd.oam_locked == LOCKED || offset >= 0xa0 ) { |
| 1819 | 1821 | return; |
| 1820 | 1822 | } |
| 1821 | 1823 | gb_oam[offset] = data; |
| r1512 | r1513 | |
| 1833 | 1835 | if ( ! ( data & 0x80 ) ) { |
| 1834 | 1836 | LCDSTAT &= ~0x03; |
| 1835 | 1837 | CURLINE = 0; |
| 1838 | gb_lcd.oam_locked = UNLOCKED; |
| 1839 | gb_lcd.vram_locked = UNLOCKED; |
| 1836 | 1840 | } |
| 1837 | 1841 | /* If LCD is being switched on */ |
| 1838 | 1842 | if ( !( LCDCONT & 0x80 ) && ( data & 0x80 ) ) { |
| r1512 | r1513 | |
| 1955 | 1959 | if ( ! ( data & 0x80 ) ) { |
| 1956 | 1960 | LCDSTAT &= ~0x03; |
| 1957 | 1961 | CURLINE = 0; |
| 1962 | gb_lcd.oam_locked = UNLOCKED; |
| 1963 | gb_lcd.vram_locked = UNLOCKED; |
| 1958 | 1964 | } |
| 1959 | 1965 | /* If LCD is being switched on */ |
| 1960 | 1966 | if ( !( LCDCONT & 0x80 ) && ( data & 0x80 ) ) { |