《操作系统》的实验代码。
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.

49 lines
1.3 KiB

10 years ago
  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. long SYSTEM_READ_TIMER( void ){
  22. return ticks;
  23. }
  24. /* *
  25. * clock_init - initialize 8253 clock to interrupt 100 times per second,
  26. * and then enable IRQ_TIMER.
  27. * */
  28. void
  29. clock_init(void) {
  30. // set 8253 timer-chip
  31. outb(TIMER_MODE, TIMER_SEL0 | TIMER_RATEGEN | TIMER_16BIT);
  32. outb(IO_TIMER1, TIMER_DIV(100) % 256);
  33. outb(IO_TIMER1, TIMER_DIV(100) / 256);
  34. // initialize time counter 'ticks' to zero
  35. ticks = 0;
  36. cprintf("++ setup timer interrupts\n");
  37. pic_enable(IRQ_TIMER);
  38. }