This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
/
usb.c
180 lines (126 loc) · 3.63 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
#include "everdrive.h"
#include "sys.h"
#include "types.h"
#include <libdragon.h>
#include "rom.h"
u8 cmdTest();
u8 cmdFill();
u8 cmdReadRom();
u8 cmdWriteRom();
u64 usb_buff[128];
u8 *usb_buff8; // = (u8 *) usb_buff;
#define PI_BSD_DOM1_LAT_REG (PI_BASE_REG+0x14)
/* PI dom1 pulse width (R/W): [7:0] domain 1 device R/W strobe pulse width */
#define PI_BSD_DOM1_PWD_REG (PI_BASE_REG+0x18)
/* PI dom1 page size (R/W): [3:0] domain 1 device page size */
#define PI_BSD_DOM1_PGS_REG (PI_BASE_REG+0x1C) /* page size */
/* PI dom1 release (R/W): [1:0] domain 1 device R/W release duration */
#define PI_BSD_DOM1_RLS_REG (PI_BASE_REG+0x20)
/* PI dom2 latency (R/W): [7:0] domain 2 device latency */
#define PI_BSD_DOM2_LAT_REG (PI_BASE_REG+0x24) /* Domain 2 latency */
/* PI dom2 pulse width (R/W): [7:0] domain 2 device R/W strobe pulse width */
#define PI_BSD_DOM2_PWD_REG (PI_BASE_REG+0x28) /* pulse width */
/* PI dom2 page size (R/W): [3:0] domain 2 device page size */
#define PI_BSD_DOM2_PGS_REG (PI_BASE_REG+0x2C) /* page size */
/* PI dom2 release (R/W): [1:0] domain 2 device R/W release duration */
#define PI_BSD_DOM2_RLS_REG (PI_BASE_REG+0x30) /* release duration */
#define PHYS_TO_K1(x) ((u32)(x)|0xA0000000) /* physical to kseg1 */
#define IO_WRITE(addr,data) (*(volatile u32*)PHYS_TO_K1(addr)=(u32)(data))
#define PI_BASE_REG 0x04600000
extern u8 system_cic;
u8 usbListener() {
volatile u16 resp;
volatile u8 cmd;
usb_buff8 = (u8 *) usb_buff;
if (evd_fifoRxf())return 0;
resp = evd_fifoRd(usb_buff, 1);
if (resp != 0) return 1;
if (usb_buff8[0] != 'C' || usb_buff8[1] != 'M' || usb_buff8[2] != 'D')return 2;
cmd = usb_buff8[3];
switch (cmd) {
case 'R':
resp = cmdReadRom();
if (resp)return 10;
break;
case 'W':
resp = cmdWriteRom();
if (resp)return 11;
break;
case 'T':
resp = cmdTest();
if (resp)return 12;
break;
case 'F':
resp = cmdFill();
if (resp)return 13;
break;
case 'S':
//IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x0c);
//IO_WRITE(PI_BSD_DOM1_PGS_REG, 0x80);
//evdSetESaveType(SAVE_TYPE_EEP16k);
system_cic = CIC_6102;
evd_lockRegs();
IO_WRITE(PI_STATUS_REG, 3);
sleep(2);
pif_boot();
break;
}
return 0;
}
u8 cmdTest() {
u16 resp;
usb_buff8[3] = 'k';
resp = evd_fifoWr(usb_buff, 1);
if (resp)return 1;
return 0;
}
u8 cmdFill() {
u16 resp;
u32 i;
//console_printf("fill...\n");
for (i = 0; i < 512; i++) {
usb_buff8[i] = 0;
}
//console_printf("buff prepared\n");
romFill(0, 0x200000, 0);
//console_printf("fill done\n");
usb_buff8[3] = 'k';
resp = evd_fifoWr(usb_buff, 1);
if (resp)return 1;
//console_printf("resp sent ok\n");
return 0;
}
u8 cmdReadRom() {
u16 resp;
u16 ptr;
u16 len;
u32 addr;
ptr = 4;
addr = usb_buff8[ptr++];
addr <<= 8;
addr |= usb_buff8[ptr++];
addr *= 2048;
len = usb_buff8[ptr++];
len <<= 8;
len |= usb_buff8[ptr++];
resp = evd_fifoWrFromCart(addr, len);
if (resp)return 1;
return 0;
}
u8 cmdWriteRom() {
u16 resp;
u16 ptr;
u16 len;
u32 addr;
ptr = 4;
addr = usb_buff8[ptr++];
addr <<= 8;
addr |= usb_buff8[ptr++];
addr *= 2048;
len = usb_buff8[ptr++];
len <<= 8;
len |= usb_buff8[ptr++];
resp = evd_fifoRdToCart(addr, len);
if (resp)return 1;
return 0;
}