src/mess/machine/gb.c
| r1411 | r1412 | |
| 106 | 106 | UINT8 gbTama5Address; |
| 107 | 107 | UINT8 gbLastTama5Command; |
| 108 | 108 | /* Timer related globals */ |
| 109 | | UINT16 gb_divcount; |
| 110 | | UINT8 gb_timer_shift; |
| 109 | static struct gb_timer_struct { |
| 110 | UINT16 divcount; |
| 111 | UINT8 shift; |
| 112 | UINT16 shift_cycles; |
| 113 | UINT8 triggering_irq; |
| 114 | UINT8 reloading; |
| 115 | } gb_timer; |
| 111 | 116 | /* Serial I/O related */ |
| 112 | 117 | static UINT32 SIOCount; /* Serial I/O counter */ |
| 113 | 118 | emu_timer *gb_serial_timer = NULL; |
| r1411 | r1412 | |
| 149 | 154 | |
| 150 | 155 | gb_io_w( 0x05, 0x00 ); /* TIMECNT */ |
| 151 | 156 | gb_io_w( 0x06, 0x00 ); /* TIMEMOD */ |
| 152 | | gb_io_w( 0x07, 0x00 ); /* TIMEFRQ */ |
| 153 | 157 | gb_video_w( 0x0, 0x91 ); /* LCDCONT */ |
| 154 | 158 | gb_video_w( 0x7, 0xFC ); /* BGRDPAL */ |
| 155 | 159 | gb_video_w( 0x8, 0xFC ); /* SPR0PAL */ |
| r1411 | r1412 | |
| 235 | 239 | gb_serial_timer = timer_alloc( gb_serial_timer_proc , NULL); |
| 236 | 240 | timer_enable( gb_serial_timer, 0 ); |
| 237 | 241 | |
| 238 | | gb_divcount = 0; |
| 242 | gb_timer.divcount = 0; |
| 243 | gb_timer.triggering_irq = 0; |
| 244 | gb_io[0x07] = 0xF8; /* Upper bits of TIMEFRQ register are set to 1 */ |
| 239 | 245 | } |
| 240 | 246 | |
| 241 | 247 | MACHINE_START( gb ) |
| r1411 | r1412 | |
| 694 | 700 | break; |
| 695 | 701 | case 0x04: /* DIV - Divider register */ |
| 696 | 702 | /* Force increment of TIMECNT register */ |
| 697 | | if ( gb_divcount >= 16 ) |
| 703 | if ( gb_timer.divcount >= 16 ) |
| 698 | 704 | gb_timer_increment(); |
| 699 | | gb_divcount = 0; |
| 705 | gb_timer.divcount = 0; |
| 700 | 706 | return; |
| 701 | 707 | case 0x05: /* TIMA - Timer counter */ |
| 708 | /* Check if the counter is being reloaded in this cycle */ |
| 709 | if ( gb_timer.reloading && ( gb_timer.divcount & ( gb_timer.shift_cycles - 1 ) ) == 4 ) { |
| 710 | data = TIMECNT; |
| 711 | } |
| 702 | 712 | break; |
| 703 | 713 | case 0x06: /* TMA - Timer module */ |
| 714 | /* Check if the counter is being reloaded in this cycle */ |
| 715 | if ( gb_timer.reloading && ( gb_timer.divcount & ( gb_timer.shift_cycles - 1 ) ) == 4 ) { |
| 716 | TIMECNT = data; |
| 717 | } |
| 704 | 718 | break; |
| 705 | 719 | case 0x07: /* TAC - Timer control */ |
| 706 | 720 | data |= 0xF8; |
| 707 | | gb_timer_shift = timer_shifts[data & 0x03]; |
| 708 | | /* Check if timer is just enabled */ |
| 709 | | if ( ( data & 0x04 ) && ! ( TIMEFRQ & 0x04 ) ) { |
| 710 | | TIMECNT = TIMEMOD; |
| 721 | /* Check if timer is just disabled or the timer frequency is changing */ |
| 722 | if ( ( ! ( data & 0x04 ) && ( TIMEFRQ & 0x04 ) ) || ( ( data & 0x04 ) && ( TIMEFRQ & 0x04 ) && ( data & 0x03 ) != ( TIMEFRQ & 0x03 ) ) ) { |
| 723 | /* Check if TIMECNT should be incremented */ |
| 724 | if ( ( gb_timer.divcount & ( gb_timer.shift_cycles - 1 ) ) >= ( gb_timer.shift_cycles >> 1 ) ) { |
| 725 | gb_timer_increment(); |
| 726 | } |
| 711 | 727 | } |
| 728 | gb_timer.shift = timer_shifts[data & 0x03]; |
| 729 | gb_timer.shift_cycles = 1 << gb_timer.shift; |
| 712 | 730 | break; |
| 713 | 731 | case 0x0F: /* IF - Interrupt flag */ |
| 714 | 732 | data &= 0x1F; |
| r1411 | r1412 | |
| 716 | 734 | break; |
| 717 | 735 | } |
| 718 | 736 | |
| 719 | | gb_io [offset] = data; |
| 737 | gb_io[offset] = data; |
| 720 | 738 | } |
| 721 | 739 | |
| 722 | 740 | WRITE8_HANDLER ( gb_io2_w ) |
| r1411 | r1412 | |
| 1269 | 1287 | switch(offset) |
| 1270 | 1288 | { |
| 1271 | 1289 | case 0x04: |
| 1272 | | return ( gb_divcount >> 8 ) & 0xFF; |
| 1290 | return ( gb_timer.divcount >> 8 ) & 0xFF; |
| 1273 | 1291 | case 0x00: |
| 1274 | 1292 | case 0x01: |
| 1275 | 1293 | case 0x02: |
| r1411 | r1412 | |
| 1747 | 1765 | } |
| 1748 | 1766 | } |
| 1749 | 1767 | |
| 1768 | INLINE void gb_timer_check_irq( void ) { |
| 1769 | gb_timer.reloading = 0; |
| 1770 | if ( gb_timer.triggering_irq ) { |
| 1771 | gb_timer.triggering_irq = 0; |
| 1772 | if ( TIMECNT == 0 ) { |
| 1773 | TIMECNT = TIMEMOD; |
| 1774 | cpunum_set_input_line( 0, TIM_INT, HOLD_LINE ); |
| 1775 | gb_timer.reloading = 1; |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1750 | 1780 | static void gb_timer_increment( void ) { |
| 1781 | gb_timer_check_irq(); |
| 1782 | |
| 1751 | 1783 | TIMECNT += 1; |
| 1752 | 1784 | if ( TIMECNT == 0 ) { |
| 1753 | | TIMECNT = TIMEMOD; |
| 1754 | | cpunum_set_input_line( 0, TIM_INT, HOLD_LINE ); |
| 1785 | gb_timer.triggering_irq = 1; |
| 1755 | 1786 | } |
| 1756 | 1787 | } |
| 1757 | 1788 | |
| 1758 | 1789 | void gb_timer_callback(int cycles) { |
| 1759 | | UINT16 old_gb_divcount = gb_divcount; |
| 1760 | | gb_divcount += cycles; |
| 1790 | UINT16 old_gb_divcount = gb_timer.divcount; |
| 1791 | gb_timer.divcount += cycles; |
| 1792 | |
| 1793 | gb_timer_check_irq(); |
| 1794 | |
| 1761 | 1795 | if ( TIMEFRQ & 0x04 ) { |
| 1762 | | UINT16 old_count = old_gb_divcount >> gb_timer_shift; |
| 1763 | | UINT16 new_count = gb_divcount >> gb_timer_shift; |
| 1764 | | if ( cycles > ( 1 << gb_timer_shift ) ) { |
| 1796 | UINT16 old_count = old_gb_divcount >> gb_timer.shift; |
| 1797 | UINT16 new_count = gb_timer.divcount >> gb_timer.shift; |
| 1798 | if ( cycles > gb_timer.shift_cycles ) { |
| 1765 | 1799 | gb_timer_increment(); |
| 1766 | | old_gb_divcount += ( 1 << gb_timer_shift ); |
| 1767 | 1800 | old_count++; |
| 1768 | 1801 | } |
| 1769 | 1802 | if ( new_count != old_count ) { |
| 1770 | 1803 | gb_timer_increment(); |
| 1771 | 1804 | } |
| 1805 | if ( new_count << gb_timer.shift < gb_timer.divcount ) { |
| 1806 | gb_timer_check_irq(); |
| 1807 | } |
| 1772 | 1808 | } |
| 1773 | 1809 | } |
| 1774 | 1810 | |