src/mess/machine/gb.c
| r1089 | r1090 | |
| 136 | 136 | WRITE8_HANDLER( gb_rom_bank_mmm01_2000_w ); |
| 137 | 137 | WRITE8_HANDLER( gb_rom_bank_mmm01_4000_w ); |
| 138 | 138 | WRITE8_HANDLER( gb_rom_bank_mmm01_6000_w ); |
| 139 | static void gb_timer_increment( void ); |
| 139 | 140 | |
| 140 | 141 | #ifdef MAME_DEBUG |
| 141 | 142 | /* #define V_GENERAL*/ /* Display general debug information */ |
| r1089 | r1090 | |
| 646 | 647 | |
| 647 | 648 | WRITE8_HANDLER ( gb_io_w ) |
| 648 | 649 | { |
| 649 | | static UINT8 timer_shifts[4] = {10, 4, 6, 8}; |
| 650 | static const UINT8 timer_shifts[4] = {10, 4, 6, 8}; |
| 650 | 651 | |
| 651 | 652 | switch (offset) |
| 652 | 653 | { |
| r1089 | r1090 | |
| 677 | 678 | case 0x04: /* DIV - Divider register */ |
| 678 | 679 | /* Force increment of TIMECNT register */ |
| 679 | 680 | if ( gb_divcount >= 16 ) |
| 680 | | gb_timer_callback( 1024 ); |
| 681 | gb_timer_increment(); |
| 681 | 682 | gb_divcount = 0; |
| 682 | 683 | return; |
| 683 | 684 | case 0x05: /* TIMA - Timer counter */ |
| r1089 | r1090 | |
| 687 | 688 | case 0x07: /* TAC - Timer control */ |
| 688 | 689 | data |= 0xF8; |
| 689 | 690 | gb_timer_shift = timer_shifts[data & 0x03]; |
| 691 | gb_timer_count = 1 << gb_timer_shift; |
| 690 | 692 | /* Check if timer is just enabled */ |
| 691 | 693 | if ( ( data & 0x04 ) && ! ( TIMEFRQ & 0x04 ) ) { |
| 692 | 694 | TIMECNT = TIMEMOD; |
| r1089 | r1090 | |
| 1729 | 1731 | } |
| 1730 | 1732 | } |
| 1731 | 1733 | |
| 1734 | static void gb_timer_increment( void ) { |
| 1735 | TIMECNT += 1; |
| 1736 | if ( TIMECNT == 0 ) { |
| 1737 | TIMECNT = TIMEMOD; |
| 1738 | cpunum_set_input_line( 0, TIM_INT, HOLD_LINE ); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1732 | 1742 | void gb_timer_callback(int cycles) { |
| 1733 | 1743 | UINT16 old_gb_divcount = gb_divcount; |
| 1734 | 1744 | gb_divcount += cycles; |
| 1735 | 1745 | if ( TIMEFRQ & 0x04 ) { |
| 1736 | 1746 | UINT16 old_count = old_gb_divcount >> gb_timer_shift; |
| 1737 | 1747 | UINT16 new_count = gb_divcount >> gb_timer_shift; |
| 1748 | if ( cycles > gb_timer_count ) { |
| 1749 | gb_timer_increment(); |
| 1750 | old_gb_divcount += gb_timer_count; |
| 1751 | old_count++; |
| 1752 | } |
| 1738 | 1753 | if ( new_count != old_count ) { |
| 1739 | | /* this increment is probably not correct when cycles > 1 timer cycle */ |
| 1740 | | TIMECNT += 1; |
| 1741 | | if ( TIMECNT == 0x00 ) { |
| 1742 | | TIMECNT = TIMEMOD; |
| 1743 | | cpunum_set_input_line( 0, TIM_INT, HOLD_LINE ); |
| 1744 | | } |
| 1754 | gb_timer_increment(); |
| 1745 | 1755 | } |
| 1746 | 1756 | } |
| 1747 | 1757 | } |