--- np2/timing.c 2003/10/23 12:58:44 1.3 +++ np2/timing.c 2004/02/09 20:39:39 1.7 @@ -5,47 +5,55 @@ #include "timing.h" -static UINT timercnt = 0; -static UINT32 tick = 0; -static UINT32 cnt = 0; +#define MSSHIFT 16 + +typedef struct { + UINT32 tick; + UINT32 msstep; + UINT cnt; + UINT32 fraction; +} TIMING; + +static TIMING timing; void timing_reset(void) { - cnt = 0; - timercnt = 0; - tick = GETTICK(); + timing.tick = GETTICK(); + timing.cnt = 0; + timing.fraction = 0; +} + +void timing_setrate(UINT lines, UINT crthz) { + + timing.msstep = (crthz << (MSSHIFT - 3)) / lines / (1000 >> 3); } void timing_setcount(UINT value) { - timercnt = value; + timing.cnt = value; } -UINT timing_getcount(void) { // ver0.12 60.0Hz вк 56.4Hz +UINT timing_getcount(void) { UINT32 ticknow; UINT32 span; - UINT steps; + UINT32 fraction; ticknow = GETTICK(); - span = ticknow - tick; + span = ticknow - timing.tick; if (span) { + timing.tick = ticknow; fddmtr_callback(ticknow); - tick = ticknow; - if (span < 1000) { - cnt += (span * 388); - steps = cnt / 6875; - timercnt += steps; - cnt -= (steps * 6875); - } - else { - cnt = 0; - timercnt += 56; + if (span >= 1000) { + span = 1000; } + fraction = timing.fraction + (span * timing.msstep); + timing.cnt += fraction >> MSSHIFT; + timing.fraction = fraction & ((1 << MSSHIFT) - 1); } - return(timercnt); + return(timing.cnt); } void timing_term(void) {