1 /* Hardware event manager.
2 Copyright (C) 1998-2024 Free Software Foundation, Inc.
3 Contributed by Cygnus Support.
5 This file is part of GDB, the GNU debugger.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 /* This must come before any other includes. */
26 #include "sim/callback.h"
31 #include "sim-events.h"
33 /* The hw-events object is implemented using sim-events */
39 hw_event_callback
*callback
;
41 struct hw_event_data
*entry
;
46 struct hw_event event
;
47 struct hw_event_data
*next
;
51 create_hw_event_data (struct hw
*me
)
53 if (me
->events_of_hw
!= NULL
)
54 hw_abort (me
, "stray events");
59 delete_hw_event_data (struct hw
*me
)
61 /* Remove the scheduled event. */
62 while (me
->events_of_hw
)
63 hw_event_queue_deschedule (me
, &me
->events_of_hw
->event
);
67 /* Pass the H/W event onto the real callback */
70 bounce_hw_event (SIM_DESC sd
,
74 struct hw_event_data
*entry
= (struct hw_event_data
*) data
;
75 struct hw
*me
= entry
->event
.me
;
76 void *event_data
= entry
->event
.data
;
77 hw_event_callback
*callback
= entry
->event
.callback
;
78 struct hw_event_data
**prev
= &me
->events_of_hw
;
79 while ((*prev
) != entry
)
80 prev
= &(*prev
)->next
;
81 (*prev
) = entry
->next
;
83 callback (me
, event_data
); /* may not return */
88 /* Map onto the event functions */
91 hw_event_queue_schedule (struct hw
*me
,
93 hw_event_callback
*callback
,
96 return hw_event_queue_schedule_tracef (me
, delta_time
, callback
, data
, NULL
);
100 hw_event_queue_schedule_tracef (struct hw
*me
,
102 hw_event_callback
*callback
,
107 struct hw_event
*event
;
110 event
= hw_event_queue_schedule_vtracef (me
, delta_time
, callback
, data
, fmt
, ap
);
116 hw_event_queue_schedule_vtracef (struct hw
*me
,
118 hw_event_callback
*callback
,
123 struct hw_event_data
*entry
= HW_ZALLOC (me
, struct hw_event_data
);
124 entry
->next
= me
->events_of_hw
;
125 me
->events_of_hw
= entry
;
127 entry
->event
.entry
= entry
;
128 entry
->event
.data
= data
;
129 entry
->event
.callback
= callback
;
130 entry
->event
.me
= me
;
131 entry
->event
.real
= sim_events_schedule_vtracef (hw_system (me
),
136 return &entry
->event
;
141 hw_event_queue_deschedule (struct hw
*me
,
142 struct hw_event
*event_to_remove
)
144 /* ZAP the event but only if it is still in the event queue. Note
145 that event_to_remove is only de-referenced after its validity has
147 struct hw_event_data
**prev
;
148 for (prev
= &me
->events_of_hw
;
150 prev
= &(*prev
)->next
)
152 struct hw_event_data
*entry
= (*prev
);
153 if (&entry
->event
== event_to_remove
)
155 sim_events_deschedule (hw_system (me
),
157 (*prev
) = entry
->next
;
166 hw_event_queue_time (struct hw
*me
)
168 return sim_events_time (hw_system (me
));
171 /* Returns the time that remains before the event is raised. */
173 hw_event_remain_time (struct hw
*me
, struct hw_event
*event
)
177 t
= sim_events_remain_time (hw_system (me
), event
->real
);
181 /* Only worry about this compling on ANSI systems.
182 Build with `make test-hw-events' in sim/<cpu> directory*/
189 #include "sim-main.h"
192 test_handler (struct hw
*me
,
196 if (*n
!= hw_event_queue_time (me
))
205 host_callback
*cb
= ZALLOC (host_callback
);
206 struct sim_state
*sd
= sim_state_alloc (0, cb
);
207 struct hw
*me
= ZALLOC (struct hw
);
208 sim_pre_argv_init (sd
, "test-hw-events");
209 sim_post_argv_init (sd
);
210 me
->system_of_hw
= sd
;
212 printf ("Create hw-event-data\n");
214 create_hw_alloc_data (me
);
215 create_hw_event_data (me
);
216 delete_hw_event_data (me
);
217 delete_hw_alloc_data (me
);
220 printf ("Create hw-events\n");
226 create_hw_alloc_data (me
);
227 create_hw_event_data (me
);
228 a
= hw_event_queue_schedule (me
, 0, NULL
, NULL
);
229 b
= hw_event_queue_schedule (me
, 1, NULL
, NULL
);
230 c
= hw_event_queue_schedule (me
, 2, NULL
, NULL
);
231 d
= hw_event_queue_schedule (me
, 1, NULL
, NULL
);
232 hw_event_queue_deschedule (me
, c
);
233 hw_event_queue_deschedule (me
, b
);
234 hw_event_queue_deschedule (me
, a
);
235 hw_event_queue_deschedule (me
, d
);
236 c
= HW_ZALLOC (me
, struct hw_event
);
237 hw_event_queue_deschedule (me
, b
); /* OOPS! */
239 delete_hw_event_data (me
);
240 delete_hw_alloc_data (me
);
243 printf ("Schedule hw-events\n");
249 e
= HW_NZALLOC (me
, struct hw_event
*, nr
);
250 n
= HW_NZALLOC (me
, int, nr
);
251 create_hw_alloc_data (me
);
252 create_hw_event_data (me
);
253 for (i
= 0; i
< nr
; i
++)
256 e
[i
] = hw_event_queue_schedule (me
, i
, test_handler
, &n
[i
]);
258 sim_events_preprocess (sd
, 1, 1);
259 for (i
= 0; i
< nr
; i
++)
261 if (sim_events_tick (sd
))
262 sim_events_process (sd
);
264 for (i
= 0; i
< nr
; i
++)
268 hw_event_queue_deschedule (me
, e
[i
]);
272 delete_hw_event_data (me
);
273 delete_hw_alloc_data (me
);