Timer 2 / TOSC1 question

I have an Arduino Mega 2560. I’ve been told that this board has a 32,768KHz watch crystal connected to TOSC1. Is this correct?

There’s also an internal, but less accurate, crystal within the AVR chip that also should run at 32,768 KHz, and can be selected by setting AS2 and clearing EXCLK. Is this right?

The following code seems to increment time.ticks at a rate of one per second. There’s no prescaler set, so that would indicate the internal crystal actually runs at 256 Hz. What’s going on?

Furthermore, how does the Arduino IDE implement millis() exactly?

#include <string.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/io.h>

struct timer {
  volatile uint32_t sec;
  volatile uint8_t ticks; } time;

ISR(TIMER2_OVF_vect) {
  time.ticks++;
  if(time.ticks >= 128) {
    time.ticks = 0;
    time.sec++; } }

void timer_init(void) {
  memset(&time, 0, sizeof time);
  TIMSK2 = 0;
  ASSR = (1 << AS2);
  TCCR2B = (1 << CS20);
  for(;ASSR & (1 << TCR2BUB););
  TIMSK2 |= (1 << TOIE2);
  sei(); }

int main(void) {
  timer_init();
  for(;;);
  return 0; }

I've been told that [mega 2560] has a 32,768KHz watch crystal connected to TOSC1

Not that I can find on the reference schematic...

There's also an internal, but less accurate, crystal within the AVR chip that also should run at 32,768 KHz, and can be selected by setting AS2 and clearing EXCLK. Is this right?

No, that doesn't look right either. According to the data sheet, the choices for timer2 clock are: 1) CPU clock (AS2=0, EXCLK =don't care) 2) External Crystal on TOSC1/TOSC2 (AS2=1, EXCLK=0) 3) External clock signal on TOSC1 (AS2=1, EXCLK=1)

This kind of question might be better asked at avrfreaks.net, since you're operating completely outside the Arduino environment.