]> git.sur5r.net Git - cc65/blob - src/sim65/callback.c
Change the makefiles so that CFLAGS that are special for the application are
[cc65] / src / sim65 / callback.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                callback.c                                 */
4 /*                                                                           */
5 /*                              Chip callbacks                               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003      Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 52                                             */
11 /*               D-70794 Filderstadt                                         */
12 /* EMail:        uz@cc65.org                                                 */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 /* common */
37 #include "xmalloc.h"
38
39 /* sim65 */
40 #include "error.h"
41 #include "callback.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   Data                                    */
47 /*****************************************************************************/
48
49
50
51 struct Callback {
52     Callback*           Next;           /* Next entry in list */
53     unsigned            Ticks;          /* Remaining ticks */
54     CallbackFunc        UserFunc;       /* User function */
55     void*               UserData;       /* User data */
56 };
57
58 /* Delta list that keeps existing callbacks */
59 static Callback* List = 0;
60
61
62
63 /*****************************************************************************/
64 /*                    Routines that handle the delta list                    */
65 /*****************************************************************************/
66
67
68
69 static void InsertCallback (Callback* C, unsigned Ticks)
70 /* Insert the callback C into the delta list */
71 {
72     /* Search for the insertion point */
73     Callback*  N;
74     Callback** L = &List;
75     while ((N = *L) != 0) {
76         /* Check if the next callback in the list has a higher wait time */
77         if (N->Ticks > Ticks) {
78             /* Insert before this callback */
79             N->Ticks -= Ticks;
80             break;
81         } else {
82             /* Insert behind this callback */
83             Ticks -= N->Ticks;
84             L = &N->Next;
85         }
86     }
87
88     /* Insert the new task */
89     C->Ticks = Ticks;
90     C->Next  = N;
91     *L       = C;
92 }
93
94
95
96 static void RemoveCallback (Callback* C)
97 /* Remove a callback from the list. If the callback is not in the list, this
98  * is a fatal error.
99  */
100 {
101     Callback* N;
102     Callback** L = &List;
103     while ((N = *L) != 0) {
104         if (N == C) {
105             /* Found, remove it */
106             if (C->Next) {
107                 /* Adjust the counter of the following callback */
108                 C->Next->Ticks += C->Ticks;
109             }
110             *L = C->Next;
111             return;
112         } else {
113             L = &N->Next;
114         }
115     }
116
117     /* Callback was not found */
118     Internal ("RemoveCallback: Callback not found in list!");
119 }
120
121
122
123 /*****************************************************************************/
124 /*                                   Code                                    */
125 /*****************************************************************************/
126
127
128
129 Callback* NewCallback (unsigned Ticks, CallbackFunc Func, void* Data)
130 /* Create a callback for function F to be called in Ticks ticks. */
131 {
132     /* Allocate memory */
133     Callback* C = xmalloc (sizeof (Callback));
134
135     /* Initialize the fields */
136     C->UserFunc = Func;
137     C->UserData = Data;
138
139     /* Insert the callback into the delta list */
140     InsertCallback (C, Ticks);
141
142     /* Return the new callback */
143     return C;
144 }
145
146
147
148 void FreeCallback (Callback* C)
149 /* Delete a callback (remove from the queue) */
150 {
151     /* Remove the callback from the list */
152     RemoveCallback (C);
153
154     /* Delete it */
155     xfree (C);
156 }
157
158
159
160 void HandleCallbacks (unsigned TicksSinceLastCall)
161 /* Handle the callback queue */
162 {
163     while (List) {
164
165         /* Check if this one is due */
166         if (List->Ticks <= TicksSinceLastCall) {
167
168             /* Calclulate the tick offset */
169             int TickOffs = ((int) List->Ticks) - ((int) TicksSinceLastCall);
170
171             /* Retrieve the first callback from the list */
172             Callback* C = List;
173             List        = C->Next;
174
175             /* Call the user function */
176             C->UserFunc (TickOffs, C->UserData);
177
178             /* Delete the callback */
179             xfree (C);
180
181         } else {
182
183             List->Ticks -= TicksSinceLastCall;
184             break;
185
186         }
187     }
188 }
189
190
191