source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Device/IDE/IdeCommand.asm @ 445

Last change on this file since 445 was 445, checked in by krille_n_@…, 12 years ago

Changes:

  • A speed optimization to the eSHL_IM macro for 386 and higher. This change breaks emulation in the sense that the macro will fail when given a memory operand as the first parameter.
  • Memory_SumCXbytesFromESSItoAL now returns with the zero flag set/cleared according to the result.
  • Unrolled all the 8 bit READ transfer loops to do 16 bytes per iteration. Added a new macro (UNROLL_SECTORS_IN_CX_TO_OWORDS) as part of it. Size wise this is expensive but I think it should be worth the ROM space. The WRITE transfer loops were left as is since writes are rare anyway (<10% of all disk I/O IIRC).
  • Minor optimizations and fixes here and there.
File size: 8.4 KB
Line 
1; Project name  :   XTIDE Universal BIOS
2; Description   :   IDE Device Command functions.
3
4;
5; XTIDE Universal BIOS and Associated Tools
6; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2012 by XTIDE Universal BIOS Team.
7;
8; This program is free software; you can redistribute it and/or modify
9; it under the terms of the GNU General Public License as published by
10; the Free Software Foundation; either version 2 of the License, or
11; (at your option) any later version.
12;
13; This program is distributed in the hope that it will be useful,
14; but WITHOUT ANY WARRANTY; without even the implied warranty of
15; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16; GNU General Public License for more details.
17; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
18;
19
20; Section containing code
21SECTION .text
22
23;--------------------------------------------------------------------
24; IdeCommand_ResetMasterAndSlaveController
25;   Parameters:
26;       DS:DI:  Ptr to DPT (in RAMVARS segment)
27;   Returns:
28;       AH:     INT 13h Error Code
29;       CF:     Cleared if success, Set if error
30;   Corrupts registers:
31;       AL, BX, CX, DX
32;--------------------------------------------------------------------
33IdeCommand_ResetMasterAndSlaveController:
34    ; HSR0: Set_SRST
35    call    AccessDPT_GetDeviceControlByteToAL
36    or      al, FLG_DEVCONTROL_SRST | FLG_DEVCONTROL_nIEN   ; Set Reset bit
37    OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER     DEVICE_CONTROL_REGISTER_out
38    mov     ax, HSR0_RESET_WAIT_US
39    call    Timer_DelayMicrosecondsFromAX
40
41    ; HSR1: Clear_wait
42    call    AccessDPT_GetDeviceControlByteToAL
43    or      al, FLG_DEVCONTROL_nIEN
44    and     al, ~FLG_DEVCONTROL_SRST                        ; Clear reset bit
45    OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER     DEVICE_CONTROL_REGISTER_out
46    mov     ax, HSR1_RESET_WAIT_US
47    call    Timer_DelayMicrosecondsFromAX
48
49    ; HSR2: Check_status
50    mov     bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_MAXIMUM, FLG_STATUS_BSY)
51    jmp     IdeWait_PollStatusFlagInBLwithTimeoutInBH
52
53
54;--------------------------------------------------------------------
55; IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH
56;   Parameters:
57;       BH:     Drive Select byte for Drive and Head Select Register
58;       DS:     Segment to RAMVARS
59;       ES:SI:  Ptr to buffer to receive 512-byte IDE Information
60;       CS:BP:  Ptr to IDEVARS
61;   Returns:
62;       AH:     INT 13h Error Code
63;       CF:     Cleared if success, Set if error
64;   Corrupts registers:
65;       AL, BX, CX, DX, SI, DI, ES
66;--------------------------------------------------------------------
67IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH:
68    ; Create fake DPT to be able to use Device.asm functions
69    call    FindDPT_ForNewDriveToDSDI
70    eMOVZX  ax, bh
71    mov     [di+DPT.wFlags], ax
72    mov     [di+DPT.bIdevarsOffset], bp
73    mov     BYTE [di+DPT_ATA.bBlockSize], 1 ; Block = 1 sector
74    call    IdeDPT_StoreDeviceTypeFromIdevarsInCSBPtoDPTinDSDI
75
76    ; Wait until drive motors have reached full speed
77    cmp     bp, BYTE ROMVARS.ideVars0       ; First controller?
78    jne     SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
79    test    bh, FLG_DRVNHEAD_DRV            ; Wait already done for Master
80    jnz     SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
81    call    AHDh_WaitUntilDriveMotorHasReachedFullSpeed
82.SkipLongWaitSinceDriveIsNotPrimaryMaster:
83
84    ; Create IDEPACK without INTPACK
85    push    bp
86    call    Idepack_FakeToSSBP
87
88%ifdef MODULE_8BIT_IDE
89    ; Enable 8-bit PIO mode for Lo-tech XT-CF
90    push    si
91    call    AH9h_Enable8bitPioModeForXTCF
92    pop     si
93    jc      SHORT .FailedToSet8bitMode
94%endif
95
96    ; Prepare to output Identify Device command
97    mov     dl, 1                       ; Sector count (required by IdeTransfer.asm)
98    mov     al, COMMAND_IDENTIFY_DEVICE
99    mov     bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ)
100    call    Idepack_StoreNonExtParametersAndIssueCommandFromAL
101
102    ; Clean stack and return
103.FailedToSet8bitMode:
104    lea     sp, [bp+SIZE_OF_IDEPACK_WITHOUT_INTPACK]    ; This assumes BP hasn't changed between Idepack_FakeToSSBP and here
105    pop     bp
106    ret
107
108
109;--------------------------------------------------------------------
110; IdeCommand_OutputWithParameters
111;   Parameters:
112;       BH:     System timer ticks for timeout
113;       BL:     IDE Status Register bit to poll after command
114;       ES:SI:  Ptr to buffer (for data transfer commands)
115;       DS:DI:  Ptr to DPT (in RAMVARS segment)
116;       SS:BP:  Ptr to IDEPACK
117;   Returns:
118;       AH:     INT 13h Error Code
119;       CX:     Number of successfully transferred sectors (for transfer commands)
120;       CF:     Cleared if success, Set if error
121;   Corrupts registers:
122;       AL, BX, (CX), DX, (ES:SI for data transfer commands)
123;--------------------------------------------------------------------
124ALIGN JUMP_ALIGN
125IdeCommand_OutputWithParameters:
126    push    bx                      ; Store status register bits to poll
127
128    ; Select Master or Slave drive and output head number or LBA28 top bits
129    call    IdeCommand_SelectDrive
130    jc      SHORT .DriveNotReady
131
132    ; Output Device Control Byte to enable or disable interrupts
133    mov     al, [bp+IDEPACK.bDeviceControl]
134%ifdef MODULE_IRQ
135    test    al, FLG_DEVCONTROL_nIEN ; Interrupts disabled?
136    jnz     SHORT .DoNotSetInterruptInServiceFlag
137
138    ; Clear Task Flag and set Interrupt In-Service Flag
139    or      BYTE [di+DPT.bFlagsHigh], FLGH_DPT_INTERRUPT_IN_SERVICE
140    push    ds
141    LOAD_BDA_SEGMENT_TO ds, dx, !   ; Also zero DX
142    mov     [BDA.bHDTaskFlg], dl
143    pop     ds
144.DoNotSetInterruptInServiceFlag:
145%endif
146    OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER     DEVICE_CONTROL_REGISTER_out
147
148    ; Output Feature Number
149    mov     al, [bp+IDEPACK.bFeatures]
150    OUTPUT_AL_TO_IDE_REGISTER   FEATURES_REGISTER_out
151
152    ; Output Sector Address High (only used by LBA48)
153%ifdef MODULE_EBIOS
154    eMOVZX  ax, [bp+IDEPACK.bLbaLowExt]     ; Zero sector count
155    mov     cx, [bp+IDEPACK.wLbaMiddleAndHighExt]
156    call    OutputSectorCountAndAddress
157%endif
158
159    ; Output Sector Address Low
160    mov     ax, [bp+IDEPACK.wSectorCountAndLbaLow]
161    mov     cx, [bp+IDEPACK.wLbaMiddleAndHigh]
162    call    OutputSectorCountAndAddress
163
164    ; Output command
165    mov     al, [bp+IDEPACK.bCommand]
166    OUTPUT_AL_TO_IDE_REGISTER   COMMAND_REGISTER_out
167
168    ; Wait until command completed
169    pop     bx                              ; Pop status and timeout for polling
170    cmp     bl, FLG_STATUS_DRQ              ; Data transfer started?
171    jne     SHORT .WaitUntilNonTransferCommandCompletes
172%ifdef MODULE_JRIDE
173    cmp     BYTE [di+DPT_ATA.bDevice], DEVICE_8BIT_JRIDE_ISA
174    je      SHORT JrIdeTransfer_StartWithCommandInAL
175%endif
176    jmp     IdeTransfer_StartWithCommandInAL
177
178.WaitUntilNonTransferCommandCompletes:
179%ifdef MODULE_IRQ
180    test    BYTE [bp+IDEPACK.bDeviceControl], FLG_DEVCONTROL_nIEN
181    jz      SHORT .PollStatusFlagInsteadOfWaitIrq
182    jmp     IdeWait_IRQorStatusFlagInBLwithTimeoutInBH
183.PollStatusFlagInsteadOfWaitIrq:
184%endif
185    jmp     IdeWait_PollStatusFlagInBLwithTimeoutInBH
186
187.DriveNotReady:
188    pop     bx                          ; Clean stack
189    ret
190
191
192;--------------------------------------------------------------------
193; IdeCommand_SelectDrive
194;   Parameters:
195;       DS:DI:  Ptr to DPT (in RAMVARS segment)
196;       SS:BP:  Ptr to IDEPACK
197;   Returns:
198;       AH:     INT 13h Error Code
199;       CF:     Cleared if success, Set if error
200;   Corrupts registers:
201;       AL, BX, CX, DX
202;--------------------------------------------------------------------
203ALIGN JUMP_ALIGN
204IdeCommand_SelectDrive:
205    ; We use different timeout value when detecting drives.
206    ; This prevents unnecessary long delays when drive is not present.
207    mov     bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRDY, FLG_STATUS_DRDY)
208    cmp     WORD [RAMVARS.wDrvDetectSignature], RAMVARS_DRV_DETECT_SIGNATURE
209    eCMOVE  bh, TIMEOUT_SELECT_DRIVE_DURING_DRIVE_DETECTION
210
211    ; Select Master or Slave Drive
212    mov     al, [bp+IDEPACK.bDrvAndHead]
213    OUTPUT_AL_TO_IDE_REGISTER   DRIVE_AND_HEAD_SELECT_REGISTER
214    call    IdeWait_PollStatusFlagInBLwithTimeoutInBH
215
216    ; Ignore errors from IDE Error Register (set by previous command)
217    cmp     ah, RET_HD_TIMEOUT
218    je      SHORT .FailedToSelectDrive
219    xor     ax, ax                  ; Always success unless timeout
220    ret
221.FailedToSelectDrive:
222    stc
223    ret
224
225
226;--------------------------------------------------------------------
227; OutputSectorCountAndAddress
228;   Parameters:
229;       AH:     LBA low bits (Sector Number)
230;       AL:     Sector Count
231;       CL:     LBA middle bits (Cylinder Number low)
232;       CH:     LBA high bits (Cylinder Number high)
233;       DS:DI:  Ptr to DPT (in RAMVARS segment)
234;   Returns:
235;       Nothing
236;   Corrupts registers:
237;       AL, BX, DX
238;--------------------------------------------------------------------
239ALIGN JUMP_ALIGN
240OutputSectorCountAndAddress:
241    OUTPUT_AL_TO_IDE_REGISTER   SECTOR_COUNT_REGISTER
242
243    mov     al, ah
244    OUTPUT_AL_TO_IDE_REGISTER   LBA_LOW_REGISTER
245
246    mov     al, cl
247    OUTPUT_AL_TO_IDE_REGISTER   LBA_MIDDLE_REGISTER
248
249    mov     al, ch
250    JUMP_TO_OUTPUT_AL_TO_IDE_REGISTER   LBA_HIGH_REGISTER
Note: See TracBrowser for help on using the repository browser.