cd: switch to CD controller code from genplus
[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"
e71fae1f 10#include "../cd/cue.h"
8b71d0eb 11
8b71d0eb 12int PicoCDBuffers = 0;
13static unsigned char *cd_buffer = NULL;
14static int prev_lba = 0x80000000;
15
16static int hits, reads;
17
9c9cda8c 18#undef dprintf
19#define dprintf(...)
8b71d0eb 20
21void PicoCDBufferInit(void)
22{
ca482e5d 23 void *tmp = NULL;
8b71d0eb 24
25 prev_lba = 0x80000000;
26 hits = reads = 0;
27
28 if (PicoCDBuffers <= 1) {
29 PicoCDBuffers = 0;
a5f80ce4 30 return; /* buffering off */
8b71d0eb 31 }
32
33 /* try alloc'ing until we succeed */
34 while (PicoCDBuffers > 0)
35 {
8022f53d 36 tmp = realloc(cd_buffer, PicoCDBuffers * 2048 + 304);
8b71d0eb 37 if (tmp != NULL) break;
38 PicoCDBuffers >>= 1;
39 }
40
a5f80ce4 41 if (PicoCDBuffers <= 0) return; /* buffering became off */
8b71d0eb 42
a5f80ce4 43 cd_buffer = tmp;
8b71d0eb 44}
45
46
47void PicoCDBufferFree(void)
48{
49 if (cd_buffer) {
50 free(cd_buffer);
51 cd_buffer = NULL;
52 }
53 if (reads)
c6196c0f 54 elprintf(EL_STATUS, "CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
8b71d0eb 55}
56
57
c9e1affc 58void PicoCDBufferFlush(void)
59{
60 prev_lba = 0x80000000;
61}
62
63
64/* this is was a try to fight slow SD access of GP2X */
eff55556 65PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba)
8b71d0eb 66{
66fdc0f0 67 int is_bin, offs, read_len, moved = 0;
8b71d0eb 68 reads++;
69
e71fae1f 70 is_bin = Pico_mcd->TOC.Tracks[0].ftype == CT_BIN;
8b71d0eb 71
72 if (PicoCDBuffers <= 0)
73 {
74 /* no buffering */
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);
78 return;
79 }
80
81 /* hit? */
82 offs = lba - prev_lba;
83 if (offs >= 0 && offs < PicoCDBuffers)
84 {
85 hits++;
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);
88 return;
89 }
90
91 if (prev_lba + PicoCDBuffers != lba)
92 {
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);
96 }
97
98 dprintf("CD buffer miss %i -> %i\n", prev_lba, lba);
99
100 if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
101 {
8b71d0eb 102 read_len = prev_lba - lba;
66fdc0f0 103 dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
8b71d0eb 104 memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
66fdc0f0 105 moved = 1;
8b71d0eb 106 }
107 else
108 {
109 read_len = PicoCDBuffers;
110 }
111
66fdc0f0 112 if (PicoMessage != NULL && read_len >= 512)
113 {
114 PicoMessage("Buffering data...");
115 }
116
8b71d0eb 117 if (is_bin)
118 {
8022f53d 119 int i = 0;
9c9cda8c 120#ifdef _PSP_FW_VERSION
b542be46 121 int bufs = (read_len*2048) / (2048+304);
8022f53d 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);
126#endif
b542be46 127 for (; i < read_len - 1; i++)
8b71d0eb 128 {
8022f53d 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
8b71d0eb 131 }
b542be46 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);
8b71d0eb 135 }
136 else
137 {
138 pm_read(cd_buffer, read_len*2048, Pico_mcd->TOC.Tracks[0].F);
139 }
140 memcpy32(dest, (int *) cd_buffer, 2048/4);
141 prev_lba = lba;
66fdc0f0 142
143 if (moved)
144 {
145 /* file pointer must point to the same data in file, as would-be data after our buffer */
146 int where_seek;
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);
150 }
8b71d0eb 151}
152