#ifndef __CLOCK_H_
#define __CLOCK_H_

#define TIMER_MODE (0x36)
#define TIMER_ADDR1 (0x43)
#define TIMER_ADDR2 (0x40)

void interrupt (*old_clock_int)();


unsigned int clock_counter;
unsigned int clock_top;

void rev_clock ( void interrupt (*newint)(), unsigned srate) {
  long temp;
  unsigned int delay = (1193181L / srate) & 0xFFFF;
  clock_counter = 0;
  temp = srate & 0x0000FFFFL;
  temp *= 10;
  temp /= 182;
  clock_top = temp;
  old_clock_int = getvect(8);
  setvect(8, *newint);
  asm {
    cli
    mov     al, TIMER_MODE
    out     TIMER_ADDR1, al
    mov     ax, delay
    out     TIMER_ADDR2, al
    mov     al, ah
    out     TIMER_ADDR2, al
    sti
  }
}

void fix_clock() {
  asm {
    cli
    mov     al, TIMER_MODE
    out     TIMER_ADDR1, al
    sub     al, al
    out     TIMER_ADDR2, al
    out     TIMER_ADDR2, al
    sti
  }
  setvect(8,old_clock_int);
}
#endif

