CHD EOL normalization and cleanup

This commit is contained in:
Zapeth 2017-09-02 16:06:08 +02:00
parent a44baaefa8
commit 790a3d3478
2 changed files with 168 additions and 172 deletions

View File

@ -116,7 +116,7 @@ bool CDAccess_CHD::ImageOpen(const char *path, bool image_memcache)
// should this be treated as a fatal error?
log_cb(RETRO_LOG_WARN, "chd tracks are out of order, missing a track or contain a duplicate!\n");
}
if (strncmp(type, "MODE2_RAW", 9) == 0)
{
Tracks[tkid].DIFormat = DI_FORMAT_MODE2_RAW;
@ -129,26 +129,26 @@ bool CDAccess_CHD::ImageOpen(const char *path, bool image_memcache)
Tracks[tkid].RawAudioMSBFirst = true;
}
Tracks[tkid].pregap = (tkid == 1) ? 150 : (pgtype[0] == 'V') ? 0 : pregap;
Tracks[tkid].pregap = (tkid == 1) ? 150 : (pgtype[0] == 'V') ? 0 : pregap;
Tracks[tkid].pregap_dv = (pgtype[0] == 'V') ? pregap : 0;
plba += Tracks[tkid].pregap + Tracks[tkid].pregap_dv;
Tracks[tkid].LBA = plba;
Tracks[tkid].postgap = postgap;
Tracks[tkid].sectors = frames - Tracks[tkid].pregap_dv;
Tracks[tkid].SubchannelMode = 0;
Tracks[tkid].index[0] = -1;
plba += Tracks[tkid].pregap + Tracks[tkid].pregap_dv;
Tracks[tkid].LBA = plba;
Tracks[tkid].postgap = postgap;
Tracks[tkid].sectors = frames - Tracks[tkid].pregap_dv;
Tracks[tkid].SubchannelMode = 0;
Tracks[tkid].index[0] = -1;
Tracks[tkid].index[1] = 0;
fileOffset += Tracks[tkid].pregap_dv;
//printf("Tracks[%d].fileOffset=%d\n",NumTracks, fileOffset);
Tracks[tkid].FileOffset = fileOffset;
fileOffset += frames - Tracks[tkid].pregap_dv;
fileOffset += Tracks[tkid].postgap;
fileOffset += ((frames + 3) & ~3) - frames;
plba += frames - Tracks[tkid].pregap_dv;
plba += Tracks[tkid].postgap;
fileOffset += Tracks[tkid].pregap_dv;
//printf("Tracks[%d].fileOffset=%d\n",NumTracks, fileOffset);
Tracks[tkid].FileOffset = fileOffset;
fileOffset += frames - Tracks[tkid].pregap_dv;
fileOffset += Tracks[tkid].postgap;
fileOffset += ((frames + 3) & ~3) - frames;
plba += frames - Tracks[tkid].pregap_dv;
plba += Tracks[tkid].postgap;
total_sectors += (tkid == 1) ? frames : frames + Tracks[tkid].pregap;
if (tkid < FirstTrack)
@ -333,105 +333,105 @@ int32_t CDAccess_CHD::MakeSubPQ(int32 lba, uint8 *SubPWBuf)
bool CDAccess_CHD::Read_Raw_Sector(uint8 *buf, int32 lba)
{
uint8_t SimuQ[0xC];
int32_t track;
CDRFILE_TRACK_INFO *ct;
//
// Leadout synthesis
//
if (lba >= total_sectors)
{
uint8_t data_synth_mode = 0x01; // Default for DISC_TYPE_CDDA_OR_M1, would be 0x02 for DISC_TYPE_CD_XA
switch (Tracks[LastTrack].DIFormat)
{
case DI_FORMAT_AUDIO:
break;
case DI_FORMAT_MODE1_RAW:
case DI_FORMAT_MODE1:
data_synth_mode = 0x01;
break;
case DI_FORMAT_MODE2_RAW:
case DI_FORMAT_MODE2_FORM1:
case DI_FORMAT_MODE2_FORM2:
case DI_FORMAT_MODE2:
data_synth_mode = 0x02;
break;
}
synth_leadout_sector_lba(data_synth_mode, ptoc, lba, buf);
}
memset(buf + 2352, 0, 96);
track = MakeSubPQ(lba, buf + 2352);
subq_deinterleave(buf + 2352, SimuQ);
ct = &Tracks[track];
//
// Handle pregap and postgap reading
//
if (lba < (ct->LBA - ct->pregap_dv) || lba >= (ct->LBA + ct->sectors))
{
int32_t pg_offset = lba - ct->LBA;
CDRFILE_TRACK_INFO *et = ct;
if (pg_offset < -150)
{
if ((Tracks[track].subq_control & SUBQ_CTRLF_DATA) && (FirstTrack < track) && !(Tracks[track - 1].subq_control & SUBQ_CTRLF_DATA))
et = &Tracks[track - 1];
}
memset(buf, 0, 2352);
switch (et->DIFormat)
{
case DI_FORMAT_AUDIO:
break;
case DI_FORMAT_MODE1_RAW:
case DI_FORMAT_MODE1:
encode_mode1_sector(lba + 150, buf);
break;
case DI_FORMAT_MODE2_RAW:
case DI_FORMAT_MODE2_FORM1:
case DI_FORMAT_MODE2_FORM2:
case DI_FORMAT_MODE2:
buf[12 + 6] = 0x20;
buf[12 + 10] = 0x20;
encode_mode2_form2_sector(lba + 150, buf);
// TODO: Zero out optional(?) checksum bytes?
break;
}
printf("Pre/post-gap read, LBA=%d(LBA-track_start_LBA=%d)\n", lba, lba - ct->LBA);
}
else
{
// read CHD hunk
const chd_header *head = chd_get_header(chd);
//int cad = (((lba - ct->LBA) * 2352) + ct->FileOffset) / 2352;
int cad = lba - ct->LBA + ct->FileOffset;
int sph = head->hunkbytes / (2352 + 96);
int hunknum = cad / sph; //(cad * head->unitbytes) / head->hunkbytes;
int hunkofs = cad % sph; //(cad * head->unitbytes) % head->hunkbytes;
int err = CHDERR_NONE;
/* each hunk holds ~8 sectors, optimize when reading contiguous sectors */
if (hunknum != oldhunk)
{
err = chd_read(chd, hunknum, hunkmem);
if (err != CHDERR_NONE)
log_cb(RETRO_LOG_ERROR, "chd_read_sector failed lba=%d error=%d\n", lba, err);
else
oldhunk = hunknum;
}
memcpy(buf, hunkmem + hunkofs * (2352 + 96), 2352);
if (ct->DIFormat == DI_FORMAT_AUDIO && ct->RawAudioMSBFirst)
Endian_A16_Swap(buf, 588 * 2);
uint8_t SimuQ[0xC];
int32_t track;
CDRFILE_TRACK_INFO *ct;
//
// Leadout synthesis
//
if (lba >= total_sectors)
{
uint8_t data_synth_mode = 0x01; // Default for DISC_TYPE_CDDA_OR_M1, would be 0x02 for DISC_TYPE_CD_XA
switch (Tracks[LastTrack].DIFormat)
{
case DI_FORMAT_AUDIO:
break;
case DI_FORMAT_MODE1_RAW:
case DI_FORMAT_MODE1:
data_synth_mode = 0x01;
break;
case DI_FORMAT_MODE2_RAW:
case DI_FORMAT_MODE2_FORM1:
case DI_FORMAT_MODE2_FORM2:
case DI_FORMAT_MODE2:
data_synth_mode = 0x02;
break;
}
synth_leadout_sector_lba(data_synth_mode, ptoc, lba, buf);
}
memset(buf + 2352, 0, 96);
track = MakeSubPQ(lba, buf + 2352);
subq_deinterleave(buf + 2352, SimuQ);
ct = &Tracks[track];
//
// Handle pregap and postgap reading
//
if (lba < (ct->LBA - ct->pregap_dv) || lba >= (ct->LBA + ct->sectors))
{
int32_t pg_offset = lba - ct->LBA;
CDRFILE_TRACK_INFO *et = ct;
if (pg_offset < -150)
{
if ((Tracks[track].subq_control & SUBQ_CTRLF_DATA) && (FirstTrack < track) && !(Tracks[track - 1].subq_control & SUBQ_CTRLF_DATA))
et = &Tracks[track - 1];
}
memset(buf, 0, 2352);
switch (et->DIFormat)
{
case DI_FORMAT_AUDIO:
break;
case DI_FORMAT_MODE1_RAW:
case DI_FORMAT_MODE1:
encode_mode1_sector(lba + 150, buf);
break;
case DI_FORMAT_MODE2_RAW:
case DI_FORMAT_MODE2_FORM1:
case DI_FORMAT_MODE2_FORM2:
case DI_FORMAT_MODE2:
buf[12 + 6] = 0x20;
buf[12 + 10] = 0x20;
encode_mode2_form2_sector(lba + 150, buf);
// TODO: Zero out optional(?) checksum bytes?
break;
}
printf("Pre/post-gap read, LBA=%d(LBA-track_start_LBA=%d)\n", lba, lba - ct->LBA);
}
else
{
// read CHD hunk
const chd_header *head = chd_get_header(chd);
//int cad = (((lba - ct->LBA) * 2352) + ct->FileOffset) / 2352;
int cad = lba - ct->LBA + ct->FileOffset;
int sph = head->hunkbytes / (2352 + 96);
int hunknum = cad / sph; //(cad * head->unitbytes) / head->hunkbytes;
int hunkofs = cad % sph; //(cad * head->unitbytes) % head->hunkbytes;
int err = CHDERR_NONE;
/* each hunk holds ~8 sectors, optimize when reading contiguous sectors */
if (hunknum != oldhunk)
{
err = chd_read(chd, hunknum, hunkmem);
if (err != CHDERR_NONE)
log_cb(RETRO_LOG_ERROR, "chd_read_sector failed lba=%d error=%d\n", lba, err);
else
oldhunk = hunknum;
}
memcpy(buf, hunkmem + hunkofs * (2352 + 96), 2352);
if (ct->DIFormat == DI_FORMAT_AUDIO && ct->RawAudioMSBFirst)
Endian_A16_Swap(buf, 588 * 2);
}
return true;
}

View File

@ -1,55 +1,51 @@
#ifndef __MDFN_CDACCESS_CHD_H
#define __MDFN_CDACCESS_CHD_H
#include "CDAccess.h"
#include "CDAccess_Image.h"
#include "chd.h"
class CDAccess_CHD : public CDAccess
{
public:
CDAccess_CHD(const char *path, bool image_memcache);
virtual ~CDAccess_CHD();
virtual bool Read_Raw_Sector(uint8_t *buf, int32_t lba);
virtual bool Read_TOC(TOC *toc);
virtual void Eject(bool eject_status);
private:
chd_file *chd;
/* hunk data cache */
uint8_t *hunkmem;
/* last hunknum read */
int oldhunk;
int32_t NumTracks;
int32_t FirstTrack;
int32_t LastTrack;
int32_t total_sectors;
TOC* ptoc;
std::string sbi_path;
uint32_t discs_start_offset[5];
uint32_t psisoimg_offset;
uint16_t fixed_sectors;
bool ImageOpen(const char *path, bool image_memcache);
int LoadSBI(const char* sbi_path);
void Cleanup(void);
CDRFILE_TRACK_INFO Tracks[100]; // Track #0(HMM?) through 99
struct cpp11_array_doodad
{
uint8 data[12];
};
std::map<uint32, cpp11_array_doodad> SubQReplaceMap;
int32_t MakeSubPQ(int32 lba, uint8 *SubPWBuf);
};
#endif
#ifndef __MDFN_CDACCESS_CHD_H
#define __MDFN_CDACCESS_CHD_H
#include "CDAccess.h"
#include "CDAccess_Image.h"
#include "chd.h"
class CDAccess_CHD : public CDAccess
{
public:
CDAccess_CHD(const char *path, bool image_memcache);
virtual ~CDAccess_CHD();
virtual bool Read_Raw_Sector(uint8_t *buf, int32_t lba);
virtual bool Read_TOC(TOC *toc);
virtual void Eject(bool eject_status);
private:
chd_file *chd;
/* hunk data cache */
uint8_t *hunkmem;
/* last hunknum read */
int oldhunk;
int32_t NumTracks;
int32_t FirstTrack;
int32_t LastTrack;
int32_t total_sectors;
TOC* ptoc;
std::string sbi_path;
bool ImageOpen(const char *path, bool image_memcache);
int LoadSBI(const char* sbi_path);
void Cleanup(void);
CDRFILE_TRACK_INFO Tracks[100]; // Track #0(HMM?) through 99
struct cpp11_array_doodad
{
uint8 data[12];
};
std::map<uint32, cpp11_array_doodad> SubQReplaceMap;
int32_t MakeSubPQ(int32 lba, uint8 *SubPWBuf);
};
#endif