X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=Pico%2Fcd%2Fcd_file.c;h=9c898a920272452023a24ea41f9ce7d9be6241d7;hb=8f8fe01e049d9963523928a910eb4f83aac1da54;hp=06141402fd806ca21062135c0277c8e330ab7134;hpb=75736070161d40608ba3052b4c95b42943f9de3d;p=picodrive.git diff --git a/Pico/cd/cd_file.c b/Pico/cd/cd_file.c index 0614140..9c898a9 100644 --- a/Pico/cd/cd_file.c +++ b/Pico/cd/cd_file.c @@ -1,48 +1,40 @@ -#include -#include "cd_file.h" +/*********************************************************** + * * + * This source was taken from the Gens project * + * Written by Stéphane Dallongeville * + * Copyright (c) 2002 by Stéphane Dallongeville * + * Modified/adapted for PicoDrive by notaz, 2007 * + * * + ***********************************************************/ #include "../PicoInt.h" +#include "cd_file.h" -#define cdprintf dprintf -//#define cdprintf(x...) +#define cdprintf(x...) +//#define cdprintf(f,...) printf(f "\n",##__VA_ARGS__) // tmp #define DEBUG_CD - -void FILE_End(void) +PICO_INTERNAL int Load_ISO(const char *iso_name, int is_bin) { - Unload_ISO(); -} - - -int Load_ISO(const char *iso_name, int is_bin) -{ - struct stat file_stat; int i, j, num_track, Cur_LBA, index, ret, iso_name_len; _scd_track *Tracks = Pico_mcd->TOC.Tracks; - FILE *tmp_file; char tmp_name[1024], tmp_ext[10]; + pm_file *pmf; static char *exts[] = { "%02d.mp3", " %02d.mp3", "-%02d.mp3", "_%02d.mp3", " - %02d.mp3", "%d.mp3", " %d.mp3", "-%d.mp3", "_%d.mp3", " - %d.mp3", +#if CASE_SENSITIVE_FS "%02d.MP3", " %02d.MP3", "-%02d.MP3", "_%02d.MP3", " - %02d.MP3", - /* "%02d.wav", " %02d.wav", "-%02d.wav", "_%02d.wav", " - %02d.wav", - "%d.wav", " %d.wav", "-%d.wav", "_%d.wav", " - %2d.wav" */ +#endif }; + if (PicoCDLoadProgressCB != NULL) PicoCDLoadProgressCB(1); + Unload_ISO(); Tracks[0].ftype = is_bin ? TYPE_BIN : TYPE_ISO; - ret = stat(iso_name, &file_stat); - if (ret != 0) return -1; - - Tracks[0].Length = file_stat.st_size; - - if (Tracks[0].ftype == TYPE_ISO) Tracks[0].Length >>= 11; // size in sectors - else Tracks[0].Length /= 2352; // size in sectors - - - Tracks[0].F = fopen(iso_name, "rb"); + Tracks[0].F = pmf = pm_open(iso_name); if (Tracks[0].F == NULL) { Tracks[0].ftype = 0; @@ -50,11 +42,9 @@ int Load_ISO(const char *iso_name, int is_bin) return -1; } - if (Tracks[0].ftype == TYPE_ISO) fseek(Tracks[0].F, 0x100, SEEK_SET); - else fseek(Tracks[0].F, 0x110, SEEK_SET); - - // fread(buf, 1, 0x200, Tracks[0].F); - fseek(Tracks[0].F, 0, SEEK_SET); + if (Tracks[0].ftype == TYPE_ISO) + Tracks[0].Length = pmf->size >>= 11; // size in sectors + else Tracks[0].Length = pmf->size /= 2352; Tracks[0].MSF.M = 0; // minutes Tracks[0].MSF.S = 2; // seconds @@ -64,14 +54,18 @@ int Load_ISO(const char *iso_name, int is_bin) Cur_LBA = Tracks[0].Length; // Size in sectors - strcpy(tmp_name, iso_name); iso_name_len = strlen(iso_name); + if (iso_name_len >= sizeof(tmp_name)) + iso_name_len = sizeof(tmp_name) - 1; for (num_track = 2, i = 0; i < 100; i++) { - for(j = 0; j < sizeof(exts)/sizeof(char *); j++) + if (PicoCDLoadProgressCB != NULL && i > 1) PicoCDLoadProgressCB(i); + + for (j = 0; j < sizeof(exts)/sizeof(char *); j++) { int ext_len; + FILE *tmp_file; sprintf(tmp_ext, exts[j], i); ext_len = strlen(tmp_ext); @@ -88,19 +82,29 @@ int Load_ISO(const char *iso_name, int is_bin) if (tmp_file) { - // float fs; int fs; index = num_track - 1; - ret = stat(tmp_name, &file_stat); - fs = file_stat.st_size; // used to calculate lenght + ret = fseek(tmp_file, 0, SEEK_END); + fs = ftell(tmp_file); // used to calculate lenght + fseek(tmp_file, 0, SEEK_SET); +#if DONT_OPEN_MANY_FILES + // some systems (like PSP) can't have many open files at a time, + // so we work with their names instead. + fclose(tmp_file); + tmp_file = (void *) strdup(tmp_name); +#endif Tracks[index].KBtps = (short) mp3_get_bitrate(tmp_file, fs); Tracks[index].KBtps >>= 3; if (ret != 0 || Tracks[index].KBtps <= 0) { - cdprintf("Error track %i: stat %i, rate %i", index, ret, Tracks[index].KBtps); + cdprintf("Error track %i: rate %i", index, Tracks[index].KBtps); +#if !DONT_OPEN_MANY_FILES fclose(tmp_file); +#else + free(tmp_file); +#endif continue; } @@ -133,44 +137,41 @@ int Load_ISO(const char *iso_name, int is_bin) cdprintf("End CD - %02d:%02d:%02d\n\n", Tracks[index].MSF.M, Tracks[index].MSF.S, Tracks[index].MSF.F); + if (PicoCDLoadProgressCB != NULL) PicoCDLoadProgressCB(100); + return 0; } -void Unload_ISO(void) +PICO_INTERNAL void Unload_ISO(void) { int i; if (Pico_mcd == NULL) return; - for(i = 0; i < 100; i++) + if (Pico_mcd->TOC.Tracks[0].F) pm_close(Pico_mcd->TOC.Tracks[0].F); + + for(i = 1; i < 100; i++) { - if (Pico_mcd->TOC.Tracks[i].F) fclose(Pico_mcd->TOC.Tracks[i].F); - Pico_mcd->TOC.Tracks[i].F = NULL; - Pico_mcd->TOC.Tracks[i].Length = 0; - Pico_mcd->TOC.Tracks[i].ftype = 0; + if (Pico_mcd->TOC.Tracks[i].F != NULL) +#if !DONT_OPEN_MANY_FILES + fclose(Pico_mcd->TOC.Tracks[i].F); +#else + free(Pico_mcd->TOC.Tracks[i].F); +#endif } + memset(Pico_mcd->TOC.Tracks, 0, sizeof(Pico_mcd->TOC.Tracks)); } -int FILE_Read_One_LBA_CDC(void) +PICO_INTERNAL int FILE_Read_One_LBA_CDC(void) { - int where_read = 0; // static char cp_buf[2560]; if (Pico_mcd->s68k_regs[0x36] & 1) // DATA { if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1; - if (Pico_mcd->scd.Cur_LBA < 0) - where_read = 0; - else if (Pico_mcd->scd.Cur_LBA >= Pico_mcd->TOC.Tracks[0].Length) - where_read = Pico_mcd->TOC.Tracks[0].Length - 1; - else where_read = Pico_mcd->scd.Cur_LBA; - - if (Pico_mcd->TOC.Tracks[0].ftype == TYPE_ISO) where_read <<= 11; - else where_read = (where_read * 2352 + 16); - // moved below.. //fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET); //fread(cp_buf, 1, 2048, Pico_mcd->TOC.Tracks[0].F); @@ -181,7 +182,7 @@ int FILE_Read_One_LBA_CDC(void) { // int rate, channel; - if (Pico_mcd->TOC.Tracks[Pico_mcd->scd.Cur_Track - 1].ftype == TYPE_MP3) + // if (Pico_mcd->TOC.Tracks[Pico_mcd->scd.Cur_Track - 1].ftype == TYPE_MP3) { // TODO // MP3_Update(cp_buf, &rate, &channel, 0); @@ -201,18 +202,27 @@ int FILE_Read_One_LBA_CDC(void) { if (Pico_mcd->cdc.CTRL.B.B0 & 0x04) // WRRQ : this bit enable write to buffer { + int where_read = 0; + // CAUTION : lookahead bit not implemented + if (Pico_mcd->scd.Cur_LBA < 0) + where_read = 0; + else if (Pico_mcd->scd.Cur_LBA >= Pico_mcd->TOC.Tracks[0].Length) + where_read = Pico_mcd->TOC.Tracks[0].Length - 1; + else where_read = Pico_mcd->scd.Cur_LBA; + Pico_mcd->scd.Cur_LBA++; Pico_mcd->cdc.WA.N = (Pico_mcd->cdc.WA.N + 2352) & 0x7FFF; // add one sector to WA Pico_mcd->cdc.PT.N = (Pico_mcd->cdc.PT.N + 2352) & 0x7FFF; - memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], &Pico_mcd->cdc.HEAD, 4); + *(unsigned int *)(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N) = Pico_mcd->cdc.HEAD.N; //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N + 4], cp_buf, 2048); - fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET); - fread(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 1, 2048, Pico_mcd->TOC.Tracks[0].F); + //pm_seek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET); + //pm_read(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, 2048, Pico_mcd->TOC.Tracks[0].F); + PicoCDBufferRead(Pico_mcd->cdc.Buffer + Pico_mcd->cdc.PT.N + 4, where_read); #ifdef DEBUG_CD cdprintf("Read -> WA = %d Buffer[%d] =", Pico_mcd->cdc.WA.N, Pico_mcd->cdc.PT.N & 0x3FFF); @@ -268,7 +278,7 @@ int FILE_Read_One_LBA_CDC(void) { if (Pico_mcd->s68k_regs[0x33] & (1<<5)) { - dprintf("cdc dec irq 5"); + elprintf(EL_INTS, "cdc dec irq 5"); SekInterruptS68k(5); } @@ -282,7 +292,7 @@ int FILE_Read_One_LBA_CDC(void) } -int FILE_Play_CD_LBA(void) +PICO_INTERNAL int FILE_Play_CD_LBA(void) { int index = Pico_mcd->scd.Cur_Track - 1; Pico_mcd->m.audio_track = index;