Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 1 | // 16bit code to access floppy drives. |
| 2 | // |
| 3 | // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net> |
| 4 | // Copyright (C) 2002 MandrakeSoft S.A. |
| 5 | // |
| 6 | // This file may be distributed under the terms of the GNU GPLv3 license. |
| 7 | |
| 8 | #include "types.h" // u8 |
| 9 | #include "disk.h" // DISK_RET_SUCCESS |
| 10 | #include "config.h" // CONFIG_FLOPPY_SUPPORT |
| 11 | #include "biosvar.h" // struct bregs |
| 12 | #include "util.h" // irq_disable |
| 13 | #include "cmos.h" // inb_cmos |
| 14 | |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 15 | #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args) |
| 16 | #define DEBUGF(fmt, args...) |
| 17 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 18 | #define BX_FLOPPY_ON_CNT 37 /* 2 seconds */ |
| 19 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 20 | // New diskette parameter table adding 3 parameters from IBM |
| 21 | // Since no provisions are made for multiple drive types, most |
| 22 | // values in this table are ignored. I set parameters for 1.44M |
| 23 | // floppy here |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 24 | struct floppy_ext_dbt_s diskette_param_table2 VISIBLE16 = { |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 25 | .dbt = { |
| 26 | .specify1 = 0xAF, |
| 27 | .specify2 = 0x02, // head load time 0000001, DMA used |
| 28 | .shutoff_ticks = 0x25, |
| 29 | .bps_code = 0x02, |
| 30 | .sectors = 18, |
| 31 | .interblock_len = 0x1B, |
| 32 | .data_len = 0xFF, |
| 33 | .gap_len = 0x6C, |
| 34 | .fill_byte = 0xF6, |
| 35 | .settle_time = 0x0F, |
| 36 | .startup_time = 0x08, |
| 37 | }, |
| 38 | .max_track = 79, // maximum track |
| 39 | .data_rate = 0, // data transfer rate |
| 40 | .drive_type = 4, // drive type in cmos |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | // Oddities: |
| 44 | // Return codes vary greatly - AL not cleared consistenlty, BDA return |
| 45 | // status not set consistently, sometimes panics. |
| 46 | // Extra outb(0x000a, 0x02) in read? |
| 47 | // Does not disable interrupts on failure paths. |
| 48 | // numfloppies used before set in int_1308 |
| 49 | // int_1305 verifies track but doesn't use it? |
| 50 | |
| 51 | static inline void |
| 52 | set_diskette_current_cyl(u8 drive, u8 cyl) |
| 53 | { |
| 54 | if (drive) |
| 55 | SET_BDA(floppy_track1, cyl); |
| 56 | else |
| 57 | SET_BDA(floppy_track0, cyl); |
| 58 | } |
| 59 | |
| 60 | static u16 |
| 61 | get_drive_type(u8 drive) |
| 62 | { |
| 63 | // check CMOS to see if drive exists |
| 64 | u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE); |
| 65 | if (drive == 0) |
| 66 | drive_type >>= 4; |
| 67 | else |
| 68 | drive_type &= 0x0f; |
| 69 | return drive_type; |
| 70 | } |
| 71 | |
| 72 | static u16 |
| 73 | floppy_media_known(u8 drive) |
| 74 | { |
| 75 | if (!(GET_BDA(floppy_recalibration_status) & (1<<drive))) |
| 76 | return 0; |
| 77 | u8 v = GET_BDA(floppy_media_state[drive]); |
| 78 | if (!(v & FMS_MEDIA_DRIVE_ESTABLISHED)) |
| 79 | return 0; |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | static void |
| 84 | floppy_reset_controller() |
| 85 | { |
| 86 | // Reset controller |
| 87 | u8 val8 = inb(PORT_FD_DOR); |
| 88 | outb(val8 & ~0x04, PORT_FD_DOR); |
| 89 | outb(val8 | 0x04, PORT_FD_DOR); |
| 90 | |
| 91 | // Wait for controller to come out of reset |
| 92 | while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80) |
| 93 | ; |
| 94 | } |
| 95 | |
| 96 | static void |
| 97 | floppy_prepare_controller(u8 drive) |
| 98 | { |
| 99 | CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT); |
| 100 | |
| 101 | // turn on motor of selected drive, DMA & int enabled, normal operation |
| 102 | u8 prev_reset = inb(PORT_FD_DOR) & 0x04; |
| 103 | u8 dor = 0x10; |
| 104 | if (drive) |
| 105 | dor = 0x20; |
| 106 | dor |= 0x0c; |
| 107 | dor |= drive; |
| 108 | outb(dor, PORT_FD_DOR); |
| 109 | |
| 110 | // reset the disk motor timeout value of INT 08 |
| 111 | SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT); |
| 112 | |
| 113 | // wait for drive readiness |
| 114 | while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80) |
| 115 | ; |
| 116 | |
| 117 | if (prev_reset == 0) { |
| 118 | irq_enable(); |
| 119 | u8 v; |
| 120 | do { |
| 121 | v = GET_BDA(floppy_recalibration_status); |
| 122 | } while ((v & FRS_TIMEOUT) == 0); |
| 123 | irq_disable(); |
| 124 | |
| 125 | v &= ~FRS_TIMEOUT; |
| 126 | SET_BDA(floppy_recalibration_status, v); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | static u8 |
| 131 | floppy_pio(u8 *cmd, u8 cmdlen) |
| 132 | { |
| 133 | floppy_prepare_controller(cmd[1] & 1); |
| 134 | |
| 135 | // send command to controller |
| 136 | u8 i; |
| 137 | for (i=0; i<cmdlen; i++) |
| 138 | outb(cmd[i], PORT_FD_DATA); |
| 139 | |
| 140 | irq_enable(); |
| 141 | u8 v; |
| 142 | do { |
| 143 | if (!GET_BDA(floppy_motor_counter)) { |
| 144 | irq_disable(); |
| 145 | floppy_reset_controller(); |
| 146 | return DISK_RET_ETIMEOUT; |
| 147 | } |
| 148 | v = GET_BDA(floppy_recalibration_status); |
| 149 | } while (!(v & FRS_TIMEOUT)); |
| 150 | irq_disable(); |
| 151 | |
| 152 | v &= ~FRS_TIMEOUT; |
| 153 | SET_BDA(floppy_recalibration_status, v); |
| 154 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 155 | return 0; |
| 156 | } |
| 157 | |
| 158 | static u8 |
| 159 | floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen) |
| 160 | { |
| 161 | // es:bx = pointer to where to place information from diskette |
| 162 | // port 04: DMA-1 base and current address, channel 2 |
| 163 | // port 05: DMA-1 base and current count, channel 2 |
| 164 | u16 page = regs->es >> 12; // upper 4 bits |
| 165 | u16 base_es = regs->es << 4; // lower 16bits contributed by ES |
| 166 | u16 base_address = base_es + regs->bx; // lower 16 bits of address |
| 167 | // contributed by ES:BX |
| 168 | if (base_address < base_es) |
| 169 | // in case of carry, adjust page by 1 |
| 170 | page++; |
| 171 | |
| 172 | // check for 64K boundary overrun |
| 173 | u16 last_addr = base_address + count; |
| 174 | if (last_addr < base_address) |
| 175 | return DISK_RET_EBOUNDARY; |
| 176 | |
| 177 | u8 mode_register = 0x4a; // single mode, increment, autoinit disable, |
| 178 | if (cmd[0] == 0xe6) |
| 179 | // read |
| 180 | mode_register = 0x46; |
| 181 | |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 182 | //DEBUGF("floppy dma c2\n"); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 183 | outb(0x06, PORT_DMA1_MASK_REG); |
| 184 | outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop |
| 185 | outb(base_address, PORT_DMA_ADDR_2); |
| 186 | outb(base_address>>8, PORT_DMA_ADDR_2); |
| 187 | outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop |
| 188 | outb(count, PORT_DMA_CNT_2); |
| 189 | outb(count>>8, PORT_DMA_CNT_2); |
| 190 | |
| 191 | // port 0b: DMA-1 Mode Register |
| 192 | // transfer type=write, channel 2 |
| 193 | outb(mode_register, PORT_DMA1_MODE_REG); |
| 194 | |
| 195 | // port 81: DMA-1 Page Register, channel 2 |
| 196 | outb(page, PORT_DMA_PAGE_2); |
| 197 | |
| 198 | outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2 |
| 199 | |
| 200 | u8 ret = floppy_pio(cmd, cmdlen); |
| 201 | if (ret) |
| 202 | return ret; |
| 203 | |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 204 | // check port 3f4 for accessibility to status bytes |
| 205 | if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) |
| 206 | BX_PANIC("int13_diskette: ctrl not ready\n"); |
| 207 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 208 | // read 7 return status bytes from controller |
| 209 | u8 i; |
| 210 | for (i=0; i<7; i++) { |
| 211 | u8 v = inb(PORT_FD_DATA); |
| 212 | cmd[i] = v; |
| 213 | SET_BDA(floppy_return_status[i], v); |
| 214 | } |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static void |
| 220 | floppy_drive_recal(u8 drive) |
| 221 | { |
| 222 | // send Recalibrate command (2 bytes) to controller |
| 223 | u8 data[12]; |
| 224 | data[0] = 0x07; // 07: Recalibrate |
| 225 | data[1] = drive; // 0=drive0, 1=drive1 |
| 226 | floppy_pio(data, 2); |
| 227 | |
| 228 | SETBITS_BDA(floppy_recalibration_status, 1<<drive); |
| 229 | set_diskette_current_cyl(drive, 0); |
| 230 | } |
| 231 | |
| 232 | static u16 |
| 233 | floppy_media_sense(u8 drive) |
| 234 | { |
| 235 | u16 rv; |
| 236 | u8 config_data, media_state; |
| 237 | |
| 238 | floppy_drive_recal(drive); |
| 239 | |
| 240 | // for now cheat and get drive type from CMOS, |
| 241 | // assume media is same as drive type |
| 242 | |
| 243 | // ** config_data ** |
| 244 | // Bitfields for diskette media control: |
| 245 | // Bit(s) Description (Table M0028) |
| 246 | // 7-6 last data rate set by controller |
| 247 | // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps |
| 248 | // 5-4 last diskette drive step rate selected |
| 249 | // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah |
| 250 | // 3-2 {data rate at start of operation} |
| 251 | // 1-0 reserved |
| 252 | |
| 253 | // ** media_state ** |
| 254 | // Bitfields for diskette drive media state: |
| 255 | // Bit(s) Description (Table M0030) |
| 256 | // 7-6 data rate |
| 257 | // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps |
| 258 | // 5 double stepping required (e.g. 360kB in 1.2MB) |
| 259 | // 4 media type established |
| 260 | // 3 drive capable of supporting 4MB media |
| 261 | // 2-0 on exit from BIOS, contains |
| 262 | // 000 trying 360kB in 360kB |
| 263 | // 001 trying 360kB in 1.2MB |
| 264 | // 010 trying 1.2MB in 1.2MB |
| 265 | // 011 360kB in 360kB established |
| 266 | // 100 360kB in 1.2MB established |
| 267 | // 101 1.2MB in 1.2MB established |
| 268 | // 110 reserved |
| 269 | // 111 all other formats/drives |
| 270 | |
| 271 | switch (get_drive_type(drive)) { |
| 272 | case 1: |
| 273 | // 360K 5.25" drive |
| 274 | config_data = 0x00; // 0000 0000 |
| 275 | media_state = 0x25; // 0010 0101 |
| 276 | rv = 1; |
| 277 | break; |
| 278 | case 2: |
| 279 | // 1.2 MB 5.25" drive |
| 280 | config_data = 0x00; // 0000 0000 |
| 281 | media_state = 0x25; // 0010 0101 // need double stepping??? (bit 5) |
| 282 | rv = 1; |
| 283 | break; |
| 284 | case 3: |
| 285 | // 720K 3.5" drive |
| 286 | config_data = 0x00; // 0000 0000 ??? |
| 287 | media_state = 0x17; // 0001 0111 |
| 288 | rv = 1; |
| 289 | break; |
| 290 | case 4: |
| 291 | // 1.44 MB 3.5" drive |
| 292 | config_data = 0x00; // 0000 0000 |
| 293 | media_state = 0x17; // 0001 0111 |
| 294 | rv = 1; |
| 295 | break; |
| 296 | case 5: |
| 297 | // 2.88 MB 3.5" drive |
| 298 | config_data = 0xCC; // 1100 1100 |
| 299 | media_state = 0xD7; // 1101 0111 |
| 300 | rv = 1; |
| 301 | break; |
| 302 | // |
| 303 | // Extended floppy size uses special cmos setting |
| 304 | case 6: |
| 305 | // 160k 5.25" drive |
| 306 | config_data = 0x00; // 0000 0000 |
| 307 | media_state = 0x27; // 0010 0111 |
| 308 | rv = 1; |
| 309 | break; |
| 310 | case 7: |
| 311 | // 180k 5.25" drive |
| 312 | config_data = 0x00; // 0000 0000 |
| 313 | media_state = 0x27; // 0010 0111 |
| 314 | rv = 1; |
| 315 | break; |
| 316 | case 8: |
| 317 | // 320k 5.25" drive |
| 318 | config_data = 0x00; // 0000 0000 |
| 319 | media_state = 0x27; // 0010 0111 |
| 320 | rv = 1; |
| 321 | break; |
| 322 | default: |
| 323 | // not recognized |
| 324 | config_data = 0x00; // 0000 0000 |
| 325 | media_state = 0x00; // 0000 0000 |
| 326 | rv = 0; |
| 327 | } |
| 328 | |
| 329 | SET_BDA(floppy_last_data_rate, config_data); |
| 330 | SET_BDA(floppy_media_state[drive], media_state); |
| 331 | return rv; |
| 332 | } |
| 333 | |
| 334 | static inline void |
| 335 | floppy_ret(struct bregs *regs, u8 code) |
| 336 | { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 337 | SET_BDA(floppy_last_status, code); |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 338 | if (code) |
| 339 | set_code_fail(regs, code); |
| 340 | else |
| 341 | set_code_success(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | static inline void |
| 345 | floppy_fail(struct bregs *regs, u8 code) |
| 346 | { |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 347 | floppy_ret(regs, code); |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 348 | regs->al = 0; // no sectors read |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static u16 |
| 352 | check_drive(struct bregs *regs, u8 drive) |
| 353 | { |
| 354 | // see if drive exists |
| 355 | if (drive > 1 || !get_drive_type(drive)) { |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 356 | // XXX - return type doesn't match |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 357 | floppy_fail(regs, DISK_RET_ETIMEOUT); |
| 358 | return 1; |
| 359 | } |
| 360 | |
| 361 | // see if media in drive, and type is known |
| 362 | if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) { |
| 363 | floppy_fail(regs, DISK_RET_EMEDIA); |
| 364 | return 1; |
| 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | // diskette controller reset |
| 370 | static void |
| 371 | floppy_1300(struct bregs *regs, u8 drive) |
| 372 | { |
| 373 | if (drive > 1) { |
| 374 | floppy_ret(regs, DISK_RET_EPARAM); |
| 375 | return; |
| 376 | } |
| 377 | if (!get_drive_type(drive)) { |
| 378 | floppy_ret(regs, DISK_RET_ETIMEOUT); |
| 379 | return; |
| 380 | } |
| 381 | set_diskette_current_cyl(drive, 0); // current cylinder |
| 382 | floppy_ret(regs, DISK_RET_SUCCESS); |
| 383 | } |
| 384 | |
| 385 | // Read Diskette Status |
| 386 | static void |
| 387 | floppy_1301(struct bregs *regs, u8 drive) |
| 388 | { |
| 389 | u8 v = GET_BDA(floppy_last_status); |
| 390 | regs->ah = v; |
| 391 | set_cf(regs, v); |
| 392 | } |
| 393 | |
| 394 | // Read Diskette Sectors |
| 395 | static void |
| 396 | floppy_1302(struct bregs *regs, u8 drive) |
| 397 | { |
| 398 | if (check_drive(regs, drive)) |
| 399 | return; |
| 400 | |
| 401 | u8 num_sectors = regs->al; |
| 402 | u8 track = regs->ch; |
| 403 | u8 sector = regs->cl; |
| 404 | u8 head = regs->dh; |
| 405 | |
| 406 | if (head > 1 || sector == 0 || num_sectors == 0 |
| 407 | || track > 79 || num_sectors > 72) { |
| 408 | BX_INFO("int13_diskette: read/write/verify: parameter out of range\n"); |
| 409 | floppy_fail(regs, DISK_RET_EPARAM); |
| 410 | return; |
| 411 | } |
| 412 | |
| 413 | // send read-normal-data command (9 bytes) to controller |
| 414 | u8 data[12]; |
| 415 | data[0] = 0xe6; // e6: read normal data |
| 416 | data[1] = (head << 2) | drive; // HD DR1 DR2 |
| 417 | data[2] = track; |
| 418 | data[3] = head; |
| 419 | data[4] = sector; |
| 420 | data[5] = 2; // 512 byte sector size |
| 421 | data[6] = sector + num_sectors - 1; // last sector to read on track |
| 422 | data[7] = 0; // Gap length |
| 423 | data[8] = 0xff; // Gap length |
| 424 | |
| 425 | u16 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9); |
| 426 | if (ret) { |
| 427 | floppy_fail(regs, ret); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | if (data[0] & 0xc0) { |
| 432 | floppy_fail(regs, DISK_RET_ECONTROLLER); |
| 433 | return; |
| 434 | } |
| 435 | |
| 436 | // ??? should track be new val from return_status[3] ? |
| 437 | set_diskette_current_cyl(drive, track); |
| 438 | // AL = number of sectors read (same value as passed) |
| 439 | floppy_ret(regs, DISK_RET_SUCCESS); |
| 440 | } |
| 441 | |
| 442 | // Write Diskette Sectors |
| 443 | static void |
| 444 | floppy_1303(struct bregs *regs, u8 drive) |
| 445 | { |
| 446 | if (check_drive(regs, drive)) |
| 447 | return; |
| 448 | |
| 449 | u8 num_sectors = regs->al; |
| 450 | u8 track = regs->ch; |
| 451 | u8 sector = regs->cl; |
| 452 | u8 head = regs->dh; |
| 453 | |
| 454 | if (head > 1 || sector == 0 || num_sectors == 0 |
| 455 | || track > 79 || num_sectors > 72) { |
| 456 | BX_INFO("int13_diskette: read/write/verify: parameter out of range\n"); |
| 457 | floppy_fail(regs, DISK_RET_EPARAM); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | // send write-normal-data command (9 bytes) to controller |
| 462 | u8 data[12]; |
| 463 | data[0] = 0xc5; // c5: write normal data |
| 464 | data[1] = (head << 2) | drive; // HD DR1 DR2 |
| 465 | data[2] = track; |
| 466 | data[3] = head; |
| 467 | data[4] = sector; |
| 468 | data[5] = 2; // 512 byte sector size |
| 469 | data[6] = sector + num_sectors - 1; // last sector to write on track |
| 470 | data[7] = 0; // Gap length |
| 471 | data[8] = 0xff; // Gap length |
| 472 | |
| 473 | u8 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9); |
| 474 | if (ret) { |
| 475 | floppy_fail(regs, ret); |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | if (data[0] & 0xc0) { |
| 480 | if (data[1] & 0x02) { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 481 | set_fail(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 482 | regs->ax = 0x0300; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | BX_PANIC("int13_diskette_function: read error\n"); |
| 486 | } |
| 487 | |
| 488 | // ??? should track be new val from return_status[3] ? |
| 489 | set_diskette_current_cyl(drive, track); |
| 490 | // AL = number of sectors read (same value as passed) |
| 491 | floppy_ret(regs, DISK_RET_SUCCESS); |
| 492 | } |
| 493 | |
| 494 | // Verify Diskette Sectors |
| 495 | static void |
| 496 | floppy_1304(struct bregs *regs, u8 drive) |
| 497 | { |
| 498 | if (check_drive(regs, drive)) |
| 499 | return; |
| 500 | |
| 501 | u8 num_sectors = regs->al; |
| 502 | u8 track = regs->ch; |
| 503 | u8 sector = regs->cl; |
| 504 | u8 head = regs->dh; |
| 505 | |
| 506 | if (head > 1 || sector == 0 || num_sectors == 0 |
| 507 | || track > 79 || num_sectors > 72) { |
| 508 | BX_INFO("int13_diskette: read/write/verify: parameter out of range\n"); |
| 509 | floppy_fail(regs, DISK_RET_EPARAM); |
| 510 | return; |
| 511 | } |
| 512 | |
| 513 | // ??? should track be new val from return_status[3] ? |
| 514 | set_diskette_current_cyl(drive, track); |
| 515 | // AL = number of sectors verified (same value as passed) |
| 516 | floppy_ret(regs, DISK_RET_SUCCESS); |
| 517 | } |
| 518 | |
| 519 | // format diskette track |
| 520 | static void |
| 521 | floppy_1305(struct bregs *regs, u8 drive) |
| 522 | { |
| 523 | DEBUGF("floppy f05\n"); |
| 524 | |
| 525 | if (check_drive(regs, drive)) |
| 526 | return; |
| 527 | |
| 528 | u8 num_sectors = regs->al; |
| 529 | u8 head = regs->dh; |
| 530 | |
| 531 | if (head > 1 || num_sectors == 0 || num_sectors > 18) { |
| 532 | BX_INFO("int13_diskette: read/write/verify: parameter out of range\n"); |
| 533 | floppy_fail(regs, DISK_RET_EPARAM); |
| 534 | return; |
| 535 | } |
| 536 | |
| 537 | // send format-track command (6 bytes) to controller |
| 538 | u8 data[12]; |
| 539 | data[0] = 0x4d; // 4d: format track |
| 540 | data[1] = (head << 2) | drive; // HD DR1 DR2 |
| 541 | data[2] = 2; // 512 byte sector size |
| 542 | data[3] = num_sectors; // number of sectors per track |
| 543 | data[4] = 0; // Gap length |
| 544 | data[5] = 0xf6; // Fill byte |
| 545 | |
| 546 | u8 ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6); |
| 547 | if (ret) { |
| 548 | floppy_fail(regs, ret); |
| 549 | return; |
| 550 | } |
| 551 | |
| 552 | if (data[0] & 0xc0) { |
| 553 | if (data[1] & 0x02) { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 554 | set_fail(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 555 | regs->ax = 0x0300; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 556 | return; |
| 557 | } |
| 558 | BX_PANIC("int13_diskette_function: read error\n"); |
| 559 | } |
| 560 | |
| 561 | set_diskette_current_cyl(drive, 0); |
| 562 | floppy_ret(regs, 0); |
| 563 | } |
| 564 | |
| 565 | // read diskette drive parameters |
| 566 | static void |
| 567 | floppy_1308(struct bregs *regs, u8 drive) |
| 568 | { |
| 569 | DEBUGF("floppy f08\n"); |
| 570 | |
| 571 | u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE); |
| 572 | u8 num_floppies = 0; |
| 573 | if (drive_type & 0xf0) |
| 574 | num_floppies++; |
| 575 | if (drive_type & 0x0f) |
| 576 | num_floppies++; |
| 577 | |
| 578 | if (drive > 1) { |
| 579 | regs->ax = 0; |
| 580 | regs->bx = 0; |
| 581 | regs->cx = 0; |
| 582 | regs->dx = 0; |
| 583 | regs->es = 0; |
| 584 | regs->di = 0; |
| 585 | regs->dl = num_floppies; |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 586 | set_success(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 587 | return; |
| 588 | } |
| 589 | |
| 590 | if (drive == 0) |
| 591 | drive_type >>= 4; |
| 592 | else |
| 593 | drive_type &= 0x0f; |
| 594 | |
| 595 | regs->bh = 0; |
| 596 | regs->bl = drive_type; |
| 597 | regs->ah = 0; |
| 598 | regs->al = 0; |
| 599 | regs->dl = num_floppies; |
| 600 | |
| 601 | switch (drive_type) { |
| 602 | case 0: // none |
| 603 | regs->cx = 0; |
| 604 | regs->dh = 0; // max head # |
| 605 | break; |
| 606 | |
| 607 | case 1: // 360KB, 5.25" |
| 608 | regs->cx = 0x2709; // 40 tracks, 9 sectors |
| 609 | regs->dh = 1; // max head # |
| 610 | break; |
| 611 | |
| 612 | case 2: // 1.2MB, 5.25" |
| 613 | regs->cx = 0x4f0f; // 80 tracks, 15 sectors |
| 614 | regs->dh = 1; // max head # |
| 615 | break; |
| 616 | |
| 617 | case 3: // 720KB, 3.5" |
| 618 | regs->cx = 0x4f09; // 80 tracks, 9 sectors |
| 619 | regs->dh = 1; // max head # |
| 620 | break; |
| 621 | |
| 622 | case 4: // 1.44MB, 3.5" |
| 623 | regs->cx = 0x4f12; // 80 tracks, 18 sectors |
| 624 | regs->dh = 1; // max head # |
| 625 | break; |
| 626 | |
| 627 | case 5: // 2.88MB, 3.5" |
| 628 | regs->cx = 0x4f24; // 80 tracks, 36 sectors |
| 629 | regs->dh = 1; // max head # |
| 630 | break; |
| 631 | |
| 632 | case 6: // 160k, 5.25" |
| 633 | regs->cx = 0x2708; // 40 tracks, 8 sectors |
| 634 | regs->dh = 0; // max head # |
| 635 | break; |
| 636 | |
| 637 | case 7: // 180k, 5.25" |
| 638 | regs->cx = 0x2709; // 40 tracks, 9 sectors |
| 639 | regs->dh = 0; // max head # |
| 640 | break; |
| 641 | |
| 642 | case 8: // 320k, 5.25" |
| 643 | regs->cx = 0x2708; // 40 tracks, 8 sectors |
| 644 | regs->dh = 1; // max head # |
| 645 | break; |
| 646 | |
| 647 | default: // ? |
| 648 | BX_PANIC("floppy: int13: bad floppy type\n"); |
| 649 | } |
| 650 | |
| 651 | /* set es & di to point to 11 byte diskette param table in ROM */ |
| 652 | regs->es = SEG_BIOS; |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 653 | regs->di = (u16)&diskette_param_table2; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 654 | /* disk status not changed upon success */ |
| 655 | } |
| 656 | |
| 657 | // read diskette drive type |
| 658 | static void |
| 659 | floppy_1315(struct bregs *regs, u8 drive) |
| 660 | { |
| 661 | DEBUGF("floppy f15\n"); |
| 662 | if (drive > 1) { |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 663 | set_fail(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 664 | regs->ah = 0; // only 2 drives supported |
| 665 | // set_diskette_ret_status here ??? |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 666 | return; |
| 667 | } |
| 668 | u8 drive_type = get_drive_type(drive); |
| 669 | |
| 670 | regs->ah = (drive_type != 0); |
Kevin O'Connor | 6c78122 | 2008-03-09 12:19:23 -0400 | [diff] [blame^] | 671 | set_success(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // get diskette change line status |
| 675 | static void |
| 676 | floppy_1316(struct bregs *regs, u8 drive) |
| 677 | { |
| 678 | DEBUGF("floppy f16\n"); |
| 679 | if (drive > 1) { |
| 680 | floppy_ret(regs, DISK_RET_EPARAM); |
| 681 | return; |
| 682 | } |
| 683 | floppy_ret(regs, DISK_RET_ECHANGED); |
| 684 | } |
| 685 | |
| 686 | static void |
| 687 | floppy_13XX(struct bregs *regs, u8 drive) |
| 688 | { |
Kevin O'Connor | b792b3c | 2008-02-29 00:20:32 -0500 | [diff] [blame] | 689 | BX_INFO("int13_diskette: unsupported AH=%02x\n", regs->ah); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 690 | floppy_ret(regs, DISK_RET_EPARAM); |
| 691 | } |
| 692 | |
| 693 | void |
| 694 | floppy_13(struct bregs *regs, u8 drive) |
| 695 | { |
Kevin O'Connor | b792b3c | 2008-02-29 00:20:32 -0500 | [diff] [blame] | 696 | if (! CONFIG_FLOPPY_SUPPORT) { |
| 697 | // Minimal stubs |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 698 | switch (regs->ah) { |
| 699 | case 0x01: floppy_1301(regs, drive); break; |
| 700 | default: floppy_13XX(regs, drive); break; |
| 701 | } |
Kevin O'Connor | b792b3c | 2008-02-29 00:20:32 -0500 | [diff] [blame] | 702 | return; |
| 703 | } |
| 704 | switch (regs->ah) { |
| 705 | case 0x00: floppy_1300(regs, drive); break; |
| 706 | case 0x01: floppy_1301(regs, drive); break; |
| 707 | case 0x02: floppy_1302(regs, drive); break; |
| 708 | case 0x03: floppy_1303(regs, drive); break; |
| 709 | case 0x04: floppy_1304(regs, drive); break; |
| 710 | case 0x05: floppy_1305(regs, drive); break; |
| 711 | case 0x08: floppy_1308(regs, drive); break; |
| 712 | case 0x15: floppy_1315(regs, drive); break; |
| 713 | case 0x16: floppy_1316(regs, drive); break; |
| 714 | default: floppy_13XX(regs, drive); break; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | |
| 718 | // INT 0Eh Diskette Hardware ISR Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 719 | void VISIBLE16 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 720 | handle_0e(struct bregs *regs) |
| 721 | { |
Kevin O'Connor | c65a380 | 2008-03-02 13:58:23 -0500 | [diff] [blame] | 722 | //debug_isr(regs); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 723 | if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) { |
| 724 | outb(0x08, PORT_FD_DATA); // sense interrupt status |
| 725 | while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) |
| 726 | ; |
| 727 | do { |
| 728 | inb(PORT_FD_DATA); |
| 729 | } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0); |
| 730 | } |
| 731 | eoi_master_pic(); |
| 732 | // diskette interrupt has occurred |
| 733 | SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT); |
| 734 | } |
| 735 | |
| 736 | // Called from int08 handler. |
| 737 | void |
| 738 | floppy_tick() |
| 739 | { |
| 740 | // time to turn off drive(s)? |
| 741 | u8 fcount = GET_BDA(floppy_motor_counter); |
| 742 | if (fcount) { |
| 743 | fcount--; |
| 744 | SET_BDA(floppy_motor_counter, fcount); |
| 745 | if (fcount == 0) |
| 746 | // turn motor(s) off |
| 747 | outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR); |
| 748 | } |
| 749 | } |