# HG changeset patch # User Michael Pavone # Date 1644184309 28800 # Node ID 0db3af42dd72666de04e82b9de9af7074a64c47b # Parent c716af3f89807174fff6c128e57323abae34de4b Fix some byte order stuff for audio tracks diff -r c716af3f8980 -r 0db3af42dd72 cdd_mcu.c --- a/cdd_mcu.c Sun Feb 06 13:51:09 2022 -0800 +++ b/cdd_mcu.c Sun Feb 06 13:51:49 2022 -0800 @@ -39,7 +39,7 @@ context->next_int_cycle = CYCLE_NEVER; context->last_subcode_cycle = CYCLE_NEVER; context->last_nibble_cycle = CYCLE_NEVER; - context->last_byte_cycle = 0; + context->next_byte_cycle = 0; context->requested_format = SF_NOTREADY; context->media = media; context->current_status_nibble = -1; @@ -510,15 +510,13 @@ void cdd_mcu_run(cdd_mcu *context, uint32_t cycle, uint16_t *gate_array, lc8951* cdc, cdd_fader* fader) { uint32_t cd_cycle = mclks_to_cd_block(cycle); - uint32_t next_byte = context->last_byte_cycle + BYTE_CLOCKS; if (!(gate_array[GAO_CDD_CTRL] & BIT_HOCK)) { //it's a little unclear if this gates the actual cd block clock or just handshaking //assum it's actually the clock for now for (; context->cycle < cd_cycle; context->cycle += CDD_MCU_DIVIDER) { - if (context->cycle >= next_byte) { + if (context->cycle >= context->next_byte_cycle) { cdd_fader_data(fader, 0); - next_byte = context->cycle + BYTE_CLOCKS; - context->last_byte_cycle = context->cycle; + context->next_byte_cycle += BYTE_CLOCKS; } } gate_array[GAO_CDD_CTRL] |= BIT_MUTE; @@ -585,7 +583,7 @@ next_cmd_nibble = context->cycle + NIBBLE_CLOCKS; } } - if (context->cycle >= next_byte) { + if (context->cycle >= context->next_byte_cycle) { if (context->current_sector_byte >= 0) { uint8_t byte = context->media->read(context->media, context->current_sector_byte); lc8951_write_byte(cdc, cd_block_to_mclks(context->cycle), context->current_sector_byte++, byte); @@ -593,11 +591,10 @@ } else { cdd_fader_data(fader, 0); } - context->last_byte_cycle = context->cycle; if (context->current_sector_byte == 2352) { context->current_sector_byte = -1; } - next_byte = context->cycle + BYTE_CLOCKS; + context->next_byte_cycle += BYTE_CLOCKS; } } } @@ -654,11 +651,5 @@ context->last_nibble_cycle = 0; } } - if (context->last_byte_cycle != CYCLE_NEVER) { - if (context->last_byte_cycle > cd_deduction) { - context->last_byte_cycle -= cd_deduction; - } else { - context->last_byte_cycle = 0; - } - } + context->next_byte_cycle -= deduction; } diff -r c716af3f8980 -r 0db3af42dd72 cdd_mcu.h --- a/cdd_mcu.h Sun Feb 06 13:51:09 2022 -0800 +++ b/cdd_mcu.h Sun Feb 06 13:51:49 2022 -0800 @@ -138,7 +138,7 @@ uint32_t next_int_cycle; //this is in SCD MCLKS uint32_t last_subcode_cycle; uint32_t last_nibble_cycle; - uint32_t last_byte_cycle; + uint32_t next_byte_cycle; int current_status_nibble; int current_cmd_nibble; int current_sector_byte; diff -r c716af3f8980 -r 0db3af42dd72 cue.c --- a/cue.c Sun Feb 06 13:51:09 2022 -0800 +++ b/cue.c Sun Feb 06 13:51:49 2022 -0800 @@ -52,7 +52,7 @@ { media->cur_sector = sector; uint32_t lba = sector; - uint8_t track; + uint32_t track; for (track = 0; track < media->num_tracks; track++) { if (lba < media->tracks[track].fake_pregap) { @@ -69,6 +69,9 @@ break; } } + if (track < media->num_tracks) { + media->cur_track = track; + } if (!media->in_fake_pregap) { fseek(media->f, lba * 2352, SEEK_SET); } @@ -103,6 +106,12 @@ } else if (media->in_fake_pregap == FAKE_AUDIO) { return 0; } else { + if (media->tracks[media->cur_track].need_swap) { + if (offset & 1) { + return media->byte_storage; + } + media->byte_storage = fgetc(media->f); + } return fgetc(media->f); } } @@ -147,6 +156,7 @@ media->tracks = tracks; line = media->buffer; int track = -1; + uint8_t audio_byte_swap = 0; do { char *cmd = cmd_start(line); if (cmd) { @@ -161,6 +171,7 @@ cmd = cmd_start(end); if (cmd) { tracks[track].type = startswith(cmd, "AUDIO") ? TRACK_AUDIO : TRACK_DATA; + tracks[track].need_swap = tracks[track].type == TRACK_AUDIO && audio_byte_swap; } } else if (startswith(cmd, "FILE ")) { if (media->f) { @@ -191,6 +202,19 @@ fatal_error("Failed to open %s specified by FILE command in CUE sheet %s.%s\n", fname, media->name, media->extension); } free(fname); + for (end++; *end && *end != '\n' && *end != '\r'; end++) + { + if (!isspace(*end)) { + if (startswith(end, "BINARY")) { + audio_byte_swap = 0; + } else if (startswith(end, "MOTOROLA")) { + audio_byte_swap = 1; + } else { + warning("Unsupported FILE type in CUE sheet. Only BINARY and MOTOROLA are supported\n"); + } + break; + } + } } } } @@ -227,13 +251,15 @@ //replace cue sheet with first sector free(media->buffer); media->buffer = calloc(2048, 1); - if (tracks[0].type = TRACK_DATA) { + if (tracks[0].type == TRACK_DATA) { // if the first track is a data track, don't trust the CUE sheet and look at the MM:SS:FF from first sector uint8_t msf[3]; fseek(media->f, 12, SEEK_SET); if (sizeof(msf) == fread(msf, 1, sizeof(msf), media->f)) { tracks[0].fake_pregap = msf[2] + (msf[0] * 60 + msf[1]) * 75; } + } else if (!tracks[0].start_lba && !tracks[0].fake_pregap) { + tracks[0].fake_pregap = 2 * 75; } fseek(media->f, 16, SEEK_SET); diff -r c716af3f8980 -r 0db3af42dd72 system.h --- a/system.h Sun Feb 06 13:51:09 2022 -0800 +++ b/system.h Sun Feb 06 13:51:49 2022 -0800 @@ -95,6 +95,7 @@ uint32_t pregap_lba; uint32_t start_lba; uint32_t end_lba; + uint8_t need_swap; track_type type; } track_info; @@ -112,10 +113,12 @@ seek_fun seek; read_fun read; uint32_t num_tracks; + uint32_t cur_track; uint32_t size; uint32_t cur_sector; media_type type; uint8_t in_fake_pregap; + uint8_t byte_storage; }; #define OPT_ADDRESS_LOG (1U << 31U)