forked from mrehkopf/sd2snes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskio.c
202 lines (160 loc) · 4.66 KB
/
diskio.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
/* sd2snes - SD card based universal cartridge for the SNES
Copyright (C) 2009-2010 Maximilian Rehkopf <[email protected]>
This file was adapted from sd2iec, written by Ingo Korb.
Original copyright header follows:
*/
/* sd2iec - SD/MMC to Commodore serial bus interface/controller
Copyright (C) 2007-2009 Ingo Korb <[email protected]>
Inspiration and low-level SD/MMC access based on code from MMC2IEC
by Lars Pontoppidan et al., see sdcard.c|h and config.h.
FAT filesystem access based on code from ChaN and Jim Brain, see ff.c|h.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; version 2 of the License only.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
diskio.c: Generic disk access routines and supporting stuff
*/
#include "config.h"
#include "diskio.h"
#include "sdcard.h"
volatile enum diskstates disk_state;
#ifdef NEED_DISKMUX
uint32_t drive_config;
/* This function calculates the default drive configuration. */
/* Its result is static after compilation, but doing this in */
/* C in less messy than doing it with the preprocessor. */
uint32_t get_default_driveconfig(void) {
uint32_t result = 0xffffffffL;
/* Order matters: Whatever is checked first will be last in the config */
#ifdef HAVE_DF
result = (result << 4) + (DISK_TYPE_DF << DRIVE_BITS) + 0;
#endif
#ifdef CONFIG_TWINSD
result = (result << 4) + (DISK_TYPE_SD << DRIVE_BITS) + 1;
#endif
#ifdef HAVE_SD
result = (result << 4) + (DISK_TYPE_SD << DRIVE_BITS) + 0;
#endif
#ifdef HAVE_ATA
result = (result << 4) + (DISK_TYPE_ATA << DRIVE_BITS) + 0;
#endif
return result;
}
void disk_init(void) {
#ifdef HAVE_SD
sd_init();
#endif
#ifdef HAVE_ATA
ata_init();
#endif
#ifdef HAVE_DF
df_init();
#endif
}
DSTATUS disk_status(BYTE drv) {
switch(drv >> DRIVE_BITS) {
#ifdef HAVE_DF
case DISK_TYPE_DF:
return df_status(drv & DRIVE_MASK);
#endif
#ifdef HAVE_ATA
case DISK_TYPE_ATA:
return ata_status(drv & DRIVE_MASK);
case DISK_TYPE_ATA2:
return ata_status((drv & DRIVE_MASK) + 2);
#endif
#ifdef HAVE_SD
case DISK_TYPE_SD:
return sd_status(drv & DRIVE_MASK);
#endif
default:
return STA_NOINIT|STA_NODISK;
}
}
DSTATUS disk_initialize(BYTE drv) {
switch(drv >> DRIVE_BITS) {
#ifdef HAVE_DF
case DISK_TYPE_DF:
return df_initialize(drv & DRIVE_MASK);
#endif
#ifdef HAVE_ATA
case DISK_TYPE_ATA:
return ata_initialize(drv & DRIVE_MASK);
case DISK_TYPE_ATA2:
return ata_initialize((drv & DRIVE_MASK) + 2);
#endif
#ifdef HAVE_SD
case DISK_TYPE_SD:
return sd_initialize(drv & DRIVE_MASK);
#endif
default:
return STA_NOINIT|STA_NODISK;
}
}
DRESULT disk_read(BYTE drv, BYTE *buffer, DWORD sector, BYTE count) {
switch(drv >> DRIVE_BITS) {
#ifdef HAVE_DF
case DISK_TYPE_DF:
return df_read(drv & DRIVE_MASK,buffer,sector,count);
#endif
#ifdef HAVE_ATA
case DISK_TYPE_ATA:
return ata_read(drv & DRIVE_MASK,buffer,sector,count);
case DISK_TYPE_ATA2:
return ata_read((drv & DRIVE_MASK) + 2,buffer,sector,count);
#endif
#ifdef HAVE_SD
case DISK_TYPE_SD:
return sd_read(drv & DRIVE_MASK,buffer,sector,count);
#endif
default:
return RES_ERROR;
}
}
DRESULT disk_write(BYTE drv, const BYTE *buffer, DWORD sector, BYTE count) {
switch(drv >> DRIVE_BITS) {
#ifdef HAVE_DF
case DISK_TYPE_DF:
return df_write(drv & DRIVE_MASK,buffer,sector,count);
#endif
#ifdef HAVE_ATA
case DISK_TYPE_ATA:
return ata_write(drv & DRIVE_MASK,buffer,sector,count);
case DISK_TYPE_ATA2:
return ata_write((drv & DRIVE_MASK) + 2,buffer,sector,count);
#endif
#ifdef HAVE_SD
case DISK_TYPE_SD:
return sd_write(drv & DRIVE_MASK,buffer,sector,count);
#endif
default:
return RES_ERROR;
}
}
DRESULT disk_getinfo(BYTE drv, BYTE page, void *buffer) {
switch(drv >> DRIVE_BITS) {
#ifdef HAVE_DF
case DISK_TYPE_DF:
return df_getinfo(drv & DRIVE_MASK,page,buffer);
#endif
#ifdef HAVE_ATA
case DISK_TYPE_ATA:
return ata_getinfo(drv & DRIVE_MASK,page,buffer);
case DISK_TYPE_ATA2:
return ata_getinfo((drv & DRIVE_MASK) + 2,page,buffer);
#endif
#ifdef HAVE_SD
case DISK_TYPE_SD:
return sd_getinfo(drv & DRIVE_MASK,page,buffer);
#endif
default:
return RES_ERROR;
}
}
#endif