2 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
4 #include "../PicoInt.h"
7 static unsigned char *cd_buffer = NULL;
8 static int prev_lba = 0x80000000;
10 static int hits, reads;
12 //#define THREADED_CD_IO
17 #define tioprintf printf
19 static pthread_t thr_thread = 0;
20 static pthread_cond_t thr_cond = PTHREAD_COND_INITIALIZER;
21 static pthread_mutex_t thr_mutex = PTHREAD_MUTEX_INITIALIZER;
22 static unsigned char *thr_buffer[2][2048 + 304] __attribute__((aligned(4)));
23 static int thr_lba_need;
24 static int thr_lba_have[2];
26 static void thr_read_lba(int slot, int lba)
28 int is_bin = Pico_mcd->TOC.Tracks[0].ftype == TYPE_BIN;
29 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
31 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
32 pm_read(thr_buffer[slot], 2048, Pico_mcd->TOC.Tracks[0].F);
33 thr_lba_have[slot] = lba;
36 static void *buffering_thread(void *arg)
40 elprintf(EL_STATUS, "CD I/O thread started.");
42 pthread_mutex_lock(&thr_mutex);
46 if (thr_lba_need < 0) goto wait;
49 if (thr_lba_have[0] == -1) free_slot = 0;
50 if (thr_lba_have[1] == -1) free_slot = 1;
51 if (free_slot == -1) goto wait;
54 if (lba != thr_lba_have[free_slot^1]) {
55 thr_read_lba(free_slot, lba);
56 tioprintf("t done %i %i\n", lba, free_slot);
60 if (lba != thr_lba_have[free_slot^1]) {
61 thr_read_lba(free_slot, lba);
62 tioprintf("t done %i %i\n", lba, free_slot);
67 pthread_cond_wait(&thr_cond, &thr_mutex);
68 tioprintf("t wake\n");
71 pthread_mutex_unlock(&thr_mutex);
76 static void threaded_read(void *dest, int lba)
81 if (lba == thr_lba_have[0]) have = 0;
82 if (lba == thr_lba_have[1]) have = 1;
85 tioprintf("r hit %i %i\n", lba, have);
86 memcpy32(dest, (int *)thr_buffer[have], 2048/4);
87 if (lba != prev_lba) {
88 thr_lba_have[have] = -1; // make free slot
89 thr_lba_need = lba + 1; // guess a sequential read..
90 pthread_cond_signal(&thr_cond);
97 tioprintf("r miss %i\n", lba);
99 pthread_mutex_lock(&thr_mutex);
100 pthread_mutex_unlock(&thr_mutex);
101 if (lba == thr_lba_have[0]) have = 0;
102 if (lba == thr_lba_have[1]) have = 1;
106 thr_lba_have[0] = thr_lba_have[1] = -1;
107 for (i = 0; have == -1 && i < 10; i++)
109 tioprintf("r hard %i\n", lba);
110 pthread_cond_signal(&thr_cond);
112 pthread_mutex_lock(&thr_mutex);
113 pthread_mutex_unlock(&thr_mutex);
114 if (lba == thr_lba_have[0]) have = 0;
115 if (lba == thr_lba_have[1]) have = 1;
119 // we MUST have the data at this point..
120 if (have == -1) { tioprintf("BUG!\n"); exit(1); }
121 tioprintf("r reco %i %i\n", lba, have);
122 memcpy32(dest, (int *)thr_buffer[have], 2048/4);
123 thr_lba_have[have] = -1;
124 pthread_cond_signal(&thr_cond);
132 void PicoCDBufferInit(void)
136 prev_lba = 0x80000000;
140 if (PicoCDBuffers <= 1) {
142 goto no_buffering; /* buffering off */
145 /* try alloc'ing until we succeed */
146 while (PicoCDBuffers > 0)
148 tmp = realloc(cd_buffer, PicoCDBuffers * 2048 + 304);
149 if (tmp != NULL) break;
153 if (PicoCDBuffers > 0) {
159 #ifdef THREADED_CD_IO
160 thr_lba_need = thr_lba_have[0] = thr_lba_have[1] = -1;
163 pthread_create(&thr_thread, NULL, buffering_thread, NULL);
169 void PicoCDBufferFree(void)
171 #ifdef THREADED_CD_IO
172 pthread_mutex_lock(&thr_mutex);
173 pthread_mutex_unlock(&thr_mutex);
180 elprintf(EL_STATUS, "CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
184 /* this is a try to fight slow SD access of GP2X */
185 PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba)
187 int is_bin, offs, read_len, moved = 0;
190 is_bin = Pico_mcd->TOC.Tracks[0].ftype == TYPE_BIN;
192 if (PicoCDBuffers <= 0)
194 #ifdef THREADED_CD_IO
195 threaded_read(dest, lba);
199 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
200 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
201 pm_read(dest, 2048, Pico_mcd->TOC.Tracks[0].F);
207 offs = lba - prev_lba;
208 if (offs >= 0 && offs < PicoCDBuffers)
211 if (offs == 0) dprintf("CD buffer seek to old %i -> %i\n", prev_lba, lba);
212 memcpy32(dest, (int *)(cd_buffer + offs*2048), 2048/4);
216 if (prev_lba + PicoCDBuffers != lba)
218 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
219 dprintf("CD buffer seek %i -> %i\n", prev_lba, lba);
220 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
223 dprintf("CD buffer miss %i -> %i\n", prev_lba, lba);
225 if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
227 read_len = prev_lba - lba;
228 dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
229 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
234 read_len = PicoCDBuffers;
237 if (PicoMessage != NULL && read_len >= 512)
239 PicoMessage("Buffering data...");
246 int bufs = (read_len*2048) / (2048+304);
247 pm_read(cd_buffer, bufs*(2048+304), Pico_mcd->TOC.Tracks[0].F);
248 for (i = 1; i < bufs; i++)
249 // should really use memmove here, but my memcpy32 implementation is also suitable here
250 memcpy32((int *)(cd_buffer + i*2048), (int *)(cd_buffer + i*(2048+304)), 2048/4);
252 for (; i < read_len - 1; i++)
254 pm_read(cd_buffer + i*2048, 2048 + 304, Pico_mcd->TOC.Tracks[0].F);
255 // pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR); // seeking is slower, in PSP case even more
257 // further data might be moved, do not overwrite
258 pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
259 pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
263 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
265 memcpy32(dest, (int *) cd_buffer, 2048/4);
270 /* file pointer must point to the same data in file, as would-be data after our buffer */
272 lba += PicoCDBuffers;
273 where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
274 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);