clarify PicoDrive's license
[picodrive.git] / pico / cd / buffering.c
CommitLineData
cff531af 1/*
2 * Buffering handling
3 * (C) notaz, 2007,2008
4 *
5 * This work is licensed under the terms of MAME license.
6 * See COPYING file in the top-level directory.
7 */
6cadc2da 8
efcba75f 9#include "../pico_int.h"
8b71d0eb 10
8b71d0eb 11int PicoCDBuffers = 0;
12static unsigned char *cd_buffer = NULL;
13static int prev_lba = 0x80000000;
14
15static int hits, reads;
16
17
18void PicoCDBufferInit(void)
19{
ca482e5d 20 void *tmp = NULL;
8b71d0eb 21
22 prev_lba = 0x80000000;
23 hits = reads = 0;
24
25 if (PicoCDBuffers <= 1) {
26 PicoCDBuffers = 0;
a5f80ce4 27 return; /* buffering off */
8b71d0eb 28 }
29
30 /* try alloc'ing until we succeed */
31 while (PicoCDBuffers > 0)
32 {
8022f53d 33 tmp = realloc(cd_buffer, PicoCDBuffers * 2048 + 304);
8b71d0eb 34 if (tmp != NULL) break;
35 PicoCDBuffers >>= 1;
36 }
37
a5f80ce4 38 if (PicoCDBuffers <= 0) return; /* buffering became off */
8b71d0eb 39
a5f80ce4 40 cd_buffer = tmp;
8b71d0eb 41}
42
43
44void PicoCDBufferFree(void)
45{
46 if (cd_buffer) {
47 free(cd_buffer);
48 cd_buffer = NULL;
49 }
50 if (reads)
c6196c0f 51 elprintf(EL_STATUS, "CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
8b71d0eb 52}
53
54
c9e1affc 55void PicoCDBufferFlush(void)
56{
57 prev_lba = 0x80000000;
58}
59
60
61/* this is was a try to fight slow SD access of GP2X */
eff55556 62PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba)
8b71d0eb 63{
66fdc0f0 64 int is_bin, offs, read_len, moved = 0;
8b71d0eb 65 reads++;
66
67 is_bin = Pico_mcd->TOC.Tracks[0].ftype == TYPE_BIN;
68
69 if (PicoCDBuffers <= 0)
70 {
71 /* no buffering */
72 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
73 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
74 pm_read(dest, 2048, Pico_mcd->TOC.Tracks[0].F);
75 return;
76 }
77
78 /* hit? */
79 offs = lba - prev_lba;
80 if (offs >= 0 && offs < PicoCDBuffers)
81 {
82 hits++;
83 if (offs == 0) dprintf("CD buffer seek to old %i -> %i\n", prev_lba, lba);
84 memcpy32(dest, (int *)(cd_buffer + offs*2048), 2048/4);
85 return;
86 }
87
88 if (prev_lba + PicoCDBuffers != lba)
89 {
90 int where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
91 dprintf("CD buffer seek %i -> %i\n", prev_lba, lba);
92 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
93 }
94
95 dprintf("CD buffer miss %i -> %i\n", prev_lba, lba);
96
97 if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
98 {
8b71d0eb 99 read_len = prev_lba - lba;
66fdc0f0 100 dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
8b71d0eb 101 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
66fdc0f0 102 moved = 1;
8b71d0eb 103 }
104 else
105 {
106 read_len = PicoCDBuffers;
107 }
108
66fdc0f0 109 if (PicoMessage != NULL && read_len >= 512)
110 {
111 PicoMessage("Buffering data...");
112 }
113
8b71d0eb 114 if (is_bin)
115 {
8022f53d 116 int i = 0;
117#if REDUCE_IO_CALLS
b542be46 118 int bufs = (read_len*2048) / (2048+304);
8022f53d 119 pm_read(cd_buffer, bufs*(2048+304), Pico_mcd->TOC.Tracks[0].F);
120 for (i = 1; i < bufs; i++)
121 // should really use memmove here, but my memcpy32 implementation is also suitable here
122 memcpy32((int *)(cd_buffer + i*2048), (int *)(cd_buffer + i*(2048+304)), 2048/4);
123#endif
b542be46 124 for (; i < read_len - 1; i++)
8b71d0eb 125 {
8022f53d 126 pm_read(cd_buffer + i*2048, 2048 + 304, Pico_mcd->TOC.Tracks[0].F);
127 // pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR); // seeking is slower, in PSP case even more
8b71d0eb 128 }
b542be46 129 // further data might be moved, do not overwrite
130 pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
131 pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
8b71d0eb 132 }
133 else
134 {
135 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
136 }
137 memcpy32(dest, (int *) cd_buffer, 2048/4);
138 prev_lba = lba;
66fdc0f0 139
140 if (moved)
141 {
142 /* file pointer must point to the same data in file, as would-be data after our buffer */
143 int where_seek;
144 lba += PicoCDBuffers;
145 where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
146 pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
147 }
8b71d0eb 148}
149