-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusb.c
1693 lines (1403 loc) · 51.8 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 "usb.h"
#ifndef LIBDRAGON
#include <ultra64.h>
#else
#include <libdragon.h>
#endif
#include <string.h>
/*********************************
Data macros
*********************************/
// Input/Output buffer size. Always keep it at 512
#define BUFFER_SIZE 512
// USB Memory location
#define DEBUG_ADDRESS 0x04000000-DEBUG_ADDRESS_SIZE // Put the debug area at the 63MB area in ROM space
// Data header related
#define USBHEADER_CREATE(type, left) (((type<<24) | (left & 0x00FFFFFF)))
/*********************************
Libultra macros for libdragon
*********************************/
#ifdef LIBDRAGON
// Useful
#define ALIGN(s, align) (((u32)(s) + ((align)-1)) & ~((align)-1))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif
// MIPS addresses
#define KSEG0 0x80000000
#define KSEG1 0xA0000000
// Memory translation stuff
#define PHYS_TO_K1(x) ((u32)(x)|KSEG1)
#define IO_WRITE(addr,data) (*(vu32 *)PHYS_TO_K1(addr)=(u32)(data))
#define IO_READ(addr) (*(vu32 *)PHYS_TO_K1(addr))
// PI registers
#define PI_BASE_REG 0x04600000
#define PI_STATUS_REG (PI_BASE_REG+0x10)
#define PI_STATUS_ERROR 0x04
#define PI_STATUS_IO_BUSY 0x02
#define PI_STATUS_DMA_BUSY 0x01
#define PI_BSD_DOM1_LAT_REG (PI_BASE_REG+0x14)
#define PI_BSD_DOM1_PWD_REG (PI_BASE_REG+0x18)
#define PI_BSD_DOM1_PGS_REG (PI_BASE_REG+0x1C)
#define PI_BSD_DOM1_RLS_REG (PI_BASE_REG+0x20)
#define PI_BSD_DOM2_LAT_REG (PI_BASE_REG+0x24)
#define PI_BSD_DOM2_PWD_REG (PI_BASE_REG+0x28)
#define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C)
#define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30)
#endif
/*********************************
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
*********************************/
// How many cycles for the 64Drive to wait for data.
// Lowering this might improve performance slightly faster at the expense of USB reading accuracy
#define D64_POLLTIME 2000
// 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_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_IDLEUNARMED 0x00
#define D64_USB_ARMED 0x01
#define D64_USB_DATA 0x02
#define D64_USB_ARM 0x0A
#define D64_USB_BUSY 0x0F
#define D64_USB_DISARM 0x0F
#define D64_USB_ARMING 0x0F
#define D64_CI_IDLE 0x00
#define D64_CI_BUSY 0x10
#define D64_CI_WRITE 0x20
/*********************************
EverDrive macros
*********************************/
#define ED_BASE 0x10000000
#define ED_BASE_ADDRESS 0x1F800000
#define ED_GET_REGADD(reg) (0xA0000000 | ED_BASE_ADDRESS | (reg))
#define ED_REG_USBCFG 0x0004
#define ED_REG_VERSION 0x0014
#define ED_REG_USBDAT 0x0400
#define ED_REG_SYSCFG 0x8000
#define ED_REG_KEY 0x8004
#define ED_USBMODE_RDNOP 0xC400
#define ED_USBMODE_RD 0xC600
#define ED_USBMODE_WRNOP 0xC000
#define ED_USBMODE_WR 0xC200
#define ED_USBSTAT_ACT 0x0200
#define ED_USBSTAT_RXF 0x0400
#define ED_USBSTAT_TXE 0x0800
#define ED_USBSTAT_POWER 0x1000
#define ED_USBSTAT_BUSY 0x2000
#define ED_REGKEY 0xAA55
#define ED3_VERSION 0xED640008
#define ED7_VERSION 0xED640013
/*********************************
SummerCart64 macros
*********************************/
#define SC64_SDRAM_BASE 0x10000000
#define SC64_BANK_ROM 1
#define SC64_REGS_BASE 0x1E000000
#define SC64_REG_SCR (SC64_REGS_BASE + 0x00)
#define SC64_REG_VERSION (SC64_REGS_BASE + 0x08)
#define SC64_REG_USB_SCR (SC64_REGS_BASE + 0x10)
#define SC64_REG_USB_DMA_ADDR (SC64_REGS_BASE + 0x14)
#define SC64_REG_USB_DMA_LEN (SC64_REGS_BASE + 0x18)
#define SC64_MEM_BASE (SC64_REGS_BASE + 0x1000)
#define SC64_MEM_USB_FIFO_BASE (SC64_MEM_BASE + 0x0000)
#define SC64_MEM_USB_FIFO_LEN (4 * 1024)
#define SC64_SCR_SDRAM_WRITE_EN (1 << 0)
#define SC64_VERSION_A 0x53363461
#define SC64_USB_STATUS_BUSY (1 << 0)
#define SC64_USB_STATUS_READY (1 << 1)
#define SC64_USB_CONTROL_START (1 << 0)
#define SC64_USB_CONTROL_FIFO_FLUSH (1 << 2)
#define SC64_USB_BANK_ADDR(b, a) ((((b) & 0xF) << 28) | ((a) & 0x3FFFFFF))
#define SC64_USB_LENGTH(l) (ALIGN((l), 4) / 4)
#define SC64_USB_DMA_MAX_LEN (2 * 1024 * 1024)
#define SC64_USB_FIFO_ITEMS(s) (((s) >> 3) & 0x7FF)
/*********************************
Libultra types (for libdragon)
*********************************/
#ifdef LIBDRAGON
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t s8;
typedef int16_t s16;
typedef int32_t s32;
typedef int64_t s64;
typedef volatile uint8_t vu8;
typedef volatile uint16_t vu16;
typedef volatile uint32_t vu32;
typedef volatile uint64_t vu64;
typedef volatile int8_t vs8;
typedef volatile int16_t vs16;
typedef volatile int32_t vs32;
typedef volatile int64_t vs64;
typedef float f32;
typedef double f64;
#endif
/*********************************
Function Prototypes
*********************************/
static void usb_findcart();
static void usb_64drive_write(int datatype, const void* data, int size);
static u32 usb_64drive_poll();
static void usb_64drive_read();
static void usb_everdrive_readreg(u32 reg, u32* result);
static void usb_everdrive_write(int datatype, const void* data, int size);
static u32 usb_everdrive_poll();
static void usb_everdrive_read();
static void usb_everdrive_writereg(u64 reg, u32 value);
static void usb_sc64_write(int datatype, const void* data, int size);
static u32 usb_sc64_poll();
static void usb_sc64_read();
/*********************************
Globals
*********************************/
// Function pointers
void (*funcPointer_write)(int datatype, const void* data, int size);
u32 (*funcPointer_poll)();
void (*funcPointer_read)();
// USB globals
static s8 usb_cart = CART_NONE;
static u8 __attribute__((aligned(16))) usb_buffer[BUFFER_SIZE];
int usb_datatype = 0;
int usb_datasize = 0;
int usb_dataleft = 0;
int usb_readblock = -1;
#ifndef LIBDRAGON
// 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
#endif
/*********************************
USB functions
*********************************/
/*==============================
usb_initialize
Initializes the USB buffers and pointers
@returns 1 if the USB initialization was successful, 0 if not
==============================*/
char usb_initialize()
{
// Initialize the debug related globals
memset(usb_buffer, 0, BUFFER_SIZE);
#ifndef LIBDRAGON
// Create the message queue
#if !USE_OSRAW
osCreateMesgQueue(&dmaMessageQ, &dmaMessageBuf, 1);
#endif
#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_EVERDRIVE:
funcPointer_write = usb_everdrive_write;
funcPointer_poll = usb_everdrive_poll;
funcPointer_read = usb_everdrive_read;
break;
case CART_SC64:
funcPointer_write = usb_sc64_write;
funcPointer_poll = usb_sc64_poll;
funcPointer_read = usb_sc64_read;
break;
default:
return 0;
}
return 1;
}
/*==============================
usb_findcart
Checks if the game is running on a 64Drive, EverDrive or a SummerCart64.
==============================*/
static void usb_findcart()
{
u32 buff __attribute__((aligned(8)));
// Read the cartridge and check if we have a 64Drive.
#ifdef LIBDRAGON
buff = io_read(D64_CIBASE_ADDRESS + D64_REGISTER_MAGIC);
#else
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_MAGIC, &buff);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_MAGIC, &buff);
#endif
#endif
if (buff == D64_MAGIC)
{
usb_cart = CART_64DRIVE;
return;
}
// Read the cartridge and check if we have a SummerCart64.
#ifdef LIBDRAGON
buff = io_read(SC64_REG_VERSION);
#else
#if USE_OSRAW
osPiRawReadIo(SC64_REG_VERSION, &buff);
#else
osPiReadIo(SC64_REG_VERSION, &buff);
#endif
#endif
if (buff == SC64_VERSION_A)
{
usb_cart = CART_SC64;
return;
}
// Since we didn't find a 64Drive or SummerCart64, let's assume we have an EverDrive
// Write the key to unlock the registers, then read the version register
usb_everdrive_writereg(ED_REG_KEY, ED_REGKEY);
usb_everdrive_readreg(ED_REG_VERSION, &buff);
// Check if we have an EverDrive
if (buff == ED7_VERSION || buff == ED3_VERSION)
{
// Set the USB mode
usb_everdrive_writereg(ED_REG_SYSCFG, 0);
usb_everdrive_writereg(ED_REG_USBCFG, ED_USBMODE_RDNOP);
// Set the cart to EverDrive
usb_cart = CART_EVERDRIVE;
return;
}
}
/*==============================
usb_getcart
Returns which flashcart is currently connected
@return The CART macro that corresponds to the identified flashcart
==============================*/
char usb_getcart()
{
return usb_cart;
}
/*==============================
usb_write
Writes data to the USB.
Will not write if there is data to read from 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;
// If there's data to read first, stop
if (usb_dataleft != 0)
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 number of bytes left to read
@return The data header, or 0
==============================*/
u32 usb_poll()
{
// If no debug cart exists, stop
if (usb_cart == CART_NONE)
return 0;
// If we're out of USB data to read, we don't need the header info anymore
if (usb_dataleft <= 0)
{
usb_dataleft = 0;
usb_datatype = 0;
usb_datasize = 0;
usb_readblock = -1;
}
// If there's still data that needs to be read, return the header with the data left
if (usb_dataleft != 0)
return USBHEADER_CREATE(usb_datatype, usb_dataleft);
// Call the correct read function
return funcPointer_poll();
}
/*==============================
usb_read
Reads bytes from 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 nbytes)
{
int read = 0;
int left = nbytes;
int offset = usb_datasize-usb_dataleft;
int copystart = offset%BUFFER_SIZE;
int block = BUFFER_SIZE-copystart;
int blockoffset = (offset/BUFFER_SIZE)*BUFFER_SIZE;
// If no debug cart exists, stop
if (usb_cart == CART_NONE)
return;
// If there's no data to read, stop
if (usb_dataleft == 0)
return;
// Read chunks from ROM
while (left > 0)
{
// Ensure we don't read too much data
if (left > usb_dataleft)
left = usb_dataleft;
if (block > left)
block = left;
// Call the read function if we're reading a new block
if (usb_readblock != blockoffset)
{
usb_readblock = blockoffset;
funcPointer_read();
}
// Copy from the USB buffer to the supplied buffer
memcpy(buffer+read, usb_buffer+copystart, block);
// Increment/decrement all our counters
read += block;
left -= block;
usb_dataleft -= block;
blockoffset += BUFFER_SIZE;
block = BUFFER_SIZE;
copystart = 0;
}
}
/*==============================
usb_skip
Skips a USB read by the specified amount of bytes
@param The number of bytes to skip
==============================*/
void usb_skip(int nbytes)
{
// Subtract the amount of bytes to skip to the data pointers
usb_dataleft -= nbytes;
if (usb_dataleft < 0)
usb_dataleft = 0;
}
/*==============================
usb_rewind
Rewinds a USB read by the specified amount of bytes
@param The number of bytes to rewind
==============================*/
void usb_rewind(int nbytes)
{
// Add the amount of bytes to rewind to the data pointers
usb_dataleft += nbytes;
if (usb_dataleft > usb_datasize)
usb_dataleft = usb_datasize;
}
/*==============================
usb_purge
Purges the incoming USB data
==============================*/
void usb_purge()
{
usb_dataleft = 0;
usb_datatype = 0;
usb_datasize = 0;
usb_readblock = -1;
}
/*********************************
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 __attribute__((aligned(8)));
u32 timeout = 0; // I wanted to use osGetTime() but that requires the VI manager
// Wait until the cartridge interface is ready
do
{
#ifdef LIBDRAGON
ret = io_read(D64_CIBASE_ADDRESS + D64_REGISTER_STATUS);
#else
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_STATUS, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_STATUS, &ret);
#endif
#endif
// Took too long, abort
if((timeout++) > 10000)
return -1;
}
while((ret >> 8) & D64_CI_BUSY);
(void) timeout; // Needed to stop unused variable warning
// 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();
#ifdef LIBDRAGON
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_COMMAND, enable ? D64_ENABLE_ROMWR : D64_DISABLE_ROMWR);
#else
#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
#endif
usb_64drive_wait();
}
/*==============================
usb_64drive_waitidle
Waits for the 64Drive's USB to be idle
==============================*/
static int usb_64drive_waitidle()
{
u32 status __attribute__((aligned(8)));
u32 timeout = 0;
do
{
#ifdef LIBDRAGON
status = io_read(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT);
#else
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#endif
#endif
status = (status >> 4) & D64_USB_BUSY;
if (timeout++ > 128)
return 0;
}
while(status != D64_USB_IDLE);
return 1;
}
/*==============================
usb_64drive_armstatus
Checks if the 64Drive is armed
@return The arming status
==============================*/
static u32 usb_64drive_armstatus()
{
u32 status __attribute__((aligned(8)));
#ifdef LIBDRAGON
status = io_read(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT);
#else
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#endif
#endif
return status & 0xf;
}
/*==============================
usb_64drive_waitdisarmed
Waits for the 64Drive's USB to be disarmed
==============================*/
static void usb_64drive_waitdisarmed()
{
u32 status __attribute__((aligned(8)));
do
{
#ifdef LIBDRAGON
status = io_read(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT);
#else
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, &status);
#endif
#endif
status &= 0x0F;
}
while (status != D64_USB_IDLEUNARMED);
}
/*==============================
usb_64drive_write
Sends data through USB from the 64Drive
Will not write if there is data to read from USB
@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
if (!usb_64drive_waitidle())
return;
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_buffer, (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_buffer[i] = 0;
size = size_new;
}
// Spin until the write buffer is free
if (!usb_64drive_waitidle())
{
usb_64drive_setwritable(FALSE);
return;
}
// Set up DMA transfer between RDRAM and the PI
#ifdef LIBDRAGON
data_cache_hit_writeback(usb_buffer, block);
dma_write(usb_buffer, D64_BASE_ADDRESS + DEBUG_ADDRESS + read, block);
#else
osWritebackDCache(usb_buffer, block);
#if USE_OSRAW
osPiRawStartDma(OS_WRITE,
D64_BASE_ADDRESS + DEBUG_ADDRESS + read,
usb_buffer, block);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_WRITE,
D64_BASE_ADDRESS + DEBUG_ADDRESS + read,
usb_buffer, block, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
#endif
// Keep track of what we've read so far
left -= block;
read += block;
}
// Send the data through USB
#ifdef LIBDRAGON
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, (DEBUG_ADDRESS) >> 1);
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, (size & 0xFFFFFF) | (datatype << 24));
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_COMMAND_WRITE);
#else
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, (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, (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
#endif
// Spin until the write buffer is free and then disable write mode
usb_64drive_waitidle();
usb_64drive_setwritable(FALSE);
}
/*==============================
usb_64drive_arm
Arms the 64Drive's USB
@param The ROM offset to arm
@param The size of the data to transfer
==============================*/
static void usb_64drive_arm(u32 offset, u32 size)
{
u32 ret __attribute__((aligned(8)));
ret = usb_64drive_armstatus();
if (ret != D64_USB_ARMING && ret != D64_USB_ARMED)
{
usb_64drive_waitidle();
// Arm the 64Drive, using the ROM space as a buffer
#ifdef LIBDRAGON
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, (offset >> 1));
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, (size & 0xFFFFFF));
#else
#if USE_OSRAW
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, (offset >> 1));
osPiRawWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, (size & 0xFFFFFF));
#else
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_ARM);
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, (offset >> 1));
osPiWriteIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP1R1, (size & 0xFFFFFF));
#endif
#endif
}
}
/*==============================
usb_64drive_disarm
Disarms the 64Drive's USB
==============================*/
static void usb_64drive_disarm()
{
// Disarm the USB
#ifdef LIBDRAGON
io_write(D64_CIBASE_ADDRESS + D64_REGISTER_USBCOMSTAT, D64_USB_DISARM);
#else
#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
#endif
usb_64drive_waitdisarmed();
}
/*==============================
usb_64drive_poll
Returns the header of data being received via USB on the 64Drive
The first byte contains the data type, the next 3 the number of bytes left to read
@return The data header, or 0
==============================*/
static u32 usb_64drive_poll()
{
int i;
u32 ret __attribute__((aligned(8)));
// Arm the USB buffer
usb_64drive_waitidle();
usb_64drive_setwritable(TRUE);
usb_64drive_arm(DEBUG_ADDRESS, DEBUG_ADDRESS_SIZE);
// Burn some time to see if any USB data comes in
for (i=0; i<D64_POLLTIME; i++)
;
// If there's data to service
if (usb_64drive_armstatus() == D64_USB_DATA)
{
// Read the data header from the Param0 register
#ifdef LIBDRAGON
ret = io_read(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0);
#else
#if USE_OSRAW
osPiRawReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, &ret);
#else
osPiReadIo(D64_CIBASE_ADDRESS + D64_REGISTER_USBP0R0, &ret);
#endif
#endif
// Get the data header
usb_datatype = USBHEADER_GETTYPE(ret);
usb_dataleft = USBHEADER_GETSIZE(ret);
usb_datasize = usb_dataleft;
usb_readblock = -1;
// Return the data header
usb_64drive_waitidle();
usb_64drive_setwritable(FALSE);
return USBHEADER_CREATE(usb_datatype, usb_datasize);
}
// Disarm the USB if no data arrived
usb_64drive_disarm();
usb_64drive_waitidle();
usb_64drive_setwritable(FALSE);
return 0;
}
/*==============================
usb_64drive_read
Reads bytes from the 64Drive ROM into the global buffer with the block offset
==============================*/
static void usb_64drive_read()
{
// Set up DMA transfer between RDRAM and the PI
#ifdef LIBDRAGON
data_cache_hit_writeback_invalidate(usb_buffer, BUFFER_SIZE);
dma_read(usb_buffer, D64_BASE_ADDRESS + DEBUG_ADDRESS + usb_readblock, BUFFER_SIZE);
#else
osWritebackDCacheAll();
#if USE_OSRAW
osPiRawStartDma(OS_READ,
D64_BASE_ADDRESS + DEBUG_ADDRESS + usb_readblock, usb_buffer,
BUFFER_SIZE);
#else
osPiStartDma(&dmaIOMessageBuf, OS_MESG_PRI_NORMAL, OS_READ,
D64_BASE_ADDRESS + DEBUG_ADDRESS + usb_readblock, usb_buffer,
BUFFER_SIZE, &dmaMessageQ);
(void)osRecvMesg(&dmaMessageQ, NULL, OS_MESG_BLOCK);
#endif
#endif
}
/*********************************
EverDrive functions
*********************************/
/*==============================
usb_everdrive_wait_pidma
Spins until the EverDrive's DMA is ready
==============================*/
static void usb_everdrive_wait_pidma()
{
u32 status __attribute__((aligned(8)));
do
{
status = *(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_STATUS);
status &= (PI_STATUS_DMA_BUSY | PI_STATUS_IO_BUSY);
}
while (status);
}
/*==============================
usb_everdrive_readdata
Reads data from a specific address on the EverDrive
@param The buffer with the data
@param The register address to write to the PI
@param The size of the data
==============================*/
static void usb_everdrive_readdata(void* buff, u32 pi_address, u32 len)
{
// Correct the PI address
pi_address &= 0x1FFFFFFF;
// Set up DMA transfer between RDRAM and the PI
#ifdef LIBDRAGON
data_cache_hit_writeback_invalidate(buff, len);
disable_interrupts();
// Write the data to the PI
usb_everdrive_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;
*(volatile unsigned long *)(N64_PI_ADDRESS + N64_PI_WRITELENGTH) = len-1;
usb_everdrive_wait_pidma();
// Enable system interrupts
enable_interrupts();
#else
osInvalDCache(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
#endif
}
/*==============================
usb_everdrive_readreg
Reads data from a specific register on the EverDrive
@param The register to read from