This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

[PATCH]: Fix 68hc11 timer device (accuracy, io, timer overflow)



Hi!

I have commited the following patch to fix accuracy and problems in
the 68hc11 timer device:

  - There was a time shift of all timer interrupts compared to the cpu
    absolute cycle.  It was due to the simulator pending ticks that may
    not yet be processed when the timer event is raised (causing the next
    event to shift by a few cycles).
  - The io read/write must support reading/writing of several bytes
  - The timer overflow interrupt is now raised correctly.

	Stephane


2000-09-06  Stephane Carrez  <Stephane.Carrez@worldnet.fr>

	* dv-m68hc11tim.c (m68hc11tim_timer_event): Compute the overflow
	interrupt and compare events accurately.  Take into account the
	pending ticks not processed by the simulator yet (introduced a shift).
	(m68hc11_port_event): Reset the timer interrupt delays.
	(m68hc11tim_io_read_buffer): Be able to read several bytes.
	(m68hc11tim_io_write_buffer): Likewise for write.
	(m68hc11tim_io_write_buffer): Recompute the timer overflow interrupt.
Index: dv-m68hc11tim.c
===================================================================
RCS file: /cvs/src/src/sim/m68hc11/dv-m68hc11tim.c,v
retrieving revision 1.2
diff -p -r1.2 dv-m68hc11tim.c
*** dv-m68hc11tim.c	2000/08/11 18:44:59	1.2
--- dv-m68hc11tim.c	2000/09/06 19:30:23
*************** struct m68hc11tim 
*** 77,82 ****
--- 77,84 ----
    unsigned long ovf_delay;
    signed64      clock_prescaler;
    signed64      tcnt_adjust;
+   signed64      cop_prev_interrupt;
+   signed64      rti_prev_interrupt;
  
    /* Periodic timers.  */
    struct hw_event *rti_timer_event;
*************** m68hc11tim_port_event (struct hw *me,
*** 165,175 ****
--- 167,179 ----
            {
              hw_event_queue_deschedule (me, controller->rti_timer_event);
              controller->rti_timer_event = 0;
+             controller->rti_prev_interrupt = 0;
            }
          if (controller->cop_timer_event)
            {
              hw_event_queue_deschedule (me, controller->cop_timer_event);
              controller->cop_timer_event = 0;
+             controller->cop_prev_interrupt = 0;
            }
          if (controller->tof_timer_event)
            {
*************** m68hc11tim_timer_event (struct hw *me, v
*** 220,232 ****
--- 224,239 ----
    int check_interrupt = 0;
    unsigned mask;
    unsigned flags;
+   unsigned long tcnt_internal;
    unsigned long tcnt;
    int i;
+   sim_events *events;
    
    controller = hw_data (me);
    sd         = hw_system (me);
    cpu        = STATE_CPU (sd, 0);
    type       = (enum event_type) ((long) data) & 0x0FF;
+   events     = STATE_EVENTS (sd);
  
    delay = 0;
    switch (type)
*************** m68hc11tim_timer_event (struct hw *me, v
*** 234,276 ****
      case COP_EVENT:
        eventp = &controller->cop_timer_event;
        delay  = controller->cop_delay;
        check_interrupt = 1;
        break;
  
      case RTI_EVENT:
        eventp = &controller->rti_timer_event;
!       delay  = controller->rti_delay;
        if (((long) (data) & 0x0100) == 0)
          {
            cpu->ios[M6811_TFLG2] |= M6811_RTIF;
            check_interrupt = 1;
          }
        break;
  
      case OVERFLOW_EVENT:
        eventp = &controller->tof_timer_event;
!       delay  = controller->ovf_delay;
!       cpu->ios[M6811_TFLG2] |= M6811_TOF;
        break;
  
      case COMPARE_EVENT:
        eventp = &controller->cmp_timer_event;
  
!       /* Get current free running counter.  */
!       tcnt = ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!               / controller->clock_prescaler);
!       tcnt &= 0x0ffffL;
        
        flags = cpu->ios[M6811_TMSK1];
        mask  = 0x80;
!       delay = 65536;
  
        /* Scan each output compare register to see if one matches
           the free running counter.  Set the corresponding OCi flag
           if the output compare is enabled.  */
        for (i = M6811_TOC1; i <= M6811_TOC5; i += 2, mask >>= 1)
          {
!           unsigned short compare;
            
            compare = (cpu->ios[i] << 8) + cpu->ios[i+1];
            if (compare == tcnt && (flags & mask))
--- 241,322 ----
      case COP_EVENT:
        eventp = &controller->cop_timer_event;
        delay  = controller->cop_delay;
+       delay  = controller->cop_prev_interrupt + controller->cop_delay;
+       controller->cop_prev_interrupt = delay;
+       delay  = delay - cpu->cpu_absolute_cycle;
        check_interrupt = 1;
+       delay += events->nr_ticks_to_process;
        break;
  
      case RTI_EVENT:
        eventp = &controller->rti_timer_event;
!       delay  = controller->rti_prev_interrupt + controller->rti_delay;
!       
        if (((long) (data) & 0x0100) == 0)
          {
            cpu->ios[M6811_TFLG2] |= M6811_RTIF;
            check_interrupt = 1;
+           controller->rti_prev_interrupt = delay;
+           delay += controller->rti_delay;
          }
+       delay = delay - cpu->cpu_absolute_cycle;
+       delay += events->nr_ticks_to_process;
        break;
  
      case OVERFLOW_EVENT:
+       /* Compute the 68HC11 internal free running counter.
+          There may be 'nr_ticks_to_process' pending cycles that are
+          not (yet) taken into account by 'sim_events_time'.  */
+       tcnt_internal = sim_events_time (sd) - controller->tcnt_adjust;
+       tcnt_internal += events->nr_ticks_to_process;
+ 
+       /* We must take into account the prescaler that comes
+          before the counter (it's a power of 2).  */
+       tcnt_internal &= 0x0ffff * controller->clock_prescaler;
+ 
+       /* Compute the time when the overflow will occur.  It occurs when
+          the counter increments from 0x0ffff to 0x10000 (and thus resets).  */
+       delay = (0x10000 * controller->clock_prescaler) - tcnt_internal;
+ 
+       /* The 'nr_ticks_to_process' will be subtracted when the event
+          is scheduled.  */
+       delay += events->nr_ticks_to_process;
+ 
        eventp = &controller->tof_timer_event;
!       if (((long) (data) & 0x100) == 0)
!         {
!           cpu->ios[M6811_TFLG2] |= M6811_TOF;
!           check_interrupt = 1;
!         }
        break;
  
      case COMPARE_EVENT:
        eventp = &controller->cmp_timer_event;
+ 
+       /* Compute the 68HC11 internal free running counter.
+          There may be 'nr_ticks_to_process' pending cycles that are
+          not (yet) taken into account by 'sim_events_time'.  */
+       events = STATE_EVENTS (sd);
+       tcnt_internal = sim_events_time (sd) - controller->tcnt_adjust;
+       tcnt_internal += events->nr_ticks_to_process;
+ 
+       /* We must take into account the prescaler that comes
+          before the counter (it's a power of 2).  */
+       tcnt_internal &= 0x0ffff * controller->clock_prescaler;
  
!       /* Get current visible TCNT register value.  */
!       tcnt = tcnt_internal / controller->clock_prescaler;
        
        flags = cpu->ios[M6811_TMSK1];
        mask  = 0x80;
!       delay = 65536 * controller->clock_prescaler;
  
        /* Scan each output compare register to see if one matches
           the free running counter.  Set the corresponding OCi flag
           if the output compare is enabled.  */
        for (i = M6811_TOC1; i <= M6811_TOC5; i += 2, mask >>= 1)
          {
!           unsigned long compare;
            
            compare = (cpu->ios[i] << 8) + cpu->ios[i+1];
            if (compare == tcnt && (flags & mask))
*************** m68hc11tim_timer_event (struct hw *me, v
*** 279,294 ****
                check_interrupt++;
              }
  
!           /* Compute how many times for the next match.  */
!           if (compare > tcnt)
!             compare = compare - tcnt;
            else
!             compare = compare - tcnt + 65536;
            
            if (compare < delay)
              delay = compare;
          }
-       delay = delay * controller->clock_prescaler;
  
        /* Deactivate the compare timer if no output compare is enabled.  */
        if ((flags & 0xF0) == 0)
--- 325,343 ----
                check_interrupt++;
              }
  
!           /* Compute how many times for the next match.
!              Use the internal counter value to take into account the
!              prescaler accurately.  */
!           compare = compare * controller->clock_prescaler;
!           if (compare > tcnt_internal)
!             compare = compare - tcnt_internal;
            else
!             compare = compare - tcnt_internal
!               + 65536 * controller->clock_prescaler;
            
            if (compare < delay)
              delay = compare;
          }
  
        /* Deactivate the compare timer if no output compare is enabled.  */
        if ((flags & 0xF0) == 0)
*************** m68hc11tim_io_read_buffer (struct hw *me
*** 442,447 ****
--- 491,497 ----
    struct m68hc11tim *controller;
    sim_cpu *cpu;
    unsigned8 val;
+   unsigned cnt = 0;
    
    HW_TRACE ((me, "read 0x%08lx %d", (long) base, (int) nr_bytes));
  
*************** m68hc11tim_io_read_buffer (struct hw *me
*** 449,475 ****
    cpu = STATE_CPU (sd, 0);
    controller = hw_data (me);
  
!   switch (base)
      {
!       /* The cpu_absolute_cycle is updated after each instruction.
!          Reading in a 16-bit register will be split in two accesses
!          but this will be atomic within the simulator.  */
!     case M6811_TCTN_H:
!       val = (uint8) ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!                      / (controller->clock_prescaler * 256));
!       break;
  
!     case M6811_TCTN_L:
!       val = (uint8) ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!                      / controller->clock_prescaler);
!       break;
  
!     default:
!       val = cpu->ios[base];
!       break;
      }
!   *((unsigned8*) dest) = val;
!   return 1;
  }
  
  static unsigned
--- 499,532 ----
    cpu = STATE_CPU (sd, 0);
    controller = hw_data (me);
  
!   while (nr_bytes)
      {
!       switch (base)
!         {
!           /* The cpu_absolute_cycle is updated after each instruction.
!              Reading in a 16-bit register will be split in two accesses
!              but this will be atomic within the simulator.  */
!         case M6811_TCTN_H:
!           val = (uint8) ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!                          / (controller->clock_prescaler * 256));
!           break;
  
!         case M6811_TCTN_L:
!           val = (uint8) ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!                          / controller->clock_prescaler);
!           break;
  
!         default:
!           val = cpu->ios[base];
!           break;
!         }
!       *((unsigned8*) dest) = val;
!       dest++;
!       base++;
!       nr_bytes--;
!       cnt++;
      }
!   return cnt;
  }
  
  static unsigned
*************** m68hc11tim_io_write_buffer (struct hw *m
*** 485,601 ****
    unsigned8 val, n;
    signed64 adj;
    int reset_compare = 0;
    
    HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  
    sd  = hw_system (me);
    cpu = STATE_CPU (sd, 0);
    controller = hw_data (me);
!   
!   val = *((const unsigned8*) source);
!   switch (base)
      {
!       /* Set the timer counter low part, trying to preserve the low part.
!          We compute the absolute cycle adjustment that we have to apply
!          to obtain the timer current value.  Computation must be made
!          in 64-bit to avoid overflow problems.  */
!     case M6811_TCTN_L:
!       adj = ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!              / (controller->clock_prescaler * (signed64) 256)) & 0x0FF;
!       adj = cpu->cpu_absolute_cycle
!         - (adj * controller->clock_prescaler * (signed64) 256)
!         - ((signed64) adj * controller->clock_prescaler);
!       controller->tcnt_adjust = adj;
!       reset_compare = 1;
!       break;
  
!     case M6811_TCTN_H:
!       adj = ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!              / controller->clock_prescaler) & 0x0ff;
!       adj = cpu->cpu_absolute_cycle
!         - ((signed64) val * controller->clock_prescaler * (signed64) 256)
!         - (adj * controller->clock_prescaler);
!       controller->tcnt_adjust = adj;
!       reset_compare = 1;
!       break;
  
!     case M6811_TMSK2:
  
        /* Timer prescaler cannot be changed after 64 bus cycles.  */
!       if (cpu->cpu_absolute_cycle >= 64)
!         {
!           val &= ~(M6811_PR1 | M6811_PR0);
!           val |= cpu->ios[M6811_TMSK2] & (M6811_PR1 | M6811_PR0);
!         }
!       switch (val & (M6811_PR1 | M6811_PR0))
!         {
!         case 0:
!           n = 1;
!           break;
!         case M6811_PR0:
!           n = 4;
!           break;
!         case M6811_PR1:
!           n = 8;
!           break;
!         default:
!         case M6811_PR1 | M6811_PR0:
!           n = 16;
            break;
-         }
-       if (controller->clock_prescaler != n)
-         {
-           controller->clock_prescaler = n;
-           controller->ovf_delay = n * 65536;
-           m68hc11tim_timer_event (me, (void*) (OVERFLOW_EVENT| 0x100));
-         }
-       cpu->ios[base] = val;
-       interrupts_update_pending (&cpu->cpu_interrupts);
-       break;
  
!     case M6811_PACTL:
!       n = (1 << ((val & (M6811_RTR1 | M6811_RTR0))));
!       cpu->ios[base] = val;
  
!       controller->rti_delay = (long) (n) * 8192;
!       m68hc11tim_timer_event (me, (void*) (RTI_EVENT| 0x100));
!       break;
        
!     case M6811_TFLG2:
!       if (val & M6811_TOF)
!         val &= ~M6811_TOF;
!       else
!         val |= cpu->ios[M6811_TFLG2] & M6811_TOF;
  
        /* Clear the Real Time interrupt flag. */
!       if (val & M6811_RTIF)
!         val &= ~M6811_RTIF;
!       else
!         val |= cpu->ios[M6811_TFLG2] & M6811_RTIF;
        
!       cpu->ios[base] = val;
!       interrupts_update_pending (&cpu->cpu_interrupts);
!       break;
  
!     case M6811_TOC1:
!     case M6811_TOC2:
!     case M6811_TOC3:
!     case M6811_TOC4:
!     case M6811_TOC5:
!       cpu->ios[base] = val;
!       reset_compare = 1;
!       break;
        
!     default:
!       return 0;
      }
  
    /* Re-compute the next timer compare event.  */
    if (reset_compare)
      {
        m68hc11tim_timer_event (me, (void*) (COMPARE_EVENT));
      }
!   return nr_bytes;
  }     
  
  
--- 542,673 ----
    unsigned8 val, n;
    signed64 adj;
    int reset_compare = 0;
+   int reset_overflow = 0;
+   int cnt = 0;
    
    HW_TRACE ((me, "write 0x%08lx %d", (long) base, (int) nr_bytes));
  
    sd  = hw_system (me);
    cpu = STATE_CPU (sd, 0);
    controller = hw_data (me);
! 
!   while (nr_bytes)
      {
!       val = *((const unsigned8*) source);
!       switch (base)
!         {
!           /* Set the timer counter low part, trying to preserve the low part.
!              We compute the absolute cycle adjustment that we have to apply
!              to obtain the timer current value.  Computation must be made
!              in 64-bit to avoid overflow problems.  */
!         case M6811_TCTN_L:
!           adj = ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!                  / (controller->clock_prescaler * (signed64) 256)) & 0x0FF;
!           adj = cpu->cpu_absolute_cycle
!             - (adj * controller->clock_prescaler * (signed64) 256)
!             - ((signed64) adj * controller->clock_prescaler);
!           controller->tcnt_adjust = adj;
!           reset_compare = 1;
!           reset_overflow = 1;
!           break;
  
!         case M6811_TCTN_H:
!           adj = ((cpu->cpu_absolute_cycle - controller->tcnt_adjust)
!                  / controller->clock_prescaler) & 0x0ff;
!           adj = cpu->cpu_absolute_cycle
!             - ((signed64) val * controller->clock_prescaler * (signed64) 256)
!             - (adj * controller->clock_prescaler);
!           controller->tcnt_adjust = adj;
!           reset_compare = 1;
!           reset_overflow = 1;
!           break;
  
!         case M6811_TMSK2:
  
        /* Timer prescaler cannot be changed after 64 bus cycles.  */
!           if (cpu->cpu_absolute_cycle >= 64)
!             {
!               val &= ~(M6811_PR1 | M6811_PR0);
!               val |= cpu->ios[M6811_TMSK2] & (M6811_PR1 | M6811_PR0);
!             }
!           switch (val & (M6811_PR1 | M6811_PR0))
!             {
!             case 0:
!               n = 1;
!               break;
!             case M6811_PR0:
!               n = 4;
!               break;
!             case M6811_PR1:
!               n = 8;
!               break;
!             default:
!             case M6811_PR1 | M6811_PR0:
!               n = 16;
!               break;
!             }
!           if (cpu->cpu_absolute_cycle < 64)
!             {
!               reset_overflow = 1;
!               controller->clock_prescaler = n;
!             }
!           cpu->ios[base] = val;
!           interrupts_update_pending (&cpu->cpu_interrupts);
            break;
  
!         case M6811_PACTL:
!           n = (1 << ((val & (M6811_RTR1 | M6811_RTR0))));
!           cpu->ios[base] = val;
  
!           controller->rti_delay = (long) (n) * 8192;
!           m68hc11tim_timer_event (me, (void*) (RTI_EVENT| 0x100));
!           break;
        
!         case M6811_TFLG2:
!           if (val & M6811_TOF)
!             val &= ~M6811_TOF;
!           else
!             val |= cpu->ios[M6811_TFLG2] & M6811_TOF;
  
        /* Clear the Real Time interrupt flag. */
!           if (val & M6811_RTIF)
!             val &= ~M6811_RTIF;
!           else
!             val |= cpu->ios[M6811_TFLG2] & M6811_RTIF;
        
!           cpu->ios[base] = val;
!           interrupts_update_pending (&cpu->cpu_interrupts);
!           break;
  
!         case M6811_TOC1:
!         case M6811_TOC2:
!         case M6811_TOC3:
!         case M6811_TOC4:
!         case M6811_TOC5:
!           cpu->ios[base] = val;
!           reset_compare = 1;
!           break;
        
!         default:
!           break;
!         }
! 
!       base++;
!       nr_bytes--;
!       cnt++;
!       source++;
      }
  
    /* Re-compute the next timer compare event.  */
    if (reset_compare)
      {
        m68hc11tim_timer_event (me, (void*) (COMPARE_EVENT));
+     }
+   if (reset_overflow)
+     {
+       m68hc11tim_timer_event (me, (void*) (OVERFLOW_EVENT| 0x100));
      }
!   return cnt;
  }     
  
  

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]