cdromif.cpp 17.9 KB
Newer Older
twinaphex's avatar
twinaphex committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* Mednafen - Multi-system Emulator
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "../mednafen.h"
19
#include "../error.h"
twinaphex's avatar
twinaphex committed
20
21
22
23
24
25
26
#include <string.h>
#include <sys/types.h>
#include "cdromif.h"
#include "CDAccess.h"
#include "../general.h"

#include <algorithm>
Libretro-Admin's avatar
Libretro-Admin committed
27

Libretro-Admin's avatar
Libretro-Admin committed
28
#include <boolean.h>
29
#include <rthreads/rthreads.h>
Libretro-Admin's avatar
Libretro-Admin committed
30
31
#include <retro_miscellaneous.h>

Libretro-Admin's avatar
Libretro-Admin committed
32
#include <libretro.h>
33
34

extern retro_log_printf_t log_cb;
twinaphex's avatar
twinaphex committed
35
36
37

enum
{
Libretro-Admin's avatar
Libretro-Admin committed
38
39
40
41
42
43
44
45
46
   // Status/Error messages
   CDIF_MSG_DONE = 0,
   CDIF_MSG_INFO,
   CDIF_MSG_FATAL_ERROR,

   // Command messages.
   CDIF_MSG_DIEDIEDIE,
   CDIF_MSG_READ_SECTOR,
   CDIF_MSG_EJECT
twinaphex's avatar
twinaphex committed
47
48
};

Themaister's avatar
Themaister committed
49
50
class CDIF_Message
{
Libretro-Admin's avatar
Libretro-Admin committed
51
   public:
Themaister's avatar
Themaister committed
52

Libretro-Admin's avatar
Libretro-Admin committed
53
54
55
56
      CDIF_Message();
      CDIF_Message(unsigned int message_, uint32 arg0 = 0, uint32 arg1 = 0, uint32 arg2 = 0, uint32 arg3 = 0);
      CDIF_Message(unsigned int message_, const std::string &str);
      ~CDIF_Message();
Themaister's avatar
Themaister committed
57

Libretro-Admin's avatar
Libretro-Admin committed
58
59
60
61
      unsigned int message;
      uint32 args[4];
      void *parg;
      std::string str_message;
Themaister's avatar
Themaister committed
62
63
};

64
65
#if HAVE_THREADS

Themaister's avatar
Themaister committed
66
67
class CDIF_Queue
{
Libretro-Admin's avatar
Libretro-Admin committed
68
   public:
Themaister's avatar
Themaister committed
69

Libretro-Admin's avatar
Libretro-Admin committed
70
71
      CDIF_Queue();
      ~CDIF_Queue();
Themaister's avatar
Themaister committed
72

Libretro-Admin's avatar
Libretro-Admin committed
73
      bool Read(CDIF_Message *message, bool blocking = true);
Themaister's avatar
Themaister committed
74

Libretro-Admin's avatar
Libretro-Admin committed
75
      void Write(const CDIF_Message &message);
Themaister's avatar
Themaister committed
76

Libretro-Admin's avatar
Libretro-Admin committed
77
78
   private:
      std::queue<CDIF_Message> ze_queue;
79
80
      slock_t *ze_mutex;
      scond_t *ze_cond;
Themaister's avatar
Themaister committed
81
82
83
84
85
};


typedef struct
{
Libretro-Admin's avatar
Libretro-Admin committed
86
87
   bool valid;
   bool error;
88
89
   uint32 lba;
   uint8 data[2352 + 96];
Themaister's avatar
Themaister committed
90
91
} CDIF_Sector_Buffer;

Libretro-Admin's avatar
Libretro-Admin committed
92
/* TODO: prohibit copy constructor */
Themaister's avatar
Themaister committed
93
94
class CDIF_MT : public CDIF
{
Libretro-Admin's avatar
Libretro-Admin committed
95
   public:
Themaister's avatar
Themaister committed
96

Libretro-Admin's avatar
Libretro-Admin committed
97
98
      CDIF_MT(CDAccess *cda);
      virtual ~CDIF_MT();
Themaister's avatar
Themaister committed
99

100
      virtual void HintReadSector(uint32 lba);
101
      virtual bool ReadRawSector(uint8 *buf, uint32 lba, int64 timeout_us);
102
      virtual bool ReadRawSectorPWOnly(uint8 *buf, uint32 lba, bool hint_fullread);
Themaister's avatar
Themaister committed
103

Libretro-Admin's avatar
Libretro-Admin committed
104
105
106
      // Return true if operation succeeded or it was a NOP(either due to not being implemented, or the current status matches eject_status).
      // Returns false on failure(usually drive error of some kind; not completely fatal, can try again).
      virtual bool Eject(bool eject_status);
Themaister's avatar
Themaister committed
107

Libretro-Admin's avatar
Libretro-Admin committed
108
109
      // FIXME: Semi-private:
      int ReadThreadStart(void);
Themaister's avatar
Themaister committed
110

Libretro-Admin's avatar
Libretro-Admin committed
111
   private:
Themaister's avatar
Themaister committed
112

113
114
      CDAccess  *disc_cdaccess;
      sthread_t *CDReadThread;
Themaister's avatar
Themaister committed
115

Libretro-Admin's avatar
Libretro-Admin committed
116
117
      // Queue for messages to the read thread.
      CDIF_Queue ReadThreadQueue;
Themaister's avatar
Themaister committed
118

Libretro-Admin's avatar
Libretro-Admin committed
119
120
      // Queue for messages to the emu thread.
      CDIF_Queue EmuThreadQueue;
Themaister's avatar
Themaister committed
121
122


Libretro-Admin's avatar
Libretro-Admin committed
123
124
      enum { SBSize = 256 };
      CDIF_Sector_Buffer SectorBuffers[SBSize];
Themaister's avatar
Themaister committed
125

126
      uint32 SBWritePos;
Themaister's avatar
Themaister committed
127

128
129
      slock_t *SBMutex;
      scond_t *SBCond;
Themaister's avatar
Themaister committed
130

Libretro-Admin's avatar
Libretro-Admin committed
131
132
133
      //
      // Read-thread-only:
      //
Libretro-Admin's avatar
Libretro-Admin committed
134
      bool RT_EjectDisc(bool eject_status, bool skip_actual_eject = false);
Themaister's avatar
Themaister committed
135

136
137
138
      uint32 ra_lba;
      int ra_count;
      uint32 last_read_lba;
Libretro-Admin's avatar
Libretro-Admin committed
139
};
Themaister's avatar
Themaister committed
140

141
142
#endif /* HAVE_THREAD */

Libretro-Admin's avatar
Libretro-Admin committed
143
/* TODO: prohibit copy constructor */
Themaister's avatar
Themaister committed
144
145
class CDIF_ST : public CDIF
{
Libretro-Admin's avatar
Libretro-Admin committed
146
   public:
Themaister's avatar
Themaister committed
147

Libretro-Admin's avatar
Libretro-Admin committed
148
149
      CDIF_ST(CDAccess *cda);
      virtual ~CDIF_ST();
Themaister's avatar
Themaister committed
150

151
      virtual void HintReadSector(uint32 lba);
152
      virtual bool ReadRawSector(uint8 *buf, uint32 lba, int64 timeout_us);
153
      virtual bool ReadRawSectorPWOnly(uint8 *buf, uint32 lba, bool hint_fullread);
Libretro-Admin's avatar
Libretro-Admin committed
154
      virtual bool Eject(bool eject_status);
Themaister's avatar
Themaister committed
155

Libretro-Admin's avatar
Libretro-Admin committed
156
157
   private:
      CDAccess *disc_cdaccess;
Themaister's avatar
Themaister committed
158
159
};

Libretro-Admin's avatar
Libretro-Admin committed
160
CDIF::CDIF() : UnrecoverableError(false), DiscEjected(false)
twinaphex's avatar
twinaphex committed
161
{
Libretro-Admin's avatar
Libretro-Admin committed
162
   TOC_Clear(&disc_toc);
twinaphex's avatar
twinaphex committed
163
164
165
166
167
168
169
170
171
172
}

CDIF::~CDIF()
{

}


CDIF_Message::CDIF_Message()
{
Libretro-Admin's avatar
Libretro-Admin committed
173
   message = 0;
twinaphex's avatar
twinaphex committed
174

Libretro-Admin's avatar
Libretro-Admin committed
175
   memset(args, 0, sizeof(args));
twinaphex's avatar
twinaphex committed
176
177
178
179
}

CDIF_Message::CDIF_Message(unsigned int message_, uint32 arg0, uint32 arg1, uint32 arg2, uint32 arg3)
{
Libretro-Admin's avatar
Libretro-Admin committed
180
181
182
183
184
   message = message_;
   args[0] = arg0;
   args[1] = arg1;
   args[2] = arg2;
   args[3] = arg3;
twinaphex's avatar
twinaphex committed
185
186
187
188
}

CDIF_Message::CDIF_Message(unsigned int message_, const std::string &str)
{
Libretro-Admin's avatar
Libretro-Admin committed
189
190
   message = message_;
   str_message = str;
twinaphex's avatar
twinaphex committed
191
192
193
194
195
196
197
}

CDIF_Message::~CDIF_Message()
{

}

198
199
#if HAVE_THREADS

twinaphex's avatar
twinaphex committed
200
201
CDIF_Queue::CDIF_Queue()
{
202
203
   ze_mutex = slock_new();
   ze_cond  = scond_new();
twinaphex's avatar
twinaphex committed
204
205
206
207
}

CDIF_Queue::~CDIF_Queue()
{
208
209
   slock_free(ze_mutex);
   scond_free(ze_cond);
twinaphex's avatar
twinaphex committed
210
211
}

Libretro-Admin's avatar
Libretro-Admin committed
212
// Returns false if message not read, true if it was read.  Will always return true if "blocking" is set.
twinaphex's avatar
twinaphex committed
213
214
215
// Will throw MDFN_Error if the read message code is CDIF_MSG_FATAL_ERROR
bool CDIF_Queue::Read(CDIF_Message *message, bool blocking)
{
216
   bool ret = true;
twinaphex's avatar
twinaphex committed
217

218
   slock_lock((slock_t*)ze_mutex);
twinaphex's avatar
twinaphex committed
219

220
221
222
   if(blocking)
   {
      while(ze_queue.size() == 0)	// while, not just if.
223
         scond_wait((scond_t*)ze_cond, (slock_t*)ze_mutex);
224
225
226
227
228
229
230
231
232
   }

   if(ze_queue.size() == 0)
      ret = false;
   else
   {
      *message = ze_queue.front();
      ze_queue.pop();
   }  
twinaphex's avatar
twinaphex committed
233

234
   slock_unlock((slock_t*)ze_mutex);
235
236

   if(ret && message->message == CDIF_MSG_FATAL_ERROR)
Libretro-Admin's avatar
Libretro-Admin committed
237
   {
238
      log_cb(RETRO_LOG_ERROR, "%s\n", message->str_message.c_str());
Libretro-Admin's avatar
Libretro-Admin committed
239
240
      return false;
   }
241
242

   return(ret);
twinaphex's avatar
twinaphex committed
243
244
245
246
}

void CDIF_Queue::Write(const CDIF_Message &message)
{
247
   slock_lock((slock_t*)ze_mutex);
twinaphex's avatar
twinaphex committed
248

Libretro-Admin's avatar
Libretro-Admin committed
249
   ze_queue.push(message);
twinaphex's avatar
twinaphex committed
250

251
   scond_signal((scond_t*)ze_cond); // Signal while the mutex is held to prevent icky race conditions
252

253
   slock_unlock((slock_t*)ze_mutex);
twinaphex's avatar
twinaphex committed
254
255
}

Themaister's avatar
Themaister committed
256

Libretro-Admin's avatar
Libretro-Admin committed
257
bool CDIF_MT::RT_EjectDisc(bool eject_status, bool skip_actual_eject)
twinaphex's avatar
twinaphex committed
258
{
Libretro-Admin's avatar
Libretro-Admin committed
259
   int32_t old_de = DiscEjected;
twinaphex's avatar
twinaphex committed
260

Libretro-Admin's avatar
Libretro-Admin committed
261
   DiscEjected = eject_status;
twinaphex's avatar
twinaphex committed
262

Libretro-Admin's avatar
Libretro-Admin committed
263
   if(old_de != DiscEjected)
twinaphex's avatar
twinaphex committed
264
   {
Libretro-Admin's avatar
Libretro-Admin committed
265
266
267
268
269
270
271
272
      if(!skip_actual_eject)
         disc_cdaccess->Eject(eject_status);

      if(!eject_status)	// Re-read the TOC
      {
         disc_cdaccess->Read_TOC(&disc_toc);

         if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track)
Libretro-Admin's avatar
Libretro-Admin committed
273
         {
274
            log_cb(RETRO_LOG_ERROR, "TOC first(%d)/last(%d) track numbers bad.\n", disc_toc.first_track, disc_toc.last_track);
Libretro-Admin's avatar
Libretro-Admin committed
275
276
            return false;
         }
Libretro-Admin's avatar
Libretro-Admin committed
277
278
279
280
281
      }

      SBWritePos = 0;
      ra_lba = 0;
      ra_count = 0;
282
      last_read_lba = ~0U;
Libretro-Admin's avatar
Libretro-Admin committed
283
      memset(SectorBuffers, 0, SBSize * sizeof(CDIF_Sector_Buffer));
twinaphex's avatar
twinaphex committed
284
   }
Libretro-Admin's avatar
Libretro-Admin committed
285
286

   return true;
twinaphex's avatar
twinaphex committed
287
288
289
290
}

struct RTS_Args
{
Libretro-Admin's avatar
Libretro-Admin committed
291
   CDIF_MT *cdif_ptr;
twinaphex's avatar
twinaphex committed
292
293
294
295
};

static int ReadThreadStart_C(void *v_arg)
{
Libretro-Admin's avatar
Libretro-Admin committed
296
   RTS_Args *args = (RTS_Args *)v_arg;
twinaphex's avatar
twinaphex committed
297

Libretro-Admin's avatar
Libretro-Admin committed
298
   return args->cdif_ptr->ReadThreadStart();
twinaphex's avatar
twinaphex committed
299
300
301
302
}

int CDIF_MT::ReadThreadStart()
{
Libretro-Admin's avatar
Libretro-Admin committed
303
   bool Running = true;
twinaphex's avatar
twinaphex committed
304

Libretro-Admin's avatar
Libretro-Admin committed
305
306
307
   DiscEjected = true;
   SBWritePos = 0;
   ra_lba = 0;
twinaphex's avatar
twinaphex committed
308
   ra_count = 0;
309
   last_read_lba = ~0U;
twinaphex's avatar
twinaphex committed
310

311
   RT_EjectDisc(false, true);
twinaphex's avatar
twinaphex committed
312

Libretro-Admin's avatar
Libretro-Admin committed
313
   EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_DONE));
twinaphex's avatar
twinaphex committed
314

Libretro-Admin's avatar
Libretro-Admin committed
315
316
317
318
319
   while(Running)
   {
      CDIF_Message msg;

      // Only do a blocking-wait for a message if we don't have any sectors to read-ahead.
Libretro-Admin's avatar
Libretro-Admin committed
320
      if(ReadThreadQueue.Read(&msg, ra_count ? false : true))
Libretro-Admin's avatar
Libretro-Admin committed
321
322
323
324
      {
         switch(msg.message)
         {
            case CDIF_MSG_DIEDIEDIE:
Libretro-Admin's avatar
Libretro-Admin committed
325
               Running = false;
Libretro-Admin's avatar
Libretro-Admin committed
326
327
328
               break;

            case CDIF_MSG_EJECT:
329
330
               RT_EjectDisc(msg.args[0]);
               EmuThreadQueue.Write(CDIF_Message(CDIF_MSG_DONE));
Libretro-Admin's avatar
Libretro-Admin committed
331
332
333
334
335
336
337
               break;

            case CDIF_MSG_READ_SECTOR:
               {
                  static const int       max_ra = 16;
                  static const int   initial_ra = 1;
                  static const int speedmult_ra = 2;
338
                  uint32_t              new_lba = msg.args[0];
Libretro-Admin's avatar
Libretro-Admin committed
339
340
341

                  assert((unsigned int)max_ra < (SBSize / 4));

342
                  if(last_read_lba != ~0U && new_lba == (last_read_lba + 1))
Libretro-Admin's avatar
Libretro-Admin committed
343
344
345
346
                  {
                     int how_far_ahead = ra_lba - new_lba;

                     if(how_far_ahead <= max_ra)
Libretro-Admin's avatar
Libretro-Admin committed
347
                        ra_count = MIN(speedmult_ra, 1 + max_ra - how_far_ahead);
Libretro-Admin's avatar
Libretro-Admin committed
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
                     else
                        ra_count++;
                  }
                  else if(new_lba != last_read_lba)
                  {
                     ra_lba = new_lba;
                     ra_count = initial_ra;
                  }

                  last_read_lba = new_lba;
               }
               break;
         }
      }

363
364
365
      // Don't read >= the "end" of the disc, silly snake.  Slither.
      if(ra_count && ra_lba == disc_toc.tracks[100].lba)
      {
Libretro-Admin's avatar
Libretro-Admin committed
366
         ra_count = 0;
367
368
         //printf("Ephemeral scarabs: %d!\n", ra_lba);
      }
Libretro-Admin's avatar
Libretro-Admin committed
369

370
      if(ra_count)
Libretro-Admin's avatar
Libretro-Admin committed
371
372
373
374
      {
         uint8_t tmpbuf[2352 + 96];
         bool error_condition = false;

375
         disc_cdaccess->Read_Raw_Sector(tmpbuf, ra_lba);
Libretro-Admin's avatar
Libretro-Admin committed
376

377
         slock_lock((slock_t*)SBMutex);
Libretro-Admin's avatar
Libretro-Admin committed
378
379
380

         SectorBuffers[SBWritePos].lba = ra_lba;
         memcpy(SectorBuffers[SBWritePos].data, tmpbuf, 2352 + 96);
Libretro-Admin's avatar
Libretro-Admin committed
381
         SectorBuffers[SBWritePos].valid = true;
Libretro-Admin's avatar
Libretro-Admin committed
382
383
384
         SectorBuffers[SBWritePos].error = error_condition;
         SBWritePos = (SBWritePos + 1) % SBSize;

385
386
         scond_signal((scond_t*)SBCond);
         slock_unlock((slock_t*)SBMutex);
Libretro-Admin's avatar
Libretro-Admin committed
387
388
389
390
391

         ra_lba++;
         ra_count--;
      }
   }
twinaphex's avatar
twinaphex committed
392

Libretro-Admin's avatar
Libretro-Admin committed
393
   return(1);
twinaphex's avatar
twinaphex committed
394
395
}

396
CDIF_MT::CDIF_MT(CDAccess *cda) : disc_cdaccess(cda), CDReadThread(NULL), SBMutex(NULL), SBCond(NULL)
twinaphex's avatar
twinaphex committed
397
{
398
399
   CDIF_Message msg;
   RTS_Args s;
Libretro-Admin's avatar
Libretro-Admin committed
400

401
402
403
   SBMutex            = slock_new();
   SBCond             = scond_new();
   UnrecoverableError = false;
Libretro-Admin's avatar
Libretro-Admin committed
404

405
   s.cdif_ptr = this;
Libretro-Admin's avatar
Libretro-Admin committed
406

407
408
   CDReadThread = sthread_create((void (*)(void*))ReadThreadStart_C, &s);
   EmuThreadQueue.Read(&msg);
twinaphex's avatar
twinaphex committed
409
410
411
412
413
}


CDIF_MT::~CDIF_MT()
{
Libretro-Admin's avatar
Libretro-Admin committed
414
   bool thread_deaded_failed = false;
twinaphex's avatar
twinaphex committed
415

416
   ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_DIEDIEDIE));
twinaphex's avatar
twinaphex committed
417

Libretro-Admin's avatar
Libretro-Admin committed
418
   if(!thread_deaded_failed)
419
      sthread_join((sthread_t*)CDReadThread);
twinaphex's avatar
twinaphex committed
420

Libretro-Admin's avatar
Libretro-Admin committed
421
422
   if(SBMutex)
   {
423
      slock_free((slock_t*)SBMutex);
Libretro-Admin's avatar
Libretro-Admin committed
424
425
      SBMutex = NULL;
   }
twinaphex's avatar
twinaphex committed
426

jdgleaver's avatar
jdgleaver committed
427
428
429
430
431
432
   if(SBCond)
   {
      scond_free(SBCond);
      SBCond = NULL;
   }

Libretro-Admin's avatar
Libretro-Admin committed
433
434
435
436
437
   if(disc_cdaccess)
   {
      delete disc_cdaccess;
      disc_cdaccess = NULL;
   }
twinaphex's avatar
twinaphex committed
438
439
}

440
bool CDIF_MT::ReadRawSector(uint8 *buf, uint32 lba, int64 timeout_us)
twinaphex's avatar
twinaphex committed
441
{
Libretro-Admin's avatar
Libretro-Admin committed
442
   bool found = false;
Libretro-Admin's avatar
Libretro-Admin committed
443
   bool error_condition = false;
twinaphex's avatar
twinaphex committed
444

Libretro-Admin's avatar
Libretro-Admin committed
445
446
447
448
449
   if(UnrecoverableError)
   {
      memset(buf, 0, 2352 + 96);
      return(false);
   }
twinaphex's avatar
twinaphex committed
450

451
452
453
   // This shouldn't happen, the emulated-system-specific CDROM emulation code should make sure the emulated program doesn't try
   // to read past the last "real" sector of the disc.
   if(lba >= disc_toc.tracks[100].lba)
Libretro-Admin's avatar
Libretro-Admin committed
454
   {
455
      printf("Attempt to read LBA %d, >= LBA %d\n", lba, disc_toc.tracks[100].lba);
Libretro-Admin's avatar
Libretro-Admin committed
456
      return(false);
Libretro-Admin's avatar
Libretro-Admin committed
457
   }
twinaphex's avatar
twinaphex committed
458

Libretro-Admin's avatar
Libretro-Admin committed
459
   ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_READ_SECTOR, lba));
460

461
   slock_lock((slock_t*)SBMutex);
twinaphex's avatar
twinaphex committed
462

Libretro-Admin's avatar
Libretro-Admin committed
463
   do
twinaphex's avatar
twinaphex committed
464
   {
Libretro-Admin's avatar
Libretro-Admin committed
465
466
467
468
469
470
471
      int i;
      for(i = 0; i < SBSize; i++)
      {
         if(SectorBuffers[i].valid && SectorBuffers[i].lba == lba)
         {
            error_condition = SectorBuffers[i].error;
            memcpy(buf, SectorBuffers[i].data, 2352 + 96);
Libretro-Admin's avatar
Libretro-Admin committed
472
            found = true;
Libretro-Admin's avatar
Libretro-Admin committed
473
474
475
476
         }
      }

      if(!found)
477
      {
478
         if (timeout_us >= 0)
479
         {
480
481
482
483
484
485
            if (!scond_wait_timeout((scond_t*)SBCond, (slock_t*)SBMutex, timeout_us))
            {
               error_condition = true;
               memset(buf, 0, 2352 + 96);
               break;
            }
486
         }
487
488
         else
            scond_wait((scond_t*)SBCond, (slock_t*)SBMutex);
489
      }
Libretro-Admin's avatar
Libretro-Admin committed
490
   } while(!found);
twinaphex's avatar
twinaphex committed
491

492
   slock_unlock((slock_t*)SBMutex);
493

Libretro-Admin's avatar
Libretro-Admin committed
494
   return(!error_condition);
twinaphex's avatar
twinaphex committed
495
496
}

497
498
499
500
501
502
503
504
505
506
507
508
509
510
bool CDIF_MT::ReadRawSectorPWOnly(uint8 *buf, uint32 lba, bool hint_fullread)
{
   if(UnrecoverableError)
   {
      memset(buf, 0, 96);
      return(false);
   }

   // This shouldn't happen, the emulated-system-specific CDROM emulation code should make sure the emulated program doesn't try
   // to read past the last "real" sector of the disc.
   if(lba >= disc_toc.tracks[100].lba)
   {
      printf("Attempt to read LBA %d, >= LBA %d\n", lba, disc_toc.tracks[100].lba);
      memset(buf, 0, 96);
Libretro-Admin's avatar
Libretro-Admin committed
511
      return(false);
512
513
   }

514
515
516
517
   if (hint_fullread)
   {
      HintReadSector(lba);
   }
518

519
   return disc_cdaccess->Read_Raw_PW(buf, lba);
520
521
}

522
void CDIF_MT::HintReadSector(uint32 lba)
twinaphex's avatar
twinaphex committed
523
{
Libretro-Admin's avatar
Libretro-Admin committed
524
525
   if(UnrecoverableError)
      return;
twinaphex's avatar
twinaphex committed
526

Libretro-Admin's avatar
Libretro-Admin committed
527
   ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_READ_SECTOR, lba));
twinaphex's avatar
twinaphex committed
528
529
}

530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
bool CDIF_MT::Eject(bool eject_status)
{
   CDIF_Message msg;

   if(UnrecoverableError)
      return(false);

   ReadThreadQueue.Write(CDIF_Message(CDIF_MSG_EJECT, eject_status));
   EmuThreadQueue.Read(&msg);

   return(true);
}

#endif /* HAVE_THREADS */

bool CDIF::ValidateRawSector(uint8 *buf)
{
   int mode = buf[12 + 3];

   if(mode != 0x1 && mode != 0x2)
      return(false);

   if(!edc_lec_check_and_correct(buf, mode == 2))
      return(false);

   return(true);
}

558
int CDIF::ReadSector(uint8* pBuf, uint32 lba, uint32 nSectors)
twinaphex's avatar
twinaphex committed
559
{
Libretro-Admin's avatar
Libretro-Admin committed
560
561
562
563
564
   int ret = 0;

   if(UnrecoverableError)
      return(false);

565
   while(nSectors--)
Libretro-Admin's avatar
Libretro-Admin committed
566
567
568
569
570
   {
      int mode;
      uint8_t tmpbuf[2352 + 96];

      if(!ReadRawSector(tmpbuf, lba))
Libretro-Admin's avatar
Libretro-Admin committed
571
         return(false);
Libretro-Admin's avatar
Libretro-Admin committed
572
573
574
575
576
577
578
579
580
581
582
583

      if(!ValidateRawSector(tmpbuf))
         return(false);

      mode = tmpbuf[12 + 3];

      if(!ret)
         ret = mode;

      switch (mode)
      {
         case 1:
584
            memcpy(pBuf, &tmpbuf[12 + 4], 2048);
Libretro-Admin's avatar
Libretro-Admin committed
585
586
            break;
         case 2:
587
            memcpy(pBuf, &tmpbuf[12 + 4 + 8], 2048);
Libretro-Admin's avatar
Libretro-Admin committed
588
589
590
591
592
593
            break;
         default:
            printf("CDIF_ReadSector() invalid sector type at LBA=%u\n", (unsigned int)lba);
            return(false);
      }

594
      pBuf += 2048;
Libretro-Admin's avatar
Libretro-Admin committed
595
596
597
598
      lba++;
   }

   return(ret);
twinaphex's avatar
twinaphex committed
599
600
}

Themaister's avatar
Themaister committed
601
602
603
604
// Single-threaded implementation follows.

CDIF_ST::CDIF_ST(CDAccess *cda) : disc_cdaccess(cda)
{
Libretro-Admin's avatar
Libretro-Admin committed
605
606
   UnrecoverableError = false;
   DiscEjected = false;
Themaister's avatar
Themaister committed
607

Libretro-Admin's avatar
Libretro-Admin committed
608
   disc_cdaccess->Read_TOC(&disc_toc);
Themaister's avatar
Themaister committed
609

Libretro-Admin's avatar
Libretro-Admin committed
610
   if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track)
Libretro-Admin's avatar
Libretro-Admin committed
611
      throw(MDFN_Error(0, "TOC first(%d)/last(%d) track numbers bad.", disc_toc.first_track, disc_toc.last_track));
Themaister's avatar
Themaister committed
612
613
614
615
}

CDIF_ST::~CDIF_ST()
{
Libretro-Admin's avatar
Libretro-Admin committed
616
617
618
619
620
   if(disc_cdaccess)
   {
      delete disc_cdaccess;
      disc_cdaccess = NULL;
   }
Themaister's avatar
Themaister committed
621
622
}

623
void CDIF_ST::HintReadSector(uint32 lba)
Themaister's avatar
Themaister committed
624
{
Libretro-Admin's avatar
Libretro-Admin committed
625
   /* TODO: disc_cdaccess seek hint? (probably not, would require asynchronousitycamel) */
Themaister's avatar
Themaister committed
626
627
}

628
bool CDIF_ST::ReadRawSector(uint8 *buf, uint32 lba, int64 timeout_us)
Themaister's avatar
Themaister committed
629
{
Libretro-Admin's avatar
Libretro-Admin committed
630
631
632
633
634
   if(UnrecoverableError)
   {
      memset(buf, 0, 2352 + 96);
      return(false);
   }
Themaister's avatar
Themaister committed
635

636
   disc_cdaccess->Read_Raw_Sector(buf, lba);
Themaister's avatar
Themaister committed
637

Libretro-Admin's avatar
Libretro-Admin committed
638
   return(true);
Themaister's avatar
Themaister committed
639
640
}

641
642
643
644
645
646
647
648
649
650
651
652
653
654
bool CDIF_ST::ReadRawSectorPWOnly(uint8 *buf, uint32 lba, bool hint_fullread)
{
   if(UnrecoverableError)
   {
      memset(buf, 0, 96);
      return(false);
   }

   // This shouldn't happen, the emulated-system-specific CDROM emulation code should make sure the emulated program doesn't try
   // to read past the last "real" sector of the disc.
   if(lba >= disc_toc.tracks[100].lba)
   {
      printf("Attempt to read LBA %d, >= LBA %d\n", lba, disc_toc.tracks[100].lba);
      memset(buf, 0, 96);
Libretro-Admin's avatar
Libretro-Admin committed
655
      return(false);
656
657
   }

658
   return disc_cdaccess->Read_Raw_PW(buf, lba);
659
660
}

Themaister's avatar
Themaister committed
661
662
bool CDIF_ST::Eject(bool eject_status)
{
Libretro-Admin's avatar
Libretro-Admin committed
663
664
   if(UnrecoverableError)
      return(false);
Themaister's avatar
Themaister committed
665

Libretro-Admin's avatar
Libretro-Admin committed
666
667
668
   int32_t old_de = DiscEjected;

   DiscEjected = eject_status;
Themaister's avatar
Themaister committed
669

Libretro-Admin's avatar
Libretro-Admin committed
670
671
672
   if(old_de != DiscEjected)
   {
      disc_cdaccess->Eject(eject_status);
Themaister's avatar
Themaister committed
673

Libretro-Admin's avatar
Libretro-Admin committed
674
      if(!eject_status)     // Re-read the TOC
Libretro-Admin's avatar
Libretro-Admin committed
675
      {
Libretro-Admin's avatar
Libretro-Admin committed
676
         disc_cdaccess->Read_TOC(&disc_toc);
Themaister's avatar
Themaister committed
677

Libretro-Admin's avatar
Libretro-Admin committed
678
         if(disc_toc.first_track < 1 || disc_toc.last_track > 99 || disc_toc.first_track > disc_toc.last_track)
Libretro-Admin's avatar
Libretro-Admin committed
679
         {
680
            log_cb(RETRO_LOG_ERROR, "TOC first(%d)/last(%d) track numbers bad.\n", disc_toc.first_track, disc_toc.last_track);
Libretro-Admin's avatar
Libretro-Admin committed
681
            return false;
Libretro-Admin's avatar
Libretro-Admin committed
682
683
684
         }
      }
   }
Themaister's avatar
Themaister committed
685

Libretro-Admin's avatar
Libretro-Admin committed
686
   return(true);
Themaister's avatar
Themaister committed
687
688
689
}


twinaphex's avatar
twinaphex committed
690
691
class CDIF_Stream_Thing : public Stream
{
Libretro-Admin's avatar
Libretro-Admin committed
692
693
694
695
   public:

      CDIF_Stream_Thing(CDIF *cdintf_arg, uint32 lba_arg, uint32 sector_count_arg);
      ~CDIF_Stream_Thing();
twinaphex's avatar
twinaphex committed
696

Libretro-Admin's avatar
Libretro-Admin committed
697
698
      virtual uint8 *map(void);
      virtual void unmap(void);
twinaphex's avatar
twinaphex committed
699

Libretro-Admin's avatar
Libretro-Admin committed
700
      virtual uint64 read(void *data, uint64 count);
Libretro-Admin's avatar
Libretro-Admin committed
701
      virtual void write(const void *data, uint64 count);
twinaphex's avatar
twinaphex committed
702

Libretro-Admin's avatar
Libretro-Admin committed
703
      virtual void seek(int64 offset, int whence);
Libretro-Admin's avatar
Updates    
Libretro-Admin committed
704
705
      virtual uint64_t tell(void);
      virtual uint64_t size(void);
Libretro-Admin's avatar
Libretro-Admin committed
706
      virtual void flush(void);
Libretro-Admin's avatar
Libretro-Admin committed
707
      virtual void close(void);
twinaphex's avatar
twinaphex committed
708

Libretro-Admin's avatar
Libretro-Admin committed
709
710
711
712
713
   private:
      CDIF *cdintf;
      const uint32 start_lba;
      const uint32 sector_count;
      int64 position;
twinaphex's avatar
twinaphex committed
714
715
};

Libretro-Admin's avatar
Libretro-Admin committed
716
717
CDIF_Stream_Thing::CDIF_Stream_Thing(CDIF *cdintf_arg, uint32 start_lba_arg,
      uint32 sector_count_arg) : cdintf(cdintf_arg), start_lba(start_lba_arg), sector_count(sector_count_arg)
twinaphex's avatar
twinaphex committed
718
719
720
721
722
723
724
725
726
727
728
{

}

CDIF_Stream_Thing::~CDIF_Stream_Thing()
{

}

uint8 *CDIF_Stream_Thing::map(void)
{
Libretro-Admin's avatar
Libretro-Admin committed
729
   return NULL;
twinaphex's avatar
twinaphex committed
730
731
732
733
734
735
}

void CDIF_Stream_Thing::unmap(void)
{

}
Libretro-Admin's avatar
Libretro-Admin committed
736

Libretro-Admin's avatar
Libretro-Admin committed
737
uint64 CDIF_Stream_Thing::read(void *data, uint64 count)
twinaphex's avatar
twinaphex committed
738
{
Libretro-Admin's avatar
Libretro-Admin committed
739
   uint64_t rp;
Themaister's avatar
Themaister committed
740

Libretro-Admin's avatar
Libretro-Admin committed
741
742
   if(count > (((uint64)sector_count * 2048) - position))
      count = ((uint64)sector_count * 2048) - position;
twinaphex's avatar
twinaphex committed
743

Libretro-Admin's avatar
Libretro-Admin committed
744
745
   if(!count)
      return(0);
twinaphex's avatar
twinaphex committed
746

Libretro-Admin's avatar
Libretro-Admin committed
747
748
749
750
   for(rp = position; rp < (position + count); rp = (rp &~ 2047) + 2048)
   {
      uint8_t buf[2048];  

751
      cdintf->ReadSector(buf, start_lba + (rp / 2048), 1);
twinaphex's avatar
twinaphex committed
752

Libretro-Admin's avatar
Libretro-Admin committed
753
754
755
756
757
      memcpy((uint8_t*)data + (rp - position),
            buf + (rp & 2047),
            std::min<uint64>(2048 - (rp & 2047),count - (rp - position))
            );
   }
twinaphex's avatar
twinaphex committed
758

Libretro-Admin's avatar
Libretro-Admin committed
759
760
761
   position += count;

   return count;
twinaphex's avatar
twinaphex committed
762
763
764
765
766
767
768
769
}

void CDIF_Stream_Thing::write(const void *data, uint64 count)
{
}

void CDIF_Stream_Thing::seek(int64 offset, int whence)
{
Libretro-Admin's avatar
Libretro-Admin committed
770
   int64 new_position;
Themaister's avatar
Themaister committed
771

Libretro-Admin's avatar
Libretro-Admin committed
772
773
774
775
776
   switch(whence)
   {
      case SEEK_SET:
         new_position = offset;
         break;
twinaphex's avatar
twinaphex committed
777

Libretro-Admin's avatar
Libretro-Admin committed
778
779
780
      case SEEK_CUR:
         new_position = position + offset;
         break;
twinaphex's avatar
twinaphex committed
781

Libretro-Admin's avatar
Libretro-Admin committed
782
783
784
785
      case SEEK_END:
         new_position = ((int64)sector_count * 2048) + offset;
         break;
   }
Themaister's avatar
Themaister committed
786

Libretro-Admin's avatar
Libretro-Admin committed
787
   position = new_position;
twinaphex's avatar
twinaphex committed
788
789
}

Libretro-Admin's avatar
Updates    
Libretro-Admin committed
790
uint64_t CDIF_Stream_Thing::tell(void)
twinaphex's avatar
twinaphex committed
791
{
Libretro-Admin's avatar
Libretro-Admin committed
792
   return position;
twinaphex's avatar
twinaphex committed
793
794
}

Libretro-Admin's avatar
Updates    
Libretro-Admin committed
795
uint64_t CDIF_Stream_Thing::size(void)
twinaphex's avatar
twinaphex committed
796
{
Libretro-Admin's avatar
Libretro-Admin committed
797
   return(sector_count * 2048);
twinaphex's avatar
twinaphex committed
798
799
}

Libretro-Admin's avatar
Updates    
Libretro-Admin committed
800
801
802
803
804
void CDIF_Stream_Thing::flush(void)
{

}

twinaphex's avatar
twinaphex committed
805
806
807
808
809
810
void CDIF_Stream_Thing::close(void)
{

}


811
Stream *CDIF::MakeStream(uint32 lba, uint32 sector_count)
twinaphex's avatar
twinaphex committed
812
{
Libretro-Admin's avatar
Libretro-Admin committed
813
   return new CDIF_Stream_Thing(this, lba, sector_count);
twinaphex's avatar
twinaphex committed
814
815
816
}


Libretro-Admin's avatar
Libretro-Admin committed
817
CDIF *CDIF_Open(bool *success, const char *path, const bool is_device, bool image_memcache)
twinaphex's avatar
twinaphex committed
818
{
Libretro-Admin's avatar
Libretro-Admin committed
819
   CDAccess *cda = cdaccess_open_image(success, path, image_memcache);
twinaphex's avatar
twinaphex committed
820

821
#if HAVE_THREADS
Libretro-Admin's avatar
Libretro-Admin committed
822
823
   if(!image_memcache)
      return new CDIF_MT(cda);
824
825
#endif /* HAVE_THREADS */

Libretro-Admin's avatar
Libretro-Admin committed
826
   return new CDIF_ST(cda); 
twinaphex's avatar
twinaphex committed
827
}