2 Skelton for retropc emulator
4 Author : Takeda.Toshiya
10 #include "scsi_cdrom.h"
14 #define CDDA_PLAYING 1
16 //#define _SCSI_DEBUG_LOG
17 #define _CDROM_DEBUG_LOG
18 // 0-99 is reserved for SCSI_DEV class
19 #define EVENT_CDDA 100
21 void SCSI_CDROM::initialize()
23 SCSI_DEV::initialize();
24 fio_img = new FILEIO();
26 if(44100 % emu->get_sound_rate() == 0) {
27 mix_loop_num = 44100 / emu->get_sound_rate();
32 cdda_status = CDDA_OFF;
35 for(int i = 0; i < 99; i++) {
36 memset(track_data_path[i], 0x00, _MAX_PATH * sizeof(_TCHAR));
40 void SCSI_CDROM::release()
42 if(fio_img->IsOpened()) {
49 void SCSI_CDROM::reset()
53 set_cdda_status(CDDA_OFF);
54 // Q: Does not seek to track 0? 20181118 K.O
58 uint32_t SCSI_CDROM::read_signal(int id)
61 case SIG_SCSI_CDROM_PLAYING:
62 return (cdda_status == CDDA_PLAYING && cdda_interrupt) ? 0xffffffff : 0;
64 case SIG_SCSI_CDROM_SAMPLE_L:
65 return (uint32_t)abs(cdda_sample_l);
67 case SIG_SCSI_CDROM_SAMPLE_R:
68 return (uint32_t)abs(cdda_sample_r);
70 return SCSI_DEV::read_signal(id);
73 void SCSI_CDROM::event_callback(int event_id, int err)
77 // read 16bit 2ch samples in the cd-da buffer, called 44100 times/sec
78 cdda_sample_l = (int)(int16_t)(cdda_buffer[cdda_buffer_ptr + 0] + cdda_buffer[cdda_buffer_ptr + 1] * 0x100);
79 cdda_sample_r = (int)(int16_t)(cdda_buffer[cdda_buffer_ptr + 2] + cdda_buffer[cdda_buffer_ptr + 3] * 0x100);
81 if((cdda_buffer_ptr += 4) % 2352 == 0) {
83 if(++cdda_playing_frame == cdda_end_frame) {
84 // reached to end frame
87 fio_img->Fseek(cdda_start_frame * 2352, FILEIO_SEEK_SET);
88 fio_img->Fread(cdda_buffer, sizeof(cdda_buffer), 1);
90 cdda_playing_frame = cdda_start_frame;
95 write_signals(&outputs_done, 0xffffffff);
97 set_cdda_status(CDDA_OFF);
99 } else if(cdda_buffer_ptr == array_length(cdda_buffer)) {
101 fio_img->Fread(cdda_buffer, sizeof(cdda_buffer), 1);
108 SCSI_DEV::event_callback(event_id, err);
113 void SCSI_CDROM::set_cdda_status(uint8_t status)
115 if(status == CDDA_PLAYING) {
116 if(mix_loop_num == 0) {
117 if(event_cdda == -1) {
118 register_event(this, EVENT_CDDA, 1000000.0 / 44100.0, true, &event_cdda);
121 if(cdda_status != CDDA_PLAYING) {
123 set_realtime_render(this, true);
126 //if(status == CDDA_OFF) {
127 // if((status != cdda_status) && (is_cue)) {
128 // if(fio_img->IsOpened()) fio_img->Fclose();
131 if(event_cdda != -1) {
132 cancel_event(this, event_cdda);
135 if(cdda_status == CDDA_PLAYING) {
137 set_realtime_render(this, false);
140 cdda_status = status;
143 void SCSI_CDROM::reset_device()
145 set_cdda_status(CDDA_OFF);
146 SCSI_DEV::reset_device();
149 bool SCSI_CDROM::is_device_ready()
154 int SCSI_CDROM::get_command_length(int value)
164 return SCSI_DEV::get_command_length(value);
167 void SCSI_CDROM::get_track_by_track_num(int track)
169 if((track <= 0) || (track > track_num)) {
171 if(fio_img->IsOpened()) fio_img->Fclose();
173 //if(track <= 0) current_track = 0;
174 //if(track >= track_num)current_track = track_num;
179 // ToDo: Apply audio with some codecs.
180 if(current_track != track) {
181 if(fio_img != NULL) {
182 if(fio_img->IsOpened()) {
185 current_track = track;
187 #ifdef _CDROM_DEBUG_LOG
188 this->out_debug_log(_T("LOAD TRK #%02d from %s\n"), track, track_data_path[track - 1]);
191 if((track > 0) && (track < 100) && (track <= track_num)) {
192 if((strlen(track_data_path[track - 1]) <= 0) ||
193 !(fio_img->Fopen(track_data_path[track - 1], FILEIO_READ_BINARY))) {
203 int SCSI_CDROM::get_track(uint32_t lba)
208 for(int i = 1; i <= track_num; i++) {
209 if(lba >= toc_table[i].lba_offset) {
216 for(int i = 0; i < track_num; i++) {
217 if(lba >= toc_table[i].index0) {
225 get_track_by_track_num(track);
227 current_track = track;
232 double SCSI_CDROM::get_seek_time(uint32_t lba)
234 if(fio_img->IsOpened()) {
235 uint32_t cur_position = (int)fio_img->Ftell();
236 int distance = abs((int)(lba * physical_block_size()) - (int)cur_position);
237 double ratio = (double)distance / 333000 / physical_block_size(); // 333000: sectors in media
238 return max(10, (int)(400000 * 2 * ratio));
240 return 400000; // 400msec
244 uint32_t SCSI_CDROM::lba_to_msf(int trk, uint32_t lba)
248 lba = lba + toc_table[trk - 1].lba_offset;
251 uint8_t m = lba / (60 * 75);
252 lba -= m * (60 * 75);
253 uint8_t s = lba / 75;
254 uint8_t f = lba % 75;
256 return ((m / 10) << 20) | ((m % 10) << 16) | ((s / 10) << 12) | ((s % 10) << 8) | ((f / 10) << 4) | ((f % 10) << 0);
259 uint32_t SCSI_CDROM::lba_to_msf_alt(int trk, uint32_t lba)
263 lba = lba + toc_table[trk - 1].lba_offset;
267 ret |= ((lba / (60 * 75)) & 0xff) << 16;
268 ret |= (((lba / 75) % 60) & 0xff) << 8;
269 ret |= ((lba % 75) & 0xff) << 0;
273 void SCSI_CDROM::start_command()
276 #ifdef _SCSI_DEBUG_LOG
277 this->out_debug_log(_T("[SCSI_DEV:ID=%d] Command: #%02x %02x %02x %02x %02x %02x\n"), scsi_id, command[0], command[1], command[2], command[3], command[4], command[5]);
281 seek_time = 10;//get_seek_time((command[1] & 0x1f) * 0x10000 + command[2] * 0x100 + command[3]);
282 set_cdda_status(CDDA_OFF);
285 case SCSI_CMD_READ10:
286 case SCSI_CMD_READ12:
287 seek_time = 10;//get_seek_time(command[2] * 0x1000000 + command[3] * 0x10000 + command[4] * 0x100 + command[5]);
288 set_cdda_status(CDDA_OFF);
292 #ifdef _SCSI_DEBUG_LOG
293 this->out_debug_log(_T("[SCSI_DEV:ID=%d] Command: NEC Set Audio Playback Start Position\n"), scsi_id);
295 if(is_device_ready()) {
296 if(command[2] == 0 && command[3] == 0 && command[4] == 0) {
297 // stop cd-da if all params are zero
298 cdda_start_frame = 0;
299 cdda_end_frame = toc_table[track_num].index0; // end of disc
301 get_track_by_track_num(track_num);
303 set_cdda_status(CDDA_OFF);
305 uint32_t seek_offset = 0;
306 switch(command[9] & 0xc0) {
308 cdda_start_frame = (command[2] << 16) | (command[3] << 8) | command[4];
312 uint8_t m = FROM_BCD(command[2]);
313 uint8_t s = FROM_BCD(command[3]);
314 uint8_t f = FROM_BCD(command[4]);
315 cdda_start_frame = f + 75 * (s + m * 60);
317 // PCE tries to be clever here and set (start of track + track pregap size) to skip the pregap
318 // (I guess it wants the TOC to have the real start sector for data tracks and the start of the pregap for audio?)
319 int track = get_track(cdda_start_frame);
320 if((is_cue) && (track > 1)) {
321 if(cdda_start_frame >= toc_table[track].lba_offset) {
322 cdda_start_frame = cdda_start_frame - toc_table[track].lba_offset;
324 cdda_start_frame = 0;
327 cdda_start_frame -= toc_table[track].pregap;
329 if(cdda_start_frame < toc_table[track].index1) {
330 cdda_start_frame = toc_table[track].index1; // don't play pregap
331 } else if(cdda_start_frame > max_logical_block) {
332 cdda_start_frame = 0;
338 int trk = FROM_BCD(command[2]) - 1;
339 get_track_by_track_num(trk);
341 cdda_start_frame = toc_table[FROM_BCD(command[2]) - 1].index1;
344 cdda_start_frame = 0;
347 // if(cdda_status == CDDA_PAUSED) {
348 // cdda_end_frame = toc_table[track_num].index0; // end of disc
349 // set_cdda_status(CDDA_OFF);
351 if((command[1] & 3) != 0) {
352 if((is_cue) && (current_track != track_num)){
353 seek_offset = toc_table[current_track].lba_offset;
354 get_track_by_track_num(track_num);
356 cdda_end_frame = toc_table[track_num].index0; // end of disc
357 set_cdda_status(CDDA_PLAYING);
359 uint32_t _sframe = cdda_start_frame;
361 if((is_cue) && (current_track > 0)) {
362 _sframe = _sframe + toc_table[current_track].lba_offset;
363 cdda_end_frame = toc_table[get_track(_sframe) + 1].index1; // end of this track
365 cdda_end_frame = toc_table[get_track(_sframe) + 1].index1; // end of this track
367 set_cdda_status(CDDA_PAUSED);
370 cdda_interrupt = ((command[1] & 3) == 2);
373 double seek_time = get_seek_time(cdda_start_frame);
374 fio_img->Fseek(cdda_start_frame * 2352, FILEIO_SEEK_SET);
375 fio_img->Fread(cdda_buffer, sizeof(cdda_buffer), 1);
377 cdda_playing_frame = cdda_start_frame;
380 // change to status phase
381 set_dat(SCSI_STATUS_GOOD);
382 set_phase_delay(SCSI_PHASE_STATUS, seek_time);
386 // change to status phase
387 set_dat(is_device_ready() ? SCSI_STATUS_GOOD : SCSI_STATUS_CHKCOND);
388 set_phase_delay(SCSI_PHASE_STATUS, 10.0);
392 #ifdef _SCSI_DEBUG_LOG
393 this->out_debug_log(_T("[SCSI_DEV:ID=%d] Command: NEC Set Audio Playback End Position\n"), scsi_id);
395 if(is_device_ready()) {
396 switch(command[9] & 0xc0) {
398 cdda_end_frame = (command[3] << 16) | (command[4] << 8) | command[5];
402 uint8_t m = FROM_BCD(command[2]);
403 uint8_t s = FROM_BCD(command[3]);
404 uint8_t f = FROM_BCD(command[4]);
405 cdda_end_frame = f + 75 * (s + m * 60);
407 // PCE tries to be clever here and set (start of track + track pregap size) to skip the pregap
408 // (I guess it wants the TOC to have the real start sector for data tracks and the start of the pregap for audio?)
409 int track =get_track(cdda_end_frame);
410 if((is_cue) && (track > 1)) {
411 if(cdda_end_frame >= toc_table[track].lba_offset) {
412 cdda_end_frame = cdda_end_frame - toc_table[track].lba_offset;
418 if(cdda_end_frame > toc_table[track].index1 && (cdda_end_frame - toc_table[track].pregap) <= toc_table[track].index1) {
419 cdda_end_frame = toc_table[track].index1;
424 cdda_end_frame = toc_table[FROM_BCD(command[2]) - 1].index1;
427 if((command[1] & 3) != 0) {
428 set_cdda_status(CDDA_PLAYING);
429 cdda_repeat = ((command[1] & 3) == 1);
430 cdda_interrupt = ((command[1] & 3) == 2);
432 set_cdda_status(CDDA_OFF);
435 // change to status phase
436 set_dat(is_device_ready() ? SCSI_STATUS_GOOD : SCSI_STATUS_CHKCOND);
437 set_phase_delay(SCSI_PHASE_STATUS, 10.0);
441 #ifdef _SCSI_DEBUG_LOG
442 this->out_debug_log(_T("[SCSI_DEV:ID=%d] Command: NEC Pause\n"), scsi_id);
444 if(is_device_ready()) {
445 if(cdda_status == CDDA_PLAYING) {
446 set_cdda_status(CDDA_OFF);
449 if(is_cue) { // NEC PC-ENGINE ONLY.This expect to be temporally workaround.20181119 K.O
450 get_track_by_track_num(2);
452 // change to status phase
453 set_dat(is_device_ready() ? SCSI_STATUS_GOOD : SCSI_STATUS_CHKCOND);
454 set_phase_delay(SCSI_PHASE_STATUS, 10.0);
458 #ifdef _SCSI_DEBUG_LOG
459 this->out_debug_log(_T("[SCSI_DEV:ID=%d] Command: NEC Read Sub Channel Q\n"), scsi_id);
461 if(is_device_ready()) {
463 uint32_t frame = (cdda_status == CDDA_OFF) ? cdda_start_frame : cdda_playing_frame;
464 uint32_t msf_abs = lba_to_msf_alt(current_track - 1, frame);
467 //track = get_track(frame + ((current_track > 0) ? toc_table[current_track - 1].lba_offset : 0));
468 track = current_track;
470 track = get_track(frame);
472 uint32_t msf_rel = lba_to_msf_alt(track - 1, frame - toc_table[track].index0);
474 buffer->write((cdda_status == CDDA_PLAYING) ? 0x00 : (cdda_status == CDDA_PAUSED) ? 0x02 : 0x03);
475 buffer->write(0x01 | (toc_table[track].is_audio ? 0x00 : 0x40));
476 buffer->write(TO_BCD(track + 1)); // Track
477 buffer->write(0x01); // Index
478 buffer->write(TO_BCD((msf_rel >> 16) & 0xff)); // M (relative)
479 buffer->write(TO_BCD((msf_rel >> 8) & 0xff)); // S (relative)
480 buffer->write(TO_BCD((msf_rel >> 0) & 0xff)); // F (relative)
481 buffer->write(TO_BCD((msf_abs >> 16) & 0xff)); // M (absolute)
482 buffer->write(TO_BCD((msf_abs >> 8) & 0xff)); // S (absolute)
483 buffer->write(TO_BCD((msf_abs >> 0) & 0xff)); // F (absolute)
485 remain = buffer->count();
487 set_dat(buffer->read());
488 // change to data in phase
489 set_phase_delay(SCSI_PHASE_DATA_IN, 10.0);
491 // change to status phase
492 set_dat(is_device_ready() ? SCSI_STATUS_GOOD : SCSI_STATUS_CHKCOND);
493 set_phase_delay(SCSI_PHASE_STATUS, 10.0);
498 #ifdef _SCSI_DEBUG_LOG
499 this->out_debug_log(_T("[SCSI_DEV:ID=%d] Command: NEC Get Dir Info\n"), scsi_id);
501 if(is_device_ready()) {
503 #ifdef _SCSI_DEBUG_LOG
504 this->out_debug_log(_T("[SCSI_DEV:ID=%d] CMD=%02x ARG=%02x \n"), scsi_id, command[1], command[2]);
507 case 0x00: /* Get first and last track numbers */
508 buffer->write(TO_BCD(1));
509 buffer->write(TO_BCD(track_num));
511 case 0x01: /* Get total disk size in MSF format */
513 uint32_t msf = lba_to_msf(track_num - 1, toc_table[track_num].index0 + 150);
514 buffer->write((msf >> 16) & 0xff);
515 buffer->write((msf >> 8) & 0xff);
516 buffer->write((msf >> 0) & 0xff);
519 case 0x02: /* Get track information */
520 if(command[2] == 0xaa) {
521 uint32_t msf = lba_to_msf(track_num - 1, toc_table[track_num].index0 + 150);
522 buffer->write((msf >> 16) & 0xff);
523 buffer->write((msf >> 8) & 0xff);
524 buffer->write((msf >> 0) & 0xff);
525 buffer->write(0x04); // correct ?
527 int track = max(FROM_BCD(command[2]), 1);
528 uint32_t frame = toc_table[track - 1].index0;
529 // PCE wants the start sector for data tracks to *not* include the pregap
530 if(!toc_table[track - 1].is_audio) {
531 frame += toc_table[track - 1].pregap;
533 get_track_by_track_num(track);
535 uint32_t msf = lba_to_msf(track - 1, toc_table[track - 1].index1 + 150);
536 buffer->write((msf >> 16) & 0xff); // M
537 buffer->write((msf >> 8) & 0xff); // S
538 buffer->write((msf >> 0) & 0xff); // F
539 buffer->write(toc_table[track - 1].is_audio ? 0x00 : 0x04);
544 remain = buffer->count();
546 set_dat(buffer->read());
547 // change to data in phase
548 set_phase_delay(SCSI_PHASE_DATA_IN, 10.0);
550 // change to status phase
551 set_dat(is_device_ready() ? SCSI_STATUS_GOOD : SCSI_STATUS_CHKCOND);
552 set_phase_delay(SCSI_PHASE_STATUS, 10.0);
557 // start standard command
558 SCSI_DEV::start_command();
561 bool SCSI_CDROM::read_buffer(int length)
563 if(!fio_img->IsOpened()) {
564 set_sense_code(SCSI_SENSE_NOTREADY);
567 uint32_t offset = (uint32_t)(position % 2352);
569 if(fio_img->Fseek((long)position, FILEIO_SEEK_SET) != 0) {
570 set_sense_code(SCSI_SENSE_ILLGLBLKADDR); //SCSI_SENSE_SEEKERR
574 uint8_t tmp_buffer[SCSI_BUFFER_SIZE];
575 int tmp_length = min(length, (int)sizeof(tmp_buffer));
577 if(fio_img->Fread(tmp_buffer, tmp_length, 1) != 1) {
578 set_sense_code(SCSI_SENSE_ILLGLBLKADDR); //SCSI_SENSE_NORECORDFND
581 for(int i = 0; i < tmp_length && length > 0; i++) {
582 if(offset >= 16 && offset < 16 + 2048) {
583 int value = tmp_buffer[i];
584 buffer->write(value);
588 offset = (offset + 1) % 2352;
592 set_sense_code(SCSI_SENSE_NOSENSE);
596 int get_frames_from_msf(const char *string)
598 const char *ptr = string;
603 if(*ptr >= '0' && *ptr <= '9') {
604 frames[index] = frames[index] * 10 + (*ptr - '0');
605 } else if(*ptr == ':') {
610 } else if(*ptr == '\r' || *ptr == '\n' || *ptr == '\0') {
616 return (frames[0] * 60 + frames[1]) * 75 + frames[2]; // 75frames/sec
619 int hexatoi(const char *string)
621 const char *ptr = string;
625 if(*ptr >= '0' && *ptr <= '9') {
626 value = value * 16 + (*ptr - '0');
627 } else if(*ptr >= 'a' && *ptr <= 'f') {
628 value = value * 16 + (*ptr - 'a' + 10);
629 } else if(*ptr >= 'A' && *ptr <= 'F') {
630 value = value * 16 + (*ptr - 'A' + 10);
631 } else if(*ptr == '\r' || *ptr == '\n' || *ptr == '\0') {
639 bool SCSI_CDROM::open_cue_file(const _TCHAR* file_path)
642 _TCHAR line_buf[2048];
643 _TCHAR line_buf_shadow[2048];
644 _TCHAR full_path_cue[_MAX_PATH];
645 _TCHAR image_tmp_data_path[_MAX_PATH];
649 int nr_current_track = 0;
650 FILEIO* fio = new FILEIO();
651 if(fio == NULL) return false;
653 memset(full_path_cue, 0x00, sizeof(full_path_cue));
654 memset(image_tmp_data_path, 0x00, sizeof(image_tmp_data_path));
656 get_long_full_path_name(file_path, full_path_cue, sizeof(full_path_cue));
658 _TCHAR *parent_dir = get_parent_dir(full_path_cue);
659 if(fio->Fopen(file_path, FILEIO_READ_ASCII)) { // ToDo: Support not ASCII cue file (i.e. SJIS/UTF8).20181118 K.O
660 memset(line_buf, 0x00, sizeof(line_buf));
662 for(int i = 0; i < 100; i++) {
663 memset(&(track_data_path[i][0]), 0x00, _MAX_PATH * sizeof(_TCHAR));
669 memset(line_buf, 0x00, sizeof(line_buf));
674 if((_c == '\n') || (_c == EOF) || (_np >= 2048)) break;
675 if(_c != '\r') line_buf[_np++] = (_TCHAR)_c;
677 if(_c == EOF) is_eof = true;
678 slen = strlen(line_buf);
679 if(slen <= 0) goto _n_continue;
680 // Trim head of Space or TAB
683 while((line_buf[ptr] == '\t') || (line_buf[ptr] == ' ')) {
685 if(ptr >= slen) break;
690 memset(line_buf_shadow, 0x00, sizeof(line_buf_shadow));
691 while((line_buf[ptr] != ' ') && (line_buf[ptr] != '\0') && (line_buf[ptr] != '\t')) {
692 line_buf_shadow[sptr++] = (_TCHAR)(toupper(line_buf[ptr++]));
693 if(ptr >= slen) break;
695 if((ptr > slen) || (sptr == 0)) {
699 if(strcmp(line_buf_shadow, "REM") == 0) {
702 } else if(strcmp(line_buf_shadow, "FILE") == 0) {
703 while((line_buf[ptr] != '"') && (line_buf[ptr] != '\0')) {
705 if(ptr > slen) break;
712 if(ptr >= slen) goto _n_continue;
714 _TCHAR _tmps[_MAX_PATH];
715 memset(_tmps, 0x00, sizeof(_tmps));
716 while((line_buf[ptr] != '"') && (line_buf[ptr] != '\0')) {
717 _tmps[sptr++] = line_buf[ptr++];
718 if(ptr >= slen) break;
723 if((sptr != 0) && (line_buf[ptr] == '"')) {
724 strncpy(image_tmp_data_path, parent_dir, _MAX_PATH);
725 strncat(image_tmp_data_path, _tmps, _MAX_PATH - 1);
726 #ifdef _CDROM_DEBUG_LOG
727 this->out_debug_log(_T("**FILE %s\n"), image_tmp_data_path);
731 goto _n_continue; // ToDo: Check ARG2 (BINARY etc..) 20181118 K.O
732 } else if(strcmp(line_buf_shadow, "TRACK") == 0) {
733 while((line_buf[ptr] == ' ') || (line_buf[ptr] == '\t')) {
735 if(ptr >= slen) break;
737 if(ptr > slen) goto _n_continue;
741 memset(_tmps, 0x00, sizeof(_tmps));
742 while((line_buf[ptr] != ' ') && (line_buf[ptr] != '\t') && (line_buf[ptr] != '\0')) {
743 if(ptr >= slen) break;
744 _tmps[_n++] = line_buf[ptr++];
746 _nr_num = atoi(_tmps);
749 if((_nr_num > 0) && (_nr_num < 100)) {
750 nr_current_track = _nr_num;
751 if(strlen(image_tmp_data_path) > 0) {
752 strncpy(track_data_path[_nr_num - 1], image_tmp_data_path, _MAX_PATH);
754 while((line_buf[ptr] == ' ') || (line_buf[ptr] == '\t')) {
756 if(ptr >= slen) break;
758 memset(_tmps, 0x00, sizeof(_tmps));
759 for(int i = 0; i < 127; i++) {
760 if((line_buf[ptr] == '\0') || (line_buf[ptr] == ' ') ||
761 (line_buf[ptr] == '\n') || (line_buf[ptr] == '\t')) break;
762 _tmps[i] = (_TCHAR)(toupper(line_buf[ptr++]));
763 if(ptr > slen) break;
766 toc_table[nr_current_track - 1].is_audio = false;
767 toc_table[nr_current_track - 1].index0 = 0;
768 toc_table[nr_current_track - 1].index1 = 0;
769 toc_table[nr_current_track - 1].pregap = 0;
771 if(strcmp(_tmps, "AUDIO") == 0) {
772 toc_table[nr_current_track - 1].is_audio = true;
773 } else if(strcmp(_tmps, "MODE1/2352") == 0) {
774 toc_table[nr_current_track - 1].is_audio = false;
776 // ToDo: Another data mode 20181118 K.Ohta
777 memset(track_data_path[_nr_num - 1], 0x00, _MAX_PATH * sizeof(_TCHAR));
779 if(track_num < _nr_num) track_num = _nr_num;
782 // ToDo: 20181118 K.Ohta
783 nr_current_track = 0;
786 } else if(strcmp(line_buf_shadow, "INDEX") == 0) {
788 if((nr_current_track > 0) && (nr_current_track < 100)) {
789 while((line_buf[ptr] == '\t') || (line_buf[ptr] == ' ')) {
791 if(ptr >= slen) break;
796 memset(_tmps, 0x00, sizeof(_tmps));
797 for(int i = 0; i < 31; i++) {
798 if((line_buf[ptr] < '0') || (line_buf[ptr] > '9')) break;
799 _tmps[i] = line_buf[ptr++];
800 if(ptr > slen) break;
802 index_type = atoi(_tmps);
803 if(ptr >= slen) goto _n_continue;
806 while((line_buf[ptr] == '\t') || (line_buf[ptr] == ' ')) {
808 if(ptr > slen) break;
810 memset(_tmps, 0x00, sizeof(_tmps));
811 for(int i = 0; i < 31; i++) {
812 if((line_buf[ptr] < '0') || (line_buf[ptr] > '9')) {
813 if(line_buf[ptr] != ':') break;
815 _tmps[i] = line_buf[ptr++];
816 if(ptr > slen) break;
821 toc_table[nr_current_track - 1].index0 = get_frames_from_msf(_tmps);
824 toc_table[nr_current_track - 1].index1 = get_frames_from_msf(_tmps);
833 } else if(strcmp(line_buf_shadow, "PREGAP") == 0) {
834 if((nr_current_track > 0) && (nr_current_track < 100)) {
835 while((line_buf[ptr] == '\t') || (line_buf[ptr] == ' ')) {
837 if(ptr >= slen) break;
841 memset(_tmps, 0x00, sizeof(_tmps));
842 for(int i = 0; i < 31; i++) {
843 if((line_buf[ptr] < '0') || (line_buf[ptr] > '9')) {
844 if(line_buf[ptr] != ':') break;
846 _tmps[i] = line_buf[ptr++];
847 if(ptr >= slen) goto _n_continue;
849 toc_table[nr_current_track - 1].pregap = get_frames_from_msf(_tmps);
857 memset(line_buf, 0x00, sizeof(line_buf));
861 max_logical_block = 0;
863 toc_table[0].lba_offset = 0;
864 for(int i = 0; i < track_num; i++) {
866 if(toc_table[i].index0 == 0) {
867 toc_table[i].index0 = toc_table[i].index1 - toc_table[i].pregap;
868 } else if(toc_table[i].pregap == 0) {
869 toc_table[i].pregap = toc_table[i].index1 - toc_table[i].index0;
871 if(fio_img->IsOpened()) {
874 // ToDo: Support unreguler (!CDDA, i.e. MP3/WAV...) codec.20181118 K.O
876 toc_table[i].lba_offset = max_logical_block;
877 if(strlen(track_data_path[i]) > 0) {
878 if(fio_img->Fopen(track_data_path[i], FILEIO_READ_BINARY)) {
879 if((_n = fio_img->FileLength() / 2352) > 0) {
880 max_logical_block += _n;
885 #ifdef _CDROM_DEBUG_LOG
886 this->out_debug_log(_T("TRACK#%02d TYPE=%s PREGAP=%d INDEX0=%d INDEX1=%d LBA_SIZE=%d LBA_OFFSET=%d PATH=%s\n"),
887 i + 1, (toc_table[i].is_audio) ? _T("AUDIO") : _T("MODE1/2352"),
888 toc_table[i].pregap, toc_table[i].index0, toc_table[i].index1,
889 _n, toc_table[i].lba_offset, track_data_path[i]);
892 //toc_table[0].index0 = toc_table[0].index1 = toc_table[0].pregap = 0;
893 toc_table[track_num].index0 = toc_table[track_num].index1 = max_logical_block;
894 toc_table[track_num].lba_offset = max_logical_block;
901 if(track_num > 0) is_cue = true;
906 void SCSI_CDROM::open(const _TCHAR* file_path)
908 _TCHAR img_file_path[_MAX_PATH];
912 // ToDo: Process multi track cue file.Most of CDROMs contain both audio and data and more than 2 tracks.
914 if(check_file_extension(file_path, _T(".cue"))) {
918 open_cue_file(file_path);
920 // get image file name
921 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.img"), get_file_path_without_extensiton(file_path));
922 if(!FILEIO::IsFileExisting(img_file_path)) {
923 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.bin"), get_file_path_without_extensiton(file_path));
924 if(!FILEIO::IsFileExisting(img_file_path)) {
925 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.gz"), get_file_path_without_extensiton(file_path));
926 if(!FILEIO::IsFileExisting(img_file_path)) {
927 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.img.gz"), get_file_path_without_extensiton(file_path));
928 if(!FILEIO::IsFileExisting(img_file_path)) {
929 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.bin.gz"), get_file_path_without_extensiton(file_path));
934 if(fio_img->Fopen(img_file_path, FILEIO_READ_BINARY)) {
935 // get image file size
936 if((max_logical_block = fio_img->FileLength() / 2352) > 0) {
938 FILEIO* fio = new FILEIO();
939 if(fio->Fopen(file_path, FILEIO_READ_BINARY)) {
940 char line[1024], *ptr;
942 while(fio->Fgets(line, 1024) != NULL) {
943 if(strstr(line, "FILE") != NULL) {
945 } else if((ptr = strstr(line, "TRACK")) != NULL) {
947 // "TRACK 02 MODE1/2352"
949 while(*ptr == ' ' || *ptr == 0x09) {
952 if((track = atoi(ptr)) > 0) {
953 if(track > track_num) {
956 toc_table[track - 1].is_audio = (strstr(line, "AUDIO") != NULL);
958 } else if((ptr = strstr(line, "PREGAP")) != NULL) {
961 toc_table[track - 1].pregap = get_frames_from_msf(ptr + 7);
963 } else if((ptr = strstr(line, "INDEX")) != NULL) {
964 // "INDEX 01 00:00:00"
967 while(*ptr == ' ' || *ptr == 0x09) {
971 while(*ptr >= '0' && *ptr <= '9') {
975 toc_table[track - 1].index0 = get_frames_from_msf(ptr);
976 } else if(num == 1) {
977 toc_table[track - 1].index1 = get_frames_from_msf(ptr);
983 for(int i = 1; i < track_num; i++) {
984 if(toc_table[i].index0 == 0) {
985 toc_table[i].index0 = toc_table[i].index1 - toc_table[i].pregap;
986 } else if(toc_table[i].pregap == 0) {
987 toc_table[i].pregap = toc_table[i].index1 - toc_table[i].index0;
990 toc_table[0].index0 = toc_table[0].index1 = toc_table[0].pregap = 0;
991 toc_table[track_num].index0 = toc_table[track_num].index1 = max_logical_block;
1001 } else if(check_file_extension(file_path, _T(".ccd"))) {
1002 // get image file name
1003 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.img"), get_file_path_without_extensiton(file_path));
1004 if(!FILEIO::IsFileExisting(img_file_path)) {
1005 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.gz"), get_file_path_without_extensiton(file_path));
1006 if(!FILEIO::IsFileExisting(img_file_path)) {
1007 my_stprintf_s(img_file_path, _MAX_PATH, _T("%s.img.gz"), get_file_path_without_extensiton(file_path));
1010 if(fio_img->Fopen(img_file_path, FILEIO_READ_BINARY)) {
1013 // get image file size
1014 if((max_logical_block = fio_img->FileLength() / 2352) > 0) {
1016 FILEIO* fio = new FILEIO();
1017 if(fio->Fopen(file_path, FILEIO_READ_BINARY)) {
1018 char line[1024], *ptr;
1020 while(fio->Fgets(line, 1024) != NULL) {
1021 if(strstr(line, "[Session ") != NULL) {
1023 } else if((ptr = strstr(line, "Point=0x")) != NULL) {
1024 if((track = hexatoi(ptr + 8)) > 0 && track < 0xa0) {
1025 if(track > track_num) {
1029 } else if((ptr = strstr(line, "Control=0x")) != NULL) {
1030 if(track > 0 && track < 0xa0) {
1031 toc_table[track - 1].is_audio = (hexatoi(ptr + 10) != 4);
1033 } else if((ptr = strstr(line, "ALBA=-")) != NULL) {
1034 if(track > 0 && track < 0xa0) {
1035 toc_table[track - 1].pregap = atoi(ptr + 6);
1037 } else if((ptr = strstr(line, "PLBA=")) != NULL) {
1038 if(track > 0 && track < 0xa0) {
1039 toc_table[track - 1].index1 = atoi(ptr + 5);
1043 if(track_num != 0) {
1044 toc_table[0].lba_offset = 0;
1045 for(int i = 1; i < track_num; i++) {
1046 toc_table[i].index0 = toc_table[i].index1 - toc_table[i].pregap;
1047 toc_table[i].lba_offset = toc_table[i].index0;
1049 toc_table[0].index0 = toc_table[0].index1 = toc_table[0].pregap = 0;
1050 toc_table[track_num].index0 = toc_table[track_num].index1 = max_logical_block;
1060 #ifdef _SCSI_DEBUG_LOG
1062 for(int i = 0; i < track_num + 1; i++) {
1063 uint32_t idx0_msf = lba_to_msf(i, toc_table[i].index0);
1064 uint32_t idx1_msf = lba_to_msf(i, toc_table[i].index1);
1065 uint32_t pgap_msf = lba_to_msf(i, toc_table[i].pregap);
1066 this->out_debug_log(_T("Track%02d: Index0=%02x:%02x:%02x Index1=%02x:%02x:%02x PreGpap=%02x:%02x:%02x\n"), i + 1,
1067 (idx0_msf >> 16) & 0xff, (idx0_msf >> 8) & 0xff, idx0_msf & 0xff,
1068 (idx1_msf >> 16) & 0xff, (idx1_msf >> 8) & 0xff, idx1_msf & 0xff,
1069 (pgap_msf >> 16) & 0xff, (pgap_msf >> 8) & 0xff, pgap_msf & 0xff);
1075 void SCSI_CDROM::close()
1077 if(fio_img->IsOpened()) {
1080 memset(toc_table, 0, sizeof(toc_table));
1084 set_cdda_status(CDDA_OFF);
1087 bool SCSI_CDROM::mounted()
1089 if(is_cue) return true;
1090 return fio_img->IsOpened();
1093 bool SCSI_CDROM::accessed()
1095 bool value = access;
1100 void SCSI_CDROM::mix(int32_t* buffer, int cnt)
1102 if(cdda_status == CDDA_PLAYING) {
1103 if(mix_loop_num != 0) {
1104 int tmp_l = 0, tmp_r = 0;
1105 for(int i = 0; i < mix_loop_num; i++) {
1106 event_callback(EVENT_CDDA, 0);
1107 tmp_l += cdda_sample_l;
1108 tmp_r += cdda_sample_r;
1110 cdda_sample_l = tmp_l / mix_loop_num;
1111 cdda_sample_r = tmp_r / mix_loop_num;
1113 int32_t val_l = apply_volume(apply_volume(cdda_sample_l, volume_m), volume_l);
1114 int32_t val_r = apply_volume(apply_volume(cdda_sample_r, volume_m), volume_r);
1116 for(int i = 0; i < cnt; i++) {
1117 *buffer++ += val_l; // L
1118 *buffer++ += val_r; // R
1123 void SCSI_CDROM::set_volume(int ch, int decibel_l, int decibel_r)
1125 volume_l = decibel_to_volume(decibel_l);
1126 volume_r = decibel_to_volume(decibel_r);
1129 void SCSI_CDROM::set_volume(int volume)
1131 volume_m = (int)(1024.0 * (max(0, min(100, volume)) / 100.0));
1134 #define STATE_VERSION 3
1136 // Q: If loading state when using another (saved) image? 20181013 K.O
1137 // May need close() and open() (or ...?).
1138 bool SCSI_CDROM::process_state(FILEIO* state_fio, bool loading)
1140 uint32_t offset = 0;
1142 if(!state_fio->StateCheckUint32(STATE_VERSION)) {
1145 if(!state_fio->StateCheckInt32(this_device_id)) {
1148 state_fio->StateValue(cdda_start_frame);
1149 state_fio->StateValue(cdda_end_frame);
1150 state_fio->StateValue(cdda_playing_frame);
1151 state_fio->StateValue(cdda_status);
1152 state_fio->StateValue(cdda_repeat);
1153 state_fio->StateValue(cdda_interrupt);
1154 state_fio->StateArray(cdda_buffer, sizeof(cdda_buffer), 1);
1155 state_fio->StateValue(cdda_buffer_ptr);
1156 state_fio->StateValue(cdda_sample_l);
1157 state_fio->StateValue(cdda_sample_r);
1158 state_fio->StateValue(event_cdda);
1159 // state_fio->StateValue(mix_loop_num);
1160 state_fio->StateValue(volume_m);
1162 offset = state_fio->FgetUint32_LE();
1164 if(fio_img->IsOpened()) {
1165 offset = fio_img->Ftell();
1167 state_fio->FputUint32_LE(offset);
1170 state_fio->StateValue(is_cue);
1171 state_fio->StateValue(current_track);
1172 // ToDo: Re-Open Image.20181118 K.O
1174 if(loading && fio_img->IsOpened()) {
1175 fio_img->Fseek(offset, FILEIO_SEEK_SET);
1177 return SCSI_DEV::process_state(state_fio, loading);