aboutsummaryrefslogtreecommitdiffstats
path: root/clock.c
blob: 5171b505ef79513974992a206e6480146533e7ed (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include "compiler.h"
#include "z80.h"
#include "z80irq.h"
#include "screen.h"
#include "abcio.h"
#include "clock.h"
#include "nstime.h"

static void abc80_clock_tick(void);
static void abc800_clock_tick(void);
static struct abctimer *ctc_timer[4];

/*
 * Initialize the time for next event
 */
struct abctimer {
    uint64_t period;            /* Period in ns */
    uint64_t last;              /* Last checkpoint in ns */
    uint64_t ltst;              /* TSTATE value for last checkpoint */
    void (*func) (void);
};

#define MAX_TIMERS 2
static struct abctimer timers[MAX_TIMERS];
static int ntimers;

#define MS(x) ((x) * INT64_C(1000000))

static struct abctimer *create_timer(uint64_t period, void (*func) (void))
{
    struct abctimer *t;

    if (ntimers >= MAX_TIMERS)
        abort();

    t = &timers[ntimers++];

    t->period = period;
    t->func = func;
    t->last = 0;
    t->ltst = 0;

    return t;
}

static unsigned int poll_tstate_period;
#define MAX_TSTATE_PERIOD 512

void timer_init(void)
{
    nstime_init();

    /* Limit polling to once every μs simulated time */
    poll_tstate_period = 1000 * ns_per_tstate;
    if (!limit_speed || poll_tstate_period > MAX_TSTATE_PERIOD)
        poll_tstate_period = MAX_TSTATE_PERIOD;

    if (is_abc80()) {
        /* 20 ms = 50 Hz */
        create_timer(MS(20), abc80_clock_tick);
    } else {
        /* 10.67 ms = 93.75 Hz */
        ctc_timer[3] = create_timer(10666667, abc800_clock_tick);

        /* 20 ms = 50 Hz */
        create_timer(MS(20), abc800_vsync);
    }
}

static inline bool trigger(uint64_t now, struct abctimer *tmr)
{
    /* This expression: a) will overflow safely, b) will never trigger for 0 */
    if (likely((now - tmr->last) <= (tmr->period - 1)))
        return false;

    tmr->last += tmr->period;
    if (unlikely((now - tmr->last) >= tmr->period)) {
        /* Missed tick(s), advance clock to skip missed */
        tmr->last = now - ((now - tmr->last) % tmr->period);
    }

    return true;
}

/* See if it is time to slow down a bit */
static void consider_napping(uint64_t now, uint64_t next)
{
    uint64_t when;
    int64_t ahead, behind;
    static uint64_t ref_time, ref_tstate;

    if (unlikely(now <= ref_time || TSTATE <= ref_tstate))
        goto weird;

    when = ref_time + (TSTATE - ref_tstate) * ns_per_tstate;
    behind = now - when;
    ahead = when - next;

    /* Sanity range check: 100 ms behind or 100 ms ahead of schedule */
    if (unlikely(behind >= MS(200) || ahead >= MS(100)))
        goto weird;

    /* If we are ahead of the next event, hold off and wait for it */
    if (ahead >= 0)
        mynssleep(next, now);
    return;

weird:
    /* We fell too far behind, got suspended, or the clock jumped... */
    ref_time = now;
    ref_tstate = TSTATE;
}

/* Poll for timers - these the only external event we look for */
atomic_bool z80_quit;

enum z80_cond z80_poll_external(void)
{
    uint64_t now;
    static uint64_t next = 0;
    int i;
    bool sleepy = limit_speed;
    static uint64_t next_check_tstate;

    if (unlikely(z80_quit))
	return Z80_QUIT;

    if (likely(TSTATE < next_check_tstate))
        return 0;

    now = nstime();

    if (unlikely(now >= next)) {
        next = UINT64_MAX;

        for (i = 0; i < ntimers; i++) {
            struct abctimer *t = &timers[i];
            if (t->period) {
                uint64_t tnext = t->last + t->period;
                if (unlikely(now >= tnext)) {
                    t->last += t->period;
                    if (unlikely(now >= tnext)) {
                        /* Missed tick(s), advance to skip missed */
                        t->last = now - (now - t->last) % t->period;
                        tnext = t->last + t->period;
                    }
                    /* TSTATE value corresponding to t->last */
                    t->ltst = TSTATE - (now - t->last) * tstate_per_ns;
                    t->func();
                    sleepy = false;     /* Just in case "now" is too far behind now */
                }
                if (next > tnext)
                    next = tnext;       /* The next event is closer than you thought */
            }
        }
    }

    next_check_tstate = TSTATE + poll_tstate_period;
    if (limit_speed) {
        uint64_t next_ev = TSTATE + (next - now) * tstate_per_ns;
        if (next_ev < next_check_tstate)
            next_check_tstate = next_ev;
    }

    if (sleepy)
        consider_napping(now, next);

    return 0;
}

/*
 * ABC80: Trig a non maskable interrupt in the Z80 on the clock signal.
 */
static void abc80_clock_tick(void)
{
    vsync_screen();             /* Also vertical retrace */
    z80_nmi();
}

/*
 * ABC800: Clock interrupt through the CTC
 */
static uint8_t ctc_ctl[4], ctc_div[4];
static struct z80_irq ctc_irq[4] = {
    IRQ(IRQ800_CTC0, NULL, NULL, NULL),
    IRQ(IRQ800_CTC1, NULL, NULL, NULL),
    IRQ(IRQ800_CTC2, NULL, NULL, NULL),
    IRQ(IRQ800_CTC3, NULL, NULL, NULL),
};

static void abc800_clock_tick(void)
{
    if ((ctc_ctl[3] & 0xc0) == 0x80)
        z80_interrupt(&ctc_irq[3]);
}

/*
 * CTC I/O
 */
void abc800_ctc_out(uint16_t port, uint8_t v)
{
    port &= 3;                  /* Get channel */

    if (ctc_ctl[port] & 4) {
        ctc_div[port] = v;
        ctc_ctl[port] &= ~4;
        return;
    }

    if ((v & 1) == 0) {
        int i;
        v &= ~7;
        for (i = 0; i <= 3; i++)
            ctc_irq[i].vector = v | (i << 1);
        return;
    }

    if (v & 2)
        v = 1;                  /* Reset channel */

    ctc_ctl[port] = v;
}

uint8_t abc800_ctc_in(uint16_t port)
{
    uint8_t v, div;
    const struct abctimer *t;

    port &= 3;
    t = ctc_timer[port];
    div = ctc_div[port];

    if (!t)
        return -1;

    if (limit_speed) {
        /* Interpolate based on TSTATEs (virtual time) */
        v = ((int64_t) t->period -
             (int64_t) (((TSTATE - t->ltst) * ns_per_tstate)) * div) /
            t->period;
    } else {
        /* Interpolate based on nanoseconds (real time) */
        v = ((t->period - (nstime() - t->last)) * div) / t->period;
    }

    return v;
}

void abc800_ctc_init(void)
{
    int i;
    for (i = 0; i < 4; i++)
        z80_register_irq(&ctc_irq[i]);
}