forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.cpp
421 lines (380 loc) · 8.11 KB
/
events.cpp
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
* UAE - The Un*x Amiga Emulator
*
* Event stuff
*
* Copyright 1995-2002 Bernd Schmidt
* Copyright 1995 Alessandro Bissacco
* Copyright 2000-2012 Toni Wilen
*/
#include "sysconfig.h"
#include "sysdeps.h"
#include "options.h"
#include "events.h"
#include "memory.h"
#include "newcpu.h"
#include "uae/ppc.h"
#include "xwin.h"
#include "x86.h"
#include "audio.h"
static const int pissoff_nojit_value = 256 * CYCLE_UNIT;
uae_u32 event_cycles, nextevent, currcycle;
int is_syncline, is_syncline_end;
int cycles_to_next_event;
int max_cycles_to_next_event;
int cycles_to_hsync_event;
uae_u32 start_cycles;
bool event_wait;
frame_time_t vsyncmintime, vsyncmintimepre;
frame_time_t vsyncmaxtime, vsyncwaittime;
int vsynctimebase;
int event2_count;
static void events_fast(void)
{
cycles_do_special();
}
void events_reset_syncline(void)
{
is_syncline = 0;
events_fast();
}
void events_schedule (void)
{
int i;
uae_u32 mintime = ~0L;
for (i = 0; i < ev_max; i++) {
if (eventtab[i].active) {
uae_u32 eventtime = eventtab[i].evtime - currcycle;
if (eventtime < mintime)
mintime = eventtime;
}
}
nextevent = currcycle + mintime;
}
extern void vsync_event_done(void);
extern int vsync_activeheight;
static bool event_check_vsync(void)
{
/* Keep only CPU emulation running while waiting for sync point. */
if (is_syncline == -1) {
if (!isvsync_chipset()) {
events_reset_syncline();
return false;
}
// wait for vblank
audio_finish_pull();
int done = vsync_isdone(NULL);
if (done == -2) {
// if no vsync thread
int vp = target_get_display_scanline(-1);
if (vp < is_syncline_end)
done = 1;
else if (vp > is_syncline_end)
is_syncline_end = vp;
}
if (!done) {
#ifdef WITH_PPC
if (ppc_state) {
uae_ppc_execute_quick();
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
vsync_clear();
vsync_event_done();
} else if (is_syncline == -2) {
if (!isvsync_chipset()) {
events_reset_syncline();
return false;
}
// wait for vblank or early vblank
audio_finish_pull();
int done = vsync_isdone(NULL);
if (done == -2)
done = 0;
int vp = target_get_display_scanline(-1);
if (vp < 0 || vp >= is_syncline_end)
done = 1;
if (!done) {
#ifdef WITH_PPC
if (ppc_state) {
uae_ppc_execute_quick();
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
vsync_clear();
vsync_event_done();
} else if (is_syncline == -3) {
if (!isvsync_chipset()) {
events_reset_syncline();
return false;
}
// not vblank
audio_finish_pull();
int vp = target_get_display_scanline(-1);
if (vp <= 0) {
#ifdef WITH_PPC
if (ppc_state) {
uae_ppc_execute_quick();
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
vsync_clear();
vsync_event_done();
} else if (is_syncline > 0) {
if (!isvsync_chipset()) {
events_reset_syncline();
return false;
}
audio_finish_pull();
// wait for specific scanline
int vp = target_get_display_scanline(-1);
if (vp < 0 || is_syncline > vp) {
#ifdef WITH_PPC
if (ppc_state) {
uae_ppc_execute_check();
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
vsync_event_done();
}
else if (is_syncline <= -100) {
if (!isvsync_chipset()) {
events_reset_syncline();
return false;
}
audio_finish_pull();
// wait for specific scanline
int vp = target_get_display_scanline(-1);
if (vp < 0 || vp >= (-(is_syncline + 100))) {
#ifdef WITH_PPC
if (ppc_state) {
uae_ppc_execute_check();
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
vsync_event_done();
} else if (is_syncline == -10) {
// wait is_syncline_end
if (event_wait) {
int rpt = read_processor_time();
int v = rpt - is_syncline_end;
if (v < 0) {
#ifdef WITH_PPC
if (ppc_state) {
uae_ppc_execute_check();
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
}
events_reset_syncline();
} else if (is_syncline < -10) {
// wait is_syncline_end/vsyncmintime
if (event_wait) {
int rpt = read_processor_time();
int v = rpt - vsyncmintime;
int v2 = rpt - is_syncline_end;
if (v > vsynctimebase || v < -vsynctimebase) {
v = 0;
}
if (v < 0 && v2 < 0) {
#ifdef WITH_PPC
if (ppc_state) {
if (is_syncline == -11) {
uae_ppc_execute_check();
} else {
uae_ppc_execute_quick();
}
}
#endif
if (currprefs.cachesize)
pissoff = pissoff_value;
else
pissoff = pissoff_nojit_value;
return true;
}
}
events_reset_syncline();
}
return false;
}
void do_cycles_slow (uae_u32 cycles_to_add)
{
#ifdef WITH_X86
#if 0
if (x86_turbo_on) {
execute_other_cpu_single();
}
#endif
#endif
if (!currprefs.cpu_thread) {
if ((pissoff -= cycles_to_add) >= 0)
return;
cycles_to_add = -pissoff;
pissoff = 0;
} else {
pissoff = 0x40000000;
}
while ((nextevent - currcycle) <= cycles_to_add) {
if (is_syncline) {
if (event_check_vsync())
return;
}
cycles_to_add -= nextevent - currcycle;
currcycle = nextevent;
for (int i = 0; i < ev_max; i++) {
if (eventtab[i].active && eventtab[i].evtime == currcycle) {
if (eventtab[i].handler == NULL) {
gui_message(_T("eventtab[%d].handler is null!\n"), i);
eventtab[i].active = 0;
} else {
(*eventtab[i].handler)();
}
}
}
events_schedule ();
}
currcycle += cycles_to_add;
}
void MISC_handler (void)
{
static bool dorecheck;
bool recheck;
int i;
evt mintime;
evt ct = get_cycles ();
static int recursive;
if (recursive) {
dorecheck = true;
return;
}
recursive++;
eventtab[ev_misc].active = 0;
recheck = true;
while (recheck) {
recheck = false;
mintime = ~0L;
for (i = 0; i < ev2_max; i++) {
if (eventtab2[i].active) {
if (eventtab2[i].evtime == ct) {
eventtab2[i].active = false;
event2_count--;
eventtab2[i].handler (eventtab2[i].data);
if (dorecheck || eventtab2[i].active) {
recheck = true;
dorecheck = false;
}
} else {
evt eventtime = eventtab2[i].evtime - ct;
if (eventtime < mintime)
mintime = eventtime;
}
}
}
}
if (mintime != ~0UL) {
eventtab[ev_misc].active = true;
eventtab[ev_misc].oldcycles = ct;
eventtab[ev_misc].evtime = ct + mintime;
events_schedule ();
}
recursive--;
}
void event2_newevent_xx (int no, evt t, uae_u32 data, evfunc2 func)
{
evt et;
static int next = ev2_misc;
et = t + get_cycles ();
if (no < 0) {
no = next;
for (;;) {
if (!eventtab2[no].active) {
event2_count++;
break;
}
if (eventtab2[no].evtime == et && eventtab2[no].handler == func && eventtab2[no].data == data)
break;
no++;
if (no == ev2_max)
no = ev2_misc;
if (no == next) {
write_log (_T("out of event2's!\n"));
// execute most recent event immediately
evt mintime = ~0L;
int minevent = -1;
evt ct = get_cycles();
for (int i = 0; i < ev2_max; i++) {
if (eventtab2[i].active) {
evt eventtime = eventtab2[i].evtime - ct;
if (eventtime < mintime) {
mintime = eventtime;
minevent = i;
}
}
}
if (minevent >= 0) {
eventtab2[minevent].active = false;
eventtab2[minevent].handler(eventtab2[minevent].data);
}
continue;
}
}
next = no;
}
eventtab2[no].active = true;
eventtab2[no].evtime = et;
eventtab2[no].handler = func;
eventtab2[no].data = data;
MISC_handler ();
}
void event2_newevent_x_replace(evt t, uae_u32 data, evfunc2 func)
{
for (int i = 0; i < ev2_max; i++) {
if (eventtab2[i].active && eventtab2[i].handler == func) {
eventtab2[i].active = false;
}
}
if (((int)t) <= 0) {
func(data);
return;
}
event2_newevent_xx(-1, t * CYCLE_UNIT, data, func);
}
int current_hpos (void)
{
int hp = current_hpos_safe();
if (hp < 0 || hp > 256) {
gui_message(_T("hpos = %d!?\n"), hp);
hp = 0;
}
return hp;
}