2 // (c) Copyright 2007, Grazvydas "notaz" Ignotas
4 #include "../PicoInt.h"
9 static unsigned char *cd_buffer = NULL;
10 static int prev_lba = 0x80000000;
12 static int hits, reads;
15 void PicoCDBufferInit(void)
19 prev_lba = 0x80000000;
22 if (PicoCDBuffers <= 1) {
24 return; /* buffering off */
27 /* try alloc'ing until we succeed */
28 while (PicoCDBuffers > 0)
30 tmp = realloc(cd_buffer, PicoCDBuffers * 2048);
31 if (tmp != NULL) break;
35 if (PicoCDBuffers <= 0) return; /* buffering became off */
41 void PicoCDBufferFree(void)
48 printf("CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
52 /* this is a try to fight slow SD access of GP2X */
53 PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba)
55 int is_bin, offs, read_len, moved = 0;
58 is_bin = Pico_mcd->TOC.Tracks[0].ftype == TYPE_BIN;
60 if (PicoCDBuffers <= 0)
63 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
64 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
65 pm_read(dest, 2048, Pico_mcd->TOC.Tracks[0].F);
70 offs = lba - prev_lba;
71 if (offs >= 0 && offs < PicoCDBuffers)
74 if (offs == 0) dprintf("CD buffer seek to old %i -> %i\n", prev_lba, lba);
75 memcpy32(dest, (int *)(cd_buffer + offs*2048), 2048/4);
79 if (prev_lba + PicoCDBuffers != lba)
81 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
82 dprintf("CD buffer seek %i -> %i\n", prev_lba, lba);
83 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
86 dprintf("CD buffer miss %i -> %i\n", prev_lba, lba);
88 if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
90 read_len = prev_lba - lba;
91 dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
92 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
97 read_len = PicoCDBuffers;
100 if (PicoMessage != NULL && read_len >= 512)
102 PicoMessage("Buffering data...");
108 for (i = 0; i < read_len; i++)
110 pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
111 pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
116 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
118 memcpy32(dest, (int *) cd_buffer, 2048/4);
123 /* file pointer must point to the same data in file, as would-be data after our buffer */
125 lba += PicoCDBuffers;
126 where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
127 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);