]> git.unchartedbackwaters.co.uk Git - francis/winuae.git/commitdiff
Fix CIA-A TOD counting if CIA internal delays caused it to cross scanline.
authorToni Wilen <twilen@winuae.net>
Sun, 27 Nov 2022 11:45:17 +0000 (13:45 +0200)
committerToni Wilen <twilen@winuae.net>
Sun, 27 Nov 2022 11:45:17 +0000 (13:45 +0200)
cia.cpp

diff --git a/cia.cpp b/cia.cpp
index a9169b63f8df71414373546ac14b3dc56be44ed7..5b7ba2b32cf2495d031c1a3da8e4e1a3ceb2f77a 100644 (file)
--- 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();