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

Last change on this file since 362 was 361, checked in by aitotat@…, 13 years ago

Changes to XTIDE Universal BIOS:

  • Added untested support for Fast XTIDE (CPLD v2 project).
  • Removed 8-bit single port device since there does not seem to be any need for one.
File size: 11.2 KB
RevLine 
[150]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
[218]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
[223]10 resb 1 ; 6,
[218]11 resb 1 ; 7, IDEPACK.bDeviceControl
12 .wDataPort resb 2 ; 8, IDE Data Port
13 .fnXfer resb 2 ; 10, Offset to transfer function
[150]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
[171]24; (all PIO read and write commands including Identify Device)
[218]25; ES:SI: Ptr to normalized data buffer
[150]26; DS:DI: Ptr to DPT (in RAMVARS segment)
27; SS:BP: Ptr to IDEPACK
28; Returns:
29; AH: INT 13h Error Code
[218]30; CX: Number of successfully transferred sectors
[150]31; CF: Cleared if success, Set if error
32; Corrupts registers:
[218]33; AL, BX, DX, SI, ES
[150]34;--------------------------------------------------------------------
35ALIGN JUMP_ALIGN
36IdeTransfer_StartWithCommandInAL:
[157]37 ; Are we reading or writing?
[171]38 test al, 16 ; Bit 4 is cleared on all the read commands but set on 3 of the 4 write commands
[242]39 mov ah, [bp+IDEPACK.bSectorCount]
40 jnz SHORT WriteToDrive
[171]41 cmp al, COMMAND_WRITE_MULTIPLE
[242]42 je SHORT WriteToDrive
43 ; Fall to ReadFromDrive
[150]44
45;--------------------------------------------------------------------
[242]46; ReadFromDrive
[150]47; Parameters:
[242]48; AH: Number of sectors to transfer (1...128)
49; ES:SI: Normalized ptr to buffer to receive data
[150]50; DS:DI: Ptr to DPT (in RAMVARS segment)
51; SS:BP: Ptr to PIOVARS
52; Returns:
[242]53; DS:DI: Ptr to DPT (in RAMVARS segment)
[150]54; AH: BIOS Error code
[218]55; CX: Number of successfully transferred sectors
[294]56; CF: 0 if transfer successful
[150]57; 1 if any error
58; Corrupts registers:
[218]59; AL, BX, DX, SI, ES
[150]60;--------------------------------------------------------------------
[242]61ReadFromDrive:
62 ; Prepare to read data to ESSI
63 mov bx, g_rgfnPioRead
64 call InitializePiovarsInSSBPwithSectorCountInAH
65
66 ; Wait until drive is ready to transfer
67 call IdeWait_IRQorDRQ ; Wait until ready to transfer
[218]68 jc SHORT ReturnWithTransferErrorInAH
[242]69 xchg si, di ; ES:DI now points buffer
[218]70
[242]71 mov cx, [bp+PIOVARS.wWordsInBlock]
72
[150]73ALIGN JUMP_ALIGN
[242]74.ReadNextBlockFromDrive:
[150]75 mov dx, [bp+PIOVARS.wDataPort]
[218]76 cmp [bp+PIOVARS.wWordsLeft], cx
[242]77 jbe SHORT .ReadLastBlockFromDrive
[218]78 call [bp+PIOVARS.fnXfer]
[169]79
[218]80 ; Wait until ready for next block and check for errors
[242]81 xchg di, si ; DS:DI now points DPT
[218]82 call IdeWait_IRQorDRQ ; Wait until ready to transfer
83 jc SHORT ReturnWithTransferErrorInAH
[242]84 xchg si, di ; ES:DI now points buffer
[169]85
[242]86 ; Increment number of successfully read WORDs
87 mov cx, [bp+PIOVARS.wWordsInBlock]
88 sub [bp+PIOVARS.wWordsLeft], cx
89 add [bp+PIOVARS.wWordsDone], cx
90 jmp SHORT .ReadNextBlockFromDrive
[150]91
92ALIGN JUMP_ALIGN
[242]93.ReadLastBlockFromDrive:
[218]94 mov cx, [bp+PIOVARS.wWordsLeft]
[150]95 call [bp+PIOVARS.fnXfer] ; Transfer possibly partial block
96
[242]97 ; Check for errors in last block
98 mov di, si ; DS:DI now points DPT
99CheckErrorsAfterTransferringLastBlock:
100 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRDY)
101 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
[150]102
[242]103 ; Return number of successfully read sectors
104ReturnWithTransferErrorInAH:
105 mov cx, [bp+PIOVARS.wWordsDone]
106 jc SHORT .ConvertTransferredWordsInCXtoSectors
107 add cx, [bp+PIOVARS.wWordsLeft] ; Never sets CF
108.ConvertTransferredWordsInCXtoSectors:
109 xchg cl, ch
110 ret
111
112
[150]113;--------------------------------------------------------------------
[242]114; WriteToDrive
[150]115; Parameters:
[242]116; AH: Number of sectors to transfer (1...128)
117; DS:DI: Ptr to DPT (in RAMVARS segment)
118; ES:SI: Normalized ptr to buffer containing data
[150]119; SS:BP: Ptr to PIOVARS
120; Returns:
121; AH: BIOS Error code
[218]122; CX: Number of successfully transferred sectors
[294]123; CF: 0 if transfer successful
[150]124; 1 if any error
125; Corrupts registers:
[218]126; AL, BX, DX, SI, ES
[150]127;--------------------------------------------------------------------
128ALIGN JUMP_ALIGN
[242]129WriteToDrive:
130 ; Prepare to write data from ESSI
131 mov bx, g_rgfnPioWrite
132 call InitializePiovarsInSSBPwithSectorCountInAH
133
134 ; Always poll when writing first block (IRQs are generated for following blocks)
135 mov bx, TIMEOUT_AND_STATUS_TO_WAIT(TIMEOUT_DRQ, FLG_STATUS_DRQ)
136 call IdeWait_PollStatusFlagInBLwithTimeoutInBH
[218]137 jc SHORT ReturnWithTransferErrorInAH
[150]138
[242]139 mov cx, [bp+PIOVARS.wWordsInBlock]
140
[218]141ALIGN JUMP_ALIGN
[242]142.WriteNextBlockToDrive:
[150]143 mov dx, [bp+PIOVARS.wDataPort]
[218]144 cmp [bp+PIOVARS.wWordsLeft], cx
[242]145 jbe SHORT .WriteLastBlockToDrive
[218]146 call [bp+PIOVARS.fnXfer]
[169]147
[218]148 ; Wait until ready for next block and check for errors
149 call IdeWait_IRQorDRQ ; Wait until ready to transfer
150 jc SHORT ReturnWithTransferErrorInAH
[150]151
[242]152 ; Increment number of successfully written WORDs
153 mov cx, [bp+PIOVARS.wWordsInBlock]
154 sub [bp+PIOVARS.wWordsLeft], cx
155 add [bp+PIOVARS.wWordsDone], cx
156 jmp SHORT .WriteNextBlockToDrive
[169]157
[150]158ALIGN JUMP_ALIGN
[242]159.WriteLastBlockToDrive:
[218]160 mov cx, [bp+PIOVARS.wWordsLeft]
[242]161%ifdef USE_186
162 push CheckErrorsAfterTransferringLastBlock
163 jmp [bp+PIOVARS.fnXfer] ; Transfer possibly partial block
164%else
[150]165 call [bp+PIOVARS.fnXfer] ; Transfer possibly partial block
[242]166 jmp SHORT CheckErrorsAfterTransferringLastBlock
167%endif
[218]168
[150]169
170;--------------------------------------------------------------------
[218]171; InitializePiovarsInSSBPwithSectorCountInAH
[150]172; Parameters:
[218]173; AH: Number of sectors to transfer (1...128)
[150]174; BX: Offset to transfer function lookup table
175; DS:DI: Ptr to DPT (in RAMVARS segment)
[169]176; SS:BP: Ptr to PIOVARS
[167]177; Returns:
[169]178; Nothing
[150]179; Corrupts registers:
[242]180; AX, BX, DX
[150]181;--------------------------------------------------------------------
182ALIGN JUMP_ALIGN
[218]183InitializePiovarsInSSBPwithSectorCountInAH:
184 ; Store sizes
185 xor al, al
186 mov [bp+PIOVARS.wWordsLeft], ax
[242]187 mov ah, [di+DPT_ATA.bSetBlock]
188 mov [bp+PIOVARS.wWordsInBlock], ax
[218]189 cbw
190 mov [bp+PIOVARS.wWordsDone], ax ; Zero
[150]191
192 ; Get transfer function based on bus type
[158]193 xchg ax, bx ; Lookup table offset to AX
[242]194 mov bl, [di+DPT.bIdevarsOffset] ; CS:BX now points to IDEVARS
[150]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
[242]198 mov [bp+PIOVARS.wDataPort], dx
[150]199 mov ax, [cs:bx] ; Load offset to transfer function
200 mov [bp+PIOVARS.fnXfer], ax
201 ret
202
203
204;--------------------------------------------------------------------
[361]205; ReadBlockFromXtideRev1 XTIDE rev 1
206; ReadBlockFromXtideRev2 XTIDE rev 2 or rev 1 with swapped A0 and A3 (chuck-mod)
207; ReadBlockFrom16bitDataPort Normal 16-bit IDE
208; ReadBlockFrom32bitDataPort VLB/PCI 32-bit IDE
[150]209; Parameters:
210; CX: Block size in WORDs
211; DX: IDE Data port address
[242]212; ES:DI: Normalized ptr to buffer to receive data
[150]213; Returns:
214; Nothing
215; Corrupts registers:
216; AX, BX, CX
217;--------------------------------------------------------------------
218ALIGN JUMP_ALIGN
[361]219ReadBlockFromXtideRev1:
[181]220 eSHR_IM cx, 2 ; Loop unrolling
[152]221 mov bx, 8 ; Bit mask for toggling data low/high reg
[150]222ALIGN JUMP_ALIGN
223.InswLoop:
[152]224 XTIDE_INSW
225 XTIDE_INSW
226 XTIDE_INSW
227 XTIDE_INSW
[150]228 loop .InswLoop
229 ret
230
[361]231;--------------------------------------------------------------------
232%ifndef USE_186 ; 8086/8088 compatible WORD read
[150]233ALIGN JUMP_ALIGN
[361]234ReadBlockFromXtideRev2:
[152]235 times 2 shr cx, 1 ; WORD count to QWORD count
236ALIGN JUMP_ALIGN
237.ReadNextQword:
238 in ax, dx ; Read 1st WORD
239 stosw ; Store 1st WORD to [ES:DI]
240 in ax, dx
241 stosw ; 2nd
242 in ax, dx
243 stosw ; 3rd
244 in ax, dx
245 stosw ; 4th
246 loop .ReadNextQword
247 ret
248%endif
[150]249
[361]250;--------------------------------------------------------------------
[152]251ALIGN JUMP_ALIGN
[361]252ReadBlockFrom16bitDataPort:
[152]253 rep
254 db 6Dh ; INSW (we want this in XT build)
255 ret
256
[361]257;--------------------------------------------------------------------
[152]258ALIGN JUMP_ALIGN
[361]259ReadBlockFrom32bitDataPort:
[152]260 shr cx, 1 ; WORD count to DWORD count
261 rep
262 db 66h ; Override operand size to 32-bit
263 db 6Dh ; INSW/INSD
264 ret
265
266
[150]267;--------------------------------------------------------------------
[361]268; WriteBlockToXtideRev1 XTIDE rev 1
269; WriteBlockToXtideRev2 XTIDE rev 2 or rev 1 with swapped A0 and A3 (chuck-mod)
270; WriteBlockToFastXtide Fast XTIDE (CPLD v2 project)
271; WriteBlockTo16bitDataPort Normal 16-bit IDE
272; WriteBlockTo32bitDataPort VLB/PCI 32-bit IDE
[150]273; Parameters:
274; CX: Block size in WORDs
275; DX: IDE Data port address
276; ES:SI: Normalized ptr to buffer containing data
277; Returns:
278; Nothing
279; Corrupts registers:
280; AX, CX
281;--------------------------------------------------------------------
282ALIGN JUMP_ALIGN
[361]283WriteBlockToXtideRev1:
[150]284 push ds
285 push bx
[181]286 eSHR_IM cx, 2 ; Loop unrolling
[152]287 mov bx, 8 ; Bit mask for toggling data low/high reg
288 push es ; Copy ES...
289 pop ds ; ...to DS
[150]290ALIGN JUMP_ALIGN
291.OutswLoop:
[152]292 XTIDE_OUTSW
293 XTIDE_OUTSW
294 XTIDE_OUTSW
295 XTIDE_OUTSW
[150]296 loop .OutswLoop
297 pop bx
298 pop ds
299 ret
300
[361]301;--------------------------------------------------------------------
[150]302ALIGN JUMP_ALIGN
[361]303WriteBlockToXtideRev2:
[152]304 push ds
[181]305 eSHR_IM cx, 2 ; Loop unrolling
[152]306 push es ; Copy ES...
307 pop ds ; ...to DS
308ALIGN JUMP_ALIGN
309.WriteNextQword:
310 XTIDE_MOD_OUTSW
311 XTIDE_MOD_OUTSW
312 XTIDE_MOD_OUTSW
313 XTIDE_MOD_OUTSW
314 loop .WriteNextQword
315 pop ds
316 ret
[150]317
[361]318;--------------------------------------------------------------------
319%ifndef USE_186 ; 8086/8088 compatible WORD write
[152]320ALIGN JUMP_ALIGN
[361]321WriteBlockToFastXtide:
322 times 2 shr cx, 1 ; WORD count to QWORD count
323 push ds
324 push es
325 pop ds
326ALIGN JUMP_ALIGN
327.ReadNextQword:
328 lodsw ; Load 1st WORD from [DS:SI]
329 out dx, ax ; Write 1st WORD
330 lodsw
331 out dx, ax ; 2nd
332 lodsw
333 out dx, ax ; 3rd
334 lodsw
335 out dx, ax ; 4th
336 loop .ReadNextQword
337 pop ds
338 ret
339%endif
340
341;--------------------------------------------------------------------
342ALIGN JUMP_ALIGN
343WriteBlockTo16bitDataPort:
[223]344 es ; Source is ES segment
[152]345 rep
[155]346 db 6Fh ; OUTSW (we want this in XT build)
[152]347 ret
348
[361]349;--------------------------------------------------------------------
[152]350ALIGN JUMP_ALIGN
[361]351WriteBlockTo32bitDataPort:
[152]352 shr cx, 1 ; WORD count to DWORD count
[223]353 es ; Source is ES segment
[152]354 rep
355 db 66h ; Override operand size to 32-bit
356 db 6Fh ; OUTSW/OUTSD
357 ret
358
359
[361]360
[150]361; Lookup tables to get transfer function based on bus type
362ALIGN WORD_ALIGN
363g_rgfnPioRead:
[361]364 dw ReadBlockFromXtideRev1 ; DEVICE_XTIDE_REV1
[152]365%ifdef USE_186
[361]366 dw ReadBlockFrom16bitDataPort ; DEVICE_XTIDE_REV2
367 dw ReadBlockFrom16bitDataPort ; DEVICE_FAST_XTIDE
[152]368%else
[361]369 dw ReadBlockFromXtideRev2 ; DEVICE_XTIDE_REV2
370 dw ReadBlockFromXtideRev2 ; DEVICE_FAST_XTIDE
[152]371%endif
[361]372 dw ReadBlockFrom16bitDataPort ; DEVICE_16BIT_ATA
373 dw ReadBlockFrom32bitDataPort ; DEVICE_32BIT_ATA
[152]374
[150]375g_rgfnPioWrite:
[361]376 dw WriteBlockToXtideRev1 ; DEVICE_XTIDE_REV1
377 dw WriteBlockToXtideRev2 ; DEVICE_XTIDE_REV2
378%ifdef USE_186
379 dw WriteBlockTo16bitDataPort ; DEVICE_FAST_XTIDE
380%else
381 dw WriteBlockToFastXtide ; DEVICE_FAST_XTIDE
382%endif
383 dw WriteBlockTo16bitDataPort ; DEVICE_16BIT_ATA
384 dw WriteBlockTo32bitDataPort ; DEVICE_32BIT_ATA
Note: See TracBrowser for help on using the repository browser.