Previous 15994 Revisions Next

r1412 Sunday 16th December, 2007 at 21:04:15 UTC by Wilbert Pol
[GAMEBOY] Fixed timer handling details and timer irq generation.
[docs]messnew.txt
[src/mess/machine]gb.c

src/mess/machine/gb.c
r1411r1412
106106UINT8 gbTama5Address;
107107UINT8 gbLastTama5Command;
108108/* Timer related globals */
109UINT16   gb_divcount;
110UINT8 gb_timer_shift;
109static struct gb_timer_struct {
110   UINT16   divcount;
111   UINT8   shift;
112   UINT16   shift_cycles;
113   UINT8   triggering_irq;
114   UINT8   reloading;
115} gb_timer;
111116/* Serial I/O related */
112117static UINT32 SIOCount;         /* Serial I/O counter                          */
113118emu_timer   *gb_serial_timer = NULL;
r1411r1412
149154
150155   gb_io_w( 0x05, 0x00 );      /* TIMECNT */
151156   gb_io_w( 0x06, 0x00 );      /* TIMEMOD */
152   gb_io_w( 0x07, 0x00 );      /* TIMEFRQ */
153157   gb_video_w( 0x0, 0x91 );   /* LCDCONT */
154158   gb_video_w( 0x7, 0xFC );   /* BGRDPAL */
155159   gb_video_w( 0x8, 0xFC );   /* SPR0PAL */
r1411r1412
235239   gb_serial_timer = timer_alloc( gb_serial_timer_proc , NULL);
236240   timer_enable( gb_serial_timer, 0 );
237241
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 */
239245}
240246
241247MACHINE_START( gb )
r1411r1412
694700      break;
695701   case 0x04:                  /* DIV - Divider register */
696702      /* Force increment of TIMECNT register */
697      if ( gb_divcount >= 16 )
703      if ( gb_timer.divcount >= 16 )
698704         gb_timer_increment();
699      gb_divcount = 0;
705      gb_timer.divcount = 0;
700706      return;
701707   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      }
702712      break;
703713   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      }
704718      break;
705719   case 0x07:                  /* TAC - Timer control */
706720      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         }
711727      }
728      gb_timer.shift = timer_shifts[data & 0x03];
729      gb_timer.shift_cycles = 1 << gb_timer.shift;
712730      break;
713731   case 0x0F:                  /* IF - Interrupt flag */
714732      data &= 0x1F;
r1411r1412
716734      break;
717735   }
718736
719   gb_io [offset] = data;
737   gb_io[offset] = data;
720738}
721739
722740WRITE8_HANDLER ( gb_io2_w )
r1411r1412
12691287   switch(offset)
12701288   {
12711289      case 0x04:
1272         return ( gb_divcount >> 8 ) & 0xFF;
1290         return ( gb_timer.divcount >> 8 ) & 0xFF;
12731291      case 0x00:
12741292      case 0x01:
12751293      case 0x02:
r1411r1412
17471765   }
17481766}
17491767
1768INLINE 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
17501780static void gb_timer_increment( void ) {
1781   gb_timer_check_irq();
1782
17511783   TIMECNT += 1;
17521784   if ( TIMECNT == 0 ) {
1753      TIMECNT = TIMEMOD;
1754      cpunum_set_input_line( 0, TIM_INT, HOLD_LINE );
1785      gb_timer.triggering_irq = 1;
17551786   }
17561787}
17571788
17581789void 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
17611795   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 ) {
17651799         gb_timer_increment();
1766         old_gb_divcount += ( 1 << gb_timer_shift );
17671800         old_count++;
17681801      }
17691802      if ( new_count != old_count ) {
17701803         gb_timer_increment();
17711804      }
1805      if ( new_count << gb_timer.shift < gb_timer.divcount ) {
1806         gb_timer_check_irq();
1807      }
17721808   }
17731809}
17741810
docs/messnew.txt
r1411r1412
1616
1717- [GAMEBOY] Fixed a bug in the handling of 256 and 1024 cycle timers.  [Wilbert Pol]
1818
19- [GAMEBOY] Fixed timer handling details and timer irq generation.  [Wilbert Pol]
20
1921- [QL] Added sound. [Curt Coder]
2022
2123- [SG1000] Fixed keyboard/joystick detection. (bug #1444) [Curt Coder]

Previous 15994 Revisions Next


© 1998-2018 The MESS Team