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
|
#include "dolphin/os.h"
#define EXI_TX 0x800400u
#define EXI_MAGIC 0xa5ff005a
static s32 Chan;
static u32 Dev;
static u32 Enabled = 0;
static u32 BarnacleEnabled = 0;
static BOOL ProbeBarnacle(s32 chan, u32 dev, u32* revision) {
BOOL err;
u32 cmd;
if (chan != 2 && dev == 0 && !EXIAttach(chan, NULL)) {
return FALSE;
}
err = !EXILock(chan, dev, NULL);
if (!err) {
err = !EXISelect(chan, dev, EXI_FREQ_1M);
if (!err) {
cmd = 0x20011300;
err = FALSE;
err |= !EXIImm(chan, &cmd, 4, EXI_WRITE, NULL);
err |= !EXISync(chan);
err |= !EXIImm(chan, revision, 4, EXI_READ, NULL);
err |= !EXISync(chan);
err |= !EXIDeselect(chan);
}
EXIUnlock(chan);
}
if (chan != 2 && dev == 0) {
EXIDetach(chan);
}
if (err) {
return FALSE;
}
return (*revision != 0xFFFFFFFF) ? TRUE : FALSE;
}
void __OSEnableBarnacle(s32 chan, u32 dev) {
u32 id;
if (EXIGetID(chan, dev, &id)) {
switch (id) {
case 0xffffffff:
case EXI_MEMORY_CARD_59:
case EXI_MEMORY_CARD_123:
case EXI_MEMORY_CARD_251:
case EXI_MEMORY_CARD_507:
case EXI_USB_ADAPTER:
case EXI_NPDP_GDEV:
case EXI_MODEM:
case EXI_MARLIN:
case 0x04220000:
case 0x04020100:
case 0x04020200:
case 0x04020300:
case 0x04040404:
case 0x04060000:
case 0x04120000:
case 0x04130000:
case 0x80000000 | EXI_MEMORY_CARD_59:
case 0x80000000 | EXI_MEMORY_CARD_123:
case 0x80000000 | EXI_MEMORY_CARD_251:
case 0x80000000 | EXI_MEMORY_CARD_507:
break;
default:
if (ProbeBarnacle(chan, dev, &id)) {
Chan = chan;
Dev = dev;
Enabled = BarnacleEnabled = EXI_MAGIC;
}
break;
}
}
}
u32 InitializeUART(u32 baudRate) {
if (BarnacleEnabled == EXI_MAGIC) {
return 0;
}
if (!(OSGetConsoleType() & OS_CONSOLE_DEVELOPMENT)) {
Enabled = 0;
return 2;
} else {
Chan = 0;
Dev = 1;
Enabled = EXI_MAGIC;
return 0;
}
}
u32 ReadUARTN(void* bytes, unsigned long length) { return 4; }
static int QueueLength(void) {
u32 cmd;
if (!EXISelect(Chan, Dev, EXI_FREQ_8M))
return -1;
cmd = EXI_TX << 6;
EXIImm(Chan, &cmd, 4, EXI_WRITE, NULL);
EXISync(Chan);
EXIImm(Chan, &cmd, 1, EXI_READ, NULL);
EXISync(Chan);
EXIDeselect(Chan);
return 16 - (int)((cmd >> 24) & 0xff);
}
u32 WriteUARTN(const void* buf, unsigned long len) {
u32 cmd;
int qLen;
long xLen;
char* ptr;
BOOL locked;
u32 error;
if (Enabled != EXI_MAGIC)
return 2;
locked = EXILock(Chan, Dev, 0);
if (!locked) {
return 0;
}
for (ptr = (char*)buf; ptr - buf < len; ptr++) {
if (*ptr == '\n')
*ptr = '\r';
}
error = 0;
cmd = (EXI_TX | 0x2000000) << 6;
while (len) {
qLen = QueueLength();
if (qLen < 0) {
error = 3;
break;
}
if (qLen < 12 && qLen < len)
continue;
if (!EXISelect(Chan, Dev, EXI_FREQ_8M)) {
error = 3;
break;
}
EXIImm(Chan, &cmd, 4, EXI_WRITE, NULL);
EXISync(Chan);
while (qLen && len) {
if (qLen < 4 && qLen < len)
break;
xLen = (len < 4) ? (long)len : 4;
EXIImm(Chan, (void*)buf, xLen, EXI_WRITE, NULL);
(u8*)buf += xLen;
len -= xLen;
qLen -= xLen;
EXISync(Chan);
}
EXIDeselect(Chan);
}
EXIUnlock(Chan);
return error;
}
|