Previous 15994 Revisions Next

r13181 Sunday 6th November, 2011 at 18:46:27 UTC by Olivier Galibert
floppy,wd1772: First stab at write support, in-memory only for now [O. Galibert]
[src/emu/imagedev]floppy.c floppy.h
[src/mess/machine]wd1772.c wd1772.h

src/emu/imagedev/floppy.c
r13180r13181
279279   }
280280}
281281
282int floppy_image_device::find_position(int position, const UINT32 *buf, int buf_size)
282int floppy_image_device::find_index(UINT32 position, const UINT32 *buf, int buf_size)
283283{
284284   int spos = (buf_size >> 1)-1;
285285   int step;
r13180r13181
298298   }
299299}
300300
301UINT32 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
301316attotime floppy_image_device::get_next_transition(attotime from_when)
302317{
303318   if(!image || mon)
r13180r13181
307322   if(cells <= 1)
308323      return attotime::never;
309324
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);
313327
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
321328   const UINT32 *buf = image->get_buffer(cyl, ss);
322   int index = find_position(position, buf, cells);
329   int index = find_index(position, buf, cells);
323330
324331   if(index == -1)
325332      return attotime::never;
326333
327   int next_position;
334   UINT32 next_position;
328335   if(index < cells-1)
329336      next_position = buf[index+1] & floppy_image::TIME_MASK;
330337   else if((buf[index]^buf[0]) & floppy_image::MG_MASK)
r13180r13181
332339   else
333340      next_position = 200000000 + (buf[1] & floppy_image::TIME_MASK);
334341
342
335343   return base + attotime::from_nsec(next_position*(300/rpm));
336344}
345
346void 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
405void 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/emu/imagedev/floppy.h
r13180r13181
4646
4747   virtual iodevice_t image_type() const { return IO_FLOPPY; }
4848
49   virtual bool is_readable()  const { return 1; }
50   virtual bool is_writeable() const { return 1; }
51   virtual bool is_creatable() const { return 0; }
52   virtual bool must_be_loaded() const { return 0; }
53   virtual bool is_reset_on_load() const { return 0; }
49   virtual bool is_readable()  const { return true; }
50   virtual bool is_writeable() const { return true; }
51   virtual bool is_creatable() const { return true; }
52   virtual bool must_be_loaded() const { return false; }
53   virtual bool is_reset_on_load() const { return false; }
5454   virtual const char *file_extensions() const { return extension_list; }
5555   virtual const option_guide *create_option_guide() const { return NULL; }
5656
r13180r13181
7777   void index_resync();
7878   attotime time_next_index();
7979   attotime get_next_transition(attotime from_when);
80   void write_flux(attotime start, attotime end, int transition_count, const attotime *transitions);
8081
8182protected:
8283   // device-level overrides
r13180r13181
120121   unload_cb cur_unload_cb;
121122   index_pulse_cb cur_index_pulse_cb;
122123
123   int find_position(int position, const UINT32 *buf, int buf_size);
124   UINT32 find_position(attotime &base, attotime when);
125   int find_index(UINT32 position, const UINT32 *buf, int buf_size);
126   void write_zone(UINT32 *buf, int &cells, int &index, UINT32 spos, UINT32 epos, UINT32 mg);
124127};
125128
126129// device type definition
src/mess/machine/wd1772.c
r13180r13181
285285         if(command & 4) {
286286            sub_state = SETTLE_WAIT;
287287            delay_cycles(t_gen, 120000);
288            return;
288289         } else {
289            sub_state = SCAN_ID;
290            counter = 0;
291            live_start(SEARCH_ADDRESS_MARK);
290            sub_state = SETTLE_DONE;
291            break;
292292         }
293         return;
294293
295294      case SETTLE_WAIT:
296295         return;
297296
297      case SETTLE_DONE:
298         sub_state = SCAN_ID;
299         counter = 0;
300         live_start(SEARCH_ADDRESS_MARK);
301         return;
302
298303      case SCAN_ID:
299304         if(cur_live.idbuf[0] != track || cur_live.idbuf[2] != sector) {
300305            live_start(SEARCH_ADDRESS_MARK);
r13180r13181
361366         return;
362367
363368      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;
366373
374         } else {
375            sub_state = SETTLE_DONE;
376            break;
377         }
378
367379      case SETTLE_WAIT:
368380         return;
369381
382      case SETTLE_DONE:
383         sub_state = WAIT_INDEX;
384         return;
385
370386      case WAIT_INDEX:
371387         return;
372388
373389      case WAIT_INDEX_DONE:
374390         sub_state = TRACK_DONE;
375391         live_start(READ_TRACK_DATA);
392         cur_live.pll.start_writing(machine().time());
376393         return;
377394
378395      case TRACK_DONE:
r13180r13181
415432         if(command & 4) {
416433            sub_state = SETTLE_WAIT;
417434            delay_cycles(t_gen, 120000);
435            return;
418436         } else {
419            sub_state = SCAN_ID;
420            counter = 0;
421            live_start(SEARCH_ADDRESS_MARK);
437            sub_state = SETTLE_DONE;
438            break;
422439         }
423         return;
424440
425441      case SETTLE_WAIT:
426442         return;
r13180r13181
441457         return;
442458
443459      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);
445461         return;
446462      }
447463   }
448464}
449465
466void 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
476void 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
542void 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
552void 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
450625void wd1772_t::interrupt_start()
451626{
452627   if(status & S_BUSY) {
r13180r13181
465640{
466641   if(cur_live.state != IDLE) {
467642      live_run();
468      return;
643      if(cur_live.state != IDLE)
644         return;
469645   }
470646
471647   switch(main_state) {
r13180r13181
483659   case READ_ID:
484660      read_id_continue();
485661      break;
662   case WRITE_TRACK:
663      write_track_continue();
664      break;
665   case WRITE_SECTOR:
666      write_sector_continue();
667      break;
486668   default:
487669      logerror("%s: general_continue on unknown main-state %d\n", ttsn().cstr(), main_state);
488670      break;
r13180r13181
509691      sub_state = SEEK_WAIT_STABILIZATION_TIME_DONE;
510692      break;
511693
694   case DATA_LOAD_WAIT:
695      sub_state = DATA_LOAD_WAIT_DONE;
696      break;
697
512698   default:
513699      if(cur_live.tm.is_never())
514700         logerror("%s: do_generic on unknown sub-state %d\n", ttsn().cstr(), sub_state);
r13180r13181
535721   case 0x40: case 0x50: logerror("wd1772: step +\n"); last_dir = 0; seek_start(STEP); break;
536722   case 0x60: case 0x70: logerror("wd1772: step -\n"); last_dir = 1; seek_start(STEP); break;
537723   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;
538725   case 0xc0: logerror("wd1772: read id\n"); read_id_start(); break;
539726   case 0xd0: logerror("wd1772: interrupt\n"); interrupt_start(); break;
540727   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;
545729   }
546730}
547731
r13180r13181
724908   case SPINUP_DONE:
725909   case SETTLE_WAIT:
726910   case SETTLE_DONE:
911   case DATA_LOAD_WAIT:
912   case DATA_LOAD_WAIT_DONE:
727913   case SEEK_MOVE:
728914   case SEEK_WAIT_STEP_TIME:
729915   case SEEK_WAIT_STEP_TIME_DONE:
r13180r13181
733919   case WAIT_INDEX_DONE:
734920   case SCAN_ID_FAILED:
735921   case SECTOR_READ:
922   case SECTOR_WRITE:
736923      break;
737924
738925   case SCAN_ID:
r13180r13181
777964   cur_live.bit_counter = 0;
778965   cur_live.data_separator_phase = false;
779966   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;
780970   cur_live.pll.reset(cur_live.tm);
781971   cur_live.pll.set_clock(clocks_to_attotime(1));
782972   checkpoint_live = cur_live;
r13180r13181
786976
787977void wd1772_t::checkpoint()
788978{
979   cur_live.pll.commit(floppy, cur_live.tm);
789980   checkpoint_live = cur_live;
790981}
791982
r13180r13181
807998         //          fprintf(stderr, "%s: Rolling back and replaying (%s)\n", ttsn().cstr(), tts(cur_live.tm).cstr());
808999         rollback();
8091000         live_run(machine().time());
1001         cur_live.pll.commit(floppy, cur_live.tm);
8101002      } else {
8111003         //          fprintf(stderr, "%s: Committing (%s)\n", ttsn().cstr(), tts(cur_live.tm).cstr());
1004         cur_live.pll.commit(floppy, cur_live.tm);
8121005         if(cur_live.next_state != -1)
8131006            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);
8151009            cur_live.tm = attotime::never;
1010         }
8161011      }
8171012      cur_live.next_state = -1;
8181013      checkpoint();
r13180r13181
8211016
8221017void wd1772_t::live_abort()
8231018{
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);
8241025   cur_live.tm = attotime::never;
8251026   cur_live.state = IDLE;
8261027   cur_live.next_state = -1;
r13180r13181
8441045   return false;
8451046}
8461047
1048bool 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
1064void 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
1071void 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
8471088void wd1772_t::live_run(attotime limit)
8481089{
8491090   if(cur_live.state == IDLE || cur_live.next_state != -1)
r13180r13181
10471288         checkpoint();
10481289         break;
10491290
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
10501431      default:
10511432         logerror("%s: Unknown live state %d\n", tts(cur_live.tm).cstr(), cur_live.state);
10521433         return;
r13180r13181
10921473   phase_sub = 0x00;
10931474   freq_add  = 0x00;
10941475   freq_sub  = 0x00;
1476   write_position = 0;
1477   write_start_time = attotime::never;
10951478}
10961479
10971480int wd1772_t::pll_t::get_next_bit(attotime &tm, floppy_image_device *floppy, attotime limit)
r13180r13181
11311514      if(counter & 0x800)
11321515         break;
11331516   }
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);
11351518   int bit = transition_time != 0xffff;
11361519
11371520   if(transition_time != 0xffff) {
r13180r13181
11731556
11741557   return bit;
11751558}
1559
1560void wd1772_t::pll_t::start_writing(attotime tm)
1561{
1562   write_start_time = tm;
1563   write_position = 0;
1564}
1565
1566void 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
1572bool 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
1602void 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}
src/mess/machine/wd1772.h
r13180r13181
9898      READ_SECTOR,
9999      READ_TRACK,
100100      READ_ID,
101      WRITE_TRACK,
102      WRITE_SECTOR,
101103
102104      // Sub states
103105
r13180r13181
108110      SETTLE_WAIT,
109111      SETTLE_DONE,
110112
113      DATA_LOAD_WAIT,
114      DATA_LOAD_WAIT_DONE,
115
111116      SEEK_MOVE,
112117      SEEK_WAIT_STEP_TIME,
113118      SEEK_WAIT_STEP_TIME_DONE,
r13180r13181
122127      SCAN_ID_FAILED,
123128
124129      SECTOR_READ,
130      SECTOR_WRITE,
125131      TRACK_DONE,
126132
127133      // Live states
r13180r13181
135141      READ_SECTOR_DATA_BYTE,
136142      READ_TRACK_DATA,
137143      READ_TRACK_DATA_BYTE,
144      WRITE_TRACK_DATA,
145      WRITE_BYTE,
146      WRITE_BYTE_DONE,
147      WRITE_SECTOR_PRE,
148      WRITE_SECTOR_PRE_BYTE,
138149   };
139150
140151   struct pll_t {
r13180r13181
148159
149160      attotime delays[42];
150161
162      attotime write_start_time;
163      attotime write_buffer[32];
164      int write_position;
165
151166      void set_clock(attotime period);
152167      void reset(attotime when);
153168      int get_next_bit(attotime &tm, floppy_image_device *floppy, attotime limit);
169      bool write_next_bit(bool bit, attotime &tm, floppy_image_device *floppy, attotime limit);
170      void start_writing(attotime tm);
171      void commit(floppy_image_device *floppy, attotime tm);
172      void stop_writing(floppy_image_device *floppy, attotime tm);
154173   };
155174
156175   struct live_info {
176      enum { PT_NONE, PT_SYNC, PT_CRC_1, PT_CRC_2 };
177
157178      attotime tm;
158179      int state, next_state;
159180      UINT16 shift_reg;
160181      UINT16 crc;
161      int bit_counter;
162      bool data_separator_phase;
182      int bit_counter, byte_counter, previous_type;
183      bool data_separator_phase, data_bit_context;
163184      UINT8 data_reg;
164185      UINT8 idbuf[6];
165186      pll_t pll;
r13180r13181
222243   void read_id_start();
223244   void read_id_continue();
224245
246   void write_track_start();
247   void write_track_continue();
248
249   void write_sector_start();
250   void write_sector_continue();
251
225252   void interrupt_start();
226253
227254   void general_continue();
r13180r13181
239266   void live_sync();
240267   void live_run(attotime limit = attotime::never);
241268   bool read_one_bit(attotime limit);
269   bool write_one_bit(attotime limit);
270
271   void live_write_raw(UINT16 raw);
272   void live_write_mfm(UINT8 mfm);
273
242274   void drop_drq();
243275   void set_drq();
244276};

Previous 15994 Revisions Next


© 1998-2018 The MESS Team