X-Git-Url: https://notaz.gp2x.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=Pico%2Fcd%2Fcd_file.c;h=97623616f5a3df06f35095cf43124643520feac6;hb=b5e5172d049a83251874fb33e65a53c9654dc0bc;hp=4f11ba57ebbb66dd3a8b81e379fd05c070ddbae6;hpb=c459aefdad169c97b6fb93a20c1219a76aa07505;p=picodrive.git diff --git a/Pico/cd/cd_file.c b/Pico/cd/cd_file.c index 4f11ba5..9762361 100644 --- a/Pico/cd/cd_file.c +++ b/Pico/cd/cd_file.c @@ -1,143 +1,113 @@ -/* -#include -#include -#if defined(__WIN__) -#include -#else -#include "port.h" -#endif -#include "cd_sys.h" -#include "cd_file.h" -#include "lc89510.h" -#include "cdda_mp3.h" -#include "star_68k.h" -#include "rom.h" -#include "mem_s68k.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 -#include "cd_file.h" #include "../PicoInt.h" +#include "cd_file.h" #define cdprintf dprintf //#define cdprintf(x...) #define DEBUG_CD -struct _file_track Tracks[100]; -char Track_Played; - -int FILE_Init(void) +PICO_INTERNAL int Load_ISO(const char *iso_name, int is_bin) { -// MP3_Init(); // TODO - Unload_ISO(); - - return 0; -} - - -void FILE_End(void) -{ - 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; - _scd_track *SCD_TOC_Tracks = Pico_mcd->scd.TOC.Tracks; - FILE *tmp_file; + int i, j, num_track, Cur_LBA, index, ret, iso_name_len; + _scd_track *Tracks = Pico_mcd->TOC.Tracks; 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", + "%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" */ }; Unload_ISO(); - Tracks[0].Type = is_bin ? TYPE_BIN : TYPE_ISO; - - ret = stat(iso_name, &file_stat); - if (ret != 0) return -1; - - Tracks[0].Lenght = file_stat.st_size; + Tracks[0].ftype = is_bin ? TYPE_BIN : TYPE_ISO; - if (Tracks[0].Type == TYPE_ISO) Tracks[0].Lenght >>= 11; // size in sectors - else Tracks[0].Lenght /= 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].Type = 0; - Tracks[0].Lenght = 0; + Tracks[0].ftype = 0; + Tracks[0].Length = 0; return -1; } - if (Tracks[0].Type == 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); - - Pico_mcd->scd.TOC.First_Track = 1; - - SCD_TOC_Tracks[0].Num = 1; - SCD_TOC_Tracks[0].Type = 1; // DATA + if (Tracks[0].ftype == TYPE_ISO) + Tracks[0].Length = pmf->size >>= 11; // size in sectors + else Tracks[0].Length = pmf->size /= 2352; - SCD_TOC_Tracks[0].MSF.M = 0; - SCD_TOC_Tracks[0].MSF.S = 2; - SCD_TOC_Tracks[0].MSF.F = 0; + Tracks[0].MSF.M = 0; // minutes + Tracks[0].MSF.S = 2; // seconds + Tracks[0].MSF.F = 0; // frames - cdprintf("Track 0 - %02d:%02d:%02d %s", SCD_TOC_Tracks[0].MSF.M, SCD_TOC_Tracks[0].MSF.S, SCD_TOC_Tracks[0].MSF.F, - SCD_TOC_Tracks[0].Type ? "DATA" : "AUDIO"); + cdprintf("Track 0 - %02d:%02d:%02d DATA", Tracks[0].MSF.M, Tracks[0].MSF.S, Tracks[0].MSF.F); - Cur_LBA = Tracks[0].Lenght; // Size in sectors + Cur_LBA = Tracks[0].Length; // Size in sectors - strcpy(tmp_name, iso_name); + iso_name_len = strlen(iso_name); - for(num_track = 2, i = 0; i < 100; i++) + for (num_track = 2, i = 0; i < 100; i++) { - if (sizeof(exts)/sizeof(char *) != 10) { printf("eee"); exit(1); } - for(j = 0; j < sizeof(exts)/sizeof(char *); j++) { - tmp_name[strlen(iso_name) - 4] = 0; + int ext_len; + FILE *tmp_file; sprintf(tmp_ext, exts[j], i); + ext_len = strlen(tmp_ext); + + memcpy(tmp_name, iso_name, iso_name_len + 1); + tmp_name[iso_name_len - 4] = 0; strcat(tmp_name, tmp_ext); tmp_file = fopen(tmp_name, "rb"); + if (!tmp_file && i > 1 && iso_name_len > ext_len) { + tmp_name[iso_name_len - ext_len] = 0; + strcat(tmp_name, tmp_ext); + tmp_file = fopen(tmp_name, "rb"); + } if (tmp_file) { - float fs; - index = num_track - Pico_mcd->scd.TOC.First_Track; - - stat(tmp_name, &file_stat); - - fs = (float) file_stat.st_size; // used to calculate lenght + int fs; + struct stat file_stat; + index = num_track - 1; + + ret = stat(tmp_name, &file_stat); + fs = file_stat.st_size; // used to calculate lenght + + 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); + fclose(tmp_file); + continue; + } Tracks[index].F = tmp_file; - SCD_TOC_Tracks[index].Num = num_track; - SCD_TOC_Tracks[index].Type = 0; // AUDIO - - LBA_to_MSF(Cur_LBA, &(SCD_TOC_Tracks[index].MSF)); - - cdprintf("Track %i - %02d:%02d:%02d %s", index, SCD_TOC_Tracks[index].MSF.M, - SCD_TOC_Tracks[index].MSF.S, SCD_TOC_Tracks[index].MSF.F, - SCD_TOC_Tracks[index].Type ? "DATA" : "AUDIO"); + LBA_to_MSF(Cur_LBA, &Tracks[index].MSF); // MP3 File - Tracks[index].Type = TYPE_MP3; - fs /= (128>>3); // (float) (MP3_Get_Bitrate(Tracks[num_track - 1].F) >> 3); + Tracks[index].ftype = TYPE_MP3; fs *= 75; - Tracks[index].Lenght = (int) fs; - Cur_LBA += Tracks[index].Lenght; + fs /= Tracks[index].KBtps * 1000; + Tracks[index].Length = fs; + Cur_LBA += Tracks[index].Length; + + cdprintf("Track %i: %s - %02d:%02d:%02d len=%i AUDIO", index, tmp_name, Tracks[index].MSF.M, + Tracks[index].MSF.S, Tracks[index].MSF.F, fs); num_track++; break; @@ -145,55 +115,46 @@ int Load_ISO(const char *iso_name, int is_bin) } } - Pico_mcd->scd.TOC.Last_Track = num_track - 1; + Pico_mcd->TOC.Last_Track = num_track - 1; - index = num_track - Pico_mcd->scd.TOC.First_Track; - SCD_TOC_Tracks[index].Num = num_track; - SCD_TOC_Tracks[index].Type = 0; + index = num_track - 1; - LBA_to_MSF(Cur_LBA, &(SCD_TOC_Tracks[index].MSF)); + LBA_to_MSF(Cur_LBA, &Tracks[index].MSF); - cdprintf("End CD - %02d:%02d:%02d\n\n", SCD_TOC_Tracks[index].MSF.M, - SCD_TOC_Tracks[index].MSF.S, SCD_TOC_Tracks[index].MSF.F); + cdprintf("End CD - %02d:%02d:%02d\n\n", Tracks[index].MSF.M, + Tracks[index].MSF.S, Tracks[index].MSF.F); return 0; } -void Unload_ISO(void) +PICO_INTERNAL void Unload_ISO(void) { int i; - Track_Played = 99; + 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 (Tracks[i].F) fclose(Tracks[i].F); - Tracks[i].F = NULL; - Tracks[i].Lenght = 0; - Tracks[i].Type = 0; + if (Pico_mcd->TOC.Tracks[i].F) fclose(Pico_mcd->TOC.Tracks[i].F); } + 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; - static char cp_buf[2560]; +// static char cp_buf[2560]; if (Pico_mcd->s68k_regs[0x36] & 1) // DATA { - if (Tracks[0].F == NULL) return -1; - - if (Pico_mcd->scd.Cur_LBA < 0) where_read = 0; - else if (Pico_mcd->scd.Cur_LBA >= Tracks[0].Lenght) where_read = Tracks[0].Lenght - 1; - else where_read = Pico_mcd->scd.Cur_LBA; + if (Pico_mcd->TOC.Tracks[0].F == NULL) return -1; - if (Tracks[0].Type == TYPE_ISO) where_read <<= 11; - else where_read = (where_read * 2352 + 16); - - fseek(Tracks[0].F, where_read, SEEK_SET); - fread(cp_buf, 1, 2048, Tracks[0].F); + // moved below.. + //fseek(Pico_mcd->TOC.Tracks[0].F, where_read, SEEK_SET); + //fread(cp_buf, 1, 2048, Pico_mcd->TOC.Tracks[0].F); cdprintf("Read file CDC 1 data sector :\n"); } @@ -201,7 +162,7 @@ int FILE_Read_One_LBA_CDC(void) { // int rate, channel; - if (Tracks[Pico_mcd->scd.Cur_Track - Pico_mcd->scd.TOC.First_Track].Type == TYPE_MP3) + // if (Pico_mcd->TOC.Tracks[Pico_mcd->scd.Cur_Track - 1].ftype == TYPE_MP3) { // TODO // MP3_Update(cp_buf, &rate, &channel, 0); @@ -221,15 +182,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 + 4], cp_buf, 2048); - 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); + + //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); @@ -260,7 +233,7 @@ int FILE_Read_One_LBA_CDC(void) { // CAUTION : lookahead bit not implemented - memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], cp_buf, 2352); + //memcpy(&Pico_mcd->cdc.Buffer[Pico_mcd->cdc.PT.N], cp_buf, 2352); } } } @@ -289,8 +262,8 @@ int FILE_Read_One_LBA_CDC(void) SekInterruptS68k(5); } - Pico_mcd->cdc.IFSTAT &= ~0x20; // DEC interrupt happen - CDC_Decode_Reg_Read = 0; // Reset read after DEC int + Pico_mcd->cdc.IFSTAT &= ~0x20; // DEC interrupt happen + Pico_mcd->cdc.Decode_Reg_Read = 0; // Reset read after DEC int } } @@ -299,23 +272,27 @@ 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 - Pico_mcd->scd.TOC.First_Track; + int index = Pico_mcd->scd.Cur_Track - 1; + Pico_mcd->m.audio_track = index; - cdprintf("Play FILE Comp"); + cdprintf("Play track #%i", Pico_mcd->scd.Cur_Track); - if (Tracks[index].F == NULL) + if (Pico_mcd->TOC.Tracks[index].F == NULL) { return 1; } - if (Tracks[index].Type == TYPE_MP3) + if (Pico_mcd->TOC.Tracks[index].ftype == TYPE_MP3) { + int pos1024 = 0; int Track_LBA_Pos = Pico_mcd->scd.Cur_LBA - Track_to_LBA(Pico_mcd->scd.Cur_Track); if (Track_LBA_Pos < 0) Track_LBA_Pos = 0; + if (Track_LBA_Pos) + pos1024 = Track_LBA_Pos * 1024 / Pico_mcd->TOC.Tracks[index].Length; - // MP3_Play(index, Track_LBA_Pos); // TODO + mp3_start_play(Pico_mcd->TOC.Tracks[index].F, pos1024); } else {