From 18ea4397883d16d15251e3796ea249b6b2e54e5a Mon Sep 17 00:00:00 2001 From: Phil Hord Date: Tue, 29 Oct 2013 07:52:23 -0400 Subject: [PATCH] simulator: fix timer overflow problem Next-interrupt-time calculations were made in 16-bit registers but moved to 32-bit ones for convenience. But they forgot to round off at 16-bits. Force the round-off so we do not wait forever. --- simulator/timer_ext.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/simulator/timer_ext.c b/simulator/timer_ext.c index ee11ef8..ee426b4 100644 --- a/simulator/timer_ext.c +++ b/simulator/timer_ext.c @@ -115,13 +115,13 @@ void sim_setTimer() { //-- Calculate time in clock ticks until next timer events if (TIMSK1 & MASK(OCIE1A)) { sim_debug("Timer1 Interrupt A: Enabled"); - nextA = OCR1A - now; + nextA = (OCR1A - now) & 0xFFFF ; // 0 = No timer; 1-0x10000 = time until next occurrence if ( ! nextA) nextA = 0x10000; } if (TIMSK1 & MASK(OCIE1B)) { sim_debug("Timer1 Interrupt B: Enabled"); - nextB = OCR1B - now; + nextB = (OCR1B - now) & 0xFFFF; // 0 = No timer; 1-0x10000 = time until next occurrence if ( ! nextB) nextB = 0x10000; }