-
Notifications
You must be signed in to change notification settings - Fork 12
/
sdl_timer.c
106 lines (87 loc) · 1.94 KB
/
sdl_timer.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <math.h>
#if defined(DIGGER_DEBUG)
#include <stdio.h>
#endif
#include <SDL.h>
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#define SDL_Delay(x) emscripten_sleep(x)
#endif
#include "def.h"
#include "digger_math.h"
#include "hardware.h"
#include "game.h"
#ifdef _SDL
#include "sdl_vid.h"
#endif
#if defined(DIGGER_DEBUG)
#include "digger_log.h"
#endif
static struct PFD phase_detector;
static struct recfilter *loop_error;
void inittimer(void)
{
double tfreq;
tfreq = 1000000.0 / dgstate.ftime;
loop_error = recfilter_init(tfreq, 0.1);
PFD_init(&phase_detector, 0.0);
#if defined(DIGGER_DEBUG)
fprintf(digger_log, "inittimer: ftime = %u\n", dgstate.ftime);
#endif
}
void
gethrt(bool minsleep)
{
uint32_t add_delay;
double eval, clk_rl, tfreq, add_delay_d, filterval;
static double cum_error = 0.0;
if (dgstate.ftime <= 1) {
doscreenupdate();
if (minsleep)
SDL_Delay(10);
return;
}
tfreq = 1000000.0 / dgstate.ftime;
clk_rl = (double)SDL_GetTicks() * tfreq / 1000.0;
eval = PFD_get_error(&phase_detector, clk_rl);
if (eval != 0) {
filterval = recfilter_apply(loop_error, sigmoid(eval));
} else {
filterval = recfilter_getlast(loop_error);
}
add_delay_d = (freqoff_to_period(tfreq, 1.0, filterval) * 1000.0) + cum_error;
add_delay = round(add_delay_d);
cum_error = add_delay_d - (double)add_delay;
#if defined(DIGGER_DEBUG)
fprintf(digger_log, "clk_rl = %f, add_delay = %d, eval = %f, filterval = %f, cum_error = %f\n",
clk_rl, add_delay, eval, filterval, cum_error);
#endif
doscreenupdate();
SDL_Delay(add_delay);
return;
}
int32_t getkips(void)
{
return(1);
}
void s0soundoff(void)
{
}
void s0setspkrt2(void)
{
}
void s0settimer0(uint16_t t0v)
{
}
void s0settimer2(uint16_t t0v, bool mode)
{
}
void s0timer0(uint16_t t0v)
{
}
void s0timer2(uint16_t t0v, bool mode)
{
}
void s0soundkillglob(void)
{
}