-
Notifications
You must be signed in to change notification settings - Fork 0
/
ior4tf.c
183 lines (157 loc) · 3.02 KB
/
ior4tf.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
#include "u.h"
#include "io.h"
#include "mem.h"
#include "arm7/card.h"
/*
* storage support for the r4ds card
* Copyright (c) 2006 Michael "Chishm" Chisholm
*/
#define DPRINT if(1)
static void
cardWaitReady(ulong flags, uchar *cmd)
{
int ready = 0;
while (!ready){
cardWriteCommand(cmd);
CARD_CR2 = flags;
if(!(CARD_CR2 & CARD_BUSY))
continue;
if(!(CARD_CR2 & CARD_DATA_READY))
continue;
if (!CARD_DATA_RD)
ready = 1;
}
}
static void
bytecardPolledTransfer(ulong flags, ulong * dst, ulong len, uchar* cmd) {
ulong i, data;
uchar *p;
cardWriteCommand(cmd);
CARD_CR2 = flags;
for (i=0; i < len; i++){
if (!(CARD_CR2 & CARD_BUSY))
break;
while (!(CARD_CR2 & CARD_DATA_READY));
data = CARD_DATA_RD;
p = (uchar*)&dst[i];
p[0] = data & 0xff;
p[1] = (data >> 8) & 0xff;
p[2] = (data >> 16) & 0xff;
p[3] = (data >> 24) & 0xff;
}
}
static void
LogicCardRead(ulong addr, ulong *dst, ulong len)
{
uchar cmd[8];
cmd[7] = 0xb9;
cmd[6] = (addr >> 24) & 0xff;
cmd[5] = (addr >> 16) & 0xff;
cmd[4] = (addr >> 8) & 0xff;
cmd[3] = addr & 0xff;
cmd[2] = 0;
cmd[1] = 0;
cmd[0] = 0;
cardWaitReady(0xa7586000, cmd);
cmd[7] = 0xba;
if ((ulong)dst & 0x03)
bytecardPolledTransfer(0xa1586000, dst, len, cmd);
else
cardPolledTransfer(0xa1586000, dst, len, cmd);
}
static ulong
ReadCardInfo(void)
{
uchar cmd[8] = {0, 0, 0, 0, 0, 0, 0, 0xb0};
ulong ret;
cardPolledTransfer(0xa7586000, &ret, 1, cmd);
return ret;
}
static void
LogicCardWrite(ulong addr, ulong *src, ulong len)
{
uchar cmd[8];
ulong i;
cmd[7] = 0xbb;
cmd[6] = (addr >> 24) & 0xff;
cmd[5] = (addr >> 16) & 0xff;
cmd[4] = (addr >> 8) & 0xff;
cmd[3] = addr & 0xff;
cmd[2] = 0;
cmd[1] = 0;
cmd[0] = 0;
cardWriteCommand(cmd);
CARD_CR2 = 0xe1586000;
if ((ulong)src & 0x03){
for(i=0; i<len; i++){
if(!(CARD_CR2 & CARD_BUSY))
break;
while(!(CARD_CR2 & CARD_DATA_READY));
CARD_DATA_RD = ((uchar*)src[i])[0] |
(((uchar*)src[i])[1] << 8) |
(((uchar*)src[i])[2] << 16) |
(((uchar*)src[i])[3] << 24);
}
}else{
for(i=0; i<len; i++){
if(!(CARD_CR2 & CARD_BUSY))
break;
while(!(CARD_CR2 & CARD_DATA_READY));
CARD_DATA_RD = src[i];
}
}
cmd[7] = 0xbc;
cardWaitReady(0xa7586000, cmd);
}
static int
r4tf_init(void)
{
ulong CardInfo;
CardInfo = ReadCardInfo();
return ((CardInfo & 0x07) == 0x04);
}
enum
{
Blkbits = 9,
Blksize = 1<<9,
};
static int
r4tf_read(ulong start, ulong n, void* d)
{
ulong *ud = (ulong*)d, i;
for (i = 0; i < n; i++) {
LogicCardRead((start+i)<<Blkbits, ud, (Blksize/4));
ud += (Blksize/4);
}
return 1;
}
static int
r4tf_write(ulong start, ulong n, void* d)
{
ulong *ud = (ulong*)d, i;
for (i = 0; i < n; i++) {
LogicCardWrite((start+i)<<Blkbits, ud, (Blksize/4));
ud += (Blksize/4);
}
return 1;
}
static int
r4tf_clrstat(void)
{
return 1;
}
static Ioifc io_r4tf = {
"R4TF",
Cread|Cwrite|Cslotnds,
r4tf_init,
r4tf_init,
(void*)r4tf_read,
(void*)r4tf_write,
r4tf_clrstat,
r4tf_clrstat,
};
void
ior4tflink(void)
{
addioifc(&io_r4tf);
}