forked from scarybeasts/beebjit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intel_fdc.c
2121 lines (1928 loc) · 70.5 KB
/
intel_fdc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "intel_fdc.h"
#include "bbc_options.h"
#include "disc_drive.h"
#include "ibm_disc_format.h"
#include "log.h"
#include "state_6502.h"
#include "timing.h"
#include "util.h"
#include <assert.h>
#include <string.h>
/* Public command constants from the datasheet, included here for reference.
* These are not used internally. Internally, the command is derived by
* shifting right by 2. The remaining bits are used to describe parameter
* counts and variants.
enum {
k_intel_fdc_command_scan_sectors = 0x00,
k_intel_fdc_command_scan_sectors_with_deleted = 0x04,
k_intel_fdc_command_write_sector_128 = 0x0A,
k_intel_fdc_command_write_sectors = 0x0B,
k_intel_fdc_command_write_sector_deleted_128 = 0x0E,
k_intel_fdc_command_write_sectors_deleted = 0x0F,
k_intel_fdc_command_read_sector_128 = 0x12,
k_intel_fdc_command_read_sectors = 0x13,
k_intel_fdc_command_read_sector_with_deleted_128 = 0x16,
k_intel_fdc_command_read_sectors_with_deleted = 0x17,
k_intel_fdc_command_read_sector_ids = 0x1B,
k_intel_fdc_command_verify_sector_128 = 0x1E,
k_intel_fdc_command_verify_sectors = 0x1F,
k_intel_fdc_command_format = 0x23,
k_intel_fdc_command_seek = 0x29,
k_intel_fdc_command_read_drive_status = 0x2C,
k_intel_fdc_command_specify = 0x35,
k_intel_fdc_command_write_special_register = 0x3A,
k_intel_fdc_command_read_special_register = 0x3D,
};
*/
enum {
/* Read. */
k_intel_fdc_status = 0,
k_intel_fdc_result = 1,
k_intel_fdc_unknown_read_2 = 2,
k_intel_fdc_unknown_read_3 = 3,
/* Write. */
k_intel_fdc_command = 0,
k_intel_fdc_parameter = 1,
k_intel_fdc_reset = 2,
/* Read / write. */
k_intel_fdc_data = 4,
};
enum {
k_intel_fdc_parameter_accept_none = 1,
k_intel_fdc_parameter_accept_command = 2,
k_intel_fdc_parameter_accept_specify = 3,
};
enum {
k_intel_fdc_index_pulse_none = 1,
k_intel_fdc_index_pulse_timeout = 2,
k_intel_fdc_index_pulse_spindown = 3,
k_intel_fdc_index_pulse_start_read_id = 4,
k_intel_fdc_index_pulse_start_format = 5,
k_intel_fdc_index_pulse_stop_format = 6,
};
enum {
k_intel_fdc_status_flag_busy = 0x80,
k_intel_fdc_status_flag_command_full = 0x40,
k_intel_fdc_status_flag_param_full = 0x20,
k_intel_fdc_status_flag_result_ready = 0x10,
k_intel_fdc_status_flag_nmi = 0x08,
k_intel_fdc_status_flag_need_data = 0x04,
};
enum {
k_intel_fdc_result_ok = 0x00,
k_intel_fdc_result_clock_error = 0x08,
k_intel_fdc_result_late_dma = 0x0A,
k_intel_fdc_result_id_crc_error = 0x0C,
k_intel_fdc_result_data_crc_error = 0x0E,
k_intel_fdc_result_drive_not_ready = 0x10,
k_intel_fdc_result_write_protected = 0x12,
k_intel_fdc_result_sector_not_found = 0x18,
k_intel_fdc_result_flag_deleted_data = 0x20,
};
enum {
k_intel_fdc_command_SCAN_DATA = 0,
k_intel_fdc_command_SCAN_DATA_AND_DELETED = 1,
k_intel_fdc_command_WRITE_DATA = 2,
k_intel_fdc_command_WRITE_DELETED_DATA = 3,
k_intel_fdc_command_READ_DATA = 4,
k_intel_fdc_command_READ_DATA_AND_DELETED = 5,
k_intel_fdc_command_READ_ID = 6,
k_intel_fdc_command_VERIFY = 7,
k_intel_fdc_command_FORMAT = 8,
k_intel_fdc_command_UNUSED_9 = 9,
k_intel_fdc_command_SEEK = 10,
k_intel_fdc_command_READ_DRIVE_STATUS = 11,
k_intel_fdc_command_UNUSED_12 = 12,
k_intel_fdc_command_SPECIFY = 13,
k_intel_fdc_command_WRITE_SPECIAL_REGISTER = 14,
k_intel_fdc_command_READ_SPECIAL_REGISTER = 15,
};
enum {
k_intel_fdc_register_internal_pointer = 0x00,
k_intel_fdc_register_internal_count_msb_copy = 0x00,
k_intel_fdc_register_internal_param_count = 0x01,
k_intel_fdc_register_internal_seek_retry_count = 0x01,
k_intel_fdc_register_internal_param_data_marker = 0x02,
k_intel_fdc_register_internal_param_5 = 0x03,
k_intel_fdc_register_internal_param_4 = 0x04,
k_intel_fdc_register_internal_param_3 = 0x05,
k_intel_fdc_register_current_sector = 0x06,
k_intel_fdc_register_internal_param_2 = 0x06,
k_intel_fdc_register_internal_param_1 = 0x07,
k_intel_fdc_register_internal_header_pointer = 0x08,
k_intel_fdc_register_internal_ms_count_hi = 0x08,
k_intel_fdc_register_internal_ms_count_lo = 0x09,
k_intel_fdc_register_internal_seek_count = 0x0A,
k_intel_fdc_register_internal_id_sector = 0x0A,
k_intel_fdc_register_internal_seek_target_1 = 0x0B,
k_intel_fdc_register_internal_dynamic_dispatch = 0x0B,
k_intel_fdc_register_internal_seek_target_2 = 0x0C,
k_intel_fdc_register_internal_id_track = 0x0C,
k_intel_fdc_register_head_step_rate = 0x0D,
k_intel_fdc_register_head_settle_time = 0x0E,
k_intel_fdc_register_head_load_unload = 0x0F,
k_intel_fdc_register_bad_track_1_drive_0 = 0x10,
k_intel_fdc_register_bad_track_2_drive_0 = 0x11,
k_intel_fdc_register_track_drive_0 = 0x12,
k_intel_fdc_register_internal_count_lsb = 0x13,
k_intel_fdc_register_internal_count_msb = 0x14,
k_intel_fdc_register_internal_drive_in_copy = 0x15,
k_intel_fdc_register_internal_write_run_data = 0x15,
k_intel_fdc_register_internal_gap2_skip = 0x15,
k_intel_fdc_register_internal_result = 0x16,
k_intel_fdc_register_mode = 0x17,
k_intel_fdc_register_internal_status = 0x17,
k_intel_fdc_register_bad_track_1_drive_1 = 0x18,
k_intel_fdc_register_bad_track_2_drive_1 = 0x19,
k_intel_fdc_register_track_drive_1 = 0x1A,
k_intel_fdc_register_internal_drive_in_latched = 0x1B,
k_intel_fdc_register_internal_index_pulse_count = 0x1C,
k_intel_fdc_register_internal_data = 0x1D,
k_intel_fdc_register_internal_parameter = 0x1E,
k_intel_fdc_register_internal_command = 0x1F,
k_intel_fdc_register_mmio_drive_in = 0x22,
k_intel_fdc_register_mmio_drive_out = 0x23,
k_intel_fdc_register_mmio_clocks = 0x24,
k_intel_fdc_register_mmio_data = 0x25,
};
enum {
k_intel_fdc_drive_out_select_1 = 0x80,
k_intel_fdc_drive_out_select_0 = 0x40,
k_intel_fdc_drive_out_side = 0x20,
k_intel_fdc_drive_out_low_head_current = 0x10,
k_intel_fdc_drive_out_load_head = 0x08,
k_intel_fdc_drive_out_direction = 0x04,
k_intel_fdc_drive_out_step = 0x02,
k_intel_fdc_drive_out_write_enable = 0x01,
};
enum {
k_intel_fdc_mode_single_actuator = 0x02,
k_intel_fdc_mode_no_dma = 0x01,
};
enum {
k_intel_fdc_state_null = 0,
k_intel_fdc_state_idle,
k_intel_fdc_state_syncing_for_id_wait,
k_intel_fdc_state_syncing_for_id,
k_intel_fdc_state_check_id_marker,
k_intel_fdc_state_in_id,
k_intel_fdc_state_in_id_crc,
k_intel_fdc_state_syncing_for_data,
k_intel_fdc_state_check_data_marker,
k_intel_fdc_state_in_data,
k_intel_fdc_state_in_data_crc,
k_intel_fdc_state_skip_gap_2,
k_intel_fdc_state_write_run,
k_intel_fdc_state_write_data_mark,
k_intel_fdc_state_write_sector_data,
k_intel_fdc_state_write_crc_2,
k_intel_fdc_state_write_crc_3,
k_intel_fdc_state_dynamic_dispatch,
k_intel_fdc_state_format_write_id_marker,
k_intel_fdc_state_format_id_crc_2,
k_intel_fdc_state_format_id_crc_3,
k_intel_fdc_state_format_write_data_marker,
k_intel_fdc_state_format_data_crc_2,
k_intel_fdc_state_format_data_crc_3,
k_intel_fdc_state_format_gap_4,
};
enum {
k_intel_fdc_timer_none = 0,
k_intel_fdc_timer_seek_step = 1,
k_intel_fdc_timer_post_seek = 2,
};
enum {
k_intel_fdc_call_unchanged = 1,
k_intel_fdc_call_seek = 2,
k_intel_fdc_call_read_id = 3,
k_intel_fdc_call_read = 4,
k_intel_fdc_call_write = 5,
k_intel_fdc_call_format = 6,
k_intel_fdc_call_format_GAP1_or_GAP3_FFs = 7,
k_intel_fdc_call_format_GAP1_or_GAP3_00s = 8,
k_intel_fdc_call_format_GAP2_FFs = 9,
k_intel_fdc_call_format_GAP2_00s = 10,
k_intel_fdc_call_format_data = 11,
};
enum {
k_intel_num_registers = 32,
};
struct intel_fdc_struct {
struct state_6502* p_state_6502;
struct timing_struct* p_timing;
uint32_t timer_id;
int log_commands;
struct disc_drive_struct* p_drive_0;
struct disc_drive_struct* p_drive_1;
struct disc_drive_struct* p_current_drive;
/* Event callbacks and execution contexts. */
int parameter_callback;
int index_pulse_callback;
int timer_state;
int call_context;
int did_seek_step;
uint8_t regs[k_intel_num_registers];
int is_result_ready;
uint8_t mmio_data;
uint8_t mmio_clocks;
uint8_t drive_out;
uint32_t shift_register;
uint32_t num_shifts;
int state;
uint32_t state_count;
int state_is_index_pulse;
uint16_t crc;
uint16_t on_disc_crc;
};
static inline uint8_t
intel_fdc_get_status(struct intel_fdc_struct* p_fdc) {
return p_fdc->regs[k_intel_fdc_register_internal_status];
}
static inline uint8_t
intel_fdc_get_external_status(struct intel_fdc_struct* p_fdc) {
/* EMU NOTE: currently, the emulation responds instantly to getting commands
* started, accepting parameters, transitioning between states, etc.
* This is inaccurate.
* The real 8271 is an asynchronously running general purpose microcontroller,
* where each instruction takes 2us+ and processing between states uses a
* large and variable number of instructions.
* Some food for though, latency timings on a BBC + 8271, including setup:
* WRITE SPECIAL REGISTER: 211us
* WRITE SPECIAL REGISTER (0 param version): 157us
* READ DRIVE STATUS: 188us
* write $35 to parameter register: 31us
* write 3rd SPECIFY parameter: 27us
*/
uint8_t status = intel_fdc_get_status(p_fdc);
/* The internal status register appears to be shared with some mode bits that
* must be masked out.
*/
status &= ~0x03;
/* Current best thinking is that the internal register uses bit value 0x10 for
* something different, and that "result ready" is maintained by the external
* register logic.
*/
status &= ~k_intel_fdc_status_flag_result_ready;
if (p_fdc->is_result_ready) {
status |= k_intel_fdc_status_flag_result_ready;
}
/* TODO: "command register full", bit value 0x40, isn't understood. In
* particular, the mode register (shared with the status register we
* believe) is set to 0xC1 in typical operation. This would seem to raise
* 0x40 after it has been lowered at command register acceptance. However,
* the bit is not returned.
* Don't return it, ever, for now.
*/
/* Also avoid "parameter register full". */
status &= ~0x60;
return status;
}
static void
intel_fdc_update_nmi(struct intel_fdc_struct* p_fdc) {
struct state_6502* p_state_6502 = p_fdc->p_state_6502;
uint8_t status = intel_fdc_get_status(p_fdc);
int level = !!(status & k_intel_fdc_status_flag_nmi);
int firing = state_6502_check_irq_firing(p_state_6502, k_state_6502_irq_nmi);
if (firing && (level == 1)) {
log_do_log(k_log_disc, k_log_error, "edge triggered NMI already high");
}
state_6502_set_irq_level(p_state_6502, k_state_6502_irq_nmi, level);
}
static inline void
intel_fdc_status_raise(struct intel_fdc_struct* p_fdc, uint8_t bits) {
p_fdc->regs[k_intel_fdc_register_internal_status] |= bits;
if (bits & k_intel_fdc_status_flag_nmi) {
intel_fdc_update_nmi(p_fdc);
}
}
static inline void
intel_fdc_status_lower(struct intel_fdc_struct* p_fdc, uint8_t bits) {
p_fdc->regs[k_intel_fdc_register_internal_status] &= ~bits;
if (bits & k_intel_fdc_status_flag_nmi) {
intel_fdc_update_nmi(p_fdc);
}
}
static inline uint8_t
intel_fdc_get_result(struct intel_fdc_struct* p_fdc) {
return p_fdc->regs[k_intel_fdc_register_internal_result];
}
static void
intel_fdc_set_result(struct intel_fdc_struct* p_fdc, uint8_t result) {
p_fdc->regs[k_intel_fdc_register_internal_result] = result;
p_fdc->is_result_ready = 1;
}
static inline uint8_t
intel_fdc_get_internal_command(struct intel_fdc_struct* p_fdc) {
uint8_t command = p_fdc->regs[k_intel_fdc_register_internal_command];
command &= 0x3C;
return (command >> 2);
}
static inline uint32_t
intel_fdc_get_sector_size(struct intel_fdc_struct* p_fdc) {
uint32_t size = (p_fdc->regs[k_intel_fdc_register_internal_param_3] >> 5);
size = (128 << size);
return size;
}
static void
intel_fdc_setup_sector_size(struct intel_fdc_struct* p_fdc) {
uint32_t size = intel_fdc_get_sector_size(p_fdc);
uint32_t msb = ((size / 128) - 1);
p_fdc->regs[k_intel_fdc_register_internal_count_lsb] = 0x80;
p_fdc->regs[k_intel_fdc_register_internal_count_msb] = msb;
/* NOTE: this is R0, i.e. R0 is trashed here. */
p_fdc->regs[k_intel_fdc_register_internal_count_msb_copy] = msb;
}
static void
intel_fdc_start_irq_callbacks(struct intel_fdc_struct* p_fdc) {
p_fdc->regs[k_intel_fdc_register_internal_status] |= 0x30;
}
static void
intel_fdc_stop_irq_callbacks(struct intel_fdc_struct* p_fdc) {
p_fdc->regs[k_intel_fdc_register_internal_status] &= ~0x30;
}
static int
intel_fdc_is_irq_callbacks(struct intel_fdc_struct* p_fdc) {
if ((p_fdc->regs[k_intel_fdc_register_internal_status] & 0x30) == 0x30) {
return 1;
}
return 0;
}
static int
intel_fdc_decrement_counter(struct intel_fdc_struct* p_fdc) {
p_fdc->regs[k_intel_fdc_register_internal_count_lsb]--;
if (p_fdc->regs[k_intel_fdc_register_internal_count_lsb] != 0) {
return 0;
}
p_fdc->regs[k_intel_fdc_register_internal_count_msb]--;
if (p_fdc->regs[k_intel_fdc_register_internal_count_msb] != 0xFF) {
p_fdc->regs[k_intel_fdc_register_internal_count_lsb] = 0x80;
return 0;
}
p_fdc->regs[k_intel_fdc_register_internal_count_msb] = 0;
intel_fdc_stop_irq_callbacks(p_fdc);
return 1;
}
static void
intel_fdc_start_index_pulse_timeout(struct intel_fdc_struct* p_fdc) {
p_fdc->regs[k_intel_fdc_register_internal_index_pulse_count] = 3;
p_fdc->index_pulse_callback = k_intel_fdc_index_pulse_timeout;
}
static void
intel_fdc_set_drive_out(struct intel_fdc_struct* p_fdc, uint8_t drive_out) {
uint8_t select_bits;
struct disc_drive_struct* p_current_drive = p_fdc->p_current_drive;
if (p_current_drive != NULL) {
if (p_fdc->drive_out & k_intel_fdc_drive_out_load_head) {
disc_drive_stop_spinning(p_current_drive);
}
p_current_drive = NULL;
}
/* NOTE: unclear what to do if both drives are selected. We select no drive
* for now, to avoid shenanigans.
*/
select_bits = (drive_out & 0xC0);
if (select_bits == 0x40) {
p_current_drive = p_fdc->p_drive_0;
} else if (select_bits == 0x80) {
p_current_drive = p_fdc->p_drive_1;
}
p_fdc->p_current_drive = p_current_drive;
if (p_current_drive != NULL) {
if (drive_out & k_intel_fdc_drive_out_load_head) {
disc_drive_start_spinning(p_current_drive);
}
disc_drive_select_side(p_current_drive,
!!(drive_out & k_intel_fdc_drive_out_side));
}
p_fdc->drive_out = drive_out;
}
static void
intel_fdc_drive_out_raise(struct intel_fdc_struct* p_fdc, uint8_t bits) {
uint8_t drive_out = (p_fdc->drive_out | bits);
intel_fdc_set_drive_out(p_fdc, drive_out);
}
static void
intel_fdc_drive_out_lower(struct intel_fdc_struct* p_fdc, uint8_t bits) {
uint8_t drive_out = (p_fdc->drive_out & ~bits);
intel_fdc_set_drive_out(p_fdc, drive_out);
}
static void
intel_fdc_set_state(struct intel_fdc_struct* p_fdc, int state) {
p_fdc->state = state;
p_fdc->state_count = 0;
if ((state == k_intel_fdc_state_syncing_for_id) ||
(state == k_intel_fdc_state_syncing_for_data)) {
p_fdc->shift_register = 0;
p_fdc->num_shifts = 0;
}
}
static void
intel_fdc_clear_callbacks(struct intel_fdc_struct* p_fdc) {
p_fdc->parameter_callback = k_intel_fdc_parameter_accept_none;
p_fdc->index_pulse_callback = k_intel_fdc_index_pulse_none;
if (p_fdc->timer_state != k_intel_fdc_timer_none) {
(void) timing_stop_timer(p_fdc->p_timing, p_fdc->timer_id);
p_fdc->timer_state = k_intel_fdc_timer_none;
}
/* Think of this as the read / write callback from the bit processor. */
intel_fdc_set_state(p_fdc, k_intel_fdc_state_idle);
}
static void
intel_fdc_lower_busy_and_log(struct intel_fdc_struct* p_fdc) {
intel_fdc_status_lower(p_fdc, k_intel_fdc_status_flag_busy);
if (p_fdc->log_commands) {
log_do_log(k_log_disc,
k_log_info,
"8271: status $%x result $%x",
intel_fdc_get_external_status(p_fdc),
intel_fdc_get_result(p_fdc));
}
}
static void
intel_fdc_spindown(struct intel_fdc_struct* p_fdc) {
intel_fdc_drive_out_lower(p_fdc,
(k_intel_fdc_drive_out_select_1 |
k_intel_fdc_drive_out_select_0 |
k_intel_fdc_drive_out_load_head));
}
static void
intel_fdc_finish_simple_command(struct intel_fdc_struct* p_fdc) {
uint8_t head_unload_count;
intel_fdc_lower_busy_and_log(p_fdc);
intel_fdc_stop_irq_callbacks(p_fdc);
intel_fdc_clear_callbacks(p_fdc);
head_unload_count = (p_fdc->regs[k_intel_fdc_register_head_load_unload] >> 4);
if (head_unload_count == 0) {
/* Unload immediately. */
intel_fdc_spindown(p_fdc);
} else if (head_unload_count == 0xF) {
/* Never automatically unload. */
} else {
p_fdc->regs[k_intel_fdc_register_internal_index_pulse_count] =
head_unload_count;
p_fdc->index_pulse_callback = k_intel_fdc_index_pulse_spindown;
}
}
static void
intel_fdc_finish_command(struct intel_fdc_struct* p_fdc, uint8_t result) {
if (result != k_intel_fdc_result_ok) {
intel_fdc_drive_out_lower(p_fdc, (k_intel_fdc_drive_out_direction |
k_intel_fdc_drive_out_step |
k_intel_fdc_drive_out_write_enable));
}
result |= intel_fdc_get_result(p_fdc);
intel_fdc_set_result(p_fdc, result);
/* Raise command completion IRQ. */
intel_fdc_status_raise(p_fdc, k_intel_fdc_status_flag_nmi);
intel_fdc_finish_simple_command(p_fdc);
}
static void
intel_fdc_command_abort(struct intel_fdc_struct* p_fdc) {
/* If we're aborting a command in the middle of writing data, it usually
* doesn't leave a clean byte end on the disc. This is not particularly
* important to emulate at all but it does help create new copy protection
* schemes under emulation.
*/
if (p_fdc->drive_out & k_intel_fdc_drive_out_write_enable) {
uint32_t pulses = ibm_disc_format_fm_to_2us_pulses(0xFF, 0xFF);
disc_drive_write_pulses(p_fdc->p_current_drive, pulses);
}
/* Lower any NMI assertion. This is particularly important for error $0A,
* aka. late DMA, which will abort the command while NMI is asserted. We
* therefore need to de-assert NMI so that the NMI for command completion
* isn't lost.
*/
state_6502_set_irq_level(p_fdc->p_state_6502, k_state_6502_irq_nmi, 0);
}
void
intel_fdc_power_on_reset(struct intel_fdc_struct* p_fdc) {
/* The chip's reset line does take care of a lot of things.... */
intel_fdc_break_reset(p_fdc);
/* ... but not everything. Note that not all of these have been verified as
* to whether the reset line changes them or not.
*/
assert(p_fdc->parameter_callback == k_intel_fdc_parameter_accept_none);
assert(p_fdc->index_pulse_callback == k_intel_fdc_index_pulse_none);
assert(p_fdc->timer_state == k_intel_fdc_timer_none);
assert(p_fdc->state == k_intel_fdc_state_idle);
assert(p_fdc->p_current_drive == NULL);
assert(p_fdc->drive_out == 0);
(void) memset(&p_fdc->regs[0], '\0', sizeof(p_fdc->regs));
p_fdc->is_result_ready = 0;
p_fdc->mmio_data = 0;
p_fdc->mmio_clocks = 0;
p_fdc->state_count = 0;
p_fdc->state_is_index_pulse = 0;
}
void
intel_fdc_break_reset(struct intel_fdc_struct* p_fdc) {
/* Abort any in-progress command. */
intel_fdc_command_abort(p_fdc);
intel_fdc_clear_callbacks(p_fdc);
/* Deselect any drive; ensures spin-down. */
intel_fdc_set_drive_out(p_fdc, 0);
/* EMU: on a real machine, status appears to be cleared but result and data
* register not. */
intel_fdc_status_lower(p_fdc, intel_fdc_get_status(p_fdc));
}
static void
intel_fdc_start_syncing_for_header(struct intel_fdc_struct* p_fdc) {
p_fdc->regs[k_intel_fdc_register_internal_header_pointer] = 0x0C;
intel_fdc_set_state(p_fdc, k_intel_fdc_state_syncing_for_id);
}
static void
intel_fdc_set_timer_ms(struct intel_fdc_struct* p_fdc,
int timer_state,
uint32_t wait_ms) {
struct timing_struct* p_timing = p_fdc->p_timing;
uint32_t timer_id = p_fdc->timer_id;
if (timing_timer_is_running(p_fdc->p_timing, timer_id)) {
(void) timing_stop_timer(p_timing, timer_id);
}
p_fdc->timer_state = timer_state;
(void) timing_start_timer_with_value(p_timing, timer_id, (wait_ms * 2000));
}
static int
intel_fdc_get_WRPROT(struct intel_fdc_struct* p_fdc) {
struct disc_drive_struct* p_current_drive = p_fdc->p_current_drive;
if (p_current_drive == NULL) {
return 0;
}
return disc_drive_is_write_protect(p_current_drive);
}
static int
intel_fdc_get_TRK0(struct intel_fdc_struct* p_fdc) {
struct disc_drive_struct* p_current_drive = p_fdc->p_current_drive;
if (p_current_drive == NULL) {
return 0;
}
return (disc_drive_get_track(p_current_drive) == 0);
}
static int
intel_fdc_get_INDEX(struct intel_fdc_struct* p_fdc) {
struct disc_drive_struct* p_current_drive = p_fdc->p_current_drive;
if (p_current_drive == NULL) {
return 0;
}
return disc_drive_is_index_pulse(p_current_drive);
}
static int
intel_fdc_current_disc_is_spinning(struct intel_fdc_struct* p_fdc) {
struct disc_drive_struct* p_current_drive = p_fdc->p_current_drive;
if (p_current_drive == NULL) {
return 0;
}
return disc_drive_is_spinning(p_current_drive);
}
static uint8_t
intel_fdc_read_drive_in(struct intel_fdc_struct* p_fdc) {
/* EMU NOTE: on my machine, bit 7 and bit 0 appear to be set all the time. */
uint8_t drive_in = 0x81;
if (intel_fdc_current_disc_is_spinning(p_fdc)) {
if (intel_fdc_get_TRK0(p_fdc)) {
/* TRK0 */
drive_in |= 0x02;
}
if (p_fdc->drive_out & 0x40) {
/* RDY0 */
drive_in |= 0x04;
}
if (p_fdc->drive_out & 0x80) {
/* RDY1 */
drive_in |= 0x40;
}
if (intel_fdc_get_WRPROT(p_fdc)) {
/* WR PROT */
drive_in |= 0x08;
}
if (intel_fdc_get_INDEX(p_fdc)) {
/* INDEX */
drive_in |= 0x10;
}
}
return drive_in;
}
static uint8_t
intel_fdc_do_read_drive_status(struct intel_fdc_struct* p_fdc) {
uint8_t drive_in = intel_fdc_read_drive_in(p_fdc);
p_fdc->regs[k_intel_fdc_register_internal_drive_in_copy] = drive_in;
p_fdc->regs[k_intel_fdc_register_internal_drive_in_latched] |= 0xBB;
drive_in &= p_fdc->regs[k_intel_fdc_register_internal_drive_in_latched];
p_fdc->regs[k_intel_fdc_register_internal_drive_in_latched] = drive_in;
return drive_in;
}
static int
intel_fdc_check_drive_ready(struct intel_fdc_struct* p_fdc) {
uint8_t mask;
(void) intel_fdc_do_read_drive_status(p_fdc);
if (p_fdc->drive_out & k_intel_fdc_drive_out_select_1) {
mask = 0x40;
} else {
mask = 0x04;
}
if (!(p_fdc->regs[k_intel_fdc_register_internal_drive_in_latched] & mask)) {
intel_fdc_finish_command(p_fdc, k_intel_fdc_result_drive_not_ready);
return 0;
}
return 1;
}
static void
intel_fdc_check_write_protect(struct intel_fdc_struct* p_fdc) {
if (p_fdc->regs[k_intel_fdc_register_internal_drive_in_latched] & 0x08) {
intel_fdc_finish_command(p_fdc, k_intel_fdc_result_write_protected);
}
}
static void
intel_fdc_post_seek_dispatch(struct intel_fdc_struct* p_fdc) {
p_fdc->timer_state = k_intel_fdc_timer_none;
if (!intel_fdc_check_drive_ready(p_fdc)) {
return;
}
switch (p_fdc->call_context) {
case k_intel_fdc_call_seek:
intel_fdc_finish_command(p_fdc, k_intel_fdc_result_ok);
break;
case k_intel_fdc_call_read_id:
p_fdc->index_pulse_callback = k_intel_fdc_index_pulse_start_read_id;
break;
case k_intel_fdc_call_format:
intel_fdc_setup_sector_size(p_fdc);
p_fdc->index_pulse_callback = k_intel_fdc_index_pulse_start_format;
intel_fdc_check_write_protect(p_fdc);
break;
case k_intel_fdc_call_read:
case k_intel_fdc_call_write:
intel_fdc_setup_sector_size(p_fdc);
intel_fdc_start_index_pulse_timeout(p_fdc);
intel_fdc_start_syncing_for_header(p_fdc);
if (p_fdc->call_context == k_intel_fdc_call_write) {
intel_fdc_check_write_protect(p_fdc);
}
break;
default:
assert(0);
break;
}
}
static void
intel_fdc_do_load_head(struct intel_fdc_struct* p_fdc) {
uint32_t post_seek_time = 0;
/* The head load wait replaces the settle delay if there is both. */
if (!(p_fdc->drive_out & k_intel_fdc_drive_out_load_head)) {
intel_fdc_drive_out_raise(p_fdc, k_intel_fdc_drive_out_load_head);
post_seek_time =
(p_fdc->regs[k_intel_fdc_register_head_load_unload] & 0x0F);
/* Head load units are 4ms. */
post_seek_time *= 4;
} else if (p_fdc->did_seek_step) {
/* EMU: all references state the units are 2ms for 5.25" drives. */
post_seek_time = (p_fdc->regs[k_intel_fdc_register_head_settle_time] * 2);
}
if (post_seek_time > 0) {
intel_fdc_set_timer_ms(p_fdc, k_intel_fdc_timer_post_seek, post_seek_time);
} else {
intel_fdc_post_seek_dispatch(p_fdc);
}
}
static void
intel_fdc_do_seek_step(struct intel_fdc_struct* p_fdc) {
uint32_t step_rate;
struct disc_drive_struct* p_current_drive = p_fdc->p_current_drive;
if (intel_fdc_get_TRK0(p_fdc) &&
(p_fdc->regs[k_intel_fdc_register_internal_seek_target_2] == 0)) {
/* Seek to 0 done, TRK0 detected. */
intel_fdc_do_load_head(p_fdc);
return;
} else if (p_fdc->regs[k_intel_fdc_register_internal_seek_count] == 0) {
intel_fdc_do_load_head(p_fdc);
return;
}
/* We're going to actually step so we'll need settle if the head is already
* loaded.
*/
p_fdc->did_seek_step = 1;
p_fdc->regs[k_intel_fdc_register_internal_seek_count]--;
if (p_current_drive != NULL) {
if (p_fdc->drive_out & k_intel_fdc_drive_out_direction) {
disc_drive_seek_track(p_current_drive, 1);
} else {
disc_drive_seek_track(p_current_drive, -1);
}
}
step_rate = p_fdc->regs[k_intel_fdc_register_head_step_rate];
if (step_rate == 0) {
/* Seek time is up to drive. Let's just say it can handle 3ms. */
step_rate = 3;
} else {
/* EMU NOTE: the datasheet is ambiguous about whether the units are 1ms
* or 2ms for 5.25" drives. 1ms might be your best guess from the
* datasheet, but timing on a real machine, it appears to be 2ms.
*/
step_rate *= 2;
}
intel_fdc_set_timer_ms(p_fdc, k_intel_fdc_timer_seek_step, step_rate);
}
static void
intel_fdc_timer_fired(void* p) {
struct intel_fdc_struct* p_fdc = (struct intel_fdc_struct*) p;
(void) timing_stop_timer(p_fdc->p_timing, p_fdc->timer_id);
/* Counting milliseconds is done with R8 and R9, which are left at zero
* after a busy wait.
*/
p_fdc->regs[k_intel_fdc_register_internal_ms_count_hi] = 0;
p_fdc->regs[k_intel_fdc_register_internal_ms_count_lo] = 0;
switch (p_fdc->timer_state) {
case k_intel_fdc_timer_seek_step:
intel_fdc_do_seek_step(p_fdc);
break;
case k_intel_fdc_timer_post_seek:
intel_fdc_post_seek_dispatch(p_fdc);
break;
default:
assert(0);
break;
}
}
struct intel_fdc_struct*
intel_fdc_create(struct state_6502* p_state_6502,
struct timing_struct* p_timing,
struct bbc_options* p_options) {
struct intel_fdc_struct* p_fdc =
util_mallocz(sizeof(struct intel_fdc_struct));
p_fdc->p_state_6502 = p_state_6502;
p_fdc->p_timing = p_timing;
p_fdc->timer_id = timing_register_timer(p_timing,
intel_fdc_timer_fired,
p_fdc);
p_fdc->log_commands = util_has_option(p_options->p_log_flags,
"disc:commands");
intel_fdc_clear_callbacks(p_fdc);
return p_fdc;
}
void
intel_fdc_destroy(struct intel_fdc_struct* p_fdc) {
struct disc_drive_struct* p_drive_0 = p_fdc->p_drive_0;
struct disc_drive_struct* p_drive_1 = p_fdc->p_drive_1;
disc_drive_set_pulses_callback(p_drive_0, NULL, NULL);
disc_drive_set_pulses_callback(p_drive_1, NULL, NULL);
if (disc_drive_is_spinning(p_drive_0)) {
disc_drive_stop_spinning(p_drive_0);
}
if (disc_drive_is_spinning(p_drive_1)) {
disc_drive_stop_spinning(p_drive_1);
}
if (timing_timer_is_running(p_fdc->p_timing, p_fdc->timer_id)) {
(void) timing_stop_timer(p_fdc->p_timing, p_fdc->timer_id);
}
util_free(p_fdc);
}
uint8_t
intel_fdc_read(struct intel_fdc_struct* p_fdc, uint16_t addr) {
uint8_t result;
switch (addr & 0x07) {
case k_intel_fdc_status:
return intel_fdc_get_external_status(p_fdc);
case k_intel_fdc_result:
result = intel_fdc_get_result(p_fdc);
p_fdc->is_result_ready = 0;
intel_fdc_status_lower(p_fdc, k_intel_fdc_status_flag_nmi);
return result;
/* EMU: on a real model B, the i8271 has the data register mapped for all of
* register address 4 - 7.
*/
case k_intel_fdc_data:
case (k_intel_fdc_data + 1):
case (k_intel_fdc_data + 2):
case (k_intel_fdc_data + 3):
result = intel_fdc_get_result(p_fdc);
intel_fdc_status_lower(p_fdc, (k_intel_fdc_status_flag_need_data |
k_intel_fdc_status_flag_nmi));
return p_fdc->regs[k_intel_fdc_register_internal_data];
/* EMU: register address 2 and 3 are not documented as having anything
* wired up for reading, BUT on a model B, they appear to give the MSB and
* LSB of the sector byte counter in internal registers 19 ($13) and 20 ($14).
*/
case k_intel_fdc_unknown_read_2:
return p_fdc->regs[k_intel_fdc_register_internal_count_msb];
case k_intel_fdc_unknown_read_3:
return p_fdc->regs[k_intel_fdc_register_internal_count_lsb];
default:
assert(0);
return 0;
}
}
static uint8_t
intel_fdc_read_register(struct intel_fdc_struct* p_fdc, uint8_t reg) {
uint8_t ret = 0;
reg = (reg & 0x3F);
if (reg < k_intel_num_registers) {
return p_fdc->regs[reg];
}
reg = (reg & 0x07);
switch (reg) {
case (k_intel_fdc_register_mmio_drive_in & 0x07):
ret = intel_fdc_read_drive_in(p_fdc);
break;
case (k_intel_fdc_register_mmio_drive_out & 0x07):
/* DFS-1.2 reads drive out in normal operation. */
ret = p_fdc->drive_out;
break;
case (k_intel_fdc_register_mmio_clocks & 0x07):
ret = p_fdc->mmio_clocks;
break;
case (k_intel_fdc_register_mmio_data & 0x07):
ret = p_fdc->mmio_data;
break;
default:
log_do_log(k_log_disc,
k_log_unimplemented,
"direct read to MMIO register %d",
reg);
break;
}
return ret;
}
static void
intel_fdc_write_register(struct intel_fdc_struct* p_fdc,
uint8_t reg,
uint8_t val) {
reg = (reg & 0x3F);
if (reg < k_intel_num_registers) {
p_fdc->regs[reg] = val;
return;
}
reg = (reg & 0x07);
switch (reg) {
case (k_intel_fdc_register_mmio_drive_out & 0x07):
/* Bit 0x20 is important as it's used to select the side of the disc for
* double sided discs.
* Bit 0x08 is important as it provides manual head load / unload control,
* which includes motor spin up / down.
* The parameter also includes drive select bits which override those in
* the command.
*/
intel_fdc_set_drive_out(p_fdc, val);
break;
case (k_intel_fdc_register_mmio_clocks & 0x07):
p_fdc->mmio_clocks = val;
break;
case (k_intel_fdc_register_mmio_data & 0x07):
p_fdc->mmio_data = val;
break;
default:
log_do_log(k_log_disc,
k_log_unimplemented,
"direct write to MMIO register %d",
reg);
break;
}
}
static void
intel_fdc_do_seek(struct intel_fdc_struct* p_fdc, int call_context) {