UIQ3 update, some makefile unification, rm old configs, stuff
[picodrive.git] / Pico / cd / buffering.c
index 5afc6e8..e0e888d 100644 (file)
@@ -1,6 +1,7 @@
-#include "../PicoInt.h"
+// Buffering handling
+// (c) Copyright 2007, Grazvydas "notaz" Ignotas
 
-//#include <stdlib.h>
+#include "../PicoInt.h"
 
 int PicoCDBuffers = 0;
 static unsigned char *cd_buffer = NULL;
@@ -11,7 +12,7 @@ static int hits, reads;
 
 void PicoCDBufferInit(void)
 {
-       void *tmp;
+       void *tmp = NULL;
 
        prev_lba = 0x80000000;
        hits = reads = 0;
@@ -24,7 +25,7 @@ void PicoCDBufferInit(void)
        /* try alloc'ing until we succeed */
        while (PicoCDBuffers > 0)
        {
-               tmp = realloc(cd_buffer, PicoCDBuffers * 2048);
+               tmp = realloc(cd_buffer, PicoCDBuffers * 2048 + 304);
                if (tmp != NULL) break;
                PicoCDBuffers >>= 1;
        }
@@ -42,14 +43,20 @@ void PicoCDBufferFree(void)
                cd_buffer = NULL;
        }
        if (reads)
-               printf("CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
+               elprintf(EL_STATUS, "CD buffer hits: %i/%i (%i%%)\n", hits, reads, hits * 100 / reads);
+}
+
+
+void PicoCDBufferFlush(void)
+{
+       prev_lba = 0x80000000;
 }
 
 
-/* this is a try to fight slow SD access of GP2X */
-void PicoCDBufferRead(void *dest, int lba)
+/* this is was a try to fight slow SD access of GP2X */
+PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba)
 {
-       int is_bin, offs, read_len;
+       int is_bin, offs, read_len, moved = 0;
        reads++;
 
        is_bin = Pico_mcd->TOC.Tracks[0].ftype == TYPE_BIN;
@@ -84,23 +91,39 @@ void PicoCDBufferRead(void *dest, int lba)
 
        if (lba < prev_lba && prev_lba - lba < PicoCDBuffers)
        {
-               dprintf("CD buffer move");
                read_len = prev_lba - lba;
+               dprintf("CD buffer move=%i, read_len=%i", PicoCDBuffers - read_len, read_len);
                memmove(cd_buffer + read_len*2048, cd_buffer, (PicoCDBuffers - read_len)*2048);
+               moved = 1;
        }
        else
        {
                read_len = PicoCDBuffers;
        }
 
+       if (PicoMessage != NULL && read_len >= 512)
+       {
+               PicoMessage("Buffering data...");
+       }
+
        if (is_bin)
        {
-               int i;
-               for (i = 0; i < read_len; i++)
+               int i = 0;
+#if REDUCE_IO_CALLS
+               int bufs = (read_len*2048) / (2048+304);
+               pm_read(cd_buffer, bufs*(2048+304), Pico_mcd->TOC.Tracks[0].F);
+               for (i = 1; i < bufs; i++)
+                       // should really use memmove here, but my memcpy32 implementation is also suitable here
+                       memcpy32((int *)(cd_buffer + i*2048), (int *)(cd_buffer + i*(2048+304)), 2048/4);
+#endif
+               for (; i < read_len - 1; i++)
                {
-                       pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
-                       pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
+                       pm_read(cd_buffer + i*2048, 2048 + 304, Pico_mcd->TOC.Tracks[0].F);
+                       // pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR); // seeking is slower, in PSP case even more
                }
+               // further data might be moved, do not overwrite
+               pm_read(cd_buffer + i*2048, 2048, Pico_mcd->TOC.Tracks[0].F);
+               pm_seek(Pico_mcd->TOC.Tracks[0].F, 304, SEEK_CUR);
        }
        else
        {
@@ -108,5 +131,14 @@ void PicoCDBufferRead(void *dest, int lba)
        }
        memcpy32(dest, (int *) cd_buffer, 2048/4);
        prev_lba = lba;
+
+       if (moved)
+       {
+               /* file pointer must point to the same data in file, as would-be data after our buffer */
+               int where_seek;
+               lba += PicoCDBuffers;
+               where_seek = is_bin ? (lba * 2352 + 16) : (lba << 11);
+               pm_seek(Pico_mcd->TOC.Tracks[0].F, where_seek, SEEK_SET);
+       }
 }