changeset 2467:bf8a77a8ddc4

Fix some YMZ263B timer issues
author Michael Pavone <pavone@retrodev.com>
date Sat, 24 Feb 2024 21:07:38 -0800
parents b5640ac9aea9
children 0ca78837e4d2
files ymz263b.c
diffstat 1 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/ymz263b.c	Sat Feb 24 20:41:02 2024 -0800
+++ b/ymz263b.c	Sat Feb 24 21:07:38 2024 -0800
@@ -319,6 +319,7 @@
 		}
 		if (timer_ctrl & BIT_STBC) {
 			ymz->timers[3]--;
+			ymz->timers[3] &= 0xFFF;
 			if (!ymz->timers[3]) {
 				ymz->timers[3] = ymz->base_regs[YMZ_TIMER1] << 8 & 0xF00;
 				ymz->timers[3] |= ymz->base_regs[YMZ_TIMER_BASE];
@@ -472,13 +473,13 @@
 				load = 0x1000;
 			}
 			if (enabled_ints & BIT_ST1) {
-				uint32_t t1 = (ymz->timers[1] ? ymz->timers[1] - 1 : 0xF) * load * ymz->clock_inc;
+				uint32_t t1 = base + (ymz->timers[1] ? ymz->timers[1] - 1 : 0xF) * load * ymz->clock_inc;
 				if (t1 < ret) {
 					ret = t1;
 				}
 			}
 			if (enabled_ints & BIT_ST2) {
-				uint32_t t2 = (ymz->timers[2] ? ymz->timers[2] - 1 : 0xFFFF) * load * ymz->clock_inc;
+				uint32_t t2 = base + (ymz->timers[2] ? ymz->timers[2] - 1 : 0xFFFF) * load * ymz->clock_inc;
 				if (t2 < ret) {
 					ret = t2;
 				}
@@ -515,6 +516,21 @@
 		}
 	} else {
 		if (ymz->address < YMZ_PCM_PLAY_CTRL) {
+			if (ymz->address == YMZ_TIMER_CTRL) {
+				uint8_t newly_set = (ymz->base_regs[ymz->address] ^ value) & value;
+				if (newly_set & BIT_ST0) {
+					ymz->timers[0] = ymz->base_regs[YMZ_TIMER0_HIGH] << 8 | ymz->base_regs[YMZ_TIMER0_LOW];
+				}
+				if (newly_set & BIT_ST1) {
+					ymz->timers[1] = ymz->base_regs[YMZ_TIMER1] >> 4;
+				}
+				if (newly_set & BIT_ST2) {
+					ymz->timers[2] = ymz->base_regs[YMZ_TIMER2_HIGH] << 8 | ymz->base_regs[YMZ_TIMER2_LOW];
+				}
+				if (newly_set & BIT_STBC) {
+					ymz->timers[0] = (ymz->base_regs[YMZ_TIMER1] << 8 | ymz->base_regs[YMZ_TIMER_BASE]) & 0xFFF;
+				}
+			}
 			ymz->base_regs[ymz->address] = value;
 		} else if (ymz->address < YMZ_MIDI_CTRL) {
 			if (((value ^ ymz->pcm[0].regs[0]) & ymz->pcm[1].regs[0]) & BIT_ADP_RST) {