Minor improvement in timing and jitter of simulation

r3654 | dcl | 2012-07-19 14:55:45 +1000 (Thu, 19 Jul 2012) | 1 line
This commit is contained in:
Douglas Clowes
2012-07-19 14:55:45 +10:00
parent 87e623c9af
commit 45f27b98cc

View File

@@ -895,9 +895,12 @@ typedef struct device_private_t
{
/** extended 64-bit counter value */
unsigned long long count64;
/* TODO time and count */
int source; /* count rate to be simulated */
struct timeval last_time;
/** count rate to be simulated */
int source;
/** time that count64 was last updated */
struct timeval prev_time;
/** time of most recent read operation */
struct timeval curr_time;
} DEVICE_PRIVATE;
int hware_ctor(const char* device_name, pHWARE* ptr)
@@ -908,56 +911,70 @@ int hware_ctor(const char* device_name, pHWARE* ptr)
*ptr = hware;
memset(hware, 0, sizeof(DEVICE_PRIVATE));
/* TODO time and count */
/* set initital conditions */
hware->source = 1000; /* initial count rate */
gettimeofday(&hware->last_time, NULL);
#if 0 /* DEBUG */
printf("hware_ctor: %s at %p\n", device_name, *ptr);
#endif
hware->count64 = 0; /* initial counter value */
gettimeofday(&hware->curr_time, NULL); /* current time */
hware->prev_time = hware->curr_time; /* update time */
return 0;
}
int hware_read(pHWARE hware, unsigned long long* value)
{
struct timeval this_time;
double delta_time;
long long delta_count = 0;
/* TODO time and count */
gettimeofday(&this_time, NULL);
delta_time = (this_time.tv_sec + 1e-6 * this_time.tv_usec) -
(hware->last_time.tv_sec + 1e-6 * hware->last_time.tv_usec);
delta_count = (long long)(0.5 + 1000 * delta_time * hware->source);
hware->count64 += delta_count;
hware->last_time = this_time;
*value = hware->count64 / 1000;
unsigned long long delta_count = 0;
unsigned long long total_count = 0;
/* calculate time since count64 update in microseconds */
gettimeofday(&hware->curr_time, NULL);
delta_time = (hware->curr_time.tv_sec + 1e-6 * hware->curr_time.tv_usec) -
(hware->prev_time.tv_sec + 1e-6 * hware->prev_time.tv_usec);
/* calculate counts since then */
delta_count = (long long)(0.5 + delta_time * hware->source);
/* calculate the new total counts */
total_count = hware->count64 + delta_count;
/* periodically update count64 */
if (delta_time > 60) {
hware->count64 = total_count;
hware->prev_time = hware->curr_time;
}
/* return the total count */
*value = total_count;
return 0;
}
/*
* Change the count rate
*/
int hware_source(pHWARE hware, int value)
{
#if 0 /* DEBUG */
int old_source = hware->source;
#endif
/* calculate the new count rate */
int new_source;
switch (value)
{
case 0:
hware->source = 10000;
new_source = 10000;
break;
case 1:
hware->source = 20000000;
new_source = 20000000;
break;
case 2:
hware->source = 100000;
new_source = 100000;
break;
case 3:
hware->source = 80000000;
new_source = 80000000;
break;
default:
hware->source = value;
new_source = abs(value);
break;
}
#if 0 /* DEBUG */
printf("hware_source: %d moves %d to %d at %p\n", value, old_source, hware->source, hware);
#endif
/* only do this if it has changed */
if (new_source != hware->source) {
/* save the current count where the slope changes */
hware_read(hware, &hware->count64);
/* save the current time where the slope changes */
hware->prev_time = hware->curr_time;
/* save the new slope */
hware->source = new_source;
}
return 0;
}
int hware_filter(pHWARE hware, int value)