source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Device/IDE/JrIdeTransfer.asm @ 488

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

Removed unused '4' entry from Strings.asm and recompressed. Added 8x multiplier value for serial drives. Wrapped some preprocessor symbol references so that 'make xt_unused' analysis continues to work.

File size: 9.6 KB
Line 
1; Project name  :   XTIDE Universal BIOS
2; Description   :   Memory mapped IDE Device transfer 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; Structure containing variables for PIO transfer functions.
21; This struct must not be larger than IDEPACK without INTPACK.
22struc MEMPIOVARS    ; Must not be larger than 9 bytes! See IDEPACK in RamVars.inc.
23    .wSectorsInBlock        resb    2   ; 0-1, Block size in sectors
24    .fpDPT                  resb    4   ; 2-5, Far pointer to DPT
25    .bSectorsLeft           resb    1   ; 6, Sectors left to transfer
26                            resb    1   ; 7, IDEPACK.bDeviceControl
27    .bSectorsDone           resb    1   ; 8, Number of sectors xferred
28endstruc
29
30SECTOR_ACCESS_WINDOW_SIZE   EQU     512 ; 512 bytes
31
32
33; Section containing code
34SECTION .text
35
36;--------------------------------------------------------------------
37; JrIdeTransfer_StartWithCommandInAL
38;   Parameters:
39;       AL:     IDE command that was used to start the transfer
40;               (all PIO read and write commands including Identify Device)
41;       ES:SI:  Ptr to data buffer
42;       DS:DI:  Ptr to DPT (in RAMVARS segment)
43;       SS:BP:  Ptr to IDEPACK
44;   Returns:
45;       AH:     INT 13h Error Code
46;       CX:     Number of successfully transferred sectors
47;       CF:     Cleared if success, Set if error
48;   Corrupts registers:
49;       AL, BX, DX, SI, ES
50;--------------------------------------------------------------------
51ALIGN JUMP_ALIGN
52JrIdeTransfer_StartWithCommandInAL:
53    ; Initialize MEMPIOVARS
54    xchg    cx, ax          ; IDE command to CL
55    xor     ax, ax
56    mov     [bp+MEMPIOVARS.bSectorsDone], al
57    mov     al, [bp+IDEPACK.bSectorCount]
58    mov     [bp+MEMPIOVARS.bSectorsLeft], al
59    mov     al, [di+DPT_ATA.bBlockSize]
60    mov     [bp+MEMPIOVARS.wSectorsInBlock], ax
61    mov     [bp+MEMPIOVARS.fpDPT], di
62    mov     [bp+MEMPIOVARS.fpDPT+2], ds
63
64    ; Normalize pointer
65    call    IdeTransfer_NormalizePointerInESSI
66
67    ; Get far pointer to Sector Access Window
68    mov     dx, [di+DPT.wBasePort]
69    cmp     BYTE [di+DPT_ATA.bDevice], DEVICE_8BIT_JRIDE_ISA
70    jne     SHORT .GetSectorAccessWindowForXTCF
71
72    ; Get Sector Access Window for JR-IDE/ISA
73    mov     di, JRIDE_SECTOR_ACCESS_WINDOW_OFFSET
74    mov     ds, dx      ; Segment for JR-IDE/ISA
75    jmp     SHORT .SectorAccessWindowLoadedToDSDI
76
77.GetSectorAccessWindowForXTCF:
78    xor     di, di
79    add     dl, XTCF_CONTROL_REGISTER
80    in      al, dx                  ; Read high byte for Sector Access Window segment
81    xchg    ah, al
82    mov     ds, ax
83
84    ; Are we reading or writing?
85.SectorAccessWindowLoadedToDSDI:
86    test    cl, 16  ; Bit 4 is cleared on all the read commands but set on 3 of the 4 write commands
87    jnz     SHORT WriteToSectorAccessWindow
88    cmp     cl, COMMAND_WRITE_MULTIPLE
89    je      SHORT WriteToSectorAccessWindow
90    ; Fall to ReadFromSectorAccessWindow
91
92;--------------------------------------------------------------------
93; ReadFromSectorAccessWindow
94;   Parameters:
95;       DS:DI:  Ptr to Sector Access Window
96;       ES:SI:  Normalized ptr to buffer to receive data
97;       SS:BP:  Ptr to MEMPIOVARS
98;   Returns:
99;       DS:DI:  Ptr to DPT (in RAMVARS segment)
100;       AH:     BIOS Error code
101;       CX:     Number of successfully transferred sectors
102;       CF:     0 if transfer successful
103;               1 if any error
104;   Corrupts registers:
105;       AL, BX, DX, SI
106;--------------------------------------------------------------------
107ReadFromSectorAccessWindow:
108    xchg    si, di  ; DS:SI = source, ES:DI = Destination
109    call    WaitUntilReadyToTransferNextBlock
110    jc      SHORT ReturnWithMemoryIOtransferErrorInAH
111
112    mov     cx, [bp+MEMPIOVARS.wSectorsInBlock] ; Clears CH
113    cmp     [bp+MEMPIOVARS.bSectorsLeft], cl
114    jbe     SHORT .ReadLastBlockFromDrive
115
116ALIGN JUMP_ALIGN
117.ReadNextBlockFromDrive:
118    call    ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
119    call    WaitUntilReadyToTransferNextBlock
120    jc      SHORT ReturnWithMemoryIOtransferErrorInAH
121
122    ; Update number of successfully read sectors and sectors left to transfer.
123    ; We cannot use SUB instruction as a comparison since it will not
124    ; work in this situation:
125    ; Before SUB we have 2 full blocks left to transfer. SUB command
126    ; then compares 2 full blocks against one full block and updates
127    ; sectors left to one full block and jumps to .ReadNextBlockFromDrive,
128    ; since we have one full block left to transfer. After it has been
129    ; transferred, there will be a wait for next block but DRQ is never
130    ; set since all is transferred! Then we get timeout error.
131    mov     cx, [bp+MEMPIOVARS.wSectorsInBlock]
132    sub     [bp+MEMPIOVARS.bSectorsLeft], cl
133    add     [bp+MEMPIOVARS.bSectorsDone], cl
134    cmp     [bp+MEMPIOVARS.bSectorsLeft], cl
135    ja      SHORT .ReadNextBlockFromDrive
136
137ALIGN JUMP_ALIGN
138.ReadLastBlockFromDrive:
139    mov     cl, [bp+MEMPIOVARS.bSectorsLeft]
140    push    cx
141    call    ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
142
143    ; Check for errors in last block
144CheckErrorsAfterTransferringLastMemoryMappedBlock:
145    lds     di, [bp+MEMPIOVARS.fpDPT]           ; DPT now in DS:DI
146    mov     bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_BSY)
147    call    IdeWait_PollStatusFlagInBLwithTimeoutInBH
148    pop     cx  ; [bp+MEMPIOVARS.bSectorsLeft]
149    jc      SHORT ReturnWithMemoryIOtransferErrorInAH
150
151    ; All sectors successfully transferred
152    add     cl, [bp+MEMPIOVARS.bSectorsDone]    ; Never sets CF
153    ret
154
155    ; Return number of successfully transferred sectors
156ReturnWithMemoryIOtransferErrorInAH:
157    lds     di, [bp+MEMPIOVARS.fpDPT]           ; DPT now in DS:DI
158%ifdef USE_386
159    movzx   cx, BYTE [bp+MEMPIOVARS.bSectorsDone]
160%else
161    mov     ch, 0                               ; Preserve CF
162    mov     cl, [bp+MEMPIOVARS.bSectorsDone]
163%endif
164    ret
165
166
167;--------------------------------------------------------------------
168; WriteToSectorAccessWindow
169;   Parameters:
170;       DS:DI:  Ptr to Sector Access Window
171;       ES:SI:  Normalized ptr to buffer containing data
172;       SS:BP:  Ptr to MEMPIOVARS
173;   Returns:
174;       DS:DI:  Ptr to DPT (in RAMVARS segment)
175;       AH:     BIOS Error code
176;       CX:     Number of successfully transferred sectors
177;       CF:     0 if transfer successful
178;               1 if any error
179;   Corrupts registers:
180;       AL, BX, DX, SI, ES
181;--------------------------------------------------------------------
182ALIGN JUMP_ALIGN
183WriteToSectorAccessWindow:
184    push    es
185    push    ds
186    pop     es      ; ES:DI = Sector Access Window (destination)
187    pop     ds      ; DS:SI = Ptr to source buffer
188
189    ; Always poll when writing first block (IRQs are generated for following blocks)
190    call    WaitUntilReadyToTransferNextBlock
191    jc      SHORT ReturnWithMemoryIOtransferErrorInAH
192
193    mov     cx, [bp+MEMPIOVARS.wSectorsInBlock]
194    cmp     [bp+MEMPIOVARS.bSectorsLeft], cl
195    jbe     SHORT .WriteLastBlockToDrive
196
197ALIGN JUMP_ALIGN
198.WriteNextBlockToDrive:
199    call    WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
200    call    WaitUntilReadyToTransferNextBlock
201    jc      SHORT ReturnWithMemoryIOtransferErrorInAH
202
203    ; Increment number of successfully written WORDs
204    mov     cx, [bp+MEMPIOVARS.wSectorsInBlock]
205    sub     [bp+MEMPIOVARS.bSectorsLeft], cl
206    add     [bp+MEMPIOVARS.bSectorsDone], cl
207    cmp     [bp+MEMPIOVARS.bSectorsLeft], cl
208    ja      SHORT .WriteNextBlockToDrive
209
210ALIGN JUMP_ALIGN
211.WriteLastBlockToDrive:
212    mov     cl, [bp+MEMPIOVARS.bSectorsLeft]
213    push    cx
214    ePUSH_T bx, CheckErrorsAfterTransferringLastMemoryMappedBlock
215    ; Fall to WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
216
217;--------------------------------------------------------------------
218; WriteSingleBlockFromDSSIToSectorAccessWindowInESDI
219;   Parameters:
220;       CX:     Number of sectors in block
221;       DS:SI:  Normalized ptr to source buffer
222;       ES:DI:  Ptr to Sector Access Window
223;   Returns:
224;       CX, DX: Zero
225;       SI:     Updated
226;   Corrupts registers:
227;       BX
228;--------------------------------------------------------------------
229ALIGN JUMP_ALIGN
230WriteSingleBlockFromDSSIToSectorAccessWindowInESDI:
231    mov     bx, di
232    mov     dx, cx
233    xor     cl, cl
234ALIGN JUMP_ALIGN
235.WriteNextSector:
236    mov     ch, SECTOR_ACCESS_WINDOW_SIZE >> 9
237    rep movsw
238    mov     di, bx  ; Reset for next sector
239    dec     dx
240    jnz     SHORT .WriteNextSector
241    ret
242
243
244;--------------------------------------------------------------------
245; ReadSingleBlockFromSectorAccessWindowInDSSItoESDI
246;   Parameters:
247;       CX      Number of sectors in full block or sectors in last partial block
248;       ES:DI:  Normalized ptr to buffer to receive data (destination)
249;       DS:SI:  Ptr to Sector Access Window (source)
250;   Returns:
251;       CX, DX: Zero
252;       DI:     Updated
253;   Corrupts registers:
254;       BX
255;--------------------------------------------------------------------
256ALIGN JUMP_ALIGN
257ReadSingleBlockFromSectorAccessWindowInDSSItoESDI:
258    mov     bx, si
259    mov     dx, cx
260    xor     cl, cl
261ALIGN JUMP_ALIGN
262.ReadNextSector:
263    mov     ch, SECTOR_ACCESS_WINDOW_SIZE >> 9
264    rep movsw
265    mov     si, bx  ; Reset for next sector
266    dec     dx
267    jnz     SHORT .ReadNextSector
268    ret
269
270
271;--------------------------------------------------------------------
272; WaitUntilReadyToTransferNextBlock
273;   Parameters:
274;       SS:BP:  Ptr to MEMPIOVARS
275;   Returns:
276;       AH:     INT 13h Error Code
277;       CF:     Cleared if success, Set if error
278;   Corrupts registers:
279;       AL, BX, CX, DX
280;--------------------------------------------------------------------
281ALIGN JUMP_ALIGN
282WaitUntilReadyToTransferNextBlock:
283    push    ds
284    push    di
285    lds     di, [bp+MEMPIOVARS.fpDPT]   ; DPT now in DS:DI
286    call    IdeWait_IRQorDRQ            ; Always polls
287    pop     di
288    pop     ds
289    ret
290
291
292%ifndef CHECK_FOR_UNUSED_ENTRYPOINTS       
293%if SECTOR_ACCESS_WINDOW_SIZE <> 512
294    %error "SECTOR_ACCESS_WINDOW_SIZE is no longer equal to 512. JrIdeTransfer.asm needs changes."
295%endif
296%endif
Note: See TracBrowser for help on using the repository browser.