1 | ; Project name : Assembly Library |
---|
2 | ; Description : Delay functions. |
---|
3 | |
---|
4 | ; Section containing code |
---|
5 | SECTION .text |
---|
6 | |
---|
7 | ;-------------------------------------------------------------------- |
---|
8 | ; Clears prefetch queue by jumping to next instruction. |
---|
9 | ; This delays much more than nop instruction of fast systems. |
---|
10 | ; |
---|
11 | ; JMP_DELAY |
---|
12 | ; Parameters |
---|
13 | ; Nothing |
---|
14 | ; Returns: |
---|
15 | ; Nothing |
---|
16 | ; Corrupts registers: |
---|
17 | ; Nothing |
---|
18 | ;-------------------------------------------------------------------- |
---|
19 | %macro JMP_DELAY 0 |
---|
20 | jmp SHORT %%NextInstruction |
---|
21 | %%NextInstruction: |
---|
22 | %endmacro |
---|
23 | |
---|
24 | |
---|
25 | ;-------------------------------------------------------------------- |
---|
26 | ; Mimimun delays (without fetching) with some CPU architectures: |
---|
27 | ; 8088/8086: 17 cycles for jump + 5 cycles for last comparison |
---|
28 | ; 286: 10 cycles for jump + 4 cycles for last comparison |
---|
29 | ; 386: 13 cycles for jump + ? cycles for last comparison |
---|
30 | ; 486: 7 cycles for jump + 6 cycles for last comparison |
---|
31 | ; |
---|
32 | ; DELAY_WITH_LOOP_INSTRUCTION |
---|
33 | ; Parameters |
---|
34 | ; CX: Loop iterations (0 is maximum delay with 65536 iterations) |
---|
35 | ; Returns: |
---|
36 | ; Nothing |
---|
37 | ; Corrupts registers: |
---|
38 | ; CX |
---|
39 | ;-------------------------------------------------------------------- |
---|
40 | %macro DELAY_WITH_LOOP_INSTRUCTION 0 |
---|
41 | %%StartOfLoop: |
---|
42 | loop %%StartOfLoop |
---|
43 | %endmacro |
---|
44 | |
---|
45 | |
---|
46 | ;-------------------------------------------------------------------- |
---|
47 | ; Delay is always at least one millisecond since |
---|
48 | ; RTC resolution is 977 microsecs. |
---|
49 | ; |
---|
50 | ; Delay_MicrosecondsFromAX |
---|
51 | ; Parameters: |
---|
52 | ; AX: Number of microsecs to wait |
---|
53 | ; Returns: |
---|
54 | ; Nothing |
---|
55 | ; Corrupts registers: |
---|
56 | ; AX |
---|
57 | ;-------------------------------------------------------------------- |
---|
58 | ALIGN JUMP_ALIGN |
---|
59 | Delay_MicrosecondsFromAX: |
---|
60 | push dx |
---|
61 | push cx |
---|
62 | |
---|
63 | xor cx, cx |
---|
64 | xchg dx, ax ; Microsecs now in CX:DX |
---|
65 | mov ah, EVENT_WAIT ; Event Wait |
---|
66 | int BIOS_SYSTEM_INTERRUPT_15h |
---|
67 | sti ; XT BIOSes return with interrupt disabled |
---|
68 | |
---|
69 | pop cx |
---|
70 | pop dx |
---|
71 | mov ax, 1 ; Prepare to wait 1 timer tick |
---|
72 | jc SHORT Delay_TimerTicksFromAX ; Event Wait was unsupported or busy |
---|
73 | ret |
---|
74 | |
---|
75 | |
---|
76 | ;-------------------------------------------------------------------- |
---|
77 | ; First tick might take 0...54.9 ms and remaining ticks |
---|
78 | ; will occur at 54.9 ms intervals. |
---|
79 | ; |
---|
80 | ; Delay_TimerTicksFromAX |
---|
81 | ; Parameters: |
---|
82 | ; AX: Number of timer ticks to wait |
---|
83 | ; Returns: |
---|
84 | ; Nothing |
---|
85 | ; Corrupts registers: |
---|
86 | ; AX |
---|
87 | ;-------------------------------------------------------------------- |
---|
88 | ALIGN JUMP_ALIGN |
---|
89 | Delay_TimerTicksFromAX: |
---|
90 | push dx |
---|
91 | |
---|
92 | sti ; Make sure that interrupts are enabled |
---|
93 | xchg dx, ax |
---|
94 | call TimerTicks_ReadFromBdaToAX |
---|
95 | add dx, ax ; DX = end time |
---|
96 | .WaitLoop: |
---|
97 | call TimerTicks_ReadFromBdaToAX |
---|
98 | cmp ax, dx |
---|
99 | jb SHORT .WaitLoop ; Loop until end time is reached |
---|
100 | |
---|
101 | pop dx |
---|
102 | ret |
---|