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

Last change on this file since 501 was 501, checked in by aitotat@…, 11 years ago

Changes to XTIDE Universal BIOS:

  • XTIDE rev 2 and modded XTIDE rev 1 work again (fixed A0<->A3 swap when accessing Control Block Registers).
  • System INT 13h handler is no longer copied to INT 40h (testing if something uses INT 40h).
  • Removed controller hardware reset: now AH=0h and AH=Dh will only re-initialize drives (SB16 Tertiary and Quaternary IDE should now be safe to use when using Secondary IDE).
File size: 7.5 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_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH
25;   Parameters:
26;       BH:     Drive Select byte for Drive and Head Select Register
27;       DX:     Autodetected port for XT-CF
28;       DS:     Segment to RAMVARS
29;       ES:SI:  Ptr to buffer to receive 512-byte IDE Information
30;       CS:BP:  Ptr to IDEVARS
31;   Returns:
32;       AH:     INT 13h Error Code
33;       CF:     Cleared if success, Set if error
34;   Corrupts registers:
35;       AL, BX, CX, DX, SI, DI, ES
36;--------------------------------------------------------------------
37IdeCommand_IdentifyDeviceToBufferInESSIwithDriveSelectByteInBH:
38    ; Create fake DPT to be able to use Device.asm functions
39    call    FindDPT_ForNewDriveToDSDI
40    eMOVZX  ax, bh
41    mov     [di+DPT.wFlags], ax
42    call    CreateDPT_StoreIdevarsOffsetAndBasePortFromCSBPtoDPTinDSDI
43    call    IdeDPT_StoreDeviceTypeToDPTinDSDIfromIdevarsInCSBP
44    mov     BYTE [di+DPT_ATA.bBlockSize], 1 ; Block = 1 sector
45
46    ; Wait until drive motors have reached full speed
47    cmp     bp, BYTE ROMVARS.ideVars0   ; First controller?
48    jne     SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
49    test    bh, FLG_DRVNHEAD_DRV        ; Wait already done for Master
50    jnz     SHORT .SkipLongWaitSinceDriveIsNotPrimaryMaster
51    call    AHDh_WaitUntilDriveMotorHasReachedFullSpeed
52.SkipLongWaitSinceDriveIsNotPrimaryMaster:
53
54    ; Create IDEPACK without INTPACK
55    push    bp
56    call    Idepack_FakeToSSBP
57
58%ifdef MODULE_8BIT_IDE_ADVANCED
59    ; Enable 8-bit PIO mode for 8-bit ATA and XT-CF
60    push    si
61    call    AH9h_Enable8bitModeForDevice8bitAta
62    xor     al, al                      ; XTCF_8BIT_PIO_MODE
63    call    AH9h_SetModeFromALtoXTCF
64    pop     si
65%endif ; MODULE_8BIT_IDE_ADVANCED
66
67    ; Prepare to output Identify Device command
68    mov     dl, 1                       ; Sector count (required by IdeTransfer.asm)
69    mov     al, COMMAND_IDENTIFY_DEVICE
70    mov     bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ)
71    call    Idepack_StoreNonExtParametersAndIssueCommandFromAL
72
73    ; Clean stack and return
74.FailedToSet8bitMode:
75    lea     sp, [bp+SIZE_OF_IDEPACK_WITHOUT_INTPACK]    ; This assumes BP hasn't changed between Idepack_FakeToSSBP and here
76    pop     bp
77    ret
78
79
80;--------------------------------------------------------------------
81; IdeCommand_OutputWithParameters
82;   Parameters:
83;       BH:     System timer ticks for timeout
84;       BL:     IDE Status Register bit to poll after command
85;       ES:SI:  Ptr to buffer (for data transfer commands)
86;       DS:DI:  Ptr to DPT (in RAMVARS segment)
87;       SS:BP:  Ptr to IDEPACK
88;   Returns:
89;       AH:     INT 13h Error Code
90;       CX:     Number of successfully transferred sectors (for transfer commands)
91;       CF:     Cleared if success, Set if error
92;   Corrupts registers:
93;       AL, BX, (CX), DX, (ES:SI for data transfer commands)
94;--------------------------------------------------------------------
95ALIGN JUMP_ALIGN
96IdeCommand_OutputWithParameters:
97    push    bx                      ; Store status register bits to poll
98
99    ; Select Master or Slave drive and output head number or LBA28 top bits
100    call    IdeCommand_SelectDrive
101    jc      SHORT .DriveNotReady
102
103    ; Output Device Control Byte to enable or disable interrupts
104    mov     al, [bp+IDEPACK.bDeviceControl]
105%ifdef MODULE_IRQ
106    test    al, FLG_DEVCONTROL_nIEN ; Interrupts disabled?
107    jnz     SHORT .DoNotSetInterruptInServiceFlag
108
109    ; Clear Task Flag and set Interrupt In-Service Flag
110    or      BYTE [di+DPT.bFlagsHigh], FLGH_DPT_INTERRUPT_IN_SERVICE
111    push    ds
112    LOAD_BDA_SEGMENT_TO ds, dx, !   ; Also zero DX
113    mov     [BDA.bHDTaskFlg], dl
114    pop     ds
115.DoNotSetInterruptInServiceFlag:
116%endif
117    OUTPUT_AL_TO_IDE_CONTROL_BLOCK_REGISTER     DEVICE_CONTROL_REGISTER_out
118
119    ; Output Feature Number
120    mov     al, [bp+IDEPACK.bFeatures]
121    OUTPUT_AL_TO_IDE_REGISTER   FEATURES_REGISTER_out
122
123    ; Output Sector Address High (only used by LBA48)
124%ifdef MODULE_EBIOS
125    eMOVZX  ax, [bp+IDEPACK.bLbaLowExt]     ; Zero sector count
126    mov     cx, [bp+IDEPACK.wLbaMiddleAndHighExt]
127    call    OutputSectorCountAndAddress
128%endif
129
130    ; Output Sector Address Low
131    mov     ax, [bp+IDEPACK.wSectorCountAndLbaLow]
132    mov     cx, [bp+IDEPACK.wLbaMiddleAndHigh]
133    call    OutputSectorCountAndAddress
134
135    ; Output command
136    mov     al, [bp+IDEPACK.bCommand]
137    OUTPUT_AL_TO_IDE_REGISTER   COMMAND_REGISTER_out
138
139    ; Wait until command completed
140    pop     bx                              ; Pop status and timeout for polling
141    cmp     bl, FLG_STATUS_DRQ              ; Data transfer started?
142    jne     SHORT .WaitUntilNonTransferCommandCompletes
143%ifdef MODULE_8BIT_IDE_ADVANCED
144    cmp     BYTE [di+DPT_ATA.bDevice], DEVICE_8BIT_XTCF_MEMMAP
145    jae     SHORT JrIdeTransfer_StartWithCommandInAL    ; DEVICE_8BIT_XTCF_MEMMAP or DEVICE_8BIT_JRIDE_ISA
146%endif
147    jmp     IdeTransfer_StartWithCommandInAL
148
149.WaitUntilNonTransferCommandCompletes:
150%ifdef MODULE_IRQ
151    test    BYTE [bp+IDEPACK.bDeviceControl], FLG_DEVCONTROL_nIEN
152    jz      SHORT .PollStatusFlagInsteadOfWaitIrq
153    jmp     IdeWait_IRQorStatusFlagInBLwithTimeoutInBH
154.PollStatusFlagInsteadOfWaitIrq:
155%endif
156    jmp     IdeWait_PollStatusFlagInBLwithTimeoutInBH
157
158.DriveNotReady:
159    pop     bx                          ; Clean stack
160    ret
161
162
163;--------------------------------------------------------------------
164; IdeCommand_SelectDrive
165;   Parameters:
166;       DS:DI:  Ptr to DPT (in RAMVARS segment)
167;       SS:BP:  Ptr to IDEPACK
168;   Returns:
169;       AH:     INT 13h Error Code
170;       CF:     Cleared if success, Set if error
171;   Corrupts registers:
172;       AL, BX, CX, DX
173;--------------------------------------------------------------------
174ALIGN JUMP_ALIGN
175IdeCommand_SelectDrive:
176    ; We use different timeout value when detecting drives.
177    ; This prevents unnecessary long delays when drive is not present.
178    mov     cx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRDY, FLG_STATUS_DRDY)
179    cmp     WORD [RAMVARS.wDrvDetectSignature], RAMVARS_DRV_DETECT_SIGNATURE
180    eCMOVE  ch, TIMEOUT_SELECT_DRIVE_DURING_DRIVE_DETECTION
181
182    ; Select Master or Slave Drive
183    mov     al, [bp+IDEPACK.bDrvAndHead]
184    OUTPUT_AL_TO_IDE_REGISTER   DRIVE_AND_HEAD_SELECT_REGISTER
185    mov     bx, cx
186    call    IdeWait_PollStatusFlagInBLwithTimeoutInBH
187
188    ; Ignore errors from IDE Error Register (set by previous command)
189    cmp     ah, RET_HD_TIMEOUT
190    je      SHORT .FailedToSelectDrive
191    xor     ax, ax                  ; Always success unless timeout
192    ret
193.FailedToSelectDrive:
194    stc
195    ret
196
197
198;--------------------------------------------------------------------
199; OutputSectorCountAndAddress
200;   Parameters:
201;       AH:     LBA low bits (Sector Number)
202;       AL:     Sector Count
203;       CL:     LBA middle bits (Cylinder Number low)
204;       CH:     LBA high bits (Cylinder Number high)
205;       DS:DI:  Ptr to DPT (in RAMVARS segment)
206;   Returns:
207;       Nothing
208;   Corrupts registers:
209;       AL, BX, DX
210;--------------------------------------------------------------------
211ALIGN JUMP_ALIGN
212OutputSectorCountAndAddress:
213    OUTPUT_AL_TO_IDE_REGISTER   SECTOR_COUNT_REGISTER
214
215    mov     al, ah
216    OUTPUT_AL_TO_IDE_REGISTER   LBA_LOW_REGISTER
217
218    mov     al, cl
219    OUTPUT_AL_TO_IDE_REGISTER   LBA_MIDDLE_REGISTER
220
221    mov     al, ch
222    OUTPUT_AL_TO_IDE_REGISTER   LBA_HIGH_REGISTER
223    ret
Note: See TracBrowser for help on using the repository browser.