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, moved = 0;
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 read_len = prev_lba - lba;
88 dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
89 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
94 read_len = PicoCDBuffers;
97 if (PicoMessage != NULL && read_len >= 512)
99 PicoMessage("Buffering data...");
105 for (i = 0; i < read_len; i++)
107 pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
108 pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
113 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
115 memcpy32(dest, (int *) cd_buffer, 2048/4);
120 /* file pointer must point to the same data in file, as would-be data after our buffer */
122 lba += PicoCDBuffers;
123 where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
124 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);