source: xtideuniversalbios/trunk/XTIDE_Universal_BIOS/Src/Device/IDE/IdeTransfer.asm@ 232

Last change on this file since 232 was 223, checked in by krille_n_@…, 13 years ago

Changes:

  • Fixed what appears to be a mistake in EBIOS.inc
  • Added a 'release' option to the makefile of the Serial Server (invokes UPX)
  • Some very minor optimizations
  • Removed the eSEG macro and did some other cleanups (fixed typos etc)
File size: 11.1 KB
Line 
1; Project name : XTIDE Universal BIOS
2; Description : IDE Device transfer functions.
3
4; Structure containing variables for PIO transfer functions.
5; This struct must not be larger than IDEPACK without INTPACK.
6struc PIOVARS
7 .wWordsInBlock resb 2 ; 0, Block size in WORDs
8 .wWordsLeft resb 2 ; 2, WORDs left to transfer
9 .wWordsDone resb 2 ; 4, Number of sectors xferred
10 resb 1 ; 6,
11 resb 1 ; 7, IDEPACK.bDeviceControl
12 .wDataPort resb 2 ; 8, IDE Data Port
13 .fnXfer resb 2 ; 10, Offset to transfer function
14endstruc
15
16
17; Section containing code
18SECTION .text
19
20;--------------------------------------------------------------------
21; IdeTransfer_StartWithCommandInAL
22; Parameters:
23; AL: IDE command that was used to start the transfer
24; (all PIO read and write commands including Identify Device)
25; ES:SI: Ptr to normalized data buffer
26; DS:DI: Ptr to DPT (in RAMVARS segment)
27; SS:BP: Ptr to IDEPACK
28; Returns:
29; AH: INT 13h Error Code
30; CX: Number of successfully transferred sectors
31; CF: Cleared if success, Set if error
32; Corrupts registers:
33; AL, BX, DX, SI, ES
34;--------------------------------------------------------------------
35ALIGN JUMP_ALIGN
36IdeTransfer_StartWithCommandInAL:
37 mov ah, [bp+IDEPACK.bSectorCount]
38
39 ; Are we reading or writing?
40 test al, 16 ; Bit 4 is cleared on all the read commands but set on 3 of the 4 write commands
41 jnz SHORT .PrepareToWriteDataFromESSI
42 cmp al, COMMAND_WRITE_MULTIPLE
43 je SHORT .PrepareToWriteDataFromESSI
44
45 ; Prepare to read data to ESSI
46 mov bx, g_rgfnPioRead
47 call InitializePiovarsInSSBPwithSectorCountInAH
48 xchg si, di
49 jmp SHORT ReadFromDrive
50
51ALIGN JUMP_ALIGN
52.PrepareToWriteDataFromESSI:
53 mov bx, g_rgfnPioWrite
54 call InitializePiovarsInSSBPwithSectorCountInAH
55 ; Fall to WriteToDrive
56
57
58;--------------------------------------------------------------------
59; WriteToDrive
60; Parameters:
61; DS:DI: Ptr to DPT (in RAMVARS segment)
62; ES:SI: Normalized ptr to buffer containing data
63; SS:BP: Ptr to PIOVARS
64; Returns:
65; AH: BIOS Error code
66; CX: Number of successfully transferred sectors
67; CF: 0 if transfer succesfull
68; 1 if any error
69; Corrupts registers:
70; AL, BX, DX, SI, ES
71;--------------------------------------------------------------------
72WriteToDrive:
73 ; Always poll when writing first block (IRQs are generated for following blocks)
74 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ)
75 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
76 jc SHORT ReturnWithTransferErrorInAH
77
78ALIGN JUMP_ALIGN
79.WriteNextBlockToDrive:
80 mov cx, [bp+PIOVARS.wWordsInBlock]
81 mov dx, [bp+PIOVARS.wDataPort]
82 cmp [bp+PIOVARS.wWordsLeft], cx
83 jbe SHORT .WriteLastBlockToDrive
84 call [bp+PIOVARS.fnXfer]
85
86 ; Wait until ready for next block and check for errors
87 call IdeWait_IRQorDRQ ; Wait until ready to transfer
88 jc SHORT ReturnWithTransferErrorInAH
89
90 ; Increment number of successfully written WORDs
91 mov ax, [bp+PIOVARS.wWordsInBlock]
92 sub [bp+PIOVARS.wWordsLeft], ax
93 add [bp+PIOVARS.wWordsDone], ax
94 jmp SHORT .WriteNextBlockToDrive
95
96ALIGN JUMP_ALIGN
97.WriteLastBlockToDrive:
98 mov cx, [bp+PIOVARS.wWordsLeft]
99 call [bp+PIOVARS.fnXfer] ; Transfer possibly partial block
100
101 ; Check for errors in last block
102 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRDY)
103 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
104 jmp SHORT ReturnWithTransferErrorInAH
105
106
107;--------------------------------------------------------------------
108; ReadFromDrive
109; Parameters:
110; ES:DI: Normalized ptr to buffer to recieve data
111; DS:SI: Ptr to DPT (in RAMVARS segment)
112; SS:BP: Ptr to PIOVARS
113; Returns:
114; DS:DI: Ptr to DPT (in RAMVARS segment)
115; AH: BIOS Error code
116; CX: Number of successfully transferred sectors
117; CF: 0 if transfer succesfull
118; 1 if any error
119; Corrupts registers:
120; AL, BX, DX, SI, ES
121;--------------------------------------------------------------------
122ALIGN JUMP_ALIGN
123ReadFromDrive:
124 ; Wait until drive is ready to transfer
125 xchg di, si ; DS:DI now points DPT
126 call IdeWait_IRQorDRQ ; Wait until ready to transfer
127 jc SHORT ReturnWithTransferErrorInAH
128 xchg si, di ; ES:DI now points buffer
129
130ALIGN JUMP_ALIGN
131.ReadNextBlockFromDrive:
132 mov cx, [bp+PIOVARS.wWordsInBlock]
133 mov dx, [bp+PIOVARS.wDataPort]
134 cmp [bp+PIOVARS.wWordsLeft], cx
135 jbe SHORT .ReadLastBlockFromDrive
136 call [bp+PIOVARS.fnXfer]
137
138 ; Wait until ready for next block and check for errors
139 xchg di, si ; DS:DI now points DPT
140 call IdeWait_IRQorDRQ ; Wait until ready to transfer
141 jc SHORT ReturnWithTransferErrorInAH
142 xchg si, di ; ES:DI now points buffer
143
144 ; Increment number of successfully read WORDs
145 mov ax, [bp+PIOVARS.wWordsInBlock]
146 sub [bp+PIOVARS.wWordsLeft], ax
147 add [bp+PIOVARS.wWordsDone], ax
148 jmp SHORT .ReadNextBlockFromDrive
149
150ALIGN JUMP_ALIGN
151.ReadLastBlockFromDrive:
152 mov cx, [bp+PIOVARS.wWordsLeft]
153 call [bp+PIOVARS.fnXfer] ; Transfer possibly partial block
154
155 ; Check for errors in last block
156 mov di, si ; DS:DI now points DPT
157 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRDY)
158 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
159
160 ; Return number of successfully read sectors
161ReturnWithTransferErrorInAH:
162 mov cx, [bp+PIOVARS.wWordsDone]
163 jc SHORT .ConvertTransferredWordsInCXtoSectors
164 add cx, [bp+PIOVARS.wWordsLeft] ; Never sets CF
165.ConvertTransferredWordsInCXtoSectors:
166 xchg cl, ch
167 ret
168
169
170;--------------------------------------------------------------------
171; InitializePiovarsInSSBPwithSectorCountInAH
172; Parameters:
173; AH: Number of sectors to transfer (1...128)
174; BX: Offset to transfer function lookup table
175; DS:DI: Ptr to DPT (in RAMVARS segment)
176; SS:BP: Ptr to PIOVARS
177; Returns:
178; Nothing
179; Corrupts registers:
180; AX, BX, CX, DX
181;--------------------------------------------------------------------
182ALIGN JUMP_ALIGN
183InitializePiovarsInSSBPwithSectorCountInAH:
184 ; Store sizes
185 xor al, al
186 mov [bp+PIOVARS.wWordsLeft], ax
187 cbw
188 mov [bp+PIOVARS.wWordsDone], ax ; Zero
189 mov ah, [di+DPT_ATA.bSetBlock]
190 mov [bp+PIOVARS.wWordsInBlock], ax
191
192 ; Get transfer function based on bus type
193 xchg ax, bx ; Lookup table offset to AX
194 eMOVZX bx, BYTE [di+DPT.bIdevarsOffset] ; CS:BX now points to IDEVARS
195 mov dx, [cs:bx+IDEVARS.wPort] ; Load IDE Data port address
196 mov bl, [cs:bx+IDEVARS.bDevice] ; Load device type to BX
197 add bx, ax
198 mov ax, [cs:bx] ; Load offset to transfer function
199 mov [bp+PIOVARS.wDataPort], dx
200 mov [bp+PIOVARS.fnXfer], ax
201 ret
202
203
204;--------------------------------------------------------------------
205; DualByteReadForXtide Dual port 8-bit XTIDE PIO read transfer
206; SingleByteRead Single port 8-bit PIO read transfer
207; WordReadForXTIDEmod 8088/8086 compatible 16-bit IDE PIO read transfer
208; WordReadForXTplusAndAT Normal 16-bit IDE PIO read transfer
209; DWordRead VLB/PCI 32-bit IDE PIO read transfer
210; Parameters:
211; CX: Block size in WORDs
212; DX: IDE Data port address
213; ES:DI: Normalized ptr to buffer to recieve data
214; Returns:
215; Nothing
216; Corrupts registers:
217; AX, BX, CX
218;--------------------------------------------------------------------
219ALIGN JUMP_ALIGN
220DualByteReadForXtide:
221 eSHR_IM cx, 2 ; Loop unrolling
222 mov bx, 8 ; Bit mask for toggling data low/high reg
223ALIGN JUMP_ALIGN
224.InswLoop:
225 XTIDE_INSW
226 XTIDE_INSW
227 XTIDE_INSW
228 XTIDE_INSW
229 loop .InswLoop
230 ret
231
232;----
233ALIGN JUMP_ALIGN
234SingleByteRead:
235%ifdef USE_186 ; INS instruction available
236 shl cx, 1 ; WORD count to BYTE count
237 rep insb
238%else ; If 8088/8086
239 shr cx, 1 ; WORD count to DWORD count
240ALIGN JUMP_ALIGN
241.InsdLoop:
242 in al, dx
243 stosb ; Store to [ES:DI]
244 in al, dx
245 stosb
246 in al, dx
247 stosb
248 in al, dx
249 stosb
250 loop .InsdLoop
251%endif
252 ret
253
254;----
255%ifndef USE_186
256ALIGN JUMP_ALIGN
257WordReadForXTIDEmod:
258 times 2 shr cx, 1 ; WORD count to QWORD count
259ALIGN JUMP_ALIGN
260.ReadNextQword:
261 in ax, dx ; Read 1st WORD
262 stosw ; Store 1st WORD to [ES:DI]
263 in ax, dx
264 stosw ; 2nd
265 in ax, dx
266 stosw ; 3rd
267 in ax, dx
268 stosw ; 4th
269 loop .ReadNextQword
270 ret
271%endif
272
273;----
274ALIGN JUMP_ALIGN
275WordReadForXTplusAndAT:
276 rep
277 db 6Dh ; INSW (we want this in XT build)
278 ret
279
280;----
281ALIGN JUMP_ALIGN
282DWordRead:
283 shr cx, 1 ; WORD count to DWORD count
284 rep
285 db 66h ; Override operand size to 32-bit
286 db 6Dh ; INSW/INSD
287 ret
288
289
290;--------------------------------------------------------------------
291; DualByteWriteForXtide Dual port 8-bit XTIDE PIO write transfer
292; SingleByteWrite Single port 8-bit PIO write transfer
293; WordWriteForXTIDEmod 8088/8086 compatible 16-bit IDE PIO read transfer
294; WordWrite Normal 16-bit IDE PIO write transfer
295; DWordWrite VLB/PCI 32-bit IDE PIO write transfer
296; Parameters:
297; CX: Block size in WORDs
298; DX: IDE Data port address
299; ES:SI: Normalized ptr to buffer containing data
300; Returns:
301; Nothing
302; Corrupts registers:
303; AX, CX
304;--------------------------------------------------------------------
305ALIGN JUMP_ALIGN
306DualByteWriteForXtide:
307 push ds
308 push bx
309 eSHR_IM cx, 2 ; Loop unrolling
310 mov bx, 8 ; Bit mask for toggling data low/high reg
311 push es ; Copy ES...
312 pop ds ; ...to DS
313ALIGN JUMP_ALIGN
314.OutswLoop:
315 XTIDE_OUTSW
316 XTIDE_OUTSW
317 XTIDE_OUTSW
318 XTIDE_OUTSW
319 loop .OutswLoop
320 pop bx
321 pop ds
322 ret
323
324;----
325ALIGN JUMP_ALIGN
326SingleByteWrite:
327%ifdef USE_186 ; OUTS instruction available
328 shl cx, 1 ; WORD count to BYTE count
329 es ; Source is ES segment
330 rep outsb
331%else ; If 8088/8086
332 shr cx, 1 ; WORD count to DWORD count
333 push ds ; Store DS
334 push es ; Copy ES...
335 pop ds ; ...to DS
336ALIGN JUMP_ALIGN
337.OutsdLoop:
338 lodsb ; Load from [DS:SI] to AL
339 out dx, al
340 lodsb
341 out dx, al
342 lodsb
343 out dx, al
344 lodsb
345 out dx, al
346 loop .OutsdLoop
347 pop ds ; Restore DS
348%endif
349 ret
350
351;---
352ALIGN JUMP_ALIGN
353WordWriteForXTIDEmod:
354 push ds
355 eSHR_IM cx, 2 ; Loop unrolling
356 push es ; Copy ES...
357 pop ds ; ...to DS
358ALIGN JUMP_ALIGN
359.WriteNextQword:
360 XTIDE_MOD_OUTSW
361 XTIDE_MOD_OUTSW
362 XTIDE_MOD_OUTSW
363 XTIDE_MOD_OUTSW
364 loop .WriteNextQword
365 pop ds
366 ret
367
368;----
369ALIGN JUMP_ALIGN
370WordWrite:
371 es ; Source is ES segment
372 rep
373 db 6Fh ; OUTSW (we want this in XT build)
374 ret
375
376ALIGN JUMP_ALIGN
377DWordWrite:
378 shr cx, 1 ; WORD count to DWORD count
379 es ; Source is ES segment
380 rep
381 db 66h ; Override operand size to 32-bit
382 db 6Fh ; OUTSW/OUTSD
383 ret
384
385
386
387; Lookup tables to get transfer function based on bus type
388ALIGN WORD_ALIGN
389g_rgfnPioRead:
390 dw DualByteReadForXtide ; DEVICE_8BIT_DUAL_PORT_XTIDE
391%ifdef USE_186
392 dw WordReadForXTplusAndAT ; DEVICE_XTIDE_WITH_REVERSED_A3_AND_A0
393%else
394 dw WordReadForXTIDEmod
395%endif
396 dw SingleByteRead ; DEVICE_8BIT_SINGLE_PORT
397 dw WordReadForXTplusAndAT ; DEVICE_16BIT_ATA
398 dw DWordRead ; DEVICE_32BIT_ATA
399
400g_rgfnPioWrite:
401 dw DualByteWriteForXtide ; DEVICE_8BIT_DUAL_PORT_XTIDE
402 dw WordWriteForXTIDEmod ; DEVICE_XTIDE_WITH_REVERSED_A3_AND_A0
403 dw SingleByteWrite ; DEVICE_8BIT_SINGLE_PORT
404 dw WordWrite ; DEVICE_16BIT_ATA
405 dw DWordWrite ; DEVICE_32BIT_ATA
Note: See TracBrowser for help on using the repository browser.