| 1 | ;; ----------------------------------------------------------------------- |
|---|
| 2 | ;; |
|---|
| 3 | ;; Copyright 2009 Intel Corporation; author: H. Peter Anvin |
|---|
| 4 | ;; |
|---|
| 5 | ;; This program is free software; you can redistribute it and/or modify |
|---|
| 6 | ;; it under the terms of the GNU General Public License as published by |
|---|
| 7 | ;; the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, |
|---|
| 8 | ;; Boston MA 02110-1301, USA; either version 2 of the License, or |
|---|
| 9 | ;; (at your option) any later version; incorporated herein by reference. |
|---|
| 10 | ;; |
|---|
| 11 | ;; ----------------------------------------------------------------------- |
|---|
| 12 | |
|---|
| 13 | ;; |
|---|
| 14 | ;; timer.inc |
|---|
| 15 | ;; |
|---|
| 16 | ;; Very simple counting timer |
|---|
| 17 | ;; |
|---|
| 18 | ;; This lets us have a simple incrementing variable without worrying |
|---|
| 19 | ;; about the BIOS_timer variable wrapping around at "midnight" and other |
|---|
| 20 | ;; weird things. |
|---|
| 21 | ;; |
|---|
| 22 | ;; This also maintains a timer variable calibrated in milliseconds |
|---|
| 23 | ;; (wraparound time = 49.7 days!) |
|---|
| 24 | ;; |
|---|
| 25 | |
|---|
| 26 | section .text16 |
|---|
| 27 | |
|---|
| 28 | timer_init: |
|---|
| 29 | ; Hook INT 1Ch |
|---|
| 30 | mov eax,[BIOS_timer_hook] |
|---|
| 31 | mov [BIOS_timer_next],eax |
|---|
| 32 | mov dword [BIOS_timer_hook],timer_irq |
|---|
| 33 | ret |
|---|
| 34 | |
|---|
| 35 | timer_cleanup: |
|---|
| 36 | ; Unhook INT 1Ch |
|---|
| 37 | mov eax,[BIOS_timer_next] |
|---|
| 38 | mov [BIOS_timer_hook],eax |
|---|
| 39 | ret |
|---|
| 40 | |
|---|
| 41 | ; |
|---|
| 42 | ; The specified frequency is 14.31818 MHz/12/65536; this turns out |
|---|
| 43 | ; to be a period of 54.92542 ms, or 0x36.ece8(187c) hexadecimal. |
|---|
| 44 | ; |
|---|
| 45 | timer_irq: |
|---|
| 46 | inc dword [cs:__jiffies] |
|---|
| 47 | add word [cs:__ms_timer_adj],0xece8 |
|---|
| 48 | adc dword [cs:__ms_timer],0x36 |
|---|
| 49 | jmp 0:0 |
|---|
| 50 | BIOS_timer_next equ $-4 |
|---|
| 51 | |
|---|
| 52 | section .data16 |
|---|
| 53 | alignz 4 |
|---|
| 54 | global __jiffies, __ms_timer |
|---|
| 55 | __jiffies dd 0 ; Clock tick timer |
|---|
| 56 | __ms_timer dd 0 ; Millisecond timer |
|---|
| 57 | __ms_timer_adj dw 0 ; Millisecond timer correction factor |
|---|