forked from r-type/xmil-libretro
-
Notifications
You must be signed in to change notification settings - Fork 12
/
nevent.c
219 lines (181 loc) · 3.93 KB
/
nevent.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
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
/**
* @file nevent.c
* @brief Implementation of the event
*/
#include "compiler.h"
#include "nevent.h"
#include "z80core.h"
#include "pccore.h"
_NEVENT nevent;
void nevent_allreset(void) {
UINT i;
ZeroMemory(&nevent, sizeof(nevent));
nevent.first = NEVENTITEM_TERM;
for (i=0; i<NEVENT_MAXEVENTS; i++) {
nevent.item[i].next = NEVENTITEM_NONE;
}
}
void nevent_progress(void) {
NEVENTITEM item;
NEVENTITEM itemnext;
NEVENTITEM *witem;
NEVENTITEM waititem[NEVENT_MAXEVENTS];
SINT32 nextbase;
NEVENTITEM *wi;
CPU_CLOCK += CPU_BASECLOCK;
item = nevent.first;
witem = waititem;
while(item != NEVENTITEM_TERM) {
item->clock -= CPU_BASECLOCK;
if (item->clock > 0) {
break;
}
*witem++ = item;
itemnext = item->next;
item->next = NEVENTITEM_NONE;
item = itemnext;
}
nevent.first = item;
if (item != NEVENTITEM_TERM) {
nextbase = item->clock;
item = item->next;
while(item != NEVENTITEM_TERM) {
item->clock -= CPU_BASECLOCK;
item = item->next;
}
}
else {
nextbase = NEVENT_MAXCLOCK;
}
CPU_BASECLOCK = nextbase;
CPU_REMCLOCK += nextbase;
/* TRACEOUT(("nextbase = %d (%d)", nextbase, CPU_REMCLOCK)); */
/* Cx“g”¶ */
wi = waititem;
while(wi < witem) {
item = *wi++;
if (item->proc != NULL) {
item->proc((NEVENTID)(item - nevent.item));
}
}
}
void nevent_reset(NEVENTID id) {
NEVENTITEM item;
NEVENTITEM *back;
NEVENTITEM cur;
item = nevent.item + id;
if (item->next != NEVENTITEM_NONE) {
back = &nevent.first;
cur = *back;
while(cur != item) {
__ASSERT(cur != NEVENTITEM_NONE);
__ASSERT(cur != NEVENTITEM_TERM);
back = &cur->next;
cur = *back;
}
*back = cur->next;
cur->next = NEVENTITEM_NONE;
}
}
void nevent_set(NEVENTID id, SINT32 eventclock, NEVENTCB proc, NEVENTPOSITION absolute) {
SINT32 clock;
NEVENTITEM item;
NEVENTITEM *back;
NEVENTITEM n;
NEVENTITEM p;
/* TRACEOUT(("event %d - %xclocks", id, eventclock)); */
clock = CPU_BASECLOCK - CPU_REMCLOCK;
item = nevent.item + id;
item->baseclock = eventclock;
item->proc = proc;
if (absolute) {
item->clock = eventclock + clock;
}
else {
item->clock += eventclock;
}
if (item->next != NEVENTITEM_NONE) {
back = &nevent.first;
n = *back;
while(n != item) {
__ASSERT(n != NEVENTITEM_NONE);
__ASSERT(n != NEVENTITEM_TERM);
back = &n->next;
n = *back;
}
*back = n->next;
n->next = NEVENTITEM_NONE;
}
n = nevent.first;
if ((n == NEVENTITEM_TERM) || (item->clock < n->clock)) {
item->next = n;
nevent.first = item;
clock = CPU_BASECLOCK;
if (item->clock > 0) {
clock -= item->clock;
}
CPU_BASECLOCK -= clock;
CPU_REMCLOCK -= clock;
/* TRACEOUT(("reset nextbase -%d (%d)", clock, CPU_REMCLOCK)); */
}
else {
p = n->next;
while((p != NEVENTITEM_TERM) && (item->clock >= p->clock)) {
n = p;
p = n->next;
}
item->next = p;
n->next = item;
}
}
void nevent_repeat(NEVENTID id) {
NEVENTITEM item;
item = nevent.item + id;
nevent_set(id, item->baseclock, item->proc, NEVENT_RELATIVE);
}
void nevent_setbyms(NEVENTID id, SINT32 ms, NEVENTCB proc, NEVENTPOSITION absolute) {
SINT32 clock;
#if defined(FIX_Z80A)
clock = (2000000 * 2 / 1000) * ms;
#else
clock = (pccore.realclock / 1000) * ms;
#endif
nevent_set(id, clock, proc, absolute);
}
BOOL nevent_iswork(NEVENTID id) {
NEVENTITEM item;
item = nevent.item + id;
if (item->next != NEVENTITEM_NONE) {
return(TRUE);
}
else {
return(FALSE);
}
}
SINT32 nevent_getwork(NEVENTID id) {
NEVENTITEM item;
item = nevent.item + id;
if (item->next != NEVENTITEM_NONE) {
return(item->baseclock -
(item->clock - (CPU_BASECLOCK - CPU_REMCLOCK)));
}
else {
return(-1);
}
}
SINT32 nevent_getremain(NEVENTID id) {
NEVENTITEM item;
item = nevent.item + id;
if (item->next != NEVENTITEM_NONE) {
return(item->clock - (CPU_BASECLOCK - CPU_REMCLOCK));
}
else {
return(-1);
}
}
void nevent_forceexit(void) {
if (CPU_REMCLOCK > 0) {
CPU_BASECLOCK -= CPU_REMCLOCK;
CPU_REMCLOCK = 0;
}
}