5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
9 #include "../pico_int.h"
10 #include "../cd/cue.h"
12 int PicoCDBuffers = 0;
13 static unsigned char *cd_buffer = NULL;
14 static int prev_lba = 0x80000000;
16 static int hits, reads;
21 void PicoCDBufferInit(void)
25 prev_lba = 0x80000000;
28 if (PicoCDBuffers <= 1) {
30 return; /* buffering off */
33 /* try alloc'ing until we succeed */
34 while (PicoCDBuffers > 0)
36 tmp = realloc(cd_buffer, PicoCDBuffers * 2048 + 304);
37 if (tmp != NULL) break;
41 if (PicoCDBuffers <= 0) return; /* buffering became off */
47 void PicoCDBufferFree(void)
54 elprintf(EL_STATUS, "CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
58 void PicoCDBufferFlush(void)
60 prev_lba = 0x80000000;
64 /* this is was a try to fight slow SD access of GP2X */
65 PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba)
67 int is_bin, offs, read_len, moved = 0;
70 is_bin = Pico_mcd->TOC.Tracks[0].ftype == CT_BIN;
72 if (PicoCDBuffers <= 0)
75 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
76 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
77 pm_read(dest, 2048, Pico_mcd->TOC.Tracks[0].F);
82 offs = lba - prev_lba;
83 if (offs >= 0 && offs < PicoCDBuffers)
86 if (offs == 0) dprintf("CD buffer seek to old %i -> %i\n", prev_lba, lba);
87 memcpy32(dest, (int *)(cd_buffer + offs*2048), 2048/4);
91 if (prev_lba + PicoCDBuffers != lba)
93 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
94 dprintf("CD buffer seek %i -> %i\n", prev_lba, lba);
95 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
98 dprintf("CD buffer miss %i -> %i\n", prev_lba, lba);
100 if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
102 read_len = prev_lba - lba;
103 dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
104 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
109 read_len = PicoCDBuffers;
112 if (PicoMessage != NULL && read_len >= 512)
114 PicoMessage("Buffering data...");
120 #ifdef _PSP_FW_VERSION
121 int bufs = (read_len*2048) / (2048+304);
122 pm_read(cd_buffer, bufs*(2048+304), Pico_mcd->TOC.Tracks[0].F);
123 for (i = 1; i < bufs; i++)
124 // should really use memmove here, but my memcpy32 implementation is also suitable here
125 memcpy32((int *)(cd_buffer + i*2048), (int *)(cd_buffer + i*(2048+304)), 2048/4);
127 for (; i < read_len - 1; i++)
129 pm_read(cd_buffer + i*2048, 2048 + 304, Pico_mcd->TOC.Tracks[0].F);
130 // pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR); // seeking is slower, in PSP case even more
132 // further data might be moved, do not overwrite
133 pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
134 pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
138 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
140 memcpy32(dest, (int *) cd_buffer, 2048/4);
145 /* file pointer must point to the same data in file, as would-be data after our buffer */
147 lba += PicoCDBuffers;
148 where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
149 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);