Skip to content

Commit 37911b0

Browse files
committed
SCSI loading sectors
1 parent 2bdf4ae commit 37911b0

8 files changed

Lines changed: 295 additions & 57 deletions

File tree

platforms/shared/desktop/backers.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
#define BACKERS_H
2222

2323
static const char * BACKERS_STR =
24-
" · Michael Mellor (dinglyburrow)"
24+
" · Dave Shadoff (dshadoff)"
25+
"\n · Michael Mellor (dinglyburrow)"
2526
"\n · Francisco Javier Trujillo (fjtrujy)"
2627
"\n · Libretro / RetroArch team"
2728
"\n · Rupert Carmichael (carmiker)"

src/cdrom.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,9 +81,13 @@ u8 CdRom::ReadRegister(u16 address)
8181
Debug("CDROM Read Is BRAM Locked? %02X", reg);
8282
return 0x00;
8383
case 0x08:
84+
{
8485
// SCSI get data
85-
Debug("CDROM Read SCSI get data %02X", reg);
86-
return m_scsi_controller->ReadData();
86+
//Debug("+++ CDROM Read SCSI get data %02X", reg);
87+
u8 ret = m_scsi_controller->ReadData();
88+
m_scsi_controller->AutoAck();
89+
return ret;
90+
}
8791
case 0x09:
8892
case 0x0A:
8993
case 0x0B:
@@ -127,7 +131,6 @@ void CdRom::WriteRegister(u16 address, u8 value)
127131
// SCSI command
128132
//Debug("CDROM Write SCSI command %02X, value: %02X", reg, value);
129133
m_scsi_controller->WriteData(value);
130-
m_scsi_controller->BusChange();
131134
break;
132135
case 0x02:
133136
{
@@ -138,7 +141,6 @@ void CdRom::WriteRegister(u16 address, u8 value)
138141
m_scsi_controller->SetSignal(ScsiController::SCSI_SIGNAL_ACK);
139142
else
140143
m_scsi_controller->ClearSignal(ScsiController::SCSI_SIGNAL_ACK);
141-
m_scsi_controller->BusChange();
142144
break;
143145
}
144146
case 0x04:

src/cdrom_media.cpp

Lines changed: 80 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,29 +541,39 @@ bool CdRomMedia::ReadSector(u32 lba, u8* buffer)
541541
return false;
542542
}
543543

544+
u32 file_offset = 0;
544545
for (size_t i = 0; i < m_tracks.size(); i++)
545546
{
546547
const Track& track = m_tracks[i];
548+
u32 sector_size = track.sector_size;
547549
u32 start = track.start_lba;
548550
u32 end = start + track.sector_count;
549551

550552
if (lba >= start && lba < end)
551553
{
552554
u32 sector_offset = lba - start;
553-
u32 sector_size = GetTrackSectorSize(track.type);
554555
ImgFile* img_file = track.img_file;
555556

556557
if (img_file == NULL || img_file->file_size == 0)
557558
return false;
558559

559-
u64 byte_offset = sector_offset * (u64)sector_size;
560+
u64 byte_offset = file_offset + (sector_offset * sector_size);
561+
562+
if (sector_size == 2352)
563+
{
564+
byte_offset += 16;
565+
sector_size = 2048;
566+
}
567+
560568
if (byte_offset + sector_size > img_file->file_size)
561569
return false;
562570

563-
Debug("Reading sector %d from track %d (LBA: %d)", lba, track.number, byte_offset);
571+
Debug("Reading sector %d from track %d (LBA: %d)", lba, i, byte_offset);
564572

565573
return ReadFromImgFile(img_file, byte_offset, buffer, sector_size);
566574
}
575+
else
576+
file_offset += (track.sector_size * track.sector_count);
567577
}
568578

569579
Debug("ERROR: ReadSector failed - LBA %d not found in any track", lba);
@@ -657,7 +667,6 @@ bool CdRomMedia::LoadChunk(ImgFile* img_file, u32 chunk_index)
657667
}
658668

659669
file.close();
660-
Debug("Loaded chunk %d from %s", chunk_index, img_file->file_path);
661670
return true;
662671
}
663672

@@ -714,4 +723,71 @@ bool CdRomMedia::PreloadTrackChunks(u32 track_number, u32 sectors)
714723
u32 chunks_needed = (total_bytes + track.img_file->chunk_size - 1) / track.img_file->chunk_size;
715724

716725
return PreloadChunks(track.img_file, start_chunk, chunks_needed);
726+
}
727+
728+
///////////////////////////////////////////////////////////////
729+
// Seek time, based on the work by Dave Shadoff
730+
// https://github.com/pce-devel/PCECD_seek
731+
732+
u32 CdRomMedia::SeekFindGroup(u32 lba)
733+
{
734+
for (u32 i = 0; i < GG_SEEK_NUM_SECTOR_GROUPS; i++)
735+
if ((lba >= k_seek_sector_list[i].sec_start) && (lba <= k_seek_sector_list[i].sec_end))
736+
return i;
737+
return 0;
738+
}
739+
740+
u32 CdRomMedia::SeekTime(u32 start_lba, u32 end_lba)
741+
{
742+
u32 start_index = SeekFindGroup(start_lba);
743+
u32 target_index = SeekFindGroup(end_lba);
744+
u32 lba_difference = (u32)std::abs((int)end_lba - (int)start_lba);
745+
float track_difference = 0.0f;
746+
747+
// Now we find the track difference
748+
//
749+
// Note: except for the first and last sector groups, all groups are 1606.48 tracks per group.
750+
//
751+
if (target_index == start_index)
752+
{
753+
track_difference = (lba_difference / k_seek_sector_list[target_index].sec_per_revolution);
754+
}
755+
else if (target_index > start_index)
756+
{
757+
track_difference = (k_seek_sector_list[start_index].sec_end - start_lba) / k_seek_sector_list[start_index].sec_per_revolution;
758+
track_difference += (end_lba - k_seek_sector_list[target_index].sec_start) / k_seek_sector_list[target_index].sec_per_revolution;
759+
track_difference += (1606.48 * (target_index - start_index - 1));
760+
}
761+
else // start_index > target_index
762+
{
763+
track_difference = (start_lba - k_seek_sector_list[start_index].sec_start) / k_seek_sector_list[start_index].sec_per_revolution;
764+
track_difference += (k_seek_sector_list[target_index].sec_end - end_lba) / k_seek_sector_list[target_index].sec_per_revolution;
765+
track_difference += (1606.48 * (start_index - target_index - 1));
766+
}
767+
768+
// Now, we use the algorithm to determine how long to wait
769+
if (lba_difference < 2)
770+
{
771+
return (3 * 1000 / 60);
772+
}
773+
if (lba_difference < 5)
774+
{
775+
return (9 * 1000 / 60) + (float)(k_seek_sector_list[target_index].rotation_ms / 2);
776+
}
777+
else if (track_difference <= 80)
778+
{
779+
return (16 * 1000 / 60) + (float)(k_seek_sector_list[target_index].rotation_ms / 2);
780+
}
781+
else if (track_difference <= 160)
782+
{
783+
return (22 * 1000 / 60) + (float)(k_seek_sector_list[target_index].rotation_ms / 2);
784+
}
785+
else if (track_difference <= 644)
786+
{
787+
return (22 * 1000 / 60) + (float)(k_seek_sector_list[target_index].rotation_ms / 2) + (float)((track_difference - 161) * 16.66 / 80);
788+
}
789+
else
790+
{
791+
return (36 * 1000 / 60) + (float)((track_difference - 644) * 16.66 / 195);
792+
}
717793
}

src/cdrom_media.h

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class CdRomMedia
8585
bool LoadCueFromFile(const char* path);
8686
bool LoadCueFromBuffer(const u8* buffer, int size, const char* path);
8787
bool ReadSector(u32 lba, u8* buffer);
88+
u32 SeekTime(u32 start_lba, u32 end_lba);
89+
u32 SectorTransferTime();
8890

8991
private:
9092
void DestroyImgFiles();
@@ -95,6 +97,7 @@ class CdRomMedia
9597
bool LoadChunk(ImgFile* img_file, u32 chunk_index);
9698
bool PreloadChunks(ImgFile* img_file, u32 start_chunk, u32 count);
9799
bool PreloadTrackChunks(u32 track_number, u32 sectors);
100+
u32 SeekFindGroup(u32 lba);
98101

99102
private:
100103
bool m_ready;
@@ -110,7 +113,41 @@ class CdRomMedia
110113
};
111114

112115
static const u32 k_cdrom_track_type_size[3] = { 2352, 2048, 2352};
113-
static const char* k_cdrom_track_type_name[3] = { "AUDIO", "MODE1/2048", "MODE1/2352" };
116+
static const char* k_cdrom_track_type_name[3] = { "AUDIO", "MODE1/2048", "MODE1/2352" };
117+
118+
INLINE u32 CdRomMedia::SectorTransferTime()
119+
{
120+
// Standard CD-ROM 1x speed: 75 sectors/sec
121+
return 1000 / 75;
122+
}
123+
124+
// Seek time, based on the work by Dave Shadoff
125+
// https://github.com/pce-devel/PCECD_seek
126+
struct GG_Seek_Sector_Group
127+
{
128+
u32 sec_per_revolution;
129+
u32 sec_start;
130+
u32 sec_end;
131+
float rotation_ms;
132+
};
133+
134+
#define GG_SEEK_NUM_SECTOR_GROUPS 14
135+
static const GG_Seek_Sector_Group k_seek_sector_list[GG_SEEK_NUM_SECTOR_GROUPS] = {
136+
{ 10, 0, 12572, 133.47 },
137+
{ 11, 12573, 30244, 146.82 }, // Except for the first and last groups,
138+
{ 12, 30245, 49523, 160.17 }, // there are 1606.5 tracks in each range
139+
{ 13, 49524, 70408, 173.51 },
140+
{ 14, 70409, 92900, 186.86 },
141+
{ 15, 92901, 116998, 200.21 },
142+
{ 16, 116999, 142703, 213.56 },
143+
{ 17, 142704, 170014, 226.90 },
144+
{ 18, 170015, 198932, 240.25 },
145+
{ 19, 198933, 229456, 253.60 },
146+
{ 20, 229457, 261587, 266.95 },
147+
{ 21, 261588, 295324, 280.29 },
148+
{ 22, 295325, 330668, 293.64 },
149+
{ 23, 330669, 333012, 306.99 }
150+
};
114151

115152
#include "cdrom_media_inline.h"
116153

src/defines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#define GG_DEBUG 1
4040
#endif
4141

42+
#define GG_MASTER_CLOCK_RATE 21477273
43+
4244
#define GG_SAVESTATE_VERSION 20
4345
#define GG_SAVESTATE_MAGIC 0x82190619
4446

src/huc6280_psg.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <fstream>
2525
#include "common.h"
2626

27-
#define GG_MASTER_CLOCK_RATE 21477273
2827
#define GG_AUDIO_SAMPLE_RATE 44100
2928
#define GG_AUDIO_BUFFER_SIZE 2048
3029
#define GG_AUDIO_BUFFER_COUNT 3

0 commit comments

Comments
 (0)