forked from tonioni/WinUAE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.cpp
486 lines (443 loc) · 9.41 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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* 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"
#include "cia.h"
static const int pissoff_nojit_value = 256 * CYCLE_UNIT;
evt_t event_cycles, nextevent, currcycle;
int is_syncline;
frame_time_t is_syncline_end;
int cycles_to_next_event;
int max_cycles_to_next_event;
int cycles_to_hsync_event;
evt_t start_cycles;
bool event_wait;
frame_time_t vsyncmintime, vsyncmintimepre;
frame_time_t vsyncmaxtime, vsyncwaittime;
frame_time_t vsynctimebase;
static void events_fast(void)
{
cycles_do_special();
}
void events_reset_syncline(void)
{
is_syncline = 0;
events_fast();
}
void events_schedule(void)
{
int i;
evt_t mintime = EVT_MAX;
for (i = 0; i < ev_max; i++) {
if (eventtab[i].active) {
evt_t eventtime = eventtab[i].evtime - currcycle;
if (eventtime < mintime)
mintime = eventtime;
}
}
if (mintime < EVT_MAX) {
nextevent = currcycle + mintime;
} else {
nextevent = EVT_MAX;
}
}
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) {
frame_time_t rpt = read_processor_time();
frame_time_t 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) {
frame_time_t rpt = read_processor_time();
frame_time_t v = rpt - vsyncmintime;
frame_time_t 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(int 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 -= (int)(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;
}
static ev2 *last_event2;
static ev2 dummy_event;
void MISC_handler(void)
{
static bool dorecheck;
bool recheck;
int i;
evt_t mintime;
evt_t ct = get_cycles();
static int recursive;
if (recursive) {
dorecheck = true;
return;
}
recursive++;
eventtab[ev_misc].active = 0;
recheck = true;
while (recheck) {
recheck = false;
mintime = EVT_MAX;
for (i = 0; i < ev2_max; i++) {
ev2 *e = &eventtab2[i];
if (e->active) {
if (e->evtime == ct) {
e->active = false;
e->handler(e->data);
ev2 *e2 = e->next;
if (e2) {
e->next = NULL;
if (e2->active && e2->evtime == e->evtime + 1) {
e2->active = false;
e2->handler(e2->data);
}
}
if (dorecheck || e->active) {
recheck = true;
dorecheck = false;
}
} else {
evt_t eventtime = e->evtime - ct;
if (eventtime < mintime)
mintime = eventtime;
}
}
}
}
if (mintime < EVT_MAX) {
ev *e = &eventtab[ev_misc];
e->active = true;
e->oldcycles = ct;
e->evtime = ct + mintime;
events_schedule();
}
recursive--;
}
void event2_newevent_xx_ce(evt_t t, uae_u32 data, evfunc2 func)
{
if (!currprefs.cpu_memory_cycle_exact) {
func(data);
return;
}
event2_newevent_xx(-1, t, data, func);
}
void event2_newevent_xx(int no, evt_t t, uae_u32 data, evfunc2 func)
{
evt_t et;
static int next = ev2_misc;
et = t + get_cycles();
if (no < 0) {
no = next;
for (;;) {
if (!eventtab2[no].active) {
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_t mintime = EVT_MAX;
int minevent = -1;
evt_t ct = get_cycles();
for (int i = 0; i < ev2_max; i++) {
if (eventtab2[i].active) {
evt_t 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;
}
ev2 *e = &eventtab2[no];
// if previous event has same expiry time, make sure it gets executed first.
if (last_event2->active && last_event2 != e && et == last_event2->evtime) {
last_event2->next = e;
et++;
}
e->active = true;
e->evtime = et;
e->handler = func;
e->data = data;
last_event2 = e;
MISC_handler();
}
void event2_newevent_x_replace_exists(evt_t 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 (t <= 0) {
func(data);
return;
}
event2_newevent_xx(-1, t * CYCLE_UNIT, data, func);
return;
}
}
}
void event2_newevent_x_remove(evfunc2 func)
{
for (int i = 0; i < ev2_max; i++) {
if (eventtab2[i].active && eventtab2[i].handler == func) {
eventtab2[i].active = false;
}
}
}
void event2_newevent_x_replace(evt_t t, uae_u32 data, evfunc2 func)
{
event2_newevent_x_remove(func);
if (t <= 0) {
func(data);
return;
}
event2_newevent_xx(-1, t * CYCLE_UNIT, data, func);
}
void event_init(void)
{
last_event2 = &dummy_event;
}
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;
}
void clear_events(void)
{
nextevent = EVT_MAX;
for (int i = 0; i < ev_max; i++) {
eventtab[i].active = 0;
eventtab[i].oldcycles = get_cycles();
}
for (int i = 0; i < ev2_max; i++) {
eventtab2[i].active = 0;
}
}