$NetBSD: patch-ad,v 1.2 2004/08/15 12:13:53 dillo Exp $ --- src/gens/segacd/cd_file.c.orig 2004-05-18 22:34:00.000000000 +0200 +++ src/gens/segacd/cd_file.c @@ -39,7 +39,7 @@ FILE_End (void) int Load_ISO (char *buf, char *iso_name) { - FILE *File_Size; + int fmt; int i, j, num_track, Cur_LBA; FILE *tmp_file; char tmp_name[1024], tmp_ext[10]; @@ -52,41 +52,62 @@ Load_ISO (char *buf, char *iso_name) Unload_ISO (); - if (Detect_Format (iso_name) == SEGACD_IMAGE + 1) + fmt = Detect_Format (iso_name); + if (fmt == SEGACD_IMAGE + 1) Tracks[0].Type = TYPE_BIN; - else if (Detect_Format (iso_name) == SEGACD_IMAGE) + else if (fmt == SEGACD_IMAGE) Tracks[0].Type = TYPE_ISO; else return -2; - File_Size = fopen (iso_name, "rb"); - fseek (File_Size, 0, SEEK_END); - Tracks[0].Lenght = ftell (File_Size); - fseek (File_Size, 0, SEEK_SET); + if (!stricmp (".chd", &iso_name[strlen (iso_name) -4])) + { + Tracks[0].F = NULL; + Tracks[0].chd = chd_open(iso_name, NULL); + + if (Tracks[0].chd == NULL) + { + Tracks[0].Type = 0; + Tracks[0].Lenght = 0; + return -1; + } - if (Tracks[0].Type == TYPE_ISO) - Tracks[0].Lenght >>= 11; // size in sectors + Tracks[0].Lenght = Tracks[0].chd->total_len; + + if (Tracks[0].Type == TYPE_ISO) + chd_read_range(Tracks[0].chd, buf, 0x100, 0x200); + else + chd_read_range(Tracks[0].chd, buf, 0x110, 0x200); + } else - Tracks[0].Lenght /= 2352; // size in sectors + { + Tracks[0].F = fopen (iso_name, "rb"); + Tracks[0].chd = NULL; + + if (Tracks[0].F == NULL) + { + Tracks[0].Type = 0; + Tracks[0].Lenght = 0; + return -1; + } - fclose (File_Size); + fseek (Tracks[0].F, 0, SEEK_END); + Tracks[0].Lenght = ftell (Tracks[0].F); - Tracks[0].F = fopen (iso_name, "rb"); + 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); - if (Tracks[0].F == NULL) - { - Tracks[0].Type = 0; - Tracks[0].Lenght = 0; - return -1; } if (Tracks[0].Type == TYPE_ISO) - fseek (Tracks[0].F, 0x100, SEEK_SET); + Tracks[0].Lenght >>= 11; // size in sectors else - fseek (Tracks[0].F, 0x110, SEEK_SET); - - fread (buf, 1, 0x200, Tracks[0].F); - fseek (Tracks[0].F, 0, SEEK_SET); + Tracks[0].Lenght /= 2352; // size in sectors SCD.TOC.First_Track = 1; @@ -219,6 +240,10 @@ Unload_ISO (void) Track_Played = 99; + if (Tracks[0].chd) + chd_close(Tracks[0].chd); + Tracks[0].chd = NULL; + for (i = 0; i < 100; i++) { if (Tracks[i].F) @@ -237,7 +262,7 @@ FILE_Read_One_LBA_CDC (void) if (CDD.Control & 0x0100) // DATA { - if (Tracks[0].F == NULL) + if (Tracks[0].F == NULL && Tracks[0].chd == NULL) return -1; if (SCD.Cur_LBA < 0) @@ -254,8 +279,13 @@ FILE_Read_One_LBA_CDC (void) // memset(cp_buf, 0, 2048); - fseek (Tracks[0].F, where_read, SEEK_SET); - fread (cp_buf, 1, 2048, Tracks[0].F); + if (Tracks[0].chd != NULL) + chd_read_range(Tracks[0].chd, cp_buf, where_read, 2048); + else + { + fseek (Tracks[0].F, where_read, SEEK_SET); + fread (cp_buf, 1, 2048, Tracks[0].F); + } #ifdef DEBUG_CD fprintf (debug_SCD_file, "\n\nRead file CDC 1 data sector :\n");