changeset 2073:c69e42444f96

Fix some cycle adjustment stuff and an off-by one on hte TOCT response
author Michael Pavone <pavone@retrodev.com>
date Mon, 31 Jan 2022 00:01:15 -0800
parents cc13c100b027
children c5323c02dde4
files cdd_mcu.c lc8951.c segacd.c
diffstat 3 files changed, 32 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/cdd_mcu.c	Sun Jan 30 22:29:29 2022 -0800
+++ b/cdd_mcu.c	Mon Jan 31 00:01:15 2022 -0800
@@ -255,8 +255,8 @@
 			if (context->toc_valid) {
 				context->status_buffer.b.toct.first_track_high = 0;
 				context->status_buffer.b.toct.first_track_low = 1;
-				context->status_buffer.b.toct.last_track_high = (context->media->num_tracks + 1) / 10;
-				context->status_buffer.b.toct.last_track_low = (context->media->num_tracks + 1) % 10;
+				context->status_buffer.b.toct.last_track_high = (context->media->num_tracks) / 10;
+				context->status_buffer.b.toct.last_track_low = (context->media->num_tracks) % 10;
 				context->status_buffer.b.toct.version = 0;
 				context->status_buffer.format = SF_TOCT;
 			} else {
@@ -265,6 +265,10 @@
 			break;
 		case SF_TOCN:
 			if (context->toc_valid) {
+				if (context->requested_track > context->media->num_tracks) {
+					printf("track number %d is bad\n", context->requested_track);
+					exit(0);
+				}
 				uint32_t lba = context->media->tracks[context->requested_track - 1].start_lba;
 				for (uint32_t i = 0; i < context->requested_track; i++) {
 					lba += context->media->tracks[i].fake_pregap;
@@ -407,6 +411,7 @@
 			if (!context->media || context->requested_track > context->media->num_tracks) {
 				context->requested_format = SF_ABSOLUTE;
 				context->error_status = DS_CMD_ERROR;
+				break;
 			}
 			context->status = DS_TOC_READ;
 			context->seeking = 1;
@@ -581,16 +586,33 @@
 void cdd_mcu_adjust_cycle(cdd_mcu *context, uint32_t deduction)
 {
 	uint32_t cd_deduction = mclks_to_cd_block(deduction);
+	if (context->cycle > cd_deduction) {
+		context->cycle -= cd_deduction;
+	} else {
+		context->cycle = 0;
+	}
 	if (context->next_int_cycle != CYCLE_NEVER) {
 		context->next_int_cycle -= deduction;
 	}
 	if (context->last_subcode_cycle != CYCLE_NEVER) {
-		context->last_subcode_cycle -= cd_deduction;
+		if (context->last_subcode_cycle > cd_deduction) {
+			context->last_subcode_cycle -= cd_deduction;
+		} else {
+			context->last_subcode_cycle = 0;
+		}
 	}
 	if (context->last_nibble_cycle != CYCLE_NEVER) {
-		context->last_nibble_cycle -= cd_deduction;
+		if (context->last_nibble_cycle > cd_deduction) {
+			context->last_nibble_cycle -= cd_deduction;
+		} else {
+			context->last_nibble_cycle = 0;
+		}
 	}
 	if (context->last_byte_cycle != CYCLE_NEVER) {
-		context->last_byte_cycle -= cd_deduction;
+		if (context->last_byte_cycle > cd_deduction) {
+			context->last_byte_cycle -= cd_deduction;
+		} else {
+			context->last_byte_cycle = 0;
+		}
 	}
 }
--- a/lc8951.c	Sun Jan 30 22:29:29 2022 -0800
+++ b/lc8951.c	Mon Jan 31 00:01:15 2022 -0800
@@ -317,10 +317,13 @@
 
 void lc8951_adjust_cycles(lc8951 *context, uint32_t deduction)
 {
+	printf("CDC deduction of %u cycles @ %u, ", deduction, context->cycle);
+	context->cycle -= deduction;
 	if (context->decode_end != CYCLE_NEVER) {
 		context->decode_end -= deduction;
 	}
 	if (context->transfer_end != CYCLE_NEVER) {
 		context->transfer_end -= deduction;
 	}
+	printf("cycle is now %u, decode_end %u, transfer_end %u\n", context->cycle, context->decode_end, context->transfer_end);
 }
--- a/segacd.c	Sun Jan 30 22:29:29 2022 -0800
+++ b/segacd.c	Mon Jan 31 00:01:15 2022 -0800
@@ -597,9 +597,9 @@
 	case GA_CDD_CTRL: {
 		cdd_run(cd, m68k->current_cycle);
 		uint16_t changed = cd->gate_array[reg] ^ value;
-		cd->gate_array[reg] &= ~BIT_HOCK;
-		cd->gate_array[reg] |= value & BIT_HOCK;
 		if (changed & BIT_HOCK) {
+			cd->gate_array[reg] &= ~BIT_HOCK;
+			cd->gate_array[reg] |= value & BIT_HOCK;
 			if (value & BIT_HOCK) {
 				cdd_hock_enabled(&cd->cdd);
 			} else {
@@ -928,11 +928,6 @@
 		return 0xFFFF;
 	default:
 		if (offset < GA_TIMER) {
-			if (offset == GA_CDC_CTRL) {
-				printf("CDC read(main): %X - %X @ %u (%u)\n", address, cd->gate_array[offset], m68k->current_cycle, scd_cycle);
-			} else if (offset >= GA_COMM_FLAG && offset <= GA_COMM_STATUS7) {
-				printf("COMM read(main): %X - %X @ %u (%u)\n", address, cd->gate_array[offset], m68k->current_cycle, scd_cycle);
-			}
 			return cd->gate_array[offset];
 		}
 		//TODO: open bus maybe?
@@ -1049,7 +1044,6 @@
 		//Main CPU can only write the upper byte;
 		cd->gate_array[reg] &= 0xFF;
 		cd->gate_array[reg] |= value & 0xFF00;
-		printf("COMM write(main): %X - %X @ %u (%u)\n", address, value, m68k->current_cycle, scd_cycle);
 		break;
 	case GA_COMM_CMD0:
 	case GA_COMM_CMD1:
@@ -1060,7 +1054,6 @@
 	case GA_COMM_CMD6:
 	case GA_COMM_CMD7:
 		//no effects for these other than saving the value
-		printf("COMM write(main): %X - %X @ %u (%u)\n", address, value, m68k->current_cycle, scd_cycle);
 		cd->gate_array[reg] = value;
 		break;
 	default: