-
Notifications
You must be signed in to change notification settings - Fork 0
/
iointerface.c
150 lines (133 loc) · 3.39 KB
/
iointerface.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
#include <nds/card.h>
void ioM3CardWaitReady(u32 flags, u8 *command)
{
bool ready = false;
do {
cardWriteCommand(command);
REG_ROMCTRL = flags;
do {
if (REG_ROMCTRL & CARD_DATA_READY)
if (!CARD_DATA_RD) ready = true;
} while (REG_ROMCTRL & CARD_BUSY);
} while (!ready);
}
void ioM3ByteCardPolledTransfer(uint32 flags, uint32 * destination, uint32 length, uint8 * command)
{
u32 data;;
cardWriteCommand(command);
REG_ROMCTRL = flags;
uint32 * target = destination + length;
do {
// Read data if available
if (REG_ROMCTRL & CARD_DATA_READY) {
data=CARD_DATA_RD;
if (destination < target) {
((uint8*)destination)[0] = data & 0xff;
((uint8*)destination)[1] = (data >> 8) & 0xff;
((uint8*)destination)[2] = (data >> 16) & 0xff;
((uint8*)destination)[3] = (data >> 24) & 0xff;
}
destination++;
}
} while (REG_ROMCTRL & CARD_BUSY);
}
void ioM3LogicCardRead(u32 address, u32 *destination, u32 length)
{
u8 command[8];
command[7] = 0xC9; // GMP-Z003
command[6] = (address >> 24) & 0xff;
command[5] = (address >> 16) & 0xff;
command[4] = (address >> 8) & 0xff;
command[3] = address & 0xff;
command[2] = 0;
command[1] = 0;
command[0] = 0;
ioR4CardWaitReady(0xa7586000, command);
command[7] = 0xCA; // GMP-Z003
if ((u32)destination & 0x03)
ioM3ByteCardPolledTransfer(0xa1586000, destination, length, command);
else // read logic might not be perfect..
cardPolledTransfer(0xa1586000, destination, length, command);
}
void ioM3LogicCardWrite(u32 address, u32 *source, u32 length)
{
u8 command[8];
u32 data = 0;
command[7] = 0xC5; // GMP-Z003
command[6] = (address >> 24) & 0xff;
command[5] = (address >> 16) & 0xff;
command[4] = (address >> 8) & 0xff;
command[3] = address & 0xff;
command[2] = 0;
command[1] = 0;
command[0] = 0;
cardWriteCommand(command);
REG_ROMCTRL = 0xe1586000;
u32 * target = source + length;
do {
// Write data if ready
if (REG_ROMCTRL & CARD_DATA_READY) {
if (source < target) {
if ((u32)source & 0x03)
data = ((uint8*)source)[0] | (((uint8*)source)[1] << 8) | (((uint8*)source)[2] << 16) | (((uint8*)source)[3] << 24);
else
data = *source;
}
source++;
CARD_DATA_RD = data;
}
} while (REG_ROMCTRL & CARD_BUSY);
command[7] = 0xC6; // GMP-Z003
ioM3CardWaitReady(0xa7586000, command);
}
u32 ioM3ReadCardInfo(void)
{
u8 command[8];
u32 ret;
command[7] = 0xb0; // GMP-Z003
command[6] = 0;
command[5] = 0;
command[4] = 0;
command[3] = 0;
command[2] = 0;
command[1] = 0;
command[0] = 0;
cardPolledTransfer(0xa7586000, &ret, 1, command);
return ret;
}
bool startup(void)
{
return (ioM3ReadCardInfo() & 0x07) == 0x04;
}
bool isInserted(void)
{
return (ioM3ReadCardInfo() & 0x07) == 0x04;
}
bool readSectors(u32 sector, u32 numSecs, void* buffer)
{
u32 *u32_buffer = (u32*)buffer, i;
for (i = 0; i < numSecs; i++) {
ioM3LogicCardRead(sector << 9, u32_buffer, 128);
sector++;
u32_buffer += 128;
}
return true;
}
bool writeSectors(u32 sector, u32 numSecs, void* buffer)
{
u32 *u32_buffer = (u32*)buffer, i;
for (i = 0; i < numSecs; i++) {
ioM3LogicCardWrite(sector << 9, u32_buffer, 128);
sector++;
u32_buffer += 128;
}
return true;
}
bool clearStatus(void)
{
return true;
}
bool shutdown(void)
{
return true;
}