《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2 KiB

пре 12 година
  1. #include <x86.h>
  2. #include <trap.h>
  3. #include <stdio.h>
  4. #include <picirq.h>
  5. /* *
  6. * Support for time-related hardware gadgets - the 8253 timer,
  7. * which generates interruptes on IRQ-0.
  8. * */
  9. #define IO_TIMER1 0x040 // 8253 Timer #1
  10. /* *
  11. * Frequency of all three count-down timers; (TIMER_FREQ/freq)
  12. * is the appropriate count to generate a frequency of freq Hz.
  13. * */
  14. #define TIMER_FREQ 1193182
  15. #define TIMER_DIV(x) ((TIMER_FREQ + (x) / 2) / (x))
  16. #define TIMER_MODE (IO_TIMER1 + 3) // timer mode port
  17. #define TIMER_SEL0 0x00 // select counter 0
  18. #define TIMER_RATEGEN 0x04 // mode 2, rate generator
  19. #define TIMER_16BIT 0x30 // r/w counter 16 bits, LSB first
  20. volatile size_t ticks;
  21. /* *
  22. * clock_init - initialize 8253 clock to interrupt 100 times per second,
  23. * and then enable IRQ_TIMER.
  24. * */
  25. void
  26. clock_init(void) {
  27. // set 8253 timer-chip
  28. outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
  29. outb(IO_TIMER1, TIMER_DIV(100) % 256);
  30. outb(IO_TIMER1, TIMER_DIV(100) / 256);
  31. // initialize time counter 'ticks' to zero
  32. ticks = 0;
  33. cprintf("++ setup timer interrupts\n");
  34. pic_enable(IRQ_TIMER);
  35. }