forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathide.cpp
1238 lines (1158 loc) · 34.3 KB
/
ide.cpp
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
/*
* UAE - The Un*x Amiga Emulator
*
* IDE HD/ATAPI CD emulation
*
* (c) 2006 - 2015 Toni Wilen
*/
#define IDE_LOG 0
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "blkdev.h"
#include "filesys.h"
#include "gui.h"
#include "uae.h"
#include "memory.h"
#include "newcpu.h"
#include "threaddep/thread.h"
#include "debug.h"
#include "savestate.h"
#include "scsi.h"
#include "ide.h"
/* STATUS bits */
#define IDE_STATUS_ERR 0x01 // 0
#define IDE_STATUS_IDX 0x02 // 1
#define IDE_STATUS_DRQ 0x08 // 3
#define IDE_STATUS_DSC 0x10 // 4
#define IDE_STATUS_DRDY 0x40 // 6
#define IDE_STATUS_BSY 0x80 // 7
#define ATAPI_STATUS_CHK IDE_STATUS_ERR
/* ERROR bits */
#define IDE_ERR_UNC 0x40
#define IDE_ERR_MC 0x20
#define IDE_ERR_IDNF 0x10
#define IDE_ERR_MCR 0x08
#define IDE_ERR_ABRT 0x04
#define IDE_ERR_NM 0x02
#define ATAPI_ERR_EOM 0x02
#define ATAPI_ERR_ILI 0x01
/* ATAPI interrupt reason (Sector Count) */
#define ATAPI_IO 0x02
#define ATAPI_CD 0x01
#define ATAPI_MAX_TRANSFER 32768
#define MAX_IDE_MULTIPLE_SECTORS 64
static void ide_grow_buffer(struct ide_hdf *ide, int newsize)
{
if (ide->secbuf_size >= newsize)
return;
uae_u8 *oldbuf = ide->secbuf;
int oldsize = ide->secbuf_size;
ide->secbuf_size = newsize + 16384;
if (oldsize)
write_log(_T("IDE%d buffer %d -> %d\n"), ide->num, oldsize, ide->secbuf_size);
ide->secbuf = xmalloc(uae_u8, ide->secbuf_size);
memcpy(ide->secbuf, oldbuf, oldsize);
xfree(oldbuf);
}
static void pw (struct ide_hdf *ide, int offset, uae_u16 w)
{
if (ide->byteswap) {
ide->secbuf[offset * 2 + 1] = (uae_u8)w;
ide->secbuf[offset * 2 + 0] = w >> 8;
} else {
ide->secbuf[offset * 2 + 0] = (uae_u8)w;
ide->secbuf[offset * 2 + 1] = w >> 8;
}
}
static void ps (struct ide_hdf *ide, int offset, const TCHAR *src, int max)
{
int i, len, swap;
char *s;
swap = ide->byteswap ? 0 : 1;
offset *= 2;
s = ua (src);
len = strlen (s);
for (i = 0; i < max; i++) {
char c = ' ';
if (i < len)
c = s[i];
ide->secbuf[offset ^ swap] = c;
offset++;
}
xfree (s);
}
bool ide_isdrive (struct ide_hdf *ide)
{
return ide && (ide->hdhfd.size != 0 || ide->atapi);
}
static void ide_interrupt (struct ide_hdf *ide)
{
ide->regs.ide_status |= IDE_STATUS_BSY;
ide->regs.ide_status &= ~IDE_STATUS_DRQ;
ide->irq_delay = 2;
}
static void ide_fast_interrupt (struct ide_hdf *ide)
{
ide->regs.ide_status |= IDE_STATUS_BSY;
ide->regs.ide_status &= ~IDE_STATUS_DRQ;
ide->irq_delay = 1;
}
static bool ide_interrupt_do (struct ide_hdf *ide)
{
uae_u8 os = ide->regs.ide_status;
ide->regs.ide_status &= ~IDE_STATUS_DRQ;
if (ide->intdrq)
ide->regs.ide_status |= IDE_STATUS_DRQ;
ide->regs.ide_status &= ~IDE_STATUS_BSY;
if (IDE_LOG > 1)
write_log (_T("IDE INT %02X -> %02X\n"), os, ide->regs.ide_status);
ide->intdrq = false;
ide->irq_delay = 0;
if (ide->regs.ide_devcon & 2)
return false;
ide->irq = 1;
return true;
}
bool ide_drq_check(struct ide_hdf *idep)
{
for (int i = 0; i < 2; i++) {
struct ide_hdf *ide = i == 0 ? idep : idep->pair;
if (ide) {
if (ide->regs.ide_status & IDE_STATUS_DRQ)
return true;
}
}
return false;
}
bool ide_interrupt_check(struct ide_hdf *idep)
{
bool irq = false;
for (int i = 0; i < 2; i++) {
struct ide_hdf *ide = i == 0 ? idep : idep->pair;
if (ide) {
if (ide->irq_delay > 0) {
ide->irq_delay--;
if (ide->irq_delay == 0) {
ide_interrupt_do (ide);
}
}
if (ide->irq && !(ide->regs.ide_devcon & 2))
irq = true;
}
}
return irq;
}
static void ide_fail_err (struct ide_hdf *ide, uae_u8 err)
{
ide->regs.ide_error |= err;
if (ide->ide_drv == 1 && !ide_isdrive (ide->pair)) {
ide->pair->regs.ide_status |= IDE_STATUS_ERR;
}
ide->regs.ide_status |= IDE_STATUS_ERR;
ide_interrupt (ide);
}
static void ide_fail (struct ide_hdf *ide)
{
ide_fail_err (ide, IDE_ERR_ABRT);
}
static void ide_data_ready (struct ide_hdf *ide)
{
memset (ide->secbuf, 0, ide->blocksize);
ide->data_offset = 0;
ide->data_size = ide->blocksize;
ide->data_multi = 1;
ide->intdrq = true;
ide_interrupt (ide);
}
static void ide_recalibrate (struct ide_hdf *ide)
{
write_log (_T("IDE%d recalibrate\n"), ide->num);
ide->regs.ide_sector = 0;
ide->regs.ide_lcyl = ide->regs.ide_hcyl = 0;
ide_interrupt (ide);
}
static void ide_identify_drive (struct ide_hdf *ide)
{
uae_u64 totalsecs;
int v;
uae_u8 *buf = ide->secbuf;
TCHAR tmp[100];
bool atapi = ide->atapi;
if (!ide_isdrive (ide)) {
ide_fail (ide);
return;
}
memset (buf, 0, ide->blocksize);
if (IDE_LOG > 0)
write_log (_T("IDE%d identify drive\n"), ide->num);
ide_data_ready (ide);
ide->direction = 0;
pw (ide, 0, atapi ? 0x85c0 : 1 << 6);
pw (ide, 1, ide->hdhfd.cyls_def);
pw (ide, 2, 0xc837);
pw (ide, 3, ide->hdhfd.heads_def);
pw (ide, 4, ide->blocksize * ide->hdhfd.secspertrack_def);
pw (ide, 5, ide->blocksize);
pw (ide, 6, ide->hdhfd.secspertrack_def);
ps (ide, 10, _T("68000"), 20); /* serial */
pw (ide, 20, 3);
pw (ide, 21, ide->blocksize);
pw (ide, 22, 4);
ps (ide, 23, _T("0.6"), 8); /* firmware revision */
if (ide->atapi)
_tcscpy (tmp, _T("UAE-ATAPI"));
else
_stprintf (tmp, _T("UAE-IDE %s"), ide->hdhfd.hfd.product_id);
ps (ide, 27, tmp, 40); /* model */
pw (ide, 47, MAX_IDE_MULTIPLE_SECTORS >> (ide->blocksize / 512 - 1)); /* max sectors in multiple mode */
pw (ide, 48, 1);
pw (ide, 49, (1 << 9) | (1 << 8)); /* LBA and DMA supported */
pw (ide, 51, 0x200); /* PIO cycles */
pw (ide, 52, 0x200); /* DMA cycles */
pw (ide, 53, 1 | 2 | 4);
pw (ide, 54, ide->hdhfd.cyls);
pw (ide, 55, ide->hdhfd.heads);
pw (ide, 56, ide->hdhfd.secspertrack);
totalsecs = ide->hdhfd.cyls * ide->hdhfd.heads * ide->hdhfd.secspertrack;
pw (ide, 57, (uae_u16)totalsecs);
pw (ide, 58, (uae_u16)(totalsecs >> 16));
v = ide->multiple_mode;
pw (ide, 59, (v > 0 ? 0x100 : 0) | v);
totalsecs = ide->blocksize ? ide->hdhfd.size / ide->blocksize : 0;
if (totalsecs > 0x0fffffff)
totalsecs = 0x0fffffff;
pw (ide, 60, (uae_u16)totalsecs);
pw (ide, 61, (uae_u16)(totalsecs >> 16));
pw (ide, 62, 0x0f);
pw (ide, 63, 0x0f);
pw (ide, 64, 0x03); /* PIO3 and PIO4 */
pw (ide, 65, 120); /* MDMA2 supported */
pw (ide, 66, 120);
pw (ide, 67, 120);
pw (ide, 68, 120);
pw (ide, 80, (1 << 1) | (1 << 2) | (1 << 3) | (1 << 4) | (1 << 5) | (1 << 6)); /* ATA-1 to ATA-6 */
pw (ide, 81, 0x1c); /* ATA revision */
pw (ide, 82, (1 << 14) | (atapi ? 0x10 | 4 : 0)); /* NOP, ATAPI: PACKET and Removable media features supported */
pw (ide, 83, (1 << 14) | (1 << 13) | (1 << 12) | (ide->lba48 ? (1 << 10) : 0)); /* cache flushes, LBA 48 supported */
pw (ide, 84, 1 << 14);
pw (ide, 85, 1 << 14);
pw (ide, 86, (1 << 14) | (1 << 13) | (1 << 12) | (ide->lba48 ? (1 << 10) : 0)); /* cache flushes, LBA 48 enabled */
pw (ide, 87, 1 << 14);
pw (ide, 88, (1 << 5) | (1 << 4) | (1 << 3) | (1 << 2) | (1 << 1) | (1 << 0)); /* UDMA modes */
pw (ide, 93, (1 << 14) | (1 << 13) | (1 << 0));
if (ide->lba48) {
totalsecs = ide->hdhfd.size / ide->blocksize;
pw (ide, 100, (uae_u16)(totalsecs >> 0));
pw (ide, 101, (uae_u16)(totalsecs >> 16));
pw (ide, 102, (uae_u16)(totalsecs >> 32));
pw (ide, 103, (uae_u16)(totalsecs >> 48));
}
}
static void set_signature (struct ide_hdf *ide)
{
if (ide->atapi) {
ide->regs.ide_sector = 1;
ide->regs.ide_nsector = 1;
ide->regs.ide_lcyl = 0x14;
ide->regs.ide_hcyl = 0xeb;
ide->regs.ide_status = 0;
ide->atapi_drdy = false;
} else {
ide->regs.ide_nsector = 1;
ide->regs.ide_sector = 1;
ide->regs.ide_lcyl = 0;
ide->regs.ide_hcyl = 0;
ide->regs.ide_status = 0;
}
ide->regs.ide_error = 0x01; // device ok
ide->packet_state = 0;
}
static void reset_device (struct ide_hdf *ide, bool both)
{
set_signature (ide);
if (both)
set_signature (ide->pair);
}
static void ide_execute_drive_diagnostics (struct ide_hdf *ide, bool irq)
{
reset_device (ide, irq);
if (irq)
ide_interrupt (ide);
else
ide->regs.ide_status &= ~IDE_STATUS_BSY;
}
static void ide_initialize_drive_parameters (struct ide_hdf *ide)
{
if (ide->hdhfd.size) {
ide->hdhfd.secspertrack = ide->regs.ide_nsector == 0 ? 256 : ide->regs.ide_nsector;
ide->hdhfd.heads = (ide->regs.ide_select & 15) + 1;
if (ide->hdhfd.hfd.ci.pcyls)
ide->hdhfd.cyls = ide->hdhfd.hfd.ci.pcyls;
else
ide->hdhfd.cyls = (ide->hdhfd.size / ide->blocksize) / (ide->hdhfd.secspertrack * ide->hdhfd.heads);
if (ide->hdhfd.heads * ide->hdhfd.cyls * ide->hdhfd.secspertrack > 16515072 || ide->lba48) {
if (ide->hdhfd.hfd.ci.pcyls)
ide->hdhfd.cyls = ide->hdhfd.hfd.ci.pcyls;
else
ide->hdhfd.cyls = ide->hdhfd.cyls_def;
ide->hdhfd.heads = ide->hdhfd.heads_def;
ide->hdhfd.secspertrack = ide->hdhfd.secspertrack_def;
}
} else {
ide->regs.ide_error |= IDE_ERR_ABRT;
ide->regs.ide_status |= IDE_STATUS_ERR;
}
write_log (_T("IDE%d initialize drive parameters, CYL=%d,SPT=%d,HEAD=%d\n"),
ide->num, ide->hdhfd.cyls, ide->hdhfd.secspertrack, ide->hdhfd.heads);
ide_interrupt (ide);
}
static void ide_set_multiple_mode (struct ide_hdf *ide)
{
write_log (_T("IDE%d drive multiple mode = %d\n"), ide->num, ide->regs.ide_nsector);
ide->multiple_mode = ide->regs.ide_nsector;
ide_interrupt (ide);
}
static void ide_set_features (struct ide_hdf *ide)
{
int type = ide->regs.ide_nsector >> 3;
int mode = ide->regs.ide_nsector & 7;
write_log (_T("IDE%d set features %02X (%02X)\n"), ide->num, ide->regs.ide_feat, ide->regs.ide_nsector);
ide_fail (ide);
}
static void get_lbachs (struct ide_hdf *ide, uae_u64 *lbap, unsigned int *cyl, unsigned int *head, unsigned int *sec)
{
if (ide->lba48 && ide->lba48cmd && (ide->regs.ide_select & 0x40)) {
uae_u64 lba;
lba = (ide->regs.ide_hcyl << 16) | (ide->regs.ide_lcyl << 8) | ide->regs.ide_sector;
lba |= ((ide->regs.ide_hcyl2 << 16) | (ide->regs.ide_lcyl2 << 8) | ide->regs.ide_sector2) << 24;
*lbap = lba;
} else {
if (ide->regs.ide_select & 0x40) {
*lbap = ((ide->regs.ide_select & 15) << 24) | (ide->regs.ide_hcyl << 16) | (ide->regs.ide_lcyl << 8) | ide->regs.ide_sector;
} else {
*cyl = (ide->regs.ide_hcyl << 8) | ide->regs.ide_lcyl;
*head = ide->regs.ide_select & 15;
*sec = ide->regs.ide_sector;
*lbap = (((*cyl) * ide->hdhfd.heads + (*head)) * ide->hdhfd.secspertrack) + (*sec) - 1;
}
}
}
static int get_nsec (struct ide_hdf *ide)
{
if (ide->lba48 && ide->lba48cmd)
return (ide->regs.ide_nsector == 0 && ide->regs.ide_nsector2 == 0) ? 65536 : (ide->regs.ide_nsector2 * 256 + ide->regs.ide_nsector);
else
return ide->regs.ide_nsector == 0 ? 256 : ide->regs.ide_nsector;
}
static int dec_nsec (struct ide_hdf *ide, int v)
{
if (ide->lba48 && ide->lba48cmd) {
uae_u16 nsec;
nsec = ide->regs.ide_nsector2 * 256 + ide->regs.ide_nsector;
ide->regs.ide_nsector -= v;
ide->regs.ide_nsector2 = nsec >> 8;
ide->regs.ide_nsector = nsec & 0xff;
return (ide->regs.ide_nsector2 << 8) | ide->regs.ide_nsector;
} else {
ide->regs.ide_nsector -= v;
return ide->regs.ide_nsector;
}
}
static void put_lbachs (struct ide_hdf *ide, uae_u64 lba, unsigned int cyl, unsigned int head, unsigned int sec, unsigned int inc)
{
if (ide->lba48 && ide->lba48cmd) {
lba += inc;
ide->regs.ide_hcyl = (lba >> 16) & 0xff;
ide->regs.ide_lcyl = (lba >> 8) & 0xff;
ide->regs.ide_sector = lba & 0xff;
lba >>= 24;
ide->regs.ide_hcyl2 = (lba >> 16) & 0xff;
ide->regs.ide_lcyl2 = (lba >> 8) & 0xff;
ide->regs.ide_sector2 = lba & 0xff;
} else {
if (ide->regs.ide_select & 0x40) {
lba += inc;
ide->regs.ide_select &= ~15;
ide->regs.ide_select |= (lba >> 24) & 15;
ide->regs.ide_hcyl = (lba >> 16) & 0xff;
ide->regs.ide_lcyl = (lba >> 8) & 0xff;
ide->regs.ide_sector = lba & 0xff;
} else {
sec += inc;
while (sec >= ide->hdhfd.secspertrack) {
sec -= ide->hdhfd.secspertrack;
head++;
if (head >= ide->hdhfd.heads) {
head -= ide->hdhfd.heads;
cyl++;
}
}
ide->regs.ide_select &= ~15;
ide->regs.ide_select |= head;
ide->regs.ide_sector = sec;
ide->regs.ide_hcyl = cyl >> 8;
ide->regs.ide_lcyl = (uae_u8)cyl;
}
}
}
static void check_maxtransfer (struct ide_hdf *ide, int state)
{
if (state == 1) {
// transfer was started
if (ide->maxtransferstate < 2 && ide->regs.ide_nsector == 0) {
ide->maxtransferstate = 1;
} else if (ide->maxtransferstate == 2) {
// second transfer was started (part of split)
write_log (_T("IDE maxtransfer check detected split >256 block transfer\n"));
ide->maxtransferstate = 0;
} else {
ide->maxtransferstate = 0;
}
} else if (state == 2) {
// address was read
if (ide->maxtransferstate == 1)
ide->maxtransferstate++;
else
ide->maxtransferstate = 0;
}
}
static void setdrq (struct ide_hdf *ide)
{
ide->regs.ide_status |= IDE_STATUS_DRQ;
ide->regs.ide_status &= ~IDE_STATUS_BSY;
}
static void setbsy (struct ide_hdf *ide)
{
ide->regs.ide_status |= IDE_STATUS_BSY;
ide->regs.ide_status &= ~IDE_STATUS_DRQ;
}
static void process_rw_command (struct ide_hdf *ide)
{
setbsy (ide);
write_comm_pipe_u32 (&ide->its->requests, ide->num, 1);
}
static void process_packet_command (struct ide_hdf *ide)
{
setbsy (ide);
write_comm_pipe_u32 (&ide->its->requests, ide->num | 0x80, 1);
}
static void atapi_data_done (struct ide_hdf *ide)
{
ide->regs.ide_nsector = ATAPI_IO | ATAPI_CD;
ide->regs.ide_status = IDE_STATUS_DRDY;
ide->data_size = 0;
ide->packet_data_offset = 0;
ide->data_offset = 0;
}
static bool atapi_set_size (struct ide_hdf *ide)
{
int size;
size = ide->data_size;
ide->data_offset = 0;
if (!size) {
ide->packet_state = 0;
ide->packet_transfer_size = 0;
return false;
}
if (ide->packet_state == 2) {
if (size > ide->packet_data_size)
size = ide->packet_data_size;
if (size > ATAPI_MAX_TRANSFER)
size = ATAPI_MAX_TRANSFER;
ide->packet_transfer_size = size & ~1;
ide->regs.ide_lcyl = size & 0xff;
ide->regs.ide_hcyl = size >> 8;
} else {
ide->packet_transfer_size = 12;
}
if (IDE_LOG > 1)
write_log (_T("ATAPI data transfer %d/%d bytes\n"), ide->packet_transfer_size, ide->data_size);
return true;
}
static void atapi_packet (struct ide_hdf *ide)
{
ide->packet_data_offset = 0;
ide->packet_data_size = (ide->regs.ide_hcyl << 8) | ide->regs.ide_lcyl;
if (ide->packet_data_size == 65535)
ide->packet_data_size = 65534;
ide->data_size = 12;
if (IDE_LOG > 0)
write_log (_T("ATAPI packet command. Data size = %d\n"), ide->packet_data_size);
ide->packet_state = 1;
ide->data_multi = 1;
ide->data_offset = 0;
ide->regs.ide_nsector = ATAPI_CD;
ide->regs.ide_error = 0;
if (atapi_set_size (ide))
setdrq (ide);
}
static void do_packet_command (struct ide_hdf *ide)
{
memcpy (ide->scsi->cmd, ide->secbuf, 12);
ide->scsi->cmd_len = 12;
if (IDE_LOG > 0) {
uae_u8 *c = ide->scsi->cmd;
write_log (_T("ATASCSI %02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x\n"),
c[0], c[1], c[2], c[3], c[4], c[5], c[6], c[7], c[8], c[9], c[10], c[11]);
}
ide->direction = 0;
scsi_emulate_analyze (ide->scsi);
if (ide->scsi->direction <= 0) {
// data in
scsi_emulate_cmd (ide->scsi);
ide->data_size = ide->scsi->data_len;
ide->regs.ide_status = 0;
if (ide->scsi->status) {
// error
ide->regs.ide_error = (ide->scsi->sense[2] << 4) | 4;
atapi_data_done (ide);
ide->regs.ide_status |= ATAPI_STATUS_CHK;
atapi_set_size (ide);
return;
} else if (ide->scsi->data_len) {
// data in
ide_grow_buffer(ide, ide->scsi->data_len);
memcpy (ide->secbuf, ide->scsi->buffer, ide->scsi->data_len);
ide->regs.ide_nsector = ATAPI_IO;
} else {
// no data
atapi_data_done (ide);
}
} else {
// data out
ide->direction = 1;
ide->regs.ide_nsector = 0;
ide->data_size = ide->scsi->data_len;
}
ide->packet_state = 2; // data phase
if (atapi_set_size (ide))
ide->intdrq = true;
}
static void do_process_packet_command (struct ide_hdf *ide)
{
if (ide->packet_state == 1) {
do_packet_command (ide);
} else {
ide->packet_data_offset += ide->packet_transfer_size;
if (!ide->direction) {
// data still remaining, next transfer
if (atapi_set_size (ide))
ide->intdrq = true;
} else {
if (atapi_set_size (ide)) {
ide->intdrq = true;
} else {
memcpy (&ide->scsi->buffer, ide->secbuf, ide->data_size);
ide->scsi->data_len = ide->data_size;
scsi_emulate_cmd (ide->scsi);
if (IDE_LOG > 1)
write_log (_T("IDE%d ATAPI write finished, %d bytes\n"), ide->num, ide->data_size);
}
}
}
ide_fast_interrupt (ide);
}
static void do_process_rw_command (struct ide_hdf *ide)
{
unsigned int cyl, head, sec, nsec;
uae_u64 lba;
bool last;
ide->data_offset = 0;
nsec = get_nsec (ide);
get_lbachs (ide, &lba, &cyl, &head, &sec);
if (IDE_LOG > 1)
write_log (_T("IDE%d off=%d, nsec=%d (%d) lba48=%d\n"), ide->num, (uae_u32)lba, nsec, ide->multiple_mode, ide->lba48 + ide->lba48cmd);
if (nsec * ide->blocksize > ide->hdhfd.size - lba * ide->blocksize) {
nsec = (ide->hdhfd.size - lba * ide->blocksize) / ide->blocksize;
if (IDE_LOG > 1)
write_log (_T("IDE%d nsec changed to %d\n"), ide->num, nsec);
}
if (nsec <= 0) {
ide_data_ready (ide);
ide_fail_err (ide, IDE_ERR_IDNF);
return;
}
if (nsec > ide->data_multi)
nsec = ide->data_multi;
ide_grow_buffer(ide, nsec * ide->blocksize);
if (ide->direction) {
hdf_write (&ide->hdhfd.hfd, ide->secbuf, lba * ide->blocksize, nsec * ide->blocksize);
if (IDE_LOG > 1)
write_log (_T("IDE%d write, %d bytes written\n"), ide->num, nsec * ide->blocksize);
} else {
hdf_read (&ide->hdhfd.hfd, ide->secbuf, lba * ide->blocksize, nsec * ide->blocksize);
if (IDE_LOG > 1)
write_log (_T("IDE%d read, read %d bytes\n"), ide->num, nsec * ide->blocksize);
}
ide->intdrq = true;
last = dec_nsec (ide, nsec) == 0;
put_lbachs (ide, lba, cyl, head, sec, last ? nsec - 1 : nsec);
if (last && ide->direction) {
ide->intdrq = false;
if (IDE_LOG > 1)
write_log (_T("IDE%d write finished\n"), ide->num);
}
ide_fast_interrupt (ide);
}
static void ide_read_sectors (struct ide_hdf *ide, int flags)
{
unsigned int cyl, head, sec, nsec;
uae_u64 lba;
int multi = flags & 1;
ide->lba48cmd = (flags & 2) != 0;
if (multi && ide->multiple_mode == 0) {
ide_fail (ide);
return;
}
check_maxtransfer (ide, 1);
gui_flicker_led (LED_HD, ide->num, 1);
nsec = get_nsec (ide);
get_lbachs (ide, &lba, &cyl, &head, &sec);
if (lba * ide->blocksize >= ide->hdhfd.size) {
ide_data_ready (ide);
ide_fail_err (ide, IDE_ERR_IDNF);
return;
}
if (IDE_LOG > 0)
write_log (_T("IDE%d read off=%d, sec=%d (%d) lba48=%d\n"), ide->num, (uae_u32)lba, nsec, ide->multiple_mode, ide->lba48 + ide->lba48cmd);
ide->data_multi = multi ? ide->multiple_mode : 1;
ide->data_offset = 0;
ide->data_size = nsec * ide->blocksize;
ide->direction = 0;
// read start: preload sector(s), then trigger interrupt.
process_rw_command (ide);
}
static void ide_write_sectors (struct ide_hdf *ide, int flags)
{
unsigned int cyl, head, sec, nsec;
uae_u64 lba;
int multi = flags & 1;
ide->lba48cmd = (flags & 2) != 0;
if (multi && ide->multiple_mode == 0) {
ide_fail (ide);
return;
}
check_maxtransfer (ide, 1);
gui_flicker_led (LED_HD, ide->num, 2);
nsec = get_nsec (ide);
get_lbachs (ide, &lba, &cyl, &head, &sec);
if (lba * ide->blocksize >= ide->hdhfd.size) {
ide_data_ready (ide);
ide_fail_err (ide, IDE_ERR_IDNF);
return;
}
if (IDE_LOG > 0)
write_log (_T("IDE%d write off=%d, sec=%d (%d) lba48=%d\n"), ide->num, (uae_u32)lba, nsec, ide->multiple_mode, ide->lba48 + ide->lba48cmd);
if (nsec * ide->blocksize > ide->hdhfd.size - lba * ide->blocksize)
nsec = (ide->hdhfd.size - lba * ide->blocksize) / ide->blocksize;
if (nsec <= 0) {
ide_data_ready (ide);
ide_fail_err (ide, IDE_ERR_IDNF);
return;
}
ide->data_multi = multi ? ide->multiple_mode : 1;
ide->data_offset = 0;
ide->data_size = nsec * ide->blocksize;
ide->direction = 1;
// write start: set DRQ and clear BSY. No interrupt.
ide->regs.ide_status |= IDE_STATUS_DRQ;
ide->regs.ide_status &= ~IDE_STATUS_BSY;
}
static void ide_do_command (struct ide_hdf *ide, uae_u8 cmd)
{
int lba48 = ide->lba48;
if (IDE_LOG > 1)
write_log (_T("**** IDE%d command %02X\n"), ide->num, cmd);
ide->regs.ide_status &= ~ (IDE_STATUS_DRDY | IDE_STATUS_DRQ | IDE_STATUS_ERR);
ide->regs.ide_error = 0;
ide->lba48cmd = false;
if (ide->atapi) {
gui_flicker_led (LED_CD, ide->num, 1);
ide->atapi_drdy = true;
if (cmd == 0x00) { /* nop */
ide_interrupt (ide);
} else if (cmd == 0x08) { /* device reset */
ide_execute_drive_diagnostics (ide, true);
} else if (cmd == 0xa1) { /* identify packet device */
ide_identify_drive (ide);
} else if (cmd == 0xa0) { /* packet */
atapi_packet (ide);
} else if (cmd == 0x90) { /* execute drive diagnostics */
ide_execute_drive_diagnostics (ide, true);
} else {
ide_execute_drive_diagnostics (ide, false);
ide->atapi_drdy = false;
ide_fail (ide);
write_log (_T("IDE%d: unknown ATAPI command 0x%02x\n"), ide->num, cmd);
}
} else {
if (cmd == 0x10) { /* recalibrate */
ide_recalibrate (ide);
} else if (cmd == 0xec) { /* identify drive */
ide_identify_drive (ide);
} else if (cmd == 0x90) { /* execute drive diagnostics */
ide_execute_drive_diagnostics (ide, true);
} else if (cmd == 0x91) { /* initialize drive parameters */
ide_initialize_drive_parameters (ide);
} else if (cmd == 0xc6) { /* set multiple mode */
ide_set_multiple_mode (ide);
} else if (cmd == 0x20 || cmd == 0x21) { /* read sectors */
ide_read_sectors (ide, 0);
} else if (cmd == 0x24 && lba48) { /* read sectors ext */
ide_read_sectors (ide, 2);
} else if (cmd == 0xc4) { /* read multiple */
ide_read_sectors (ide, 1);
} else if (cmd == 0x29 && lba48) { /* read multiple ext */
ide_read_sectors (ide, 1|2);
} else if (cmd == 0x30 || cmd == 0x31) { /* write sectors */
ide_write_sectors (ide, 0);
} else if (cmd == 0x34 && lba48) { /* write sectors ext */
ide_write_sectors (ide, 2);
} else if (cmd == 0xc5) { /* write multiple */
ide_write_sectors (ide, 1);
} else if (cmd == 0x39 && lba48) { /* write multiple ext */
ide_write_sectors (ide, 1|2);
} else if (cmd == 0x50) { /* format track (nop) */
ide_interrupt (ide);
} else if (cmd == 0xef) { /* set features */
ide_set_features (ide);
} else if (cmd == 0x00) { /* nop */
ide_fail (ide);
} else if (cmd == 0x70) { /* seek */
ide_interrupt (ide);
} else if (cmd == 0xe0 || cmd == 0xe1 || cmd == 0xe7 || cmd == 0xea) { /* standby now/idle/flush cache/flush cache ext */
ide_interrupt (ide);
} else if (cmd == 0xe5) { /* check power mode */
ide->regs.ide_nsector = 0xff;
ide_interrupt (ide);
} else {
ide_fail (ide);
write_log (_T("IDE%d: unknown ATA command 0x%02x\n"), ide->num, cmd);
}
}
}
uae_u16 ide_get_data (struct ide_hdf *ide)
{
bool irq = false;
uae_u16 v;
if (ide->data_size == 0) {
if (IDE_LOG > 0)
write_log (_T("IDE%d DATA but no data left!? %02X PC=%08X\n"), ide->num, ide->regs.ide_status, m68k_getpc ());
if (!ide_isdrive (ide))
return 0xffff;
return 0;
}
if (ide->packet_state) {
v = ide->secbuf[ide->packet_data_offset + ide->data_offset + 1] | (ide->secbuf[ide->packet_data_offset + ide->data_offset + 0] << 8);
if (IDE_LOG > 4)
write_log (_T("IDE%d DATA read %04x\n"), ide->num, v);
ide->data_offset += 2;
if (ide->data_size < 0)
ide->data_size += 2;
else
ide->data_size -= 2;
if (ide->data_offset == ide->packet_transfer_size) {
if (IDE_LOG > 1)
write_log (_T("IDE%d ATAPI partial read finished, %d bytes remaining\n"), ide->num, ide->data_size);
if (ide->data_size == 0) {
ide->packet_state = 0;
atapi_data_done (ide);
if (IDE_LOG > 1)
write_log (_T("IDE%d ATAPI read finished, %d bytes\n"), ide->num, ide->packet_data_offset + ide->data_offset);
irq = true;
} else {
process_packet_command (ide);
}
}
} else {
v = ide->secbuf[ide->data_offset + 1] | (ide->secbuf[ide->data_offset + 0] << 8);
if (IDE_LOG > 4)
write_log (_T("IDE%d DATA read %04x\n"), ide->num, v);
ide->data_offset += 2;
if (ide->data_size < 0) {
ide->data_size += 2;
} else {
ide->data_size -= 2;
if (((ide->data_offset % ide->blocksize) == 0) && ((ide->data_offset / ide->blocksize) % ide->data_multi) == 0) {
if (ide->data_size)
process_rw_command (ide);
}
}
if (ide->data_size == 0) {
if (!(ide->regs.ide_status & IDE_STATUS_DRQ)) {
write_log (_T("IDE%d read finished but DRQ was not active?\n"), ide->num);
}
ide->regs.ide_status &= ~IDE_STATUS_DRQ;
if (IDE_LOG > 1)
write_log (_T("IDE%d read finished\n"), ide->num);
}
}
if (irq)
ide_fast_interrupt (ide);
return v;
}
void ide_put_data (struct ide_hdf *ide, uae_u16 v)
{
if (IDE_LOG > 4)
write_log (_T("IDE%d DATA write %04x %d/%d\n"), ide->num, v, ide->data_offset, ide->data_size);
if (ide->data_size == 0) {
if (IDE_LOG > 0)
write_log (_T("IDE%d DATA write without request!? %02X PC=%08X\n"), ide->num, ide->regs.ide_status, m68k_getpc ());
return;
}
ide_grow_buffer(ide, ide->packet_data_offset + ide->data_offset + 2);
ide->secbuf[ide->packet_data_offset + ide->data_offset + 1] = v & 0xff;
ide->secbuf[ide->packet_data_offset + ide->data_offset + 0] = v >> 8;
ide->data_offset += 2;
ide->data_size -= 2;
if (ide->packet_state) {
if (ide->data_offset == ide->packet_transfer_size) {
if (IDE_LOG > 0) {
uae_u16 v = (ide->regs.ide_hcyl << 8) | ide->regs.ide_lcyl;
write_log (_T("Data size after command received = %d (%d)\n"), v, ide->packet_data_size);
}
process_packet_command (ide);
}
} else {
if (ide->data_size == 0) {
process_rw_command (ide);
} else if (((ide->data_offset % ide->blocksize) == 0) && ((ide->data_offset / ide->blocksize) % ide->data_multi) == 0) {
process_rw_command (ide);
}
}
}
uae_u32 ide_read_reg (struct ide_hdf *ide, int ide_reg)
{
uae_u8 v = 0;
bool isdrv = ide_isdrive (ide);
if (!ide)
goto end;
if (ide->regs.ide_status & IDE_STATUS_BSY)
ide_reg = IDE_STATUS;
if (!ide_isdrive (ide)) {
if (ide_reg == IDE_STATUS && ide->pair->irq)
ide->pair->irq = 0;
if (ide_isdrive (ide->pair))
v = 0x01;
else
v = 0xff;
goto end;
}
switch (ide_reg)
{
case IDE_SECONDARY:
case IDE_SECONDARY + 1:
case IDE_SECONDARY + 2:
case IDE_SECONDARY + 3:
case IDE_SECONDARY + 4:
case IDE_SECONDARY + 5:
v = 0xff;
break;
case IDE_DRVADDR:
v = ((ide->ide_drv ? 2 : 1) | ((ide->regs.ide_select & 15) << 2)) ^ 0xff;
break;
case IDE_DATA:
break;
case IDE_ERROR:
v = ide->regs.ide_error;
break;
case IDE_NSECTOR:
if (isdrv) {
if (ide->regs.ide_devcon & 0x80)
v = ide->regs.ide_nsector2;
else
v = ide->regs.ide_nsector;
}
break;
case IDE_SECTOR:
if (isdrv) {
if (ide->regs.ide_devcon & 0x80)
v = ide->regs.ide_sector2;
else
v = ide->regs.ide_sector;
check_maxtransfer (ide, 2);
}
break;
case IDE_LCYL:
if (isdrv) {
if (ide->regs.ide_devcon & 0x80)
v = ide->regs.ide_lcyl2;
else
v = ide->regs.ide_lcyl;
}
break;
case IDE_HCYL:
if (isdrv) {
if (ide->regs.ide_devcon & 0x80)
v = ide->regs.ide_hcyl2;
else
v = ide->regs.ide_hcyl;
}
break;
case IDE_SELECT:
v = ide->regs.ide_select;
break;
case IDE_STATUS:
ide->irq = 0;
/* fall through */
case IDE_DEVCON: /* ALTSTATUS when reading */
if (!isdrv) {
v = 0;
if (ide->regs.ide_error)
v |= IDE_STATUS_ERR;
} else {
v = ide->regs.ide_status;
if (!ide->atapi || (ide->atapi && ide->atapi_drdy))
v |= IDE_STATUS_DRDY | IDE_STATUS_DSC;
}
break;
}
end:
if (IDE_LOG > 2 && ide_reg > 0 && (1 || ide->num > 0))
write_log (_T("IDE%d GET register %d->%02X (%08X)\n"), ide->num, ide_reg, (uae_u32)v & 0xff, m68k_getpc ());
return v;
}
void ide_write_reg (struct ide_hdf *ide, int ide_reg, uae_u32 val)
{
if (!ide)
return;
ide->regs1->ide_devcon &= ~0x80; /* clear HOB */
ide->regs0->ide_devcon &= ~0x80; /* clear HOB */
if (IDE_LOG > 2 && ide_reg > 0 && (1 || ide->num > 0))
write_log (_T("IDE%d PUT register %d=%02X (%08X)\n"), ide->num, ide_reg, (uae_u32)val & 0xff, m68k_getpc ());
switch (ide_reg)
{
case IDE_DRVADDR:
break;
case IDE_DEVCON:
if ((ide->regs.ide_devcon & 4) == 0 && (val & 4) != 0) {
reset_device (ide, true);
if (IDE_LOG > 1)
write_log (_T("IDE%d: SRST\n"), ide->num);
}
ide->regs0->ide_devcon = val;
ide->regs1->ide_devcon = val;
break;
case IDE_DATA:
break;