changeset 2089:0db3af42dd72

Fix some byte order stuff for audio tracks
author Michael Pavone <pavone@retrodev.com>
date Sun, 06 Feb 2022 13:51:49 -0800
parents c716af3f8980
children 00b6592cad42
files cdd_mcu.c cdd_mcu.h cue.c system.h
diffstat 4 files changed, 38 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- 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;
 }
--- 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;
--- 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);
--- 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)