-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb.c
1092 lines (895 loc) · 31.4 KB
/
usb.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
/***************************************************************
usb.c
Allows USB communication between an N64 flashcart and the PC
using UNFLoader.
https://github.com/buu342/N64-UNFLoader
***************************************************************/
#include <ultra64.h>
#include <string.h>
#include "usb.h"
/*********************************
Standard Definitions
*********************************/
// Input/Output buffer size. Keep at 512
#define BUFFER_SIZE 512
// Cart definitions
#define CART_NONE 0
#define CART_64DRIVE 2
#define CART_EVERDRIVE3 3
#define CART_EVERDRIVE7 4
/*********************************
Parallel Interface macros
*********************************/
#define N64_PI_ADDRESS 0xA4600000
#define N64_PI_RAMADDRESS 0x00
#define N64_PI_PIADDRESS 0x04
#define N64_PI_READLENGTH 0x08
#define N64_PI_WRITELENGTH 0x0C
#define N64_PI_STATUS 0x10
/*********************************
64Drive macros
*********************************/
// Cartridge Interface definitions. Obtained from 64Drive's Spec Sheet
#define D64_BASE_ADDRESS 0xB0000000
#define D64_CIREG_ADDRESS 0x08000000
#define D64_CIBASE_ADDRESS 0xB8000000
#define D64_DEBUG_ADDRESS 0x03F00000 // Put the debug area at the 63MB area in SDRAM
#define D64_DEBUG_ADDRESS_SIZE 1*1024*1024
#define D64_REGISTER_STATUS 0x00000200
#define D64_REGISTER_COMMAND 0x00000208
#define D64_REGISTER_LBA 0x00000210
#define D64_REGISTER_LENGTH 0x00000218
#define D64_REGISTER_RESULT 0x00000220
#define D64_REGISTER_MAGIC 0x000002EC
#define D64_REGISTER_VARIANT 0x000002F0
#define D64_REGISTER_BUTTON 0x000002F8
#define D64_REGISTER_REVISION 0x000002FC
#define D64_REGISTER_USBCOMSTAT 0x00000400
#define D64_REGISTER_USBP0R0 0x00000404
#define D64_REGISTER_USBP1R1 0x00000408
#define D64_ENABLE_ROMWR 0xF0
#define D64_DISABLE_ROMWR 0xF1
#define D64_COMMAND_WRITE 0x08
// Cartridge Interface return values
#define D64_MAGIC 0x55444556
#define D64_USB_IDLE 0x00
#define D64_USB_DATA 0x02
#define D64_USB_BUSY 0x0F
#define D64_USB_DISARM 0x0F
#define D64_USB_ARM 0x0A
#define D64_USB_ARMED 0x01
#define D64_USB_ARMING 0x0F
#define D64_USB_IDLEUNARMED 0x00
#define D64_CI_IDLE 0x00
#define D64_CI_BUSY 0x10
#define D64_CI_WRITE 0x20
/*********************************
EverDrive 3.0 macros
*********************************/
#define ED3_BASE_ADDRESS 0xA8040000
#define ED3_WRITE_ADDRESS 0xB3FFF800
#define ED3_ROM_ADDRESS 0xB0000000
#define ED3_ROM_BUFFER 0x7FFF
#define ED3_REGISTER_CFG 0x00
#define ED3_REGISTER_STATUS 0x04
#define ED3_REGISTER_DMALEN 0x08
#define ED3_REGISTER_DMAADD 0x0C
#define ED3_REGISTER_MSG 0x10
#define ED3_REGISTER_DMACFG 0x14
#define ED3_REGISTER_KEY 0x20
#define ED3_REGISTER_VERSION 0x2C
#define ED3_DMA_BUSY 1
#define ED3_DMA_WRITE 4
#define ED3_DMACFG_USB2RAM 3
#define ED3_DMACFG_RAM2USB 4
#define ED3_VERSION 0xA3F0A3F0
#define ED3_REGKEY 0x1234
/*********************************
EverDrive X7 macros
*********************************/
#define ED7_BASE_ADDRESS 0x1F800000
#define ED7_GET_REGADD(reg) (0xA0000000 | ED7_BASE_ADDRESS | (reg))
#define ED7_REGISTER_VERSION 0x14
#define ED7_REG_USBCFG 0x0004
#define ED7_REG_USBDAT 0x0400
#define ED7_REG_SYSCFG 0x8000
#define ED7_REG_KEY 0x8004
#define ED7_USBMODE_RDNOP 0xC400
#define ED7_USBMODE_RD 0xC600
#define ED7_USBMODE_WRNOP 0xC000
#define ED7_USBMODE_WR 0xC200
#define ED7_USBSTAT_ACT 0x0200
#define ED7_USBSTAT_BUSY 0x2000
#define ED7_REGKEY 0xAA55
#define ED7_VERSION 0x00000000
/*********************************
Function Prototypes
*********************************/
static void usb_findcart();
static void usb_64drive_write(int datatype, const void* data, int size);
static int usb_64drive_poll();
static void usb_64drive_read(void* buffer, int size);
static void usb_everdrive3_write(int datatype, const void* data, int size);
static int usb_everdrive3_poll();
static void usb_everdrive3_read(void* buffer, int size);
static void usb_everdrive7_write(int datatype, const void* data, int size);
static int usb_everdrive7_poll();
static void usb_everdrive7_read(void* buffer, int size);
static void usb_everdrive7_writereg(u64 reg, u32 value);
/*********************************
Globals
*********************************/
// Function pointers
void (*funcPointer_write)(int datatype, const void* data, int size);
int (*funcPointer_poll)();
void (*funcPointer_read)(void* buffer, int size);
// USB globals
static s8 usb_cart = CART_NONE;
static u8 usb_bufferout[BUFFER_SIZE] __attribute__((aligned(16)));
// Message globals
#if !USE_OSRAW
OSMesg dmaMessageBuf;
OSIoMesg dmaIOMessageBuf;
OSMesgQueue dmaMessageQ;
#endif
// osPiRaw
#if USE_OSRAW
extern s32 __osPiRawWriteIo(u32, u32);
extern s32 __osPiRawReadIo(u32, u32 *);
extern s32 __osPiRawStartDma(s32, u32, void *, u32);
#define osPiRawWriteIo(a, b) __osPiRawWriteIo(a, b)
#define osPiRawReadIo(a, b) __osPiRawReadIo(a, b)
#define osPiRawStartDma(a, b, c, d) __osPiRawStartDma(a, b, c, d)
#endif
/*********************************
USB functions
*********************************/
/*==============================
usb_initialize
Initializes the USB buffers and pointers
==============================*/
void usb_initialize()
{
// Initialize the debug related globals
memset(usb_bufferout, 0, BUFFER_SIZE);
// Create the message queue
#if !USE_OSRAW
osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);
#endif
// Find the flashcart
usb_findcart();
// Set the function pointers based on the flashcart
switch(usb_cart)
{
case CART_64DRIVE:
funcPointer_write = usb_64drive_write;
funcPointer_poll = usb_64drive_poll;
funcPointer_read = usb_64drive_read;
break;
case CART_EVERDRIVE3:
funcPointer_write = usb_everdrive3_write;
funcPointer_poll = usb_everdrive3_poll;
funcPointer_read = usb_everdrive3_read;
break;
case CART_EVERDRIVE7:
funcPointer_write = usb_everdrive7_write;
funcPointer_poll = usb_everdrive7_poll;
funcPointer_read = usb_everdrive7_read;
break;
default:
return;
}
}
/*==============================
usb_findcart
Checks if the game is running on a 64Drive or an EverDrive.
==============================*/
static void usb_findcart()
{
u32 buff;
// Read the cartridge and check if we have a 64Drive.
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_MAGIC, &buff);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_MAGIC, &buff);
#endif
if (buff == D64_MAGIC)
{
usb_cart = CART_64DRIVE;
return;
}
// Read the cartridge and check if we have an EverDrive 3.0
#if USE_OSRAW
osPiRawReadIo(ED3_BASE_ADDRESS + ED3_REGISTER_VERSION, &buff);
#else
osPiReadIo(ED3_BASE_ADDRESS + ED3_REGISTER_VERSION, &buff);
#endif
if (buff == ED3_VERSION)
{
// Write the key to unlock the registers and set the debug cart to ED3
IO_WRITE(ED3_BASE_ADDRESS + ED3_REGISTER_KEY, ED3_REGKEY);
usb_cart = CART_EVERDRIVE3;
return;
}
// Read the cartridge and check if we have an EverDrive X7
#if USE_OSRAW
osPiRawReadIo(ED7_BASE_ADDRESS + ED7_REGISTER_VERSION, &buff);
#else
osPiReadIo(ED7_BASE_ADDRESS + ED7_REGISTER_VERSION, &buff);
#endif
if (buff == ED7_VERSION)
{
// Initialize the PI
IO_WRITE(PI_BSD_DOM1_LAT_REG, 0x04);
IO_WRITE(PI_BSD_DOM1_PWD_REG, 0x0C);
// Write the key to unlock the registers and set the USB mode
usb_everdrive7_writereg(ED7_REG_KEY, ED7_REGKEY);
usb_everdrive7_writereg(ED7_REG_SYSCFG, 0);
usb_everdrive7_writereg(ED7_REG_USBCFG, ED7_USBMODE_RDNOP);
// Set the cart to EverDrive X7
usb_cart = CART_EVERDRIVE7;
return;
}
}
/*==============================
usb_write
Writes data to the USB.
@param The DATATYPE that is being sent
@param A buffer with the data to send
@param The size of the data being sent
==============================*/
void usb_write(int datatype, const void* data, int size)
{
// If no debug cart exists, stop
if (usb_cart == CART_NONE)
return;
// Call the correct write function
funcPointer_write(datatype, data, size);
}
/*==============================
usb_poll
Returns the header of data being received via USB
The first byte contains the data type, the next 3 the size
@return The header of incoming data, or 0 if no data
==============================*/
int usb_poll()
{
// If no debug cart exists, stop
if (usb_cart == CART_NONE)
return 0;
// Call the correct read function
return funcPointer_poll();
}
/*==============================
usb_read
Reads bytes from the USB into the provided buffer
@param The buffer to put the read data in
@param The number of bytes to read
==============================*/
void usb_read(void* buffer, int size)
{
// If no debug cart exists, stop
if (usb_cart == CART_NONE)
return;
// Call the correct read function
funcPointer_read(buffer, size);
}
/*********************************
64Drive functions
*********************************/
/*==============================
usb_64drive_wait
Wait until the 64Drive is ready
@return 0 if success or -1 if failure
==============================*/
static s8 usb_64drive_wait()
{
u32 ret;
u32 timeout = 0; // I wanted to use osGetTime() but that requires the VI manager
// Wait until the cartridge interface is ready
do
{
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_STATUS, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_STATUS, &ret);
#endif
// Took too long, abort
if((timeout++) > 1000000)
return -1;
}
while((ret >> 8) & D64_CI_BUSY);
// Success
return 0;
}
/*==============================
usb_64drive_setwritable
Set the write mode on the 64Drive
@param A boolean with whether to enable or disable
==============================*/
static void usb_64drive_setwritable(u8 enable)
{
usb_64drive_wait();
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_COMMAND, enable ? D64_ENABLE_ROMWR : D64_DISABLE_ROMWR);
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_COMMAND, enable ? D64_ENABLE_ROMWR : D64_DISABLE_ROMWR);
#endif
usb_64drive_wait();
}
/*==============================
usb_64drive_waitidle
Waits for the 64Drive's USB to be idle
==============================*/
static void usb_64drive_waitidle()
{
u32 status;
do
{
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#endif
status = (status >> 4) & D64_USB_BUSY;
}
while(status != D64_USB_IDLE);
}
/*==============================
usb_64drive_waitdata
Waits for the 64Drive's USB be ablt to receive data
==============================*/
static void usb_64drive_waitdata()
{
u32 status;
do
{
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#endif
status &= 0x0F;
}
while (status == D64_USB_IDLEUNARMED || status == D64_USB_ARMED);
}
/*==============================
usb_64drive_waitdisarmed
Waits for the 64Drive's USB to be disarmed
==============================*/
static void usb_64drive_waitdisarmed()
{
u32 status;
do
{
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#endif
status &= 0x0F;
}
while (status != D64_USB_IDLEUNARMED);
}
/*==============================
usb_64drive_write
Sends data through USB from the 64Drive
@param The DATATYPE that is being sent
@param A buffer with the data to send
@param The size of the data being sent
==============================*/
static void usb_64drive_write(int datatype, const void* data, int size)
{
int left = size;
int read = 0;
// Spin until the write buffer is free and then set the cartridge to write mode
usb_64drive_waitidle();
usb_64drive_setwritable(TRUE);
// Write data to SDRAM until we've finished
while (left > 0)
{
int block = left;
if (block > BUFFER_SIZE)
block = BUFFER_SIZE;
// Copy the data to the global buffer
memcpy(usb_bufferout, (void*)((char*)data+read), block);
// If the data was not 32-bit aligned, pad the buffer
if(block < BUFFER_SIZE && size%4 != 0)
{
u32 i;
u32 size_new = (size & ~3)+4;
block += size_new-size;
for(i=size; i<size_new; i++)
usb_bufferout[i] = 0;
size = size_new;
}
// Spin until the write buffer is free
usb_64drive_waitidle();
// Set up DMA transfer between RDRAM and the PI
osWritebackDCache(usb_bufferout, block);
#if USE_OSRAW
osPiRawStartDma(OS_WRITE,
D64_BASE_ADDRESS + D64_DEBUG_ADDRESS + read,
usb_bufferout, block);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_WRITE,
D64_BASE_ADDRESS + D64_DEBUG_ADDRESS + read,
usb_bufferout, block, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
// Keep track of what we've read so far
left -= block;
read += block;
}
// Send the data through USB
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, D64_DEBUG_ADDRESS >> 1);
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, (size & 0xFFFFFF) | (datatype << 24));
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_COMMAND_WRITE);
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, D64_DEBUG_ADDRESS >> 1);
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, (size & 0xFFFFFF) | (datatype << 24));
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_COMMAND_WRITE);
#endif
// Spin until the write buffer is free and then disable write mode
usb_64drive_waitidle();
usb_64drive_setwritable(FALSE);
}
/*==============================
usb_64drive_poll
Checks the USB for data on the 64Drive
@return The number of bytes to read
==============================*/
static int usb_64drive_poll()
{
u32 ret;
// Check if we've received data
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#endif
// No data, stop here
if (ret != D64_USB_DATA)
return 0;
// Check if the USB is armed, and arm it if it isn't
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#endif
if (ret != D64_USB_ARMING || ret != D64_USB_ARMED)
{
// Ensure the 64Drive is idle
usb_64drive_waitidle();
// Arm the USB FIFO DMA
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, D64_DEBUG_ADDRESS >> 1);
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, BUFFER_SIZE & 0xFFFFFF);
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, D64_DEBUG_ADDRESS >> 1);
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, BUFFER_SIZE & 0xFFFFFF);
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
#endif
}
// Wait for the data to arrive
usb_64drive_waitdata();
// Read the data header
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, &ret);
#endif
// Disarm the USB and return the data header
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_DISARM);
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_DISARM);
#endif
usb_64drive_waitdisarmed();
return ret;
}
/*==============================
usb_64drive_read
Reads data from the 64Drive
==============================*/
static void usb_64drive_read(void* buffer, int nbytes)
{
u32 ret;
// Check if we've received data
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#endif
if (ret != D64_USB_DATA)
return;
// Check if the USB is armed, and arm it if it isn't
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &ret);
#endif
if (ret != D64_USB_ARMING || ret != D64_USB_ARMED)
{
// Ensure the 64Drive is idle
usb_64drive_waitidle();
// Arm the USB FIFO DMA
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, D64_DEBUG_ADDRESS >> 1);
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, BUFFER_SIZE & 0xFFFFFF);
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, D64_DEBUG_ADDRESS >> 1);
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, BUFFER_SIZE & 0xFFFFFF);
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
#endif
}
// Wait for data
usb_64drive_waitdata();
// Set up DMA transfer between RDRAM and the PI
osWritebackDCache(buffer, nbytes);
#if USE_OSRAW
osPiRawStartDma(OS_READ,
D64_BASE_ADDRESS + D64_DEBUG_ADDRESS, buffer,
nbytes);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_READ,
D64_BASE_ADDRESS + D64_DEBUG_ADDRESS, buffer,
nbytes, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
// Disarm the USB
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_DISARM);
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_DISARM);
#endif
usb_64drive_waitdisarmed();
}
/*********************************
EverDrive 3.0 functions
*********************************/
/*==============================
usb_everdrive3_regread
Writes data to the specified register on the EverDrive 3.0
@param The register to write to
@param The data to write
==============================*/
static void usb_everdrive3_regwrite(unsigned long reg, unsigned long data)
{
*(volatile unsigned long *) (ED3_BASE_ADDRESS);
*(volatile unsigned long *) (ED3_BASE_ADDRESS + reg) = data;
}
/*==============================
usb_everdrive3_regread
Reads data from the specified register on the EverDrive 3.0
@param The register to read from
==============================*/
static unsigned long usb_everdrive3_regread(unsigned long reg)
{
*(volatile unsigned long *) (ED3_BASE_ADDRESS);
return *(volatile unsigned long *) (ED3_BASE_ADDRESS + reg);
}
/*==============================
usb_everdrive3_wait_pidma
Spins until the EverDrive 3.0's PI is ready
==============================*/
static void usb_everdrive3_wait_pidma()
{
u32 status;
do
{
status = *(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_STATUS);
status &= (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY);
}
while (status);
}
/*==============================
usb_everdrive3_wait_write
Spins until the EverDrive 3.0's can write
==============================*/
static void usb_everdrive3_wait_write()
{
u32 status;
do
{
status = usb_everdrive3_regread(ED3_REGISTER_STATUS);
status &= ED3_DMA_WRITE;
}
while (status != 0);
}
/*==============================
usb_everdrive3_wait_dma
Spins until the EverDrive 3.0's DMA is ready
==============================*/
static void usb_everdrive3_wait_dma()
{
u32 status;
do
{
status = usb_everdrive3_regread(ED3_REGISTER_STATUS);
status &= ED3_DMA_BUSY;
}
while (status != 0);
}
/*==============================
usb_everdrive3_write
Sends data through USB from the EverDrive 3.0
@param The DATATYPE that is being sent
@param A buffer with the data to send
@param The size of the data being sent
==============================*/
static void usb_everdrive3_write(int datatype, const void* data, int size)
{
char wrotecmp = 0;
char cmp[] = {'C', 'M', 'P', 'H'};
int read = 0;
int left = size;
int offset = 8;
u32 header = (size & 0xFFFFFF) | (datatype << 24);
// Put in the DMA header along with size and type information in the global buffer
usb_bufferout[0] = 'D';
usb_bufferout[1] = 'M';
usb_bufferout[2] = 'A';
usb_bufferout[3] = '@';
usb_bufferout[4] = (header >> 24) & 0xFF;
usb_bufferout[5] = (header >> 16) & 0xFF;
usb_bufferout[6] = (header >> 8) & 0xFF;
usb_bufferout[7] = header & 0xFF;
// Write data to USB until we've finished
while (left > 0)
{
int block = left;
if (block+offset > BUFFER_SIZE)
block = BUFFER_SIZE-offset;
// Copy the data to the next available spots in the global buffer
memcpy(usb_bufferout+offset, (void*)((char*)data+read), block);
// Restart the loop to write the CMP signal if we've finished
if (!wrotecmp && read+block >= size)
{
left = 4;
offset = block+offset;
data = cmp;
wrotecmp = 1;
read = 0;
continue;
}
// Set up DMA transfer between RDRAM and the PI
osWritebackDCache(usb_bufferout, BUFFER_SIZE);
#if USE_OSRAW
osPiRawStartDma(OS_WRITE,
ED3_WRITE_ADDRESS, usb_bufferout,
BUFFER_SIZE);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_WRITE,
ED3_WRITE_ADDRESS, usb_bufferout,
BUFFER_SIZE, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
// Write the data to the PI
usb_everdrive3_wait_pidma();
IO_WRITE(PI_STATUS_REG, 3);
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_RAMADDRESS) = (u32)usb_bufferout;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_PIADDRESS) = ED3_WRITE_ADDRESS & 0x1FFFFFFF;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_READLENGTH) = BUFFER_SIZE-1;
usb_everdrive3_wait_pidma();
// Write the data to the EverDrive 3.0's registers
usb_everdrive3_wait_write();
usb_everdrive3_regwrite(ED3_REGISTER_DMALEN, 0);
usb_everdrive3_regwrite(ED3_REGISTER_DMAADD, ED3_ROM_BUFFER);
usb_everdrive3_regwrite(ED3_REGISTER_DMACFG, ED3_DMACFG_RAM2USB);
usb_everdrive3_wait_dma();
// Keep track of what we've read so far
left -= block;
read += block;
offset = 0;
}
}
/*==============================
usb_everdrive3_poll
Checks the USB for data on the EverDrive 3.0
@return The number of bytes to read
==============================*/
static int usb_everdrive3_poll()
{
// TODO: Implement this function
return 0;
}
/*==============================
usb_everdrive3_read
TODO: Write this header
==============================*/
static void usb_everdrive3_read(void* buffer, int nbytes)
{
// TODO: Implement this function
}
/*********************************
EverDrive X7 functions
*********************************/
/*==============================
usb_everdrive7_wait_pidma
Spins until the EverDrive X7's DMA is ready
==============================*/
static void usb_everdrive7_wait_pidma()
{
u32 status;
do
{
status = *(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_STATUS);
status &= (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY);
}
while (status);
}
/*==============================
usb_everdrive7_readdata
Reads data from a specific address on the EverDrive X7
@param The buffer with the data
@param The register address to write to the PI
@param The size of the data
==============================*/
static void usb_everdrive7_readdata(void* buff, u32 pi_address, u32 len)
{
// Correct the PI address
pi_address = ED7_GET_REGADD(pi_address);
// Set up DMA transfer between RDRAM and the PI
osWritebackDCache(buff, len);
#if USE_OSRAW
osPiRawStartDma(OS_READ,
pi_address, buff,
len);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_READ,
pi_address, buff,
len, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
// Write the data to the PI
usb_everdrive7_wait_pidma();
IO_WRITE(PI_STATUS_REG, 3);
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_RAMADDRESS) = (u32)buff;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_PIADDRESS) = pi_address & 0x1FFFFFFF;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_READLENGTH) = len-1;
usb_everdrive7_wait_pidma();
}
/*==============================
usb_everdrive7_readreg
Reads data from a specific register on the EverDrive X7
@param The register to read from
@param A pointer to write the read value to
==============================*/
static void usb_everdrive7_readreg(u32 reg, u32* result)
{
usb_everdrive7_readdata(result, reg, sizeof(u32));
}
/*==============================
usb_everdrive7_writedata
Writes data to a specific address on the EverDrive X7
@param A buffer with the data to write
@param The register address to write to the PI
@param The length of the data
==============================*/
static void usb_everdrive7_writedata(void* buff, u32 pi_address, u32 len)
{
// Correct the PI address
pi_address = ED7_GET_REGADD(pi_address);
// Set up DMA transfer between RDRAM and the PI
osWritebackDCache(buff, len);
#if USE_OSRAW
osPiRawStartDma(OS_WRITE,
pi_address, buff,
len);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_WRITE,
pi_address, buff,
len, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
// Write the data to the PI
usb_everdrive7_wait_pidma();
IO_WRITE(PI_STATUS_REG, 3);
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_RAMADDRESS) = (u32)buff;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_PIADDRESS) = pi_address & 0x1FFFFFFF;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_WRITELENGTH) = len-1;
usb_everdrive7_wait_pidma();
}
/*==============================
usb_everdrive7_writereg
Writes data to a specific register on the EverDrive X7
@param The register to write to
@param The value to write to the register
==============================*/
static void usb_everdrive7_writereg(u64 reg, u32 value)
{
usb_everdrive7_writedata(&value, reg, sizeof(u32));
}
/*==============================
usb_everdrive7_usbbusy
Spins until the USB is no longer busy
==============================*/
static void usb_everdrive7_usbbusy()
{
u32 val;
do
{
usb_everdrive7_readreg(ED7_REG_USBCFG, &val);
}
while ((val & ED7_USBSTAT_ACT) != 0);
}