run copyright.sh for 2011.
[deliverable/binutils-gdb.git] / sim / common / hw-events.c
1 /* Hardware event manager.
2 Copyright (C) 1998, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Cygnus Support.
5
6 This file is part of GDB, the GNU debugger.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20
21
22 #include "hw-main.h"
23 #include "hw-base.h"
24
25 #include "sim-events.h"
26
27
28 /* The hw-events object is implemented using sim-events */
29
30 struct hw_event {
31 void *data;
32 struct hw *me;
33 hw_event_callback *callback;
34 sim_event *real;
35 struct hw_event_data *entry;
36 };
37
38 struct hw_event_data {
39 struct hw_event event;
40 struct hw_event_data *next;
41 };
42
43 void
44 create_hw_event_data (struct hw *me)
45 {
46 if (me->events_of_hw != NULL)
47 hw_abort (me, "stray events");
48 /* NOP */
49 }
50
51 void
52 delete_hw_event_data (struct hw *me)
53 {
54 /* Remove the scheduled event. */
55 while (me->events_of_hw)
56 hw_event_queue_deschedule (me, &me->events_of_hw->event);
57 }
58
59
60 /* Pass the H/W event onto the real callback */
61
62 static void
63 bounce_hw_event (SIM_DESC sd,
64 void *data)
65 {
66 /* save the data */
67 struct hw_event_data *entry = (struct hw_event_data *) data;
68 struct hw *me = entry->event.me;
69 void *event_data = entry->event.data;
70 hw_event_callback *callback = entry->event.callback;
71 struct hw_event_data **prev = &me->events_of_hw;
72 while ((*prev) != entry)
73 prev = &(*prev)->next;
74 (*prev) = entry->next;
75 hw_free (me, entry);
76 callback (me, event_data); /* may not return */
77 }
78
79
80
81 /* Map onto the event functions */
82
83 struct hw_event *
84 hw_event_queue_schedule (struct hw *me,
85 signed64 delta_time,
86 hw_event_callback *callback,
87 void *data)
88 {
89 struct hw_event *event;
90 va_list dummy;
91 memset (&dummy, 0, sizeof dummy);
92 event = hw_event_queue_schedule_vtracef (me, delta_time, callback, data,
93 NULL, dummy);
94 return event;
95 }
96
97 struct hw_event *
98 hw_event_queue_schedule_tracef (struct hw *me,
99 signed64 delta_time,
100 hw_event_callback *callback,
101 void *data,
102 const char *fmt,
103 ...)
104 {
105 struct hw_event *event;
106 va_list ap;
107 va_start (ap, fmt);
108 event = hw_event_queue_schedule_vtracef (me, delta_time, callback, data, fmt, ap);
109 va_end (ap);
110 return event;
111 }
112
113 struct hw_event *
114 hw_event_queue_schedule_vtracef (struct hw *me,
115 signed64 delta_time,
116 hw_event_callback *callback,
117 void *data,
118 const char *fmt,
119 va_list ap)
120 {
121 struct hw_event_data *entry = HW_ZALLOC (me, struct hw_event_data);
122 entry->next = me->events_of_hw;
123 me->events_of_hw = entry;
124 /* fill it in */
125 entry->event.entry = entry;
126 entry->event.data = data;
127 entry->event.callback = callback;
128 entry->event.me = me;
129 entry->event.real = sim_events_schedule_vtracef (hw_system (me),
130 delta_time,
131 bounce_hw_event,
132 entry,
133 fmt, ap);
134 return &entry->event;
135 }
136
137
138 void
139 hw_event_queue_deschedule (struct hw *me,
140 struct hw_event *event_to_remove)
141 {
142 /* ZAP the event but only if it is still in the event queue. Note
143 that event_to_remove is only de-referenced after its validity has
144 been confirmed. */
145 struct hw_event_data **prev;
146 for (prev = &me->events_of_hw;
147 (*prev) != NULL;
148 prev = &(*prev)->next)
149 {
150 struct hw_event_data *entry = (*prev);
151 if (&entry->event == event_to_remove)
152 {
153 sim_events_deschedule (hw_system (me),
154 entry->event.real);
155 (*prev) = entry->next;
156 hw_free (me, entry);
157 return;
158 }
159 }
160 }
161
162
163 signed64
164 hw_event_queue_time (struct hw *me)
165 {
166 return sim_events_time (hw_system (me));
167 }
168
169 /* Returns the time that remains before the event is raised. */
170 signed64
171 hw_event_remain_time (struct hw *me, struct hw_event *event)
172 {
173 signed64 t;
174
175 t = sim_events_remain_time (hw_system (me), event->real);
176 return t;
177 }
178
179 /* Only worry about this compling on ANSI systems.
180 Build with `make test-hw-events' in sim/<cpu> directory*/
181
182 #if defined (MAIN)
183 #include "sim-main.h"
184 #include <string.h>
185 #include <stdio.h>
186
187 static void
188 test_handler (struct hw *me,
189 void *data)
190 {
191 int *n = data;
192 if (*n != hw_event_queue_time (me))
193 abort ();
194 *n = -(*n);
195 }
196
197 int
198 main (int argc,
199 char **argv)
200 {
201 host_callback *cb = ZALLOC (host_callback);
202 struct sim_state *sd = sim_state_alloc (0, cb);
203 struct hw *me = ZALLOC (struct hw);
204 sim_pre_argv_init (sd, "test-hw-events");
205 sim_post_argv_init (sd);
206 me->system_of_hw = sd;
207
208 printf ("Create hw-event-data\n");
209 {
210 create_hw_alloc_data (me);
211 create_hw_event_data (me);
212 delete_hw_event_data (me);
213 delete_hw_alloc_data (me);
214 }
215
216 printf ("Create hw-events\n");
217 {
218 struct hw_event *a;
219 struct hw_event *b;
220 struct hw_event *c;
221 struct hw_event *d;
222 create_hw_alloc_data (me);
223 create_hw_event_data (me);
224 a = hw_event_queue_schedule (me, 0, NULL, NULL);
225 b = hw_event_queue_schedule (me, 1, NULL, NULL);
226 c = hw_event_queue_schedule (me, 2, NULL, NULL);
227 d = hw_event_queue_schedule (me, 1, NULL, NULL);
228 hw_event_queue_deschedule (me, c);
229 hw_event_queue_deschedule (me, b);
230 hw_event_queue_deschedule (me, a);
231 hw_event_queue_deschedule (me, d);
232 c = HW_ZALLOC (me, struct hw_event);
233 hw_event_queue_deschedule (me, b); /* OOPS! */
234 hw_free (me, c);
235 delete_hw_event_data (me);
236 delete_hw_alloc_data (me);
237 }
238
239 printf ("Schedule hw-events\n");
240 {
241 struct hw_event **e;
242 int *n;
243 int i;
244 int nr = 4;
245 e = HW_NZALLOC (me, struct hw_event *, nr);
246 n = HW_NZALLOC (me, int, nr);
247 create_hw_alloc_data (me);
248 create_hw_event_data (me);
249 for (i = 0; i < nr; i++)
250 {
251 n[i] = i;
252 e[i] = hw_event_queue_schedule (me, i, test_handler, &n[i]);
253 }
254 sim_events_preprocess (sd, 1, 1);
255 for (i = 0; i < nr; i++)
256 {
257 if (sim_events_tick (sd))
258 sim_events_process (sd);
259 }
260 for (i = 0; i < nr; i++)
261 {
262 if (n[i] != -i)
263 abort ();
264 hw_event_queue_deschedule (me, e[i]);
265 }
266 hw_free (me, n);
267 hw_free (me, e);
268 delete_hw_event_data (me);
269 delete_hw_alloc_data (me);
270 }
271
272 return 0;
273 }
274 #endif
This page took 0.046275 seconds and 4 git commands to generate.