Repository navigation
Expand file tree
/
Copy pathgdromv3.cpp
More file actions
1384 lines (1161 loc) · 41.6 KB
/
Copy pathgdromv3.cpp
File metadata and controls
1384 lines (1161 loc) · 41.6 KB
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
/*
This file is part of libswirl
*/
#include "license/bsd"
/*
gdrom, v3
Overly complex implementation of a very ugly device
*/
#include "gdromv3.h"
#include "types.h"
#include "libswirl.h"
#include "hw/sh4/sh4_mem.h"
#include "hw/holly/sb.h"
#include "hw/sh4/modules/dmac.h"
#include "hw/sh4/sh4_core.h"
#include "hw/holly/holly_intc.h"
#include "hw/sh4/sh4_mmr.h"
#include "hw/sh4/sh4_sched.h"
#include <thread>
#include <mutex>
#include <atomic>
#include <semaphore.h>
#include <pthread.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "hw/sh4/sh4_sched.h"
void nilprintf(...) {}
#define printf_rm nilprintf
#define printf_ata nilprintf
#define printf_spi nilprintf
#define printf_spicmd nilprintf
#define printf_subcode nilprintf
//// TOPO: FIX THIS <<<
//// Hotfix for android / travis CI
struct GDRomV3_impl final : MMIODevice {
int gdrom_schid;
//Sense: ASC - ASCQ - Key
signed int sns_asc = 0;
signed int sns_ascq = 0;
signed int sns_key = 0;
u32 set_mode_offset = 0;
read_params_t read_params = { 0 };
packet_cmd_t packet_cmd = { 0 };
read_buff_t read_buff = { 0 };
pio_buff_t pio_buff = { gds_waitcmd, 0 };
ata_cmd_t ata_cmd = { 0 };
cdda_t cdda = { 0 };
gd_states gd_state = gds_waitcmd;
DiscType gd_disk_type = NoDisk;
/*
GD rom reset -> GDS_WAITCMD
GDS_WAITCMD -> ATA/SPI command [Command code is on ata_cmd]
SPI Command -> GDS_WAITPACKET -> GDS_SPI_* , depending on input
GDS_SPI_READSECTOR -> Depending on features , it can do quite a few things
*/
u32 data_write_mode = 0;
//Registers
u32 DriveSel = 0;
GD_ErrRegT Error = { 0 };
GD_InterruptReasonT IntReason = { 0 };
GD_FeaturesT Features = { 0 };
GD_SecCountT SecCount = { 0 };
GD_SecNumbT SecNumber = { 0 };
GD_StatusT GDStatus = { 0 };
ByteCount_t ByteCount = { 0 };
//end
// Async disc reads: FillReadBuffer runs on its own high-priority thread
// so SD-card latency never stalls the sh4 thread mid-DMA. Protocol:
// FillReadBufferAsync() kicks the worker; read_buff/read_params must not
// be touched until the fill retires (poll FillReadBufferBusy, or block in
// FillReadBufferSync). The handoff is a semaphore + atomics on purpose:
// both are EINTR/signal-safe, unlike condvars on this glibc (a signal
// landing mid pthread_cond_wait made the noexcept std:: wrapper
// terminate). disc_io_mtx serializes g_GDRDisc access against the emu
// thread's CDDA/PIO reads.
std::thread fill_thread;
sem_t fill_kick;
std::atomic<bool> fill_pending{false};
std::atomic<bool> fill_exit{false};
std::mutex disc_io_mtx;
void FillReadBuffer()
{
read_buff.cache_index = 0;
u32 count = read_params.remaining_sectors;
u32 hint = 0;
if (count > 32)
{
hint = std::max(count - 32, (u32)32);
count = 32;
}
read_buff.cache_size = count * read_params.sector_type;
{
std::lock_guard<std::mutex> lk(disc_io_mtx);
g_GDRDisc->ReadSector(read_buff.cache, read_params.start_sector, count, read_params.sector_type);
}
read_params.start_sector += count;
read_params.remaining_sectors -= count;
}
void FillReadBufferAsync()
{
fill_pending.store(true, std::memory_order_release);
sem_post(&fill_kick);
}
bool FillReadBufferBusy()
{
return fill_pending.load(std::memory_order_acquire);
}
void FillReadBufferSync()
{
while (FillReadBufferBusy())
usleep(50);
}
void fill_thread_main()
{
// no signal may interrupt this thread's blocking waits (the polly2
// kernel module SIGUSR1s every /dev/polly2 holder, nixprof SIGPROFs)
sigset_t all;
sigfillset(&all);
pthread_sigmask(SIG_BLOCK, &all, NULL);
for (;;)
{
while (sem_wait(&fill_kick) != 0 && errno == EINTR)
;
if (fill_exit.load(std::memory_order_acquire))
return;
if (!fill_pending.load(std::memory_order_acquire))
continue;
FillReadBuffer();
fill_pending.store(false, std::memory_order_release);
}
}
void gd_set_state(gd_states state)
{
gd_states prev = gd_state;
gd_state = state;
switch (state)
{
case gds_waitcmd:
GDStatus.DRDY = 1; // Can accept ATA command :)
GDStatus.BSY = 0; // Does not access command block
break;
case gds_procata:
//verify(prev==gds_waitcmd); // Validate the previous command ;)
GDStatus.DRDY = 0; // Can't accept ATA command
GDStatus.BSY = 1; // Accessing command block to process command
gd_process_ata_cmd();
break;
case gds_waitpacket:
verify(prev == gds_procata); // Validate the previous command ;)
// Prepare for packet command
packet_cmd.index = 0;
// Set CoD, clear BSY and IO
IntReason.CoD = 1;
GDStatus.BSY = 0;
IntReason.IO =
8000
0;
// Make DRQ valid
GDStatus.DRQ = 1;
// ATA can optionally raise the interrupt ...
// RaiseInterrupt(holly_GDROM_CMD);
break;
case gds_procpacket:
verify(prev == gds_waitpacket); // Validate the previous state ;)
GDStatus.DRQ = 0; // Can't accept ATA command
GDStatus.BSY = 1; // Accessing command block to process command
gd_process_spi_cmd();
break;
//yep , get/set are the same !
case gds_pio_get_data:
case gds_pio_send_data:
// When preparations are complete, the following steps are carried out at the device.
//(1) Number of bytes to be read is set in "Byte Count" register.
ByteCount.full = (u16)(pio_buff.size << 1);
//(2) IO bit is set and CoD bit is cleared.
IntReason.IO = 1;
IntReason.CoD = 0;
//(3) DRQ bit is set, BSY bit is cleared.
GDStatus.DRQ = 1;
GDStatus.BSY = 0;
//(4) INTRQ is set, and a host interrupt is issued.
asic->RaiseInterrupt(holly_GDROM_CMD);
/*
The number of bytes normally is the byte number in the register at the time of receiving
the command, but it may also be the total of several devices handled by the buffer at that point.
*/
break;
case gds_readsector_pio:
{
/*
If more data are to be sent, the device sets the BSY bit and repeats the above sequence
from step 7.
*/
GDStatus.BSY = 1;
u32 sector_count = read_params.remaining_sectors;
gd_states next_state = gds_pio_end;
if (sector_count > 27)
{
sector_count = 27;
next_state = gds_readsector_pio;
}
{
std::lock_guard<std::mutex> lk(disc_io_mtx);
g_GDRDisc->ReadSector((u8*)&pio_buff.data[0], read_params.start_sector, sector_count, read_params.sector_type);
}
read_params.start_sector += sector_count;
read_params.remaining_sectors -= sector_count;
gd_spi_pio_end(0, sector_count * read_params.sector_type, next_state);
}
break;
case gds_readsector_dma:
if (settings.gdrom.async_dma)
FillReadBufferAsync();
else
FillReadBuffer();
break;
case gds_pio_end:
GDStatus.DRQ = 0;//all data is sent !
gd_set_state(gds_procpacketdone);
break;
case gds_procpacketdone:
/*
7. When the device is ready to send the status, it writes the
final status (IO, CoD, DRDY set, BSY, DRQ cleared) to the "Status" register before making INTRQ valid.
After checking INTRQ, the host reads the "Status" register to check the completion status.
*/
//Set IO, CoD, DRDY
GDStatus.DRDY = 1;
IntReason.CoD = 1;
IntReason.IO = 1;
//Clear DRQ,BSY
GDStatus.DRQ = 0;
GDStatus.BSY = 0;
//Make INTRQ valid
asic->RaiseInterrupt(holly_GDROM_CMD);
//command finished !
gd_set_state(gds_waitcmd);
break;
case gds_process_set_mode:
memcpy(&reply_11[set_mode_offset], pio_buff.data, pio_buff.size << 1);
//end pio transfer ;)
gd_set_state(gds_pio_end);
break;
default:
die("Unhandled GDROM state ...");
break;
}
}
void gd_setdisc()
{
cdda.playing = false;
DiscType newd = NoDisk;
if (g_GDRDisc) {
newd = (DiscType)g_GDRDisc->GetDiscType();
}
if (newd == NoDisk) {
sns_asc = 0x29;
sns_ascq = 0x00;
sns_key = 0x6;
} else {
sns_asc = 0x28;
sns_ascq = 0x00;
}
switch (newd)
{
case NoDisk:
SecNumber.Status = GD_NODISC;
//GDStatus.BSY=0;
//GDStatus.DRDY=1;
break;
case Open:
SecNumber.Status = GD_OPEN;
//GDStatus.BSY=0;
//GDStatus.DRDY=1;
break;
case Busy:
SecNumber.Status = GD_BUSY;
GDStatus.BSY = 1;
GDStatus.DRDY = 0;
break;
default:
if (SecNumber.Status == GD_BUSY)
SecNumber.Status = GD_PAUSE;
else
SecNumber.Status = GD_STANDBY;
//GDStatus.BSY=0;
//GDStatus.DRDY=1;
break;
}
if (gd_disk_type == Busy && newd != Busy)
{
GDStatus.BSY = 0;
GDStatus.DRDY = 1;
}
gd_disk_type = newd;
SecNumber.DiscFormat = gd_disk_type >> 4;
}
void gd_reset()
{
//Reset the drive
gd_setdisc();
gd_set_state(gds_waitcmd);
}
u32 GetFAD(u8* data, bool msf)
{
if (msf)
{
printf("GDROM: MSF FORMAT\n");
return ((data[0] * 60 * 75) + (data[1] * 75) + (data[2]));
}
else
{
return (data[0] << 16) | (data[1] << 8) | (data[2]);
}
}
//This handles the work of setting up the pio regs/state :)
void gd_spi_pio_end(u8* buffer, u32 len, gd_states next_state = gds_pio_end)
{
verify(len < 0xFFFF);
pio_buff.index = 0;
pio_buff.size = len >> 1;
pio_buff.next_state = next_state;
if (buffer != 0)
memcpy(pio_buff.data, buffer, len);
if (len == 0)
gd_set_state(next_state);
else
gd_set_state(gds_pio_send_data);
}
void gd_spi_pio_read_end(u32 len, gd_states next_state)
{
verify(len < 0xFFFF);
pio_buff.index = 0;
pio_buff.size = len >> 1;
pio_buff.next_state = next_state;
if (len == 0)
gd_set_state(next_state);
else
gd_set_state(gds_pio_get_data);
}
void gd_process_ata_cmd()
{
//Any ATA command clears these bits, unless aborted/error :p
Error.ABRT = 0;
if (sns_key == 0x0 || sns_key == 0xB)
GDStatus.CHECK = 0;
else
GDStatus.CHECK = 1;
switch (ata_cmd.command)
{
case ATA_NOP:
printf_ata("ATA_NOP\n");
/*
Setting "abort" in the error register
Setting an error in the status register
Clearing "busy" in the status register
Asserting the INTRQ signal
*/
//this is all very hacky, I don't know if the abort is correct actually
//the above comment is from a wrong place in the docs ...
Error.ABRT = 1;
Error.Sense = sns_key;
GDStatus.BSY = 0;
GDStatus.CHECK = 1;
asic->RaiseInterrupt(holly_GDROM_CMD);
gd_set_state(gds_waitcmd);
break;
case ATA_SOFT_RESET:
{
printf_ata("ATA_SOFT_RESET\n");
//DRV -> preserved -> wtf is it anyway ?
gd_reset();
}
break;
case ATA_EXEC_DIAG:
printf_ata("ATA_EXEC_DIAG\n");
printf("ATA_EXEC_DIAG -- not implemented\n");
break;
case ATA_SPI_PACKET:
printf_ata("ATA_SPI_PACKET\n");
gd_set_state(gds_waitpacket);
break;
case ATA_IDENTIFY_DEV:
printf_ata("ATA_IDENTIFY_DEV\n");
gd_spi_pio_end((u8*
10000
)&reply_a1[packet_cmd.data_8[2] >> 1], packet_cmd.data_8[4]);
break;
case ATA_IDENTIFY:
printf_ata("ATA_IDENTIFY\n");
// Set Signature
DriveSel &= 0xf0;
SecCount.full = 1;
SecNumber.full = 1;
ByteCount.low = 0x14;
ByteCount.hi = 0xeb;
// where did this come from?
//GDStatus.DRQ = 0;
// ABORT command
Error.full = 0x4;
GDStatus.full = 0;
GDStatus.DRDY = 1;
GDStatus.CHECK = 1;
asic->RaiseInterrupt(holly_GDROM_CMD);
gd_set_state(gds_waitcmd);
break;
case ATA_SET_FEATURES:
printf_ata("ATA_SET_FEATURES\n");
//Set features sets :
//Error : ABRT
Error.ABRT = 0; // Command was not aborted ;) [hopefully ...]
//status : DRDY , DSC , DF , CHECK
//DRDY is set on state change
GDStatus.DSC = 0;
GDStatus.DF = 0;
GDStatus.CHECK = 0;
asic->RaiseInterrupt(holly_GDROM_CMD); //???
gd_set_state(gds_waitcmd);
break;
default:
die("Unknown ATA command...");
break;
};
}
void gd_process_spi_cmd()
{
printf_spi("Sense: %02x %02x %02x \n", sns_asc, sns_ascq, sns_key);
printf_spi("SPI command %02x;", packet_cmd.data_8[0]);
printf_spi("Params: %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x \n",
packet_cmd.data_8[0], packet_cmd.data_8[1], packet_cmd.data_8[2], packet_cmd.data_8[3], packet_cmd.data_8[4], packet_cmd.data_8[5],
packet_cmd.data_8[6], packet_cmd.data_8[7], packet_cmd.data_8[8], packet_cmd.data_8[9], packet_cmd.data_8[10], packet_cmd.data_8[11]);
if (sns_key == 0x0 || sns_key == 0xB)
GDStatus.CHECK = 0;
else
GDStatus.CHECK = 1;
switch (packet_cmd.data_8[0])
{
case SPI_TEST_UNIT:
printf_spicmd("SPI_TEST_UNIT\n");
GDStatus.CHECK = SecNumber.Status == GD_BUSY; // Drive is ready ;)
gd_set_state(gds_procpacketdone);
break;
case SPI_REQ_MODE:
printf_spicmd("SPI_REQ_MODE\n");
gd_spi_pio_end((u8*)&reply_11[packet_cmd.data_8[2] >> 1], packet_cmd.data_8[4]);
break;
/////////////////////////////////////////////////
// *FIXME* CHECK FOR DMA, Diff Settings !?!@$#!@%
case SPI_CD_READ:
{
#define readcmd packet_cmd.GDReadBlock
u32 sector_type = 2048;
if (readcmd.head == 1 && readcmd.subh == 1 && readcmd.data == 1 && readcmd.expdtype == 3 && readcmd.other == 0)
sector_type = 2340;
else if (readcmd.head || readcmd.subh || readcmd.other || (!readcmd.data)) // assert
printf("GDROM: *FIXME* ADD MORE CD READ SETTINGS %d %d %d %d 0x%01X\n", readcmd.head, readcmd.subh, readcmd.other, readcmd.data, readcmd.expdtype);
u32 start_sector = GetFAD(&readcmd.b[2], readcmd.prmtype);
u32 sector_count = (readcmd.b[8] << 16) | (readcmd.b[9] << 8) | (readcmd.b[10]);
FillReadBufferSync(); // a stale in-flight fill must retire before re-arming
read_params.start_sector = start_sector;
read_params.remaining_sectors = sector_count;
read_params.sector_type = sector_type;//yeah i know , not really many types supported...
printf_spicmd("SPI_CD_READ - Sector=%d Size=%d/%d DMA=%d\n", read_params.start_sector, read_params.remaining_sectors, read_params.sector_type, Features.CDRead.DMA);
if (Features.CDRead.DMA == 1)
{
gd_set_state(gds_readsector_dma);
}
else
{
gd_set_state(gds_readsector_pio);
}
}
break;
case SPI_GET_TOC:
{
printf_spicmd("SPI_GET_TOC\n");
//printf("SPI_GET_TOC - %d\n",(packet_cmd.data_8[4]) | (packet_cmd.data_8[3]<<8) );
u32 toc_gd[102];
//toc - dd/sd
g_GDRDisc->GetToc(&toc_gd[0], packet_cmd.data_8[1] & 0x1);
gd_spi_pio_end((u8*)&toc_gd[0], (packet_cmd.data_8[4]) | (packet_cmd.data_8[3] << 8));
}
break;
//mount/map drive ? some kind of reset/unlock ??
//seems like a non data command :)
case 0x70:
printf_spicmd("SPI : unknown ? [0x70]\n");
//printf("SPI : unknown ? [0x70]\n");
/*GDStatus.full=0x50; //FIXME
RaiseInterrupt(holly_GDROM_CMD);*/
gd_set_state(gds_procpacketdone);
break;
// Command 71 seems to trigger some sort of authentication check(?).
// Update Sept 1st 2010: It looks like after a sequence of events the drive ends up having a specific state.
// If the drive is fed with a "bootable" disc it ends up in "PAUSE" state. On all other cases it ends up in "STANDBY".
// Cmd 70 and Error Handling / Sense also seem to take part in the above mentioned sequence of events.
// This is more or less a hack until more info about this command becomes available. ~Psy
case 0x71:
{
printf_spicmd("SPI : unknown ? [0x71]\n");
//printf("SPI : unknown ? [0x71]\n");
extern u32 reply_71_sz;
gd_spi_pio_end((u8*)&reply_71[0], reply_71_sz);//uCount
if (g_GDRDisc->GetDiscType() == GdRom || g_GDRDisc->GetDiscType() == CdRom_XA)
SecNumber.Status = GD_PAUSE;
else
SecNumber.Status = GD_STANDBY;
}
break;
case SPI_SET_MODE:
{
printf_spicmd("SPI_SET_MODE\n");
u32 Offset = packet_cmd.data_8[2];
u32 Count = packet_cmd.data_8[4];
verify((Offset + Count) < 11); //cant set write olny things :P
set_mode_offset = Offset;
gd_spi_pio_read_end(Count, gds_process_set_mode);
}
break;
case SPI_CD_READ2:
printf_spicmd("SPI_CD_READ2\n");
printf("GDROM: Unhandled Sega SPI frame: SPI_CD_READ2\n");
gd_set_state(gds_procpacketdone);
break;
case SPI_REQ_STAT:
{
printf_spicmd("SPI_REQ_STAT\n");
//printf("GDROM: Unhandled Sega SPI frame: SPI_REQ_STAT\n");
u8 stat[10];
//0 0 0 0 0 STATUS
stat[0] = SecNumber.Status; //low nibble
//1 Disc Format Repeat Count
stat[1] = (u8)(SecNumber.DiscFormat << 4) | (cdda.repeats);
//2 Address Control
stat[2] = 0x4;
//3 TNO
stat[3] = 2;
//4 X
stat[4] = 0;
//5 FAD
stat[5] = cdda.CurrAddr.B0;
//6 FAD
stat[6] = cdda.CurrAddr.B1;
//7 FAD
stat[7] = cdda.CurrAddr.B2;
//8 Max Read Error Retry Times
stat[8] = 0;
//9 0 0 0 0 0 0 0 0
stat[9] = 0;
verify((packet_cmd.data_8[2] + packet_cmd.data_8[4]) < 11);
gd_spi_pio_end(&stat[packet_cmd.data_8[2]], packet_cmd.data_8[4]);
}
break;
case SPI_REQ_ERROR:
printf_spicmd("SPI_REQ_ERROR\n");
//printf("GDROM: Unhandled Sega SPI frame: SPI_REQ_ERROR\n");
u8 resp[10];
resp[0] = 0xF0;
resp[1] = 0;
resp[2] = sns_key;//sense
resp[3] = 0;
resp[4] = resp[5] = resp[6] = resp[7] = 0; //Command Specific Information
resp[8] = sns_asc;//Additional Sense Code
resp[9] = sns_ascq;//Additional Sense Code Qualifier
gd_spi_pio_end(resp, packet_cmd.data_8[4]);
sns_key = 0;
sns_asc = 0;
sns_ascq = 0;
//GDStatus.CHECK=0;
break;
case SPI_REQ_SES:
printf_spicmd("SPI_REQ_SES\n");
u8 ses_inf[6];
g_GDRDisc->GetSessionInfo(ses_inf, packet_cmd.data_8[2]);
ses_inf[0] = SecNumber.Status;
gd_spi_pio_end((u8*)&ses_inf[0], packet_cmd.data_8[4]);
break;
case SPI_CD_OPEN:
printf_spicmd("SPI_CD_OPEN\n");
printf("GDROM: Unhandled Sega SPI frame: SPI_CD_OPEN\n");
gd_set_state(gds_procpacketdone);
break;
case SPI_CD_PLAY:
{
printf_spicmd("SPI_CD_PLAY\n");
printf("GDROM: Unhandled Sega SPI frame: SPI_CD_PLAY\n");
//cdda.CurrAddr.FAD=60000;
cdda.playing = true;
SecNumber.Status = GD_PLAY;
u32 param_type = packet_cmd.data_8[1] & 0x7;
printf("param_type=%d\n", param_type);
if (param_type == 1)
{
cdda.StartAddr.FAD = cdda.CurrAddr.FAD = GetFAD(&packet_cmd.data_8[2], 0);
cdda.EndAddr.FAD = GetFAD(&packet_cmd.data_8[8], 0);
GDStatus.DSC = 1; //we did the seek xD lol
}
else if (param_type == 2)
{
cdda.StartAddr.FAD = cdda.CurrAddr.FAD = GetFAD(&packet_cmd.data_8[2], 1);
cdda.EndAddr.FAD = GetFAD(&packet_cmd.data_8[8], 1);
GDStatus.DSC = 1; //we did the seek xD lol
}
else if (param_type == 7)
{
//Resume from previous pos :)
}
else
{
die("SPI_CD_SEEK : not known parameter..");
}
cdda.repeats = packet_cmd.data_8[6] & 0xF;
printf("cdda.StartAddr=%d\n", cdda.StartAddr.FAD);
printf("cdda.EndAddr=%d\n", cdda.EndAddr.FAD);
printf("cdda.repeats=%d\n", cdda.repeats);
printf("cdda.playing=%d\n", cdda.playing);
printf("cdda.CurrAddr=%d\n", cdda.CurrAddr.FAD);
gd_set_state(gds_procpacketdone);
}
break;
case SPI_CD_SEEK:
{
printf_spicmd("SPI_CD_SEEK\n");
printf("GDROM: Unhandled Sega SPI frame: SPI_CD_SEEK\n");
SecNumber.Status = GD_PAUSE;
cdda.playing = false;
u32 param_type = packet_cmd.data_8[1] & 0x7;
printf("param_type=%d\n", param_type);
if (param_type == 1)
{
cdda.StartAddr.FAD = cdda.CurrAddr.FAD = GetFAD(&packet_cmd.data_8[2], 0);
GDStatus.DSC = 1; //we did the seek xD lol
}
else if (param_type == 2)
{
cdda.StartAddr.FAD = cdda.CurrAddr.FAD = GetFAD(&packet_cmd.data_8[2], 1);
GDStatus.DSC = 1; //we did the seek xD lol
}
else if (param_type == 3)
{
//stop audio , goto home
SecNumber.Status = GD_STANDBY;
cdda.StartAddr.FAD = cdda.CurrAddr.FAD = 150;
GDStatus.DSC = 1; //we did the seek xD lol
}
else if (param_type == 4)
{
//pause audio -- nothing more
}
else
{
die("SPI_CD_SEEK : not known parameter..");
}
printf("cdda.StartAddr=%d\n", cdda.StartAddr.FAD);
printf("cdda.EndAddr=%d\n", cdda.EndAddr.FAD);
printf("cdda.repeats=%d\n", cdda.repeats);
printf("cdda.playing=%d\n", cdda.playing);
printf("cdda.CurrAddr=%d\n", cdda.CurrAddr.FAD);
gd_set_state(gds_procpacketdone);
}
break;
case SPI_CD_SCAN:
printf_spicmd("SPI_CD_SCAN\n");
printf("GDROM: Unhandled Sega SPI frame: SPI_CD_SCAN\n");
gd_set_state(gds_procpacketdone);
break;
case SPI_GET_SCD:
{
printf_spicmd("SPI_GET_SCD\n");
//printf("\nGDROM:\tUnhandled Sega SPI frame: SPI_GET_SCD\n");
u32 format;
format = packet_cmd.data_8[1] & 0xF;
u32 sz;
u8 subc_info[100];
//0 Reserved
subc_info[0] = 0;
//1 Audio status
if (SecNumber.Status == GD_STANDBY)
{
//13h Audio playback ended normally
subc_info[1] = 0x13;
}
else if (SecNumber.Status == GD_PAUSE)
{
//12h Audio playback paused
subc_info[1] = 0x12;
}
else if (SecNumber.Status == GD_PLAY)
{
//11h Audio playback in progress
subc_info[1] = 0x11;
}
else
{
if (cdda.playing)
subc_info[1] = 0x11;//11h Audio playback in progress
else
subc_info[1] = 0x15;//15h No audio status information
}
subc_info[1] = 0x15;
if (format == 0)
{
sz = 100;
subc_info[2] = 0;
subc_info[3] = 100;
g_GDRDisc->ReadSubChannel(subc_info + 4, 0, 96);
}
else
{
//2 DATA Length MSB (0 = 0h)
subc_info[2] = 0;
//3 DATA Length LSB (14 = Eh)
subc_info[3] = 0xE;
//4 Control ADR
subc_info[4] = (4 << 4) | (1); //Audio :p
//5-13 DATA-Q
u8* data_q = &subc_info[5 - 1];
//-When ADR = 1
//Byte Description
//1 TNO
data_q[1] = 1;//Track number .. dunno whats it :P gotta parse toc xD ;p
//2 X
data_q[2] = 1;//gap #1 (main track)
//3-5 Elapsed FAD within track
//u32 FAD_el = cdda.CurrAddr.FAD - cdda.StartAddr.FAD;
data_q[3] = 0;//(u8)(FAD_el>>16);
data_q[4] = 0;//(u8)(FAD_el>>8);
data_q[5] = 0;//(u8)(FAD_el>>0);
//6 0 0 0 0 0 0 0 0
data_q[6] = 0;//
//7-9 -> seems to be FAD
data_q[7] = 0; //(u8)(cdda.CurrAddr.FAD>>16);
data_q[8] = 0x0; //(u8)(cdda.CurrAddr.FAD>>8);
data_q[9] = 0x96;//(u8)(cdda.CurrAddr.FAD>>0);
sz = 0xE;
printf_subcode("NON raw subcode read -- partially wrong [format=%d]\n", format);
}
gd_spi_pio_end((u8*)&subc_info[0], sz);
}
break;
default:
printf("GDROM: Unhandled Sega SPI frame: %X\n", packet_cmd.data_8[0]);
gd_set_state(gds_procpacketdone);
break;
}
}
//Read handler
u32 Read(u32 Addr, u32 sz)
{
switch (Addr)
{
//cancel interrupt
case GD_STATUS_Read:
asic->CancelInterrupt(holly_GDROM_CMD); //Clear INTRQ signal
printf_rm("GDROM: STATUS [cancel int](v=%X)\n", GDStatus.full);
return GDStatus.full | (1 << 4);
case GD_ALTSTAT_Read:
printf_rm("GDROM: Read From AltStatus (v=%X)\n", GDStatus.full);
return GDStatus.full | (1 << 4);
case GD_BYCTLLO:
printf_rm("GDROM: Read From GD_BYCTLLO\n");
return ByteCount.low;
case GD_BYCTLHI:
printf_rm("GDROM: Read From GD_BYCTLHI\n");
return ByteCount.hi;
case GD_DATA:
if (2 != sz)
printf("GDROM: Bad size on DATA REG Read\n");
//if (gd_state == gds_pio_send_data)
//{
if (pio_buff.index == pio_buff.size)
{
printf("GDROM: Illegal Read From DATA (underflow)\n");
}
else
{
u32 rv = pio_buff.data[pio_buff.index];
pio_buff.index += 1;
ByteCount.full -= 2;
if (pio_buff.index == pio_buff.size)
{
verify(pio_buff.next_state != gds_pio_send_data);
//end of pio transfer !
gd_set_state(pio_buff.next_state);
}
return rv;
}
//}
//else
// printf("GDROM: Illegal Read From DATA (wrong mode)\n");
return 0;
case GD_DRVSEL:
printf_rm("GDROM: Read From DriveSel\n");
return DriveSel;
case GD_ERROR_Read:
printf_rm("GDROM: Read from ERROR Register\n");
Error.Sense = sns_key;
return Error.full;
case GD_IREASON_Read:
printf_rm("GDROM: Read from INTREASON Register\n");
return IntReason.full;
case GD_SECTNUM:
printf_rm("GDROM: Read from SecNumber Register (v=%X)\n", SecNumber.full);
return SecNumber.full;
default:
printf("GDROM: Unhandled read from address %X, Size:%X\n", Addr, sz);
return 0;
}
}
//Write Handler
void Write(u32 Addr, u32 data, u32 sz)
{
switch (Addr)
{
//ATA_IOPORT_WR_CYLINDER_LOW
case GD_BYCTLLO:
printf_rm("GDROM: Write to GD_BYCTLLO = %X, Size:%X\n", data, sz);
ByteCount.low = (u8)data;
break;
//ATA_IOPORT_WR_CYLINDER_HIGH
3CD8
case GD_BYCTLHI:
printf_rm("GDROM: Write to GD_BYCTLHI = %X, Size:%X\n", data, sz);
ByteCount.hi = (u8)data;
break;
case GD_DATA:
{
if (2 != sz)
printf("GDROM: Bad size on DATA REG\n");
if (gd_state == gds_waitpacket)
{
packet_cmd.data_16[packet_cmd.index] = (u16)data;
packet_cmd.index += 1;
if (packet_cmd.index == 6)