/[pcsx2_0.9.7]/trunk/plugins/CDVDpeops/CDVDisodrv.c
ViewVC logotype

Contents of /trunk/plugins/CDVDpeops/CDVDisodrv.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 31 - (show annotations) (download)
Tue Sep 7 03:24:11 2010 UTC (9 years, 11 months ago) by william
File MIME type: text/plain
File size: 7124 byte(s)
committing r3113 initial commit again...
1 /*
2 * Original code from libcdvd by Hiryu & Sjeep (C) 2002
3 * Modified by Florin for PCSX2 emu
4 */
5
6 #include <string.h>
7
8 #include "CDVDlib.h"
9 #include "CDVDiso.h"
10 #include "CDVDisodrv.h"
11
12 CdRMode cdReadMode;
13
14 struct fdtable{
15 //int fd;
16 int fileSize;
17 int LBA;
18 int filePos;
19 };
20
21 static struct fdtable fd_table[16];
22 static int fd_used[16];
23 static int files_open=0;
24 static int inited=FALSE;
25
26 /*************************************************************
27 * The functions below are the normal file-system operations, *
28 * used to provide a standard filesystem interface *
29 *************************************************************/
30
31 //////////////////////////////////////////////////////////////////////
32 // CDVDFS_init
33 // called by 80000592 sceCdInit()
34 //////////////////////////////////////////////////////////////////////
35 void CDVDFS_init(){
36
37 if (inited) return;//might change in the future as a param; forceInit/Reset
38
39 #ifdef RPC_LOG
40 RPC_LOG("[CDVDisodrv:init] CDVD Filesystem v1.00\n");
41 RPC_LOG("[CDVDisodrv ] \tby A.Lee (aka Hiryu) & Nicholas Van Veen (aka Sjeep)\n");
42 RPC_LOG("[CDVDisodrv ] Initializing '%s' file driver.\n", "cdfs");
43 #endif
44
45 //CdInit(0); already called by plugin loading system ;)
46
47 cdReadMode.trycount = 0;
48 cdReadMode.spindlctrl = CdSpinStm;
49 cdReadMode.datapattern = CdSecS2048; //isofs driver only needs
50 //2KB sectors
51
52 memset(fd_table, 0, sizeof(fd_table));
53 memset(fd_used, 0, 16*sizeof(int));
54
55 inited = TRUE;
56
57 return;
58 }
59
60 //////////////////////////////////////////////////////////////////////
61 // CDVDFS_open
62 // called by 80000001 fileio_open for devices: "cdrom:", "cdrom0:"
63 //////////////////////////////////////////////////////////////////////
64 int CDVDFS_open(char *name, int mode){
65 register int j;
66 static struct TocEntry tocEntry;
67
68 // check if the file exists
69 if (CDVD_findfile(name, &tocEntry) != TRUE)
70 return -1;
71
72 if(mode != 1) return -2; //SCE_RDONLY
73
74 // set up a new file descriptor
75 for(j=0; j < 16; j++) if(fd_used[j] == 0) break;
76 if(j >= 16) return -3;
77
78 fd_used[j] = 1;
79 files_open++;
80
81 #ifdef RPC_LOG
82 RPC_LOG("[CDVDisodrv:open] internal fd=%d\n", j);
83 #endif
84
85 fd_table[j].fileSize = tocEntry.fileSize;
86 fd_table[j].LBA = tocEntry.fileLBA;
87 fd_table[j].filePos = 0;
88
89 #ifdef RPC_LOG
90 RPC_LOG("[CDVDisodrv ] tocEntry.fileSize = %d\n",tocEntry.fileSize);
91 #endif
92
93 return j;
94 }
95
96 //////////////////////////////////////////////////////////////////////
97 // CDVDFS_lseek
98 // called by 80000001 fileio_lseek for devices: "cdrom:", "cdrom0:"
99 //////////////////////////////////////////////////////////////////////
100 int CDVDFS_lseek(int fd, int offset, int whence){
101
102 if ((fd >= 16) || (fd_used[fd]==0)){
103 #ifdef RPC_LOG
104 RPC_LOG("[CDVDisodrv:lseek] ERROR: File does not appear to be open!\n");
105 #endif
106 return -1;
107 }
108
109 switch(whence){
110 case SEEK_SET:
111 fd_table[fd].filePos = offset;
112 break;
113
114 case SEEK_CUR:
115 fd_table[fd].filePos += offset;
116 break;
117
118 case SEEK_END:
119 fd_table[fd].filePos = fd_table[fd].fileSize + offset;
120 break;
121
122 default:
123 return -1;
124 }
125
126 if (fd_table[fd].filePos < 0)
127 fd_table[fd].filePos = 0;
128
129 if (fd_table[fd].filePos > fd_table[fd].fileSize)
130 fd_table[fd].filePos = fd_table[fd].fileSize;
131
132 return fd_table[fd].filePos;
133 }
134
135 //////////////////////////////////////////////////////////////////////
136 // CDVDFS_read
137 // called by 80000001 fileio_read for devices: "cdrom:", "cdrom0:", "cdfs:"
138 //////////////////////////////////////////////////////////////////////
139 int CDVDFS_read( int fd, char *buffer, int size ){
140 // int start_sector;
141 int off_sector;
142 // int num_sectors;
143
144 //static char local_buffer[2024*2048]; //4MB
145 static char lb[2048]; //2KB
146 //Start, Aligned, End
147 int ssector, asector, esector;
148 int ssize=0, asize, esize;
149
150 if ((fd >= 16) || (fd_used[fd]==0)){
151 #ifdef RPC_LOG
152 RPC_LOG("[CDVDisodrv:read] ERROR: File does not appear to be open!\n");
153 #endif
154 return -1;
155 }
156
157 // A few sanity checks
158 if (fd_table[fd].filePos > fd_table[fd].fileSize){
159 // We cant start reading from past the beginning of the file
160 return 0; // File exists but we couldnt read anything from it
161 }
162
163 if ((fd_table[fd].filePos + size) > fd_table[fd].fileSize)
164 size = fd_table[fd].fileSize - fd_table[fd].filePos;
165
166 // Now work out where we want to start reading from
167 asector = ssector = fd_table[fd].LBA + (fd_table[fd].filePos >> 11);
168 off_sector = (fd_table[fd].filePos & 0x7FF);
169 if (off_sector){
170 ssize = min(2048 - off_sector, size);
171 size -= ssize;
172 asector++;
173 }
174 asize = size & 0xFFFFF800;
175 esize = size & 0x000007FF;
176 esector=asector + (asize >> 11);
177 size += ssize;
178
179 #ifdef RPC_LOG
180 RPC_LOG("[CDVDisodrv:read] read sectors 0x%08X to 0x%08X\n", ssector, esector-(esize==0));
181 #endif
182
183 if (ssize){ if (CdRead(ssector, 1, lb, &cdReadMode) != TRUE){
184 #ifdef RPC_LOG
185 RPC_LOG("[CDVDisodrv: ] Couldn't Read from file for some reason\n");
186 #endif
187 return 0;
188 }
189 memcpy(buffer, lb + off_sector, ssize);
190 }
191 if (asize) if (CdRead(asector, asize >> 11, buffer+ssize, &cdReadMode) != TRUE){
192 #ifdef RPC_LOG
193 RPC_LOG("[CDVDisodrv: ] Couldn't Read from file for some reason\n");
194 #endif
195 return 0;
196 }
197 if (esize){ if (CdRead(esector, 1, lb, &cdReadMode) != TRUE){
198 #ifdef RPC_LOG
199 RPC_LOG("[CDVDisodrv: ] Couldn't Read from file for some reason\n");
200 #endif
201 return 0;
202 }
203 memcpy(buffer+ssize+asize, lb, esize);
204 }
205 /***********************
206 // Now work out where we want to start reading from
207 start_sector = fd_table[fd].LBA + (fd_table[fd].filePos >> 11);
208 off_sector = (fd_table[fd].filePos & 0x7FF);
209 num_sectors = ((off_sector + size) >> 11) + 1;
210
211 #ifdef RPC_LOG
212 RPC_LOG("[CDVDisodrv:read] read sectors 0x%08X to 0x%08X\n",start_sector,start_sector+num_sectors);
213 #endif
214
215 // Read the data (we only ever get 16KB max request at once)
216 if (CdRead(start_sector, num_sectors, local_buffer, &cdReadMode) != TRUE){
217 #ifdef RPC_LOG
218 //RPC_LOG("sector = %d, start sector = %d\n",sector,start_sector);
219 RPC_LOG("[CDVDisodrv: ] Couldn't Read from file for some reason\n");
220 #endif
221 return 0;
222 }
223 //CdSync(0); hm, a wait function maybe...
224
225 memcpy(buffer,local_buffer+off_sector,size);
226 **************************/
227 fd_table[fd].filePos += size;
228
229 return (size);
230 }
231
232 //////////////////////////////////////////////////////////////////////
233 // CDVDFS_write
234 // called by 80000001 fileio_write for devices: "cdrom:", "cdrom0:"
235 // hehe, this ain't a CD writing option :D
236 //////////////////////////////////////////////////////////////////////
237 int CDVDFS_write( int fd, char * buffer, int size ){
238 if(size == 0) return 0;
239 else return -1;
240 }
241
242 //////////////////////////////////////////////////////////////////////
243 // CDVDFS_close
244 // called by 80000001 fileio_close for devices: "cdrom:", "cdrom0:"
245 //////////////////////////////////////////////////////////////////////
246 int CDVDFS_close( int fd){
247
248 if ((fd >= 16) || (fd_used[fd]==0)){
249 #ifdef RPC_LOG
250 RPC_LOG("[CDVDisodrv:close] ERROR: File does not appear to be open!\n");
251 #endif
252 return -1;
253 }
254
255 #ifdef RPC_LOG
256 RPC_LOG("[CDVDisodrv:close] internal fd %d\n", fd);
257 #endif
258
259 fd_used[fd] = 0;
260 files_open--;
261
262 return 0;
263 }
264

  ViewVC Help
Powered by ViewVC 1.1.22