From 83cc46dae321701579fac7c57500111b37f97c95 Mon Sep 17 00:00:00 2001 From: Toni Wilen Date: Sun, 27 Nov 2022 13:45:17 +0200 Subject: [PATCH] Fix CIA-A TOD counting if CIA internal delays caused it to cross scanline. --- cia.cpp | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/cia.cpp b/cia.cpp index a9169b63..5b7ba2b3 100644 --- a/cia.cpp +++ b/cia.cpp @@ -1046,8 +1046,9 @@ static void CIA_tod_inc(bool irq, int num) { struct CIA *c = &cia[num]; c->tod_event_state = 3; // done - if (!c->todon) + if (!c->todon) { return; + } c->tod++; c->tod &= 0xFFFFFF; cia_checkalarm(true, irq, num); @@ -1056,8 +1057,9 @@ static void CIA_tod_inc(bool irq, int num) static void CIA_tod_inc_event(uae_u32 num) { struct CIA *c = &cia[num]; - if (c->tod_event_state != 2) + if (c->tod_event_state != 2) { return; + } CIA_tod_inc(true, num); } @@ -1093,12 +1095,18 @@ static void CIA_tod_check(int num) event2_newevent_xx(-1, -hpos * CYCLE_UNIT, num, CIA_tod_inc_event); } -static void CIA_tod_handler(int hoffset, int num) +static void CIA_tod_handler(int hoffset, int num, bool delayedevent) { struct CIA *c = &cia[num]; c->tod_event_state = 0; c->tod_offset = tod_inc_delay(hoffset); if (c->tod_offset >= maxhpos) { + if (!delayedevent) { + return; + } + // crossed scanline, increase in next line + c->tod_offset -= maxhpos; + c->tod_event_state = 4; return; } c->tod_event_state = 1; // TOD inc needed @@ -1118,11 +1126,11 @@ void CIAA_tod_handler(int hoffset) return; } #endif - CIA_tod_handler(hoffset, 0); + CIA_tod_handler(hoffset, 0, true); } void CIAB_tod_handler(int hoffset) { - CIA_tod_handler(hoffset, 1); + CIA_tod_handler(hoffset, 1, false); } void keyboard_connected(bool connect) @@ -1171,8 +1179,9 @@ static void check_keyboard(void) static void cia_delayed_tod(int num) { struct CIA *c = &cia[num]; - if (c->tod_event_state == 1) + if (c->tod_event_state == 1) { CIA_tod_inc(false, num); + } c->tod_event_state = 0; c->tod_offset = -1; } @@ -1203,6 +1212,10 @@ void CIA_hsync_posthandler(bool ciahsync, bool dotod) if (!ciahsync) { // Increase CIA-A TOD if delayed from previous line + struct CIA *c = &cia[0]; + if (c->tod_event_state == 4) { + c->tod_event_state = 1; + } cia_delayed_tod(0); if (currprefs.tod_hack && cia[0].todon) { do_tod_hack(); -- 2.47.3