diff --git a/GenerateCPPCSP.hs b/GenerateCPPCSP.hs index af6d0fc..22cc759 100644 --- a/GenerateCPPCSP.hs +++ b/GenerateCPPCSP.hs @@ -336,7 +336,7 @@ genCPPCSPTime ops e tell ["unsigned ",curTimeLow," = (unsigned)remainder(1000000.0 * ",curTime,",4294967296.0);"] tell ["unsigned ",curTimeHigh," = (unsigned)((1000000.0 * ",curTime,") / 4294967296.0);"] --if time is less than curTime, it must have wrapped around so add one: - tell ["csp::Time ",retTime," = csp::Seconds((((double)(",curTimeHigh," + (",time," < ",curTimeLow, " ? 1 : 0)) * 4294967296.0) + (double)",time,") / 1000000.0);"] + tell ["csp::Time ",retTime," = csp::Seconds((((double)(",curTimeHigh," + TimeDiffHelper(",curTimeLow,",",time,")) * 4294967296.0) + (double)",time,") / 1000000.0);"] return retTime cppgenTimerWait :: GenOps -> A.Expression -> CGen () diff --git a/tock_support_cppcsp.h b/tock_support_cppcsp.h index bd3129f..471772e 100644 --- a/tock_support_cppcsp.h +++ b/tock_support_cppcsp.h @@ -35,6 +35,35 @@ public: #include #include +inline unsigned TimeDiffHelper(unsigned now,unsigned waitFor) +{ + //If waitFor is in the 2^31 before now, it is in the past: + if (now - waitFor < 2147483648) + { + //if we have wrapped round between waitFor and now, we actually want to return a minus-one: + if ((waitFor >= 2147483648) && (now < 2147483648)) + { + return (unsigned)(int)(-1); + } + else + { + return 0; + } + } + else + { + //It is in the future. If we will wrap round before getting there, return one: + if ((now >= 2147483648) && (waitFor < 2147483648)) + { + return 1; + } + else + { + return 0; + } + } +} + class StreamWriter : public csp::CSProcess { private: