1 #include "../PicoInt.h"
6 static unsigned char *cd_buffer = NULL;
7 static int prev_lba = 0x80000000;
9 static int hits, reads;
12 void PicoCDBufferInit(void)
16 prev_lba = 0x80000000;
19 if (PicoCDBuffers <= 1) {
21 return; /* buffering off */
24 /* try alloc'ing until we succeed */
25 while (PicoCDBuffers > 0)
27 tmp = realloc(cd_buffer, PicoCDBuffers * 2048);
28 if (tmp != NULL) break;
32 if (PicoCDBuffers <= 0) return; /* buffering became off */
38 void PicoCDBufferFree(void)
45 printf("CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
49 /* this is a try to fight slow SD access of GP2X */
50 void PicoCDBufferRead(void *dest, int lba)
52 int is_bin, offs, read_len;
55 is_bin = Pico_mcd->TOC.Tracks[0].ftype == TYPE_BIN;
57 if (PicoCDBuffers <= 0)
60 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
61 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
62 pm_read(dest, 2048, Pico_mcd->TOC.Tracks[0].F);
67 offs = lba - prev_lba;
68 if (offs >= 0 && offs < PicoCDBuffers)
71 if (offs == 0) dprintf("CD buffer seek to old %i -> %i\n", prev_lba, lba);
72 memcpy32(dest, (int *)(cd_buffer + offs*2048), 2048/4);
76 if (prev_lba + PicoCDBuffers != lba)
78 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
79 dprintf("CD buffer seek %i -> %i\n", prev_lba, lba);
80 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
83 dprintf("CD buffer miss %i -> %i\n", prev_lba, lba);
85 if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
87 dprintf("CD buffer move");
88 read_len = prev_lba - lba;
89 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
93 read_len = PicoCDBuffers;
99 for (i = 0; i < read_len; i++)
101 pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
102 pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
107 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
109 memcpy32(dest, (int *) cd_buffer, 2048/4);