[3] | 1 | ; File name : HAddress.asm |
---|
| 2 | ; Project name : IDE BIOS |
---|
| 3 | ; Created date : 11.3.2010 |
---|
| 4 | ; Last update : 4.4.2010 |
---|
| 5 | ; Author : Tomi Tilli |
---|
| 6 | ; Description : Functions for address translations. |
---|
| 7 | |
---|
| 8 | ; Section containing code |
---|
| 9 | SECTION .text |
---|
| 10 | |
---|
| 11 | ;-------------------------------------------------------------------- |
---|
| 12 | ; Outputs sector count, L-CHS address and command to IDE registers. |
---|
| 13 | ; This function does not wait until command has been completed. |
---|
| 14 | ; |
---|
| 15 | ; HAddress_ConvertParamsFromBiosLCHStoIDE |
---|
| 16 | ; Parameters: |
---|
| 17 | ; CH: Cylinder number, bits 7...0 |
---|
| 18 | ; CL: Bits 7...6: Cylinder number bits 9 and 8 |
---|
| 19 | ; Bits 5...0: Starting sector number (1...63) |
---|
| 20 | ; DH: Starting head number (0...255) |
---|
| 21 | ; DS:DI: Ptr to DPT |
---|
| 22 | ; Returns: |
---|
| 23 | ; BL: LBA Low Register / Sector Number Register (LBA 7...0) |
---|
| 24 | ; CL: LBA Mid Register / Low Cylinder Register (LBA 15...8) |
---|
| 25 | ; CH: LBA High Register / High Cylinder Register (LBA 23...16) |
---|
| 26 | ; BH: Drive and Head Register (LBA 27...24) |
---|
| 27 | ; Corrupts registers: |
---|
| 28 | ; AX, DX |
---|
| 29 | ;-------------------------------------------------------------------- |
---|
| 30 | ALIGN WORD_ALIGN |
---|
| 31 | g_rgfnAddressTranslation: |
---|
| 32 | dw HAddress_DoNotConvertLCHS ; 0, ADDR_DPT_LCHS |
---|
| 33 | dw HAddress_ConvertLCHStoPCHS ; 1, ADDR_DPT_PCHS |
---|
| 34 | dw HAddress_ConvertLCHStoLBARegisterValues ; 2, ADDR_DPT_LBA28 |
---|
| 35 | dw HAddress_ConvertLCHStoLBARegisterValues ; 3, ADDR_DPT_LBA48 |
---|
| 36 | |
---|
| 37 | ALIGN JUMP_ALIGN |
---|
| 38 | HAddress_ConvertParamsFromBiosLCHStoIDE: |
---|
| 39 | mov bl, [di+DPT.bFlags] |
---|
| 40 | and bx, BYTE MASK_DPT_ADDR ; Addressing mode to BX |
---|
| 41 | push WORD [cs:bx+g_rgfnAddressTranslation] ; Push return address |
---|
| 42 | ; Fall to HAddress_ExtractLCHSFromBiosParams |
---|
| 43 | |
---|
| 44 | ;--------------------------------------------------------------------- |
---|
| 45 | ; Extracts L-CHS parameters from BIOS function parameters. |
---|
| 46 | ; |
---|
| 47 | ; HAddress_ExtractLCHSFromBiosParams: |
---|
| 48 | ; Parameters: |
---|
| 49 | ; CH: Cylinder number, bits 7...0 |
---|
| 50 | ; CL: Bits 7...6: Cylinder number bits 9 and 8 |
---|
| 51 | ; Bits 5...0: Sector number |
---|
| 52 | ; DH: Head number |
---|
| 53 | ; Returns: |
---|
| 54 | ; BL: Sector number (1...63) |
---|
| 55 | ; BH: Head number (0...255) |
---|
| 56 | ; CX: Cylinder number (0...1023) |
---|
| 57 | ; Corrupts registers: |
---|
| 58 | ; Nothing |
---|
| 59 | ;-------------------------------------------------------------------- |
---|
| 60 | ALIGN JUMP_ALIGN |
---|
| 61 | HAddress_ExtractLCHSFromBiosParams: |
---|
| 62 | mov bl, cl ; Copy sector number... |
---|
| 63 | and bl, 3Fh ; ...and limit to 1...63 |
---|
| 64 | sub cl, bl ; Remove from cylinder number high |
---|
| 65 | eROL_IM cl, 2 ; High bits to beginning |
---|
| 66 | mov bh, dh ; Copy Head number |
---|
| 67 | xchg cl, ch ; Cylinder number now in CX |
---|
| 68 | ret |
---|
| 69 | |
---|
| 70 | |
---|
| 71 | ;--------------------------------------------------------------------- |
---|
| 72 | ; Converts BIOS LCHS parameters to IDE P-CHS parameters. |
---|
| 73 | ; PCylinder = (LCylinder << n) + (LHead / PHeadCount) |
---|
| 74 | ; PHead = LHead % PHeadCount |
---|
| 75 | ; PSector = LSector |
---|
| 76 | ; |
---|
| 77 | ; HAddress_ConvertLCHStoPCHS: |
---|
| 78 | ; Parameters: |
---|
| 79 | ; BL: Sector number (1...63) |
---|
| 80 | ; BH: Head number (0...255) |
---|
| 81 | ; CX: Cylinder number (0...1023) |
---|
| 82 | ; DS:DI: Ptr to Disk Parameter Table |
---|
| 83 | ; Returns: |
---|
| 84 | ; BL: Sector number (1...63) |
---|
| 85 | ; BH: Head number (0...15) |
---|
| 86 | ; CX: Cylinder number (0...16382) |
---|
| 87 | ; Corrupts registers: |
---|
| 88 | ; AX, DX |
---|
| 89 | ;-------------------------------------------------------------------- |
---|
| 90 | ALIGN JUMP_ALIGN |
---|
| 91 | HAddress_ConvertLCHStoPCHS: |
---|
| 92 | ; LHead / PHeadCount and LHead % PHeadCount |
---|
| 93 | eMOVZX ax, bh ; Copy L-CHS Head number to AX |
---|
| 94 | div BYTE [di+DPT.bPHeads] ; AL = LHead / PHeadCount, AH = LHead % PHeadCount |
---|
| 95 | mov bh, ah ; Copy P-CHS Head number to BH |
---|
| 96 | xor ah, ah ; AX = LHead / PHeadCount |
---|
| 97 | |
---|
| 98 | ; (LCylinder << n) + (LHead / PHeadCount) |
---|
| 99 | mov dx, cx ; Copy L-CHS Cylinder number to DX |
---|
| 100 | mov cl, [di+DPT.bShLtoP] ; Load shift count |
---|
| 101 | shl dx, cl ; DX = LCylinder << n |
---|
| 102 | add ax, dx ; AX = P-CHS Cylinder number |
---|
| 103 | mov cx, ax ; Copy P-CHS Cylinder number to CX |
---|
| 104 | ALIGN JUMP_ALIGN |
---|
| 105 | HAddress_DoNotConvertLCHS: |
---|
| 106 | ret |
---|
| 107 | |
---|
| 108 | |
---|
| 109 | ;--------------------------------------------------------------------- |
---|
| 110 | ; Converts LCHS parameters to 28-bit LBA address. |
---|
| 111 | ; Returned address is in same registers that |
---|
| 112 | ; HAddress_DoNotConvertLCHS and HAddress_ConvertLCHStoPCHS returns. |
---|
| 113 | ; |
---|
| 114 | ; HAddress_ConvertLCHStoLBARegisterValues: |
---|
| 115 | ; Parameters: |
---|
| 116 | ; BL: Sector number (1...63) |
---|
| 117 | ; BH: Head number (0...255) |
---|
| 118 | ; CX: Cylinder number (0...1023) |
---|
| 119 | ; DS:DI: Ptr to Disk Parameter Table |
---|
| 120 | ; Returns: |
---|
| 121 | ; BL: LBA Low Register / Sector Number Register (LBA 7...0) |
---|
| 122 | ; CL: LBA Mid Register / Low Cylinder Register (LBA 15...8) |
---|
| 123 | ; CH: LBA High Register / High Cylinder Register (LBA 23...16) |
---|
| 124 | ; BH: Drive and Head Register (LBA 27...24) |
---|
| 125 | ; Corrupts registers: |
---|
| 126 | ; AX, DX |
---|
| 127 | ;-------------------------------------------------------------------- |
---|
| 128 | ALIGN JUMP_ALIGN |
---|
| 129 | HAddress_ConvertLCHStoLBARegisterValues: |
---|
| 130 | call HAddress_ConvertLCHStoLBA28 |
---|
| 131 | mov bl, al ; Sector Number Register (LBA 7...0) |
---|
| 132 | mov cl, ah ; Low Cylinder Register (LBA 15...8) |
---|
| 133 | mov ch, dl ; High Cylinder Register (LBA 23...16) |
---|
| 134 | mov bh, dh ; Drive and Head Register (LBA 27...24) |
---|
| 135 | ret |
---|
| 136 | |
---|
| 137 | ;--------------------------------------------------------------------- |
---|
| 138 | ; Converts LCHS parameters to 28-bit LBA address. |
---|
| 139 | ; Only 24-bits are used since LHCS to LBA28 conversion has 8.4GB limit. |
---|
| 140 | ; LBA = ((cylToSeek*headsPerCyl+headToSeek)*sectPerTrack)+sectToSeek-1 |
---|
| 141 | ; |
---|
| 142 | ; HAddress_ConvertLCHStoLBA28: |
---|
| 143 | ; Parameters: |
---|
| 144 | ; BL: Sector number (1...63) |
---|
| 145 | ; BH: Head number (0...255) |
---|
| 146 | ; CX: Cylinder number (0...1023) |
---|
| 147 | ; DS:DI: Ptr to Disk Parameter Table |
---|
| 148 | ; Returns: |
---|
| 149 | ; DX:AX: 28-bit LBA address (DH is always zero) |
---|
| 150 | ; Corrupts registers: |
---|
| 151 | ; BX, CX |
---|
| 152 | ;-------------------------------------------------------------------- |
---|
| 153 | ALIGN JUMP_ALIGN |
---|
| 154 | HAddress_ConvertLCHStoLBA28: |
---|
| 155 | ; cylToSeek*headsPerCyl (18-bit result) |
---|
| 156 | mov ax, cx ; Copy Cylinder number to AX |
---|
| 157 | mul WORD [di+DPT.wLHeads] ; DX:AX = cylToSeek*headsPerCyl |
---|
| 158 | |
---|
| 159 | ; +=headToSeek (18-bit result) |
---|
| 160 | add al, bh ; Add Head number to DX:AX |
---|
| 161 | adc ah, dh ; DH = Zero after previous multiplication |
---|
| 162 | adc dl, dh |
---|
| 163 | |
---|
| 164 | ; *=sectPerTrack (18-bit by 6-bit multiplication with 24-bit result) |
---|
| 165 | eMOVZX cx, BYTE [di+DPT.bPSect]; Load Sectors per Track |
---|
| 166 | xchg ax, dx ; Hiword to AX, loword to DX |
---|
| 167 | mul cl ; AX = hiword * Sectors per Track |
---|
| 168 | mov bh, al ; Backup hiword * Sectors per Track |
---|
| 169 | xchg ax, dx ; Loword back to AX |
---|
| 170 | mul cx ; DX:AX = loword * Sectors per Track |
---|
| 171 | add dl, bh ; DX:AX = (cylToSeek*headsPerCyl+headToSeek)*sectPerTrack |
---|
| 172 | |
---|
| 173 | ; +=sectToSeek-1 (24-bit result) |
---|
| 174 | xor bh, bh ; Sector number now in BX |
---|
| 175 | dec bx ; sectToSeek-=1 |
---|
| 176 | add ax, bx ; Add to loword |
---|
| 177 | adc dl, bh ; Add possible carry to byte2, BH=zero |
---|
| 178 | ret |
---|