src/emu/imagedev/floppy.c
| r13180 | r13181 | |
| 279 | 279 | } |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | | int floppy_image_device::find_position(int position, const UINT32 *buf, int buf_size) |
| 282 | int floppy_image_device::find_index(UINT32 position, const UINT32 *buf, int buf_size) |
| 283 | 283 | { |
| 284 | 284 | int spos = (buf_size >> 1)-1; |
| 285 | 285 | int step; |
| r13180 | r13181 | |
| 298 | 298 | } |
| 299 | 299 | } |
| 300 | 300 | |
| 301 | UINT32 floppy_image_device::find_position(attotime &base, attotime when) |
| 302 | { |
| 303 | base = revolution_start_time; |
| 304 | UINT32 revc = revolution_count; |
| 305 | attotime delta = when - base; |
| 306 | |
| 307 | while(delta >= rev_time) { |
| 308 | delta -= rev_time; |
| 309 | base += rev_time; |
| 310 | revc++; |
| 311 | } |
| 312 | |
| 313 | return (delta*(rpm/300)).as_ticks(1000000000); |
| 314 | } |
| 315 | |
| 301 | 316 | attotime floppy_image_device::get_next_transition(attotime from_when) |
| 302 | 317 | { |
| 303 | 318 | if(!image || mon) |
| r13180 | r13181 | |
| 307 | 322 | if(cells <= 1) |
| 308 | 323 | return attotime::never; |
| 309 | 324 | |
| 310 | | attotime base = revolution_start_time; |
| 311 | | UINT32 revc = revolution_count; |
| 312 | | attotime delta = from_when - base; |
| 325 | attotime base; |
| 326 | UINT32 position = find_position(base, from_when); |
| 313 | 327 | |
| 314 | | while(delta >= rev_time) { |
| 315 | | delta -= rev_time; |
| 316 | | base += rev_time; |
| 317 | | revc++; |
| 318 | | } |
| 319 | | int position = (delta*(rpm/300)).as_ticks(1000000000); |
| 320 | | |
| 321 | 328 | const UINT32 *buf = image->get_buffer(cyl, ss); |
| 322 | | int index = find_position(position, buf, cells); |
| 329 | int index = find_index(position, buf, cells); |
| 323 | 330 | |
| 324 | 331 | if(index == -1) |
| 325 | 332 | return attotime::never; |
| 326 | 333 | |
| 327 | | int next_position; |
| 334 | UINT32 next_position; |
| 328 | 335 | if(index < cells-1) |
| 329 | 336 | next_position = buf[index+1] & floppy_image::TIME_MASK; |
| 330 | 337 | else if((buf[index]^buf[0]) & floppy_image::MG_MASK) |
| r13180 | r13181 | |
| 332 | 339 | else |
| 333 | 340 | next_position = 200000000 + (buf[1] & floppy_image::TIME_MASK); |
| 334 | 341 | |
| 342 | |
| 335 | 343 | return base + attotime::from_nsec(next_position*(300/rpm)); |
| 336 | 344 | } |
| 345 | |
| 346 | void floppy_image_device::write_flux(attotime start, attotime end, int transition_count, const attotime *transitions) |
| 347 | { |
| 348 | attotime base; |
| 349 | int start_pos = find_position(base, start); |
| 350 | int end_pos = find_position(base, end); |
| 351 | |
| 352 | int *trans_pos = transition_count ? global_alloc_array(int, transition_count) : 0; |
| 353 | for(int i=0; i != transition_count; i++) |
| 354 | trans_pos[i] = find_position(base, transitions[i]); |
| 355 | |
| 356 | #if 0 |
| 357 | logerror("write %d-%d\n ", start_pos, end_pos); |
| 358 | for(int i=0; i != transition_count; i++) |
| 359 | logerror(" %d", trans_pos[i]); |
| 360 | logerror("\n"); |
| 361 | #endif |
| 362 | |
| 363 | int cells = image->get_track_size(cyl, ss); |
| 364 | UINT32 *buf = image->get_buffer(cyl, ss); |
| 365 | |
| 366 | int index; |
| 367 | if(cells) |
| 368 | index = find_index(start_pos, buf, cells); |
| 369 | else { |
| 370 | index = 0; |
| 371 | buf[cells++] = floppy_image::MG_N | 200000000; |
| 372 | } |
| 373 | |
| 374 | if(index && (buf[index] & floppy_image::TIME_MASK) == start_pos) |
| 375 | index--; |
| 376 | |
| 377 | UINT32 cur_mg = buf[index] & floppy_image::MG_MASK; |
| 378 | if(cur_mg == floppy_image::MG_N || cur_mg == floppy_image::MG_D) |
| 379 | cur_mg = floppy_image::MG_A; |
| 380 | |
| 381 | UINT32 pos = start_pos; |
| 382 | int ti = 0; |
| 383 | while(pos != end_pos) { |
| 384 | UINT32 next_pos; |
| 385 | if(ti != transition_count) |
| 386 | next_pos = trans_pos[ti++]; |
| 387 | else |
| 388 | next_pos = end_pos; |
| 389 | if(next_pos > pos) |
| 390 | write_zone(buf, cells, index, pos, next_pos, cur_mg); |
| 391 | else { |
| 392 | write_zone(buf, cells, index, pos, 200000000, cur_mg); |
| 393 | write_zone(buf, cells, index, 0, next_pos, cur_mg); |
| 394 | } |
| 395 | pos = next_pos; |
| 396 | cur_mg = cur_mg == floppy_image::MG_A ? floppy_image::MG_B : floppy_image::MG_A; |
| 397 | } |
| 398 | |
| 399 | image->set_track_size(cyl, ss, cells); |
| 400 | |
| 401 | if(trans_pos) |
| 402 | global_free(trans_pos); |
| 403 | } |
| 404 | |
| 405 | void floppy_image_device::write_zone(UINT32 *buf, int &cells, int &index, UINT32 spos, UINT32 epos, UINT32 mg) |
| 406 | { |
| 407 | while(spos < epos) { |
| 408 | while(index != cells-1 && (buf[index+1] & floppy_image::TIME_MASK) <= spos) |
| 409 | index++; |
| 410 | |
| 411 | UINT32 ref_start = buf[index] & floppy_image::TIME_MASK; |
| 412 | UINT32 ref_end = index == cells-1 ? 200000000 : buf[index+1] & floppy_image::TIME_MASK; |
| 413 | UINT32 ref_mg = buf[index] & floppy_image::MG_MASK; |
| 414 | |
| 415 | // Can't overwrite a damaged zone |
| 416 | if(ref_mg == floppy_image::MG_D) { |
| 417 | spos = ref_end; |
| 418 | continue; |
| 419 | } |
| 420 | |
| 421 | // If the zone is of the type we want, we don't need to touch it |
| 422 | if(ref_mg == mg) { |
| 423 | spos = ref_end; |
| 424 | continue; |
| 425 | } |
| 426 | |
| 427 | // Check the overlaps, act accordingly |
| 428 | if(spos == ref_start) { |
| 429 | if(epos >= ref_end) { |
| 430 | // Full overlap, that cell is dead, we need to see which ones we can extend |
| 431 | UINT32 prev_mg = index != 0 ? buf[index-1] & floppy_image::MG_MASK : ~0; |
| 432 | UINT32 next_mg = index != cells-1 ? buf[index+1] & floppy_image::MG_MASK : ~0; |
| 433 | if(prev_mg == mg) { |
| 434 | if(next_mg == mg) { |
| 435 | // Both match, merge all three in one |
| 436 | memmove(buf+index, buf+index+2, (cells-index-2)*sizeof(UINT32)); |
| 437 | cells -= 2; |
| 438 | index--; |
| 439 | |
| 440 | } else { |
| 441 | // Previous matches, drop the current cell |
| 442 | memmove(buf+index, buf+index+1, (cells-index-1)*sizeof(UINT32)); |
| 443 | cells --; |
| 444 | } |
| 445 | |
| 446 | } else { |
| 447 | if(next_mg == mg) { |
| 448 | // Following matches, extend it |
| 449 | memmove(buf+index, buf+index+1, (cells-index-1)*sizeof(UINT32)); |
| 450 | cells --; |
| 451 | buf[index] = mg | spos; |
| 452 | } else { |
| 453 | // None match, convert the current cell |
| 454 | buf[index] = mg | spos; |
| 455 | index++; |
| 456 | } |
| 457 | } |
| 458 | spos = ref_end; |
| 459 | |
| 460 | } else { |
| 461 | // Overlap at the start only |
| 462 | // Check if we can just extend the previous cell |
| 463 | if(index != 0 && (buf[index-1] & floppy_image::MG_MASK) == mg) |
| 464 | buf[index] = ref_mg | epos; |
| 465 | else { |
| 466 | // Otherwise we need to insert a new cell |
| 467 | if(index != cells-1) |
| 468 | memmove(buf+index+1, buf+index, (cells-index)*sizeof(UINT32)); |
| 469 | cells++; |
| 470 | buf[index] = mg | spos; |
| 471 | buf[index+1] = ref_mg | epos; |
| 472 | } |
| 473 | spos = epos; |
| 474 | } |
| 475 | |
| 476 | } else { |
| 477 | if(epos >= ref_end) { |
| 478 | // Overlap at the end only |
| 479 | // If we can't just extend the following cell, we need to insert a new one |
| 480 | if(index == cells-1 || (buf[index+1] & floppy_image::MG_MASK) != mg) { |
| 481 | if(index != cells-1) |
| 482 | memmove(buf+index+2, buf+index+1, (cells-index-1)*sizeof(UINT32)); |
| 483 | cells++; |
| 484 | } |
| 485 | buf[index+1] = mg | spos; |
| 486 | index++; |
| 487 | spos = ref_end; |
| 488 | |
| 489 | } else { |
| 490 | // Full inclusion |
| 491 | // We need to split the zone in 3 |
| 492 | if(index != cells-1) |
| 493 | memmove(buf+index+3, buf+index+1, (cells-index-1)*sizeof(UINT32)); |
| 494 | cells += 2; |
| 495 | buf[index+1] = mg | spos; |
| 496 | buf[index+2] = ref_mg | epos; |
| 497 | spos = epos; |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | } |
| 502 | } |
src/mess/machine/wd1772.c
| r13180 | r13181 | |
| 285 | 285 | if(command & 4) { |
| 286 | 286 | sub_state = SETTLE_WAIT; |
| 287 | 287 | delay_cycles(t_gen, 120000); |
| 288 | return; |
| 288 | 289 | } else { |
| 289 | | sub_state = SCAN_ID; |
| 290 | | counter = 0; |
| 291 | | live_start(SEARCH_ADDRESS_MARK); |
| 290 | sub_state = SETTLE_DONE; |
| 291 | break; |
| 292 | 292 | } |
| 293 | | return; |
| 294 | 293 | |
| 295 | 294 | case SETTLE_WAIT: |
| 296 | 295 | return; |
| 297 | 296 | |
| 297 | case SETTLE_DONE: |
| 298 | sub_state = SCAN_ID; |
| 299 | counter = 0; |
| 300 | live_start(SEARCH_ADDRESS_MARK); |
| 301 | return; |
| 302 | |
| 298 | 303 | case SCAN_ID: |
| 299 | 304 | if(cur_live.idbuf[0] != track || cur_live.idbuf[2] != sector) { |
| 300 | 305 | live_start(SEARCH_ADDRESS_MARK); |
| r13180 | r13181 | |
| 361 | 366 | return; |
| 362 | 367 | |
| 363 | 368 | case SPINUP_DONE: |
| 364 | | sub_state = WAIT_INDEX; |
| 365 | | return; |
| 369 | if(command & 4) { |
| 370 | sub_state = SETTLE_WAIT; |
| 371 | delay_cycles(t_gen, 120000); |
| 372 | return; |
| 366 | 373 | |
| 374 | } else { |
| 375 | sub_state = SETTLE_DONE; |
| 376 | break; |
| 377 | } |
| 378 | |
| 367 | 379 | case SETTLE_WAIT: |
| 368 | 380 | return; |
| 369 | 381 | |
| 382 | case SETTLE_DONE: |
| 383 | sub_state = WAIT_INDEX; |
| 384 | return; |
| 385 | |
| 370 | 386 | case WAIT_INDEX: |
| 371 | 387 | return; |
| 372 | 388 | |
| 373 | 389 | case WAIT_INDEX_DONE: |
| 374 | 390 | sub_state = TRACK_DONE; |
| 375 | 391 | live_start(READ_TRACK_DATA); |
| 392 | cur_live.pll.start_writing(machine().time()); |
| 376 | 393 | return; |
| 377 | 394 | |
| 378 | 395 | case TRACK_DONE: |
| r13180 | r13181 | |
| 415 | 432 | if(command & 4) { |
| 416 | 433 | sub_state = SETTLE_WAIT; |
| 417 | 434 | delay_cycles(t_gen, 120000); |
| 435 | return; |
| 418 | 436 | } else { |
| 419 | | sub_state = SCAN_ID; |
| 420 | | counter = 0; |
| 421 | | live_start(SEARCH_ADDRESS_MARK); |
| 437 | sub_state = SETTLE_DONE; |
| 438 | break; |
| 422 | 439 | } |
| 423 | | return; |
| 424 | 440 | |
| 425 | 441 | case SETTLE_WAIT: |
| 426 | 442 | return; |
| r13180 | r13181 | |
| 441 | 457 | return; |
| 442 | 458 | |
| 443 | 459 | default: |
| 444 | | logerror("%s: read track unknown sub-state %d\n", ttsn().cstr(), sub_state); |
| 460 | logerror("%s: read id unknown sub-state %d\n", ttsn().cstr(), sub_state); |
| 445 | 461 | return; |
| 446 | 462 | } |
| 447 | 463 | } |
| 448 | 464 | } |
| 449 | 465 | |
| 466 | void wd1772_t::write_track_start() |
| 467 | { |
| 468 | main_state = WRITE_TRACK; |
| 469 | status = (status & ~(S_WP|S_DDM|S_LOST|S_RNF)) | S_BUSY; |
| 470 | drop_drq(); |
| 471 | sub_state = SPINUP; |
| 472 | status_type_1 = false; |
| 473 | write_track_continue(); |
| 474 | } |
| 475 | |
| 476 | void wd1772_t::write_track_continue() |
| 477 | { |
| 478 | for(;;) { |
| 479 | switch(sub_state) { |
| 480 | case SPINUP: |
| 481 | if(!(status & S_MON)) { |
| 482 | spinup(); |
| 483 | return; |
| 484 | } |
| 485 | sub_state = SPINUP_DONE; |
| 486 | break; |
| 487 | |
| 488 | case SPINUP_WAIT: |
| 489 | return; |
| 490 | |
| 491 | case SPINUP_DONE: |
| 492 | if(command & 4) { |
| 493 | sub_state = SETTLE_WAIT; |
| 494 | delay_cycles(t_gen, 120000); |
| 495 | return; |
| 496 | } else { |
| 497 | sub_state = SETTLE_DONE; |
| 498 | break; |
| 499 | } |
| 500 | |
| 501 | case SETTLE_WAIT: |
| 502 | return; |
| 503 | |
| 504 | case SETTLE_DONE: |
| 505 | set_drq(); |
| 506 | sub_state = DATA_LOAD_WAIT; |
| 507 | delay_cycles(t_gen, 768); |
| 508 | return; |
| 509 | |
| 510 | case DATA_LOAD_WAIT: |
| 511 | return; |
| 512 | |
| 513 | case DATA_LOAD_WAIT_DONE: |
| 514 | if(drq) { |
| 515 | status |= S_LOST; |
| 516 | command_end(); |
| 517 | return; |
| 518 | } |
| 519 | sub_state = WAIT_INDEX; |
| 520 | break; |
| 521 | |
| 522 | case WAIT_INDEX: |
| 523 | return; |
| 524 | |
| 525 | case WAIT_INDEX_DONE: |
| 526 | sub_state = TRACK_DONE; |
| 527 | live_start(WRITE_TRACK_DATA); |
| 528 | return; |
| 529 | |
| 530 | case TRACK_DONE: |
| 531 | command_end(); |
| 532 | return; |
| 533 | |
| 534 | default: |
| 535 | logerror("%s: write track unknown sub-state %d\n", ttsn().cstr(), sub_state); |
| 536 | return; |
| 537 | } |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | |
| 542 | void wd1772_t::write_sector_start() |
| 543 | { |
| 544 | main_state = WRITE_SECTOR; |
| 545 | status = (status & ~(S_CRC|S_LOST|S_RNF|S_WP|S_DDM)) | S_BUSY; |
| 546 | drop_drq(); |
| 547 | sub_state = SPINUP; |
| 548 | status_type_1 = false; |
| 549 | write_sector_continue(); |
| 550 | } |
| 551 | |
| 552 | void wd1772_t::write_sector_continue() |
| 553 | { |
| 554 | static int size_codes[4] = { 128, 256, 512, 1024 }; |
| 555 | |
| 556 | for(;;) { |
| 557 | switch(sub_state) { |
| 558 | case SPINUP: |
| 559 | if(!(status & S_MON)) { |
| 560 | spinup(); |
| 561 | return; |
| 562 | } |
| 563 | sub_state = SPINUP_DONE; |
| 564 | break; |
| 565 | |
| 566 | case SPINUP_WAIT: |
| 567 | return; |
| 568 | |
| 569 | case SPINUP_DONE: |
| 570 | if(command & 4) { |
| 571 | sub_state = SETTLE_WAIT; |
| 572 | delay_cycles(t_gen, 120000); |
| 573 | return; |
| 574 | } else { |
| 575 | sub_state = SETTLE_DONE; |
| 576 | break; |
| 577 | } |
| 578 | |
| 579 | case SETTLE_WAIT: |
| 580 | return; |
| 581 | |
| 582 | case SETTLE_DONE: |
| 583 | sub_state = SCAN_ID; |
| 584 | counter = 0; |
| 585 | live_start(SEARCH_ADDRESS_MARK); |
| 586 | return; |
| 587 | |
| 588 | case SCAN_ID: |
| 589 | if(cur_live.idbuf[0] != track || cur_live.idbuf[2] != sector) { |
| 590 | live_start(SEARCH_ADDRESS_MARK); |
| 591 | return; |
| 592 | } |
| 593 | if(cur_live.crc) { |
| 594 | status |= S_CRC; |
| 595 | live_start(SEARCH_ADDRESS_MARK); |
| 596 | return; |
| 597 | } |
| 598 | sector_size = size_codes[cur_live.idbuf[3] & 3]; |
| 599 | sub_state = SECTOR_WRITE; |
| 600 | live_start(WRITE_SECTOR_PRE); |
| 601 | return; |
| 602 | |
| 603 | case SCAN_ID_FAILED: |
| 604 | status |= S_RNF; |
| 605 | command_end(); |
| 606 | return; |
| 607 | |
| 608 | case SECTOR_WRITE: |
| 609 | if(command & 0x10) { |
| 610 | sector++; |
| 611 | sub_state = SPINUP_DONE; |
| 612 | } else { |
| 613 | command_end(); |
| 614 | return; |
| 615 | } |
| 616 | break; |
| 617 | |
| 618 | default: |
| 619 | logerror("%s: write sector unknown sub-state %d\n", ttsn().cstr(), sub_state); |
| 620 | return; |
| 621 | } |
| 622 | } |
| 623 | } |
| 624 | |
| 450 | 625 | void wd1772_t::interrupt_start() |
| 451 | 626 | { |
| 452 | 627 | if(status & S_BUSY) { |
| r13180 | r13181 | |
| 465 | 640 | { |
| 466 | 641 | if(cur_live.state != IDLE) { |
| 467 | 642 | live_run(); |
| 468 | | return; |
| 643 | if(cur_live.state != IDLE) |
| 644 | return; |
| 469 | 645 | } |
| 470 | 646 | |
| 471 | 647 | switch(main_state) { |
| r13180 | r13181 | |
| 483 | 659 | case READ_ID: |
| 484 | 660 | read_id_continue(); |
| 485 | 661 | break; |
| 662 | case WRITE_TRACK: |
| 663 | write_track_continue(); |
| 664 | break; |
| 665 | case WRITE_SECTOR: |
| 666 | write_sector_continue(); |
| 667 | break; |
| 486 | 668 | default: |
| 487 | 669 | logerror("%s: general_continue on unknown main-state %d\n", ttsn().cstr(), main_state); |
| 488 | 670 | break; |
| r13180 | r13181 | |
| 509 | 691 | sub_state = SEEK_WAIT_STABILIZATION_TIME_DONE; |
| 510 | 692 | break; |
| 511 | 693 | |
| 694 | case DATA_LOAD_WAIT: |
| 695 | sub_state = DATA_LOAD_WAIT_DONE; |
| 696 | break; |
| 697 | |
| 512 | 698 | default: |
| 513 | 699 | if(cur_live.tm.is_never()) |
| 514 | 700 | logerror("%s: do_generic on unknown sub-state %d\n", ttsn().cstr(), sub_state); |
| r13180 | r13181 | |
| 535 | 721 | case 0x40: case 0x50: logerror("wd1772: step +\n"); last_dir = 0; seek_start(STEP); break; |
| 536 | 722 | case 0x60: case 0x70: logerror("wd1772: step -\n"); last_dir = 1; seek_start(STEP); break; |
| 537 | 723 | case 0x80: case 0x90: logerror("wd1772: read sector%s %d, %d\n", command & 0x10 ? " multiple" : "", track, sector); read_sector_start(); break; |
| 724 | case 0xa0: case 0xb0: logerror("wd1772: write sector%s %d, %d\n", command & 0x10 ? " multiple" : "", track, sector); write_sector_start(); break; |
| 538 | 725 | case 0xc0: logerror("wd1772: read id\n"); read_id_start(); break; |
| 539 | 726 | case 0xd0: logerror("wd1772: interrupt\n"); interrupt_start(); break; |
| 540 | 727 | case 0xe0: logerror("wd1772: read track %d\n", track); read_track_start(); break; |
| 541 | | |
| 542 | | default: |
| 543 | | logerror("%s: Unhandled command %02x\n", ttsn().cstr(), command); |
| 544 | | break; |
| 728 | case 0xf0: logerror("wd1772: write track %d\n", track); write_track_start(); break; |
| 545 | 729 | } |
| 546 | 730 | } |
| 547 | 731 | |
| r13180 | r13181 | |
| 724 | 908 | case SPINUP_DONE: |
| 725 | 909 | case SETTLE_WAIT: |
| 726 | 910 | case SETTLE_DONE: |
| 911 | case DATA_LOAD_WAIT: |
| 912 | case DATA_LOAD_WAIT_DONE: |
| 727 | 913 | case SEEK_MOVE: |
| 728 | 914 | case SEEK_WAIT_STEP_TIME: |
| 729 | 915 | case SEEK_WAIT_STEP_TIME_DONE: |
| r13180 | r13181 | |
| 733 | 919 | case WAIT_INDEX_DONE: |
| 734 | 920 | case SCAN_ID_FAILED: |
| 735 | 921 | case SECTOR_READ: |
| 922 | case SECTOR_WRITE: |
| 736 | 923 | break; |
| 737 | 924 | |
| 738 | 925 | case SCAN_ID: |
| r13180 | r13181 | |
| 777 | 964 | cur_live.bit_counter = 0; |
| 778 | 965 | cur_live.data_separator_phase = false; |
| 779 | 966 | cur_live.data_reg = 0; |
| 967 | cur_live.previous_type = live_info::PT_NONE; |
| 968 | cur_live.data_bit_context = false; |
| 969 | cur_live.byte_counter = 0; |
| 780 | 970 | cur_live.pll.reset(cur_live.tm); |
| 781 | 971 | cur_live.pll.set_clock(clocks_to_attotime(1)); |
| 782 | 972 | checkpoint_live = cur_live; |
| r13180 | r13181 | |
| 786 | 976 | |
| 787 | 977 | void wd1772_t::checkpoint() |
| 788 | 978 | { |
| 979 | cur_live.pll.commit(floppy, cur_live.tm); |
| 789 | 980 | checkpoint_live = cur_live; |
| 790 | 981 | } |
| 791 | 982 | |
| r13180 | r13181 | |
| 807 | 998 | // fprintf(stderr, "%s: Rolling back and replaying (%s)\n", ttsn().cstr(), tts(cur_live.tm).cstr()); |
| 808 | 999 | rollback(); |
| 809 | 1000 | live_run(machine().time()); |
| 1001 | cur_live.pll.commit(floppy, cur_live.tm); |
| 810 | 1002 | } else { |
| 811 | 1003 | // fprintf(stderr, "%s: Committing (%s)\n", ttsn().cstr(), tts(cur_live.tm).cstr()); |
| 1004 | cur_live.pll.commit(floppy, cur_live.tm); |
| 812 | 1005 | if(cur_live.next_state != -1) |
| 813 | 1006 | cur_live.state = cur_live.next_state; |
| 814 | | if(cur_live.state == IDLE) |
| 1007 | if(cur_live.state == IDLE) { |
| 1008 | cur_live.pll.stop_writing(floppy, cur_live.tm); |
| 815 | 1009 | cur_live.tm = attotime::never; |
| 1010 | } |
| 816 | 1011 | } |
| 817 | 1012 | cur_live.next_state = -1; |
| 818 | 1013 | checkpoint(); |
| r13180 | r13181 | |
| 821 | 1016 | |
| 822 | 1017 | void wd1772_t::live_abort() |
| 823 | 1018 | { |
| 1019 | if(cur_live.tm > machine().time()) { |
| 1020 | rollback(); |
| 1021 | live_run(machine().time()); |
| 1022 | } |
| 1023 | |
| 1024 | cur_live.pll.stop_writing(floppy, cur_live.tm); |
| 824 | 1025 | cur_live.tm = attotime::never; |
| 825 | 1026 | cur_live.state = IDLE; |
| 826 | 1027 | cur_live.next_state = -1; |
| r13180 | r13181 | |
| 844 | 1045 | return false; |
| 845 | 1046 | } |
| 846 | 1047 | |
| 1048 | bool wd1772_t::write_one_bit(attotime limit) |
| 1049 | { |
| 1050 | bool bit = cur_live.shift_reg & 0x8000; |
| 1051 | if(cur_live.pll.write_next_bit(bit, cur_live.tm, floppy, limit)) |
| 1052 | return true; |
| 1053 | if(cur_live.bit_counter & 1) { |
| 1054 | if((cur_live.crc ^ (bit ? 0x8000 : 0x0000)) & 0x8000) |
| 1055 | cur_live.crc = (cur_live.crc << 1) ^ 0x1021; |
| 1056 | else |
| 1057 | cur_live.crc = cur_live.crc << 1; |
| 1058 | } |
| 1059 | cur_live.shift_reg = cur_live.shift_reg << 1; |
| 1060 | cur_live.bit_counter--; |
| 1061 | return false; |
| 1062 | } |
| 1063 | |
| 1064 | void wd1772_t::live_write_raw(UINT16 raw) |
| 1065 | { |
| 1066 | // logerror("write %04x %04x\n", raw, cur_live.crc); |
| 1067 | cur_live.shift_reg = raw; |
| 1068 | cur_live.data_bit_context = raw & 1; |
| 1069 | } |
| 1070 | |
| 1071 | void wd1772_t::live_write_mfm(UINT8 mfm) |
| 1072 | { |
| 1073 | bool context = cur_live.data_bit_context; |
| 1074 | UINT16 raw = 0; |
| 1075 | for(int i=0; i<8; i++) { |
| 1076 | bool bit = mfm & (0x80 >> i); |
| 1077 | if(!(bit || context)) |
| 1078 | raw |= 0x8000 >> (2*i); |
| 1079 | if(bit) |
| 1080 | raw |= 0x4000 >> (2*i); |
| 1081 | context = bit; |
| 1082 | } |
| 1083 | cur_live.shift_reg = raw; |
| 1084 | cur_live.data_bit_context = context; |
| 1085 | // logerror("write %02x %04x %04x\n", mfm, cur_live.crc, raw); |
| 1086 | } |
| 1087 | |
| 847 | 1088 | void wd1772_t::live_run(attotime limit) |
| 848 | 1089 | { |
| 849 | 1090 | if(cur_live.state == IDLE || cur_live.next_state != -1) |
| r13180 | r13181 | |
| 1047 | 1288 | checkpoint(); |
| 1048 | 1289 | break; |
| 1049 | 1290 | |
| 1291 | case WRITE_TRACK_DATA: |
| 1292 | if(drq) { |
| 1293 | status |= S_LOST; |
| 1294 | data = 0; |
| 1295 | } |
| 1296 | switch(data) { |
| 1297 | case 0xf5: |
| 1298 | if(cur_live.previous_type != live_info::PT_SYNC) |
| 1299 | cur_live.crc = 0xffff; |
| 1300 | live_write_raw(0x4489); |
| 1301 | cur_live.previous_type = live_info::PT_SYNC; |
| 1302 | break; |
| 1303 | case 0xf6: |
| 1304 | cur_live.previous_type = live_info::PT_NONE; |
| 1305 | live_write_raw(0x5224); |
| 1306 | break; |
| 1307 | case 0xf7: |
| 1308 | if(cur_live.previous_type == live_info::PT_CRC_2) { |
| 1309 | cur_live.previous_type = live_info::PT_NONE; |
| 1310 | live_write_mfm(0xf7); |
| 1311 | } else { |
| 1312 | cur_live.previous_type = live_info::PT_CRC_1; |
| 1313 | live_write_mfm(cur_live.crc >> 8); |
| 1314 | } |
| 1315 | break; |
| 1316 | default: |
| 1317 | cur_live.previous_type = live_info::PT_NONE; |
| 1318 | live_write_mfm(data); |
| 1319 | break; |
| 1320 | } |
| 1321 | set_drq(); |
| 1322 | cur_live.state = WRITE_BYTE; |
| 1323 | cur_live.bit_counter = 16; |
| 1324 | checkpoint(); |
| 1325 | break; |
| 1326 | |
| 1327 | case WRITE_BYTE: |
| 1328 | if(write_one_bit(limit)) |
| 1329 | return; |
| 1330 | if(cur_live.bit_counter == 0) { |
| 1331 | live_delay(WRITE_BYTE_DONE); |
| 1332 | return; |
| 1333 | } |
| 1334 | break; |
| 1335 | |
| 1336 | case WRITE_BYTE_DONE: |
| 1337 | switch(sub_state) { |
| 1338 | case TRACK_DONE: |
| 1339 | if(cur_live.previous_type == live_info::PT_CRC_1) { |
| 1340 | cur_live.previous_type = live_info::PT_CRC_2; |
| 1341 | live_write_mfm(cur_live.crc >> 8); |
| 1342 | cur_live.state = WRITE_BYTE; |
| 1343 | cur_live.bit_counter = 16; |
| 1344 | checkpoint(); |
| 1345 | } else |
| 1346 | cur_live.state = WRITE_TRACK_DATA; |
| 1347 | break; |
| 1348 | |
| 1349 | case SECTOR_WRITE: |
| 1350 | cur_live.state = WRITE_BYTE; |
| 1351 | cur_live.bit_counter = 16; |
| 1352 | cur_live.byte_counter++; |
| 1353 | if(cur_live.byte_counter <= 11) |
| 1354 | live_write_mfm(0x00); |
| 1355 | else if(cur_live.byte_counter == 12) { |
| 1356 | cur_live.crc = 0xffff; |
| 1357 | live_write_raw(0x4489); |
| 1358 | } else if(cur_live.byte_counter <= 14) |
| 1359 | live_write_raw(0x4489); |
| 1360 | else if(cur_live.byte_counter == 15) |
| 1361 | live_write_mfm(command & 1 ? 0xf8 : 0xfb); |
| 1362 | else if(cur_live.byte_counter <= sector_size + 16-2) { |
| 1363 | if(drq) { |
| 1364 | status |= S_LOST; |
| 1365 | data = 0; |
| 1366 | } |
| 1367 | live_write_mfm(data); |
| 1368 | set_drq(); |
| 1369 | } else if(cur_live.byte_counter == sector_size + 16-1) { |
| 1370 | if(drq) { |
| 1371 | status |= S_LOST; |
| 1372 | data = 0; |
| 1373 | } |
| 1374 | live_write_mfm(data); |
| 1375 | } else if(cur_live.byte_counter <= sector_size + 16+1) |
| 1376 | live_write_mfm(cur_live.crc >> 8); |
| 1377 | else if(cur_live.byte_counter == sector_size + 16+2) |
| 1378 | // Is that correct? It seems required (try ST formatting) |
| 1379 | live_write_mfm(0xff); |
| 1380 | else { |
| 1381 | cur_live.pll.stop_writing(floppy, cur_live.tm); |
| 1382 | cur_live.state = IDLE; |
| 1383 | return; |
| 1384 | } |
| 1385 | |
| 1386 | checkpoint(); |
| 1387 | break; |
| 1388 | |
| 1389 | default: |
| 1390 | logerror("%s: Unknown sub state %d in WRITE_BYTE_DONE\n", tts(cur_live.tm).cstr(), sub_state); |
| 1391 | live_abort(); |
| 1392 | return; |
| 1393 | } |
| 1394 | break; |
| 1395 | |
| 1396 | case WRITE_SECTOR_PRE: |
| 1397 | if(read_one_bit(limit)) |
| 1398 | return; |
| 1399 | if(cur_live.bit_counter != 16) |
| 1400 | break; |
| 1401 | live_delay(WRITE_SECTOR_PRE_BYTE); |
| 1402 | return; |
| 1403 | |
| 1404 | case WRITE_SECTOR_PRE_BYTE: |
| 1405 | cur_live.state = WRITE_SECTOR_PRE; |
| 1406 | cur_live.byte_counter++; |
| 1407 | cur_live.bit_counter = 0; |
| 1408 | switch(cur_live.byte_counter) { |
| 1409 | case 2: |
| 1410 | set_drq(); |
| 1411 | checkpoint(); |
| 1412 | break; |
| 1413 | case 11: |
| 1414 | if(drq) { |
| 1415 | status |= S_LOST; |
| 1416 | cur_live.state = IDLE; |
| 1417 | return; |
| 1418 | } |
| 1419 | break; |
| 1420 | case 22: |
| 1421 | cur_live.state = WRITE_BYTE; |
| 1422 | cur_live.bit_counter = 16; |
| 1423 | cur_live.byte_counter = 0; |
| 1424 | cur_live.data_bit_context = cur_live.data_reg & 1; |
| 1425 | cur_live.pll.start_writing(cur_live.tm); |
| 1426 | live_write_mfm(0x00); |
| 1427 | break; |
| 1428 | } |
| 1429 | break; |
| 1430 | |
| 1050 | 1431 | default: |
| 1051 | 1432 | logerror("%s: Unknown live state %d\n", tts(cur_live.tm).cstr(), cur_live.state); |
| 1052 | 1433 | return; |
| r13180 | r13181 | |
| 1092 | 1473 | phase_sub = 0x00; |
| 1093 | 1474 | freq_add = 0x00; |
| 1094 | 1475 | freq_sub = 0x00; |
| 1476 | write_position = 0; |
| 1477 | write_start_time = attotime::never; |
| 1095 | 1478 | } |
| 1096 | 1479 | |
| 1097 | 1480 | int wd1772_t::pll_t::get_next_bit(attotime &tm, floppy_image_device *floppy, attotime limit) |
| r13180 | r13181 | |
| 1131 | 1514 | if(counter & 0x800) |
| 1132 | 1515 | break; |
| 1133 | 1516 | } |
| 1134 | | // fprintf(stderr, "first transition, time=%03x, inc=%3d\n", transition_time, increment); |
| 1517 | // fprintf(stderr, "first transition, time=%03x, inc=%3d\n", transition_time, increment); |
| 1135 | 1518 | int bit = transition_time != 0xffff; |
| 1136 | 1519 | |
| 1137 | 1520 | if(transition_time != 0xffff) { |
| r13180 | r13181 | |
| 1173 | 1556 | |
| 1174 | 1557 | return bit; |
| 1175 | 1558 | } |
| 1559 | |
| 1560 | void wd1772_t::pll_t::start_writing(attotime tm) |
| 1561 | { |
| 1562 | write_start_time = tm; |
| 1563 | write_position = 0; |
| 1564 | } |
| 1565 | |
| 1566 | void wd1772_t::pll_t::stop_writing(floppy_image_device *floppy, attotime tm) |
| 1567 | { |
| 1568 | commit(floppy, tm); |
| 1569 | write_start_time = attotime::never; |
| 1570 | } |
| 1571 | |
| 1572 | bool wd1772_t::pll_t::write_next_bit(bool bit, attotime &tm, floppy_image_device *floppy, attotime limit) |
| 1573 | { |
| 1574 | if(write_start_time == attotime::never) { |
| 1575 | write_start_time = ctime; |
| 1576 | write_position = 0; |
| 1577 | } |
| 1578 | |
| 1579 | for(;;) { |
| 1580 | attotime etime = ctime+delays[slot]; |
| 1581 | if(etime > limit) |
| 1582 | return true; |
| 1583 | UINT16 pre_counter = counter; |
| 1584 | counter += increment; |
| 1585 | if(bit && !(pre_counter & 0x400) && (counter & 0x400)) |
| 1586 | if(write_position < ARRAY_LENGTH(write_buffer)) |
| 1587 | write_buffer[write_position++] = etime; |
| 1588 | slot++; |
| 1589 | tm = etime; |
| 1590 | if(counter & 0x800) |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | counter &= 0x7ff; |
| 1595 | |
| 1596 | ctime = tm; |
| 1597 | slot = 0; |
| 1598 | |
| 1599 | return false; |
| 1600 | } |
| 1601 | |
| 1602 | void wd1772_t::pll_t::commit(floppy_image_device *floppy, attotime tm) |
| 1603 | { |
| 1604 | if(write_start_time == attotime::never || tm == write_start_time) |
| 1605 | return; |
| 1606 | |
| 1607 | if(floppy) |
| 1608 | floppy->write_flux(write_start_time, tm, write_position, write_buffer); |
| 1609 | write_start_time = tm; |
| 1610 | write_position = 0; |
| 1611 | } |