[480] | 1 | ; Project name : XTIDE Universal BIOS
|
---|
[558] | 2 | ; Description : IDE Read/Write functions for transferring block using DMA.
|
---|
| 3 | ; These functions should only be called from IdeTransfer.asm.
|
---|
| 4 |
|
---|
[480] | 5 | ;
|
---|
| 6 | ; XTIDE Universal BIOS and Associated Tools
|
---|
[526] | 7 | ; Copyright (C) 2009-2010 by Tomi Tilli, 2011-2013 by XTIDE Universal BIOS Team.
|
---|
[480] | 8 | ;
|
---|
| 9 | ; This program is free software; you can redistribute it and/or modify
|
---|
| 10 | ; it under the terms of the GNU General Public License as published by
|
---|
| 11 | ; the Free Software Foundation; either version 2 of the License, or
|
---|
| 12 | ; (at your option) any later version.
|
---|
| 13 | ;
|
---|
| 14 | ; This program is distributed in the hope that it will be useful,
|
---|
| 15 | ; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 16 | ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 17 | ; GNU General Public License for more details.
|
---|
| 18 | ; Visit http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
|
---|
| 19 | ;
|
---|
| 20 |
|
---|
| 21 | ; Section containing code
|
---|
| 22 | SECTION .text
|
---|
| 23 |
|
---|
| 24 | ;--------------------------------------------------------------------
|
---|
| 25 | ; IdeDmaBlock_WriteToXTCF
|
---|
| 26 | ; Parameters:
|
---|
| 27 | ; CX: Block size in 512 byte sectors
|
---|
| 28 | ; DX: XTCF Base Port Address
|
---|
| 29 | ; ES:SI: Physical address to buffer to receive data
|
---|
| 30 | ; Returns:
|
---|
| 31 | ; Nothing
|
---|
| 32 | ; Corrupts registers:
|
---|
| 33 | ; AX, BX, CX, DX
|
---|
| 34 | ;--------------------------------------------------------------------
|
---|
| 35 | ALIGN JUMP_ALIGN
|
---|
| 36 | IdeDmaBlock_WriteToXTCF:
|
---|
| 37 | xchg si, di
|
---|
| 38 | mov bl, CHANNEL_3 | READ | AUTOINIT_DISABLE | ADDRESS_INCREMENT | DEMAND_MODE
|
---|
| 39 | call TransferBlockToOrFromXTCF
|
---|
| 40 | xchg di, si
|
---|
| 41 | ret
|
---|
| 42 |
|
---|
| 43 |
|
---|
| 44 | ;--------------------------------------------------------------------
|
---|
| 45 | ; IdeDmaBlock_ReadFromXTCF
|
---|
| 46 | ; Parameters:
|
---|
| 47 | ; CX: Block size in 512 byte sectors
|
---|
| 48 | ; DX: XTCF Base Port Address
|
---|
| 49 | ; ES:DI: Physical address to buffer to receive data
|
---|
| 50 | ; Returns:
|
---|
| 51 | ; Nothing
|
---|
| 52 | ; Corrupts registers:
|
---|
| 53 | ; AX, BX, CX, DX
|
---|
| 54 | ;--------------------------------------------------------------------
|
---|
| 55 | ALIGN JUMP_ALIGN
|
---|
| 56 | IdeDmaBlock_ReadFromXTCF:
|
---|
| 57 | mov bl, CHANNEL_3 | WRITE | AUTOINIT_DISABLE | ADDRESS_INCREMENT | DEMAND_MODE
|
---|
| 58 | ; Fall to TransferBlockToOrFromXTCF
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | ;--------------------------------------------------------------------
|
---|
| 62 | ; TransferBlockToOrFromXTCF
|
---|
| 63 | ; Parameters:
|
---|
| 64 | ; BL: Mode byte for DMA Mode Register
|
---|
| 65 | ; CX: Block size in 512 byte sectors
|
---|
| 66 | ; DX: XTCF Base Port Address
|
---|
| 67 | ; ES:DI: Physical address to buffer to receive data
|
---|
| 68 | ; Returns:
|
---|
| 69 | ; Nothing
|
---|
| 70 | ; Corrupts registers:
|
---|
| 71 | ; AX, BX, CX, DX
|
---|
| 72 | ;--------------------------------------------------------------------
|
---|
| 73 | TransferBlockToOrFromXTCF:
|
---|
[491] | 74 | ; 8-bit DMA transfers must be done within 64k physical page.
|
---|
[486] | 75 | ; XT-CF support maximum of 64 sector (32768 bytes) blocks in DMA mode
|
---|
| 76 | ; so we never need to separate transfer to more than 2 separate DMA operations.
|
---|
[480] | 77 |
|
---|
[545] | 78 | ; Load XT-CFv3 Control Register port to DX
|
---|
[480] | 79 | add dl, XTCF_CONTROL_REGISTER
|
---|
| 80 |
|
---|
[551] | 81 | ; convert sectors in CX to BYTES
|
---|
[480] | 82 | %ifdef USE_186
|
---|
| 83 | shl cx, 9 ; CX = Block size in BYTEs
|
---|
| 84 | %else
|
---|
[558] | 85 | xchg cl, ch
|
---|
[480] | 86 | shl cx, 1
|
---|
| 87 | %endif
|
---|
[551] | 88 |
|
---|
| 89 | ; Calculate bytes for first page
|
---|
| 90 | mov ax, di
|
---|
[567] | 91 | neg ax ; 2s complement
|
---|
[551] | 92 |
|
---|
[558] | 93 | ; If DI was zero carry flag will be cleared (and set otherwise)
|
---|
[551] | 94 | ; When DI is zero only one transfer is required since we've limited the
|
---|
| 95 | ; XT-CFv3 block size to 32k
|
---|
[558] | 96 | jnc SHORT .TransferLastDmaPageWithSizeInCX
|
---|
[551] | 97 |
|
---|
| 98 | ; CF was set, so DI != 0 and we might need one or two transfers
|
---|
[545] | 99 | cmp cx, ax ; if we won't cross a physical page boundary...
|
---|
[558] | 100 | jbe SHORT .TransferLastDmaPageWithSizeInCX ; ...perform the transfer in one operation
|
---|
[480] | 101 |
|
---|
[545] | 102 | ; Calculate how much we can transfer on first and second rounds
|
---|
[558] | 103 | xchg cx, ax ; CX = BYTEs for first page
|
---|
| 104 | sub ax, cx ; AX = BYTEs for second page
|
---|
| 105 | push ax ; Save bytes for second transfer on stack
|
---|
[480] | 106 |
|
---|
| 107 | ; Transfer first DMA page
|
---|
| 108 | call StartDMAtransferForXTCFwithDmaModeInBL
|
---|
| 109 | pop cx ; Pop size for second DMA page
|
---|
| 110 |
|
---|
| 111 | .TransferLastDmaPageWithSizeInCX:
|
---|
| 112 | ; Fall to StartDMAtransferForXTCFwithDmaModeInBL
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | ;--------------------------------------------------------------------
|
---|
| 116 | ; StartDMAtransferForXTCFwithDmaModeInBL
|
---|
[545] | 117 | ; Updated for XT-CFv3, 11-Apr-13
|
---|
[480] | 118 | ; Parameters:
|
---|
[482] | 119 | ; BL: Byte for DMA Mode Register
|
---|
[551] | 120 | ; CX: Number of BYTEs to transfer (1...32768 since max block size is limited to 64)
|
---|
[545] | 121 | ; DX: XT-CFv3 Control Register
|
---|
| 122 | ; ES: Bits 3..0 have physical address bits 19..16
|
---|
| 123 | ; DI: Physical address bits 15..0
|
---|
[480] | 124 | ; Returns:
|
---|
[545] | 125 | ; ES:DI updated (CX is added)
|
---|
[480] | 126 | ; Corrupts registers:
|
---|
[558] | 127 | ; AX, CX
|
---|
[480] | 128 | ;--------------------------------------------------------------------
|
---|
| 129 | ALIGN JUMP_ALIGN
|
---|
| 130 | StartDMAtransferForXTCFwithDmaModeInBL:
|
---|
| 131 | ; Program 8-bit DMA Controller
|
---|
| 132 | ; Disable Interrupts and DMA Channel 3 during DMA setup
|
---|
| 133 | mov al, SET_CH3_MASK_BIT
|
---|
[545] | 134 | cli ; Disable interrupts - programming must be atomic
|
---|
| 135 | out MASK_REGISTER_DMA8_out, al ; Disable DMA Channel 3
|
---|
[480] | 136 |
|
---|
| 137 | ; Set DMA Mode (read or write using channel 3)
|
---|
| 138 | mov al, bl
|
---|
| 139 | out MODE_REGISTER_DMA8_out, al
|
---|
| 140 |
|
---|
[545] | 141 | ; Send start address to DMA controller
|
---|
[480] | 142 | mov ax, es
|
---|
| 143 | out PAGE_DMA8_CH_3, al
|
---|
| 144 | mov ax, di
|
---|
[545] | 145 | out CLEAR_FLIPFLOP_DMA8_out, al ; Reset flip-flop to low byte
|
---|
[480] | 146 | out BASE_AND_CURRENT_ADDRESS_REGISTER_DMA8_CH3_out, al ; Low byte
|
---|
| 147 | mov al, ah
|
---|
| 148 | out BASE_AND_CURRENT_ADDRESS_REGISTER_DMA8_CH3_out, al ; High byte
|
---|
| 149 |
|
---|
| 150 | ; Set number of bytes to transfer (DMA controller must be programmed number of bytes - 1)
|
---|
| 151 | mov ax, cx
|
---|
[545] | 152 | dec ax ; DMA controller is programmed for one byte less
|
---|
[480] | 153 | out BASE_AND_CURRENT_COUNT_REGISTER_DMA8_CH3_out, al ; Low byte
|
---|
| 154 | mov al, ah
|
---|
| 155 | out BASE_AND_CURRENT_COUNT_REGISTER_DMA8_CH3_out, al ; High byte
|
---|
| 156 |
|
---|
| 157 | ; Enable DMA Channel 3
|
---|
| 158 | mov al, CLEAR_CH3_MASK_BIT
|
---|
[545] | 159 | out MASK_REGISTER_DMA8_out, al ; Enable DMA Channel 3
|
---|
| 160 | sti ; Enable interrupts
|
---|
[480] | 161 |
|
---|
[558] | 162 | ; Update physical address in ES:DI - since IO might need several calls through this function either from here
|
---|
| 163 | ; if crossing a physical page boundary, or from IdeTransfer.asm if requested sectors was > PIOVARS.wSectorsInBlock
|
---|
| 164 | ; We update the pointer here (before the actual transfer) to avoid having to save the byte count on the stack
|
---|
| 165 | mov ax, es ; copy physical page address to ax
|
---|
| 166 | add di, cx ; add requested bytes to di
|
---|
| 167 | adc al, 0 ; and increment physical page address, if required
|
---|
| 168 | mov es, ax ; and save it back in es
|
---|
| 169 |
|
---|
[545] | 170 | ; XT-CF transfers 16 bytes at a time. We need to manually start transfer for every block by writing (anything)
|
---|
| 171 | ; to the XT-CFv3 Control Register, which raises DRQ thereby passing system control to the 8237 DMA controller.
|
---|
| 172 | ; The XT-CFv3 logic releases DRQ after 16 transfers, thereby handing control back to the CPU and allowing any other IRQs or
|
---|
| 173 | ; DRQs to be serviced (which, on the PC and PC/XT will include DRAM refresh via DMA channel 0). The 16-byte transfers can
|
---|
| 174 | ; also be interrupted by the DMA controller raising TC (i.e. when done). Each transfer cannot be otherwise interrupted
|
---|
| 175 | ; and is therefore atomic (and hence fast).
|
---|
[480] | 176 |
|
---|
[545] | 177 | %if 0 ; Slow DMA code - works by checking 8237 status register after each 16-byte transfer, until it reports TC has been raised.
|
---|
[558] | 178 | ALIGN JUMP_ALIGN
|
---|
| 179 | .TransferNextBlock:
|
---|
| 180 | cli ; We want no ISR to read DMA Status Register before we do
|
---|
| 181 | out dx, al ; Transfer up to 16 bytes to/from XT-CF card
|
---|
| 182 | in al, STATUS_REGISTER_DMA8_in
|
---|
| 183 | sti
|
---|
| 184 | test al, FLG_CH3_HAS_REACHED_TERMINAL_COUNT
|
---|
| 185 | jz SHORT .TransferNextBlock ; All bytes transferred?
|
---|
| 186 | %else ; Fast DMA code - perform computed number of transfers, then check DMA status register to be sure
|
---|
[567] | 187 | ; We'll divide transfers in 16-byte atomic transfers, so include any partial block, which will be terminated by the DMA controller raising T/C
|
---|
| 188 | add cx, BYTE 15
|
---|
| 189 |
|
---|
| 190 | %ifdef USE_186
|
---|
| 191 | shr cx, 4
|
---|
| 192 | %else
|
---|
| 193 | xchg cx, ax
|
---|
| 194 | mov cl, 4
|
---|
| 195 | shr ax, cl
|
---|
| 196 | xchg cx, ax
|
---|
| 197 | %endif
|
---|
| 198 |
|
---|
| 199 | ALIGN JUMP_ALIGN
|
---|
[486] | 200 | .TransferNextDmaBlock:
|
---|
[545] | 201 | out dx, al ; Transfer up to 16 bytes to/from XT-CF card
|
---|
| 202 | loop .TransferNextDmaBlock ; dec CX and loop if CX > 0, also adds required wait-state
|
---|
[486] | 203 | inc cx ; set up CX, in case we need to do an extra iteration
|
---|
[545] | 204 | in al, STATUS_REGISTER_DMA8_in ; check 8237 DMA controller status flags...
|
---|
| 205 | test al, FLG_CH3_HAS_REACHED_TERMINAL_COUNT ; ... for channel 3 terminal count
|
---|
| 206 | jz SHORT .TransferNextDmaBlock ; If not set, get more bytes
|
---|
[558] | 207 | %endif
|
---|
[486] | 208 |
|
---|
[480] | 209 | ret
|
---|