[3] | 1 | ; File name : SoftDelay.asm |
---|
| 2 | ; Project name : IDE BIOS |
---|
| 3 | ; Created date : 22.9.2007 |
---|
| 4 | ; Last update : 13.4.2010 |
---|
| 5 | ; Author : Tomi Tilli |
---|
| 6 | ; Description : Software delay loops for I/O timeout and other loops. |
---|
| 7 | |
---|
| 8 | ; Section containing code |
---|
| 9 | SECTION .text |
---|
| 10 | |
---|
| 11 | ;-------------------------------------------------------------------- |
---|
| 12 | ; There should be at least 400ns delay between outputting |
---|
| 13 | ; IDE command / drive selection and reading IDE Status Register. |
---|
| 14 | ; |
---|
| 15 | ; SoftDelay_BeforePollingStatusRegister |
---|
| 16 | ; Parameters: |
---|
| 17 | ; Nothing |
---|
| 18 | ; Returns: |
---|
| 19 | ; Nothing |
---|
| 20 | ; Corrupts registers: |
---|
| 21 | ; Nothing |
---|
| 22 | ;-------------------------------------------------------------------- |
---|
| 23 | ALIGN JUMP_ALIGN ; Cycles required |
---|
| 24 | SoftDelay_BeforePollingStatusRegister: ; 8088 | 286 | 386 | 486 |
---|
| 25 | push cx ; 15 | 3 | 2 | 1 |
---|
| 26 | mov cx, 2 ; 4 | 2 | 2 | 1 |
---|
| 27 | .DelayLoop: |
---|
| 28 | loop .DelayLoop ; | 10 | 12 | 6 |
---|
| 29 | pop cx ; 8 | 5 | 4 | 4 |
---|
| 30 | ret ; 20 | 11m| 10m| 5 |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | ;-------------------------------------------------------------------- |
---|
| 34 | ; Initializes timeout counter. Timeouts are implemented using system |
---|
| 35 | ; timer ticks. First tick might take 0...54.9ms and remaining ticks |
---|
| 36 | ; will occur at 54.9ms intervals. Use delay of two (or more) ticks to |
---|
| 37 | ; ensure at least 54.9ms wait. |
---|
| 38 | ; |
---|
| 39 | ; SoftDelay_InitTimeout |
---|
| 40 | ; Parameters: |
---|
| 41 | ; CL: Number of system timer ticks before timeout |
---|
| 42 | ; DS: Segment to RAMVARS |
---|
| 43 | ; Returns: |
---|
| 44 | ; Nothing |
---|
| 45 | ; Corrupts registers: |
---|
| 46 | ; CX |
---|
| 47 | ;-------------------------------------------------------------------- |
---|
| 48 | ALIGN JUMP_ALIGN |
---|
| 49 | SoftDelay_InitTimeout: |
---|
| 50 | mov [RAMVARS.bEndTime], cl ; Store timeout ticks |
---|
| 51 | push ds |
---|
| 52 | LOAD_BDA_SEGMENT_TO ds, cx |
---|
| 53 | mov cl, [BDA.dwTimerTicks] ; Load current time lowest byte |
---|
| 54 | pop ds |
---|
| 55 | add [RAMVARS.bEndTime], cl ; Timeout to end time |
---|
| 56 | sti ; Enable interrupts |
---|
| 57 | ret |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | ;-------------------------------------------------------------------- |
---|
| 61 | ; Updates timeout counter. Timeout counter can be |
---|
| 62 | ; initialized with SoftDelay_InitTimeout. |
---|
| 63 | ; |
---|
| 64 | ; SoftDelay_UpdTimeout |
---|
| 65 | ; Parameters: |
---|
| 66 | ; DS: Segment to RAMVARS |
---|
| 67 | ; Returns: |
---|
| 68 | ; CF: Set if timeout |
---|
| 69 | ; Cleared if time left |
---|
| 70 | ; Corrupts registers: |
---|
| 71 | ; Nothing |
---|
| 72 | ;-------------------------------------------------------------------- |
---|
| 73 | ALIGN JUMP_ALIGN |
---|
| 74 | SoftDelay_UpdTimeout: |
---|
| 75 | push ax |
---|
| 76 | push ds |
---|
| 77 | LOAD_BDA_SEGMENT_TO ds, ax |
---|
| 78 | mov al, [BDA.dwTimerTicks] ; Load current time lowest byte |
---|
| 79 | pop ds |
---|
| 80 | |
---|
| 81 | cmp al, [RAMVARS.bEndTime] ; End time reached? |
---|
| 82 | pop ax |
---|
| 83 | je SHORT .ReturnTimeout |
---|
| 84 | clc |
---|
| 85 | ret |
---|
| 86 | .ReturnTimeout: |
---|
| 87 | stc |
---|
| 88 | ret |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | ;-------------------------------------------------------------------- |
---|
| 92 | ; Microsecond delay. |
---|
| 93 | ; Current implementation uses BIOS event wait services that are not |
---|
| 94 | ; available on XT systems. Calling overhead should be enough so it does |
---|
| 95 | ; not matter for IDE waits. On AT+ the delay will be at least 1ms since |
---|
| 96 | ; RTC resolution is 977 microsecs. |
---|
| 97 | ; |
---|
| 98 | ; SoftDelay_us |
---|
| 99 | ; Parameters: |
---|
| 100 | ; CX: Number of microsecs to wait |
---|
| 101 | ; Returns: |
---|
| 102 | ; Nothing |
---|
| 103 | ; Corrupts registers: |
---|
| 104 | ; CX |
---|
| 105 | ;-------------------------------------------------------------------- |
---|
| 106 | ALIGN JUMP_ALIGN |
---|
| 107 | SoftDelay_us: |
---|
| 108 | push dx |
---|
| 109 | push ax |
---|
| 110 | |
---|
| 111 | xor dx, dx ; Zero DX |
---|
| 112 | xchg cx, dx ; Microsecs now in CX:DX |
---|
| 113 | mov ah, 86h ; Event Wait |
---|
| 114 | int INTV_SYSTEM_SERVICES |
---|
| 115 | |
---|
| 116 | pop ax |
---|
| 117 | pop dx |
---|
| 118 | mov cx, 2 ; Prepare to wait 2 timer ticks |
---|
| 119 | jc SHORT SoftDelay_TimerTicks ; Unsupported or busy |
---|
| 120 | ret |
---|
| 121 | |
---|
| 122 | |
---|
| 123 | ;-------------------------------------------------------------------- |
---|
| 124 | ; Timer tick delay. |
---|
| 125 | ; One tick is about 54.9ms but first tick may occur anytime between |
---|
| 126 | ; 0...54.9ms! |
---|
| 127 | ; |
---|
| 128 | ; SoftDelay_TimerTicks |
---|
| 129 | ; Parameters: |
---|
| 130 | ; CX: Number of timer ticks to wait |
---|
| 131 | ; Returns: |
---|
| 132 | ; Nothing |
---|
| 133 | ; Corrupts registers: |
---|
| 134 | ; CX |
---|
| 135 | ;-------------------------------------------------------------------- |
---|
| 136 | ALIGN JUMP_ALIGN |
---|
| 137 | SoftDelay_TimerTicks: |
---|
| 138 | push ds |
---|
| 139 | push ax |
---|
| 140 | LOAD_BDA_SEGMENT_TO ds, ax |
---|
| 141 | add cx, [BDA.dwTimerTicks] ; CX to end time |
---|
| 142 | sti ; Enable interrupts |
---|
| 143 | .DelayLoop: |
---|
| 144 | cmp cx, [BDA.dwTimerTicks] ; Wait complete? |
---|
| 145 | jne SHORT .DelayLoop ; If not, loop |
---|
| 146 | pop ax |
---|
| 147 | pop ds |
---|
| 148 | ret |
---|